Automatic date update in version.in
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
3666a048 3 Copyright (C) 1986-2021 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"
0f8e2034 70#include "cli/cli-decode.h"
d3ce09f5 71
1042e4c0 72/* readline include files */
073bbbb0 73#include "readline/tilde.h"
1042e4c0
SS
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
325fac50 80#include <algorithm>
5ed8105e 81#include "progspace-and-thread.h"
268a13a5
TT
82#include "gdbsupport/array-view.h"
83#include "gdbsupport/gdb_optional.h"
104c1213 84
4a64f543 85/* Prototypes for local functions. */
c906108c 86
896b6bda 87static void map_breakpoint_numbers (const char *,
48649e1b 88 gdb::function_view<void (breakpoint *)>);
c906108c 89
348d480f
PA
90static void breakpoint_re_set_default (struct breakpoint *);
91
f00aae0f 92static void
626d2320 93 create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
94 struct linespec_result *canonical,
95 enum bptype type_wanted);
983af33b
SDJ
96
97static void create_breakpoints_sal_default (struct gdbarch *,
98 struct linespec_result *,
e1e01040
PA
99 gdb::unique_xmalloc_ptr<char>,
100 gdb::unique_xmalloc_ptr<char>,
101 enum bptype,
983af33b
SDJ
102 enum bpdisp, int, int,
103 int,
104 const struct breakpoint_ops *,
44f238bb 105 int, int, int, unsigned);
983af33b 106
6c5b2ebe 107static std::vector<symtab_and_line> decode_location_default
626d2320 108 (struct breakpoint *b, struct event_location *location,
6c5b2ebe 109 struct program_space *search_pspace);
983af33b 110
a6535de1
TT
111static int can_use_hardware_watchpoint
112 (const std::vector<value_ref_ptr> &vals);
c906108c 113
a14ed312 114static void mention (struct breakpoint *);
c906108c 115
348d480f
PA
116static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
117 enum bptype,
c0a91b2b 118 const struct breakpoint_ops *);
3742cc8b
YQ
119static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
120 const struct symtab_and_line *);
121
4a64f543
MS
122/* This function is used in gdbtk sources and thus can not be made
123 static. */
63c252f8 124struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 125 struct symtab_and_line,
c0a91b2b
TT
126 enum bptype,
127 const struct breakpoint_ops *);
c906108c 128
06edf0c0
PA
129static struct breakpoint *
130 momentary_breakpoint_from_master (struct breakpoint *orig,
131 enum bptype type,
a1aa2221
LM
132 const struct breakpoint_ops *ops,
133 int loc_enabled);
06edf0c0 134
76897487
KB
135static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
136
a6d9a66e
UW
137static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
138 CORE_ADDR bpaddr,
dda83cd7 139 enum bptype bptype);
76897487 140
6c95b8df
PA
141static void describe_other_breakpoints (struct gdbarch *,
142 struct program_space *, CORE_ADDR,
5af949e3 143 struct obj_section *, int);
c906108c 144
85d721b8
PA
145static int watchpoint_locations_match (struct bp_location *loc1,
146 struct bp_location *loc2);
147
7f32a4d5
PA
148static int breakpoint_locations_match (struct bp_location *loc1,
149 struct bp_location *loc2,
150 bool sw_hw_bps_match = false);
151
f1310107 152static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 153 const struct address_space *aspace,
f1310107
TJB
154 CORE_ADDR addr);
155
d35ae833 156static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 157 const address_space *,
d35ae833
PA
158 CORE_ADDR, int);
159
834c0d03 160static int remove_breakpoint (struct bp_location *);
b2b6a7da 161static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 162
e514a9d6 163static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 164
a14ed312 165static int hw_breakpoint_used_count (void);
c906108c 166
a1398e0c
PA
167static int hw_watchpoint_use_count (struct breakpoint *);
168
169static int hw_watchpoint_used_count_others (struct breakpoint *except,
170 enum bptype type,
171 int *other_type_used);
c906108c 172
816338b5
SS
173static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
174 int count);
c906108c 175
f431efe5 176static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 177
39d61571 178static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 179
44702360
PA
180/* update_global_location_list's modes of operation wrt to whether to
181 insert locations now. */
182enum ugll_insert_mode
183{
184 /* Don't insert any breakpoint locations into the inferior, only
185 remove already-inserted locations that no longer should be
186 inserted. Functions that delete a breakpoint or breakpoints
187 should specify this mode, so that deleting a breakpoint doesn't
188 have the side effect of inserting the locations of other
189 breakpoints that are marked not-inserted, but should_be_inserted
190 returns true on them.
191
192 This behavior is useful is situations close to tear-down -- e.g.,
193 after an exec, while the target still has execution, but
194 breakpoint shadows of the previous executable image should *NOT*
195 be restored to the new image; or before detaching, where the
196 target still has execution and wants to delete breakpoints from
197 GDB's lists, and all breakpoints had already been removed from
198 the inferior. */
199 UGLL_DONT_INSERT,
200
a25a5a45
PA
201 /* May insert breakpoints iff breakpoints_should_be_inserted_now
202 claims breakpoints should be inserted now. */
04086b45
PA
203 UGLL_MAY_INSERT,
204
a25a5a45
PA
205 /* Insert locations now, irrespective of
206 breakpoints_should_be_inserted_now. E.g., say all threads are
207 stopped right now, and the user did "continue". We need to
208 insert breakpoints _before_ resuming the target, but
209 UGLL_MAY_INSERT wouldn't insert them, because
210 breakpoints_should_be_inserted_now returns false at that point,
211 as no thread is running yet. */
04086b45 212 UGLL_INSERT
44702360
PA
213};
214
215static void update_global_location_list (enum ugll_insert_mode);
a5606eee 216
44702360 217static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 218
74960c60 219static void insert_breakpoint_locations (void);
a5606eee 220
0b39b52e 221static void trace_pass_command (const char *, int);
1042e4c0 222
558a9d82
YQ
223static void set_tracepoint_count (int num);
224
f2478a7e 225static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 226
983af33b
SDJ
227/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
228 otherwise. */
229
230static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 231
2060206e
PA
232/* The breakpoint_ops structure to be inherited by all breakpoint_ops
233 that are implemented on top of software or hardware breakpoints
234 (user breakpoints, internal and momentary breakpoints, etc.). */
235static struct breakpoint_ops bkpt_base_breakpoint_ops;
236
237/* Internal breakpoints class type. */
06edf0c0 238static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
239
240/* Momentary breakpoints class type. */
06edf0c0
PA
241static struct breakpoint_ops momentary_breakpoint_ops;
242
2060206e
PA
243/* The breakpoint_ops structure to be used in regular user created
244 breakpoints. */
245struct breakpoint_ops bkpt_breakpoint_ops;
246
55aa24fb
SDJ
247/* Breakpoints set on probes. */
248static struct breakpoint_ops bkpt_probe_breakpoint_ops;
249
bac7c5cf
GB
250/* Tracepoints set on probes. */
251static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
252
e7e0cddf 253/* Dynamic printf class type. */
c5867ab6 254struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 255
d3ce09f5
SS
256/* The style in which to perform a dynamic printf. This is a user
257 option because different output options have different tradeoffs;
258 if GDB does the printing, there is better error handling if there
259 is a problem with any of the arguments, but using an inferior
260 function lets you have special-purpose printers and sending of
261 output to the same place as compiled-in print functions. */
262
263static const char dprintf_style_gdb[] = "gdb";
264static const char dprintf_style_call[] = "call";
265static const char dprintf_style_agent[] = "agent";
266static const char *const dprintf_style_enums[] = {
267 dprintf_style_gdb,
268 dprintf_style_call,
269 dprintf_style_agent,
270 NULL
271};
272static const char *dprintf_style = dprintf_style_gdb;
273
274/* The function to use for dynamic printf if the preferred style is to
275 call into the inferior. The value is simply a string that is
276 copied into the command, so it can be anything that GDB can
277 evaluate to a callable address, not necessarily a function name. */
278
bde6261a 279static char *dprintf_function;
d3ce09f5
SS
280
281/* The channel to use for dynamic printf if the preferred style is to
282 call into the inferior; if a nonempty string, it will be passed to
283 the call as the first argument, with the format string as the
284 second. As with the dprintf function, this can be anything that
285 GDB knows how to evaluate, so in addition to common choices like
286 "stderr", this could be an app-specific expression like
287 "mystreams[curlogger]". */
288
bde6261a 289static char *dprintf_channel;
d3ce09f5
SS
290
291/* True if dprintf commands should continue to operate even if GDB
292 has disconnected. */
491144b5 293static bool disconnected_dprintf = true;
d3ce09f5 294
5cea2a26
PA
295struct command_line *
296breakpoint_commands (struct breakpoint *b)
297{
d1b0a7bf 298 return b->commands ? b->commands.get () : NULL;
5cea2a26 299}
3daf8fe5 300
f3b1572e
PA
301/* Flag indicating that a command has proceeded the inferior past the
302 current breakpoint. */
303
491144b5 304static bool breakpoint_proceeded;
f3b1572e 305
956a9fb9 306const char *
2cec12e5
AR
307bpdisp_text (enum bpdisp disp)
308{
4a64f543
MS
309 /* NOTE: the following values are a part of MI protocol and
310 represent values of 'disp' field returned when inferior stops at
311 a breakpoint. */
bc043ef3 312 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 313
2cec12e5
AR
314 return bpdisps[(int) disp];
315}
c906108c 316
4a64f543 317/* Prototypes for exported functions. */
c906108c 318/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 319 if such is available. */
c906108c
SS
320static int can_use_hw_watchpoints;
321
920d2a44
AC
322static void
323show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
324 struct cmd_list_element *c,
325 const char *value)
326{
3e43a32a
MS
327 fprintf_filtered (file,
328 _("Debugger's willingness to use "
329 "watchpoint hardware is %s.\n"),
920d2a44
AC
330 value);
331}
332
fa8d40ab
JJ
333/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
334 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 335 for unrecognized breakpoint locations.
fa8d40ab
JJ
336 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
337static enum auto_boolean pending_break_support;
920d2a44
AC
338static void
339show_pending_break_support (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342{
3e43a32a
MS
343 fprintf_filtered (file,
344 _("Debugger's behavior regarding "
345 "pending breakpoints is %s.\n"),
920d2a44
AC
346 value);
347}
fa8d40ab 348
491144b5 349/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 350 set with "break" but falling in read-only memory.
491144b5 351 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 352 use hardware breakpoints. */
491144b5 353static bool automatic_hardware_breakpoints;
765dc015
VP
354static void
355show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358{
3e43a32a
MS
359 fprintf_filtered (file,
360 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
361 value);
362}
363
a25a5a45
PA
364/* If on, GDB keeps breakpoints inserted even if the inferior is
365 stopped, and immediately inserts any new breakpoints as soon as
366 they're created. If off (default), GDB keeps breakpoints off of
367 the target as long as possible. That is, it delays inserting
368 breakpoints until the next resume, and removes them again when the
369 target fully stops. This is a bit safer in case GDB crashes while
370 processing user input. */
491144b5 371static bool always_inserted_mode = false;
72d0e2c5 372
33e5cbd6 373static void
74960c60 374show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 375 struct cmd_list_element *c, const char *value)
74960c60 376{
a25a5a45
PA
377 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
378 value);
74960c60
VP
379}
380
b57bacec
PA
381/* See breakpoint.h. */
382
33e5cbd6 383int
a25a5a45 384breakpoints_should_be_inserted_now (void)
33e5cbd6 385{
a25a5a45
PA
386 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
387 {
388 /* If breakpoints are global, they should be inserted even if no
389 thread under gdb's control is running, or even if there are
390 no threads under GDB's control yet. */
391 return 1;
392 }
5b6d1e4f 393 else
a25a5a45 394 {
a25a5a45
PA
395 if (always_inserted_mode)
396 {
397 /* The user wants breakpoints inserted even if all threads
398 are stopped. */
399 return 1;
400 }
401
5b6d1e4f
PA
402 for (inferior *inf : all_inferiors ())
403 if (inf->has_execution ()
404 && threads_are_executing (inf->process_target ()))
405 return 1;
372316f1
PA
406
407 /* Don't remove breakpoints yet if, even though all threads are
408 stopped, we still have events to process. */
08036331 409 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
410 if (tp->resumed
411 && tp->suspend.waitstatus_pending_p)
412 return 1;
a25a5a45
PA
413 }
414 return 0;
33e5cbd6 415}
765dc015 416
b775012e
LM
417static const char condition_evaluation_both[] = "host or target";
418
419/* Modes for breakpoint condition evaluation. */
420static const char condition_evaluation_auto[] = "auto";
421static const char condition_evaluation_host[] = "host";
422static const char condition_evaluation_target[] = "target";
423static const char *const condition_evaluation_enums[] = {
424 condition_evaluation_auto,
425 condition_evaluation_host,
426 condition_evaluation_target,
427 NULL
428};
429
430/* Global that holds the current mode for breakpoint condition evaluation. */
431static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
432
433/* Global that we use to display information to the user (gets its value from
434 condition_evaluation_mode_1. */
435static const char *condition_evaluation_mode = condition_evaluation_auto;
436
437/* Translate a condition evaluation mode MODE into either "host"
438 or "target". This is used mostly to translate from "auto" to the
439 real setting that is being used. It returns the translated
440 evaluation mode. */
441
442static const char *
443translate_condition_evaluation_mode (const char *mode)
444{
445 if (mode == condition_evaluation_auto)
446 {
447 if (target_supports_evaluation_of_breakpoint_conditions ())
448 return condition_evaluation_target;
449 else
450 return condition_evaluation_host;
451 }
452 else
453 return mode;
454}
455
456/* Discovers what condition_evaluation_auto translates to. */
457
458static const char *
459breakpoint_condition_evaluation_mode (void)
460{
461 return translate_condition_evaluation_mode (condition_evaluation_mode);
462}
463
464/* Return true if GDB should evaluate breakpoint conditions or false
465 otherwise. */
466
467static int
468gdb_evaluates_breakpoint_condition_p (void)
469{
470 const char *mode = breakpoint_condition_evaluation_mode ();
471
472 return (mode == condition_evaluation_host);
473}
474
c906108c
SS
475/* Are we executing breakpoint commands? */
476static int executing_breakpoint_commands;
477
c02f5703
MS
478/* Are overlay event breakpoints enabled? */
479static int overlay_events_enabled;
480
e09342b5 481/* See description in breakpoint.h. */
491144b5 482bool target_exact_watchpoints = false;
e09342b5 483
c906108c 484/* Walk the following statement or block through all breakpoints.
e5dd4106 485 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 486 current breakpoint. */
c906108c 487
5c44784c
JM
488#define ALL_BREAKPOINTS_SAFE(B,TMP) \
489 for (B = breakpoint_chain; \
490 B ? (TMP=B->next, 1): 0; \
491 B = TMP)
c906108c 492
7cc221ef 493/* Chains of all breakpoints defined. */
c906108c 494
81e6b8eb 495static struct breakpoint *breakpoint_chain;
c906108c 496
240edef6 497/* See breakpoint.h. */
43892fdf 498
240edef6 499breakpoint_range
43892fdf
SM
500all_breakpoints ()
501{
502 return breakpoint_range (breakpoint_chain);
503}
504
240edef6 505/* See breakpoint.h. */
1428b37a 506
240edef6 507breakpoint_safe_range
1428b37a
SM
508all_breakpoints_safe ()
509{
510 return breakpoint_safe_range (all_breakpoints ());
511}
512
f6d17b2b
SM
513/* See breakpoint.h. */
514
515tracepoint_range
516all_tracepoints ()
517{
518 return tracepoint_range (breakpoint_chain);
519}
520
39ef2f62 521/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 522
5d51cd5d 523static std::vector<bp_location *> bp_locations;
876fa593 524
055c879f
SM
525/* See breakpoint.h. */
526
527const std::vector<bp_location *> &
48d7020b
SM
528all_bp_locations ()
529{
530 return bp_locations;
531}
532
e0d9a270
SM
533/* Range to iterate over breakpoint locations at a given address. */
534
535struct bp_locations_at_addr_range
536{
537 using iterator = std::vector<bp_location *>::iterator;
538
539 bp_locations_at_addr_range (CORE_ADDR addr)
540 {
541 struct compare
542 {
543 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
544 { return loc->address < addr_; }
545
546 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
547 { return addr_ < loc->address; }
548 };
549
550 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
551 addr, compare ());
552
553 m_begin = it_pair.first;
554 m_end = it_pair.second;
555 }
556
557 iterator begin () const
558 { return m_begin; }
559
560 iterator end () const
561 { return m_end; }
562
563private:
564 iterator m_begin;
565 iterator m_end;
566};
567
568/* Return a range to iterate over all breakpoint locations exactly at address
569 ADDR.
570
571 If it's needed to iterate multiple times on the same range, it's possible
572 to save the range in a local variable and use it multiple times:
573
574 auto range = all_bp_locations_at_addr (addr);
575
576 for (bp_location *loc : range)
577 // use loc
578
579 for (bp_location *loc : range)
580 // use loc
581
582 This saves a bit of time, as it avoids re-doing the binary searches to find
583 the range's boundaries. Just remember not to change the bp_locations vector
584 in the mean time, as it could make the range's iterators stale. */
585
586static bp_locations_at_addr_range
587all_bp_locations_at_addr (CORE_ADDR addr)
588{
589 return bp_locations_at_addr_range (addr);
590}
591
4a64f543 592/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 593 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 594 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 595 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 596 an address you need to read. */
876fa593 597
f5336ca5 598static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 599
4a64f543
MS
600/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
601 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
602 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
603 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 604 scan for shadow bytes for an address you need to read. */
876fa593 605
f5336ca5 606static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 607
4a64f543 608/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
609 from the bp_locations array, but for which a hit may still be
610 reported by a target. */
1123588c 611static std::vector<bp_location *> moribund_locations;
20874c92 612
c906108c
SS
613/* Number of last breakpoint made. */
614
95a42b64
TT
615static int breakpoint_count;
616
86b17b60
PA
617/* The value of `breakpoint_count' before the last command that
618 created breakpoints. If the last (break-like) command created more
619 than one breakpoint, then the difference between BREAKPOINT_COUNT
620 and PREV_BREAKPOINT_COUNT is more than one. */
621static int prev_breakpoint_count;
c906108c 622
1042e4c0
SS
623/* Number of last tracepoint made. */
624
95a42b64 625static int tracepoint_count;
1042e4c0 626
6149aea9
PA
627static struct cmd_list_element *breakpoint_set_cmdlist;
628static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 629struct cmd_list_element *save_cmdlist;
6149aea9 630
badd37ce
SDJ
631/* See declaration at breakpoint.h. */
632
633struct breakpoint *
634breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
635 void *user_data)
636{
43892fdf
SM
637 for (breakpoint *b : all_breakpoints ())
638 if (func (b, user_data) != 0)
639 return b;
badd37ce 640
43892fdf 641 return nullptr;
badd37ce
SDJ
642}
643
468d015d
JJ
644/* Return whether a breakpoint is an active enabled breakpoint. */
645static int
646breakpoint_enabled (struct breakpoint *b)
647{
0d381245 648 return (b->enable_state == bp_enabled);
468d015d
JJ
649}
650
c906108c
SS
651/* Set breakpoint count to NUM. */
652
95a42b64 653static void
fba45db2 654set_breakpoint_count (int num)
c906108c 655{
86b17b60 656 prev_breakpoint_count = breakpoint_count;
c906108c 657 breakpoint_count = num;
4fa62494 658 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
659}
660
86b17b60
PA
661/* Used by `start_rbreak_breakpoints' below, to record the current
662 breakpoint count before "rbreak" creates any breakpoint. */
663static int rbreak_start_breakpoint_count;
664
95a42b64
TT
665/* Called at the start an "rbreak" command to record the first
666 breakpoint made. */
86b17b60 667
c80049d3 668scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 669{
86b17b60 670 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
671}
672
673/* Called at the end of an "rbreak" command to record the last
674 breakpoint made. */
86b17b60 675
c80049d3 676scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 677{
86b17b60 678 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
679}
680
4a64f543 681/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
682
683void
fba45db2 684clear_breakpoint_hit_counts (void)
c906108c 685{
43892fdf 686 for (breakpoint *b : all_breakpoints ())
c906108c
SS
687 b->hit_count = 0;
688}
689
c906108c 690\f
48cb2d85
VP
691/* Return the breakpoint with the specified number, or NULL
692 if the number does not refer to an existing breakpoint. */
693
694struct breakpoint *
695get_breakpoint (int num)
696{
43892fdf 697 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
698 if (b->number == num)
699 return b;
700
43892fdf 701 return nullptr;
48cb2d85 702}
5c44784c 703
c906108c 704\f
adc36818 705
b775012e
LM
706/* Mark locations as "conditions have changed" in case the target supports
707 evaluating conditions on its side. */
708
709static void
710mark_breakpoint_modified (struct breakpoint *b)
711{
b775012e
LM
712 /* This is only meaningful if the target is
713 evaluating conditions and if the user has
714 opted for condition evaluation on the target's
715 side. */
716 if (gdb_evaluates_breakpoint_condition_p ()
717 || !target_supports_evaluation_of_breakpoint_conditions ())
718 return;
719
720 if (!is_breakpoint (b))
721 return;
722
40cb8ca5 723 for (bp_location *loc : b->locations ())
b775012e
LM
724 loc->condition_changed = condition_modified;
725}
726
727/* Mark location as "conditions have changed" in case the target supports
728 evaluating conditions on its side. */
729
730static void
731mark_breakpoint_location_modified (struct bp_location *loc)
732{
733 /* This is only meaningful if the target is
734 evaluating conditions and if the user has
735 opted for condition evaluation on the target's
736 side. */
737 if (gdb_evaluates_breakpoint_condition_p ()
738 || !target_supports_evaluation_of_breakpoint_conditions ())
739
740 return;
741
742 if (!is_breakpoint (loc->owner))
743 return;
744
745 loc->condition_changed = condition_modified;
746}
747
748/* Sets the condition-evaluation mode using the static global
749 condition_evaluation_mode. */
750
751static void
eb4c3f4a 752set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
753 struct cmd_list_element *c)
754{
b775012e
LM
755 const char *old_mode, *new_mode;
756
757 if ((condition_evaluation_mode_1 == condition_evaluation_target)
758 && !target_supports_evaluation_of_breakpoint_conditions ())
759 {
760 condition_evaluation_mode_1 = condition_evaluation_mode;
761 warning (_("Target does not support breakpoint condition evaluation.\n"
762 "Using host evaluation mode instead."));
763 return;
764 }
765
766 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
767 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
768
abf1152a
JK
769 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
770 settings was "auto". */
771 condition_evaluation_mode = condition_evaluation_mode_1;
772
b775012e
LM
773 /* Only update the mode if the user picked a different one. */
774 if (new_mode != old_mode)
775 {
b775012e
LM
776 /* If the user switched to a different evaluation mode, we
777 need to synch the changes with the target as follows:
778
779 "host" -> "target": Send all (valid) conditions to the target.
780 "target" -> "host": Remove all the conditions from the target.
781 */
782
b775012e
LM
783 if (new_mode == condition_evaluation_target)
784 {
785 /* Mark everything modified and synch conditions with the
786 target. */
48d7020b 787 for (bp_location *loc : all_bp_locations ())
b775012e 788 mark_breakpoint_location_modified (loc);
24b21115 789 }
b775012e
LM
790 else
791 {
792 /* Manually mark non-duplicate locations to synch conditions
793 with the target. We do this to remove all the conditions the
794 target knows about. */
48d7020b 795 for (bp_location *loc : all_bp_locations ())
b775012e
LM
796 if (is_breakpoint (loc->owner) && loc->inserted)
797 loc->needs_update = 1;
798 }
799
800 /* Do the update. */
44702360 801 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
802 }
803
804 return;
805}
806
807/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
808 what "auto" is translating to. */
809
810static void
811show_condition_evaluation_mode (struct ui_file *file, int from_tty,
812 struct cmd_list_element *c, const char *value)
813{
814 if (condition_evaluation_mode == condition_evaluation_auto)
815 fprintf_filtered (file,
816 _("Breakpoint condition evaluation "
817 "mode is %s (currently %s).\n"),
818 value,
819 breakpoint_condition_evaluation_mode ());
820 else
821 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
822 value);
823}
824
b5fa468f
TBA
825/* Parse COND_STRING in the context of LOC and set as the condition
826 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
827 the number of LOC within its owner. In case of parsing error, mark
828 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
829
830static void
831set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
832 int bp_num, int loc_num)
833{
834 bool has_junk = false;
835 try
836 {
837 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
838 block_for_pc (loc->address), 0);
839 if (*cond_string != 0)
840 has_junk = true;
841 else
842 {
843 loc->cond = std::move (new_exp);
844 if (loc->disabled_by_cond && loc->enabled)
845 printf_filtered (_("Breakpoint %d's condition is now valid at "
846 "location %d, enabling.\n"),
847 bp_num, loc_num);
848
849 loc->disabled_by_cond = false;
850 }
851 }
852 catch (const gdb_exception_error &e)
853 {
854 if (loc->enabled)
855 {
856 /* Warn if a user-enabled location is now becoming disabled-by-cond.
857 BP_NUM is 0 if the breakpoint is being defined for the first
858 time using the "break ... if ..." command, and non-zero if
859 already defined. */
860 if (bp_num != 0)
861 warning (_("failed to validate condition at location %d.%d, "
862 "disabling:\n %s"), bp_num, loc_num, e.what ());
863 else
864 warning (_("failed to validate condition at location %d, "
865 "disabling:\n %s"), loc_num, e.what ());
866 }
867
868 loc->disabled_by_cond = true;
869 }
870
871 if (has_junk)
872 error (_("Garbage '%s' follows condition"), cond_string);
873}
874
adc36818 875void
7a26bd4d 876set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 877 int from_tty, bool force)
adc36818 878{
4c55e970 879 if (*exp == 0)
3a5c3e22 880 {
4c55e970
TBA
881 xfree (b->cond_string);
882 b->cond_string = nullptr;
3a5c3e22 883
4c55e970 884 if (is_watchpoint (b))
78319c15 885 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
886 else
887 {
b5fa468f 888 int loc_num = 1;
40cb8ca5 889 for (bp_location *loc : b->locations ())
4c55e970
TBA
890 {
891 loc->cond.reset ();
b5fa468f
TBA
892 if (loc->disabled_by_cond && loc->enabled)
893 printf_filtered (_("Breakpoint %d's condition is now valid at "
894 "location %d, enabling.\n"),
895 b->number, loc_num);
896 loc->disabled_by_cond = false;
897 loc_num++;
4c55e970
TBA
898
899 /* No need to free the condition agent expression
900 bytecode (if we have one). We will handle this
901 when we go through update_global_location_list. */
902 }
903 }
1e620590 904
adc36818
PM
905 if (from_tty)
906 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
907 }
908 else
909 {
adc36818
PM
910 if (is_watchpoint (b))
911 {
699bd4cf 912 innermost_block_tracker tracker;
78319c15 913 const char *arg = exp;
4c55e970 914 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 915 if (*arg != 0)
adc36818 916 error (_("Junk at end of expression"));
78319c15 917 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 918 w->cond_exp = std::move (new_exp);
699bd4cf 919 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
920 }
921 else
922 {
4c55e970
TBA
923 /* Parse and set condition expressions. We make two passes.
924 In the first, we parse the condition string to see if it
b5fa468f
TBA
925 is valid in at least one location. If so, the condition
926 would be accepted. So we go ahead and set the locations'
927 conditions. In case no valid case is found, we throw
4c55e970
TBA
928 the error and the condition string will be rejected.
929 This two-pass approach is taken to avoid setting the
930 state of locations in case of a reject. */
40cb8ca5 931 for (bp_location *loc : b->locations ())
4c55e970 932 {
b5fa468f
TBA
933 try
934 {
935 const char *arg = exp;
936 parse_exp_1 (&arg, loc->address,
937 block_for_pc (loc->address), 0);
938 if (*arg != 0)
939 error (_("Junk at end of expression"));
940 break;
941 }
942 catch (const gdb_exception_error &e)
943 {
944 /* Condition string is invalid. If this happens to
733d554a
TBA
945 be the last loc, abandon (if not forced) or continue
946 (if forced). */
947 if (loc->next == nullptr && !force)
b5fa468f
TBA
948 throw;
949 }
4c55e970
TBA
950 }
951
b5fa468f 952 /* If we reach here, the condition is valid at some locations. */
dda83cd7 953 int loc_num = 1;
40cb8ca5
SM
954 for (bp_location *loc : b->locations ())
955 {
956 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
957 loc_num++;
958 }
adc36818 959 }
1e620590
TBA
960
961 /* We know that the new condition parsed successfully. The
962 condition string of the breakpoint can be safely updated. */
963 xfree (b->cond_string);
964 b->cond_string = xstrdup (exp);
965 b->condition_not_parsed = 0;
adc36818 966 }
b775012e
LM
967 mark_breakpoint_modified (b);
968
76727919 969 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
970}
971
79aabb73
TBA
972/* See breakpoint.h. */
973
974void
975set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
976 bool force)
977{
43892fdf 978 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
979 if (b->number == bpnum)
980 {
981 /* Check if this breakpoint has a "stop" method implemented in an
982 extension language. This method and conditions entered into GDB
983 from the CLI are mutually exclusive. */
984 const struct extension_language_defn *extlang
985 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
986
987 if (extlang != NULL)
988 {
989 error (_("Only one stop condition allowed. There is currently"
990 " a %s stop condition defined for this breakpoint."),
991 ext_lang_capitalized_name (extlang));
992 }
993 set_breakpoint_condition (b, exp, from_tty, force);
994
995 if (is_breakpoint (b))
996 update_global_location_list (UGLL_MAY_INSERT);
997
998 return;
999 }
1000
1001 error (_("No breakpoint number %d."), bpnum);
1002}
1003
b1d4d8d1
TBA
1004/* The options for the "condition" command. */
1005
1006struct condition_command_opts
1007{
1008 /* For "-force". */
1009 bool force_condition = false;
1010};
1011
1012static const gdb::option::option_def condition_command_option_defs[] = {
1013
1014 gdb::option::flag_option_def<condition_command_opts> {
1015 "force",
1016 [] (condition_command_opts *opts) { return &opts->force_condition; },
1017 N_("Set the condition even if it is invalid for all current locations."),
1018 },
1019
1020};
1021
1022/* Create an option_def_group for the "condition" options, with
1023 CC_OPTS as context. */
1024
1025static inline gdb::option::option_def_group
1026make_condition_command_options_def_group (condition_command_opts *cc_opts)
1027{
1028 return {{condition_command_option_defs}, cc_opts};
1029}
1030
d55637df
TT
1031/* Completion for the "condition" command. */
1032
eb3ff9a5 1033static void
6f937416 1034condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1035 completion_tracker &tracker,
b1d4d8d1 1036 const char *text, const char * /*word*/)
d55637df 1037{
b1d4d8d1
TBA
1038 bool has_no_arguments = (*text == '\0');
1039 condition_command_opts cc_opts;
1040 const auto group = make_condition_command_options_def_group (&cc_opts);
1041 if (gdb::option::complete_options
1042 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1043 return;
d55637df 1044
f1735a53 1045 text = skip_spaces (text);
b1d4d8d1 1046 const char *space = skip_to_space (text);
d55637df
TT
1047 if (*space == '\0')
1048 {
1049 int len;
d55637df
TT
1050
1051 if (text[0] == '$')
1052 {
b1d4d8d1 1053 tracker.advance_custom_word_point_by (1);
d55637df 1054 /* We don't support completion of history indices. */
eb3ff9a5
PA
1055 if (!isdigit (text[1]))
1056 complete_internalvar (tracker, &text[1]);
1057 return;
d55637df
TT
1058 }
1059
b1d4d8d1
TBA
1060 /* Suggest the "-force" flag if no arguments are given. If
1061 arguments were passed, they either already include the flag,
1062 or we are beyond the point of suggesting it because it's
1063 positionally the first argument. */
1064 if (has_no_arguments)
1065 gdb::option::complete_on_all_options (tracker, group);
1066
d55637df
TT
1067 /* We're completing the breakpoint number. */
1068 len = strlen (text);
1069
43892fdf 1070 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1071 {
1072 char number[50];
1073
1074 xsnprintf (number, sizeof (number), "%d", b->number);
1075
1076 if (strncmp (number, text, len) == 0)
b02f78f9 1077 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1078 }
d55637df 1079
eb3ff9a5 1080 return;
d55637df
TT
1081 }
1082
b1d4d8d1
TBA
1083 /* We're completing the expression part. Skip the breakpoint num. */
1084 const char *exp_start = skip_spaces (space);
1085 tracker.advance_custom_word_point_by (exp_start - text);
1086 text = exp_start;
1087 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1088 expression_completer (cmd, tracker, text, word);
d55637df
TT
1089}
1090
c906108c
SS
1091/* condition N EXP -- set break condition of breakpoint N to EXP. */
1092
1093static void
0b39b52e 1094condition_command (const char *arg, int from_tty)
c906108c 1095{
0b39b52e 1096 const char *p;
52f0bd74 1097 int bnum;
c906108c
SS
1098
1099 if (arg == 0)
e2e0b3e5 1100 error_no_arg (_("breakpoint number"));
c906108c
SS
1101
1102 p = arg;
733d554a
TBA
1103
1104 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1105 condition_command_opts cc_opts;
1106 const auto group = make_condition_command_options_def_group (&cc_opts);
1107 gdb::option::process_options
1108 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1109
c906108c 1110 bnum = get_number (&p);
5c44784c 1111 if (bnum == 0)
8a3fe4f8 1112 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1113
79aabb73 1114 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1115}
1116
a7bdde9e
VP
1117/* Check that COMMAND do not contain commands that are suitable
1118 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1119 Throw if any such commands is found. */
1120
a7bdde9e
VP
1121static void
1122check_no_tracepoint_commands (struct command_line *commands)
1123{
1124 struct command_line *c;
cc59ec59 1125
a7bdde9e
VP
1126 for (c = commands; c; c = c->next)
1127 {
a7bdde9e 1128 if (c->control_type == while_stepping_control)
3e43a32a
MS
1129 error (_("The 'while-stepping' command can "
1130 "only be used for tracepoints"));
a7bdde9e 1131
12973681
TT
1132 check_no_tracepoint_commands (c->body_list_0.get ());
1133 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1134
1135 /* Not that command parsing removes leading whitespace and comment
4a64f543 1136 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1137 command directly. */
1138 if (strstr (c->line, "collect ") == c->line)
1139 error (_("The 'collect' command can only be used for tracepoints"));
1140
51661e93
VP
1141 if (strstr (c->line, "teval ") == c->line)
1142 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1143 }
1144}
1145
c1fc2657 1146struct longjmp_breakpoint : public breakpoint
3b0871f4 1147{
c1fc2657 1148 ~longjmp_breakpoint () override;
3b0871f4
SM
1149};
1150
d77f58be
SS
1151/* Encapsulate tests for different types of tracepoints. */
1152
3b0871f4
SM
1153static bool
1154is_tracepoint_type (bptype type)
d9b3f62e
PA
1155{
1156 return (type == bp_tracepoint
1157 || type == bp_fast_tracepoint
1158 || type == bp_static_tracepoint);
1159}
1160
3b0871f4
SM
1161static bool
1162is_longjmp_type (bptype type)
1163{
1164 return type == bp_longjmp || type == bp_exception;
1165}
1166
f2478a7e
SM
1167/* See breakpoint.h. */
1168
1169bool
d77f58be 1170is_tracepoint (const struct breakpoint *b)
a7bdde9e 1171{
d9b3f62e 1172 return is_tracepoint_type (b->type);
a7bdde9e 1173}
d9b3f62e 1174
a5e364af
SM
1175/* Factory function to create an appropriate instance of breakpoint given
1176 TYPE. */
1177
1178static std::unique_ptr<breakpoint>
1179new_breakpoint_from_type (bptype type)
1180{
1181 breakpoint *b;
1182
1183 if (is_tracepoint_type (type))
c1fc2657 1184 b = new tracepoint ();
3b0871f4 1185 else if (is_longjmp_type (type))
c1fc2657 1186 b = new longjmp_breakpoint ();
a5e364af
SM
1187 else
1188 b = new breakpoint ();
1189
1190 return std::unique_ptr<breakpoint> (b);
1191}
1192
e5dd4106 1193/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1194 breakpoint. This function will throw an exception if a problem is
1195 found. */
48cb2d85 1196
95a42b64
TT
1197static void
1198validate_commands_for_breakpoint (struct breakpoint *b,
1199 struct command_line *commands)
48cb2d85 1200{
d77f58be 1201 if (is_tracepoint (b))
a7bdde9e 1202 {
c9a6ce02 1203 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1204 struct command_line *c;
1205 struct command_line *while_stepping = 0;
c9a6ce02
PA
1206
1207 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1208 might have included a while-stepping action, while the new
1209 ones might not. */
c9a6ce02
PA
1210 t->step_count = 0;
1211
1212 /* We need to verify that each top-level element of commands is
1213 valid for tracepoints, that there's at most one
1214 while-stepping element, and that the while-stepping's body
1215 has valid tracing commands excluding nested while-stepping.
1216 We also need to validate the tracepoint action line in the
1217 context of the tracepoint --- validate_actionline actually
1218 has side effects, like setting the tracepoint's
1219 while-stepping STEP_COUNT, in addition to checking if the
1220 collect/teval actions parse and make sense in the
1221 tracepoint's context. */
a7bdde9e
VP
1222 for (c = commands; c; c = c->next)
1223 {
a7bdde9e
VP
1224 if (c->control_type == while_stepping_control)
1225 {
1226 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1227 error (_("The 'while-stepping' command "
1228 "cannot be used for fast tracepoint"));
0fb4aa4b 1229 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1230 error (_("The 'while-stepping' command "
1231 "cannot be used for static tracepoint"));
a7bdde9e
VP
1232
1233 if (while_stepping)
3e43a32a
MS
1234 error (_("The 'while-stepping' command "
1235 "can be used only once"));
a7bdde9e
VP
1236 else
1237 while_stepping = c;
1238 }
c9a6ce02
PA
1239
1240 validate_actionline (c->line, b);
a7bdde9e
VP
1241 }
1242 if (while_stepping)
1243 {
1244 struct command_line *c2;
1245
12973681
TT
1246 gdb_assert (while_stepping->body_list_1 == nullptr);
1247 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1248 for (; c2; c2 = c2->next)
1249 {
a7bdde9e
VP
1250 if (c2->control_type == while_stepping_control)
1251 error (_("The 'while-stepping' command cannot be nested"));
1252 }
1253 }
1254 }
1255 else
1256 {
1257 check_no_tracepoint_commands (commands);
1258 }
95a42b64
TT
1259}
1260
0fb4aa4b
PA
1261/* Return a vector of all the static tracepoints set at ADDR. The
1262 caller is responsible for releasing the vector. */
1263
f51e0e20 1264std::vector<breakpoint *>
0fb4aa4b
PA
1265static_tracepoints_here (CORE_ADDR addr)
1266{
f51e0e20 1267 std::vector<breakpoint *> found;
0fb4aa4b 1268
43892fdf 1269 for (breakpoint *b : all_breakpoints ())
0fb4aa4b
PA
1270 if (b->type == bp_static_tracepoint)
1271 {
40cb8ca5 1272 for (bp_location *loc : b->locations ())
0fb4aa4b 1273 if (loc->address == addr)
f51e0e20 1274 found.push_back (b);
0fb4aa4b
PA
1275 }
1276
1277 return found;
1278}
1279
95a42b64 1280/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1281 validate that only allowed commands are included. */
95a42b64
TT
1282
1283void
4a64f543 1284breakpoint_set_commands (struct breakpoint *b,
12973681 1285 counted_command_line &&commands)
95a42b64 1286{
93921405 1287 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1288
d1b0a7bf 1289 b->commands = std::move (commands);
76727919 1290 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1291}
1292
45a43567
TT
1293/* Set the internal `silent' flag on the breakpoint. Note that this
1294 is not the same as the "silent" that may appear in the breakpoint's
1295 commands. */
1296
1297void
1298breakpoint_set_silent (struct breakpoint *b, int silent)
1299{
1300 int old_silent = b->silent;
1301
1302 b->silent = silent;
1303 if (old_silent != silent)
76727919 1304 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1305}
1306
1307/* Set the thread for this breakpoint. If THREAD is -1, make the
1308 breakpoint work for any thread. */
1309
1310void
1311breakpoint_set_thread (struct breakpoint *b, int thread)
1312{
1313 int old_thread = b->thread;
1314
1315 b->thread = thread;
1316 if (old_thread != thread)
76727919 1317 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1318}
1319
1320/* Set the task for this breakpoint. If TASK is 0, make the
1321 breakpoint work for any task. */
1322
1323void
1324breakpoint_set_task (struct breakpoint *b, int task)
1325{
1326 int old_task = b->task;
1327
1328 b->task = task;
1329 if (old_task != task)
76727919 1330 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1331}
1332
95a42b64 1333static void
896b6bda 1334commands_command_1 (const char *arg, int from_tty,
4a64f543 1335 struct command_line *control)
95a42b64 1336{
d1b0a7bf 1337 counted_command_line cmd;
999700cd
PW
1338 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1339 NULL after the call to read_command_lines if the user provides an empty
1340 list of command by just typing "end". */
1341 bool cmd_read = false;
95a42b64 1342
896b6bda
PA
1343 std::string new_arg;
1344
95a42b64
TT
1345 if (arg == NULL || !*arg)
1346 {
da1df1db 1347 /* Argument not explicitly given. Synthesize it. */
86b17b60 1348 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1349 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1350 breakpoint_count);
95a42b64 1351 else if (breakpoint_count > 0)
896b6bda 1352 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1353 }
da1df1db
TBA
1354 else
1355 {
1356 /* Create a copy of ARG. This is needed because the "commands"
1357 command may be coming from a script. In that case, the read
1358 line buffer is going to be overwritten in the lambda of
1359 'map_breakpoint_numbers' below when reading the next line
1360 before we are are done parsing the breakpoint numbers. */
1361 new_arg = arg;
1362 }
1363 arg = new_arg.c_str ();
48649e1b
TT
1364
1365 map_breakpoint_numbers
1366 (arg, [&] (breakpoint *b)
1367 {
999700cd 1368 if (!cmd_read)
48649e1b 1369 {
999700cd 1370 gdb_assert (cmd == NULL);
48649e1b 1371 if (control != NULL)
12973681 1372 cmd = control->body_list_0;
48649e1b
TT
1373 else
1374 {
81b1e71c
TT
1375 std::string str
1376 = string_printf (_("Type commands for breakpoint(s) "
1377 "%s, one per line."),
1378 arg);
48649e1b 1379
60b3cef2
TT
1380 auto do_validate = [=] (const char *line)
1381 {
1382 validate_actionline (line, b);
1383 };
1384 gdb::function_view<void (const char *)> validator;
1385 if (is_tracepoint (b))
1386 validator = do_validate;
1387
1388 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1389 }
999700cd 1390 cmd_read = true;
48649e1b
TT
1391 }
1392
1393 /* If a breakpoint was on the list more than once, we don't need to
1394 do anything. */
1395 if (b->commands != cmd)
1396 {
d1b0a7bf 1397 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1398 b->commands = cmd;
76727919 1399 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1400 }
1401 });
95a42b64
TT
1402}
1403
1404static void
0b39b52e 1405commands_command (const char *arg, int from_tty)
95a42b64
TT
1406{
1407 commands_command_1 (arg, from_tty, NULL);
c906108c 1408}
40c03ae8
EZ
1409
1410/* Like commands_command, but instead of reading the commands from
1411 input stream, takes them from an already parsed command structure.
1412
1413 This is used by cli-script.c to DTRT with breakpoint commands
1414 that are part of if and while bodies. */
1415enum command_control_type
896b6bda 1416commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1417{
95a42b64
TT
1418 commands_command_1 (arg, 0, cmd);
1419 return simple_control;
40c03ae8 1420}
876fa593
JK
1421
1422/* Return non-zero if BL->TARGET_INFO contains valid information. */
1423
1424static int
1425bp_location_has_shadow (struct bp_location *bl)
1426{
1427 if (bl->loc_type != bp_loc_software_breakpoint)
1428 return 0;
1429 if (!bl->inserted)
1430 return 0;
1431 if (bl->target_info.shadow_len == 0)
e5dd4106 1432 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1433 return 0;
1434 return 1;
1435}
1436
9d497a19
PA
1437/* Update BUF, which is LEN bytes read from the target address
1438 MEMADDR, by replacing a memory breakpoint with its shadowed
1439 contents.
1440
1441 If READBUF is not NULL, this buffer must not overlap with the of
1442 the breakpoint location's shadow_contents buffer. Otherwise, a
1443 failed assertion internal error will be raised. */
1444
1445static void
1446one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1447 const gdb_byte *writebuf_org,
1448 ULONGEST memaddr, LONGEST len,
1449 struct bp_target_info *target_info,
1450 struct gdbarch *gdbarch)
1451{
1452 /* Now do full processing of the found relevant range of elements. */
1453 CORE_ADDR bp_addr = 0;
1454 int bp_size = 0;
1455 int bptoffset = 0;
1456
1457 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1458 current_program_space->aspace, 0))
1459 {
1460 /* The breakpoint is inserted in a different address space. */
1461 return;
1462 }
1463
1464 /* Addresses and length of the part of the breakpoint that
1465 we need to copy. */
1466 bp_addr = target_info->placed_address;
1467 bp_size = target_info->shadow_len;
1468
1469 if (bp_addr + bp_size <= memaddr)
1470 {
1471 /* The breakpoint is entirely before the chunk of memory we are
1472 reading. */
1473 return;
1474 }
1475
1476 if (bp_addr >= memaddr + len)
1477 {
1478 /* The breakpoint is entirely after the chunk of memory we are
1479 reading. */
1480 return;
1481 }
1482
1483 /* Offset within shadow_contents. */
1484 if (bp_addr < memaddr)
1485 {
1486 /* Only copy the second part of the breakpoint. */
1487 bp_size -= memaddr - bp_addr;
1488 bptoffset = memaddr - bp_addr;
1489 bp_addr = memaddr;
1490 }
1491
1492 if (bp_addr + bp_size > memaddr + len)
1493 {
1494 /* Only copy the first part of the breakpoint. */
1495 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1496 }
1497
1498 if (readbuf != NULL)
1499 {
1500 /* Verify that the readbuf buffer does not overlap with the
1501 shadow_contents buffer. */
1502 gdb_assert (target_info->shadow_contents >= readbuf + len
1503 || readbuf >= (target_info->shadow_contents
1504 + target_info->shadow_len));
1505
1506 /* Update the read buffer with this inserted breakpoint's
1507 shadow. */
1508 memcpy (readbuf + bp_addr - memaddr,
1509 target_info->shadow_contents + bptoffset, bp_size);
1510 }
1511 else
1512 {
1513 const unsigned char *bp;
0d5ed153
MR
1514 CORE_ADDR addr = target_info->reqstd_address;
1515 int placed_size;
9d497a19
PA
1516
1517 /* Update the shadow with what we want to write to memory. */
1518 memcpy (target_info->shadow_contents + bptoffset,
1519 writebuf_org + bp_addr - memaddr, bp_size);
1520
1521 /* Determine appropriate breakpoint contents and size for this
1522 address. */
0d5ed153 1523 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1524
1525 /* Update the final write buffer with this inserted
1526 breakpoint's INSN. */
1527 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1528 }
1529}
1530
8defab1a 1531/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1532 by replacing any memory breakpoints with their shadowed contents.
1533
35c63cd8
JB
1534 If READBUF is not NULL, this buffer must not overlap with any of
1535 the breakpoint location's shadow_contents buffers. Otherwise,
1536 a failed assertion internal error will be raised.
1537
876fa593 1538 The range of shadowed area by each bp_location is:
f5336ca5
PA
1539 bl->address - bp_locations_placed_address_before_address_max
1540 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1541 The range we were requested to resolve shadows for is:
1542 memaddr ... memaddr + len
1543 Thus the safe cutoff boundaries for performance optimization are
35df4500 1544 memaddr + len <= (bl->address
f5336ca5 1545 - bp_locations_placed_address_before_address_max)
876fa593 1546 and:
f5336ca5 1547 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1548
8defab1a 1549void
f0ba3972
PA
1550breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1551 const gdb_byte *writebuf_org,
1552 ULONGEST memaddr, LONGEST len)
c906108c 1553{
4a64f543
MS
1554 /* Left boundary, right boundary and median element of our binary
1555 search. */
876fa593
JK
1556 unsigned bc_l, bc_r, bc;
1557
4a64f543
MS
1558 /* Find BC_L which is a leftmost element which may affect BUF
1559 content. It is safe to report lower value but a failure to
1560 report higher one. */
876fa593
JK
1561
1562 bc_l = 0;
5d51cd5d 1563 bc_r = bp_locations.size ();
876fa593
JK
1564 while (bc_l + 1 < bc_r)
1565 {
35df4500 1566 struct bp_location *bl;
876fa593
JK
1567
1568 bc = (bc_l + bc_r) / 2;
f5336ca5 1569 bl = bp_locations[bc];
876fa593 1570
4a64f543
MS
1571 /* Check first BL->ADDRESS will not overflow due to the added
1572 constant. Then advance the left boundary only if we are sure
1573 the BC element can in no way affect the BUF content (MEMADDR
1574 to MEMADDR + LEN range).
876fa593 1575
f5336ca5 1576 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1577 offset so that we cannot miss a breakpoint with its shadow
1578 range tail still reaching MEMADDR. */
c5aa993b 1579
f5336ca5 1580 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1581 >= bl->address)
f5336ca5 1582 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1583 <= memaddr))
876fa593
JK
1584 bc_l = bc;
1585 else
1586 bc_r = bc;
1587 }
1588
128070bb
PA
1589 /* Due to the binary search above, we need to make sure we pick the
1590 first location that's at BC_L's address. E.g., if there are
1591 multiple locations at the same address, BC_L may end up pointing
1592 at a duplicate location, and miss the "master"/"inserted"
1593 location. Say, given locations L1, L2 and L3 at addresses A and
1594 B:
1595
1596 L1@A, L2@A, L3@B, ...
1597
1598 BC_L could end up pointing at location L2, while the "master"
1599 location could be L1. Since the `loc->inserted' flag is only set
1600 on "master" locations, we'd forget to restore the shadow of L1
1601 and L2. */
1602 while (bc_l > 0
f5336ca5 1603 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1604 bc_l--;
1605
876fa593
JK
1606 /* Now do full processing of the found relevant range of elements. */
1607
5d51cd5d 1608 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1609 {
1610 struct bp_location *bl = bp_locations[bc];
876fa593 1611
01add95b
SM
1612 /* bp_location array has BL->OWNER always non-NULL. */
1613 if (bl->owner->type == bp_none)
1614 warning (_("reading through apparently deleted breakpoint #%d?"),
1615 bl->owner->number);
ffce0d52 1616
01add95b
SM
1617 /* Performance optimization: any further element can no longer affect BUF
1618 content. */
876fa593 1619
01add95b
SM
1620 if (bl->address >= bp_locations_placed_address_before_address_max
1621 && (memaddr + len
1622 <= (bl->address
1623 - bp_locations_placed_address_before_address_max)))
1624 break;
876fa593 1625
01add95b
SM
1626 if (!bp_location_has_shadow (bl))
1627 continue;
6c95b8df 1628
01add95b
SM
1629 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1630 memaddr, len, &bl->target_info, bl->gdbarch);
1631 }
c906108c 1632}
9d497a19 1633
f2478a7e 1634/* See breakpoint.h. */
b775012e 1635
f2478a7e 1636bool
b775012e
LM
1637is_breakpoint (const struct breakpoint *bpt)
1638{
1639 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1640 || bpt->type == bp_hardware_breakpoint
1641 || bpt->type == bp_dprintf);
b775012e
LM
1642}
1643
60e1c644
PA
1644/* Return true if BPT is of any hardware watchpoint kind. */
1645
f2478a7e 1646static bool
d77f58be 1647is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1648{
1649 return (bpt->type == bp_hardware_watchpoint
1650 || bpt->type == bp_read_watchpoint
1651 || bpt->type == bp_access_watchpoint);
1652}
7270d8f2 1653
f2478a7e 1654/* See breakpoint.h. */
60e1c644 1655
f2478a7e 1656bool
d77f58be 1657is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1658{
1659 return (is_hardware_watchpoint (bpt)
1660 || bpt->type == bp_watchpoint);
1661}
1662
3a5c3e22
PA
1663/* Returns true if the current thread and its running state are safe
1664 to evaluate or update watchpoint B. Watchpoints on local
1665 expressions need to be evaluated in the context of the thread that
1666 was current when the watchpoint was created, and, that thread needs
1667 to be stopped to be able to select the correct frame context.
1668 Watchpoints on global expressions can be evaluated on any thread,
1669 and in any state. It is presently left to the target allowing
1670 memory accesses when threads are running. */
f6bc2008
PA
1671
1672static int
3a5c3e22 1673watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1674{
c1fc2657 1675 return (b->pspace == current_program_space
d7e15655
TT
1676 && (b->watchpoint_thread == null_ptid
1677 || (inferior_ptid == b->watchpoint_thread
00431a78 1678 && !inferior_thread ()->executing)));
f6bc2008
PA
1679}
1680
d0fb5eae
JK
1681/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1682 associated bp_watchpoint_scope breakpoint. */
1683
1684static void
3a5c3e22 1685watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1686{
c1fc2657 1687 if (w->related_breakpoint != w)
d0fb5eae 1688 {
c1fc2657
SM
1689 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1690 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1691 w->related_breakpoint->disposition = disp_del_at_next_stop;
1692 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1693 w->related_breakpoint = w;
d0fb5eae 1694 }
c1fc2657 1695 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1696}
1697
bb9d5f81
PP
1698/* Extract a bitfield value from value VAL using the bit parameters contained in
1699 watchpoint W. */
1700
1701static struct value *
1702extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1703{
1704 struct value *bit_val;
1705
1706 if (val == NULL)
1707 return NULL;
1708
1709 bit_val = allocate_value (value_type (val));
1710
1711 unpack_value_bitfield (bit_val,
1712 w->val_bitpos,
1713 w->val_bitsize,
1714 value_contents_for_printing (val),
1715 value_offset (val),
1716 val);
1717
1718 return bit_val;
1719}
1720
c6d81124
PA
1721/* Allocate a dummy location and add it to B, which must be a software
1722 watchpoint. This is required because even if a software watchpoint
1723 is not watching any memory, bpstat_stop_status requires a location
1724 to be able to report stops. */
1725
1726static void
1727software_watchpoint_add_no_memory_location (struct breakpoint *b,
1728 struct program_space *pspace)
1729{
1730 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1731
1732 b->loc = allocate_bp_location (b);
1733 b->loc->pspace = pspace;
1734 b->loc->address = -1;
1735 b->loc->length = -1;
1736}
1737
1738/* Returns true if B is a software watchpoint that is not watching any
1739 memory (e.g., "watch $pc"). */
1740
f2478a7e 1741static bool
c6d81124
PA
1742is_no_memory_software_watchpoint (struct breakpoint *b)
1743{
1744 return (b->type == bp_watchpoint
1745 && b->loc != NULL
1746 && b->loc->next == NULL
1747 && b->loc->address == -1
1748 && b->loc->length == -1);
1749}
1750
567e1b4e
JB
1751/* Assuming that B is a watchpoint:
1752 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1753 - Evaluate expression and store the result in B->val
567e1b4e
JB
1754 - Evaluate the condition if there is one, and store the result
1755 in b->loc->cond.
a5606eee
VP
1756 - Update the list of values that must be watched in B->loc.
1757
4a64f543
MS
1758 If the watchpoint disposition is disp_del_at_next_stop, then do
1759 nothing. If this is local watchpoint that is out of scope, delete
1760 it.
1761
1762 Even with `set breakpoint always-inserted on' the watchpoints are
1763 removed + inserted on each stop here. Normal breakpoints must
1764 never be removed because they might be missed by a running thread
1765 when debugging in non-stop mode. On the other hand, hardware
1766 watchpoints (is_hardware_watchpoint; processed here) are specific
1767 to each LWP since they are stored in each LWP's hardware debug
1768 registers. Therefore, such LWP must be stopped first in order to
1769 be able to modify its hardware watchpoints.
1770
1771 Hardware watchpoints must be reset exactly once after being
1772 presented to the user. It cannot be done sooner, because it would
1773 reset the data used to present the watchpoint hit to the user. And
1774 it must not be done later because it could display the same single
1775 watchpoint hit during multiple GDB stops. Note that the latter is
1776 relevant only to the hardware watchpoint types bp_read_watchpoint
1777 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1778 not user-visible - its hit is suppressed if the memory content has
1779 not changed.
1780
1781 The following constraints influence the location where we can reset
1782 hardware watchpoints:
1783
1784 * target_stopped_by_watchpoint and target_stopped_data_address are
1785 called several times when GDB stops.
1786
1787 [linux]
1788 * Multiple hardware watchpoints can be hit at the same time,
1789 causing GDB to stop. GDB only presents one hardware watchpoint
1790 hit at a time as the reason for stopping, and all the other hits
1791 are presented later, one after the other, each time the user
1792 requests the execution to be resumed. Execution is not resumed
1793 for the threads still having pending hit event stored in
1794 LWP_INFO->STATUS. While the watchpoint is already removed from
1795 the inferior on the first stop the thread hit event is kept being
1796 reported from its cached value by linux_nat_stopped_data_address
1797 until the real thread resume happens after the watchpoint gets
1798 presented and thus its LWP_INFO->STATUS gets reset.
1799
1800 Therefore the hardware watchpoint hit can get safely reset on the
1801 watchpoint removal from inferior. */
a79d3c27 1802
b40ce68a 1803static void
3a5c3e22 1804update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1805{
a5606eee 1806 int within_current_scope;
a5606eee 1807 struct frame_id saved_frame_id;
66076460 1808 int frame_saved;
a5606eee 1809
f6bc2008
PA
1810 /* If this is a local watchpoint, we only want to check if the
1811 watchpoint frame is in scope if the current thread is the thread
1812 that was used to create the watchpoint. */
1813 if (!watchpoint_in_thread_scope (b))
1814 return;
1815
c1fc2657 1816 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1817 return;
1818
66076460 1819 frame_saved = 0;
a5606eee
VP
1820
1821 /* Determine if the watchpoint is within scope. */
1822 if (b->exp_valid_block == NULL)
1823 within_current_scope = 1;
1824 else
1825 {
b5db5dfc
UW
1826 struct frame_info *fi = get_current_frame ();
1827 struct gdbarch *frame_arch = get_frame_arch (fi);
1828 CORE_ADDR frame_pc = get_frame_pc (fi);
1829
c9cf6e20
MG
1830 /* If we're at a point where the stack has been destroyed
1831 (e.g. in a function epilogue), unwinding may not work
1832 properly. Do not attempt to recreate locations at this
b5db5dfc 1833 point. See similar comments in watchpoint_check. */
c9cf6e20 1834 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1835 return;
66076460
DJ
1836
1837 /* Save the current frame's ID so we can restore it after
dda83cd7 1838 evaluating the watchpoint expression on its own frame. */
66076460 1839 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1840 took a frame parameter, so that we didn't have to change the
1841 selected frame. */
66076460
DJ
1842 frame_saved = 1;
1843 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1844
a5606eee
VP
1845 fi = frame_find_by_id (b->watchpoint_frame);
1846 within_current_scope = (fi != NULL);
1847 if (within_current_scope)
1848 select_frame (fi);
1849 }
1850
b5db5dfc
UW
1851 /* We don't free locations. They are stored in the bp_location array
1852 and update_global_location_list will eventually delete them and
1853 remove breakpoints if needed. */
c1fc2657 1854 b->loc = NULL;
b5db5dfc 1855
a5606eee
VP
1856 if (within_current_scope && reparse)
1857 {
bbc13ae3 1858 const char *s;
d63d0675 1859
4d01a485 1860 b->exp.reset ();
d63d0675 1861 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1862 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1863 /* If the meaning of expression itself changed, the old value is
1864 no longer relevant. We don't want to report a watchpoint hit
1865 to the user when the old value and the new value may actually
1866 be completely different objects. */
fa4727a6 1867 b->val = NULL;
4c1d86d9 1868 b->val_valid = false;
60e1c644
PA
1869
1870 /* Note that unlike with breakpoints, the watchpoint's condition
1871 expression is stored in the breakpoint object, not in the
1872 locations (re)created below. */
c1fc2657 1873 if (b->cond_string != NULL)
60e1c644 1874 {
4d01a485 1875 b->cond_exp.reset ();
60e1c644 1876
c1fc2657 1877 s = b->cond_string;
1bb9788d 1878 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1879 }
a5606eee 1880 }
a5606eee
VP
1881
1882 /* If we failed to parse the expression, for example because
1883 it refers to a global variable in a not-yet-loaded shared library,
1884 don't try to insert watchpoint. We don't automatically delete
1885 such watchpoint, though, since failure to parse expression
1886 is different from out-of-scope watchpoint. */
55f6301a 1887 if (!target_has_execution ())
2d134ed3
PA
1888 {
1889 /* Without execution, memory can't change. No use to try and
1890 set watchpoint locations. The watchpoint will be reset when
1891 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1892 if (!can_use_hw_watchpoints)
1893 {
c1fc2657
SM
1894 if (b->ops->works_in_software_mode (b))
1895 b->type = bp_watchpoint;
e8369a73 1896 else
638aa5a1
AB
1897 error (_("Can't set read/access watchpoint when "
1898 "hardware watchpoints are disabled."));
e8369a73 1899 }
2d134ed3
PA
1900 }
1901 else if (within_current_scope && b->exp)
a5606eee 1902 {
a6535de1 1903 std::vector<value_ref_ptr> val_chain;
8d49165d 1904 struct value *v, *result;
2d134ed3 1905 struct program_space *frame_pspace;
a5606eee 1906
1eaebe02 1907 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 1908 &val_chain, false);
a5606eee 1909
a5606eee
VP
1910 /* Avoid setting b->val if it's already set. The meaning of
1911 b->val is 'the last value' user saw, and we should update
1912 it only if we reported that last value to user. As it
9c06b0b4
TJB
1913 happens, the code that reports it updates b->val directly.
1914 We don't keep track of the memory value for masked
1915 watchpoints. */
c1fc2657 1916 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1917 {
bb9d5f81 1918 if (b->val_bitsize != 0)
850645cf
TT
1919 v = extract_bitfield_from_watchpoint_value (b, v);
1920 b->val = release_value (v);
4c1d86d9 1921 b->val_valid = true;
fa4727a6 1922 }
a5606eee 1923
2d134ed3
PA
1924 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1925
a5606eee 1926 /* Look at each value on the value chain. */
a6535de1
TT
1927 gdb_assert (!val_chain.empty ());
1928 for (const value_ref_ptr &iter : val_chain)
a5606eee 1929 {
a6535de1
TT
1930 v = iter.get ();
1931
a5606eee
VP
1932 /* If it's a memory location, and GDB actually needed
1933 its contents to evaluate the expression, then we
fa4727a6
DJ
1934 must watch it. If the first value returned is
1935 still lazy, that means an error occurred reading it;
1936 watch it anyway in case it becomes readable. */
a5606eee 1937 if (VALUE_LVAL (v) == lval_memory
a6535de1 1938 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1939 {
1940 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1941
a5606eee
VP
1942 /* We only watch structs and arrays if user asked
1943 for it explicitly, never if they just happen to
1944 appear in the middle of some value chain. */
fa4727a6 1945 if (v == result
78134374
SM
1946 || (vtype->code () != TYPE_CODE_STRUCT
1947 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1948 {
1949 CORE_ADDR addr;
f486487f 1950 enum target_hw_bp_type type;
a5606eee 1951 struct bp_location *loc, **tmp;
bb9d5f81
PP
1952 int bitpos = 0, bitsize = 0;
1953
1954 if (value_bitsize (v) != 0)
1955 {
1956 /* Extract the bit parameters out from the bitfield
1957 sub-expression. */
1958 bitpos = value_bitpos (v);
1959 bitsize = value_bitsize (v);
1960 }
1961 else if (v == result && b->val_bitsize != 0)
1962 {
1963 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1964 lvalue whose bit parameters are saved in the fields
1965 VAL_BITPOS and VAL_BITSIZE. */
1966 bitpos = b->val_bitpos;
1967 bitsize = b->val_bitsize;
1968 }
a5606eee 1969
42ae5230 1970 addr = value_address (v);
bb9d5f81
PP
1971 if (bitsize != 0)
1972 {
1973 /* Skip the bytes that don't contain the bitfield. */
1974 addr += bitpos / 8;
1975 }
1976
a5606eee 1977 type = hw_write;
c1fc2657 1978 if (b->type == bp_read_watchpoint)
a5606eee 1979 type = hw_read;
c1fc2657 1980 else if (b->type == bp_access_watchpoint)
a5606eee 1981 type = hw_access;
3a5c3e22 1982
c1fc2657
SM
1983 loc = allocate_bp_location (b);
1984 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1985 ;
1986 *tmp = loc;
8ee511af 1987 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
1988
1989 loc->pspace = frame_pspace;
f17d9474 1990 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1991
1992 if (bitsize != 0)
1993 {
1994 /* Just cover the bytes that make up the bitfield. */
1995 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1996 }
1997 else
1998 loc->length = TYPE_LENGTH (value_type (v));
1999
a5606eee
VP
2000 loc->watchpoint_type = type;
2001 }
2002 }
9fa40276
TJB
2003 }
2004
2005 /* Change the type of breakpoint between hardware assisted or
2006 an ordinary watchpoint depending on the hardware support
2007 and free hardware slots. REPARSE is set when the inferior
2008 is started. */
a9634178 2009 if (reparse)
9fa40276 2010 {
e09342b5 2011 int reg_cnt;
9fa40276 2012 enum bp_loc_type loc_type;
a5606eee 2013
a9634178 2014 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2015
2016 if (reg_cnt)
9fa40276
TJB
2017 {
2018 int i, target_resources_ok, other_type_used;
a1398e0c 2019 enum bptype type;
9fa40276 2020
a9634178
TJB
2021 /* Use an exact watchpoint when there's only one memory region to be
2022 watched, and only one debug register is needed to watch it. */
2023 b->exact = target_exact_watchpoints && reg_cnt == 1;
2024
9fa40276 2025 /* We need to determine how many resources are already
e09342b5
TJB
2026 used for all other hardware watchpoints plus this one
2027 to see if we still have enough resources to also fit
a1398e0c
PA
2028 this watchpoint in as well. */
2029
2030 /* If this is a software watchpoint, we try to turn it
2031 to a hardware one -- count resources as if B was of
2032 hardware watchpoint type. */
c1fc2657 2033 type = b->type;
a1398e0c
PA
2034 if (type == bp_watchpoint)
2035 type = bp_hardware_watchpoint;
2036
2037 /* This watchpoint may or may not have been placed on
2038 the list yet at this point (it won't be in the list
2039 if we're trying to create it for the first time,
2040 through watch_command), so always account for it
2041 manually. */
2042
2043 /* Count resources used by all watchpoints except B. */
c1fc2657 2044 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2045
2046 /* Add in the resources needed for B. */
c1fc2657 2047 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2048
2049 target_resources_ok
2050 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2051 if (target_resources_ok <= 0)
a9634178 2052 {
c1fc2657 2053 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2054
2055 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2056 error (_("Target does not support this type of "
2057 "hardware watchpoint."));
9c06b0b4
TJB
2058 else if (target_resources_ok < 0 && !sw_mode)
2059 error (_("There are not enough available hardware "
2060 "resources for this watchpoint."));
a1398e0c
PA
2061
2062 /* Downgrade to software watchpoint. */
c1fc2657 2063 b->type = bp_watchpoint;
a1398e0c
PA
2064 }
2065 else
2066 {
2067 /* If this was a software watchpoint, we've just
2068 found we have enough resources to turn it to a
2069 hardware watchpoint. Otherwise, this is a
2070 nop. */
c1fc2657 2071 b->type = type;
a9634178 2072 }
9fa40276 2073 }
c1fc2657 2074 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2075 {
2076 if (!can_use_hw_watchpoints)
2077 error (_("Can't set read/access watchpoint when "
2078 "hardware watchpoints are disabled."));
2079 else
2080 error (_("Expression cannot be implemented with "
2081 "read/access watchpoint."));
2082 }
9fa40276 2083 else
c1fc2657 2084 b->type = bp_watchpoint;
9fa40276 2085
c1fc2657 2086 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2087 : bp_loc_hardware_watchpoint);
40cb8ca5 2088 for (bp_location *bl : b->locations ())
9fa40276
TJB
2089 bl->loc_type = loc_type;
2090 }
2091
c7437ca6
PA
2092 /* If a software watchpoint is not watching any memory, then the
2093 above left it without any location set up. But,
2094 bpstat_stop_status requires a location to be able to report
2095 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2096 if (b->type == bp_watchpoint && b->loc == NULL)
2097 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2098 }
2099 else if (!within_current_scope)
7270d8f2 2100 {
ac74f770
MS
2101 printf_filtered (_("\
2102Watchpoint %d deleted because the program has left the block\n\
2103in which its expression is valid.\n"),
c1fc2657 2104 b->number);
d0fb5eae 2105 watchpoint_del_at_next_stop (b);
7270d8f2 2106 }
a5606eee
VP
2107
2108 /* Restore the selected frame. */
66076460
DJ
2109 if (frame_saved)
2110 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2111}
2112
a5606eee 2113
74960c60 2114/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2115 inserted in the inferior. We don't differentiate the type of BL's owner
2116 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2117 breakpoint_ops is not defined, because in insert_bp_location,
2118 tracepoint's insert_location will not be called. */
74960c60 2119static int
35df4500 2120should_be_inserted (struct bp_location *bl)
74960c60 2121{
35df4500 2122 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2123 return 0;
2124
35df4500 2125 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2126 return 0;
2127
b5fa468f
TBA
2128 if (!bl->enabled || bl->disabled_by_cond
2129 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2130 return 0;
2131
f8eba3c6
TT
2132 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2133 return 0;
2134
56710373
PA
2135 /* This is set for example, when we're attached to the parent of a
2136 vfork, and have detached from the child. The child is running
2137 free, and we expect it to do an exec or exit, at which point the
2138 OS makes the parent schedulable again (and the target reports
2139 that the vfork is done). Until the child is done with the shared
2140 memory region, do not insert breakpoints in the parent, otherwise
2141 the child could still trip on the parent's breakpoints. Since
2142 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2143 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2144 return 0;
2145
31e77af2 2146 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2147 location, except if the breakpoint is a single-step breakpoint,
2148 and the breakpoint's thread is the thread which is stepping past
2149 a breakpoint. */
31e77af2
PA
2150 if ((bl->loc_type == bp_loc_software_breakpoint
2151 || bl->loc_type == bp_loc_hardware_breakpoint)
2152 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2153 bl->address)
2154 /* The single-step breakpoint may be inserted at the location
2155 we're trying to step if the instruction branches to itself.
2156 However, the instruction won't be executed at all and it may
2157 break the semantics of the instruction, for example, the
2158 instruction is a conditional branch or updates some flags.
2159 We can't fix it unless GDB is able to emulate the instruction
2160 or switch to displaced stepping. */
2161 && !(bl->owner->type == bp_single_step
2162 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2163 {
1eb8556f
SM
2164 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2165 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2166 return 0;
2167 }
31e77af2 2168
963f9c80
PA
2169 /* Don't insert watchpoints if we're trying to step past the
2170 instruction that triggered one. */
2171 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2172 && stepping_past_nonsteppable_watchpoint ())
2173 {
1eb8556f 2174 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2175 "skipping watchpoint at %s:%d",
1eb8556f 2176 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2177 return 0;
2178 }
2179
74960c60
VP
2180 return 1;
2181}
2182
934709f0
PW
2183/* Same as should_be_inserted but does the check assuming
2184 that the location is not duplicated. */
2185
2186static int
2187unduplicated_should_be_inserted (struct bp_location *bl)
2188{
2189 int result;
2190 const int save_duplicate = bl->duplicate;
2191
2192 bl->duplicate = 0;
2193 result = should_be_inserted (bl);
2194 bl->duplicate = save_duplicate;
2195 return result;
2196}
2197
b775012e
LM
2198/* Parses a conditional described by an expression COND into an
2199 agent expression bytecode suitable for evaluation
2200 by the bytecode interpreter. Return NULL if there was
2201 any error during parsing. */
2202
833177a4 2203static agent_expr_up
b775012e
LM
2204parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2205{
833177a4 2206 if (cond == NULL)
b775012e
LM
2207 return NULL;
2208
833177a4
PA
2209 agent_expr_up aexpr;
2210
b775012e
LM
2211 /* We don't want to stop processing, so catch any errors
2212 that may show up. */
a70b8144 2213 try
b775012e 2214 {
036e657b 2215 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2216 }
2217
230d2906 2218 catch (const gdb_exception_error &ex)
b775012e
LM
2219 {
2220 /* If we got here, it means the condition could not be parsed to a valid
2221 bytecode expression and thus can't be evaluated on the target's side.
2222 It's no use iterating through the conditions. */
b775012e
LM
2223 }
2224
2225 /* We have a valid agent expression. */
2226 return aexpr;
2227}
2228
2229/* Based on location BL, create a list of breakpoint conditions to be
2230 passed on to the target. If we have duplicated locations with different
2231 conditions, we will add such conditions to the list. The idea is that the
2232 target will evaluate the list of conditions and will only notify GDB when
2233 one of them is true. */
2234
2235static void
2236build_target_condition_list (struct bp_location *bl)
2237{
b775012e
LM
2238 int null_condition_or_parse_error = 0;
2239 int modified = bl->needs_update;
b775012e 2240
8b4f3082 2241 /* Release conditions left over from a previous insert. */
3cde5c42 2242 bl->target_info.conditions.clear ();
8b4f3082 2243
b775012e
LM
2244 /* This is only meaningful if the target is
2245 evaluating conditions and if the user has
2246 opted for condition evaluation on the target's
2247 side. */
2248 if (gdb_evaluates_breakpoint_condition_p ()
2249 || !target_supports_evaluation_of_breakpoint_conditions ())
2250 return;
2251
e0d9a270
SM
2252 auto loc_range = all_bp_locations_at_addr (bl->address);
2253
b775012e 2254 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2255 conditions or conditions that fail to parse to a valid agent
2256 expression bytecode. If any of these happen, then it's no use to
2257 send conditions to the target since this location will always
2258 trigger and generate a response back to GDB. Note we consider
2259 all locations at the same address irrespective of type, i.e.,
2260 even if the locations aren't considered duplicates (e.g.,
2261 software breakpoint and hardware breakpoint at the same
2262 address). */
e0d9a270 2263 for (bp_location *loc : loc_range)
b775012e 2264 {
b775012e
LM
2265 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2266 {
2267 if (modified)
2268 {
b775012e
LM
2269 /* Re-parse the conditions since something changed. In that
2270 case we already freed the condition bytecodes (see
2271 force_breakpoint_reinsertion). We just
2272 need to parse the condition to bytecodes again. */
833177a4
PA
2273 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2274 loc->cond.get ());
b775012e
LM
2275 }
2276
2277 /* If we have a NULL bytecode expression, it means something
2278 went wrong or we have a null condition expression. */
2279 if (!loc->cond_bytecode)
2280 {
2281 null_condition_or_parse_error = 1;
2282 break;
2283 }
2284 }
2285 }
2286
2287 /* If any of these happened, it means we will have to evaluate the conditions
2288 for the location's address on gdb's side. It is no use keeping bytecodes
2289 for all the other duplicate locations, thus we free all of them here.
2290
2291 This is so we have a finer control over which locations' conditions are
2292 being evaluated by GDB or the remote stub. */
2293 if (null_condition_or_parse_error)
2294 {
e0d9a270 2295 for (bp_location *loc : loc_range)
b775012e 2296 {
b775012e
LM
2297 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2298 {
2299 /* Only go as far as the first NULL bytecode is
2300 located. */
2301 if (!loc->cond_bytecode)
2302 return;
2303
833177a4 2304 loc->cond_bytecode.reset ();
b775012e
LM
2305 }
2306 }
2307 }
2308
7f32a4d5
PA
2309 /* No NULL conditions or failed bytecode generation. Build a
2310 condition list for this location's address. If we have software
2311 and hardware locations at the same address, they aren't
2312 considered duplicates, but we still marge all the conditions
2313 anyway, as it's simpler, and doesn't really make a practical
2314 difference. */
e0d9a270
SM
2315 for (bp_location *loc : loc_range)
2316 if (loc->cond
2317 && is_breakpoint (loc->owner)
2318 && loc->pspace->num == bl->pspace->num
2319 && loc->owner->enable_state == bp_enabled
2320 && loc->enabled
2321 && !loc->disabled_by_cond)
2322 {
2323 /* Add the condition to the vector. This will be used later
2324 to send the conditions to the target. */
2325 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2326 }
b775012e
LM
2327
2328 return;
2329}
2330
d3ce09f5
SS
2331/* Parses a command described by string CMD into an agent expression
2332 bytecode suitable for evaluation by the bytecode interpreter.
2333 Return NULL if there was any error during parsing. */
2334
833177a4 2335static agent_expr_up
d3ce09f5
SS
2336parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2337{
bbc13ae3
KS
2338 const char *cmdrest;
2339 const char *format_start, *format_end;
d3ce09f5
SS
2340 struct gdbarch *gdbarch = get_current_arch ();
2341
833177a4 2342 if (cmd == NULL)
d3ce09f5
SS
2343 return NULL;
2344
2345 cmdrest = cmd;
2346
2347 if (*cmdrest == ',')
2348 ++cmdrest;
f1735a53 2349 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2350
2351 if (*cmdrest++ != '"')
2352 error (_("No format string following the location"));
2353
2354 format_start = cmdrest;
2355
8e481c3b 2356 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2357
2358 format_end = cmdrest;
2359
2360 if (*cmdrest++ != '"')
2361 error (_("Bad format string, non-terminated '\"'."));
2362
f1735a53 2363 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2364
2365 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2366 error (_("Invalid argument syntax"));
2367
2368 if (*cmdrest == ',')
2369 cmdrest++;
f1735a53 2370 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2371
2372 /* For each argument, make an expression. */
2373
8e481c3b 2374 std::vector<struct expression *> argvec;
d3ce09f5
SS
2375 while (*cmdrest != '\0')
2376 {
bbc13ae3 2377 const char *cmd1;
d3ce09f5
SS
2378
2379 cmd1 = cmdrest;
4d01a485 2380 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2381 argvec.push_back (expr.release ());
d3ce09f5
SS
2382 cmdrest = cmd1;
2383 if (*cmdrest == ',')
2384 ++cmdrest;
2385 }
2386
833177a4
PA
2387 agent_expr_up aexpr;
2388
d3ce09f5
SS
2389 /* We don't want to stop processing, so catch any errors
2390 that may show up. */
a70b8144 2391 try
d3ce09f5 2392 {
036e657b
JB
2393 aexpr = gen_printf (scope, gdbarch, 0, 0,
2394 format_start, format_end - format_start,
8e481c3b 2395 argvec.size (), argvec.data ());
d3ce09f5 2396 }
230d2906 2397 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2398 {
2399 /* If we got here, it means the command could not be parsed to a valid
2400 bytecode expression and thus can't be evaluated on the target's side.
2401 It's no use iterating through the other commands. */
d3ce09f5 2402 }
492d29ea 2403
d3ce09f5
SS
2404 /* We have a valid agent expression, return it. */
2405 return aexpr;
2406}
2407
2408/* Based on location BL, create a list of breakpoint commands to be
2409 passed on to the target. If we have duplicated locations with
2410 different commands, we will add any such to the list. */
2411
2412static void
2413build_target_command_list (struct bp_location *bl)
2414{
d3ce09f5
SS
2415 int null_command_or_parse_error = 0;
2416 int modified = bl->needs_update;
d3ce09f5 2417
3cde5c42
PA
2418 /* Clear commands left over from a previous insert. */
2419 bl->target_info.tcommands.clear ();
8b4f3082 2420
41fac0cf 2421 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2422 return;
2423
41fac0cf
PA
2424 /* For now, limit to agent-style dprintf breakpoints. */
2425 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2426 return;
2427
e0d9a270
SM
2428 auto loc_range = all_bp_locations_at_addr (bl->address);
2429
7f32a4d5
PA
2430 /* For now, if we have any location at the same address that isn't a
2431 dprintf, don't install the target-side commands, as that would
2432 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2433 control. */
e0d9a270
SM
2434 for (bp_location *loc : loc_range)
2435 if (is_breakpoint (loc->owner)
2436 && loc->pspace->num == bl->pspace->num
2437 && loc->owner->type != bp_dprintf)
2438 return;
41fac0cf 2439
d3ce09f5
SS
2440 /* Do a first pass to check for locations with no assigned
2441 conditions or conditions that fail to parse to a valid agent expression
2442 bytecode. If any of these happen, then it's no use to send conditions
2443 to the target since this location will always trigger and generate a
2444 response back to GDB. */
e0d9a270 2445 for (bp_location *loc : loc_range)
d3ce09f5 2446 {
d3ce09f5
SS
2447 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2448 {
2449 if (modified)
2450 {
d3ce09f5
SS
2451 /* Re-parse the commands since something changed. In that
2452 case we already freed the command bytecodes (see
2453 force_breakpoint_reinsertion). We just
2454 need to parse the command to bytecodes again. */
833177a4
PA
2455 loc->cmd_bytecode
2456 = parse_cmd_to_aexpr (bl->address,
2457 loc->owner->extra_string);
d3ce09f5
SS
2458 }
2459
2460 /* If we have a NULL bytecode expression, it means something
2461 went wrong or we have a null command expression. */
2462 if (!loc->cmd_bytecode)
2463 {
2464 null_command_or_parse_error = 1;
2465 break;
2466 }
2467 }
2468 }
2469
2470 /* If anything failed, then we're not doing target-side commands,
2471 and so clean up. */
2472 if (null_command_or_parse_error)
2473 {
e0d9a270
SM
2474 for (bp_location *loc : loc_range)
2475 if (is_breakpoint (loc->owner)
2476 && loc->pspace->num == bl->pspace->num)
2477 {
2478 /* Only go as far as the first NULL bytecode is
2479 located. */
2480 if (loc->cmd_bytecode == NULL)
2481 return;
d3ce09f5 2482
e0d9a270
SM
2483 loc->cmd_bytecode.reset ();
2484 }
d3ce09f5
SS
2485 }
2486
7f32a4d5
PA
2487 /* No NULL commands or failed bytecode generation. Build a command
2488 list for all duplicate locations at this location's address.
2489 Note that here we must care for whether the breakpoint location
2490 types are considered duplicates, otherwise, say, if we have a
2491 software and hardware location at the same address, the target
2492 could end up running the commands twice. For the moment, we only
2493 support targets-side commands with dprintf, but it doesn't hurt
2494 to be pedantically correct in case that changes. */
e0d9a270
SM
2495 for (bp_location *loc : loc_range)
2496 if (breakpoint_locations_match (bl, loc)
2497 && loc->owner->extra_string
2498 && is_breakpoint (loc->owner)
2499 && loc->pspace->num == bl->pspace->num
2500 && loc->owner->enable_state == bp_enabled
2501 && loc->enabled
2502 && !loc->disabled_by_cond)
2503 {
2504 /* Add the command to the vector. This will be used later
2505 to send the commands to the target. */
2506 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2507 }
d3ce09f5
SS
2508
2509 bl->target_info.persist = 0;
2510 /* Maybe flag this location as persistent. */
2511 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2512 bl->target_info.persist = 1;
2513}
2514
833b7ab5
YQ
2515/* Return the kind of breakpoint on address *ADDR. Get the kind
2516 of breakpoint according to ADDR except single-step breakpoint.
2517 Get the kind of single-step breakpoint according to the current
2518 registers state. */
cd6c3b4f
YQ
2519
2520static int
2521breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2522{
833b7ab5
YQ
2523 if (bl->owner->type == bp_single_step)
2524 {
2525 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2526 struct regcache *regcache;
2527
00431a78 2528 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2529
2530 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2531 regcache, addr);
2532 }
2533 else
2534 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2535}
2536
35df4500
TJB
2537/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2538 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2539 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2540 Returns 0 for success, 1 if the bp_location type is not supported or
2541 -1 for failure.
879bfdc2 2542
4a64f543
MS
2543 NOTE drow/2003-09-09: This routine could be broken down to an
2544 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2545static int
35df4500 2546insert_bp_location (struct bp_location *bl,
26bb91f3 2547 struct ui_file *tmp_error_stream,
3fbb6ffa 2548 int *disabled_breaks,
dd61ec5c
MW
2549 int *hw_breakpoint_error,
2550 int *hw_bp_error_explained_already)
879bfdc2 2551{
cc06b668 2552 gdb_exception bp_excpt;
879bfdc2 2553
b775012e 2554 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2555 return 0;
2556
35c63cd8
JB
2557 /* Note we don't initialize bl->target_info, as that wipes out
2558 the breakpoint location's shadow_contents if the breakpoint
2559 is still inserted at that location. This in turn breaks
2560 target_read_memory which depends on these buffers when
2561 a memory read is requested at the breakpoint location:
2562 Once the target_info has been wiped, we fail to see that
2563 we have a breakpoint inserted at that address and thus
2564 read the breakpoint instead of returning the data saved in
2565 the breakpoint location's shadow contents. */
0d5ed153 2566 bl->target_info.reqstd_address = bl->address;
35df4500 2567 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2568 bl->target_info.length = bl->length;
8181d85f 2569
b775012e
LM
2570 /* When working with target-side conditions, we must pass all the conditions
2571 for the same breakpoint address down to the target since GDB will not
2572 insert those locations. With a list of breakpoint conditions, the target
2573 can decide when to stop and notify GDB. */
2574
2575 if (is_breakpoint (bl->owner))
2576 {
2577 build_target_condition_list (bl);
d3ce09f5
SS
2578 build_target_command_list (bl);
2579 /* Reset the modification marker. */
b775012e
LM
2580 bl->needs_update = 0;
2581 }
2582
7f32a4d5
PA
2583 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2584 set at a read-only address, then a breakpoint location will have
2585 been changed to hardware breakpoint before we get here. If it is
2586 "off" however, error out before actually trying to insert the
2587 breakpoint, with a nicer error message. */
35df4500 2588 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2589 && !automatic_hardware_breakpoints)
879bfdc2 2590 {
7f32a4d5 2591 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2592
7f32a4d5
PA
2593 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2594 {
2595 fprintf_unfiltered (tmp_error_stream,
2596 _("Cannot insert breakpoint %d.\n"
2597 "Cannot set software breakpoint "
2598 "at read-only address %s\n"),
2599 bl->owner->number,
2600 paddress (bl->gdbarch, bl->address));
2601 return 1;
765dc015 2602 }
7f32a4d5
PA
2603 }
2604
2605 if (bl->loc_type == bp_loc_software_breakpoint
2606 || bl->loc_type == bp_loc_hardware_breakpoint)
2607 {
879bfdc2
DJ
2608 /* First check to see if we have to handle an overlay. */
2609 if (overlay_debugging == ovly_off
35df4500
TJB
2610 || bl->section == NULL
2611 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2612 {
2613 /* No overlay handling: just set the breakpoint. */
a70b8144 2614 try
dd61ec5c 2615 {
0000e5cc
PA
2616 int val;
2617
dd61ec5c 2618 val = bl->owner->ops->insert_location (bl);
0000e5cc 2619 if (val)
688fca4f 2620 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2621 }
94aeb44b 2622 catch (gdb_exception &e)
dd61ec5c 2623 {
94aeb44b 2624 bp_excpt = std::move (e);
dd61ec5c 2625 }
879bfdc2
DJ
2626 }
2627 else
2628 {
4a64f543 2629 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2630 Shall we set a breakpoint at the LMA? */
2631 if (!overlay_events_enabled)
2632 {
2633 /* Yes -- overlay event support is not active,
2634 so we must try to set a breakpoint at the LMA.
2635 This will not work for a hardware breakpoint. */
35df4500 2636 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2637 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2638 bl->owner->number);
879bfdc2
DJ
2639 else
2640 {
35df4500
TJB
2641 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2642 bl->section);
879bfdc2 2643 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2644 bl->overlay_target_info = bl->target_info;
0d5ed153 2645 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2646
2647 /* No overlay handling: just set the breakpoint. */
a70b8144 2648 try
0000e5cc
PA
2649 {
2650 int val;
2651
579c6ad9 2652 bl->overlay_target_info.kind
cd6c3b4f
YQ
2653 = breakpoint_kind (bl, &addr);
2654 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2655 val = target_insert_breakpoint (bl->gdbarch,
2656 &bl->overlay_target_info);
2657 if (val)
688fca4f
PA
2658 bp_excpt
2659 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2660 }
94aeb44b 2661 catch (gdb_exception &e)
0000e5cc 2662 {
94aeb44b 2663 bp_excpt = std::move (e);
0000e5cc
PA
2664 }
2665
688fca4f 2666 if (bp_excpt.reason != 0)
99361f52 2667 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2668 "Overlay breakpoint %d "
2669 "failed: in ROM?\n",
35df4500 2670 bl->owner->number);
879bfdc2
DJ
2671 }
2672 }
2673 /* Shall we set a breakpoint at the VMA? */
35df4500 2674 if (section_is_mapped (bl->section))
879bfdc2
DJ
2675 {
2676 /* Yes. This overlay section is mapped into memory. */
a70b8144 2677 try
dda83cd7 2678 {
0000e5cc
PA
2679 int val;
2680
dda83cd7 2681 val = bl->owner->ops->insert_location (bl);
0000e5cc 2682 if (val)
688fca4f 2683 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2684 }
94aeb44b 2685 catch (gdb_exception &e)
dda83cd7 2686 {
94aeb44b 2687 bp_excpt = std::move (e);
dda83cd7 2688 }
879bfdc2
DJ
2689 }
2690 else
2691 {
2692 /* No. This breakpoint will not be inserted.
2693 No error, but do not mark the bp as 'inserted'. */
2694 return 0;
2695 }
2696 }
2697
688fca4f 2698 if (bp_excpt.reason != 0)
879bfdc2
DJ
2699 {
2700 /* Can't set the breakpoint. */
0000e5cc 2701
80dc83fd
AB
2702 /* If the target has closed then it will have deleted any
2703 breakpoints inserted within the target inferior, as a result
2704 any further attempts to interact with the breakpoint objects
2705 is not possible. Just rethrow the error. */
2706 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2707 throw bp_excpt;
2708 gdb_assert (bl->owner != nullptr);
2709
0000e5cc
PA
2710 /* In some cases, we might not be able to insert a
2711 breakpoint in a shared library that has already been
2712 removed, but we have not yet processed the shlib unload
2713 event. Unfortunately, some targets that implement
076855f9
PA
2714 breakpoint insertion themselves can't tell why the
2715 breakpoint insertion failed (e.g., the remote target
2716 doesn't define error codes), so we must treat generic
2717 errors as memory errors. */
688fca4f
PA
2718 if (bp_excpt.reason == RETURN_ERROR
2719 && (bp_excpt.error == GENERIC_ERROR
2720 || bp_excpt.error == MEMORY_ERROR)
076855f9 2721 && bl->loc_type == bp_loc_software_breakpoint
08351840 2722 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2723 || shared_objfile_contains_address_p (bl->pspace,
2724 bl->address)))
879bfdc2 2725 {
4a64f543 2726 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2727 bl->shlib_disabled = 1;
76727919 2728 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2729 if (!*disabled_breaks)
2730 {
2731 fprintf_unfiltered (tmp_error_stream,
2732 "Cannot insert breakpoint %d.\n",
2733 bl->owner->number);
2734 fprintf_unfiltered (tmp_error_stream,
2735 "Temporarily disabling shared "
2736 "library breakpoints:\n");
2737 }
2738 *disabled_breaks = 1;
879bfdc2 2739 fprintf_unfiltered (tmp_error_stream,
35df4500 2740 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2741 return 0;
879bfdc2
DJ
2742 }
2743 else
879bfdc2 2744 {
35df4500 2745 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2746 {
0000e5cc 2747 *hw_breakpoint_error = 1;
688fca4f 2748 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2749 fprintf_unfiltered (tmp_error_stream,
2750 "Cannot insert hardware breakpoint %d%s",
2751 bl->owner->number,
688fca4f 2752 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2753 if (bp_excpt.message != NULL)
2754 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2755 bp_excpt.what ());
879bfdc2
DJ
2756 }
2757 else
2758 {
688fca4f 2759 if (bp_excpt.message == NULL)
0000e5cc 2760 {
1ccbe998 2761 std::string message
0000e5cc
PA
2762 = memory_error_message (TARGET_XFER_E_IO,
2763 bl->gdbarch, bl->address);
0000e5cc
PA
2764
2765 fprintf_unfiltered (tmp_error_stream,
2766 "Cannot insert breakpoint %d.\n"
2767 "%s\n",
1ccbe998 2768 bl->owner->number, message.c_str ());
0000e5cc
PA
2769 }
2770 else
2771 {
2772 fprintf_unfiltered (tmp_error_stream,
2773 "Cannot insert breakpoint %d: %s\n",
2774 bl->owner->number,
3d6e9d23 2775 bp_excpt.what ());
0000e5cc 2776 }
879bfdc2 2777 }
0000e5cc 2778 return 1;
879bfdc2
DJ
2779
2780 }
2781 }
2782 else
35df4500 2783 bl->inserted = 1;
879bfdc2 2784
0000e5cc 2785 return 0;
879bfdc2
DJ
2786 }
2787
35df4500 2788 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2789 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2790 watchpoints. It's not clear that it's necessary... */
35df4500 2791 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2792 {
0000e5cc
PA
2793 int val;
2794
77b06cd7
TJB
2795 gdb_assert (bl->owner->ops != NULL
2796 && bl->owner->ops->insert_location != NULL);
2797
2798 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2799
2800 /* If trying to set a read-watchpoint, and it turns out it's not
2801 supported, try emulating one with an access watchpoint. */
35df4500 2802 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2803 {
85d721b8
PA
2804 /* But don't try to insert it, if there's already another
2805 hw_access location that would be considered a duplicate
2806 of this one. */
48d7020b 2807 for (bp_location *loc : all_bp_locations ())
35df4500 2808 if (loc != bl
85d721b8 2809 && loc->watchpoint_type == hw_access
35df4500 2810 && watchpoint_locations_match (bl, loc))
85d721b8 2811 {
35df4500
TJB
2812 bl->duplicate = 1;
2813 bl->inserted = 1;
2814 bl->target_info = loc->target_info;
2815 bl->watchpoint_type = hw_access;
85d721b8
PA
2816 val = 0;
2817 break;
2818 }
2819
2820 if (val == 1)
2821 {
77b06cd7
TJB
2822 bl->watchpoint_type = hw_access;
2823 val = bl->owner->ops->insert_location (bl);
2824
2825 if (val)
2826 /* Back to the original value. */
2827 bl->watchpoint_type = hw_read;
85d721b8
PA
2828 }
2829 }
2830
35df4500 2831 bl->inserted = (val == 0);
879bfdc2
DJ
2832 }
2833
35df4500 2834 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2835 {
0000e5cc
PA
2836 int val;
2837
77b06cd7
TJB
2838 gdb_assert (bl->owner->ops != NULL
2839 && bl->owner->ops->insert_location != NULL);
2840
2841 val = bl->owner->ops->insert_location (bl);
2842 if (val)
2843 {
2844 bl->owner->enable_state = bp_disabled;
2845
2846 if (val == 1)
2847 warning (_("\
2848Error inserting catchpoint %d: Your system does not support this type\n\
2849of catchpoint."), bl->owner->number);
2850 else
2851 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2852 }
2853
2854 bl->inserted = (val == 0);
1640b821
DJ
2855
2856 /* We've already printed an error message if there was a problem
2857 inserting this catchpoint, and we've disabled the catchpoint,
2858 so just return success. */
2859 return 0;
879bfdc2
DJ
2860 }
2861
2862 return 0;
2863}
2864
6c95b8df
PA
2865/* This function is called when program space PSPACE is about to be
2866 deleted. It takes care of updating breakpoints to not reference
2867 PSPACE anymore. */
2868
2869void
2870breakpoint_program_space_exit (struct program_space *pspace)
2871{
6c95b8df 2872 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2873 for (breakpoint *b : all_breakpoints_safe ())
2874 if (b->pspace == pspace)
2875 delete_breakpoint (b);
6c95b8df
PA
2876
2877 /* Breakpoints set through other program spaces could have locations
2878 bound to PSPACE as well. Remove those. */
48d7020b 2879 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2880 {
2881 struct bp_location *tmp;
2882
2883 if (loc->pspace == pspace)
2884 {
2bdf28a0 2885 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2886 if (loc->owner->loc == loc)
2887 loc->owner->loc = loc->next;
2888 else
2889 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2890 if (tmp->next == loc)
2891 {
2892 tmp->next = loc->next;
2893 break;
2894 }
2895 }
2896 }
2897
2898 /* Now update the global location list to permanently delete the
2899 removed locations above. */
44702360 2900 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2901}
2902
74960c60
VP
2903/* Make sure all breakpoints are inserted in inferior.
2904 Throws exception on any error.
2905 A breakpoint that is already inserted won't be inserted
2906 again, so calling this function twice is safe. */
2907void
2908insert_breakpoints (void)
2909{
43892fdf 2910 for (breakpoint *bpt : all_breakpoints ())
74960c60 2911 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2912 {
2913 struct watchpoint *w = (struct watchpoint *) bpt;
2914
2915 update_watchpoint (w, 0 /* don't reparse. */);
2916 }
74960c60 2917
04086b45
PA
2918 /* Updating watchpoints creates new locations, so update the global
2919 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2920 ignore breakpoints_always_inserted_mode. Also,
2921 update_global_location_list tries to "upgrade" software
2922 breakpoints to hardware breakpoints to handle "set breakpoint
2923 auto-hw", so we need to call it even if we don't have new
2924 locations. */
04086b45 2925 update_global_location_list (UGLL_INSERT);
74960c60
VP
2926}
2927
b775012e
LM
2928/* This is used when we need to synch breakpoint conditions between GDB and the
2929 target. It is the case with deleting and disabling of breakpoints when using
2930 always-inserted mode. */
2931
2932static void
2933update_inserted_breakpoint_locations (void)
2934{
b775012e
LM
2935 int error_flag = 0;
2936 int val = 0;
2937 int disabled_breaks = 0;
2938 int hw_breakpoint_error = 0;
dd61ec5c 2939 int hw_bp_details_reported = 0;
b775012e 2940
d7e74731 2941 string_file tmp_error_stream;
b775012e
LM
2942
2943 /* Explicitly mark the warning -- this will only be printed if
2944 there was an error. */
d7e74731 2945 tmp_error_stream.puts ("Warning:\n");
b775012e 2946
5ed8105e 2947 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 2948
48d7020b 2949 for (bp_location *bl : all_bp_locations ())
b775012e
LM
2950 {
2951 /* We only want to update software breakpoints and hardware
2952 breakpoints. */
2953 if (!is_breakpoint (bl->owner))
2954 continue;
2955
2956 /* We only want to update locations that are already inserted
2957 and need updating. This is to avoid unwanted insertion during
2958 deletion of breakpoints. */
4daf1902 2959 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2960 continue;
2961
2962 switch_to_program_space_and_thread (bl->pspace);
2963
2964 /* For targets that support global breakpoints, there's no need
2965 to select an inferior to insert breakpoint to. In fact, even
2966 if we aren't attached to any process yet, we should still
2967 insert breakpoints. */
f5656ead 2968 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2969 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2970 continue;
2971
d7e74731 2972 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2973 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2974 if (val)
2975 error_flag = val;
2976 }
2977
2978 if (error_flag)
2979 {
223ffa71 2980 target_terminal::ours_for_output ();
b775012e
LM
2981 error_stream (tmp_error_stream);
2982 }
b775012e
LM
2983}
2984
c30eee59 2985/* Used when starting or continuing the program. */
c906108c 2986
74960c60
VP
2987static void
2988insert_breakpoint_locations (void)
c906108c 2989{
eacd795a 2990 int error_flag = 0;
c906108c 2991 int val = 0;
3fbb6ffa 2992 int disabled_breaks = 0;
81d0cc19 2993 int hw_breakpoint_error = 0;
dd61ec5c 2994 int hw_bp_error_explained_already = 0;
c906108c 2995
d7e74731
PA
2996 string_file tmp_error_stream;
2997
81d0cc19
GS
2998 /* Explicitly mark the warning -- this will only be printed if
2999 there was an error. */
d7e74731 3000 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3001
5ed8105e 3002 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3003
48d7020b 3004 for (bp_location *bl : all_bp_locations ())
879bfdc2 3005 {
b775012e 3006 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3007 continue;
3008
4a64f543
MS
3009 /* There is no point inserting thread-specific breakpoints if
3010 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3011 has BL->OWNER always non-NULL. */
35df4500 3012 if (bl->owner->thread != -1
5d5658a1 3013 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3014 continue;
3015
35df4500 3016 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3017
3018 /* For targets that support global breakpoints, there's no need
3019 to select an inferior to insert breakpoint to. In fact, even
3020 if we aren't attached to any process yet, we should still
3021 insert breakpoints. */
f5656ead 3022 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3023 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3024 continue;
3025
d7e74731 3026 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3027 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3028 if (val)
eacd795a 3029 error_flag = val;
879bfdc2 3030 }
c906108c 3031
4a64f543
MS
3032 /* If we failed to insert all locations of a watchpoint, remove
3033 them, as half-inserted watchpoint is of limited use. */
43892fdf 3034 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3035 {
3036 int some_failed = 0;
a5606eee
VP
3037
3038 if (!is_hardware_watchpoint (bpt))
3039 continue;
3040
d6b74ac4 3041 if (!breakpoint_enabled (bpt))
a5606eee 3042 continue;
74960c60
VP
3043
3044 if (bpt->disposition == disp_del_at_next_stop)
3045 continue;
a5606eee 3046
40cb8ca5 3047 for (bp_location *loc : bpt->locations ())
56710373 3048 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3049 {
3050 some_failed = 1;
3051 break;
3052 }
40cb8ca5 3053
a5606eee
VP
3054 if (some_failed)
3055 {
40cb8ca5 3056 for (bp_location *loc : bpt->locations ())
a5606eee 3057 if (loc->inserted)
834c0d03 3058 remove_breakpoint (loc);
a5606eee
VP
3059
3060 hw_breakpoint_error = 1;
d7e74731
PA
3061 tmp_error_stream.printf ("Could not insert "
3062 "hardware watchpoint %d.\n",
3063 bpt->number);
eacd795a 3064 error_flag = -1;
a5606eee
VP
3065 }
3066 }
3067
eacd795a 3068 if (error_flag)
81d0cc19
GS
3069 {
3070 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3071 message about possibly exhausted resources. */
dd61ec5c 3072 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3073 {
d7e74731 3074 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3075You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3076 }
223ffa71 3077 target_terminal::ours_for_output ();
81d0cc19
GS
3078 error_stream (tmp_error_stream);
3079 }
c906108c
SS
3080}
3081
c30eee59
TJB
3082/* Used when the program stops.
3083 Returns zero if successful, or non-zero if there was a problem
3084 removing a breakpoint location. */
3085
c906108c 3086int
fba45db2 3087remove_breakpoints (void)
c906108c 3088{
3a1bae8e 3089 int val = 0;
c906108c 3090
48d7020b 3091 for (bp_location *bl : all_bp_locations ())
1e4d1764 3092 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3093 val |= remove_breakpoint (bl);
01add95b 3094
3a1bae8e 3095 return val;
c906108c
SS
3096}
3097
49fa26b0
PA
3098/* When a thread exits, remove breakpoints that are related to
3099 that thread. */
3100
3101static void
3102remove_threaded_breakpoints (struct thread_info *tp, int silent)
3103{
1428b37a 3104 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3105 {
5d5658a1 3106 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3107 {
3108 b->disposition = disp_del_at_next_stop;
3109
3110 printf_filtered (_("\
43792cf0
PA
3111Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3112 b->number, print_thread_id (tp));
49fa26b0
PA
3113
3114 /* Hide it from the user. */
3115 b->number = 0;
3116 }
3117 }
3118}
3119
f3869b1a 3120/* See breakpoint.h. */
6c95b8df 3121
f3869b1a 3122void
00431a78 3123remove_breakpoints_inf (inferior *inf)
6c95b8df 3124{
6c95b8df 3125 int val;
6c95b8df 3126
48d7020b 3127 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3128 {
3129 if (bl->pspace != inf->pspace)
3130 continue;
6c95b8df 3131
01add95b
SM
3132 if (bl->inserted && !bl->target_info.persist)
3133 {
3134 val = remove_breakpoint (bl);
3135 if (val != 0)
3136 return;
3137 }
3138 }
6c95b8df
PA
3139}
3140
e58b0e63
PA
3141static int internal_breakpoint_number = -1;
3142
84f4c1fe
PM
3143/* Set the breakpoint number of B, depending on the value of INTERNAL.
3144 If INTERNAL is non-zero, the breakpoint number will be populated
3145 from internal_breakpoint_number and that variable decremented.
e5dd4106 3146 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3147 breakpoint_count and that value incremented. Internal breakpoints
3148 do not set the internal var bpnum. */
3149static void
3150set_breakpoint_number (int internal, struct breakpoint *b)
3151{
3152 if (internal)
3153 b->number = internal_breakpoint_number--;
3154 else
3155 {
3156 set_breakpoint_count (breakpoint_count + 1);
3157 b->number = breakpoint_count;
3158 }
3159}
3160
e62c965a 3161static struct breakpoint *
a6d9a66e 3162create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3163 CORE_ADDR address, enum bptype type,
c0a91b2b 3164 const struct breakpoint_ops *ops)
e62c965a 3165{
51abb421 3166 symtab_and_line sal;
e62c965a
PP
3167 sal.pc = address;
3168 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3169 sal.pspace = current_program_space;
e62c965a 3170
51abb421 3171 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3172 b->number = internal_breakpoint_number--;
3173 b->disposition = disp_donttouch;
3174
3175 return b;
3176}
3177
17450429
PP
3178static const char *const longjmp_names[] =
3179 {
3180 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3181 };
3182#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3183
3184/* Per-objfile data private to breakpoint.c. */
3185struct breakpoint_objfile_data
3186{
3187 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3188 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3189
3190 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3191 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3192
28106bc2 3193 /* True if we have looked for longjmp probes. */
43dce439 3194 int longjmp_searched = 0;
28106bc2 3195
45461e0d
SM
3196 /* SystemTap probe points for longjmp (if any). These are non-owning
3197 references. */
3198 std::vector<probe *> longjmp_probes;
28106bc2 3199
17450429 3200 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3201 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3202
3203 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3204 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3205
3206 /* True if we have looked for exception probes. */
43dce439 3207 int exception_searched = 0;
28106bc2 3208
45461e0d
SM
3209 /* SystemTap probe points for unwinding (if any). These are non-owning
3210 references. */
3211 std::vector<probe *> exception_probes;
17450429
PP
3212};
3213
51d3063a
TT
3214static const struct objfile_key<breakpoint_objfile_data>
3215 breakpoint_objfile_key;
17450429
PP
3216
3217/* Minimal symbol not found sentinel. */
3218static struct minimal_symbol msym_not_found;
3219
3220/* Returns TRUE if MSYM point to the "not found" sentinel. */
3221
3222static int
3223msym_not_found_p (const struct minimal_symbol *msym)
3224{
3225 return msym == &msym_not_found;
3226}
3227
3228/* Return per-objfile data needed by breakpoint.c.
3229 Allocate the data if necessary. */
3230
3231static struct breakpoint_objfile_data *
3232get_breakpoint_objfile_data (struct objfile *objfile)
3233{
3234 struct breakpoint_objfile_data *bp_objfile_data;
3235
51d3063a 3236 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3237 if (bp_objfile_data == NULL)
51d3063a 3238 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3239 return bp_objfile_data;
3240}
3241
e62c965a 3242static void
af02033e 3243create_overlay_event_breakpoint (void)
e62c965a 3244{
af02033e 3245 const char *const func_name = "_ovly_debug_event";
e62c965a 3246
2030c079 3247 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3248 {
3249 struct breakpoint *b;
17450429
PP
3250 struct breakpoint_objfile_data *bp_objfile_data;
3251 CORE_ADDR addr;
67994074 3252 struct explicit_location explicit_loc;
69de3c6a 3253
17450429
PP
3254 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3255
3b7344d5 3256 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3257 continue;
3258
3b7344d5 3259 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3260 {
3b7344d5 3261 struct bound_minimal_symbol m;
17450429
PP
3262
3263 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3264 if (m.minsym == NULL)
17450429
PP
3265 {
3266 /* Avoid future lookups in this objfile. */
3b7344d5 3267 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3268 continue;
3269 }
3270 bp_objfile_data->overlay_msym = m;
3271 }
e62c965a 3272
77e371c0 3273 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3274 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3275 bp_overlay_event,
06edf0c0 3276 &internal_breakpoint_ops);
67994074
KS
3277 initialize_explicit_location (&explicit_loc);
3278 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3279 b->location = new_explicit_location (&explicit_loc);
e62c965a 3280
69de3c6a 3281 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3282 {
3283 b->enable_state = bp_enabled;
3284 overlay_events_enabled = 1;
3285 }
69de3c6a
PP
3286 else
3287 {
dda83cd7
SM
3288 b->enable_state = bp_disabled;
3289 overlay_events_enabled = 0;
69de3c6a 3290 }
e62c965a 3291 }
e62c965a
PP
3292}
3293
2a7f6487
TV
3294/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3295 true if a breakpoint was installed. */
3296
3297static bool
3298create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3299{
2a7f6487
TV
3300 struct gdbarch *gdbarch = objfile->arch ();
3301 struct breakpoint_objfile_data *bp_objfile_data
3302 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3303
2a7f6487 3304 if (!bp_objfile_data->longjmp_searched)
94c93c35 3305 {
2a7f6487
TV
3306 std::vector<probe *> ret
3307 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3308
2a7f6487 3309 if (!ret.empty ())
94c93c35 3310 {
2a7f6487
TV
3311 /* We are only interested in checking one element. */
3312 probe *p = ret[0];
0fd8e87f 3313
2a7f6487
TV
3314 if (!p->can_evaluate_arguments ())
3315 {
3316 /* We cannot use the probe interface here,
3317 because it does not know how to evaluate
3318 arguments. */
3319 ret.clear ();
3320 }
3321 }
3322 bp_objfile_data->longjmp_probes = ret;
3323 bp_objfile_data->longjmp_searched = 1;
3324 }
0fd8e87f 3325
2a7f6487
TV
3326 if (bp_objfile_data->longjmp_probes.empty ())
3327 return false;
17450429 3328
2a7f6487
TV
3329 for (probe *p : bp_objfile_data->longjmp_probes)
3330 {
3331 struct breakpoint *b;
25f9533e 3332
2a7f6487
TV
3333 b = create_internal_breakpoint (gdbarch,
3334 p->get_relocated_address (objfile),
3335 bp_longjmp_master,
3336 &internal_breakpoint_ops);
3337 b->location = new_probe_location ("-probe-stap libc:longjmp");
3338 b->enable_state = bp_disabled;
3339 }
aed57c53 3340
2a7f6487
TV
3341 return true;
3342}
25f9533e 3343
2a7f6487
TV
3344/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3345 Return true if at least one breakpoint was installed. */
3346
3347static bool
3348create_longjmp_master_breakpoint_names (objfile *objfile)
3349{
3350 struct gdbarch *gdbarch = objfile->arch ();
3351 if (!gdbarch_get_longjmp_target_p (gdbarch))
3352 return false;
3353
3354 struct breakpoint_objfile_data *bp_objfile_data
3355 = get_breakpoint_objfile_data (objfile);
3356 unsigned int installed_bp = 0;
3357
3358 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3359 {
3360 struct breakpoint *b;
3361 const char *func_name;
3362 CORE_ADDR addr;
3363 struct explicit_location explicit_loc;
3364
3365 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3366 continue;
28106bc2 3367
2a7f6487
TV
3368 func_name = longjmp_names[i];
3369 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3370 {
3371 struct bound_minimal_symbol m;
3372
3373 m = lookup_minimal_symbol_text (func_name, objfile);
3374 if (m.minsym == NULL)
3375 {
3376 /* Prevent future lookups in this objfile. */
3377 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3378 continue;
3379 }
2a7f6487
TV
3380 bp_objfile_data->longjmp_msym[i] = m;
3381 }
28106bc2 3382
2a7f6487
TV
3383 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3384 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3385 &internal_breakpoint_ops);
3386 initialize_explicit_location (&explicit_loc);
3387 explicit_loc.function_name = ASTRDUP (func_name);
3388 b->location = new_explicit_location (&explicit_loc);
3389 b->enable_state = bp_disabled;
3390 installed_bp++;
3391 }
28106bc2 3392
2a7f6487
TV
3393 return installed_bp > 0;
3394}
0fd8e87f 3395
2a7f6487 3396/* Create a master longjmp breakpoint. */
17450429 3397
2a7f6487
TV
3398static void
3399create_longjmp_master_breakpoint (void)
3400{
3401 scoped_restore_current_program_space restore_pspace;
aed57c53 3402
2a7f6487
TV
3403 for (struct program_space *pspace : program_spaces)
3404 {
3405 set_current_program_space (pspace);
17450429 3406
2a7f6487
TV
3407 for (objfile *obj : current_program_space->objfiles ())
3408 {
3409 /* Skip separate debug object, it's handled in the loop below. */
3410 if (obj->separate_debug_objfile_backlink != nullptr)
3411 continue;
3412
3413 /* Try a probe kind breakpoint on main objfile. */
3414 if (create_longjmp_master_breakpoint_probe (obj))
3415 continue;
3416
3417 /* Try longjmp_names kind breakpoints on main and separate_debug
3418 objfiles. */
3419 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3420 if (create_longjmp_master_breakpoint_names (debug_objfile))
3421 break;
94c93c35
TT
3422 }
3423 }
0fd8e87f
UW
3424}
3425
af02033e 3426/* Create a master std::terminate breakpoint. */
aa7d318d 3427static void
af02033e 3428create_std_terminate_master_breakpoint (void)
aa7d318d 3429{
af02033e 3430 const char *const func_name = "std::terminate()";
aa7d318d 3431
5ed8105e 3432 scoped_restore_current_program_space restore_pspace;
aa7d318d 3433
94c93c35
TT
3434 for (struct program_space *pspace : program_spaces)
3435 {
3436 CORE_ADDR addr;
17450429 3437
94c93c35 3438 set_current_program_space (pspace);
17450429 3439
94c93c35
TT
3440 for (objfile *objfile : current_program_space->objfiles ())
3441 {
3442 struct breakpoint *b;
3443 struct breakpoint_objfile_data *bp_objfile_data;
3444 struct explicit_location explicit_loc;
aa7d318d 3445
94c93c35 3446 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3447
94c93c35
TT
3448 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3449 continue;
17450429 3450
94c93c35
TT
3451 if (bp_objfile_data->terminate_msym.minsym == NULL)
3452 {
3453 struct bound_minimal_symbol m;
17450429 3454
94c93c35
TT
3455 m = lookup_minimal_symbol (func_name, NULL, objfile);
3456 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3457 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3458 {
3459 /* Prevent future lookups in this objfile. */
3460 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3461 continue;
3462 }
3463 bp_objfile_data->terminate_msym = m;
3464 }
aa7d318d 3465
94c93c35
TT
3466 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3467 b = create_internal_breakpoint (objfile->arch (), addr,
3468 bp_std_terminate_master,
3469 &internal_breakpoint_ops);
3470 initialize_explicit_location (&explicit_loc);
3471 explicit_loc.function_name = ASTRDUP (func_name);
3472 b->location = new_explicit_location (&explicit_loc);
3473 b->enable_state = bp_disabled;
3474 }
3475 }
aa7d318d
TT
3476}
3477
1940319c
TV
3478/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3479 probe. Return true if a breakpoint was installed. */
186c406b 3480
1940319c
TV
3481static bool
3482create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3483{
1940319c
TV
3484 struct breakpoint *b;
3485 struct gdbarch *gdbarch;
3486 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3487
1940319c 3488 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3489
1940319c
TV
3490 /* We prefer the SystemTap probe point if it exists. */
3491 if (!bp_objfile_data->exception_searched)
3492 {
3493 std::vector<probe *> ret
3494 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3495
1940319c 3496 if (!ret.empty ())
28106bc2 3497 {
1940319c
TV
3498 /* We are only interested in checking one element. */
3499 probe *p = ret[0];
25f9533e 3500
1940319c 3501 if (!p->can_evaluate_arguments ())
25f9533e 3502 {
1940319c
TV
3503 /* We cannot use the probe interface here, because it does
3504 not know how to evaluate arguments. */
3505 ret.clear ();
25f9533e 3506 }
28106bc2 3507 }
1940319c
TV
3508 bp_objfile_data->exception_probes = ret;
3509 bp_objfile_data->exception_searched = 1;
3510 }
28106bc2 3511
1940319c
TV
3512 if (bp_objfile_data->exception_probes.empty ())
3513 return false;
45461e0d 3514
1940319c 3515 gdbarch = objfile->arch ();
28106bc2 3516
1940319c
TV
3517 for (probe *p : bp_objfile_data->exception_probes)
3518 {
3519 b = create_internal_breakpoint (gdbarch,
3520 p->get_relocated_address (objfile),
3521 bp_exception_master,
3522 &internal_breakpoint_ops);
3523 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3524 b->enable_state = bp_disabled;
3525 }
28106bc2 3526
1940319c
TV
3527 return true;
3528}
28106bc2 3529
1940319c
TV
3530/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3531 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3532
1940319c
TV
3533static bool
3534create_exception_master_breakpoint_hook (objfile *objfile)
3535{
3536 const char *const func_name = "_Unwind_DebugHook";
3537 struct breakpoint *b;
3538 struct gdbarch *gdbarch;
3539 struct breakpoint_objfile_data *bp_objfile_data;
3540 CORE_ADDR addr;
3541 struct explicit_location explicit_loc;
186c406b 3542
1940319c 3543 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3544
1940319c
TV
3545 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3546 return false;
17450429 3547
1940319c
TV
3548 gdbarch = objfile->arch ();
3549
3550 if (bp_objfile_data->exception_msym.minsym == NULL)
3551 {
3552 struct bound_minimal_symbol debug_hook;
3553
3554 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3555 if (debug_hook.minsym == NULL)
3556 {
3557 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3558 return false;
186c406b 3559 }
17450429 3560
1940319c
TV
3561 bp_objfile_data->exception_msym = debug_hook;
3562 }
3563
3564 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3565 addr = gdbarch_convert_from_func_ptr_addr
3566 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3567 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3568 &internal_breakpoint_ops);
3569 initialize_explicit_location (&explicit_loc);
3570 explicit_loc.function_name = ASTRDUP (func_name);
3571 b->location = new_explicit_location (&explicit_loc);
3572 b->enable_state = bp_disabled;
3573
3574 return true;
3575}
3576
3577/* Install a master breakpoint on the unwinder's debug hook. */
3578
3579static void
3580create_exception_master_breakpoint (void)
3581{
3582 for (objfile *obj : current_program_space->objfiles ())
3583 {
3584 /* Skip separate debug object. */
3585 if (obj->separate_debug_objfile_backlink)
3586 continue;
3587
3588 /* Try a probe kind breakpoint. */
3589 if (create_exception_master_breakpoint_probe (obj))
3590 continue;
3591
7c6944ab
TV
3592 /* Iterate over main and separate debug objects and try an
3593 _Unwind_DebugHook kind breakpoint. */
3594 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3595 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3596 break;
186c406b 3597 }
186c406b
TT
3598}
3599
9ef9e6a6
KS
3600/* Does B have a location spec? */
3601
3602static int
3603breakpoint_event_location_empty_p (const struct breakpoint *b)
3604{
d28cd78a 3605 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3606}
3607
c906108c 3608void
fba45db2 3609update_breakpoints_after_exec (void)
c906108c 3610{
25b22b0a
PA
3611 /* We're about to delete breakpoints from GDB's lists. If the
3612 INSERTED flag is true, GDB will try to lift the breakpoints by
3613 writing the breakpoints' "shadow contents" back into memory. The
3614 "shadow contents" are NOT valid after an exec, so GDB should not
3615 do that. Instead, the target is responsible from marking
3616 breakpoints out as soon as it detects an exec. We don't do that
3617 here instead, because there may be other attempts to delete
3618 breakpoints after detecting an exec and before reaching here. */
48d7020b 3619 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3620 if (bploc->pspace == current_program_space)
3621 gdb_assert (!bploc->inserted);
c906108c 3622
1428b37a 3623 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3624 {
3625 if (b->pspace != current_program_space)
c5aa993b 3626 continue;
c906108c 3627
01add95b
SM
3628 /* Solib breakpoints must be explicitly reset after an exec(). */
3629 if (b->type == bp_shlib_event)
3630 {
3631 delete_breakpoint (b);
3632 continue;
3633 }
4efc6507 3634
01add95b
SM
3635 /* JIT breakpoints must be explicitly reset after an exec(). */
3636 if (b->type == bp_jit_event)
3637 {
3638 delete_breakpoint (b);
3639 continue;
3640 }
c4093a6a 3641
01add95b
SM
3642 /* Thread event breakpoints must be set anew after an exec(),
3643 as must overlay event and longjmp master breakpoints. */
3644 if (b->type == bp_thread_event || b->type == bp_overlay_event
3645 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3646 || b->type == bp_exception_master)
3647 {
3648 delete_breakpoint (b);
3649 continue;
3650 }
c5aa993b 3651
01add95b
SM
3652 /* Step-resume breakpoints are meaningless after an exec(). */
3653 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3654 {
3655 delete_breakpoint (b);
3656 continue;
3657 }
7c16b83e 3658
01add95b
SM
3659 /* Just like single-step breakpoints. */
3660 if (b->type == bp_single_step)
3661 {
3662 delete_breakpoint (b);
3663 continue;
3664 }
611c83ae 3665
01add95b
SM
3666 /* Longjmp and longjmp-resume breakpoints are also meaningless
3667 after an exec. */
3668 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3669 || b->type == bp_longjmp_call_dummy
3670 || b->type == bp_exception || b->type == bp_exception_resume)
3671 {
3672 delete_breakpoint (b);
3673 continue;
3674 }
ce78b96d 3675
01add95b
SM
3676 if (b->type == bp_catchpoint)
3677 {
3678 /* For now, none of the bp_catchpoint breakpoints need to
3679 do anything at this point. In the future, if some of
3680 the catchpoints need to something, we will need to add
3681 a new method, and call this method from here. */
3682 continue;
3683 }
c5aa993b 3684
01add95b
SM
3685 /* bp_finish is a special case. The only way we ought to be able
3686 to see one of these when an exec() has happened, is if the user
3687 caught a vfork, and then said "finish". Ordinarily a finish just
3688 carries them to the call-site of the current callee, by setting
3689 a temporary bp there and resuming. But in this case, the finish
3690 will carry them entirely through the vfork & exec.
3691
3692 We don't want to allow a bp_finish to remain inserted now. But
3693 we can't safely delete it, 'cause finish_command has a handle to
3694 the bp on a bpstat, and will later want to delete it. There's a
3695 chance (and I've seen it happen) that if we delete the bp_finish
3696 here, that its storage will get reused by the time finish_command
3697 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3698 We really must allow finish_command to delete a bp_finish.
3699
3700 In the absence of a general solution for the "how do we know
3701 it's safe to delete something others may have handles to?"
3702 problem, what we'll do here is just uninsert the bp_finish, and
3703 let finish_command delete it.
3704
3705 (We know the bp_finish is "doomed" in the sense that it's
3706 momentary, and will be deleted as soon as finish_command sees
3707 the inferior stopped. So it doesn't matter that the bp's
3708 address is probably bogus in the new a.out, unlike e.g., the
3709 solib breakpoints.) */
3710
3711 if (b->type == bp_finish)
3712 {
3713 continue;
3714 }
3715
3716 /* Without a symbolic address, we have little hope of the
3717 pre-exec() address meaning the same thing in the post-exec()
3718 a.out. */
3719 if (breakpoint_event_location_empty_p (b))
3720 {
3721 delete_breakpoint (b);
3722 continue;
3723 }
3724 }
c906108c
SS
3725}
3726
3727int
d80ee84f 3728detach_breakpoints (ptid_t ptid)
c906108c 3729{
3a1bae8e 3730 int val = 0;
2989a365 3731 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3732 struct inferior *inf = current_inferior ();
c5aa993b 3733
e99b03dc 3734 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3735 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3736
6c95b8df 3737 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3738 inferior_ptid = ptid;
48d7020b 3739 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3740 {
3741 if (bl->pspace != inf->pspace)
3742 continue;
3743
3744 /* This function must physically remove breakpoints locations
3745 from the specified ptid, without modifying the breakpoint
3746 package's state. Locations of type bp_loc_other are only
3747 maintained at GDB side. So, there is no need to remove
3748 these bp_loc_other locations. Moreover, removing these
3749 would modify the breakpoint package's state. */
3750 if (bl->loc_type == bp_loc_other)
3751 continue;
3752
3753 if (bl->inserted)
3754 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3755 }
d03285ec 3756
3a1bae8e 3757 return val;
c906108c
SS
3758}
3759
35df4500 3760/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3761 Note that this is used to detach breakpoints from a child fork.
3762 When we get here, the child isn't in the inferior list, and neither
3763 do we have objects to represent its address space --- we should
35df4500 3764 *not* look at bl->pspace->aspace here. */
6c95b8df 3765
c906108c 3766static int
b2b6a7da 3767remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3768{
3769 int val;
c5aa993b 3770
35df4500
TJB
3771 /* BL is never in moribund_locations by our callers. */
3772 gdb_assert (bl->owner != NULL);
2bdf28a0 3773
74960c60
VP
3774 /* The type of none suggests that owner is actually deleted.
3775 This should not ever happen. */
35df4500 3776 gdb_assert (bl->owner->type != bp_none);
0bde7532 3777
35df4500
TJB
3778 if (bl->loc_type == bp_loc_software_breakpoint
3779 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3780 {
c02f5703
MS
3781 /* "Normal" instruction breakpoint: either the standard
3782 trap-instruction bp (bp_breakpoint), or a
3783 bp_hardware_breakpoint. */
3784
3785 /* First check to see if we have to handle an overlay. */
3786 if (overlay_debugging == ovly_off
35df4500
TJB
3787 || bl->section == NULL
3788 || !(section_is_overlay (bl->section)))
c02f5703
MS
3789 {
3790 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3791
3792 /* If we're trying to uninsert a memory breakpoint that we
3793 know is set in a dynamic object that is marked
3794 shlib_disabled, then either the dynamic object was
3795 removed with "remove-symbol-file" or with
3796 "nosharedlibrary". In the former case, we don't know
3797 whether another dynamic object might have loaded over the
3798 breakpoint's address -- the user might well let us know
3799 about it next with add-symbol-file (the whole point of
d03de421 3800 add-symbol-file is letting the user manually maintain a
08351840
PA
3801 list of dynamically loaded objects). If we have the
3802 breakpoint's shadow memory, that is, this is a software
3803 breakpoint managed by GDB, check whether the breakpoint
3804 is still inserted in memory, to avoid overwriting wrong
3805 code with stale saved shadow contents. Note that HW
3806 breakpoints don't have shadow memory, as they're
3807 implemented using a mechanism that is not dependent on
3808 being able to modify the target's memory, and as such
3809 they should always be removed. */
3810 if (bl->shlib_disabled
3811 && bl->target_info.shadow_len != 0
3812 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3813 val = 0;
3814 else
73971819 3815 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3816 }
c906108c
SS
3817 else
3818 {
4a64f543 3819 /* This breakpoint is in an overlay section.
c02f5703
MS
3820 Did we set a breakpoint at the LMA? */
3821 if (!overlay_events_enabled)
3822 {
3823 /* Yes -- overlay event support is not active, so we
3824 should have set a breakpoint at the LMA. Remove it.
3825 */
c02f5703
MS
3826 /* Ignore any failures: if the LMA is in ROM, we will
3827 have already warned when we failed to insert it. */
35df4500
TJB
3828 if (bl->loc_type == bp_loc_hardware_breakpoint)
3829 target_remove_hw_breakpoint (bl->gdbarch,
3830 &bl->overlay_target_info);
c02f5703 3831 else
35df4500 3832 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3833 &bl->overlay_target_info,
3834 reason);
c02f5703
MS
3835 }
3836 /* Did we set a breakpoint at the VMA?
3837 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3838 if (bl->inserted)
c906108c 3839 {
c02f5703
MS
3840 /* Yes -- remove it. Previously we did not bother to
3841 remove the breakpoint if the section had been
3842 unmapped, but let's not rely on that being safe. We
3843 don't know what the overlay manager might do. */
aa67235e
UW
3844
3845 /* However, we should remove *software* breakpoints only
3846 if the section is still mapped, or else we overwrite
3847 wrong code with the saved shadow contents. */
348d480f
PA
3848 if (bl->loc_type == bp_loc_hardware_breakpoint
3849 || section_is_mapped (bl->section))
73971819 3850 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3851 else
3852 val = 0;
c906108c 3853 }
c02f5703
MS
3854 else
3855 {
3856 /* No -- not inserted, so no need to remove. No error. */
3857 val = 0;
3858 }
c906108c 3859 }
879d1e6b 3860
08351840
PA
3861 /* In some cases, we might not be able to remove a breakpoint in
3862 a shared library that has already been removed, but we have
3863 not yet processed the shlib unload event. Similarly for an
3864 unloaded add-symbol-file object - the user might not yet have
3865 had the chance to remove-symbol-file it. shlib_disabled will
3866 be set if the library/object has already been removed, but
3867 the breakpoint hasn't been uninserted yet, e.g., after
3868 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3869 always-inserted mode. */
076855f9 3870 if (val
08351840
PA
3871 && (bl->loc_type == bp_loc_software_breakpoint
3872 && (bl->shlib_disabled
3873 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3874 || shared_objfile_contains_address_p (bl->pspace,
3875 bl->address))))
879d1e6b
UW
3876 val = 0;
3877
c906108c
SS
3878 if (val)
3879 return val;
b2b6a7da 3880 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3881 }
35df4500 3882 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3883 {
77b06cd7
TJB
3884 gdb_assert (bl->owner->ops != NULL
3885 && bl->owner->ops->remove_location != NULL);
3886
b2b6a7da 3887 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3888 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3889
c906108c 3890 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3891 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3892 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3893 bl->owner->number);
c906108c 3894 }
35df4500 3895 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3896 && breakpoint_enabled (bl->owner)
3897 && !bl->duplicate)
ce78b96d 3898 {
77b06cd7
TJB
3899 gdb_assert (bl->owner->ops != NULL
3900 && bl->owner->ops->remove_location != NULL);
ce78b96d 3901
73971819 3902 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3903 if (val)
3904 return val;
77b06cd7 3905
b2b6a7da 3906 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3907 }
c906108c
SS
3908
3909 return 0;
3910}
3911
6c95b8df 3912static int
834c0d03 3913remove_breakpoint (struct bp_location *bl)
6c95b8df 3914{
35df4500
TJB
3915 /* BL is never in moribund_locations by our callers. */
3916 gdb_assert (bl->owner != NULL);
2bdf28a0 3917
6c95b8df
PA
3918 /* The type of none suggests that owner is actually deleted.
3919 This should not ever happen. */
35df4500 3920 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3921
5ed8105e 3922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3923
35df4500 3924 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3925
5ed8105e 3926 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3927}
3928
c906108c
SS
3929/* Clear the "inserted" flag in all breakpoints. */
3930
25b22b0a 3931void
fba45db2 3932mark_breakpoints_out (void)
c906108c 3933{
48d7020b 3934 for (bp_location *bl : all_bp_locations ())
66c4b3e8 3935 if (bl->pspace == current_program_space)
35df4500 3936 bl->inserted = 0;
c906108c
SS
3937}
3938
53a5351d
JM
3939/* Clear the "inserted" flag in all breakpoints and delete any
3940 breakpoints which should go away between runs of the program.
c906108c
SS
3941
3942 Plus other such housekeeping that has to be done for breakpoints
3943 between runs.
3944
53a5351d
JM
3945 Note: this function gets called at the end of a run (by
3946 generic_mourn_inferior) and when a run begins (by
4a64f543 3947 init_wait_for_inferior). */
c906108c
SS
3948
3949
3950
3951void
fba45db2 3952breakpoint_init_inferior (enum inf_context context)
c906108c 3953{
6c95b8df 3954 struct program_space *pspace = current_program_space;
c906108c 3955
50c71eaf
PA
3956 /* If breakpoint locations are shared across processes, then there's
3957 nothing to do. */
f5656ead 3958 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3959 return;
3960
1a853c52 3961 mark_breakpoints_out ();
075f6582 3962
1428b37a 3963 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3964 {
3965 if (b->loc && b->loc->pspace != pspace)
3966 continue;
6c95b8df 3967
01add95b
SM
3968 switch (b->type)
3969 {
3970 case bp_call_dummy:
3971 case bp_longjmp_call_dummy:
c906108c 3972
01add95b
SM
3973 /* If the call dummy breakpoint is at the entry point it will
3974 cause problems when the inferior is rerun, so we better get
3975 rid of it. */
ab92d69b 3976
01add95b 3977 case bp_watchpoint_scope:
ab92d69b 3978
01add95b 3979 /* Also get rid of scope breakpoints. */
ab92d69b 3980
01add95b 3981 case bp_shlib_event:
ab92d69b 3982
01add95b
SM
3983 /* Also remove solib event breakpoints. Their addresses may
3984 have changed since the last time we ran the program.
3985 Actually we may now be debugging against different target;
3986 and so the solib backend that installed this breakpoint may
3987 not be used in by the target. E.g.,
ab92d69b 3988
01add95b
SM
3989 (gdb) file prog-linux
3990 (gdb) run # native linux target
3991 ...
3992 (gdb) kill
3993 (gdb) file prog-win.exe
3994 (gdb) tar rem :9999 # remote Windows gdbserver.
3995 */
c906108c 3996
01add95b 3997 case bp_step_resume:
f59f708a 3998
01add95b 3999 /* Also remove step-resume breakpoints. */
f59f708a 4000
01add95b 4001 case bp_single_step:
7c16b83e 4002
01add95b 4003 /* Also remove single-step breakpoints. */
7c16b83e 4004
01add95b
SM
4005 delete_breakpoint (b);
4006 break;
c906108c 4007
01add95b
SM
4008 case bp_watchpoint:
4009 case bp_hardware_watchpoint:
4010 case bp_read_watchpoint:
4011 case bp_access_watchpoint:
4012 {
4013 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4014
01add95b
SM
4015 /* Likewise for watchpoints on local expressions. */
4016 if (w->exp_valid_block != NULL)
4017 delete_breakpoint (b);
4018 else
4019 {
4020 /* Get rid of existing locations, which are no longer
4021 valid. New ones will be created in
4022 update_watchpoint, when the inferior is restarted.
4023 The next update_global_location_list call will
4024 garbage collect them. */
4025 b->loc = NULL;
4026
4027 if (context == inf_starting)
4028 {
4029 /* Reset val field to force reread of starting value in
4030 insert_breakpoints. */
4031 w->val.reset (nullptr);
4032 w->val_valid = false;
4033 }
4034 }
4035 }
4036 break;
4037 default:
4038 break;
3a5c3e22 4039 }
01add95b 4040 }
1c5cfe86
PA
4041
4042 /* Get rid of the moribund locations. */
1123588c 4043 for (bp_location *bl : moribund_locations)
35df4500 4044 decref_bp_location (&bl);
1123588c 4045 moribund_locations.clear ();
c906108c
SS
4046}
4047
6c95b8df
PA
4048/* These functions concern about actual breakpoints inserted in the
4049 target --- to e.g. check if we need to do decr_pc adjustment or if
4050 we need to hop over the bkpt --- so we check for address space
4051 match, not program space. */
4052
c2c6d25f
JM
4053/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4054 exists at PC. It returns ordinary_breakpoint_here if it's an
4055 ordinary breakpoint, or permanent_breakpoint_here if it's a
4056 permanent breakpoint.
4057 - When continuing from a location with an ordinary breakpoint, we
4058 actually single step once before calling insert_breakpoints.
e5dd4106 4059 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4060 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4061 the target, to advance the PC past the breakpoint. */
c906108c 4062
c2c6d25f 4063enum breakpoint_here
accd0bcd 4064breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4065{
c2c6d25f 4066 int any_breakpoint_here = 0;
c906108c 4067
48d7020b 4068 for (bp_location *bl : all_bp_locations ())
075f6582 4069 {
35df4500
TJB
4070 if (bl->loc_type != bp_loc_software_breakpoint
4071 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4072 continue;
4073
f1310107 4074 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4075 if ((breakpoint_enabled (bl->owner)
1a853c52 4076 || bl->permanent)
f1310107 4077 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4078 {
4079 if (overlay_debugging
35df4500
TJB
4080 && section_is_overlay (bl->section)
4081 && !section_is_mapped (bl->section))
075f6582 4082 continue; /* unmapped overlay -- can't be a match */
1a853c52 4083 else if (bl->permanent)
075f6582
DJ
4084 return permanent_breakpoint_here;
4085 else
4086 any_breakpoint_here = 1;
4087 }
4088 }
c906108c 4089
f486487f 4090 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4091}
4092
d35ae833
PA
4093/* See breakpoint.h. */
4094
4095int
accd0bcd 4096breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4097 CORE_ADDR addr, ULONGEST len)
4098{
48d7020b 4099 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4100 {
4101 if (bl->loc_type != bp_loc_software_breakpoint
4102 && bl->loc_type != bp_loc_hardware_breakpoint)
4103 continue;
4104
4105 if ((breakpoint_enabled (bl->owner)
4106 || bl->permanent)
4107 && breakpoint_location_address_range_overlap (bl, aspace,
4108 addr, len))
4109 {
4110 if (overlay_debugging
4111 && section_is_overlay (bl->section)
4112 && !section_is_mapped (bl->section))
4113 {
4114 /* Unmapped overlay -- can't be a match. */
4115 continue;
4116 }
4117
4118 return 1;
4119 }
4120 }
4121
4122 return 0;
4123}
4124
1c5cfe86
PA
4125/* Return true if there's a moribund breakpoint at PC. */
4126
4127int
accd0bcd 4128moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4129{
1123588c 4130 for (bp_location *loc : moribund_locations)
f1310107 4131 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4132 return 1;
4133
4134 return 0;
4135}
c2c6d25f 4136
f7ce857f
PA
4137/* Returns non-zero iff BL is inserted at PC, in address space
4138 ASPACE. */
4139
4140static int
4141bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4142 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4143{
4144 if (bl->inserted
4145 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4146 aspace, pc))
4147 {
4148 if (overlay_debugging
4149 && section_is_overlay (bl->section)
4150 && !section_is_mapped (bl->section))
4151 return 0; /* unmapped overlay -- can't be a match */
4152 else
4153 return 1;
4154 }
4155 return 0;
4156}
4157
a1fd2fa5 4158/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4159
4160int
accd0bcd 4161breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4162{
e0d9a270 4163 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4164 {
35df4500
TJB
4165 if (bl->loc_type != bp_loc_software_breakpoint
4166 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4167 continue;
4168
f7ce857f
PA
4169 if (bp_location_inserted_here_p (bl, aspace, pc))
4170 return 1;
c5aa993b 4171 }
c36b740a
VP
4172 return 0;
4173}
4174
a1fd2fa5
PA
4175/* This function returns non-zero iff there is a software breakpoint
4176 inserted at PC. */
c36b740a
VP
4177
4178int
accd0bcd 4179software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4180 CORE_ADDR pc)
4fa8626c 4181{
e0d9a270 4182 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4183 {
35df4500 4184 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4185 continue;
4186
f7ce857f
PA
4187 if (bp_location_inserted_here_p (bl, aspace, pc))
4188 return 1;
4fa8626c
DJ
4189 }
4190
4191 return 0;
9c02b525
PA
4192}
4193
4194/* See breakpoint.h. */
4195
4196int
accd0bcd 4197hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4198 CORE_ADDR pc)
4199{
e0d9a270 4200 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4201 {
9c02b525
PA
4202 if (bl->loc_type != bp_loc_hardware_breakpoint)
4203 continue;
4204
4205 if (bp_location_inserted_here_p (bl, aspace, pc))
4206 return 1;
4207 }
4208
4209 return 0;
4fa8626c
DJ
4210}
4211
9093389c 4212int
accd0bcd 4213hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4214 CORE_ADDR addr, ULONGEST len)
4215{
43892fdf 4216 for (breakpoint *bpt : all_breakpoints ())
9093389c 4217 {
9093389c
PA
4218 if (bpt->type != bp_hardware_watchpoint
4219 && bpt->type != bp_access_watchpoint)
4220 continue;
4221
4222 if (!breakpoint_enabled (bpt))
4223 continue;
4224
40cb8ca5 4225 for (bp_location *loc : bpt->locations ())
9093389c
PA
4226 if (loc->pspace->aspace == aspace && loc->inserted)
4227 {
4228 CORE_ADDR l, h;
4229
4230 /* Check for intersection. */
768adc05
PA
4231 l = std::max<CORE_ADDR> (loc->address, addr);
4232 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4233 if (l < h)
4234 return 1;
4235 }
4236 }
4237 return 0;
4238}
c5aa993b 4239
f2478a7e 4240/* See breakpoint.h. */
c906108c 4241
f2478a7e
SM
4242bool
4243is_catchpoint (struct breakpoint *b)
c906108c 4244{
f2478a7e 4245 return (b->type == bp_catchpoint);
c906108c
SS
4246}
4247
c906108c
SS
4248/* Clear a bpstat so that it says we are not at any breakpoint.
4249 Also free any storage that is part of a bpstat. */
4250
4251void
fba45db2 4252bpstat_clear (bpstat *bsp)
c906108c
SS
4253{
4254 bpstat p;
4255 bpstat q;
4256
4257 if (bsp == 0)
4258 return;
4259 p = *bsp;
4260 while (p != NULL)
4261 {
4262 q = p->next;
04afa70c 4263 delete p;
c906108c
SS
4264 p = q;
4265 }
4266 *bsp = NULL;
4267}
4268
04afa70c
TT
4269bpstats::bpstats (const bpstats &other)
4270 : next (NULL),
4271 bp_location_at (other.bp_location_at),
4272 breakpoint_at (other.breakpoint_at),
4273 commands (other.commands),
04afa70c
TT
4274 print (other.print),
4275 stop (other.stop),
4276 print_it (other.print_it)
4277{
850645cf
TT
4278 if (other.old_val != NULL)
4279 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4280}
4281
c906108c
SS
4282/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4283 is part of the bpstat is copied as well. */
4284
4285bpstat
fba45db2 4286bpstat_copy (bpstat bs)
c906108c
SS
4287{
4288 bpstat p = NULL;
4289 bpstat tmp;
4290 bpstat retval = NULL;
4291
4292 if (bs == NULL)
4293 return bs;
4294
4295 for (; bs != NULL; bs = bs->next)
4296 {
04afa70c 4297 tmp = new bpstats (*bs);
31cc81e9 4298
c906108c
SS
4299 if (p == NULL)
4300 /* This is the first thing in the chain. */
4301 retval = tmp;
4302 else
4303 p->next = tmp;
4304 p = tmp;
4305 }
4306 p->next = NULL;
4307 return retval;
4308}
4309
4a64f543 4310/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4311
4312bpstat
fba45db2 4313bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4314{
c5aa993b
JM
4315 if (bsp == NULL)
4316 return NULL;
c906108c 4317
c5aa993b
JM
4318 for (; bsp != NULL; bsp = bsp->next)
4319 {
f431efe5 4320 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4321 return bsp;
4322 }
c906108c
SS
4323 return NULL;
4324}
4325
ab04a2af
TT
4326/* See breakpoint.h. */
4327
4c462cb0 4328bool
427cd150 4329bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4330{
ab04a2af
TT
4331 for (; bsp != NULL; bsp = bsp->next)
4332 {
427cd150
TT
4333 if (bsp->breakpoint_at == NULL)
4334 {
4335 /* A moribund location can never explain a signal other than
4336 GDB_SIGNAL_TRAP. */
4337 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4338 return true;
427cd150
TT
4339 }
4340 else
47591c29
PA
4341 {
4342 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4343 sig))
4c462cb0 4344 return true;
47591c29 4345 }
ab04a2af
TT
4346 }
4347
4c462cb0 4348 return false;
ab04a2af
TT
4349}
4350
4a64f543
MS
4351/* Put in *NUM the breakpoint number of the first breakpoint we are
4352 stopped at. *BSP upon return is a bpstat which points to the
4353 remaining breakpoints stopped at (but which is not guaranteed to be
4354 good for anything but further calls to bpstat_num).
4355
8671a17b
PA
4356 Return 0 if passed a bpstat which does not indicate any breakpoints.
4357 Return -1 if stopped at a breakpoint that has been deleted since
4358 we set it.
4359 Return 1 otherwise. */
c906108c
SS
4360
4361int
8671a17b 4362bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4363{
4364 struct breakpoint *b;
4365
4366 if ((*bsp) == NULL)
4367 return 0; /* No more breakpoint values */
8671a17b 4368
4a64f543
MS
4369 /* We assume we'll never have several bpstats that correspond to a
4370 single breakpoint -- otherwise, this function might return the
4371 same number more than once and this will look ugly. */
f431efe5 4372 b = (*bsp)->breakpoint_at;
8671a17b
PA
4373 *bsp = (*bsp)->next;
4374 if (b == NULL)
4375 return -1; /* breakpoint that's been deleted since */
4376
4377 *num = b->number; /* We have its number */
4378 return 1;
c906108c
SS
4379}
4380
e93ca019 4381/* See breakpoint.h. */
c906108c
SS
4382
4383void
e93ca019 4384bpstat_clear_actions (void)
c906108c 4385{
e93ca019
JK
4386 bpstat bs;
4387
00431a78 4388 if (inferior_ptid == null_ptid)
e93ca019
JK
4389 return;
4390
00431a78 4391 thread_info *tp = inferior_thread ();
e93ca019 4392 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4393 {
d1b0a7bf 4394 bs->commands = NULL;
850645cf 4395 bs->old_val.reset (nullptr);
c906108c
SS
4396 }
4397}
4398
f3b1572e
PA
4399/* Called when a command is about to proceed the inferior. */
4400
4401static void
4402breakpoint_about_to_proceed (void)
4403{
d7e15655 4404 if (inferior_ptid != null_ptid)
f3b1572e
PA
4405 {
4406 struct thread_info *tp = inferior_thread ();
4407
4408 /* Allow inferior function calls in breakpoint commands to not
4409 interrupt the command list. When the call finishes
4410 successfully, the inferior will be standing at the same
4411 breakpoint as if nothing happened. */
16c381f0 4412 if (tp->control.in_infcall)
f3b1572e
PA
4413 return;
4414 }
4415
4416 breakpoint_proceeded = 1;
4417}
4418
abf85f46
JK
4419/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4420 or its equivalent. */
4421
4422static int
4423command_line_is_silent (struct command_line *cmd)
4424{
4f45d445 4425 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4426}
4427
4a64f543
MS
4428/* Execute all the commands associated with all the breakpoints at
4429 this location. Any of these commands could cause the process to
4430 proceed beyond this point, etc. We look out for such changes by
4431 checking the global "breakpoint_proceeded" after each command.
c906108c 4432
347bddb7
PA
4433 Returns true if a breakpoint command resumed the inferior. In that
4434 case, it is the caller's responsibility to recall it again with the
4435 bpstat of the current thread. */
4436
4437static int
4438bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4439{
4440 bpstat bs;
347bddb7 4441 int again = 0;
c906108c
SS
4442
4443 /* Avoid endless recursion if a `source' command is contained
4444 in bs->commands. */
4445 if (executing_breakpoint_commands)
347bddb7 4446 return 0;
c906108c 4447
81b1e71c
TT
4448 scoped_restore save_executing
4449 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4450
1ac32117 4451 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4452
4a64f543 4453 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4454 bs = *bsp;
4455
4456 breakpoint_proceeded = 0;
4457 for (; bs != NULL; bs = bs->next)
4458 {
d1b0a7bf 4459 struct command_line *cmd = NULL;
6c50ab1c
JB
4460
4461 /* Take ownership of the BSP's command tree, if it has one.
4462
dda83cd7
SM
4463 The command tree could legitimately contain commands like
4464 'step' and 'next', which call clear_proceed_status, which
4465 frees stop_bpstat's command tree. To make sure this doesn't
4466 free the tree we're executing out from under us, we need to
4467 take ownership of the tree ourselves. Since a given bpstat's
4468 commands are only executed once, we don't need to copy it; we
4469 can clear the pointer in the bpstat, and make sure we free
4470 the tree when we're done. */
d1b0a7bf 4471 counted_command_line ccmd = bs->commands;
9add0f1b 4472 bs->commands = NULL;
d1b0a7bf
TT
4473 if (ccmd != NULL)
4474 cmd = ccmd.get ();
abf85f46
JK
4475 if (command_line_is_silent (cmd))
4476 {
4477 /* The action has been already done by bpstat_stop_status. */
4478 cmd = cmd->next;
4479 }
6c50ab1c 4480
c906108c
SS
4481 while (cmd != NULL)
4482 {
4483 execute_control_command (cmd);
4484
4485 if (breakpoint_proceeded)
4486 break;
4487 else
4488 cmd = cmd->next;
4489 }
6c50ab1c 4490
c906108c 4491 if (breakpoint_proceeded)
32c1e744 4492 {
cb814510 4493 if (current_ui->async)
347bddb7
PA
4494 /* If we are in async mode, then the target might be still
4495 running, not stopped at any breakpoint, so nothing for
4496 us to do here -- just return to the event loop. */
4497 ;
32c1e744
VP
4498 else
4499 /* In sync mode, when execute_control_command returns
4500 we're already standing on the next breakpoint.
347bddb7
PA
4501 Breakpoint commands for that stop were not run, since
4502 execute_command does not run breakpoint commands --
4503 only command_line_handler does, but that one is not
4504 involved in execution of breakpoint commands. So, we
4505 can now execute breakpoint commands. It should be
4506 noted that making execute_command do bpstat actions is
4507 not an option -- in this case we'll have recursive
4508 invocation of bpstat for each breakpoint with a
4509 command, and can easily blow up GDB stack. Instead, we
4510 return true, which will trigger the caller to recall us
4511 with the new stop_bpstat. */
4512 again = 1;
4513 break;
32c1e744 4514 }
c906108c 4515 }
347bddb7
PA
4516 return again;
4517}
4518
00431a78
PA
4519/* Helper for bpstat_do_actions. Get the current thread, if there's
4520 one, is alive and has execution. Return NULL otherwise. */
4521
4522static thread_info *
4523get_bpstat_thread ()
4524{
55f6301a 4525 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4526 return NULL;
4527
4528 thread_info *tp = inferior_thread ();
4529 if (tp->state == THREAD_EXITED || tp->executing)
4530 return NULL;
4531 return tp;
4532}
4533
347bddb7
PA
4534void
4535bpstat_do_actions (void)
4536{
694c6bf5 4537 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4538 thread_info *tp;
353d1d73 4539
347bddb7 4540 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4541 while ((tp = get_bpstat_thread ()) != NULL)
4542 {
4543 /* Since in sync mode, bpstat_do_actions may resume the
4544 inferior, and only return when it is stopped at the next
4545 breakpoint, we keep doing breakpoint actions until it returns
4546 false to indicate the inferior was not resumed. */
4547 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4548 break;
4549 }
353d1d73 4550
694c6bf5 4551 cleanup_if_error.release ();
c906108c
SS
4552}
4553
fa4727a6
DJ
4554/* Print out the (old or new) value associated with a watchpoint. */
4555
4556static void
4557watchpoint_value_print (struct value *val, struct ui_file *stream)
4558{
4559 if (val == NULL)
7f6aba03 4560 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4561 else
79a45b7d
TT
4562 {
4563 struct value_print_options opts;
4564 get_user_print_options (&opts);
4565 value_print (val, stream, &opts);
4566 }
fa4727a6
DJ
4567}
4568
f303dbd6
PA
4569/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4570 debugging multiple threads. */
4571
4572void
4573maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4574{
112e8700 4575 if (uiout->is_mi_like_p ())
f303dbd6
PA
4576 return;
4577
112e8700 4578 uiout->text ("\n");
f303dbd6
PA
4579
4580 if (show_thread_that_caused_stop ())
4581 {
4582 const char *name;
4583 struct thread_info *thr = inferior_thread ();
4584
112e8700 4585 uiout->text ("Thread ");
33eca680 4586 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4587
4588 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4589 if (name != NULL)
4590 {
112e8700 4591 uiout->text (" \"");
33eca680 4592 uiout->field_string ("name", name);
112e8700 4593 uiout->text ("\"");
f303dbd6
PA
4594 }
4595
112e8700 4596 uiout->text (" hit ");
f303dbd6
PA
4597 }
4598}
4599
e514a9d6 4600/* Generic routine for printing messages indicating why we
4a64f543 4601 stopped. The behavior of this function depends on the value
e514a9d6
JM
4602 'print_it' in the bpstat structure. Under some circumstances we
4603 may decide not to print anything here and delegate the task to
4a64f543 4604 normal_stop(). */
e514a9d6
JM
4605
4606static enum print_stop_action
4607print_bp_stop_message (bpstat bs)
4608{
4609 switch (bs->print_it)
4610 {
4611 case print_it_noop:
4a64f543 4612 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4613 return PRINT_UNKNOWN;
4614 break;
4615
4616 case print_it_done:
4617 /* We still want to print the frame, but we already printed the
dda83cd7 4618 relevant messages. */
e514a9d6
JM
4619 return PRINT_SRC_AND_LOC;
4620 break;
4621
4622 case print_it_normal:
4f8d1dc6 4623 {
f431efe5
PA
4624 struct breakpoint *b = bs->breakpoint_at;
4625
1a6a67de
TJB
4626 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4627 which has since been deleted. */
4628 if (b == NULL)
4629 return PRINT_UNKNOWN;
4630
348d480f
PA
4631 /* Normal case. Call the breakpoint's print_it method. */
4632 return b->ops->print_it (bs);
4f8d1dc6 4633 }
348d480f 4634 break;
3086aeae 4635
e514a9d6 4636 default:
8e65ff28 4637 internal_error (__FILE__, __LINE__,
e2e0b3e5 4638 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4639 break;
c906108c 4640 }
c906108c
SS
4641}
4642
edcc5120
TT
4643/* A helper function that prints a shared library stopped event. */
4644
4645static void
4646print_solib_event (int is_catchpoint)
4647{
6fb16ce6 4648 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4649 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4650
4651 if (!is_catchpoint)
4652 {
4653 if (any_added || any_deleted)
112e8700 4654 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4655 else
112e8700
SM
4656 current_uiout->text (_("Stopped due to shared library event (no "
4657 "libraries added or removed)\n"));
edcc5120
TT
4658 }
4659
112e8700
SM
4660 if (current_uiout->is_mi_like_p ())
4661 current_uiout->field_string ("reason",
4662 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4663
4664 if (any_deleted)
4665 {
112e8700 4666 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4667 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4668 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4669 {
6fb16ce6
SM
4670 const std::string &name = current_program_space->deleted_solibs[ix];
4671
edcc5120 4672 if (ix > 0)
112e8700
SM
4673 current_uiout->text (" ");
4674 current_uiout->field_string ("library", name);
4675 current_uiout->text ("\n");
edcc5120 4676 }
edcc5120
TT
4677 }
4678
4679 if (any_added)
4680 {
112e8700 4681 current_uiout->text (_(" Inferior loaded "));
10f489e5 4682 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4683 bool first = true;
52941706 4684 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4685 {
bcb430e4 4686 if (!first)
112e8700 4687 current_uiout->text (" ");
bcb430e4 4688 first = false;
112e8700
SM
4689 current_uiout->field_string ("library", iter->so_name);
4690 current_uiout->text ("\n");
edcc5120 4691 }
edcc5120
TT
4692 }
4693}
4694
e514a9d6
JM
4695/* Print a message indicating what happened. This is called from
4696 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4697 list - a list of the eventpoints that caused this stop. KIND is
4698 the target_waitkind for the stopping event. This
e514a9d6
JM
4699 routine calls the generic print routine for printing a message
4700 about reasons for stopping. This will print (for example) the
4701 "Breakpoint n," part of the output. The return value of this
4702 routine is one of:
c906108c 4703
4a64f543 4704 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4705 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4706 code to print the location. An example is
c5aa993b
JM
4707 "Breakpoint 1, " which should be followed by
4708 the location.
917317f4 4709 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4710 to also print the location part of the message.
4711 An example is the catch/throw messages, which
4a64f543 4712 don't require a location appended to the end.
917317f4 4713 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4714 further info to be printed. */
c906108c 4715
917317f4 4716enum print_stop_action
36dfb11c 4717bpstat_print (bpstat bs, int kind)
c906108c 4718{
f486487f 4719 enum print_stop_action val;
c5aa993b 4720
c906108c 4721 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4722 (Currently all watchpoints go on the bpstat whether hit or not.
4723 That probably could (should) be changed, provided care is taken
c906108c 4724 with respect to bpstat_explains_signal). */
e514a9d6
JM
4725 for (; bs; bs = bs->next)
4726 {
4727 val = print_bp_stop_message (bs);
4728 if (val == PRINT_SRC_ONLY
4729 || val == PRINT_SRC_AND_LOC
4730 || val == PRINT_NOTHING)
4731 return val;
4732 }
c906108c 4733
36dfb11c
TT
4734 /* If we had hit a shared library event breakpoint,
4735 print_bp_stop_message would print out this message. If we hit an
4736 OS-level shared library event, do the same thing. */
4737 if (kind == TARGET_WAITKIND_LOADED)
4738 {
edcc5120 4739 print_solib_event (0);
36dfb11c
TT
4740 return PRINT_NOTHING;
4741 }
4742
e514a9d6 4743 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4744 with and nothing was printed. */
917317f4 4745 return PRINT_UNKNOWN;
c906108c
SS
4746}
4747
bf469271 4748/* Evaluate the boolean expression EXP and return the result. */
c906108c 4749
bf469271
PA
4750static bool
4751breakpoint_cond_eval (expression *exp)
c906108c 4752{
278cd55f 4753 struct value *mark = value_mark ();
bf469271 4754 bool res = value_true (evaluate_expression (exp));
cc59ec59 4755
c906108c 4756 value_free_to_mark (mark);
bf469271 4757 return res;
c906108c
SS
4758}
4759
5760d0ab 4760/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4761
04afa70c
TT
4762bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4763 : next (NULL),
b6433ede 4764 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4765 breakpoint_at (bl->owner),
4766 commands (NULL),
04afa70c
TT
4767 print (0),
4768 stop (0),
4769 print_it (print_it_normal)
c906108c 4770{
04afa70c
TT
4771 **bs_link_pointer = this;
4772 *bs_link_pointer = &next;
4773}
4774
4775bpstats::bpstats ()
4776 : next (NULL),
04afa70c
TT
4777 breakpoint_at (NULL),
4778 commands (NULL),
04afa70c
TT
4779 print (0),
4780 stop (0),
4781 print_it (print_it_normal)
4782{
c906108c
SS
4783}
4784\f
d983da9c
DJ
4785/* The target has stopped with waitstatus WS. Check if any hardware
4786 watchpoints have triggered, according to the target. */
4787
4788int
4789watchpoints_triggered (struct target_waitstatus *ws)
4790{
57810aa7 4791 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4792 CORE_ADDR addr;
d983da9c
DJ
4793
4794 if (!stopped_by_watchpoint)
4795 {
4796 /* We were not stopped by a watchpoint. Mark all watchpoints
4797 as not triggered. */
43892fdf 4798 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4799 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4800 {
4801 struct watchpoint *w = (struct watchpoint *) b;
4802
4803 w->watchpoint_triggered = watch_triggered_no;
4804 }
d983da9c
DJ
4805
4806 return 0;
4807 }
4808
328d42d8 4809 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4810 {
4811 /* We were stopped by a watchpoint, but we don't know where.
4812 Mark all watchpoints as unknown. */
43892fdf 4813 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4814 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4815 {
4816 struct watchpoint *w = (struct watchpoint *) b;
4817
4818 w->watchpoint_triggered = watch_triggered_unknown;
4819 }
d983da9c 4820
3c4797ba 4821 return 1;
d983da9c
DJ
4822 }
4823
4824 /* The target could report the data address. Mark watchpoints
4825 affected by this data address as triggered, and all others as not
4826 triggered. */
4827
43892fdf 4828 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4829 if (is_hardware_watchpoint (b))
d983da9c 4830 {
3a5c3e22 4831 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4832
3a5c3e22 4833 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4834 for (bp_location *loc : b->locations ())
9c06b0b4 4835 {
3a5c3e22 4836 if (is_masked_watchpoint (b))
9c06b0b4 4837 {
3a5c3e22
PA
4838 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4839 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4840
4841 if (newaddr == start)
4842 {
3a5c3e22 4843 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4844 break;
4845 }
4846 }
4847 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4848 else if (target_watchpoint_addr_within_range
4849 (current_inferior ()->top_target (), addr, loc->address,
4850 loc->length))
9c06b0b4 4851 {
3a5c3e22 4852 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4853 break;
4854 }
4855 }
d983da9c
DJ
4856 }
4857
4858 return 1;
4859}
4860
bf469271
PA
4861/* Possible return values for watchpoint_check. */
4862enum wp_check_result
4863 {
4864 /* The watchpoint has been deleted. */
4865 WP_DELETED = 1,
4866
4867 /* The value has changed. */
4868 WP_VALUE_CHANGED = 2,
4869
4870 /* The value has not changed. */
4871 WP_VALUE_NOT_CHANGED = 3,
4872
4873 /* Ignore this watchpoint, no matter if the value changed or not. */
4874 WP_IGNORE = 4,
4875 };
c906108c
SS
4876
4877#define BP_TEMPFLAG 1
4878#define BP_HARDWAREFLAG 2
4879
4a64f543 4880/* Evaluate watchpoint condition expression and check if its value
bf469271 4881 changed. */
553e4c11 4882
bf469271
PA
4883static wp_check_result
4884watchpoint_check (bpstat bs)
c906108c 4885{
3a5c3e22 4886 struct watchpoint *b;
c906108c
SS
4887 struct frame_info *fr;
4888 int within_current_scope;
4889
f431efe5 4890 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4891 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4892 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4893
f6bc2008
PA
4894 /* If this is a local watchpoint, we only want to check if the
4895 watchpoint frame is in scope if the current thread is the thread
4896 that was used to create the watchpoint. */
4897 if (!watchpoint_in_thread_scope (b))
60e1c644 4898 return WP_IGNORE;
f6bc2008 4899
c906108c
SS
4900 if (b->exp_valid_block == NULL)
4901 within_current_scope = 1;
4902 else
4903 {
edb3359d
DJ
4904 struct frame_info *frame = get_current_frame ();
4905 struct gdbarch *frame_arch = get_frame_arch (frame);
4906 CORE_ADDR frame_pc = get_frame_pc (frame);
4907
c9cf6e20 4908 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4909 still in the function but the stack frame has already been
4910 invalidated. Since we can't rely on the values of local
4911 variables after the stack has been destroyed, we are treating
4912 the watchpoint in that state as `not changed' without further
4913 checking. Don't mark watchpoints as changed if the current
4914 frame is in an epilogue - even if they are in some other
4915 frame, our view of the stack is likely to be wrong and
4916 frame_find_by_id could error out. */
c9cf6e20 4917 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4918 return WP_IGNORE;
a0f49112 4919
101dcfbe 4920 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4921 within_current_scope = (fr != NULL);
69fbadd5
DJ
4922
4923 /* If we've gotten confused in the unwinder, we might have
4924 returned a frame that can't describe this variable. */
edb3359d
DJ
4925 if (within_current_scope)
4926 {
4927 struct symbol *function;
4928
4929 function = get_frame_function (fr);
4930 if (function == NULL
4931 || !contained_in (b->exp_valid_block,
4932 SYMBOL_BLOCK_VALUE (function)))
4933 within_current_scope = 0;
4934 }
69fbadd5 4935
edb3359d 4936 if (within_current_scope)
c906108c
SS
4937 /* If we end up stopping, the current frame will get selected
4938 in normal_stop. So this call to select_frame won't affect
4939 the user. */
0f7d239c 4940 select_frame (fr);
c906108c 4941 }
c5aa993b 4942
c906108c
SS
4943 if (within_current_scope)
4944 {
4a64f543 4945 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
4946 time before we return to the command level and call
4947 free_all_values. We can't call free_all_values because we
4948 might be in the middle of evaluating a function call. */
c906108c 4949
9c06b0b4 4950 struct value *mark;
fa4727a6
DJ
4951 struct value *new_val;
4952
c1fc2657 4953 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4954 /* Since we don't know the exact trigger address (from
4955 stopped_data_address), just tell the user we've triggered
4956 a mask watchpoint. */
4957 return WP_VALUE_CHANGED;
4958
4959 mark = value_mark ();
1eaebe02 4960 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 4961 NULL, NULL, false);
218d2fc6 4962
bb9d5f81
PP
4963 if (b->val_bitsize != 0)
4964 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4965
4a64f543
MS
4966 /* We use value_equal_contents instead of value_equal because
4967 the latter coerces an array to a pointer, thus comparing just
4968 the address of the array instead of its contents. This is
4969 not what we want. */
fa4727a6 4970 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4971 || (b->val != NULL && !value_equal_contents (b->val.get (),
4972 new_val)))
c906108c 4973 {
c906108c 4974 bs->old_val = b->val;
850645cf 4975 b->val = release_value (new_val);
4c1d86d9 4976 b->val_valid = true;
850645cf
TT
4977 if (new_val != NULL)
4978 value_free_to_mark (mark);
c906108c
SS
4979 return WP_VALUE_CHANGED;
4980 }
4981 else
4982 {
60e1c644 4983 /* Nothing changed. */
c906108c 4984 value_free_to_mark (mark);
c906108c
SS
4985 return WP_VALUE_NOT_CHANGED;
4986 }
4987 }
4988 else
4989 {
4990 /* This seems like the only logical thing to do because
dda83cd7
SM
4991 if we temporarily ignored the watchpoint, then when
4992 we reenter the block in which it is valid it contains
4993 garbage (in the case of a function, it may have two
4994 garbage values, one before and one after the prologue).
4995 So we can't even detect the first assignment to it and
4996 watch after that (since the garbage may or may not equal
4997 the first value assigned). */
348d480f
PA
4998 /* We print all the stop information in
4999 breakpoint_ops->print_it, but in this case, by the time we
5000 call breakpoint_ops->print_it this bp will be deleted
5001 already. So we have no choice but print the information
5002 here. */
468afe6c 5003
0e454242 5004 SWITCH_THRU_ALL_UIS ()
dda83cd7 5005 {
468afe6c
PA
5006 struct ui_out *uiout = current_uiout;
5007
112e8700
SM
5008 if (uiout->is_mi_like_p ())
5009 uiout->field_string
5010 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5011 uiout->message ("\nWatchpoint %pF deleted because the program has "
5012 "left the block in\n"
5013 "which its expression is valid.\n",
5014 signed_field ("wpnum", b->number));
468afe6c 5015 }
4ce44c66 5016
cdac0397 5017 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5018 b->commands = NULL;
d0fb5eae 5019 watchpoint_del_at_next_stop (b);
c906108c
SS
5020
5021 return WP_DELETED;
5022 }
5023}
5024
18a18393 5025/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5026 breakpoint location BL. This function does not check if we should
5027 stop, only if BL explains the stop. */
5028
18a18393 5029static int
6c95b8df 5030bpstat_check_location (const struct bp_location *bl,
accd0bcd 5031 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5032 const struct target_waitstatus *ws)
18a18393
VP
5033{
5034 struct breakpoint *b = bl->owner;
5035
348d480f 5036 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5037 gdb_assert (b != NULL);
5038
bd522513 5039 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5040}
5041
3a5c3e22
PA
5042/* Determine if the watched values have actually changed, and we
5043 should stop. If not, set BS->stop to 0. */
5044
18a18393
VP
5045static void
5046bpstat_check_watchpoint (bpstat bs)
5047{
2bdf28a0 5048 const struct bp_location *bl;
3a5c3e22 5049 struct watchpoint *b;
2bdf28a0
JK
5050
5051 /* BS is built for existing struct breakpoint. */
b6433ede 5052 bl = bs->bp_location_at.get ();
2bdf28a0 5053 gdb_assert (bl != NULL);
3a5c3e22 5054 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5055 gdb_assert (b != NULL);
18a18393 5056
18a18393 5057 {
18a18393
VP
5058 int must_check_value = 0;
5059
c1fc2657 5060 if (b->type == bp_watchpoint)
18a18393
VP
5061 /* For a software watchpoint, we must always check the
5062 watched value. */
5063 must_check_value = 1;
5064 else if (b->watchpoint_triggered == watch_triggered_yes)
5065 /* We have a hardware watchpoint (read, write, or access)
5066 and the target earlier reported an address watched by
5067 this watchpoint. */
5068 must_check_value = 1;
5069 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5070 && b->type == bp_hardware_watchpoint)
18a18393
VP
5071 /* We were stopped by a hardware watchpoint, but the target could
5072 not report the data address. We must check the watchpoint's
5073 value. Access and read watchpoints are out of luck; without
5074 a data address, we can't figure it out. */
5075 must_check_value = 1;
3a5c3e22 5076
18a18393
VP
5077 if (must_check_value)
5078 {
bf469271
PA
5079 wp_check_result e;
5080
a70b8144 5081 try
bf469271
PA
5082 {
5083 e = watchpoint_check (bs);
5084 }
230d2906 5085 catch (const gdb_exception &ex)
bf469271
PA
5086 {
5087 exception_fprintf (gdb_stderr, ex,
5088 "Error evaluating expression "
5089 "for watchpoint %d\n",
5090 b->number);
5091
5092 SWITCH_THRU_ALL_UIS ()
5093 {
5094 printf_filtered (_("Watchpoint %d deleted.\n"),
5095 b->number);
5096 }
5097 watchpoint_del_at_next_stop (b);
5098 e = WP_DELETED;
5099 }
bf469271 5100
18a18393
VP
5101 switch (e)
5102 {
5103 case WP_DELETED:
5104 /* We've already printed what needs to be printed. */
5105 bs->print_it = print_it_done;
5106 /* Stop. */
5107 break;
60e1c644
PA
5108 case WP_IGNORE:
5109 bs->print_it = print_it_noop;
5110 bs->stop = 0;
5111 break;
18a18393 5112 case WP_VALUE_CHANGED:
c1fc2657 5113 if (b->type == bp_read_watchpoint)
18a18393 5114 {
85d721b8
PA
5115 /* There are two cases to consider here:
5116
4a64f543 5117 1. We're watching the triggered memory for reads.
85d721b8
PA
5118 In that case, trust the target, and always report
5119 the watchpoint hit to the user. Even though
5120 reads don't cause value changes, the value may
5121 have changed since the last time it was read, and
5122 since we're not trapping writes, we will not see
5123 those, and as such we should ignore our notion of
5124 old value.
5125
4a64f543 5126 2. We're watching the triggered memory for both
85d721b8
PA
5127 reads and writes. There are two ways this may
5128 happen:
5129
4a64f543 5130 2.1. This is a target that can't break on data
85d721b8
PA
5131 reads only, but can break on accesses (reads or
5132 writes), such as e.g., x86. We detect this case
5133 at the time we try to insert read watchpoints.
5134
4a64f543 5135 2.2. Otherwise, the target supports read
85d721b8
PA
5136 watchpoints, but, the user set an access or write
5137 watchpoint watching the same memory as this read
5138 watchpoint.
5139
5140 If we're watching memory writes as well as reads,
5141 ignore watchpoint hits when we find that the
5142 value hasn't changed, as reads don't cause
5143 changes. This still gives false positives when
5144 the program writes the same value to memory as
5145 what there was already in memory (we will confuse
5146 it for a read), but it's much better than
5147 nothing. */
5148
5149 int other_write_watchpoint = 0;
5150
5151 if (bl->watchpoint_type == hw_read)
5152 {
43892fdf 5153 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5154 if (other_b->type == bp_hardware_watchpoint
5155 || other_b->type == bp_access_watchpoint)
85d721b8 5156 {
3a5c3e22
PA
5157 struct watchpoint *other_w =
5158 (struct watchpoint *) other_b;
5159
5160 if (other_w->watchpoint_triggered
5161 == watch_triggered_yes)
5162 {
5163 other_write_watchpoint = 1;
5164 break;
5165 }
85d721b8
PA
5166 }
5167 }
5168
5169 if (other_write_watchpoint
5170 || bl->watchpoint_type == hw_access)
5171 {
5172 /* We're watching the same memory for writes,
5173 and the value changed since the last time we
5174 updated it, so this trap must be for a write.
5175 Ignore it. */
5176 bs->print_it = print_it_noop;
5177 bs->stop = 0;
5178 }
18a18393
VP
5179 }
5180 break;
5181 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5182 if (b->type == bp_hardware_watchpoint
5183 || b->type == bp_watchpoint)
18a18393
VP
5184 {
5185 /* Don't stop: write watchpoints shouldn't fire if
5186 the value hasn't changed. */
5187 bs->print_it = print_it_noop;
5188 bs->stop = 0;
5189 }
5190 /* Stop. */
5191 break;
5192 default:
5193 /* Can't happen. */
18a18393
VP
5194 break;
5195 }
5196 }
5197 else /* must_check_value == 0 */
5198 {
5199 /* This is a case where some watchpoint(s) triggered, but
5200 not at the address of this watchpoint, or else no
5201 watchpoint triggered after all. So don't print
5202 anything for this watchpoint. */
5203 bs->print_it = print_it_noop;
5204 bs->stop = 0;
5205 }
5206 }
5207}
5208
7d4df6a4
DE
5209/* For breakpoints that are currently marked as telling gdb to stop,
5210 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5211 of breakpoint referred to by BS. If we should not stop for this
5212 breakpoint, set BS->stop to 0. */
f431efe5 5213
18a18393 5214static void
00431a78 5215bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5216{
2bdf28a0
JK
5217 const struct bp_location *bl;
5218 struct breakpoint *b;
bf469271
PA
5219 /* Assume stop. */
5220 bool condition_result = true;
7d4df6a4
DE
5221 struct expression *cond;
5222
5223 gdb_assert (bs->stop);
2bdf28a0
JK
5224
5225 /* BS is built for existing struct breakpoint. */
b6433ede 5226 bl = bs->bp_location_at.get ();
2bdf28a0 5227 gdb_assert (bl != NULL);
f431efe5 5228 b = bs->breakpoint_at;
2bdf28a0 5229 gdb_assert (b != NULL);
18a18393 5230
b775012e
LM
5231 /* Even if the target evaluated the condition on its end and notified GDB, we
5232 need to do so again since GDB does not know if we stopped due to a
5233 breakpoint or a single step breakpoint. */
5234
18a18393 5235 if (frame_id_p (b->frame_id)
edb3359d 5236 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5237 {
7d4df6a4
DE
5238 bs->stop = 0;
5239 return;
5240 }
60e1c644 5241
12ab52e9
PA
5242 /* If this is a thread/task-specific breakpoint, don't waste cpu
5243 evaluating the condition if this isn't the specified
5244 thread/task. */
00431a78
PA
5245 if ((b->thread != -1 && b->thread != thread->global_num)
5246 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5247 {
5248 bs->stop = 0;
5249 return;
5250 }
5251
6dddc817
DE
5252 /* Evaluate extension language breakpoints that have a "stop" method
5253 implemented. */
5254 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5255
7d4df6a4
DE
5256 if (is_watchpoint (b))
5257 {
5258 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5259
4d01a485 5260 cond = w->cond_exp.get ();
7d4df6a4
DE
5261 }
5262 else
4d01a485 5263 cond = bl->cond.get ();
60e1c644 5264
7d4df6a4
DE
5265 if (cond && b->disposition != disp_del_at_next_stop)
5266 {
5267 int within_current_scope = 1;
5268 struct watchpoint * w;
60e1c644 5269
7d4df6a4
DE
5270 /* We use value_mark and value_free_to_mark because it could
5271 be a long time before we return to the command level and
5272 call free_all_values. We can't call free_all_values
5273 because we might be in the middle of evaluating a
5274 function call. */
5275 struct value *mark = value_mark ();
5276
5277 if (is_watchpoint (b))
5278 w = (struct watchpoint *) b;
5279 else
5280 w = NULL;
5281
5282 /* Need to select the frame, with all that implies so that
5283 the conditions will have the right context. Because we
5284 use the frame, we will not see an inlined function's
5285 variables when we arrive at a breakpoint at the start
5286 of the inlined function; the current frame will be the
5287 call site. */
5288 if (w == NULL || w->cond_exp_valid_block == NULL)
5289 select_frame (get_current_frame ());
5290 else
18a18393 5291 {
7d4df6a4
DE
5292 struct frame_info *frame;
5293
5294 /* For local watchpoint expressions, which particular
5295 instance of a local is being watched matters, so we
5296 keep track of the frame to evaluate the expression
5297 in. To evaluate the condition however, it doesn't
5298 really matter which instantiation of the function
5299 where the condition makes sense triggers the
5300 watchpoint. This allows an expression like "watch
5301 global if q > 10" set in `func', catch writes to
5302 global on all threads that call `func', or catch
5303 writes on all recursive calls of `func' by a single
5304 thread. We simply always evaluate the condition in
5305 the innermost frame that's executing where it makes
5306 sense to evaluate the condition. It seems
5307 intuitive. */
5308 frame = block_innermost_frame (w->cond_exp_valid_block);
5309 if (frame != NULL)
5310 select_frame (frame);
5311 else
5312 within_current_scope = 0;
18a18393 5313 }
7d4df6a4 5314 if (within_current_scope)
bf469271 5315 {
a70b8144 5316 try
bf469271
PA
5317 {
5318 condition_result = breakpoint_cond_eval (cond);
5319 }
230d2906 5320 catch (const gdb_exception &ex)
bf469271
PA
5321 {
5322 exception_fprintf (gdb_stderr, ex,
5323 "Error in testing breakpoint condition:\n");
5324 }
bf469271 5325 }
7d4df6a4 5326 else
18a18393 5327 {
7d4df6a4
DE
5328 warning (_("Watchpoint condition cannot be tested "
5329 "in the current scope"));
5330 /* If we failed to set the right context for this
5331 watchpoint, unconditionally report it. */
18a18393 5332 }
7d4df6a4
DE
5333 /* FIXME-someday, should give breakpoint #. */
5334 value_free_to_mark (mark);
18a18393 5335 }
7d4df6a4 5336
bf469271 5337 if (cond && !condition_result)
7d4df6a4
DE
5338 {
5339 bs->stop = 0;
5340 }
7d4df6a4
DE
5341 else if (b->ignore_count > 0)
5342 {
5343 b->ignore_count--;
5344 bs->stop = 0;
5345 /* Increase the hit count even though we don't stop. */
5346 ++(b->hit_count);
76727919 5347 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5348 }
18a18393
VP
5349}
5350
1cf4d951
PA
5351/* Returns true if we need to track moribund locations of LOC's type
5352 on the current target. */
5353
5354static int
5355need_moribund_for_location_type (struct bp_location *loc)
5356{
5357 return ((loc->loc_type == bp_loc_software_breakpoint
5358 && !target_supports_stopped_by_sw_breakpoint ())
5359 || (loc->loc_type == bp_loc_hardware_breakpoint
5360 && !target_supports_stopped_by_hw_breakpoint ()));
5361}
5362
ddfe970e 5363/* See breakpoint.h. */
c906108c
SS
5364
5365bpstat
ddfe970e 5366build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5367 const struct target_waitstatus *ws)
c906108c 5368{
5760d0ab 5369 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5370
43892fdf 5371 for (breakpoint *b : all_breakpoints ())
429374b8 5372 {
1a853c52 5373 if (!breakpoint_enabled (b))
429374b8 5374 continue;
a5606eee 5375
40cb8ca5 5376 for (bp_location *bl : b->locations ())
429374b8 5377 {
4a64f543
MS
5378 /* For hardware watchpoints, we look only at the first
5379 location. The watchpoint_check function will work on the
5380 entire expression, not the individual locations. For
5381 read watchpoints, the watchpoints_triggered function has
5382 checked all locations already. */
429374b8
JK
5383 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5384 break;
18a18393 5385
b5fa468f 5386 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5387 continue;
c5aa993b 5388
09ac7c10 5389 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5390 continue;
c5aa993b 5391
4a64f543
MS
5392 /* Come here if it's a watchpoint, or if the break address
5393 matches. */
c5aa993b 5394
ddfe970e
KS
5395 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5396 explain stop. */
c5aa993b 5397
f431efe5
PA
5398 /* Assume we stop. Should we find a watchpoint that is not
5399 actually triggered, or if the condition of the breakpoint
5400 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5401 bs->stop = 1;
5402 bs->print = 1;
d983da9c 5403
f431efe5
PA
5404 /* If this is a scope breakpoint, mark the associated
5405 watchpoint as triggered so that we will handle the
5406 out-of-scope event. We'll get to the watchpoint next
5407 iteration. */
d0fb5eae 5408 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5409 {
5410 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5411
5412 w->watchpoint_triggered = watch_triggered_yes;
5413 }
f431efe5
PA
5414 }
5415 }
5416
7c16b83e 5417 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5418 if (!target_supports_stopped_by_sw_breakpoint ()
5419 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5420 {
1123588c 5421 for (bp_location *loc : moribund_locations)
f431efe5 5422 {
1cf4d951
PA
5423 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5424 && need_moribund_for_location_type (loc))
5425 {
ddfe970e 5426 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5427 /* For hits of moribund locations, we should just proceed. */
5428 bs->stop = 0;
5429 bs->print = 0;
5430 bs->print_it = print_it_noop;
5431 }
f431efe5
PA
5432 }
5433 }
5434
ddfe970e
KS
5435 return bs_head;
5436}
5437
5438/* See breakpoint.h. */
5439
5440bpstat
5441bpstat_stop_status (const address_space *aspace,
00431a78 5442 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5443 const struct target_waitstatus *ws,
5444 bpstat stop_chain)
5445{
5446 struct breakpoint *b = NULL;
5447 /* First item of allocated bpstat's. */
5448 bpstat bs_head = stop_chain;
5449 bpstat bs;
5450 int need_remove_insert;
5451 int removed_any;
5452
5453 /* First, build the bpstat chain with locations that explain a
5454 target stop, while being careful to not set the target running,
5455 as that may invalidate locations (in particular watchpoint
5456 locations are recreated). Resuming will happen here with
5457 breakpoint conditions or watchpoint expressions that include
5458 inferior function calls. */
5459 if (bs_head == NULL)
5460 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5461
edcc5120
TT
5462 /* A bit of special processing for shlib breakpoints. We need to
5463 process solib loading here, so that the lists of loaded and
5464 unloaded libraries are correct before we handle "catch load" and
5465 "catch unload". */
5466 for (bs = bs_head; bs != NULL; bs = bs->next)
5467 {
5d268276 5468 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5469 {
5470 handle_solib_event ();
5471 break;
5472 }
5473 }
5474
f431efe5
PA
5475 /* Now go through the locations that caused the target to stop, and
5476 check whether we're interested in reporting this stop to higher
5477 layers, or whether we should resume the target transparently. */
5478
5479 removed_any = 0;
5480
5760d0ab 5481 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5482 {
5483 if (!bs->stop)
5484 continue;
5485
f431efe5 5486 b = bs->breakpoint_at;
348d480f
PA
5487 b->ops->check_status (bs);
5488 if (bs->stop)
28010a5d 5489 {
00431a78 5490 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5491
429374b8
JK
5492 if (bs->stop)
5493 {
5494 ++(b->hit_count);
76727919 5495 gdb::observers::breakpoint_modified.notify (b);
c906108c 5496
4a64f543 5497 /* We will stop here. */
429374b8
JK
5498 if (b->disposition == disp_disable)
5499 {
816338b5 5500 --(b->enable_count);
1a853c52 5501 if (b->enable_count <= 0)
429374b8 5502 b->enable_state = bp_disabled;
f431efe5 5503 removed_any = 1;
429374b8
JK
5504 }
5505 if (b->silent)
5506 bs->print = 0;
5507 bs->commands = b->commands;
abf85f46 5508 if (command_line_is_silent (bs->commands
d1b0a7bf 5509 ? bs->commands.get () : NULL))
abf85f46 5510 bs->print = 0;
9d6e6e84
HZ
5511
5512 b->ops->after_condition_true (bs);
429374b8
JK
5513 }
5514
348d480f 5515 }
a9b3a50f
PA
5516
5517 /* Print nothing for this entry if we don't stop or don't
5518 print. */
5519 if (!bs->stop || !bs->print)
5520 bs->print_it = print_it_noop;
429374b8 5521 }
876fa593 5522
d983da9c
DJ
5523 /* If we aren't stopping, the value of some hardware watchpoint may
5524 not have changed, but the intermediate memory locations we are
5525 watching may have. Don't bother if we're stopping; this will get
5526 done later. */
d832cb68 5527 need_remove_insert = 0;
5760d0ab
JK
5528 if (! bpstat_causes_stop (bs_head))
5529 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5530 if (!bs->stop
f431efe5
PA
5531 && bs->breakpoint_at
5532 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5533 {
3a5c3e22
PA
5534 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5535
5536 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5537 need_remove_insert = 1;
d983da9c
DJ
5538 }
5539
d832cb68 5540 if (need_remove_insert)
44702360 5541 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5542 else if (removed_any)
44702360 5543 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5544
5760d0ab 5545 return bs_head;
c906108c 5546}
628fe4e4
JK
5547
5548static void
61c26be8 5549handle_jit_event (CORE_ADDR address)
628fe4e4 5550{
628fe4e4
JK
5551 struct gdbarch *gdbarch;
5552
1eb8556f 5553 infrun_debug_printf ("handling bp_jit_event");
243a9253 5554
628fe4e4
JK
5555 /* Switch terminal for any messages produced by
5556 breakpoint_re_set. */
223ffa71 5557 target_terminal::ours_for_output ();
628fe4e4 5558
61c26be8
MS
5559 gdbarch = get_frame_arch (get_current_frame ());
5560 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5561 thus it is expected that its objectfile can be found through
5562 minimal symbol lookup. If it doesn't work (and assert fails), it
5563 most likely means that `jit_breakpoint_re_set` was changes and this
5564 function needs to be updated too. */
5565 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5566 gdb_assert (jit_bp_sym.objfile != nullptr);
5567 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5568
223ffa71 5569 target_terminal::inferior ();
628fe4e4
JK
5570}
5571
5572/* Prepare WHAT final decision for infrun. */
5573
5574/* Decide what infrun needs to do with this bpstat. */
5575
c906108c 5576struct bpstat_what
0e30163f 5577bpstat_what (bpstat bs_head)
c906108c 5578{
c906108c 5579 struct bpstat_what retval;
0e30163f 5580 bpstat bs;
c906108c 5581
628fe4e4 5582 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5583 retval.call_dummy = STOP_NONE;
e2d0f980 5584 retval.is_longjmp = false;
628fe4e4 5585
0e30163f 5586 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5587 {
628fe4e4
JK
5588 /* Extract this BS's action. After processing each BS, we check
5589 if its action overrides all we've seem so far. */
5590 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5591 enum bptype bptype;
5592
c906108c 5593 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5594 {
5595 /* I suspect this can happen if it was a momentary
5596 breakpoint which has since been deleted. */
5597 bptype = bp_none;
5598 }
20874c92 5599 else
f431efe5 5600 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5601
5602 switch (bptype)
c906108c
SS
5603 {
5604 case bp_none:
628fe4e4 5605 break;
c906108c
SS
5606 case bp_breakpoint:
5607 case bp_hardware_breakpoint:
7c16b83e 5608 case bp_single_step:
c906108c
SS
5609 case bp_until:
5610 case bp_finish:
a9b3a50f 5611 case bp_shlib_event:
c906108c
SS
5612 if (bs->stop)
5613 {
5614 if (bs->print)
628fe4e4 5615 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5616 else
628fe4e4 5617 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5618 }
5619 else
628fe4e4 5620 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5621 break;
5622 case bp_watchpoint:
5623 case bp_hardware_watchpoint:
5624 case bp_read_watchpoint:
5625 case bp_access_watchpoint:
5626 if (bs->stop)
5627 {
5628 if (bs->print)
628fe4e4 5629 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5630 else
628fe4e4 5631 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5632 }
5633 else
628fe4e4
JK
5634 {
5635 /* There was a watchpoint, but we're not stopping.
5636 This requires no further action. */
5637 }
c906108c
SS
5638 break;
5639 case bp_longjmp:
e2e4d78b 5640 case bp_longjmp_call_dummy:
186c406b 5641 case bp_exception:
0a39bb32
PA
5642 if (bs->stop)
5643 {
5644 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5645 retval.is_longjmp = bptype != bp_exception;
5646 }
5647 else
5648 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5649 break;
5650 case bp_longjmp_resume:
186c406b 5651 case bp_exception_resume:
0a39bb32
PA
5652 if (bs->stop)
5653 {
5654 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5655 retval.is_longjmp = bptype == bp_longjmp_resume;
5656 }
5657 else
5658 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5659 break;
5660 case bp_step_resume:
5661 if (bs->stop)
628fe4e4
JK
5662 this_action = BPSTAT_WHAT_STEP_RESUME;
5663 else
c906108c 5664 {
628fe4e4
JK
5665 /* It is for the wrong frame. */
5666 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5667 }
c906108c 5668 break;
2c03e5be
PA
5669 case bp_hp_step_resume:
5670 if (bs->stop)
5671 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5672 else
5673 {
5674 /* It is for the wrong frame. */
5675 this_action = BPSTAT_WHAT_SINGLE;
5676 }
5677 break;
c906108c 5678 case bp_watchpoint_scope:
c4093a6a 5679 case bp_thread_event:
1900040c 5680 case bp_overlay_event:
0fd8e87f 5681 case bp_longjmp_master:
aa7d318d 5682 case bp_std_terminate_master:
186c406b 5683 case bp_exception_master:
628fe4e4 5684 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5685 break;
ce78b96d 5686 case bp_catchpoint:
c5aa993b
JM
5687 if (bs->stop)
5688 {
5689 if (bs->print)
628fe4e4 5690 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5691 else
628fe4e4 5692 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5693 }
5694 else
628fe4e4 5695 {
cb1e4e32
PA
5696 /* Some catchpoints are implemented with breakpoints.
5697 For those, we need to step over the breakpoint. */
5698 if (bs->bp_location_at->loc_type != bp_loc_other)
5699 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5700 }
5701 break;
628fe4e4 5702 case bp_jit_event:
628fe4e4 5703 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5704 break;
c906108c 5705 case bp_call_dummy:
53a5351d
JM
5706 /* Make sure the action is stop (silent or noisy),
5707 so infrun.c pops the dummy frame. */
aa7d318d 5708 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5709 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5710 break;
5711 case bp_std_terminate:
5712 /* Make sure the action is stop (silent or noisy),
5713 so infrun.c pops the dummy frame. */
aa7d318d 5714 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5715 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5716 break;
1042e4c0 5717 case bp_tracepoint:
7a697b8d 5718 case bp_fast_tracepoint:
0fb4aa4b 5719 case bp_static_tracepoint:
1042e4c0
SS
5720 /* Tracepoint hits should not be reported back to GDB, and
5721 if one got through somehow, it should have been filtered
5722 out already. */
5723 internal_error (__FILE__, __LINE__,
7a697b8d 5724 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5725 break;
5726 case bp_gnu_ifunc_resolver:
5727 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 break;
5730 case bp_gnu_ifunc_resolver_return:
5731 /* The breakpoint will be removed, execution will restart from the
5732 PC of the former breakpoint. */
5733 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5734 break;
e7e0cddf
SS
5735
5736 case bp_dprintf:
a11cfd87
HZ
5737 if (bs->stop)
5738 this_action = BPSTAT_WHAT_STOP_SILENT;
5739 else
5740 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5741 break;
5742
628fe4e4
JK
5743 default:
5744 internal_error (__FILE__, __LINE__,
5745 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5746 }
628fe4e4 5747
325fac50 5748 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5749 }
628fe4e4 5750
243a9253
PA
5751 return retval;
5752}
628fe4e4 5753
243a9253
PA
5754void
5755bpstat_run_callbacks (bpstat bs_head)
5756{
5757 bpstat bs;
628fe4e4 5758
0e30163f
JK
5759 for (bs = bs_head; bs != NULL; bs = bs->next)
5760 {
5761 struct breakpoint *b = bs->breakpoint_at;
5762
5763 if (b == NULL)
5764 continue;
5765 switch (b->type)
5766 {
243a9253 5767 case bp_jit_event:
61c26be8 5768 handle_jit_event (bs->bp_location_at->address);
243a9253 5769 break;
0e30163f
JK
5770 case bp_gnu_ifunc_resolver:
5771 gnu_ifunc_resolver_stop (b);
5772 break;
5773 case bp_gnu_ifunc_resolver_return:
5774 gnu_ifunc_resolver_return_stop (b);
5775 break;
5776 }
5777 }
c906108c
SS
5778}
5779
4c462cb0 5780/* See breakpoint.h. */
c906108c 5781
4c462cb0
SM
5782bool
5783bpstat_should_step ()
c906108c 5784{
43892fdf 5785 for (breakpoint *b : all_breakpoints ())
717a8278 5786 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5787 return true;
43892fdf 5788
4c462cb0 5789 return false;
c906108c
SS
5790}
5791
4c462cb0
SM
5792/* See breakpoint.h. */
5793
5794bool
67822962
PA
5795bpstat_causes_stop (bpstat bs)
5796{
5797 for (; bs != NULL; bs = bs->next)
5798 if (bs->stop)
4c462cb0 5799 return true;
67822962 5800
4c462cb0 5801 return false;
67822962
PA
5802}
5803
c906108c 5804\f
c5aa993b 5805
170b53b2
UW
5806/* Compute a string of spaces suitable to indent the next line
5807 so it starts at the position corresponding to the table column
5808 named COL_NAME in the currently active table of UIOUT. */
5809
5810static char *
5811wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5812{
5813 static char wrap_indent[80];
5814 int i, total_width, width, align;
c5209615 5815 const char *text;
170b53b2
UW
5816
5817 total_width = 0;
112e8700 5818 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5819 {
5820 if (strcmp (text, col_name) == 0)
5821 {
5822 gdb_assert (total_width < sizeof wrap_indent);
5823 memset (wrap_indent, ' ', total_width);
5824 wrap_indent[total_width] = 0;
5825
5826 return wrap_indent;
5827 }
5828
5829 total_width += width + 1;
5830 }
5831
5832 return NULL;
5833}
5834
b775012e
LM
5835/* Determine if the locations of this breakpoint will have their conditions
5836 evaluated by the target, host or a mix of both. Returns the following:
5837
5838 "host": Host evals condition.
5839 "host or target": Host or Target evals condition.
5840 "target": Target evals condition.
5841*/
5842
5843static const char *
5844bp_condition_evaluator (struct breakpoint *b)
5845{
b775012e
LM
5846 char host_evals = 0;
5847 char target_evals = 0;
5848
5849 if (!b)
5850 return NULL;
5851
5852 if (!is_breakpoint (b))
5853 return NULL;
5854
5855 if (gdb_evaluates_breakpoint_condition_p ()
5856 || !target_supports_evaluation_of_breakpoint_conditions ())
5857 return condition_evaluation_host;
5858
40cb8ca5 5859 for (bp_location *bl : b->locations ())
b775012e
LM
5860 {
5861 if (bl->cond_bytecode)
5862 target_evals++;
5863 else
5864 host_evals++;
5865 }
5866
5867 if (host_evals && target_evals)
5868 return condition_evaluation_both;
5869 else if (target_evals)
5870 return condition_evaluation_target;
5871 else
5872 return condition_evaluation_host;
5873}
5874
5875/* Determine the breakpoint location's condition evaluator. This is
5876 similar to bp_condition_evaluator, but for locations. */
5877
5878static const char *
5879bp_location_condition_evaluator (struct bp_location *bl)
5880{
5881 if (bl && !is_breakpoint (bl->owner))
5882 return NULL;
5883
5884 if (gdb_evaluates_breakpoint_condition_p ()
5885 || !target_supports_evaluation_of_breakpoint_conditions ())
5886 return condition_evaluation_host;
5887
5888 if (bl && bl->cond_bytecode)
5889 return condition_evaluation_target;
5890 else
5891 return condition_evaluation_host;
5892}
5893
859825b8
JK
5894/* Print the LOC location out of the list of B->LOC locations. */
5895
170b53b2
UW
5896static void
5897print_breakpoint_location (struct breakpoint *b,
5898 struct bp_location *loc)
0d381245 5899{
79a45e25 5900 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5901
5902 scoped_restore_current_program_space restore_pspace;
6c95b8df 5903
859825b8
JK
5904 if (loc != NULL && loc->shlib_disabled)
5905 loc = NULL;
5906
6c95b8df
PA
5907 if (loc != NULL)
5908 set_current_program_space (loc->pspace);
5909
56435ebe 5910 if (b->display_canonical)
d28cd78a 5911 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5912 else if (loc && loc->symtab)
0d381245 5913 {
4a27f119
KS
5914 const struct symbol *sym = loc->symbol;
5915
0d381245
VP
5916 if (sym)
5917 {
112e8700 5918 uiout->text ("in ");
987012b8 5919 uiout->field_string ("func", sym->print_name (),
e43b10e1 5920 function_name_style.style ());
112e8700
SM
5921 uiout->text (" ");
5922 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5923 uiout->text ("at ");
0d381245 5924 }
112e8700 5925 uiout->field_string ("file",
cbe56571 5926 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5927 file_name_style.style ());
112e8700 5928 uiout->text (":");
05cba821 5929
112e8700
SM
5930 if (uiout->is_mi_like_p ())
5931 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5932
381befee 5933 uiout->field_signed ("line", loc->line_number);
0d381245 5934 }
859825b8 5935 else if (loc)
0d381245 5936 {
d7e74731 5937 string_file stb;
170b53b2 5938
d7e74731 5939 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5940 demangle, "");
112e8700 5941 uiout->field_stream ("at", stb);
0d381245 5942 }
859825b8 5943 else
f00aae0f 5944 {
d28cd78a
TT
5945 uiout->field_string ("pending",
5946 event_location_to_string (b->location.get ()));
f00aae0f
KS
5947 /* If extra_string is available, it could be holding a condition
5948 or dprintf arguments. In either case, make sure it is printed,
5949 too, but only for non-MI streams. */
112e8700 5950 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5951 {
5952 if (b->type == bp_dprintf)
112e8700 5953 uiout->text (",");
f00aae0f 5954 else
112e8700
SM
5955 uiout->text (" ");
5956 uiout->text (b->extra_string);
f00aae0f
KS
5957 }
5958 }
6c95b8df 5959
b775012e
LM
5960 if (loc && is_breakpoint (b)
5961 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5962 && bp_condition_evaluator (b) == condition_evaluation_both)
5963 {
112e8700
SM
5964 uiout->text (" (");
5965 uiout->field_string ("evaluated-by",
b775012e 5966 bp_location_condition_evaluator (loc));
112e8700 5967 uiout->text (")");
b775012e 5968 }
0d381245
VP
5969}
5970
269b11a2
PA
5971static const char *
5972bptype_string (enum bptype type)
c906108c 5973{
c4093a6a
JM
5974 struct ep_type_description
5975 {
5976 enum bptype type;
a121b7c1 5977 const char *description;
c4093a6a
JM
5978 };
5979 static struct ep_type_description bptypes[] =
c906108c 5980 {
c5aa993b
JM
5981 {bp_none, "?deleted?"},
5982 {bp_breakpoint, "breakpoint"},
c906108c 5983 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5984 {bp_single_step, "sw single-step"},
c5aa993b
JM
5985 {bp_until, "until"},
5986 {bp_finish, "finish"},
5987 {bp_watchpoint, "watchpoint"},
c906108c 5988 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5989 {bp_read_watchpoint, "read watchpoint"},
5990 {bp_access_watchpoint, "acc watchpoint"},
5991 {bp_longjmp, "longjmp"},
5992 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5993 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5994 {bp_exception, "exception"},
5995 {bp_exception_resume, "exception resume"},
c5aa993b 5996 {bp_step_resume, "step resume"},
2c03e5be 5997 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5998 {bp_watchpoint_scope, "watchpoint scope"},
5999 {bp_call_dummy, "call dummy"},
aa7d318d 6000 {bp_std_terminate, "std::terminate"},
c5aa993b 6001 {bp_shlib_event, "shlib events"},
c4093a6a 6002 {bp_thread_event, "thread events"},
1900040c 6003 {bp_overlay_event, "overlay events"},
0fd8e87f 6004 {bp_longjmp_master, "longjmp master"},
aa7d318d 6005 {bp_std_terminate_master, "std::terminate master"},
186c406b 6006 {bp_exception_master, "exception master"},
ce78b96d 6007 {bp_catchpoint, "catchpoint"},
1042e4c0 6008 {bp_tracepoint, "tracepoint"},
7a697b8d 6009 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6010 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6011 {bp_dprintf, "dprintf"},
4efc6507 6012 {bp_jit_event, "jit events"},
0e30163f
JK
6013 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6014 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6015 };
269b11a2
PA
6016
6017 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6018 || ((int) type != bptypes[(int) type].type))
6019 internal_error (__FILE__, __LINE__,
6020 _("bptypes table does not describe type #%d."),
6021 (int) type);
6022
6023 return bptypes[(int) type].description;
6024}
6025
998580f1
MK
6026/* For MI, output a field named 'thread-groups' with a list as the value.
6027 For CLI, prefix the list with the string 'inf'. */
6028
6029static void
6030output_thread_groups (struct ui_out *uiout,
6031 const char *field_name,
5c632425 6032 const std::vector<int> &inf_nums,
998580f1
MK
6033 int mi_only)
6034{
112e8700 6035 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6036
6037 /* For backward compatibility, don't display inferiors in CLI unless
6038 there are several. Always display them for MI. */
6039 if (!is_mi && mi_only)
6040 return;
6041
10f489e5 6042 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6043
5c632425 6044 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6045 {
6046 if (is_mi)
6047 {
6048 char mi_group[10];
6049
5c632425 6050 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6051 uiout->field_string (NULL, mi_group);
998580f1
MK
6052 }
6053 else
6054 {
6055 if (i == 0)
112e8700 6056 uiout->text (" inf ");
998580f1 6057 else
112e8700 6058 uiout->text (", ");
998580f1 6059
5c632425 6060 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6061 }
6062 }
998580f1
MK
6063}
6064
a38118e5
PA
6065/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6066 instead of going via breakpoint_ops::print_one. This makes "maint
6067 info breakpoints" show the software breakpoint locations of
6068 catchpoints, which are considered internal implementation
6069 detail. */
269b11a2
PA
6070
6071static void
6072print_one_breakpoint_location (struct breakpoint *b,
6073 struct bp_location *loc,
6074 int loc_number,
6075 struct bp_location **last_loc,
a38118e5 6076 int allflag, bool raw_loc)
269b11a2
PA
6077{
6078 struct command_line *l;
c2c6d25f 6079 static char bpenables[] = "nynny";
c906108c 6080
79a45e25 6081 struct ui_out *uiout = current_uiout;
0d381245
VP
6082 int header_of_multiple = 0;
6083 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6084 struct value_print_options opts;
6085
6086 get_user_print_options (&opts);
0d381245
VP
6087
6088 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6089 /* See comment in print_one_breakpoint concerning treatment of
6090 breakpoints with single disabled location. */
0d381245
VP
6091 if (loc == NULL
6092 && (b->loc != NULL
b5fa468f
TBA
6093 && (b->loc->next != NULL
6094 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6095 header_of_multiple = 1;
6096 if (loc == NULL)
6097 loc = b->loc;
6098
c4093a6a
JM
6099 annotate_record ();
6100
6101 /* 1 */
6102 annotate_field (0);
0d381245 6103 if (part_of_multiple)
528e1572 6104 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6105 else
381befee 6106 uiout->field_signed ("number", b->number);
c4093a6a
JM
6107
6108 /* 2 */
6109 annotate_field (1);
0d381245 6110 if (part_of_multiple)
112e8700 6111 uiout->field_skip ("type");
269b11a2 6112 else
112e8700 6113 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6114
6115 /* 3 */
6116 annotate_field (2);
0d381245 6117 if (part_of_multiple)
112e8700 6118 uiout->field_skip ("disp");
0d381245 6119 else
112e8700 6120 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6121
c4093a6a
JM
6122 /* 4 */
6123 annotate_field (3);
85c88e2a
TBA
6124 /* For locations that are disabled because of an invalid condition,
6125 display "N*" on CLI, where "*" refers to a footnote below the
6126 table. For MI, simply display a "N" without a footnote. */
6127 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6128 if (part_of_multiple)
85c88e2a 6129 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6130 : (loc->enabled ? "y" : "n")));
0d381245 6131 else
112e8700 6132 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6133
c4093a6a 6134 /* 5 and 6 */
a38118e5 6135 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6136 b->ops->print_one (b, last_loc);
3086aeae 6137 else
a38118e5
PA
6138 {
6139 if (is_watchpoint (b))
3a5c3e22
PA
6140 {
6141 struct watchpoint *w = (struct watchpoint *) b;
6142
6143 /* Field 4, the address, is omitted (which makes the columns
6144 not line up too nicely with the headers, but the effect
6145 is relatively readable). */
6146 if (opts.addressprint)
112e8700 6147 uiout->field_skip ("addr");
3a5c3e22 6148 annotate_field (5);
112e8700 6149 uiout->field_string ("what", w->exp_string);
3a5c3e22 6150 }
f06f1252
TT
6151 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6152 || is_ada_exception_catchpoint (b))
a38118e5
PA
6153 {
6154 if (opts.addressprint)
6155 {
6156 annotate_field (4);
6157 if (header_of_multiple)
7f6aba03
TT
6158 uiout->field_string ("addr", "<MULTIPLE>",
6159 metadata_style.style ());
a38118e5 6160 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6161 uiout->field_string ("addr", "<PENDING>",
6162 metadata_style.style ());
a38118e5
PA
6163 else
6164 uiout->field_core_addr ("addr",
6165 loc->gdbarch, loc->address);
6166 }
6167 annotate_field (5);
6168 if (!header_of_multiple)
6169 print_breakpoint_location (b, loc);
6170 if (b->loc)
6171 *last_loc = b->loc;
6172 }
6173 }
6c95b8df 6174
998580f1 6175 if (loc != NULL && !header_of_multiple)
6c95b8df 6176 {
5c632425 6177 std::vector<int> inf_nums;
998580f1 6178 int mi_only = 1;
6c95b8df 6179
08036331 6180 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6181 {
6182 if (inf->pspace == loc->pspace)
5c632425 6183 inf_nums.push_back (inf->num);
6c95b8df 6184 }
998580f1 6185
dda83cd7 6186 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6187 there are several. Always display for MI. */
6188 if (allflag
6189 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6190 && (program_spaces.size () > 1
998580f1
MK
6191 || number_of_inferiors () > 1)
6192 /* LOC is for existing B, it cannot be in
6193 moribund_locations and thus having NULL OWNER. */
6194 && loc->owner->type != bp_catchpoint))
6195 mi_only = 0;
5c632425 6196 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6197 }
6198
4a306c9a 6199 if (!part_of_multiple)
c4093a6a 6200 {
4a306c9a
JB
6201 if (b->thread != -1)
6202 {
6203 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6204 "stop only in" line a little further down. */
112e8700 6205 uiout->text (" thread ");
381befee 6206 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6207 }
6208 else if (b->task != 0)
6209 {
112e8700 6210 uiout->text (" task ");
381befee 6211 uiout->field_signed ("task", b->task);
4a306c9a 6212 }
c4093a6a 6213 }
f1310107 6214
112e8700 6215 uiout->text ("\n");
f1310107 6216
348d480f 6217 if (!part_of_multiple)
f1310107
TJB
6218 b->ops->print_one_detail (b, uiout);
6219
0d381245 6220 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6221 {
6222 annotate_field (6);
112e8700 6223 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6224 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6225 the frame ID. */
112e8700 6226 uiout->field_core_addr ("frame",
5af949e3 6227 b->gdbarch, b->frame_id.stack_addr);
112e8700 6228 uiout->text ("\n");
c4093a6a
JM
6229 }
6230
28010a5d 6231 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6232 {
6233 annotate_field (7);
d77f58be 6234 if (is_tracepoint (b))
112e8700 6235 uiout->text ("\ttrace only if ");
1042e4c0 6236 else
112e8700
SM
6237 uiout->text ("\tstop only if ");
6238 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6239
6240 /* Print whether the target is doing the breakpoint's condition
6241 evaluation. If GDB is doing the evaluation, don't print anything. */
6242 if (is_breakpoint (b)
6243 && breakpoint_condition_evaluation_mode ()
6244 == condition_evaluation_target)
6245 {
6a831f06
PA
6246 uiout->message (" (%pF evals)",
6247 string_field ("evaluated-by",
6248 bp_condition_evaluator (b)));
b775012e 6249 }
112e8700 6250 uiout->text ("\n");
0101ce28
JJ
6251 }
6252
0d381245 6253 if (!part_of_multiple && b->thread != -1)
c4093a6a 6254 {
4a64f543 6255 /* FIXME should make an annotation for this. */
112e8700
SM
6256 uiout->text ("\tstop only in thread ");
6257 if (uiout->is_mi_like_p ())
381befee 6258 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6259 else
6260 {
6261 struct thread_info *thr = find_thread_global_id (b->thread);
6262
112e8700 6263 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6264 }
112e8700 6265 uiout->text ("\n");
c4093a6a
JM
6266 }
6267
556ec64d
YQ
6268 if (!part_of_multiple)
6269 {
6270 if (b->hit_count)
31f56a27
YQ
6271 {
6272 /* FIXME should make an annotation for this. */
6273 if (is_catchpoint (b))
112e8700 6274 uiout->text ("\tcatchpoint");
31f56a27 6275 else if (is_tracepoint (b))
112e8700 6276 uiout->text ("\ttracepoint");
31f56a27 6277 else
112e8700
SM
6278 uiout->text ("\tbreakpoint");
6279 uiout->text (" already hit ");
381befee 6280 uiout->field_signed ("times", b->hit_count);
31f56a27 6281 if (b->hit_count == 1)
112e8700 6282 uiout->text (" time\n");
31f56a27 6283 else
112e8700 6284 uiout->text (" times\n");
31f56a27 6285 }
556ec64d
YQ
6286 else
6287 {
31f56a27 6288 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6289 if (uiout->is_mi_like_p ())
381befee 6290 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6291 }
6292 }
8b93c638 6293
0d381245 6294 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6295 {
6296 annotate_field (8);
6a831f06
PA
6297 uiout->message ("\tignore next %pF hits\n",
6298 signed_field ("ignore", b->ignore_count));
c4093a6a 6299 }
059fb39f 6300
816338b5
SS
6301 /* Note that an enable count of 1 corresponds to "enable once"
6302 behavior, which is reported by the combination of enablement and
6303 disposition, so we don't need to mention it here. */
6304 if (!part_of_multiple && b->enable_count > 1)
6305 {
6306 annotate_field (8);
112e8700 6307 uiout->text ("\tdisable after ");
816338b5
SS
6308 /* Tweak the wording to clarify that ignore and enable counts
6309 are distinct, and have additive effect. */
6310 if (b->ignore_count)
112e8700 6311 uiout->text ("additional ");
816338b5 6312 else
112e8700 6313 uiout->text ("next ");
381befee 6314 uiout->field_signed ("enable", b->enable_count);
112e8700 6315 uiout->text (" hits\n");
816338b5
SS
6316 }
6317
f196051f
SS
6318 if (!part_of_multiple && is_tracepoint (b))
6319 {
6320 struct tracepoint *tp = (struct tracepoint *) b;
6321
6322 if (tp->traceframe_usage)
6323 {
112e8700 6324 uiout->text ("\ttrace buffer usage ");
381befee 6325 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6326 uiout->text (" bytes\n");
f196051f
SS
6327 }
6328 }
d3ce09f5 6329
d1b0a7bf 6330 l = b->commands ? b->commands.get () : NULL;
059fb39f 6331 if (!part_of_multiple && l)
c4093a6a
JM
6332 {
6333 annotate_field (9);
2e783024 6334 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6335 print_command_lines (uiout, l, 4);
c4093a6a 6336 }
d24317b4 6337
d9b3f62e 6338 if (is_tracepoint (b))
1042e4c0 6339 {
d9b3f62e
PA
6340 struct tracepoint *t = (struct tracepoint *) b;
6341
6342 if (!part_of_multiple && t->pass_count)
6343 {
6344 annotate_field (10);
112e8700 6345 uiout->text ("\tpass count ");
381befee 6346 uiout->field_signed ("pass", t->pass_count);
112e8700 6347 uiout->text (" \n");
d9b3f62e 6348 }
f2a8bc8a
YQ
6349
6350 /* Don't display it when tracepoint or tracepoint location is
6351 pending. */
6352 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6353 {
6354 annotate_field (11);
6355
112e8700
SM
6356 if (uiout->is_mi_like_p ())
6357 uiout->field_string ("installed",
f2a8bc8a
YQ
6358 loc->inserted ? "y" : "n");
6359 else
6360 {
6361 if (loc->inserted)
112e8700 6362 uiout->text ("\t");
f2a8bc8a 6363 else
112e8700
SM
6364 uiout->text ("\tnot ");
6365 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6366 }
6367 }
1042e4c0
SS
6368 }
6369
112e8700 6370 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6371 {
3a5c3e22
PA
6372 if (is_watchpoint (b))
6373 {
6374 struct watchpoint *w = (struct watchpoint *) b;
6375
112e8700 6376 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6377 }
f00aae0f 6378 else if (b->location != NULL
d28cd78a 6379 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6380 uiout->field_string ("original-location",
d28cd78a 6381 event_location_to_string (b->location.get ()));
d24317b4 6382 }
c4093a6a 6383}
c5aa993b 6384
13674803
SM
6385/* See breakpoint.h. */
6386
6387bool fix_multi_location_breakpoint_output_globally = false;
6388
0d381245
VP
6389static void
6390print_one_breakpoint (struct breakpoint *b,
4a64f543 6391 struct bp_location **last_loc,
6c95b8df 6392 int allflag)
0d381245 6393{
79a45e25 6394 struct ui_out *uiout = current_uiout;
13674803
SM
6395 bool use_fixed_output
6396 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6397 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6398
b4be1b06 6399 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6400 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6401
b4be1b06
SM
6402 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6403 are outside. */
6404 if (!use_fixed_output)
6405 bkpt_tuple_emitter.reset ();
0d381245
VP
6406
6407 /* If this breakpoint has custom print function,
6408 it's already printed. Otherwise, print individual
6409 locations, if any. */
a38118e5
PA
6410 if (b->ops == NULL
6411 || b->ops->print_one == NULL
6412 || allflag)
0d381245 6413 {
4a64f543
MS
6414 /* If breakpoint has a single location that is disabled, we
6415 print it as if it had several locations, since otherwise it's
6416 hard to represent "breakpoint enabled, location disabled"
6417 situation.
6418
6419 Note that while hardware watchpoints have several locations
a38118e5
PA
6420 internally, that's not a property exposed to users.
6421
6422 Likewise, while catchpoints may be implemented with
6423 breakpoints (e.g., catch throw), that's not a property
6424 exposed to users. We do however display the internal
6425 breakpoint locations with "maint info breakpoints". */
6426 if (!is_hardware_watchpoint (b)
f06f1252
TT
6427 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6428 || is_ada_exception_catchpoint (b))
a38118e5 6429 && (allflag
b5fa468f
TBA
6430 || (b->loc && (b->loc->next
6431 || !b->loc->enabled
6432 || b->loc->disabled_by_cond))))
0d381245 6433 {
b4be1b06
SM
6434 gdb::optional<ui_out_emit_list> locations_list;
6435
6436 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6437 MI record. For later versions, place breakpoint locations in a
6438 list. */
6439 if (uiout->is_mi_like_p () && use_fixed_output)
6440 locations_list.emplace (uiout, "locations");
8d3788bd 6441
b4be1b06 6442 int n = 1;
40cb8ca5 6443 for (bp_location *loc : b->locations ())
8d3788bd 6444 {
b4be1b06 6445 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6446 print_one_breakpoint_location (b, loc, n, last_loc,
6447 allflag, allflag);
40cb8ca5 6448 n++;
8d3788bd 6449 }
0d381245
VP
6450 }
6451 }
6452}
6453
a6d9a66e
UW
6454static int
6455breakpoint_address_bits (struct breakpoint *b)
6456{
6457 int print_address_bits = 0;
a6d9a66e 6458
c6d81124
PA
6459 /* Software watchpoints that aren't watching memory don't have an
6460 address to print. */
6461 if (is_no_memory_software_watchpoint (b))
6462 return 0;
6463
40cb8ca5 6464 for (bp_location *loc : b->locations ())
a6d9a66e 6465 {
c7437ca6
PA
6466 int addr_bit;
6467
c7437ca6 6468 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6469 if (addr_bit > print_address_bits)
6470 print_address_bits = addr_bit;
6471 }
6472
6473 return print_address_bits;
6474}
0d381245 6475
65630365 6476/* See breakpoint.h. */
c5aa993b 6477
65630365
PA
6478void
6479print_breakpoint (breakpoint *b)
c4093a6a 6480{
a6d9a66e 6481 struct bp_location *dummy_loc = NULL;
65630365 6482 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6483}
c5aa993b 6484
09d682a4
TT
6485/* Return true if this breakpoint was set by the user, false if it is
6486 internal or momentary. */
6487
6488int
6489user_breakpoint_p (struct breakpoint *b)
6490{
46c6471b 6491 return b->number > 0;
09d682a4
TT
6492}
6493
93daf339
TT
6494/* See breakpoint.h. */
6495
6496int
6497pending_breakpoint_p (struct breakpoint *b)
6498{
6499 return b->loc == NULL;
6500}
6501
5c458ae8
SM
6502/* Print information on breakpoints (including watchpoints and tracepoints).
6503
6504 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6505 understood by number_or_range_parser. Only breakpoints included in this
6506 list are then printed.
6507
6508 If SHOW_INTERNAL is true, print internal breakpoints.
6509
6510 If FILTER is non-NULL, call it on each breakpoint and only include the
6511 ones for which it returns true.
6512
6513 Return the total number of breakpoints listed. */
c906108c 6514
d77f58be 6515static int
5c458ae8 6516breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6517 bool (*filter) (const struct breakpoint *))
c4093a6a 6518{
a6d9a66e 6519 struct bp_location *last_loc = NULL;
7f3b0473 6520 int nr_printable_breakpoints;
79a45b7d 6521 struct value_print_options opts;
a6d9a66e 6522 int print_address_bits = 0;
269b11a2 6523 int print_type_col_width = 14;
79a45e25 6524 struct ui_out *uiout = current_uiout;
b5fa468f 6525 bool has_disabled_by_cond_location = false;
269b11a2 6526
79a45b7d
TT
6527 get_user_print_options (&opts);
6528
4a64f543
MS
6529 /* Compute the number of rows in the table, as well as the size
6530 required for address fields. */
7f3b0473 6531 nr_printable_breakpoints = 0;
43892fdf 6532 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6533 {
6534 /* If we have a filter, only list the breakpoints it accepts. */
6535 if (filter && !filter (b))
6536 continue;
6537
5c458ae8 6538 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6539 accept. Skip the others. */
5c458ae8 6540 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6541 {
5c458ae8 6542 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6543 continue;
5c458ae8 6544 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6545 continue;
6546 }
269b11a2 6547
5c458ae8 6548 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6549 {
6550 int addr_bit, type_len;
a6d9a66e 6551
e5a67952
MS
6552 addr_bit = breakpoint_address_bits (b);
6553 if (addr_bit > print_address_bits)
6554 print_address_bits = addr_bit;
269b11a2 6555
e5a67952
MS
6556 type_len = strlen (bptype_string (b->type));
6557 if (type_len > print_type_col_width)
6558 print_type_col_width = type_len;
6559
6560 nr_printable_breakpoints++;
6561 }
6562 }
7f3b0473 6563
4a2b031d
TT
6564 {
6565 ui_out_emit_table table_emitter (uiout,
6566 opts.addressprint ? 6 : 5,
6567 nr_printable_breakpoints,
6568 "BreakpointTable");
6569
6570 if (nr_printable_breakpoints > 0)
6571 annotate_breakpoints_headers ();
6572 if (nr_printable_breakpoints > 0)
6573 annotate_field (0);
6574 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6575 if (nr_printable_breakpoints > 0)
6576 annotate_field (1);
6577 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6578 if (nr_printable_breakpoints > 0)
6579 annotate_field (2);
6580 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6581 if (nr_printable_breakpoints > 0)
6582 annotate_field (3);
6583 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6584 if (opts.addressprint)
6585 {
6586 if (nr_printable_breakpoints > 0)
6587 annotate_field (4);
6588 if (print_address_bits <= 32)
6589 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6590 else
6591 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6592 }
6593 if (nr_printable_breakpoints > 0)
6594 annotate_field (5);
6595 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6596 uiout->table_body ();
6597 if (nr_printable_breakpoints > 0)
6598 annotate_breakpoints_table ();
6599
43892fdf 6600 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6601 {
6602 QUIT;
6603 /* If we have a filter, only list the breakpoints it accepts. */
6604 if (filter && !filter (b))
6605 continue;
e5a67952 6606
5c458ae8 6607 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6608 accept. Skip the others. */
e5a67952 6609
5c458ae8 6610 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6611 {
5c458ae8 6612 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6613 {
5c458ae8 6614 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6615 continue;
6616 }
6617 else /* all others */
6618 {
5c458ae8 6619 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6620 continue;
6621 }
6622 }
6623 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6624 show_internal is set. */
6625 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6626 {
6627 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6628 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6629 if (loc->disabled_by_cond)
6630 has_disabled_by_cond_location = true;
6631 }
4a2b031d
TT
6632 }
6633 }
698384cd 6634
7f3b0473 6635 if (nr_printable_breakpoints == 0)
c906108c 6636 {
4a64f543
MS
6637 /* If there's a filter, let the caller decide how to report
6638 empty list. */
d77f58be
SS
6639 if (!filter)
6640 {
5c458ae8 6641 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6642 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6643 else
112e8700 6644 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6645 bp_num_list);
d77f58be 6646 }
c906108c
SS
6647 }
6648 else
c4093a6a 6649 {
a6d9a66e
UW
6650 if (last_loc && !server_command)
6651 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6652
85c88e2a 6653 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6654 uiout->message (_("(*): Breakpoint condition is invalid at this "
6655 "location.\n"));
c4093a6a 6656 }
c906108c 6657
4a64f543 6658 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6659 there have been breakpoints? */
c906108c 6660 annotate_breakpoints_table_end ();
d77f58be
SS
6661
6662 return nr_printable_breakpoints;
c906108c
SS
6663}
6664
ad443146
SS
6665/* Display the value of default-collect in a way that is generally
6666 compatible with the breakpoint list. */
6667
6668static void
6669default_collect_info (void)
6670{
79a45e25
PA
6671 struct ui_out *uiout = current_uiout;
6672
ad443146
SS
6673 /* If it has no value (which is frequently the case), say nothing; a
6674 message like "No default-collect." gets in user's face when it's
6675 not wanted. */
6676 if (!*default_collect)
6677 return;
6678
6679 /* The following phrase lines up nicely with per-tracepoint collect
6680 actions. */
112e8700
SM
6681 uiout->text ("default collect ");
6682 uiout->field_string ("default-collect", default_collect);
6683 uiout->text (" \n");
ad443146
SS
6684}
6685
c906108c 6686static void
0b39b52e 6687info_breakpoints_command (const char *args, int from_tty)
c906108c 6688{
5c458ae8 6689 breakpoint_1 (args, false, NULL);
ad443146
SS
6690
6691 default_collect_info ();
d77f58be
SS
6692}
6693
6694static void
1d12d88f 6695info_watchpoints_command (const char *args, int from_tty)
d77f58be 6696{
5c458ae8 6697 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6698 struct ui_out *uiout = current_uiout;
d77f58be
SS
6699
6700 if (num_printed == 0)
6701 {
e5a67952 6702 if (args == NULL || *args == '\0')
112e8700 6703 uiout->message ("No watchpoints.\n");
d77f58be 6704 else
112e8700 6705 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6706 }
c906108c
SS
6707}
6708
7a292a7a 6709static void
4495129a 6710maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6711{
5c458ae8 6712 breakpoint_1 (args, true, NULL);
ad443146
SS
6713
6714 default_collect_info ();
c906108c
SS
6715}
6716
0d381245 6717static int
714835d5 6718breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6719 struct program_space *pspace,
714835d5 6720 CORE_ADDR pc, struct obj_section *section)
0d381245 6721{
40cb8ca5 6722 for (bp_location *bl : b->locations ())
0d381245 6723 {
6c95b8df
PA
6724 if (bl->pspace == pspace
6725 && bl->address == pc
0d381245
VP
6726 && (!overlay_debugging || bl->section == section))
6727 return 1;
6728 }
6729 return 0;
6730}
6731
672f9b60 6732/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6733 concerns with logical breakpoints, so we match program spaces, not
6734 address spaces. */
c906108c
SS
6735
6736static void
6c95b8df
PA
6737describe_other_breakpoints (struct gdbarch *gdbarch,
6738 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6739 struct obj_section *section, int thread)
c906108c 6740{
52f0bd74 6741 int others = 0;
c906108c 6742
43892fdf 6743 for (breakpoint *b : all_breakpoints ())
672f9b60 6744 others += (user_breakpoint_p (b)
dda83cd7 6745 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6746
c906108c
SS
6747 if (others > 0)
6748 {
a3f17187
AC
6749 if (others == 1)
6750 printf_filtered (_("Note: breakpoint "));
6751 else /* if (others == ???) */
6752 printf_filtered (_("Note: breakpoints "));
43892fdf 6753 for (breakpoint *b : all_breakpoints ())
672f9b60 6754 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6755 {
6756 others--;
6757 printf_filtered ("%d", b->number);
6758 if (b->thread == -1 && thread != -1)
6759 printf_filtered (" (all threads)");
6760 else if (b->thread != -1)
6761 printf_filtered (" (thread %d)", b->thread);
6762 printf_filtered ("%s%s ",
059fb39f 6763 ((b->enable_state == bp_disabled
f8eba3c6 6764 || b->enable_state == bp_call_disabled)
0d381245 6765 ? " (disabled)"
0d381245
VP
6766 : ""),
6767 (others > 1) ? ","
6768 : ((others == 1) ? " and" : ""));
6769 }
6a831f06
PA
6770 current_uiout->message (_("also set at pc %ps.\n"),
6771 styled_string (address_style.style (),
6772 paddress (gdbarch, pc)));
c906108c
SS
6773 }
6774}
6775\f
c906108c 6776
cb1e4e32
PA
6777/* Return true iff it is meaningful to use the address member of LOC.
6778 For some breakpoint types, the locations' address members are
6779 irrelevant and it makes no sense to attempt to compare them to
6780 other addresses (or use them for any other purpose either).
2d134ed3 6781
cb1e4e32
PA
6782 More specifically, software watchpoints and catchpoints that are
6783 not backed by breakpoints always have a zero valued location
6784 address and we don't want to mark breakpoints of any of these types
6785 to be a duplicate of an actual breakpoint location at address
6786 zero. */
e4f237da 6787
cb1e4e32
PA
6788static bool
6789bl_address_is_meaningful (bp_location *loc)
e4f237da 6790{
cb1e4e32 6791 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6792}
6793
6794/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6795 true if LOC1 and LOC2 represent the same watchpoint location. */
6796
6797static int
4a64f543
MS
6798watchpoint_locations_match (struct bp_location *loc1,
6799 struct bp_location *loc2)
2d134ed3 6800{
3a5c3e22
PA
6801 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6802 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6803
6804 /* Both of them must exist. */
6805 gdb_assert (w1 != NULL);
6806 gdb_assert (w2 != NULL);
2bdf28a0 6807
4a64f543
MS
6808 /* If the target can evaluate the condition expression in hardware,
6809 then we we need to insert both watchpoints even if they are at
6810 the same place. Otherwise the watchpoint will only trigger when
6811 the condition of whichever watchpoint was inserted evaluates to
6812 true, not giving a chance for GDB to check the condition of the
6813 other watchpoint. */
3a5c3e22 6814 if ((w1->cond_exp
4a64f543
MS
6815 && target_can_accel_watchpoint_condition (loc1->address,
6816 loc1->length,
0cf6dd15 6817 loc1->watchpoint_type,
4d01a485 6818 w1->cond_exp.get ()))
3a5c3e22 6819 || (w2->cond_exp
4a64f543
MS
6820 && target_can_accel_watchpoint_condition (loc2->address,
6821 loc2->length,
0cf6dd15 6822 loc2->watchpoint_type,
4d01a485 6823 w2->cond_exp.get ())))
0cf6dd15
TJB
6824 return 0;
6825
85d721b8
PA
6826 /* Note that this checks the owner's type, not the location's. In
6827 case the target does not support read watchpoints, but does
6828 support access watchpoints, we'll have bp_read_watchpoint
6829 watchpoints with hw_access locations. Those should be considered
6830 duplicates of hw_read locations. The hw_read locations will
6831 become hw_access locations later. */
2d134ed3
PA
6832 return (loc1->owner->type == loc2->owner->type
6833 && loc1->pspace->aspace == loc2->pspace->aspace
6834 && loc1->address == loc2->address
6835 && loc1->length == loc2->length);
e4f237da
KB
6836}
6837
31e77af2 6838/* See breakpoint.h. */
6c95b8df 6839
31e77af2 6840int
accd0bcd
YQ
6841breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6842 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6843{
f5656ead 6844 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6845 || aspace1 == aspace2)
6846 && addr1 == addr2);
6847}
6848
f1310107
TJB
6849/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6850 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6851 matches ASPACE2. On targets that have global breakpoints, the address
6852 space doesn't really matter. */
6853
6854static int
accd0bcd
YQ
6855breakpoint_address_match_range (const address_space *aspace1,
6856 CORE_ADDR addr1,
6857 int len1, const address_space *aspace2,
f1310107
TJB
6858 CORE_ADDR addr2)
6859{
f5656ead 6860 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6861 || aspace1 == aspace2)
6862 && addr2 >= addr1 && addr2 < addr1 + len1);
6863}
6864
6865/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6866 a ranged breakpoint. In most targets, a match happens only if ASPACE
6867 matches the breakpoint's address space. On targets that have global
6868 breakpoints, the address space doesn't really matter. */
6869
6870static int
6871breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6872 const address_space *aspace,
f1310107
TJB
6873 CORE_ADDR addr)
6874{
6875 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6876 aspace, addr)
6877 || (bl->length
6878 && breakpoint_address_match_range (bl->pspace->aspace,
6879 bl->address, bl->length,
6880 aspace, addr)));
6881}
6882
d35ae833
PA
6883/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6884 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6885 match happens only if ASPACE matches the breakpoint's address
6886 space. On targets that have global breakpoints, the address space
6887 doesn't really matter. */
6888
6889static int
6890breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6891 const address_space *aspace,
d35ae833
PA
6892 CORE_ADDR addr, int len)
6893{
6894 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6895 || bl->pspace->aspace == aspace)
6896 {
6897 int bl_len = bl->length != 0 ? bl->length : 1;
6898
6899 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6900 return 1;
6901 }
6902 return 0;
6903}
6904
1e4d1764
YQ
6905/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6906 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6907 true, otherwise returns false. */
6908
6909static int
6910tracepoint_locations_match (struct bp_location *loc1,
6911 struct bp_location *loc2)
6912{
6913 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6914 /* Since tracepoint locations are never duplicated with others', tracepoint
6915 locations at the same address of different tracepoints are regarded as
6916 different locations. */
6917 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6918 else
6919 return 0;
6920}
6921
2d134ed3 6922/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6923 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6924 the same location. If SW_HW_BPS_MATCH is true, then software
6925 breakpoint locations and hardware breakpoint locations match,
6926 otherwise they don't. */
2d134ed3
PA
6927
6928static int
7f32a4d5
PA
6929breakpoint_locations_match (struct bp_location *loc1,
6930 struct bp_location *loc2,
6931 bool sw_hw_bps_match)
2d134ed3 6932{
2bdf28a0
JK
6933 int hw_point1, hw_point2;
6934
6935 /* Both of them must not be in moribund_locations. */
6936 gdb_assert (loc1->owner != NULL);
6937 gdb_assert (loc2->owner != NULL);
6938
6939 hw_point1 = is_hardware_watchpoint (loc1->owner);
6940 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6941
6942 if (hw_point1 != hw_point2)
6943 return 0;
6944 else if (hw_point1)
6945 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6946 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6947 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6948 else
7f32a4d5
PA
6949 /* We compare bp_location.length in order to cover ranged
6950 breakpoints. Keep this in sync with
6951 bp_location_is_less_than. */
f1310107
TJB
6952 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6953 loc2->pspace->aspace, loc2->address)
7f32a4d5 6954 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6955 && loc1->length == loc2->length);
2d134ed3
PA
6956}
6957
76897487
KB
6958static void
6959breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 6960 int bnum, int have_bnum)
76897487 6961{
f63fbe86
MS
6962 /* The longest string possibly returned by hex_string_custom
6963 is 50 chars. These must be at least that big for safety. */
6964 char astr1[64];
6965 char astr2[64];
76897487 6966
bb599908
PH
6967 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6968 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6969 if (have_bnum)
8a3fe4f8 6970 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 6971 bnum, astr1, astr2);
76897487 6972 else
8a3fe4f8 6973 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6974}
6975
4a64f543
MS
6976/* Adjust a breakpoint's address to account for architectural
6977 constraints on breakpoint placement. Return the adjusted address.
6978 Note: Very few targets require this kind of adjustment. For most
6979 targets, this function is simply the identity function. */
76897487
KB
6980
6981static CORE_ADDR
a6d9a66e
UW
6982adjust_breakpoint_address (struct gdbarch *gdbarch,
6983 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6984{
a0de8c21
YQ
6985 if (bptype == bp_watchpoint
6986 || bptype == bp_hardware_watchpoint
6987 || bptype == bp_read_watchpoint
6988 || bptype == bp_access_watchpoint
6989 || bptype == bp_catchpoint)
88f7da05
KB
6990 {
6991 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 6992 have their addresses modified. */
88f7da05
KB
6993 return bpaddr;
6994 }
7c16b83e
PA
6995 else if (bptype == bp_single_step)
6996 {
6997 /* Single-step breakpoints should not have their addresses
6998 modified. If there's any architectural constrain that
6999 applies to this address, then it should have already been
7000 taken into account when the breakpoint was created in the
7001 first place. If we didn't do this, stepping through e.g.,
7002 Thumb-2 IT blocks would break. */
7003 return bpaddr;
7004 }
76897487
KB
7005 else
7006 {
a0de8c21
YQ
7007 CORE_ADDR adjusted_bpaddr = bpaddr;
7008
7009 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7010 {
7011 /* Some targets have architectural constraints on the placement
7012 of breakpoint instructions. Obtain the adjusted address. */
7013 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7014 }
76897487 7015
a0de8c21 7016 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7017
7018 /* An adjusted breakpoint address can significantly alter
dda83cd7 7019 a user's expectations. Print a warning if an adjustment
76897487
KB
7020 is required. */
7021 if (adjusted_bpaddr != bpaddr)
7022 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7023
7024 return adjusted_bpaddr;
7025 }
7026}
7027
cb1e4e32
PA
7028static bp_loc_type
7029bp_location_from_bp_type (bptype type)
7cc221ef 7030{
cb1e4e32 7031 switch (type)
e049a4b5
DJ
7032 {
7033 case bp_breakpoint:
7c16b83e 7034 case bp_single_step:
e049a4b5
DJ
7035 case bp_until:
7036 case bp_finish:
7037 case bp_longjmp:
7038 case bp_longjmp_resume:
e2e4d78b 7039 case bp_longjmp_call_dummy:
186c406b
TT
7040 case bp_exception:
7041 case bp_exception_resume:
e049a4b5 7042 case bp_step_resume:
2c03e5be 7043 case bp_hp_step_resume:
e049a4b5
DJ
7044 case bp_watchpoint_scope:
7045 case bp_call_dummy:
aa7d318d 7046 case bp_std_terminate:
e049a4b5
DJ
7047 case bp_shlib_event:
7048 case bp_thread_event:
7049 case bp_overlay_event:
4efc6507 7050 case bp_jit_event:
0fd8e87f 7051 case bp_longjmp_master:
aa7d318d 7052 case bp_std_terminate_master:
186c406b 7053 case bp_exception_master:
0e30163f
JK
7054 case bp_gnu_ifunc_resolver:
7055 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7056 case bp_dprintf:
cb1e4e32 7057 return bp_loc_software_breakpoint;
e049a4b5 7058 case bp_hardware_breakpoint:
cb1e4e32 7059 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7060 case bp_hardware_watchpoint:
7061 case bp_read_watchpoint:
7062 case bp_access_watchpoint:
cb1e4e32 7063 return bp_loc_hardware_watchpoint;
e049a4b5 7064 case bp_watchpoint:
ce78b96d 7065 case bp_catchpoint:
15c3d785
PA
7066 case bp_tracepoint:
7067 case bp_fast_tracepoint:
0fb4aa4b 7068 case bp_static_tracepoint:
cb1e4e32 7069 return bp_loc_other;
e049a4b5 7070 default:
e2e0b3e5 7071 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7072 }
cb1e4e32
PA
7073}
7074
7075bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7076{
7077 this->owner = owner;
7078 this->cond_bytecode = NULL;
7079 this->shlib_disabled = 0;
7080 this->enabled = 1;
b5fa468f 7081 this->disabled_by_cond = false;
cb1e4e32
PA
7082
7083 this->loc_type = type;
e049a4b5 7084
cb1e4e32
PA
7085 if (this->loc_type == bp_loc_software_breakpoint
7086 || this->loc_type == bp_loc_hardware_breakpoint)
7087 mark_breakpoint_location_modified (this);
7088
b6433ede 7089 incref ();
cb1e4e32
PA
7090}
7091
7092bp_location::bp_location (breakpoint *owner)
7093 : bp_location::bp_location (owner,
7094 bp_location_from_bp_type (owner->type))
7095{
28010a5d
PA
7096}
7097
7098/* Allocate a struct bp_location. */
7099
7100static struct bp_location *
7101allocate_bp_location (struct breakpoint *bpt)
7102{
348d480f
PA
7103 return bpt->ops->allocate_location (bpt);
7104}
7cc221ef 7105
f431efe5
PA
7106/* Decrement reference count. If the reference count reaches 0,
7107 destroy the bp_location. Sets *BLP to NULL. */
7108
7109static void
7110decref_bp_location (struct bp_location **blp)
7111{
b6433ede 7112 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7113 *blp = NULL;
7114}
7115
346774a9 7116/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7117
b270e6f9
TT
7118static breakpoint *
7119add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7120{
346774a9 7121 struct breakpoint *b1;
b270e6f9 7122 struct breakpoint *result = b.get ();
c906108c 7123
346774a9
PA
7124 /* Add this breakpoint to the end of the chain so that a list of
7125 breakpoints will come out in order of increasing numbers. */
7126
7127 b1 = breakpoint_chain;
7128 if (b1 == 0)
b270e6f9 7129 breakpoint_chain = b.release ();
346774a9
PA
7130 else
7131 {
7132 while (b1->next)
7133 b1 = b1->next;
b270e6f9 7134 b1->next = b.release ();
346774a9 7135 }
b270e6f9
TT
7136
7137 return result;
346774a9
PA
7138}
7139
7140/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7141
7142static void
7143init_raw_breakpoint_without_location (struct breakpoint *b,
7144 struct gdbarch *gdbarch,
28010a5d 7145 enum bptype bptype,
c0a91b2b 7146 const struct breakpoint_ops *ops)
346774a9 7147{
348d480f
PA
7148 gdb_assert (ops != NULL);
7149
28010a5d 7150 b->ops = ops;
4d28f7a8 7151 b->type = bptype;
a6d9a66e 7152 b->gdbarch = gdbarch;
c906108c
SS
7153 b->language = current_language->la_language;
7154 b->input_radix = input_radix;
d0fb5eae 7155 b->related_breakpoint = b;
346774a9
PA
7156}
7157
7158/* Helper to set_raw_breakpoint below. Creates a breakpoint
7159 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7160
7161static struct breakpoint *
7162set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7163 enum bptype bptype,
c0a91b2b 7164 const struct breakpoint_ops *ops)
346774a9 7165{
3b0871f4 7166 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7167
3b0871f4 7168 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7169 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7170}
7171
0ba852ab 7172/* Initialize loc->function_name. */
0e30163f 7173
0d381245 7174static void
0ba852ab 7175set_breakpoint_location_function (struct bp_location *loc)
0d381245 7176{
2bdf28a0
JK
7177 gdb_assert (loc->owner != NULL);
7178
0d381245 7179 if (loc->owner->type == bp_breakpoint
1042e4c0 7180 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7181 || is_tracepoint (loc->owner))
0d381245 7182 {
2c02bd72 7183 const char *function_name;
0e30163f 7184
3467ec66 7185 if (loc->msymbol != NULL
f50776aa 7186 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7187 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7188 {
7189 struct breakpoint *b = loc->owner;
7190
c9d95fa3 7191 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7192
7193 if (b->type == bp_breakpoint && b->loc == loc
7194 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7195 {
7196 /* Create only the whole new breakpoint of this type but do not
7197 mess more complicated breakpoints with multiple locations. */
7198 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7199 /* Remember the resolver's address for use by the return
dda83cd7 7200 breakpoint. */
3467ec66 7201 loc->related_address = loc->address;
0e30163f
JK
7202 }
7203 }
3467ec66
PA
7204 else
7205 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7206
2c02bd72
DE
7207 if (function_name)
7208 loc->function_name = xstrdup (function_name);
0d381245
VP
7209 }
7210}
7211
a6d9a66e 7212/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7213struct gdbarch *
a6d9a66e
UW
7214get_sal_arch (struct symtab_and_line sal)
7215{
7216 if (sal.section)
08feed99 7217 return sal.section->objfile->arch ();
a6d9a66e 7218 if (sal.symtab)
08feed99 7219 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7220
7221 return NULL;
7222}
7223
346774a9
PA
7224/* Low level routine for partially initializing a breakpoint of type
7225 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7226 file name, and line number are provided by SAL.
0d381245
VP
7227
7228 It is expected that the caller will complete the initialization of
7229 the newly created breakpoint struct as well as output any status
c56053d2 7230 information regarding the creation of a new breakpoint. */
0d381245 7231
346774a9
PA
7232static void
7233init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7234 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7235 const struct breakpoint_ops *ops)
0d381245 7236{
28010a5d 7237 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7238
3742cc8b 7239 add_location_to_breakpoint (b, &sal);
0d381245 7240
6c95b8df
PA
7241 if (bptype != bp_catchpoint)
7242 gdb_assert (sal.pspace != NULL);
7243
f8eba3c6
TT
7244 /* Store the program space that was used to set the breakpoint,
7245 except for ordinary breakpoints, which are independent of the
7246 program space. */
7247 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7248 b->pspace = sal.pspace;
346774a9 7249}
c906108c 7250
346774a9
PA
7251/* set_raw_breakpoint is a low level routine for allocating and
7252 partially initializing a breakpoint of type BPTYPE. The newly
7253 created breakpoint's address, section, source file name, and line
7254 number are provided by SAL. The newly created and partially
7255 initialized breakpoint is added to the breakpoint chain and
7256 is also returned as the value of this function.
7257
7258 It is expected that the caller will complete the initialization of
7259 the newly created breakpoint struct as well as output any status
7260 information regarding the creation of a new breakpoint. In
7261 particular, set_raw_breakpoint does NOT set the breakpoint
7262 number! Care should be taken to not allow an error to occur
7263 prior to completing the initialization of the breakpoint. If this
7264 should happen, a bogus breakpoint will be left on the chain. */
7265
7266struct breakpoint *
7267set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7268 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7269 const struct breakpoint_ops *ops)
346774a9 7270{
3b0871f4 7271 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7272
3b0871f4 7273 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7274 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7275}
7276
53a5351d 7277/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7278 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7279 initiated the operation. */
c906108c
SS
7280
7281void
186c406b 7282set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7283{
5d5658a1 7284 int thread = tp->global_num;
0fd8e87f
UW
7285
7286 /* To avoid having to rescan all objfile symbols at every step,
7287 we maintain a list of continually-inserted but always disabled
7288 longjmp "master" breakpoints. Here, we simply create momentary
7289 clones of those and enable them for the requested thread. */
1428b37a 7290 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7291 if (b->pspace == current_program_space
186c406b
TT
7292 && (b->type == bp_longjmp_master
7293 || b->type == bp_exception_master))
0fd8e87f 7294 {
06edf0c0
PA
7295 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7296 struct breakpoint *clone;
cc59ec59 7297
e2e4d78b
JK
7298 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7299 after their removal. */
06edf0c0 7300 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7301 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7302 clone->thread = thread;
7303 }
186c406b
TT
7304
7305 tp->initiating_frame = frame;
c906108c
SS
7306}
7307
611c83ae 7308/* Delete all longjmp breakpoints from THREAD. */
c906108c 7309void
611c83ae 7310delete_longjmp_breakpoint (int thread)
c906108c 7311{
1428b37a 7312 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7313 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7314 {
7315 if (b->thread == thread)
7316 delete_breakpoint (b);
7317 }
c906108c
SS
7318}
7319
f59f708a
PA
7320void
7321delete_longjmp_breakpoint_at_next_stop (int thread)
7322{
1428b37a 7323 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7324 if (b->type == bp_longjmp || b->type == bp_exception)
7325 {
7326 if (b->thread == thread)
7327 b->disposition = disp_del_at_next_stop;
7328 }
7329}
7330
e2e4d78b
JK
7331/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7332 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7333 pointer to any of them. Return NULL if this system cannot place longjmp
7334 breakpoints. */
7335
7336struct breakpoint *
7337set_longjmp_breakpoint_for_call_dummy (void)
7338{
43892fdf 7339 breakpoint *retval = nullptr;
e2e4d78b 7340
43892fdf 7341 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7342 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7343 {
7344 struct breakpoint *new_b;
7345
7346 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7347 &momentary_breakpoint_ops,
7348 1);
00431a78 7349 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7350
7351 /* Link NEW_B into the chain of RETVAL breakpoints. */
7352
7353 gdb_assert (new_b->related_breakpoint == new_b);
7354 if (retval == NULL)
7355 retval = new_b;
7356 new_b->related_breakpoint = retval;
7357 while (retval->related_breakpoint != new_b->related_breakpoint)
7358 retval = retval->related_breakpoint;
7359 retval->related_breakpoint = new_b;
7360 }
7361
7362 return retval;
7363}
7364
7365/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7366 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7367 stack.
7368
b4b3e2de
AB
7369 If the unwind fails then there is not sufficient information to discard
7370 dummy frames. In this case, elide the clean up and the dummy frames will
7371 be cleaned up next time this function is called from a location where
7372 unwinding is possible. */
e2e4d78b
JK
7373
7374void
b67a2c6f 7375check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7376{
7377 struct breakpoint *b, *b_tmp;
7378
7379 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7380 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7381 {
7382 struct breakpoint *dummy_b = b->related_breakpoint;
7383
b4b3e2de
AB
7384 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7385 chained off b->related_breakpoint. */
e2e4d78b
JK
7386 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7387 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7388
7389 /* If there was no bp_call_dummy breakpoint then there's nothing
7390 more to do. Or, if the dummy frame associated with the
7391 bp_call_dummy is still on the stack then we need to leave this
7392 bp_call_dummy in place. */
e2e4d78b
JK
7393 if (dummy_b->type != bp_call_dummy
7394 || frame_find_by_id (dummy_b->frame_id) != NULL)
7395 continue;
b4b3e2de
AB
7396
7397 /* We didn't find the dummy frame on the stack, this could be
7398 because we have longjmp'd to a stack frame that is previous to
7399 the dummy frame, or it could be because the stack unwind is
7400 broken at some point between the longjmp frame and the dummy
7401 frame.
7402
7403 Next we figure out why the stack unwind stopped. If it looks
7404 like the unwind is complete then we assume the dummy frame has
7405 been jumped over, however, if the unwind stopped for an
7406 unexpected reason then we assume the stack unwind is currently
7407 broken, and that we will (eventually) return to the dummy
7408 frame.
7409
7410 It might be tempting to consider using frame_id_inner here, but
7411 that is not safe. There is no guarantee that the stack frames
7412 we are looking at here are even on the same stack as the
7413 original dummy frame, hence frame_id_inner can't be used. See
7414 the comments on frame_id_inner for more details. */
7415 bool unwind_finished_unexpectedly = false;
7416 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7417 {
7418 struct frame_info *prev = get_prev_frame (fi);
7419 if (prev == nullptr)
7420 {
7421 /* FI is the last stack frame. Why did this frame not
7422 unwind further? */
7423 auto stop_reason = get_frame_unwind_stop_reason (fi);
7424 if (stop_reason != UNWIND_NO_REASON
7425 && stop_reason != UNWIND_OUTERMOST)
7426 unwind_finished_unexpectedly = true;
7427 }
7428 fi = prev;
7429 }
7430 if (unwind_finished_unexpectedly)
7431 continue;
7432
00431a78 7433 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7434
7435 while (b->related_breakpoint != b)
7436 {
7437 if (b_tmp == b->related_breakpoint)
7438 b_tmp = b->related_breakpoint->next;
7439 delete_breakpoint (b->related_breakpoint);
7440 }
7441 delete_breakpoint (b);
7442 }
7443}
7444
1900040c
MS
7445void
7446enable_overlay_breakpoints (void)
7447{
43892fdf 7448 for (breakpoint *b : all_breakpoints ())
1900040c 7449 if (b->type == bp_overlay_event)
01add95b
SM
7450 {
7451 b->enable_state = bp_enabled;
7452 update_global_location_list (UGLL_MAY_INSERT);
7453 overlay_events_enabled = 1;
7454 }
1900040c
MS
7455}
7456
7457void
7458disable_overlay_breakpoints (void)
7459{
43892fdf 7460 for (breakpoint *b : all_breakpoints ())
1900040c 7461 if (b->type == bp_overlay_event)
01add95b
SM
7462 {
7463 b->enable_state = bp_disabled;
7464 update_global_location_list (UGLL_DONT_INSERT);
7465 overlay_events_enabled = 0;
7466 }
1900040c
MS
7467}
7468
aa7d318d
TT
7469/* Set an active std::terminate breakpoint for each std::terminate
7470 master breakpoint. */
7471void
7472set_std_terminate_breakpoint (void)
7473{
1428b37a 7474 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7475 if (b->pspace == current_program_space
7476 && b->type == bp_std_terminate_master)
7477 {
06edf0c0 7478 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7479 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7480 }
7481}
7482
7483/* Delete all the std::terminate breakpoints. */
7484void
7485delete_std_terminate_breakpoint (void)
7486{
1428b37a 7487 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7488 if (b->type == bp_std_terminate)
7489 delete_breakpoint (b);
7490}
7491
c4093a6a 7492struct breakpoint *
a6d9a66e 7493create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7494{
7495 struct breakpoint *b;
c4093a6a 7496
06edf0c0
PA
7497 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7498 &internal_breakpoint_ops);
7499
b5de0fa7 7500 b->enable_state = bp_enabled;
f00aae0f 7501 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7502 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7503
44702360 7504 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7505
c4093a6a
JM
7506 return b;
7507}
7508
0101ce28
JJ
7509struct lang_and_radix
7510 {
7511 enum language lang;
7512 int radix;
7513 };
7514
4efc6507
DE
7515/* Create a breakpoint for JIT code registration and unregistration. */
7516
7517struct breakpoint *
7518create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7519{
2a7f3dff
PA
7520 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7521 &internal_breakpoint_ops);
4efc6507 7522}
0101ce28 7523
03673fc7
PP
7524/* Remove JIT code registration and unregistration breakpoint(s). */
7525
7526void
7527remove_jit_event_breakpoints (void)
7528{
1428b37a 7529 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7530 if (b->type == bp_jit_event
7531 && b->loc->pspace == current_program_space)
7532 delete_breakpoint (b);
7533}
7534
cae688ec
JJ
7535void
7536remove_solib_event_breakpoints (void)
7537{
1428b37a 7538 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7539 if (b->type == bp_shlib_event
7540 && b->loc->pspace == current_program_space)
cae688ec
JJ
7541 delete_breakpoint (b);
7542}
7543
f37f681c
PA
7544/* See breakpoint.h. */
7545
7546void
7547remove_solib_event_breakpoints_at_next_stop (void)
7548{
1428b37a 7549 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7550 if (b->type == bp_shlib_event
7551 && b->loc->pspace == current_program_space)
7552 b->disposition = disp_del_at_next_stop;
7553}
7554
04086b45
PA
7555/* Helper for create_solib_event_breakpoint /
7556 create_and_insert_solib_event_breakpoint. Allows specifying which
7557 INSERT_MODE to pass through to update_global_location_list. */
7558
7559static struct breakpoint *
7560create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7561 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7562{
7563 struct breakpoint *b;
7564
06edf0c0
PA
7565 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7566 &internal_breakpoint_ops);
04086b45 7567 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7568 return b;
7569}
7570
04086b45
PA
7571struct breakpoint *
7572create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7573{
7574 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7575}
7576
f37f681c
PA
7577/* See breakpoint.h. */
7578
7579struct breakpoint *
7580create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7581{
7582 struct breakpoint *b;
7583
04086b45
PA
7584 /* Explicitly tell update_global_location_list to insert
7585 locations. */
7586 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7587 if (!b->loc->inserted)
7588 {
7589 delete_breakpoint (b);
7590 return NULL;
7591 }
7592 return b;
7593}
7594
cae688ec
JJ
7595/* Disable any breakpoints that are on code in shared libraries. Only
7596 apply to enabled breakpoints, disabled ones can just stay disabled. */
7597
7598void
cb851954 7599disable_breakpoints_in_shlibs (void)
cae688ec 7600{
48d7020b 7601 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7602 {
7603 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7604 struct breakpoint *b = loc->owner;
7605
7606 /* We apply the check to all breakpoints, including disabled for
7607 those with loc->duplicate set. This is so that when breakpoint
7608 becomes enabled, or the duplicate is removed, gdb will try to
7609 insert all breakpoints. If we don't set shlib_disabled here,
7610 we'll try to insert those breakpoints and fail. */
7611 if (((b->type == bp_breakpoint)
7612 || (b->type == bp_jit_event)
7613 || (b->type == bp_hardware_breakpoint)
7614 || (is_tracepoint (b)))
7615 && loc->pspace == current_program_space
7616 && !loc->shlib_disabled
7617 && solib_name_from_address (loc->pspace, loc->address)
7618 )
7619 {
7620 loc->shlib_disabled = 1;
7621 }
7622 }
cae688ec
JJ
7623}
7624
63644780
NB
7625/* Disable any breakpoints and tracepoints that are in SOLIB upon
7626 notification of unloaded_shlib. Only apply to enabled breakpoints,
7627 disabled ones can just stay disabled. */
84acb35a 7628
75149521 7629static void
84acb35a
JJ
7630disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7631{
84acb35a
JJ
7632 int disabled_shlib_breaks = 0;
7633
48d7020b 7634 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7635 {
7636 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7637 struct breakpoint *b = loc->owner;
7638
7639 if (solib->pspace == loc->pspace
7640 && !loc->shlib_disabled
7641 && (((b->type == bp_breakpoint
7642 || b->type == bp_jit_event
7643 || b->type == bp_hardware_breakpoint)
7644 && (loc->loc_type == bp_loc_hardware_breakpoint
7645 || loc->loc_type == bp_loc_software_breakpoint))
7646 || is_tracepoint (b))
7647 && solib_contains_address_p (solib, loc->address))
7648 {
7649 loc->shlib_disabled = 1;
7650 /* At this point, we cannot rely on remove_breakpoint
7651 succeeding so we must mark the breakpoint as not inserted
7652 to prevent future errors occurring in remove_breakpoints. */
7653 loc->inserted = 0;
7654
7655 /* This may cause duplicate notifications for the same breakpoint. */
7656 gdb::observers::breakpoint_modified.notify (b);
7657
7658 if (!disabled_shlib_breaks)
7659 {
7660 target_terminal::ours_for_output ();
7661 warning (_("Temporarily disabling breakpoints "
7662 "for unloaded shared library \"%s\""),
7663 solib->so_name);
7664 }
7665 disabled_shlib_breaks = 1;
7666 }
7667 }
84acb35a
JJ
7668}
7669
63644780
NB
7670/* Disable any breakpoints and tracepoints in OBJFILE upon
7671 notification of free_objfile. Only apply to enabled breakpoints,
7672 disabled ones can just stay disabled. */
7673
7674static void
7675disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7676{
63644780
NB
7677 if (objfile == NULL)
7678 return;
7679
d03de421
PA
7680 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7681 managed by the user with add-symbol-file/remove-symbol-file.
7682 Similarly to how breakpoints in shared libraries are handled in
7683 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7684 shlib_disabled so they end up uninserted on the next global
7685 location list update. Shared libraries not loaded by the user
7686 aren't handled here -- they're already handled in
7687 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7688 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7689 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7690 main objfile). */
7691 if ((objfile->flags & OBJF_SHARED) == 0
7692 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7693 return;
7694
43892fdf 7695 for (breakpoint *b : all_breakpoints ())
63644780 7696 {
63644780
NB
7697 int bp_modified = 0;
7698
7699 if (!is_breakpoint (b) && !is_tracepoint (b))
7700 continue;
7701
40cb8ca5 7702 for (bp_location *loc : b->locations ())
63644780
NB
7703 {
7704 CORE_ADDR loc_addr = loc->address;
7705
7706 if (loc->loc_type != bp_loc_hardware_breakpoint
7707 && loc->loc_type != bp_loc_software_breakpoint)
7708 continue;
7709
7710 if (loc->shlib_disabled != 0)
7711 continue;
7712
7713 if (objfile->pspace != loc->pspace)
7714 continue;
7715
7716 if (loc->loc_type != bp_loc_hardware_breakpoint
7717 && loc->loc_type != bp_loc_software_breakpoint)
7718 continue;
7719
7720 if (is_addr_in_objfile (loc_addr, objfile))
7721 {
7722 loc->shlib_disabled = 1;
08351840
PA
7723 /* At this point, we don't know whether the object was
7724 unmapped from the inferior or not, so leave the
7725 inserted flag alone. We'll handle failure to
7726 uninsert quietly, in case the object was indeed
7727 unmapped. */
63644780
NB
7728
7729 mark_breakpoint_location_modified (loc);
7730
7731 bp_modified = 1;
7732 }
7733 }
7734
7735 if (bp_modified)
76727919 7736 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7737 }
7738}
7739
ce78b96d
JB
7740/* FORK & VFORK catchpoints. */
7741
e29a4733 7742/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7743 catchpoint. A breakpoint is really of this type iff its ops pointer points
7744 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7745
c1fc2657 7746struct fork_catchpoint : public breakpoint
e29a4733 7747{
e29a4733
PA
7748 /* Process id of a child process whose forking triggered this
7749 catchpoint. This field is only valid immediately after this
7750 catchpoint has triggered. */
7751 ptid_t forked_inferior_pid;
7752};
7753
4a64f543
MS
7754/* Implement the "insert" breakpoint_ops method for fork
7755 catchpoints. */
ce78b96d 7756
77b06cd7
TJB
7757static int
7758insert_catch_fork (struct bp_location *bl)
ce78b96d 7759{
e99b03dc 7760 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7761}
7762
4a64f543
MS
7763/* Implement the "remove" breakpoint_ops method for fork
7764 catchpoints. */
ce78b96d
JB
7765
7766static int
73971819 7767remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7768{
e99b03dc 7769 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7770}
7771
7772/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7773 catchpoints. */
7774
7775static int
f1310107 7776breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7777 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7778 const struct target_waitstatus *ws)
ce78b96d 7779{
e29a4733
PA
7780 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7781
f90263c1
TT
7782 if (ws->kind != TARGET_WAITKIND_FORKED)
7783 return 0;
7784
7785 c->forked_inferior_pid = ws->value.related_pid;
7786 return 1;
ce78b96d
JB
7787}
7788
4a64f543
MS
7789/* Implement the "print_it" breakpoint_ops method for fork
7790 catchpoints. */
ce78b96d
JB
7791
7792static enum print_stop_action
348d480f 7793print_it_catch_fork (bpstat bs)
ce78b96d 7794{
36dfb11c 7795 struct ui_out *uiout = current_uiout;
348d480f
PA
7796 struct breakpoint *b = bs->breakpoint_at;
7797 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7798
ce78b96d 7799 annotate_catchpoint (b->number);
f303dbd6 7800 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7801 if (b->disposition == disp_del)
112e8700 7802 uiout->text ("Temporary catchpoint ");
36dfb11c 7803 else
112e8700
SM
7804 uiout->text ("Catchpoint ");
7805 if (uiout->is_mi_like_p ())
36dfb11c 7806 {
112e8700
SM
7807 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7808 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7809 }
381befee 7810 uiout->field_signed ("bkptno", b->number);
112e8700 7811 uiout->text (" (forked process ");
381befee 7812 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7813 uiout->text ("), ");
ce78b96d
JB
7814 return PRINT_SRC_AND_LOC;
7815}
7816
4a64f543
MS
7817/* Implement the "print_one" breakpoint_ops method for fork
7818 catchpoints. */
ce78b96d
JB
7819
7820static void
a6d9a66e 7821print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7822{
e29a4733 7823 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7824 struct value_print_options opts;
79a45e25 7825 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7826
7827 get_user_print_options (&opts);
7828
4a64f543
MS
7829 /* Field 4, the address, is omitted (which makes the columns not
7830 line up too nicely with the headers, but the effect is relatively
7831 readable). */
79a45b7d 7832 if (opts.addressprint)
112e8700 7833 uiout->field_skip ("addr");
ce78b96d 7834 annotate_field (5);
112e8700 7835 uiout->text ("fork");
d7e15655 7836 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7837 {
112e8700 7838 uiout->text (", process ");
381befee 7839 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7840 uiout->spaces (1);
ce78b96d 7841 }
8ac3646f 7842
112e8700
SM
7843 if (uiout->is_mi_like_p ())
7844 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7845}
7846
7847/* Implement the "print_mention" breakpoint_ops method for fork
7848 catchpoints. */
7849
7850static void
7851print_mention_catch_fork (struct breakpoint *b)
7852{
7853 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7854}
7855
6149aea9
PA
7856/* Implement the "print_recreate" breakpoint_ops method for fork
7857 catchpoints. */
7858
7859static void
7860print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7861{
7862 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7863 print_recreate_thread (b, fp);
6149aea9
PA
7864}
7865
ce78b96d
JB
7866/* The breakpoint_ops structure to be used in fork catchpoints. */
7867
2060206e 7868static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7869
4a64f543
MS
7870/* Implement the "insert" breakpoint_ops method for vfork
7871 catchpoints. */
ce78b96d 7872
77b06cd7
TJB
7873static int
7874insert_catch_vfork (struct bp_location *bl)
ce78b96d 7875{
e99b03dc 7876 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7877}
7878
4a64f543
MS
7879/* Implement the "remove" breakpoint_ops method for vfork
7880 catchpoints. */
ce78b96d
JB
7881
7882static int
73971819 7883remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7884{
e99b03dc 7885 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7886}
7887
7888/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7889 catchpoints. */
7890
7891static int
f1310107 7892breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7893 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7894 const struct target_waitstatus *ws)
ce78b96d 7895{
e29a4733
PA
7896 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7897
f90263c1
TT
7898 if (ws->kind != TARGET_WAITKIND_VFORKED)
7899 return 0;
7900
7901 c->forked_inferior_pid = ws->value.related_pid;
7902 return 1;
ce78b96d
JB
7903}
7904
4a64f543
MS
7905/* Implement the "print_it" breakpoint_ops method for vfork
7906 catchpoints. */
ce78b96d
JB
7907
7908static enum print_stop_action
348d480f 7909print_it_catch_vfork (bpstat bs)
ce78b96d 7910{
36dfb11c 7911 struct ui_out *uiout = current_uiout;
348d480f 7912 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7913 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7914
ce78b96d 7915 annotate_catchpoint (b->number);
f303dbd6 7916 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7917 if (b->disposition == disp_del)
112e8700 7918 uiout->text ("Temporary catchpoint ");
36dfb11c 7919 else
112e8700
SM
7920 uiout->text ("Catchpoint ");
7921 if (uiout->is_mi_like_p ())
36dfb11c 7922 {
112e8700
SM
7923 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7924 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7925 }
381befee 7926 uiout->field_signed ("bkptno", b->number);
112e8700 7927 uiout->text (" (vforked process ");
381befee 7928 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7929 uiout->text ("), ");
ce78b96d
JB
7930 return PRINT_SRC_AND_LOC;
7931}
7932
4a64f543
MS
7933/* Implement the "print_one" breakpoint_ops method for vfork
7934 catchpoints. */
ce78b96d
JB
7935
7936static void
a6d9a66e 7937print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7938{
e29a4733 7939 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7940 struct value_print_options opts;
79a45e25 7941 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7942
7943 get_user_print_options (&opts);
4a64f543
MS
7944 /* Field 4, the address, is omitted (which makes the columns not
7945 line up too nicely with the headers, but the effect is relatively
7946 readable). */
79a45b7d 7947 if (opts.addressprint)
112e8700 7948 uiout->field_skip ("addr");
ce78b96d 7949 annotate_field (5);
112e8700 7950 uiout->text ("vfork");
d7e15655 7951 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7952 {
112e8700 7953 uiout->text (", process ");
381befee 7954 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7955 uiout->spaces (1);
ce78b96d 7956 }
8ac3646f 7957
112e8700
SM
7958 if (uiout->is_mi_like_p ())
7959 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7960}
7961
7962/* Implement the "print_mention" breakpoint_ops method for vfork
7963 catchpoints. */
7964
7965static void
7966print_mention_catch_vfork (struct breakpoint *b)
7967{
7968 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7969}
7970
6149aea9
PA
7971/* Implement the "print_recreate" breakpoint_ops method for vfork
7972 catchpoints. */
7973
7974static void
7975print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7976{
7977 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7978 print_recreate_thread (b, fp);
6149aea9
PA
7979}
7980
ce78b96d
JB
7981/* The breakpoint_ops structure to be used in vfork catchpoints. */
7982
2060206e 7983static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7984
edcc5120 7985/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7986 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7987 CATCH_SOLIB_BREAKPOINT_OPS. */
7988
c1fc2657 7989struct solib_catchpoint : public breakpoint
edcc5120 7990{
c1fc2657 7991 ~solib_catchpoint () override;
edcc5120
TT
7992
7993 /* True for "catch load", false for "catch unload". */
b650a282 7994 bool is_load;
edcc5120
TT
7995
7996 /* Regular expression to match, if any. COMPILED is only valid when
7997 REGEX is non-NULL. */
7998 char *regex;
2d7cc5c7 7999 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8000};
8001
c1fc2657 8002solib_catchpoint::~solib_catchpoint ()
edcc5120 8003{
c1fc2657 8004 xfree (this->regex);
edcc5120
TT
8005}
8006
8007static int
8008insert_catch_solib (struct bp_location *ignore)
8009{
8010 return 0;
8011}
8012
8013static int
73971819 8014remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8015{
8016 return 0;
8017}
8018
8019static int
8020breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8021 const address_space *aspace,
edcc5120
TT
8022 CORE_ADDR bp_addr,
8023 const struct target_waitstatus *ws)
8024{
8025 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
edcc5120
TT
8026
8027 if (ws->kind == TARGET_WAITKIND_LOADED)
8028 return 1;
8029
43892fdf 8030 for (breakpoint *other : all_breakpoints ())
01add95b
SM
8031 {
8032 if (other == bl->owner)
8033 continue;
edcc5120 8034
01add95b
SM
8035 if (other->type != bp_shlib_event)
8036 continue;
edcc5120 8037
01add95b
SM
8038 if (self->pspace != NULL && other->pspace != self->pspace)
8039 continue;
edcc5120 8040
01add95b
SM
8041 for (bp_location *other_bl : other->locations ())
8042 {
8043 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8044 return 1;
8045 }
8046 }
edcc5120
TT
8047
8048 return 0;
8049}
8050
8051static void
8052check_status_catch_solib (struct bpstats *bs)
8053{
8054 struct solib_catchpoint *self
8055 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8056
8057 if (self->is_load)
8058 {
52941706 8059 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8060 {
8061 if (!self->regex
2d7cc5c7 8062 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8063 return;
8064 }
8065 }
8066 else
8067 {
6fb16ce6 8068 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8069 {
8070 if (!self->regex
6fb16ce6 8071 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8072 return;
8073 }
8074 }
8075
8076 bs->stop = 0;
8077 bs->print_it = print_it_noop;
8078}
8079
8080static enum print_stop_action
8081print_it_catch_solib (bpstat bs)
8082{
8083 struct breakpoint *b = bs->breakpoint_at;
8084 struct ui_out *uiout = current_uiout;
8085
8086 annotate_catchpoint (b->number);
f303dbd6 8087 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8088 if (b->disposition == disp_del)
112e8700 8089 uiout->text ("Temporary catchpoint ");
edcc5120 8090 else
112e8700 8091 uiout->text ("Catchpoint ");
381befee 8092 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8093 uiout->text ("\n");
8094 if (uiout->is_mi_like_p ())
8095 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8096 print_solib_event (1);
8097 return PRINT_SRC_AND_LOC;
8098}
8099
8100static void
8101print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8102{
8103 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8104 struct value_print_options opts;
8105 struct ui_out *uiout = current_uiout;
edcc5120
TT
8106
8107 get_user_print_options (&opts);
8108 /* Field 4, the address, is omitted (which makes the columns not
8109 line up too nicely with the headers, but the effect is relatively
8110 readable). */
8111 if (opts.addressprint)
8112 {
8113 annotate_field (4);
112e8700 8114 uiout->field_skip ("addr");
edcc5120
TT
8115 }
8116
528e1572 8117 std::string msg;
edcc5120
TT
8118 annotate_field (5);
8119 if (self->is_load)
8120 {
8121 if (self->regex)
528e1572 8122 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8123 else
528e1572 8124 msg = _("load of library");
edcc5120
TT
8125 }
8126 else
8127 {
8128 if (self->regex)
528e1572 8129 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8130 else
528e1572 8131 msg = _("unload of library");
edcc5120 8132 }
112e8700 8133 uiout->field_string ("what", msg);
8ac3646f 8134
112e8700
SM
8135 if (uiout->is_mi_like_p ())
8136 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8137}
8138
8139static void
8140print_mention_catch_solib (struct breakpoint *b)
8141{
8142 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8143
8144 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8145 self->is_load ? "load" : "unload");
8146}
8147
8148static void
8149print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8150{
8151 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8152
8153 fprintf_unfiltered (fp, "%s %s",
8154 b->disposition == disp_del ? "tcatch" : "catch",
8155 self->is_load ? "load" : "unload");
8156 if (self->regex)
8157 fprintf_unfiltered (fp, " %s", self->regex);
8158 fprintf_unfiltered (fp, "\n");
8159}
8160
8161static struct breakpoint_ops catch_solib_breakpoint_ops;
8162
b650a282 8163/* See breakpoint.h. */
edcc5120 8164
91985142 8165void
b650a282 8166add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8167{
edcc5120 8168 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8169
edcc5120
TT
8170 if (!arg)
8171 arg = "";
f1735a53 8172 arg = skip_spaces (arg);
edcc5120 8173
36bd8eaa 8174 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8175
8176 if (*arg != '\0')
8177 {
2d7cc5c7
PA
8178 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8179 _("Invalid regexp")));
edcc5120
TT
8180 c->regex = xstrdup (arg);
8181 }
8182
8183 c->is_load = is_load;
36bd8eaa 8184 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8185 &catch_solib_breakpoint_ops);
8186
c1fc2657 8187 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8188
b270e6f9 8189 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8190}
8191
91985142
MG
8192/* A helper function that does all the work for "catch load" and
8193 "catch unload". */
8194
8195static void
eb4c3f4a 8196catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8197 struct cmd_list_element *command)
8198{
91985142 8199 const int enabled = 1;
0f8e2034 8200 bool temp = command->context () == CATCH_TEMPORARY;
91985142 8201
b650a282 8202 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8203}
8204
edcc5120 8205static void
eb4c3f4a 8206catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8207 struct cmd_list_element *command)
8208{
8209 catch_load_or_unload (arg, from_tty, 1, command);
8210}
8211
8212static void
eb4c3f4a 8213catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8214 struct cmd_list_element *command)
8215{
8216 catch_load_or_unload (arg, from_tty, 0, command);
8217}
8218
b650a282 8219/* See breakpoint.h. */
ce78b96d 8220
ab04a2af 8221void
346774a9 8222init_catchpoint (struct breakpoint *b,
b650a282 8223 struct gdbarch *gdbarch, bool temp,
63160a43 8224 const char *cond_string,
c0a91b2b 8225 const struct breakpoint_ops *ops)
c906108c 8226{
51abb421 8227 symtab_and_line sal;
6c95b8df 8228 sal.pspace = current_program_space;
c5aa993b 8229
28010a5d 8230 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8231
1b36a34b 8232 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8233 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8234}
8235
28010a5d 8236void
b270e6f9 8237install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8238{
b270e6f9 8239 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8240 set_breakpoint_number (internal, b);
558a9d82
YQ
8241 if (is_tracepoint (b))
8242 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8243 if (!internal)
8244 mention (b);
76727919 8245 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8246
8247 if (update_gll)
44702360 8248 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8249}
8250
9b70b993 8251static void
a6d9a66e 8252create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8253 bool temp, const char *cond_string,
dda83cd7 8254 const struct breakpoint_ops *ops)
c906108c 8255{
b270e6f9 8256 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8257
b650a282 8258 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8259
8260 c->forked_inferior_pid = null_ptid;
8261
b270e6f9 8262 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8263}
8264
fe798b75
JB
8265/* Exec catchpoints. */
8266
b4d90040 8267/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8268 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8269 CATCH_EXEC_BREAKPOINT_OPS. */
8270
c1fc2657 8271struct exec_catchpoint : public breakpoint
b4d90040 8272{
c1fc2657 8273 ~exec_catchpoint () override;
b4d90040
PA
8274
8275 /* Filename of a program whose exec triggered this catchpoint.
8276 This field is only valid immediately after this catchpoint has
8277 triggered. */
8278 char *exec_pathname;
8279};
8280
c1fc2657 8281/* Exec catchpoint destructor. */
b4d90040 8282
c1fc2657 8283exec_catchpoint::~exec_catchpoint ()
b4d90040 8284{
c1fc2657 8285 xfree (this->exec_pathname);
b4d90040
PA
8286}
8287
77b06cd7
TJB
8288static int
8289insert_catch_exec (struct bp_location *bl)
c906108c 8290{
e99b03dc 8291 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8292}
c906108c 8293
fe798b75 8294static int
73971819 8295remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8296{
e99b03dc 8297 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8298}
c906108c 8299
fe798b75 8300static int
f1310107 8301breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8302 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8303 const struct target_waitstatus *ws)
fe798b75 8304{
b4d90040
PA
8305 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8306
f90263c1
TT
8307 if (ws->kind != TARGET_WAITKIND_EXECD)
8308 return 0;
8309
8310 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8311 return 1;
fe798b75 8312}
c906108c 8313
fe798b75 8314static enum print_stop_action
348d480f 8315print_it_catch_exec (bpstat bs)
fe798b75 8316{
36dfb11c 8317 struct ui_out *uiout = current_uiout;
348d480f 8318 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8319 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8320
fe798b75 8321 annotate_catchpoint (b->number);
f303dbd6 8322 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8323 if (b->disposition == disp_del)
112e8700 8324 uiout->text ("Temporary catchpoint ");
36dfb11c 8325 else
112e8700
SM
8326 uiout->text ("Catchpoint ");
8327 if (uiout->is_mi_like_p ())
36dfb11c 8328 {
112e8700
SM
8329 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8330 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8331 }
381befee 8332 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8333 uiout->text (" (exec'd ");
8334 uiout->field_string ("new-exec", c->exec_pathname);
8335 uiout->text ("), ");
36dfb11c 8336
fe798b75 8337 return PRINT_SRC_AND_LOC;
c906108c
SS
8338}
8339
fe798b75 8340static void
a6d9a66e 8341print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8342{
b4d90040 8343 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8344 struct value_print_options opts;
79a45e25 8345 struct ui_out *uiout = current_uiout;
fe798b75
JB
8346
8347 get_user_print_options (&opts);
8348
8349 /* Field 4, the address, is omitted (which makes the columns
8350 not line up too nicely with the headers, but the effect
8351 is relatively readable). */
8352 if (opts.addressprint)
112e8700 8353 uiout->field_skip ("addr");
fe798b75 8354 annotate_field (5);
112e8700 8355 uiout->text ("exec");
b4d90040 8356 if (c->exec_pathname != NULL)
fe798b75 8357 {
112e8700
SM
8358 uiout->text (", program \"");
8359 uiout->field_string ("what", c->exec_pathname);
8360 uiout->text ("\" ");
fe798b75 8361 }
8ac3646f 8362
112e8700
SM
8363 if (uiout->is_mi_like_p ())
8364 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8365}
8366
8367static void
8368print_mention_catch_exec (struct breakpoint *b)
8369{
8370 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8371}
8372
6149aea9
PA
8373/* Implement the "print_recreate" breakpoint_ops method for exec
8374 catchpoints. */
8375
8376static void
8377print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8378{
8379 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8380 print_recreate_thread (b, fp);
6149aea9
PA
8381}
8382
2060206e 8383static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8384
c906108c 8385static int
fba45db2 8386hw_breakpoint_used_count (void)
c906108c 8387{
c906108c
SS
8388 int i = 0;
8389
43892fdf 8390 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8391 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8392 for (bp_location *bl : b->locations ())
f1310107
TJB
8393 {
8394 /* Special types of hardware breakpoints may use more than
8395 one register. */
348d480f 8396 i += b->ops->resources_needed (bl);
f1310107 8397 }
c906108c
SS
8398
8399 return i;
8400}
8401
a1398e0c
PA
8402/* Returns the resources B would use if it were a hardware
8403 watchpoint. */
8404
c906108c 8405static int
a1398e0c 8406hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8407{
c906108c
SS
8408 int i = 0;
8409
a1398e0c
PA
8410 if (!breakpoint_enabled (b))
8411 return 0;
8412
40cb8ca5 8413 for (bp_location *bl : b->locations ())
a1398e0c
PA
8414 {
8415 /* Special types of hardware watchpoints may use more than
8416 one register. */
8417 i += b->ops->resources_needed (bl);
8418 }
8419
8420 return i;
8421}
8422
8423/* Returns the sum the used resources of all hardware watchpoints of
8424 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8425 the sum of the used resources of all hardware watchpoints of other
8426 types _not_ TYPE. */
8427
8428static int
8429hw_watchpoint_used_count_others (struct breakpoint *except,
8430 enum bptype type, int *other_type_used)
8431{
8432 int i = 0;
a1398e0c 8433
c906108c 8434 *other_type_used = 0;
43892fdf 8435 for (breakpoint *b : all_breakpoints ())
e09342b5 8436 {
a1398e0c
PA
8437 if (b == except)
8438 continue;
e09342b5
TJB
8439 if (!breakpoint_enabled (b))
8440 continue;
8441
a1398e0c
PA
8442 if (b->type == type)
8443 i += hw_watchpoint_use_count (b);
8444 else if (is_hardware_watchpoint (b))
8445 *other_type_used = 1;
e09342b5
TJB
8446 }
8447
c906108c
SS
8448 return i;
8449}
8450
c906108c 8451void
fba45db2 8452disable_watchpoints_before_interactive_call_start (void)
c906108c 8453{
43892fdf 8454 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8455 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8456 {
b5de0fa7 8457 b->enable_state = bp_call_disabled;
44702360 8458 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8459 }
c906108c
SS
8460}
8461
8462void
fba45db2 8463enable_watchpoints_after_interactive_call_stop (void)
c906108c 8464{
43892fdf 8465 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8466 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8467 {
b5de0fa7 8468 b->enable_state = bp_enabled;
44702360 8469 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8470 }
c906108c
SS
8471}
8472
8bea4e01
UW
8473void
8474disable_breakpoints_before_startup (void)
8475{
6c95b8df 8476 current_program_space->executing_startup = 1;
44702360 8477 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8478}
8479
8480void
8481enable_breakpoints_after_startup (void)
8482{
6c95b8df 8483 current_program_space->executing_startup = 0;
f8eba3c6 8484 breakpoint_re_set ();
8bea4e01
UW
8485}
8486
7c16b83e
PA
8487/* Create a new single-step breakpoint for thread THREAD, with no
8488 locations. */
c906108c 8489
7c16b83e
PA
8490static struct breakpoint *
8491new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8492{
b270e6f9 8493 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8494
b270e6f9 8495 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8496 &momentary_breakpoint_ops);
8497
8498 b->disposition = disp_donttouch;
8499 b->frame_id = null_frame_id;
8500
8501 b->thread = thread;
8502 gdb_assert (b->thread != 0);
8503
b270e6f9 8504 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8505}
8506
8507/* Set a momentary breakpoint of type TYPE at address specified by
8508 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8509 frame. */
c906108c 8510
454dafbd 8511breakpoint_up
a6d9a66e
UW
8512set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8513 struct frame_id frame_id, enum bptype type)
c906108c 8514{
52f0bd74 8515 struct breakpoint *b;
edb3359d 8516
193facb3
JK
8517 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8518 tail-called one. */
8519 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8520
06edf0c0 8521 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8522 b->enable_state = bp_enabled;
8523 b->disposition = disp_donttouch;
818dd999 8524 b->frame_id = frame_id;
c906108c 8525
00431a78 8526 b->thread = inferior_thread ()->global_num;
c906108c 8527
44702360 8528 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8529
454dafbd 8530 return breakpoint_up (b);
c906108c 8531}
611c83ae 8532
06edf0c0 8533/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8534 The new breakpoint will have type TYPE, use OPS as its
8535 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8536
06edf0c0
PA
8537static struct breakpoint *
8538momentary_breakpoint_from_master (struct breakpoint *orig,
8539 enum bptype type,
a1aa2221
LM
8540 const struct breakpoint_ops *ops,
8541 int loc_enabled)
e58b0e63
PA
8542{
8543 struct breakpoint *copy;
8544
06edf0c0 8545 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8546 copy->loc = allocate_bp_location (copy);
0ba852ab 8547 set_breakpoint_location_function (copy->loc);
e58b0e63 8548
a6d9a66e 8549 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8550 copy->loc->requested_address = orig->loc->requested_address;
8551 copy->loc->address = orig->loc->address;
8552 copy->loc->section = orig->loc->section;
6c95b8df 8553 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8554 copy->loc->probe = orig->loc->probe;
f8eba3c6 8555 copy->loc->line_number = orig->loc->line_number;
2f202fde 8556 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8557 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8558 copy->frame_id = orig->frame_id;
8559 copy->thread = orig->thread;
6c95b8df 8560 copy->pspace = orig->pspace;
e58b0e63
PA
8561
8562 copy->enable_state = bp_enabled;
8563 copy->disposition = disp_donttouch;
8564 copy->number = internal_breakpoint_number--;
8565
44702360 8566 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8567 return copy;
8568}
8569
06edf0c0
PA
8570/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8571 ORIG is NULL. */
8572
8573struct breakpoint *
8574clone_momentary_breakpoint (struct breakpoint *orig)
8575{
8576 /* If there's nothing to clone, then return nothing. */
8577 if (orig == NULL)
8578 return NULL;
8579
a1aa2221 8580 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8581}
8582
454dafbd 8583breakpoint_up
a6d9a66e
UW
8584set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8585 enum bptype type)
611c83ae
PA
8586{
8587 struct symtab_and_line sal;
8588
8589 sal = find_pc_line (pc, 0);
8590 sal.pc = pc;
8591 sal.section = find_pc_overlay (pc);
8592 sal.explicit_pc = 1;
8593
a6d9a66e 8594 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8595}
c906108c 8596\f
c5aa993b 8597
c906108c
SS
8598/* Tell the user we have just set a breakpoint B. */
8599
8600static void
fba45db2 8601mention (struct breakpoint *b)
c906108c 8602{
348d480f 8603 b->ops->print_mention (b);
2d33446d 8604 current_uiout->text ("\n");
c906108c 8605}
c906108c 8606\f
c5aa993b 8607
5133a315 8608static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8609
7f32a4d5
PA
8610/* Handle "set breakpoint auto-hw on".
8611
8612 If the explicitly specified breakpoint type is not hardware
8613 breakpoint, check the memory map to see whether the breakpoint
8614 address is in read-only memory.
8615
8616 - location type is not hardware breakpoint, memory is read-only.
8617 We change the type of the location to hardware breakpoint.
8618
8619 - location type is hardware breakpoint, memory is read-write. This
8620 means we've previously made the location hardware one, but then the
8621 memory map changed, so we undo.
8622*/
8623
8624static void
8625handle_automatic_hardware_breakpoints (bp_location *bl)
8626{
8627 if (automatic_hardware_breakpoints
8628 && bl->owner->type != bp_hardware_breakpoint
8629 && (bl->loc_type == bp_loc_software_breakpoint
8630 || bl->loc_type == bp_loc_hardware_breakpoint))
8631 {
8632 /* When breakpoints are removed, remove_breakpoints will use
8633 location types we've just set here, the only possible problem
8634 is that memory map has changed during running program, but
8635 it's not going to work anyway with current gdb. */
8636 mem_region *mr = lookup_mem_region (bl->address);
8637
8638 if (mr != nullptr)
8639 {
8640 enum bp_loc_type new_type;
8641
8642 if (mr->attrib.mode != MEM_RW)
8643 new_type = bp_loc_hardware_breakpoint;
8644 else
8645 new_type = bp_loc_software_breakpoint;
8646
8647 if (new_type != bl->loc_type)
8648 {
8649 static bool said = false;
8650
8651 bl->loc_type = new_type;
8652 if (!said)
8653 {
8654 fprintf_filtered (gdb_stdout,
8655 _("Note: automatically using "
8656 "hardware breakpoints for "
8657 "read-only addresses.\n"));
8658 said = true;
8659 }
8660 }
8661 }
8662 }
8663}
8664
0d381245 8665static struct bp_location *
39d61571 8666add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8667 const struct symtab_and_line *sal)
8668{
8669 struct bp_location *loc, **tmp;
3742cc8b
YQ
8670 CORE_ADDR adjusted_address;
8671 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8672
8673 if (loc_gdbarch == NULL)
8674 loc_gdbarch = b->gdbarch;
8675
8676 /* Adjust the breakpoint's address prior to allocating a location.
8677 Once we call allocate_bp_location(), that mostly uninitialized
8678 location will be placed on the location chain. Adjustment of the
8679 breakpoint may cause target_read_memory() to be called and we do
8680 not want its scan of the location chain to find a breakpoint and
8681 location that's only been partially initialized. */
8682 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8683 sal->pc, b->type);
0d381245 8684
d30113d4 8685 /* Sort the locations by their ADDRESS. */
39d61571 8686 loc = allocate_bp_location (b);
d30113d4
JK
8687 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8688 tmp = &((*tmp)->next))
0d381245 8689 ;
d30113d4 8690 loc->next = *tmp;
0d381245 8691 *tmp = loc;
3742cc8b 8692
0d381245 8693 loc->requested_address = sal->pc;
3742cc8b 8694 loc->address = adjusted_address;
6c95b8df 8695 loc->pspace = sal->pspace;
935676c9 8696 loc->probe.prob = sal->prob;
729662a5 8697 loc->probe.objfile = sal->objfile;
6c95b8df 8698 gdb_assert (loc->pspace != NULL);
0d381245 8699 loc->section = sal->section;
3742cc8b 8700 loc->gdbarch = loc_gdbarch;
f8eba3c6 8701 loc->line_number = sal->line;
2f202fde 8702 loc->symtab = sal->symtab;
4a27f119 8703 loc->symbol = sal->symbol;
3467ec66
PA
8704 loc->msymbol = sal->msymbol;
8705 loc->objfile = sal->objfile;
f8eba3c6 8706
0ba852ab 8707 set_breakpoint_location_function (loc);
1a853c52 8708
6ae88661
LM
8709 /* While by definition, permanent breakpoints are already present in the
8710 code, we don't mark the location as inserted. Normally one would expect
8711 that GDB could rely on that breakpoint instruction to stop the program,
8712 thus removing the need to insert its own breakpoint, except that executing
8713 the breakpoint instruction can kill the target instead of reporting a
8714 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8715 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8716 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8717 breakpoint be inserted normally results in QEMU knowing about the GDB
8718 breakpoint, and thus trap before the breakpoint instruction is executed.
8719 (If GDB later needs to continue execution past the permanent breakpoint,
8720 it manually increments the PC, thus avoiding executing the breakpoint
8721 instruction.) */
1a853c52 8722 if (bp_loc_is_permanent (loc))
6ae88661 8723 loc->permanent = 1;
1a853c52 8724
0d381245
VP
8725 return loc;
8726}
514f746b
AR
8727\f
8728
5133a315
LM
8729/* Return true if LOC is pointing to a permanent breakpoint,
8730 return false otherwise. */
1cf4d951 8731
5133a315 8732static bool
1cf4d951
PA
8733bp_loc_is_permanent (struct bp_location *loc)
8734{
514f746b
AR
8735 gdb_assert (loc != NULL);
8736
cb1e4e32
PA
8737 /* If we have a non-breakpoint-backed catchpoint or a software
8738 watchpoint, just return 0. We should not attempt to read from
8739 the addresses the locations of these breakpoint types point to.
5133a315 8740 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8741 memory. */
cb1e4e32 8742 if (!bl_address_is_meaningful (loc))
5133a315 8743 return false;
244558af 8744
5ed8105e 8745 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8746 switch_to_program_space_and_thread (loc->pspace);
5133a315 8747 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8748}
8749
e7e0cddf
SS
8750/* Build a command list for the dprintf corresponding to the current
8751 settings of the dprintf style options. */
8752
8753static void
8754update_dprintf_command_list (struct breakpoint *b)
8755{
8756 char *dprintf_args = b->extra_string;
8757 char *printf_line = NULL;
8758
8759 if (!dprintf_args)
8760 return;
8761
8762 dprintf_args = skip_spaces (dprintf_args);
8763
8764 /* Allow a comma, as it may have terminated a location, but don't
8765 insist on it. */
8766 if (*dprintf_args == ',')
8767 ++dprintf_args;
8768 dprintf_args = skip_spaces (dprintf_args);
8769
8770 if (*dprintf_args != '"')
8771 error (_("Bad format string, missing '\"'."));
8772
d3ce09f5 8773 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8774 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8775 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8776 {
8777 if (!dprintf_function)
8778 error (_("No function supplied for dprintf call"));
8779
8780 if (dprintf_channel && strlen (dprintf_channel) > 0)
8781 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8782 dprintf_function,
8783 dprintf_channel,
8784 dprintf_args);
8785 else
8786 printf_line = xstrprintf ("call (void) %s (%s)",
8787 dprintf_function,
8788 dprintf_args);
8789 }
d3ce09f5
SS
8790 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8791 {
8792 if (target_can_run_breakpoint_commands ())
8793 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8794 else
8795 {
8796 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8797 printf_line = xstrprintf ("printf %s", dprintf_args);
8798 }
8799 }
e7e0cddf
SS
8800 else
8801 internal_error (__FILE__, __LINE__,
8802 _("Invalid dprintf style."));
8803
f28045c2 8804 gdb_assert (printf_line != NULL);
e7e0cddf 8805
12973681
TT
8806 /* Manufacture a printf sequence. */
8807 struct command_line *printf_cmd_line
8808 = new struct command_line (simple_control, printf_line);
8809 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8810 command_lines_deleter ()));
e7e0cddf
SS
8811}
8812
8813/* Update all dprintf commands, making their command lists reflect
8814 current style settings. */
8815
8816static void
eb4c3f4a 8817update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8818 struct cmd_list_element *c)
8819{
43892fdf
SM
8820 for (breakpoint *b : all_breakpoints ())
8821 if (b->type == bp_dprintf)
e7e0cddf 8822 update_dprintf_command_list (b);
e7e0cddf 8823}
c3f6f71d 8824
f00aae0f
KS
8825/* Create a breakpoint with SAL as location. Use LOCATION
8826 as a description of the location, and COND_STRING
b35a8b2f
DE
8827 as condition expression. If LOCATION is NULL then create an
8828 "address location" from the address in the SAL. */
018d34a4
VP
8829
8830static void
d9b3f62e 8831init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8832 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8833 event_location_up &&location,
e1e01040
PA
8834 gdb::unique_xmalloc_ptr<char> filter,
8835 gdb::unique_xmalloc_ptr<char> cond_string,
8836 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8837 enum bptype type, enum bpdisp disposition,
8838 int thread, int task, int ignore_count,
c0a91b2b 8839 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8840 int enabled, int internal, unsigned flags,
8841 int display_canonical)
018d34a4 8842{
0d381245 8843 int i;
018d34a4
VP
8844
8845 if (type == bp_hardware_breakpoint)
8846 {
fbbd034e
AS
8847 int target_resources_ok;
8848
8849 i = hw_breakpoint_used_count ();
8850 target_resources_ok =
8851 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8852 i + 1, 0);
8853 if (target_resources_ok == 0)
8854 error (_("No hardware breakpoint support in the target."));
8855 else if (target_resources_ok < 0)
8856 error (_("Hardware breakpoints used exceeds limit."));
8857 }
8858
6c5b2ebe 8859 gdb_assert (!sals.empty ());
6c95b8df 8860
6c5b2ebe 8861 for (const auto &sal : sals)
0d381245 8862 {
0d381245
VP
8863 struct bp_location *loc;
8864
8865 if (from_tty)
5af949e3
UW
8866 {
8867 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8868 if (!loc_gdbarch)
8869 loc_gdbarch = gdbarch;
8870
8871 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8872 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8873 }
0d381245 8874
6c5b2ebe 8875 if (&sal == &sals[0])
0d381245 8876 {
d9b3f62e 8877 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8878 b->thread = thread;
4a306c9a 8879 b->task = task;
855a6e68 8880
e1e01040
PA
8881 b->cond_string = cond_string.release ();
8882 b->extra_string = extra_string.release ();
0d381245 8883 b->ignore_count = ignore_count;
41447f92 8884 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8885 b->disposition = disposition;
6c95b8df 8886
44f238bb
PA
8887 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8888 b->loc->inserted = 1;
8889
0fb4aa4b
PA
8890 if (type == bp_static_tracepoint)
8891 {
d9b3f62e 8892 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8893 struct static_tracepoint_marker marker;
8894
983af33b 8895 if (strace_marker_p (b))
0fb4aa4b
PA
8896 {
8897 /* We already know the marker exists, otherwise, we
8898 wouldn't see a sal for it. */
d28cd78a
TT
8899 const char *p
8900 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8901 const char *endp;
0fb4aa4b 8902
f1735a53 8903 p = skip_spaces (p);
0fb4aa4b 8904
f1735a53 8905 endp = skip_to_space (p);
0fb4aa4b 8906
5d9310c4 8907 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8908
3e43a32a
MS
8909 printf_filtered (_("Probed static tracepoint "
8910 "marker \"%s\"\n"),
5d9310c4 8911 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8912 }
8913 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8914 {
5d9310c4 8915 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8916
3e43a32a
MS
8917 printf_filtered (_("Probed static tracepoint "
8918 "marker \"%s\"\n"),
5d9310c4 8919 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8920 }
8921 else
3e43a32a
MS
8922 warning (_("Couldn't determine the static "
8923 "tracepoint marker to probe"));
0fb4aa4b
PA
8924 }
8925
0d381245
VP
8926 loc = b->loc;
8927 }
8928 else
018d34a4 8929 {
39d61571 8930 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8931 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8932 loc->inserted = 1;
0d381245
VP
8933 }
8934
b5fa468f
TBA
8935 /* Do not set breakpoint locations conditions yet. As locations
8936 are inserted, they get sorted based on their addresses. Let
8937 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8938
8939 /* Dynamic printf requires and uses additional arguments on the
8940 command line, otherwise it's an error. */
8941 if (type == bp_dprintf)
8942 {
8943 if (b->extra_string)
8944 update_dprintf_command_list (b);
8945 else
8946 error (_("Format string required"));
8947 }
8948 else if (b->extra_string)
588ae58c 8949 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8950 }
018d34a4 8951
b5fa468f
TBA
8952
8953 /* The order of the locations is now stable. Set the location
8954 condition using the location's number. */
8955 int loc_num = 1;
40cb8ca5 8956 for (bp_location *loc : b->locations ())
b5fa468f
TBA
8957 {
8958 if (b->cond_string != nullptr)
8959 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8960 loc_num);
8961
8962 ++loc_num;
8963 }
8964
56435ebe 8965 b->display_canonical = display_canonical;
f00aae0f 8966 if (location != NULL)
d28cd78a 8967 b->location = std::move (location);
018d34a4 8968 else
d28cd78a 8969 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8970 b->filter = std::move (filter);
d9b3f62e 8971}
018d34a4 8972
d9b3f62e
PA
8973static void
8974create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8975 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8976 event_location_up &&location,
e1e01040
PA
8977 gdb::unique_xmalloc_ptr<char> filter,
8978 gdb::unique_xmalloc_ptr<char> cond_string,
8979 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8980 enum bptype type, enum bpdisp disposition,
8981 int thread, int task, int ignore_count,
c0a91b2b 8982 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8983 int enabled, int internal, unsigned flags,
8984 int display_canonical)
d9b3f62e 8985{
a5e364af 8986 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8987
a5e364af 8988 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8989 sals, std::move (location),
e1e01040
PA
8990 std::move (filter),
8991 std::move (cond_string),
8992 std::move (extra_string),
d9b3f62e
PA
8993 type, disposition,
8994 thread, task, ignore_count,
8995 ops, from_tty,
44f238bb
PA
8996 enabled, internal, flags,
8997 display_canonical);
d9b3f62e 8998
b270e6f9 8999 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9000}
9001
9002/* Add SALS.nelts breakpoints to the breakpoint table. For each
9003 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9004 value. COND_STRING, if not NULL, specified the condition to be
9005 used for all breakpoints. Essentially the only case where
9006 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9007 function. In that case, it's still not possible to specify
9008 separate conditions for different overloaded functions, so
9009 we take just a single condition string.
9010
c3f6f71d 9011 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9012 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9013 array contents). If the function fails (error() is called), the
9014 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9015 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9016
9017static void
8cdf0e15 9018create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9019 struct linespec_result *canonical,
e1e01040
PA
9020 gdb::unique_xmalloc_ptr<char> cond_string,
9021 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9022 enum bptype type, enum bpdisp disposition,
9023 int thread, int task, int ignore_count,
c0a91b2b 9024 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9025 int enabled, int internal, unsigned flags)
c906108c 9026{
f8eba3c6 9027 if (canonical->pre_expanded)
6c5b2ebe 9028 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9029
6c5b2ebe 9030 for (const auto &lsal : canonical->lsals)
c3f6f71d 9031 {
f00aae0f 9032 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9033 'break', without arguments. */
ffc2605c 9034 event_location_up location
f00aae0f 9035 = (canonical->location != NULL
8e9e35b1 9036 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9037 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9038 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9039
6c5b2ebe 9040 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9041 std::move (location),
e1e01040
PA
9042 std::move (filter_string),
9043 std::move (cond_string),
9044 std::move (extra_string),
e7e0cddf 9045 type, disposition,
84f4c1fe 9046 thread, task, ignore_count, ops,
44f238bb 9047 from_tty, enabled, internal, flags,
56435ebe 9048 canonical->special_display);
c3f6f71d 9049 }
c3f6f71d 9050}
c906108c 9051
f00aae0f 9052/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9053 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9054 addresses found. LOCATION points to the end of the SAL (for
9055 linespec locations).
9998af43
TJB
9056
9057 The array and the line spec strings are allocated on the heap, it is
9058 the caller's responsibility to free them. */
c906108c 9059
b9362cc7 9060static void
626d2320 9061parse_breakpoint_sals (struct event_location *location,
58438ac1 9062 struct linespec_result *canonical)
c3f6f71d 9063{
f00aae0f
KS
9064 struct symtab_and_line cursal;
9065
9066 if (event_location_type (location) == LINESPEC_LOCATION)
9067 {
a20714ff 9068 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9069
a20714ff 9070 if (spec == NULL)
f00aae0f
KS
9071 {
9072 /* The last displayed codepoint, if it's valid, is our default
9073 breakpoint address. */
9074 if (last_displayed_sal_is_valid ())
9075 {
f00aae0f
KS
9076 /* Set sal's pspace, pc, symtab, and line to the values
9077 corresponding to the last call to print_frame_info.
9078 Be sure to reinitialize LINE with NOTCURRENT == 0
9079 as the breakpoint line number is inappropriate otherwise.
9080 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9081 symtab_and_line sal = get_last_displayed_sal ();
9082 CORE_ADDR pc = sal.pc;
9083
f00aae0f
KS
9084 sal = find_pc_line (pc, 0);
9085
9086 /* "break" without arguments is equivalent to "break *PC"
9087 where PC is the last displayed codepoint's address. So
9088 make sure to set sal.explicit_pc to prevent GDB from
9089 trying to expand the list of sals to include all other
9090 instances with the same symtab and line. */
9091 sal.pc = pc;
9092 sal.explicit_pc = 1;
9093
6c5b2ebe
PA
9094 struct linespec_sals lsal;
9095 lsal.sals = {sal};
f00aae0f
KS
9096 lsal.canonical = NULL;
9097
6c5b2ebe 9098 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9099 return;
9100 }
9101 else
9102 error (_("No default breakpoint address now."));
c906108c 9103 }
c906108c 9104 }
f00aae0f
KS
9105
9106 /* Force almost all breakpoints to be in terms of the
9107 current_source_symtab (which is decode_line_1's default).
9108 This should produce the results we want almost all of the
9109 time while leaving default_breakpoint_* alone.
9110
9111 ObjC: However, don't match an Objective-C method name which
9112 may have a '+' or '-' succeeded by a '['. */
9113 cursal = get_current_source_symtab_and_line ();
9114 if (last_displayed_sal_is_valid ())
c906108c 9115 {
a20714ff 9116 const char *spec = NULL;
cc80f267 9117
f00aae0f 9118 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9119 spec = get_linespec_location (location)->spec_string;
cc80f267 9120
f00aae0f 9121 if (!cursal.symtab
a20714ff
PA
9122 || (spec != NULL
9123 && strchr ("+-", spec[0]) != NULL
9124 && spec[1] != '['))
f00aae0f 9125 {
c2f4122d 9126 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9127 get_last_displayed_symtab (),
9128 get_last_displayed_line (),
9129 canonical, NULL, NULL);
9130 return;
9131 }
c906108c 9132 }
f00aae0f 9133
c2f4122d 9134 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9135 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9136}
c906108c 9137
c906108c 9138
c3f6f71d 9139/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9140 inserted as a breakpoint. If it can't throw an error. */
c906108c 9141
b9362cc7 9142static void
6c5b2ebe 9143breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9144{
6c5b2ebe
PA
9145 for (auto &sal : sals)
9146 resolve_sal_pc (&sal);
c3f6f71d
JM
9147}
9148
7a697b8d
SS
9149/* Fast tracepoints may have restrictions on valid locations. For
9150 instance, a fast tracepoint using a jump instead of a trap will
9151 likely have to overwrite more bytes than a trap would, and so can
9152 only be placed where the instruction is longer than the jump, or a
9153 multi-instruction sequence does not have a jump into the middle of
9154 it, etc. */
9155
9156static void
9157check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9158 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9159{
6c5b2ebe 9160 for (const auto &sal : sals)
7a697b8d 9161 {
f8eba3c6
TT
9162 struct gdbarch *sarch;
9163
6c5b2ebe 9164 sarch = get_sal_arch (sal);
f8eba3c6
TT
9165 /* We fall back to GDBARCH if there is no architecture
9166 associated with SAL. */
9167 if (sarch == NULL)
9168 sarch = gdbarch;
281d762b
TT
9169 std::string msg;
9170 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9171 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9172 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9173 }
9174}
9175
018d34a4
VP
9176/* Given TOK, a string specification of condition and thread, as
9177 accepted by the 'break' command, extract the condition
9178 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9179 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9180 If no condition is found, *COND_STRING is set to NULL.
9181 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9182
9183static void
bbc13ae3 9184find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9185 char **cond_string, int *thread, int *task,
9186 char **rest)
018d34a4
VP
9187{
9188 *cond_string = NULL;
9189 *thread = -1;
ed1d1739
KS
9190 *task = 0;
9191 *rest = NULL;
733d554a 9192 bool force = false;
ed1d1739 9193
018d34a4
VP
9194 while (tok && *tok)
9195 {
bbc13ae3 9196 const char *end_tok;
018d34a4 9197 int toklen;
bbc13ae3
KS
9198 const char *cond_start = NULL;
9199 const char *cond_end = NULL;
cc59ec59 9200
f1735a53 9201 tok = skip_spaces (tok);
e7e0cddf
SS
9202
9203 if ((*tok == '"' || *tok == ',') && rest)
9204 {
9205 *rest = savestring (tok, strlen (tok));
9206 return;
9207 }
9208
f1735a53 9209 end_tok = skip_to_space (tok);
d634f2de 9210
018d34a4 9211 toklen = end_tok - tok;
d634f2de 9212
018d34a4
VP
9213 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9214 {
9215 tok = cond_start = end_tok + 1;
733d554a
TBA
9216 try
9217 {
9218 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9219 }
9220 catch (const gdb_exception_error &)
9221 {
9222 if (!force)
9223 throw;
9224 else
9225 tok = tok + strlen (tok);
9226 }
018d34a4 9227 cond_end = tok;
d634f2de 9228 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9229 }
733d554a
TBA
9230 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9231 {
5759831a 9232 tok = tok + toklen;
733d554a
TBA
9233 force = true;
9234 }
018d34a4
VP
9235 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9236 {
5d5658a1
PA
9237 const char *tmptok;
9238 struct thread_info *thr;
d634f2de 9239
018d34a4 9240 tok = end_tok + 1;
5d5658a1 9241 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9242 if (tok == tmptok)
9243 error (_("Junk after thread keyword."));
5d5658a1 9244 *thread = thr->global_num;
bbc13ae3 9245 tok = tmptok;
018d34a4 9246 }
4a306c9a
JB
9247 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9248 {
9249 char *tmptok;
9250
9251 tok = end_tok + 1;
bbc13ae3 9252 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9253 if (tok == tmptok)
9254 error (_("Junk after task keyword."));
9255 if (!valid_task_id (*task))
b6199126 9256 error (_("Unknown task %d."), *task);
bbc13ae3 9257 tok = tmptok;
4a306c9a 9258 }
e7e0cddf
SS
9259 else if (rest)
9260 {
9261 *rest = savestring (tok, strlen (tok));
ccab2054 9262 return;
e7e0cddf 9263 }
018d34a4
VP
9264 else
9265 error (_("Junk at end of arguments."));
9266 }
9267}
9268
b5fa468f
TBA
9269/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9270 succeeds. The parsed values are written to COND_STRING, THREAD,
9271 TASK, and REST. See the comment of 'find_condition_and_thread'
9272 for the description of these parameters and INPUT. */
9273
9274static void
9275find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9276 const char *input, char **cond_string,
9277 int *thread, int *task, char **rest)
9278{
9279 int num_failures = 0;
9280 for (auto &sal : sals)
9281 {
9282 char *cond = nullptr;
9283 int thread_id = 0;
9284 int task_id = 0;
9285 char *remaining = nullptr;
9286
9287 /* Here we want to parse 'arg' to separate condition from thread
9288 number. But because parsing happens in a context and the
9289 contexts of sals might be different, try each until there is
9290 success. Finding one successful parse is sufficient for our
9291 goal. When setting the breakpoint we'll re-parse the
9292 condition in the context of each sal. */
9293 try
9294 {
9295 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9296 &task_id, &remaining);
9297 *cond_string = cond;
9298 *thread = thread_id;
9299 *task = task_id;
9300 *rest = remaining;
9301 break;
9302 }
9303 catch (const gdb_exception_error &e)
9304 {
9305 num_failures++;
9306 /* If no sal remains, do not continue. */
9307 if (num_failures == sals.size ())
9308 throw;
9309 }
9310 }
9311}
9312
0fb4aa4b
PA
9313/* Decode a static tracepoint marker spec. */
9314
6c5b2ebe 9315static std::vector<symtab_and_line>
f00aae0f 9316decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9317{
f00aae0f
KS
9318 const char *p = &(*arg_p)[3];
9319 const char *endp;
0fb4aa4b 9320
f1735a53 9321 p = skip_spaces (p);
0fb4aa4b 9322
f1735a53 9323 endp = skip_to_space (p);
0fb4aa4b 9324
81b1e71c 9325 std::string marker_str (p, endp - p);
0fb4aa4b 9326
5d9310c4
SM
9327 std::vector<static_tracepoint_marker> markers
9328 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9329 if (markers.empty ())
81b1e71c
TT
9330 error (_("No known static tracepoint marker named %s"),
9331 marker_str.c_str ());
0fb4aa4b 9332
6c5b2ebe 9333 std::vector<symtab_and_line> sals;
5d9310c4 9334 sals.reserve (markers.size ());
0fb4aa4b 9335
5d9310c4 9336 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9337 {
5d9310c4
SM
9338 symtab_and_line sal = find_pc_line (marker.address, 0);
9339 sal.pc = marker.address;
6c5b2ebe 9340 sals.push_back (sal);
5d9310c4 9341 }
0fb4aa4b 9342
0fb4aa4b
PA
9343 *arg_p = endp;
9344 return sals;
9345}
9346
bac7c5cf
GB
9347/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9348 according to IS_TRACEPOINT. */
9349
9350static const struct breakpoint_ops *
9351breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9352 bool is_tracepoint)
9353{
9354 if (is_tracepoint)
9355 {
9356 if (location_type == PROBE_LOCATION)
9357 return &tracepoint_probe_breakpoint_ops;
9358 else
9359 return &tracepoint_breakpoint_ops;
9360 }
9361 else
9362 {
9363 if (location_type == PROBE_LOCATION)
9364 return &bkpt_probe_breakpoint_ops;
9365 else
9366 return &bkpt_breakpoint_ops;
9367 }
9368}
9369
9370/* See breakpoint.h. */
9371
9372const struct breakpoint_ops *
9373breakpoint_ops_for_event_location (const struct event_location *location,
9374 bool is_tracepoint)
9375{
9376 if (location != nullptr)
9377 return breakpoint_ops_for_event_location_type
9378 (event_location_type (location), is_tracepoint);
9379 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9380}
9381
f00aae0f 9382/* See breakpoint.h. */
0101ce28 9383
8cdf0e15
VP
9384int
9385create_breakpoint (struct gdbarch *gdbarch,
626d2320 9386 struct event_location *location,
e1e01040
PA
9387 const char *cond_string,
9388 int thread, const char *extra_string,
10a636cc 9389 bool force_condition, int parse_extra,
0fb4aa4b 9390 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9391 int ignore_count,
9392 enum auto_boolean pending_break_support,
c0a91b2b 9393 const struct breakpoint_ops *ops,
44f238bb
PA
9394 int from_tty, int enabled, int internal,
9395 unsigned flags)
c3f6f71d 9396{
7efd8fc2 9397 struct linespec_result canonical;
0101ce28 9398 int pending = 0;
4a306c9a 9399 int task = 0;
86b17b60 9400 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9401
348d480f
PA
9402 gdb_assert (ops != NULL);
9403
f00aae0f
KS
9404 /* If extra_string isn't useful, set it to NULL. */
9405 if (extra_string != NULL && *extra_string == '\0')
9406 extra_string = NULL;
9407
a70b8144 9408 try
b78a6381 9409 {
f00aae0f 9410 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9411 }
230d2906 9412 catch (const gdb_exception_error &e)
0101ce28 9413 {
492d29ea
PA
9414 /* If caller is interested in rc value from parse, set
9415 value. */
9416 if (e.error == NOT_FOUND_ERROR)
0101ce28 9417 {
05ff989b
AC
9418 /* If pending breakpoint support is turned off, throw
9419 error. */
fa8d40ab
JJ
9420
9421 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9422 throw;
723a2275
VP
9423
9424 exception_print (gdb_stderr, e);
fa8d40ab 9425
dda83cd7 9426 /* If pending breakpoint support is auto query and the user
05ff989b 9427 selects no, then simply return the error code. */
059fb39f 9428 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9429 && !nquery (_("Make %s pending on future shared library load? "),
9430 bptype_string (type_wanted)))
fd9b8c24 9431 return 0;
fa8d40ab 9432
05ff989b
AC
9433 /* At this point, either the user was queried about setting
9434 a pending breakpoint and selected yes, or pending
9435 breakpoint behavior is on and thus a pending breakpoint
9436 is defaulted on behalf of the user. */
f00aae0f 9437 pending = 1;
0101ce28 9438 }
492d29ea 9439 else
eedc3f4f 9440 throw;
0101ce28 9441 }
492d29ea 9442
6c5b2ebe 9443 if (!pending && canonical.lsals.empty ())
492d29ea 9444 return 0;
c3f6f71d 9445
c3f6f71d
JM
9446 /* Resolve all line numbers to PC's and verify that the addresses
9447 are ok for the target. */
0101ce28 9448 if (!pending)
f8eba3c6 9449 {
6c5b2ebe
PA
9450 for (auto &lsal : canonical.lsals)
9451 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9452 }
c3f6f71d 9453
7a697b8d 9454 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9455 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9456 {
6c5b2ebe
PA
9457 for (const auto &lsal : canonical.lsals)
9458 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9459 }
7a697b8d 9460
c3f6f71d
JM
9461 /* Verify that condition can be parsed, before setting any
9462 breakpoints. Allocate a separate condition expression for each
4a64f543 9463 breakpoint. */
0101ce28 9464 if (!pending)
c3f6f71d 9465 {
e1e01040
PA
9466 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9467 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9468
f00aae0f 9469 if (parse_extra)
dda83cd7 9470 {
0878d0fa 9471 char *rest;
e1e01040 9472 char *cond;
52d361e1 9473
6c5b2ebe 9474 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9475
b5fa468f
TBA
9476 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9477 &cond, &thread, &task, &rest);
e1e01040
PA
9478 cond_string_copy.reset (cond);
9479 extra_string_copy.reset (rest);
dda83cd7 9480 }
2f069f6f 9481 else
dda83cd7 9482 {
f00aae0f
KS
9483 if (type_wanted != bp_dprintf
9484 && extra_string != NULL && *extra_string != '\0')
9485 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9486
10a636cc
TBA
9487 /* Check the validity of the condition. We should error out
9488 if the condition is invalid at all of the locations and
9489 if it is not forced. In the PARSE_EXTRA case above, this
9490 check is done when parsing the EXTRA_STRING. */
9491 if (cond_string != nullptr && !force_condition)
9492 {
9493 int num_failures = 0;
9494 const linespec_sals &lsal = canonical.lsals[0];
9495 for (const auto &sal : lsal.sals)
9496 {
9497 const char *cond = cond_string;
9498 try
9499 {
9500 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9501 /* One success is sufficient to keep going. */
9502 break;
9503 }
9504 catch (const gdb_exception_error &)
9505 {
9506 num_failures++;
9507 /* If this is the last sal, error out. */
9508 if (num_failures == lsal.sals.size ())
9509 throw;
9510 }
9511 }
9512 }
9513
0878d0fa
YQ
9514 /* Create a private copy of condition string. */
9515 if (cond_string)
e1e01040 9516 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9517 /* Create a private copy of any extra string. */
9518 if (extra_string)
e1e01040 9519 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9520 }
0fb4aa4b 9521
52d361e1 9522 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9523 std::move (cond_string_copy),
9524 std::move (extra_string_copy),
9525 type_wanted,
d9b3f62e
PA
9526 tempflag ? disp_del : disp_donttouch,
9527 thread, task, ignore_count, ops,
44f238bb 9528 from_tty, enabled, internal, flags);
c906108c 9529 }
0101ce28
JJ
9530 else
9531 {
a5e364af 9532 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9533
a5e364af 9534 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9535 b->location = copy_event_location (location);
bfccc43c 9536
f00aae0f
KS
9537 if (parse_extra)
9538 b->cond_string = NULL;
e12c7713
MK
9539 else
9540 {
9541 /* Create a private copy of condition string. */
e1e01040 9542 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9543 b->thread = thread;
e12c7713 9544 }
f00aae0f
KS
9545
9546 /* Create a private copy of any extra string. */
e1e01040 9547 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9548 b->ignore_count = ignore_count;
0101ce28 9549 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9550 b->condition_not_parsed = 1;
41447f92 9551 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9552 if ((type_wanted != bp_breakpoint
dda83cd7 9553 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9554 b->pspace = current_program_space;
8bea4e01 9555
b270e6f9 9556 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9557 }
9558
6c5b2ebe 9559 if (canonical.lsals.size () > 1)
95a42b64 9560 {
3e43a32a
MS
9561 warning (_("Multiple breakpoints were set.\nUse the "
9562 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9563 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9564 }
9565
44702360 9566 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9567
9568 return 1;
c3f6f71d 9569}
c906108c 9570
348d480f 9571/* Set a breakpoint.
72b2ff0e
VP
9572 ARG is a string describing breakpoint address,
9573 condition, and thread.
9574 FLAG specifies if a breakpoint is hardware on,
9575 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9576 and BP_TEMPFLAG. */
348d480f 9577
98deb0da 9578static void
f2fc3015 9579break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9580{
72b2ff0e 9581 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9582 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9583 ? bp_hardware_breakpoint
9584 : bp_breakpoint);
f00aae0f 9585
ffc2605c 9586 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9587 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9588 (location.get (), false /* is_tracepoint */);
c3f6f71d 9589
8cdf0e15 9590 create_breakpoint (get_current_arch (),
ffc2605c 9591 location.get (),
10a636cc 9592 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9593 tempflag, type_wanted,
8cdf0e15
VP
9594 0 /* Ignore count */,
9595 pending_break_support,
55aa24fb 9596 ops,
8cdf0e15 9597 from_tty,
84f4c1fe 9598 1 /* enabled */,
44f238bb
PA
9599 0 /* internal */,
9600 0);
c906108c
SS
9601}
9602
c906108c
SS
9603/* Helper function for break_command_1 and disassemble_command. */
9604
9605void
fba45db2 9606resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9607{
9608 CORE_ADDR pc;
9609
9610 if (sal->pc == 0 && sal->symtab != NULL)
9611 {
9612 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9613 error (_("No line %d in file \"%s\"."),
05cba821 9614 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9615 sal->pc = pc;
6a048695 9616
4a64f543 9617 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9618 number, then skip the function prologue if necessary. */
6a048695 9619 if (sal->explicit_line)
059acae7 9620 skip_prologue_sal (sal);
c906108c
SS
9621 }
9622
9623 if (sal->section == 0 && sal->symtab != NULL)
9624 {
346d1dfe 9625 const struct blockvector *bv;
3977b71f 9626 const struct block *b;
c5aa993b 9627 struct symbol *sym;
c906108c 9628
43f3e411
DE
9629 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9630 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9631 if (bv != NULL)
9632 {
7f0df278 9633 sym = block_linkage_function (b);
c906108c
SS
9634 if (sym != NULL)
9635 {
eb822aa6 9636 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9637 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9638 }
9639 else
9640 {
4a64f543 9641 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9642 just have to look harder. This case can be executed
9643 if we have line numbers but no functions (as can
9644 happen in assembly source). */
c906108c 9645
5ed8105e 9646 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9647 switch_to_program_space_and_thread (sal->pspace);
c906108c 9648
5ed8105e 9649 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9650 if (msym.minsym)
1db66e34 9651 sal->section = msym.obj_section ();
c906108c
SS
9652 }
9653 }
9654 }
9655}
9656
9657void
0b39b52e 9658break_command (const char *arg, int from_tty)
c906108c 9659{
db107f19 9660 break_command_1 (arg, 0, from_tty);
c906108c
SS
9661}
9662
c906108c 9663void
0b39b52e 9664tbreak_command (const char *arg, int from_tty)
c906108c 9665{
db107f19 9666 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9667}
9668
c906108c 9669static void
0b39b52e 9670hbreak_command (const char *arg, int from_tty)
c906108c 9671{
db107f19 9672 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9673}
9674
9675static void
0b39b52e 9676thbreak_command (const char *arg, int from_tty)
c906108c 9677{
db107f19 9678 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9679}
9680
9681static void
ee7ddd71 9682stop_command (const char *arg, int from_tty)
c906108c 9683{
a3f17187 9684 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9685Usage: stop in <function | address>\n\
a3f17187 9686 stop at <line>\n"));
c906108c
SS
9687}
9688
9689static void
4495129a 9690stopin_command (const char *arg, int from_tty)
c906108c
SS
9691{
9692 int badInput = 0;
9693
cafb3438 9694 if (arg == NULL)
c906108c
SS
9695 badInput = 1;
9696 else if (*arg != '*')
9697 {
4495129a 9698 const char *argptr = arg;
c906108c
SS
9699 int hasColon = 0;
9700
4a64f543 9701 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9702 say it is bad, otherwise, it should be an address or
9703 function/method name. */
c906108c 9704 while (*argptr && !hasColon)
c5aa993b
JM
9705 {
9706 hasColon = (*argptr == ':');
9707 argptr++;
9708 }
c906108c
SS
9709
9710 if (hasColon)
c5aa993b 9711 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9712 else
c5aa993b 9713 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9714 }
9715
9716 if (badInput)
a3f17187 9717 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9718 else
db107f19 9719 break_command_1 (arg, 0, from_tty);
c906108c
SS
9720}
9721
9722static void
4495129a 9723stopat_command (const char *arg, int from_tty)
c906108c
SS
9724{
9725 int badInput = 0;
9726
cafb3438 9727 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9728 badInput = 1;
9729 else
9730 {
4495129a 9731 const char *argptr = arg;
c906108c
SS
9732 int hasColon = 0;
9733
4a64f543 9734 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9735 it is probably a line number. */
c906108c 9736 while (*argptr && !hasColon)
c5aa993b
JM
9737 {
9738 hasColon = (*argptr == ':');
9739 argptr++;
9740 }
c906108c
SS
9741
9742 if (hasColon)
c5aa993b 9743 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9744 else
c5aa993b 9745 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9746 }
9747
9748 if (badInput)
65e65158 9749 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9750 else
db107f19 9751 break_command_1 (arg, 0, from_tty);
c906108c
SS
9752}
9753
e7e0cddf
SS
9754/* The dynamic printf command is mostly like a regular breakpoint, but
9755 with a prewired command list consisting of a single output command,
9756 built from extra arguments supplied on the dprintf command
9757 line. */
9758
da821c7b 9759static void
0b39b52e 9760dprintf_command (const char *arg, int from_tty)
e7e0cddf 9761{
ffc2605c 9762 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9763
9764 /* If non-NULL, ARG should have been advanced past the location;
9765 the next character must be ','. */
9766 if (arg != NULL)
9767 {
9768 if (arg[0] != ',' || arg[1] == '\0')
9769 error (_("Format string required"));
9770 else
9771 {
9772 /* Skip the comma. */
9773 ++arg;
9774 }
9775 }
9776
e7e0cddf 9777 create_breakpoint (get_current_arch (),
ffc2605c 9778 location.get (),
10a636cc 9779 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9780 0, bp_dprintf,
9781 0 /* Ignore count */,
9782 pending_break_support,
9783 &dprintf_breakpoint_ops,
9784 from_tty,
9785 1 /* enabled */,
9786 0 /* internal */,
9787 0);
9788}
9789
d3ce09f5 9790static void
0b39b52e 9791agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9792{
9793 error (_("May only run agent-printf on the target"));
9794}
9795
f1310107
TJB
9796/* Implement the "breakpoint_hit" breakpoint_ops method for
9797 ranged breakpoints. */
9798
9799static int
9800breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9801 const address_space *aspace,
09ac7c10
TT
9802 CORE_ADDR bp_addr,
9803 const struct target_waitstatus *ws)
f1310107 9804{
09ac7c10 9805 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9806 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9807 return 0;
9808
f1310107
TJB
9809 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9810 bl->length, aspace, bp_addr);
9811}
9812
9813/* Implement the "resources_needed" breakpoint_ops method for
9814 ranged breakpoints. */
9815
9816static int
9817resources_needed_ranged_breakpoint (const struct bp_location *bl)
9818{
9819 return target_ranged_break_num_registers ();
9820}
9821
9822/* Implement the "print_it" breakpoint_ops method for
9823 ranged breakpoints. */
9824
9825static enum print_stop_action
348d480f 9826print_it_ranged_breakpoint (bpstat bs)
f1310107 9827{
348d480f 9828 struct breakpoint *b = bs->breakpoint_at;
f1310107 9829 struct bp_location *bl = b->loc;
79a45e25 9830 struct ui_out *uiout = current_uiout;
f1310107
TJB
9831
9832 gdb_assert (b->type == bp_hardware_breakpoint);
9833
9834 /* Ranged breakpoints have only one location. */
9835 gdb_assert (bl && bl->next == NULL);
9836
9837 annotate_breakpoint (b->number);
f303dbd6
PA
9838
9839 maybe_print_thread_hit_breakpoint (uiout);
9840
f1310107 9841 if (b->disposition == disp_del)
112e8700 9842 uiout->text ("Temporary ranged breakpoint ");
f1310107 9843 else
112e8700
SM
9844 uiout->text ("Ranged breakpoint ");
9845 if (uiout->is_mi_like_p ())
f1310107 9846 {
112e8700 9847 uiout->field_string ("reason",
f1310107 9848 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9849 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9850 }
381befee 9851 uiout->field_signed ("bkptno", b->number);
112e8700 9852 uiout->text (", ");
f1310107
TJB
9853
9854 return PRINT_SRC_AND_LOC;
9855}
9856
9857/* Implement the "print_one" breakpoint_ops method for
9858 ranged breakpoints. */
9859
9860static void
9861print_one_ranged_breakpoint (struct breakpoint *b,
9862 struct bp_location **last_loc)
9863{
9864 struct bp_location *bl = b->loc;
9865 struct value_print_options opts;
79a45e25 9866 struct ui_out *uiout = current_uiout;
f1310107
TJB
9867
9868 /* Ranged breakpoints have only one location. */
9869 gdb_assert (bl && bl->next == NULL);
9870
9871 get_user_print_options (&opts);
9872
9873 if (opts.addressprint)
9874 /* We don't print the address range here, it will be printed later
9875 by print_one_detail_ranged_breakpoint. */
112e8700 9876 uiout->field_skip ("addr");
f1310107
TJB
9877 annotate_field (5);
9878 print_breakpoint_location (b, bl);
9879 *last_loc = bl;
9880}
9881
9882/* Implement the "print_one_detail" breakpoint_ops method for
9883 ranged breakpoints. */
9884
9885static void
9886print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9887 struct ui_out *uiout)
9888{
9889 CORE_ADDR address_start, address_end;
9890 struct bp_location *bl = b->loc;
d7e74731 9891 string_file stb;
f1310107
TJB
9892
9893 gdb_assert (bl);
9894
9895 address_start = bl->address;
9896 address_end = address_start + bl->length - 1;
9897
112e8700 9898 uiout->text ("\taddress range: ");
d7e74731
PA
9899 stb.printf ("[%s, %s]",
9900 print_core_address (bl->gdbarch, address_start),
9901 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9902 uiout->field_stream ("addr", stb);
9903 uiout->text ("\n");
f1310107
TJB
9904}
9905
9906/* Implement the "print_mention" breakpoint_ops method for
9907 ranged breakpoints. */
9908
9909static void
9910print_mention_ranged_breakpoint (struct breakpoint *b)
9911{
9912 struct bp_location *bl = b->loc;
79a45e25 9913 struct ui_out *uiout = current_uiout;
f1310107
TJB
9914
9915 gdb_assert (bl);
9916 gdb_assert (b->type == bp_hardware_breakpoint);
9917
2d33446d
TT
9918 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9919 b->number, paddress (bl->gdbarch, bl->address),
9920 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9921}
9922
9923/* Implement the "print_recreate" breakpoint_ops method for
9924 ranged breakpoints. */
9925
9926static void
9927print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9928{
f00aae0f 9929 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9930 event_location_to_string (b->location.get ()),
9931 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9932 print_recreate_thread (b, fp);
f1310107
TJB
9933}
9934
9935/* The breakpoint_ops structure to be used in ranged breakpoints. */
9936
2060206e 9937static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9938
9939/* Find the address where the end of the breakpoint range should be
9940 placed, given the SAL of the end of the range. This is so that if
9941 the user provides a line number, the end of the range is set to the
9942 last instruction of the given line. */
9943
9944static CORE_ADDR
9945find_breakpoint_range_end (struct symtab_and_line sal)
9946{
9947 CORE_ADDR end;
9948
9949 /* If the user provided a PC value, use it. Otherwise,
9950 find the address of the end of the given location. */
9951 if (sal.explicit_pc)
9952 end = sal.pc;
9953 else
9954 {
9955 int ret;
9956 CORE_ADDR start;
9957
9958 ret = find_line_pc_range (sal, &start, &end);
9959 if (!ret)
9960 error (_("Could not find location of the end of the range."));
9961
9962 /* find_line_pc_range returns the start of the next line. */
9963 end--;
9964 }
9965
9966 return end;
9967}
9968
9969/* Implement the "break-range" CLI command. */
9970
9971static void
0b39b52e 9972break_range_command (const char *arg, int from_tty)
f1310107 9973{
f2fc3015 9974 const char *arg_start;
f1310107
TJB
9975 struct linespec_result canonical_start, canonical_end;
9976 int bp_count, can_use_bp, length;
9977 CORE_ADDR end;
9978 struct breakpoint *b;
f1310107
TJB
9979
9980 /* We don't support software ranged breakpoints. */
9981 if (target_ranged_break_num_registers () < 0)
9982 error (_("This target does not support hardware ranged breakpoints."));
9983
9984 bp_count = hw_breakpoint_used_count ();
9985 bp_count += target_ranged_break_num_registers ();
9986 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9987 bp_count, 0);
9988 if (can_use_bp < 0)
9989 error (_("Hardware breakpoints used exceeds limit."));
9990
f8eba3c6 9991 arg = skip_spaces (arg);
f1310107
TJB
9992 if (arg == NULL || arg[0] == '\0')
9993 error(_("No address range specified."));
9994
f8eba3c6 9995 arg_start = arg;
ffc2605c
TT
9996 event_location_up start_location = string_to_event_location (&arg,
9997 current_language);
9998 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9999
10000 if (arg[0] != ',')
10001 error (_("Too few arguments."));
6c5b2ebe 10002 else if (canonical_start.lsals.empty ())
f1310107 10003 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10004
6c5b2ebe 10005 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10006
6c5b2ebe
PA
10007 if (canonical_start.lsals.size () > 1
10008 || lsal_start.sals.size () != 1)
f1310107
TJB
10009 error (_("Cannot create a ranged breakpoint with multiple locations."));
10010
6c5b2ebe 10011 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10012 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10013
10014 arg++; /* Skip the comma. */
f8eba3c6 10015 arg = skip_spaces (arg);
f1310107
TJB
10016
10017 /* Parse the end location. */
10018
f1310107
TJB
10019 arg_start = arg;
10020
f8eba3c6 10021 /* We call decode_line_full directly here instead of using
f1310107
TJB
10022 parse_breakpoint_sals because we need to specify the start location's
10023 symtab and line as the default symtab and line for the end of the
10024 range. This makes it possible to have ranges like "foo.c:27, +14",
10025 where +14 means 14 lines from the start location. */
ffc2605c
TT
10026 event_location_up end_location = string_to_event_location (&arg,
10027 current_language);
10028 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10029 sal_start.symtab, sal_start.line,
10030 &canonical_end, NULL, NULL);
10031
6c5b2ebe 10032 if (canonical_end.lsals.empty ())
f1310107 10033 error (_("Could not find location of the end of the range."));
f8eba3c6 10034
6c5b2ebe
PA
10035 const linespec_sals &lsal_end = canonical_end.lsals[0];
10036 if (canonical_end.lsals.size () > 1
10037 || lsal_end.sals.size () != 1)
f1310107
TJB
10038 error (_("Cannot create a ranged breakpoint with multiple locations."));
10039
6c5b2ebe 10040 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10041
10042 end = find_breakpoint_range_end (sal_end);
10043 if (sal_start.pc > end)
177b42fe 10044 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10045
10046 length = end - sal_start.pc + 1;
10047 if (length < 0)
10048 /* Length overflowed. */
10049 error (_("Address range too large."));
10050 else if (length == 1)
10051 {
10052 /* This range is simple enough to be handled by
10053 the `hbreak' command. */
81b1e71c 10054 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10055
10056 return;
10057 }
10058
10059 /* Now set up the breakpoint. */
10060 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10061 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10062 set_breakpoint_count (breakpoint_count + 1);
10063 b->number = breakpoint_count;
10064 b->disposition = disp_donttouch;
d28cd78a
TT
10065 b->location = std::move (start_location);
10066 b->location_range_end = std::move (end_location);
f1310107
TJB
10067 b->loc->length = length;
10068
f1310107 10069 mention (b);
76727919 10070 gdb::observers::breakpoint_created.notify (b);
44702360 10071 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10072}
10073
4a64f543
MS
10074/* Return non-zero if EXP is verified as constant. Returned zero
10075 means EXP is variable. Also the constant detection may fail for
10076 some constant expressions and in such case still falsely return
10077 zero. */
2e6e3d9c 10078
dab72643 10079static bool
65d79d4b
SDJ
10080watchpoint_exp_is_const (const struct expression *exp)
10081{
1eaebe02 10082 return exp->op->constant_p ();
65d79d4b
SDJ
10083}
10084
c1fc2657 10085/* Watchpoint destructor. */
3a5c3e22 10086
c1fc2657 10087watchpoint::~watchpoint ()
3a5c3e22 10088{
c1fc2657
SM
10089 xfree (this->exp_string);
10090 xfree (this->exp_string_reparse);
3a5c3e22
PA
10091}
10092
348d480f
PA
10093/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10094
10095static void
10096re_set_watchpoint (struct breakpoint *b)
10097{
3a5c3e22
PA
10098 struct watchpoint *w = (struct watchpoint *) b;
10099
348d480f
PA
10100 /* Watchpoint can be either on expression using entirely global
10101 variables, or it can be on local variables.
10102
10103 Watchpoints of the first kind are never auto-deleted, and even
10104 persist across program restarts. Since they can use variables
10105 from shared libraries, we need to reparse expression as libraries
10106 are loaded and unloaded.
10107
10108 Watchpoints on local variables can also change meaning as result
10109 of solib event. For example, if a watchpoint uses both a local
10110 and a global variables in expression, it's a local watchpoint,
10111 but unloading of a shared library will make the expression
10112 invalid. This is not a very common use case, but we still
10113 re-evaluate expression, to avoid surprises to the user.
10114
10115 Note that for local watchpoints, we re-evaluate it only if
10116 watchpoints frame id is still valid. If it's not, it means the
10117 watchpoint is out of scope and will be deleted soon. In fact,
10118 I'm not sure we'll ever be called in this case.
10119
10120 If a local watchpoint's frame id is still valid, then
3a5c3e22 10121 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10122
3a5c3e22
PA
10123 Don't do anything about disabled watchpoints, since they will be
10124 reevaluated again when enabled. */
10125 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10126}
10127
77b06cd7
TJB
10128/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10129
10130static int
10131insert_watchpoint (struct bp_location *bl)
10132{
3a5c3e22
PA
10133 struct watchpoint *w = (struct watchpoint *) bl->owner;
10134 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10135
10136 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10137 w->cond_exp.get ());
77b06cd7
TJB
10138}
10139
10140/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10141
10142static int
73971819 10143remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10144{
3a5c3e22
PA
10145 struct watchpoint *w = (struct watchpoint *) bl->owner;
10146 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10147
10148 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10149 w->cond_exp.get ());
e09342b5
TJB
10150}
10151
e09342b5 10152static int
348d480f 10153breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10154 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10155 const struct target_waitstatus *ws)
e09342b5 10156{
348d480f 10157 struct breakpoint *b = bl->owner;
3a5c3e22 10158 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10159
348d480f
PA
10160 /* Continuable hardware watchpoints are treated as non-existent if the
10161 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10162 some data address). Otherwise gdb won't stop on a break instruction
10163 in the code (not from a breakpoint) when a hardware watchpoint has
10164 been defined. Also skip watchpoints which we know did not trigger
10165 (did not match the data address). */
10166 if (is_hardware_watchpoint (b)
3a5c3e22 10167 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10168 return 0;
9c06b0b4 10169
348d480f 10170 return 1;
9c06b0b4
TJB
10171}
10172
348d480f
PA
10173static void
10174check_status_watchpoint (bpstat bs)
9c06b0b4 10175{
348d480f 10176 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10177
348d480f 10178 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10179}
10180
10181/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10182 hardware watchpoints. */
9c06b0b4
TJB
10183
10184static int
348d480f 10185resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10186{
3a5c3e22
PA
10187 struct watchpoint *w = (struct watchpoint *) bl->owner;
10188 int length = w->exact? 1 : bl->length;
348d480f
PA
10189
10190 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10191}
10192
10193/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10194 hardware watchpoints. */
9c06b0b4
TJB
10195
10196static int
348d480f 10197works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10198{
efa80663
PA
10199 /* Read and access watchpoints only work with hardware support. */
10200 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10201}
10202
9c06b0b4 10203static enum print_stop_action
348d480f 10204print_it_watchpoint (bpstat bs)
9c06b0b4 10205{
348d480f 10206 struct breakpoint *b;
348d480f 10207 enum print_stop_action result;
3a5c3e22 10208 struct watchpoint *w;
79a45e25 10209 struct ui_out *uiout = current_uiout;
348d480f
PA
10210
10211 gdb_assert (bs->bp_location_at != NULL);
10212
348d480f 10213 b = bs->breakpoint_at;
3a5c3e22 10214 w = (struct watchpoint *) b;
348d480f 10215
f303dbd6
PA
10216 annotate_watchpoint (b->number);
10217 maybe_print_thread_hit_breakpoint (uiout);
10218
d7e74731
PA
10219 string_file stb;
10220
76f9c9cf 10221 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10222 switch (b->type)
10223 {
348d480f 10224 case bp_watchpoint:
9c06b0b4 10225 case bp_hardware_watchpoint:
112e8700
SM
10226 if (uiout->is_mi_like_p ())
10227 uiout->field_string
10228 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10229 mention (b);
76f9c9cf 10230 tuple_emitter.emplace (uiout, "value");
112e8700 10231 uiout->text ("\nOld value = ");
850645cf 10232 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10233 uiout->field_stream ("old", stb);
10234 uiout->text ("\nNew value = ");
850645cf 10235 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10236 uiout->field_stream ("new", stb);
10237 uiout->text ("\n");
348d480f
PA
10238 /* More than one watchpoint may have been triggered. */
10239 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10240 break;
10241
10242 case bp_read_watchpoint:
112e8700
SM
10243 if (uiout->is_mi_like_p ())
10244 uiout->field_string
10245 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10246 mention (b);
76f9c9cf 10247 tuple_emitter.emplace (uiout, "value");
112e8700 10248 uiout->text ("\nValue = ");
850645cf 10249 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10250 uiout->field_stream ("value", stb);
10251 uiout->text ("\n");
348d480f 10252 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10253 break;
10254
10255 case bp_access_watchpoint:
348d480f
PA
10256 if (bs->old_val != NULL)
10257 {
112e8700
SM
10258 if (uiout->is_mi_like_p ())
10259 uiout->field_string
10260 ("reason",
348d480f
PA
10261 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10262 mention (b);
76f9c9cf 10263 tuple_emitter.emplace (uiout, "value");
112e8700 10264 uiout->text ("\nOld value = ");
850645cf 10265 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10266 uiout->field_stream ("old", stb);
10267 uiout->text ("\nNew value = ");
348d480f
PA
10268 }
10269 else
10270 {
10271 mention (b);
112e8700
SM
10272 if (uiout->is_mi_like_p ())
10273 uiout->field_string
10274 ("reason",
348d480f 10275 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10276 tuple_emitter.emplace (uiout, "value");
112e8700 10277 uiout->text ("\nValue = ");
348d480f 10278 }
850645cf 10279 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10280 uiout->field_stream ("new", stb);
10281 uiout->text ("\n");
348d480f 10282 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10283 break;
10284 default:
348d480f 10285 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10286 }
10287
348d480f
PA
10288 return result;
10289}
10290
10291/* Implement the "print_mention" breakpoint_ops method for hardware
10292 watchpoints. */
10293
10294static void
10295print_mention_watchpoint (struct breakpoint *b)
10296{
3a5c3e22 10297 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10298 struct ui_out *uiout = current_uiout;
46b9c129 10299 const char *tuple_name;
348d480f
PA
10300
10301 switch (b->type)
10302 {
10303 case bp_watchpoint:
112e8700 10304 uiout->text ("Watchpoint ");
46b9c129 10305 tuple_name = "wpt";
348d480f
PA
10306 break;
10307 case bp_hardware_watchpoint:
112e8700 10308 uiout->text ("Hardware watchpoint ");
46b9c129 10309 tuple_name = "wpt";
348d480f
PA
10310 break;
10311 case bp_read_watchpoint:
112e8700 10312 uiout->text ("Hardware read watchpoint ");
46b9c129 10313 tuple_name = "hw-rwpt";
348d480f
PA
10314 break;
10315 case bp_access_watchpoint:
112e8700 10316 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10317 tuple_name = "hw-awpt";
348d480f
PA
10318 break;
10319 default:
10320 internal_error (__FILE__, __LINE__,
10321 _("Invalid hardware watchpoint type."));
10322 }
10323
46b9c129 10324 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10325 uiout->field_signed ("number", b->number);
112e8700
SM
10326 uiout->text (": ");
10327 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10328}
10329
10330/* Implement the "print_recreate" breakpoint_ops method for
10331 watchpoints. */
10332
10333static void
10334print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10335{
3a5c3e22
PA
10336 struct watchpoint *w = (struct watchpoint *) b;
10337
348d480f
PA
10338 switch (b->type)
10339 {
10340 case bp_watchpoint:
10341 case bp_hardware_watchpoint:
10342 fprintf_unfiltered (fp, "watch");
10343 break;
10344 case bp_read_watchpoint:
10345 fprintf_unfiltered (fp, "rwatch");
10346 break;
10347 case bp_access_watchpoint:
10348 fprintf_unfiltered (fp, "awatch");
10349 break;
10350 default:
10351 internal_error (__FILE__, __LINE__,
10352 _("Invalid watchpoint type."));
10353 }
10354
3a5c3e22 10355 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10356 print_recreate_thread (b, fp);
348d480f
PA
10357}
10358
427cd150
TT
10359/* Implement the "explains_signal" breakpoint_ops method for
10360 watchpoints. */
10361
47591c29 10362static int
427cd150
TT
10363explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10364{
10365 /* A software watchpoint cannot cause a signal other than
10366 GDB_SIGNAL_TRAP. */
10367 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10368 return 0;
427cd150 10369
47591c29 10370 return 1;
427cd150
TT
10371}
10372
348d480f
PA
10373/* The breakpoint_ops structure to be used in hardware watchpoints. */
10374
2060206e 10375static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10376
10377/* Implement the "insert" breakpoint_ops method for
10378 masked hardware watchpoints. */
10379
10380static int
10381insert_masked_watchpoint (struct bp_location *bl)
10382{
3a5c3e22
PA
10383 struct watchpoint *w = (struct watchpoint *) bl->owner;
10384
10385 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10386 bl->watchpoint_type);
10387}
10388
10389/* Implement the "remove" breakpoint_ops method for
10390 masked hardware watchpoints. */
10391
10392static int
73971819 10393remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10394{
3a5c3e22
PA
10395 struct watchpoint *w = (struct watchpoint *) bl->owner;
10396
10397 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10398 bl->watchpoint_type);
348d480f
PA
10399}
10400
10401/* Implement the "resources_needed" breakpoint_ops method for
10402 masked hardware watchpoints. */
10403
10404static int
10405resources_needed_masked_watchpoint (const struct bp_location *bl)
10406{
3a5c3e22
PA
10407 struct watchpoint *w = (struct watchpoint *) bl->owner;
10408
10409 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10410}
10411
10412/* Implement the "works_in_software_mode" breakpoint_ops method for
10413 masked hardware watchpoints. */
10414
10415static int
10416works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10417{
10418 return 0;
10419}
10420
10421/* Implement the "print_it" breakpoint_ops method for
10422 masked hardware watchpoints. */
10423
10424static enum print_stop_action
10425print_it_masked_watchpoint (bpstat bs)
10426{
10427 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10428 struct ui_out *uiout = current_uiout;
348d480f
PA
10429
10430 /* Masked watchpoints have only one location. */
10431 gdb_assert (b->loc && b->loc->next == NULL);
10432
f303dbd6
PA
10433 annotate_watchpoint (b->number);
10434 maybe_print_thread_hit_breakpoint (uiout);
10435
348d480f
PA
10436 switch (b->type)
10437 {
10438 case bp_hardware_watchpoint:
112e8700
SM
10439 if (uiout->is_mi_like_p ())
10440 uiout->field_string
10441 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10442 break;
10443
10444 case bp_read_watchpoint:
112e8700
SM
10445 if (uiout->is_mi_like_p ())
10446 uiout->field_string
10447 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10448 break;
10449
10450 case bp_access_watchpoint:
112e8700
SM
10451 if (uiout->is_mi_like_p ())
10452 uiout->field_string
10453 ("reason",
348d480f
PA
10454 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10455 break;
10456 default:
10457 internal_error (__FILE__, __LINE__,
10458 _("Invalid hardware watchpoint type."));
10459 }
10460
10461 mention (b);
112e8700 10462 uiout->text (_("\n\
9c06b0b4
TJB
10463Check the underlying instruction at PC for the memory\n\
10464address and value which triggered this watchpoint.\n"));
112e8700 10465 uiout->text ("\n");
9c06b0b4
TJB
10466
10467 /* More than one watchpoint may have been triggered. */
10468 return PRINT_UNKNOWN;
10469}
10470
10471/* Implement the "print_one_detail" breakpoint_ops method for
10472 masked hardware watchpoints. */
10473
10474static void
10475print_one_detail_masked_watchpoint (const struct breakpoint *b,
10476 struct ui_out *uiout)
10477{
3a5c3e22
PA
10478 struct watchpoint *w = (struct watchpoint *) b;
10479
9c06b0b4
TJB
10480 /* Masked watchpoints have only one location. */
10481 gdb_assert (b->loc && b->loc->next == NULL);
10482
112e8700
SM
10483 uiout->text ("\tmask ");
10484 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10485 uiout->text ("\n");
9c06b0b4
TJB
10486}
10487
10488/* Implement the "print_mention" breakpoint_ops method for
10489 masked hardware watchpoints. */
10490
10491static void
10492print_mention_masked_watchpoint (struct breakpoint *b)
10493{
3a5c3e22 10494 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10495 struct ui_out *uiout = current_uiout;
46b9c129 10496 const char *tuple_name;
9c06b0b4
TJB
10497
10498 switch (b->type)
10499 {
10500 case bp_hardware_watchpoint:
112e8700 10501 uiout->text ("Masked hardware watchpoint ");
46b9c129 10502 tuple_name = "wpt";
9c06b0b4
TJB
10503 break;
10504 case bp_read_watchpoint:
112e8700 10505 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10506 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10507 break;
10508 case bp_access_watchpoint:
112e8700 10509 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10510 tuple_name = "hw-awpt";
9c06b0b4
TJB
10511 break;
10512 default:
10513 internal_error (__FILE__, __LINE__,
10514 _("Invalid hardware watchpoint type."));
10515 }
10516
46b9c129 10517 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10518 uiout->field_signed ("number", b->number);
112e8700
SM
10519 uiout->text (": ");
10520 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10521}
10522
10523/* Implement the "print_recreate" breakpoint_ops method for
10524 masked hardware watchpoints. */
10525
10526static void
10527print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10528{
3a5c3e22 10529 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10530
10531 switch (b->type)
10532 {
10533 case bp_hardware_watchpoint:
10534 fprintf_unfiltered (fp, "watch");
10535 break;
10536 case bp_read_watchpoint:
10537 fprintf_unfiltered (fp, "rwatch");
10538 break;
10539 case bp_access_watchpoint:
10540 fprintf_unfiltered (fp, "awatch");
10541 break;
10542 default:
10543 internal_error (__FILE__, __LINE__,
10544 _("Invalid hardware watchpoint type."));
10545 }
10546
53807e9f
TT
10547 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10548 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10549 print_recreate_thread (b, fp);
9c06b0b4
TJB
10550}
10551
10552/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10553
2060206e 10554static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10555
10556/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10557
f2478a7e 10558static bool
9c06b0b4
TJB
10559is_masked_watchpoint (const struct breakpoint *b)
10560{
10561 return b->ops == &masked_watchpoint_breakpoint_ops;
10562}
10563
53a5351d 10564/* accessflag: hw_write: watch write,
dda83cd7 10565 hw_read: watch read,
53a5351d 10566 hw_access: watch access (read or write) */
c906108c 10567static void
bbc13ae3 10568watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10569 bool just_location, bool internal)
c906108c 10570{
c1fc2657 10571 struct breakpoint *scope_breakpoint = NULL;
270140bd 10572 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10573 struct value *result;
bb9d5f81 10574 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10575 const char *exp_start = NULL;
10576 const char *exp_end = NULL;
10577 const char *tok, *end_tok;
9c06b0b4 10578 int toklen = -1;
bbc13ae3
KS
10579 const char *cond_start = NULL;
10580 const char *cond_end = NULL;
c906108c 10581 enum bptype bp_type;
37e4754d 10582 int thread = -1;
9c06b0b4
TJB
10583 /* Flag to indicate whether we are going to use masks for
10584 the hardware watchpoint. */
2e362716 10585 bool use_mask = false;
9c06b0b4 10586 CORE_ADDR mask = 0;
c906108c 10587
37e4754d
LM
10588 /* Make sure that we actually have parameters to parse. */
10589 if (arg != NULL && arg[0] != '\0')
10590 {
bbc13ae3
KS
10591 const char *value_start;
10592
10593 exp_end = arg + strlen (arg);
37e4754d 10594
9c06b0b4
TJB
10595 /* Look for "parameter value" pairs at the end
10596 of the arguments string. */
bbc13ae3 10597 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10598 {
10599 /* Skip whitespace at the end of the argument list. */
10600 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10601 tok--;
10602
10603 /* Find the beginning of the last token.
10604 This is the value of the parameter. */
10605 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10606 tok--;
10607 value_start = tok + 1;
10608
10609 /* Skip whitespace. */
10610 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10611 tok--;
10612
10613 end_tok = tok;
10614
10615 /* Find the beginning of the second to last token.
10616 This is the parameter itself. */
10617 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10618 tok--;
10619 tok++;
10620 toklen = end_tok - tok + 1;
10621
61012eef 10622 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10623 {
5d5658a1 10624 struct thread_info *thr;
9c06b0b4
TJB
10625 /* At this point we've found a "thread" token, which means
10626 the user is trying to set a watchpoint that triggers
10627 only in a specific thread. */
5d5658a1 10628 const char *endp;
37e4754d 10629
9c06b0b4
TJB
10630 if (thread != -1)
10631 error(_("You can specify only one thread."));
37e4754d 10632
9c06b0b4 10633 /* Extract the thread ID from the next token. */
5d5658a1 10634 thr = parse_thread_id (value_start, &endp);
37e4754d 10635
5d5658a1 10636 /* Check if the user provided a valid thread ID. */
9c06b0b4 10637 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10638 invalid_thread_id_error (value_start);
9c06b0b4 10639
5d5658a1 10640 thread = thr->global_num;
9c06b0b4 10641 }
61012eef 10642 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10643 {
10644 /* We've found a "mask" token, which means the user wants to
10645 create a hardware watchpoint that is going to have the mask
10646 facility. */
10647 struct value *mask_value, *mark;
37e4754d 10648
9c06b0b4
TJB
10649 if (use_mask)
10650 error(_("You can specify only one mask."));
37e4754d 10651
2e362716 10652 use_mask = just_location = true;
37e4754d 10653
9c06b0b4
TJB
10654 mark = value_mark ();
10655 mask_value = parse_to_comma_and_eval (&value_start);
10656 mask = value_as_address (mask_value);
10657 value_free_to_mark (mark);
10658 }
10659 else
10660 /* We didn't recognize what we found. We should stop here. */
10661 break;
37e4754d 10662
9c06b0b4
TJB
10663 /* Truncate the string and get rid of the "parameter value" pair before
10664 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10665 exp_end = tok;
9c06b0b4 10666 }
37e4754d 10667 }
bbc13ae3
KS
10668 else
10669 exp_end = arg;
37e4754d 10670
bbc13ae3
KS
10671 /* Parse the rest of the arguments. From here on out, everything
10672 is in terms of a newly allocated string instead of the original
10673 ARG. */
81b1e71c
TT
10674 std::string expression (arg, exp_end - arg);
10675 exp_start = arg = expression.c_str ();
699bd4cf
TT
10676 innermost_block_tracker tracker;
10677 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10678 exp_end = arg;
fa8a61dc
TT
10679 /* Remove trailing whitespace from the expression before saving it.
10680 This makes the eventual display of the expression string a bit
10681 prettier. */
10682 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10683 --exp_end;
10684
65d79d4b 10685 /* Checking if the expression is not constant. */
4d01a485 10686 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10687 {
10688 int len;
10689
10690 len = exp_end - exp_start;
10691 while (len > 0 && isspace (exp_start[len - 1]))
10692 len--;
10693 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10694 }
10695
699bd4cf 10696 exp_valid_block = tracker.block ();
b926417a 10697 struct value *mark = value_mark ();
850645cf 10698 struct value *val_as_value = nullptr;
1eaebe02
TT
10699 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10700 just_location);
06a64a0b 10701
850645cf 10702 if (val_as_value != NULL && just_location)
bb9d5f81 10703 {
850645cf
TT
10704 saved_bitpos = value_bitpos (val_as_value);
10705 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10706 }
10707
850645cf 10708 value_ref_ptr val;
06a64a0b
TT
10709 if (just_location)
10710 {
9c06b0b4
TJB
10711 int ret;
10712
06a64a0b 10713 exp_valid_block = NULL;
850645cf 10714 val = release_value (value_addr (result));
06a64a0b 10715 value_free_to_mark (mark);
9c06b0b4
TJB
10716
10717 if (use_mask)
10718 {
850645cf 10719 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10720 mask);
10721 if (ret == -1)
10722 error (_("This target does not support masked watchpoints."));
10723 else if (ret == -2)
10724 error (_("Invalid mask or memory region."));
10725 }
06a64a0b 10726 }
850645cf
TT
10727 else if (val_as_value != NULL)
10728 val = release_value (val_as_value);
c906108c 10729
f1735a53
TT
10730 tok = skip_spaces (arg);
10731 end_tok = skip_to_space (tok);
c906108c
SS
10732
10733 toklen = end_tok - tok;
10734 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10735 {
10736 tok = cond_start = end_tok + 1;
699bd4cf
TT
10737 innermost_block_tracker if_tracker;
10738 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10739
10740 /* The watchpoint expression may not be local, but the condition
10741 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10742 cond_exp_valid_block = if_tracker.block ();
60e1c644 10743
c906108c
SS
10744 cond_end = tok;
10745 }
10746 if (*tok)
8a3fe4f8 10747 error (_("Junk at end of command."));
c906108c 10748
441d7c93
PA
10749 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10750
10751 /* Save this because create_internal_breakpoint below invalidates
10752 'wp_frame'. */
10753 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10754
10755 /* If the expression is "local", then set up a "watchpoint scope"
10756 breakpoint at the point where we've left the scope of the watchpoint
10757 expression. Create the scope breakpoint before the watchpoint, so
10758 that we will encounter it first in bpstat_stop_status. */
441d7c93 10759 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10760 {
441d7c93
PA
10761 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10762
10763 if (frame_id_p (caller_frame_id))
edb3359d 10764 {
441d7c93
PA
10765 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10766 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10767
24b21115 10768 scope_breakpoint
441d7c93 10769 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10770 bp_watchpoint_scope,
10771 &momentary_breakpoint_ops);
d983da9c 10772
441d7c93
PA
10773 /* create_internal_breakpoint could invalidate WP_FRAME. */
10774 wp_frame = NULL;
10775
edb3359d 10776 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10777
edb3359d
DJ
10778 /* Automatically delete the breakpoint when it hits. */
10779 scope_breakpoint->disposition = disp_del;
d983da9c 10780
edb3359d 10781 /* Only break in the proper frame (help with recursion). */
441d7c93 10782 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10783
edb3359d 10784 /* Set the address at which we will stop. */
441d7c93
PA
10785 scope_breakpoint->loc->gdbarch = caller_arch;
10786 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10787 scope_breakpoint->loc->address
a6d9a66e
UW
10788 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10789 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10790 scope_breakpoint->type);
10791 }
d983da9c
DJ
10792 }
10793
e8369a73
AB
10794 /* Now set up the breakpoint. We create all watchpoints as hardware
10795 watchpoints here even if hardware watchpoints are turned off, a call
10796 to update_watchpoint later in this function will cause the type to
10797 drop back to bp_watchpoint (software watchpoint) if required. */
10798
10799 if (accessflag == hw_read)
10800 bp_type = bp_read_watchpoint;
10801 else if (accessflag == hw_access)
10802 bp_type = bp_access_watchpoint;
10803 else
10804 bp_type = bp_hardware_watchpoint;
3a5c3e22 10805
b270e6f9 10806 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10807
348d480f 10808 if (use_mask)
b270e6f9 10809 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10810 &masked_watchpoint_breakpoint_ops);
348d480f 10811 else
b270e6f9 10812 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10813 &watchpoint_breakpoint_ops);
c1fc2657
SM
10814 w->thread = thread;
10815 w->disposition = disp_donttouch;
10816 w->pspace = current_program_space;
b22e99fd 10817 w->exp = std::move (exp);
3a5c3e22
PA
10818 w->exp_valid_block = exp_valid_block;
10819 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10820 if (just_location)
10821 {
850645cf
TT
10822 struct type *t = value_type (val.get ());
10823 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10824
43cc5389 10825 w->exp_string_reparse
f16a9f57 10826 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10827
3a5c3e22 10828 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10829 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10830 }
10831 else
3a5c3e22 10832 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10833
10834 if (use_mask)
10835 {
3a5c3e22 10836 w->hw_wp_mask = mask;
9c06b0b4
TJB
10837 }
10838 else
10839 {
3a5c3e22 10840 w->val = val;
bb9d5f81
PP
10841 w->val_bitpos = saved_bitpos;
10842 w->val_bitsize = saved_bitsize;
4c1d86d9 10843 w->val_valid = true;
9c06b0b4 10844 }
77b06cd7 10845
c906108c 10846 if (cond_start)
c1fc2657 10847 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10848 else
c1fc2657 10849 w->cond_string = 0;
c5aa993b 10850
441d7c93 10851 if (frame_id_p (watchpoint_frame))
f6bc2008 10852 {
441d7c93 10853 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10854 w->watchpoint_thread = inferior_ptid;
f6bc2008 10855 }
c906108c 10856 else
f6bc2008 10857 {
3a5c3e22
PA
10858 w->watchpoint_frame = null_frame_id;
10859 w->watchpoint_thread = null_ptid;
f6bc2008 10860 }
c906108c 10861
d983da9c 10862 if (scope_breakpoint != NULL)
c906108c 10863 {
d983da9c
DJ
10864 /* The scope breakpoint is related to the watchpoint. We will
10865 need to act on them together. */
c1fc2657 10866 w->related_breakpoint = scope_breakpoint;
b270e6f9 10867 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10868 }
d983da9c 10869
06a64a0b
TT
10870 if (!just_location)
10871 value_free_to_mark (mark);
2d134ed3 10872
b270e6f9
TT
10873 /* Finally update the new watchpoint. This creates the locations
10874 that should be inserted. */
10875 update_watchpoint (w.get (), 1);
a9634178 10876
b270e6f9 10877 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10878}
10879
e09342b5 10880/* Return count of debug registers needed to watch the given expression.
e09342b5 10881 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10882
c906108c 10883static int
a6535de1 10884can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10885{
10886 int found_memory_cnt = 0;
10887
10888 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10889 if (!can_use_hw_watchpoints)
c906108c 10890 return 0;
c5aa993b 10891
a6535de1
TT
10892 gdb_assert (!vals.empty ());
10893 struct value *head = vals[0].get ();
10894
5c44784c
JM
10895 /* Make sure that the value of the expression depends only upon
10896 memory contents, and values computed from them within GDB. If we
10897 find any register references or function calls, we can't use a
10898 hardware watchpoint.
10899
10900 The idea here is that evaluating an expression generates a series
10901 of values, one holding the value of every subexpression. (The
10902 expression a*b+c has five subexpressions: a, b, a*b, c, and
10903 a*b+c.) GDB's values hold almost enough information to establish
10904 the criteria given above --- they identify memory lvalues,
10905 register lvalues, computed values, etcetera. So we can evaluate
10906 the expression, and then scan the chain of values that leaves
10907 behind to decide whether we can detect any possible change to the
10908 expression's final value using only hardware watchpoints.
10909
10910 However, I don't think that the values returned by inferior
10911 function calls are special in any way. So this function may not
10912 notice that an expression involving an inferior function call
10913 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10914 for (const value_ref_ptr &iter : vals)
c906108c 10915 {
a6535de1
TT
10916 struct value *v = iter.get ();
10917
5c44784c 10918 if (VALUE_LVAL (v) == lval_memory)
c906108c 10919 {
8464be76
DJ
10920 if (v != head && value_lazy (v))
10921 /* A lazy memory lvalue in the chain is one that GDB never
10922 needed to fetch; we either just used its address (e.g.,
10923 `a' in `a.b') or we never needed it at all (e.g., `a'
10924 in `a,b'). This doesn't apply to HEAD; if that is
10925 lazy then it was not readable, but watch it anyway. */
5c44784c 10926 ;
53a5351d 10927 else
5c44784c
JM
10928 {
10929 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10930 it with hardware watchpoints. */
df407dfe 10931 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10932
10933 /* We only watch structs and arrays if user asked for it
10934 explicitly, never if they just happen to appear in a
10935 middle of some value chain. */
10936 if (v == head
78134374
SM
10937 || (vtype->code () != TYPE_CODE_STRUCT
10938 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10939 {
42ae5230 10940 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10941 int len;
10942 int num_regs;
10943
a9634178 10944 len = (target_exact_watchpoints
e09342b5
TJB
10945 && is_scalar_type_recursive (vtype))?
10946 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10947
e09342b5
TJB
10948 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10949 if (!num_regs)
2e70b7b9
MS
10950 return 0;
10951 else
e09342b5 10952 found_memory_cnt += num_regs;
2e70b7b9 10953 }
5c44784c 10954 }
c5aa993b 10955 }
5086187c
AC
10956 else if (VALUE_LVAL (v) != not_lval
10957 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10958 return 0; /* These are values from the history (e.g., $1). */
5086187c 10959 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10960 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10961 }
10962
10963 /* The expression itself looks suitable for using a hardware
10964 watchpoint, but give the target machine a chance to reject it. */
10965 return found_memory_cnt;
10966}
10967
8b93c638 10968void
2e362716 10969watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10970{
84f4c1fe 10971 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10972}
10973
a15a5258
AB
10974/* Options for the watch, awatch, and rwatch commands. */
10975
10976struct watch_options
10977{
10978 /* For -location. */
10979 bool location = false;
10980};
10981
10982/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10983
10984 Historically GDB always accepted both '-location' and '-l' flags for
10985 these commands (both flags being synonyms). When converting to the
10986 newer option scheme only '-location' is added here. That's fine (for
10987 backward compatibility) as any non-ambiguous prefix of a flag will be
10988 accepted, so '-l', '-loc', are now all accepted.
10989
10990 What this means is that, if in the future, we add any new flag here
10991 that starts with '-l' then this will break backward compatibility, so
10992 please, don't do that! */
10993
10994static const gdb::option::option_def watch_option_defs[] = {
10995 gdb::option::flag_option_def<watch_options> {
10996 "location",
10997 [] (watch_options *opt) { return &opt->location; },
10998 N_("\
10999This evaluates EXPRESSION and watches the memory to which is refers.\n\
11000-l can be used as a short form of -location."),
11001 },
11002};
11003
11004/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11005 commands. */
11006
11007static gdb::option::option_def_group
11008make_watch_options_def_group (watch_options *opts)
11009{
11010 return {{watch_option_defs}, opts};
11011}
11012
06a64a0b
TT
11013/* A helper function that looks for the "-location" argument and then
11014 calls watch_command_1. */
11015
11016static void
0b39b52e 11017watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 11018{
a15a5258
AB
11019 watch_options opts;
11020 auto grp = make_watch_options_def_group (&opts);
11021 gdb::option::process_options
11022 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11023 if (arg != nullptr && *arg == '\0')
11024 arg = nullptr;
11025
11026 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11027}
06a64a0b 11028
a15a5258
AB
11029/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11030static void
11031watch_command_completer (struct cmd_list_element *ignore,
11032 completion_tracker &tracker,
11033 const char *text, const char * /*word*/)
11034{
11035 const auto group = make_watch_options_def_group (nullptr);
11036 if (gdb::option::complete_options
11037 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11038 return;
06a64a0b 11039
a15a5258
AB
11040 const char *word = advance_to_expression_complete_word_point (tracker, text);
11041 expression_completer (ignore, tracker, text, word);
8b93c638 11042}
8926118c 11043
c5aa993b 11044static void
0b39b52e 11045watch_command (const char *arg, int from_tty)
c906108c 11046{
06a64a0b 11047 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11048}
11049
8b93c638 11050void
2e362716 11051rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11052{
84f4c1fe 11053 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11054}
8926118c 11055
c5aa993b 11056static void
0b39b52e 11057rwatch_command (const char *arg, int from_tty)
c906108c 11058{
06a64a0b 11059 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11060}
11061
8b93c638 11062void
2e362716 11063awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11064{
84f4c1fe 11065 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11066}
8926118c 11067
c5aa993b 11068static void
0b39b52e 11069awatch_command (const char *arg, int from_tty)
c906108c 11070{
06a64a0b 11071 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11072}
c906108c 11073\f
c5aa993b 11074
cfc31633
PA
11075/* Data for the FSM that manages the until(location)/advance commands
11076 in infcmd.c. Here because it uses the mechanisms of
11077 breakpoints. */
c906108c 11078
46e3ed7f 11079struct until_break_fsm : public thread_fsm
bfec99b2 11080{
46e3ed7f 11081 /* The thread that was current when the command was executed. */
cfc31633
PA
11082 int thread;
11083
df631783
PA
11084 /* The breakpoint set at the return address in the caller frame,
11085 plus breakpoints at all the destination locations. */
11086 std::vector<breakpoint_up> breakpoints;
cfc31633 11087
46e3ed7f 11088 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11089 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11090 : thread_fsm (cmd_interp),
11091 thread (thread),
df631783 11092 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11093 {
11094 }
cfc31633 11095
46e3ed7f
TT
11096 void clean_up (struct thread_info *thread) override;
11097 bool should_stop (struct thread_info *thread) override;
11098 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11099};
11100
cfc31633
PA
11101/* Implementation of the 'should_stop' FSM method for the
11102 until(location)/advance commands. */
11103
46e3ed7f
TT
11104bool
11105until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11106{
df631783
PA
11107 for (const breakpoint_up &bp : breakpoints)
11108 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11109 bp.get ()) != NULL)
11110 {
11111 set_finished ();
11112 break;
11113 }
cfc31633 11114
46e3ed7f 11115 return true;
cfc31633
PA
11116}
11117
11118/* Implementation of the 'clean_up' FSM method for the
11119 until(location)/advance commands. */
11120
46e3ed7f
TT
11121void
11122until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11123{
cfc31633 11124 /* Clean up our temporary breakpoints. */
df631783 11125 breakpoints.clear ();
46e3ed7f 11126 delete_longjmp_breakpoint (thread);
cfc31633
PA
11127}
11128
11129/* Implementation of the 'async_reply_reason' FSM method for the
11130 until(location)/advance commands. */
11131
46e3ed7f
TT
11132enum async_reply_reason
11133until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11134{
11135 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11136}
11137
c906108c 11138void
f2fc3015 11139until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11140{
8556afb4
PA
11141 struct frame_info *frame;
11142 struct gdbarch *frame_gdbarch;
11143 struct frame_id stack_frame_id;
11144 struct frame_id caller_frame_id;
186c406b
TT
11145 int thread;
11146 struct thread_info *tp;
c906108c 11147
70509625 11148 clear_proceed_status (0);
c906108c
SS
11149
11150 /* Set a breakpoint where the user wants it and at return from
4a64f543 11151 this function. */
c5aa993b 11152
ffc2605c 11153 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11154
6c5b2ebe
PA
11155 std::vector<symtab_and_line> sals
11156 = (last_displayed_sal_is_valid ()
11157 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11158 get_last_displayed_symtab (),
11159 get_last_displayed_line ())
11160 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11161 NULL, NULL, 0));
c5aa993b 11162
df631783 11163 if (sals.empty ())
8a3fe4f8 11164 error (_("Couldn't get information on specified line."));
c5aa993b 11165
c906108c 11166 if (*arg)
8a3fe4f8 11167 error (_("Junk at end of arguments."));
c5aa993b 11168
186c406b 11169 tp = inferior_thread ();
5d5658a1 11170 thread = tp->global_num;
186c406b 11171
8556afb4
PA
11172 /* Note linespec handling above invalidates the frame chain.
11173 Installing a breakpoint also invalidates the frame chain (as it
11174 may need to switch threads), so do any frame handling before
11175 that. */
11176
11177 frame = get_selected_frame (NULL);
11178 frame_gdbarch = get_frame_arch (frame);
11179 stack_frame_id = get_stack_frame_id (frame);
11180 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11181
ae66c1fc
EZ
11182 /* Keep within the current frame, or in frames called by the current
11183 one. */
edb3359d 11184
df631783 11185 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11186
11187 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11188
883bc8d1 11189 if (frame_id_p (caller_frame_id))
c906108c 11190 {
883bc8d1 11191 struct symtab_and_line sal2;
cfc31633 11192 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11193
11194 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11195 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11196 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11197
11198 breakpoint_up caller_breakpoint
11199 = set_momentary_breakpoint (caller_gdbarch, sal2,
11200 caller_frame_id, bp_until);
11201 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11202
883bc8d1 11203 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11204 lj_deleter.emplace (thread);
c906108c 11205 }
c5aa993b 11206
c70a6932
JK
11207 /* set_momentary_breakpoint could invalidate FRAME. */
11208 frame = NULL;
11209
df631783
PA
11210 /* If the user told us to continue until a specified location, we
11211 don't specify a frame at which we need to stop. Otherwise,
11212 specify the selected frame, because we want to stop only at the
11213 very same frame. */
11214 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11215
11216 for (symtab_and_line &sal : sals)
11217 {
11218 resolve_sal_pc (&sal);
11219
11220 breakpoint_up location_breakpoint
11221 = set_momentary_breakpoint (frame_gdbarch, sal,
11222 stop_frame_id, bp_until);
11223 breakpoints.emplace_back (std::move (location_breakpoint));
11224 }
883bc8d1 11225
46e3ed7f 11226 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11227 std::move (breakpoints));
f107f563 11228
5419bdae
TT
11229 if (lj_deleter)
11230 lj_deleter->release ();
f107f563 11231
cfc31633 11232 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11233}
ae66c1fc 11234
c906108c
SS
11235/* This function attempts to parse an optional "if <cond>" clause
11236 from the arg string. If one is not found, it returns NULL.
c5aa993b 11237
c906108c
SS
11238 Else, it returns a pointer to the condition string. (It does not
11239 attempt to evaluate the string against a particular block.) And,
11240 it updates arg to point to the first character following the parsed
4a64f543 11241 if clause in the arg string. */
53a5351d 11242
63160a43
PA
11243const char *
11244ep_parse_optional_if_clause (const char **arg)
c906108c 11245{
63160a43 11246 const char *cond_string;
c5aa993b
JM
11247
11248 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11249 return NULL;
c5aa993b 11250
4a64f543 11251 /* Skip the "if" keyword. */
c906108c 11252 (*arg) += 2;
c5aa993b 11253
c906108c 11254 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11255 condition string. */
f1735a53 11256 *arg = skip_spaces (*arg);
c906108c 11257 cond_string = *arg;
c5aa993b 11258
4a64f543
MS
11259 /* Assume that the condition occupies the remainder of the arg
11260 string. */
c906108c 11261 (*arg) += strlen (cond_string);
c5aa993b 11262
c906108c
SS
11263 return cond_string;
11264}
c5aa993b 11265
c906108c
SS
11266/* Commands to deal with catching events, such as signals, exceptions,
11267 process start/exit, etc. */
c5aa993b
JM
11268
11269typedef enum
11270{
44feb3ce
TT
11271 catch_fork_temporary, catch_vfork_temporary,
11272 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11273}
11274catch_fork_kind;
11275
c906108c 11276static void
eb4c3f4a 11277catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11278 struct cmd_list_element *command)
c906108c 11279{
a6d9a66e 11280 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11281 const char *cond_string = NULL;
44feb3ce 11282 catch_fork_kind fork_kind;
44feb3ce 11283
0f8e2034 11284 fork_kind = (catch_fork_kind) (uintptr_t) command->context ();
b650a282
SM
11285 bool temp = (fork_kind == catch_fork_temporary
11286 || fork_kind == catch_vfork_temporary);
c5aa993b 11287
44feb3ce
TT
11288 if (!arg)
11289 arg = "";
f1735a53 11290 arg = skip_spaces (arg);
c5aa993b 11291
c906108c 11292 /* The allowed syntax is:
c5aa993b
JM
11293 catch [v]fork
11294 catch [v]fork if <cond>
11295
4a64f543 11296 First, check if there's an if clause. */
c906108c 11297 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11298
c906108c 11299 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11300 error (_("Junk at end of arguments."));
c5aa993b 11301
c906108c 11302 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11303 and enable reporting of such events. */
c5aa993b
JM
11304 switch (fork_kind)
11305 {
44feb3ce
TT
11306 case catch_fork_temporary:
11307 case catch_fork_permanent:
b650a282 11308 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11309 &catch_fork_breakpoint_ops);
c906108c 11310 break;
44feb3ce
TT
11311 case catch_vfork_temporary:
11312 case catch_vfork_permanent:
b650a282 11313 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11314 &catch_vfork_breakpoint_ops);
c906108c 11315 break;
c5aa993b 11316 default:
8a3fe4f8 11317 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11318 break;
c5aa993b 11319 }
c906108c
SS
11320}
11321
11322static void
eb4c3f4a 11323catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11324 struct cmd_list_element *command)
c906108c 11325{
a6d9a66e 11326 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11327 const char *cond_string = NULL;
0f8e2034 11328 bool temp = command->context () == CATCH_TEMPORARY;
44feb3ce
TT
11329
11330 if (!arg)
11331 arg = "";
f1735a53 11332 arg = skip_spaces (arg);
c906108c
SS
11333
11334 /* The allowed syntax is:
c5aa993b
JM
11335 catch exec
11336 catch exec if <cond>
c906108c 11337
4a64f543 11338 First, check if there's an if clause. */
c906108c
SS
11339 cond_string = ep_parse_optional_if_clause (&arg);
11340
11341 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11342 error (_("Junk at end of arguments."));
c906108c 11343
b270e6f9 11344 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11345 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11346 &catch_exec_breakpoint_ops);
11347 c->exec_pathname = NULL;
11348
b270e6f9 11349 install_breakpoint (0, std::move (c), 1);
c906108c 11350}
c5aa993b 11351
9ac4176b 11352void
28010a5d
PA
11353init_ada_exception_breakpoint (struct breakpoint *b,
11354 struct gdbarch *gdbarch,
11355 struct symtab_and_line sal,
f2fc3015 11356 const char *addr_string,
c0a91b2b 11357 const struct breakpoint_ops *ops,
28010a5d 11358 int tempflag,
349774ef 11359 int enabled,
28010a5d 11360 int from_tty)
f7f9143b 11361{
f7f9143b
JB
11362 if (from_tty)
11363 {
5af949e3
UW
11364 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11365 if (!loc_gdbarch)
11366 loc_gdbarch = gdbarch;
11367
6c95b8df
PA
11368 describe_other_breakpoints (loc_gdbarch,
11369 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11370 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11371 version for exception catchpoints, because two catchpoints
11372 used for different exception names will use the same address.
11373 In this case, a "breakpoint ... also set at..." warning is
11374 unproductive. Besides, the warning phrasing is also a bit
11375 inappropriate, we should use the word catchpoint, and tell
11376 the user what type of catchpoint it is. The above is good
11377 enough for now, though. */
f7f9143b
JB
11378 }
11379
f06f1252 11380 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11381
349774ef 11382 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11383 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11384 b->location = string_to_event_location (&addr_string,
11385 language_def (language_ada));
f7f9143b 11386 b->language = language_ada;
f7f9143b
JB
11387}
11388
c906108c
SS
11389\f
11390
81b1e71c 11391/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11392
11393static int
81b1e71c 11394compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11395{
81b1e71c
TT
11396 uintptr_t ua = (uintptr_t) a;
11397 uintptr_t ub = (uintptr_t) b;
8a2c437b 11398
81b1e71c 11399 if (a->number < b->number)
8a2c437b 11400 return -1;
81b1e71c 11401 else if (a->number > b->number)
8a2c437b
TT
11402 return 1;
11403
11404 /* Now sort by address, in case we see, e..g, two breakpoints with
11405 the number 0. */
11406 if (ua < ub)
11407 return -1;
94b0e70d 11408 return ua > ub ? 1 : 0;
8a2c437b
TT
11409}
11410
80f8a6eb 11411/* Delete breakpoints by address or line. */
c906108c
SS
11412
11413static void
0b39b52e 11414clear_command (const char *arg, int from_tty)
c906108c 11415{
c906108c 11416 int default_match;
c906108c 11417
6c5b2ebe
PA
11418 std::vector<symtab_and_line> decoded_sals;
11419 symtab_and_line last_sal;
11420 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11421 if (arg)
11422 {
6c5b2ebe
PA
11423 decoded_sals
11424 = decode_line_with_current_source (arg,
11425 (DECODE_LINE_FUNFIRSTLINE
11426 | DECODE_LINE_LIST_MODE));
c906108c 11427 default_match = 0;
6c5b2ebe 11428 sals = decoded_sals;
c906108c
SS
11429 }
11430 else
11431 {
1bfeeb0f
JL
11432 /* Set sal's line, symtab, pc, and pspace to the values
11433 corresponding to the last call to print_frame_info. If the
11434 codepoint is not valid, this will set all the fields to 0. */
51abb421 11435 last_sal = get_last_displayed_sal ();
6c5b2ebe 11436 if (last_sal.symtab == 0)
8a3fe4f8 11437 error (_("No source file specified."));
c906108c 11438
c906108c 11439 default_match = 1;
6c5b2ebe 11440 sals = last_sal;
c906108c
SS
11441 }
11442
4a64f543
MS
11443 /* We don't call resolve_sal_pc here. That's not as bad as it
11444 seems, because all existing breakpoints typically have both
11445 file/line and pc set. So, if clear is given file/line, we can
11446 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11447
11448 We only support clearing given the address explicitly
11449 present in breakpoint table. Say, we've set breakpoint
4a64f543 11450 at file:line. There were several PC values for that file:line,
ed0616c6 11451 due to optimization, all in one block.
4a64f543
MS
11452
11453 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11454 PC corresponding to the same file:line, the breakpoint won't
11455 be cleared. We probably can still clear the breakpoint, but
11456 since the other PC value is never presented to user, user
11457 can only find it by guessing, and it does not seem important
11458 to support that. */
11459
4a64f543
MS
11460 /* For each line spec given, delete bps which correspond to it. Do
11461 it in two passes, solely to preserve the current behavior that
11462 from_tty is forced true if we delete more than one
11463 breakpoint. */
c906108c 11464
81b1e71c 11465 std::vector<struct breakpoint *> found;
6c5b2ebe 11466 for (const auto &sal : sals)
c906108c 11467 {
05cba821
JK
11468 const char *sal_fullname;
11469
c906108c 11470 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11471 If line given (pc == 0), clear all bpts on specified line.
11472 If defaulting, clear all bpts on default line
11473 or at default pc.
c5aa993b 11474
dda83cd7 11475 defaulting sal.pc != 0 tests to do
c5aa993b 11476
dda83cd7
SM
11477 0 1 pc
11478 1 1 pc _and_ line
11479 0 0 line
11480 1 0 <can't happen> */
c906108c 11481
05cba821
JK
11482 sal_fullname = (sal.symtab == NULL
11483 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11484
4a64f543 11485 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 11486 for (breakpoint *b : all_breakpoints ())
c5aa993b 11487 {
0d381245 11488 int match = 0;
4a64f543 11489 /* Are we going to delete b? */
cc60f2e3 11490 if (b->type != bp_none && !is_watchpoint (b))
0d381245 11491 {
40cb8ca5 11492 for (bp_location *loc : b->locations ())
0d381245 11493 {
f8eba3c6
TT
11494 /* If the user specified file:line, don't allow a PC
11495 match. This matches historical gdb behavior. */
11496 int pc_match = (!sal.explicit_line
11497 && sal.pc
11498 && (loc->pspace == sal.pspace)
11499 && (loc->address == sal.pc)
11500 && (!section_is_overlay (loc->section)
11501 || loc->section == sal.section));
4aac40c8
TT
11502 int line_match = 0;
11503
11504 if ((default_match || sal.explicit_line)
2f202fde 11505 && loc->symtab != NULL
05cba821 11506 && sal_fullname != NULL
4aac40c8 11507 && sal.pspace == loc->pspace
05cba821
JK
11508 && loc->line_number == sal.line
11509 && filename_cmp (symtab_to_fullname (loc->symtab),
11510 sal_fullname) == 0)
11511 line_match = 1;
4aac40c8 11512
0d381245
VP
11513 if (pc_match || line_match)
11514 {
11515 match = 1;
11516 break;
11517 }
11518 }
11519 }
11520
11521 if (match)
81b1e71c 11522 found.push_back (b);
c906108c 11523 }
80f8a6eb 11524 }
8a2c437b 11525
80f8a6eb 11526 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11527 if (found.empty ())
80f8a6eb
MS
11528 {
11529 if (arg)
8a3fe4f8 11530 error (_("No breakpoint at %s."), arg);
80f8a6eb 11531 else
8a3fe4f8 11532 error (_("No breakpoint at this line."));
80f8a6eb 11533 }
c906108c 11534
8a2c437b 11535 /* Remove duplicates from the vec. */
81b1e71c 11536 std::sort (found.begin (), found.end (),
b926417a 11537 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11538 {
b926417a 11539 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11540 });
11541 found.erase (std::unique (found.begin (), found.end (),
b926417a 11542 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11543 {
b926417a 11544 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11545 }),
11546 found.end ());
8a2c437b 11547
81b1e71c 11548 if (found.size () > 1)
4a64f543 11549 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11550 if (from_tty)
a3f17187 11551 {
81b1e71c 11552 if (found.size () == 1)
a3f17187
AC
11553 printf_unfiltered (_("Deleted breakpoint "));
11554 else
11555 printf_unfiltered (_("Deleted breakpoints "));
11556 }
d6e956e5 11557
81b1e71c 11558 for (breakpoint *iter : found)
80f8a6eb 11559 {
c5aa993b 11560 if (from_tty)
81b1e71c
TT
11561 printf_unfiltered ("%d ", iter->number);
11562 delete_breakpoint (iter);
c906108c 11563 }
80f8a6eb
MS
11564 if (from_tty)
11565 putchar_unfiltered ('\n');
c906108c
SS
11566}
11567\f
11568/* Delete breakpoint in BS if they are `delete' breakpoints and
11569 all breakpoints that are marked for deletion, whether hit or not.
11570 This is called after any breakpoint is hit, or after errors. */
11571
11572void
fba45db2 11573breakpoint_auto_delete (bpstat bs)
c906108c 11574{
c906108c 11575 for (; bs; bs = bs->next)
f431efe5
PA
11576 if (bs->breakpoint_at
11577 && bs->breakpoint_at->disposition == disp_del
c906108c 11578 && bs->stop)
f431efe5 11579 delete_breakpoint (bs->breakpoint_at);
c906108c 11580
1428b37a 11581 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 11582 if (b->disposition == disp_del_at_next_stop)
c5aa993b 11583 delete_breakpoint (b);
c906108c
SS
11584}
11585
4a64f543 11586/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11587 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11588 bl_address_is_meaningful says), secondarily by ordering first
11589 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11590 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11591
11592static int
39ef2f62 11593bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11594{
876fa593 11595 if (a->address != b->address)
39ef2f62 11596 return a->address < b->address;
876fa593 11597
dea2aa5f
LM
11598 /* Sort locations at the same address by their pspace number, keeping
11599 locations of the same inferior (in a multi-inferior environment)
11600 grouped. */
11601
11602 if (a->pspace->num != b->pspace->num)
39ef2f62 11603 return a->pspace->num < b->pspace->num;
dea2aa5f 11604
876fa593 11605 /* Sort permanent breakpoints first. */
1a853c52 11606 if (a->permanent != b->permanent)
39ef2f62 11607 return a->permanent > b->permanent;
876fa593 11608
7f32a4d5
PA
11609 /* Sort by type in order to make duplicate determination easier.
11610 See update_global_location_list. This is kept in sync with
11611 breakpoint_locations_match. */
11612 if (a->loc_type < b->loc_type)
11613 return true;
11614
11615 /* Likewise, for range-breakpoints, sort by length. */
11616 if (a->loc_type == bp_loc_hardware_breakpoint
11617 && b->loc_type == bp_loc_hardware_breakpoint
11618 && a->length < b->length)
11619 return true;
11620
c56a97f9
JK
11621 /* Make the internal GDB representation stable across GDB runs
11622 where A and B memory inside GDB can differ. Breakpoint locations of
11623 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11624
11625 if (a->owner->number != b->owner->number)
39ef2f62 11626 return a->owner->number < b->owner->number;
876fa593 11627
39ef2f62 11628 return a < b;
876fa593
JK
11629}
11630
f5336ca5
PA
11631/* Set bp_locations_placed_address_before_address_max and
11632 bp_locations_shadow_len_after_address_max according to the current
11633 content of the bp_locations array. */
f7545552
TT
11634
11635static void
f5336ca5 11636bp_locations_target_extensions_update (void)
f7545552 11637{
f5336ca5
PA
11638 bp_locations_placed_address_before_address_max = 0;
11639 bp_locations_shadow_len_after_address_max = 0;
876fa593 11640
48d7020b 11641 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11642 {
11643 CORE_ADDR start, end, addr;
11644
11645 if (!bp_location_has_shadow (bl))
11646 continue;
11647
11648 start = bl->target_info.placed_address;
11649 end = start + bl->target_info.shadow_len;
11650
11651 gdb_assert (bl->address >= start);
11652 addr = bl->address - start;
f5336ca5
PA
11653 if (addr > bp_locations_placed_address_before_address_max)
11654 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11655
11656 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11657
11658 gdb_assert (bl->address < end);
11659 addr = end - bl->address;
f5336ca5
PA
11660 if (addr > bp_locations_shadow_len_after_address_max)
11661 bp_locations_shadow_len_after_address_max = addr;
876fa593 11662 }
f7545552
TT
11663}
11664
1e4d1764
YQ
11665/* Download tracepoint locations if they haven't been. */
11666
11667static void
11668download_tracepoint_locations (void)
11669{
dd2e65cc 11670 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11671
5ed8105e 11672 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11673
f6d17b2b 11674 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11675 {
11676 struct tracepoint *t;
f2a8bc8a 11677 int bp_location_downloaded = 0;
1e4d1764 11678
7ed2c994 11679 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11680 ? !may_insert_fast_tracepoints
11681 : !may_insert_tracepoints))
11682 continue;
11683
dd2e65cc
YQ
11684 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11685 {
11686 if (target_can_download_tracepoint ())
11687 can_download_tracepoint = TRIBOOL_TRUE;
11688 else
11689 can_download_tracepoint = TRIBOOL_FALSE;
11690 }
11691
11692 if (can_download_tracepoint == TRIBOOL_FALSE)
11693 break;
11694
40cb8ca5 11695 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11696 {
11697 /* In tracepoint, locations are _never_ duplicated, so
11698 should_be_inserted is equivalent to
11699 unduplicated_should_be_inserted. */
11700 if (!should_be_inserted (bl) || bl->inserted)
11701 continue;
1e4d1764 11702
7ed2c994 11703 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11704
7ed2c994 11705 target_download_tracepoint (bl);
1e4d1764 11706
7ed2c994 11707 bl->inserted = 1;
f2a8bc8a 11708 bp_location_downloaded = 1;
7ed2c994
YQ
11709 }
11710 t = (struct tracepoint *) b;
11711 t->number_on_target = b->number;
f2a8bc8a 11712 if (bp_location_downloaded)
76727919 11713 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11714 }
1e4d1764
YQ
11715}
11716
934709f0
PW
11717/* Swap the insertion/duplication state between two locations. */
11718
11719static void
11720swap_insertion (struct bp_location *left, struct bp_location *right)
11721{
11722 const int left_inserted = left->inserted;
11723 const int left_duplicate = left->duplicate;
b775012e 11724 const int left_needs_update = left->needs_update;
934709f0
PW
11725 const struct bp_target_info left_target_info = left->target_info;
11726
1e4d1764
YQ
11727 /* Locations of tracepoints can never be duplicated. */
11728 if (is_tracepoint (left->owner))
11729 gdb_assert (!left->duplicate);
11730 if (is_tracepoint (right->owner))
11731 gdb_assert (!right->duplicate);
11732
934709f0
PW
11733 left->inserted = right->inserted;
11734 left->duplicate = right->duplicate;
b775012e 11735 left->needs_update = right->needs_update;
934709f0
PW
11736 left->target_info = right->target_info;
11737 right->inserted = left_inserted;
11738 right->duplicate = left_duplicate;
b775012e 11739 right->needs_update = left_needs_update;
934709f0
PW
11740 right->target_info = left_target_info;
11741}
11742
b775012e
LM
11743/* Force the re-insertion of the locations at ADDRESS. This is called
11744 once a new/deleted/modified duplicate location is found and we are evaluating
11745 conditions on the target's side. Such conditions need to be updated on
11746 the target. */
11747
11748static void
11749force_breakpoint_reinsertion (struct bp_location *bl)
11750{
b775012e
LM
11751 CORE_ADDR address = 0;
11752 int pspace_num;
11753
11754 address = bl->address;
11755 pspace_num = bl->pspace->num;
11756
11757 /* This is only meaningful if the target is
11758 evaluating conditions and if the user has
11759 opted for condition evaluation on the target's
11760 side. */
11761 if (gdb_evaluates_breakpoint_condition_p ()
11762 || !target_supports_evaluation_of_breakpoint_conditions ())
11763 return;
11764
11765 /* Flag all breakpoint locations with this address and
11766 the same program space as the location
11767 as "its condition has changed". We need to
11768 update the conditions on the target's side. */
e0d9a270 11769 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11770 {
b775012e
LM
11771 if (!is_breakpoint (loc->owner)
11772 || pspace_num != loc->pspace->num)
11773 continue;
11774
11775 /* Flag the location appropriately. We use a different state to
11776 let everyone know that we already updated the set of locations
11777 with addr bl->address and program space bl->pspace. This is so
11778 we don't have to keep calling these functions just to mark locations
11779 that have already been marked. */
11780 loc->condition_changed = condition_updated;
11781
11782 /* Free the agent expression bytecode as well. We will compute
11783 it later on. */
833177a4 11784 loc->cond_bytecode.reset ();
b775012e
LM
11785 }
11786}
7f32a4d5 11787
44702360
PA
11788/* Called whether new breakpoints are created, or existing breakpoints
11789 deleted, to update the global location list and recompute which
11790 locations are duplicate of which.
b775012e 11791
04086b45
PA
11792 The INSERT_MODE flag determines whether locations may not, may, or
11793 shall be inserted now. See 'enum ugll_insert_mode' for more
11794 info. */
b60e7edf 11795
0d381245 11796static void
44702360 11797update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11798{
b775012e
LM
11799 /* Last breakpoint location address that was marked for update. */
11800 CORE_ADDR last_addr = 0;
11801 /* Last breakpoint location program space that was marked for update. */
11802 int last_pspace_num = -1;
f7545552 11803
2d134ed3
PA
11804 /* Used in the duplicates detection below. When iterating over all
11805 bp_locations, points to the first bp_location of a given address.
11806 Breakpoints and watchpoints of different types are never
11807 duplicates of each other. Keep one pointer for each type of
11808 breakpoint/watchpoint, so we only need to loop over all locations
11809 once. */
11810 struct bp_location *bp_loc_first; /* breakpoint */
11811 struct bp_location *wp_loc_first; /* hardware watchpoint */
11812 struct bp_location *awp_loc_first; /* access watchpoint */
11813 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11814
f5336ca5
PA
11815 /* Saved former bp_locations array which we compare against the newly
11816 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11817 std::vector<bp_location *> old_locations = std::move (bp_locations);
11818 bp_locations.clear ();
876fa593 11819
43892fdf 11820 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11821 for (bp_location *loc : b->locations ())
5d51cd5d 11822 bp_locations.push_back (loc);
7f32a4d5
PA
11823
11824 /* See if we need to "upgrade" a software breakpoint to a hardware
11825 breakpoint. Do this before deciding whether locations are
11826 duplicates. Also do this before sorting because sorting order
11827 depends on location type. */
5d51cd5d
SM
11828 for (bp_location *loc : bp_locations)
11829 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11830 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11831
5d51cd5d 11832 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11833 bp_location_is_less_than);
876fa593 11834
f5336ca5 11835 bp_locations_target_extensions_update ();
74960c60 11836
4a64f543
MS
11837 /* Identify bp_location instances that are no longer present in the
11838 new list, and therefore should be freed. Note that it's not
11839 necessary that those locations should be removed from inferior --
11840 if there's another location at the same address (previously
11841 marked as duplicate), we don't need to remove/insert the
11842 location.
876fa593 11843
4a64f543
MS
11844 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11845 and former bp_location array state respectively. */
876fa593 11846
5d51cd5d
SM
11847 size_t loc_i = 0;
11848 for (bp_location *old_loc : old_locations)
74960c60 11849 {
e5dd4106 11850 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11851 not, we have to free it. */
c7d46a38 11852 int found_object = 0;
20874c92
VP
11853 /* Tells if the location should remain inserted in the target. */
11854 int keep_in_target = 0;
11855 int removed = 0;
876fa593 11856
4a64f543
MS
11857 /* Skip LOCP entries which will definitely never be needed.
11858 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11859 while (loc_i < bp_locations.size ()
11860 && bp_locations[loc_i]->address < old_loc->address)
11861 loc_i++;
c7d46a38 11862
5d51cd5d
SM
11863 for (size_t loc2_i = loc_i;
11864 (loc2_i < bp_locations.size ()
11865 && bp_locations[loc2_i]->address == old_loc->address);
11866 loc2_i++)
c7d46a38 11867 {
b775012e
LM
11868 /* Check if this is a new/duplicated location or a duplicated
11869 location that had its condition modified. If so, we want to send
11870 its condition to the target if evaluation of conditions is taking
11871 place there. */
5d51cd5d 11872 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11873 && (last_addr != old_loc->address
11874 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11875 {
5d51cd5d 11876 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11877 last_pspace_num = old_loc->pspace->num;
c7d46a38 11878 }
b775012e 11879
5d51cd5d 11880 if (bp_locations[loc2_i] == old_loc)
b775012e 11881 found_object = 1;
c7d46a38 11882 }
74960c60 11883
b775012e
LM
11884 /* We have already handled this address, update it so that we don't
11885 have to go through updates again. */
11886 last_addr = old_loc->address;
11887
11888 /* Target-side condition evaluation: Handle deleted locations. */
11889 if (!found_object)
11890 force_breakpoint_reinsertion (old_loc);
11891
4a64f543
MS
11892 /* If this location is no longer present, and inserted, look if
11893 there's maybe a new location at the same address. If so,
11894 mark that one inserted, and don't remove this one. This is
11895 needed so that we don't have a time window where a breakpoint
11896 at certain location is not inserted. */
74960c60 11897
876fa593 11898 if (old_loc->inserted)
0d381245 11899 {
4a64f543
MS
11900 /* If the location is inserted now, we might have to remove
11901 it. */
74960c60 11902
876fa593 11903 if (found_object && should_be_inserted (old_loc))
74960c60 11904 {
4a64f543
MS
11905 /* The location is still present in the location list,
11906 and still should be inserted. Don't do anything. */
20874c92 11907 keep_in_target = 1;
74960c60
VP
11908 }
11909 else
11910 {
b775012e
LM
11911 /* This location still exists, but it won't be kept in the
11912 target since it may have been disabled. We proceed to
11913 remove its target-side condition. */
11914
4a64f543
MS
11915 /* The location is either no longer present, or got
11916 disabled. See if there's another location at the
11917 same address, in which case we don't need to remove
11918 this one from the target. */
876fa593 11919
2bdf28a0 11920 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11921 if (bl_address_is_meaningful (old_loc))
876fa593 11922 {
5d51cd5d
SM
11923 for (size_t loc2_i = loc_i;
11924 (loc2_i < bp_locations.size ()
11925 && bp_locations[loc2_i]->address == old_loc->address);
11926 loc2_i++)
876fa593 11927 {
5d51cd5d 11928 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11929
7f32a4d5
PA
11930 if (loc2 == old_loc)
11931 continue;
11932
2d134ed3 11933 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11934 {
85d721b8
PA
11935 /* Read watchpoint locations are switched to
11936 access watchpoints, if the former are not
11937 supported, but the latter are. */
11938 if (is_hardware_watchpoint (old_loc->owner))
11939 {
11940 gdb_assert (is_hardware_watchpoint (loc2->owner));
11941 loc2->watchpoint_type = old_loc->watchpoint_type;
11942 }
11943
934709f0
PW
11944 /* loc2 is a duplicated location. We need to check
11945 if it should be inserted in case it will be
11946 unduplicated. */
7f32a4d5 11947 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11948 {
934709f0 11949 swap_insertion (old_loc, loc2);
c7d46a38
PA
11950 keep_in_target = 1;
11951 break;
11952 }
876fa593
JK
11953 }
11954 }
11955 }
74960c60
VP
11956 }
11957
20874c92
VP
11958 if (!keep_in_target)
11959 {
834c0d03 11960 if (remove_breakpoint (old_loc))
20874c92 11961 {
4a64f543
MS
11962 /* This is just about all we can do. We could keep
11963 this location on the global list, and try to
11964 remove it next time, but there's no particular
11965 reason why we will succeed next time.
20874c92 11966
4a64f543
MS
11967 Note that at this point, old_loc->owner is still
11968 valid, as delete_breakpoint frees the breakpoint
11969 only after calling us. */
3e43a32a
MS
11970 printf_filtered (_("warning: Error removing "
11971 "breakpoint %d\n"),
876fa593 11972 old_loc->owner->number);
20874c92
VP
11973 }
11974 removed = 1;
11975 }
0d381245 11976 }
74960c60
VP
11977
11978 if (!found_object)
1c5cfe86 11979 {
fbea99ea 11980 if (removed && target_is_non_stop_p ()
1cf4d951 11981 && need_moribund_for_location_type (old_loc))
20874c92 11982 {
db82e815
PA
11983 /* This location was removed from the target. In
11984 non-stop mode, a race condition is possible where
11985 we've removed a breakpoint, but stop events for that
11986 breakpoint are already queued and will arrive later.
11987 We apply an heuristic to be able to distinguish such
11988 SIGTRAPs from other random SIGTRAPs: we keep this
11989 breakpoint location for a bit, and will retire it
11990 after we see some number of events. The theory here
11991 is that reporting of events should, "on the average",
11992 be fair, so after a while we'll see events from all
11993 threads that have anything of interest, and no longer
11994 need to keep this breakpoint location around. We
11995 don't hold locations forever so to reduce chances of
11996 mistaking a non-breakpoint SIGTRAP for a breakpoint
11997 SIGTRAP.
11998
11999 The heuristic failing can be disastrous on
12000 decr_pc_after_break targets.
12001
12002 On decr_pc_after_break targets, like e.g., x86-linux,
12003 if we fail to recognize a late breakpoint SIGTRAP,
12004 because events_till_retirement has reached 0 too
12005 soon, we'll fail to do the PC adjustment, and report
12006 a random SIGTRAP to the user. When the user resumes
12007 the inferior, it will most likely immediately crash
2dec564e 12008 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12009 corrupted, because of being resumed e.g., in the
12010 middle of a multi-byte instruction, or skipped a
12011 one-byte instruction. This was actually seen happen
12012 on native x86-linux, and should be less rare on
12013 targets that do not support new thread events, like
12014 remote, due to the heuristic depending on
12015 thread_count.
12016
12017 Mistaking a random SIGTRAP for a breakpoint trap
12018 causes similar symptoms (PC adjustment applied when
12019 it shouldn't), but then again, playing with SIGTRAPs
12020 behind the debugger's back is asking for trouble.
12021
12022 Since hardware watchpoint traps are always
12023 distinguishable from other traps, so we don't need to
12024 apply keep hardware watchpoint moribund locations
12025 around. We simply always ignore hardware watchpoint
12026 traps we can no longer explain. */
12027
5b6d1e4f
PA
12028 process_stratum_target *proc_target = nullptr;
12029 for (inferior *inf : all_inferiors ())
12030 if (inf->pspace == old_loc->pspace)
12031 {
12032 proc_target = inf->process_target ();
12033 break;
12034 }
12035 if (proc_target != nullptr)
12036 old_loc->events_till_retirement
12037 = 3 * (thread_count (proc_target) + 1);
12038 else
12039 old_loc->events_till_retirement = 1;
876fa593 12040 old_loc->owner = NULL;
20874c92 12041
1123588c 12042 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12043 }
12044 else
f431efe5
PA
12045 {
12046 old_loc->owner = NULL;
12047 decref_bp_location (&old_loc);
12048 }
20874c92 12049 }
74960c60 12050 }
1c5cfe86 12051
348d480f
PA
12052 /* Rescan breakpoints at the same address and section, marking the
12053 first one as "first" and any others as "duplicates". This is so
12054 that the bpt instruction is only inserted once. If we have a
12055 permanent breakpoint at the same place as BPT, make that one the
12056 official one, and the rest as duplicates. Permanent breakpoints
12057 are sorted first for the same address.
12058
12059 Do the same for hardware watchpoints, but also considering the
12060 watchpoint's type (regular/access/read) and length. */
12061
12062 bp_loc_first = NULL;
12063 wp_loc_first = NULL;
12064 awp_loc_first = NULL;
12065 rwp_loc_first = NULL;
40cb8ca5 12066
48d7020b 12067 for (bp_location *loc : all_bp_locations ())
348d480f
PA
12068 {
12069 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12070 non-NULL. */
348d480f 12071 struct bp_location **loc_first_p;
43892fdf 12072 breakpoint *b = loc->owner;
348d480f 12073
6f380991 12074 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12075 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12076 /* Don't detect duplicate for tracepoint locations because they are
12077 never duplicated. See the comments in field `duplicate' of
12078 `struct bp_location'. */
348d480f 12079 || is_tracepoint (b))
b775012e
LM
12080 {
12081 /* Clear the condition modification flag. */
12082 loc->condition_changed = condition_unchanged;
12083 continue;
12084 }
348d480f 12085
348d480f
PA
12086 if (b->type == bp_hardware_watchpoint)
12087 loc_first_p = &wp_loc_first;
12088 else if (b->type == bp_read_watchpoint)
12089 loc_first_p = &rwp_loc_first;
12090 else if (b->type == bp_access_watchpoint)
12091 loc_first_p = &awp_loc_first;
12092 else
12093 loc_first_p = &bp_loc_first;
12094
12095 if (*loc_first_p == NULL
12096 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12097 || !breakpoint_locations_match (loc, *loc_first_p))
12098 {
12099 *loc_first_p = loc;
12100 loc->duplicate = 0;
b775012e
LM
12101
12102 if (is_breakpoint (loc->owner) && loc->condition_changed)
12103 {
12104 loc->needs_update = 1;
12105 /* Clear the condition modification flag. */
12106 loc->condition_changed = condition_unchanged;
12107 }
348d480f
PA
12108 continue;
12109 }
12110
934709f0
PW
12111
12112 /* This and the above ensure the invariant that the first location
12113 is not duplicated, and is the inserted one.
12114 All following are marked as duplicated, and are not inserted. */
12115 if (loc->inserted)
12116 swap_insertion (loc, *loc_first_p);
348d480f
PA
12117 loc->duplicate = 1;
12118
b775012e
LM
12119 /* Clear the condition modification flag. */
12120 loc->condition_changed = condition_unchanged;
348d480f
PA
12121 }
12122
a25a5a45 12123 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12124 {
04086b45 12125 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12126 insert_breakpoint_locations ();
12127 else
12128 {
44702360
PA
12129 /* Even though the caller told us to not insert new
12130 locations, we may still need to update conditions on the
12131 target's side of breakpoints that were already inserted
12132 if the target is evaluating breakpoint conditions. We
b775012e
LM
12133 only update conditions for locations that are marked
12134 "needs_update". */
12135 update_inserted_breakpoint_locations ();
12136 }
12137 }
348d480f 12138
04086b45 12139 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12140 download_tracepoint_locations ();
348d480f
PA
12141}
12142
12143void
12144breakpoint_retire_moribund (void)
12145{
1123588c
TT
12146 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12147 {
12148 struct bp_location *loc = moribund_locations[ix];
12149 if (--(loc->events_till_retirement) == 0)
12150 {
12151 decref_bp_location (&loc);
12152 unordered_remove (moribund_locations, ix);
12153 --ix;
12154 }
12155 }
348d480f
PA
12156}
12157
12158static void
44702360 12159update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12160{
348d480f 12161
a70b8144 12162 try
492d29ea
PA
12163 {
12164 update_global_location_list (insert_mode);
12165 }
230d2906 12166 catch (const gdb_exception_error &e)
492d29ea
PA
12167 {
12168 }
348d480f
PA
12169}
12170
12171/* Clear BKP from a BPS. */
12172
12173static void
12174bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12175{
12176 bpstat bs;
12177
12178 for (bs = bps; bs; bs = bs->next)
12179 if (bs->breakpoint_at == bpt)
12180 {
12181 bs->breakpoint_at = NULL;
12182 bs->old_val = NULL;
12183 /* bs->commands will be freed later. */
12184 }
12185}
12186
12187/* Callback for iterate_over_threads. */
12188static int
12189bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12190{
9a3c8263 12191 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12192
12193 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12194 return 0;
12195}
12196
12197/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12198 callbacks. */
12199
12200static void
12201say_where (struct breakpoint *b)
12202{
12203 struct value_print_options opts;
12204
12205 get_user_print_options (&opts);
12206
12207 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12208 single string. */
12209 if (b->loc == NULL)
12210 {
f00aae0f
KS
12211 /* For pending locations, the output differs slightly based
12212 on b->extra_string. If this is non-NULL, it contains either
12213 a condition or dprintf arguments. */
12214 if (b->extra_string == NULL)
12215 {
12216 printf_filtered (_(" (%s) pending."),
d28cd78a 12217 event_location_to_string (b->location.get ()));
f00aae0f
KS
12218 }
12219 else if (b->type == bp_dprintf)
12220 {
12221 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12222 event_location_to_string (b->location.get ()),
f00aae0f
KS
12223 b->extra_string);
12224 }
12225 else
12226 {
12227 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12228 event_location_to_string (b->location.get ()),
f00aae0f
KS
12229 b->extra_string);
12230 }
348d480f
PA
12231 }
12232 else
12233 {
2f202fde 12234 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12235 printf_filtered (" at %ps",
12236 styled_string (address_style.style (),
12237 paddress (b->loc->gdbarch,
12238 b->loc->address)));
2f202fde 12239 if (b->loc->symtab != NULL)
f8eba3c6
TT
12240 {
12241 /* If there is a single location, we can print the location
12242 more nicely. */
12243 if (b->loc->next == NULL)
0bb296cb 12244 {
6a831f06
PA
12245 const char *filename
12246 = symtab_to_filename_for_display (b->loc->symtab);
12247 printf_filtered (": file %ps, line %d.",
12248 styled_string (file_name_style.style (),
12249 filename),
0bb296cb
TT
12250 b->loc->line_number);
12251 }
f8eba3c6
TT
12252 else
12253 /* This is not ideal, but each location may have a
12254 different file name, and this at least reflects the
12255 real situation somewhat. */
f00aae0f 12256 printf_filtered (": %s.",
d28cd78a 12257 event_location_to_string (b->location.get ()));
f8eba3c6 12258 }
348d480f
PA
12259
12260 if (b->loc->next)
12261 {
12262 struct bp_location *loc = b->loc;
12263 int n = 0;
12264 for (; loc; loc = loc->next)
12265 ++n;
12266 printf_filtered (" (%d locations)", n);
12267 }
12268 }
12269}
12270
5f486660 12271bp_location::~bp_location ()
348d480f 12272{
5f486660 12273 xfree (function_name);
348d480f
PA
12274}
12275
c1fc2657 12276/* Destructor for the breakpoint base class. */
348d480f 12277
c1fc2657 12278breakpoint::~breakpoint ()
348d480f 12279{
c1fc2657
SM
12280 xfree (this->cond_string);
12281 xfree (this->extra_string);
348d480f
PA
12282}
12283
40cb8ca5
SM
12284/* See breakpoint.h. */
12285
12286bp_locations_range breakpoint::locations ()
12287{
12288 return bp_locations_range (this->loc);
12289}
12290
2060206e
PA
12291static struct bp_location *
12292base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12293{
5f486660 12294 return new bp_location (self);
348d480f
PA
12295}
12296
2060206e
PA
12297static void
12298base_breakpoint_re_set (struct breakpoint *b)
12299{
12300 /* Nothing to re-set. */
12301}
12302
12303#define internal_error_pure_virtual_called() \
12304 gdb_assert_not_reached ("pure virtual function called")
12305
12306static int
12307base_breakpoint_insert_location (struct bp_location *bl)
12308{
12309 internal_error_pure_virtual_called ();
12310}
12311
12312static int
73971819
PA
12313base_breakpoint_remove_location (struct bp_location *bl,
12314 enum remove_bp_reason reason)
2060206e
PA
12315{
12316 internal_error_pure_virtual_called ();
12317}
12318
12319static int
12320base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12321 const address_space *aspace,
09ac7c10
TT
12322 CORE_ADDR bp_addr,
12323 const struct target_waitstatus *ws)
2060206e
PA
12324{
12325 internal_error_pure_virtual_called ();
12326}
12327
12328static void
12329base_breakpoint_check_status (bpstat bs)
12330{
12331 /* Always stop. */
12332}
12333
12334/* A "works_in_software_mode" breakpoint_ops method that just internal
12335 errors. */
12336
12337static int
12338base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12339{
12340 internal_error_pure_virtual_called ();
12341}
12342
12343/* A "resources_needed" breakpoint_ops method that just internal
12344 errors. */
12345
12346static int
12347base_breakpoint_resources_needed (const struct bp_location *bl)
12348{
12349 internal_error_pure_virtual_called ();
12350}
12351
12352static enum print_stop_action
12353base_breakpoint_print_it (bpstat bs)
12354{
12355 internal_error_pure_virtual_called ();
12356}
12357
12358static void
12359base_breakpoint_print_one_detail (const struct breakpoint *self,
12360 struct ui_out *uiout)
12361{
12362 /* nothing */
12363}
12364
12365static void
12366base_breakpoint_print_mention (struct breakpoint *b)
12367{
12368 internal_error_pure_virtual_called ();
12369}
12370
12371static void
12372base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12373{
12374 internal_error_pure_virtual_called ();
12375}
12376
983af33b 12377static void
f00aae0f 12378base_breakpoint_create_sals_from_location
626d2320 12379 (struct event_location *location,
f00aae0f
KS
12380 struct linespec_result *canonical,
12381 enum bptype type_wanted)
983af33b
SDJ
12382{
12383 internal_error_pure_virtual_called ();
12384}
12385
12386static void
12387base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12388 struct linespec_result *c,
e1e01040
PA
12389 gdb::unique_xmalloc_ptr<char> cond_string,
12390 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12391 enum bptype type_wanted,
12392 enum bpdisp disposition,
12393 int thread,
12394 int task, int ignore_count,
12395 const struct breakpoint_ops *o,
12396 int from_tty, int enabled,
44f238bb 12397 int internal, unsigned flags)
983af33b
SDJ
12398{
12399 internal_error_pure_virtual_called ();
12400}
12401
6c5b2ebe 12402static std::vector<symtab_and_line>
f00aae0f 12403base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12404 struct event_location *location,
6c5b2ebe 12405 struct program_space *search_pspace)
983af33b
SDJ
12406{
12407 internal_error_pure_virtual_called ();
12408}
12409
ab04a2af
TT
12410/* The default 'explains_signal' method. */
12411
47591c29 12412static int
427cd150 12413base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12414{
47591c29 12415 return 1;
ab04a2af
TT
12416}
12417
9d6e6e84
HZ
12418/* The default "after_condition_true" method. */
12419
12420static void
12421base_breakpoint_after_condition_true (struct bpstats *bs)
12422{
12423 /* Nothing to do. */
12424}
12425
ab04a2af 12426struct breakpoint_ops base_breakpoint_ops =
2060206e 12427{
2060206e
PA
12428 base_breakpoint_allocate_location,
12429 base_breakpoint_re_set,
12430 base_breakpoint_insert_location,
12431 base_breakpoint_remove_location,
12432 base_breakpoint_breakpoint_hit,
12433 base_breakpoint_check_status,
12434 base_breakpoint_resources_needed,
12435 base_breakpoint_works_in_software_mode,
12436 base_breakpoint_print_it,
12437 NULL,
12438 base_breakpoint_print_one_detail,
12439 base_breakpoint_print_mention,
983af33b 12440 base_breakpoint_print_recreate,
5f700d83 12441 base_breakpoint_create_sals_from_location,
983af33b 12442 base_breakpoint_create_breakpoints_sal,
5f700d83 12443 base_breakpoint_decode_location,
9d6e6e84
HZ
12444 base_breakpoint_explains_signal,
12445 base_breakpoint_after_condition_true,
2060206e
PA
12446};
12447
12448/* Default breakpoint_ops methods. */
12449
12450static void
348d480f
PA
12451bkpt_re_set (struct breakpoint *b)
12452{
06edf0c0 12453 /* FIXME: is this still reachable? */
9ef9e6a6 12454 if (breakpoint_event_location_empty_p (b))
06edf0c0 12455 {
f00aae0f 12456 /* Anything without a location can't be re-set. */
348d480f 12457 delete_breakpoint (b);
06edf0c0 12458 return;
348d480f 12459 }
06edf0c0
PA
12460
12461 breakpoint_re_set_default (b);
348d480f
PA
12462}
12463
2060206e 12464static int
348d480f
PA
12465bkpt_insert_location (struct bp_location *bl)
12466{
cd6c3b4f
YQ
12467 CORE_ADDR addr = bl->target_info.reqstd_address;
12468
579c6ad9 12469 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12470 bl->target_info.placed_address = addr;
12471
348d480f 12472 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12473 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12474 else
7c16b83e 12475 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12476}
12477
2060206e 12478static int
73971819 12479bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12480{
12481 if (bl->loc_type == bp_loc_hardware_breakpoint)
12482 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12483 else
73971819 12484 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12485}
12486
2060206e 12487static int
348d480f 12488bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12489 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12490 const struct target_waitstatus *ws)
348d480f 12491{
09ac7c10 12492 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12493 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12494 return 0;
12495
348d480f
PA
12496 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12497 aspace, bp_addr))
12498 return 0;
12499
12500 if (overlay_debugging /* unmapped overlay section */
12501 && section_is_overlay (bl->section)
12502 && !section_is_mapped (bl->section))
12503 return 0;
12504
12505 return 1;
12506}
12507
cd1608cc
PA
12508static int
12509dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12510 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12511 const struct target_waitstatus *ws)
12512{
12513 if (dprintf_style == dprintf_style_agent
12514 && target_can_run_breakpoint_commands ())
12515 {
12516 /* An agent-style dprintf never causes a stop. If we see a trap
12517 for this address it must be for a breakpoint that happens to
12518 be set at the same address. */
12519 return 0;
12520 }
12521
12522 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12523}
12524
2060206e 12525static int
348d480f
PA
12526bkpt_resources_needed (const struct bp_location *bl)
12527{
12528 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12529
12530 return 1;
12531}
12532
2060206e 12533static enum print_stop_action
348d480f
PA
12534bkpt_print_it (bpstat bs)
12535{
348d480f
PA
12536 struct breakpoint *b;
12537 const struct bp_location *bl;
001c8c33 12538 int bp_temp;
79a45e25 12539 struct ui_out *uiout = current_uiout;
348d480f
PA
12540
12541 gdb_assert (bs->bp_location_at != NULL);
12542
b6433ede 12543 bl = bs->bp_location_at.get ();
348d480f
PA
12544 b = bs->breakpoint_at;
12545
001c8c33
PA
12546 bp_temp = b->disposition == disp_del;
12547 if (bl->address != bl->requested_address)
12548 breakpoint_adjustment_warning (bl->requested_address,
12549 bl->address,
12550 b->number, 1);
12551 annotate_breakpoint (b->number);
f303dbd6
PA
12552 maybe_print_thread_hit_breakpoint (uiout);
12553
112e8700 12554 if (uiout->is_mi_like_p ())
348d480f 12555 {
112e8700 12556 uiout->field_string ("reason",
001c8c33 12557 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12558 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12559 }
6a831f06
PA
12560 if (bp_temp)
12561 uiout->message ("Temporary breakpoint %pF, ",
12562 signed_field ("bkptno", b->number));
12563 else
12564 uiout->message ("Breakpoint %pF, ",
12565 signed_field ("bkptno", b->number));
06edf0c0 12566
001c8c33 12567 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12568}
12569
2060206e 12570static void
06edf0c0
PA
12571bkpt_print_mention (struct breakpoint *b)
12572{
112e8700 12573 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12574 return;
12575
12576 switch (b->type)
12577 {
12578 case bp_breakpoint:
12579 case bp_gnu_ifunc_resolver:
12580 if (b->disposition == disp_del)
12581 printf_filtered (_("Temporary breakpoint"));
12582 else
12583 printf_filtered (_("Breakpoint"));
12584 printf_filtered (_(" %d"), b->number);
12585 if (b->type == bp_gnu_ifunc_resolver)
12586 printf_filtered (_(" at gnu-indirect-function resolver"));
12587 break;
12588 case bp_hardware_breakpoint:
12589 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12590 break;
e7e0cddf
SS
12591 case bp_dprintf:
12592 printf_filtered (_("Dprintf %d"), b->number);
12593 break;
06edf0c0
PA
12594 }
12595
12596 say_where (b);
12597}
12598
2060206e 12599static void
06edf0c0
PA
12600bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12601{
12602 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12603 fprintf_unfiltered (fp, "tbreak");
12604 else if (tp->type == bp_breakpoint)
12605 fprintf_unfiltered (fp, "break");
12606 else if (tp->type == bp_hardware_breakpoint
12607 && tp->disposition == disp_del)
12608 fprintf_unfiltered (fp, "thbreak");
12609 else if (tp->type == bp_hardware_breakpoint)
12610 fprintf_unfiltered (fp, "hbreak");
12611 else
12612 internal_error (__FILE__, __LINE__,
12613 _("unhandled breakpoint type %d"), (int) tp->type);
12614
f00aae0f 12615 fprintf_unfiltered (fp, " %s",
d28cd78a 12616 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12617
12618 /* Print out extra_string if this breakpoint is pending. It might
12619 contain, for example, conditions that were set by the user. */
12620 if (tp->loc == NULL && tp->extra_string != NULL)
12621 fprintf_unfiltered (fp, " %s", tp->extra_string);
12622
dd11a36c 12623 print_recreate_thread (tp, fp);
06edf0c0
PA
12624}
12625
983af33b 12626static void
626d2320 12627bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12628 struct linespec_result *canonical,
12629 enum bptype type_wanted)
983af33b 12630{
f00aae0f 12631 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12632}
12633
12634static void
12635bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12636 struct linespec_result *canonical,
e1e01040
PA
12637 gdb::unique_xmalloc_ptr<char> cond_string,
12638 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12639 enum bptype type_wanted,
12640 enum bpdisp disposition,
12641 int thread,
12642 int task, int ignore_count,
12643 const struct breakpoint_ops *ops,
12644 int from_tty, int enabled,
44f238bb 12645 int internal, unsigned flags)
983af33b 12646{
023fa29b 12647 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12648 std::move (cond_string),
12649 std::move (extra_string),
e7e0cddf 12650 type_wanted,
983af33b
SDJ
12651 disposition, thread, task,
12652 ignore_count, ops, from_tty,
44f238bb 12653 enabled, internal, flags);
983af33b
SDJ
12654}
12655
6c5b2ebe 12656static std::vector<symtab_and_line>
f00aae0f 12657bkpt_decode_location (struct breakpoint *b,
626d2320 12658 struct event_location *location,
6c5b2ebe 12659 struct program_space *search_pspace)
983af33b 12660{
6c5b2ebe 12661 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12662}
12663
06edf0c0
PA
12664/* Virtual table for internal breakpoints. */
12665
12666static void
12667internal_bkpt_re_set (struct breakpoint *b)
12668{
12669 switch (b->type)
12670 {
12671 /* Delete overlay event and longjmp master breakpoints; they
12672 will be reset later by breakpoint_re_set. */
12673 case bp_overlay_event:
12674 case bp_longjmp_master:
12675 case bp_std_terminate_master:
12676 case bp_exception_master:
12677 delete_breakpoint (b);
12678 break;
12679
12680 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12681 starts and we really don't want to touch it. */
06edf0c0
PA
12682 case bp_shlib_event:
12683
12684 /* Like bp_shlib_event, this breakpoint type is special. Once
12685 it is set up, we do not want to touch it. */
12686 case bp_thread_event:
12687 break;
12688 }
12689}
12690
12691static void
12692internal_bkpt_check_status (bpstat bs)
12693{
a9b3a50f
PA
12694 if (bs->breakpoint_at->type == bp_shlib_event)
12695 {
12696 /* If requested, stop when the dynamic linker notifies GDB of
12697 events. This allows the user to get control and place
12698 breakpoints in initializer routines for dynamically loaded
12699 objects (among other things). */
12700 bs->stop = stop_on_solib_events;
12701 bs->print = stop_on_solib_events;
12702 }
12703 else
12704 bs->stop = 0;
06edf0c0
PA
12705}
12706
12707static enum print_stop_action
12708internal_bkpt_print_it (bpstat bs)
12709{
06edf0c0 12710 struct breakpoint *b;
06edf0c0 12711
06edf0c0
PA
12712 b = bs->breakpoint_at;
12713
06edf0c0
PA
12714 switch (b->type)
12715 {
348d480f
PA
12716 case bp_shlib_event:
12717 /* Did we stop because the user set the stop_on_solib_events
12718 variable? (If so, we report this as a generic, "Stopped due
12719 to shlib event" message.) */
edcc5120 12720 print_solib_event (0);
348d480f
PA
12721 break;
12722
12723 case bp_thread_event:
12724 /* Not sure how we will get here.
12725 GDB should not stop for these breakpoints. */
12726 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12727 break;
12728
12729 case bp_overlay_event:
12730 /* By analogy with the thread event, GDB should not stop for these. */
12731 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12732 break;
12733
12734 case bp_longjmp_master:
12735 /* These should never be enabled. */
12736 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12737 break;
12738
12739 case bp_std_terminate_master:
12740 /* These should never be enabled. */
12741 printf_filtered (_("std::terminate Master Breakpoint: "
12742 "gdb should not stop!\n"));
348d480f
PA
12743 break;
12744
12745 case bp_exception_master:
12746 /* These should never be enabled. */
12747 printf_filtered (_("Exception Master Breakpoint: "
12748 "gdb should not stop!\n"));
06edf0c0
PA
12749 break;
12750 }
12751
001c8c33 12752 return PRINT_NOTHING;
06edf0c0
PA
12753}
12754
12755static void
12756internal_bkpt_print_mention (struct breakpoint *b)
12757{
12758 /* Nothing to mention. These breakpoints are internal. */
12759}
12760
06edf0c0
PA
12761/* Virtual table for momentary breakpoints */
12762
12763static void
12764momentary_bkpt_re_set (struct breakpoint *b)
12765{
12766 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12767 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12768 Otherwise these should have been blown away via the cleanup chain
12769 or by breakpoint_init_inferior when we rerun the executable. */
12770}
12771
12772static void
12773momentary_bkpt_check_status (bpstat bs)
12774{
12775 /* Nothing. The point of these breakpoints is causing a stop. */
12776}
12777
12778static enum print_stop_action
12779momentary_bkpt_print_it (bpstat bs)
12780{
001c8c33 12781 return PRINT_UNKNOWN;
348d480f
PA
12782}
12783
06edf0c0
PA
12784static void
12785momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12786{
06edf0c0 12787 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12788}
12789
e2e4d78b
JK
12790/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12791
12792 It gets cleared already on the removal of the first one of such placed
12793 breakpoints. This is OK as they get all removed altogether. */
12794
c1fc2657 12795longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12796{
c1fc2657 12797 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12798
c1fc2657 12799 if (tp != NULL)
e2e4d78b 12800 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12801}
12802
55aa24fb
SDJ
12803/* Specific methods for probe breakpoints. */
12804
12805static int
12806bkpt_probe_insert_location (struct bp_location *bl)
12807{
12808 int v = bkpt_insert_location (bl);
12809
12810 if (v == 0)
12811 {
12812 /* The insertion was successful, now let's set the probe's semaphore
12813 if needed. */
935676c9 12814 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12815 }
12816
12817 return v;
12818}
12819
12820static int
73971819
PA
12821bkpt_probe_remove_location (struct bp_location *bl,
12822 enum remove_bp_reason reason)
55aa24fb
SDJ
12823{
12824 /* Let's clear the semaphore before removing the location. */
935676c9 12825 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12826
73971819 12827 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12828}
12829
12830static void
626d2320 12831bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12832 struct linespec_result *canonical,
f00aae0f 12833 enum bptype type_wanted)
55aa24fb
SDJ
12834{
12835 struct linespec_sals lsal;
12836
c2f4122d 12837 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12838 lsal.canonical
12839 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12840 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12841}
12842
6c5b2ebe 12843static std::vector<symtab_and_line>
f00aae0f 12844bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12845 struct event_location *location,
6c5b2ebe 12846 struct program_space *search_pspace)
55aa24fb 12847{
6c5b2ebe
PA
12848 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12849 if (sals.empty ())
55aa24fb 12850 error (_("probe not found"));
6c5b2ebe 12851 return sals;
55aa24fb
SDJ
12852}
12853
348d480f 12854/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12855
348d480f
PA
12856static void
12857tracepoint_re_set (struct breakpoint *b)
12858{
12859 breakpoint_re_set_default (b);
12860}
876fa593 12861
348d480f
PA
12862static int
12863tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12864 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12865 const struct target_waitstatus *ws)
348d480f
PA
12866{
12867 /* By definition, the inferior does not report stops at
12868 tracepoints. */
12869 return 0;
74960c60
VP
12870}
12871
12872static void
348d480f
PA
12873tracepoint_print_one_detail (const struct breakpoint *self,
12874 struct ui_out *uiout)
74960c60 12875{
d9b3f62e 12876 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12877 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12878 {
12879 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12880
6a831f06
PA
12881 uiout->message ("\tmarker id is %pF\n",
12882 string_field ("static-tracepoint-marker-string-id",
12883 tp->static_trace_marker_id.c_str ()));
348d480f 12884 }
0d381245
VP
12885}
12886
a474d7c2 12887static void
348d480f 12888tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12889{
112e8700 12890 if (current_uiout->is_mi_like_p ())
348d480f 12891 return;
cc59ec59 12892
348d480f
PA
12893 switch (b->type)
12894 {
12895 case bp_tracepoint:
12896 printf_filtered (_("Tracepoint"));
12897 printf_filtered (_(" %d"), b->number);
12898 break;
12899 case bp_fast_tracepoint:
12900 printf_filtered (_("Fast tracepoint"));
12901 printf_filtered (_(" %d"), b->number);
12902 break;
12903 case bp_static_tracepoint:
12904 printf_filtered (_("Static tracepoint"));
12905 printf_filtered (_(" %d"), b->number);
12906 break;
12907 default:
12908 internal_error (__FILE__, __LINE__,
12909 _("unhandled tracepoint type %d"), (int) b->type);
12910 }
12911
12912 say_where (b);
a474d7c2
PA
12913}
12914
348d480f 12915static void
d9b3f62e 12916tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12917{
d9b3f62e
PA
12918 struct tracepoint *tp = (struct tracepoint *) self;
12919
12920 if (self->type == bp_fast_tracepoint)
348d480f 12921 fprintf_unfiltered (fp, "ftrace");
c93e8391 12922 else if (self->type == bp_static_tracepoint)
348d480f 12923 fprintf_unfiltered (fp, "strace");
d9b3f62e 12924 else if (self->type == bp_tracepoint)
348d480f
PA
12925 fprintf_unfiltered (fp, "trace");
12926 else
12927 internal_error (__FILE__, __LINE__,
d9b3f62e 12928 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12929
f00aae0f 12930 fprintf_unfiltered (fp, " %s",
d28cd78a 12931 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12932 print_recreate_thread (self, fp);
12933
12934 if (tp->pass_count)
12935 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12936}
12937
983af33b 12938static void
626d2320 12939tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12940 struct linespec_result *canonical,
12941 enum bptype type_wanted)
983af33b 12942{
f00aae0f 12943 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12944}
12945
12946static void
12947tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12948 struct linespec_result *canonical,
e1e01040
PA
12949 gdb::unique_xmalloc_ptr<char> cond_string,
12950 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12951 enum bptype type_wanted,
12952 enum bpdisp disposition,
12953 int thread,
12954 int task, int ignore_count,
12955 const struct breakpoint_ops *ops,
12956 int from_tty, int enabled,
44f238bb 12957 int internal, unsigned flags)
983af33b 12958{
023fa29b 12959 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12960 std::move (cond_string),
12961 std::move (extra_string),
e7e0cddf 12962 type_wanted,
983af33b
SDJ
12963 disposition, thread, task,
12964 ignore_count, ops, from_tty,
44f238bb 12965 enabled, internal, flags);
983af33b
SDJ
12966}
12967
6c5b2ebe 12968static std::vector<symtab_and_line>
f00aae0f 12969tracepoint_decode_location (struct breakpoint *b,
626d2320 12970 struct event_location *location,
6c5b2ebe 12971 struct program_space *search_pspace)
983af33b 12972{
6c5b2ebe 12973 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12974}
12975
2060206e 12976struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12977
bac7c5cf 12978/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12979
12980static void
f00aae0f 12981tracepoint_probe_create_sals_from_location
626d2320 12982 (struct event_location *location,
f00aae0f
KS
12983 struct linespec_result *canonical,
12984 enum bptype type_wanted)
55aa24fb
SDJ
12985{
12986 /* We use the same method for breakpoint on probes. */
f00aae0f 12987 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12988}
12989
6c5b2ebe 12990static std::vector<symtab_and_line>
f00aae0f 12991tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 12992 struct event_location *location,
6c5b2ebe 12993 struct program_space *search_pspace)
55aa24fb
SDJ
12994{
12995 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12996 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12997}
12998
5c2b4418
HZ
12999/* Dprintf breakpoint_ops methods. */
13000
13001static void
13002dprintf_re_set (struct breakpoint *b)
13003{
13004 breakpoint_re_set_default (b);
13005
f00aae0f
KS
13006 /* extra_string should never be non-NULL for dprintf. */
13007 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13008
13009 /* 1 - connect to target 1, that can run breakpoint commands.
13010 2 - create a dprintf, which resolves fine.
13011 3 - disconnect from target 1
13012 4 - connect to target 2, that can NOT run breakpoint commands.
13013
13014 After steps #3/#4, you'll want the dprintf command list to
13015 be updated, because target 1 and 2 may well return different
13016 answers for target_can_run_breakpoint_commands().
13017 Given absence of finer grained resetting, we get to do
13018 it all the time. */
13019 if (b->extra_string != NULL)
13020 update_dprintf_command_list (b);
13021}
13022
2d9442cc
HZ
13023/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13024
13025static void
13026dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13027{
f00aae0f 13028 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13029 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13030 tp->extra_string);
13031 print_recreate_thread (tp, fp);
13032}
13033
9d6e6e84
HZ
13034/* Implement the "after_condition_true" breakpoint_ops method for
13035 dprintf.
13036
13037 dprintf's are implemented with regular commands in their command
13038 list, but we run the commands here instead of before presenting the
13039 stop to the user, as dprintf's don't actually cause a stop. This
13040 also makes it so that the commands of multiple dprintfs at the same
13041 address are all handled. */
13042
13043static void
13044dprintf_after_condition_true (struct bpstats *bs)
13045{
04afa70c 13046 struct bpstats tmp_bs;
9d6e6e84
HZ
13047 struct bpstats *tmp_bs_p = &tmp_bs;
13048
13049 /* dprintf's never cause a stop. This wasn't set in the
13050 check_status hook instead because that would make the dprintf's
13051 condition not be evaluated. */
13052 bs->stop = 0;
13053
13054 /* Run the command list here. Take ownership of it instead of
13055 copying. We never want these commands to run later in
13056 bpstat_do_actions, if a breakpoint that causes a stop happens to
13057 be set at same address as this dprintf, or even if running the
13058 commands here throws. */
13059 tmp_bs.commands = bs->commands;
13060 bs->commands = NULL;
9d6e6e84
HZ
13061
13062 bpstat_do_actions_1 (&tmp_bs_p);
13063
13064 /* 'tmp_bs.commands' will usually be NULL by now, but
13065 bpstat_do_actions_1 may return early without processing the whole
13066 list. */
9d6e6e84
HZ
13067}
13068
983af33b
SDJ
13069/* The breakpoint_ops structure to be used on static tracepoints with
13070 markers (`-m'). */
13071
13072static void
626d2320 13073strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13074 struct linespec_result *canonical,
f00aae0f 13075 enum bptype type_wanted)
983af33b
SDJ
13076{
13077 struct linespec_sals lsal;
f00aae0f 13078 const char *arg_start, *arg;
983af33b 13079
a20714ff 13080 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13081 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13082
f2fc3015
TT
13083 std::string str (arg_start, arg - arg_start);
13084 const char *ptr = str.c_str ();
a20714ff
PA
13085 canonical->location
13086 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13087
8e9e35b1
TT
13088 lsal.canonical
13089 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13090 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13091}
13092
13093static void
13094strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13095 struct linespec_result *canonical,
e1e01040
PA
13096 gdb::unique_xmalloc_ptr<char> cond_string,
13097 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13098 enum bptype type_wanted,
13099 enum bpdisp disposition,
13100 int thread,
13101 int task, int ignore_count,
13102 const struct breakpoint_ops *ops,
13103 int from_tty, int enabled,
44f238bb 13104 int internal, unsigned flags)
983af33b 13105{
6c5b2ebe 13106 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13107
13108 /* If the user is creating a static tracepoint by marker id
13109 (strace -m MARKER_ID), then store the sals index, so that
13110 breakpoint_re_set can try to match up which of the newly
13111 found markers corresponds to this one, and, don't try to
13112 expand multiple locations for each sal, given than SALS
13113 already should contain all sals for MARKER_ID. */
13114
6c5b2ebe 13115 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13116 {
6c5b2ebe
PA
13117 event_location_up location
13118 = copy_event_location (canonical->location.get ());
983af33b 13119
b270e6f9 13120 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13121 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13122 std::move (location), NULL,
e1e01040
PA
13123 std::move (cond_string),
13124 std::move (extra_string),
e7e0cddf 13125 type_wanted, disposition,
983af33b 13126 thread, task, ignore_count, ops,
44f238bb 13127 from_tty, enabled, internal, flags,
983af33b
SDJ
13128 canonical->special_display);
13129 /* Given that its possible to have multiple markers with
13130 the same string id, if the user is creating a static
13131 tracepoint by marker id ("strace -m MARKER_ID"), then
13132 store the sals index, so that breakpoint_re_set can
13133 try to match up which of the newly found markers
13134 corresponds to this one */
13135 tp->static_trace_marker_id_idx = i;
13136
b270e6f9 13137 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13138 }
13139}
13140
6c5b2ebe 13141static std::vector<symtab_and_line>
f00aae0f 13142strace_marker_decode_location (struct breakpoint *b,
626d2320 13143 struct event_location *location,
6c5b2ebe 13144 struct program_space *search_pspace)
983af33b
SDJ
13145{
13146 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13147 const char *s = get_linespec_location (location)->spec_string;
983af33b 13148
6c5b2ebe
PA
13149 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13150 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13151 {
6c5b2ebe
PA
13152 sals[0] = sals[tp->static_trace_marker_id_idx];
13153 sals.resize (1);
13154 return sals;
983af33b
SDJ
13155 }
13156 else
5d9310c4 13157 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13158}
13159
13160static struct breakpoint_ops strace_marker_breakpoint_ops;
13161
13162static int
13163strace_marker_p (struct breakpoint *b)
13164{
13165 return b->ops == &strace_marker_breakpoint_ops;
13166}
13167
53a5351d 13168/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13169 structures. */
c906108c
SS
13170
13171void
fba45db2 13172delete_breakpoint (struct breakpoint *bpt)
c906108c 13173{
8a3fe4f8 13174 gdb_assert (bpt != NULL);
c906108c 13175
4a64f543
MS
13176 /* Has this bp already been deleted? This can happen because
13177 multiple lists can hold pointers to bp's. bpstat lists are
13178 especial culprits.
13179
13180 One example of this happening is a watchpoint's scope bp. When
13181 the scope bp triggers, we notice that the watchpoint is out of
13182 scope, and delete it. We also delete its scope bp. But the
13183 scope bp is marked "auto-deleting", and is already on a bpstat.
13184 That bpstat is then checked for auto-deleting bp's, which are
13185 deleted.
13186
13187 A real solution to this problem might involve reference counts in
13188 bp's, and/or giving them pointers back to their referencing
13189 bpstat's, and teaching delete_breakpoint to only free a bp's
13190 storage when no more references were extent. A cheaper bandaid
13191 was chosen. */
c906108c
SS
13192 if (bpt->type == bp_none)
13193 return;
13194
4a64f543
MS
13195 /* At least avoid this stale reference until the reference counting
13196 of breakpoints gets resolved. */
d0fb5eae 13197 if (bpt->related_breakpoint != bpt)
e5a0a904 13198 {
d0fb5eae 13199 struct breakpoint *related;
3a5c3e22 13200 struct watchpoint *w;
d0fb5eae
JK
13201
13202 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13203 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13204 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13205 w = (struct watchpoint *) bpt;
13206 else
13207 w = NULL;
13208 if (w != NULL)
13209 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13210
13211 /* Unlink bpt from the bpt->related_breakpoint ring. */
13212 for (related = bpt; related->related_breakpoint != bpt;
13213 related = related->related_breakpoint);
13214 related->related_breakpoint = bpt->related_breakpoint;
13215 bpt->related_breakpoint = bpt;
e5a0a904
JK
13216 }
13217
a9634178
TJB
13218 /* watch_command_1 creates a watchpoint but only sets its number if
13219 update_watchpoint succeeds in creating its bp_locations. If there's
13220 a problem in that process, we'll be asked to delete the half-created
13221 watchpoint. In that case, don't announce the deletion. */
13222 if (bpt->number)
76727919 13223 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13224
c906108c
SS
13225 if (breakpoint_chain == bpt)
13226 breakpoint_chain = bpt->next;
13227
43892fdf 13228 for (breakpoint *b : all_breakpoints ())
c906108c 13229 if (b->next == bpt)
01add95b
SM
13230 {
13231 b->next = bpt->next;
13232 break;
13233 }
c906108c 13234
f431efe5
PA
13235 /* Be sure no bpstat's are pointing at the breakpoint after it's
13236 been freed. */
13237 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13238 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13239 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13240 commands are associated with the bpstat; if we remove it here,
13241 then the later call to bpstat_do_actions (&stop_bpstat); in
13242 event-top.c won't do anything, and temporary breakpoints with
13243 commands won't work. */
13244
13245 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13246
4a64f543
MS
13247 /* Now that breakpoint is removed from breakpoint list, update the
13248 global location list. This will remove locations that used to
13249 belong to this breakpoint. Do this before freeing the breakpoint
13250 itself, since remove_breakpoint looks at location's owner. It
13251 might be better design to have location completely
13252 self-contained, but it's not the case now. */
44702360 13253 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13254
4a64f543
MS
13255 /* On the chance that someone will soon try again to delete this
13256 same bp, we mark it as deleted before freeing its storage. */
c906108c 13257 bpt->type = bp_none;
4d01a485 13258 delete bpt;
c906108c
SS
13259}
13260
51be5b68
PA
13261/* Iterator function to call a user-provided callback function once
13262 for each of B and its related breakpoints. */
13263
13264static void
13265iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13266 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13267{
13268 struct breakpoint *related;
13269
13270 related = b;
13271 do
13272 {
13273 struct breakpoint *next;
13274
13275 /* FUNCTION may delete RELATED. */
13276 next = related->related_breakpoint;
13277
13278 if (next == related)
13279 {
13280 /* RELATED is the last ring entry. */
48649e1b 13281 function (related);
51be5b68
PA
13282
13283 /* FUNCTION may have deleted it, so we'd never reach back to
13284 B. There's nothing left to do anyway, so just break
13285 out. */
13286 break;
13287 }
13288 else
48649e1b 13289 function (related);
51be5b68
PA
13290
13291 related = next;
13292 }
13293 while (related != b);
13294}
95a42b64 13295
4495129a 13296static void
981a3fb3 13297delete_command (const char *arg, int from_tty)
c906108c 13298{
ea9365bb
TT
13299 dont_repeat ();
13300
c906108c
SS
13301 if (arg == 0)
13302 {
13303 int breaks_to_delete = 0;
13304
46c6471b 13305 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13306 internal breakpoints, these have to be deleted with an
13307 explicit breakpoint number argument. */
43892fdf 13308 for (breakpoint *b : all_breakpoints ())
46c6471b 13309 if (user_breakpoint_p (b))
973d738b
DJ
13310 {
13311 breaks_to_delete = 1;
13312 break;
13313 }
c906108c
SS
13314
13315 /* Ask user only if there are some breakpoints to delete. */
13316 if (!from_tty
e2e0b3e5 13317 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
13318 for (breakpoint *b : all_breakpoints_safe ())
13319 if (user_breakpoint_p (b))
13320 delete_breakpoint (b);
c906108c
SS
13321 }
13322 else
48649e1b 13323 map_breakpoint_numbers
b926417a 13324 (arg, [&] (breakpoint *br)
48649e1b 13325 {
b926417a 13326 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13327 });
c906108c
SS
13328}
13329
c2f4122d
PA
13330/* Return true if all locations of B bound to PSPACE are pending. If
13331 PSPACE is NULL, all locations of all program spaces are
13332 considered. */
13333
0d381245 13334static int
c2f4122d 13335all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13336{
40cb8ca5 13337 for (bp_location *loc : b->locations ())
c2f4122d
PA
13338 if ((pspace == NULL
13339 || loc->pspace == pspace)
13340 && !loc->shlib_disabled
8645ff69 13341 && !loc->pspace->executing_startup)
0d381245
VP
13342 return 0;
13343 return 1;
fe3f5fa8
VP
13344}
13345
776592bf
DE
13346/* Subroutine of update_breakpoint_locations to simplify it.
13347 Return non-zero if multiple fns in list LOC have the same name.
13348 Null names are ignored. */
13349
13350static int
13351ambiguous_names_p (struct bp_location *loc)
13352{
13353 struct bp_location *l;
2698f5ea 13354 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 13355 xcalloc, xfree));
776592bf
DE
13356
13357 for (l = loc; l != NULL; l = l->next)
13358 {
13359 const char **slot;
13360 const char *name = l->function_name;
13361
13362 /* Allow for some names to be NULL, ignore them. */
13363 if (name == NULL)
13364 continue;
13365
c1fb9836 13366 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13367 INSERT);
4a64f543
MS
13368 /* NOTE: We can assume slot != NULL here because xcalloc never
13369 returns NULL. */
776592bf 13370 if (*slot != NULL)
c1fb9836 13371 return 1;
776592bf
DE
13372 *slot = name;
13373 }
13374
776592bf
DE
13375 return 0;
13376}
13377
0fb4aa4b
PA
13378/* When symbols change, it probably means the sources changed as well,
13379 and it might mean the static tracepoint markers are no longer at
13380 the same address or line numbers they used to be at last we
13381 checked. Losing your static tracepoints whenever you rebuild is
13382 undesirable. This function tries to resync/rematch gdb static
13383 tracepoints with the markers on the target, for static tracepoints
13384 that have not been set by marker id. Static tracepoint that have
13385 been set by marker id are reset by marker id in breakpoint_re_set.
13386 The heuristic is:
13387
13388 1) For a tracepoint set at a specific address, look for a marker at
13389 the old PC. If one is found there, assume to be the same marker.
13390 If the name / string id of the marker found is different from the
13391 previous known name, assume that means the user renamed the marker
13392 in the sources, and output a warning.
13393
13394 2) For a tracepoint set at a given line number, look for a marker
13395 at the new address of the old line number. If one is found there,
13396 assume to be the same marker. If the name / string id of the
13397 marker found is different from the previous known name, assume that
13398 means the user renamed the marker in the sources, and output a
13399 warning.
13400
13401 3) If a marker is no longer found at the same address or line, it
13402 may mean the marker no longer exists. But it may also just mean
13403 the code changed a bit. Maybe the user added a few lines of code
13404 that made the marker move up or down (in line number terms). Ask
13405 the target for info about the marker with the string id as we knew
13406 it. If found, update line number and address in the matching
13407 static tracepoint. This will get confused if there's more than one
13408 marker with the same ID (possible in UST, although unadvised
13409 precisely because it confuses tools). */
13410
13411static struct symtab_and_line
13412update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13413{
d9b3f62e 13414 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13415 struct static_tracepoint_marker marker;
13416 CORE_ADDR pc;
0fb4aa4b
PA
13417
13418 pc = sal.pc;
13419 if (sal.line)
13420 find_line_pc (sal.symtab, sal.line, &pc);
13421
13422 if (target_static_tracepoint_marker_at (pc, &marker))
13423 {
5d9310c4 13424 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13425 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13426 b->number, tp->static_trace_marker_id.c_str (),
13427 marker.str_id.c_str ());
0fb4aa4b 13428
5d9310c4 13429 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13430
13431 return sal;
13432 }
13433
13434 /* Old marker wasn't found on target at lineno. Try looking it up
13435 by string ID. */
13436 if (!sal.explicit_pc
13437 && sal.line != 0
13438 && sal.symtab != NULL
5d9310c4 13439 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13440 {
5d9310c4
SM
13441 std::vector<static_tracepoint_marker> markers
13442 = target_static_tracepoint_markers_by_strid
13443 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13444
5d9310c4 13445 if (!markers.empty ())
0fb4aa4b 13446 {
0fb4aa4b 13447 struct symbol *sym;
80e1d417 13448 struct static_tracepoint_marker *tpmarker;
79a45e25 13449 struct ui_out *uiout = current_uiout;
67994074 13450 struct explicit_location explicit_loc;
0fb4aa4b 13451
5d9310c4 13452 tpmarker = &markers[0];
0fb4aa4b 13453
5d9310c4 13454 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13455
13456 warning (_("marker for static tracepoint %d (%s) not "
13457 "found at previous line number"),
5d9310c4 13458 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13459
51abb421 13460 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13461 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13462 uiout->text ("Now in ");
0fb4aa4b
PA
13463 if (sym)
13464 {
987012b8 13465 uiout->field_string ("func", sym->print_name (),
e43b10e1 13466 function_name_style.style ());
112e8700 13467 uiout->text (" at ");
0fb4aa4b 13468 }
112e8700 13469 uiout->field_string ("file",
cbe56571 13470 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13471 file_name_style.style ());
112e8700 13472 uiout->text (":");
0fb4aa4b 13473
112e8700 13474 if (uiout->is_mi_like_p ())
0fb4aa4b 13475 {
0b0865da 13476 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13477
112e8700 13478 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13479 }
13480
381befee 13481 uiout->field_signed ("line", sal2.line);
112e8700 13482 uiout->text ("\n");
0fb4aa4b 13483
80e1d417 13484 b->loc->line_number = sal2.line;
2f202fde 13485 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13486
d28cd78a 13487 b->location.reset (NULL);
67994074
KS
13488 initialize_explicit_location (&explicit_loc);
13489 explicit_loc.source_filename
00e52e53 13490 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13491 explicit_loc.line_offset.offset = b->loc->line_number;
13492 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13493 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13494
13495 /* Might be nice to check if function changed, and warn if
13496 so. */
0fb4aa4b
PA
13497 }
13498 }
13499 return sal;
13500}
13501
8d3788bd
VP
13502/* Returns 1 iff locations A and B are sufficiently same that
13503 we don't need to report breakpoint as changed. */
13504
13505static int
13506locations_are_equal (struct bp_location *a, struct bp_location *b)
13507{
13508 while (a && b)
13509 {
13510 if (a->address != b->address)
13511 return 0;
13512
13513 if (a->shlib_disabled != b->shlib_disabled)
13514 return 0;
13515
13516 if (a->enabled != b->enabled)
13517 return 0;
13518
b5fa468f
TBA
13519 if (a->disabled_by_cond != b->disabled_by_cond)
13520 return 0;
13521
8d3788bd
VP
13522 a = a->next;
13523 b = b->next;
13524 }
13525
13526 if ((a == NULL) != (b == NULL))
13527 return 0;
13528
13529 return 1;
13530}
13531
c2f4122d
PA
13532/* Split all locations of B that are bound to PSPACE out of B's
13533 location list to a separate list and return that list's head. If
13534 PSPACE is NULL, hoist out all locations of B. */
13535
13536static struct bp_location *
13537hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13538{
13539 struct bp_location head;
13540 struct bp_location *i = b->loc;
13541 struct bp_location **i_link = &b->loc;
13542 struct bp_location *hoisted = &head;
13543
13544 if (pspace == NULL)
13545 {
13546 i = b->loc;
13547 b->loc = NULL;
13548 return i;
13549 }
13550
13551 head.next = NULL;
13552
13553 while (i != NULL)
13554 {
13555 if (i->pspace == pspace)
13556 {
13557 *i_link = i->next;
13558 i->next = NULL;
13559 hoisted->next = i;
13560 hoisted = i;
13561 }
13562 else
13563 i_link = &i->next;
13564 i = *i_link;
13565 }
13566
13567 return head.next;
13568}
13569
13570/* Create new breakpoint locations for B (a hardware or software
13571 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13572 zero, then B is a ranged breakpoint. Only recreates locations for
13573 FILTER_PSPACE. Locations of other program spaces are left
13574 untouched. */
f1310107 13575
0e30163f 13576void
0d381245 13577update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13578 struct program_space *filter_pspace,
6c5b2ebe
PA
13579 gdb::array_view<const symtab_and_line> sals,
13580 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13581{
c2f4122d 13582 struct bp_location *existing_locations;
0d381245 13583
6c5b2ebe 13584 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13585 {
13586 /* Ranged breakpoints have only one start location and one end
13587 location. */
13588 b->enable_state = bp_disabled;
f8eba3c6
TT
13589 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13590 "multiple locations found\n"),
13591 b->number);
13592 return;
13593 }
f1310107 13594
4a64f543
MS
13595 /* If there's no new locations, and all existing locations are
13596 pending, don't do anything. This optimizes the common case where
13597 all locations are in the same shared library, that was unloaded.
13598 We'd like to retain the location, so that when the library is
13599 loaded again, we don't loose the enabled/disabled status of the
13600 individual locations. */
6c5b2ebe 13601 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13602 return;
13603
c2f4122d 13604 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13605
6c5b2ebe 13606 for (const auto &sal : sals)
fe3f5fa8 13607 {
f8eba3c6
TT
13608 struct bp_location *new_loc;
13609
6c5b2ebe 13610 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13611
6c5b2ebe 13612 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13613
0d381245
VP
13614 /* Reparse conditions, they might contain references to the
13615 old symtab. */
13616 if (b->cond_string != NULL)
13617 {
bbc13ae3 13618 const char *s;
fe3f5fa8 13619
0d381245 13620 s = b->cond_string;
a70b8144 13621 try
0d381245 13622 {
6c5b2ebe
PA
13623 new_loc->cond = parse_exp_1 (&s, sal.pc,
13624 block_for_pc (sal.pc),
0d381245
VP
13625 0);
13626 }
230d2906 13627 catch (const gdb_exception_error &e)
0d381245 13628 {
b5fa468f 13629 new_loc->disabled_by_cond = true;
0d381245
VP
13630 }
13631 }
fe3f5fa8 13632
6c5b2ebe 13633 if (!sals_end.empty ())
f1310107 13634 {
6c5b2ebe 13635 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13636
6c5b2ebe 13637 new_loc->length = end - sals[0].pc + 1;
f1310107 13638 }
0d381245 13639 }
fe3f5fa8 13640
4a64f543
MS
13641 /* If possible, carry over 'disable' status from existing
13642 breakpoints. */
0d381245
VP
13643 {
13644 struct bp_location *e = existing_locations;
776592bf
DE
13645 /* If there are multiple breakpoints with the same function name,
13646 e.g. for inline functions, comparing function names won't work.
13647 Instead compare pc addresses; this is just a heuristic as things
13648 may have moved, but in practice it gives the correct answer
13649 often enough until a better solution is found. */
13650 int have_ambiguous_names = ambiguous_names_p (b->loc);
13651
0d381245
VP
13652 for (; e; e = e->next)
13653 {
b5fa468f 13654 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 13655 {
776592bf
DE
13656 if (have_ambiguous_names)
13657 {
40cb8ca5 13658 for (bp_location *l : b->locations ())
7f32a4d5
PA
13659 {
13660 /* Ignore software vs hardware location type at
13661 this point, because with "set breakpoint
13662 auto-hw", after a re-set, locations that were
13663 hardware can end up as software, or vice versa.
13664 As mentioned above, this is an heuristic and in
13665 practice should give the correct answer often
13666 enough. */
13667 if (breakpoint_locations_match (e, l, true))
13668 {
b5fa468f
TBA
13669 l->enabled = e->enabled;
13670 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13671 break;
13672 }
13673 }
776592bf
DE
13674 }
13675 else
13676 {
40cb8ca5 13677 for (bp_location *l : b->locations ())
776592bf
DE
13678 if (l->function_name
13679 && strcmp (e->function_name, l->function_name) == 0)
13680 {
b5fa468f
TBA
13681 l->enabled = e->enabled;
13682 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13683 break;
13684 }
13685 }
0d381245
VP
13686 }
13687 }
13688 }
fe3f5fa8 13689
8d3788bd 13690 if (!locations_are_equal (existing_locations, b->loc))
76727919 13691 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13692}
13693
f00aae0f 13694/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13695 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13696
6c5b2ebe 13697static std::vector<symtab_and_line>
f00aae0f 13698location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13699 struct program_space *search_pspace, int *found)
ef23e705 13700{
cc06b668 13701 struct gdb_exception exception;
ef23e705 13702
983af33b 13703 gdb_assert (b->ops != NULL);
ef23e705 13704
6c5b2ebe
PA
13705 std::vector<symtab_and_line> sals;
13706
a70b8144 13707 try
ef23e705 13708 {
6c5b2ebe 13709 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13710 }
94aeb44b 13711 catch (gdb_exception_error &e)
ef23e705
TJB
13712 {
13713 int not_found_and_ok = 0;
492d29ea 13714
ef23e705
TJB
13715 /* For pending breakpoints, it's expected that parsing will
13716 fail until the right shared library is loaded. User has
13717 already told to create pending breakpoints and don't need
13718 extra messages. If breakpoint is in bp_shlib_disabled
13719 state, then user already saw the message about that
13720 breakpoint being disabled, and don't want to see more
13721 errors. */
58438ac1 13722 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13723 && (b->condition_not_parsed
13724 || (b->loc != NULL
13725 && search_pspace != NULL
13726 && b->loc->pspace != search_pspace)
ef23e705 13727 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13728 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13729 || b->enable_state == bp_disabled))
13730 not_found_and_ok = 1;
13731
13732 if (!not_found_and_ok)
13733 {
13734 /* We surely don't want to warn about the same breakpoint
13735 10 times. One solution, implemented here, is disable
13736 the breakpoint on error. Another solution would be to
13737 have separate 'warning emitted' flag. Since this
13738 happens only when a binary has changed, I don't know
13739 which approach is better. */
13740 b->enable_state = bp_disabled;
eedc3f4f 13741 throw;
ef23e705 13742 }
94aeb44b
TT
13743
13744 exception = std::move (e);
ef23e705
TJB
13745 }
13746
492d29ea 13747 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13748 {
6c5b2ebe
PA
13749 for (auto &sal : sals)
13750 resolve_sal_pc (&sal);
f00aae0f 13751 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13752 {
ed1d1739
KS
13753 char *cond_string, *extra_string;
13754 int thread, task;
ef23e705 13755
b5fa468f
TBA
13756 find_condition_and_thread_for_sals (sals, b->extra_string,
13757 &cond_string, &thread,
13758 &task, &extra_string);
f00aae0f 13759 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13760 if (cond_string)
13761 b->cond_string = cond_string;
13762 b->thread = thread;
13763 b->task = task;
e7e0cddf 13764 if (extra_string)
f00aae0f
KS
13765 {
13766 xfree (b->extra_string);
13767 b->extra_string = extra_string;
13768 }
ef23e705
TJB
13769 b->condition_not_parsed = 0;
13770 }
13771
983af33b 13772 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13773 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13774
58438ac1
TT
13775 *found = 1;
13776 }
13777 else
13778 *found = 0;
ef23e705
TJB
13779
13780 return sals;
13781}
13782
348d480f
PA
13783/* The default re_set method, for typical hardware or software
13784 breakpoints. Reevaluate the breakpoint and recreate its
13785 locations. */
13786
13787static void
28010a5d 13788breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13789{
c2f4122d 13790 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13791 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13792
6c5b2ebe
PA
13793 int found;
13794 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13795 filter_pspace, &found);
ef23e705 13796 if (found)
6c5b2ebe 13797 expanded = std::move (sals);
ef23e705 13798
f00aae0f 13799 if (b->location_range_end != NULL)
f1310107 13800 {
6c5b2ebe
PA
13801 std::vector<symtab_and_line> sals_end
13802 = location_to_sals (b, b->location_range_end.get (),
13803 filter_pspace, &found);
f1310107 13804 if (found)
6c5b2ebe 13805 expanded_end = std::move (sals_end);
f1310107
TJB
13806 }
13807
c2f4122d 13808 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13809}
13810
983af33b
SDJ
13811/* Default method for creating SALs from an address string. It basically
13812 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13813
13814static void
626d2320 13815create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13816 struct linespec_result *canonical,
13817 enum bptype type_wanted)
983af33b 13818{
f00aae0f 13819 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13820}
13821
13822/* Call create_breakpoints_sal for the given arguments. This is the default
13823 function for the `create_breakpoints_sal' method of
13824 breakpoint_ops. */
13825
13826static void
13827create_breakpoints_sal_default (struct gdbarch *gdbarch,
13828 struct linespec_result *canonical,
e1e01040
PA
13829 gdb::unique_xmalloc_ptr<char> cond_string,
13830 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13831 enum bptype type_wanted,
13832 enum bpdisp disposition,
13833 int thread,
13834 int task, int ignore_count,
13835 const struct breakpoint_ops *ops,
13836 int from_tty, int enabled,
44f238bb 13837 int internal, unsigned flags)
983af33b 13838{
e1e01040
PA
13839 create_breakpoints_sal (gdbarch, canonical,
13840 std::move (cond_string),
13841 std::move (extra_string),
983af33b
SDJ
13842 type_wanted, disposition,
13843 thread, task, ignore_count, ops, from_tty,
44f238bb 13844 enabled, internal, flags);
983af33b
SDJ
13845}
13846
13847/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13848 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13849
6c5b2ebe 13850static std::vector<symtab_and_line>
f00aae0f 13851decode_location_default (struct breakpoint *b,
626d2320 13852 struct event_location *location,
6c5b2ebe 13853 struct program_space *search_pspace)
983af33b
SDJ
13854{
13855 struct linespec_result canonical;
13856
c2f4122d 13857 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13858 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13859 b->filter.get ());
983af33b
SDJ
13860
13861 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13862 gdb_assert (canonical.lsals.size () < 2);
983af33b 13863
6c5b2ebe 13864 if (!canonical.lsals.empty ())
983af33b 13865 {
6c5b2ebe
PA
13866 const linespec_sals &lsal = canonical.lsals[0];
13867 return std::move (lsal.sals);
983af33b 13868 }
6c5b2ebe 13869 return {};
983af33b
SDJ
13870}
13871
bf469271 13872/* Reset a breakpoint. */
c906108c 13873
bf469271
PA
13874static void
13875breakpoint_re_set_one (breakpoint *b)
c906108c 13876{
fdf44873
TT
13877 input_radix = b->input_radix;
13878 set_language (b->language);
c906108c 13879
348d480f 13880 b->ops->re_set (b);
c906108c
SS
13881}
13882
c2f4122d
PA
13883/* Re-set breakpoint locations for the current program space.
13884 Locations bound to other program spaces are left untouched. */
13885
c906108c 13886void
69de3c6a 13887breakpoint_re_set (void)
c906108c 13888{
c5aa993b 13889 {
fdf44873
TT
13890 scoped_restore_current_language save_language;
13891 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13892 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13893
8e817061
JB
13894 /* breakpoint_re_set_one sets the current_language to the language
13895 of the breakpoint it is resetting (see prepare_re_set_context)
13896 before re-evaluating the breakpoint's location. This change can
13897 unfortunately get undone by accident if the language_mode is set
13898 to auto, and we either switch frames, or more likely in this context,
13899 we select the current frame.
13900
13901 We prevent this by temporarily turning the language_mode to
13902 language_mode_manual. We restore it once all breakpoints
13903 have been reset. */
13904 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13905 language_mode = language_mode_manual;
13906
5ed8105e
PA
13907 /* Note: we must not try to insert locations until after all
13908 breakpoints have been re-set. Otherwise, e.g., when re-setting
13909 breakpoint 1, we'd insert the locations of breakpoint 2, which
13910 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13911
1428b37a 13912 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 13913 {
a70b8144 13914 try
bf469271
PA
13915 {
13916 breakpoint_re_set_one (b);
13917 }
230d2906 13918 catch (const gdb_exception &ex)
bf469271
PA
13919 {
13920 exception_fprintf (gdb_stderr, ex,
13921 "Error in re-setting breakpoint %d: ",
13922 b->number);
13923 }
5ed8105e 13924 }
5ed8105e
PA
13925
13926 jit_breakpoint_re_set ();
13927 }
6c95b8df 13928
af02033e
PP
13929 create_overlay_event_breakpoint ();
13930 create_longjmp_master_breakpoint ();
13931 create_std_terminate_master_breakpoint ();
186c406b 13932 create_exception_master_breakpoint ();
2a7f3dff
PA
13933
13934 /* Now we can insert. */
13935 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13936}
13937\f
c906108c
SS
13938/* Reset the thread number of this breakpoint:
13939
13940 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13941 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13942void
fba45db2 13943breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13944{
13945 if (b->thread != -1)
13946 {
00431a78 13947 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13948
13949 /* We're being called after following a fork. The new fork is
13950 selected as current, and unless this was a vfork will have a
13951 different program space from the original thread. Reset that
13952 as well. */
13953 b->loc->pspace = current_program_space;
c906108c
SS
13954 }
13955}
13956
03ac34d5
MS
13957/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13958 If from_tty is nonzero, it prints a message to that effect,
13959 which ends with a period (no newline). */
13960
c906108c 13961void
fba45db2 13962set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13963{
c906108c
SS
13964 if (count < 0)
13965 count = 0;
13966
43892fdf 13967 for (breakpoint *b : all_breakpoints ())
c906108c 13968 if (b->number == bptnum)
01add95b
SM
13969 {
13970 if (is_tracepoint (b))
13971 {
13972 if (from_tty && count != 0)
13973 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13974 bptnum);
13975 return;
13976 }
13977
13978 b->ignore_count = count;
13979 if (from_tty)
13980 {
13981 if (count == 0)
13982 printf_filtered (_("Will stop next time "
13983 "breakpoint %d is reached."),
13984 bptnum);
13985 else if (count == 1)
13986 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13987 bptnum);
13988 else
13989 printf_filtered (_("Will ignore next %d "
13990 "crossings of breakpoint %d."),
13991 count, bptnum);
13992 }
13993 gdb::observers::breakpoint_modified.notify (b);
13994 return;
13995 }
c906108c 13996
8a3fe4f8 13997 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13998}
13999
c906108c
SS
14000/* Command to set ignore-count of breakpoint N to COUNT. */
14001
14002static void
0b39b52e 14003ignore_command (const char *args, int from_tty)
c906108c 14004{
0b39b52e 14005 const char *p = args;
52f0bd74 14006 int num;
c906108c
SS
14007
14008 if (p == 0)
e2e0b3e5 14009 error_no_arg (_("a breakpoint number"));
c5aa993b 14010
c906108c 14011 num = get_number (&p);
5c44784c 14012 if (num == 0)
8a3fe4f8 14013 error (_("bad breakpoint number: '%s'"), args);
c906108c 14014 if (*p == 0)
8a3fe4f8 14015 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14016
14017 set_ignore_count (num,
14018 longest_to_int (value_as_long (parse_and_eval (p))),
14019 from_tty);
221ea385
KS
14020 if (from_tty)
14021 printf_filtered ("\n");
c906108c
SS
14022}
14023\f
d0fe4701
XR
14024
14025/* Call FUNCTION on each of the breakpoints with numbers in the range
14026 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14027
14028static void
d0fe4701
XR
14029map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14030 gdb::function_view<void (breakpoint *)> function)
c906108c 14031{
d0fe4701
XR
14032 if (bp_num_range.first == 0)
14033 {
14034 warning (_("bad breakpoint number at or near '%d'"),
14035 bp_num_range.first);
14036 }
14037 else
c906108c 14038 {
d0fe4701 14039 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14040 {
d0fe4701
XR
14041 bool match = false;
14042
1428b37a 14043 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 14044 if (b->number == i)
5c44784c 14045 {
bfd28288 14046 match = true;
48649e1b 14047 function (b);
11cf8741 14048 break;
5c44784c 14049 }
bfd28288 14050 if (!match)
d0fe4701 14051 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14052 }
c906108c
SS
14053 }
14054}
14055
d0fe4701
XR
14056/* Call FUNCTION on each of the breakpoints whose numbers are given in
14057 ARGS. */
14058
14059static void
14060map_breakpoint_numbers (const char *args,
14061 gdb::function_view<void (breakpoint *)> function)
14062{
14063 if (args == NULL || *args == '\0')
14064 error_no_arg (_("one or more breakpoint numbers"));
14065
14066 number_or_range_parser parser (args);
14067
14068 while (!parser.finished ())
14069 {
14070 int num = parser.get_number ();
14071 map_breakpoint_number_range (std::make_pair (num, num), function);
14072 }
14073}
14074
14075/* Return the breakpoint location structure corresponding to the
14076 BP_NUM and LOC_NUM values. */
14077
0d381245 14078static struct bp_location *
d0fe4701 14079find_location_by_number (int bp_num, int loc_num)
0d381245 14080{
43892fdf 14081 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
14082
14083 if (!b || b->number != bp_num)
d0fe4701 14084 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14085
0d381245 14086 if (loc_num == 0)
d0fe4701 14087 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14088
d0fe4701 14089 int n = 0;
40cb8ca5 14090 for (bp_location *loc : b->locations ())
d0fe4701
XR
14091 if (++n == loc_num)
14092 return loc;
14093
14094 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14095}
14096
95e95a6d
PA
14097/* Modes of operation for extract_bp_num. */
14098enum class extract_bp_kind
14099{
14100 /* Extracting a breakpoint number. */
14101 bp,
14102
14103 /* Extracting a location number. */
14104 loc,
14105};
14106
14107/* Extract a breakpoint or location number (as determined by KIND)
14108 from the string starting at START. TRAILER is a character which
14109 can be found after the number. If you don't want a trailer, use
14110 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14111 string. This always returns a positive integer. */
14112
14113static int
14114extract_bp_num (extract_bp_kind kind, const char *start,
14115 int trailer, const char **end_out = NULL)
14116{
14117 const char *end = start;
14118 int num = get_number_trailer (&end, trailer);
14119 if (num < 0)
14120 error (kind == extract_bp_kind::bp
14121 ? _("Negative breakpoint number '%.*s'")
14122 : _("Negative breakpoint location number '%.*s'"),
14123 int (end - start), start);
14124 if (num == 0)
14125 error (kind == extract_bp_kind::bp
14126 ? _("Bad breakpoint number '%.*s'")
14127 : _("Bad breakpoint location number '%.*s'"),
14128 int (end - start), start);
14129
14130 if (end_out != NULL)
14131 *end_out = end;
14132 return num;
14133}
14134
14135/* Extract a breakpoint or location range (as determined by KIND) in
14136 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14137 representing the (inclusive) range. The returned pair's elements
14138 are always positive integers. */
14139
14140static std::pair<int, int>
14141extract_bp_or_bp_range (extract_bp_kind kind,
14142 const std::string &arg,
14143 std::string::size_type arg_offset)
14144{
14145 std::pair<int, int> range;
14146 const char *bp_loc = &arg[arg_offset];
14147 std::string::size_type dash = arg.find ('-', arg_offset);
14148 if (dash != std::string::npos)
14149 {
14150 /* bp_loc is a range (x-z). */
14151 if (arg.length () == dash + 1)
14152 error (kind == extract_bp_kind::bp
14153 ? _("Bad breakpoint number at or near: '%s'")
14154 : _("Bad breakpoint location number at or near: '%s'"),
14155 bp_loc);
14156
14157 const char *end;
14158 const char *start_first = bp_loc;
14159 const char *start_second = &arg[dash + 1];
14160 range.first = extract_bp_num (kind, start_first, '-');
14161 range.second = extract_bp_num (kind, start_second, '\0', &end);
14162
14163 if (range.first > range.second)
14164 error (kind == extract_bp_kind::bp
14165 ? _("Inverted breakpoint range at '%.*s'")
14166 : _("Inverted breakpoint location range at '%.*s'"),
14167 int (end - start_first), start_first);
14168 }
14169 else
14170 {
14171 /* bp_loc is a single value. */
14172 range.first = extract_bp_num (kind, bp_loc, '\0');
14173 range.second = range.first;
14174 }
14175 return range;
14176}
14177
d0fe4701
XR
14178/* Extract the breakpoint/location range specified by ARG. Returns
14179 the breakpoint range in BP_NUM_RANGE, and the location range in
14180 BP_LOC_RANGE.
14181
14182 ARG may be in any of the following forms:
14183
14184 x where 'x' is a breakpoint number.
14185 x-y where 'x' and 'y' specify a breakpoint numbers range.
14186 x.y where 'x' is a breakpoint number and 'y' a location number.
14187 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14188 location number range.
14189*/
14190
cc638e86 14191static void
d0fe4701
XR
14192extract_bp_number_and_location (const std::string &arg,
14193 std::pair<int, int> &bp_num_range,
14194 std::pair<int, int> &bp_loc_range)
14195{
14196 std::string::size_type dot = arg.find ('.');
14197
14198 if (dot != std::string::npos)
14199 {
14200 /* Handle 'x.y' and 'x.y-z' cases. */
14201
14202 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14203 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14204
95e95a6d
PA
14205 bp_num_range.first
14206 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14207 bp_num_range.second = bp_num_range.first;
d0fe4701 14208
95e95a6d
PA
14209 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14210 arg, dot + 1);
d0fe4701
XR
14211 }
14212 else
14213 {
14214 /* Handle x and x-y cases. */
d0fe4701 14215
95e95a6d 14216 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14217 bp_loc_range.first = 0;
14218 bp_loc_range.second = 0;
14219 }
d0fe4701
XR
14220}
14221
14222/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14223 specifies whether to enable or disable. */
14224
14225static void
14226enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14227{
14228 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14229 if (loc != NULL)
14230 {
b5fa468f
TBA
14231 if (loc->disabled_by_cond && enable)
14232 error (_("Breakpoint %d's condition is invalid at location %d, "
14233 "cannot enable."), bp_num, loc_num);
14234
d0fe4701
XR
14235 if (loc->enabled != enable)
14236 {
14237 loc->enabled = enable;
14238 mark_breakpoint_location_modified (loc);
14239 }
14240 if (target_supports_enable_disable_tracepoint ()
14241 && current_trace_status ()->running && loc->owner
14242 && is_tracepoint (loc->owner))
14243 target_disable_tracepoint (loc);
14244 }
14245 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14246
14247 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14248}
14249
14250/* Enable or disable a range of breakpoint locations. BP_NUM is the
14251 number of the breakpoint, and BP_LOC_RANGE specifies the
14252 (inclusive) range of location numbers of that breakpoint to
14253 enable/disable. ENABLE specifies whether to enable or disable the
14254 location. */
14255
14256static void
14257enable_disable_breakpoint_location_range (int bp_num,
14258 std::pair<int, int> &bp_loc_range,
14259 bool enable)
14260{
14261 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14262 enable_disable_bp_num_loc (bp_num, i, enable);
14263}
0d381245 14264
1900040c
MS
14265/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14266 If from_tty is nonzero, it prints a message to that effect,
14267 which ends with a period (no newline). */
14268
c906108c 14269void
fba45db2 14270disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14271{
14272 /* Never disable a watchpoint scope breakpoint; we want to
14273 hit them when we leave scope so we can delete both the
14274 watchpoint and its scope breakpoint at that time. */
14275 if (bpt->type == bp_watchpoint_scope)
14276 return;
14277
b5de0fa7 14278 bpt->enable_state = bp_disabled;
c906108c 14279
b775012e
LM
14280 /* Mark breakpoint locations modified. */
14281 mark_breakpoint_modified (bpt);
14282
d248b706
KY
14283 if (target_supports_enable_disable_tracepoint ()
14284 && current_trace_status ()->running && is_tracepoint (bpt))
14285 {
40cb8ca5 14286 for (bp_location *location : bpt->locations ())
d248b706
KY
14287 target_disable_tracepoint (location);
14288 }
14289
44702360 14290 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14291
76727919 14292 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14293}
14294
d0fe4701
XR
14295/* Enable or disable the breakpoint(s) or breakpoint location(s)
14296 specified in ARGS. ARGS may be in any of the formats handled by
14297 extract_bp_number_and_location. ENABLE specifies whether to enable
14298 or disable the breakpoints/locations. */
14299
c906108c 14300static void
d0fe4701 14301enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14302{
c906108c 14303 if (args == 0)
46c6471b 14304 {
43892fdf 14305 for (breakpoint *bpt : all_breakpoints ())
46c6471b 14306 if (user_breakpoint_p (bpt))
d0fe4701
XR
14307 {
14308 if (enable)
14309 enable_breakpoint (bpt);
14310 else
14311 disable_breakpoint (bpt);
14312 }
46c6471b 14313 }
9eaabc75 14314 else
0d381245 14315 {
cb791d59 14316 std::string num = extract_arg (&args);
9eaabc75 14317
cb791d59 14318 while (!num.empty ())
d248b706 14319 {
d0fe4701 14320 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14321
cc638e86
PA
14322 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14323
14324 if (bp_loc_range.first == bp_loc_range.second
14325 && bp_loc_range.first == 0)
d0fe4701 14326 {
cc638e86
PA
14327 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14328 map_breakpoint_number_range (bp_num_range,
14329 enable
14330 ? enable_breakpoint
14331 : disable_breakpoint);
14332 }
14333 else
14334 {
14335 /* Handle breakpoint ids with formats 'x.y' or
14336 'x.y-z'. */
14337 enable_disable_breakpoint_location_range
14338 (bp_num_range.first, bp_loc_range, enable);
b775012e 14339 }
9eaabc75 14340 num = extract_arg (&args);
d248b706 14341 }
0d381245 14342 }
c906108c
SS
14343}
14344
d0fe4701
XR
14345/* The disable command disables the specified breakpoints/locations
14346 (or all defined breakpoints) so they're no longer effective in
14347 stopping the inferior. ARGS may be in any of the forms defined in
14348 extract_bp_number_and_location. */
14349
14350static void
14351disable_command (const char *args, int from_tty)
14352{
14353 enable_disable_command (args, from_tty, false);
14354}
14355
c906108c 14356static void
816338b5
SS
14357enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14358 int count)
c906108c 14359{
afe38095 14360 int target_resources_ok;
c906108c
SS
14361
14362 if (bpt->type == bp_hardware_breakpoint)
14363 {
14364 int i;
c5aa993b 14365 i = hw_breakpoint_used_count ();
53a5351d 14366 target_resources_ok =
d92524f1 14367 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14368 i + 1, 0);
c906108c 14369 if (target_resources_ok == 0)
8a3fe4f8 14370 error (_("No hardware breakpoint support in the target."));
c906108c 14371 else if (target_resources_ok < 0)
8a3fe4f8 14372 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14373 }
14374
cc60f2e3 14375 if (is_watchpoint (bpt))
c906108c 14376 {
d07205c2 14377 /* Initialize it just to avoid a GCC false warning. */
f486487f 14378 enum enable_state orig_enable_state = bp_disabled;
dde02812 14379
a70b8144 14380 try
c906108c 14381 {
3a5c3e22
PA
14382 struct watchpoint *w = (struct watchpoint *) bpt;
14383
1e718ff1
TJB
14384 orig_enable_state = bpt->enable_state;
14385 bpt->enable_state = bp_enabled;
3a5c3e22 14386 update_watchpoint (w, 1 /* reparse */);
c906108c 14387 }
230d2906 14388 catch (const gdb_exception &e)
c5aa993b 14389 {
1e718ff1 14390 bpt->enable_state = orig_enable_state;
dde02812
ES
14391 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14392 bpt->number);
14393 return;
c5aa993b 14394 }
c906108c 14395 }
0101ce28 14396
b775012e
LM
14397 bpt->enable_state = bp_enabled;
14398
14399 /* Mark breakpoint locations modified. */
14400 mark_breakpoint_modified (bpt);
14401
d248b706
KY
14402 if (target_supports_enable_disable_tracepoint ()
14403 && current_trace_status ()->running && is_tracepoint (bpt))
14404 {
40cb8ca5 14405 for (bp_location *location : bpt->locations ())
d248b706
KY
14406 target_enable_tracepoint (location);
14407 }
14408
b4c291bb 14409 bpt->disposition = disposition;
816338b5 14410 bpt->enable_count = count;
44702360 14411 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14412
76727919 14413 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14414}
14415
fe3f5fa8 14416
c906108c 14417void
fba45db2 14418enable_breakpoint (struct breakpoint *bpt)
c906108c 14419{
816338b5 14420 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14421}
14422
d0fe4701
XR
14423/* The enable command enables the specified breakpoints/locations (or
14424 all defined breakpoints) so they once again become (or continue to
14425 be) effective in stopping the inferior. ARGS may be in any of the
14426 forms defined in extract_bp_number_and_location. */
c906108c 14427
c906108c 14428static void
981a3fb3 14429enable_command (const char *args, int from_tty)
c906108c 14430{
d0fe4701 14431 enable_disable_command (args, from_tty, true);
c906108c
SS
14432}
14433
c906108c 14434static void
4495129a 14435enable_once_command (const char *args, int from_tty)
c906108c 14436{
48649e1b
TT
14437 map_breakpoint_numbers
14438 (args, [&] (breakpoint *b)
14439 {
14440 iterate_over_related_breakpoints
14441 (b, [&] (breakpoint *bpt)
14442 {
14443 enable_breakpoint_disp (bpt, disp_disable, 1);
14444 });
14445 });
816338b5
SS
14446}
14447
14448static void
4495129a 14449enable_count_command (const char *args, int from_tty)
816338b5 14450{
b9d61307
SM
14451 int count;
14452
14453 if (args == NULL)
14454 error_no_arg (_("hit count"));
14455
14456 count = get_number (&args);
816338b5 14457
48649e1b
TT
14458 map_breakpoint_numbers
14459 (args, [&] (breakpoint *b)
14460 {
14461 iterate_over_related_breakpoints
14462 (b, [&] (breakpoint *bpt)
14463 {
14464 enable_breakpoint_disp (bpt, disp_disable, count);
14465 });
14466 });
c906108c
SS
14467}
14468
c906108c 14469static void
4495129a 14470enable_delete_command (const char *args, int from_tty)
c906108c 14471{
48649e1b
TT
14472 map_breakpoint_numbers
14473 (args, [&] (breakpoint *b)
14474 {
14475 iterate_over_related_breakpoints
14476 (b, [&] (breakpoint *bpt)
14477 {
14478 enable_breakpoint_disp (bpt, disp_del, 1);
14479 });
14480 });
c906108c
SS
14481}
14482\f
1f3b5d1b
PP
14483/* Invalidate last known value of any hardware watchpoint if
14484 the memory which that value represents has been written to by
14485 GDB itself. */
14486
14487static void
8de0566d
YQ
14488invalidate_bp_value_on_memory_change (struct inferior *inferior,
14489 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14490 const bfd_byte *data)
14491{
43892fdf 14492 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 14493 if (bp->enable_state == bp_enabled
3a5c3e22 14494 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14495 {
3a5c3e22 14496 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14497
850645cf 14498 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 14499 {
40cb8ca5 14500 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
14501 if (loc->loc_type == bp_loc_hardware_watchpoint
14502 && loc->address + loc->length > addr
14503 && addr + len > loc->address)
14504 {
3a5c3e22 14505 wp->val = NULL;
4c1d86d9 14506 wp->val_valid = false;
3a5c3e22
PA
14507 }
14508 }
1f3b5d1b
PP
14509 }
14510}
14511
8181d85f
DJ
14512/* Create and insert a breakpoint for software single step. */
14513
14514void
6c95b8df 14515insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14516 const address_space *aspace,
4a64f543 14517 CORE_ADDR next_pc)
8181d85f 14518{
7c16b83e
PA
14519 struct thread_info *tp = inferior_thread ();
14520 struct symtab_and_line sal;
14521 CORE_ADDR pc = next_pc;
8181d85f 14522
34b7e8a6
PA
14523 if (tp->control.single_step_breakpoints == NULL)
14524 {
14525 tp->control.single_step_breakpoints
5d5658a1 14526 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14527 }
8181d85f 14528
7c16b83e
PA
14529 sal = find_pc_line (pc, 0);
14530 sal.pc = pc;
14531 sal.section = find_pc_overlay (pc);
14532 sal.explicit_pc = 1;
34b7e8a6 14533 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14534
7c16b83e 14535 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14536}
14537
93f9a11f
YQ
14538/* Insert single step breakpoints according to the current state. */
14539
14540int
14541insert_single_step_breakpoints (struct gdbarch *gdbarch)
14542{
f5ea389a 14543 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14544 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14545
f5ea389a 14546 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14547
a0ff9e1a 14548 if (!next_pcs.empty ())
93f9a11f 14549 {
f5ea389a 14550 struct frame_info *frame = get_current_frame ();
8b86c959 14551 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14552
a0ff9e1a 14553 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14554 insert_single_step_breakpoint (gdbarch, aspace, pc);
14555
93f9a11f
YQ
14556 return 1;
14557 }
14558 else
14559 return 0;
14560}
14561
34b7e8a6 14562/* See breakpoint.h. */
f02253f1
HZ
14563
14564int
7c16b83e 14565breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14566 const address_space *aspace,
7c16b83e 14567 CORE_ADDR pc)
1aafd4da 14568{
40cb8ca5 14569 for (bp_location *loc : bp->locations ())
7c16b83e
PA
14570 if (loc->inserted
14571 && breakpoint_location_address_match (loc, aspace, pc))
14572 return 1;
1aafd4da 14573
7c16b83e 14574 return 0;
ef370185
JB
14575}
14576
14577/* Check whether a software single-step breakpoint is inserted at
14578 PC. */
14579
14580int
accd0bcd 14581single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14582 CORE_ADDR pc)
14583{
43892fdf 14584 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
14585 {
14586 if (bpt->type == bp_single_step
14587 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14588 return 1;
14589 }
14590 return 0;
1aafd4da
UW
14591}
14592
1042e4c0
SS
14593/* Tracepoint-specific operations. */
14594
14595/* Set tracepoint count to NUM. */
14596static void
14597set_tracepoint_count (int num)
14598{
14599 tracepoint_count = num;
4fa62494 14600 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14601}
14602
70221824 14603static void
0b39b52e 14604trace_command (const char *arg, int from_tty)
1042e4c0 14605{
ffc2605c
TT
14606 event_location_up location = string_to_event_location (&arg,
14607 current_language);
bac7c5cf
GB
14608 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14609 (location.get (), true /* is_tracepoint */);
55aa24fb 14610
558a9d82 14611 create_breakpoint (get_current_arch (),
ffc2605c 14612 location.get (),
10a636cc 14613 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14614 0 /* tempflag */,
14615 bp_tracepoint /* type_wanted */,
14616 0 /* Ignore count */,
14617 pending_break_support,
14618 ops,
14619 from_tty,
14620 1 /* enabled */,
14621 0 /* internal */, 0);
1042e4c0
SS
14622}
14623
70221824 14624static void
0b39b52e 14625ftrace_command (const char *arg, int from_tty)
7a697b8d 14626{
ffc2605c
TT
14627 event_location_up location = string_to_event_location (&arg,
14628 current_language);
558a9d82 14629 create_breakpoint (get_current_arch (),
ffc2605c 14630 location.get (),
10a636cc 14631 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14632 0 /* tempflag */,
14633 bp_fast_tracepoint /* type_wanted */,
14634 0 /* Ignore count */,
14635 pending_break_support,
14636 &tracepoint_breakpoint_ops,
14637 from_tty,
14638 1 /* enabled */,
14639 0 /* internal */, 0);
0fb4aa4b
PA
14640}
14641
14642/* strace command implementation. Creates a static tracepoint. */
14643
70221824 14644static void
0b39b52e 14645strace_command (const char *arg, int from_tty)
0fb4aa4b 14646{
983af33b 14647 struct breakpoint_ops *ops;
ffc2605c 14648 event_location_up location;
983af33b
SDJ
14649
14650 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14651 or with a normal static tracepoint. */
61012eef 14652 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14653 {
14654 ops = &strace_marker_breakpoint_ops;
a20714ff 14655 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14656 }
983af33b 14657 else
f00aae0f
KS
14658 {
14659 ops = &tracepoint_breakpoint_ops;
14660 location = string_to_event_location (&arg, current_language);
14661 }
983af33b 14662
558a9d82 14663 create_breakpoint (get_current_arch (),
ffc2605c 14664 location.get (),
10a636cc 14665 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14666 0 /* tempflag */,
14667 bp_static_tracepoint /* type_wanted */,
14668 0 /* Ignore count */,
14669 pending_break_support,
14670 ops,
14671 from_tty,
14672 1 /* enabled */,
14673 0 /* internal */, 0);
7a697b8d
SS
14674}
14675
409873ef
SS
14676/* Set up a fake reader function that gets command lines from a linked
14677 list that was acquired during tracepoint uploading. */
14678
14679static struct uploaded_tp *this_utp;
3149d8c1 14680static int next_cmd;
409873ef
SS
14681
14682static char *
14683read_uploaded_action (void)
14684{
a18ba4e4 14685 char *rslt = nullptr;
409873ef 14686
a18ba4e4
SM
14687 if (next_cmd < this_utp->cmd_strings.size ())
14688 {
67aa1f3c 14689 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14690 next_cmd++;
14691 }
409873ef
SS
14692
14693 return rslt;
14694}
14695
00bf0b85
SS
14696/* Given information about a tracepoint as recorded on a target (which
14697 can be either a live system or a trace file), attempt to create an
14698 equivalent GDB tracepoint. This is not a reliable process, since
14699 the target does not necessarily have all the information used when
14700 the tracepoint was originally defined. */
14701
d9b3f62e 14702struct tracepoint *
00bf0b85 14703create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14704{
f2fc3015
TT
14705 const char *addr_str;
14706 char small_buf[100];
d9b3f62e 14707 struct tracepoint *tp;
fd9b8c24 14708
409873ef 14709 if (utp->at_string)
67aa1f3c 14710 addr_str = utp->at_string.get ();
409873ef
SS
14711 else
14712 {
14713 /* In the absence of a source location, fall back to raw
14714 address. Since there is no way to confirm that the address
14715 means the same thing as when the trace was started, warn the
14716 user. */
3e43a32a
MS
14717 warning (_("Uploaded tracepoint %d has no "
14718 "source location, using raw address"),
409873ef 14719 utp->number);
8c042590 14720 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14721 addr_str = small_buf;
14722 }
14723
14724 /* There's not much we can do with a sequence of bytecodes. */
14725 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14726 warning (_("Uploaded tracepoint %d condition "
14727 "has no source form, ignoring it"),
409873ef 14728 utp->number);
d5551862 14729
ffc2605c
TT
14730 event_location_up location = string_to_event_location (&addr_str,
14731 current_language);
8cdf0e15 14732 if (!create_breakpoint (get_current_arch (),
ffc2605c 14733 location.get (),
67aa1f3c 14734 utp->cond_string.get (), -1, addr_str,
10a636cc 14735 false /* force_condition */,
e7e0cddf 14736 0 /* parse cond/thread */,
8cdf0e15 14737 0 /* tempflag */,
0fb4aa4b 14738 utp->type /* type_wanted */,
8cdf0e15
VP
14739 0 /* Ignore count */,
14740 pending_break_support,
348d480f 14741 &tracepoint_breakpoint_ops,
8cdf0e15 14742 0 /* from_tty */,
84f4c1fe 14743 utp->enabled /* enabled */,
44f238bb
PA
14744 0 /* internal */,
14745 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14746 return NULL;
fd9b8c24 14747
409873ef 14748 /* Get the tracepoint we just created. */
fd9b8c24
PA
14749 tp = get_tracepoint (tracepoint_count);
14750 gdb_assert (tp != NULL);
d5551862 14751
00bf0b85
SS
14752 if (utp->pass > 0)
14753 {
8c042590 14754 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14755 tp->number);
00bf0b85 14756
409873ef 14757 trace_pass_command (small_buf, 0);
00bf0b85
SS
14758 }
14759
409873ef
SS
14760 /* If we have uploaded versions of the original commands, set up a
14761 special-purpose "reader" function and call the usual command line
14762 reader, then pass the result to the breakpoint command-setting
14763 function. */
a18ba4e4 14764 if (!utp->cmd_strings.empty ())
00bf0b85 14765 {
12973681 14766 counted_command_line cmd_list;
00bf0b85 14767
409873ef 14768 this_utp = utp;
3149d8c1 14769 next_cmd = 0;
d5551862 14770
60b3cef2 14771 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14772
c1fc2657 14773 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14774 }
a18ba4e4
SM
14775 else if (!utp->actions.empty ()
14776 || !utp->step_actions.empty ())
3e43a32a
MS
14777 warning (_("Uploaded tracepoint %d actions "
14778 "have no source form, ignoring them"),
409873ef 14779 utp->number);
00bf0b85 14780
f196051f 14781 /* Copy any status information that might be available. */
c1fc2657 14782 tp->hit_count = utp->hit_count;
f196051f
SS
14783 tp->traceframe_usage = utp->traceframe_usage;
14784
00bf0b85 14785 return tp;
d9b3f62e 14786}
00bf0b85 14787
1042e4c0
SS
14788/* Print information on tracepoint number TPNUM_EXP, or all if
14789 omitted. */
14790
14791static void
1d12d88f 14792info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14793{
79a45e25 14794 struct ui_out *uiout = current_uiout;
e5a67952 14795 int num_printed;
1042e4c0 14796
5c458ae8 14797 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14798
14799 if (num_printed == 0)
1042e4c0 14800 {
e5a67952 14801 if (args == NULL || *args == '\0')
112e8700 14802 uiout->message ("No tracepoints.\n");
d77f58be 14803 else
112e8700 14804 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14805 }
ad443146
SS
14806
14807 default_collect_info ();
1042e4c0
SS
14808}
14809
4a64f543 14810/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14811 Not supported by all targets. */
14812static void
5fed81ff 14813enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14814{
14815 enable_command (args, from_tty);
14816}
14817
4a64f543 14818/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14819 Not supported by all targets. */
14820static void
5fed81ff 14821disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14822{
14823 disable_command (args, from_tty);
14824}
14825
4a64f543 14826/* Remove a tracepoint (or all if no argument). */
1042e4c0 14827static void
4495129a 14828delete_trace_command (const char *arg, int from_tty)
1042e4c0 14829{
1042e4c0
SS
14830 dont_repeat ();
14831
14832 if (arg == 0)
14833 {
14834 int breaks_to_delete = 0;
14835
14836 /* Delete all breakpoints if no argument.
dda83cd7
SM
14837 Do not delete internal or call-dummy breakpoints, these
14838 have to be deleted with an explicit breakpoint number
4a64f543 14839 argument. */
f6d17b2b 14840 for (breakpoint *tp : all_tracepoints ())
1428b37a 14841 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
14842 {
14843 breaks_to_delete = 1;
14844 break;
14845 }
1042e4c0
SS
14846
14847 /* Ask user only if there are some breakpoints to delete. */
14848 if (!from_tty
14849 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14850 {
1428b37a 14851 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14852 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14853 delete_breakpoint (b);
1042e4c0
SS
14854 }
14855 }
14856 else
48649e1b 14857 map_breakpoint_numbers
b926417a 14858 (arg, [&] (breakpoint *br)
48649e1b 14859 {
b926417a 14860 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14861 });
1042e4c0
SS
14862}
14863
197f0a60
TT
14864/* Helper function for trace_pass_command. */
14865
14866static void
d9b3f62e 14867trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14868{
d9b3f62e 14869 tp->pass_count = count;
76727919 14870 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14871 if (from_tty)
14872 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14873 tp->number, count);
197f0a60
TT
14874}
14875
1042e4c0
SS
14876/* Set passcount for tracepoint.
14877
14878 First command argument is passcount, second is tracepoint number.
14879 If tracepoint number omitted, apply to most recently defined.
14880 Also accepts special argument "all". */
14881
14882static void
0b39b52e 14883trace_pass_command (const char *args, int from_tty)
1042e4c0 14884{
d9b3f62e 14885 struct tracepoint *t1;
0b39b52e 14886 ULONGEST count;
1042e4c0
SS
14887
14888 if (args == 0 || *args == 0)
3e43a32a
MS
14889 error (_("passcount command requires an "
14890 "argument (count + optional TP num)"));
1042e4c0 14891
0b39b52e 14892 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14893
529480d0 14894 args = skip_spaces (args);
1042e4c0
SS
14895 if (*args && strncasecmp (args, "all", 3) == 0)
14896 {
14897 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14898 if (*args)
14899 error (_("Junk at end of arguments."));
1042e4c0 14900
f6d17b2b 14901 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14902 {
14903 t1 = (struct tracepoint *) b;
14904 trace_pass_set_count (t1, count, from_tty);
14905 }
197f0a60
TT
14906 }
14907 else if (*args == '\0')
1042e4c0 14908 {
5fa1d40e 14909 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14910 if (t1)
197f0a60
TT
14911 trace_pass_set_count (t1, count, from_tty);
14912 }
14913 else
14914 {
bfd28288
PA
14915 number_or_range_parser parser (args);
14916 while (!parser.finished ())
1042e4c0 14917 {
bfd28288 14918 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14919 if (t1)
14920 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14921 }
14922 }
1042e4c0
SS
14923}
14924
d9b3f62e 14925struct tracepoint *
1042e4c0
SS
14926get_tracepoint (int num)
14927{
f6d17b2b 14928 for (breakpoint *t : all_tracepoints ())
1042e4c0 14929 if (t->number == num)
d9b3f62e 14930 return (struct tracepoint *) t;
1042e4c0
SS
14931
14932 return NULL;
14933}
14934
d5551862
SS
14935/* Find the tracepoint with the given target-side number (which may be
14936 different from the tracepoint number after disconnecting and
14937 reconnecting). */
14938
d9b3f62e 14939struct tracepoint *
d5551862
SS
14940get_tracepoint_by_number_on_target (int num)
14941{
f6d17b2b 14942 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14943 {
14944 struct tracepoint *t = (struct tracepoint *) b;
14945
14946 if (t->number_on_target == num)
14947 return t;
14948 }
d5551862
SS
14949
14950 return NULL;
14951}
14952
1042e4c0 14953/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14954 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14955 If the argument is missing, the most recent tracepoint
14956 (tracepoint_count) is returned. */
14957
d9b3f62e 14958struct tracepoint *
0b39b52e 14959get_tracepoint_by_number (const char **arg,
bfd28288 14960 number_or_range_parser *parser)
1042e4c0 14961{
1042e4c0 14962 int tpnum;
0b39b52e 14963 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14964
bfd28288 14965 if (parser != NULL)
197f0a60 14966 {
bfd28288
PA
14967 gdb_assert (!parser->finished ());
14968 tpnum = parser->get_number ();
197f0a60
TT
14969 }
14970 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14971 tpnum = tracepoint_count;
1042e4c0 14972 else
197f0a60 14973 tpnum = get_number (arg);
1042e4c0
SS
14974
14975 if (tpnum <= 0)
14976 {
14977 if (instring && *instring)
14978 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14979 instring);
14980 else
5fa1d40e 14981 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14982 return NULL;
14983 }
14984
f6d17b2b 14985 for (breakpoint *t : all_tracepoints ())
1042e4c0 14986 if (t->number == tpnum)
d9b3f62e 14987 return (struct tracepoint *) t;
1042e4c0 14988
1042e4c0
SS
14989 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14990 return NULL;
14991}
14992
d9b3f62e
PA
14993void
14994print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14995{
14996 if (b->thread != -1)
14997 fprintf_unfiltered (fp, " thread %d", b->thread);
14998
14999 if (b->task != 0)
15000 fprintf_unfiltered (fp, " task %d", b->task);
15001
15002 fprintf_unfiltered (fp, "\n");
15003}
15004
6149aea9
PA
15005/* Save information on user settable breakpoints (watchpoints, etc) to
15006 a new script file named FILENAME. If FILTER is non-NULL, call it
15007 on each breakpoint and only include the ones for which it returns
f2478a7e 15008 true. */
6149aea9 15009
1042e4c0 15010static void
4495129a 15011save_breakpoints (const char *filename, int from_tty,
f2478a7e 15012 bool (*filter) (const struct breakpoint *))
1042e4c0 15013{
6149aea9 15014 int any = 0;
6149aea9 15015 int extra_trace_bits = 0;
1042e4c0 15016
6149aea9
PA
15017 if (filename == 0 || *filename == 0)
15018 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15019
15020 /* See if we have anything to save. */
43892fdf 15021 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15022 {
15023 /* Skip internal and momentary breakpoints. */
15024 if (!user_breakpoint_p (tp))
15025 continue;
6149aea9 15026
01add95b
SM
15027 /* If we have a filter, only save the breakpoints it accepts. */
15028 if (filter && !filter (tp))
15029 continue;
6149aea9 15030
01add95b 15031 any = 1;
6149aea9 15032
01add95b
SM
15033 if (is_tracepoint (tp))
15034 {
15035 extra_trace_bits = 1;
6149aea9 15036
01add95b
SM
15037 /* We can stop searching. */
15038 break;
15039 }
15040 }
6149aea9
PA
15041
15042 if (!any)
1042e4c0 15043 {
6149aea9 15044 warning (_("Nothing to save."));
1042e4c0
SS
15045 return;
15046 }
15047
ee0c3293 15048 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15049
15050 stdio_file fp;
15051
ee0c3293 15052 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15053 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15054 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15055
6149aea9 15056 if (extra_trace_bits)
d7e74731 15057 save_trace_state_variables (&fp);
8bf6485c 15058
43892fdf 15059 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15060 {
15061 /* Skip internal and momentary breakpoints. */
15062 if (!user_breakpoint_p (tp))
15063 continue;
8bf6485c 15064
01add95b
SM
15065 /* If we have a filter, only save the breakpoints it accepts. */
15066 if (filter && !filter (tp))
15067 continue;
6149aea9 15068
01add95b 15069 tp->ops->print_recreate (tp, &fp);
1042e4c0 15070
01add95b
SM
15071 /* Note, we can't rely on tp->number for anything, as we can't
15072 assume the recreated breakpoint numbers will match. Use $bpnum
15073 instead. */
6149aea9 15074
01add95b
SM
15075 if (tp->cond_string)
15076 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9 15077
01add95b
SM
15078 if (tp->ignore_count)
15079 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15080
01add95b
SM
15081 if (tp->type != bp_dprintf && tp->commands)
15082 {
15083 fp.puts (" commands\n");
1042e4c0 15084
01add95b
SM
15085 current_uiout->redirect (&fp);
15086 try
15087 {
15088 print_command_lines (current_uiout, tp->commands.get (), 2);
15089 }
15090 catch (const gdb_exception &ex)
15091 {
15092 current_uiout->redirect (NULL);
15093 throw;
15094 }
6149aea9 15095
01add95b
SM
15096 current_uiout->redirect (NULL);
15097 fp.puts (" end\n");
15098 }
6149aea9 15099
01add95b
SM
15100 if (tp->enable_state == bp_disabled)
15101 fp.puts ("disable $bpnum\n");
6149aea9 15102
01add95b
SM
15103 /* If this is a multi-location breakpoint, check if the locations
15104 should be individually disabled. Watchpoint locations are
15105 special, and not user visible. */
15106 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15107 {
15108 int n = 1;
40cb8ca5 15109
01add95b
SM
15110 for (bp_location *loc : tp->locations ())
15111 {
15112 if (!loc->enabled)
15113 fp.printf ("disable $bpnum.%d\n", n);
15114
15115 n++;
15116 }
15117 }
15118 }
8bf6485c 15119
6149aea9 15120 if (extra_trace_bits && *default_collect)
d7e74731 15121 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15122
1042e4c0 15123 if (from_tty)
ee0c3293 15124 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15125}
15126
15127/* The `save breakpoints' command. */
15128
15129static void
4495129a 15130save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15131{
15132 save_breakpoints (args, from_tty, NULL);
15133}
15134
15135/* The `save tracepoints' command. */
15136
15137static void
4495129a 15138save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15139{
15140 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15141}
15142
c906108c 15143\f
629500fa
KS
15144/* This help string is used to consolidate all the help string for specifying
15145 locations used by several commands. */
15146
15147#define LOCATION_HELP_STRING \
15148"Linespecs are colon-separated lists of location parameters, such as\n\
15149source filename, function name, label name, and line number.\n\
15150Example: To specify the start of a label named \"the_top\" in the\n\
15151function \"fact\" in the file \"factorial.c\", use\n\
15152\"factorial.c:fact:the_top\".\n\
15153\n\
15154Address locations begin with \"*\" and specify an exact address in the\n\
15155program. Example: To specify the fourth byte past the start function\n\
15156\"main\", use \"*main + 4\".\n\
15157\n\
15158Explicit locations are similar to linespecs but use an option/argument\n\
15159syntax to specify location parameters.\n\
15160Example: To specify the start of the label named \"the_top\" in the\n\
15161function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15162-function fact -label the_top\".\n\
15163\n\
15164By default, a specified function is matched against the program's\n\
15165functions in all scopes. For C++, this means in all namespaces and\n\
15166classes. For Ada, this means in all packages. E.g., in C++,\n\
15167\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15168\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15169specified name as a complete fully-qualified name instead."
629500fa 15170
4a64f543
MS
15171/* This help string is used for the break, hbreak, tbreak and thbreak
15172 commands. It is defined as a macro to prevent duplication.
15173 COMMAND should be a string constant containing the name of the
15174 command. */
629500fa 15175
31e2b00f 15176#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15177command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15178\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15179PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15180probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15181guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15182`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15183LOCATION may be a linespec, address, or explicit location as described\n\
15184below.\n\
15185\n\
dc10affe
PA
15186With no LOCATION, uses current execution address of the selected\n\
15187stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15188\n\
15189THREADNUM is the number from \"info threads\".\n\
15190CONDITION is a boolean expression.\n\
733d554a
TBA
15191\n\
15192With the \"-force-condition\" flag, the condition is defined even when\n\
15193it is invalid for all current locations.\n\
89549d7f 15194\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15195Multiple breakpoints at one place are permitted, and useful if their\n\
15196conditions are different.\n\
31e2b00f
AS
15197\n\
15198Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15199
44feb3ce
TT
15200/* List of subcommands for "catch". */
15201static struct cmd_list_element *catch_cmdlist;
15202
15203/* List of subcommands for "tcatch". */
15204static struct cmd_list_element *tcatch_cmdlist;
15205
9ac4176b 15206void
a121b7c1 15207add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15208 cmd_const_sfunc_ftype *sfunc,
625e8578 15209 completer_ftype *completer,
44feb3ce
TT
15210 void *user_data_catch,
15211 void *user_data_tcatch)
15212{
15213 struct cmd_list_element *command;
15214
0450cc4c 15215 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15216 &catch_cmdlist);
15217 set_cmd_sfunc (command, sfunc);
0f8e2034 15218 command->set_context (user_data_catch);
a96d9b2e 15219 set_cmd_completer (command, completer);
44feb3ce 15220
0450cc4c 15221 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15222 &tcatch_cmdlist);
15223 set_cmd_sfunc (command, sfunc);
0f8e2034 15224 command->set_context (user_data_tcatch);
a96d9b2e 15225 set_cmd_completer (command, completer);
44feb3ce
TT
15226}
15227
0574c78f
GB
15228/* Zero if any of the breakpoint's locations could be a location where
15229 functions have been inlined, nonzero otherwise. */
15230
15231static int
15232is_non_inline_function (struct breakpoint *b)
15233{
15234 /* The shared library event breakpoint is set on the address of a
15235 non-inline function. */
15236 if (b->type == bp_shlib_event)
15237 return 1;
15238
15239 return 0;
15240}
15241
15242/* Nonzero if the specified PC cannot be a location where functions
15243 have been inlined. */
15244
15245int
accd0bcd 15246pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15247 const struct target_waitstatus *ws)
0574c78f 15248{
43892fdf 15249 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
15250 {
15251 if (!is_non_inline_function (b))
15252 continue;
15253
40cb8ca5 15254 for (bp_location *bl : b->locations ())
0574c78f
GB
15255 {
15256 if (!bl->shlib_disabled
09ac7c10 15257 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15258 return 1;
15259 }
15260 }
15261
15262 return 0;
15263}
15264
2f202fde
JK
15265/* Remove any references to OBJFILE which is going to be freed. */
15266
15267void
15268breakpoint_free_objfile (struct objfile *objfile)
15269{
48d7020b 15270 for (bp_location *loc : all_bp_locations ())
eb822aa6 15271 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15272 loc->symtab = NULL;
15273}
15274
2060206e
PA
15275void
15276initialize_breakpoint_ops (void)
15277{
15278 static int initialized = 0;
15279
15280 struct breakpoint_ops *ops;
15281
15282 if (initialized)
15283 return;
15284 initialized = 1;
15285
15286 /* The breakpoint_ops structure to be inherit by all kinds of
15287 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15288 internal and momentary breakpoints, etc.). */
15289 ops = &bkpt_base_breakpoint_ops;
15290 *ops = base_breakpoint_ops;
15291 ops->re_set = bkpt_re_set;
15292 ops->insert_location = bkpt_insert_location;
15293 ops->remove_location = bkpt_remove_location;
15294 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15295 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15296 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15297 ops->decode_location = bkpt_decode_location;
2060206e
PA
15298
15299 /* The breakpoint_ops structure to be used in regular breakpoints. */
15300 ops = &bkpt_breakpoint_ops;
15301 *ops = bkpt_base_breakpoint_ops;
15302 ops->re_set = bkpt_re_set;
15303 ops->resources_needed = bkpt_resources_needed;
15304 ops->print_it = bkpt_print_it;
15305 ops->print_mention = bkpt_print_mention;
15306 ops->print_recreate = bkpt_print_recreate;
15307
15308 /* Ranged breakpoints. */
15309 ops = &ranged_breakpoint_ops;
15310 *ops = bkpt_breakpoint_ops;
15311 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15312 ops->resources_needed = resources_needed_ranged_breakpoint;
15313 ops->print_it = print_it_ranged_breakpoint;
15314 ops->print_one = print_one_ranged_breakpoint;
15315 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15316 ops->print_mention = print_mention_ranged_breakpoint;
15317 ops->print_recreate = print_recreate_ranged_breakpoint;
15318
15319 /* Internal breakpoints. */
15320 ops = &internal_breakpoint_ops;
15321 *ops = bkpt_base_breakpoint_ops;
15322 ops->re_set = internal_bkpt_re_set;
15323 ops->check_status = internal_bkpt_check_status;
15324 ops->print_it = internal_bkpt_print_it;
15325 ops->print_mention = internal_bkpt_print_mention;
15326
15327 /* Momentary breakpoints. */
15328 ops = &momentary_breakpoint_ops;
15329 *ops = bkpt_base_breakpoint_ops;
15330 ops->re_set = momentary_bkpt_re_set;
15331 ops->check_status = momentary_bkpt_check_status;
15332 ops->print_it = momentary_bkpt_print_it;
15333 ops->print_mention = momentary_bkpt_print_mention;
15334
55aa24fb
SDJ
15335 /* Probe breakpoints. */
15336 ops = &bkpt_probe_breakpoint_ops;
15337 *ops = bkpt_breakpoint_ops;
15338 ops->insert_location = bkpt_probe_insert_location;
15339 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15340 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15341 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15342
2060206e
PA
15343 /* Watchpoints. */
15344 ops = &watchpoint_breakpoint_ops;
15345 *ops = base_breakpoint_ops;
15346 ops->re_set = re_set_watchpoint;
15347 ops->insert_location = insert_watchpoint;
15348 ops->remove_location = remove_watchpoint;
15349 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15350 ops->check_status = check_status_watchpoint;
15351 ops->resources_needed = resources_needed_watchpoint;
15352 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15353 ops->print_it = print_it_watchpoint;
15354 ops->print_mention = print_mention_watchpoint;
15355 ops->print_recreate = print_recreate_watchpoint;
427cd150 15356 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15357
15358 /* Masked watchpoints. */
15359 ops = &masked_watchpoint_breakpoint_ops;
15360 *ops = watchpoint_breakpoint_ops;
15361 ops->insert_location = insert_masked_watchpoint;
15362 ops->remove_location = remove_masked_watchpoint;
15363 ops->resources_needed = resources_needed_masked_watchpoint;
15364 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15365 ops->print_it = print_it_masked_watchpoint;
15366 ops->print_one_detail = print_one_detail_masked_watchpoint;
15367 ops->print_mention = print_mention_masked_watchpoint;
15368 ops->print_recreate = print_recreate_masked_watchpoint;
15369
15370 /* Tracepoints. */
15371 ops = &tracepoint_breakpoint_ops;
15372 *ops = base_breakpoint_ops;
15373 ops->re_set = tracepoint_re_set;
15374 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15375 ops->print_one_detail = tracepoint_print_one_detail;
15376 ops->print_mention = tracepoint_print_mention;
15377 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15378 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15379 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15380 ops->decode_location = tracepoint_decode_location;
983af33b 15381
55aa24fb
SDJ
15382 /* Probe tracepoints. */
15383 ops = &tracepoint_probe_breakpoint_ops;
15384 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15385 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15386 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15387
983af33b
SDJ
15388 /* Static tracepoints with marker (`-m'). */
15389 ops = &strace_marker_breakpoint_ops;
15390 *ops = tracepoint_breakpoint_ops;
5f700d83 15391 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15392 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15393 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15394
15395 /* Fork catchpoints. */
15396 ops = &catch_fork_breakpoint_ops;
15397 *ops = base_breakpoint_ops;
15398 ops->insert_location = insert_catch_fork;
15399 ops->remove_location = remove_catch_fork;
15400 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15401 ops->print_it = print_it_catch_fork;
15402 ops->print_one = print_one_catch_fork;
15403 ops->print_mention = print_mention_catch_fork;
15404 ops->print_recreate = print_recreate_catch_fork;
15405
15406 /* Vfork catchpoints. */
15407 ops = &catch_vfork_breakpoint_ops;
15408 *ops = base_breakpoint_ops;
15409 ops->insert_location = insert_catch_vfork;
15410 ops->remove_location = remove_catch_vfork;
15411 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15412 ops->print_it = print_it_catch_vfork;
15413 ops->print_one = print_one_catch_vfork;
15414 ops->print_mention = print_mention_catch_vfork;
15415 ops->print_recreate = print_recreate_catch_vfork;
15416
15417 /* Exec catchpoints. */
15418 ops = &catch_exec_breakpoint_ops;
15419 *ops = base_breakpoint_ops;
2060206e
PA
15420 ops->insert_location = insert_catch_exec;
15421 ops->remove_location = remove_catch_exec;
15422 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15423 ops->print_it = print_it_catch_exec;
15424 ops->print_one = print_one_catch_exec;
15425 ops->print_mention = print_mention_catch_exec;
15426 ops->print_recreate = print_recreate_catch_exec;
15427
edcc5120
TT
15428 /* Solib-related catchpoints. */
15429 ops = &catch_solib_breakpoint_ops;
15430 *ops = base_breakpoint_ops;
edcc5120
TT
15431 ops->insert_location = insert_catch_solib;
15432 ops->remove_location = remove_catch_solib;
15433 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15434 ops->check_status = check_status_catch_solib;
15435 ops->print_it = print_it_catch_solib;
15436 ops->print_one = print_one_catch_solib;
15437 ops->print_mention = print_mention_catch_solib;
15438 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15439
15440 ops = &dprintf_breakpoint_ops;
15441 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15442 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15443 ops->resources_needed = bkpt_resources_needed;
15444 ops->print_it = bkpt_print_it;
15445 ops->print_mention = bkpt_print_mention;
2d9442cc 15446 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15447 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15448 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15449}
15450
8bfd80db
YQ
15451/* Chain containing all defined "enable breakpoint" subcommands. */
15452
15453static struct cmd_list_element *enablebreaklist = NULL;
15454
8588b356
SM
15455/* See breakpoint.h. */
15456
15457cmd_list_element *commands_cmd_element = nullptr;
15458
6c265988 15459void _initialize_breakpoint ();
c906108c 15460void
6c265988 15461_initialize_breakpoint ()
c906108c
SS
15462{
15463 struct cmd_list_element *c;
15464
2060206e
PA
15465 initialize_breakpoint_ops ();
15466
c90e7d63
SM
15467 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15468 "breakpoint");
15469 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15470 "breakpoint");
15471 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15472 "breakpoint");
84acb35a 15473
c906108c
SS
15474 breakpoint_chain = 0;
15475 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15476 before a breakpoint is set. */
15477 breakpoint_count = 0;
15478
1042e4c0
SS
15479 tracepoint_count = 0;
15480
1bedd215
AC
15481 add_com ("ignore", class_breakpoint, ignore_command, _("\
15482Set ignore-count of breakpoint number N to COUNT.\n\
15483Usage is `ignore N COUNT'."));
c906108c 15484
8588b356
SM
15485 commands_cmd_element = add_com ("commands", class_breakpoint,
15486 commands_command, _("\
18da0c51
MG
15487Set commands to be executed when the given breakpoints are hit.\n\
15488Give a space-separated breakpoint list as argument after \"commands\".\n\
15489A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15490(e.g. `5-7').\n\
c906108c
SS
15491With no argument, the targeted breakpoint is the last one set.\n\
15492The commands themselves follow starting on the next line.\n\
15493Type a line containing \"end\" to indicate the end of them.\n\
15494Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15495then no output is printed when it is hit, except what the commands print."));
c906108c 15496
b1d4d8d1
TBA
15497 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15498 static std::string condition_command_help
15499 = gdb::option::build_help (_("\
1bedd215 15500Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15501Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15502is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15503\n\
15504Options:\n\
15505%OPTIONS%"), cc_opts);
15506
15507 c = add_com ("condition", class_breakpoint, condition_command,
15508 condition_command_help.c_str ());
15509 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15510
1bedd215 15511 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15512Set a temporary breakpoint.\n\
c906108c
SS
15513Like \"break\" except the breakpoint is only temporary,\n\
15514so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15515by using \"enable delete\" on the breakpoint number.\n\
15516\n"
15517BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15518 set_cmd_completer (c, location_completer);
c94fdfd0 15519
1bedd215 15520 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15521Set a hardware assisted breakpoint.\n\
c906108c 15522Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15523some target hardware may not have this support.\n\
15524\n"
15525BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15526 set_cmd_completer (c, location_completer);
c906108c 15527
1bedd215 15528 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15529Set a temporary hardware assisted breakpoint.\n\
c906108c 15530Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15531so it will be deleted when hit.\n\
15532\n"
15533BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15534 set_cmd_completer (c, location_completer);
c906108c 15535
3947f654
SM
15536 cmd_list_element *enable_cmd
15537 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15538Enable all or some breakpoints.\n\
15539Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15540Give breakpoint numbers (separated by spaces) as arguments.\n\
15541With no subcommand, breakpoints are enabled until you command otherwise.\n\
15542This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15543With a subcommand you can enable temporarily."),
3947f654 15544 &enablelist, 1, &cmdlist);
c906108c 15545
3947f654 15546 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 15547
84951ab5 15548 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15549Enable all or some breakpoints.\n\
15550Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15551Give breakpoint numbers (separated by spaces) as arguments.\n\
15552This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15553May be abbreviated to simply \"enable\"."),
2f822da5 15554 &enablebreaklist, 1, &enablelist);
c906108c 15555
1a966eab 15556 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15557Enable some breakpoints for one hit.\n\
15558Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15559If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15560 &enablebreaklist);
15561
1a966eab 15562 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15563Enable some breakpoints and delete when hit.\n\
15564Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15565If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15566 &enablebreaklist);
15567
816338b5 15568 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15569Enable some breakpoints for COUNT hits.\n\
15570Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15571If a breakpoint is hit while enabled in this fashion,\n\
15572the count is decremented; when it reaches zero, the breakpoint is disabled."),
15573 &enablebreaklist);
15574
1a966eab 15575 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15576Enable some breakpoints and delete when hit.\n\
15577Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15578If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15579 &enablelist);
15580
1a966eab 15581 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15582Enable some breakpoints for one hit.\n\
15583Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15584If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15585 &enablelist);
15586
15587 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15588Enable some breakpoints for COUNT hits.\n\
15589Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15590If a breakpoint is hit while enabled in this fashion,\n\
15591the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15592 &enablelist);
15593
3947f654
SM
15594 cmd_list_element *disable_cmd
15595 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15596Disable all or some breakpoints.\n\
15597Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15598Arguments are breakpoint numbers with spaces in between.\n\
15599To disable all breakpoints, give no argument.\n\
64b9b334 15600A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
15601 &disablelist, 1, &cmdlist);
15602 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15603 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 15604
57b4f16e 15605 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15606Disable all or some breakpoints.\n\
15607Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15608Arguments are breakpoint numbers with spaces in between.\n\
15609To disable all breakpoints, give no argument.\n\
64b9b334 15610A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15611This command may be abbreviated \"disable\"."),
c906108c
SS
15612 &disablelist);
15613
3947f654
SM
15614 cmd_list_element *delete_cmd
15615 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15616Delete all or some breakpoints.\n\
15617Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15618Arguments are breakpoint numbers with spaces in between.\n\
15619To delete all breakpoints, give no argument.\n\
15620\n\
590042fc 15621Also a prefix command for deletion of other GDB objects."),
3947f654
SM
15622 &deletelist, 1, &cmdlist);
15623 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15624 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 15625
57b4f16e 15626 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15627Delete all or some breakpoints or auto-display expressions.\n\
15628Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15629Arguments are breakpoint numbers with spaces in between.\n\
15630To delete all breakpoints, give no argument.\n\
1a966eab 15631This command may be abbreviated \"delete\"."),
c906108c
SS
15632 &deletelist);
15633
3947f654
SM
15634 cmd_list_element *clear_cmd
15635 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15636Clear breakpoint at specified location.\n\
15637Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15638\n\
15639With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15640is executing in.\n"
89549d7f 15641"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15642See also the \"delete\" command which clears breakpoints by number."));
3947f654 15643 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 15644
3947f654
SM
15645 cmd_list_element *break_cmd
15646 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15647Set breakpoint at specified location.\n"
31e2b00f 15648BREAK_ARGS_HELP ("break")));
3947f654 15649 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 15650
3947f654
SM
15651 add_com_alias ("b", break_cmd, class_run, 1);
15652 add_com_alias ("br", break_cmd, class_run, 1);
15653 add_com_alias ("bre", break_cmd, class_run, 1);
15654 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 15655
c906108c
SS
15656 if (dbx_commands)
15657 {
1bedd215
AC
15658 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15659Break in function/address or break at a line in the current file."),
2f822da5 15660 &stoplist, 1, &cmdlist);
c5aa993b 15661 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15662 _("Break in function or address."), &stoplist);
c5aa993b 15663 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15664 _("Break at a line in the current file."), &stoplist);
11db9430 15665 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15666Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15667The \"Type\" column indicates one of:\n\
15668\tbreakpoint - normal breakpoint\n\
15669\twatchpoint - watchpoint\n\
15670The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15671the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15672breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15673address and file/line number respectively.\n\
15674\n\
15675Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15676are set to the address of the last breakpoint listed unless the command\n\
15677is prefixed with \"server \".\n\n\
c906108c 15678Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15679breakpoint set."));
c906108c
SS
15680 }
15681
e0f25bd9
SM
15682 cmd_list_element *info_breakpoints_cmd
15683 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15684Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15685The \"Type\" column indicates one of:\n\
15686\tbreakpoint - normal breakpoint\n\
15687\twatchpoint - watchpoint\n\
15688The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15689the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15690breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15691address and file/line number respectively.\n\
15692\n\
15693Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15694are set to the address of the last breakpoint listed unless the command\n\
15695is prefixed with \"server \".\n\n\
c906108c 15696Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15697breakpoint set."));
c906108c 15698
e0f25bd9 15699 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 15700
1a966eab
AC
15701 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15702Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15703The \"Type\" column indicates one of:\n\
15704\tbreakpoint - normal breakpoint\n\
15705\twatchpoint - watchpoint\n\
15706\tlongjmp - internal breakpoint used to step through longjmp()\n\
15707\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15708\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15709\tfinish - internal breakpoint used by the \"finish\" command\n\
15710The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15711the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15712breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15713address and file/line number respectively.\n\
15714\n\
15715Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15716are set to the address of the last breakpoint listed unless the command\n\
15717is prefixed with \"server \".\n\n\
c906108c 15718Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15719breakpoint set."),
c906108c
SS
15720 &maintenanceinfolist);
15721
3b6acaee 15722 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15723Set catchpoints to catch events."),
2f822da5 15724 &catch_cmdlist,
3b6acaee 15725 0/*allow-unknown*/, &cmdlist);
44feb3ce 15726
3b6acaee 15727 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15728Set temporary catchpoints to catch events."),
2f822da5 15729 &tcatch_cmdlist,
3b6acaee 15730 0/*allow-unknown*/, &cmdlist);
44feb3ce 15731
44feb3ce
TT
15732 add_catch_command ("fork", _("Catch calls to fork."),
15733 catch_fork_command_1,
dda83cd7 15734 NULL,
44feb3ce
TT
15735 (void *) (uintptr_t) catch_fork_permanent,
15736 (void *) (uintptr_t) catch_fork_temporary);
15737 add_catch_command ("vfork", _("Catch calls to vfork."),
15738 catch_fork_command_1,
dda83cd7 15739 NULL,
44feb3ce
TT
15740 (void *) (uintptr_t) catch_vfork_permanent,
15741 (void *) (uintptr_t) catch_vfork_temporary);
15742 add_catch_command ("exec", _("Catch calls to exec."),
15743 catch_exec_command_1,
dda83cd7 15744 NULL,
a96d9b2e
SDJ
15745 CATCH_PERMANENT,
15746 CATCH_TEMPORARY);
edcc5120
TT
15747 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15748Usage: catch load [REGEX]\n\
15749If REGEX is given, only stop for libraries matching the regular expression."),
15750 catch_load_command_1,
15751 NULL,
15752 CATCH_PERMANENT,
15753 CATCH_TEMPORARY);
15754 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15755Usage: catch unload [REGEX]\n\
15756If REGEX is given, only stop for libraries matching the regular expression."),
15757 catch_unload_command_1,
15758 NULL,
15759 CATCH_PERMANENT,
15760 CATCH_TEMPORARY);
c5aa993b 15761
a15a5258
AB
15762 const auto opts = make_watch_options_def_group (nullptr);
15763
15764 static const std::string watch_help = gdb::option::build_help (_("\
15765Set a watchpoint for EXPRESSION.\n\
15766Usage: watch [-location] EXPRESSION\n\
15767\n\
15768Options:\n\
15769%OPTIONS%\n\
15770\n\
c906108c 15771A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15772an expression changes."), opts);
15773 c = add_com ("watch", class_breakpoint, watch_command,
15774 watch_help.c_str ());
15775 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15776
15777 static const std::string rwatch_help = gdb::option::build_help (_("\
15778Set a read watchpoint for EXPRESSION.\n\
15779Usage: rwatch [-location] EXPRESSION\n\
15780\n\
15781Options:\n\
15782%OPTIONS%\n\
15783\n\
15784A read watchpoint stops execution of your program whenever the value of\n\
15785an expression is read."), opts);
15786 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15787 rwatch_help.c_str ());
15788 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15789
15790 static const std::string awatch_help = gdb::option::build_help (_("\
15791Set an access watchpoint for EXPRESSION.\n\
15792Usage: awatch [-location] EXPRESSION\n\
15793\n\
15794Options:\n\
15795%OPTIONS%\n\
15796\n\
15797An access watchpoint stops execution of your program whenever the value\n\
15798of an expression is either read or written."), opts);
15799 c = add_com ("awatch", class_breakpoint, awatch_command,
15800 awatch_help.c_str ());
15801 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15802
11db9430 15803 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15804Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15805
920d2a44
AC
15806 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15807 respond to changes - contrary to the description. */
85c07804
AC
15808 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15809 &can_use_hw_watchpoints, _("\
15810Set debugger's willingness to use watchpoint hardware."), _("\
15811Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15812If zero, gdb will not use hardware for new watchpoints, even if\n\
15813such is available. (However, any hardware watchpoints that were\n\
15814created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15815hardware.)"),
15816 NULL,
920d2a44 15817 show_can_use_hw_watchpoints,
85c07804 15818 &setlist, &showlist);
c906108c
SS
15819
15820 can_use_hw_watchpoints = 1;
fa8d40ab 15821
1042e4c0
SS
15822 /* Tracepoint manipulation commands. */
15823
3947f654
SM
15824 cmd_list_element *trace_cmd
15825 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15826Set a tracepoint at specified location.\n\
1042e4c0
SS
15827\n"
15828BREAK_ARGS_HELP ("trace") "\n\
15829Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 15830 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 15831
3947f654
SM
15832 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15833 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15834 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15835 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 15836
7a697b8d 15837 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15838Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15839\n"
15840BREAK_ARGS_HELP ("ftrace") "\n\
15841Do \"help tracepoints\" for info on other tracepoint commands."));
15842 set_cmd_completer (c, location_completer);
15843
0fb4aa4b 15844 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15845Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15846\n\
15847strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15848LOCATION may be a linespec, explicit, or address location (described below) \n\
15849or -m MARKER_ID.\n\n\
15850If a marker id is specified, probe the marker with that name. With\n\
15851no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15852Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15853This collects arbitrary user data passed in the probe point call to the\n\
15854tracing library. You can inspect it when analyzing the trace buffer,\n\
15855by printing the $_sdata variable like any other convenience variable.\n\
15856\n\
15857CONDITION is a boolean expression.\n\
89549d7f 15858\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15859Multiple tracepoints at one place are permitted, and useful if their\n\
15860conditions are different.\n\
0fb4aa4b
PA
15861\n\
15862Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15863Do \"help tracepoints\" for info on other tracepoint commands."));
15864 set_cmd_completer (c, location_completer);
15865
e0f25bd9
SM
15866 cmd_list_element *info_tracepoints_cmd
15867 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15868Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15869Convenience variable \"$tpnum\" contains the number of the\n\
15870last tracepoint set."));
15871
e0f25bd9 15872 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 15873
5e84b7ee
SM
15874 cmd_list_element *delete_tracepoints_cmd
15875 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
15876Delete specified tracepoints.\n\
15877Arguments are tracepoint numbers, separated by spaces.\n\
15878No argument means delete all tracepoints."),
15879 &deletelist);
5e84b7ee 15880 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
15881
15882 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15883Disable specified tracepoints.\n\
15884Arguments are tracepoint numbers, separated by spaces.\n\
15885No argument means disable all tracepoints."),
15886 &disablelist);
15887 deprecate_cmd (c, "disable");
15888
15889 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15890Enable specified tracepoints.\n\
15891Arguments are tracepoint numbers, separated by spaces.\n\
15892No argument means enable all tracepoints."),
15893 &enablelist);
15894 deprecate_cmd (c, "enable");
15895
15896 add_com ("passcount", class_trace, trace_pass_command, _("\
15897Set the passcount for a tracepoint.\n\
15898The trace will end when the tracepoint has been passed 'count' times.\n\
15899Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15900if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15901
0743fc83
TT
15902 add_basic_prefix_cmd ("save", class_breakpoint,
15903 _("Save breakpoint definitions as a script."),
2f822da5 15904 &save_cmdlist,
0743fc83 15905 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15906
15907 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15908Save current breakpoint definitions as a script.\n\
cce7e648 15909This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15910catchpoints, tracepoints). Use the 'source' command in another debug\n\
15911session to restore them."),
15912 &save_cmdlist);
15913 set_cmd_completer (c, filename_completer);
15914
3947f654
SM
15915 cmd_list_element *save_tracepoints_cmd
15916 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15917Save current tracepoint definitions as a script.\n\
6149aea9
PA
15918Use the 'source' command in another debug session to restore them."),
15919 &save_cmdlist);
3947f654 15920 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 15921
3947f654 15922 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
15923 deprecate_cmd (c, "save tracepoints");
15924
3b6acaee 15925 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15926Breakpoint specific settings.\n\
fa8d40ab 15927Configure various breakpoint-specific variables such as\n\
590042fc 15928pending breakpoint behavior."),
2f822da5 15929 &breakpoint_set_cmdlist,
3b6acaee
TT
15930 0/*allow-unknown*/, &setlist);
15931 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15932Breakpoint specific settings.\n\
fa8d40ab 15933Configure various breakpoint-specific variables such as\n\
590042fc 15934pending breakpoint behavior."),
2f822da5 15935 &breakpoint_show_cmdlist,
3b6acaee 15936 0/*allow-unknown*/, &showlist);
fa8d40ab 15937
7915a72c
AC
15938 add_setshow_auto_boolean_cmd ("pending", no_class,
15939 &pending_break_support, _("\
15940Set debugger's behavior regarding pending breakpoints."), _("\
15941Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15942If on, an unrecognized breakpoint location will cause gdb to create a\n\
15943pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15944an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15945user-query to see if a pending breakpoint should be created."),
2c5b56ce 15946 NULL,
920d2a44 15947 show_pending_break_support,
6e1d7d6c
AC
15948 &breakpoint_set_cmdlist,
15949 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15950
15951 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15952
15953 add_setshow_boolean_cmd ("auto-hw", no_class,
15954 &automatic_hardware_breakpoints, _("\
15955Set automatic usage of hardware breakpoints."), _("\
15956Show automatic usage of hardware breakpoints."), _("\
15957If set, the debugger will automatically use hardware breakpoints for\n\
15958breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15959a warning will be emitted for such breakpoints."),
15960 NULL,
15961 show_automatic_hardware_breakpoints,
15962 &breakpoint_set_cmdlist,
15963 &breakpoint_show_cmdlist);
74960c60 15964
a25a5a45
PA
15965 add_setshow_boolean_cmd ("always-inserted", class_support,
15966 &always_inserted_mode, _("\
74960c60
VP
15967Set mode for inserting breakpoints."), _("\
15968Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15969When this mode is on, breakpoints are inserted immediately as soon as\n\
15970they're created, kept inserted even when execution stops, and removed\n\
15971only when the user deletes them. When this mode is off (the default),\n\
15972breakpoints are inserted only when execution continues, and removed\n\
15973when execution stops."),
72d0e2c5
YQ
15974 NULL,
15975 &show_always_inserted_mode,
15976 &breakpoint_set_cmdlist,
15977 &breakpoint_show_cmdlist);
f1310107 15978
b775012e
LM
15979 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15980 condition_evaluation_enums,
15981 &condition_evaluation_mode_1, _("\
15982Set mode of breakpoint condition evaluation."), _("\
15983Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15984When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15985evaluated on the host's side by GDB. When it is set to \"target\",\n\
15986breakpoint conditions will be downloaded to the target (if the target\n\
15987supports such feature) and conditions will be evaluated on the target's side.\n\
15988If this is set to \"auto\" (default), this will be automatically set to\n\
15989\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 15990be set to \"host\"."),
b775012e
LM
15991 &set_condition_evaluation_mode,
15992 &show_condition_evaluation_mode,
15993 &breakpoint_set_cmdlist,
15994 &breakpoint_show_cmdlist);
15995
f1310107
TJB
15996 add_com ("break-range", class_breakpoint, break_range_command, _("\
15997Set a breakpoint for an address range.\n\
15998break-range START-LOCATION, END-LOCATION\n\
15999where START-LOCATION and END-LOCATION can be one of the following:\n\
16000 LINENUM, for that line in the current file,\n\
16001 FILE:LINENUM, for that line in that file,\n\
16002 +OFFSET, for that number of lines after the current line\n\
dda83cd7 16003 or the start of the range\n\
f1310107
TJB
16004 FUNCTION, for the first line in that function,\n\
16005 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16006 *ADDRESS, for the instruction at that address.\n\
16007\n\
16008The breakpoint will stop execution of the inferior whenever it executes\n\
16009an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16010range (including START-LOCATION and END-LOCATION)."));
16011
e7e0cddf 16012 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16013Set a dynamic printf at specified location.\n\
e7e0cddf 16014dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16015location may be a linespec, explicit, or address location.\n"
16016"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16017 set_cmd_completer (c, location_completer);
16018
16019 add_setshow_enum_cmd ("dprintf-style", class_support,
16020 dprintf_style_enums, &dprintf_style, _("\
16021Set the style of usage for dynamic printf."), _("\
16022Show the style of usage for dynamic printf."), _("\
16023This setting chooses how GDB will do a dynamic printf.\n\
16024If the value is \"gdb\", then the printing is done by GDB to its own\n\
16025console, as with the \"printf\" command.\n\
16026If the value is \"call\", the print is done by calling a function in your\n\
16027program; by default printf(), but you can choose a different function or\n\
16028output stream by setting dprintf-function and dprintf-channel."),
16029 update_dprintf_commands, NULL,
16030 &setlist, &showlist);
16031
16032 dprintf_function = xstrdup ("printf");
16033 add_setshow_string_cmd ("dprintf-function", class_support,
16034 &dprintf_function, _("\
590042fc
PW
16035Set the function to use for dynamic printf."), _("\
16036Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16037 update_dprintf_commands, NULL,
16038 &setlist, &showlist);
16039
16040 dprintf_channel = xstrdup ("");
16041 add_setshow_string_cmd ("dprintf-channel", class_support,
16042 &dprintf_channel, _("\
590042fc
PW
16043Set the channel to use for dynamic printf."), _("\
16044Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16045 update_dprintf_commands, NULL,
16046 &setlist, &showlist);
16047
d3ce09f5
SS
16048 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16049 &disconnected_dprintf, _("\
16050Set whether dprintf continues after GDB disconnects."), _("\
16051Show whether dprintf continues after GDB disconnects."), _("\
16052Use this to let dprintf commands continue to hit and produce output\n\
16053even if GDB disconnects or detaches from the target."),
16054 NULL,
16055 NULL,
16056 &setlist, &showlist);
16057
16058 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16059Target agent only formatted printing, like the C \"printf\" function.\n\
16060Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16061This supports most C printf format specifications, like %s, %d, etc.\n\
16062This is useful for formatted output in user-defined commands."));
d3ce09f5 16063
491144b5 16064 automatic_hardware_breakpoints = true;
f3b1572e 16065
c90e7d63
SM
16066 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16067 "breakpoint");
16068 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16069 "breakpoint");
c906108c 16070}
This page took 3.804273 seconds and 4 git commands to generate.