Consolidate CU language setting
[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"
d3ce09f5 70
1042e4c0 71/* readline include files */
073bbbb0 72#include "readline/tilde.h"
1042e4c0
SS
73
74/* readline defines this. */
75#undef savestring
76
034dad6f 77#include "mi/mi-common.h"
6dddc817 78#include "extension.h"
325fac50 79#include <algorithm>
5ed8105e 80#include "progspace-and-thread.h"
268a13a5
TT
81#include "gdbsupport/array-view.h"
82#include "gdbsupport/gdb_optional.h"
104c1213 83
4a64f543 84/* Prototypes for local functions. */
c906108c 85
896b6bda 86static void map_breakpoint_numbers (const char *,
48649e1b 87 gdb::function_view<void (breakpoint *)>);
c906108c 88
348d480f
PA
89static void breakpoint_re_set_default (struct breakpoint *);
90
f00aae0f 91static void
626d2320 92 create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
93 struct linespec_result *canonical,
94 enum bptype type_wanted);
983af33b
SDJ
95
96static void create_breakpoints_sal_default (struct gdbarch *,
97 struct linespec_result *,
e1e01040
PA
98 gdb::unique_xmalloc_ptr<char>,
99 gdb::unique_xmalloc_ptr<char>,
100 enum bptype,
983af33b
SDJ
101 enum bpdisp, int, int,
102 int,
103 const struct breakpoint_ops *,
44f238bb 104 int, int, int, unsigned);
983af33b 105
6c5b2ebe 106static std::vector<symtab_and_line> decode_location_default
626d2320 107 (struct breakpoint *b, struct event_location *location,
6c5b2ebe 108 struct program_space *search_pspace);
983af33b 109
a6535de1
TT
110static int can_use_hardware_watchpoint
111 (const std::vector<value_ref_ptr> &vals);
c906108c 112
a14ed312 113static void mention (struct breakpoint *);
c906108c 114
348d480f
PA
115static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
116 enum bptype,
c0a91b2b 117 const struct breakpoint_ops *);
3742cc8b
YQ
118static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
119 const struct symtab_and_line *);
120
4a64f543
MS
121/* This function is used in gdbtk sources and thus can not be made
122 static. */
63c252f8 123struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 124 struct symtab_and_line,
c0a91b2b
TT
125 enum bptype,
126 const struct breakpoint_ops *);
c906108c 127
06edf0c0
PA
128static struct breakpoint *
129 momentary_breakpoint_from_master (struct breakpoint *orig,
130 enum bptype type,
a1aa2221
LM
131 const struct breakpoint_ops *ops,
132 int loc_enabled);
06edf0c0 133
76897487
KB
134static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
135
a6d9a66e
UW
136static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
137 CORE_ADDR bpaddr,
dda83cd7 138 enum bptype bptype);
76897487 139
6c95b8df
PA
140static void describe_other_breakpoints (struct gdbarch *,
141 struct program_space *, CORE_ADDR,
5af949e3 142 struct obj_section *, int);
c906108c 143
85d721b8
PA
144static int watchpoint_locations_match (struct bp_location *loc1,
145 struct bp_location *loc2);
146
7f32a4d5
PA
147static int breakpoint_locations_match (struct bp_location *loc1,
148 struct bp_location *loc2,
149 bool sw_hw_bps_match = false);
150
f1310107 151static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 152 const struct address_space *aspace,
f1310107
TJB
153 CORE_ADDR addr);
154
d35ae833 155static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 156 const address_space *,
d35ae833
PA
157 CORE_ADDR, int);
158
834c0d03 159static int remove_breakpoint (struct bp_location *);
b2b6a7da 160static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 161
e514a9d6 162static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 163
a14ed312 164static int hw_breakpoint_used_count (void);
c906108c 165
a1398e0c
PA
166static int hw_watchpoint_use_count (struct breakpoint *);
167
168static int hw_watchpoint_used_count_others (struct breakpoint *except,
169 enum bptype type,
170 int *other_type_used);
c906108c 171
816338b5
SS
172static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
173 int count);
c906108c 174
f431efe5 175static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 176
39d61571 177static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 178
44702360
PA
179/* update_global_location_list's modes of operation wrt to whether to
180 insert locations now. */
181enum ugll_insert_mode
182{
183 /* Don't insert any breakpoint locations into the inferior, only
184 remove already-inserted locations that no longer should be
185 inserted. Functions that delete a breakpoint or breakpoints
186 should specify this mode, so that deleting a breakpoint doesn't
187 have the side effect of inserting the locations of other
188 breakpoints that are marked not-inserted, but should_be_inserted
189 returns true on them.
190
191 This behavior is useful is situations close to tear-down -- e.g.,
192 after an exec, while the target still has execution, but
193 breakpoint shadows of the previous executable image should *NOT*
194 be restored to the new image; or before detaching, where the
195 target still has execution and wants to delete breakpoints from
196 GDB's lists, and all breakpoints had already been removed from
197 the inferior. */
198 UGLL_DONT_INSERT,
199
a25a5a45
PA
200 /* May insert breakpoints iff breakpoints_should_be_inserted_now
201 claims breakpoints should be inserted now. */
04086b45
PA
202 UGLL_MAY_INSERT,
203
a25a5a45
PA
204 /* Insert locations now, irrespective of
205 breakpoints_should_be_inserted_now. E.g., say all threads are
206 stopped right now, and the user did "continue". We need to
207 insert breakpoints _before_ resuming the target, but
208 UGLL_MAY_INSERT wouldn't insert them, because
209 breakpoints_should_be_inserted_now returns false at that point,
210 as no thread is running yet. */
04086b45 211 UGLL_INSERT
44702360
PA
212};
213
214static void update_global_location_list (enum ugll_insert_mode);
a5606eee 215
44702360 216static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 217
74960c60 218static void insert_breakpoint_locations (void);
a5606eee 219
0b39b52e 220static void trace_pass_command (const char *, int);
1042e4c0 221
558a9d82
YQ
222static void set_tracepoint_count (int num);
223
f2478a7e 224static bool is_masked_watchpoint (const struct breakpoint *b);
9c06b0b4 225
983af33b
SDJ
226/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
227 otherwise. */
228
229static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 230
2060206e
PA
231/* The breakpoint_ops structure to be inherited by all breakpoint_ops
232 that are implemented on top of software or hardware breakpoints
233 (user breakpoints, internal and momentary breakpoints, etc.). */
234static struct breakpoint_ops bkpt_base_breakpoint_ops;
235
236/* Internal breakpoints class type. */
06edf0c0 237static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
238
239/* Momentary breakpoints class type. */
06edf0c0
PA
240static struct breakpoint_ops momentary_breakpoint_ops;
241
2060206e
PA
242/* The breakpoint_ops structure to be used in regular user created
243 breakpoints. */
244struct breakpoint_ops bkpt_breakpoint_ops;
245
55aa24fb
SDJ
246/* Breakpoints set on probes. */
247static struct breakpoint_ops bkpt_probe_breakpoint_ops;
248
bac7c5cf
GB
249/* Tracepoints set on probes. */
250static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
251
e7e0cddf 252/* Dynamic printf class type. */
c5867ab6 253struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 254
d3ce09f5
SS
255/* The style in which to perform a dynamic printf. This is a user
256 option because different output options have different tradeoffs;
257 if GDB does the printing, there is better error handling if there
258 is a problem with any of the arguments, but using an inferior
259 function lets you have special-purpose printers and sending of
260 output to the same place as compiled-in print functions. */
261
262static const char dprintf_style_gdb[] = "gdb";
263static const char dprintf_style_call[] = "call";
264static const char dprintf_style_agent[] = "agent";
265static const char *const dprintf_style_enums[] = {
266 dprintf_style_gdb,
267 dprintf_style_call,
268 dprintf_style_agent,
269 NULL
270};
271static const char *dprintf_style = dprintf_style_gdb;
272
273/* The function to use for dynamic printf if the preferred style is to
274 call into the inferior. The value is simply a string that is
275 copied into the command, so it can be anything that GDB can
276 evaluate to a callable address, not necessarily a function name. */
277
bde6261a 278static char *dprintf_function;
d3ce09f5
SS
279
280/* The channel to use for dynamic printf if the preferred style is to
281 call into the inferior; if a nonempty string, it will be passed to
282 the call as the first argument, with the format string as the
283 second. As with the dprintf function, this can be anything that
284 GDB knows how to evaluate, so in addition to common choices like
285 "stderr", this could be an app-specific expression like
286 "mystreams[curlogger]". */
287
bde6261a 288static char *dprintf_channel;
d3ce09f5
SS
289
290/* True if dprintf commands should continue to operate even if GDB
291 has disconnected. */
491144b5 292static bool disconnected_dprintf = true;
d3ce09f5 293
5cea2a26
PA
294struct command_line *
295breakpoint_commands (struct breakpoint *b)
296{
d1b0a7bf 297 return b->commands ? b->commands.get () : NULL;
5cea2a26 298}
3daf8fe5 299
f3b1572e
PA
300/* Flag indicating that a command has proceeded the inferior past the
301 current breakpoint. */
302
491144b5 303static bool breakpoint_proceeded;
f3b1572e 304
956a9fb9 305const char *
2cec12e5
AR
306bpdisp_text (enum bpdisp disp)
307{
4a64f543
MS
308 /* NOTE: the following values are a part of MI protocol and
309 represent values of 'disp' field returned when inferior stops at
310 a breakpoint. */
bc043ef3 311 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 312
2cec12e5
AR
313 return bpdisps[(int) disp];
314}
c906108c 315
4a64f543 316/* Prototypes for exported functions. */
c906108c 317/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 318 if such is available. */
c906108c
SS
319static int can_use_hw_watchpoints;
320
920d2a44
AC
321static void
322show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
323 struct cmd_list_element *c,
324 const char *value)
325{
3e43a32a
MS
326 fprintf_filtered (file,
327 _("Debugger's willingness to use "
328 "watchpoint hardware is %s.\n"),
920d2a44
AC
329 value);
330}
331
fa8d40ab
JJ
332/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
333 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 334 for unrecognized breakpoint locations.
fa8d40ab
JJ
335 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
336static enum auto_boolean pending_break_support;
920d2a44
AC
337static void
338show_pending_break_support (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341{
3e43a32a
MS
342 fprintf_filtered (file,
343 _("Debugger's behavior regarding "
344 "pending breakpoints is %s.\n"),
920d2a44
AC
345 value);
346}
fa8d40ab 347
491144b5 348/* If true, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 349 set with "break" but falling in read-only memory.
491144b5 350 If false, gdb will warn about such breakpoints, but won't automatically
765dc015 351 use hardware breakpoints. */
491144b5 352static bool automatic_hardware_breakpoints;
765dc015
VP
353static void
354show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357{
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
360 value);
361}
362
a25a5a45
PA
363/* If on, GDB keeps breakpoints inserted even if the inferior is
364 stopped, and immediately inserts any new breakpoints as soon as
365 they're created. If off (default), GDB keeps breakpoints off of
366 the target as long as possible. That is, it delays inserting
367 breakpoints until the next resume, and removes them again when the
368 target fully stops. This is a bit safer in case GDB crashes while
369 processing user input. */
491144b5 370static bool always_inserted_mode = false;
72d0e2c5 371
33e5cbd6 372static void
74960c60 373show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 374 struct cmd_list_element *c, const char *value)
74960c60 375{
a25a5a45
PA
376 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
377 value);
74960c60
VP
378}
379
b57bacec
PA
380/* See breakpoint.h. */
381
33e5cbd6 382int
a25a5a45 383breakpoints_should_be_inserted_now (void)
33e5cbd6 384{
a25a5a45
PA
385 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
386 {
387 /* If breakpoints are global, they should be inserted even if no
388 thread under gdb's control is running, or even if there are
389 no threads under GDB's control yet. */
390 return 1;
391 }
5b6d1e4f 392 else
a25a5a45 393 {
a25a5a45
PA
394 if (always_inserted_mode)
395 {
396 /* The user wants breakpoints inserted even if all threads
397 are stopped. */
398 return 1;
399 }
400
5b6d1e4f
PA
401 for (inferior *inf : all_inferiors ())
402 if (inf->has_execution ()
403 && threads_are_executing (inf->process_target ()))
404 return 1;
372316f1
PA
405
406 /* Don't remove breakpoints yet if, even though all threads are
407 stopped, we still have events to process. */
08036331 408 for (thread_info *tp : all_non_exited_threads ())
372316f1
PA
409 if (tp->resumed
410 && tp->suspend.waitstatus_pending_p)
411 return 1;
a25a5a45
PA
412 }
413 return 0;
33e5cbd6 414}
765dc015 415
b775012e
LM
416static const char condition_evaluation_both[] = "host or target";
417
418/* Modes for breakpoint condition evaluation. */
419static const char condition_evaluation_auto[] = "auto";
420static const char condition_evaluation_host[] = "host";
421static const char condition_evaluation_target[] = "target";
422static const char *const condition_evaluation_enums[] = {
423 condition_evaluation_auto,
424 condition_evaluation_host,
425 condition_evaluation_target,
426 NULL
427};
428
429/* Global that holds the current mode for breakpoint condition evaluation. */
430static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
431
432/* Global that we use to display information to the user (gets its value from
433 condition_evaluation_mode_1. */
434static const char *condition_evaluation_mode = condition_evaluation_auto;
435
436/* Translate a condition evaluation mode MODE into either "host"
437 or "target". This is used mostly to translate from "auto" to the
438 real setting that is being used. It returns the translated
439 evaluation mode. */
440
441static const char *
442translate_condition_evaluation_mode (const char *mode)
443{
444 if (mode == condition_evaluation_auto)
445 {
446 if (target_supports_evaluation_of_breakpoint_conditions ())
447 return condition_evaluation_target;
448 else
449 return condition_evaluation_host;
450 }
451 else
452 return mode;
453}
454
455/* Discovers what condition_evaluation_auto translates to. */
456
457static const char *
458breakpoint_condition_evaluation_mode (void)
459{
460 return translate_condition_evaluation_mode (condition_evaluation_mode);
461}
462
463/* Return true if GDB should evaluate breakpoint conditions or false
464 otherwise. */
465
466static int
467gdb_evaluates_breakpoint_condition_p (void)
468{
469 const char *mode = breakpoint_condition_evaluation_mode ();
470
471 return (mode == condition_evaluation_host);
472}
473
c906108c
SS
474/* Are we executing breakpoint commands? */
475static int executing_breakpoint_commands;
476
c02f5703
MS
477/* Are overlay event breakpoints enabled? */
478static int overlay_events_enabled;
479
e09342b5 480/* See description in breakpoint.h. */
491144b5 481bool target_exact_watchpoints = false;
e09342b5 482
c906108c 483/* Walk the following statement or block through all breakpoints.
e5dd4106 484 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 485 current breakpoint. */
c906108c 486
5c44784c
JM
487#define ALL_BREAKPOINTS_SAFE(B,TMP) \
488 for (B = breakpoint_chain; \
489 B ? (TMP=B->next, 1): 0; \
490 B = TMP)
c906108c 491
7cc221ef 492/* Chains of all breakpoints defined. */
c906108c 493
81e6b8eb 494static struct breakpoint *breakpoint_chain;
c906108c 495
240edef6 496/* See breakpoint.h. */
43892fdf 497
240edef6 498breakpoint_range
43892fdf
SM
499all_breakpoints ()
500{
501 return breakpoint_range (breakpoint_chain);
502}
503
240edef6 504/* See breakpoint.h. */
1428b37a 505
240edef6 506breakpoint_safe_range
1428b37a
SM
507all_breakpoints_safe ()
508{
509 return breakpoint_safe_range (all_breakpoints ());
510}
511
f6d17b2b
SM
512/* See breakpoint.h. */
513
514tracepoint_range
515all_tracepoints ()
516{
517 return tracepoint_range (breakpoint_chain);
518}
519
39ef2f62 520/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */
876fa593 521
5d51cd5d 522static std::vector<bp_location *> bp_locations;
876fa593 523
055c879f
SM
524/* See breakpoint.h. */
525
526const std::vector<bp_location *> &
48d7020b
SM
527all_bp_locations ()
528{
529 return bp_locations;
530}
531
e0d9a270
SM
532/* Range to iterate over breakpoint locations at a given address. */
533
534struct bp_locations_at_addr_range
535{
536 using iterator = std::vector<bp_location *>::iterator;
537
538 bp_locations_at_addr_range (CORE_ADDR addr)
539 {
540 struct compare
541 {
542 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
543 { return loc->address < addr_; }
544
545 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
546 { return addr_ < loc->address; }
547 };
548
549 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
550 addr, compare ());
551
552 m_begin = it_pair.first;
553 m_end = it_pair.second;
554 }
555
556 iterator begin () const
557 { return m_begin; }
558
559 iterator end () const
560 { return m_end; }
561
562private:
563 iterator m_begin;
564 iterator m_end;
565};
566
567/* Return a range to iterate over all breakpoint locations exactly at address
568 ADDR.
569
570 If it's needed to iterate multiple times on the same range, it's possible
571 to save the range in a local variable and use it multiple times:
572
573 auto range = all_bp_locations_at_addr (addr);
574
575 for (bp_location *loc : range)
576 // use loc
577
578 for (bp_location *loc : range)
579 // use loc
580
581 This saves a bit of time, as it avoids re-doing the binary searches to find
582 the range's boundaries. Just remember not to change the bp_locations vector
583 in the mean time, as it could make the range's iterators stale. */
584
585static bp_locations_at_addr_range
586all_bp_locations_at_addr (CORE_ADDR addr)
587{
588 return bp_locations_at_addr_range (addr);
589}
590
4a64f543 591/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 592 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 593 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 594 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 595 an address you need to read. */
876fa593 596
f5336ca5 597static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 598
4a64f543
MS
599/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
600 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
601 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
602 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 603 scan for shadow bytes for an address you need to read. */
876fa593 604
f5336ca5 605static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 606
4a64f543 607/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
608 from the bp_locations array, but for which a hit may still be
609 reported by a target. */
1123588c 610static std::vector<bp_location *> moribund_locations;
20874c92 611
c906108c
SS
612/* Number of last breakpoint made. */
613
95a42b64
TT
614static int breakpoint_count;
615
86b17b60
PA
616/* The value of `breakpoint_count' before the last command that
617 created breakpoints. If the last (break-like) command created more
618 than one breakpoint, then the difference between BREAKPOINT_COUNT
619 and PREV_BREAKPOINT_COUNT is more than one. */
620static int prev_breakpoint_count;
c906108c 621
1042e4c0
SS
622/* Number of last tracepoint made. */
623
95a42b64 624static int tracepoint_count;
1042e4c0 625
6149aea9
PA
626static struct cmd_list_element *breakpoint_set_cmdlist;
627static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 628struct cmd_list_element *save_cmdlist;
6149aea9 629
badd37ce
SDJ
630/* See declaration at breakpoint.h. */
631
632struct breakpoint *
633breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
634 void *user_data)
635{
43892fdf
SM
636 for (breakpoint *b : all_breakpoints ())
637 if (func (b, user_data) != 0)
638 return b;
badd37ce 639
43892fdf 640 return nullptr;
badd37ce
SDJ
641}
642
468d015d
JJ
643/* Return whether a breakpoint is an active enabled breakpoint. */
644static int
645breakpoint_enabled (struct breakpoint *b)
646{
0d381245 647 return (b->enable_state == bp_enabled);
468d015d
JJ
648}
649
c906108c
SS
650/* Set breakpoint count to NUM. */
651
95a42b64 652static void
fba45db2 653set_breakpoint_count (int num)
c906108c 654{
86b17b60 655 prev_breakpoint_count = breakpoint_count;
c906108c 656 breakpoint_count = num;
4fa62494 657 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
658}
659
86b17b60
PA
660/* Used by `start_rbreak_breakpoints' below, to record the current
661 breakpoint count before "rbreak" creates any breakpoint. */
662static int rbreak_start_breakpoint_count;
663
95a42b64
TT
664/* Called at the start an "rbreak" command to record the first
665 breakpoint made. */
86b17b60 666
c80049d3 667scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 668{
86b17b60 669 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
670}
671
672/* Called at the end of an "rbreak" command to record the last
673 breakpoint made. */
86b17b60 674
c80049d3 675scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 676{
86b17b60 677 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
678}
679
4a64f543 680/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
681
682void
fba45db2 683clear_breakpoint_hit_counts (void)
c906108c 684{
43892fdf 685 for (breakpoint *b : all_breakpoints ())
c906108c
SS
686 b->hit_count = 0;
687}
688
c906108c 689\f
48cb2d85
VP
690/* Return the breakpoint with the specified number, or NULL
691 if the number does not refer to an existing breakpoint. */
692
693struct breakpoint *
694get_breakpoint (int num)
695{
43892fdf 696 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
697 if (b->number == num)
698 return b;
699
43892fdf 700 return nullptr;
48cb2d85 701}
5c44784c 702
c906108c 703\f
adc36818 704
b775012e
LM
705/* Mark locations as "conditions have changed" in case the target supports
706 evaluating conditions on its side. */
707
708static void
709mark_breakpoint_modified (struct breakpoint *b)
710{
b775012e
LM
711 /* This is only meaningful if the target is
712 evaluating conditions and if the user has
713 opted for condition evaluation on the target's
714 side. */
715 if (gdb_evaluates_breakpoint_condition_p ()
716 || !target_supports_evaluation_of_breakpoint_conditions ())
717 return;
718
719 if (!is_breakpoint (b))
720 return;
721
40cb8ca5 722 for (bp_location *loc : b->locations ())
b775012e
LM
723 loc->condition_changed = condition_modified;
724}
725
726/* Mark location as "conditions have changed" in case the target supports
727 evaluating conditions on its side. */
728
729static void
730mark_breakpoint_location_modified (struct bp_location *loc)
731{
732 /* This is only meaningful if the target is
733 evaluating conditions and if the user has
734 opted for condition evaluation on the target's
735 side. */
736 if (gdb_evaluates_breakpoint_condition_p ()
737 || !target_supports_evaluation_of_breakpoint_conditions ())
738
739 return;
740
741 if (!is_breakpoint (loc->owner))
742 return;
743
744 loc->condition_changed = condition_modified;
745}
746
747/* Sets the condition-evaluation mode using the static global
748 condition_evaluation_mode. */
749
750static void
eb4c3f4a 751set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
752 struct cmd_list_element *c)
753{
b775012e
LM
754 const char *old_mode, *new_mode;
755
756 if ((condition_evaluation_mode_1 == condition_evaluation_target)
757 && !target_supports_evaluation_of_breakpoint_conditions ())
758 {
759 condition_evaluation_mode_1 = condition_evaluation_mode;
760 warning (_("Target does not support breakpoint condition evaluation.\n"
761 "Using host evaluation mode instead."));
762 return;
763 }
764
765 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
766 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
767
abf1152a
JK
768 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
769 settings was "auto". */
770 condition_evaluation_mode = condition_evaluation_mode_1;
771
b775012e
LM
772 /* Only update the mode if the user picked a different one. */
773 if (new_mode != old_mode)
774 {
b775012e
LM
775 /* If the user switched to a different evaluation mode, we
776 need to synch the changes with the target as follows:
777
778 "host" -> "target": Send all (valid) conditions to the target.
779 "target" -> "host": Remove all the conditions from the target.
780 */
781
b775012e
LM
782 if (new_mode == condition_evaluation_target)
783 {
784 /* Mark everything modified and synch conditions with the
785 target. */
48d7020b 786 for (bp_location *loc : all_bp_locations ())
b775012e 787 mark_breakpoint_location_modified (loc);
24b21115 788 }
b775012e
LM
789 else
790 {
791 /* Manually mark non-duplicate locations to synch conditions
792 with the target. We do this to remove all the conditions the
793 target knows about. */
48d7020b 794 for (bp_location *loc : all_bp_locations ())
b775012e
LM
795 if (is_breakpoint (loc->owner) && loc->inserted)
796 loc->needs_update = 1;
797 }
798
799 /* Do the update. */
44702360 800 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
801 }
802
803 return;
804}
805
806/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
807 what "auto" is translating to. */
808
809static void
810show_condition_evaluation_mode (struct ui_file *file, int from_tty,
811 struct cmd_list_element *c, const char *value)
812{
813 if (condition_evaluation_mode == condition_evaluation_auto)
814 fprintf_filtered (file,
815 _("Breakpoint condition evaluation "
816 "mode is %s (currently %s).\n"),
817 value,
818 breakpoint_condition_evaluation_mode ());
819 else
820 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
821 value);
822}
823
b5fa468f
TBA
824/* Parse COND_STRING in the context of LOC and set as the condition
825 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
826 the number of LOC within its owner. In case of parsing error, mark
827 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
828
829static void
830set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
831 int bp_num, int loc_num)
832{
833 bool has_junk = false;
834 try
835 {
836 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
837 block_for_pc (loc->address), 0);
838 if (*cond_string != 0)
839 has_junk = true;
840 else
841 {
842 loc->cond = std::move (new_exp);
843 if (loc->disabled_by_cond && loc->enabled)
844 printf_filtered (_("Breakpoint %d's condition is now valid at "
845 "location %d, enabling.\n"),
846 bp_num, loc_num);
847
848 loc->disabled_by_cond = false;
849 }
850 }
851 catch (const gdb_exception_error &e)
852 {
853 if (loc->enabled)
854 {
855 /* Warn if a user-enabled location is now becoming disabled-by-cond.
856 BP_NUM is 0 if the breakpoint is being defined for the first
857 time using the "break ... if ..." command, and non-zero if
858 already defined. */
859 if (bp_num != 0)
860 warning (_("failed to validate condition at location %d.%d, "
861 "disabling:\n %s"), bp_num, loc_num, e.what ());
862 else
863 warning (_("failed to validate condition at location %d, "
864 "disabling:\n %s"), loc_num, e.what ());
865 }
866
867 loc->disabled_by_cond = true;
868 }
869
870 if (has_junk)
871 error (_("Garbage '%s' follows condition"), cond_string);
872}
873
adc36818 874void
7a26bd4d 875set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 876 int from_tty, bool force)
adc36818 877{
4c55e970 878 if (*exp == 0)
3a5c3e22 879 {
4c55e970
TBA
880 xfree (b->cond_string);
881 b->cond_string = nullptr;
3a5c3e22 882
4c55e970 883 if (is_watchpoint (b))
78319c15 884 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
885 else
886 {
b5fa468f 887 int loc_num = 1;
40cb8ca5 888 for (bp_location *loc : b->locations ())
4c55e970
TBA
889 {
890 loc->cond.reset ();
b5fa468f
TBA
891 if (loc->disabled_by_cond && loc->enabled)
892 printf_filtered (_("Breakpoint %d's condition is now valid at "
893 "location %d, enabling.\n"),
894 b->number, loc_num);
895 loc->disabled_by_cond = false;
896 loc_num++;
4c55e970
TBA
897
898 /* No need to free the condition agent expression
899 bytecode (if we have one). We will handle this
900 when we go through update_global_location_list. */
901 }
902 }
1e620590 903
adc36818
PM
904 if (from_tty)
905 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
906 }
907 else
908 {
adc36818
PM
909 if (is_watchpoint (b))
910 {
699bd4cf 911 innermost_block_tracker tracker;
78319c15 912 const char *arg = exp;
4c55e970 913 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 914 if (*arg != 0)
adc36818 915 error (_("Junk at end of expression"));
78319c15 916 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 917 w->cond_exp = std::move (new_exp);
699bd4cf 918 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
919 }
920 else
921 {
4c55e970
TBA
922 /* Parse and set condition expressions. We make two passes.
923 In the first, we parse the condition string to see if it
b5fa468f
TBA
924 is valid in at least one location. If so, the condition
925 would be accepted. So we go ahead and set the locations'
926 conditions. In case no valid case is found, we throw
4c55e970
TBA
927 the error and the condition string will be rejected.
928 This two-pass approach is taken to avoid setting the
929 state of locations in case of a reject. */
40cb8ca5 930 for (bp_location *loc : b->locations ())
4c55e970 931 {
b5fa468f
TBA
932 try
933 {
934 const char *arg = exp;
935 parse_exp_1 (&arg, loc->address,
936 block_for_pc (loc->address), 0);
937 if (*arg != 0)
938 error (_("Junk at end of expression"));
939 break;
940 }
941 catch (const gdb_exception_error &e)
942 {
943 /* Condition string is invalid. If this happens to
733d554a
TBA
944 be the last loc, abandon (if not forced) or continue
945 (if forced). */
946 if (loc->next == nullptr && !force)
b5fa468f
TBA
947 throw;
948 }
4c55e970
TBA
949 }
950
b5fa468f 951 /* If we reach here, the condition is valid at some locations. */
dda83cd7 952 int loc_num = 1;
40cb8ca5
SM
953 for (bp_location *loc : b->locations ())
954 {
955 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
956 loc_num++;
957 }
adc36818 958 }
1e620590
TBA
959
960 /* We know that the new condition parsed successfully. The
961 condition string of the breakpoint can be safely updated. */
962 xfree (b->cond_string);
963 b->cond_string = xstrdup (exp);
964 b->condition_not_parsed = 0;
adc36818 965 }
b775012e
LM
966 mark_breakpoint_modified (b);
967
76727919 968 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
969}
970
79aabb73
TBA
971/* See breakpoint.h. */
972
973void
974set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
975 bool force)
976{
43892fdf 977 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
978 if (b->number == bpnum)
979 {
980 /* Check if this breakpoint has a "stop" method implemented in an
981 extension language. This method and conditions entered into GDB
982 from the CLI are mutually exclusive. */
983 const struct extension_language_defn *extlang
984 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
985
986 if (extlang != NULL)
987 {
988 error (_("Only one stop condition allowed. There is currently"
989 " a %s stop condition defined for this breakpoint."),
990 ext_lang_capitalized_name (extlang));
991 }
992 set_breakpoint_condition (b, exp, from_tty, force);
993
994 if (is_breakpoint (b))
995 update_global_location_list (UGLL_MAY_INSERT);
996
997 return;
998 }
999
1000 error (_("No breakpoint number %d."), bpnum);
1001}
1002
b1d4d8d1
TBA
1003/* The options for the "condition" command. */
1004
1005struct condition_command_opts
1006{
1007 /* For "-force". */
1008 bool force_condition = false;
1009};
1010
1011static const gdb::option::option_def condition_command_option_defs[] = {
1012
1013 gdb::option::flag_option_def<condition_command_opts> {
1014 "force",
1015 [] (condition_command_opts *opts) { return &opts->force_condition; },
1016 N_("Set the condition even if it is invalid for all current locations."),
1017 },
1018
1019};
1020
1021/* Create an option_def_group for the "condition" options, with
1022 CC_OPTS as context. */
1023
1024static inline gdb::option::option_def_group
1025make_condition_command_options_def_group (condition_command_opts *cc_opts)
1026{
1027 return {{condition_command_option_defs}, cc_opts};
1028}
1029
d55637df
TT
1030/* Completion for the "condition" command. */
1031
eb3ff9a5 1032static void
6f937416 1033condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1034 completion_tracker &tracker,
b1d4d8d1 1035 const char *text, const char * /*word*/)
d55637df 1036{
b1d4d8d1
TBA
1037 bool has_no_arguments = (*text == '\0');
1038 condition_command_opts cc_opts;
1039 const auto group = make_condition_command_options_def_group (&cc_opts);
1040 if (gdb::option::complete_options
1041 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1042 return;
d55637df 1043
f1735a53 1044 text = skip_spaces (text);
b1d4d8d1 1045 const char *space = skip_to_space (text);
d55637df
TT
1046 if (*space == '\0')
1047 {
1048 int len;
d55637df
TT
1049
1050 if (text[0] == '$')
1051 {
b1d4d8d1 1052 tracker.advance_custom_word_point_by (1);
d55637df 1053 /* We don't support completion of history indices. */
eb3ff9a5
PA
1054 if (!isdigit (text[1]))
1055 complete_internalvar (tracker, &text[1]);
1056 return;
d55637df
TT
1057 }
1058
b1d4d8d1
TBA
1059 /* Suggest the "-force" flag if no arguments are given. If
1060 arguments were passed, they either already include the flag,
1061 or we are beyond the point of suggesting it because it's
1062 positionally the first argument. */
1063 if (has_no_arguments)
1064 gdb::option::complete_on_all_options (tracker, group);
1065
d55637df
TT
1066 /* We're completing the breakpoint number. */
1067 len = strlen (text);
1068
43892fdf 1069 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1070 {
1071 char number[50];
1072
1073 xsnprintf (number, sizeof (number), "%d", b->number);
1074
1075 if (strncmp (number, text, len) == 0)
b02f78f9 1076 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1077 }
d55637df 1078
eb3ff9a5 1079 return;
d55637df
TT
1080 }
1081
b1d4d8d1
TBA
1082 /* We're completing the expression part. Skip the breakpoint num. */
1083 const char *exp_start = skip_spaces (space);
1084 tracker.advance_custom_word_point_by (exp_start - text);
1085 text = exp_start;
1086 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1087 expression_completer (cmd, tracker, text, word);
d55637df
TT
1088}
1089
c906108c
SS
1090/* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092static void
0b39b52e 1093condition_command (const char *arg, int from_tty)
c906108c 1094{
0b39b52e 1095 const char *p;
52f0bd74 1096 int bnum;
c906108c
SS
1097
1098 if (arg == 0)
e2e0b3e5 1099 error_no_arg (_("breakpoint number"));
c906108c
SS
1100
1101 p = arg;
733d554a
TBA
1102
1103 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1104 condition_command_opts cc_opts;
1105 const auto group = make_condition_command_options_def_group (&cc_opts);
1106 gdb::option::process_options
1107 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1108
c906108c 1109 bnum = get_number (&p);
5c44784c 1110 if (bnum == 0)
8a3fe4f8 1111 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1112
79aabb73 1113 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1114}
1115
a7bdde9e
VP
1116/* Check that COMMAND do not contain commands that are suitable
1117 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1118 Throw if any such commands is found. */
1119
a7bdde9e
VP
1120static void
1121check_no_tracepoint_commands (struct command_line *commands)
1122{
1123 struct command_line *c;
cc59ec59 1124
a7bdde9e
VP
1125 for (c = commands; c; c = c->next)
1126 {
a7bdde9e 1127 if (c->control_type == while_stepping_control)
3e43a32a
MS
1128 error (_("The 'while-stepping' command can "
1129 "only be used for tracepoints"));
a7bdde9e 1130
12973681
TT
1131 check_no_tracepoint_commands (c->body_list_0.get ());
1132 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1133
1134 /* Not that command parsing removes leading whitespace and comment
4a64f543 1135 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1136 command directly. */
1137 if (strstr (c->line, "collect ") == c->line)
1138 error (_("The 'collect' command can only be used for tracepoints"));
1139
51661e93
VP
1140 if (strstr (c->line, "teval ") == c->line)
1141 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1142 }
1143}
1144
c1fc2657 1145struct longjmp_breakpoint : public breakpoint
3b0871f4 1146{
c1fc2657 1147 ~longjmp_breakpoint () override;
3b0871f4
SM
1148};
1149
d77f58be
SS
1150/* Encapsulate tests for different types of tracepoints. */
1151
3b0871f4
SM
1152static bool
1153is_tracepoint_type (bptype type)
d9b3f62e
PA
1154{
1155 return (type == bp_tracepoint
1156 || type == bp_fast_tracepoint
1157 || type == bp_static_tracepoint);
1158}
1159
3b0871f4
SM
1160static bool
1161is_longjmp_type (bptype type)
1162{
1163 return type == bp_longjmp || type == bp_exception;
1164}
1165
f2478a7e
SM
1166/* See breakpoint.h. */
1167
1168bool
d77f58be 1169is_tracepoint (const struct breakpoint *b)
a7bdde9e 1170{
d9b3f62e 1171 return is_tracepoint_type (b->type);
a7bdde9e 1172}
d9b3f62e 1173
a5e364af
SM
1174/* Factory function to create an appropriate instance of breakpoint given
1175 TYPE. */
1176
1177static std::unique_ptr<breakpoint>
1178new_breakpoint_from_type (bptype type)
1179{
1180 breakpoint *b;
1181
1182 if (is_tracepoint_type (type))
c1fc2657 1183 b = new tracepoint ();
3b0871f4 1184 else if (is_longjmp_type (type))
c1fc2657 1185 b = new longjmp_breakpoint ();
a5e364af
SM
1186 else
1187 b = new breakpoint ();
1188
1189 return std::unique_ptr<breakpoint> (b);
1190}
1191
e5dd4106 1192/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1193 breakpoint. This function will throw an exception if a problem is
1194 found. */
48cb2d85 1195
95a42b64
TT
1196static void
1197validate_commands_for_breakpoint (struct breakpoint *b,
1198 struct command_line *commands)
48cb2d85 1199{
d77f58be 1200 if (is_tracepoint (b))
a7bdde9e 1201 {
c9a6ce02 1202 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1203 struct command_line *c;
1204 struct command_line *while_stepping = 0;
c9a6ce02
PA
1205
1206 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1207 might have included a while-stepping action, while the new
1208 ones might not. */
c9a6ce02
PA
1209 t->step_count = 0;
1210
1211 /* We need to verify that each top-level element of commands is
1212 valid for tracepoints, that there's at most one
1213 while-stepping element, and that the while-stepping's body
1214 has valid tracing commands excluding nested while-stepping.
1215 We also need to validate the tracepoint action line in the
1216 context of the tracepoint --- validate_actionline actually
1217 has side effects, like setting the tracepoint's
1218 while-stepping STEP_COUNT, in addition to checking if the
1219 collect/teval actions parse and make sense in the
1220 tracepoint's context. */
a7bdde9e
VP
1221 for (c = commands; c; c = c->next)
1222 {
a7bdde9e
VP
1223 if (c->control_type == while_stepping_control)
1224 {
1225 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1226 error (_("The 'while-stepping' command "
1227 "cannot be used for fast tracepoint"));
0fb4aa4b 1228 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1229 error (_("The 'while-stepping' command "
1230 "cannot be used for static tracepoint"));
a7bdde9e
VP
1231
1232 if (while_stepping)
3e43a32a
MS
1233 error (_("The 'while-stepping' command "
1234 "can be used only once"));
a7bdde9e
VP
1235 else
1236 while_stepping = c;
1237 }
c9a6ce02
PA
1238
1239 validate_actionline (c->line, b);
a7bdde9e
VP
1240 }
1241 if (while_stepping)
1242 {
1243 struct command_line *c2;
1244
12973681
TT
1245 gdb_assert (while_stepping->body_list_1 == nullptr);
1246 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1247 for (; c2; c2 = c2->next)
1248 {
a7bdde9e
VP
1249 if (c2->control_type == while_stepping_control)
1250 error (_("The 'while-stepping' command cannot be nested"));
1251 }
1252 }
1253 }
1254 else
1255 {
1256 check_no_tracepoint_commands (commands);
1257 }
95a42b64
TT
1258}
1259
0fb4aa4b
PA
1260/* Return a vector of all the static tracepoints set at ADDR. The
1261 caller is responsible for releasing the vector. */
1262
f51e0e20 1263std::vector<breakpoint *>
0fb4aa4b
PA
1264static_tracepoints_here (CORE_ADDR addr)
1265{
f51e0e20 1266 std::vector<breakpoint *> found;
0fb4aa4b 1267
43892fdf 1268 for (breakpoint *b : all_breakpoints ())
0fb4aa4b
PA
1269 if (b->type == bp_static_tracepoint)
1270 {
40cb8ca5 1271 for (bp_location *loc : b->locations ())
0fb4aa4b 1272 if (loc->address == addr)
f51e0e20 1273 found.push_back (b);
0fb4aa4b
PA
1274 }
1275
1276 return found;
1277}
1278
95a42b64 1279/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1280 validate that only allowed commands are included. */
95a42b64
TT
1281
1282void
4a64f543 1283breakpoint_set_commands (struct breakpoint *b,
12973681 1284 counted_command_line &&commands)
95a42b64 1285{
93921405 1286 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1287
d1b0a7bf 1288 b->commands = std::move (commands);
76727919 1289 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1290}
1291
45a43567
TT
1292/* Set the internal `silent' flag on the breakpoint. Note that this
1293 is not the same as the "silent" that may appear in the breakpoint's
1294 commands. */
1295
1296void
1297breakpoint_set_silent (struct breakpoint *b, int silent)
1298{
1299 int old_silent = b->silent;
1300
1301 b->silent = silent;
1302 if (old_silent != silent)
76727919 1303 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1304}
1305
1306/* Set the thread for this breakpoint. If THREAD is -1, make the
1307 breakpoint work for any thread. */
1308
1309void
1310breakpoint_set_thread (struct breakpoint *b, int thread)
1311{
1312 int old_thread = b->thread;
1313
1314 b->thread = thread;
1315 if (old_thread != thread)
76727919 1316 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1317}
1318
1319/* Set the task for this breakpoint. If TASK is 0, make the
1320 breakpoint work for any task. */
1321
1322void
1323breakpoint_set_task (struct breakpoint *b, int task)
1324{
1325 int old_task = b->task;
1326
1327 b->task = task;
1328 if (old_task != task)
76727919 1329 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1330}
1331
95a42b64 1332static void
896b6bda 1333commands_command_1 (const char *arg, int from_tty,
4a64f543 1334 struct command_line *control)
95a42b64 1335{
d1b0a7bf 1336 counted_command_line cmd;
999700cd
PW
1337 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1338 NULL after the call to read_command_lines if the user provides an empty
1339 list of command by just typing "end". */
1340 bool cmd_read = false;
95a42b64 1341
896b6bda
PA
1342 std::string new_arg;
1343
95a42b64
TT
1344 if (arg == NULL || !*arg)
1345 {
da1df1db 1346 /* Argument not explicitly given. Synthesize it. */
86b17b60 1347 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1348 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1349 breakpoint_count);
95a42b64 1350 else if (breakpoint_count > 0)
896b6bda 1351 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1352 }
da1df1db
TBA
1353 else
1354 {
1355 /* Create a copy of ARG. This is needed because the "commands"
1356 command may be coming from a script. In that case, the read
1357 line buffer is going to be overwritten in the lambda of
1358 'map_breakpoint_numbers' below when reading the next line
1359 before we are are done parsing the breakpoint numbers. */
1360 new_arg = arg;
1361 }
1362 arg = new_arg.c_str ();
48649e1b
TT
1363
1364 map_breakpoint_numbers
1365 (arg, [&] (breakpoint *b)
1366 {
999700cd 1367 if (!cmd_read)
48649e1b 1368 {
999700cd 1369 gdb_assert (cmd == NULL);
48649e1b 1370 if (control != NULL)
12973681 1371 cmd = control->body_list_0;
48649e1b
TT
1372 else
1373 {
81b1e71c
TT
1374 std::string str
1375 = string_printf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 arg);
48649e1b 1378
60b3cef2
TT
1379 auto do_validate = [=] (const char *line)
1380 {
1381 validate_actionline (line, b);
1382 };
1383 gdb::function_view<void (const char *)> validator;
1384 if (is_tracepoint (b))
1385 validator = do_validate;
1386
1387 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1388 }
999700cd 1389 cmd_read = true;
48649e1b
TT
1390 }
1391
1392 /* If a breakpoint was on the list more than once, we don't need to
1393 do anything. */
1394 if (b->commands != cmd)
1395 {
d1b0a7bf 1396 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1397 b->commands = cmd;
76727919 1398 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1399 }
1400 });
95a42b64
TT
1401}
1402
1403static void
0b39b52e 1404commands_command (const char *arg, int from_tty)
95a42b64
TT
1405{
1406 commands_command_1 (arg, from_tty, NULL);
c906108c 1407}
40c03ae8
EZ
1408
1409/* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414enum command_control_type
896b6bda 1415commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1416{
95a42b64
TT
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
40c03ae8 1419}
876fa593
JK
1420
1421/* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423static int
1424bp_location_has_shadow (struct bp_location *bl)
1425{
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
e5dd4106 1431 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1432 return 0;
1433 return 1;
1434}
1435
9d497a19
PA
1436/* Update BUF, which is LEN bytes read from the target address
1437 MEMADDR, by replacing a memory breakpoint with its shadowed
1438 contents.
1439
1440 If READBUF is not NULL, this buffer must not overlap with the of
1441 the breakpoint location's shadow_contents buffer. Otherwise, a
1442 failed assertion internal error will be raised. */
1443
1444static void
1445one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1446 const gdb_byte *writebuf_org,
1447 ULONGEST memaddr, LONGEST len,
1448 struct bp_target_info *target_info,
1449 struct gdbarch *gdbarch)
1450{
1451 /* Now do full processing of the found relevant range of elements. */
1452 CORE_ADDR bp_addr = 0;
1453 int bp_size = 0;
1454 int bptoffset = 0;
1455
1456 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1457 current_program_space->aspace, 0))
1458 {
1459 /* The breakpoint is inserted in a different address space. */
1460 return;
1461 }
1462
1463 /* Addresses and length of the part of the breakpoint that
1464 we need to copy. */
1465 bp_addr = target_info->placed_address;
1466 bp_size = target_info->shadow_len;
1467
1468 if (bp_addr + bp_size <= memaddr)
1469 {
1470 /* The breakpoint is entirely before the chunk of memory we are
1471 reading. */
1472 return;
1473 }
1474
1475 if (bp_addr >= memaddr + len)
1476 {
1477 /* The breakpoint is entirely after the chunk of memory we are
1478 reading. */
1479 return;
1480 }
1481
1482 /* Offset within shadow_contents. */
1483 if (bp_addr < memaddr)
1484 {
1485 /* Only copy the second part of the breakpoint. */
1486 bp_size -= memaddr - bp_addr;
1487 bptoffset = memaddr - bp_addr;
1488 bp_addr = memaddr;
1489 }
1490
1491 if (bp_addr + bp_size > memaddr + len)
1492 {
1493 /* Only copy the first part of the breakpoint. */
1494 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1495 }
1496
1497 if (readbuf != NULL)
1498 {
1499 /* Verify that the readbuf buffer does not overlap with the
1500 shadow_contents buffer. */
1501 gdb_assert (target_info->shadow_contents >= readbuf + len
1502 || readbuf >= (target_info->shadow_contents
1503 + target_info->shadow_len));
1504
1505 /* Update the read buffer with this inserted breakpoint's
1506 shadow. */
1507 memcpy (readbuf + bp_addr - memaddr,
1508 target_info->shadow_contents + bptoffset, bp_size);
1509 }
1510 else
1511 {
1512 const unsigned char *bp;
0d5ed153
MR
1513 CORE_ADDR addr = target_info->reqstd_address;
1514 int placed_size;
9d497a19
PA
1515
1516 /* Update the shadow with what we want to write to memory. */
1517 memcpy (target_info->shadow_contents + bptoffset,
1518 writebuf_org + bp_addr - memaddr, bp_size);
1519
1520 /* Determine appropriate breakpoint contents and size for this
1521 address. */
0d5ed153 1522 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1523
1524 /* Update the final write buffer with this inserted
1525 breakpoint's INSN. */
1526 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1527 }
1528}
1529
8defab1a 1530/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1531 by replacing any memory breakpoints with their shadowed contents.
1532
35c63cd8
JB
1533 If READBUF is not NULL, this buffer must not overlap with any of
1534 the breakpoint location's shadow_contents buffers. Otherwise,
1535 a failed assertion internal error will be raised.
1536
876fa593 1537 The range of shadowed area by each bp_location is:
f5336ca5
PA
1538 bl->address - bp_locations_placed_address_before_address_max
1539 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1540 The range we were requested to resolve shadows for is:
1541 memaddr ... memaddr + len
1542 Thus the safe cutoff boundaries for performance optimization are
35df4500 1543 memaddr + len <= (bl->address
f5336ca5 1544 - bp_locations_placed_address_before_address_max)
876fa593 1545 and:
f5336ca5 1546 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1547
8defab1a 1548void
f0ba3972
PA
1549breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1550 const gdb_byte *writebuf_org,
1551 ULONGEST memaddr, LONGEST len)
c906108c 1552{
4a64f543
MS
1553 /* Left boundary, right boundary and median element of our binary
1554 search. */
876fa593
JK
1555 unsigned bc_l, bc_r, bc;
1556
4a64f543
MS
1557 /* Find BC_L which is a leftmost element which may affect BUF
1558 content. It is safe to report lower value but a failure to
1559 report higher one. */
876fa593
JK
1560
1561 bc_l = 0;
5d51cd5d 1562 bc_r = bp_locations.size ();
876fa593
JK
1563 while (bc_l + 1 < bc_r)
1564 {
35df4500 1565 struct bp_location *bl;
876fa593
JK
1566
1567 bc = (bc_l + bc_r) / 2;
f5336ca5 1568 bl = bp_locations[bc];
876fa593 1569
4a64f543
MS
1570 /* Check first BL->ADDRESS will not overflow due to the added
1571 constant. Then advance the left boundary only if we are sure
1572 the BC element can in no way affect the BUF content (MEMADDR
1573 to MEMADDR + LEN range).
876fa593 1574
f5336ca5 1575 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1576 offset so that we cannot miss a breakpoint with its shadow
1577 range tail still reaching MEMADDR. */
c5aa993b 1578
f5336ca5 1579 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1580 >= bl->address)
f5336ca5 1581 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1582 <= memaddr))
876fa593
JK
1583 bc_l = bc;
1584 else
1585 bc_r = bc;
1586 }
1587
128070bb
PA
1588 /* Due to the binary search above, we need to make sure we pick the
1589 first location that's at BC_L's address. E.g., if there are
1590 multiple locations at the same address, BC_L may end up pointing
1591 at a duplicate location, and miss the "master"/"inserted"
1592 location. Say, given locations L1, L2 and L3 at addresses A and
1593 B:
1594
1595 L1@A, L2@A, L3@B, ...
1596
1597 BC_L could end up pointing at location L2, while the "master"
1598 location could be L1. Since the `loc->inserted' flag is only set
1599 on "master" locations, we'd forget to restore the shadow of L1
1600 and L2. */
1601 while (bc_l > 0
f5336ca5 1602 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1603 bc_l--;
1604
876fa593
JK
1605 /* Now do full processing of the found relevant range of elements. */
1606
5d51cd5d 1607 for (bc = bc_l; bc < bp_locations.size (); bc++)
01add95b
SM
1608 {
1609 struct bp_location *bl = bp_locations[bc];
876fa593 1610
01add95b
SM
1611 /* bp_location array has BL->OWNER always non-NULL. */
1612 if (bl->owner->type == bp_none)
1613 warning (_("reading through apparently deleted breakpoint #%d?"),
1614 bl->owner->number);
ffce0d52 1615
01add95b
SM
1616 /* Performance optimization: any further element can no longer affect BUF
1617 content. */
876fa593 1618
01add95b
SM
1619 if (bl->address >= bp_locations_placed_address_before_address_max
1620 && (memaddr + len
1621 <= (bl->address
1622 - bp_locations_placed_address_before_address_max)))
1623 break;
876fa593 1624
01add95b
SM
1625 if (!bp_location_has_shadow (bl))
1626 continue;
6c95b8df 1627
01add95b
SM
1628 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1629 memaddr, len, &bl->target_info, bl->gdbarch);
1630 }
c906108c 1631}
9d497a19 1632
f2478a7e 1633/* See breakpoint.h. */
b775012e 1634
f2478a7e 1635bool
b775012e
LM
1636is_breakpoint (const struct breakpoint *bpt)
1637{
1638 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1639 || bpt->type == bp_hardware_breakpoint
1640 || bpt->type == bp_dprintf);
b775012e
LM
1641}
1642
60e1c644
PA
1643/* Return true if BPT is of any hardware watchpoint kind. */
1644
f2478a7e 1645static bool
d77f58be 1646is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1647{
1648 return (bpt->type == bp_hardware_watchpoint
1649 || bpt->type == bp_read_watchpoint
1650 || bpt->type == bp_access_watchpoint);
1651}
7270d8f2 1652
f2478a7e 1653/* See breakpoint.h. */
60e1c644 1654
f2478a7e 1655bool
d77f58be 1656is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1657{
1658 return (is_hardware_watchpoint (bpt)
1659 || bpt->type == bp_watchpoint);
1660}
1661
3a5c3e22
PA
1662/* Returns true if the current thread and its running state are safe
1663 to evaluate or update watchpoint B. Watchpoints on local
1664 expressions need to be evaluated in the context of the thread that
1665 was current when the watchpoint was created, and, that thread needs
1666 to be stopped to be able to select the correct frame context.
1667 Watchpoints on global expressions can be evaluated on any thread,
1668 and in any state. It is presently left to the target allowing
1669 memory accesses when threads are running. */
f6bc2008
PA
1670
1671static int
3a5c3e22 1672watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1673{
c1fc2657 1674 return (b->pspace == current_program_space
d7e15655
TT
1675 && (b->watchpoint_thread == null_ptid
1676 || (inferior_ptid == b->watchpoint_thread
00431a78 1677 && !inferior_thread ()->executing)));
f6bc2008
PA
1678}
1679
d0fb5eae
JK
1680/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1681 associated bp_watchpoint_scope breakpoint. */
1682
1683static void
3a5c3e22 1684watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1685{
c1fc2657 1686 if (w->related_breakpoint != w)
d0fb5eae 1687 {
c1fc2657
SM
1688 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1689 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1690 w->related_breakpoint->disposition = disp_del_at_next_stop;
1691 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1692 w->related_breakpoint = w;
d0fb5eae 1693 }
c1fc2657 1694 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1695}
1696
bb9d5f81
PP
1697/* Extract a bitfield value from value VAL using the bit parameters contained in
1698 watchpoint W. */
1699
1700static struct value *
1701extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1702{
1703 struct value *bit_val;
1704
1705 if (val == NULL)
1706 return NULL;
1707
1708 bit_val = allocate_value (value_type (val));
1709
1710 unpack_value_bitfield (bit_val,
1711 w->val_bitpos,
1712 w->val_bitsize,
1713 value_contents_for_printing (val),
1714 value_offset (val),
1715 val);
1716
1717 return bit_val;
1718}
1719
c6d81124
PA
1720/* Allocate a dummy location and add it to B, which must be a software
1721 watchpoint. This is required because even if a software watchpoint
1722 is not watching any memory, bpstat_stop_status requires a location
1723 to be able to report stops. */
1724
1725static void
1726software_watchpoint_add_no_memory_location (struct breakpoint *b,
1727 struct program_space *pspace)
1728{
1729 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1730
1731 b->loc = allocate_bp_location (b);
1732 b->loc->pspace = pspace;
1733 b->loc->address = -1;
1734 b->loc->length = -1;
1735}
1736
1737/* Returns true if B is a software watchpoint that is not watching any
1738 memory (e.g., "watch $pc"). */
1739
f2478a7e 1740static bool
c6d81124
PA
1741is_no_memory_software_watchpoint (struct breakpoint *b)
1742{
1743 return (b->type == bp_watchpoint
1744 && b->loc != NULL
1745 && b->loc->next == NULL
1746 && b->loc->address == -1
1747 && b->loc->length == -1);
1748}
1749
567e1b4e
JB
1750/* Assuming that B is a watchpoint:
1751 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1752 - Evaluate expression and store the result in B->val
567e1b4e
JB
1753 - Evaluate the condition if there is one, and store the result
1754 in b->loc->cond.
a5606eee
VP
1755 - Update the list of values that must be watched in B->loc.
1756
4a64f543
MS
1757 If the watchpoint disposition is disp_del_at_next_stop, then do
1758 nothing. If this is local watchpoint that is out of scope, delete
1759 it.
1760
1761 Even with `set breakpoint always-inserted on' the watchpoints are
1762 removed + inserted on each stop here. Normal breakpoints must
1763 never be removed because they might be missed by a running thread
1764 when debugging in non-stop mode. On the other hand, hardware
1765 watchpoints (is_hardware_watchpoint; processed here) are specific
1766 to each LWP since they are stored in each LWP's hardware debug
1767 registers. Therefore, such LWP must be stopped first in order to
1768 be able to modify its hardware watchpoints.
1769
1770 Hardware watchpoints must be reset exactly once after being
1771 presented to the user. It cannot be done sooner, because it would
1772 reset the data used to present the watchpoint hit to the user. And
1773 it must not be done later because it could display the same single
1774 watchpoint hit during multiple GDB stops. Note that the latter is
1775 relevant only to the hardware watchpoint types bp_read_watchpoint
1776 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1777 not user-visible - its hit is suppressed if the memory content has
1778 not changed.
1779
1780 The following constraints influence the location where we can reset
1781 hardware watchpoints:
1782
1783 * target_stopped_by_watchpoint and target_stopped_data_address are
1784 called several times when GDB stops.
1785
1786 [linux]
1787 * Multiple hardware watchpoints can be hit at the same time,
1788 causing GDB to stop. GDB only presents one hardware watchpoint
1789 hit at a time as the reason for stopping, and all the other hits
1790 are presented later, one after the other, each time the user
1791 requests the execution to be resumed. Execution is not resumed
1792 for the threads still having pending hit event stored in
1793 LWP_INFO->STATUS. While the watchpoint is already removed from
1794 the inferior on the first stop the thread hit event is kept being
1795 reported from its cached value by linux_nat_stopped_data_address
1796 until the real thread resume happens after the watchpoint gets
1797 presented and thus its LWP_INFO->STATUS gets reset.
1798
1799 Therefore the hardware watchpoint hit can get safely reset on the
1800 watchpoint removal from inferior. */
a79d3c27 1801
b40ce68a 1802static void
3a5c3e22 1803update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1804{
a5606eee 1805 int within_current_scope;
a5606eee 1806 struct frame_id saved_frame_id;
66076460 1807 int frame_saved;
a5606eee 1808
f6bc2008
PA
1809 /* If this is a local watchpoint, we only want to check if the
1810 watchpoint frame is in scope if the current thread is the thread
1811 that was used to create the watchpoint. */
1812 if (!watchpoint_in_thread_scope (b))
1813 return;
1814
c1fc2657 1815 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1816 return;
1817
66076460 1818 frame_saved = 0;
a5606eee
VP
1819
1820 /* Determine if the watchpoint is within scope. */
1821 if (b->exp_valid_block == NULL)
1822 within_current_scope = 1;
1823 else
1824 {
b5db5dfc
UW
1825 struct frame_info *fi = get_current_frame ();
1826 struct gdbarch *frame_arch = get_frame_arch (fi);
1827 CORE_ADDR frame_pc = get_frame_pc (fi);
1828
c9cf6e20
MG
1829 /* If we're at a point where the stack has been destroyed
1830 (e.g. in a function epilogue), unwinding may not work
1831 properly. Do not attempt to recreate locations at this
b5db5dfc 1832 point. See similar comments in watchpoint_check. */
c9cf6e20 1833 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1834 return;
66076460
DJ
1835
1836 /* Save the current frame's ID so we can restore it after
dda83cd7 1837 evaluating the watchpoint expression on its own frame. */
66076460 1838 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1839 took a frame parameter, so that we didn't have to change the
1840 selected frame. */
66076460
DJ
1841 frame_saved = 1;
1842 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1843
a5606eee
VP
1844 fi = frame_find_by_id (b->watchpoint_frame);
1845 within_current_scope = (fi != NULL);
1846 if (within_current_scope)
1847 select_frame (fi);
1848 }
1849
b5db5dfc
UW
1850 /* We don't free locations. They are stored in the bp_location array
1851 and update_global_location_list will eventually delete them and
1852 remove breakpoints if needed. */
c1fc2657 1853 b->loc = NULL;
b5db5dfc 1854
a5606eee
VP
1855 if (within_current_scope && reparse)
1856 {
bbc13ae3 1857 const char *s;
d63d0675 1858
4d01a485 1859 b->exp.reset ();
d63d0675 1860 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1861 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1862 /* If the meaning of expression itself changed, the old value is
1863 no longer relevant. We don't want to report a watchpoint hit
1864 to the user when the old value and the new value may actually
1865 be completely different objects. */
fa4727a6 1866 b->val = NULL;
4c1d86d9 1867 b->val_valid = false;
60e1c644
PA
1868
1869 /* Note that unlike with breakpoints, the watchpoint's condition
1870 expression is stored in the breakpoint object, not in the
1871 locations (re)created below. */
c1fc2657 1872 if (b->cond_string != NULL)
60e1c644 1873 {
4d01a485 1874 b->cond_exp.reset ();
60e1c644 1875
c1fc2657 1876 s = b->cond_string;
1bb9788d 1877 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1878 }
a5606eee 1879 }
a5606eee
VP
1880
1881 /* If we failed to parse the expression, for example because
1882 it refers to a global variable in a not-yet-loaded shared library,
1883 don't try to insert watchpoint. We don't automatically delete
1884 such watchpoint, though, since failure to parse expression
1885 is different from out-of-scope watchpoint. */
55f6301a 1886 if (!target_has_execution ())
2d134ed3
PA
1887 {
1888 /* Without execution, memory can't change. No use to try and
1889 set watchpoint locations. The watchpoint will be reset when
1890 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1891 if (!can_use_hw_watchpoints)
1892 {
c1fc2657
SM
1893 if (b->ops->works_in_software_mode (b))
1894 b->type = bp_watchpoint;
e8369a73 1895 else
638aa5a1
AB
1896 error (_("Can't set read/access watchpoint when "
1897 "hardware watchpoints are disabled."));
e8369a73 1898 }
2d134ed3
PA
1899 }
1900 else if (within_current_scope && b->exp)
a5606eee 1901 {
a6535de1 1902 std::vector<value_ref_ptr> val_chain;
8d49165d 1903 struct value *v, *result;
2d134ed3 1904 struct program_space *frame_pspace;
a5606eee 1905
1eaebe02 1906 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 1907 &val_chain, false);
a5606eee 1908
a5606eee
VP
1909 /* Avoid setting b->val if it's already set. The meaning of
1910 b->val is 'the last value' user saw, and we should update
1911 it only if we reported that last value to user. As it
9c06b0b4
TJB
1912 happens, the code that reports it updates b->val directly.
1913 We don't keep track of the memory value for masked
1914 watchpoints. */
c1fc2657 1915 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1916 {
bb9d5f81 1917 if (b->val_bitsize != 0)
850645cf
TT
1918 v = extract_bitfield_from_watchpoint_value (b, v);
1919 b->val = release_value (v);
4c1d86d9 1920 b->val_valid = true;
fa4727a6 1921 }
a5606eee 1922
2d134ed3
PA
1923 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1924
a5606eee 1925 /* Look at each value on the value chain. */
a6535de1
TT
1926 gdb_assert (!val_chain.empty ());
1927 for (const value_ref_ptr &iter : val_chain)
a5606eee 1928 {
a6535de1
TT
1929 v = iter.get ();
1930
a5606eee
VP
1931 /* If it's a memory location, and GDB actually needed
1932 its contents to evaluate the expression, then we
fa4727a6
DJ
1933 must watch it. If the first value returned is
1934 still lazy, that means an error occurred reading it;
1935 watch it anyway in case it becomes readable. */
a5606eee 1936 if (VALUE_LVAL (v) == lval_memory
a6535de1 1937 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1938 {
1939 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1940
a5606eee
VP
1941 /* We only watch structs and arrays if user asked
1942 for it explicitly, never if they just happen to
1943 appear in the middle of some value chain. */
fa4727a6 1944 if (v == result
78134374
SM
1945 || (vtype->code () != TYPE_CODE_STRUCT
1946 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1947 {
1948 CORE_ADDR addr;
f486487f 1949 enum target_hw_bp_type type;
a5606eee 1950 struct bp_location *loc, **tmp;
bb9d5f81
PP
1951 int bitpos = 0, bitsize = 0;
1952
1953 if (value_bitsize (v) != 0)
1954 {
1955 /* Extract the bit parameters out from the bitfield
1956 sub-expression. */
1957 bitpos = value_bitpos (v);
1958 bitsize = value_bitsize (v);
1959 }
1960 else if (v == result && b->val_bitsize != 0)
1961 {
1962 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1963 lvalue whose bit parameters are saved in the fields
1964 VAL_BITPOS and VAL_BITSIZE. */
1965 bitpos = b->val_bitpos;
1966 bitsize = b->val_bitsize;
1967 }
a5606eee 1968
42ae5230 1969 addr = value_address (v);
bb9d5f81
PP
1970 if (bitsize != 0)
1971 {
1972 /* Skip the bytes that don't contain the bitfield. */
1973 addr += bitpos / 8;
1974 }
1975
a5606eee 1976 type = hw_write;
c1fc2657 1977 if (b->type == bp_read_watchpoint)
a5606eee 1978 type = hw_read;
c1fc2657 1979 else if (b->type == bp_access_watchpoint)
a5606eee 1980 type = hw_access;
3a5c3e22 1981
c1fc2657
SM
1982 loc = allocate_bp_location (b);
1983 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1984 ;
1985 *tmp = loc;
8ee511af 1986 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
1987
1988 loc->pspace = frame_pspace;
f17d9474 1989 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1990
1991 if (bitsize != 0)
1992 {
1993 /* Just cover the bytes that make up the bitfield. */
1994 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1995 }
1996 else
1997 loc->length = TYPE_LENGTH (value_type (v));
1998
a5606eee
VP
1999 loc->watchpoint_type = type;
2000 }
2001 }
9fa40276
TJB
2002 }
2003
2004 /* Change the type of breakpoint between hardware assisted or
2005 an ordinary watchpoint depending on the hardware support
2006 and free hardware slots. REPARSE is set when the inferior
2007 is started. */
a9634178 2008 if (reparse)
9fa40276 2009 {
e09342b5 2010 int reg_cnt;
9fa40276 2011 enum bp_loc_type loc_type;
a5606eee 2012
a9634178 2013 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2014
2015 if (reg_cnt)
9fa40276
TJB
2016 {
2017 int i, target_resources_ok, other_type_used;
a1398e0c 2018 enum bptype type;
9fa40276 2019
a9634178
TJB
2020 /* Use an exact watchpoint when there's only one memory region to be
2021 watched, and only one debug register is needed to watch it. */
2022 b->exact = target_exact_watchpoints && reg_cnt == 1;
2023
9fa40276 2024 /* We need to determine how many resources are already
e09342b5
TJB
2025 used for all other hardware watchpoints plus this one
2026 to see if we still have enough resources to also fit
a1398e0c
PA
2027 this watchpoint in as well. */
2028
2029 /* If this is a software watchpoint, we try to turn it
2030 to a hardware one -- count resources as if B was of
2031 hardware watchpoint type. */
c1fc2657 2032 type = b->type;
a1398e0c
PA
2033 if (type == bp_watchpoint)
2034 type = bp_hardware_watchpoint;
2035
2036 /* This watchpoint may or may not have been placed on
2037 the list yet at this point (it won't be in the list
2038 if we're trying to create it for the first time,
2039 through watch_command), so always account for it
2040 manually. */
2041
2042 /* Count resources used by all watchpoints except B. */
c1fc2657 2043 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2044
2045 /* Add in the resources needed for B. */
c1fc2657 2046 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2047
2048 target_resources_ok
2049 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2050 if (target_resources_ok <= 0)
a9634178 2051 {
c1fc2657 2052 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2053
2054 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2055 error (_("Target does not support this type of "
2056 "hardware watchpoint."));
9c06b0b4
TJB
2057 else if (target_resources_ok < 0 && !sw_mode)
2058 error (_("There are not enough available hardware "
2059 "resources for this watchpoint."));
a1398e0c
PA
2060
2061 /* Downgrade to software watchpoint. */
c1fc2657 2062 b->type = bp_watchpoint;
a1398e0c
PA
2063 }
2064 else
2065 {
2066 /* If this was a software watchpoint, we've just
2067 found we have enough resources to turn it to a
2068 hardware watchpoint. Otherwise, this is a
2069 nop. */
c1fc2657 2070 b->type = type;
a9634178 2071 }
9fa40276 2072 }
c1fc2657 2073 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2074 {
2075 if (!can_use_hw_watchpoints)
2076 error (_("Can't set read/access watchpoint when "
2077 "hardware watchpoints are disabled."));
2078 else
2079 error (_("Expression cannot be implemented with "
2080 "read/access watchpoint."));
2081 }
9fa40276 2082 else
c1fc2657 2083 b->type = bp_watchpoint;
9fa40276 2084
c1fc2657 2085 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2086 : bp_loc_hardware_watchpoint);
40cb8ca5 2087 for (bp_location *bl : b->locations ())
9fa40276
TJB
2088 bl->loc_type = loc_type;
2089 }
2090
c7437ca6
PA
2091 /* If a software watchpoint is not watching any memory, then the
2092 above left it without any location set up. But,
2093 bpstat_stop_status requires a location to be able to report
2094 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2095 if (b->type == bp_watchpoint && b->loc == NULL)
2096 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2097 }
2098 else if (!within_current_scope)
7270d8f2 2099 {
ac74f770
MS
2100 printf_filtered (_("\
2101Watchpoint %d deleted because the program has left the block\n\
2102in which its expression is valid.\n"),
c1fc2657 2103 b->number);
d0fb5eae 2104 watchpoint_del_at_next_stop (b);
7270d8f2 2105 }
a5606eee
VP
2106
2107 /* Restore the selected frame. */
66076460
DJ
2108 if (frame_saved)
2109 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2110}
2111
a5606eee 2112
74960c60 2113/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2114 inserted in the inferior. We don't differentiate the type of BL's owner
2115 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2116 breakpoint_ops is not defined, because in insert_bp_location,
2117 tracepoint's insert_location will not be called. */
74960c60 2118static int
35df4500 2119should_be_inserted (struct bp_location *bl)
74960c60 2120{
35df4500 2121 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2122 return 0;
2123
35df4500 2124 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2125 return 0;
2126
b5fa468f
TBA
2127 if (!bl->enabled || bl->disabled_by_cond
2128 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2129 return 0;
2130
f8eba3c6
TT
2131 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2132 return 0;
2133
56710373
PA
2134 /* This is set for example, when we're attached to the parent of a
2135 vfork, and have detached from the child. The child is running
2136 free, and we expect it to do an exec or exit, at which point the
2137 OS makes the parent schedulable again (and the target reports
2138 that the vfork is done). Until the child is done with the shared
2139 memory region, do not insert breakpoints in the parent, otherwise
2140 the child could still trip on the parent's breakpoints. Since
2141 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2142 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2143 return 0;
2144
31e77af2 2145 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2146 location, except if the breakpoint is a single-step breakpoint,
2147 and the breakpoint's thread is the thread which is stepping past
2148 a breakpoint. */
31e77af2
PA
2149 if ((bl->loc_type == bp_loc_software_breakpoint
2150 || bl->loc_type == bp_loc_hardware_breakpoint)
2151 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2152 bl->address)
2153 /* The single-step breakpoint may be inserted at the location
2154 we're trying to step if the instruction branches to itself.
2155 However, the instruction won't be executed at all and it may
2156 break the semantics of the instruction, for example, the
2157 instruction is a conditional branch or updates some flags.
2158 We can't fix it unless GDB is able to emulate the instruction
2159 or switch to displaced stepping. */
2160 && !(bl->owner->type == bp_single_step
2161 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2162 {
1eb8556f
SM
2163 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2164 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2165 return 0;
2166 }
31e77af2 2167
963f9c80
PA
2168 /* Don't insert watchpoints if we're trying to step past the
2169 instruction that triggered one. */
2170 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2171 && stepping_past_nonsteppable_watchpoint ())
2172 {
1eb8556f 2173 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2174 "skipping watchpoint at %s:%d",
1eb8556f 2175 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2176 return 0;
2177 }
2178
74960c60
VP
2179 return 1;
2180}
2181
934709f0
PW
2182/* Same as should_be_inserted but does the check assuming
2183 that the location is not duplicated. */
2184
2185static int
2186unduplicated_should_be_inserted (struct bp_location *bl)
2187{
2188 int result;
2189 const int save_duplicate = bl->duplicate;
2190
2191 bl->duplicate = 0;
2192 result = should_be_inserted (bl);
2193 bl->duplicate = save_duplicate;
2194 return result;
2195}
2196
b775012e
LM
2197/* Parses a conditional described by an expression COND into an
2198 agent expression bytecode suitable for evaluation
2199 by the bytecode interpreter. Return NULL if there was
2200 any error during parsing. */
2201
833177a4 2202static agent_expr_up
b775012e
LM
2203parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2204{
833177a4 2205 if (cond == NULL)
b775012e
LM
2206 return NULL;
2207
833177a4
PA
2208 agent_expr_up aexpr;
2209
b775012e
LM
2210 /* We don't want to stop processing, so catch any errors
2211 that may show up. */
a70b8144 2212 try
b775012e 2213 {
036e657b 2214 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2215 }
2216
230d2906 2217 catch (const gdb_exception_error &ex)
b775012e
LM
2218 {
2219 /* If we got here, it means the condition could not be parsed to a valid
2220 bytecode expression and thus can't be evaluated on the target's side.
2221 It's no use iterating through the conditions. */
b775012e
LM
2222 }
2223
2224 /* We have a valid agent expression. */
2225 return aexpr;
2226}
2227
2228/* Based on location BL, create a list of breakpoint conditions to be
2229 passed on to the target. If we have duplicated locations with different
2230 conditions, we will add such conditions to the list. The idea is that the
2231 target will evaluate the list of conditions and will only notify GDB when
2232 one of them is true. */
2233
2234static void
2235build_target_condition_list (struct bp_location *bl)
2236{
b775012e
LM
2237 int null_condition_or_parse_error = 0;
2238 int modified = bl->needs_update;
b775012e 2239
8b4f3082 2240 /* Release conditions left over from a previous insert. */
3cde5c42 2241 bl->target_info.conditions.clear ();
8b4f3082 2242
b775012e
LM
2243 /* This is only meaningful if the target is
2244 evaluating conditions and if the user has
2245 opted for condition evaluation on the target's
2246 side. */
2247 if (gdb_evaluates_breakpoint_condition_p ()
2248 || !target_supports_evaluation_of_breakpoint_conditions ())
2249 return;
2250
e0d9a270
SM
2251 auto loc_range = all_bp_locations_at_addr (bl->address);
2252
b775012e 2253 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2254 conditions or conditions that fail to parse to a valid agent
2255 expression bytecode. If any of these happen, then it's no use to
2256 send conditions to the target since this location will always
2257 trigger and generate a response back to GDB. Note we consider
2258 all locations at the same address irrespective of type, i.e.,
2259 even if the locations aren't considered duplicates (e.g.,
2260 software breakpoint and hardware breakpoint at the same
2261 address). */
e0d9a270 2262 for (bp_location *loc : loc_range)
b775012e 2263 {
b775012e
LM
2264 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2265 {
2266 if (modified)
2267 {
b775012e
LM
2268 /* Re-parse the conditions since something changed. In that
2269 case we already freed the condition bytecodes (see
2270 force_breakpoint_reinsertion). We just
2271 need to parse the condition to bytecodes again. */
833177a4
PA
2272 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2273 loc->cond.get ());
b775012e
LM
2274 }
2275
2276 /* If we have a NULL bytecode expression, it means something
2277 went wrong or we have a null condition expression. */
2278 if (!loc->cond_bytecode)
2279 {
2280 null_condition_or_parse_error = 1;
2281 break;
2282 }
2283 }
2284 }
2285
2286 /* If any of these happened, it means we will have to evaluate the conditions
2287 for the location's address on gdb's side. It is no use keeping bytecodes
2288 for all the other duplicate locations, thus we free all of them here.
2289
2290 This is so we have a finer control over which locations' conditions are
2291 being evaluated by GDB or the remote stub. */
2292 if (null_condition_or_parse_error)
2293 {
e0d9a270 2294 for (bp_location *loc : loc_range)
b775012e 2295 {
b775012e
LM
2296 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2297 {
2298 /* Only go as far as the first NULL bytecode is
2299 located. */
2300 if (!loc->cond_bytecode)
2301 return;
2302
833177a4 2303 loc->cond_bytecode.reset ();
b775012e
LM
2304 }
2305 }
2306 }
2307
7f32a4d5
PA
2308 /* No NULL conditions or failed bytecode generation. Build a
2309 condition list for this location's address. If we have software
2310 and hardware locations at the same address, they aren't
2311 considered duplicates, but we still marge all the conditions
2312 anyway, as it's simpler, and doesn't really make a practical
2313 difference. */
e0d9a270
SM
2314 for (bp_location *loc : loc_range)
2315 if (loc->cond
2316 && is_breakpoint (loc->owner)
2317 && loc->pspace->num == bl->pspace->num
2318 && loc->owner->enable_state == bp_enabled
2319 && loc->enabled
2320 && !loc->disabled_by_cond)
2321 {
2322 /* Add the condition to the vector. This will be used later
2323 to send the conditions to the target. */
2324 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2325 }
b775012e
LM
2326
2327 return;
2328}
2329
d3ce09f5
SS
2330/* Parses a command described by string CMD into an agent expression
2331 bytecode suitable for evaluation by the bytecode interpreter.
2332 Return NULL if there was any error during parsing. */
2333
833177a4 2334static agent_expr_up
d3ce09f5
SS
2335parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2336{
bbc13ae3
KS
2337 const char *cmdrest;
2338 const char *format_start, *format_end;
d3ce09f5
SS
2339 struct gdbarch *gdbarch = get_current_arch ();
2340
833177a4 2341 if (cmd == NULL)
d3ce09f5
SS
2342 return NULL;
2343
2344 cmdrest = cmd;
2345
2346 if (*cmdrest == ',')
2347 ++cmdrest;
f1735a53 2348 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2349
2350 if (*cmdrest++ != '"')
2351 error (_("No format string following the location"));
2352
2353 format_start = cmdrest;
2354
8e481c3b 2355 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2356
2357 format_end = cmdrest;
2358
2359 if (*cmdrest++ != '"')
2360 error (_("Bad format string, non-terminated '\"'."));
2361
f1735a53 2362 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2363
2364 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2365 error (_("Invalid argument syntax"));
2366
2367 if (*cmdrest == ',')
2368 cmdrest++;
f1735a53 2369 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2370
2371 /* For each argument, make an expression. */
2372
8e481c3b 2373 std::vector<struct expression *> argvec;
d3ce09f5
SS
2374 while (*cmdrest != '\0')
2375 {
bbc13ae3 2376 const char *cmd1;
d3ce09f5
SS
2377
2378 cmd1 = cmdrest;
4d01a485 2379 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2380 argvec.push_back (expr.release ());
d3ce09f5
SS
2381 cmdrest = cmd1;
2382 if (*cmdrest == ',')
2383 ++cmdrest;
2384 }
2385
833177a4
PA
2386 agent_expr_up aexpr;
2387
d3ce09f5
SS
2388 /* We don't want to stop processing, so catch any errors
2389 that may show up. */
a70b8144 2390 try
d3ce09f5 2391 {
036e657b
JB
2392 aexpr = gen_printf (scope, gdbarch, 0, 0,
2393 format_start, format_end - format_start,
8e481c3b 2394 argvec.size (), argvec.data ());
d3ce09f5 2395 }
230d2906 2396 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2397 {
2398 /* If we got here, it means the command could not be parsed to a valid
2399 bytecode expression and thus can't be evaluated on the target's side.
2400 It's no use iterating through the other commands. */
d3ce09f5 2401 }
492d29ea 2402
d3ce09f5
SS
2403 /* We have a valid agent expression, return it. */
2404 return aexpr;
2405}
2406
2407/* Based on location BL, create a list of breakpoint commands to be
2408 passed on to the target. If we have duplicated locations with
2409 different commands, we will add any such to the list. */
2410
2411static void
2412build_target_command_list (struct bp_location *bl)
2413{
d3ce09f5
SS
2414 int null_command_or_parse_error = 0;
2415 int modified = bl->needs_update;
d3ce09f5 2416
3cde5c42
PA
2417 /* Clear commands left over from a previous insert. */
2418 bl->target_info.tcommands.clear ();
8b4f3082 2419
41fac0cf 2420 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2421 return;
2422
41fac0cf
PA
2423 /* For now, limit to agent-style dprintf breakpoints. */
2424 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2425 return;
2426
e0d9a270
SM
2427 auto loc_range = all_bp_locations_at_addr (bl->address);
2428
7f32a4d5
PA
2429 /* For now, if we have any location at the same address that isn't a
2430 dprintf, don't install the target-side commands, as that would
2431 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2432 control. */
e0d9a270
SM
2433 for (bp_location *loc : loc_range)
2434 if (is_breakpoint (loc->owner)
2435 && loc->pspace->num == bl->pspace->num
2436 && loc->owner->type != bp_dprintf)
2437 return;
41fac0cf 2438
d3ce09f5
SS
2439 /* Do a first pass to check for locations with no assigned
2440 conditions or conditions that fail to parse to a valid agent expression
2441 bytecode. If any of these happen, then it's no use to send conditions
2442 to the target since this location will always trigger and generate a
2443 response back to GDB. */
e0d9a270 2444 for (bp_location *loc : loc_range)
d3ce09f5 2445 {
d3ce09f5
SS
2446 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2447 {
2448 if (modified)
2449 {
d3ce09f5
SS
2450 /* Re-parse the commands since something changed. In that
2451 case we already freed the command bytecodes (see
2452 force_breakpoint_reinsertion). We just
2453 need to parse the command to bytecodes again. */
833177a4
PA
2454 loc->cmd_bytecode
2455 = parse_cmd_to_aexpr (bl->address,
2456 loc->owner->extra_string);
d3ce09f5
SS
2457 }
2458
2459 /* If we have a NULL bytecode expression, it means something
2460 went wrong or we have a null command expression. */
2461 if (!loc->cmd_bytecode)
2462 {
2463 null_command_or_parse_error = 1;
2464 break;
2465 }
2466 }
2467 }
2468
2469 /* If anything failed, then we're not doing target-side commands,
2470 and so clean up. */
2471 if (null_command_or_parse_error)
2472 {
e0d9a270
SM
2473 for (bp_location *loc : loc_range)
2474 if (is_breakpoint (loc->owner)
2475 && loc->pspace->num == bl->pspace->num)
2476 {
2477 /* Only go as far as the first NULL bytecode is
2478 located. */
2479 if (loc->cmd_bytecode == NULL)
2480 return;
d3ce09f5 2481
e0d9a270
SM
2482 loc->cmd_bytecode.reset ();
2483 }
d3ce09f5
SS
2484 }
2485
7f32a4d5
PA
2486 /* No NULL commands or failed bytecode generation. Build a command
2487 list for all duplicate locations at this location's address.
2488 Note that here we must care for whether the breakpoint location
2489 types are considered duplicates, otherwise, say, if we have a
2490 software and hardware location at the same address, the target
2491 could end up running the commands twice. For the moment, we only
2492 support targets-side commands with dprintf, but it doesn't hurt
2493 to be pedantically correct in case that changes. */
e0d9a270
SM
2494 for (bp_location *loc : loc_range)
2495 if (breakpoint_locations_match (bl, loc)
2496 && loc->owner->extra_string
2497 && is_breakpoint (loc->owner)
2498 && loc->pspace->num == bl->pspace->num
2499 && loc->owner->enable_state == bp_enabled
2500 && loc->enabled
2501 && !loc->disabled_by_cond)
2502 {
2503 /* Add the command to the vector. This will be used later
2504 to send the commands to the target. */
2505 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2506 }
d3ce09f5
SS
2507
2508 bl->target_info.persist = 0;
2509 /* Maybe flag this location as persistent. */
2510 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2511 bl->target_info.persist = 1;
2512}
2513
833b7ab5
YQ
2514/* Return the kind of breakpoint on address *ADDR. Get the kind
2515 of breakpoint according to ADDR except single-step breakpoint.
2516 Get the kind of single-step breakpoint according to the current
2517 registers state. */
cd6c3b4f
YQ
2518
2519static int
2520breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2521{
833b7ab5
YQ
2522 if (bl->owner->type == bp_single_step)
2523 {
2524 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2525 struct regcache *regcache;
2526
00431a78 2527 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2528
2529 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2530 regcache, addr);
2531 }
2532 else
2533 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2534}
2535
35df4500
TJB
2536/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2537 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2538 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2539 Returns 0 for success, 1 if the bp_location type is not supported or
2540 -1 for failure.
879bfdc2 2541
4a64f543
MS
2542 NOTE drow/2003-09-09: This routine could be broken down to an
2543 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2544static int
35df4500 2545insert_bp_location (struct bp_location *bl,
26bb91f3 2546 struct ui_file *tmp_error_stream,
3fbb6ffa 2547 int *disabled_breaks,
dd61ec5c
MW
2548 int *hw_breakpoint_error,
2549 int *hw_bp_error_explained_already)
879bfdc2 2550{
cc06b668 2551 gdb_exception bp_excpt;
879bfdc2 2552
b775012e 2553 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2554 return 0;
2555
35c63cd8
JB
2556 /* Note we don't initialize bl->target_info, as that wipes out
2557 the breakpoint location's shadow_contents if the breakpoint
2558 is still inserted at that location. This in turn breaks
2559 target_read_memory which depends on these buffers when
2560 a memory read is requested at the breakpoint location:
2561 Once the target_info has been wiped, we fail to see that
2562 we have a breakpoint inserted at that address and thus
2563 read the breakpoint instead of returning the data saved in
2564 the breakpoint location's shadow contents. */
0d5ed153 2565 bl->target_info.reqstd_address = bl->address;
35df4500 2566 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2567 bl->target_info.length = bl->length;
8181d85f 2568
b775012e
LM
2569 /* When working with target-side conditions, we must pass all the conditions
2570 for the same breakpoint address down to the target since GDB will not
2571 insert those locations. With a list of breakpoint conditions, the target
2572 can decide when to stop and notify GDB. */
2573
2574 if (is_breakpoint (bl->owner))
2575 {
2576 build_target_condition_list (bl);
d3ce09f5
SS
2577 build_target_command_list (bl);
2578 /* Reset the modification marker. */
b775012e
LM
2579 bl->needs_update = 0;
2580 }
2581
7f32a4d5
PA
2582 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2583 set at a read-only address, then a breakpoint location will have
2584 been changed to hardware breakpoint before we get here. If it is
2585 "off" however, error out before actually trying to insert the
2586 breakpoint, with a nicer error message. */
35df4500 2587 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2588 && !automatic_hardware_breakpoints)
879bfdc2 2589 {
7f32a4d5 2590 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2591
7f32a4d5
PA
2592 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2593 {
2594 fprintf_unfiltered (tmp_error_stream,
2595 _("Cannot insert breakpoint %d.\n"
2596 "Cannot set software breakpoint "
2597 "at read-only address %s\n"),
2598 bl->owner->number,
2599 paddress (bl->gdbarch, bl->address));
2600 return 1;
765dc015 2601 }
7f32a4d5
PA
2602 }
2603
2604 if (bl->loc_type == bp_loc_software_breakpoint
2605 || bl->loc_type == bp_loc_hardware_breakpoint)
2606 {
879bfdc2
DJ
2607 /* First check to see if we have to handle an overlay. */
2608 if (overlay_debugging == ovly_off
35df4500
TJB
2609 || bl->section == NULL
2610 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2611 {
2612 /* No overlay handling: just set the breakpoint. */
a70b8144 2613 try
dd61ec5c 2614 {
0000e5cc
PA
2615 int val;
2616
dd61ec5c 2617 val = bl->owner->ops->insert_location (bl);
0000e5cc 2618 if (val)
688fca4f 2619 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2620 }
94aeb44b 2621 catch (gdb_exception &e)
dd61ec5c 2622 {
94aeb44b 2623 bp_excpt = std::move (e);
dd61ec5c 2624 }
879bfdc2
DJ
2625 }
2626 else
2627 {
4a64f543 2628 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2629 Shall we set a breakpoint at the LMA? */
2630 if (!overlay_events_enabled)
2631 {
2632 /* Yes -- overlay event support is not active,
2633 so we must try to set a breakpoint at the LMA.
2634 This will not work for a hardware breakpoint. */
35df4500 2635 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2636 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2637 bl->owner->number);
879bfdc2
DJ
2638 else
2639 {
35df4500
TJB
2640 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2641 bl->section);
879bfdc2 2642 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2643 bl->overlay_target_info = bl->target_info;
0d5ed153 2644 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2645
2646 /* No overlay handling: just set the breakpoint. */
a70b8144 2647 try
0000e5cc
PA
2648 {
2649 int val;
2650
579c6ad9 2651 bl->overlay_target_info.kind
cd6c3b4f
YQ
2652 = breakpoint_kind (bl, &addr);
2653 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2654 val = target_insert_breakpoint (bl->gdbarch,
2655 &bl->overlay_target_info);
2656 if (val)
688fca4f
PA
2657 bp_excpt
2658 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2659 }
94aeb44b 2660 catch (gdb_exception &e)
0000e5cc 2661 {
94aeb44b 2662 bp_excpt = std::move (e);
0000e5cc
PA
2663 }
2664
688fca4f 2665 if (bp_excpt.reason != 0)
99361f52 2666 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2667 "Overlay breakpoint %d "
2668 "failed: in ROM?\n",
35df4500 2669 bl->owner->number);
879bfdc2
DJ
2670 }
2671 }
2672 /* Shall we set a breakpoint at the VMA? */
35df4500 2673 if (section_is_mapped (bl->section))
879bfdc2
DJ
2674 {
2675 /* Yes. This overlay section is mapped into memory. */
a70b8144 2676 try
dda83cd7 2677 {
0000e5cc
PA
2678 int val;
2679
dda83cd7 2680 val = bl->owner->ops->insert_location (bl);
0000e5cc 2681 if (val)
688fca4f 2682 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2683 }
94aeb44b 2684 catch (gdb_exception &e)
dda83cd7 2685 {
94aeb44b 2686 bp_excpt = std::move (e);
dda83cd7 2687 }
879bfdc2
DJ
2688 }
2689 else
2690 {
2691 /* No. This breakpoint will not be inserted.
2692 No error, but do not mark the bp as 'inserted'. */
2693 return 0;
2694 }
2695 }
2696
688fca4f 2697 if (bp_excpt.reason != 0)
879bfdc2
DJ
2698 {
2699 /* Can't set the breakpoint. */
0000e5cc 2700
80dc83fd
AB
2701 /* If the target has closed then it will have deleted any
2702 breakpoints inserted within the target inferior, as a result
2703 any further attempts to interact with the breakpoint objects
2704 is not possible. Just rethrow the error. */
2705 if (bp_excpt.error == TARGET_CLOSE_ERROR)
2706 throw bp_excpt;
2707 gdb_assert (bl->owner != nullptr);
2708
0000e5cc
PA
2709 /* In some cases, we might not be able to insert a
2710 breakpoint in a shared library that has already been
2711 removed, but we have not yet processed the shlib unload
2712 event. Unfortunately, some targets that implement
076855f9
PA
2713 breakpoint insertion themselves can't tell why the
2714 breakpoint insertion failed (e.g., the remote target
2715 doesn't define error codes), so we must treat generic
2716 errors as memory errors. */
688fca4f
PA
2717 if (bp_excpt.reason == RETURN_ERROR
2718 && (bp_excpt.error == GENERIC_ERROR
2719 || bp_excpt.error == MEMORY_ERROR)
076855f9 2720 && bl->loc_type == bp_loc_software_breakpoint
08351840 2721 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2722 || shared_objfile_contains_address_p (bl->pspace,
2723 bl->address)))
879bfdc2 2724 {
4a64f543 2725 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2726 bl->shlib_disabled = 1;
76727919 2727 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2728 if (!*disabled_breaks)
2729 {
2730 fprintf_unfiltered (tmp_error_stream,
2731 "Cannot insert breakpoint %d.\n",
2732 bl->owner->number);
2733 fprintf_unfiltered (tmp_error_stream,
2734 "Temporarily disabling shared "
2735 "library breakpoints:\n");
2736 }
2737 *disabled_breaks = 1;
879bfdc2 2738 fprintf_unfiltered (tmp_error_stream,
35df4500 2739 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2740 return 0;
879bfdc2
DJ
2741 }
2742 else
879bfdc2 2743 {
35df4500 2744 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2745 {
0000e5cc 2746 *hw_breakpoint_error = 1;
688fca4f 2747 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2748 fprintf_unfiltered (tmp_error_stream,
2749 "Cannot insert hardware breakpoint %d%s",
2750 bl->owner->number,
688fca4f 2751 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2752 if (bp_excpt.message != NULL)
2753 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2754 bp_excpt.what ());
879bfdc2
DJ
2755 }
2756 else
2757 {
688fca4f 2758 if (bp_excpt.message == NULL)
0000e5cc 2759 {
1ccbe998 2760 std::string message
0000e5cc
PA
2761 = memory_error_message (TARGET_XFER_E_IO,
2762 bl->gdbarch, bl->address);
0000e5cc
PA
2763
2764 fprintf_unfiltered (tmp_error_stream,
2765 "Cannot insert breakpoint %d.\n"
2766 "%s\n",
1ccbe998 2767 bl->owner->number, message.c_str ());
0000e5cc
PA
2768 }
2769 else
2770 {
2771 fprintf_unfiltered (tmp_error_stream,
2772 "Cannot insert breakpoint %d: %s\n",
2773 bl->owner->number,
3d6e9d23 2774 bp_excpt.what ());
0000e5cc 2775 }
879bfdc2 2776 }
0000e5cc 2777 return 1;
879bfdc2
DJ
2778
2779 }
2780 }
2781 else
35df4500 2782 bl->inserted = 1;
879bfdc2 2783
0000e5cc 2784 return 0;
879bfdc2
DJ
2785 }
2786
35df4500 2787 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2788 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2789 watchpoints. It's not clear that it's necessary... */
35df4500 2790 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2791 {
0000e5cc
PA
2792 int val;
2793
77b06cd7
TJB
2794 gdb_assert (bl->owner->ops != NULL
2795 && bl->owner->ops->insert_location != NULL);
2796
2797 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2798
2799 /* If trying to set a read-watchpoint, and it turns out it's not
2800 supported, try emulating one with an access watchpoint. */
35df4500 2801 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2802 {
85d721b8
PA
2803 /* But don't try to insert it, if there's already another
2804 hw_access location that would be considered a duplicate
2805 of this one. */
48d7020b 2806 for (bp_location *loc : all_bp_locations ())
35df4500 2807 if (loc != bl
85d721b8 2808 && loc->watchpoint_type == hw_access
35df4500 2809 && watchpoint_locations_match (bl, loc))
85d721b8 2810 {
35df4500
TJB
2811 bl->duplicate = 1;
2812 bl->inserted = 1;
2813 bl->target_info = loc->target_info;
2814 bl->watchpoint_type = hw_access;
85d721b8
PA
2815 val = 0;
2816 break;
2817 }
2818
2819 if (val == 1)
2820 {
77b06cd7
TJB
2821 bl->watchpoint_type = hw_access;
2822 val = bl->owner->ops->insert_location (bl);
2823
2824 if (val)
2825 /* Back to the original value. */
2826 bl->watchpoint_type = hw_read;
85d721b8
PA
2827 }
2828 }
2829
35df4500 2830 bl->inserted = (val == 0);
879bfdc2
DJ
2831 }
2832
35df4500 2833 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2834 {
0000e5cc
PA
2835 int val;
2836
77b06cd7
TJB
2837 gdb_assert (bl->owner->ops != NULL
2838 && bl->owner->ops->insert_location != NULL);
2839
2840 val = bl->owner->ops->insert_location (bl);
2841 if (val)
2842 {
2843 bl->owner->enable_state = bp_disabled;
2844
2845 if (val == 1)
2846 warning (_("\
2847Error inserting catchpoint %d: Your system does not support this type\n\
2848of catchpoint."), bl->owner->number);
2849 else
2850 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2851 }
2852
2853 bl->inserted = (val == 0);
1640b821
DJ
2854
2855 /* We've already printed an error message if there was a problem
2856 inserting this catchpoint, and we've disabled the catchpoint,
2857 so just return success. */
2858 return 0;
879bfdc2
DJ
2859 }
2860
2861 return 0;
2862}
2863
6c95b8df
PA
2864/* This function is called when program space PSPACE is about to be
2865 deleted. It takes care of updating breakpoints to not reference
2866 PSPACE anymore. */
2867
2868void
2869breakpoint_program_space_exit (struct program_space *pspace)
2870{
6c95b8df 2871 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2872 for (breakpoint *b : all_breakpoints_safe ())
2873 if (b->pspace == pspace)
2874 delete_breakpoint (b);
6c95b8df
PA
2875
2876 /* Breakpoints set through other program spaces could have locations
2877 bound to PSPACE as well. Remove those. */
48d7020b 2878 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2879 {
2880 struct bp_location *tmp;
2881
2882 if (loc->pspace == pspace)
2883 {
2bdf28a0 2884 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2885 if (loc->owner->loc == loc)
2886 loc->owner->loc = loc->next;
2887 else
2888 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2889 if (tmp->next == loc)
2890 {
2891 tmp->next = loc->next;
2892 break;
2893 }
2894 }
2895 }
2896
2897 /* Now update the global location list to permanently delete the
2898 removed locations above. */
44702360 2899 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2900}
2901
74960c60
VP
2902/* Make sure all breakpoints are inserted in inferior.
2903 Throws exception on any error.
2904 A breakpoint that is already inserted won't be inserted
2905 again, so calling this function twice is safe. */
2906void
2907insert_breakpoints (void)
2908{
43892fdf 2909 for (breakpoint *bpt : all_breakpoints ())
74960c60 2910 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2911 {
2912 struct watchpoint *w = (struct watchpoint *) bpt;
2913
2914 update_watchpoint (w, 0 /* don't reparse. */);
2915 }
74960c60 2916
04086b45
PA
2917 /* Updating watchpoints creates new locations, so update the global
2918 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2919 ignore breakpoints_always_inserted_mode. Also,
2920 update_global_location_list tries to "upgrade" software
2921 breakpoints to hardware breakpoints to handle "set breakpoint
2922 auto-hw", so we need to call it even if we don't have new
2923 locations. */
04086b45 2924 update_global_location_list (UGLL_INSERT);
74960c60
VP
2925}
2926
b775012e
LM
2927/* This is used when we need to synch breakpoint conditions between GDB and the
2928 target. It is the case with deleting and disabling of breakpoints when using
2929 always-inserted mode. */
2930
2931static void
2932update_inserted_breakpoint_locations (void)
2933{
b775012e
LM
2934 int error_flag = 0;
2935 int val = 0;
2936 int disabled_breaks = 0;
2937 int hw_breakpoint_error = 0;
dd61ec5c 2938 int hw_bp_details_reported = 0;
b775012e 2939
d7e74731 2940 string_file tmp_error_stream;
b775012e
LM
2941
2942 /* Explicitly mark the warning -- this will only be printed if
2943 there was an error. */
d7e74731 2944 tmp_error_stream.puts ("Warning:\n");
b775012e 2945
5ed8105e 2946 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 2947
48d7020b 2948 for (bp_location *bl : all_bp_locations ())
b775012e
LM
2949 {
2950 /* We only want to update software breakpoints and hardware
2951 breakpoints. */
2952 if (!is_breakpoint (bl->owner))
2953 continue;
2954
2955 /* We only want to update locations that are already inserted
2956 and need updating. This is to avoid unwanted insertion during
2957 deletion of breakpoints. */
4daf1902 2958 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2959 continue;
2960
2961 switch_to_program_space_and_thread (bl->pspace);
2962
2963 /* For targets that support global breakpoints, there's no need
2964 to select an inferior to insert breakpoint to. In fact, even
2965 if we aren't attached to any process yet, we should still
2966 insert breakpoints. */
f5656ead 2967 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2968 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2969 continue;
2970
d7e74731 2971 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2972 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2973 if (val)
2974 error_flag = val;
2975 }
2976
2977 if (error_flag)
2978 {
223ffa71 2979 target_terminal::ours_for_output ();
b775012e
LM
2980 error_stream (tmp_error_stream);
2981 }
b775012e
LM
2982}
2983
c30eee59 2984/* Used when starting or continuing the program. */
c906108c 2985
74960c60
VP
2986static void
2987insert_breakpoint_locations (void)
c906108c 2988{
eacd795a 2989 int error_flag = 0;
c906108c 2990 int val = 0;
3fbb6ffa 2991 int disabled_breaks = 0;
81d0cc19 2992 int hw_breakpoint_error = 0;
dd61ec5c 2993 int hw_bp_error_explained_already = 0;
c906108c 2994
d7e74731
PA
2995 string_file tmp_error_stream;
2996
81d0cc19
GS
2997 /* Explicitly mark the warning -- this will only be printed if
2998 there was an error. */
d7e74731 2999 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3000
5ed8105e 3001 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3002
48d7020b 3003 for (bp_location *bl : all_bp_locations ())
879bfdc2 3004 {
b775012e 3005 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3006 continue;
3007
4a64f543
MS
3008 /* There is no point inserting thread-specific breakpoints if
3009 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3010 has BL->OWNER always non-NULL. */
35df4500 3011 if (bl->owner->thread != -1
5d5658a1 3012 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3013 continue;
3014
35df4500 3015 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3016
3017 /* For targets that support global breakpoints, there's no need
3018 to select an inferior to insert breakpoint to. In fact, even
3019 if we aren't attached to any process yet, we should still
3020 insert breakpoints. */
f5656ead 3021 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3022 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3023 continue;
3024
d7e74731 3025 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3026 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3027 if (val)
eacd795a 3028 error_flag = val;
879bfdc2 3029 }
c906108c 3030
4a64f543
MS
3031 /* If we failed to insert all locations of a watchpoint, remove
3032 them, as half-inserted watchpoint is of limited use. */
43892fdf 3033 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3034 {
3035 int some_failed = 0;
a5606eee
VP
3036
3037 if (!is_hardware_watchpoint (bpt))
3038 continue;
3039
d6b74ac4 3040 if (!breakpoint_enabled (bpt))
a5606eee 3041 continue;
74960c60
VP
3042
3043 if (bpt->disposition == disp_del_at_next_stop)
3044 continue;
a5606eee 3045
40cb8ca5 3046 for (bp_location *loc : bpt->locations ())
56710373 3047 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3048 {
3049 some_failed = 1;
3050 break;
3051 }
40cb8ca5 3052
a5606eee
VP
3053 if (some_failed)
3054 {
40cb8ca5 3055 for (bp_location *loc : bpt->locations ())
a5606eee 3056 if (loc->inserted)
834c0d03 3057 remove_breakpoint (loc);
a5606eee
VP
3058
3059 hw_breakpoint_error = 1;
d7e74731
PA
3060 tmp_error_stream.printf ("Could not insert "
3061 "hardware watchpoint %d.\n",
3062 bpt->number);
eacd795a 3063 error_flag = -1;
a5606eee
VP
3064 }
3065 }
3066
eacd795a 3067 if (error_flag)
81d0cc19
GS
3068 {
3069 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3070 message about possibly exhausted resources. */
dd61ec5c 3071 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3072 {
d7e74731 3073 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3074You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3075 }
223ffa71 3076 target_terminal::ours_for_output ();
81d0cc19
GS
3077 error_stream (tmp_error_stream);
3078 }
c906108c
SS
3079}
3080
c30eee59
TJB
3081/* Used when the program stops.
3082 Returns zero if successful, or non-zero if there was a problem
3083 removing a breakpoint location. */
3084
c906108c 3085int
fba45db2 3086remove_breakpoints (void)
c906108c 3087{
3a1bae8e 3088 int val = 0;
c906108c 3089
48d7020b 3090 for (bp_location *bl : all_bp_locations ())
1e4d1764 3091 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3092 val |= remove_breakpoint (bl);
01add95b 3093
3a1bae8e 3094 return val;
c906108c
SS
3095}
3096
49fa26b0
PA
3097/* When a thread exits, remove breakpoints that are related to
3098 that thread. */
3099
3100static void
3101remove_threaded_breakpoints (struct thread_info *tp, int silent)
3102{
1428b37a 3103 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3104 {
5d5658a1 3105 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3106 {
3107 b->disposition = disp_del_at_next_stop;
3108
3109 printf_filtered (_("\
43792cf0
PA
3110Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3111 b->number, print_thread_id (tp));
49fa26b0
PA
3112
3113 /* Hide it from the user. */
3114 b->number = 0;
3115 }
3116 }
3117}
3118
f3869b1a 3119/* See breakpoint.h. */
6c95b8df 3120
f3869b1a 3121void
00431a78 3122remove_breakpoints_inf (inferior *inf)
6c95b8df 3123{
6c95b8df 3124 int val;
6c95b8df 3125
48d7020b 3126 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3127 {
3128 if (bl->pspace != inf->pspace)
3129 continue;
6c95b8df 3130
01add95b
SM
3131 if (bl->inserted && !bl->target_info.persist)
3132 {
3133 val = remove_breakpoint (bl);
3134 if (val != 0)
3135 return;
3136 }
3137 }
6c95b8df
PA
3138}
3139
e58b0e63
PA
3140static int internal_breakpoint_number = -1;
3141
84f4c1fe
PM
3142/* Set the breakpoint number of B, depending on the value of INTERNAL.
3143 If INTERNAL is non-zero, the breakpoint number will be populated
3144 from internal_breakpoint_number and that variable decremented.
e5dd4106 3145 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3146 breakpoint_count and that value incremented. Internal breakpoints
3147 do not set the internal var bpnum. */
3148static void
3149set_breakpoint_number (int internal, struct breakpoint *b)
3150{
3151 if (internal)
3152 b->number = internal_breakpoint_number--;
3153 else
3154 {
3155 set_breakpoint_count (breakpoint_count + 1);
3156 b->number = breakpoint_count;
3157 }
3158}
3159
e62c965a 3160static struct breakpoint *
a6d9a66e 3161create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3162 CORE_ADDR address, enum bptype type,
c0a91b2b 3163 const struct breakpoint_ops *ops)
e62c965a 3164{
51abb421 3165 symtab_and_line sal;
e62c965a
PP
3166 sal.pc = address;
3167 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3168 sal.pspace = current_program_space;
e62c965a 3169
51abb421 3170 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3171 b->number = internal_breakpoint_number--;
3172 b->disposition = disp_donttouch;
3173
3174 return b;
3175}
3176
17450429
PP
3177static const char *const longjmp_names[] =
3178 {
3179 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3180 };
3181#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3182
3183/* Per-objfile data private to breakpoint.c. */
3184struct breakpoint_objfile_data
3185{
3186 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3187 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3188
3189 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3190 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3191
28106bc2 3192 /* True if we have looked for longjmp probes. */
43dce439 3193 int longjmp_searched = 0;
28106bc2 3194
45461e0d
SM
3195 /* SystemTap probe points for longjmp (if any). These are non-owning
3196 references. */
3197 std::vector<probe *> longjmp_probes;
28106bc2 3198
17450429 3199 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3200 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3201
3202 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3203 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3204
3205 /* True if we have looked for exception probes. */
43dce439 3206 int exception_searched = 0;
28106bc2 3207
45461e0d
SM
3208 /* SystemTap probe points for unwinding (if any). These are non-owning
3209 references. */
3210 std::vector<probe *> exception_probes;
17450429
PP
3211};
3212
51d3063a
TT
3213static const struct objfile_key<breakpoint_objfile_data>
3214 breakpoint_objfile_key;
17450429
PP
3215
3216/* Minimal symbol not found sentinel. */
3217static struct minimal_symbol msym_not_found;
3218
3219/* Returns TRUE if MSYM point to the "not found" sentinel. */
3220
3221static int
3222msym_not_found_p (const struct minimal_symbol *msym)
3223{
3224 return msym == &msym_not_found;
3225}
3226
3227/* Return per-objfile data needed by breakpoint.c.
3228 Allocate the data if necessary. */
3229
3230static struct breakpoint_objfile_data *
3231get_breakpoint_objfile_data (struct objfile *objfile)
3232{
3233 struct breakpoint_objfile_data *bp_objfile_data;
3234
51d3063a 3235 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3236 if (bp_objfile_data == NULL)
51d3063a 3237 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3238 return bp_objfile_data;
3239}
3240
e62c965a 3241static void
af02033e 3242create_overlay_event_breakpoint (void)
e62c965a 3243{
af02033e 3244 const char *const func_name = "_ovly_debug_event";
e62c965a 3245
2030c079 3246 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3247 {
3248 struct breakpoint *b;
17450429
PP
3249 struct breakpoint_objfile_data *bp_objfile_data;
3250 CORE_ADDR addr;
67994074 3251 struct explicit_location explicit_loc;
69de3c6a 3252
17450429
PP
3253 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3254
3b7344d5 3255 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3256 continue;
3257
3b7344d5 3258 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3259 {
3b7344d5 3260 struct bound_minimal_symbol m;
17450429
PP
3261
3262 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3263 if (m.minsym == NULL)
17450429
PP
3264 {
3265 /* Avoid future lookups in this objfile. */
3b7344d5 3266 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3267 continue;
3268 }
3269 bp_objfile_data->overlay_msym = m;
3270 }
e62c965a 3271
77e371c0 3272 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3273 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3274 bp_overlay_event,
06edf0c0 3275 &internal_breakpoint_ops);
67994074
KS
3276 initialize_explicit_location (&explicit_loc);
3277 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3278 b->location = new_explicit_location (&explicit_loc);
e62c965a 3279
69de3c6a 3280 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3281 {
3282 b->enable_state = bp_enabled;
3283 overlay_events_enabled = 1;
3284 }
69de3c6a
PP
3285 else
3286 {
dda83cd7
SM
3287 b->enable_state = bp_disabled;
3288 overlay_events_enabled = 0;
69de3c6a 3289 }
e62c965a 3290 }
e62c965a
PP
3291}
3292
2a7f6487
TV
3293/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3294 true if a breakpoint was installed. */
3295
3296static bool
3297create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3298{
2a7f6487
TV
3299 struct gdbarch *gdbarch = objfile->arch ();
3300 struct breakpoint_objfile_data *bp_objfile_data
3301 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3302
2a7f6487 3303 if (!bp_objfile_data->longjmp_searched)
94c93c35 3304 {
2a7f6487
TV
3305 std::vector<probe *> ret
3306 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3307
2a7f6487 3308 if (!ret.empty ())
94c93c35 3309 {
2a7f6487
TV
3310 /* We are only interested in checking one element. */
3311 probe *p = ret[0];
0fd8e87f 3312
2a7f6487
TV
3313 if (!p->can_evaluate_arguments ())
3314 {
3315 /* We cannot use the probe interface here,
3316 because it does not know how to evaluate
3317 arguments. */
3318 ret.clear ();
3319 }
3320 }
3321 bp_objfile_data->longjmp_probes = ret;
3322 bp_objfile_data->longjmp_searched = 1;
3323 }
0fd8e87f 3324
2a7f6487
TV
3325 if (bp_objfile_data->longjmp_probes.empty ())
3326 return false;
17450429 3327
2a7f6487
TV
3328 for (probe *p : bp_objfile_data->longjmp_probes)
3329 {
3330 struct breakpoint *b;
25f9533e 3331
2a7f6487
TV
3332 b = create_internal_breakpoint (gdbarch,
3333 p->get_relocated_address (objfile),
3334 bp_longjmp_master,
3335 &internal_breakpoint_ops);
3336 b->location = new_probe_location ("-probe-stap libc:longjmp");
3337 b->enable_state = bp_disabled;
3338 }
aed57c53 3339
2a7f6487
TV
3340 return true;
3341}
25f9533e 3342
2a7f6487
TV
3343/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3344 Return true if at least one breakpoint was installed. */
3345
3346static bool
3347create_longjmp_master_breakpoint_names (objfile *objfile)
3348{
3349 struct gdbarch *gdbarch = objfile->arch ();
3350 if (!gdbarch_get_longjmp_target_p (gdbarch))
3351 return false;
3352
3353 struct breakpoint_objfile_data *bp_objfile_data
3354 = get_breakpoint_objfile_data (objfile);
3355 unsigned int installed_bp = 0;
3356
3357 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3358 {
3359 struct breakpoint *b;
3360 const char *func_name;
3361 CORE_ADDR addr;
3362 struct explicit_location explicit_loc;
3363
3364 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3365 continue;
28106bc2 3366
2a7f6487
TV
3367 func_name = longjmp_names[i];
3368 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3369 {
3370 struct bound_minimal_symbol m;
3371
3372 m = lookup_minimal_symbol_text (func_name, objfile);
3373 if (m.minsym == NULL)
3374 {
3375 /* Prevent future lookups in this objfile. */
3376 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3377 continue;
3378 }
2a7f6487
TV
3379 bp_objfile_data->longjmp_msym[i] = m;
3380 }
28106bc2 3381
2a7f6487
TV
3382 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3383 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3384 &internal_breakpoint_ops);
3385 initialize_explicit_location (&explicit_loc);
3386 explicit_loc.function_name = ASTRDUP (func_name);
3387 b->location = new_explicit_location (&explicit_loc);
3388 b->enable_state = bp_disabled;
3389 installed_bp++;
3390 }
28106bc2 3391
2a7f6487
TV
3392 return installed_bp > 0;
3393}
0fd8e87f 3394
2a7f6487 3395/* Create a master longjmp breakpoint. */
17450429 3396
2a7f6487
TV
3397static void
3398create_longjmp_master_breakpoint (void)
3399{
3400 scoped_restore_current_program_space restore_pspace;
aed57c53 3401
2a7f6487
TV
3402 for (struct program_space *pspace : program_spaces)
3403 {
3404 set_current_program_space (pspace);
17450429 3405
2a7f6487
TV
3406 for (objfile *obj : current_program_space->objfiles ())
3407 {
3408 /* Skip separate debug object, it's handled in the loop below. */
3409 if (obj->separate_debug_objfile_backlink != nullptr)
3410 continue;
3411
3412 /* Try a probe kind breakpoint on main objfile. */
3413 if (create_longjmp_master_breakpoint_probe (obj))
3414 continue;
3415
3416 /* Try longjmp_names kind breakpoints on main and separate_debug
3417 objfiles. */
3418 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3419 if (create_longjmp_master_breakpoint_names (debug_objfile))
3420 break;
94c93c35
TT
3421 }
3422 }
0fd8e87f
UW
3423}
3424
af02033e 3425/* Create a master std::terminate breakpoint. */
aa7d318d 3426static void
af02033e 3427create_std_terminate_master_breakpoint (void)
aa7d318d 3428{
af02033e 3429 const char *const func_name = "std::terminate()";
aa7d318d 3430
5ed8105e 3431 scoped_restore_current_program_space restore_pspace;
aa7d318d 3432
94c93c35
TT
3433 for (struct program_space *pspace : program_spaces)
3434 {
3435 CORE_ADDR addr;
17450429 3436
94c93c35 3437 set_current_program_space (pspace);
17450429 3438
94c93c35
TT
3439 for (objfile *objfile : current_program_space->objfiles ())
3440 {
3441 struct breakpoint *b;
3442 struct breakpoint_objfile_data *bp_objfile_data;
3443 struct explicit_location explicit_loc;
aa7d318d 3444
94c93c35 3445 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3446
94c93c35
TT
3447 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3448 continue;
17450429 3449
94c93c35
TT
3450 if (bp_objfile_data->terminate_msym.minsym == NULL)
3451 {
3452 struct bound_minimal_symbol m;
17450429 3453
94c93c35
TT
3454 m = lookup_minimal_symbol (func_name, NULL, objfile);
3455 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3456 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3457 {
3458 /* Prevent future lookups in this objfile. */
3459 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3460 continue;
3461 }
3462 bp_objfile_data->terminate_msym = m;
3463 }
aa7d318d 3464
94c93c35
TT
3465 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3466 b = create_internal_breakpoint (objfile->arch (), addr,
3467 bp_std_terminate_master,
3468 &internal_breakpoint_ops);
3469 initialize_explicit_location (&explicit_loc);
3470 explicit_loc.function_name = ASTRDUP (func_name);
3471 b->location = new_explicit_location (&explicit_loc);
3472 b->enable_state = bp_disabled;
3473 }
3474 }
aa7d318d
TT
3475}
3476
1940319c
TV
3477/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3478 probe. Return true if a breakpoint was installed. */
186c406b 3479
1940319c
TV
3480static bool
3481create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3482{
1940319c
TV
3483 struct breakpoint *b;
3484 struct gdbarch *gdbarch;
3485 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3486
1940319c 3487 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3488
1940319c
TV
3489 /* We prefer the SystemTap probe point if it exists. */
3490 if (!bp_objfile_data->exception_searched)
3491 {
3492 std::vector<probe *> ret
3493 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3494
1940319c 3495 if (!ret.empty ())
28106bc2 3496 {
1940319c
TV
3497 /* We are only interested in checking one element. */
3498 probe *p = ret[0];
25f9533e 3499
1940319c 3500 if (!p->can_evaluate_arguments ())
25f9533e 3501 {
1940319c
TV
3502 /* We cannot use the probe interface here, because it does
3503 not know how to evaluate arguments. */
3504 ret.clear ();
25f9533e 3505 }
28106bc2 3506 }
1940319c
TV
3507 bp_objfile_data->exception_probes = ret;
3508 bp_objfile_data->exception_searched = 1;
3509 }
28106bc2 3510
1940319c
TV
3511 if (bp_objfile_data->exception_probes.empty ())
3512 return false;
45461e0d 3513
1940319c 3514 gdbarch = objfile->arch ();
28106bc2 3515
1940319c
TV
3516 for (probe *p : bp_objfile_data->exception_probes)
3517 {
3518 b = create_internal_breakpoint (gdbarch,
3519 p->get_relocated_address (objfile),
3520 bp_exception_master,
3521 &internal_breakpoint_ops);
3522 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3523 b->enable_state = bp_disabled;
3524 }
28106bc2 3525
1940319c
TV
3526 return true;
3527}
28106bc2 3528
1940319c
TV
3529/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3530 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3531
1940319c
TV
3532static bool
3533create_exception_master_breakpoint_hook (objfile *objfile)
3534{
3535 const char *const func_name = "_Unwind_DebugHook";
3536 struct breakpoint *b;
3537 struct gdbarch *gdbarch;
3538 struct breakpoint_objfile_data *bp_objfile_data;
3539 CORE_ADDR addr;
3540 struct explicit_location explicit_loc;
186c406b 3541
1940319c 3542 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3543
1940319c
TV
3544 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3545 return false;
17450429 3546
1940319c
TV
3547 gdbarch = objfile->arch ();
3548
3549 if (bp_objfile_data->exception_msym.minsym == NULL)
3550 {
3551 struct bound_minimal_symbol debug_hook;
3552
3553 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3554 if (debug_hook.minsym == NULL)
3555 {
3556 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3557 return false;
186c406b 3558 }
17450429 3559
1940319c
TV
3560 bp_objfile_data->exception_msym = debug_hook;
3561 }
3562
3563 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3564 addr = gdbarch_convert_from_func_ptr_addr
3565 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3566 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3567 &internal_breakpoint_ops);
3568 initialize_explicit_location (&explicit_loc);
3569 explicit_loc.function_name = ASTRDUP (func_name);
3570 b->location = new_explicit_location (&explicit_loc);
3571 b->enable_state = bp_disabled;
3572
3573 return true;
3574}
3575
3576/* Install a master breakpoint on the unwinder's debug hook. */
3577
3578static void
3579create_exception_master_breakpoint (void)
3580{
3581 for (objfile *obj : current_program_space->objfiles ())
3582 {
3583 /* Skip separate debug object. */
3584 if (obj->separate_debug_objfile_backlink)
3585 continue;
3586
3587 /* Try a probe kind breakpoint. */
3588 if (create_exception_master_breakpoint_probe (obj))
3589 continue;
3590
7c6944ab
TV
3591 /* Iterate over main and separate debug objects and try an
3592 _Unwind_DebugHook kind breakpoint. */
3593 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3594 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3595 break;
186c406b 3596 }
186c406b
TT
3597}
3598
9ef9e6a6
KS
3599/* Does B have a location spec? */
3600
3601static int
3602breakpoint_event_location_empty_p (const struct breakpoint *b)
3603{
d28cd78a 3604 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3605}
3606
c906108c 3607void
fba45db2 3608update_breakpoints_after_exec (void)
c906108c 3609{
25b22b0a
PA
3610 /* We're about to delete breakpoints from GDB's lists. If the
3611 INSERTED flag is true, GDB will try to lift the breakpoints by
3612 writing the breakpoints' "shadow contents" back into memory. The
3613 "shadow contents" are NOT valid after an exec, so GDB should not
3614 do that. Instead, the target is responsible from marking
3615 breakpoints out as soon as it detects an exec. We don't do that
3616 here instead, because there may be other attempts to delete
3617 breakpoints after detecting an exec and before reaching here. */
48d7020b 3618 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3619 if (bploc->pspace == current_program_space)
3620 gdb_assert (!bploc->inserted);
c906108c 3621
1428b37a 3622 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3623 {
3624 if (b->pspace != current_program_space)
c5aa993b 3625 continue;
c906108c 3626
01add95b
SM
3627 /* Solib breakpoints must be explicitly reset after an exec(). */
3628 if (b->type == bp_shlib_event)
3629 {
3630 delete_breakpoint (b);
3631 continue;
3632 }
4efc6507 3633
01add95b
SM
3634 /* JIT breakpoints must be explicitly reset after an exec(). */
3635 if (b->type == bp_jit_event)
3636 {
3637 delete_breakpoint (b);
3638 continue;
3639 }
c4093a6a 3640
01add95b
SM
3641 /* Thread event breakpoints must be set anew after an exec(),
3642 as must overlay event and longjmp master breakpoints. */
3643 if (b->type == bp_thread_event || b->type == bp_overlay_event
3644 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3645 || b->type == bp_exception_master)
3646 {
3647 delete_breakpoint (b);
3648 continue;
3649 }
c5aa993b 3650
01add95b
SM
3651 /* Step-resume breakpoints are meaningless after an exec(). */
3652 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3653 {
3654 delete_breakpoint (b);
3655 continue;
3656 }
7c16b83e 3657
01add95b
SM
3658 /* Just like single-step breakpoints. */
3659 if (b->type == bp_single_step)
3660 {
3661 delete_breakpoint (b);
3662 continue;
3663 }
611c83ae 3664
01add95b
SM
3665 /* Longjmp and longjmp-resume breakpoints are also meaningless
3666 after an exec. */
3667 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3668 || b->type == bp_longjmp_call_dummy
3669 || b->type == bp_exception || b->type == bp_exception_resume)
3670 {
3671 delete_breakpoint (b);
3672 continue;
3673 }
ce78b96d 3674
01add95b
SM
3675 if (b->type == bp_catchpoint)
3676 {
3677 /* For now, none of the bp_catchpoint breakpoints need to
3678 do anything at this point. In the future, if some of
3679 the catchpoints need to something, we will need to add
3680 a new method, and call this method from here. */
3681 continue;
3682 }
c5aa993b 3683
01add95b
SM
3684 /* bp_finish is a special case. The only way we ought to be able
3685 to see one of these when an exec() has happened, is if the user
3686 caught a vfork, and then said "finish". Ordinarily a finish just
3687 carries them to the call-site of the current callee, by setting
3688 a temporary bp there and resuming. But in this case, the finish
3689 will carry them entirely through the vfork & exec.
3690
3691 We don't want to allow a bp_finish to remain inserted now. But
3692 we can't safely delete it, 'cause finish_command has a handle to
3693 the bp on a bpstat, and will later want to delete it. There's a
3694 chance (and I've seen it happen) that if we delete the bp_finish
3695 here, that its storage will get reused by the time finish_command
3696 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3697 We really must allow finish_command to delete a bp_finish.
3698
3699 In the absence of a general solution for the "how do we know
3700 it's safe to delete something others may have handles to?"
3701 problem, what we'll do here is just uninsert the bp_finish, and
3702 let finish_command delete it.
3703
3704 (We know the bp_finish is "doomed" in the sense that it's
3705 momentary, and will be deleted as soon as finish_command sees
3706 the inferior stopped. So it doesn't matter that the bp's
3707 address is probably bogus in the new a.out, unlike e.g., the
3708 solib breakpoints.) */
3709
3710 if (b->type == bp_finish)
3711 {
3712 continue;
3713 }
3714
3715 /* Without a symbolic address, we have little hope of the
3716 pre-exec() address meaning the same thing in the post-exec()
3717 a.out. */
3718 if (breakpoint_event_location_empty_p (b))
3719 {
3720 delete_breakpoint (b);
3721 continue;
3722 }
3723 }
c906108c
SS
3724}
3725
3726int
d80ee84f 3727detach_breakpoints (ptid_t ptid)
c906108c 3728{
3a1bae8e 3729 int val = 0;
2989a365 3730 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3731 struct inferior *inf = current_inferior ();
c5aa993b 3732
e99b03dc 3733 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3734 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3735
6c95b8df 3736 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3737 inferior_ptid = ptid;
48d7020b 3738 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3739 {
3740 if (bl->pspace != inf->pspace)
3741 continue;
3742
3743 /* This function must physically remove breakpoints locations
3744 from the specified ptid, without modifying the breakpoint
3745 package's state. Locations of type bp_loc_other are only
3746 maintained at GDB side. So, there is no need to remove
3747 these bp_loc_other locations. Moreover, removing these
3748 would modify the breakpoint package's state. */
3749 if (bl->loc_type == bp_loc_other)
3750 continue;
3751
3752 if (bl->inserted)
3753 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3754 }
d03285ec 3755
3a1bae8e 3756 return val;
c906108c
SS
3757}
3758
35df4500 3759/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3760 Note that this is used to detach breakpoints from a child fork.
3761 When we get here, the child isn't in the inferior list, and neither
3762 do we have objects to represent its address space --- we should
35df4500 3763 *not* look at bl->pspace->aspace here. */
6c95b8df 3764
c906108c 3765static int
b2b6a7da 3766remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3767{
3768 int val;
c5aa993b 3769
35df4500
TJB
3770 /* BL is never in moribund_locations by our callers. */
3771 gdb_assert (bl->owner != NULL);
2bdf28a0 3772
74960c60
VP
3773 /* The type of none suggests that owner is actually deleted.
3774 This should not ever happen. */
35df4500 3775 gdb_assert (bl->owner->type != bp_none);
0bde7532 3776
35df4500
TJB
3777 if (bl->loc_type == bp_loc_software_breakpoint
3778 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3779 {
c02f5703
MS
3780 /* "Normal" instruction breakpoint: either the standard
3781 trap-instruction bp (bp_breakpoint), or a
3782 bp_hardware_breakpoint. */
3783
3784 /* First check to see if we have to handle an overlay. */
3785 if (overlay_debugging == ovly_off
35df4500
TJB
3786 || bl->section == NULL
3787 || !(section_is_overlay (bl->section)))
c02f5703
MS
3788 {
3789 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3790
3791 /* If we're trying to uninsert a memory breakpoint that we
3792 know is set in a dynamic object that is marked
3793 shlib_disabled, then either the dynamic object was
3794 removed with "remove-symbol-file" or with
3795 "nosharedlibrary". In the former case, we don't know
3796 whether another dynamic object might have loaded over the
3797 breakpoint's address -- the user might well let us know
3798 about it next with add-symbol-file (the whole point of
d03de421 3799 add-symbol-file is letting the user manually maintain a
08351840
PA
3800 list of dynamically loaded objects). If we have the
3801 breakpoint's shadow memory, that is, this is a software
3802 breakpoint managed by GDB, check whether the breakpoint
3803 is still inserted in memory, to avoid overwriting wrong
3804 code with stale saved shadow contents. Note that HW
3805 breakpoints don't have shadow memory, as they're
3806 implemented using a mechanism that is not dependent on
3807 being able to modify the target's memory, and as such
3808 they should always be removed. */
3809 if (bl->shlib_disabled
3810 && bl->target_info.shadow_len != 0
3811 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3812 val = 0;
3813 else
73971819 3814 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3815 }
c906108c
SS
3816 else
3817 {
4a64f543 3818 /* This breakpoint is in an overlay section.
c02f5703
MS
3819 Did we set a breakpoint at the LMA? */
3820 if (!overlay_events_enabled)
3821 {
3822 /* Yes -- overlay event support is not active, so we
3823 should have set a breakpoint at the LMA. Remove it.
3824 */
c02f5703
MS
3825 /* Ignore any failures: if the LMA is in ROM, we will
3826 have already warned when we failed to insert it. */
35df4500
TJB
3827 if (bl->loc_type == bp_loc_hardware_breakpoint)
3828 target_remove_hw_breakpoint (bl->gdbarch,
3829 &bl->overlay_target_info);
c02f5703 3830 else
35df4500 3831 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3832 &bl->overlay_target_info,
3833 reason);
c02f5703
MS
3834 }
3835 /* Did we set a breakpoint at the VMA?
3836 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3837 if (bl->inserted)
c906108c 3838 {
c02f5703
MS
3839 /* Yes -- remove it. Previously we did not bother to
3840 remove the breakpoint if the section had been
3841 unmapped, but let's not rely on that being safe. We
3842 don't know what the overlay manager might do. */
aa67235e
UW
3843
3844 /* However, we should remove *software* breakpoints only
3845 if the section is still mapped, or else we overwrite
3846 wrong code with the saved shadow contents. */
348d480f
PA
3847 if (bl->loc_type == bp_loc_hardware_breakpoint
3848 || section_is_mapped (bl->section))
73971819 3849 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3850 else
3851 val = 0;
c906108c 3852 }
c02f5703
MS
3853 else
3854 {
3855 /* No -- not inserted, so no need to remove. No error. */
3856 val = 0;
3857 }
c906108c 3858 }
879d1e6b 3859
08351840
PA
3860 /* In some cases, we might not be able to remove a breakpoint in
3861 a shared library that has already been removed, but we have
3862 not yet processed the shlib unload event. Similarly for an
3863 unloaded add-symbol-file object - the user might not yet have
3864 had the chance to remove-symbol-file it. shlib_disabled will
3865 be set if the library/object has already been removed, but
3866 the breakpoint hasn't been uninserted yet, e.g., after
3867 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3868 always-inserted mode. */
076855f9 3869 if (val
08351840
PA
3870 && (bl->loc_type == bp_loc_software_breakpoint
3871 && (bl->shlib_disabled
3872 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3873 || shared_objfile_contains_address_p (bl->pspace,
3874 bl->address))))
879d1e6b
UW
3875 val = 0;
3876
c906108c
SS
3877 if (val)
3878 return val;
b2b6a7da 3879 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3880 }
35df4500 3881 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3882 {
77b06cd7
TJB
3883 gdb_assert (bl->owner->ops != NULL
3884 && bl->owner->ops->remove_location != NULL);
3885
b2b6a7da 3886 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3887 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3888
c906108c 3889 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3890 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3891 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3892 bl->owner->number);
c906108c 3893 }
35df4500 3894 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3895 && breakpoint_enabled (bl->owner)
3896 && !bl->duplicate)
ce78b96d 3897 {
77b06cd7
TJB
3898 gdb_assert (bl->owner->ops != NULL
3899 && bl->owner->ops->remove_location != NULL);
ce78b96d 3900
73971819 3901 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3902 if (val)
3903 return val;
77b06cd7 3904
b2b6a7da 3905 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3906 }
c906108c
SS
3907
3908 return 0;
3909}
3910
6c95b8df 3911static int
834c0d03 3912remove_breakpoint (struct bp_location *bl)
6c95b8df 3913{
35df4500
TJB
3914 /* BL is never in moribund_locations by our callers. */
3915 gdb_assert (bl->owner != NULL);
2bdf28a0 3916
6c95b8df
PA
3917 /* The type of none suggests that owner is actually deleted.
3918 This should not ever happen. */
35df4500 3919 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3920
5ed8105e 3921 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3922
35df4500 3923 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3924
5ed8105e 3925 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3926}
3927
c906108c
SS
3928/* Clear the "inserted" flag in all breakpoints. */
3929
25b22b0a 3930void
fba45db2 3931mark_breakpoints_out (void)
c906108c 3932{
48d7020b 3933 for (bp_location *bl : all_bp_locations ())
66c4b3e8 3934 if (bl->pspace == current_program_space)
35df4500 3935 bl->inserted = 0;
c906108c
SS
3936}
3937
53a5351d
JM
3938/* Clear the "inserted" flag in all breakpoints and delete any
3939 breakpoints which should go away between runs of the program.
c906108c
SS
3940
3941 Plus other such housekeeping that has to be done for breakpoints
3942 between runs.
3943
53a5351d
JM
3944 Note: this function gets called at the end of a run (by
3945 generic_mourn_inferior) and when a run begins (by
4a64f543 3946 init_wait_for_inferior). */
c906108c
SS
3947
3948
3949
3950void
fba45db2 3951breakpoint_init_inferior (enum inf_context context)
c906108c 3952{
6c95b8df 3953 struct program_space *pspace = current_program_space;
c906108c 3954
50c71eaf
PA
3955 /* If breakpoint locations are shared across processes, then there's
3956 nothing to do. */
f5656ead 3957 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3958 return;
3959
1a853c52 3960 mark_breakpoints_out ();
075f6582 3961
1428b37a 3962 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3963 {
3964 if (b->loc && b->loc->pspace != pspace)
3965 continue;
6c95b8df 3966
01add95b
SM
3967 switch (b->type)
3968 {
3969 case bp_call_dummy:
3970 case bp_longjmp_call_dummy:
c906108c 3971
01add95b
SM
3972 /* If the call dummy breakpoint is at the entry point it will
3973 cause problems when the inferior is rerun, so we better get
3974 rid of it. */
ab92d69b 3975
01add95b 3976 case bp_watchpoint_scope:
ab92d69b 3977
01add95b 3978 /* Also get rid of scope breakpoints. */
ab92d69b 3979
01add95b 3980 case bp_shlib_event:
ab92d69b 3981
01add95b
SM
3982 /* Also remove solib event breakpoints. Their addresses may
3983 have changed since the last time we ran the program.
3984 Actually we may now be debugging against different target;
3985 and so the solib backend that installed this breakpoint may
3986 not be used in by the target. E.g.,
ab92d69b 3987
01add95b
SM
3988 (gdb) file prog-linux
3989 (gdb) run # native linux target
3990 ...
3991 (gdb) kill
3992 (gdb) file prog-win.exe
3993 (gdb) tar rem :9999 # remote Windows gdbserver.
3994 */
c906108c 3995
01add95b 3996 case bp_step_resume:
f59f708a 3997
01add95b 3998 /* Also remove step-resume breakpoints. */
f59f708a 3999
01add95b 4000 case bp_single_step:
7c16b83e 4001
01add95b 4002 /* Also remove single-step breakpoints. */
7c16b83e 4003
01add95b
SM
4004 delete_breakpoint (b);
4005 break;
c906108c 4006
01add95b
SM
4007 case bp_watchpoint:
4008 case bp_hardware_watchpoint:
4009 case bp_read_watchpoint:
4010 case bp_access_watchpoint:
4011 {
4012 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4013
01add95b
SM
4014 /* Likewise for watchpoints on local expressions. */
4015 if (w->exp_valid_block != NULL)
4016 delete_breakpoint (b);
4017 else
4018 {
4019 /* Get rid of existing locations, which are no longer
4020 valid. New ones will be created in
4021 update_watchpoint, when the inferior is restarted.
4022 The next update_global_location_list call will
4023 garbage collect them. */
4024 b->loc = NULL;
4025
4026 if (context == inf_starting)
4027 {
4028 /* Reset val field to force reread of starting value in
4029 insert_breakpoints. */
4030 w->val.reset (nullptr);
4031 w->val_valid = false;
4032 }
4033 }
4034 }
4035 break;
4036 default:
4037 break;
3a5c3e22 4038 }
01add95b 4039 }
1c5cfe86
PA
4040
4041 /* Get rid of the moribund locations. */
1123588c 4042 for (bp_location *bl : moribund_locations)
35df4500 4043 decref_bp_location (&bl);
1123588c 4044 moribund_locations.clear ();
c906108c
SS
4045}
4046
6c95b8df
PA
4047/* These functions concern about actual breakpoints inserted in the
4048 target --- to e.g. check if we need to do decr_pc adjustment or if
4049 we need to hop over the bkpt --- so we check for address space
4050 match, not program space. */
4051
c2c6d25f
JM
4052/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4053 exists at PC. It returns ordinary_breakpoint_here if it's an
4054 ordinary breakpoint, or permanent_breakpoint_here if it's a
4055 permanent breakpoint.
4056 - When continuing from a location with an ordinary breakpoint, we
4057 actually single step once before calling insert_breakpoints.
e5dd4106 4058 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4059 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4060 the target, to advance the PC past the breakpoint. */
c906108c 4061
c2c6d25f 4062enum breakpoint_here
accd0bcd 4063breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4064{
c2c6d25f 4065 int any_breakpoint_here = 0;
c906108c 4066
48d7020b 4067 for (bp_location *bl : all_bp_locations ())
075f6582 4068 {
35df4500
TJB
4069 if (bl->loc_type != bp_loc_software_breakpoint
4070 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4071 continue;
4072
f1310107 4073 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4074 if ((breakpoint_enabled (bl->owner)
1a853c52 4075 || bl->permanent)
f1310107 4076 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4077 {
4078 if (overlay_debugging
35df4500
TJB
4079 && section_is_overlay (bl->section)
4080 && !section_is_mapped (bl->section))
075f6582 4081 continue; /* unmapped overlay -- can't be a match */
1a853c52 4082 else if (bl->permanent)
075f6582
DJ
4083 return permanent_breakpoint_here;
4084 else
4085 any_breakpoint_here = 1;
4086 }
4087 }
c906108c 4088
f486487f 4089 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4090}
4091
d35ae833
PA
4092/* See breakpoint.h. */
4093
4094int
accd0bcd 4095breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4096 CORE_ADDR addr, ULONGEST len)
4097{
48d7020b 4098 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4099 {
4100 if (bl->loc_type != bp_loc_software_breakpoint
4101 && bl->loc_type != bp_loc_hardware_breakpoint)
4102 continue;
4103
4104 if ((breakpoint_enabled (bl->owner)
4105 || bl->permanent)
4106 && breakpoint_location_address_range_overlap (bl, aspace,
4107 addr, len))
4108 {
4109 if (overlay_debugging
4110 && section_is_overlay (bl->section)
4111 && !section_is_mapped (bl->section))
4112 {
4113 /* Unmapped overlay -- can't be a match. */
4114 continue;
4115 }
4116
4117 return 1;
4118 }
4119 }
4120
4121 return 0;
4122}
4123
1c5cfe86
PA
4124/* Return true if there's a moribund breakpoint at PC. */
4125
4126int
accd0bcd 4127moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4128{
1123588c 4129 for (bp_location *loc : moribund_locations)
f1310107 4130 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4131 return 1;
4132
4133 return 0;
4134}
c2c6d25f 4135
f7ce857f
PA
4136/* Returns non-zero iff BL is inserted at PC, in address space
4137 ASPACE. */
4138
4139static int
4140bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4141 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4142{
4143 if (bl->inserted
4144 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4145 aspace, pc))
4146 {
4147 if (overlay_debugging
4148 && section_is_overlay (bl->section)
4149 && !section_is_mapped (bl->section))
4150 return 0; /* unmapped overlay -- can't be a match */
4151 else
4152 return 1;
4153 }
4154 return 0;
4155}
4156
a1fd2fa5 4157/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4158
4159int
accd0bcd 4160breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4161{
e0d9a270 4162 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4163 {
35df4500
TJB
4164 if (bl->loc_type != bp_loc_software_breakpoint
4165 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4166 continue;
4167
f7ce857f
PA
4168 if (bp_location_inserted_here_p (bl, aspace, pc))
4169 return 1;
c5aa993b 4170 }
c36b740a
VP
4171 return 0;
4172}
4173
a1fd2fa5
PA
4174/* This function returns non-zero iff there is a software breakpoint
4175 inserted at PC. */
c36b740a
VP
4176
4177int
accd0bcd 4178software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4179 CORE_ADDR pc)
4fa8626c 4180{
e0d9a270 4181 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4182 {
35df4500 4183 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4184 continue;
4185
f7ce857f
PA
4186 if (bp_location_inserted_here_p (bl, aspace, pc))
4187 return 1;
4fa8626c
DJ
4188 }
4189
4190 return 0;
9c02b525
PA
4191}
4192
4193/* See breakpoint.h. */
4194
4195int
accd0bcd 4196hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4197 CORE_ADDR pc)
4198{
e0d9a270 4199 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4200 {
9c02b525
PA
4201 if (bl->loc_type != bp_loc_hardware_breakpoint)
4202 continue;
4203
4204 if (bp_location_inserted_here_p (bl, aspace, pc))
4205 return 1;
4206 }
4207
4208 return 0;
4fa8626c
DJ
4209}
4210
9093389c 4211int
accd0bcd 4212hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4213 CORE_ADDR addr, ULONGEST len)
4214{
43892fdf 4215 for (breakpoint *bpt : all_breakpoints ())
9093389c 4216 {
9093389c
PA
4217 if (bpt->type != bp_hardware_watchpoint
4218 && bpt->type != bp_access_watchpoint)
4219 continue;
4220
4221 if (!breakpoint_enabled (bpt))
4222 continue;
4223
40cb8ca5 4224 for (bp_location *loc : bpt->locations ())
9093389c
PA
4225 if (loc->pspace->aspace == aspace && loc->inserted)
4226 {
4227 CORE_ADDR l, h;
4228
4229 /* Check for intersection. */
768adc05
PA
4230 l = std::max<CORE_ADDR> (loc->address, addr);
4231 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4232 if (l < h)
4233 return 1;
4234 }
4235 }
4236 return 0;
4237}
c5aa993b 4238
f2478a7e 4239/* See breakpoint.h. */
c906108c 4240
f2478a7e
SM
4241bool
4242is_catchpoint (struct breakpoint *b)
c906108c 4243{
f2478a7e 4244 return (b->type == bp_catchpoint);
c906108c
SS
4245}
4246
c906108c
SS
4247/* Clear a bpstat so that it says we are not at any breakpoint.
4248 Also free any storage that is part of a bpstat. */
4249
4250void
fba45db2 4251bpstat_clear (bpstat *bsp)
c906108c
SS
4252{
4253 bpstat p;
4254 bpstat q;
4255
4256 if (bsp == 0)
4257 return;
4258 p = *bsp;
4259 while (p != NULL)
4260 {
4261 q = p->next;
04afa70c 4262 delete p;
c906108c
SS
4263 p = q;
4264 }
4265 *bsp = NULL;
4266}
4267
04afa70c
TT
4268bpstats::bpstats (const bpstats &other)
4269 : next (NULL),
4270 bp_location_at (other.bp_location_at),
4271 breakpoint_at (other.breakpoint_at),
4272 commands (other.commands),
04afa70c
TT
4273 print (other.print),
4274 stop (other.stop),
4275 print_it (other.print_it)
4276{
850645cf
TT
4277 if (other.old_val != NULL)
4278 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4279}
4280
c906108c
SS
4281/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4282 is part of the bpstat is copied as well. */
4283
4284bpstat
fba45db2 4285bpstat_copy (bpstat bs)
c906108c
SS
4286{
4287 bpstat p = NULL;
4288 bpstat tmp;
4289 bpstat retval = NULL;
4290
4291 if (bs == NULL)
4292 return bs;
4293
4294 for (; bs != NULL; bs = bs->next)
4295 {
04afa70c 4296 tmp = new bpstats (*bs);
31cc81e9 4297
c906108c
SS
4298 if (p == NULL)
4299 /* This is the first thing in the chain. */
4300 retval = tmp;
4301 else
4302 p->next = tmp;
4303 p = tmp;
4304 }
4305 p->next = NULL;
4306 return retval;
4307}
4308
4a64f543 4309/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4310
4311bpstat
fba45db2 4312bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4313{
c5aa993b
JM
4314 if (bsp == NULL)
4315 return NULL;
c906108c 4316
c5aa993b
JM
4317 for (; bsp != NULL; bsp = bsp->next)
4318 {
f431efe5 4319 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4320 return bsp;
4321 }
c906108c
SS
4322 return NULL;
4323}
4324
ab04a2af
TT
4325/* See breakpoint.h. */
4326
4c462cb0 4327bool
427cd150 4328bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4329{
ab04a2af
TT
4330 for (; bsp != NULL; bsp = bsp->next)
4331 {
427cd150
TT
4332 if (bsp->breakpoint_at == NULL)
4333 {
4334 /* A moribund location can never explain a signal other than
4335 GDB_SIGNAL_TRAP. */
4336 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4337 return true;
427cd150
TT
4338 }
4339 else
47591c29
PA
4340 {
4341 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4342 sig))
4c462cb0 4343 return true;
47591c29 4344 }
ab04a2af
TT
4345 }
4346
4c462cb0 4347 return false;
ab04a2af
TT
4348}
4349
4a64f543
MS
4350/* Put in *NUM the breakpoint number of the first breakpoint we are
4351 stopped at. *BSP upon return is a bpstat which points to the
4352 remaining breakpoints stopped at (but which is not guaranteed to be
4353 good for anything but further calls to bpstat_num).
4354
8671a17b
PA
4355 Return 0 if passed a bpstat which does not indicate any breakpoints.
4356 Return -1 if stopped at a breakpoint that has been deleted since
4357 we set it.
4358 Return 1 otherwise. */
c906108c
SS
4359
4360int
8671a17b 4361bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4362{
4363 struct breakpoint *b;
4364
4365 if ((*bsp) == NULL)
4366 return 0; /* No more breakpoint values */
8671a17b 4367
4a64f543
MS
4368 /* We assume we'll never have several bpstats that correspond to a
4369 single breakpoint -- otherwise, this function might return the
4370 same number more than once and this will look ugly. */
f431efe5 4371 b = (*bsp)->breakpoint_at;
8671a17b
PA
4372 *bsp = (*bsp)->next;
4373 if (b == NULL)
4374 return -1; /* breakpoint that's been deleted since */
4375
4376 *num = b->number; /* We have its number */
4377 return 1;
c906108c
SS
4378}
4379
e93ca019 4380/* See breakpoint.h. */
c906108c
SS
4381
4382void
e93ca019 4383bpstat_clear_actions (void)
c906108c 4384{
e93ca019
JK
4385 bpstat bs;
4386
00431a78 4387 if (inferior_ptid == null_ptid)
e93ca019
JK
4388 return;
4389
00431a78 4390 thread_info *tp = inferior_thread ();
e93ca019 4391 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4392 {
d1b0a7bf 4393 bs->commands = NULL;
850645cf 4394 bs->old_val.reset (nullptr);
c906108c
SS
4395 }
4396}
4397
f3b1572e
PA
4398/* Called when a command is about to proceed the inferior. */
4399
4400static void
4401breakpoint_about_to_proceed (void)
4402{
d7e15655 4403 if (inferior_ptid != null_ptid)
f3b1572e
PA
4404 {
4405 struct thread_info *tp = inferior_thread ();
4406
4407 /* Allow inferior function calls in breakpoint commands to not
4408 interrupt the command list. When the call finishes
4409 successfully, the inferior will be standing at the same
4410 breakpoint as if nothing happened. */
16c381f0 4411 if (tp->control.in_infcall)
f3b1572e
PA
4412 return;
4413 }
4414
4415 breakpoint_proceeded = 1;
4416}
4417
abf85f46
JK
4418/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4419 or its equivalent. */
4420
4421static int
4422command_line_is_silent (struct command_line *cmd)
4423{
4f45d445 4424 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4425}
4426
4a64f543
MS
4427/* Execute all the commands associated with all the breakpoints at
4428 this location. Any of these commands could cause the process to
4429 proceed beyond this point, etc. We look out for such changes by
4430 checking the global "breakpoint_proceeded" after each command.
c906108c 4431
347bddb7
PA
4432 Returns true if a breakpoint command resumed the inferior. In that
4433 case, it is the caller's responsibility to recall it again with the
4434 bpstat of the current thread. */
4435
4436static int
4437bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4438{
4439 bpstat bs;
347bddb7 4440 int again = 0;
c906108c
SS
4441
4442 /* Avoid endless recursion if a `source' command is contained
4443 in bs->commands. */
4444 if (executing_breakpoint_commands)
347bddb7 4445 return 0;
c906108c 4446
81b1e71c
TT
4447 scoped_restore save_executing
4448 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4449
1ac32117 4450 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4451
4a64f543 4452 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4453 bs = *bsp;
4454
4455 breakpoint_proceeded = 0;
4456 for (; bs != NULL; bs = bs->next)
4457 {
d1b0a7bf 4458 struct command_line *cmd = NULL;
6c50ab1c
JB
4459
4460 /* Take ownership of the BSP's command tree, if it has one.
4461
dda83cd7
SM
4462 The command tree could legitimately contain commands like
4463 'step' and 'next', which call clear_proceed_status, which
4464 frees stop_bpstat's command tree. To make sure this doesn't
4465 free the tree we're executing out from under us, we need to
4466 take ownership of the tree ourselves. Since a given bpstat's
4467 commands are only executed once, we don't need to copy it; we
4468 can clear the pointer in the bpstat, and make sure we free
4469 the tree when we're done. */
d1b0a7bf 4470 counted_command_line ccmd = bs->commands;
9add0f1b 4471 bs->commands = NULL;
d1b0a7bf
TT
4472 if (ccmd != NULL)
4473 cmd = ccmd.get ();
abf85f46
JK
4474 if (command_line_is_silent (cmd))
4475 {
4476 /* The action has been already done by bpstat_stop_status. */
4477 cmd = cmd->next;
4478 }
6c50ab1c 4479
c906108c
SS
4480 while (cmd != NULL)
4481 {
4482 execute_control_command (cmd);
4483
4484 if (breakpoint_proceeded)
4485 break;
4486 else
4487 cmd = cmd->next;
4488 }
6c50ab1c 4489
c906108c 4490 if (breakpoint_proceeded)
32c1e744 4491 {
cb814510 4492 if (current_ui->async)
347bddb7
PA
4493 /* If we are in async mode, then the target might be still
4494 running, not stopped at any breakpoint, so nothing for
4495 us to do here -- just return to the event loop. */
4496 ;
32c1e744
VP
4497 else
4498 /* In sync mode, when execute_control_command returns
4499 we're already standing on the next breakpoint.
347bddb7
PA
4500 Breakpoint commands for that stop were not run, since
4501 execute_command does not run breakpoint commands --
4502 only command_line_handler does, but that one is not
4503 involved in execution of breakpoint commands. So, we
4504 can now execute breakpoint commands. It should be
4505 noted that making execute_command do bpstat actions is
4506 not an option -- in this case we'll have recursive
4507 invocation of bpstat for each breakpoint with a
4508 command, and can easily blow up GDB stack. Instead, we
4509 return true, which will trigger the caller to recall us
4510 with the new stop_bpstat. */
4511 again = 1;
4512 break;
32c1e744 4513 }
c906108c 4514 }
347bddb7
PA
4515 return again;
4516}
4517
00431a78
PA
4518/* Helper for bpstat_do_actions. Get the current thread, if there's
4519 one, is alive and has execution. Return NULL otherwise. */
4520
4521static thread_info *
4522get_bpstat_thread ()
4523{
55f6301a 4524 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4525 return NULL;
4526
4527 thread_info *tp = inferior_thread ();
4528 if (tp->state == THREAD_EXITED || tp->executing)
4529 return NULL;
4530 return tp;
4531}
4532
347bddb7
PA
4533void
4534bpstat_do_actions (void)
4535{
694c6bf5 4536 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4537 thread_info *tp;
353d1d73 4538
347bddb7 4539 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4540 while ((tp = get_bpstat_thread ()) != NULL)
4541 {
4542 /* Since in sync mode, bpstat_do_actions may resume the
4543 inferior, and only return when it is stopped at the next
4544 breakpoint, we keep doing breakpoint actions until it returns
4545 false to indicate the inferior was not resumed. */
4546 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4547 break;
4548 }
353d1d73 4549
694c6bf5 4550 cleanup_if_error.release ();
c906108c
SS
4551}
4552
fa4727a6
DJ
4553/* Print out the (old or new) value associated with a watchpoint. */
4554
4555static void
4556watchpoint_value_print (struct value *val, struct ui_file *stream)
4557{
4558 if (val == NULL)
7f6aba03 4559 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4560 else
79a45b7d
TT
4561 {
4562 struct value_print_options opts;
4563 get_user_print_options (&opts);
4564 value_print (val, stream, &opts);
4565 }
fa4727a6
DJ
4566}
4567
f303dbd6
PA
4568/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4569 debugging multiple threads. */
4570
4571void
4572maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4573{
112e8700 4574 if (uiout->is_mi_like_p ())
f303dbd6
PA
4575 return;
4576
112e8700 4577 uiout->text ("\n");
f303dbd6
PA
4578
4579 if (show_thread_that_caused_stop ())
4580 {
4581 const char *name;
4582 struct thread_info *thr = inferior_thread ();
4583
112e8700 4584 uiout->text ("Thread ");
33eca680 4585 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4586
4587 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4588 if (name != NULL)
4589 {
112e8700 4590 uiout->text (" \"");
33eca680 4591 uiout->field_string ("name", name);
112e8700 4592 uiout->text ("\"");
f303dbd6
PA
4593 }
4594
112e8700 4595 uiout->text (" hit ");
f303dbd6
PA
4596 }
4597}
4598
e514a9d6 4599/* Generic routine for printing messages indicating why we
4a64f543 4600 stopped. The behavior of this function depends on the value
e514a9d6
JM
4601 'print_it' in the bpstat structure. Under some circumstances we
4602 may decide not to print anything here and delegate the task to
4a64f543 4603 normal_stop(). */
e514a9d6
JM
4604
4605static enum print_stop_action
4606print_bp_stop_message (bpstat bs)
4607{
4608 switch (bs->print_it)
4609 {
4610 case print_it_noop:
4a64f543 4611 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4612 return PRINT_UNKNOWN;
4613 break;
4614
4615 case print_it_done:
4616 /* We still want to print the frame, but we already printed the
dda83cd7 4617 relevant messages. */
e514a9d6
JM
4618 return PRINT_SRC_AND_LOC;
4619 break;
4620
4621 case print_it_normal:
4f8d1dc6 4622 {
f431efe5
PA
4623 struct breakpoint *b = bs->breakpoint_at;
4624
1a6a67de
TJB
4625 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4626 which has since been deleted. */
4627 if (b == NULL)
4628 return PRINT_UNKNOWN;
4629
348d480f
PA
4630 /* Normal case. Call the breakpoint's print_it method. */
4631 return b->ops->print_it (bs);
4f8d1dc6 4632 }
348d480f 4633 break;
3086aeae 4634
e514a9d6 4635 default:
8e65ff28 4636 internal_error (__FILE__, __LINE__,
e2e0b3e5 4637 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4638 break;
c906108c 4639 }
c906108c
SS
4640}
4641
edcc5120
TT
4642/* A helper function that prints a shared library stopped event. */
4643
4644static void
4645print_solib_event (int is_catchpoint)
4646{
6fb16ce6 4647 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4648 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4649
4650 if (!is_catchpoint)
4651 {
4652 if (any_added || any_deleted)
112e8700 4653 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4654 else
112e8700
SM
4655 current_uiout->text (_("Stopped due to shared library event (no "
4656 "libraries added or removed)\n"));
edcc5120
TT
4657 }
4658
112e8700
SM
4659 if (current_uiout->is_mi_like_p ())
4660 current_uiout->field_string ("reason",
4661 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4662
4663 if (any_deleted)
4664 {
112e8700 4665 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4666 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4667 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4668 {
6fb16ce6
SM
4669 const std::string &name = current_program_space->deleted_solibs[ix];
4670
edcc5120 4671 if (ix > 0)
112e8700
SM
4672 current_uiout->text (" ");
4673 current_uiout->field_string ("library", name);
4674 current_uiout->text ("\n");
edcc5120 4675 }
edcc5120
TT
4676 }
4677
4678 if (any_added)
4679 {
112e8700 4680 current_uiout->text (_(" Inferior loaded "));
10f489e5 4681 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4682 bool first = true;
52941706 4683 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4684 {
bcb430e4 4685 if (!first)
112e8700 4686 current_uiout->text (" ");
bcb430e4 4687 first = false;
112e8700
SM
4688 current_uiout->field_string ("library", iter->so_name);
4689 current_uiout->text ("\n");
edcc5120 4690 }
edcc5120
TT
4691 }
4692}
4693
e514a9d6
JM
4694/* Print a message indicating what happened. This is called from
4695 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4696 list - a list of the eventpoints that caused this stop. KIND is
4697 the target_waitkind for the stopping event. This
e514a9d6
JM
4698 routine calls the generic print routine for printing a message
4699 about reasons for stopping. This will print (for example) the
4700 "Breakpoint n," part of the output. The return value of this
4701 routine is one of:
c906108c 4702
4a64f543 4703 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4704 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4705 code to print the location. An example is
c5aa993b
JM
4706 "Breakpoint 1, " which should be followed by
4707 the location.
917317f4 4708 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4709 to also print the location part of the message.
4710 An example is the catch/throw messages, which
4a64f543 4711 don't require a location appended to the end.
917317f4 4712 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4713 further info to be printed. */
c906108c 4714
917317f4 4715enum print_stop_action
36dfb11c 4716bpstat_print (bpstat bs, int kind)
c906108c 4717{
f486487f 4718 enum print_stop_action val;
c5aa993b 4719
c906108c 4720 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4721 (Currently all watchpoints go on the bpstat whether hit or not.
4722 That probably could (should) be changed, provided care is taken
c906108c 4723 with respect to bpstat_explains_signal). */
e514a9d6
JM
4724 for (; bs; bs = bs->next)
4725 {
4726 val = print_bp_stop_message (bs);
4727 if (val == PRINT_SRC_ONLY
4728 || val == PRINT_SRC_AND_LOC
4729 || val == PRINT_NOTHING)
4730 return val;
4731 }
c906108c 4732
36dfb11c
TT
4733 /* If we had hit a shared library event breakpoint,
4734 print_bp_stop_message would print out this message. If we hit an
4735 OS-level shared library event, do the same thing. */
4736 if (kind == TARGET_WAITKIND_LOADED)
4737 {
edcc5120 4738 print_solib_event (0);
36dfb11c
TT
4739 return PRINT_NOTHING;
4740 }
4741
e514a9d6 4742 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4743 with and nothing was printed. */
917317f4 4744 return PRINT_UNKNOWN;
c906108c
SS
4745}
4746
bf469271 4747/* Evaluate the boolean expression EXP and return the result. */
c906108c 4748
bf469271
PA
4749static bool
4750breakpoint_cond_eval (expression *exp)
c906108c 4751{
278cd55f 4752 struct value *mark = value_mark ();
bf469271 4753 bool res = value_true (evaluate_expression (exp));
cc59ec59 4754
c906108c 4755 value_free_to_mark (mark);
bf469271 4756 return res;
c906108c
SS
4757}
4758
5760d0ab 4759/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4760
04afa70c
TT
4761bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4762 : next (NULL),
b6433ede 4763 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4764 breakpoint_at (bl->owner),
4765 commands (NULL),
04afa70c
TT
4766 print (0),
4767 stop (0),
4768 print_it (print_it_normal)
c906108c 4769{
04afa70c
TT
4770 **bs_link_pointer = this;
4771 *bs_link_pointer = &next;
4772}
4773
4774bpstats::bpstats ()
4775 : next (NULL),
04afa70c
TT
4776 breakpoint_at (NULL),
4777 commands (NULL),
04afa70c
TT
4778 print (0),
4779 stop (0),
4780 print_it (print_it_normal)
4781{
c906108c
SS
4782}
4783\f
d983da9c
DJ
4784/* The target has stopped with waitstatus WS. Check if any hardware
4785 watchpoints have triggered, according to the target. */
4786
4787int
4788watchpoints_triggered (struct target_waitstatus *ws)
4789{
57810aa7 4790 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4791 CORE_ADDR addr;
d983da9c
DJ
4792
4793 if (!stopped_by_watchpoint)
4794 {
4795 /* We were not stopped by a watchpoint. Mark all watchpoints
4796 as not triggered. */
43892fdf 4797 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4798 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4799 {
4800 struct watchpoint *w = (struct watchpoint *) b;
4801
4802 w->watchpoint_triggered = watch_triggered_no;
4803 }
d983da9c
DJ
4804
4805 return 0;
4806 }
4807
328d42d8 4808 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4809 {
4810 /* We were stopped by a watchpoint, but we don't know where.
4811 Mark all watchpoints as unknown. */
43892fdf 4812 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4813 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4814 {
4815 struct watchpoint *w = (struct watchpoint *) b;
4816
4817 w->watchpoint_triggered = watch_triggered_unknown;
4818 }
d983da9c 4819
3c4797ba 4820 return 1;
d983da9c
DJ
4821 }
4822
4823 /* The target could report the data address. Mark watchpoints
4824 affected by this data address as triggered, and all others as not
4825 triggered. */
4826
43892fdf 4827 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4828 if (is_hardware_watchpoint (b))
d983da9c 4829 {
3a5c3e22 4830 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4831
3a5c3e22 4832 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4833 for (bp_location *loc : b->locations ())
9c06b0b4 4834 {
3a5c3e22 4835 if (is_masked_watchpoint (b))
9c06b0b4 4836 {
3a5c3e22
PA
4837 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4838 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4839
4840 if (newaddr == start)
4841 {
3a5c3e22 4842 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4843 break;
4844 }
4845 }
4846 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4847 else if (target_watchpoint_addr_within_range
4848 (current_inferior ()->top_target (), addr, loc->address,
4849 loc->length))
9c06b0b4 4850 {
3a5c3e22 4851 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4852 break;
4853 }
4854 }
d983da9c
DJ
4855 }
4856
4857 return 1;
4858}
4859
bf469271
PA
4860/* Possible return values for watchpoint_check. */
4861enum wp_check_result
4862 {
4863 /* The watchpoint has been deleted. */
4864 WP_DELETED = 1,
4865
4866 /* The value has changed. */
4867 WP_VALUE_CHANGED = 2,
4868
4869 /* The value has not changed. */
4870 WP_VALUE_NOT_CHANGED = 3,
4871
4872 /* Ignore this watchpoint, no matter if the value changed or not. */
4873 WP_IGNORE = 4,
4874 };
c906108c
SS
4875
4876#define BP_TEMPFLAG 1
4877#define BP_HARDWAREFLAG 2
4878
4a64f543 4879/* Evaluate watchpoint condition expression and check if its value
bf469271 4880 changed. */
553e4c11 4881
bf469271
PA
4882static wp_check_result
4883watchpoint_check (bpstat bs)
c906108c 4884{
3a5c3e22 4885 struct watchpoint *b;
c906108c
SS
4886 struct frame_info *fr;
4887 int within_current_scope;
4888
f431efe5 4889 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4890 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4891 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4892
f6bc2008
PA
4893 /* If this is a local watchpoint, we only want to check if the
4894 watchpoint frame is in scope if the current thread is the thread
4895 that was used to create the watchpoint. */
4896 if (!watchpoint_in_thread_scope (b))
60e1c644 4897 return WP_IGNORE;
f6bc2008 4898
c906108c
SS
4899 if (b->exp_valid_block == NULL)
4900 within_current_scope = 1;
4901 else
4902 {
edb3359d
DJ
4903 struct frame_info *frame = get_current_frame ();
4904 struct gdbarch *frame_arch = get_frame_arch (frame);
4905 CORE_ADDR frame_pc = get_frame_pc (frame);
4906
c9cf6e20 4907 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4908 still in the function but the stack frame has already been
4909 invalidated. Since we can't rely on the values of local
4910 variables after the stack has been destroyed, we are treating
4911 the watchpoint in that state as `not changed' without further
4912 checking. Don't mark watchpoints as changed if the current
4913 frame is in an epilogue - even if they are in some other
4914 frame, our view of the stack is likely to be wrong and
4915 frame_find_by_id could error out. */
c9cf6e20 4916 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4917 return WP_IGNORE;
a0f49112 4918
101dcfbe 4919 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4920 within_current_scope = (fr != NULL);
69fbadd5
DJ
4921
4922 /* If we've gotten confused in the unwinder, we might have
4923 returned a frame that can't describe this variable. */
edb3359d
DJ
4924 if (within_current_scope)
4925 {
4926 struct symbol *function;
4927
4928 function = get_frame_function (fr);
4929 if (function == NULL
4930 || !contained_in (b->exp_valid_block,
4931 SYMBOL_BLOCK_VALUE (function)))
4932 within_current_scope = 0;
4933 }
69fbadd5 4934
edb3359d 4935 if (within_current_scope)
c906108c
SS
4936 /* If we end up stopping, the current frame will get selected
4937 in normal_stop. So this call to select_frame won't affect
4938 the user. */
0f7d239c 4939 select_frame (fr);
c906108c 4940 }
c5aa993b 4941
c906108c
SS
4942 if (within_current_scope)
4943 {
4a64f543 4944 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
4945 time before we return to the command level and call
4946 free_all_values. We can't call free_all_values because we
4947 might be in the middle of evaluating a function call. */
c906108c 4948
9c06b0b4 4949 struct value *mark;
fa4727a6
DJ
4950 struct value *new_val;
4951
c1fc2657 4952 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4953 /* Since we don't know the exact trigger address (from
4954 stopped_data_address), just tell the user we've triggered
4955 a mask watchpoint. */
4956 return WP_VALUE_CHANGED;
4957
4958 mark = value_mark ();
1eaebe02 4959 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 4960 NULL, NULL, false);
218d2fc6 4961
bb9d5f81
PP
4962 if (b->val_bitsize != 0)
4963 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4964
4a64f543
MS
4965 /* We use value_equal_contents instead of value_equal because
4966 the latter coerces an array to a pointer, thus comparing just
4967 the address of the array instead of its contents. This is
4968 not what we want. */
fa4727a6 4969 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4970 || (b->val != NULL && !value_equal_contents (b->val.get (),
4971 new_val)))
c906108c 4972 {
c906108c 4973 bs->old_val = b->val;
850645cf 4974 b->val = release_value (new_val);
4c1d86d9 4975 b->val_valid = true;
850645cf
TT
4976 if (new_val != NULL)
4977 value_free_to_mark (mark);
c906108c
SS
4978 return WP_VALUE_CHANGED;
4979 }
4980 else
4981 {
60e1c644 4982 /* Nothing changed. */
c906108c 4983 value_free_to_mark (mark);
c906108c
SS
4984 return WP_VALUE_NOT_CHANGED;
4985 }
4986 }
4987 else
4988 {
4989 /* This seems like the only logical thing to do because
dda83cd7
SM
4990 if we temporarily ignored the watchpoint, then when
4991 we reenter the block in which it is valid it contains
4992 garbage (in the case of a function, it may have two
4993 garbage values, one before and one after the prologue).
4994 So we can't even detect the first assignment to it and
4995 watch after that (since the garbage may or may not equal
4996 the first value assigned). */
348d480f
PA
4997 /* We print all the stop information in
4998 breakpoint_ops->print_it, but in this case, by the time we
4999 call breakpoint_ops->print_it this bp will be deleted
5000 already. So we have no choice but print the information
5001 here. */
468afe6c 5002
0e454242 5003 SWITCH_THRU_ALL_UIS ()
dda83cd7 5004 {
468afe6c
PA
5005 struct ui_out *uiout = current_uiout;
5006
112e8700
SM
5007 if (uiout->is_mi_like_p ())
5008 uiout->field_string
5009 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5010 uiout->message ("\nWatchpoint %pF deleted because the program has "
5011 "left the block in\n"
5012 "which its expression is valid.\n",
5013 signed_field ("wpnum", b->number));
468afe6c 5014 }
4ce44c66 5015
cdac0397 5016 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5017 b->commands = NULL;
d0fb5eae 5018 watchpoint_del_at_next_stop (b);
c906108c
SS
5019
5020 return WP_DELETED;
5021 }
5022}
5023
18a18393 5024/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5025 breakpoint location BL. This function does not check if we should
5026 stop, only if BL explains the stop. */
5027
18a18393 5028static int
6c95b8df 5029bpstat_check_location (const struct bp_location *bl,
accd0bcd 5030 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5031 const struct target_waitstatus *ws)
18a18393
VP
5032{
5033 struct breakpoint *b = bl->owner;
5034
348d480f 5035 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5036 gdb_assert (b != NULL);
5037
bd522513 5038 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5039}
5040
3a5c3e22
PA
5041/* Determine if the watched values have actually changed, and we
5042 should stop. If not, set BS->stop to 0. */
5043
18a18393
VP
5044static void
5045bpstat_check_watchpoint (bpstat bs)
5046{
2bdf28a0 5047 const struct bp_location *bl;
3a5c3e22 5048 struct watchpoint *b;
2bdf28a0
JK
5049
5050 /* BS is built for existing struct breakpoint. */
b6433ede 5051 bl = bs->bp_location_at.get ();
2bdf28a0 5052 gdb_assert (bl != NULL);
3a5c3e22 5053 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5054 gdb_assert (b != NULL);
18a18393 5055
18a18393 5056 {
18a18393
VP
5057 int must_check_value = 0;
5058
c1fc2657 5059 if (b->type == bp_watchpoint)
18a18393
VP
5060 /* For a software watchpoint, we must always check the
5061 watched value. */
5062 must_check_value = 1;
5063 else if (b->watchpoint_triggered == watch_triggered_yes)
5064 /* We have a hardware watchpoint (read, write, or access)
5065 and the target earlier reported an address watched by
5066 this watchpoint. */
5067 must_check_value = 1;
5068 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5069 && b->type == bp_hardware_watchpoint)
18a18393
VP
5070 /* We were stopped by a hardware watchpoint, but the target could
5071 not report the data address. We must check the watchpoint's
5072 value. Access and read watchpoints are out of luck; without
5073 a data address, we can't figure it out. */
5074 must_check_value = 1;
3a5c3e22 5075
18a18393
VP
5076 if (must_check_value)
5077 {
bf469271
PA
5078 wp_check_result e;
5079
a70b8144 5080 try
bf469271
PA
5081 {
5082 e = watchpoint_check (bs);
5083 }
230d2906 5084 catch (const gdb_exception &ex)
bf469271
PA
5085 {
5086 exception_fprintf (gdb_stderr, ex,
5087 "Error evaluating expression "
5088 "for watchpoint %d\n",
5089 b->number);
5090
5091 SWITCH_THRU_ALL_UIS ()
5092 {
5093 printf_filtered (_("Watchpoint %d deleted.\n"),
5094 b->number);
5095 }
5096 watchpoint_del_at_next_stop (b);
5097 e = WP_DELETED;
5098 }
bf469271 5099
18a18393
VP
5100 switch (e)
5101 {
5102 case WP_DELETED:
5103 /* We've already printed what needs to be printed. */
5104 bs->print_it = print_it_done;
5105 /* Stop. */
5106 break;
60e1c644
PA
5107 case WP_IGNORE:
5108 bs->print_it = print_it_noop;
5109 bs->stop = 0;
5110 break;
18a18393 5111 case WP_VALUE_CHANGED:
c1fc2657 5112 if (b->type == bp_read_watchpoint)
18a18393 5113 {
85d721b8
PA
5114 /* There are two cases to consider here:
5115
4a64f543 5116 1. We're watching the triggered memory for reads.
85d721b8
PA
5117 In that case, trust the target, and always report
5118 the watchpoint hit to the user. Even though
5119 reads don't cause value changes, the value may
5120 have changed since the last time it was read, and
5121 since we're not trapping writes, we will not see
5122 those, and as such we should ignore our notion of
5123 old value.
5124
4a64f543 5125 2. We're watching the triggered memory for both
85d721b8
PA
5126 reads and writes. There are two ways this may
5127 happen:
5128
4a64f543 5129 2.1. This is a target that can't break on data
85d721b8
PA
5130 reads only, but can break on accesses (reads or
5131 writes), such as e.g., x86. We detect this case
5132 at the time we try to insert read watchpoints.
5133
4a64f543 5134 2.2. Otherwise, the target supports read
85d721b8
PA
5135 watchpoints, but, the user set an access or write
5136 watchpoint watching the same memory as this read
5137 watchpoint.
5138
5139 If we're watching memory writes as well as reads,
5140 ignore watchpoint hits when we find that the
5141 value hasn't changed, as reads don't cause
5142 changes. This still gives false positives when
5143 the program writes the same value to memory as
5144 what there was already in memory (we will confuse
5145 it for a read), but it's much better than
5146 nothing. */
5147
5148 int other_write_watchpoint = 0;
5149
5150 if (bl->watchpoint_type == hw_read)
5151 {
43892fdf 5152 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5153 if (other_b->type == bp_hardware_watchpoint
5154 || other_b->type == bp_access_watchpoint)
85d721b8 5155 {
3a5c3e22
PA
5156 struct watchpoint *other_w =
5157 (struct watchpoint *) other_b;
5158
5159 if (other_w->watchpoint_triggered
5160 == watch_triggered_yes)
5161 {
5162 other_write_watchpoint = 1;
5163 break;
5164 }
85d721b8
PA
5165 }
5166 }
5167
5168 if (other_write_watchpoint
5169 || bl->watchpoint_type == hw_access)
5170 {
5171 /* We're watching the same memory for writes,
5172 and the value changed since the last time we
5173 updated it, so this trap must be for a write.
5174 Ignore it. */
5175 bs->print_it = print_it_noop;
5176 bs->stop = 0;
5177 }
18a18393
VP
5178 }
5179 break;
5180 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5181 if (b->type == bp_hardware_watchpoint
5182 || b->type == bp_watchpoint)
18a18393
VP
5183 {
5184 /* Don't stop: write watchpoints shouldn't fire if
5185 the value hasn't changed. */
5186 bs->print_it = print_it_noop;
5187 bs->stop = 0;
5188 }
5189 /* Stop. */
5190 break;
5191 default:
5192 /* Can't happen. */
18a18393
VP
5193 break;
5194 }
5195 }
5196 else /* must_check_value == 0 */
5197 {
5198 /* This is a case where some watchpoint(s) triggered, but
5199 not at the address of this watchpoint, or else no
5200 watchpoint triggered after all. So don't print
5201 anything for this watchpoint. */
5202 bs->print_it = print_it_noop;
5203 bs->stop = 0;
5204 }
5205 }
5206}
5207
7d4df6a4
DE
5208/* For breakpoints that are currently marked as telling gdb to stop,
5209 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5210 of breakpoint referred to by BS. If we should not stop for this
5211 breakpoint, set BS->stop to 0. */
f431efe5 5212
18a18393 5213static void
00431a78 5214bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5215{
2bdf28a0
JK
5216 const struct bp_location *bl;
5217 struct breakpoint *b;
bf469271
PA
5218 /* Assume stop. */
5219 bool condition_result = true;
7d4df6a4
DE
5220 struct expression *cond;
5221
5222 gdb_assert (bs->stop);
2bdf28a0
JK
5223
5224 /* BS is built for existing struct breakpoint. */
b6433ede 5225 bl = bs->bp_location_at.get ();
2bdf28a0 5226 gdb_assert (bl != NULL);
f431efe5 5227 b = bs->breakpoint_at;
2bdf28a0 5228 gdb_assert (b != NULL);
18a18393 5229
b775012e
LM
5230 /* Even if the target evaluated the condition on its end and notified GDB, we
5231 need to do so again since GDB does not know if we stopped due to a
5232 breakpoint or a single step breakpoint. */
5233
18a18393 5234 if (frame_id_p (b->frame_id)
edb3359d 5235 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5236 {
7d4df6a4
DE
5237 bs->stop = 0;
5238 return;
5239 }
60e1c644 5240
12ab52e9
PA
5241 /* If this is a thread/task-specific breakpoint, don't waste cpu
5242 evaluating the condition if this isn't the specified
5243 thread/task. */
00431a78
PA
5244 if ((b->thread != -1 && b->thread != thread->global_num)
5245 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5246 {
5247 bs->stop = 0;
5248 return;
5249 }
5250
6dddc817
DE
5251 /* Evaluate extension language breakpoints that have a "stop" method
5252 implemented. */
5253 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5254
7d4df6a4
DE
5255 if (is_watchpoint (b))
5256 {
5257 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5258
4d01a485 5259 cond = w->cond_exp.get ();
7d4df6a4
DE
5260 }
5261 else
4d01a485 5262 cond = bl->cond.get ();
60e1c644 5263
7d4df6a4
DE
5264 if (cond && b->disposition != disp_del_at_next_stop)
5265 {
5266 int within_current_scope = 1;
5267 struct watchpoint * w;
60e1c644 5268
7d4df6a4
DE
5269 /* We use value_mark and value_free_to_mark because it could
5270 be a long time before we return to the command level and
5271 call free_all_values. We can't call free_all_values
5272 because we might be in the middle of evaluating a
5273 function call. */
5274 struct value *mark = value_mark ();
5275
5276 if (is_watchpoint (b))
5277 w = (struct watchpoint *) b;
5278 else
5279 w = NULL;
5280
5281 /* Need to select the frame, with all that implies so that
5282 the conditions will have the right context. Because we
5283 use the frame, we will not see an inlined function's
5284 variables when we arrive at a breakpoint at the start
5285 of the inlined function; the current frame will be the
5286 call site. */
5287 if (w == NULL || w->cond_exp_valid_block == NULL)
5288 select_frame (get_current_frame ());
5289 else
18a18393 5290 {
7d4df6a4
DE
5291 struct frame_info *frame;
5292
5293 /* For local watchpoint expressions, which particular
5294 instance of a local is being watched matters, so we
5295 keep track of the frame to evaluate the expression
5296 in. To evaluate the condition however, it doesn't
5297 really matter which instantiation of the function
5298 where the condition makes sense triggers the
5299 watchpoint. This allows an expression like "watch
5300 global if q > 10" set in `func', catch writes to
5301 global on all threads that call `func', or catch
5302 writes on all recursive calls of `func' by a single
5303 thread. We simply always evaluate the condition in
5304 the innermost frame that's executing where it makes
5305 sense to evaluate the condition. It seems
5306 intuitive. */
5307 frame = block_innermost_frame (w->cond_exp_valid_block);
5308 if (frame != NULL)
5309 select_frame (frame);
5310 else
5311 within_current_scope = 0;
18a18393 5312 }
7d4df6a4 5313 if (within_current_scope)
bf469271 5314 {
a70b8144 5315 try
bf469271
PA
5316 {
5317 condition_result = breakpoint_cond_eval (cond);
5318 }
230d2906 5319 catch (const gdb_exception &ex)
bf469271
PA
5320 {
5321 exception_fprintf (gdb_stderr, ex,
5322 "Error in testing breakpoint condition:\n");
5323 }
bf469271 5324 }
7d4df6a4 5325 else
18a18393 5326 {
7d4df6a4
DE
5327 warning (_("Watchpoint condition cannot be tested "
5328 "in the current scope"));
5329 /* If we failed to set the right context for this
5330 watchpoint, unconditionally report it. */
18a18393 5331 }
7d4df6a4
DE
5332 /* FIXME-someday, should give breakpoint #. */
5333 value_free_to_mark (mark);
18a18393 5334 }
7d4df6a4 5335
bf469271 5336 if (cond && !condition_result)
7d4df6a4
DE
5337 {
5338 bs->stop = 0;
5339 }
7d4df6a4
DE
5340 else if (b->ignore_count > 0)
5341 {
5342 b->ignore_count--;
5343 bs->stop = 0;
5344 /* Increase the hit count even though we don't stop. */
5345 ++(b->hit_count);
76727919 5346 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5347 }
18a18393
VP
5348}
5349
1cf4d951
PA
5350/* Returns true if we need to track moribund locations of LOC's type
5351 on the current target. */
5352
5353static int
5354need_moribund_for_location_type (struct bp_location *loc)
5355{
5356 return ((loc->loc_type == bp_loc_software_breakpoint
5357 && !target_supports_stopped_by_sw_breakpoint ())
5358 || (loc->loc_type == bp_loc_hardware_breakpoint
5359 && !target_supports_stopped_by_hw_breakpoint ()));
5360}
5361
ddfe970e 5362/* See breakpoint.h. */
c906108c
SS
5363
5364bpstat
ddfe970e 5365build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5366 const struct target_waitstatus *ws)
c906108c 5367{
5760d0ab 5368 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5369
43892fdf 5370 for (breakpoint *b : all_breakpoints ())
429374b8 5371 {
1a853c52 5372 if (!breakpoint_enabled (b))
429374b8 5373 continue;
a5606eee 5374
40cb8ca5 5375 for (bp_location *bl : b->locations ())
429374b8 5376 {
4a64f543
MS
5377 /* For hardware watchpoints, we look only at the first
5378 location. The watchpoint_check function will work on the
5379 entire expression, not the individual locations. For
5380 read watchpoints, the watchpoints_triggered function has
5381 checked all locations already. */
429374b8
JK
5382 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5383 break;
18a18393 5384
b5fa468f 5385 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5386 continue;
c5aa993b 5387
09ac7c10 5388 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5389 continue;
c5aa993b 5390
4a64f543
MS
5391 /* Come here if it's a watchpoint, or if the break address
5392 matches. */
c5aa993b 5393
ddfe970e
KS
5394 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5395 explain stop. */
c5aa993b 5396
f431efe5
PA
5397 /* Assume we stop. Should we find a watchpoint that is not
5398 actually triggered, or if the condition of the breakpoint
5399 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5400 bs->stop = 1;
5401 bs->print = 1;
d983da9c 5402
f431efe5
PA
5403 /* If this is a scope breakpoint, mark the associated
5404 watchpoint as triggered so that we will handle the
5405 out-of-scope event. We'll get to the watchpoint next
5406 iteration. */
d0fb5eae 5407 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5408 {
5409 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5410
5411 w->watchpoint_triggered = watch_triggered_yes;
5412 }
f431efe5
PA
5413 }
5414 }
5415
7c16b83e 5416 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5417 if (!target_supports_stopped_by_sw_breakpoint ()
5418 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5419 {
1123588c 5420 for (bp_location *loc : moribund_locations)
f431efe5 5421 {
1cf4d951
PA
5422 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5423 && need_moribund_for_location_type (loc))
5424 {
ddfe970e 5425 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5426 /* For hits of moribund locations, we should just proceed. */
5427 bs->stop = 0;
5428 bs->print = 0;
5429 bs->print_it = print_it_noop;
5430 }
f431efe5
PA
5431 }
5432 }
5433
ddfe970e
KS
5434 return bs_head;
5435}
5436
5437/* See breakpoint.h. */
5438
5439bpstat
5440bpstat_stop_status (const address_space *aspace,
00431a78 5441 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5442 const struct target_waitstatus *ws,
5443 bpstat stop_chain)
5444{
5445 struct breakpoint *b = NULL;
5446 /* First item of allocated bpstat's. */
5447 bpstat bs_head = stop_chain;
5448 bpstat bs;
5449 int need_remove_insert;
5450 int removed_any;
5451
5452 /* First, build the bpstat chain with locations that explain a
5453 target stop, while being careful to not set the target running,
5454 as that may invalidate locations (in particular watchpoint
5455 locations are recreated). Resuming will happen here with
5456 breakpoint conditions or watchpoint expressions that include
5457 inferior function calls. */
5458 if (bs_head == NULL)
5459 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5460
edcc5120
TT
5461 /* A bit of special processing for shlib breakpoints. We need to
5462 process solib loading here, so that the lists of loaded and
5463 unloaded libraries are correct before we handle "catch load" and
5464 "catch unload". */
5465 for (bs = bs_head; bs != NULL; bs = bs->next)
5466 {
5d268276 5467 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5468 {
5469 handle_solib_event ();
5470 break;
5471 }
5472 }
5473
f431efe5
PA
5474 /* Now go through the locations that caused the target to stop, and
5475 check whether we're interested in reporting this stop to higher
5476 layers, or whether we should resume the target transparently. */
5477
5478 removed_any = 0;
5479
5760d0ab 5480 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5481 {
5482 if (!bs->stop)
5483 continue;
5484
f431efe5 5485 b = bs->breakpoint_at;
348d480f
PA
5486 b->ops->check_status (bs);
5487 if (bs->stop)
28010a5d 5488 {
00431a78 5489 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5490
429374b8
JK
5491 if (bs->stop)
5492 {
5493 ++(b->hit_count);
76727919 5494 gdb::observers::breakpoint_modified.notify (b);
c906108c 5495
4a64f543 5496 /* We will stop here. */
429374b8
JK
5497 if (b->disposition == disp_disable)
5498 {
816338b5 5499 --(b->enable_count);
1a853c52 5500 if (b->enable_count <= 0)
429374b8 5501 b->enable_state = bp_disabled;
f431efe5 5502 removed_any = 1;
429374b8
JK
5503 }
5504 if (b->silent)
5505 bs->print = 0;
5506 bs->commands = b->commands;
abf85f46 5507 if (command_line_is_silent (bs->commands
d1b0a7bf 5508 ? bs->commands.get () : NULL))
abf85f46 5509 bs->print = 0;
9d6e6e84
HZ
5510
5511 b->ops->after_condition_true (bs);
429374b8
JK
5512 }
5513
348d480f 5514 }
a9b3a50f
PA
5515
5516 /* Print nothing for this entry if we don't stop or don't
5517 print. */
5518 if (!bs->stop || !bs->print)
5519 bs->print_it = print_it_noop;
429374b8 5520 }
876fa593 5521
d983da9c
DJ
5522 /* If we aren't stopping, the value of some hardware watchpoint may
5523 not have changed, but the intermediate memory locations we are
5524 watching may have. Don't bother if we're stopping; this will get
5525 done later. */
d832cb68 5526 need_remove_insert = 0;
5760d0ab
JK
5527 if (! bpstat_causes_stop (bs_head))
5528 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5529 if (!bs->stop
f431efe5
PA
5530 && bs->breakpoint_at
5531 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5532 {
3a5c3e22
PA
5533 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5534
5535 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5536 need_remove_insert = 1;
d983da9c
DJ
5537 }
5538
d832cb68 5539 if (need_remove_insert)
44702360 5540 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5541 else if (removed_any)
44702360 5542 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5543
5760d0ab 5544 return bs_head;
c906108c 5545}
628fe4e4
JK
5546
5547static void
61c26be8 5548handle_jit_event (CORE_ADDR address)
628fe4e4 5549{
628fe4e4
JK
5550 struct gdbarch *gdbarch;
5551
1eb8556f 5552 infrun_debug_printf ("handling bp_jit_event");
243a9253 5553
628fe4e4
JK
5554 /* Switch terminal for any messages produced by
5555 breakpoint_re_set. */
223ffa71 5556 target_terminal::ours_for_output ();
628fe4e4 5557
61c26be8
MS
5558 gdbarch = get_frame_arch (get_current_frame ());
5559 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5560 thus it is expected that its objectfile can be found through
5561 minimal symbol lookup. If it doesn't work (and assert fails), it
5562 most likely means that `jit_breakpoint_re_set` was changes and this
5563 function needs to be updated too. */
5564 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5565 gdb_assert (jit_bp_sym.objfile != nullptr);
5566 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5567
223ffa71 5568 target_terminal::inferior ();
628fe4e4
JK
5569}
5570
5571/* Prepare WHAT final decision for infrun. */
5572
5573/* Decide what infrun needs to do with this bpstat. */
5574
c906108c 5575struct bpstat_what
0e30163f 5576bpstat_what (bpstat bs_head)
c906108c 5577{
c906108c 5578 struct bpstat_what retval;
0e30163f 5579 bpstat bs;
c906108c 5580
628fe4e4 5581 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5582 retval.call_dummy = STOP_NONE;
e2d0f980 5583 retval.is_longjmp = false;
628fe4e4 5584
0e30163f 5585 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5586 {
628fe4e4
JK
5587 /* Extract this BS's action. After processing each BS, we check
5588 if its action overrides all we've seem so far. */
5589 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5590 enum bptype bptype;
5591
c906108c 5592 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5593 {
5594 /* I suspect this can happen if it was a momentary
5595 breakpoint which has since been deleted. */
5596 bptype = bp_none;
5597 }
20874c92 5598 else
f431efe5 5599 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5600
5601 switch (bptype)
c906108c
SS
5602 {
5603 case bp_none:
628fe4e4 5604 break;
c906108c
SS
5605 case bp_breakpoint:
5606 case bp_hardware_breakpoint:
7c16b83e 5607 case bp_single_step:
c906108c
SS
5608 case bp_until:
5609 case bp_finish:
a9b3a50f 5610 case bp_shlib_event:
c906108c
SS
5611 if (bs->stop)
5612 {
5613 if (bs->print)
628fe4e4 5614 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5615 else
628fe4e4 5616 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5617 }
5618 else
628fe4e4 5619 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5620 break;
5621 case bp_watchpoint:
5622 case bp_hardware_watchpoint:
5623 case bp_read_watchpoint:
5624 case bp_access_watchpoint:
5625 if (bs->stop)
5626 {
5627 if (bs->print)
628fe4e4 5628 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5629 else
628fe4e4 5630 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5631 }
5632 else
628fe4e4
JK
5633 {
5634 /* There was a watchpoint, but we're not stopping.
5635 This requires no further action. */
5636 }
c906108c
SS
5637 break;
5638 case bp_longjmp:
e2e4d78b 5639 case bp_longjmp_call_dummy:
186c406b 5640 case bp_exception:
0a39bb32
PA
5641 if (bs->stop)
5642 {
5643 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5644 retval.is_longjmp = bptype != bp_exception;
5645 }
5646 else
5647 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5648 break;
5649 case bp_longjmp_resume:
186c406b 5650 case bp_exception_resume:
0a39bb32
PA
5651 if (bs->stop)
5652 {
5653 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5654 retval.is_longjmp = bptype == bp_longjmp_resume;
5655 }
5656 else
5657 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5658 break;
5659 case bp_step_resume:
5660 if (bs->stop)
628fe4e4
JK
5661 this_action = BPSTAT_WHAT_STEP_RESUME;
5662 else
c906108c 5663 {
628fe4e4
JK
5664 /* It is for the wrong frame. */
5665 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5666 }
c906108c 5667 break;
2c03e5be
PA
5668 case bp_hp_step_resume:
5669 if (bs->stop)
5670 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5671 else
5672 {
5673 /* It is for the wrong frame. */
5674 this_action = BPSTAT_WHAT_SINGLE;
5675 }
5676 break;
c906108c 5677 case bp_watchpoint_scope:
c4093a6a 5678 case bp_thread_event:
1900040c 5679 case bp_overlay_event:
0fd8e87f 5680 case bp_longjmp_master:
aa7d318d 5681 case bp_std_terminate_master:
186c406b 5682 case bp_exception_master:
628fe4e4 5683 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5684 break;
ce78b96d 5685 case bp_catchpoint:
c5aa993b
JM
5686 if (bs->stop)
5687 {
5688 if (bs->print)
628fe4e4 5689 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5690 else
628fe4e4 5691 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5692 }
5693 else
628fe4e4 5694 {
cb1e4e32
PA
5695 /* Some catchpoints are implemented with breakpoints.
5696 For those, we need to step over the breakpoint. */
5697 if (bs->bp_location_at->loc_type != bp_loc_other)
5698 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5699 }
5700 break;
628fe4e4 5701 case bp_jit_event:
628fe4e4 5702 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5703 break;
c906108c 5704 case bp_call_dummy:
53a5351d
JM
5705 /* Make sure the action is stop (silent or noisy),
5706 so infrun.c pops the dummy frame. */
aa7d318d 5707 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5708 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5709 break;
5710 case bp_std_terminate:
5711 /* Make sure the action is stop (silent or noisy),
5712 so infrun.c pops the dummy frame. */
aa7d318d 5713 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5714 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5715 break;
1042e4c0 5716 case bp_tracepoint:
7a697b8d 5717 case bp_fast_tracepoint:
0fb4aa4b 5718 case bp_static_tracepoint:
1042e4c0
SS
5719 /* Tracepoint hits should not be reported back to GDB, and
5720 if one got through somehow, it should have been filtered
5721 out already. */
5722 internal_error (__FILE__, __LINE__,
7a697b8d 5723 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5724 break;
5725 case bp_gnu_ifunc_resolver:
5726 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5727 this_action = BPSTAT_WHAT_SINGLE;
5728 break;
5729 case bp_gnu_ifunc_resolver_return:
5730 /* The breakpoint will be removed, execution will restart from the
5731 PC of the former breakpoint. */
5732 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5733 break;
e7e0cddf
SS
5734
5735 case bp_dprintf:
a11cfd87
HZ
5736 if (bs->stop)
5737 this_action = BPSTAT_WHAT_STOP_SILENT;
5738 else
5739 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5740 break;
5741
628fe4e4
JK
5742 default:
5743 internal_error (__FILE__, __LINE__,
5744 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5745 }
628fe4e4 5746
325fac50 5747 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5748 }
628fe4e4 5749
243a9253
PA
5750 return retval;
5751}
628fe4e4 5752
243a9253
PA
5753void
5754bpstat_run_callbacks (bpstat bs_head)
5755{
5756 bpstat bs;
628fe4e4 5757
0e30163f
JK
5758 for (bs = bs_head; bs != NULL; bs = bs->next)
5759 {
5760 struct breakpoint *b = bs->breakpoint_at;
5761
5762 if (b == NULL)
5763 continue;
5764 switch (b->type)
5765 {
243a9253 5766 case bp_jit_event:
61c26be8 5767 handle_jit_event (bs->bp_location_at->address);
243a9253 5768 break;
0e30163f
JK
5769 case bp_gnu_ifunc_resolver:
5770 gnu_ifunc_resolver_stop (b);
5771 break;
5772 case bp_gnu_ifunc_resolver_return:
5773 gnu_ifunc_resolver_return_stop (b);
5774 break;
5775 }
5776 }
c906108c
SS
5777}
5778
4c462cb0 5779/* See breakpoint.h. */
c906108c 5780
4c462cb0
SM
5781bool
5782bpstat_should_step ()
c906108c 5783{
43892fdf 5784 for (breakpoint *b : all_breakpoints ())
717a8278 5785 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5786 return true;
43892fdf 5787
4c462cb0 5788 return false;
c906108c
SS
5789}
5790
4c462cb0
SM
5791/* See breakpoint.h. */
5792
5793bool
67822962
PA
5794bpstat_causes_stop (bpstat bs)
5795{
5796 for (; bs != NULL; bs = bs->next)
5797 if (bs->stop)
4c462cb0 5798 return true;
67822962 5799
4c462cb0 5800 return false;
67822962
PA
5801}
5802
c906108c 5803\f
c5aa993b 5804
170b53b2
UW
5805/* Compute a string of spaces suitable to indent the next line
5806 so it starts at the position corresponding to the table column
5807 named COL_NAME in the currently active table of UIOUT. */
5808
5809static char *
5810wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5811{
5812 static char wrap_indent[80];
5813 int i, total_width, width, align;
c5209615 5814 const char *text;
170b53b2
UW
5815
5816 total_width = 0;
112e8700 5817 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5818 {
5819 if (strcmp (text, col_name) == 0)
5820 {
5821 gdb_assert (total_width < sizeof wrap_indent);
5822 memset (wrap_indent, ' ', total_width);
5823 wrap_indent[total_width] = 0;
5824
5825 return wrap_indent;
5826 }
5827
5828 total_width += width + 1;
5829 }
5830
5831 return NULL;
5832}
5833
b775012e
LM
5834/* Determine if the locations of this breakpoint will have their conditions
5835 evaluated by the target, host or a mix of both. Returns the following:
5836
5837 "host": Host evals condition.
5838 "host or target": Host or Target evals condition.
5839 "target": Target evals condition.
5840*/
5841
5842static const char *
5843bp_condition_evaluator (struct breakpoint *b)
5844{
b775012e
LM
5845 char host_evals = 0;
5846 char target_evals = 0;
5847
5848 if (!b)
5849 return NULL;
5850
5851 if (!is_breakpoint (b))
5852 return NULL;
5853
5854 if (gdb_evaluates_breakpoint_condition_p ()
5855 || !target_supports_evaluation_of_breakpoint_conditions ())
5856 return condition_evaluation_host;
5857
40cb8ca5 5858 for (bp_location *bl : b->locations ())
b775012e
LM
5859 {
5860 if (bl->cond_bytecode)
5861 target_evals++;
5862 else
5863 host_evals++;
5864 }
5865
5866 if (host_evals && target_evals)
5867 return condition_evaluation_both;
5868 else if (target_evals)
5869 return condition_evaluation_target;
5870 else
5871 return condition_evaluation_host;
5872}
5873
5874/* Determine the breakpoint location's condition evaluator. This is
5875 similar to bp_condition_evaluator, but for locations. */
5876
5877static const char *
5878bp_location_condition_evaluator (struct bp_location *bl)
5879{
5880 if (bl && !is_breakpoint (bl->owner))
5881 return NULL;
5882
5883 if (gdb_evaluates_breakpoint_condition_p ()
5884 || !target_supports_evaluation_of_breakpoint_conditions ())
5885 return condition_evaluation_host;
5886
5887 if (bl && bl->cond_bytecode)
5888 return condition_evaluation_target;
5889 else
5890 return condition_evaluation_host;
5891}
5892
859825b8
JK
5893/* Print the LOC location out of the list of B->LOC locations. */
5894
170b53b2
UW
5895static void
5896print_breakpoint_location (struct breakpoint *b,
5897 struct bp_location *loc)
0d381245 5898{
79a45e25 5899 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5900
5901 scoped_restore_current_program_space restore_pspace;
6c95b8df 5902
859825b8
JK
5903 if (loc != NULL && loc->shlib_disabled)
5904 loc = NULL;
5905
6c95b8df
PA
5906 if (loc != NULL)
5907 set_current_program_space (loc->pspace);
5908
56435ebe 5909 if (b->display_canonical)
d28cd78a 5910 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5911 else if (loc && loc->symtab)
0d381245 5912 {
4a27f119
KS
5913 const struct symbol *sym = loc->symbol;
5914
0d381245
VP
5915 if (sym)
5916 {
112e8700 5917 uiout->text ("in ");
987012b8 5918 uiout->field_string ("func", sym->print_name (),
e43b10e1 5919 function_name_style.style ());
112e8700
SM
5920 uiout->text (" ");
5921 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5922 uiout->text ("at ");
0d381245 5923 }
112e8700 5924 uiout->field_string ("file",
cbe56571 5925 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5926 file_name_style.style ());
112e8700 5927 uiout->text (":");
05cba821 5928
112e8700
SM
5929 if (uiout->is_mi_like_p ())
5930 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5931
381befee 5932 uiout->field_signed ("line", loc->line_number);
0d381245 5933 }
859825b8 5934 else if (loc)
0d381245 5935 {
d7e74731 5936 string_file stb;
170b53b2 5937
d7e74731 5938 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5939 demangle, "");
112e8700 5940 uiout->field_stream ("at", stb);
0d381245 5941 }
859825b8 5942 else
f00aae0f 5943 {
d28cd78a
TT
5944 uiout->field_string ("pending",
5945 event_location_to_string (b->location.get ()));
f00aae0f
KS
5946 /* If extra_string is available, it could be holding a condition
5947 or dprintf arguments. In either case, make sure it is printed,
5948 too, but only for non-MI streams. */
112e8700 5949 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5950 {
5951 if (b->type == bp_dprintf)
112e8700 5952 uiout->text (",");
f00aae0f 5953 else
112e8700
SM
5954 uiout->text (" ");
5955 uiout->text (b->extra_string);
f00aae0f
KS
5956 }
5957 }
6c95b8df 5958
b775012e
LM
5959 if (loc && is_breakpoint (b)
5960 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5961 && bp_condition_evaluator (b) == condition_evaluation_both)
5962 {
112e8700
SM
5963 uiout->text (" (");
5964 uiout->field_string ("evaluated-by",
b775012e 5965 bp_location_condition_evaluator (loc));
112e8700 5966 uiout->text (")");
b775012e 5967 }
0d381245
VP
5968}
5969
269b11a2
PA
5970static const char *
5971bptype_string (enum bptype type)
c906108c 5972{
c4093a6a
JM
5973 struct ep_type_description
5974 {
5975 enum bptype type;
a121b7c1 5976 const char *description;
c4093a6a
JM
5977 };
5978 static struct ep_type_description bptypes[] =
c906108c 5979 {
c5aa993b
JM
5980 {bp_none, "?deleted?"},
5981 {bp_breakpoint, "breakpoint"},
c906108c 5982 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5983 {bp_single_step, "sw single-step"},
c5aa993b
JM
5984 {bp_until, "until"},
5985 {bp_finish, "finish"},
5986 {bp_watchpoint, "watchpoint"},
c906108c 5987 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5988 {bp_read_watchpoint, "read watchpoint"},
5989 {bp_access_watchpoint, "acc watchpoint"},
5990 {bp_longjmp, "longjmp"},
5991 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5992 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5993 {bp_exception, "exception"},
5994 {bp_exception_resume, "exception resume"},
c5aa993b 5995 {bp_step_resume, "step resume"},
2c03e5be 5996 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5997 {bp_watchpoint_scope, "watchpoint scope"},
5998 {bp_call_dummy, "call dummy"},
aa7d318d 5999 {bp_std_terminate, "std::terminate"},
c5aa993b 6000 {bp_shlib_event, "shlib events"},
c4093a6a 6001 {bp_thread_event, "thread events"},
1900040c 6002 {bp_overlay_event, "overlay events"},
0fd8e87f 6003 {bp_longjmp_master, "longjmp master"},
aa7d318d 6004 {bp_std_terminate_master, "std::terminate master"},
186c406b 6005 {bp_exception_master, "exception master"},
ce78b96d 6006 {bp_catchpoint, "catchpoint"},
1042e4c0 6007 {bp_tracepoint, "tracepoint"},
7a697b8d 6008 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6009 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6010 {bp_dprintf, "dprintf"},
4efc6507 6011 {bp_jit_event, "jit events"},
0e30163f
JK
6012 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6013 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6014 };
269b11a2
PA
6015
6016 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6017 || ((int) type != bptypes[(int) type].type))
6018 internal_error (__FILE__, __LINE__,
6019 _("bptypes table does not describe type #%d."),
6020 (int) type);
6021
6022 return bptypes[(int) type].description;
6023}
6024
998580f1
MK
6025/* For MI, output a field named 'thread-groups' with a list as the value.
6026 For CLI, prefix the list with the string 'inf'. */
6027
6028static void
6029output_thread_groups (struct ui_out *uiout,
6030 const char *field_name,
5c632425 6031 const std::vector<int> &inf_nums,
998580f1
MK
6032 int mi_only)
6033{
112e8700 6034 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6035
6036 /* For backward compatibility, don't display inferiors in CLI unless
6037 there are several. Always display them for MI. */
6038 if (!is_mi && mi_only)
6039 return;
6040
10f489e5 6041 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6042
5c632425 6043 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6044 {
6045 if (is_mi)
6046 {
6047 char mi_group[10];
6048
5c632425 6049 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6050 uiout->field_string (NULL, mi_group);
998580f1
MK
6051 }
6052 else
6053 {
6054 if (i == 0)
112e8700 6055 uiout->text (" inf ");
998580f1 6056 else
112e8700 6057 uiout->text (", ");
998580f1 6058
5c632425 6059 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6060 }
6061 }
998580f1
MK
6062}
6063
a38118e5
PA
6064/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6065 instead of going via breakpoint_ops::print_one. This makes "maint
6066 info breakpoints" show the software breakpoint locations of
6067 catchpoints, which are considered internal implementation
6068 detail. */
269b11a2
PA
6069
6070static void
6071print_one_breakpoint_location (struct breakpoint *b,
6072 struct bp_location *loc,
6073 int loc_number,
6074 struct bp_location **last_loc,
a38118e5 6075 int allflag, bool raw_loc)
269b11a2
PA
6076{
6077 struct command_line *l;
c2c6d25f 6078 static char bpenables[] = "nynny";
c906108c 6079
79a45e25 6080 struct ui_out *uiout = current_uiout;
0d381245
VP
6081 int header_of_multiple = 0;
6082 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6083 struct value_print_options opts;
6084
6085 get_user_print_options (&opts);
0d381245
VP
6086
6087 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6088 /* See comment in print_one_breakpoint concerning treatment of
6089 breakpoints with single disabled location. */
0d381245
VP
6090 if (loc == NULL
6091 && (b->loc != NULL
b5fa468f
TBA
6092 && (b->loc->next != NULL
6093 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6094 header_of_multiple = 1;
6095 if (loc == NULL)
6096 loc = b->loc;
6097
c4093a6a
JM
6098 annotate_record ();
6099
6100 /* 1 */
6101 annotate_field (0);
0d381245 6102 if (part_of_multiple)
528e1572 6103 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6104 else
381befee 6105 uiout->field_signed ("number", b->number);
c4093a6a
JM
6106
6107 /* 2 */
6108 annotate_field (1);
0d381245 6109 if (part_of_multiple)
112e8700 6110 uiout->field_skip ("type");
269b11a2 6111 else
112e8700 6112 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6113
6114 /* 3 */
6115 annotate_field (2);
0d381245 6116 if (part_of_multiple)
112e8700 6117 uiout->field_skip ("disp");
0d381245 6118 else
112e8700 6119 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6120
c4093a6a
JM
6121 /* 4 */
6122 annotate_field (3);
85c88e2a
TBA
6123 /* For locations that are disabled because of an invalid condition,
6124 display "N*" on CLI, where "*" refers to a footnote below the
6125 table. For MI, simply display a "N" without a footnote. */
6126 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6127 if (part_of_multiple)
85c88e2a 6128 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6129 : (loc->enabled ? "y" : "n")));
0d381245 6130 else
112e8700 6131 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6132
c4093a6a 6133 /* 5 and 6 */
a38118e5 6134 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6135 b->ops->print_one (b, last_loc);
3086aeae 6136 else
a38118e5
PA
6137 {
6138 if (is_watchpoint (b))
3a5c3e22
PA
6139 {
6140 struct watchpoint *w = (struct watchpoint *) b;
6141
6142 /* Field 4, the address, is omitted (which makes the columns
6143 not line up too nicely with the headers, but the effect
6144 is relatively readable). */
6145 if (opts.addressprint)
112e8700 6146 uiout->field_skip ("addr");
3a5c3e22 6147 annotate_field (5);
112e8700 6148 uiout->field_string ("what", w->exp_string);
3a5c3e22 6149 }
f06f1252
TT
6150 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6151 || is_ada_exception_catchpoint (b))
a38118e5
PA
6152 {
6153 if (opts.addressprint)
6154 {
6155 annotate_field (4);
6156 if (header_of_multiple)
7f6aba03
TT
6157 uiout->field_string ("addr", "<MULTIPLE>",
6158 metadata_style.style ());
a38118e5 6159 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6160 uiout->field_string ("addr", "<PENDING>",
6161 metadata_style.style ());
a38118e5
PA
6162 else
6163 uiout->field_core_addr ("addr",
6164 loc->gdbarch, loc->address);
6165 }
6166 annotate_field (5);
6167 if (!header_of_multiple)
6168 print_breakpoint_location (b, loc);
6169 if (b->loc)
6170 *last_loc = b->loc;
6171 }
6172 }
6c95b8df 6173
998580f1 6174 if (loc != NULL && !header_of_multiple)
6c95b8df 6175 {
5c632425 6176 std::vector<int> inf_nums;
998580f1 6177 int mi_only = 1;
6c95b8df 6178
08036331 6179 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6180 {
6181 if (inf->pspace == loc->pspace)
5c632425 6182 inf_nums.push_back (inf->num);
6c95b8df 6183 }
998580f1 6184
dda83cd7 6185 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6186 there are several. Always display for MI. */
6187 if (allflag
6188 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6189 && (program_spaces.size () > 1
998580f1
MK
6190 || number_of_inferiors () > 1)
6191 /* LOC is for existing B, it cannot be in
6192 moribund_locations and thus having NULL OWNER. */
6193 && loc->owner->type != bp_catchpoint))
6194 mi_only = 0;
5c632425 6195 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6196 }
6197
4a306c9a 6198 if (!part_of_multiple)
c4093a6a 6199 {
4a306c9a
JB
6200 if (b->thread != -1)
6201 {
6202 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6203 "stop only in" line a little further down. */
112e8700 6204 uiout->text (" thread ");
381befee 6205 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6206 }
6207 else if (b->task != 0)
6208 {
112e8700 6209 uiout->text (" task ");
381befee 6210 uiout->field_signed ("task", b->task);
4a306c9a 6211 }
c4093a6a 6212 }
f1310107 6213
112e8700 6214 uiout->text ("\n");
f1310107 6215
348d480f 6216 if (!part_of_multiple)
f1310107
TJB
6217 b->ops->print_one_detail (b, uiout);
6218
0d381245 6219 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6220 {
6221 annotate_field (6);
112e8700 6222 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6223 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6224 the frame ID. */
112e8700 6225 uiout->field_core_addr ("frame",
5af949e3 6226 b->gdbarch, b->frame_id.stack_addr);
112e8700 6227 uiout->text ("\n");
c4093a6a
JM
6228 }
6229
28010a5d 6230 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6231 {
6232 annotate_field (7);
d77f58be 6233 if (is_tracepoint (b))
112e8700 6234 uiout->text ("\ttrace only if ");
1042e4c0 6235 else
112e8700
SM
6236 uiout->text ("\tstop only if ");
6237 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6238
6239 /* Print whether the target is doing the breakpoint's condition
6240 evaluation. If GDB is doing the evaluation, don't print anything. */
6241 if (is_breakpoint (b)
6242 && breakpoint_condition_evaluation_mode ()
6243 == condition_evaluation_target)
6244 {
6a831f06
PA
6245 uiout->message (" (%pF evals)",
6246 string_field ("evaluated-by",
6247 bp_condition_evaluator (b)));
b775012e 6248 }
112e8700 6249 uiout->text ("\n");
0101ce28
JJ
6250 }
6251
0d381245 6252 if (!part_of_multiple && b->thread != -1)
c4093a6a 6253 {
4a64f543 6254 /* FIXME should make an annotation for this. */
112e8700
SM
6255 uiout->text ("\tstop only in thread ");
6256 if (uiout->is_mi_like_p ())
381befee 6257 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6258 else
6259 {
6260 struct thread_info *thr = find_thread_global_id (b->thread);
6261
112e8700 6262 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6263 }
112e8700 6264 uiout->text ("\n");
c4093a6a
JM
6265 }
6266
556ec64d
YQ
6267 if (!part_of_multiple)
6268 {
6269 if (b->hit_count)
31f56a27
YQ
6270 {
6271 /* FIXME should make an annotation for this. */
6272 if (is_catchpoint (b))
112e8700 6273 uiout->text ("\tcatchpoint");
31f56a27 6274 else if (is_tracepoint (b))
112e8700 6275 uiout->text ("\ttracepoint");
31f56a27 6276 else
112e8700
SM
6277 uiout->text ("\tbreakpoint");
6278 uiout->text (" already hit ");
381befee 6279 uiout->field_signed ("times", b->hit_count);
31f56a27 6280 if (b->hit_count == 1)
112e8700 6281 uiout->text (" time\n");
31f56a27 6282 else
112e8700 6283 uiout->text (" times\n");
31f56a27 6284 }
556ec64d
YQ
6285 else
6286 {
31f56a27 6287 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6288 if (uiout->is_mi_like_p ())
381befee 6289 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6290 }
6291 }
8b93c638 6292
0d381245 6293 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6294 {
6295 annotate_field (8);
6a831f06
PA
6296 uiout->message ("\tignore next %pF hits\n",
6297 signed_field ("ignore", b->ignore_count));
c4093a6a 6298 }
059fb39f 6299
816338b5
SS
6300 /* Note that an enable count of 1 corresponds to "enable once"
6301 behavior, which is reported by the combination of enablement and
6302 disposition, so we don't need to mention it here. */
6303 if (!part_of_multiple && b->enable_count > 1)
6304 {
6305 annotate_field (8);
112e8700 6306 uiout->text ("\tdisable after ");
816338b5
SS
6307 /* Tweak the wording to clarify that ignore and enable counts
6308 are distinct, and have additive effect. */
6309 if (b->ignore_count)
112e8700 6310 uiout->text ("additional ");
816338b5 6311 else
112e8700 6312 uiout->text ("next ");
381befee 6313 uiout->field_signed ("enable", b->enable_count);
112e8700 6314 uiout->text (" hits\n");
816338b5
SS
6315 }
6316
f196051f
SS
6317 if (!part_of_multiple && is_tracepoint (b))
6318 {
6319 struct tracepoint *tp = (struct tracepoint *) b;
6320
6321 if (tp->traceframe_usage)
6322 {
112e8700 6323 uiout->text ("\ttrace buffer usage ");
381befee 6324 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6325 uiout->text (" bytes\n");
f196051f
SS
6326 }
6327 }
d3ce09f5 6328
d1b0a7bf 6329 l = b->commands ? b->commands.get () : NULL;
059fb39f 6330 if (!part_of_multiple && l)
c4093a6a
JM
6331 {
6332 annotate_field (9);
2e783024 6333 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6334 print_command_lines (uiout, l, 4);
c4093a6a 6335 }
d24317b4 6336
d9b3f62e 6337 if (is_tracepoint (b))
1042e4c0 6338 {
d9b3f62e
PA
6339 struct tracepoint *t = (struct tracepoint *) b;
6340
6341 if (!part_of_multiple && t->pass_count)
6342 {
6343 annotate_field (10);
112e8700 6344 uiout->text ("\tpass count ");
381befee 6345 uiout->field_signed ("pass", t->pass_count);
112e8700 6346 uiout->text (" \n");
d9b3f62e 6347 }
f2a8bc8a
YQ
6348
6349 /* Don't display it when tracepoint or tracepoint location is
6350 pending. */
6351 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6352 {
6353 annotate_field (11);
6354
112e8700
SM
6355 if (uiout->is_mi_like_p ())
6356 uiout->field_string ("installed",
f2a8bc8a
YQ
6357 loc->inserted ? "y" : "n");
6358 else
6359 {
6360 if (loc->inserted)
112e8700 6361 uiout->text ("\t");
f2a8bc8a 6362 else
112e8700
SM
6363 uiout->text ("\tnot ");
6364 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6365 }
6366 }
1042e4c0
SS
6367 }
6368
112e8700 6369 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6370 {
3a5c3e22
PA
6371 if (is_watchpoint (b))
6372 {
6373 struct watchpoint *w = (struct watchpoint *) b;
6374
112e8700 6375 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6376 }
f00aae0f 6377 else if (b->location != NULL
d28cd78a 6378 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6379 uiout->field_string ("original-location",
d28cd78a 6380 event_location_to_string (b->location.get ()));
d24317b4 6381 }
c4093a6a 6382}
c5aa993b 6383
13674803
SM
6384/* See breakpoint.h. */
6385
6386bool fix_multi_location_breakpoint_output_globally = false;
6387
0d381245
VP
6388static void
6389print_one_breakpoint (struct breakpoint *b,
4a64f543 6390 struct bp_location **last_loc,
6c95b8df 6391 int allflag)
0d381245 6392{
79a45e25 6393 struct ui_out *uiout = current_uiout;
13674803
SM
6394 bool use_fixed_output
6395 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6396 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6397
b4be1b06 6398 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6399 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6400
b4be1b06
SM
6401 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6402 are outside. */
6403 if (!use_fixed_output)
6404 bkpt_tuple_emitter.reset ();
0d381245
VP
6405
6406 /* If this breakpoint has custom print function,
6407 it's already printed. Otherwise, print individual
6408 locations, if any. */
a38118e5
PA
6409 if (b->ops == NULL
6410 || b->ops->print_one == NULL
6411 || allflag)
0d381245 6412 {
4a64f543
MS
6413 /* If breakpoint has a single location that is disabled, we
6414 print it as if it had several locations, since otherwise it's
6415 hard to represent "breakpoint enabled, location disabled"
6416 situation.
6417
6418 Note that while hardware watchpoints have several locations
a38118e5
PA
6419 internally, that's not a property exposed to users.
6420
6421 Likewise, while catchpoints may be implemented with
6422 breakpoints (e.g., catch throw), that's not a property
6423 exposed to users. We do however display the internal
6424 breakpoint locations with "maint info breakpoints". */
6425 if (!is_hardware_watchpoint (b)
f06f1252
TT
6426 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6427 || is_ada_exception_catchpoint (b))
a38118e5 6428 && (allflag
b5fa468f
TBA
6429 || (b->loc && (b->loc->next
6430 || !b->loc->enabled
6431 || b->loc->disabled_by_cond))))
0d381245 6432 {
b4be1b06
SM
6433 gdb::optional<ui_out_emit_list> locations_list;
6434
6435 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6436 MI record. For later versions, place breakpoint locations in a
6437 list. */
6438 if (uiout->is_mi_like_p () && use_fixed_output)
6439 locations_list.emplace (uiout, "locations");
8d3788bd 6440
b4be1b06 6441 int n = 1;
40cb8ca5 6442 for (bp_location *loc : b->locations ())
8d3788bd 6443 {
b4be1b06 6444 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6445 print_one_breakpoint_location (b, loc, n, last_loc,
6446 allflag, allflag);
40cb8ca5 6447 n++;
8d3788bd 6448 }
0d381245
VP
6449 }
6450 }
6451}
6452
a6d9a66e
UW
6453static int
6454breakpoint_address_bits (struct breakpoint *b)
6455{
6456 int print_address_bits = 0;
a6d9a66e 6457
c6d81124
PA
6458 /* Software watchpoints that aren't watching memory don't have an
6459 address to print. */
6460 if (is_no_memory_software_watchpoint (b))
6461 return 0;
6462
40cb8ca5 6463 for (bp_location *loc : b->locations ())
a6d9a66e 6464 {
c7437ca6
PA
6465 int addr_bit;
6466
c7437ca6 6467 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6468 if (addr_bit > print_address_bits)
6469 print_address_bits = addr_bit;
6470 }
6471
6472 return print_address_bits;
6473}
0d381245 6474
65630365 6475/* See breakpoint.h. */
c5aa993b 6476
65630365
PA
6477void
6478print_breakpoint (breakpoint *b)
c4093a6a 6479{
a6d9a66e 6480 struct bp_location *dummy_loc = NULL;
65630365 6481 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6482}
c5aa993b 6483
09d682a4
TT
6484/* Return true if this breakpoint was set by the user, false if it is
6485 internal or momentary. */
6486
6487int
6488user_breakpoint_p (struct breakpoint *b)
6489{
46c6471b 6490 return b->number > 0;
09d682a4
TT
6491}
6492
93daf339
TT
6493/* See breakpoint.h. */
6494
6495int
6496pending_breakpoint_p (struct breakpoint *b)
6497{
6498 return b->loc == NULL;
6499}
6500
5c458ae8
SM
6501/* Print information on breakpoints (including watchpoints and tracepoints).
6502
6503 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6504 understood by number_or_range_parser. Only breakpoints included in this
6505 list are then printed.
6506
6507 If SHOW_INTERNAL is true, print internal breakpoints.
6508
6509 If FILTER is non-NULL, call it on each breakpoint and only include the
6510 ones for which it returns true.
6511
6512 Return the total number of breakpoints listed. */
c906108c 6513
d77f58be 6514static int
5c458ae8 6515breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6516 bool (*filter) (const struct breakpoint *))
c4093a6a 6517{
a6d9a66e 6518 struct bp_location *last_loc = NULL;
7f3b0473 6519 int nr_printable_breakpoints;
79a45b7d 6520 struct value_print_options opts;
a6d9a66e 6521 int print_address_bits = 0;
269b11a2 6522 int print_type_col_width = 14;
79a45e25 6523 struct ui_out *uiout = current_uiout;
b5fa468f 6524 bool has_disabled_by_cond_location = false;
269b11a2 6525
79a45b7d
TT
6526 get_user_print_options (&opts);
6527
4a64f543
MS
6528 /* Compute the number of rows in the table, as well as the size
6529 required for address fields. */
7f3b0473 6530 nr_printable_breakpoints = 0;
43892fdf 6531 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6532 {
6533 /* If we have a filter, only list the breakpoints it accepts. */
6534 if (filter && !filter (b))
6535 continue;
6536
5c458ae8 6537 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6538 accept. Skip the others. */
5c458ae8 6539 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6540 {
5c458ae8 6541 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6542 continue;
5c458ae8 6543 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6544 continue;
6545 }
269b11a2 6546
5c458ae8 6547 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6548 {
6549 int addr_bit, type_len;
a6d9a66e 6550
e5a67952
MS
6551 addr_bit = breakpoint_address_bits (b);
6552 if (addr_bit > print_address_bits)
6553 print_address_bits = addr_bit;
269b11a2 6554
e5a67952
MS
6555 type_len = strlen (bptype_string (b->type));
6556 if (type_len > print_type_col_width)
6557 print_type_col_width = type_len;
6558
6559 nr_printable_breakpoints++;
6560 }
6561 }
7f3b0473 6562
4a2b031d
TT
6563 {
6564 ui_out_emit_table table_emitter (uiout,
6565 opts.addressprint ? 6 : 5,
6566 nr_printable_breakpoints,
6567 "BreakpointTable");
6568
6569 if (nr_printable_breakpoints > 0)
6570 annotate_breakpoints_headers ();
6571 if (nr_printable_breakpoints > 0)
6572 annotate_field (0);
6573 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6574 if (nr_printable_breakpoints > 0)
6575 annotate_field (1);
6576 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6577 if (nr_printable_breakpoints > 0)
6578 annotate_field (2);
6579 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6580 if (nr_printable_breakpoints > 0)
6581 annotate_field (3);
6582 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6583 if (opts.addressprint)
6584 {
6585 if (nr_printable_breakpoints > 0)
6586 annotate_field (4);
6587 if (print_address_bits <= 32)
6588 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6589 else
6590 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6591 }
6592 if (nr_printable_breakpoints > 0)
6593 annotate_field (5);
6594 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6595 uiout->table_body ();
6596 if (nr_printable_breakpoints > 0)
6597 annotate_breakpoints_table ();
6598
43892fdf 6599 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6600 {
6601 QUIT;
6602 /* If we have a filter, only list the breakpoints it accepts. */
6603 if (filter && !filter (b))
6604 continue;
e5a67952 6605
5c458ae8 6606 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6607 accept. Skip the others. */
e5a67952 6608
5c458ae8 6609 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6610 {
5c458ae8 6611 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6612 {
5c458ae8 6613 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6614 continue;
6615 }
6616 else /* all others */
6617 {
5c458ae8 6618 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6619 continue;
6620 }
6621 }
6622 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6623 show_internal is set. */
6624 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6625 {
6626 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6627 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6628 if (loc->disabled_by_cond)
6629 has_disabled_by_cond_location = true;
6630 }
4a2b031d
TT
6631 }
6632 }
698384cd 6633
7f3b0473 6634 if (nr_printable_breakpoints == 0)
c906108c 6635 {
4a64f543
MS
6636 /* If there's a filter, let the caller decide how to report
6637 empty list. */
d77f58be
SS
6638 if (!filter)
6639 {
5c458ae8 6640 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6641 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6642 else
112e8700 6643 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6644 bp_num_list);
d77f58be 6645 }
c906108c
SS
6646 }
6647 else
c4093a6a 6648 {
a6d9a66e
UW
6649 if (last_loc && !server_command)
6650 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6651
85c88e2a 6652 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6653 uiout->message (_("(*): Breakpoint condition is invalid at this "
6654 "location.\n"));
c4093a6a 6655 }
c906108c 6656
4a64f543 6657 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6658 there have been breakpoints? */
c906108c 6659 annotate_breakpoints_table_end ();
d77f58be
SS
6660
6661 return nr_printable_breakpoints;
c906108c
SS
6662}
6663
ad443146
SS
6664/* Display the value of default-collect in a way that is generally
6665 compatible with the breakpoint list. */
6666
6667static void
6668default_collect_info (void)
6669{
79a45e25
PA
6670 struct ui_out *uiout = current_uiout;
6671
ad443146
SS
6672 /* If it has no value (which is frequently the case), say nothing; a
6673 message like "No default-collect." gets in user's face when it's
6674 not wanted. */
6675 if (!*default_collect)
6676 return;
6677
6678 /* The following phrase lines up nicely with per-tracepoint collect
6679 actions. */
112e8700
SM
6680 uiout->text ("default collect ");
6681 uiout->field_string ("default-collect", default_collect);
6682 uiout->text (" \n");
ad443146
SS
6683}
6684
c906108c 6685static void
0b39b52e 6686info_breakpoints_command (const char *args, int from_tty)
c906108c 6687{
5c458ae8 6688 breakpoint_1 (args, false, NULL);
ad443146
SS
6689
6690 default_collect_info ();
d77f58be
SS
6691}
6692
6693static void
1d12d88f 6694info_watchpoints_command (const char *args, int from_tty)
d77f58be 6695{
5c458ae8 6696 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6697 struct ui_out *uiout = current_uiout;
d77f58be
SS
6698
6699 if (num_printed == 0)
6700 {
e5a67952 6701 if (args == NULL || *args == '\0')
112e8700 6702 uiout->message ("No watchpoints.\n");
d77f58be 6703 else
112e8700 6704 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6705 }
c906108c
SS
6706}
6707
7a292a7a 6708static void
4495129a 6709maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6710{
5c458ae8 6711 breakpoint_1 (args, true, NULL);
ad443146
SS
6712
6713 default_collect_info ();
c906108c
SS
6714}
6715
0d381245 6716static int
714835d5 6717breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6718 struct program_space *pspace,
714835d5 6719 CORE_ADDR pc, struct obj_section *section)
0d381245 6720{
40cb8ca5 6721 for (bp_location *bl : b->locations ())
0d381245 6722 {
6c95b8df
PA
6723 if (bl->pspace == pspace
6724 && bl->address == pc
0d381245
VP
6725 && (!overlay_debugging || bl->section == section))
6726 return 1;
6727 }
6728 return 0;
6729}
6730
672f9b60 6731/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6732 concerns with logical breakpoints, so we match program spaces, not
6733 address spaces. */
c906108c
SS
6734
6735static void
6c95b8df
PA
6736describe_other_breakpoints (struct gdbarch *gdbarch,
6737 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6738 struct obj_section *section, int thread)
c906108c 6739{
52f0bd74 6740 int others = 0;
c906108c 6741
43892fdf 6742 for (breakpoint *b : all_breakpoints ())
672f9b60 6743 others += (user_breakpoint_p (b)
dda83cd7 6744 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6745
c906108c
SS
6746 if (others > 0)
6747 {
a3f17187
AC
6748 if (others == 1)
6749 printf_filtered (_("Note: breakpoint "));
6750 else /* if (others == ???) */
6751 printf_filtered (_("Note: breakpoints "));
43892fdf 6752 for (breakpoint *b : all_breakpoints ())
672f9b60 6753 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6754 {
6755 others--;
6756 printf_filtered ("%d", b->number);
6757 if (b->thread == -1 && thread != -1)
6758 printf_filtered (" (all threads)");
6759 else if (b->thread != -1)
6760 printf_filtered (" (thread %d)", b->thread);
6761 printf_filtered ("%s%s ",
059fb39f 6762 ((b->enable_state == bp_disabled
f8eba3c6 6763 || b->enable_state == bp_call_disabled)
0d381245 6764 ? " (disabled)"
0d381245
VP
6765 : ""),
6766 (others > 1) ? ","
6767 : ((others == 1) ? " and" : ""));
6768 }
6a831f06
PA
6769 current_uiout->message (_("also set at pc %ps.\n"),
6770 styled_string (address_style.style (),
6771 paddress (gdbarch, pc)));
c906108c
SS
6772 }
6773}
6774\f
c906108c 6775
cb1e4e32
PA
6776/* Return true iff it is meaningful to use the address member of LOC.
6777 For some breakpoint types, the locations' address members are
6778 irrelevant and it makes no sense to attempt to compare them to
6779 other addresses (or use them for any other purpose either).
2d134ed3 6780
cb1e4e32
PA
6781 More specifically, software watchpoints and catchpoints that are
6782 not backed by breakpoints always have a zero valued location
6783 address and we don't want to mark breakpoints of any of these types
6784 to be a duplicate of an actual breakpoint location at address
6785 zero. */
e4f237da 6786
cb1e4e32
PA
6787static bool
6788bl_address_is_meaningful (bp_location *loc)
e4f237da 6789{
cb1e4e32 6790 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6791}
6792
6793/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6794 true if LOC1 and LOC2 represent the same watchpoint location. */
6795
6796static int
4a64f543
MS
6797watchpoint_locations_match (struct bp_location *loc1,
6798 struct bp_location *loc2)
2d134ed3 6799{
3a5c3e22
PA
6800 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6801 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6802
6803 /* Both of them must exist. */
6804 gdb_assert (w1 != NULL);
6805 gdb_assert (w2 != NULL);
2bdf28a0 6806
4a64f543
MS
6807 /* If the target can evaluate the condition expression in hardware,
6808 then we we need to insert both watchpoints even if they are at
6809 the same place. Otherwise the watchpoint will only trigger when
6810 the condition of whichever watchpoint was inserted evaluates to
6811 true, not giving a chance for GDB to check the condition of the
6812 other watchpoint. */
3a5c3e22 6813 if ((w1->cond_exp
4a64f543
MS
6814 && target_can_accel_watchpoint_condition (loc1->address,
6815 loc1->length,
0cf6dd15 6816 loc1->watchpoint_type,
4d01a485 6817 w1->cond_exp.get ()))
3a5c3e22 6818 || (w2->cond_exp
4a64f543
MS
6819 && target_can_accel_watchpoint_condition (loc2->address,
6820 loc2->length,
0cf6dd15 6821 loc2->watchpoint_type,
4d01a485 6822 w2->cond_exp.get ())))
0cf6dd15
TJB
6823 return 0;
6824
85d721b8
PA
6825 /* Note that this checks the owner's type, not the location's. In
6826 case the target does not support read watchpoints, but does
6827 support access watchpoints, we'll have bp_read_watchpoint
6828 watchpoints with hw_access locations. Those should be considered
6829 duplicates of hw_read locations. The hw_read locations will
6830 become hw_access locations later. */
2d134ed3
PA
6831 return (loc1->owner->type == loc2->owner->type
6832 && loc1->pspace->aspace == loc2->pspace->aspace
6833 && loc1->address == loc2->address
6834 && loc1->length == loc2->length);
e4f237da
KB
6835}
6836
31e77af2 6837/* See breakpoint.h. */
6c95b8df 6838
31e77af2 6839int
accd0bcd
YQ
6840breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6841 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6842{
f5656ead 6843 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6844 || aspace1 == aspace2)
6845 && addr1 == addr2);
6846}
6847
f1310107
TJB
6848/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6849 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6850 matches ASPACE2. On targets that have global breakpoints, the address
6851 space doesn't really matter. */
6852
6853static int
accd0bcd
YQ
6854breakpoint_address_match_range (const address_space *aspace1,
6855 CORE_ADDR addr1,
6856 int len1, const address_space *aspace2,
f1310107
TJB
6857 CORE_ADDR addr2)
6858{
f5656ead 6859 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6860 || aspace1 == aspace2)
6861 && addr2 >= addr1 && addr2 < addr1 + len1);
6862}
6863
6864/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6865 a ranged breakpoint. In most targets, a match happens only if ASPACE
6866 matches the breakpoint's address space. On targets that have global
6867 breakpoints, the address space doesn't really matter. */
6868
6869static int
6870breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6871 const address_space *aspace,
f1310107
TJB
6872 CORE_ADDR addr)
6873{
6874 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6875 aspace, addr)
6876 || (bl->length
6877 && breakpoint_address_match_range (bl->pspace->aspace,
6878 bl->address, bl->length,
6879 aspace, addr)));
6880}
6881
d35ae833
PA
6882/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6883 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6884 match happens only if ASPACE matches the breakpoint's address
6885 space. On targets that have global breakpoints, the address space
6886 doesn't really matter. */
6887
6888static int
6889breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6890 const address_space *aspace,
d35ae833
PA
6891 CORE_ADDR addr, int len)
6892{
6893 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6894 || bl->pspace->aspace == aspace)
6895 {
6896 int bl_len = bl->length != 0 ? bl->length : 1;
6897
6898 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6899 return 1;
6900 }
6901 return 0;
6902}
6903
1e4d1764
YQ
6904/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6905 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6906 true, otherwise returns false. */
6907
6908static int
6909tracepoint_locations_match (struct bp_location *loc1,
6910 struct bp_location *loc2)
6911{
6912 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6913 /* Since tracepoint locations are never duplicated with others', tracepoint
6914 locations at the same address of different tracepoints are regarded as
6915 different locations. */
6916 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6917 else
6918 return 0;
6919}
6920
2d134ed3 6921/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6922 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6923 the same location. If SW_HW_BPS_MATCH is true, then software
6924 breakpoint locations and hardware breakpoint locations match,
6925 otherwise they don't. */
2d134ed3
PA
6926
6927static int
7f32a4d5
PA
6928breakpoint_locations_match (struct bp_location *loc1,
6929 struct bp_location *loc2,
6930 bool sw_hw_bps_match)
2d134ed3 6931{
2bdf28a0
JK
6932 int hw_point1, hw_point2;
6933
6934 /* Both of them must not be in moribund_locations. */
6935 gdb_assert (loc1->owner != NULL);
6936 gdb_assert (loc2->owner != NULL);
6937
6938 hw_point1 = is_hardware_watchpoint (loc1->owner);
6939 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6940
6941 if (hw_point1 != hw_point2)
6942 return 0;
6943 else if (hw_point1)
6944 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6945 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6946 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6947 else
7f32a4d5
PA
6948 /* We compare bp_location.length in order to cover ranged
6949 breakpoints. Keep this in sync with
6950 bp_location_is_less_than. */
f1310107
TJB
6951 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6952 loc2->pspace->aspace, loc2->address)
7f32a4d5 6953 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6954 && loc1->length == loc2->length);
2d134ed3
PA
6955}
6956
76897487
KB
6957static void
6958breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 6959 int bnum, int have_bnum)
76897487 6960{
f63fbe86
MS
6961 /* The longest string possibly returned by hex_string_custom
6962 is 50 chars. These must be at least that big for safety. */
6963 char astr1[64];
6964 char astr2[64];
76897487 6965
bb599908
PH
6966 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6967 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6968 if (have_bnum)
8a3fe4f8 6969 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 6970 bnum, astr1, astr2);
76897487 6971 else
8a3fe4f8 6972 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6973}
6974
4a64f543
MS
6975/* Adjust a breakpoint's address to account for architectural
6976 constraints on breakpoint placement. Return the adjusted address.
6977 Note: Very few targets require this kind of adjustment. For most
6978 targets, this function is simply the identity function. */
76897487
KB
6979
6980static CORE_ADDR
a6d9a66e
UW
6981adjust_breakpoint_address (struct gdbarch *gdbarch,
6982 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6983{
a0de8c21
YQ
6984 if (bptype == bp_watchpoint
6985 || bptype == bp_hardware_watchpoint
6986 || bptype == bp_read_watchpoint
6987 || bptype == bp_access_watchpoint
6988 || bptype == bp_catchpoint)
88f7da05
KB
6989 {
6990 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 6991 have their addresses modified. */
88f7da05
KB
6992 return bpaddr;
6993 }
7c16b83e
PA
6994 else if (bptype == bp_single_step)
6995 {
6996 /* Single-step breakpoints should not have their addresses
6997 modified. If there's any architectural constrain that
6998 applies to this address, then it should have already been
6999 taken into account when the breakpoint was created in the
7000 first place. If we didn't do this, stepping through e.g.,
7001 Thumb-2 IT blocks would break. */
7002 return bpaddr;
7003 }
76897487
KB
7004 else
7005 {
a0de8c21
YQ
7006 CORE_ADDR adjusted_bpaddr = bpaddr;
7007
7008 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7009 {
7010 /* Some targets have architectural constraints on the placement
7011 of breakpoint instructions. Obtain the adjusted address. */
7012 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7013 }
76897487 7014
a0de8c21 7015 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7016
7017 /* An adjusted breakpoint address can significantly alter
dda83cd7 7018 a user's expectations. Print a warning if an adjustment
76897487
KB
7019 is required. */
7020 if (adjusted_bpaddr != bpaddr)
7021 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7022
7023 return adjusted_bpaddr;
7024 }
7025}
7026
cb1e4e32
PA
7027static bp_loc_type
7028bp_location_from_bp_type (bptype type)
7cc221ef 7029{
cb1e4e32 7030 switch (type)
e049a4b5
DJ
7031 {
7032 case bp_breakpoint:
7c16b83e 7033 case bp_single_step:
e049a4b5
DJ
7034 case bp_until:
7035 case bp_finish:
7036 case bp_longjmp:
7037 case bp_longjmp_resume:
e2e4d78b 7038 case bp_longjmp_call_dummy:
186c406b
TT
7039 case bp_exception:
7040 case bp_exception_resume:
e049a4b5 7041 case bp_step_resume:
2c03e5be 7042 case bp_hp_step_resume:
e049a4b5
DJ
7043 case bp_watchpoint_scope:
7044 case bp_call_dummy:
aa7d318d 7045 case bp_std_terminate:
e049a4b5
DJ
7046 case bp_shlib_event:
7047 case bp_thread_event:
7048 case bp_overlay_event:
4efc6507 7049 case bp_jit_event:
0fd8e87f 7050 case bp_longjmp_master:
aa7d318d 7051 case bp_std_terminate_master:
186c406b 7052 case bp_exception_master:
0e30163f
JK
7053 case bp_gnu_ifunc_resolver:
7054 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7055 case bp_dprintf:
cb1e4e32 7056 return bp_loc_software_breakpoint;
e049a4b5 7057 case bp_hardware_breakpoint:
cb1e4e32 7058 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7059 case bp_hardware_watchpoint:
7060 case bp_read_watchpoint:
7061 case bp_access_watchpoint:
cb1e4e32 7062 return bp_loc_hardware_watchpoint;
e049a4b5 7063 case bp_watchpoint:
ce78b96d 7064 case bp_catchpoint:
15c3d785
PA
7065 case bp_tracepoint:
7066 case bp_fast_tracepoint:
0fb4aa4b 7067 case bp_static_tracepoint:
cb1e4e32 7068 return bp_loc_other;
e049a4b5 7069 default:
e2e0b3e5 7070 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7071 }
cb1e4e32
PA
7072}
7073
7074bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7075{
7076 this->owner = owner;
7077 this->cond_bytecode = NULL;
7078 this->shlib_disabled = 0;
7079 this->enabled = 1;
b5fa468f 7080 this->disabled_by_cond = false;
cb1e4e32
PA
7081
7082 this->loc_type = type;
e049a4b5 7083
cb1e4e32
PA
7084 if (this->loc_type == bp_loc_software_breakpoint
7085 || this->loc_type == bp_loc_hardware_breakpoint)
7086 mark_breakpoint_location_modified (this);
7087
b6433ede 7088 incref ();
cb1e4e32
PA
7089}
7090
7091bp_location::bp_location (breakpoint *owner)
7092 : bp_location::bp_location (owner,
7093 bp_location_from_bp_type (owner->type))
7094{
28010a5d
PA
7095}
7096
7097/* Allocate a struct bp_location. */
7098
7099static struct bp_location *
7100allocate_bp_location (struct breakpoint *bpt)
7101{
348d480f
PA
7102 return bpt->ops->allocate_location (bpt);
7103}
7cc221ef 7104
f431efe5
PA
7105/* Decrement reference count. If the reference count reaches 0,
7106 destroy the bp_location. Sets *BLP to NULL. */
7107
7108static void
7109decref_bp_location (struct bp_location **blp)
7110{
b6433ede 7111 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7112 *blp = NULL;
7113}
7114
346774a9 7115/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7116
b270e6f9
TT
7117static breakpoint *
7118add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7119{
346774a9 7120 struct breakpoint *b1;
b270e6f9 7121 struct breakpoint *result = b.get ();
c906108c 7122
346774a9
PA
7123 /* Add this breakpoint to the end of the chain so that a list of
7124 breakpoints will come out in order of increasing numbers. */
7125
7126 b1 = breakpoint_chain;
7127 if (b1 == 0)
b270e6f9 7128 breakpoint_chain = b.release ();
346774a9
PA
7129 else
7130 {
7131 while (b1->next)
7132 b1 = b1->next;
b270e6f9 7133 b1->next = b.release ();
346774a9 7134 }
b270e6f9
TT
7135
7136 return result;
346774a9
PA
7137}
7138
7139/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7140
7141static void
7142init_raw_breakpoint_without_location (struct breakpoint *b,
7143 struct gdbarch *gdbarch,
28010a5d 7144 enum bptype bptype,
c0a91b2b 7145 const struct breakpoint_ops *ops)
346774a9 7146{
348d480f
PA
7147 gdb_assert (ops != NULL);
7148
28010a5d 7149 b->ops = ops;
4d28f7a8 7150 b->type = bptype;
a6d9a66e 7151 b->gdbarch = gdbarch;
c906108c
SS
7152 b->language = current_language->la_language;
7153 b->input_radix = input_radix;
d0fb5eae 7154 b->related_breakpoint = b;
346774a9
PA
7155}
7156
7157/* Helper to set_raw_breakpoint below. Creates a breakpoint
7158 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7159
7160static struct breakpoint *
7161set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7162 enum bptype bptype,
c0a91b2b 7163 const struct breakpoint_ops *ops)
346774a9 7164{
3b0871f4 7165 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7166
3b0871f4 7167 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7168 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7169}
7170
0ba852ab 7171/* Initialize loc->function_name. */
0e30163f 7172
0d381245 7173static void
0ba852ab 7174set_breakpoint_location_function (struct bp_location *loc)
0d381245 7175{
2bdf28a0
JK
7176 gdb_assert (loc->owner != NULL);
7177
0d381245 7178 if (loc->owner->type == bp_breakpoint
1042e4c0 7179 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7180 || is_tracepoint (loc->owner))
0d381245 7181 {
2c02bd72 7182 const char *function_name;
0e30163f 7183
3467ec66 7184 if (loc->msymbol != NULL
f50776aa 7185 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7186 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7187 {
7188 struct breakpoint *b = loc->owner;
7189
c9d95fa3 7190 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7191
7192 if (b->type == bp_breakpoint && b->loc == loc
7193 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7194 {
7195 /* Create only the whole new breakpoint of this type but do not
7196 mess more complicated breakpoints with multiple locations. */
7197 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7198 /* Remember the resolver's address for use by the return
dda83cd7 7199 breakpoint. */
3467ec66 7200 loc->related_address = loc->address;
0e30163f
JK
7201 }
7202 }
3467ec66
PA
7203 else
7204 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7205
2c02bd72
DE
7206 if (function_name)
7207 loc->function_name = xstrdup (function_name);
0d381245
VP
7208 }
7209}
7210
a6d9a66e 7211/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7212struct gdbarch *
a6d9a66e
UW
7213get_sal_arch (struct symtab_and_line sal)
7214{
7215 if (sal.section)
08feed99 7216 return sal.section->objfile->arch ();
a6d9a66e 7217 if (sal.symtab)
08feed99 7218 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7219
7220 return NULL;
7221}
7222
346774a9
PA
7223/* Low level routine for partially initializing a breakpoint of type
7224 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7225 file name, and line number are provided by SAL.
0d381245
VP
7226
7227 It is expected that the caller will complete the initialization of
7228 the newly created breakpoint struct as well as output any status
c56053d2 7229 information regarding the creation of a new breakpoint. */
0d381245 7230
346774a9
PA
7231static void
7232init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7233 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7234 const struct breakpoint_ops *ops)
0d381245 7235{
28010a5d 7236 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7237
3742cc8b 7238 add_location_to_breakpoint (b, &sal);
0d381245 7239
6c95b8df
PA
7240 if (bptype != bp_catchpoint)
7241 gdb_assert (sal.pspace != NULL);
7242
f8eba3c6
TT
7243 /* Store the program space that was used to set the breakpoint,
7244 except for ordinary breakpoints, which are independent of the
7245 program space. */
7246 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7247 b->pspace = sal.pspace;
346774a9 7248}
c906108c 7249
346774a9
PA
7250/* set_raw_breakpoint is a low level routine for allocating and
7251 partially initializing a breakpoint of type BPTYPE. The newly
7252 created breakpoint's address, section, source file name, and line
7253 number are provided by SAL. The newly created and partially
7254 initialized breakpoint is added to the breakpoint chain and
7255 is also returned as the value of this function.
7256
7257 It is expected that the caller will complete the initialization of
7258 the newly created breakpoint struct as well as output any status
7259 information regarding the creation of a new breakpoint. In
7260 particular, set_raw_breakpoint does NOT set the breakpoint
7261 number! Care should be taken to not allow an error to occur
7262 prior to completing the initialization of the breakpoint. If this
7263 should happen, a bogus breakpoint will be left on the chain. */
7264
7265struct breakpoint *
7266set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7267 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7268 const struct breakpoint_ops *ops)
346774a9 7269{
3b0871f4 7270 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7271
3b0871f4 7272 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7273 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7274}
7275
53a5351d 7276/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7277 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7278 initiated the operation. */
c906108c
SS
7279
7280void
186c406b 7281set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7282{
5d5658a1 7283 int thread = tp->global_num;
0fd8e87f
UW
7284
7285 /* To avoid having to rescan all objfile symbols at every step,
7286 we maintain a list of continually-inserted but always disabled
7287 longjmp "master" breakpoints. Here, we simply create momentary
7288 clones of those and enable them for the requested thread. */
1428b37a 7289 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7290 if (b->pspace == current_program_space
186c406b
TT
7291 && (b->type == bp_longjmp_master
7292 || b->type == bp_exception_master))
0fd8e87f 7293 {
06edf0c0
PA
7294 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7295 struct breakpoint *clone;
cc59ec59 7296
e2e4d78b
JK
7297 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7298 after their removal. */
06edf0c0 7299 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7300 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7301 clone->thread = thread;
7302 }
186c406b
TT
7303
7304 tp->initiating_frame = frame;
c906108c
SS
7305}
7306
611c83ae 7307/* Delete all longjmp breakpoints from THREAD. */
c906108c 7308void
611c83ae 7309delete_longjmp_breakpoint (int thread)
c906108c 7310{
1428b37a 7311 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7312 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7313 {
7314 if (b->thread == thread)
7315 delete_breakpoint (b);
7316 }
c906108c
SS
7317}
7318
f59f708a
PA
7319void
7320delete_longjmp_breakpoint_at_next_stop (int thread)
7321{
1428b37a 7322 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7323 if (b->type == bp_longjmp || b->type == bp_exception)
7324 {
7325 if (b->thread == thread)
7326 b->disposition = disp_del_at_next_stop;
7327 }
7328}
7329
e2e4d78b
JK
7330/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7331 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7332 pointer to any of them. Return NULL if this system cannot place longjmp
7333 breakpoints. */
7334
7335struct breakpoint *
7336set_longjmp_breakpoint_for_call_dummy (void)
7337{
43892fdf 7338 breakpoint *retval = nullptr;
e2e4d78b 7339
43892fdf 7340 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7341 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7342 {
7343 struct breakpoint *new_b;
7344
7345 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7346 &momentary_breakpoint_ops,
7347 1);
00431a78 7348 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7349
7350 /* Link NEW_B into the chain of RETVAL breakpoints. */
7351
7352 gdb_assert (new_b->related_breakpoint == new_b);
7353 if (retval == NULL)
7354 retval = new_b;
7355 new_b->related_breakpoint = retval;
7356 while (retval->related_breakpoint != new_b->related_breakpoint)
7357 retval = retval->related_breakpoint;
7358 retval->related_breakpoint = new_b;
7359 }
7360
7361 return retval;
7362}
7363
7364/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7365 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7366 stack.
7367
b4b3e2de
AB
7368 If the unwind fails then there is not sufficient information to discard
7369 dummy frames. In this case, elide the clean up and the dummy frames will
7370 be cleaned up next time this function is called from a location where
7371 unwinding is possible. */
e2e4d78b
JK
7372
7373void
b67a2c6f 7374check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7375{
7376 struct breakpoint *b, *b_tmp;
7377
7378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7379 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7380 {
7381 struct breakpoint *dummy_b = b->related_breakpoint;
7382
b4b3e2de
AB
7383 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7384 chained off b->related_breakpoint. */
e2e4d78b
JK
7385 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7386 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7387
7388 /* If there was no bp_call_dummy breakpoint then there's nothing
7389 more to do. Or, if the dummy frame associated with the
7390 bp_call_dummy is still on the stack then we need to leave this
7391 bp_call_dummy in place. */
e2e4d78b
JK
7392 if (dummy_b->type != bp_call_dummy
7393 || frame_find_by_id (dummy_b->frame_id) != NULL)
7394 continue;
b4b3e2de
AB
7395
7396 /* We didn't find the dummy frame on the stack, this could be
7397 because we have longjmp'd to a stack frame that is previous to
7398 the dummy frame, or it could be because the stack unwind is
7399 broken at some point between the longjmp frame and the dummy
7400 frame.
7401
7402 Next we figure out why the stack unwind stopped. If it looks
7403 like the unwind is complete then we assume the dummy frame has
7404 been jumped over, however, if the unwind stopped for an
7405 unexpected reason then we assume the stack unwind is currently
7406 broken, and that we will (eventually) return to the dummy
7407 frame.
7408
7409 It might be tempting to consider using frame_id_inner here, but
7410 that is not safe. There is no guarantee that the stack frames
7411 we are looking at here are even on the same stack as the
7412 original dummy frame, hence frame_id_inner can't be used. See
7413 the comments on frame_id_inner for more details. */
7414 bool unwind_finished_unexpectedly = false;
7415 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7416 {
7417 struct frame_info *prev = get_prev_frame (fi);
7418 if (prev == nullptr)
7419 {
7420 /* FI is the last stack frame. Why did this frame not
7421 unwind further? */
7422 auto stop_reason = get_frame_unwind_stop_reason (fi);
7423 if (stop_reason != UNWIND_NO_REASON
7424 && stop_reason != UNWIND_OUTERMOST)
7425 unwind_finished_unexpectedly = true;
7426 }
7427 fi = prev;
7428 }
7429 if (unwind_finished_unexpectedly)
7430 continue;
7431
00431a78 7432 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7433
7434 while (b->related_breakpoint != b)
7435 {
7436 if (b_tmp == b->related_breakpoint)
7437 b_tmp = b->related_breakpoint->next;
7438 delete_breakpoint (b->related_breakpoint);
7439 }
7440 delete_breakpoint (b);
7441 }
7442}
7443
1900040c
MS
7444void
7445enable_overlay_breakpoints (void)
7446{
43892fdf 7447 for (breakpoint *b : all_breakpoints ())
1900040c 7448 if (b->type == bp_overlay_event)
01add95b
SM
7449 {
7450 b->enable_state = bp_enabled;
7451 update_global_location_list (UGLL_MAY_INSERT);
7452 overlay_events_enabled = 1;
7453 }
1900040c
MS
7454}
7455
7456void
7457disable_overlay_breakpoints (void)
7458{
43892fdf 7459 for (breakpoint *b : all_breakpoints ())
1900040c 7460 if (b->type == bp_overlay_event)
01add95b
SM
7461 {
7462 b->enable_state = bp_disabled;
7463 update_global_location_list (UGLL_DONT_INSERT);
7464 overlay_events_enabled = 0;
7465 }
1900040c
MS
7466}
7467
aa7d318d
TT
7468/* Set an active std::terminate breakpoint for each std::terminate
7469 master breakpoint. */
7470void
7471set_std_terminate_breakpoint (void)
7472{
1428b37a 7473 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7474 if (b->pspace == current_program_space
7475 && b->type == bp_std_terminate_master)
7476 {
06edf0c0 7477 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7478 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7479 }
7480}
7481
7482/* Delete all the std::terminate breakpoints. */
7483void
7484delete_std_terminate_breakpoint (void)
7485{
1428b37a 7486 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7487 if (b->type == bp_std_terminate)
7488 delete_breakpoint (b);
7489}
7490
c4093a6a 7491struct breakpoint *
a6d9a66e 7492create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7493{
7494 struct breakpoint *b;
c4093a6a 7495
06edf0c0
PA
7496 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7497 &internal_breakpoint_ops);
7498
b5de0fa7 7499 b->enable_state = bp_enabled;
f00aae0f 7500 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7501 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7502
44702360 7503 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7504
c4093a6a
JM
7505 return b;
7506}
7507
0101ce28
JJ
7508struct lang_and_radix
7509 {
7510 enum language lang;
7511 int radix;
7512 };
7513
4efc6507
DE
7514/* Create a breakpoint for JIT code registration and unregistration. */
7515
7516struct breakpoint *
7517create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7518{
2a7f3dff
PA
7519 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7520 &internal_breakpoint_ops);
4efc6507 7521}
0101ce28 7522
03673fc7
PP
7523/* Remove JIT code registration and unregistration breakpoint(s). */
7524
7525void
7526remove_jit_event_breakpoints (void)
7527{
1428b37a 7528 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7529 if (b->type == bp_jit_event
7530 && b->loc->pspace == current_program_space)
7531 delete_breakpoint (b);
7532}
7533
cae688ec
JJ
7534void
7535remove_solib_event_breakpoints (void)
7536{
1428b37a 7537 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7538 if (b->type == bp_shlib_event
7539 && b->loc->pspace == current_program_space)
cae688ec
JJ
7540 delete_breakpoint (b);
7541}
7542
f37f681c
PA
7543/* See breakpoint.h. */
7544
7545void
7546remove_solib_event_breakpoints_at_next_stop (void)
7547{
1428b37a 7548 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7549 if (b->type == bp_shlib_event
7550 && b->loc->pspace == current_program_space)
7551 b->disposition = disp_del_at_next_stop;
7552}
7553
04086b45
PA
7554/* Helper for create_solib_event_breakpoint /
7555 create_and_insert_solib_event_breakpoint. Allows specifying which
7556 INSERT_MODE to pass through to update_global_location_list. */
7557
7558static struct breakpoint *
7559create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7560 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7561{
7562 struct breakpoint *b;
7563
06edf0c0
PA
7564 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7565 &internal_breakpoint_ops);
04086b45 7566 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7567 return b;
7568}
7569
04086b45
PA
7570struct breakpoint *
7571create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7572{
7573 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7574}
7575
f37f681c
PA
7576/* See breakpoint.h. */
7577
7578struct breakpoint *
7579create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7580{
7581 struct breakpoint *b;
7582
04086b45
PA
7583 /* Explicitly tell update_global_location_list to insert
7584 locations. */
7585 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7586 if (!b->loc->inserted)
7587 {
7588 delete_breakpoint (b);
7589 return NULL;
7590 }
7591 return b;
7592}
7593
cae688ec
JJ
7594/* Disable any breakpoints that are on code in shared libraries. Only
7595 apply to enabled breakpoints, disabled ones can just stay disabled. */
7596
7597void
cb851954 7598disable_breakpoints_in_shlibs (void)
cae688ec 7599{
48d7020b 7600 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7601 {
7602 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7603 struct breakpoint *b = loc->owner;
7604
7605 /* We apply the check to all breakpoints, including disabled for
7606 those with loc->duplicate set. This is so that when breakpoint
7607 becomes enabled, or the duplicate is removed, gdb will try to
7608 insert all breakpoints. If we don't set shlib_disabled here,
7609 we'll try to insert those breakpoints and fail. */
7610 if (((b->type == bp_breakpoint)
7611 || (b->type == bp_jit_event)
7612 || (b->type == bp_hardware_breakpoint)
7613 || (is_tracepoint (b)))
7614 && loc->pspace == current_program_space
7615 && !loc->shlib_disabled
7616 && solib_name_from_address (loc->pspace, loc->address)
7617 )
7618 {
7619 loc->shlib_disabled = 1;
7620 }
7621 }
cae688ec
JJ
7622}
7623
63644780
NB
7624/* Disable any breakpoints and tracepoints that are in SOLIB upon
7625 notification of unloaded_shlib. Only apply to enabled breakpoints,
7626 disabled ones can just stay disabled. */
84acb35a 7627
75149521 7628static void
84acb35a
JJ
7629disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7630{
84acb35a
JJ
7631 int disabled_shlib_breaks = 0;
7632
48d7020b 7633 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7634 {
7635 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7636 struct breakpoint *b = loc->owner;
7637
7638 if (solib->pspace == loc->pspace
7639 && !loc->shlib_disabled
7640 && (((b->type == bp_breakpoint
7641 || b->type == bp_jit_event
7642 || b->type == bp_hardware_breakpoint)
7643 && (loc->loc_type == bp_loc_hardware_breakpoint
7644 || loc->loc_type == bp_loc_software_breakpoint))
7645 || is_tracepoint (b))
7646 && solib_contains_address_p (solib, loc->address))
7647 {
7648 loc->shlib_disabled = 1;
7649 /* At this point, we cannot rely on remove_breakpoint
7650 succeeding so we must mark the breakpoint as not inserted
7651 to prevent future errors occurring in remove_breakpoints. */
7652 loc->inserted = 0;
7653
7654 /* This may cause duplicate notifications for the same breakpoint. */
7655 gdb::observers::breakpoint_modified.notify (b);
7656
7657 if (!disabled_shlib_breaks)
7658 {
7659 target_terminal::ours_for_output ();
7660 warning (_("Temporarily disabling breakpoints "
7661 "for unloaded shared library \"%s\""),
7662 solib->so_name);
7663 }
7664 disabled_shlib_breaks = 1;
7665 }
7666 }
84acb35a
JJ
7667}
7668
63644780
NB
7669/* Disable any breakpoints and tracepoints in OBJFILE upon
7670 notification of free_objfile. Only apply to enabled breakpoints,
7671 disabled ones can just stay disabled. */
7672
7673static void
7674disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7675{
63644780
NB
7676 if (objfile == NULL)
7677 return;
7678
d03de421
PA
7679 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7680 managed by the user with add-symbol-file/remove-symbol-file.
7681 Similarly to how breakpoints in shared libraries are handled in
7682 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7683 shlib_disabled so they end up uninserted on the next global
7684 location list update. Shared libraries not loaded by the user
7685 aren't handled here -- they're already handled in
7686 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7687 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7688 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7689 main objfile). */
7690 if ((objfile->flags & OBJF_SHARED) == 0
7691 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7692 return;
7693
43892fdf 7694 for (breakpoint *b : all_breakpoints ())
63644780 7695 {
63644780
NB
7696 int bp_modified = 0;
7697
7698 if (!is_breakpoint (b) && !is_tracepoint (b))
7699 continue;
7700
40cb8ca5 7701 for (bp_location *loc : b->locations ())
63644780
NB
7702 {
7703 CORE_ADDR loc_addr = loc->address;
7704
7705 if (loc->loc_type != bp_loc_hardware_breakpoint
7706 && loc->loc_type != bp_loc_software_breakpoint)
7707 continue;
7708
7709 if (loc->shlib_disabled != 0)
7710 continue;
7711
7712 if (objfile->pspace != loc->pspace)
7713 continue;
7714
7715 if (loc->loc_type != bp_loc_hardware_breakpoint
7716 && loc->loc_type != bp_loc_software_breakpoint)
7717 continue;
7718
7719 if (is_addr_in_objfile (loc_addr, objfile))
7720 {
7721 loc->shlib_disabled = 1;
08351840
PA
7722 /* At this point, we don't know whether the object was
7723 unmapped from the inferior or not, so leave the
7724 inserted flag alone. We'll handle failure to
7725 uninsert quietly, in case the object was indeed
7726 unmapped. */
63644780
NB
7727
7728 mark_breakpoint_location_modified (loc);
7729
7730 bp_modified = 1;
7731 }
7732 }
7733
7734 if (bp_modified)
76727919 7735 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7736 }
7737}
7738
ce78b96d
JB
7739/* FORK & VFORK catchpoints. */
7740
e29a4733 7741/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7742 catchpoint. A breakpoint is really of this type iff its ops pointer points
7743 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7744
c1fc2657 7745struct fork_catchpoint : public breakpoint
e29a4733 7746{
e29a4733
PA
7747 /* Process id of a child process whose forking triggered this
7748 catchpoint. This field is only valid immediately after this
7749 catchpoint has triggered. */
7750 ptid_t forked_inferior_pid;
7751};
7752
4a64f543
MS
7753/* Implement the "insert" breakpoint_ops method for fork
7754 catchpoints. */
ce78b96d 7755
77b06cd7
TJB
7756static int
7757insert_catch_fork (struct bp_location *bl)
ce78b96d 7758{
e99b03dc 7759 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7760}
7761
4a64f543
MS
7762/* Implement the "remove" breakpoint_ops method for fork
7763 catchpoints. */
ce78b96d
JB
7764
7765static int
73971819 7766remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7767{
e99b03dc 7768 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7769}
7770
7771/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7772 catchpoints. */
7773
7774static int
f1310107 7775breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7776 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7777 const struct target_waitstatus *ws)
ce78b96d 7778{
e29a4733
PA
7779 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7780
f90263c1
TT
7781 if (ws->kind != TARGET_WAITKIND_FORKED)
7782 return 0;
7783
7784 c->forked_inferior_pid = ws->value.related_pid;
7785 return 1;
ce78b96d
JB
7786}
7787
4a64f543
MS
7788/* Implement the "print_it" breakpoint_ops method for fork
7789 catchpoints. */
ce78b96d
JB
7790
7791static enum print_stop_action
348d480f 7792print_it_catch_fork (bpstat bs)
ce78b96d 7793{
36dfb11c 7794 struct ui_out *uiout = current_uiout;
348d480f
PA
7795 struct breakpoint *b = bs->breakpoint_at;
7796 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7797
ce78b96d 7798 annotate_catchpoint (b->number);
f303dbd6 7799 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7800 if (b->disposition == disp_del)
112e8700 7801 uiout->text ("Temporary catchpoint ");
36dfb11c 7802 else
112e8700
SM
7803 uiout->text ("Catchpoint ");
7804 if (uiout->is_mi_like_p ())
36dfb11c 7805 {
112e8700
SM
7806 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7807 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7808 }
381befee 7809 uiout->field_signed ("bkptno", b->number);
112e8700 7810 uiout->text (" (forked process ");
381befee 7811 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7812 uiout->text ("), ");
ce78b96d
JB
7813 return PRINT_SRC_AND_LOC;
7814}
7815
4a64f543
MS
7816/* Implement the "print_one" breakpoint_ops method for fork
7817 catchpoints. */
ce78b96d
JB
7818
7819static void
a6d9a66e 7820print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7821{
e29a4733 7822 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7823 struct value_print_options opts;
79a45e25 7824 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7825
7826 get_user_print_options (&opts);
7827
4a64f543
MS
7828 /* Field 4, the address, is omitted (which makes the columns not
7829 line up too nicely with the headers, but the effect is relatively
7830 readable). */
79a45b7d 7831 if (opts.addressprint)
112e8700 7832 uiout->field_skip ("addr");
ce78b96d 7833 annotate_field (5);
112e8700 7834 uiout->text ("fork");
d7e15655 7835 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7836 {
112e8700 7837 uiout->text (", process ");
381befee 7838 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7839 uiout->spaces (1);
ce78b96d 7840 }
8ac3646f 7841
112e8700
SM
7842 if (uiout->is_mi_like_p ())
7843 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7844}
7845
7846/* Implement the "print_mention" breakpoint_ops method for fork
7847 catchpoints. */
7848
7849static void
7850print_mention_catch_fork (struct breakpoint *b)
7851{
7852 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7853}
7854
6149aea9
PA
7855/* Implement the "print_recreate" breakpoint_ops method for fork
7856 catchpoints. */
7857
7858static void
7859print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7860{
7861 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7862 print_recreate_thread (b, fp);
6149aea9
PA
7863}
7864
ce78b96d
JB
7865/* The breakpoint_ops structure to be used in fork catchpoints. */
7866
2060206e 7867static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7868
4a64f543
MS
7869/* Implement the "insert" breakpoint_ops method for vfork
7870 catchpoints. */
ce78b96d 7871
77b06cd7
TJB
7872static int
7873insert_catch_vfork (struct bp_location *bl)
ce78b96d 7874{
e99b03dc 7875 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7876}
7877
4a64f543
MS
7878/* Implement the "remove" breakpoint_ops method for vfork
7879 catchpoints. */
ce78b96d
JB
7880
7881static int
73971819 7882remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7883{
e99b03dc 7884 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7885}
7886
7887/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7888 catchpoints. */
7889
7890static int
f1310107 7891breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7892 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7893 const struct target_waitstatus *ws)
ce78b96d 7894{
e29a4733
PA
7895 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7896
f90263c1
TT
7897 if (ws->kind != TARGET_WAITKIND_VFORKED)
7898 return 0;
7899
7900 c->forked_inferior_pid = ws->value.related_pid;
7901 return 1;
ce78b96d
JB
7902}
7903
4a64f543
MS
7904/* Implement the "print_it" breakpoint_ops method for vfork
7905 catchpoints. */
ce78b96d
JB
7906
7907static enum print_stop_action
348d480f 7908print_it_catch_vfork (bpstat bs)
ce78b96d 7909{
36dfb11c 7910 struct ui_out *uiout = current_uiout;
348d480f 7911 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7912 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7913
ce78b96d 7914 annotate_catchpoint (b->number);
f303dbd6 7915 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7916 if (b->disposition == disp_del)
112e8700 7917 uiout->text ("Temporary catchpoint ");
36dfb11c 7918 else
112e8700
SM
7919 uiout->text ("Catchpoint ");
7920 if (uiout->is_mi_like_p ())
36dfb11c 7921 {
112e8700
SM
7922 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7923 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7924 }
381befee 7925 uiout->field_signed ("bkptno", b->number);
112e8700 7926 uiout->text (" (vforked process ");
381befee 7927 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7928 uiout->text ("), ");
ce78b96d
JB
7929 return PRINT_SRC_AND_LOC;
7930}
7931
4a64f543
MS
7932/* Implement the "print_one" breakpoint_ops method for vfork
7933 catchpoints. */
ce78b96d
JB
7934
7935static void
a6d9a66e 7936print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7937{
e29a4733 7938 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7939 struct value_print_options opts;
79a45e25 7940 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7941
7942 get_user_print_options (&opts);
4a64f543
MS
7943 /* Field 4, the address, is omitted (which makes the columns not
7944 line up too nicely with the headers, but the effect is relatively
7945 readable). */
79a45b7d 7946 if (opts.addressprint)
112e8700 7947 uiout->field_skip ("addr");
ce78b96d 7948 annotate_field (5);
112e8700 7949 uiout->text ("vfork");
d7e15655 7950 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7951 {
112e8700 7952 uiout->text (", process ");
381befee 7953 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7954 uiout->spaces (1);
ce78b96d 7955 }
8ac3646f 7956
112e8700
SM
7957 if (uiout->is_mi_like_p ())
7958 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7959}
7960
7961/* Implement the "print_mention" breakpoint_ops method for vfork
7962 catchpoints. */
7963
7964static void
7965print_mention_catch_vfork (struct breakpoint *b)
7966{
7967 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7968}
7969
6149aea9
PA
7970/* Implement the "print_recreate" breakpoint_ops method for vfork
7971 catchpoints. */
7972
7973static void
7974print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7975{
7976 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7977 print_recreate_thread (b, fp);
6149aea9
PA
7978}
7979
ce78b96d
JB
7980/* The breakpoint_ops structure to be used in vfork catchpoints. */
7981
2060206e 7982static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7983
edcc5120 7984/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7985 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7986 CATCH_SOLIB_BREAKPOINT_OPS. */
7987
c1fc2657 7988struct solib_catchpoint : public breakpoint
edcc5120 7989{
c1fc2657 7990 ~solib_catchpoint () override;
edcc5120
TT
7991
7992 /* True for "catch load", false for "catch unload". */
b650a282 7993 bool is_load;
edcc5120
TT
7994
7995 /* Regular expression to match, if any. COMPILED is only valid when
7996 REGEX is non-NULL. */
7997 char *regex;
2d7cc5c7 7998 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7999};
8000
c1fc2657 8001solib_catchpoint::~solib_catchpoint ()
edcc5120 8002{
c1fc2657 8003 xfree (this->regex);
edcc5120
TT
8004}
8005
8006static int
8007insert_catch_solib (struct bp_location *ignore)
8008{
8009 return 0;
8010}
8011
8012static int
73971819 8013remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8014{
8015 return 0;
8016}
8017
8018static int
8019breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8020 const address_space *aspace,
edcc5120
TT
8021 CORE_ADDR bp_addr,
8022 const struct target_waitstatus *ws)
8023{
8024 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
edcc5120
TT
8025
8026 if (ws->kind == TARGET_WAITKIND_LOADED)
8027 return 1;
8028
43892fdf 8029 for (breakpoint *other : all_breakpoints ())
01add95b
SM
8030 {
8031 if (other == bl->owner)
8032 continue;
edcc5120 8033
01add95b
SM
8034 if (other->type != bp_shlib_event)
8035 continue;
edcc5120 8036
01add95b
SM
8037 if (self->pspace != NULL && other->pspace != self->pspace)
8038 continue;
edcc5120 8039
01add95b
SM
8040 for (bp_location *other_bl : other->locations ())
8041 {
8042 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8043 return 1;
8044 }
8045 }
edcc5120
TT
8046
8047 return 0;
8048}
8049
8050static void
8051check_status_catch_solib (struct bpstats *bs)
8052{
8053 struct solib_catchpoint *self
8054 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8055
8056 if (self->is_load)
8057 {
52941706 8058 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8059 {
8060 if (!self->regex
2d7cc5c7 8061 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8062 return;
8063 }
8064 }
8065 else
8066 {
6fb16ce6 8067 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8068 {
8069 if (!self->regex
6fb16ce6 8070 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8071 return;
8072 }
8073 }
8074
8075 bs->stop = 0;
8076 bs->print_it = print_it_noop;
8077}
8078
8079static enum print_stop_action
8080print_it_catch_solib (bpstat bs)
8081{
8082 struct breakpoint *b = bs->breakpoint_at;
8083 struct ui_out *uiout = current_uiout;
8084
8085 annotate_catchpoint (b->number);
f303dbd6 8086 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8087 if (b->disposition == disp_del)
112e8700 8088 uiout->text ("Temporary catchpoint ");
edcc5120 8089 else
112e8700 8090 uiout->text ("Catchpoint ");
381befee 8091 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8092 uiout->text ("\n");
8093 if (uiout->is_mi_like_p ())
8094 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8095 print_solib_event (1);
8096 return PRINT_SRC_AND_LOC;
8097}
8098
8099static void
8100print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8101{
8102 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8103 struct value_print_options opts;
8104 struct ui_out *uiout = current_uiout;
edcc5120
TT
8105
8106 get_user_print_options (&opts);
8107 /* Field 4, the address, is omitted (which makes the columns not
8108 line up too nicely with the headers, but the effect is relatively
8109 readable). */
8110 if (opts.addressprint)
8111 {
8112 annotate_field (4);
112e8700 8113 uiout->field_skip ("addr");
edcc5120
TT
8114 }
8115
528e1572 8116 std::string msg;
edcc5120
TT
8117 annotate_field (5);
8118 if (self->is_load)
8119 {
8120 if (self->regex)
528e1572 8121 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8122 else
528e1572 8123 msg = _("load of library");
edcc5120
TT
8124 }
8125 else
8126 {
8127 if (self->regex)
528e1572 8128 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8129 else
528e1572 8130 msg = _("unload of library");
edcc5120 8131 }
112e8700 8132 uiout->field_string ("what", msg);
8ac3646f 8133
112e8700
SM
8134 if (uiout->is_mi_like_p ())
8135 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8136}
8137
8138static void
8139print_mention_catch_solib (struct breakpoint *b)
8140{
8141 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8142
8143 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8144 self->is_load ? "load" : "unload");
8145}
8146
8147static void
8148print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8149{
8150 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8151
8152 fprintf_unfiltered (fp, "%s %s",
8153 b->disposition == disp_del ? "tcatch" : "catch",
8154 self->is_load ? "load" : "unload");
8155 if (self->regex)
8156 fprintf_unfiltered (fp, " %s", self->regex);
8157 fprintf_unfiltered (fp, "\n");
8158}
8159
8160static struct breakpoint_ops catch_solib_breakpoint_ops;
8161
b650a282 8162/* See breakpoint.h. */
edcc5120 8163
91985142 8164void
b650a282 8165add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8166{
edcc5120 8167 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8168
edcc5120
TT
8169 if (!arg)
8170 arg = "";
f1735a53 8171 arg = skip_spaces (arg);
edcc5120 8172
36bd8eaa 8173 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8174
8175 if (*arg != '\0')
8176 {
2d7cc5c7
PA
8177 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8178 _("Invalid regexp")));
edcc5120
TT
8179 c->regex = xstrdup (arg);
8180 }
8181
8182 c->is_load = is_load;
36bd8eaa 8183 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8184 &catch_solib_breakpoint_ops);
8185
c1fc2657 8186 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8187
b270e6f9 8188 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8189}
8190
91985142
MG
8191/* A helper function that does all the work for "catch load" and
8192 "catch unload". */
8193
8194static void
eb4c3f4a 8195catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8196 struct cmd_list_element *command)
8197{
91985142 8198 const int enabled = 1;
b650a282 8199 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8200
b650a282 8201 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8202}
8203
edcc5120 8204static void
eb4c3f4a 8205catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8206 struct cmd_list_element *command)
8207{
8208 catch_load_or_unload (arg, from_tty, 1, command);
8209}
8210
8211static void
eb4c3f4a 8212catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8213 struct cmd_list_element *command)
8214{
8215 catch_load_or_unload (arg, from_tty, 0, command);
8216}
8217
b650a282 8218/* See breakpoint.h. */
ce78b96d 8219
ab04a2af 8220void
346774a9 8221init_catchpoint (struct breakpoint *b,
b650a282 8222 struct gdbarch *gdbarch, bool temp,
63160a43 8223 const char *cond_string,
c0a91b2b 8224 const struct breakpoint_ops *ops)
c906108c 8225{
51abb421 8226 symtab_and_line sal;
6c95b8df 8227 sal.pspace = current_program_space;
c5aa993b 8228
28010a5d 8229 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8230
1b36a34b 8231 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8232 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8233}
8234
28010a5d 8235void
b270e6f9 8236install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8237{
b270e6f9 8238 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8239 set_breakpoint_number (internal, b);
558a9d82
YQ
8240 if (is_tracepoint (b))
8241 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8242 if (!internal)
8243 mention (b);
76727919 8244 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8245
8246 if (update_gll)
44702360 8247 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8248}
8249
9b70b993 8250static void
a6d9a66e 8251create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8252 bool temp, const char *cond_string,
dda83cd7 8253 const struct breakpoint_ops *ops)
c906108c 8254{
b270e6f9 8255 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8256
b650a282 8257 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8258
8259 c->forked_inferior_pid = null_ptid;
8260
b270e6f9 8261 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8262}
8263
fe798b75
JB
8264/* Exec catchpoints. */
8265
b4d90040 8266/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8267 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8268 CATCH_EXEC_BREAKPOINT_OPS. */
8269
c1fc2657 8270struct exec_catchpoint : public breakpoint
b4d90040 8271{
c1fc2657 8272 ~exec_catchpoint () override;
b4d90040
PA
8273
8274 /* Filename of a program whose exec triggered this catchpoint.
8275 This field is only valid immediately after this catchpoint has
8276 triggered. */
8277 char *exec_pathname;
8278};
8279
c1fc2657 8280/* Exec catchpoint destructor. */
b4d90040 8281
c1fc2657 8282exec_catchpoint::~exec_catchpoint ()
b4d90040 8283{
c1fc2657 8284 xfree (this->exec_pathname);
b4d90040
PA
8285}
8286
77b06cd7
TJB
8287static int
8288insert_catch_exec (struct bp_location *bl)
c906108c 8289{
e99b03dc 8290 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8291}
c906108c 8292
fe798b75 8293static int
73971819 8294remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8295{
e99b03dc 8296 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8297}
c906108c 8298
fe798b75 8299static int
f1310107 8300breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8301 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8302 const struct target_waitstatus *ws)
fe798b75 8303{
b4d90040
PA
8304 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8305
f90263c1
TT
8306 if (ws->kind != TARGET_WAITKIND_EXECD)
8307 return 0;
8308
8309 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8310 return 1;
fe798b75 8311}
c906108c 8312
fe798b75 8313static enum print_stop_action
348d480f 8314print_it_catch_exec (bpstat bs)
fe798b75 8315{
36dfb11c 8316 struct ui_out *uiout = current_uiout;
348d480f 8317 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8318 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8319
fe798b75 8320 annotate_catchpoint (b->number);
f303dbd6 8321 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8322 if (b->disposition == disp_del)
112e8700 8323 uiout->text ("Temporary catchpoint ");
36dfb11c 8324 else
112e8700
SM
8325 uiout->text ("Catchpoint ");
8326 if (uiout->is_mi_like_p ())
36dfb11c 8327 {
112e8700
SM
8328 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8329 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8330 }
381befee 8331 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8332 uiout->text (" (exec'd ");
8333 uiout->field_string ("new-exec", c->exec_pathname);
8334 uiout->text ("), ");
36dfb11c 8335
fe798b75 8336 return PRINT_SRC_AND_LOC;
c906108c
SS
8337}
8338
fe798b75 8339static void
a6d9a66e 8340print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8341{
b4d90040 8342 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8343 struct value_print_options opts;
79a45e25 8344 struct ui_out *uiout = current_uiout;
fe798b75
JB
8345
8346 get_user_print_options (&opts);
8347
8348 /* Field 4, the address, is omitted (which makes the columns
8349 not line up too nicely with the headers, but the effect
8350 is relatively readable). */
8351 if (opts.addressprint)
112e8700 8352 uiout->field_skip ("addr");
fe798b75 8353 annotate_field (5);
112e8700 8354 uiout->text ("exec");
b4d90040 8355 if (c->exec_pathname != NULL)
fe798b75 8356 {
112e8700
SM
8357 uiout->text (", program \"");
8358 uiout->field_string ("what", c->exec_pathname);
8359 uiout->text ("\" ");
fe798b75 8360 }
8ac3646f 8361
112e8700
SM
8362 if (uiout->is_mi_like_p ())
8363 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8364}
8365
8366static void
8367print_mention_catch_exec (struct breakpoint *b)
8368{
8369 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8370}
8371
6149aea9
PA
8372/* Implement the "print_recreate" breakpoint_ops method for exec
8373 catchpoints. */
8374
8375static void
8376print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8377{
8378 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8379 print_recreate_thread (b, fp);
6149aea9
PA
8380}
8381
2060206e 8382static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8383
c906108c 8384static int
fba45db2 8385hw_breakpoint_used_count (void)
c906108c 8386{
c906108c
SS
8387 int i = 0;
8388
43892fdf 8389 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8390 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8391 for (bp_location *bl : b->locations ())
f1310107
TJB
8392 {
8393 /* Special types of hardware breakpoints may use more than
8394 one register. */
348d480f 8395 i += b->ops->resources_needed (bl);
f1310107 8396 }
c906108c
SS
8397
8398 return i;
8399}
8400
a1398e0c
PA
8401/* Returns the resources B would use if it were a hardware
8402 watchpoint. */
8403
c906108c 8404static int
a1398e0c 8405hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8406{
c906108c
SS
8407 int i = 0;
8408
a1398e0c
PA
8409 if (!breakpoint_enabled (b))
8410 return 0;
8411
40cb8ca5 8412 for (bp_location *bl : b->locations ())
a1398e0c
PA
8413 {
8414 /* Special types of hardware watchpoints may use more than
8415 one register. */
8416 i += b->ops->resources_needed (bl);
8417 }
8418
8419 return i;
8420}
8421
8422/* Returns the sum the used resources of all hardware watchpoints of
8423 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8424 the sum of the used resources of all hardware watchpoints of other
8425 types _not_ TYPE. */
8426
8427static int
8428hw_watchpoint_used_count_others (struct breakpoint *except,
8429 enum bptype type, int *other_type_used)
8430{
8431 int i = 0;
a1398e0c 8432
c906108c 8433 *other_type_used = 0;
43892fdf 8434 for (breakpoint *b : all_breakpoints ())
e09342b5 8435 {
a1398e0c
PA
8436 if (b == except)
8437 continue;
e09342b5
TJB
8438 if (!breakpoint_enabled (b))
8439 continue;
8440
a1398e0c
PA
8441 if (b->type == type)
8442 i += hw_watchpoint_use_count (b);
8443 else if (is_hardware_watchpoint (b))
8444 *other_type_used = 1;
e09342b5
TJB
8445 }
8446
c906108c
SS
8447 return i;
8448}
8449
c906108c 8450void
fba45db2 8451disable_watchpoints_before_interactive_call_start (void)
c906108c 8452{
43892fdf 8453 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8454 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8455 {
b5de0fa7 8456 b->enable_state = bp_call_disabled;
44702360 8457 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8458 }
c906108c
SS
8459}
8460
8461void
fba45db2 8462enable_watchpoints_after_interactive_call_stop (void)
c906108c 8463{
43892fdf 8464 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8465 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8466 {
b5de0fa7 8467 b->enable_state = bp_enabled;
44702360 8468 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8469 }
c906108c
SS
8470}
8471
8bea4e01
UW
8472void
8473disable_breakpoints_before_startup (void)
8474{
6c95b8df 8475 current_program_space->executing_startup = 1;
44702360 8476 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8477}
8478
8479void
8480enable_breakpoints_after_startup (void)
8481{
6c95b8df 8482 current_program_space->executing_startup = 0;
f8eba3c6 8483 breakpoint_re_set ();
8bea4e01
UW
8484}
8485
7c16b83e
PA
8486/* Create a new single-step breakpoint for thread THREAD, with no
8487 locations. */
c906108c 8488
7c16b83e
PA
8489static struct breakpoint *
8490new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8491{
b270e6f9 8492 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8493
b270e6f9 8494 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8495 &momentary_breakpoint_ops);
8496
8497 b->disposition = disp_donttouch;
8498 b->frame_id = null_frame_id;
8499
8500 b->thread = thread;
8501 gdb_assert (b->thread != 0);
8502
b270e6f9 8503 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8504}
8505
8506/* Set a momentary breakpoint of type TYPE at address specified by
8507 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8508 frame. */
c906108c 8509
454dafbd 8510breakpoint_up
a6d9a66e
UW
8511set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8512 struct frame_id frame_id, enum bptype type)
c906108c 8513{
52f0bd74 8514 struct breakpoint *b;
edb3359d 8515
193facb3
JK
8516 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8517 tail-called one. */
8518 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8519
06edf0c0 8520 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8521 b->enable_state = bp_enabled;
8522 b->disposition = disp_donttouch;
818dd999 8523 b->frame_id = frame_id;
c906108c 8524
00431a78 8525 b->thread = inferior_thread ()->global_num;
c906108c 8526
44702360 8527 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8528
454dafbd 8529 return breakpoint_up (b);
c906108c 8530}
611c83ae 8531
06edf0c0 8532/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8533 The new breakpoint will have type TYPE, use OPS as its
8534 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8535
06edf0c0
PA
8536static struct breakpoint *
8537momentary_breakpoint_from_master (struct breakpoint *orig,
8538 enum bptype type,
a1aa2221
LM
8539 const struct breakpoint_ops *ops,
8540 int loc_enabled)
e58b0e63
PA
8541{
8542 struct breakpoint *copy;
8543
06edf0c0 8544 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8545 copy->loc = allocate_bp_location (copy);
0ba852ab 8546 set_breakpoint_location_function (copy->loc);
e58b0e63 8547
a6d9a66e 8548 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8549 copy->loc->requested_address = orig->loc->requested_address;
8550 copy->loc->address = orig->loc->address;
8551 copy->loc->section = orig->loc->section;
6c95b8df 8552 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8553 copy->loc->probe = orig->loc->probe;
f8eba3c6 8554 copy->loc->line_number = orig->loc->line_number;
2f202fde 8555 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8556 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8557 copy->frame_id = orig->frame_id;
8558 copy->thread = orig->thread;
6c95b8df 8559 copy->pspace = orig->pspace;
e58b0e63
PA
8560
8561 copy->enable_state = bp_enabled;
8562 copy->disposition = disp_donttouch;
8563 copy->number = internal_breakpoint_number--;
8564
44702360 8565 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8566 return copy;
8567}
8568
06edf0c0
PA
8569/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8570 ORIG is NULL. */
8571
8572struct breakpoint *
8573clone_momentary_breakpoint (struct breakpoint *orig)
8574{
8575 /* If there's nothing to clone, then return nothing. */
8576 if (orig == NULL)
8577 return NULL;
8578
a1aa2221 8579 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8580}
8581
454dafbd 8582breakpoint_up
a6d9a66e
UW
8583set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8584 enum bptype type)
611c83ae
PA
8585{
8586 struct symtab_and_line sal;
8587
8588 sal = find_pc_line (pc, 0);
8589 sal.pc = pc;
8590 sal.section = find_pc_overlay (pc);
8591 sal.explicit_pc = 1;
8592
a6d9a66e 8593 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8594}
c906108c 8595\f
c5aa993b 8596
c906108c
SS
8597/* Tell the user we have just set a breakpoint B. */
8598
8599static void
fba45db2 8600mention (struct breakpoint *b)
c906108c 8601{
348d480f 8602 b->ops->print_mention (b);
2d33446d 8603 current_uiout->text ("\n");
c906108c 8604}
c906108c 8605\f
c5aa993b 8606
5133a315 8607static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8608
7f32a4d5
PA
8609/* Handle "set breakpoint auto-hw on".
8610
8611 If the explicitly specified breakpoint type is not hardware
8612 breakpoint, check the memory map to see whether the breakpoint
8613 address is in read-only memory.
8614
8615 - location type is not hardware breakpoint, memory is read-only.
8616 We change the type of the location to hardware breakpoint.
8617
8618 - location type is hardware breakpoint, memory is read-write. This
8619 means we've previously made the location hardware one, but then the
8620 memory map changed, so we undo.
8621*/
8622
8623static void
8624handle_automatic_hardware_breakpoints (bp_location *bl)
8625{
8626 if (automatic_hardware_breakpoints
8627 && bl->owner->type != bp_hardware_breakpoint
8628 && (bl->loc_type == bp_loc_software_breakpoint
8629 || bl->loc_type == bp_loc_hardware_breakpoint))
8630 {
8631 /* When breakpoints are removed, remove_breakpoints will use
8632 location types we've just set here, the only possible problem
8633 is that memory map has changed during running program, but
8634 it's not going to work anyway with current gdb. */
8635 mem_region *mr = lookup_mem_region (bl->address);
8636
8637 if (mr != nullptr)
8638 {
8639 enum bp_loc_type new_type;
8640
8641 if (mr->attrib.mode != MEM_RW)
8642 new_type = bp_loc_hardware_breakpoint;
8643 else
8644 new_type = bp_loc_software_breakpoint;
8645
8646 if (new_type != bl->loc_type)
8647 {
8648 static bool said = false;
8649
8650 bl->loc_type = new_type;
8651 if (!said)
8652 {
8653 fprintf_filtered (gdb_stdout,
8654 _("Note: automatically using "
8655 "hardware breakpoints for "
8656 "read-only addresses.\n"));
8657 said = true;
8658 }
8659 }
8660 }
8661 }
8662}
8663
0d381245 8664static struct bp_location *
39d61571 8665add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8666 const struct symtab_and_line *sal)
8667{
8668 struct bp_location *loc, **tmp;
3742cc8b
YQ
8669 CORE_ADDR adjusted_address;
8670 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8671
8672 if (loc_gdbarch == NULL)
8673 loc_gdbarch = b->gdbarch;
8674
8675 /* Adjust the breakpoint's address prior to allocating a location.
8676 Once we call allocate_bp_location(), that mostly uninitialized
8677 location will be placed on the location chain. Adjustment of the
8678 breakpoint may cause target_read_memory() to be called and we do
8679 not want its scan of the location chain to find a breakpoint and
8680 location that's only been partially initialized. */
8681 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8682 sal->pc, b->type);
0d381245 8683
d30113d4 8684 /* Sort the locations by their ADDRESS. */
39d61571 8685 loc = allocate_bp_location (b);
d30113d4
JK
8686 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8687 tmp = &((*tmp)->next))
0d381245 8688 ;
d30113d4 8689 loc->next = *tmp;
0d381245 8690 *tmp = loc;
3742cc8b 8691
0d381245 8692 loc->requested_address = sal->pc;
3742cc8b 8693 loc->address = adjusted_address;
6c95b8df 8694 loc->pspace = sal->pspace;
935676c9 8695 loc->probe.prob = sal->prob;
729662a5 8696 loc->probe.objfile = sal->objfile;
6c95b8df 8697 gdb_assert (loc->pspace != NULL);
0d381245 8698 loc->section = sal->section;
3742cc8b 8699 loc->gdbarch = loc_gdbarch;
f8eba3c6 8700 loc->line_number = sal->line;
2f202fde 8701 loc->symtab = sal->symtab;
4a27f119 8702 loc->symbol = sal->symbol;
3467ec66
PA
8703 loc->msymbol = sal->msymbol;
8704 loc->objfile = sal->objfile;
f8eba3c6 8705
0ba852ab 8706 set_breakpoint_location_function (loc);
1a853c52 8707
6ae88661
LM
8708 /* While by definition, permanent breakpoints are already present in the
8709 code, we don't mark the location as inserted. Normally one would expect
8710 that GDB could rely on that breakpoint instruction to stop the program,
8711 thus removing the need to insert its own breakpoint, except that executing
8712 the breakpoint instruction can kill the target instead of reporting a
8713 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8714 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8715 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8716 breakpoint be inserted normally results in QEMU knowing about the GDB
8717 breakpoint, and thus trap before the breakpoint instruction is executed.
8718 (If GDB later needs to continue execution past the permanent breakpoint,
8719 it manually increments the PC, thus avoiding executing the breakpoint
8720 instruction.) */
1a853c52 8721 if (bp_loc_is_permanent (loc))
6ae88661 8722 loc->permanent = 1;
1a853c52 8723
0d381245
VP
8724 return loc;
8725}
514f746b
AR
8726\f
8727
5133a315
LM
8728/* Return true if LOC is pointing to a permanent breakpoint,
8729 return false otherwise. */
1cf4d951 8730
5133a315 8731static bool
1cf4d951
PA
8732bp_loc_is_permanent (struct bp_location *loc)
8733{
514f746b
AR
8734 gdb_assert (loc != NULL);
8735
cb1e4e32
PA
8736 /* If we have a non-breakpoint-backed catchpoint or a software
8737 watchpoint, just return 0. We should not attempt to read from
8738 the addresses the locations of these breakpoint types point to.
5133a315 8739 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8740 memory. */
cb1e4e32 8741 if (!bl_address_is_meaningful (loc))
5133a315 8742 return false;
244558af 8743
5ed8105e 8744 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8745 switch_to_program_space_and_thread (loc->pspace);
5133a315 8746 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8747}
8748
e7e0cddf
SS
8749/* Build a command list for the dprintf corresponding to the current
8750 settings of the dprintf style options. */
8751
8752static void
8753update_dprintf_command_list (struct breakpoint *b)
8754{
8755 char *dprintf_args = b->extra_string;
8756 char *printf_line = NULL;
8757
8758 if (!dprintf_args)
8759 return;
8760
8761 dprintf_args = skip_spaces (dprintf_args);
8762
8763 /* Allow a comma, as it may have terminated a location, but don't
8764 insist on it. */
8765 if (*dprintf_args == ',')
8766 ++dprintf_args;
8767 dprintf_args = skip_spaces (dprintf_args);
8768
8769 if (*dprintf_args != '"')
8770 error (_("Bad format string, missing '\"'."));
8771
d3ce09f5 8772 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8773 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8774 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8775 {
8776 if (!dprintf_function)
8777 error (_("No function supplied for dprintf call"));
8778
8779 if (dprintf_channel && strlen (dprintf_channel) > 0)
8780 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8781 dprintf_function,
8782 dprintf_channel,
8783 dprintf_args);
8784 else
8785 printf_line = xstrprintf ("call (void) %s (%s)",
8786 dprintf_function,
8787 dprintf_args);
8788 }
d3ce09f5
SS
8789 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8790 {
8791 if (target_can_run_breakpoint_commands ())
8792 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8793 else
8794 {
8795 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8796 printf_line = xstrprintf ("printf %s", dprintf_args);
8797 }
8798 }
e7e0cddf
SS
8799 else
8800 internal_error (__FILE__, __LINE__,
8801 _("Invalid dprintf style."));
8802
f28045c2 8803 gdb_assert (printf_line != NULL);
e7e0cddf 8804
12973681
TT
8805 /* Manufacture a printf sequence. */
8806 struct command_line *printf_cmd_line
8807 = new struct command_line (simple_control, printf_line);
8808 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8809 command_lines_deleter ()));
e7e0cddf
SS
8810}
8811
8812/* Update all dprintf commands, making their command lists reflect
8813 current style settings. */
8814
8815static void
eb4c3f4a 8816update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8817 struct cmd_list_element *c)
8818{
43892fdf
SM
8819 for (breakpoint *b : all_breakpoints ())
8820 if (b->type == bp_dprintf)
e7e0cddf 8821 update_dprintf_command_list (b);
e7e0cddf 8822}
c3f6f71d 8823
f00aae0f
KS
8824/* Create a breakpoint with SAL as location. Use LOCATION
8825 as a description of the location, and COND_STRING
b35a8b2f
DE
8826 as condition expression. If LOCATION is NULL then create an
8827 "address location" from the address in the SAL. */
018d34a4
VP
8828
8829static void
d9b3f62e 8830init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8831 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8832 event_location_up &&location,
e1e01040
PA
8833 gdb::unique_xmalloc_ptr<char> filter,
8834 gdb::unique_xmalloc_ptr<char> cond_string,
8835 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8836 enum bptype type, enum bpdisp disposition,
8837 int thread, int task, int ignore_count,
c0a91b2b 8838 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8839 int enabled, int internal, unsigned flags,
8840 int display_canonical)
018d34a4 8841{
0d381245 8842 int i;
018d34a4
VP
8843
8844 if (type == bp_hardware_breakpoint)
8845 {
fbbd034e
AS
8846 int target_resources_ok;
8847
8848 i = hw_breakpoint_used_count ();
8849 target_resources_ok =
8850 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8851 i + 1, 0);
8852 if (target_resources_ok == 0)
8853 error (_("No hardware breakpoint support in the target."));
8854 else if (target_resources_ok < 0)
8855 error (_("Hardware breakpoints used exceeds limit."));
8856 }
8857
6c5b2ebe 8858 gdb_assert (!sals.empty ());
6c95b8df 8859
6c5b2ebe 8860 for (const auto &sal : sals)
0d381245 8861 {
0d381245
VP
8862 struct bp_location *loc;
8863
8864 if (from_tty)
5af949e3
UW
8865 {
8866 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8867 if (!loc_gdbarch)
8868 loc_gdbarch = gdbarch;
8869
8870 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8871 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8872 }
0d381245 8873
6c5b2ebe 8874 if (&sal == &sals[0])
0d381245 8875 {
d9b3f62e 8876 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8877 b->thread = thread;
4a306c9a 8878 b->task = task;
855a6e68 8879
e1e01040
PA
8880 b->cond_string = cond_string.release ();
8881 b->extra_string = extra_string.release ();
0d381245 8882 b->ignore_count = ignore_count;
41447f92 8883 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8884 b->disposition = disposition;
6c95b8df 8885
44f238bb
PA
8886 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8887 b->loc->inserted = 1;
8888
0fb4aa4b
PA
8889 if (type == bp_static_tracepoint)
8890 {
d9b3f62e 8891 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8892 struct static_tracepoint_marker marker;
8893
983af33b 8894 if (strace_marker_p (b))
0fb4aa4b
PA
8895 {
8896 /* We already know the marker exists, otherwise, we
8897 wouldn't see a sal for it. */
d28cd78a
TT
8898 const char *p
8899 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8900 const char *endp;
0fb4aa4b 8901
f1735a53 8902 p = skip_spaces (p);
0fb4aa4b 8903
f1735a53 8904 endp = skip_to_space (p);
0fb4aa4b 8905
5d9310c4 8906 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8907
3e43a32a
MS
8908 printf_filtered (_("Probed static tracepoint "
8909 "marker \"%s\"\n"),
5d9310c4 8910 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8911 }
8912 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8913 {
5d9310c4 8914 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8915
3e43a32a
MS
8916 printf_filtered (_("Probed static tracepoint "
8917 "marker \"%s\"\n"),
5d9310c4 8918 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8919 }
8920 else
3e43a32a
MS
8921 warning (_("Couldn't determine the static "
8922 "tracepoint marker to probe"));
0fb4aa4b
PA
8923 }
8924
0d381245
VP
8925 loc = b->loc;
8926 }
8927 else
018d34a4 8928 {
39d61571 8929 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8930 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8931 loc->inserted = 1;
0d381245
VP
8932 }
8933
b5fa468f
TBA
8934 /* Do not set breakpoint locations conditions yet. As locations
8935 are inserted, they get sorted based on their addresses. Let
8936 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8937
8938 /* Dynamic printf requires and uses additional arguments on the
8939 command line, otherwise it's an error. */
8940 if (type == bp_dprintf)
8941 {
8942 if (b->extra_string)
8943 update_dprintf_command_list (b);
8944 else
8945 error (_("Format string required"));
8946 }
8947 else if (b->extra_string)
588ae58c 8948 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8949 }
018d34a4 8950
b5fa468f
TBA
8951
8952 /* The order of the locations is now stable. Set the location
8953 condition using the location's number. */
8954 int loc_num = 1;
40cb8ca5 8955 for (bp_location *loc : b->locations ())
b5fa468f
TBA
8956 {
8957 if (b->cond_string != nullptr)
8958 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8959 loc_num);
8960
8961 ++loc_num;
8962 }
8963
56435ebe 8964 b->display_canonical = display_canonical;
f00aae0f 8965 if (location != NULL)
d28cd78a 8966 b->location = std::move (location);
018d34a4 8967 else
d28cd78a 8968 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8969 b->filter = std::move (filter);
d9b3f62e 8970}
018d34a4 8971
d9b3f62e
PA
8972static void
8973create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8974 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8975 event_location_up &&location,
e1e01040
PA
8976 gdb::unique_xmalloc_ptr<char> filter,
8977 gdb::unique_xmalloc_ptr<char> cond_string,
8978 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8979 enum bptype type, enum bpdisp disposition,
8980 int thread, int task, int ignore_count,
c0a91b2b 8981 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8982 int enabled, int internal, unsigned flags,
8983 int display_canonical)
d9b3f62e 8984{
a5e364af 8985 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8986
a5e364af 8987 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8988 sals, std::move (location),
e1e01040
PA
8989 std::move (filter),
8990 std::move (cond_string),
8991 std::move (extra_string),
d9b3f62e
PA
8992 type, disposition,
8993 thread, task, ignore_count,
8994 ops, from_tty,
44f238bb
PA
8995 enabled, internal, flags,
8996 display_canonical);
d9b3f62e 8997
b270e6f9 8998 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8999}
9000
9001/* Add SALS.nelts breakpoints to the breakpoint table. For each
9002 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9003 value. COND_STRING, if not NULL, specified the condition to be
9004 used for all breakpoints. Essentially the only case where
9005 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9006 function. In that case, it's still not possible to specify
9007 separate conditions for different overloaded functions, so
9008 we take just a single condition string.
9009
c3f6f71d 9010 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9011 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9012 array contents). If the function fails (error() is called), the
9013 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9014 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9015
9016static void
8cdf0e15 9017create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9018 struct linespec_result *canonical,
e1e01040
PA
9019 gdb::unique_xmalloc_ptr<char> cond_string,
9020 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9021 enum bptype type, enum bpdisp disposition,
9022 int thread, int task, int ignore_count,
c0a91b2b 9023 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9024 int enabled, int internal, unsigned flags)
c906108c 9025{
f8eba3c6 9026 if (canonical->pre_expanded)
6c5b2ebe 9027 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9028
6c5b2ebe 9029 for (const auto &lsal : canonical->lsals)
c3f6f71d 9030 {
f00aae0f 9031 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9032 'break', without arguments. */
ffc2605c 9033 event_location_up location
f00aae0f 9034 = (canonical->location != NULL
8e9e35b1 9035 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9036 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9037 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9038
6c5b2ebe 9039 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9040 std::move (location),
e1e01040
PA
9041 std::move (filter_string),
9042 std::move (cond_string),
9043 std::move (extra_string),
e7e0cddf 9044 type, disposition,
84f4c1fe 9045 thread, task, ignore_count, ops,
44f238bb 9046 from_tty, enabled, internal, flags,
56435ebe 9047 canonical->special_display);
c3f6f71d 9048 }
c3f6f71d 9049}
c906108c 9050
f00aae0f 9051/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9052 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9053 addresses found. LOCATION points to the end of the SAL (for
9054 linespec locations).
9998af43
TJB
9055
9056 The array and the line spec strings are allocated on the heap, it is
9057 the caller's responsibility to free them. */
c906108c 9058
b9362cc7 9059static void
626d2320 9060parse_breakpoint_sals (struct event_location *location,
58438ac1 9061 struct linespec_result *canonical)
c3f6f71d 9062{
f00aae0f
KS
9063 struct symtab_and_line cursal;
9064
9065 if (event_location_type (location) == LINESPEC_LOCATION)
9066 {
a20714ff 9067 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9068
a20714ff 9069 if (spec == NULL)
f00aae0f
KS
9070 {
9071 /* The last displayed codepoint, if it's valid, is our default
9072 breakpoint address. */
9073 if (last_displayed_sal_is_valid ())
9074 {
f00aae0f
KS
9075 /* Set sal's pspace, pc, symtab, and line to the values
9076 corresponding to the last call to print_frame_info.
9077 Be sure to reinitialize LINE with NOTCURRENT == 0
9078 as the breakpoint line number is inappropriate otherwise.
9079 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9080 symtab_and_line sal = get_last_displayed_sal ();
9081 CORE_ADDR pc = sal.pc;
9082
f00aae0f
KS
9083 sal = find_pc_line (pc, 0);
9084
9085 /* "break" without arguments is equivalent to "break *PC"
9086 where PC is the last displayed codepoint's address. So
9087 make sure to set sal.explicit_pc to prevent GDB from
9088 trying to expand the list of sals to include all other
9089 instances with the same symtab and line. */
9090 sal.pc = pc;
9091 sal.explicit_pc = 1;
9092
6c5b2ebe
PA
9093 struct linespec_sals lsal;
9094 lsal.sals = {sal};
f00aae0f
KS
9095 lsal.canonical = NULL;
9096
6c5b2ebe 9097 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9098 return;
9099 }
9100 else
9101 error (_("No default breakpoint address now."));
c906108c 9102 }
c906108c 9103 }
f00aae0f
KS
9104
9105 /* Force almost all breakpoints to be in terms of the
9106 current_source_symtab (which is decode_line_1's default).
9107 This should produce the results we want almost all of the
9108 time while leaving default_breakpoint_* alone.
9109
9110 ObjC: However, don't match an Objective-C method name which
9111 may have a '+' or '-' succeeded by a '['. */
9112 cursal = get_current_source_symtab_and_line ();
9113 if (last_displayed_sal_is_valid ())
c906108c 9114 {
a20714ff 9115 const char *spec = NULL;
cc80f267 9116
f00aae0f 9117 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9118 spec = get_linespec_location (location)->spec_string;
cc80f267 9119
f00aae0f 9120 if (!cursal.symtab
a20714ff
PA
9121 || (spec != NULL
9122 && strchr ("+-", spec[0]) != NULL
9123 && spec[1] != '['))
f00aae0f 9124 {
c2f4122d 9125 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9126 get_last_displayed_symtab (),
9127 get_last_displayed_line (),
9128 canonical, NULL, NULL);
9129 return;
9130 }
c906108c 9131 }
f00aae0f 9132
c2f4122d 9133 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9134 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9135}
c906108c 9136
c906108c 9137
c3f6f71d 9138/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9139 inserted as a breakpoint. If it can't throw an error. */
c906108c 9140
b9362cc7 9141static void
6c5b2ebe 9142breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9143{
6c5b2ebe
PA
9144 for (auto &sal : sals)
9145 resolve_sal_pc (&sal);
c3f6f71d
JM
9146}
9147
7a697b8d
SS
9148/* Fast tracepoints may have restrictions on valid locations. For
9149 instance, a fast tracepoint using a jump instead of a trap will
9150 likely have to overwrite more bytes than a trap would, and so can
9151 only be placed where the instruction is longer than the jump, or a
9152 multi-instruction sequence does not have a jump into the middle of
9153 it, etc. */
9154
9155static void
9156check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9157 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9158{
6c5b2ebe 9159 for (const auto &sal : sals)
7a697b8d 9160 {
f8eba3c6
TT
9161 struct gdbarch *sarch;
9162
6c5b2ebe 9163 sarch = get_sal_arch (sal);
f8eba3c6
TT
9164 /* We fall back to GDBARCH if there is no architecture
9165 associated with SAL. */
9166 if (sarch == NULL)
9167 sarch = gdbarch;
281d762b
TT
9168 std::string msg;
9169 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9170 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9171 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9172 }
9173}
9174
018d34a4
VP
9175/* Given TOK, a string specification of condition and thread, as
9176 accepted by the 'break' command, extract the condition
9177 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9178 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9179 If no condition is found, *COND_STRING is set to NULL.
9180 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9181
9182static void
bbc13ae3 9183find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9184 char **cond_string, int *thread, int *task,
9185 char **rest)
018d34a4
VP
9186{
9187 *cond_string = NULL;
9188 *thread = -1;
ed1d1739
KS
9189 *task = 0;
9190 *rest = NULL;
733d554a 9191 bool force = false;
ed1d1739 9192
018d34a4
VP
9193 while (tok && *tok)
9194 {
bbc13ae3 9195 const char *end_tok;
018d34a4 9196 int toklen;
bbc13ae3
KS
9197 const char *cond_start = NULL;
9198 const char *cond_end = NULL;
cc59ec59 9199
f1735a53 9200 tok = skip_spaces (tok);
e7e0cddf
SS
9201
9202 if ((*tok == '"' || *tok == ',') && rest)
9203 {
9204 *rest = savestring (tok, strlen (tok));
9205 return;
9206 }
9207
f1735a53 9208 end_tok = skip_to_space (tok);
d634f2de 9209
018d34a4 9210 toklen = end_tok - tok;
d634f2de 9211
018d34a4
VP
9212 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9213 {
9214 tok = cond_start = end_tok + 1;
733d554a
TBA
9215 try
9216 {
9217 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9218 }
9219 catch (const gdb_exception_error &)
9220 {
9221 if (!force)
9222 throw;
9223 else
9224 tok = tok + strlen (tok);
9225 }
018d34a4 9226 cond_end = tok;
d634f2de 9227 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9228 }
733d554a
TBA
9229 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9230 {
5759831a 9231 tok = tok + toklen;
733d554a
TBA
9232 force = true;
9233 }
018d34a4
VP
9234 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9235 {
5d5658a1
PA
9236 const char *tmptok;
9237 struct thread_info *thr;
d634f2de 9238
018d34a4 9239 tok = end_tok + 1;
5d5658a1 9240 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9241 if (tok == tmptok)
9242 error (_("Junk after thread keyword."));
5d5658a1 9243 *thread = thr->global_num;
bbc13ae3 9244 tok = tmptok;
018d34a4 9245 }
4a306c9a
JB
9246 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9247 {
9248 char *tmptok;
9249
9250 tok = end_tok + 1;
bbc13ae3 9251 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9252 if (tok == tmptok)
9253 error (_("Junk after task keyword."));
9254 if (!valid_task_id (*task))
b6199126 9255 error (_("Unknown task %d."), *task);
bbc13ae3 9256 tok = tmptok;
4a306c9a 9257 }
e7e0cddf
SS
9258 else if (rest)
9259 {
9260 *rest = savestring (tok, strlen (tok));
ccab2054 9261 return;
e7e0cddf 9262 }
018d34a4
VP
9263 else
9264 error (_("Junk at end of arguments."));
9265 }
9266}
9267
b5fa468f
TBA
9268/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9269 succeeds. The parsed values are written to COND_STRING, THREAD,
9270 TASK, and REST. See the comment of 'find_condition_and_thread'
9271 for the description of these parameters and INPUT. */
9272
9273static void
9274find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9275 const char *input, char **cond_string,
9276 int *thread, int *task, char **rest)
9277{
9278 int num_failures = 0;
9279 for (auto &sal : sals)
9280 {
9281 char *cond = nullptr;
9282 int thread_id = 0;
9283 int task_id = 0;
9284 char *remaining = nullptr;
9285
9286 /* Here we want to parse 'arg' to separate condition from thread
9287 number. But because parsing happens in a context and the
9288 contexts of sals might be different, try each until there is
9289 success. Finding one successful parse is sufficient for our
9290 goal. When setting the breakpoint we'll re-parse the
9291 condition in the context of each sal. */
9292 try
9293 {
9294 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9295 &task_id, &remaining);
9296 *cond_string = cond;
9297 *thread = thread_id;
9298 *task = task_id;
9299 *rest = remaining;
9300 break;
9301 }
9302 catch (const gdb_exception_error &e)
9303 {
9304 num_failures++;
9305 /* If no sal remains, do not continue. */
9306 if (num_failures == sals.size ())
9307 throw;
9308 }
9309 }
9310}
9311
0fb4aa4b
PA
9312/* Decode a static tracepoint marker spec. */
9313
6c5b2ebe 9314static std::vector<symtab_and_line>
f00aae0f 9315decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9316{
f00aae0f
KS
9317 const char *p = &(*arg_p)[3];
9318 const char *endp;
0fb4aa4b 9319
f1735a53 9320 p = skip_spaces (p);
0fb4aa4b 9321
f1735a53 9322 endp = skip_to_space (p);
0fb4aa4b 9323
81b1e71c 9324 std::string marker_str (p, endp - p);
0fb4aa4b 9325
5d9310c4
SM
9326 std::vector<static_tracepoint_marker> markers
9327 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9328 if (markers.empty ())
81b1e71c
TT
9329 error (_("No known static tracepoint marker named %s"),
9330 marker_str.c_str ());
0fb4aa4b 9331
6c5b2ebe 9332 std::vector<symtab_and_line> sals;
5d9310c4 9333 sals.reserve (markers.size ());
0fb4aa4b 9334
5d9310c4 9335 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9336 {
5d9310c4
SM
9337 symtab_and_line sal = find_pc_line (marker.address, 0);
9338 sal.pc = marker.address;
6c5b2ebe 9339 sals.push_back (sal);
5d9310c4 9340 }
0fb4aa4b 9341
0fb4aa4b
PA
9342 *arg_p = endp;
9343 return sals;
9344}
9345
bac7c5cf
GB
9346/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9347 according to IS_TRACEPOINT. */
9348
9349static const struct breakpoint_ops *
9350breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9351 bool is_tracepoint)
9352{
9353 if (is_tracepoint)
9354 {
9355 if (location_type == PROBE_LOCATION)
9356 return &tracepoint_probe_breakpoint_ops;
9357 else
9358 return &tracepoint_breakpoint_ops;
9359 }
9360 else
9361 {
9362 if (location_type == PROBE_LOCATION)
9363 return &bkpt_probe_breakpoint_ops;
9364 else
9365 return &bkpt_breakpoint_ops;
9366 }
9367}
9368
9369/* See breakpoint.h. */
9370
9371const struct breakpoint_ops *
9372breakpoint_ops_for_event_location (const struct event_location *location,
9373 bool is_tracepoint)
9374{
9375 if (location != nullptr)
9376 return breakpoint_ops_for_event_location_type
9377 (event_location_type (location), is_tracepoint);
9378 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9379}
9380
f00aae0f 9381/* See breakpoint.h. */
0101ce28 9382
8cdf0e15
VP
9383int
9384create_breakpoint (struct gdbarch *gdbarch,
626d2320 9385 struct event_location *location,
e1e01040
PA
9386 const char *cond_string,
9387 int thread, const char *extra_string,
10a636cc 9388 bool force_condition, int parse_extra,
0fb4aa4b 9389 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9390 int ignore_count,
9391 enum auto_boolean pending_break_support,
c0a91b2b 9392 const struct breakpoint_ops *ops,
44f238bb
PA
9393 int from_tty, int enabled, int internal,
9394 unsigned flags)
c3f6f71d 9395{
7efd8fc2 9396 struct linespec_result canonical;
0101ce28 9397 int pending = 0;
4a306c9a 9398 int task = 0;
86b17b60 9399 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9400
348d480f
PA
9401 gdb_assert (ops != NULL);
9402
f00aae0f
KS
9403 /* If extra_string isn't useful, set it to NULL. */
9404 if (extra_string != NULL && *extra_string == '\0')
9405 extra_string = NULL;
9406
a70b8144 9407 try
b78a6381 9408 {
f00aae0f 9409 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9410 }
230d2906 9411 catch (const gdb_exception_error &e)
0101ce28 9412 {
492d29ea
PA
9413 /* If caller is interested in rc value from parse, set
9414 value. */
9415 if (e.error == NOT_FOUND_ERROR)
0101ce28 9416 {
05ff989b
AC
9417 /* If pending breakpoint support is turned off, throw
9418 error. */
fa8d40ab
JJ
9419
9420 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9421 throw;
723a2275
VP
9422
9423 exception_print (gdb_stderr, e);
fa8d40ab 9424
dda83cd7 9425 /* If pending breakpoint support is auto query and the user
05ff989b 9426 selects no, then simply return the error code. */
059fb39f 9427 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9428 && !nquery (_("Make %s pending on future shared library load? "),
9429 bptype_string (type_wanted)))
fd9b8c24 9430 return 0;
fa8d40ab 9431
05ff989b
AC
9432 /* At this point, either the user was queried about setting
9433 a pending breakpoint and selected yes, or pending
9434 breakpoint behavior is on and thus a pending breakpoint
9435 is defaulted on behalf of the user. */
f00aae0f 9436 pending = 1;
0101ce28 9437 }
492d29ea 9438 else
eedc3f4f 9439 throw;
0101ce28 9440 }
492d29ea 9441
6c5b2ebe 9442 if (!pending && canonical.lsals.empty ())
492d29ea 9443 return 0;
c3f6f71d 9444
c3f6f71d
JM
9445 /* Resolve all line numbers to PC's and verify that the addresses
9446 are ok for the target. */
0101ce28 9447 if (!pending)
f8eba3c6 9448 {
6c5b2ebe
PA
9449 for (auto &lsal : canonical.lsals)
9450 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9451 }
c3f6f71d 9452
7a697b8d 9453 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9454 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9455 {
6c5b2ebe
PA
9456 for (const auto &lsal : canonical.lsals)
9457 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9458 }
7a697b8d 9459
c3f6f71d
JM
9460 /* Verify that condition can be parsed, before setting any
9461 breakpoints. Allocate a separate condition expression for each
4a64f543 9462 breakpoint. */
0101ce28 9463 if (!pending)
c3f6f71d 9464 {
e1e01040
PA
9465 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9466 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9467
f00aae0f 9468 if (parse_extra)
dda83cd7 9469 {
0878d0fa 9470 char *rest;
e1e01040 9471 char *cond;
52d361e1 9472
6c5b2ebe 9473 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9474
b5fa468f
TBA
9475 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9476 &cond, &thread, &task, &rest);
e1e01040
PA
9477 cond_string_copy.reset (cond);
9478 extra_string_copy.reset (rest);
dda83cd7 9479 }
2f069f6f 9480 else
dda83cd7 9481 {
f00aae0f
KS
9482 if (type_wanted != bp_dprintf
9483 && extra_string != NULL && *extra_string != '\0')
9484 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9485
10a636cc
TBA
9486 /* Check the validity of the condition. We should error out
9487 if the condition is invalid at all of the locations and
9488 if it is not forced. In the PARSE_EXTRA case above, this
9489 check is done when parsing the EXTRA_STRING. */
9490 if (cond_string != nullptr && !force_condition)
9491 {
9492 int num_failures = 0;
9493 const linespec_sals &lsal = canonical.lsals[0];
9494 for (const auto &sal : lsal.sals)
9495 {
9496 const char *cond = cond_string;
9497 try
9498 {
9499 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9500 /* One success is sufficient to keep going. */
9501 break;
9502 }
9503 catch (const gdb_exception_error &)
9504 {
9505 num_failures++;
9506 /* If this is the last sal, error out. */
9507 if (num_failures == lsal.sals.size ())
9508 throw;
9509 }
9510 }
9511 }
9512
0878d0fa
YQ
9513 /* Create a private copy of condition string. */
9514 if (cond_string)
e1e01040 9515 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9516 /* Create a private copy of any extra string. */
9517 if (extra_string)
e1e01040 9518 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9519 }
0fb4aa4b 9520
52d361e1 9521 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9522 std::move (cond_string_copy),
9523 std::move (extra_string_copy),
9524 type_wanted,
d9b3f62e
PA
9525 tempflag ? disp_del : disp_donttouch,
9526 thread, task, ignore_count, ops,
44f238bb 9527 from_tty, enabled, internal, flags);
c906108c 9528 }
0101ce28
JJ
9529 else
9530 {
a5e364af 9531 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9532
a5e364af 9533 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9534 b->location = copy_event_location (location);
bfccc43c 9535
f00aae0f
KS
9536 if (parse_extra)
9537 b->cond_string = NULL;
e12c7713
MK
9538 else
9539 {
9540 /* Create a private copy of condition string. */
e1e01040 9541 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9542 b->thread = thread;
e12c7713 9543 }
f00aae0f
KS
9544
9545 /* Create a private copy of any extra string. */
e1e01040 9546 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9547 b->ignore_count = ignore_count;
0101ce28 9548 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9549 b->condition_not_parsed = 1;
41447f92 9550 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9551 if ((type_wanted != bp_breakpoint
dda83cd7 9552 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9553 b->pspace = current_program_space;
8bea4e01 9554
b270e6f9 9555 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9556 }
9557
6c5b2ebe 9558 if (canonical.lsals.size () > 1)
95a42b64 9559 {
3e43a32a
MS
9560 warning (_("Multiple breakpoints were set.\nUse the "
9561 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9562 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9563 }
9564
44702360 9565 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9566
9567 return 1;
c3f6f71d 9568}
c906108c 9569
348d480f 9570/* Set a breakpoint.
72b2ff0e
VP
9571 ARG is a string describing breakpoint address,
9572 condition, and thread.
9573 FLAG specifies if a breakpoint is hardware on,
9574 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9575 and BP_TEMPFLAG. */
348d480f 9576
98deb0da 9577static void
f2fc3015 9578break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9579{
72b2ff0e 9580 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9581 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9582 ? bp_hardware_breakpoint
9583 : bp_breakpoint);
f00aae0f 9584
ffc2605c 9585 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9586 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9587 (location.get (), false /* is_tracepoint */);
c3f6f71d 9588
8cdf0e15 9589 create_breakpoint (get_current_arch (),
ffc2605c 9590 location.get (),
10a636cc 9591 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9592 tempflag, type_wanted,
8cdf0e15
VP
9593 0 /* Ignore count */,
9594 pending_break_support,
55aa24fb 9595 ops,
8cdf0e15 9596 from_tty,
84f4c1fe 9597 1 /* enabled */,
44f238bb
PA
9598 0 /* internal */,
9599 0);
c906108c
SS
9600}
9601
c906108c
SS
9602/* Helper function for break_command_1 and disassemble_command. */
9603
9604void
fba45db2 9605resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9606{
9607 CORE_ADDR pc;
9608
9609 if (sal->pc == 0 && sal->symtab != NULL)
9610 {
9611 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9612 error (_("No line %d in file \"%s\"."),
05cba821 9613 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9614 sal->pc = pc;
6a048695 9615
4a64f543 9616 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9617 number, then skip the function prologue if necessary. */
6a048695 9618 if (sal->explicit_line)
059acae7 9619 skip_prologue_sal (sal);
c906108c
SS
9620 }
9621
9622 if (sal->section == 0 && sal->symtab != NULL)
9623 {
346d1dfe 9624 const struct blockvector *bv;
3977b71f 9625 const struct block *b;
c5aa993b 9626 struct symbol *sym;
c906108c 9627
43f3e411
DE
9628 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9629 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9630 if (bv != NULL)
9631 {
7f0df278 9632 sym = block_linkage_function (b);
c906108c
SS
9633 if (sym != NULL)
9634 {
eb822aa6 9635 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9636 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9637 }
9638 else
9639 {
4a64f543 9640 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9641 just have to look harder. This case can be executed
9642 if we have line numbers but no functions (as can
9643 happen in assembly source). */
c906108c 9644
5ed8105e 9645 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9646 switch_to_program_space_and_thread (sal->pspace);
c906108c 9647
5ed8105e 9648 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9649 if (msym.minsym)
1db66e34 9650 sal->section = msym.obj_section ();
c906108c
SS
9651 }
9652 }
9653 }
9654}
9655
9656void
0b39b52e 9657break_command (const char *arg, int from_tty)
c906108c 9658{
db107f19 9659 break_command_1 (arg, 0, from_tty);
c906108c
SS
9660}
9661
c906108c 9662void
0b39b52e 9663tbreak_command (const char *arg, int from_tty)
c906108c 9664{
db107f19 9665 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9666}
9667
c906108c 9668static void
0b39b52e 9669hbreak_command (const char *arg, int from_tty)
c906108c 9670{
db107f19 9671 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9672}
9673
9674static void
0b39b52e 9675thbreak_command (const char *arg, int from_tty)
c906108c 9676{
db107f19 9677 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9678}
9679
9680static void
ee7ddd71 9681stop_command (const char *arg, int from_tty)
c906108c 9682{
a3f17187 9683 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9684Usage: stop in <function | address>\n\
a3f17187 9685 stop at <line>\n"));
c906108c
SS
9686}
9687
9688static void
4495129a 9689stopin_command (const char *arg, int from_tty)
c906108c
SS
9690{
9691 int badInput = 0;
9692
cafb3438 9693 if (arg == NULL)
c906108c
SS
9694 badInput = 1;
9695 else if (*arg != '*')
9696 {
4495129a 9697 const char *argptr = arg;
c906108c
SS
9698 int hasColon = 0;
9699
4a64f543 9700 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9701 say it is bad, otherwise, it should be an address or
9702 function/method name. */
c906108c 9703 while (*argptr && !hasColon)
c5aa993b
JM
9704 {
9705 hasColon = (*argptr == ':');
9706 argptr++;
9707 }
c906108c
SS
9708
9709 if (hasColon)
c5aa993b 9710 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9711 else
c5aa993b 9712 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9713 }
9714
9715 if (badInput)
a3f17187 9716 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9717 else
db107f19 9718 break_command_1 (arg, 0, from_tty);
c906108c
SS
9719}
9720
9721static void
4495129a 9722stopat_command (const char *arg, int from_tty)
c906108c
SS
9723{
9724 int badInput = 0;
9725
cafb3438 9726 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9727 badInput = 1;
9728 else
9729 {
4495129a 9730 const char *argptr = arg;
c906108c
SS
9731 int hasColon = 0;
9732
4a64f543 9733 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9734 it is probably a line number. */
c906108c 9735 while (*argptr && !hasColon)
c5aa993b
JM
9736 {
9737 hasColon = (*argptr == ':');
9738 argptr++;
9739 }
c906108c
SS
9740
9741 if (hasColon)
c5aa993b 9742 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9743 else
c5aa993b 9744 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9745 }
9746
9747 if (badInput)
65e65158 9748 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9749 else
db107f19 9750 break_command_1 (arg, 0, from_tty);
c906108c
SS
9751}
9752
e7e0cddf
SS
9753/* The dynamic printf command is mostly like a regular breakpoint, but
9754 with a prewired command list consisting of a single output command,
9755 built from extra arguments supplied on the dprintf command
9756 line. */
9757
da821c7b 9758static void
0b39b52e 9759dprintf_command (const char *arg, int from_tty)
e7e0cddf 9760{
ffc2605c 9761 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9762
9763 /* If non-NULL, ARG should have been advanced past the location;
9764 the next character must be ','. */
9765 if (arg != NULL)
9766 {
9767 if (arg[0] != ',' || arg[1] == '\0')
9768 error (_("Format string required"));
9769 else
9770 {
9771 /* Skip the comma. */
9772 ++arg;
9773 }
9774 }
9775
e7e0cddf 9776 create_breakpoint (get_current_arch (),
ffc2605c 9777 location.get (),
10a636cc 9778 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9779 0, bp_dprintf,
9780 0 /* Ignore count */,
9781 pending_break_support,
9782 &dprintf_breakpoint_ops,
9783 from_tty,
9784 1 /* enabled */,
9785 0 /* internal */,
9786 0);
9787}
9788
d3ce09f5 9789static void
0b39b52e 9790agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9791{
9792 error (_("May only run agent-printf on the target"));
9793}
9794
f1310107
TJB
9795/* Implement the "breakpoint_hit" breakpoint_ops method for
9796 ranged breakpoints. */
9797
9798static int
9799breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9800 const address_space *aspace,
09ac7c10
TT
9801 CORE_ADDR bp_addr,
9802 const struct target_waitstatus *ws)
f1310107 9803{
09ac7c10 9804 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9805 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9806 return 0;
9807
f1310107
TJB
9808 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9809 bl->length, aspace, bp_addr);
9810}
9811
9812/* Implement the "resources_needed" breakpoint_ops method for
9813 ranged breakpoints. */
9814
9815static int
9816resources_needed_ranged_breakpoint (const struct bp_location *bl)
9817{
9818 return target_ranged_break_num_registers ();
9819}
9820
9821/* Implement the "print_it" breakpoint_ops method for
9822 ranged breakpoints. */
9823
9824static enum print_stop_action
348d480f 9825print_it_ranged_breakpoint (bpstat bs)
f1310107 9826{
348d480f 9827 struct breakpoint *b = bs->breakpoint_at;
f1310107 9828 struct bp_location *bl = b->loc;
79a45e25 9829 struct ui_out *uiout = current_uiout;
f1310107
TJB
9830
9831 gdb_assert (b->type == bp_hardware_breakpoint);
9832
9833 /* Ranged breakpoints have only one location. */
9834 gdb_assert (bl && bl->next == NULL);
9835
9836 annotate_breakpoint (b->number);
f303dbd6
PA
9837
9838 maybe_print_thread_hit_breakpoint (uiout);
9839
f1310107 9840 if (b->disposition == disp_del)
112e8700 9841 uiout->text ("Temporary ranged breakpoint ");
f1310107 9842 else
112e8700
SM
9843 uiout->text ("Ranged breakpoint ");
9844 if (uiout->is_mi_like_p ())
f1310107 9845 {
112e8700 9846 uiout->field_string ("reason",
f1310107 9847 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9848 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9849 }
381befee 9850 uiout->field_signed ("bkptno", b->number);
112e8700 9851 uiout->text (", ");
f1310107
TJB
9852
9853 return PRINT_SRC_AND_LOC;
9854}
9855
9856/* Implement the "print_one" breakpoint_ops method for
9857 ranged breakpoints. */
9858
9859static void
9860print_one_ranged_breakpoint (struct breakpoint *b,
9861 struct bp_location **last_loc)
9862{
9863 struct bp_location *bl = b->loc;
9864 struct value_print_options opts;
79a45e25 9865 struct ui_out *uiout = current_uiout;
f1310107
TJB
9866
9867 /* Ranged breakpoints have only one location. */
9868 gdb_assert (bl && bl->next == NULL);
9869
9870 get_user_print_options (&opts);
9871
9872 if (opts.addressprint)
9873 /* We don't print the address range here, it will be printed later
9874 by print_one_detail_ranged_breakpoint. */
112e8700 9875 uiout->field_skip ("addr");
f1310107
TJB
9876 annotate_field (5);
9877 print_breakpoint_location (b, bl);
9878 *last_loc = bl;
9879}
9880
9881/* Implement the "print_one_detail" breakpoint_ops method for
9882 ranged breakpoints. */
9883
9884static void
9885print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9886 struct ui_out *uiout)
9887{
9888 CORE_ADDR address_start, address_end;
9889 struct bp_location *bl = b->loc;
d7e74731 9890 string_file stb;
f1310107
TJB
9891
9892 gdb_assert (bl);
9893
9894 address_start = bl->address;
9895 address_end = address_start + bl->length - 1;
9896
112e8700 9897 uiout->text ("\taddress range: ");
d7e74731
PA
9898 stb.printf ("[%s, %s]",
9899 print_core_address (bl->gdbarch, address_start),
9900 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9901 uiout->field_stream ("addr", stb);
9902 uiout->text ("\n");
f1310107
TJB
9903}
9904
9905/* Implement the "print_mention" breakpoint_ops method for
9906 ranged breakpoints. */
9907
9908static void
9909print_mention_ranged_breakpoint (struct breakpoint *b)
9910{
9911 struct bp_location *bl = b->loc;
79a45e25 9912 struct ui_out *uiout = current_uiout;
f1310107
TJB
9913
9914 gdb_assert (bl);
9915 gdb_assert (b->type == bp_hardware_breakpoint);
9916
2d33446d
TT
9917 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9918 b->number, paddress (bl->gdbarch, bl->address),
9919 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9920}
9921
9922/* Implement the "print_recreate" breakpoint_ops method for
9923 ranged breakpoints. */
9924
9925static void
9926print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9927{
f00aae0f 9928 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9929 event_location_to_string (b->location.get ()),
9930 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9931 print_recreate_thread (b, fp);
f1310107
TJB
9932}
9933
9934/* The breakpoint_ops structure to be used in ranged breakpoints. */
9935
2060206e 9936static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9937
9938/* Find the address where the end of the breakpoint range should be
9939 placed, given the SAL of the end of the range. This is so that if
9940 the user provides a line number, the end of the range is set to the
9941 last instruction of the given line. */
9942
9943static CORE_ADDR
9944find_breakpoint_range_end (struct symtab_and_line sal)
9945{
9946 CORE_ADDR end;
9947
9948 /* If the user provided a PC value, use it. Otherwise,
9949 find the address of the end of the given location. */
9950 if (sal.explicit_pc)
9951 end = sal.pc;
9952 else
9953 {
9954 int ret;
9955 CORE_ADDR start;
9956
9957 ret = find_line_pc_range (sal, &start, &end);
9958 if (!ret)
9959 error (_("Could not find location of the end of the range."));
9960
9961 /* find_line_pc_range returns the start of the next line. */
9962 end--;
9963 }
9964
9965 return end;
9966}
9967
9968/* Implement the "break-range" CLI command. */
9969
9970static void
0b39b52e 9971break_range_command (const char *arg, int from_tty)
f1310107 9972{
f2fc3015 9973 const char *arg_start;
f1310107
TJB
9974 struct linespec_result canonical_start, canonical_end;
9975 int bp_count, can_use_bp, length;
9976 CORE_ADDR end;
9977 struct breakpoint *b;
f1310107
TJB
9978
9979 /* We don't support software ranged breakpoints. */
9980 if (target_ranged_break_num_registers () < 0)
9981 error (_("This target does not support hardware ranged breakpoints."));
9982
9983 bp_count = hw_breakpoint_used_count ();
9984 bp_count += target_ranged_break_num_registers ();
9985 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9986 bp_count, 0);
9987 if (can_use_bp < 0)
9988 error (_("Hardware breakpoints used exceeds limit."));
9989
f8eba3c6 9990 arg = skip_spaces (arg);
f1310107
TJB
9991 if (arg == NULL || arg[0] == '\0')
9992 error(_("No address range specified."));
9993
f8eba3c6 9994 arg_start = arg;
ffc2605c
TT
9995 event_location_up start_location = string_to_event_location (&arg,
9996 current_language);
9997 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9998
9999 if (arg[0] != ',')
10000 error (_("Too few arguments."));
6c5b2ebe 10001 else if (canonical_start.lsals.empty ())
f1310107 10002 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10003
6c5b2ebe 10004 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10005
6c5b2ebe
PA
10006 if (canonical_start.lsals.size () > 1
10007 || lsal_start.sals.size () != 1)
f1310107
TJB
10008 error (_("Cannot create a ranged breakpoint with multiple locations."));
10009
6c5b2ebe 10010 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10011 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10012
10013 arg++; /* Skip the comma. */
f8eba3c6 10014 arg = skip_spaces (arg);
f1310107
TJB
10015
10016 /* Parse the end location. */
10017
f1310107
TJB
10018 arg_start = arg;
10019
f8eba3c6 10020 /* We call decode_line_full directly here instead of using
f1310107
TJB
10021 parse_breakpoint_sals because we need to specify the start location's
10022 symtab and line as the default symtab and line for the end of the
10023 range. This makes it possible to have ranges like "foo.c:27, +14",
10024 where +14 means 14 lines from the start location. */
ffc2605c
TT
10025 event_location_up end_location = string_to_event_location (&arg,
10026 current_language);
10027 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10028 sal_start.symtab, sal_start.line,
10029 &canonical_end, NULL, NULL);
10030
6c5b2ebe 10031 if (canonical_end.lsals.empty ())
f1310107 10032 error (_("Could not find location of the end of the range."));
f8eba3c6 10033
6c5b2ebe
PA
10034 const linespec_sals &lsal_end = canonical_end.lsals[0];
10035 if (canonical_end.lsals.size () > 1
10036 || lsal_end.sals.size () != 1)
f1310107
TJB
10037 error (_("Cannot create a ranged breakpoint with multiple locations."));
10038
6c5b2ebe 10039 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10040
10041 end = find_breakpoint_range_end (sal_end);
10042 if (sal_start.pc > end)
177b42fe 10043 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10044
10045 length = end - sal_start.pc + 1;
10046 if (length < 0)
10047 /* Length overflowed. */
10048 error (_("Address range too large."));
10049 else if (length == 1)
10050 {
10051 /* This range is simple enough to be handled by
10052 the `hbreak' command. */
81b1e71c 10053 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10054
10055 return;
10056 }
10057
10058 /* Now set up the breakpoint. */
10059 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10060 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10061 set_breakpoint_count (breakpoint_count + 1);
10062 b->number = breakpoint_count;
10063 b->disposition = disp_donttouch;
d28cd78a
TT
10064 b->location = std::move (start_location);
10065 b->location_range_end = std::move (end_location);
f1310107
TJB
10066 b->loc->length = length;
10067
f1310107 10068 mention (b);
76727919 10069 gdb::observers::breakpoint_created.notify (b);
44702360 10070 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10071}
10072
4a64f543
MS
10073/* Return non-zero if EXP is verified as constant. Returned zero
10074 means EXP is variable. Also the constant detection may fail for
10075 some constant expressions and in such case still falsely return
10076 zero. */
2e6e3d9c 10077
dab72643 10078static bool
65d79d4b
SDJ
10079watchpoint_exp_is_const (const struct expression *exp)
10080{
1eaebe02 10081 return exp->op->constant_p ();
65d79d4b
SDJ
10082}
10083
c1fc2657 10084/* Watchpoint destructor. */
3a5c3e22 10085
c1fc2657 10086watchpoint::~watchpoint ()
3a5c3e22 10087{
c1fc2657
SM
10088 xfree (this->exp_string);
10089 xfree (this->exp_string_reparse);
3a5c3e22
PA
10090}
10091
348d480f
PA
10092/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10093
10094static void
10095re_set_watchpoint (struct breakpoint *b)
10096{
3a5c3e22
PA
10097 struct watchpoint *w = (struct watchpoint *) b;
10098
348d480f
PA
10099 /* Watchpoint can be either on expression using entirely global
10100 variables, or it can be on local variables.
10101
10102 Watchpoints of the first kind are never auto-deleted, and even
10103 persist across program restarts. Since they can use variables
10104 from shared libraries, we need to reparse expression as libraries
10105 are loaded and unloaded.
10106
10107 Watchpoints on local variables can also change meaning as result
10108 of solib event. For example, if a watchpoint uses both a local
10109 and a global variables in expression, it's a local watchpoint,
10110 but unloading of a shared library will make the expression
10111 invalid. This is not a very common use case, but we still
10112 re-evaluate expression, to avoid surprises to the user.
10113
10114 Note that for local watchpoints, we re-evaluate it only if
10115 watchpoints frame id is still valid. If it's not, it means the
10116 watchpoint is out of scope and will be deleted soon. In fact,
10117 I'm not sure we'll ever be called in this case.
10118
10119 If a local watchpoint's frame id is still valid, then
3a5c3e22 10120 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10121
3a5c3e22
PA
10122 Don't do anything about disabled watchpoints, since they will be
10123 reevaluated again when enabled. */
10124 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10125}
10126
77b06cd7
TJB
10127/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10128
10129static int
10130insert_watchpoint (struct bp_location *bl)
10131{
3a5c3e22
PA
10132 struct watchpoint *w = (struct watchpoint *) bl->owner;
10133 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10134
10135 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10136 w->cond_exp.get ());
77b06cd7
TJB
10137}
10138
10139/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10140
10141static int
73971819 10142remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10143{
3a5c3e22
PA
10144 struct watchpoint *w = (struct watchpoint *) bl->owner;
10145 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10146
10147 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10148 w->cond_exp.get ());
e09342b5
TJB
10149}
10150
e09342b5 10151static int
348d480f 10152breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10153 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10154 const struct target_waitstatus *ws)
e09342b5 10155{
348d480f 10156 struct breakpoint *b = bl->owner;
3a5c3e22 10157 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10158
348d480f
PA
10159 /* Continuable hardware watchpoints are treated as non-existent if the
10160 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10161 some data address). Otherwise gdb won't stop on a break instruction
10162 in the code (not from a breakpoint) when a hardware watchpoint has
10163 been defined. Also skip watchpoints which we know did not trigger
10164 (did not match the data address). */
10165 if (is_hardware_watchpoint (b)
3a5c3e22 10166 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10167 return 0;
9c06b0b4 10168
348d480f 10169 return 1;
9c06b0b4
TJB
10170}
10171
348d480f
PA
10172static void
10173check_status_watchpoint (bpstat bs)
9c06b0b4 10174{
348d480f 10175 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10176
348d480f 10177 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10178}
10179
10180/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10181 hardware watchpoints. */
9c06b0b4
TJB
10182
10183static int
348d480f 10184resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10185{
3a5c3e22
PA
10186 struct watchpoint *w = (struct watchpoint *) bl->owner;
10187 int length = w->exact? 1 : bl->length;
348d480f
PA
10188
10189 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10190}
10191
10192/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10193 hardware watchpoints. */
9c06b0b4
TJB
10194
10195static int
348d480f 10196works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10197{
efa80663
PA
10198 /* Read and access watchpoints only work with hardware support. */
10199 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10200}
10201
9c06b0b4 10202static enum print_stop_action
348d480f 10203print_it_watchpoint (bpstat bs)
9c06b0b4 10204{
348d480f 10205 struct breakpoint *b;
348d480f 10206 enum print_stop_action result;
3a5c3e22 10207 struct watchpoint *w;
79a45e25 10208 struct ui_out *uiout = current_uiout;
348d480f
PA
10209
10210 gdb_assert (bs->bp_location_at != NULL);
10211
348d480f 10212 b = bs->breakpoint_at;
3a5c3e22 10213 w = (struct watchpoint *) b;
348d480f 10214
f303dbd6
PA
10215 annotate_watchpoint (b->number);
10216 maybe_print_thread_hit_breakpoint (uiout);
10217
d7e74731
PA
10218 string_file stb;
10219
76f9c9cf 10220 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10221 switch (b->type)
10222 {
348d480f 10223 case bp_watchpoint:
9c06b0b4 10224 case bp_hardware_watchpoint:
112e8700
SM
10225 if (uiout->is_mi_like_p ())
10226 uiout->field_string
10227 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10228 mention (b);
76f9c9cf 10229 tuple_emitter.emplace (uiout, "value");
112e8700 10230 uiout->text ("\nOld value = ");
850645cf 10231 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10232 uiout->field_stream ("old", stb);
10233 uiout->text ("\nNew value = ");
850645cf 10234 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10235 uiout->field_stream ("new", stb);
10236 uiout->text ("\n");
348d480f
PA
10237 /* More than one watchpoint may have been triggered. */
10238 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10239 break;
10240
10241 case bp_read_watchpoint:
112e8700
SM
10242 if (uiout->is_mi_like_p ())
10243 uiout->field_string
10244 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10245 mention (b);
76f9c9cf 10246 tuple_emitter.emplace (uiout, "value");
112e8700 10247 uiout->text ("\nValue = ");
850645cf 10248 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10249 uiout->field_stream ("value", stb);
10250 uiout->text ("\n");
348d480f 10251 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10252 break;
10253
10254 case bp_access_watchpoint:
348d480f
PA
10255 if (bs->old_val != NULL)
10256 {
112e8700
SM
10257 if (uiout->is_mi_like_p ())
10258 uiout->field_string
10259 ("reason",
348d480f
PA
10260 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10261 mention (b);
76f9c9cf 10262 tuple_emitter.emplace (uiout, "value");
112e8700 10263 uiout->text ("\nOld value = ");
850645cf 10264 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10265 uiout->field_stream ("old", stb);
10266 uiout->text ("\nNew value = ");
348d480f
PA
10267 }
10268 else
10269 {
10270 mention (b);
112e8700
SM
10271 if (uiout->is_mi_like_p ())
10272 uiout->field_string
10273 ("reason",
348d480f 10274 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10275 tuple_emitter.emplace (uiout, "value");
112e8700 10276 uiout->text ("\nValue = ");
348d480f 10277 }
850645cf 10278 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10279 uiout->field_stream ("new", stb);
10280 uiout->text ("\n");
348d480f 10281 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10282 break;
10283 default:
348d480f 10284 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10285 }
10286
348d480f
PA
10287 return result;
10288}
10289
10290/* Implement the "print_mention" breakpoint_ops method for hardware
10291 watchpoints. */
10292
10293static void
10294print_mention_watchpoint (struct breakpoint *b)
10295{
3a5c3e22 10296 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10297 struct ui_out *uiout = current_uiout;
46b9c129 10298 const char *tuple_name;
348d480f
PA
10299
10300 switch (b->type)
10301 {
10302 case bp_watchpoint:
112e8700 10303 uiout->text ("Watchpoint ");
46b9c129 10304 tuple_name = "wpt";
348d480f
PA
10305 break;
10306 case bp_hardware_watchpoint:
112e8700 10307 uiout->text ("Hardware watchpoint ");
46b9c129 10308 tuple_name = "wpt";
348d480f
PA
10309 break;
10310 case bp_read_watchpoint:
112e8700 10311 uiout->text ("Hardware read watchpoint ");
46b9c129 10312 tuple_name = "hw-rwpt";
348d480f
PA
10313 break;
10314 case bp_access_watchpoint:
112e8700 10315 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10316 tuple_name = "hw-awpt";
348d480f
PA
10317 break;
10318 default:
10319 internal_error (__FILE__, __LINE__,
10320 _("Invalid hardware watchpoint type."));
10321 }
10322
46b9c129 10323 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10324 uiout->field_signed ("number", b->number);
112e8700
SM
10325 uiout->text (": ");
10326 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10327}
10328
10329/* Implement the "print_recreate" breakpoint_ops method for
10330 watchpoints. */
10331
10332static void
10333print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10334{
3a5c3e22
PA
10335 struct watchpoint *w = (struct watchpoint *) b;
10336
348d480f
PA
10337 switch (b->type)
10338 {
10339 case bp_watchpoint:
10340 case bp_hardware_watchpoint:
10341 fprintf_unfiltered (fp, "watch");
10342 break;
10343 case bp_read_watchpoint:
10344 fprintf_unfiltered (fp, "rwatch");
10345 break;
10346 case bp_access_watchpoint:
10347 fprintf_unfiltered (fp, "awatch");
10348 break;
10349 default:
10350 internal_error (__FILE__, __LINE__,
10351 _("Invalid watchpoint type."));
10352 }
10353
3a5c3e22 10354 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10355 print_recreate_thread (b, fp);
348d480f
PA
10356}
10357
427cd150
TT
10358/* Implement the "explains_signal" breakpoint_ops method for
10359 watchpoints. */
10360
47591c29 10361static int
427cd150
TT
10362explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10363{
10364 /* A software watchpoint cannot cause a signal other than
10365 GDB_SIGNAL_TRAP. */
10366 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10367 return 0;
427cd150 10368
47591c29 10369 return 1;
427cd150
TT
10370}
10371
348d480f
PA
10372/* The breakpoint_ops structure to be used in hardware watchpoints. */
10373
2060206e 10374static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10375
10376/* Implement the "insert" breakpoint_ops method for
10377 masked hardware watchpoints. */
10378
10379static int
10380insert_masked_watchpoint (struct bp_location *bl)
10381{
3a5c3e22
PA
10382 struct watchpoint *w = (struct watchpoint *) bl->owner;
10383
10384 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10385 bl->watchpoint_type);
10386}
10387
10388/* Implement the "remove" breakpoint_ops method for
10389 masked hardware watchpoints. */
10390
10391static int
73971819 10392remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10393{
3a5c3e22
PA
10394 struct watchpoint *w = (struct watchpoint *) bl->owner;
10395
10396 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10397 bl->watchpoint_type);
348d480f
PA
10398}
10399
10400/* Implement the "resources_needed" breakpoint_ops method for
10401 masked hardware watchpoints. */
10402
10403static int
10404resources_needed_masked_watchpoint (const struct bp_location *bl)
10405{
3a5c3e22
PA
10406 struct watchpoint *w = (struct watchpoint *) bl->owner;
10407
10408 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10409}
10410
10411/* Implement the "works_in_software_mode" breakpoint_ops method for
10412 masked hardware watchpoints. */
10413
10414static int
10415works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10416{
10417 return 0;
10418}
10419
10420/* Implement the "print_it" breakpoint_ops method for
10421 masked hardware watchpoints. */
10422
10423static enum print_stop_action
10424print_it_masked_watchpoint (bpstat bs)
10425{
10426 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10427 struct ui_out *uiout = current_uiout;
348d480f
PA
10428
10429 /* Masked watchpoints have only one location. */
10430 gdb_assert (b->loc && b->loc->next == NULL);
10431
f303dbd6
PA
10432 annotate_watchpoint (b->number);
10433 maybe_print_thread_hit_breakpoint (uiout);
10434
348d480f
PA
10435 switch (b->type)
10436 {
10437 case bp_hardware_watchpoint:
112e8700
SM
10438 if (uiout->is_mi_like_p ())
10439 uiout->field_string
10440 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10441 break;
10442
10443 case bp_read_watchpoint:
112e8700
SM
10444 if (uiout->is_mi_like_p ())
10445 uiout->field_string
10446 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10447 break;
10448
10449 case bp_access_watchpoint:
112e8700
SM
10450 if (uiout->is_mi_like_p ())
10451 uiout->field_string
10452 ("reason",
348d480f
PA
10453 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10454 break;
10455 default:
10456 internal_error (__FILE__, __LINE__,
10457 _("Invalid hardware watchpoint type."));
10458 }
10459
10460 mention (b);
112e8700 10461 uiout->text (_("\n\
9c06b0b4
TJB
10462Check the underlying instruction at PC for the memory\n\
10463address and value which triggered this watchpoint.\n"));
112e8700 10464 uiout->text ("\n");
9c06b0b4
TJB
10465
10466 /* More than one watchpoint may have been triggered. */
10467 return PRINT_UNKNOWN;
10468}
10469
10470/* Implement the "print_one_detail" breakpoint_ops method for
10471 masked hardware watchpoints. */
10472
10473static void
10474print_one_detail_masked_watchpoint (const struct breakpoint *b,
10475 struct ui_out *uiout)
10476{
3a5c3e22
PA
10477 struct watchpoint *w = (struct watchpoint *) b;
10478
9c06b0b4
TJB
10479 /* Masked watchpoints have only one location. */
10480 gdb_assert (b->loc && b->loc->next == NULL);
10481
112e8700
SM
10482 uiout->text ("\tmask ");
10483 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10484 uiout->text ("\n");
9c06b0b4
TJB
10485}
10486
10487/* Implement the "print_mention" breakpoint_ops method for
10488 masked hardware watchpoints. */
10489
10490static void
10491print_mention_masked_watchpoint (struct breakpoint *b)
10492{
3a5c3e22 10493 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10494 struct ui_out *uiout = current_uiout;
46b9c129 10495 const char *tuple_name;
9c06b0b4
TJB
10496
10497 switch (b->type)
10498 {
10499 case bp_hardware_watchpoint:
112e8700 10500 uiout->text ("Masked hardware watchpoint ");
46b9c129 10501 tuple_name = "wpt";
9c06b0b4
TJB
10502 break;
10503 case bp_read_watchpoint:
112e8700 10504 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10505 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10506 break;
10507 case bp_access_watchpoint:
112e8700 10508 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10509 tuple_name = "hw-awpt";
9c06b0b4
TJB
10510 break;
10511 default:
10512 internal_error (__FILE__, __LINE__,
10513 _("Invalid hardware watchpoint type."));
10514 }
10515
46b9c129 10516 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10517 uiout->field_signed ("number", b->number);
112e8700
SM
10518 uiout->text (": ");
10519 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10520}
10521
10522/* Implement the "print_recreate" breakpoint_ops method for
10523 masked hardware watchpoints. */
10524
10525static void
10526print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10527{
3a5c3e22 10528 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10529
10530 switch (b->type)
10531 {
10532 case bp_hardware_watchpoint:
10533 fprintf_unfiltered (fp, "watch");
10534 break;
10535 case bp_read_watchpoint:
10536 fprintf_unfiltered (fp, "rwatch");
10537 break;
10538 case bp_access_watchpoint:
10539 fprintf_unfiltered (fp, "awatch");
10540 break;
10541 default:
10542 internal_error (__FILE__, __LINE__,
10543 _("Invalid hardware watchpoint type."));
10544 }
10545
53807e9f
TT
10546 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10547 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10548 print_recreate_thread (b, fp);
9c06b0b4
TJB
10549}
10550
10551/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10552
2060206e 10553static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10554
10555/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10556
f2478a7e 10557static bool
9c06b0b4
TJB
10558is_masked_watchpoint (const struct breakpoint *b)
10559{
10560 return b->ops == &masked_watchpoint_breakpoint_ops;
10561}
10562
53a5351d 10563/* accessflag: hw_write: watch write,
dda83cd7 10564 hw_read: watch read,
53a5351d 10565 hw_access: watch access (read or write) */
c906108c 10566static void
bbc13ae3 10567watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10568 bool just_location, bool internal)
c906108c 10569{
c1fc2657 10570 struct breakpoint *scope_breakpoint = NULL;
270140bd 10571 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10572 struct value *result;
bb9d5f81 10573 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10574 const char *exp_start = NULL;
10575 const char *exp_end = NULL;
10576 const char *tok, *end_tok;
9c06b0b4 10577 int toklen = -1;
bbc13ae3
KS
10578 const char *cond_start = NULL;
10579 const char *cond_end = NULL;
c906108c 10580 enum bptype bp_type;
37e4754d 10581 int thread = -1;
9c06b0b4
TJB
10582 /* Flag to indicate whether we are going to use masks for
10583 the hardware watchpoint. */
2e362716 10584 bool use_mask = false;
9c06b0b4 10585 CORE_ADDR mask = 0;
c906108c 10586
37e4754d
LM
10587 /* Make sure that we actually have parameters to parse. */
10588 if (arg != NULL && arg[0] != '\0')
10589 {
bbc13ae3
KS
10590 const char *value_start;
10591
10592 exp_end = arg + strlen (arg);
37e4754d 10593
9c06b0b4
TJB
10594 /* Look for "parameter value" pairs at the end
10595 of the arguments string. */
bbc13ae3 10596 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10597 {
10598 /* Skip whitespace at the end of the argument list. */
10599 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10600 tok--;
10601
10602 /* Find the beginning of the last token.
10603 This is the value of the parameter. */
10604 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10605 tok--;
10606 value_start = tok + 1;
10607
10608 /* Skip whitespace. */
10609 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10610 tok--;
10611
10612 end_tok = tok;
10613
10614 /* Find the beginning of the second to last token.
10615 This is the parameter itself. */
10616 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10617 tok--;
10618 tok++;
10619 toklen = end_tok - tok + 1;
10620
61012eef 10621 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10622 {
5d5658a1 10623 struct thread_info *thr;
9c06b0b4
TJB
10624 /* At this point we've found a "thread" token, which means
10625 the user is trying to set a watchpoint that triggers
10626 only in a specific thread. */
5d5658a1 10627 const char *endp;
37e4754d 10628
9c06b0b4
TJB
10629 if (thread != -1)
10630 error(_("You can specify only one thread."));
37e4754d 10631
9c06b0b4 10632 /* Extract the thread ID from the next token. */
5d5658a1 10633 thr = parse_thread_id (value_start, &endp);
37e4754d 10634
5d5658a1 10635 /* Check if the user provided a valid thread ID. */
9c06b0b4 10636 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10637 invalid_thread_id_error (value_start);
9c06b0b4 10638
5d5658a1 10639 thread = thr->global_num;
9c06b0b4 10640 }
61012eef 10641 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10642 {
10643 /* We've found a "mask" token, which means the user wants to
10644 create a hardware watchpoint that is going to have the mask
10645 facility. */
10646 struct value *mask_value, *mark;
37e4754d 10647
9c06b0b4
TJB
10648 if (use_mask)
10649 error(_("You can specify only one mask."));
37e4754d 10650
2e362716 10651 use_mask = just_location = true;
37e4754d 10652
9c06b0b4
TJB
10653 mark = value_mark ();
10654 mask_value = parse_to_comma_and_eval (&value_start);
10655 mask = value_as_address (mask_value);
10656 value_free_to_mark (mark);
10657 }
10658 else
10659 /* We didn't recognize what we found. We should stop here. */
10660 break;
37e4754d 10661
9c06b0b4
TJB
10662 /* Truncate the string and get rid of the "parameter value" pair before
10663 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10664 exp_end = tok;
9c06b0b4 10665 }
37e4754d 10666 }
bbc13ae3
KS
10667 else
10668 exp_end = arg;
37e4754d 10669
bbc13ae3
KS
10670 /* Parse the rest of the arguments. From here on out, everything
10671 is in terms of a newly allocated string instead of the original
10672 ARG. */
81b1e71c
TT
10673 std::string expression (arg, exp_end - arg);
10674 exp_start = arg = expression.c_str ();
699bd4cf
TT
10675 innermost_block_tracker tracker;
10676 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10677 exp_end = arg;
fa8a61dc
TT
10678 /* Remove trailing whitespace from the expression before saving it.
10679 This makes the eventual display of the expression string a bit
10680 prettier. */
10681 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10682 --exp_end;
10683
65d79d4b 10684 /* Checking if the expression is not constant. */
4d01a485 10685 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10686 {
10687 int len;
10688
10689 len = exp_end - exp_start;
10690 while (len > 0 && isspace (exp_start[len - 1]))
10691 len--;
10692 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10693 }
10694
699bd4cf 10695 exp_valid_block = tracker.block ();
b926417a 10696 struct value *mark = value_mark ();
850645cf 10697 struct value *val_as_value = nullptr;
1eaebe02
TT
10698 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10699 just_location);
06a64a0b 10700
850645cf 10701 if (val_as_value != NULL && just_location)
bb9d5f81 10702 {
850645cf
TT
10703 saved_bitpos = value_bitpos (val_as_value);
10704 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10705 }
10706
850645cf 10707 value_ref_ptr val;
06a64a0b
TT
10708 if (just_location)
10709 {
9c06b0b4
TJB
10710 int ret;
10711
06a64a0b 10712 exp_valid_block = NULL;
850645cf 10713 val = release_value (value_addr (result));
06a64a0b 10714 value_free_to_mark (mark);
9c06b0b4
TJB
10715
10716 if (use_mask)
10717 {
850645cf 10718 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10719 mask);
10720 if (ret == -1)
10721 error (_("This target does not support masked watchpoints."));
10722 else if (ret == -2)
10723 error (_("Invalid mask or memory region."));
10724 }
06a64a0b 10725 }
850645cf
TT
10726 else if (val_as_value != NULL)
10727 val = release_value (val_as_value);
c906108c 10728
f1735a53
TT
10729 tok = skip_spaces (arg);
10730 end_tok = skip_to_space (tok);
c906108c
SS
10731
10732 toklen = end_tok - tok;
10733 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10734 {
10735 tok = cond_start = end_tok + 1;
699bd4cf
TT
10736 innermost_block_tracker if_tracker;
10737 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10738
10739 /* The watchpoint expression may not be local, but the condition
10740 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10741 cond_exp_valid_block = if_tracker.block ();
60e1c644 10742
c906108c
SS
10743 cond_end = tok;
10744 }
10745 if (*tok)
8a3fe4f8 10746 error (_("Junk at end of command."));
c906108c 10747
441d7c93
PA
10748 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10749
10750 /* Save this because create_internal_breakpoint below invalidates
10751 'wp_frame'. */
10752 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10753
10754 /* If the expression is "local", then set up a "watchpoint scope"
10755 breakpoint at the point where we've left the scope of the watchpoint
10756 expression. Create the scope breakpoint before the watchpoint, so
10757 that we will encounter it first in bpstat_stop_status. */
441d7c93 10758 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10759 {
441d7c93
PA
10760 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10761
10762 if (frame_id_p (caller_frame_id))
edb3359d 10763 {
441d7c93
PA
10764 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10765 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10766
24b21115 10767 scope_breakpoint
441d7c93 10768 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10769 bp_watchpoint_scope,
10770 &momentary_breakpoint_ops);
d983da9c 10771
441d7c93
PA
10772 /* create_internal_breakpoint could invalidate WP_FRAME. */
10773 wp_frame = NULL;
10774
edb3359d 10775 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10776
edb3359d
DJ
10777 /* Automatically delete the breakpoint when it hits. */
10778 scope_breakpoint->disposition = disp_del;
d983da9c 10779
edb3359d 10780 /* Only break in the proper frame (help with recursion). */
441d7c93 10781 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10782
edb3359d 10783 /* Set the address at which we will stop. */
441d7c93
PA
10784 scope_breakpoint->loc->gdbarch = caller_arch;
10785 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10786 scope_breakpoint->loc->address
a6d9a66e
UW
10787 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10788 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10789 scope_breakpoint->type);
10790 }
d983da9c
DJ
10791 }
10792
e8369a73
AB
10793 /* Now set up the breakpoint. We create all watchpoints as hardware
10794 watchpoints here even if hardware watchpoints are turned off, a call
10795 to update_watchpoint later in this function will cause the type to
10796 drop back to bp_watchpoint (software watchpoint) if required. */
10797
10798 if (accessflag == hw_read)
10799 bp_type = bp_read_watchpoint;
10800 else if (accessflag == hw_access)
10801 bp_type = bp_access_watchpoint;
10802 else
10803 bp_type = bp_hardware_watchpoint;
3a5c3e22 10804
b270e6f9 10805 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10806
348d480f 10807 if (use_mask)
b270e6f9 10808 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10809 &masked_watchpoint_breakpoint_ops);
348d480f 10810 else
b270e6f9 10811 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10812 &watchpoint_breakpoint_ops);
c1fc2657
SM
10813 w->thread = thread;
10814 w->disposition = disp_donttouch;
10815 w->pspace = current_program_space;
b22e99fd 10816 w->exp = std::move (exp);
3a5c3e22
PA
10817 w->exp_valid_block = exp_valid_block;
10818 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10819 if (just_location)
10820 {
850645cf
TT
10821 struct type *t = value_type (val.get ());
10822 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10823
43cc5389 10824 w->exp_string_reparse
f16a9f57 10825 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10826
3a5c3e22 10827 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10828 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10829 }
10830 else
3a5c3e22 10831 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10832
10833 if (use_mask)
10834 {
3a5c3e22 10835 w->hw_wp_mask = mask;
9c06b0b4
TJB
10836 }
10837 else
10838 {
3a5c3e22 10839 w->val = val;
bb9d5f81
PP
10840 w->val_bitpos = saved_bitpos;
10841 w->val_bitsize = saved_bitsize;
4c1d86d9 10842 w->val_valid = true;
9c06b0b4 10843 }
77b06cd7 10844
c906108c 10845 if (cond_start)
c1fc2657 10846 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10847 else
c1fc2657 10848 w->cond_string = 0;
c5aa993b 10849
441d7c93 10850 if (frame_id_p (watchpoint_frame))
f6bc2008 10851 {
441d7c93 10852 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10853 w->watchpoint_thread = inferior_ptid;
f6bc2008 10854 }
c906108c 10855 else
f6bc2008 10856 {
3a5c3e22
PA
10857 w->watchpoint_frame = null_frame_id;
10858 w->watchpoint_thread = null_ptid;
f6bc2008 10859 }
c906108c 10860
d983da9c 10861 if (scope_breakpoint != NULL)
c906108c 10862 {
d983da9c
DJ
10863 /* The scope breakpoint is related to the watchpoint. We will
10864 need to act on them together. */
c1fc2657 10865 w->related_breakpoint = scope_breakpoint;
b270e6f9 10866 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10867 }
d983da9c 10868
06a64a0b
TT
10869 if (!just_location)
10870 value_free_to_mark (mark);
2d134ed3 10871
b270e6f9
TT
10872 /* Finally update the new watchpoint. This creates the locations
10873 that should be inserted. */
10874 update_watchpoint (w.get (), 1);
a9634178 10875
b270e6f9 10876 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10877}
10878
e09342b5 10879/* Return count of debug registers needed to watch the given expression.
e09342b5 10880 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10881
c906108c 10882static int
a6535de1 10883can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10884{
10885 int found_memory_cnt = 0;
10886
10887 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10888 if (!can_use_hw_watchpoints)
c906108c 10889 return 0;
c5aa993b 10890
a6535de1
TT
10891 gdb_assert (!vals.empty ());
10892 struct value *head = vals[0].get ();
10893
5c44784c
JM
10894 /* Make sure that the value of the expression depends only upon
10895 memory contents, and values computed from them within GDB. If we
10896 find any register references or function calls, we can't use a
10897 hardware watchpoint.
10898
10899 The idea here is that evaluating an expression generates a series
10900 of values, one holding the value of every subexpression. (The
10901 expression a*b+c has five subexpressions: a, b, a*b, c, and
10902 a*b+c.) GDB's values hold almost enough information to establish
10903 the criteria given above --- they identify memory lvalues,
10904 register lvalues, computed values, etcetera. So we can evaluate
10905 the expression, and then scan the chain of values that leaves
10906 behind to decide whether we can detect any possible change to the
10907 expression's final value using only hardware watchpoints.
10908
10909 However, I don't think that the values returned by inferior
10910 function calls are special in any way. So this function may not
10911 notice that an expression involving an inferior function call
10912 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10913 for (const value_ref_ptr &iter : vals)
c906108c 10914 {
a6535de1
TT
10915 struct value *v = iter.get ();
10916
5c44784c 10917 if (VALUE_LVAL (v) == lval_memory)
c906108c 10918 {
8464be76
DJ
10919 if (v != head && value_lazy (v))
10920 /* A lazy memory lvalue in the chain is one that GDB never
10921 needed to fetch; we either just used its address (e.g.,
10922 `a' in `a.b') or we never needed it at all (e.g., `a'
10923 in `a,b'). This doesn't apply to HEAD; if that is
10924 lazy then it was not readable, but watch it anyway. */
5c44784c 10925 ;
53a5351d 10926 else
5c44784c
JM
10927 {
10928 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10929 it with hardware watchpoints. */
df407dfe 10930 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10931
10932 /* We only watch structs and arrays if user asked for it
10933 explicitly, never if they just happen to appear in a
10934 middle of some value chain. */
10935 if (v == head
78134374
SM
10936 || (vtype->code () != TYPE_CODE_STRUCT
10937 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10938 {
42ae5230 10939 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10940 int len;
10941 int num_regs;
10942
a9634178 10943 len = (target_exact_watchpoints
e09342b5
TJB
10944 && is_scalar_type_recursive (vtype))?
10945 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10946
e09342b5
TJB
10947 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10948 if (!num_regs)
2e70b7b9
MS
10949 return 0;
10950 else
e09342b5 10951 found_memory_cnt += num_regs;
2e70b7b9 10952 }
5c44784c 10953 }
c5aa993b 10954 }
5086187c
AC
10955 else if (VALUE_LVAL (v) != not_lval
10956 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10957 return 0; /* These are values from the history (e.g., $1). */
5086187c 10958 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10959 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10960 }
10961
10962 /* The expression itself looks suitable for using a hardware
10963 watchpoint, but give the target machine a chance to reject it. */
10964 return found_memory_cnt;
10965}
10966
8b93c638 10967void
2e362716 10968watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10969{
84f4c1fe 10970 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10971}
10972
a15a5258
AB
10973/* Options for the watch, awatch, and rwatch commands. */
10974
10975struct watch_options
10976{
10977 /* For -location. */
10978 bool location = false;
10979};
10980
10981/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10982
10983 Historically GDB always accepted both '-location' and '-l' flags for
10984 these commands (both flags being synonyms). When converting to the
10985 newer option scheme only '-location' is added here. That's fine (for
10986 backward compatibility) as any non-ambiguous prefix of a flag will be
10987 accepted, so '-l', '-loc', are now all accepted.
10988
10989 What this means is that, if in the future, we add any new flag here
10990 that starts with '-l' then this will break backward compatibility, so
10991 please, don't do that! */
10992
10993static const gdb::option::option_def watch_option_defs[] = {
10994 gdb::option::flag_option_def<watch_options> {
10995 "location",
10996 [] (watch_options *opt) { return &opt->location; },
10997 N_("\
10998This evaluates EXPRESSION and watches the memory to which is refers.\n\
10999-l can be used as a short form of -location."),
11000 },
11001};
11002
11003/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
11004 commands. */
11005
11006static gdb::option::option_def_group
11007make_watch_options_def_group (watch_options *opts)
11008{
11009 return {{watch_option_defs}, opts};
11010}
11011
06a64a0b
TT
11012/* A helper function that looks for the "-location" argument and then
11013 calls watch_command_1. */
11014
11015static void
0b39b52e 11016watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 11017{
a15a5258
AB
11018 watch_options opts;
11019 auto grp = make_watch_options_def_group (&opts);
11020 gdb::option::process_options
11021 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11022 if (arg != nullptr && *arg == '\0')
11023 arg = nullptr;
11024
11025 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11026}
06a64a0b 11027
a15a5258
AB
11028/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11029static void
11030watch_command_completer (struct cmd_list_element *ignore,
11031 completion_tracker &tracker,
11032 const char *text, const char * /*word*/)
11033{
11034 const auto group = make_watch_options_def_group (nullptr);
11035 if (gdb::option::complete_options
11036 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11037 return;
06a64a0b 11038
a15a5258
AB
11039 const char *word = advance_to_expression_complete_word_point (tracker, text);
11040 expression_completer (ignore, tracker, text, word);
8b93c638 11041}
8926118c 11042
c5aa993b 11043static void
0b39b52e 11044watch_command (const char *arg, int from_tty)
c906108c 11045{
06a64a0b 11046 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11047}
11048
8b93c638 11049void
2e362716 11050rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11051{
84f4c1fe 11052 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11053}
8926118c 11054
c5aa993b 11055static void
0b39b52e 11056rwatch_command (const char *arg, int from_tty)
c906108c 11057{
06a64a0b 11058 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11059}
11060
8b93c638 11061void
2e362716 11062awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11063{
84f4c1fe 11064 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11065}
8926118c 11066
c5aa993b 11067static void
0b39b52e 11068awatch_command (const char *arg, int from_tty)
c906108c 11069{
06a64a0b 11070 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11071}
c906108c 11072\f
c5aa993b 11073
cfc31633
PA
11074/* Data for the FSM that manages the until(location)/advance commands
11075 in infcmd.c. Here because it uses the mechanisms of
11076 breakpoints. */
c906108c 11077
46e3ed7f 11078struct until_break_fsm : public thread_fsm
bfec99b2 11079{
46e3ed7f 11080 /* The thread that was current when the command was executed. */
cfc31633
PA
11081 int thread;
11082
df631783
PA
11083 /* The breakpoint set at the return address in the caller frame,
11084 plus breakpoints at all the destination locations. */
11085 std::vector<breakpoint_up> breakpoints;
cfc31633 11086
46e3ed7f 11087 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11088 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11089 : thread_fsm (cmd_interp),
11090 thread (thread),
df631783 11091 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11092 {
11093 }
cfc31633 11094
46e3ed7f
TT
11095 void clean_up (struct thread_info *thread) override;
11096 bool should_stop (struct thread_info *thread) override;
11097 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11098};
11099
cfc31633
PA
11100/* Implementation of the 'should_stop' FSM method for the
11101 until(location)/advance commands. */
11102
46e3ed7f
TT
11103bool
11104until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11105{
df631783
PA
11106 for (const breakpoint_up &bp : breakpoints)
11107 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11108 bp.get ()) != NULL)
11109 {
11110 set_finished ();
11111 break;
11112 }
cfc31633 11113
46e3ed7f 11114 return true;
cfc31633
PA
11115}
11116
11117/* Implementation of the 'clean_up' FSM method for the
11118 until(location)/advance commands. */
11119
46e3ed7f
TT
11120void
11121until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11122{
cfc31633 11123 /* Clean up our temporary breakpoints. */
df631783 11124 breakpoints.clear ();
46e3ed7f 11125 delete_longjmp_breakpoint (thread);
cfc31633
PA
11126}
11127
11128/* Implementation of the 'async_reply_reason' FSM method for the
11129 until(location)/advance commands. */
11130
46e3ed7f
TT
11131enum async_reply_reason
11132until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11133{
11134 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11135}
11136
c906108c 11137void
f2fc3015 11138until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11139{
8556afb4
PA
11140 struct frame_info *frame;
11141 struct gdbarch *frame_gdbarch;
11142 struct frame_id stack_frame_id;
11143 struct frame_id caller_frame_id;
186c406b
TT
11144 int thread;
11145 struct thread_info *tp;
c906108c 11146
70509625 11147 clear_proceed_status (0);
c906108c
SS
11148
11149 /* Set a breakpoint where the user wants it and at return from
4a64f543 11150 this function. */
c5aa993b 11151
ffc2605c 11152 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11153
6c5b2ebe
PA
11154 std::vector<symtab_and_line> sals
11155 = (last_displayed_sal_is_valid ()
11156 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11157 get_last_displayed_symtab (),
11158 get_last_displayed_line ())
11159 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11160 NULL, NULL, 0));
c5aa993b 11161
df631783 11162 if (sals.empty ())
8a3fe4f8 11163 error (_("Couldn't get information on specified line."));
c5aa993b 11164
c906108c 11165 if (*arg)
8a3fe4f8 11166 error (_("Junk at end of arguments."));
c5aa993b 11167
186c406b 11168 tp = inferior_thread ();
5d5658a1 11169 thread = tp->global_num;
186c406b 11170
8556afb4
PA
11171 /* Note linespec handling above invalidates the frame chain.
11172 Installing a breakpoint also invalidates the frame chain (as it
11173 may need to switch threads), so do any frame handling before
11174 that. */
11175
11176 frame = get_selected_frame (NULL);
11177 frame_gdbarch = get_frame_arch (frame);
11178 stack_frame_id = get_stack_frame_id (frame);
11179 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11180
ae66c1fc
EZ
11181 /* Keep within the current frame, or in frames called by the current
11182 one. */
edb3359d 11183
df631783 11184 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11185
11186 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11187
883bc8d1 11188 if (frame_id_p (caller_frame_id))
c906108c 11189 {
883bc8d1 11190 struct symtab_and_line sal2;
cfc31633 11191 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11192
11193 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11194 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11195 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11196
11197 breakpoint_up caller_breakpoint
11198 = set_momentary_breakpoint (caller_gdbarch, sal2,
11199 caller_frame_id, bp_until);
11200 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11201
883bc8d1 11202 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11203 lj_deleter.emplace (thread);
c906108c 11204 }
c5aa993b 11205
c70a6932
JK
11206 /* set_momentary_breakpoint could invalidate FRAME. */
11207 frame = NULL;
11208
df631783
PA
11209 /* If the user told us to continue until a specified location, we
11210 don't specify a frame at which we need to stop. Otherwise,
11211 specify the selected frame, because we want to stop only at the
11212 very same frame. */
11213 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11214
11215 for (symtab_and_line &sal : sals)
11216 {
11217 resolve_sal_pc (&sal);
11218
11219 breakpoint_up location_breakpoint
11220 = set_momentary_breakpoint (frame_gdbarch, sal,
11221 stop_frame_id, bp_until);
11222 breakpoints.emplace_back (std::move (location_breakpoint));
11223 }
883bc8d1 11224
46e3ed7f 11225 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11226 std::move (breakpoints));
f107f563 11227
5419bdae
TT
11228 if (lj_deleter)
11229 lj_deleter->release ();
f107f563 11230
cfc31633 11231 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11232}
ae66c1fc 11233
c906108c
SS
11234/* This function attempts to parse an optional "if <cond>" clause
11235 from the arg string. If one is not found, it returns NULL.
c5aa993b 11236
c906108c
SS
11237 Else, it returns a pointer to the condition string. (It does not
11238 attempt to evaluate the string against a particular block.) And,
11239 it updates arg to point to the first character following the parsed
4a64f543 11240 if clause in the arg string. */
53a5351d 11241
63160a43
PA
11242const char *
11243ep_parse_optional_if_clause (const char **arg)
c906108c 11244{
63160a43 11245 const char *cond_string;
c5aa993b
JM
11246
11247 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11248 return NULL;
c5aa993b 11249
4a64f543 11250 /* Skip the "if" keyword. */
c906108c 11251 (*arg) += 2;
c5aa993b 11252
c906108c 11253 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11254 condition string. */
f1735a53 11255 *arg = skip_spaces (*arg);
c906108c 11256 cond_string = *arg;
c5aa993b 11257
4a64f543
MS
11258 /* Assume that the condition occupies the remainder of the arg
11259 string. */
c906108c 11260 (*arg) += strlen (cond_string);
c5aa993b 11261
c906108c
SS
11262 return cond_string;
11263}
c5aa993b 11264
c906108c
SS
11265/* Commands to deal with catching events, such as signals, exceptions,
11266 process start/exit, etc. */
c5aa993b
JM
11267
11268typedef enum
11269{
44feb3ce
TT
11270 catch_fork_temporary, catch_vfork_temporary,
11271 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11272}
11273catch_fork_kind;
11274
c906108c 11275static void
eb4c3f4a 11276catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11277 struct cmd_list_element *command)
c906108c 11278{
a6d9a66e 11279 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11280 const char *cond_string = NULL;
44feb3ce 11281 catch_fork_kind fork_kind;
44feb3ce
TT
11282
11283 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11284 bool temp = (fork_kind == catch_fork_temporary
11285 || fork_kind == catch_vfork_temporary);
c5aa993b 11286
44feb3ce
TT
11287 if (!arg)
11288 arg = "";
f1735a53 11289 arg = skip_spaces (arg);
c5aa993b 11290
c906108c 11291 /* The allowed syntax is:
c5aa993b
JM
11292 catch [v]fork
11293 catch [v]fork if <cond>
11294
4a64f543 11295 First, check if there's an if clause. */
c906108c 11296 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11297
c906108c 11298 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11299 error (_("Junk at end of arguments."));
c5aa993b 11300
c906108c 11301 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11302 and enable reporting of such events. */
c5aa993b
JM
11303 switch (fork_kind)
11304 {
44feb3ce
TT
11305 case catch_fork_temporary:
11306 case catch_fork_permanent:
b650a282 11307 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11308 &catch_fork_breakpoint_ops);
c906108c 11309 break;
44feb3ce
TT
11310 case catch_vfork_temporary:
11311 case catch_vfork_permanent:
b650a282 11312 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11313 &catch_vfork_breakpoint_ops);
c906108c 11314 break;
c5aa993b 11315 default:
8a3fe4f8 11316 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11317 break;
c5aa993b 11318 }
c906108c
SS
11319}
11320
11321static void
eb4c3f4a 11322catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11323 struct cmd_list_element *command)
c906108c 11324{
a6d9a66e 11325 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11326 const char *cond_string = NULL;
b650a282 11327 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11328
11329 if (!arg)
11330 arg = "";
f1735a53 11331 arg = skip_spaces (arg);
c906108c
SS
11332
11333 /* The allowed syntax is:
c5aa993b
JM
11334 catch exec
11335 catch exec if <cond>
c906108c 11336
4a64f543 11337 First, check if there's an if clause. */
c906108c
SS
11338 cond_string = ep_parse_optional_if_clause (&arg);
11339
11340 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11341 error (_("Junk at end of arguments."));
c906108c 11342
b270e6f9 11343 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11344 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11345 &catch_exec_breakpoint_ops);
11346 c->exec_pathname = NULL;
11347
b270e6f9 11348 install_breakpoint (0, std::move (c), 1);
c906108c 11349}
c5aa993b 11350
9ac4176b 11351void
28010a5d
PA
11352init_ada_exception_breakpoint (struct breakpoint *b,
11353 struct gdbarch *gdbarch,
11354 struct symtab_and_line sal,
f2fc3015 11355 const char *addr_string,
c0a91b2b 11356 const struct breakpoint_ops *ops,
28010a5d 11357 int tempflag,
349774ef 11358 int enabled,
28010a5d 11359 int from_tty)
f7f9143b 11360{
f7f9143b
JB
11361 if (from_tty)
11362 {
5af949e3
UW
11363 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11364 if (!loc_gdbarch)
11365 loc_gdbarch = gdbarch;
11366
6c95b8df
PA
11367 describe_other_breakpoints (loc_gdbarch,
11368 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11369 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11370 version for exception catchpoints, because two catchpoints
11371 used for different exception names will use the same address.
11372 In this case, a "breakpoint ... also set at..." warning is
11373 unproductive. Besides, the warning phrasing is also a bit
11374 inappropriate, we should use the word catchpoint, and tell
11375 the user what type of catchpoint it is. The above is good
11376 enough for now, though. */
f7f9143b
JB
11377 }
11378
f06f1252 11379 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11380
349774ef 11381 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11382 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11383 b->location = string_to_event_location (&addr_string,
11384 language_def (language_ada));
f7f9143b 11385 b->language = language_ada;
f7f9143b
JB
11386}
11387
c906108c
SS
11388\f
11389
81b1e71c 11390/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11391
11392static int
81b1e71c 11393compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11394{
81b1e71c
TT
11395 uintptr_t ua = (uintptr_t) a;
11396 uintptr_t ub = (uintptr_t) b;
8a2c437b 11397
81b1e71c 11398 if (a->number < b->number)
8a2c437b 11399 return -1;
81b1e71c 11400 else if (a->number > b->number)
8a2c437b
TT
11401 return 1;
11402
11403 /* Now sort by address, in case we see, e..g, two breakpoints with
11404 the number 0. */
11405 if (ua < ub)
11406 return -1;
94b0e70d 11407 return ua > ub ? 1 : 0;
8a2c437b
TT
11408}
11409
80f8a6eb 11410/* Delete breakpoints by address or line. */
c906108c
SS
11411
11412static void
0b39b52e 11413clear_command (const char *arg, int from_tty)
c906108c 11414{
c906108c 11415 int default_match;
c906108c 11416
6c5b2ebe
PA
11417 std::vector<symtab_and_line> decoded_sals;
11418 symtab_and_line last_sal;
11419 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11420 if (arg)
11421 {
6c5b2ebe
PA
11422 decoded_sals
11423 = decode_line_with_current_source (arg,
11424 (DECODE_LINE_FUNFIRSTLINE
11425 | DECODE_LINE_LIST_MODE));
c906108c 11426 default_match = 0;
6c5b2ebe 11427 sals = decoded_sals;
c906108c
SS
11428 }
11429 else
11430 {
1bfeeb0f
JL
11431 /* Set sal's line, symtab, pc, and pspace to the values
11432 corresponding to the last call to print_frame_info. If the
11433 codepoint is not valid, this will set all the fields to 0. */
51abb421 11434 last_sal = get_last_displayed_sal ();
6c5b2ebe 11435 if (last_sal.symtab == 0)
8a3fe4f8 11436 error (_("No source file specified."));
c906108c 11437
c906108c 11438 default_match = 1;
6c5b2ebe 11439 sals = last_sal;
c906108c
SS
11440 }
11441
4a64f543
MS
11442 /* We don't call resolve_sal_pc here. That's not as bad as it
11443 seems, because all existing breakpoints typically have both
11444 file/line and pc set. So, if clear is given file/line, we can
11445 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11446
11447 We only support clearing given the address explicitly
11448 present in breakpoint table. Say, we've set breakpoint
4a64f543 11449 at file:line. There were several PC values for that file:line,
ed0616c6 11450 due to optimization, all in one block.
4a64f543
MS
11451
11452 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11453 PC corresponding to the same file:line, the breakpoint won't
11454 be cleared. We probably can still clear the breakpoint, but
11455 since the other PC value is never presented to user, user
11456 can only find it by guessing, and it does not seem important
11457 to support that. */
11458
4a64f543
MS
11459 /* For each line spec given, delete bps which correspond to it. Do
11460 it in two passes, solely to preserve the current behavior that
11461 from_tty is forced true if we delete more than one
11462 breakpoint. */
c906108c 11463
81b1e71c 11464 std::vector<struct breakpoint *> found;
6c5b2ebe 11465 for (const auto &sal : sals)
c906108c 11466 {
05cba821
JK
11467 const char *sal_fullname;
11468
c906108c 11469 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11470 If line given (pc == 0), clear all bpts on specified line.
11471 If defaulting, clear all bpts on default line
11472 or at default pc.
c5aa993b 11473
dda83cd7 11474 defaulting sal.pc != 0 tests to do
c5aa993b 11475
dda83cd7
SM
11476 0 1 pc
11477 1 1 pc _and_ line
11478 0 0 line
11479 1 0 <can't happen> */
c906108c 11480
05cba821
JK
11481 sal_fullname = (sal.symtab == NULL
11482 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11483
4a64f543 11484 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 11485 for (breakpoint *b : all_breakpoints ())
c5aa993b 11486 {
0d381245 11487 int match = 0;
4a64f543 11488 /* Are we going to delete b? */
cc60f2e3 11489 if (b->type != bp_none && !is_watchpoint (b))
0d381245 11490 {
40cb8ca5 11491 for (bp_location *loc : b->locations ())
0d381245 11492 {
f8eba3c6
TT
11493 /* If the user specified file:line, don't allow a PC
11494 match. This matches historical gdb behavior. */
11495 int pc_match = (!sal.explicit_line
11496 && sal.pc
11497 && (loc->pspace == sal.pspace)
11498 && (loc->address == sal.pc)
11499 && (!section_is_overlay (loc->section)
11500 || loc->section == sal.section));
4aac40c8
TT
11501 int line_match = 0;
11502
11503 if ((default_match || sal.explicit_line)
2f202fde 11504 && loc->symtab != NULL
05cba821 11505 && sal_fullname != NULL
4aac40c8 11506 && sal.pspace == loc->pspace
05cba821
JK
11507 && loc->line_number == sal.line
11508 && filename_cmp (symtab_to_fullname (loc->symtab),
11509 sal_fullname) == 0)
11510 line_match = 1;
4aac40c8 11511
0d381245
VP
11512 if (pc_match || line_match)
11513 {
11514 match = 1;
11515 break;
11516 }
11517 }
11518 }
11519
11520 if (match)
81b1e71c 11521 found.push_back (b);
c906108c 11522 }
80f8a6eb 11523 }
8a2c437b 11524
80f8a6eb 11525 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11526 if (found.empty ())
80f8a6eb
MS
11527 {
11528 if (arg)
8a3fe4f8 11529 error (_("No breakpoint at %s."), arg);
80f8a6eb 11530 else
8a3fe4f8 11531 error (_("No breakpoint at this line."));
80f8a6eb 11532 }
c906108c 11533
8a2c437b 11534 /* Remove duplicates from the vec. */
81b1e71c 11535 std::sort (found.begin (), found.end (),
b926417a 11536 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11537 {
b926417a 11538 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11539 });
11540 found.erase (std::unique (found.begin (), found.end (),
b926417a 11541 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11542 {
b926417a 11543 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11544 }),
11545 found.end ());
8a2c437b 11546
81b1e71c 11547 if (found.size () > 1)
4a64f543 11548 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11549 if (from_tty)
a3f17187 11550 {
81b1e71c 11551 if (found.size () == 1)
a3f17187
AC
11552 printf_unfiltered (_("Deleted breakpoint "));
11553 else
11554 printf_unfiltered (_("Deleted breakpoints "));
11555 }
d6e956e5 11556
81b1e71c 11557 for (breakpoint *iter : found)
80f8a6eb 11558 {
c5aa993b 11559 if (from_tty)
81b1e71c
TT
11560 printf_unfiltered ("%d ", iter->number);
11561 delete_breakpoint (iter);
c906108c 11562 }
80f8a6eb
MS
11563 if (from_tty)
11564 putchar_unfiltered ('\n');
c906108c
SS
11565}
11566\f
11567/* Delete breakpoint in BS if they are `delete' breakpoints and
11568 all breakpoints that are marked for deletion, whether hit or not.
11569 This is called after any breakpoint is hit, or after errors. */
11570
11571void
fba45db2 11572breakpoint_auto_delete (bpstat bs)
c906108c 11573{
c906108c 11574 for (; bs; bs = bs->next)
f431efe5
PA
11575 if (bs->breakpoint_at
11576 && bs->breakpoint_at->disposition == disp_del
c906108c 11577 && bs->stop)
f431efe5 11578 delete_breakpoint (bs->breakpoint_at);
c906108c 11579
1428b37a 11580 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 11581 if (b->disposition == disp_del_at_next_stop)
c5aa993b 11582 delete_breakpoint (b);
c906108c
SS
11583}
11584
4a64f543 11585/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11586 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11587 bl_address_is_meaningful says), secondarily by ordering first
11588 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11589 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11590
11591static int
39ef2f62 11592bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11593{
876fa593 11594 if (a->address != b->address)
39ef2f62 11595 return a->address < b->address;
876fa593 11596
dea2aa5f
LM
11597 /* Sort locations at the same address by their pspace number, keeping
11598 locations of the same inferior (in a multi-inferior environment)
11599 grouped. */
11600
11601 if (a->pspace->num != b->pspace->num)
39ef2f62 11602 return a->pspace->num < b->pspace->num;
dea2aa5f 11603
876fa593 11604 /* Sort permanent breakpoints first. */
1a853c52 11605 if (a->permanent != b->permanent)
39ef2f62 11606 return a->permanent > b->permanent;
876fa593 11607
7f32a4d5
PA
11608 /* Sort by type in order to make duplicate determination easier.
11609 See update_global_location_list. This is kept in sync with
11610 breakpoint_locations_match. */
11611 if (a->loc_type < b->loc_type)
11612 return true;
11613
11614 /* Likewise, for range-breakpoints, sort by length. */
11615 if (a->loc_type == bp_loc_hardware_breakpoint
11616 && b->loc_type == bp_loc_hardware_breakpoint
11617 && a->length < b->length)
11618 return true;
11619
c56a97f9
JK
11620 /* Make the internal GDB representation stable across GDB runs
11621 where A and B memory inside GDB can differ. Breakpoint locations of
11622 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11623
11624 if (a->owner->number != b->owner->number)
39ef2f62 11625 return a->owner->number < b->owner->number;
876fa593 11626
39ef2f62 11627 return a < b;
876fa593
JK
11628}
11629
f5336ca5
PA
11630/* Set bp_locations_placed_address_before_address_max and
11631 bp_locations_shadow_len_after_address_max according to the current
11632 content of the bp_locations array. */
f7545552
TT
11633
11634static void
f5336ca5 11635bp_locations_target_extensions_update (void)
f7545552 11636{
f5336ca5
PA
11637 bp_locations_placed_address_before_address_max = 0;
11638 bp_locations_shadow_len_after_address_max = 0;
876fa593 11639
48d7020b 11640 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11641 {
11642 CORE_ADDR start, end, addr;
11643
11644 if (!bp_location_has_shadow (bl))
11645 continue;
11646
11647 start = bl->target_info.placed_address;
11648 end = start + bl->target_info.shadow_len;
11649
11650 gdb_assert (bl->address >= start);
11651 addr = bl->address - start;
f5336ca5
PA
11652 if (addr > bp_locations_placed_address_before_address_max)
11653 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11654
11655 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11656
11657 gdb_assert (bl->address < end);
11658 addr = end - bl->address;
f5336ca5
PA
11659 if (addr > bp_locations_shadow_len_after_address_max)
11660 bp_locations_shadow_len_after_address_max = addr;
876fa593 11661 }
f7545552
TT
11662}
11663
1e4d1764
YQ
11664/* Download tracepoint locations if they haven't been. */
11665
11666static void
11667download_tracepoint_locations (void)
11668{
dd2e65cc 11669 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11670
5ed8105e 11671 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11672
f6d17b2b 11673 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11674 {
11675 struct tracepoint *t;
f2a8bc8a 11676 int bp_location_downloaded = 0;
1e4d1764 11677
7ed2c994 11678 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11679 ? !may_insert_fast_tracepoints
11680 : !may_insert_tracepoints))
11681 continue;
11682
dd2e65cc
YQ
11683 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11684 {
11685 if (target_can_download_tracepoint ())
11686 can_download_tracepoint = TRIBOOL_TRUE;
11687 else
11688 can_download_tracepoint = TRIBOOL_FALSE;
11689 }
11690
11691 if (can_download_tracepoint == TRIBOOL_FALSE)
11692 break;
11693
40cb8ca5 11694 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11695 {
11696 /* In tracepoint, locations are _never_ duplicated, so
11697 should_be_inserted is equivalent to
11698 unduplicated_should_be_inserted. */
11699 if (!should_be_inserted (bl) || bl->inserted)
11700 continue;
1e4d1764 11701
7ed2c994 11702 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11703
7ed2c994 11704 target_download_tracepoint (bl);
1e4d1764 11705
7ed2c994 11706 bl->inserted = 1;
f2a8bc8a 11707 bp_location_downloaded = 1;
7ed2c994
YQ
11708 }
11709 t = (struct tracepoint *) b;
11710 t->number_on_target = b->number;
f2a8bc8a 11711 if (bp_location_downloaded)
76727919 11712 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11713 }
1e4d1764
YQ
11714}
11715
934709f0
PW
11716/* Swap the insertion/duplication state between two locations. */
11717
11718static void
11719swap_insertion (struct bp_location *left, struct bp_location *right)
11720{
11721 const int left_inserted = left->inserted;
11722 const int left_duplicate = left->duplicate;
b775012e 11723 const int left_needs_update = left->needs_update;
934709f0
PW
11724 const struct bp_target_info left_target_info = left->target_info;
11725
1e4d1764
YQ
11726 /* Locations of tracepoints can never be duplicated. */
11727 if (is_tracepoint (left->owner))
11728 gdb_assert (!left->duplicate);
11729 if (is_tracepoint (right->owner))
11730 gdb_assert (!right->duplicate);
11731
934709f0
PW
11732 left->inserted = right->inserted;
11733 left->duplicate = right->duplicate;
b775012e 11734 left->needs_update = right->needs_update;
934709f0
PW
11735 left->target_info = right->target_info;
11736 right->inserted = left_inserted;
11737 right->duplicate = left_duplicate;
b775012e 11738 right->needs_update = left_needs_update;
934709f0
PW
11739 right->target_info = left_target_info;
11740}
11741
b775012e
LM
11742/* Force the re-insertion of the locations at ADDRESS. This is called
11743 once a new/deleted/modified duplicate location is found and we are evaluating
11744 conditions on the target's side. Such conditions need to be updated on
11745 the target. */
11746
11747static void
11748force_breakpoint_reinsertion (struct bp_location *bl)
11749{
b775012e
LM
11750 CORE_ADDR address = 0;
11751 int pspace_num;
11752
11753 address = bl->address;
11754 pspace_num = bl->pspace->num;
11755
11756 /* This is only meaningful if the target is
11757 evaluating conditions and if the user has
11758 opted for condition evaluation on the target's
11759 side. */
11760 if (gdb_evaluates_breakpoint_condition_p ()
11761 || !target_supports_evaluation_of_breakpoint_conditions ())
11762 return;
11763
11764 /* Flag all breakpoint locations with this address and
11765 the same program space as the location
11766 as "its condition has changed". We need to
11767 update the conditions on the target's side. */
e0d9a270 11768 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11769 {
b775012e
LM
11770 if (!is_breakpoint (loc->owner)
11771 || pspace_num != loc->pspace->num)
11772 continue;
11773
11774 /* Flag the location appropriately. We use a different state to
11775 let everyone know that we already updated the set of locations
11776 with addr bl->address and program space bl->pspace. This is so
11777 we don't have to keep calling these functions just to mark locations
11778 that have already been marked. */
11779 loc->condition_changed = condition_updated;
11780
11781 /* Free the agent expression bytecode as well. We will compute
11782 it later on. */
833177a4 11783 loc->cond_bytecode.reset ();
b775012e
LM
11784 }
11785}
7f32a4d5 11786
44702360
PA
11787/* Called whether new breakpoints are created, or existing breakpoints
11788 deleted, to update the global location list and recompute which
11789 locations are duplicate of which.
b775012e 11790
04086b45
PA
11791 The INSERT_MODE flag determines whether locations may not, may, or
11792 shall be inserted now. See 'enum ugll_insert_mode' for more
11793 info. */
b60e7edf 11794
0d381245 11795static void
44702360 11796update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11797{
b775012e
LM
11798 /* Last breakpoint location address that was marked for update. */
11799 CORE_ADDR last_addr = 0;
11800 /* Last breakpoint location program space that was marked for update. */
11801 int last_pspace_num = -1;
f7545552 11802
2d134ed3
PA
11803 /* Used in the duplicates detection below. When iterating over all
11804 bp_locations, points to the first bp_location of a given address.
11805 Breakpoints and watchpoints of different types are never
11806 duplicates of each other. Keep one pointer for each type of
11807 breakpoint/watchpoint, so we only need to loop over all locations
11808 once. */
11809 struct bp_location *bp_loc_first; /* breakpoint */
11810 struct bp_location *wp_loc_first; /* hardware watchpoint */
11811 struct bp_location *awp_loc_first; /* access watchpoint */
11812 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11813
f5336ca5
PA
11814 /* Saved former bp_locations array which we compare against the newly
11815 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11816 std::vector<bp_location *> old_locations = std::move (bp_locations);
11817 bp_locations.clear ();
876fa593 11818
43892fdf 11819 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11820 for (bp_location *loc : b->locations ())
5d51cd5d 11821 bp_locations.push_back (loc);
7f32a4d5
PA
11822
11823 /* See if we need to "upgrade" a software breakpoint to a hardware
11824 breakpoint. Do this before deciding whether locations are
11825 duplicates. Also do this before sorting because sorting order
11826 depends on location type. */
5d51cd5d
SM
11827 for (bp_location *loc : bp_locations)
11828 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11829 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11830
5d51cd5d 11831 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11832 bp_location_is_less_than);
876fa593 11833
f5336ca5 11834 bp_locations_target_extensions_update ();
74960c60 11835
4a64f543
MS
11836 /* Identify bp_location instances that are no longer present in the
11837 new list, and therefore should be freed. Note that it's not
11838 necessary that those locations should be removed from inferior --
11839 if there's another location at the same address (previously
11840 marked as duplicate), we don't need to remove/insert the
11841 location.
876fa593 11842
4a64f543
MS
11843 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11844 and former bp_location array state respectively. */
876fa593 11845
5d51cd5d
SM
11846 size_t loc_i = 0;
11847 for (bp_location *old_loc : old_locations)
74960c60 11848 {
e5dd4106 11849 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11850 not, we have to free it. */
c7d46a38 11851 int found_object = 0;
20874c92
VP
11852 /* Tells if the location should remain inserted in the target. */
11853 int keep_in_target = 0;
11854 int removed = 0;
876fa593 11855
4a64f543
MS
11856 /* Skip LOCP entries which will definitely never be needed.
11857 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11858 while (loc_i < bp_locations.size ()
11859 && bp_locations[loc_i]->address < old_loc->address)
11860 loc_i++;
c7d46a38 11861
5d51cd5d
SM
11862 for (size_t loc2_i = loc_i;
11863 (loc2_i < bp_locations.size ()
11864 && bp_locations[loc2_i]->address == old_loc->address);
11865 loc2_i++)
c7d46a38 11866 {
b775012e
LM
11867 /* Check if this is a new/duplicated location or a duplicated
11868 location that had its condition modified. If so, we want to send
11869 its condition to the target if evaluation of conditions is taking
11870 place there. */
5d51cd5d 11871 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11872 && (last_addr != old_loc->address
11873 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11874 {
5d51cd5d 11875 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11876 last_pspace_num = old_loc->pspace->num;
c7d46a38 11877 }
b775012e 11878
5d51cd5d 11879 if (bp_locations[loc2_i] == old_loc)
b775012e 11880 found_object = 1;
c7d46a38 11881 }
74960c60 11882
b775012e
LM
11883 /* We have already handled this address, update it so that we don't
11884 have to go through updates again. */
11885 last_addr = old_loc->address;
11886
11887 /* Target-side condition evaluation: Handle deleted locations. */
11888 if (!found_object)
11889 force_breakpoint_reinsertion (old_loc);
11890
4a64f543
MS
11891 /* If this location is no longer present, and inserted, look if
11892 there's maybe a new location at the same address. If so,
11893 mark that one inserted, and don't remove this one. This is
11894 needed so that we don't have a time window where a breakpoint
11895 at certain location is not inserted. */
74960c60 11896
876fa593 11897 if (old_loc->inserted)
0d381245 11898 {
4a64f543
MS
11899 /* If the location is inserted now, we might have to remove
11900 it. */
74960c60 11901
876fa593 11902 if (found_object && should_be_inserted (old_loc))
74960c60 11903 {
4a64f543
MS
11904 /* The location is still present in the location list,
11905 and still should be inserted. Don't do anything. */
20874c92 11906 keep_in_target = 1;
74960c60
VP
11907 }
11908 else
11909 {
b775012e
LM
11910 /* This location still exists, but it won't be kept in the
11911 target since it may have been disabled. We proceed to
11912 remove its target-side condition. */
11913
4a64f543
MS
11914 /* The location is either no longer present, or got
11915 disabled. See if there's another location at the
11916 same address, in which case we don't need to remove
11917 this one from the target. */
876fa593 11918
2bdf28a0 11919 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11920 if (bl_address_is_meaningful (old_loc))
876fa593 11921 {
5d51cd5d
SM
11922 for (size_t loc2_i = loc_i;
11923 (loc2_i < bp_locations.size ()
11924 && bp_locations[loc2_i]->address == old_loc->address);
11925 loc2_i++)
876fa593 11926 {
5d51cd5d 11927 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11928
7f32a4d5
PA
11929 if (loc2 == old_loc)
11930 continue;
11931
2d134ed3 11932 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11933 {
85d721b8
PA
11934 /* Read watchpoint locations are switched to
11935 access watchpoints, if the former are not
11936 supported, but the latter are. */
11937 if (is_hardware_watchpoint (old_loc->owner))
11938 {
11939 gdb_assert (is_hardware_watchpoint (loc2->owner));
11940 loc2->watchpoint_type = old_loc->watchpoint_type;
11941 }
11942
934709f0
PW
11943 /* loc2 is a duplicated location. We need to check
11944 if it should be inserted in case it will be
11945 unduplicated. */
7f32a4d5 11946 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11947 {
934709f0 11948 swap_insertion (old_loc, loc2);
c7d46a38
PA
11949 keep_in_target = 1;
11950 break;
11951 }
876fa593
JK
11952 }
11953 }
11954 }
74960c60
VP
11955 }
11956
20874c92
VP
11957 if (!keep_in_target)
11958 {
834c0d03 11959 if (remove_breakpoint (old_loc))
20874c92 11960 {
4a64f543
MS
11961 /* This is just about all we can do. We could keep
11962 this location on the global list, and try to
11963 remove it next time, but there's no particular
11964 reason why we will succeed next time.
20874c92 11965
4a64f543
MS
11966 Note that at this point, old_loc->owner is still
11967 valid, as delete_breakpoint frees the breakpoint
11968 only after calling us. */
3e43a32a
MS
11969 printf_filtered (_("warning: Error removing "
11970 "breakpoint %d\n"),
876fa593 11971 old_loc->owner->number);
20874c92
VP
11972 }
11973 removed = 1;
11974 }
0d381245 11975 }
74960c60
VP
11976
11977 if (!found_object)
1c5cfe86 11978 {
fbea99ea 11979 if (removed && target_is_non_stop_p ()
1cf4d951 11980 && need_moribund_for_location_type (old_loc))
20874c92 11981 {
db82e815
PA
11982 /* This location was removed from the target. In
11983 non-stop mode, a race condition is possible where
11984 we've removed a breakpoint, but stop events for that
11985 breakpoint are already queued and will arrive later.
11986 We apply an heuristic to be able to distinguish such
11987 SIGTRAPs from other random SIGTRAPs: we keep this
11988 breakpoint location for a bit, and will retire it
11989 after we see some number of events. The theory here
11990 is that reporting of events should, "on the average",
11991 be fair, so after a while we'll see events from all
11992 threads that have anything of interest, and no longer
11993 need to keep this breakpoint location around. We
11994 don't hold locations forever so to reduce chances of
11995 mistaking a non-breakpoint SIGTRAP for a breakpoint
11996 SIGTRAP.
11997
11998 The heuristic failing can be disastrous on
11999 decr_pc_after_break targets.
12000
12001 On decr_pc_after_break targets, like e.g., x86-linux,
12002 if we fail to recognize a late breakpoint SIGTRAP,
12003 because events_till_retirement has reached 0 too
12004 soon, we'll fail to do the PC adjustment, and report
12005 a random SIGTRAP to the user. When the user resumes
12006 the inferior, it will most likely immediately crash
2dec564e 12007 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12008 corrupted, because of being resumed e.g., in the
12009 middle of a multi-byte instruction, or skipped a
12010 one-byte instruction. This was actually seen happen
12011 on native x86-linux, and should be less rare on
12012 targets that do not support new thread events, like
12013 remote, due to the heuristic depending on
12014 thread_count.
12015
12016 Mistaking a random SIGTRAP for a breakpoint trap
12017 causes similar symptoms (PC adjustment applied when
12018 it shouldn't), but then again, playing with SIGTRAPs
12019 behind the debugger's back is asking for trouble.
12020
12021 Since hardware watchpoint traps are always
12022 distinguishable from other traps, so we don't need to
12023 apply keep hardware watchpoint moribund locations
12024 around. We simply always ignore hardware watchpoint
12025 traps we can no longer explain. */
12026
5b6d1e4f
PA
12027 process_stratum_target *proc_target = nullptr;
12028 for (inferior *inf : all_inferiors ())
12029 if (inf->pspace == old_loc->pspace)
12030 {
12031 proc_target = inf->process_target ();
12032 break;
12033 }
12034 if (proc_target != nullptr)
12035 old_loc->events_till_retirement
12036 = 3 * (thread_count (proc_target) + 1);
12037 else
12038 old_loc->events_till_retirement = 1;
876fa593 12039 old_loc->owner = NULL;
20874c92 12040
1123588c 12041 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12042 }
12043 else
f431efe5
PA
12044 {
12045 old_loc->owner = NULL;
12046 decref_bp_location (&old_loc);
12047 }
20874c92 12048 }
74960c60 12049 }
1c5cfe86 12050
348d480f
PA
12051 /* Rescan breakpoints at the same address and section, marking the
12052 first one as "first" and any others as "duplicates". This is so
12053 that the bpt instruction is only inserted once. If we have a
12054 permanent breakpoint at the same place as BPT, make that one the
12055 official one, and the rest as duplicates. Permanent breakpoints
12056 are sorted first for the same address.
12057
12058 Do the same for hardware watchpoints, but also considering the
12059 watchpoint's type (regular/access/read) and length. */
12060
12061 bp_loc_first = NULL;
12062 wp_loc_first = NULL;
12063 awp_loc_first = NULL;
12064 rwp_loc_first = NULL;
40cb8ca5 12065
48d7020b 12066 for (bp_location *loc : all_bp_locations ())
348d480f
PA
12067 {
12068 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12069 non-NULL. */
348d480f 12070 struct bp_location **loc_first_p;
43892fdf 12071 breakpoint *b = loc->owner;
348d480f 12072
6f380991 12073 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12074 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12075 /* Don't detect duplicate for tracepoint locations because they are
12076 never duplicated. See the comments in field `duplicate' of
12077 `struct bp_location'. */
348d480f 12078 || is_tracepoint (b))
b775012e
LM
12079 {
12080 /* Clear the condition modification flag. */
12081 loc->condition_changed = condition_unchanged;
12082 continue;
12083 }
348d480f 12084
348d480f
PA
12085 if (b->type == bp_hardware_watchpoint)
12086 loc_first_p = &wp_loc_first;
12087 else if (b->type == bp_read_watchpoint)
12088 loc_first_p = &rwp_loc_first;
12089 else if (b->type == bp_access_watchpoint)
12090 loc_first_p = &awp_loc_first;
12091 else
12092 loc_first_p = &bp_loc_first;
12093
12094 if (*loc_first_p == NULL
12095 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12096 || !breakpoint_locations_match (loc, *loc_first_p))
12097 {
12098 *loc_first_p = loc;
12099 loc->duplicate = 0;
b775012e
LM
12100
12101 if (is_breakpoint (loc->owner) && loc->condition_changed)
12102 {
12103 loc->needs_update = 1;
12104 /* Clear the condition modification flag. */
12105 loc->condition_changed = condition_unchanged;
12106 }
348d480f
PA
12107 continue;
12108 }
12109
934709f0
PW
12110
12111 /* This and the above ensure the invariant that the first location
12112 is not duplicated, and is the inserted one.
12113 All following are marked as duplicated, and are not inserted. */
12114 if (loc->inserted)
12115 swap_insertion (loc, *loc_first_p);
348d480f
PA
12116 loc->duplicate = 1;
12117
b775012e
LM
12118 /* Clear the condition modification flag. */
12119 loc->condition_changed = condition_unchanged;
348d480f
PA
12120 }
12121
a25a5a45 12122 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12123 {
04086b45 12124 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12125 insert_breakpoint_locations ();
12126 else
12127 {
44702360
PA
12128 /* Even though the caller told us to not insert new
12129 locations, we may still need to update conditions on the
12130 target's side of breakpoints that were already inserted
12131 if the target is evaluating breakpoint conditions. We
b775012e
LM
12132 only update conditions for locations that are marked
12133 "needs_update". */
12134 update_inserted_breakpoint_locations ();
12135 }
12136 }
348d480f 12137
04086b45 12138 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12139 download_tracepoint_locations ();
348d480f
PA
12140}
12141
12142void
12143breakpoint_retire_moribund (void)
12144{
1123588c
TT
12145 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12146 {
12147 struct bp_location *loc = moribund_locations[ix];
12148 if (--(loc->events_till_retirement) == 0)
12149 {
12150 decref_bp_location (&loc);
12151 unordered_remove (moribund_locations, ix);
12152 --ix;
12153 }
12154 }
348d480f
PA
12155}
12156
12157static void
44702360 12158update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12159{
348d480f 12160
a70b8144 12161 try
492d29ea
PA
12162 {
12163 update_global_location_list (insert_mode);
12164 }
230d2906 12165 catch (const gdb_exception_error &e)
492d29ea
PA
12166 {
12167 }
348d480f
PA
12168}
12169
12170/* Clear BKP from a BPS. */
12171
12172static void
12173bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12174{
12175 bpstat bs;
12176
12177 for (bs = bps; bs; bs = bs->next)
12178 if (bs->breakpoint_at == bpt)
12179 {
12180 bs->breakpoint_at = NULL;
12181 bs->old_val = NULL;
12182 /* bs->commands will be freed later. */
12183 }
12184}
12185
12186/* Callback for iterate_over_threads. */
12187static int
12188bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12189{
9a3c8263 12190 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12191
12192 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12193 return 0;
12194}
12195
12196/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12197 callbacks. */
12198
12199static void
12200say_where (struct breakpoint *b)
12201{
12202 struct value_print_options opts;
12203
12204 get_user_print_options (&opts);
12205
12206 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12207 single string. */
12208 if (b->loc == NULL)
12209 {
f00aae0f
KS
12210 /* For pending locations, the output differs slightly based
12211 on b->extra_string. If this is non-NULL, it contains either
12212 a condition or dprintf arguments. */
12213 if (b->extra_string == NULL)
12214 {
12215 printf_filtered (_(" (%s) pending."),
d28cd78a 12216 event_location_to_string (b->location.get ()));
f00aae0f
KS
12217 }
12218 else if (b->type == bp_dprintf)
12219 {
12220 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12221 event_location_to_string (b->location.get ()),
f00aae0f
KS
12222 b->extra_string);
12223 }
12224 else
12225 {
12226 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12227 event_location_to_string (b->location.get ()),
f00aae0f
KS
12228 b->extra_string);
12229 }
348d480f
PA
12230 }
12231 else
12232 {
2f202fde 12233 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12234 printf_filtered (" at %ps",
12235 styled_string (address_style.style (),
12236 paddress (b->loc->gdbarch,
12237 b->loc->address)));
2f202fde 12238 if (b->loc->symtab != NULL)
f8eba3c6
TT
12239 {
12240 /* If there is a single location, we can print the location
12241 more nicely. */
12242 if (b->loc->next == NULL)
0bb296cb 12243 {
6a831f06
PA
12244 const char *filename
12245 = symtab_to_filename_for_display (b->loc->symtab);
12246 printf_filtered (": file %ps, line %d.",
12247 styled_string (file_name_style.style (),
12248 filename),
0bb296cb
TT
12249 b->loc->line_number);
12250 }
f8eba3c6
TT
12251 else
12252 /* This is not ideal, but each location may have a
12253 different file name, and this at least reflects the
12254 real situation somewhat. */
f00aae0f 12255 printf_filtered (": %s.",
d28cd78a 12256 event_location_to_string (b->location.get ()));
f8eba3c6 12257 }
348d480f
PA
12258
12259 if (b->loc->next)
12260 {
12261 struct bp_location *loc = b->loc;
12262 int n = 0;
12263 for (; loc; loc = loc->next)
12264 ++n;
12265 printf_filtered (" (%d locations)", n);
12266 }
12267 }
12268}
12269
5f486660 12270bp_location::~bp_location ()
348d480f 12271{
5f486660 12272 xfree (function_name);
348d480f
PA
12273}
12274
c1fc2657 12275/* Destructor for the breakpoint base class. */
348d480f 12276
c1fc2657 12277breakpoint::~breakpoint ()
348d480f 12278{
c1fc2657
SM
12279 xfree (this->cond_string);
12280 xfree (this->extra_string);
348d480f
PA
12281}
12282
40cb8ca5
SM
12283/* See breakpoint.h. */
12284
12285bp_locations_range breakpoint::locations ()
12286{
12287 return bp_locations_range (this->loc);
12288}
12289
2060206e
PA
12290static struct bp_location *
12291base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12292{
5f486660 12293 return new bp_location (self);
348d480f
PA
12294}
12295
2060206e
PA
12296static void
12297base_breakpoint_re_set (struct breakpoint *b)
12298{
12299 /* Nothing to re-set. */
12300}
12301
12302#define internal_error_pure_virtual_called() \
12303 gdb_assert_not_reached ("pure virtual function called")
12304
12305static int
12306base_breakpoint_insert_location (struct bp_location *bl)
12307{
12308 internal_error_pure_virtual_called ();
12309}
12310
12311static int
73971819
PA
12312base_breakpoint_remove_location (struct bp_location *bl,
12313 enum remove_bp_reason reason)
2060206e
PA
12314{
12315 internal_error_pure_virtual_called ();
12316}
12317
12318static int
12319base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12320 const address_space *aspace,
09ac7c10
TT
12321 CORE_ADDR bp_addr,
12322 const struct target_waitstatus *ws)
2060206e
PA
12323{
12324 internal_error_pure_virtual_called ();
12325}
12326
12327static void
12328base_breakpoint_check_status (bpstat bs)
12329{
12330 /* Always stop. */
12331}
12332
12333/* A "works_in_software_mode" breakpoint_ops method that just internal
12334 errors. */
12335
12336static int
12337base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12338{
12339 internal_error_pure_virtual_called ();
12340}
12341
12342/* A "resources_needed" breakpoint_ops method that just internal
12343 errors. */
12344
12345static int
12346base_breakpoint_resources_needed (const struct bp_location *bl)
12347{
12348 internal_error_pure_virtual_called ();
12349}
12350
12351static enum print_stop_action
12352base_breakpoint_print_it (bpstat bs)
12353{
12354 internal_error_pure_virtual_called ();
12355}
12356
12357static void
12358base_breakpoint_print_one_detail (const struct breakpoint *self,
12359 struct ui_out *uiout)
12360{
12361 /* nothing */
12362}
12363
12364static void
12365base_breakpoint_print_mention (struct breakpoint *b)
12366{
12367 internal_error_pure_virtual_called ();
12368}
12369
12370static void
12371base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12372{
12373 internal_error_pure_virtual_called ();
12374}
12375
983af33b 12376static void
f00aae0f 12377base_breakpoint_create_sals_from_location
626d2320 12378 (struct event_location *location,
f00aae0f
KS
12379 struct linespec_result *canonical,
12380 enum bptype type_wanted)
983af33b
SDJ
12381{
12382 internal_error_pure_virtual_called ();
12383}
12384
12385static void
12386base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12387 struct linespec_result *c,
e1e01040
PA
12388 gdb::unique_xmalloc_ptr<char> cond_string,
12389 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12390 enum bptype type_wanted,
12391 enum bpdisp disposition,
12392 int thread,
12393 int task, int ignore_count,
12394 const struct breakpoint_ops *o,
12395 int from_tty, int enabled,
44f238bb 12396 int internal, unsigned flags)
983af33b
SDJ
12397{
12398 internal_error_pure_virtual_called ();
12399}
12400
6c5b2ebe 12401static std::vector<symtab_and_line>
f00aae0f 12402base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12403 struct event_location *location,
6c5b2ebe 12404 struct program_space *search_pspace)
983af33b
SDJ
12405{
12406 internal_error_pure_virtual_called ();
12407}
12408
ab04a2af
TT
12409/* The default 'explains_signal' method. */
12410
47591c29 12411static int
427cd150 12412base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12413{
47591c29 12414 return 1;
ab04a2af
TT
12415}
12416
9d6e6e84
HZ
12417/* The default "after_condition_true" method. */
12418
12419static void
12420base_breakpoint_after_condition_true (struct bpstats *bs)
12421{
12422 /* Nothing to do. */
12423}
12424
ab04a2af 12425struct breakpoint_ops base_breakpoint_ops =
2060206e 12426{
2060206e
PA
12427 base_breakpoint_allocate_location,
12428 base_breakpoint_re_set,
12429 base_breakpoint_insert_location,
12430 base_breakpoint_remove_location,
12431 base_breakpoint_breakpoint_hit,
12432 base_breakpoint_check_status,
12433 base_breakpoint_resources_needed,
12434 base_breakpoint_works_in_software_mode,
12435 base_breakpoint_print_it,
12436 NULL,
12437 base_breakpoint_print_one_detail,
12438 base_breakpoint_print_mention,
983af33b 12439 base_breakpoint_print_recreate,
5f700d83 12440 base_breakpoint_create_sals_from_location,
983af33b 12441 base_breakpoint_create_breakpoints_sal,
5f700d83 12442 base_breakpoint_decode_location,
9d6e6e84
HZ
12443 base_breakpoint_explains_signal,
12444 base_breakpoint_after_condition_true,
2060206e
PA
12445};
12446
12447/* Default breakpoint_ops methods. */
12448
12449static void
348d480f
PA
12450bkpt_re_set (struct breakpoint *b)
12451{
06edf0c0 12452 /* FIXME: is this still reachable? */
9ef9e6a6 12453 if (breakpoint_event_location_empty_p (b))
06edf0c0 12454 {
f00aae0f 12455 /* Anything without a location can't be re-set. */
348d480f 12456 delete_breakpoint (b);
06edf0c0 12457 return;
348d480f 12458 }
06edf0c0
PA
12459
12460 breakpoint_re_set_default (b);
348d480f
PA
12461}
12462
2060206e 12463static int
348d480f
PA
12464bkpt_insert_location (struct bp_location *bl)
12465{
cd6c3b4f
YQ
12466 CORE_ADDR addr = bl->target_info.reqstd_address;
12467
579c6ad9 12468 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12469 bl->target_info.placed_address = addr;
12470
348d480f 12471 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12472 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12473 else
7c16b83e 12474 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12475}
12476
2060206e 12477static int
73971819 12478bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12479{
12480 if (bl->loc_type == bp_loc_hardware_breakpoint)
12481 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12482 else
73971819 12483 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12484}
12485
2060206e 12486static int
348d480f 12487bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12488 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12489 const struct target_waitstatus *ws)
348d480f 12490{
09ac7c10 12491 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12492 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12493 return 0;
12494
348d480f
PA
12495 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12496 aspace, bp_addr))
12497 return 0;
12498
12499 if (overlay_debugging /* unmapped overlay section */
12500 && section_is_overlay (bl->section)
12501 && !section_is_mapped (bl->section))
12502 return 0;
12503
12504 return 1;
12505}
12506
cd1608cc
PA
12507static int
12508dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12509 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12510 const struct target_waitstatus *ws)
12511{
12512 if (dprintf_style == dprintf_style_agent
12513 && target_can_run_breakpoint_commands ())
12514 {
12515 /* An agent-style dprintf never causes a stop. If we see a trap
12516 for this address it must be for a breakpoint that happens to
12517 be set at the same address. */
12518 return 0;
12519 }
12520
12521 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12522}
12523
2060206e 12524static int
348d480f
PA
12525bkpt_resources_needed (const struct bp_location *bl)
12526{
12527 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12528
12529 return 1;
12530}
12531
2060206e 12532static enum print_stop_action
348d480f
PA
12533bkpt_print_it (bpstat bs)
12534{
348d480f
PA
12535 struct breakpoint *b;
12536 const struct bp_location *bl;
001c8c33 12537 int bp_temp;
79a45e25 12538 struct ui_out *uiout = current_uiout;
348d480f
PA
12539
12540 gdb_assert (bs->bp_location_at != NULL);
12541
b6433ede 12542 bl = bs->bp_location_at.get ();
348d480f
PA
12543 b = bs->breakpoint_at;
12544
001c8c33
PA
12545 bp_temp = b->disposition == disp_del;
12546 if (bl->address != bl->requested_address)
12547 breakpoint_adjustment_warning (bl->requested_address,
12548 bl->address,
12549 b->number, 1);
12550 annotate_breakpoint (b->number);
f303dbd6
PA
12551 maybe_print_thread_hit_breakpoint (uiout);
12552
112e8700 12553 if (uiout->is_mi_like_p ())
348d480f 12554 {
112e8700 12555 uiout->field_string ("reason",
001c8c33 12556 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12557 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12558 }
6a831f06
PA
12559 if (bp_temp)
12560 uiout->message ("Temporary breakpoint %pF, ",
12561 signed_field ("bkptno", b->number));
12562 else
12563 uiout->message ("Breakpoint %pF, ",
12564 signed_field ("bkptno", b->number));
06edf0c0 12565
001c8c33 12566 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12567}
12568
2060206e 12569static void
06edf0c0
PA
12570bkpt_print_mention (struct breakpoint *b)
12571{
112e8700 12572 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12573 return;
12574
12575 switch (b->type)
12576 {
12577 case bp_breakpoint:
12578 case bp_gnu_ifunc_resolver:
12579 if (b->disposition == disp_del)
12580 printf_filtered (_("Temporary breakpoint"));
12581 else
12582 printf_filtered (_("Breakpoint"));
12583 printf_filtered (_(" %d"), b->number);
12584 if (b->type == bp_gnu_ifunc_resolver)
12585 printf_filtered (_(" at gnu-indirect-function resolver"));
12586 break;
12587 case bp_hardware_breakpoint:
12588 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12589 break;
e7e0cddf
SS
12590 case bp_dprintf:
12591 printf_filtered (_("Dprintf %d"), b->number);
12592 break;
06edf0c0
PA
12593 }
12594
12595 say_where (b);
12596}
12597
2060206e 12598static void
06edf0c0
PA
12599bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12600{
12601 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12602 fprintf_unfiltered (fp, "tbreak");
12603 else if (tp->type == bp_breakpoint)
12604 fprintf_unfiltered (fp, "break");
12605 else if (tp->type == bp_hardware_breakpoint
12606 && tp->disposition == disp_del)
12607 fprintf_unfiltered (fp, "thbreak");
12608 else if (tp->type == bp_hardware_breakpoint)
12609 fprintf_unfiltered (fp, "hbreak");
12610 else
12611 internal_error (__FILE__, __LINE__,
12612 _("unhandled breakpoint type %d"), (int) tp->type);
12613
f00aae0f 12614 fprintf_unfiltered (fp, " %s",
d28cd78a 12615 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12616
12617 /* Print out extra_string if this breakpoint is pending. It might
12618 contain, for example, conditions that were set by the user. */
12619 if (tp->loc == NULL && tp->extra_string != NULL)
12620 fprintf_unfiltered (fp, " %s", tp->extra_string);
12621
dd11a36c 12622 print_recreate_thread (tp, fp);
06edf0c0
PA
12623}
12624
983af33b 12625static void
626d2320 12626bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12627 struct linespec_result *canonical,
12628 enum bptype type_wanted)
983af33b 12629{
f00aae0f 12630 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12631}
12632
12633static void
12634bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12635 struct linespec_result *canonical,
e1e01040
PA
12636 gdb::unique_xmalloc_ptr<char> cond_string,
12637 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12638 enum bptype type_wanted,
12639 enum bpdisp disposition,
12640 int thread,
12641 int task, int ignore_count,
12642 const struct breakpoint_ops *ops,
12643 int from_tty, int enabled,
44f238bb 12644 int internal, unsigned flags)
983af33b 12645{
023fa29b 12646 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12647 std::move (cond_string),
12648 std::move (extra_string),
e7e0cddf 12649 type_wanted,
983af33b
SDJ
12650 disposition, thread, task,
12651 ignore_count, ops, from_tty,
44f238bb 12652 enabled, internal, flags);
983af33b
SDJ
12653}
12654
6c5b2ebe 12655static std::vector<symtab_and_line>
f00aae0f 12656bkpt_decode_location (struct breakpoint *b,
626d2320 12657 struct event_location *location,
6c5b2ebe 12658 struct program_space *search_pspace)
983af33b 12659{
6c5b2ebe 12660 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12661}
12662
06edf0c0
PA
12663/* Virtual table for internal breakpoints. */
12664
12665static void
12666internal_bkpt_re_set (struct breakpoint *b)
12667{
12668 switch (b->type)
12669 {
12670 /* Delete overlay event and longjmp master breakpoints; they
12671 will be reset later by breakpoint_re_set. */
12672 case bp_overlay_event:
12673 case bp_longjmp_master:
12674 case bp_std_terminate_master:
12675 case bp_exception_master:
12676 delete_breakpoint (b);
12677 break;
12678
12679 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12680 starts and we really don't want to touch it. */
06edf0c0
PA
12681 case bp_shlib_event:
12682
12683 /* Like bp_shlib_event, this breakpoint type is special. Once
12684 it is set up, we do not want to touch it. */
12685 case bp_thread_event:
12686 break;
12687 }
12688}
12689
12690static void
12691internal_bkpt_check_status (bpstat bs)
12692{
a9b3a50f
PA
12693 if (bs->breakpoint_at->type == bp_shlib_event)
12694 {
12695 /* If requested, stop when the dynamic linker notifies GDB of
12696 events. This allows the user to get control and place
12697 breakpoints in initializer routines for dynamically loaded
12698 objects (among other things). */
12699 bs->stop = stop_on_solib_events;
12700 bs->print = stop_on_solib_events;
12701 }
12702 else
12703 bs->stop = 0;
06edf0c0
PA
12704}
12705
12706static enum print_stop_action
12707internal_bkpt_print_it (bpstat bs)
12708{
06edf0c0 12709 struct breakpoint *b;
06edf0c0 12710
06edf0c0
PA
12711 b = bs->breakpoint_at;
12712
06edf0c0
PA
12713 switch (b->type)
12714 {
348d480f
PA
12715 case bp_shlib_event:
12716 /* Did we stop because the user set the stop_on_solib_events
12717 variable? (If so, we report this as a generic, "Stopped due
12718 to shlib event" message.) */
edcc5120 12719 print_solib_event (0);
348d480f
PA
12720 break;
12721
12722 case bp_thread_event:
12723 /* Not sure how we will get here.
12724 GDB should not stop for these breakpoints. */
12725 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12726 break;
12727
12728 case bp_overlay_event:
12729 /* By analogy with the thread event, GDB should not stop for these. */
12730 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12731 break;
12732
12733 case bp_longjmp_master:
12734 /* These should never be enabled. */
12735 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12736 break;
12737
12738 case bp_std_terminate_master:
12739 /* These should never be enabled. */
12740 printf_filtered (_("std::terminate Master Breakpoint: "
12741 "gdb should not stop!\n"));
348d480f
PA
12742 break;
12743
12744 case bp_exception_master:
12745 /* These should never be enabled. */
12746 printf_filtered (_("Exception Master Breakpoint: "
12747 "gdb should not stop!\n"));
06edf0c0
PA
12748 break;
12749 }
12750
001c8c33 12751 return PRINT_NOTHING;
06edf0c0
PA
12752}
12753
12754static void
12755internal_bkpt_print_mention (struct breakpoint *b)
12756{
12757 /* Nothing to mention. These breakpoints are internal. */
12758}
12759
06edf0c0
PA
12760/* Virtual table for momentary breakpoints */
12761
12762static void
12763momentary_bkpt_re_set (struct breakpoint *b)
12764{
12765 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12766 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12767 Otherwise these should have been blown away via the cleanup chain
12768 or by breakpoint_init_inferior when we rerun the executable. */
12769}
12770
12771static void
12772momentary_bkpt_check_status (bpstat bs)
12773{
12774 /* Nothing. The point of these breakpoints is causing a stop. */
12775}
12776
12777static enum print_stop_action
12778momentary_bkpt_print_it (bpstat bs)
12779{
001c8c33 12780 return PRINT_UNKNOWN;
348d480f
PA
12781}
12782
06edf0c0
PA
12783static void
12784momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12785{
06edf0c0 12786 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12787}
12788
e2e4d78b
JK
12789/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12790
12791 It gets cleared already on the removal of the first one of such placed
12792 breakpoints. This is OK as they get all removed altogether. */
12793
c1fc2657 12794longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12795{
c1fc2657 12796 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12797
c1fc2657 12798 if (tp != NULL)
e2e4d78b 12799 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12800}
12801
55aa24fb
SDJ
12802/* Specific methods for probe breakpoints. */
12803
12804static int
12805bkpt_probe_insert_location (struct bp_location *bl)
12806{
12807 int v = bkpt_insert_location (bl);
12808
12809 if (v == 0)
12810 {
12811 /* The insertion was successful, now let's set the probe's semaphore
12812 if needed. */
935676c9 12813 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12814 }
12815
12816 return v;
12817}
12818
12819static int
73971819
PA
12820bkpt_probe_remove_location (struct bp_location *bl,
12821 enum remove_bp_reason reason)
55aa24fb
SDJ
12822{
12823 /* Let's clear the semaphore before removing the location. */
935676c9 12824 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12825
73971819 12826 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12827}
12828
12829static void
626d2320 12830bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12831 struct linespec_result *canonical,
f00aae0f 12832 enum bptype type_wanted)
55aa24fb
SDJ
12833{
12834 struct linespec_sals lsal;
12835
c2f4122d 12836 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12837 lsal.canonical
12838 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12839 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12840}
12841
6c5b2ebe 12842static std::vector<symtab_and_line>
f00aae0f 12843bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12844 struct event_location *location,
6c5b2ebe 12845 struct program_space *search_pspace)
55aa24fb 12846{
6c5b2ebe
PA
12847 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12848 if (sals.empty ())
55aa24fb 12849 error (_("probe not found"));
6c5b2ebe 12850 return sals;
55aa24fb
SDJ
12851}
12852
348d480f 12853/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12854
348d480f
PA
12855static void
12856tracepoint_re_set (struct breakpoint *b)
12857{
12858 breakpoint_re_set_default (b);
12859}
876fa593 12860
348d480f
PA
12861static int
12862tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12863 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12864 const struct target_waitstatus *ws)
348d480f
PA
12865{
12866 /* By definition, the inferior does not report stops at
12867 tracepoints. */
12868 return 0;
74960c60
VP
12869}
12870
12871static void
348d480f
PA
12872tracepoint_print_one_detail (const struct breakpoint *self,
12873 struct ui_out *uiout)
74960c60 12874{
d9b3f62e 12875 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12876 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12877 {
12878 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12879
6a831f06
PA
12880 uiout->message ("\tmarker id is %pF\n",
12881 string_field ("static-tracepoint-marker-string-id",
12882 tp->static_trace_marker_id.c_str ()));
348d480f 12883 }
0d381245
VP
12884}
12885
a474d7c2 12886static void
348d480f 12887tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12888{
112e8700 12889 if (current_uiout->is_mi_like_p ())
348d480f 12890 return;
cc59ec59 12891
348d480f
PA
12892 switch (b->type)
12893 {
12894 case bp_tracepoint:
12895 printf_filtered (_("Tracepoint"));
12896 printf_filtered (_(" %d"), b->number);
12897 break;
12898 case bp_fast_tracepoint:
12899 printf_filtered (_("Fast tracepoint"));
12900 printf_filtered (_(" %d"), b->number);
12901 break;
12902 case bp_static_tracepoint:
12903 printf_filtered (_("Static tracepoint"));
12904 printf_filtered (_(" %d"), b->number);
12905 break;
12906 default:
12907 internal_error (__FILE__, __LINE__,
12908 _("unhandled tracepoint type %d"), (int) b->type);
12909 }
12910
12911 say_where (b);
a474d7c2
PA
12912}
12913
348d480f 12914static void
d9b3f62e 12915tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12916{
d9b3f62e
PA
12917 struct tracepoint *tp = (struct tracepoint *) self;
12918
12919 if (self->type == bp_fast_tracepoint)
348d480f 12920 fprintf_unfiltered (fp, "ftrace");
c93e8391 12921 else if (self->type == bp_static_tracepoint)
348d480f 12922 fprintf_unfiltered (fp, "strace");
d9b3f62e 12923 else if (self->type == bp_tracepoint)
348d480f
PA
12924 fprintf_unfiltered (fp, "trace");
12925 else
12926 internal_error (__FILE__, __LINE__,
d9b3f62e 12927 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12928
f00aae0f 12929 fprintf_unfiltered (fp, " %s",
d28cd78a 12930 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12931 print_recreate_thread (self, fp);
12932
12933 if (tp->pass_count)
12934 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12935}
12936
983af33b 12937static void
626d2320 12938tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12939 struct linespec_result *canonical,
12940 enum bptype type_wanted)
983af33b 12941{
f00aae0f 12942 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12943}
12944
12945static void
12946tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12947 struct linespec_result *canonical,
e1e01040
PA
12948 gdb::unique_xmalloc_ptr<char> cond_string,
12949 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12950 enum bptype type_wanted,
12951 enum bpdisp disposition,
12952 int thread,
12953 int task, int ignore_count,
12954 const struct breakpoint_ops *ops,
12955 int from_tty, int enabled,
44f238bb 12956 int internal, unsigned flags)
983af33b 12957{
023fa29b 12958 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12959 std::move (cond_string),
12960 std::move (extra_string),
e7e0cddf 12961 type_wanted,
983af33b
SDJ
12962 disposition, thread, task,
12963 ignore_count, ops, from_tty,
44f238bb 12964 enabled, internal, flags);
983af33b
SDJ
12965}
12966
6c5b2ebe 12967static std::vector<symtab_and_line>
f00aae0f 12968tracepoint_decode_location (struct breakpoint *b,
626d2320 12969 struct event_location *location,
6c5b2ebe 12970 struct program_space *search_pspace)
983af33b 12971{
6c5b2ebe 12972 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12973}
12974
2060206e 12975struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12976
bac7c5cf 12977/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12978
12979static void
f00aae0f 12980tracepoint_probe_create_sals_from_location
626d2320 12981 (struct event_location *location,
f00aae0f
KS
12982 struct linespec_result *canonical,
12983 enum bptype type_wanted)
55aa24fb
SDJ
12984{
12985 /* We use the same method for breakpoint on probes. */
f00aae0f 12986 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12987}
12988
6c5b2ebe 12989static std::vector<symtab_and_line>
f00aae0f 12990tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 12991 struct event_location *location,
6c5b2ebe 12992 struct program_space *search_pspace)
55aa24fb
SDJ
12993{
12994 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12995 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12996}
12997
5c2b4418
HZ
12998/* Dprintf breakpoint_ops methods. */
12999
13000static void
13001dprintf_re_set (struct breakpoint *b)
13002{
13003 breakpoint_re_set_default (b);
13004
f00aae0f
KS
13005 /* extra_string should never be non-NULL for dprintf. */
13006 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13007
13008 /* 1 - connect to target 1, that can run breakpoint commands.
13009 2 - create a dprintf, which resolves fine.
13010 3 - disconnect from target 1
13011 4 - connect to target 2, that can NOT run breakpoint commands.
13012
13013 After steps #3/#4, you'll want the dprintf command list to
13014 be updated, because target 1 and 2 may well return different
13015 answers for target_can_run_breakpoint_commands().
13016 Given absence of finer grained resetting, we get to do
13017 it all the time. */
13018 if (b->extra_string != NULL)
13019 update_dprintf_command_list (b);
13020}
13021
2d9442cc
HZ
13022/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13023
13024static void
13025dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13026{
f00aae0f 13027 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13028 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13029 tp->extra_string);
13030 print_recreate_thread (tp, fp);
13031}
13032
9d6e6e84
HZ
13033/* Implement the "after_condition_true" breakpoint_ops method for
13034 dprintf.
13035
13036 dprintf's are implemented with regular commands in their command
13037 list, but we run the commands here instead of before presenting the
13038 stop to the user, as dprintf's don't actually cause a stop. This
13039 also makes it so that the commands of multiple dprintfs at the same
13040 address are all handled. */
13041
13042static void
13043dprintf_after_condition_true (struct bpstats *bs)
13044{
04afa70c 13045 struct bpstats tmp_bs;
9d6e6e84
HZ
13046 struct bpstats *tmp_bs_p = &tmp_bs;
13047
13048 /* dprintf's never cause a stop. This wasn't set in the
13049 check_status hook instead because that would make the dprintf's
13050 condition not be evaluated. */
13051 bs->stop = 0;
13052
13053 /* Run the command list here. Take ownership of it instead of
13054 copying. We never want these commands to run later in
13055 bpstat_do_actions, if a breakpoint that causes a stop happens to
13056 be set at same address as this dprintf, or even if running the
13057 commands here throws. */
13058 tmp_bs.commands = bs->commands;
13059 bs->commands = NULL;
9d6e6e84
HZ
13060
13061 bpstat_do_actions_1 (&tmp_bs_p);
13062
13063 /* 'tmp_bs.commands' will usually be NULL by now, but
13064 bpstat_do_actions_1 may return early without processing the whole
13065 list. */
9d6e6e84
HZ
13066}
13067
983af33b
SDJ
13068/* The breakpoint_ops structure to be used on static tracepoints with
13069 markers (`-m'). */
13070
13071static void
626d2320 13072strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13073 struct linespec_result *canonical,
f00aae0f 13074 enum bptype type_wanted)
983af33b
SDJ
13075{
13076 struct linespec_sals lsal;
f00aae0f 13077 const char *arg_start, *arg;
983af33b 13078
a20714ff 13079 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13080 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13081
f2fc3015
TT
13082 std::string str (arg_start, arg - arg_start);
13083 const char *ptr = str.c_str ();
a20714ff
PA
13084 canonical->location
13085 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13086
8e9e35b1
TT
13087 lsal.canonical
13088 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13089 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13090}
13091
13092static void
13093strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13094 struct linespec_result *canonical,
e1e01040
PA
13095 gdb::unique_xmalloc_ptr<char> cond_string,
13096 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13097 enum bptype type_wanted,
13098 enum bpdisp disposition,
13099 int thread,
13100 int task, int ignore_count,
13101 const struct breakpoint_ops *ops,
13102 int from_tty, int enabled,
44f238bb 13103 int internal, unsigned flags)
983af33b 13104{
6c5b2ebe 13105 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13106
13107 /* If the user is creating a static tracepoint by marker id
13108 (strace -m MARKER_ID), then store the sals index, so that
13109 breakpoint_re_set can try to match up which of the newly
13110 found markers corresponds to this one, and, don't try to
13111 expand multiple locations for each sal, given than SALS
13112 already should contain all sals for MARKER_ID. */
13113
6c5b2ebe 13114 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13115 {
6c5b2ebe
PA
13116 event_location_up location
13117 = copy_event_location (canonical->location.get ());
983af33b 13118
b270e6f9 13119 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13120 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13121 std::move (location), NULL,
e1e01040
PA
13122 std::move (cond_string),
13123 std::move (extra_string),
e7e0cddf 13124 type_wanted, disposition,
983af33b 13125 thread, task, ignore_count, ops,
44f238bb 13126 from_tty, enabled, internal, flags,
983af33b
SDJ
13127 canonical->special_display);
13128 /* Given that its possible to have multiple markers with
13129 the same string id, if the user is creating a static
13130 tracepoint by marker id ("strace -m MARKER_ID"), then
13131 store the sals index, so that breakpoint_re_set can
13132 try to match up which of the newly found markers
13133 corresponds to this one */
13134 tp->static_trace_marker_id_idx = i;
13135
b270e6f9 13136 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13137 }
13138}
13139
6c5b2ebe 13140static std::vector<symtab_and_line>
f00aae0f 13141strace_marker_decode_location (struct breakpoint *b,
626d2320 13142 struct event_location *location,
6c5b2ebe 13143 struct program_space *search_pspace)
983af33b
SDJ
13144{
13145 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13146 const char *s = get_linespec_location (location)->spec_string;
983af33b 13147
6c5b2ebe
PA
13148 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13149 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13150 {
6c5b2ebe
PA
13151 sals[0] = sals[tp->static_trace_marker_id_idx];
13152 sals.resize (1);
13153 return sals;
983af33b
SDJ
13154 }
13155 else
5d9310c4 13156 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13157}
13158
13159static struct breakpoint_ops strace_marker_breakpoint_ops;
13160
13161static int
13162strace_marker_p (struct breakpoint *b)
13163{
13164 return b->ops == &strace_marker_breakpoint_ops;
13165}
13166
53a5351d 13167/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13168 structures. */
c906108c
SS
13169
13170void
fba45db2 13171delete_breakpoint (struct breakpoint *bpt)
c906108c 13172{
8a3fe4f8 13173 gdb_assert (bpt != NULL);
c906108c 13174
4a64f543
MS
13175 /* Has this bp already been deleted? This can happen because
13176 multiple lists can hold pointers to bp's. bpstat lists are
13177 especial culprits.
13178
13179 One example of this happening is a watchpoint's scope bp. When
13180 the scope bp triggers, we notice that the watchpoint is out of
13181 scope, and delete it. We also delete its scope bp. But the
13182 scope bp is marked "auto-deleting", and is already on a bpstat.
13183 That bpstat is then checked for auto-deleting bp's, which are
13184 deleted.
13185
13186 A real solution to this problem might involve reference counts in
13187 bp's, and/or giving them pointers back to their referencing
13188 bpstat's, and teaching delete_breakpoint to only free a bp's
13189 storage when no more references were extent. A cheaper bandaid
13190 was chosen. */
c906108c
SS
13191 if (bpt->type == bp_none)
13192 return;
13193
4a64f543
MS
13194 /* At least avoid this stale reference until the reference counting
13195 of breakpoints gets resolved. */
d0fb5eae 13196 if (bpt->related_breakpoint != bpt)
e5a0a904 13197 {
d0fb5eae 13198 struct breakpoint *related;
3a5c3e22 13199 struct watchpoint *w;
d0fb5eae
JK
13200
13201 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13202 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13203 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13204 w = (struct watchpoint *) bpt;
13205 else
13206 w = NULL;
13207 if (w != NULL)
13208 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13209
13210 /* Unlink bpt from the bpt->related_breakpoint ring. */
13211 for (related = bpt; related->related_breakpoint != bpt;
13212 related = related->related_breakpoint);
13213 related->related_breakpoint = bpt->related_breakpoint;
13214 bpt->related_breakpoint = bpt;
e5a0a904
JK
13215 }
13216
a9634178
TJB
13217 /* watch_command_1 creates a watchpoint but only sets its number if
13218 update_watchpoint succeeds in creating its bp_locations. If there's
13219 a problem in that process, we'll be asked to delete the half-created
13220 watchpoint. In that case, don't announce the deletion. */
13221 if (bpt->number)
76727919 13222 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13223
c906108c
SS
13224 if (breakpoint_chain == bpt)
13225 breakpoint_chain = bpt->next;
13226
43892fdf 13227 for (breakpoint *b : all_breakpoints ())
c906108c 13228 if (b->next == bpt)
01add95b
SM
13229 {
13230 b->next = bpt->next;
13231 break;
13232 }
c906108c 13233
f431efe5
PA
13234 /* Be sure no bpstat's are pointing at the breakpoint after it's
13235 been freed. */
13236 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13237 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13238 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13239 commands are associated with the bpstat; if we remove it here,
13240 then the later call to bpstat_do_actions (&stop_bpstat); in
13241 event-top.c won't do anything, and temporary breakpoints with
13242 commands won't work. */
13243
13244 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13245
4a64f543
MS
13246 /* Now that breakpoint is removed from breakpoint list, update the
13247 global location list. This will remove locations that used to
13248 belong to this breakpoint. Do this before freeing the breakpoint
13249 itself, since remove_breakpoint looks at location's owner. It
13250 might be better design to have location completely
13251 self-contained, but it's not the case now. */
44702360 13252 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13253
4a64f543
MS
13254 /* On the chance that someone will soon try again to delete this
13255 same bp, we mark it as deleted before freeing its storage. */
c906108c 13256 bpt->type = bp_none;
4d01a485 13257 delete bpt;
c906108c
SS
13258}
13259
51be5b68
PA
13260/* Iterator function to call a user-provided callback function once
13261 for each of B and its related breakpoints. */
13262
13263static void
13264iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13265 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13266{
13267 struct breakpoint *related;
13268
13269 related = b;
13270 do
13271 {
13272 struct breakpoint *next;
13273
13274 /* FUNCTION may delete RELATED. */
13275 next = related->related_breakpoint;
13276
13277 if (next == related)
13278 {
13279 /* RELATED is the last ring entry. */
48649e1b 13280 function (related);
51be5b68
PA
13281
13282 /* FUNCTION may have deleted it, so we'd never reach back to
13283 B. There's nothing left to do anyway, so just break
13284 out. */
13285 break;
13286 }
13287 else
48649e1b 13288 function (related);
51be5b68
PA
13289
13290 related = next;
13291 }
13292 while (related != b);
13293}
95a42b64 13294
4495129a 13295static void
981a3fb3 13296delete_command (const char *arg, int from_tty)
c906108c 13297{
ea9365bb
TT
13298 dont_repeat ();
13299
c906108c
SS
13300 if (arg == 0)
13301 {
13302 int breaks_to_delete = 0;
13303
46c6471b 13304 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13305 internal breakpoints, these have to be deleted with an
13306 explicit breakpoint number argument. */
43892fdf 13307 for (breakpoint *b : all_breakpoints ())
46c6471b 13308 if (user_breakpoint_p (b))
973d738b
DJ
13309 {
13310 breaks_to_delete = 1;
13311 break;
13312 }
c906108c
SS
13313
13314 /* Ask user only if there are some breakpoints to delete. */
13315 if (!from_tty
e2e0b3e5 13316 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
13317 for (breakpoint *b : all_breakpoints_safe ())
13318 if (user_breakpoint_p (b))
13319 delete_breakpoint (b);
c906108c
SS
13320 }
13321 else
48649e1b 13322 map_breakpoint_numbers
b926417a 13323 (arg, [&] (breakpoint *br)
48649e1b 13324 {
b926417a 13325 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13326 });
c906108c
SS
13327}
13328
c2f4122d
PA
13329/* Return true if all locations of B bound to PSPACE are pending. If
13330 PSPACE is NULL, all locations of all program spaces are
13331 considered. */
13332
0d381245 13333static int
c2f4122d 13334all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13335{
40cb8ca5 13336 for (bp_location *loc : b->locations ())
c2f4122d
PA
13337 if ((pspace == NULL
13338 || loc->pspace == pspace)
13339 && !loc->shlib_disabled
8645ff69 13340 && !loc->pspace->executing_startup)
0d381245
VP
13341 return 0;
13342 return 1;
fe3f5fa8
VP
13343}
13344
776592bf
DE
13345/* Subroutine of update_breakpoint_locations to simplify it.
13346 Return non-zero if multiple fns in list LOC have the same name.
13347 Null names are ignored. */
13348
13349static int
13350ambiguous_names_p (struct bp_location *loc)
13351{
13352 struct bp_location *l;
2698f5ea 13353 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 13354 xcalloc, xfree));
776592bf
DE
13355
13356 for (l = loc; l != NULL; l = l->next)
13357 {
13358 const char **slot;
13359 const char *name = l->function_name;
13360
13361 /* Allow for some names to be NULL, ignore them. */
13362 if (name == NULL)
13363 continue;
13364
c1fb9836 13365 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13366 INSERT);
4a64f543
MS
13367 /* NOTE: We can assume slot != NULL here because xcalloc never
13368 returns NULL. */
776592bf 13369 if (*slot != NULL)
c1fb9836 13370 return 1;
776592bf
DE
13371 *slot = name;
13372 }
13373
776592bf
DE
13374 return 0;
13375}
13376
0fb4aa4b
PA
13377/* When symbols change, it probably means the sources changed as well,
13378 and it might mean the static tracepoint markers are no longer at
13379 the same address or line numbers they used to be at last we
13380 checked. Losing your static tracepoints whenever you rebuild is
13381 undesirable. This function tries to resync/rematch gdb static
13382 tracepoints with the markers on the target, for static tracepoints
13383 that have not been set by marker id. Static tracepoint that have
13384 been set by marker id are reset by marker id in breakpoint_re_set.
13385 The heuristic is:
13386
13387 1) For a tracepoint set at a specific address, look for a marker at
13388 the old PC. If one is found there, assume to be the same marker.
13389 If the name / string id of the marker found is different from the
13390 previous known name, assume that means the user renamed the marker
13391 in the sources, and output a warning.
13392
13393 2) For a tracepoint set at a given line number, look for a marker
13394 at the new address of the old line number. If one is found there,
13395 assume to be the same marker. If the name / string id of the
13396 marker found is different from the previous known name, assume that
13397 means the user renamed the marker in the sources, and output a
13398 warning.
13399
13400 3) If a marker is no longer found at the same address or line, it
13401 may mean the marker no longer exists. But it may also just mean
13402 the code changed a bit. Maybe the user added a few lines of code
13403 that made the marker move up or down (in line number terms). Ask
13404 the target for info about the marker with the string id as we knew
13405 it. If found, update line number and address in the matching
13406 static tracepoint. This will get confused if there's more than one
13407 marker with the same ID (possible in UST, although unadvised
13408 precisely because it confuses tools). */
13409
13410static struct symtab_and_line
13411update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13412{
d9b3f62e 13413 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13414 struct static_tracepoint_marker marker;
13415 CORE_ADDR pc;
0fb4aa4b
PA
13416
13417 pc = sal.pc;
13418 if (sal.line)
13419 find_line_pc (sal.symtab, sal.line, &pc);
13420
13421 if (target_static_tracepoint_marker_at (pc, &marker))
13422 {
5d9310c4 13423 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13424 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13425 b->number, tp->static_trace_marker_id.c_str (),
13426 marker.str_id.c_str ());
0fb4aa4b 13427
5d9310c4 13428 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13429
13430 return sal;
13431 }
13432
13433 /* Old marker wasn't found on target at lineno. Try looking it up
13434 by string ID. */
13435 if (!sal.explicit_pc
13436 && sal.line != 0
13437 && sal.symtab != NULL
5d9310c4 13438 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13439 {
5d9310c4
SM
13440 std::vector<static_tracepoint_marker> markers
13441 = target_static_tracepoint_markers_by_strid
13442 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13443
5d9310c4 13444 if (!markers.empty ())
0fb4aa4b 13445 {
0fb4aa4b 13446 struct symbol *sym;
80e1d417 13447 struct static_tracepoint_marker *tpmarker;
79a45e25 13448 struct ui_out *uiout = current_uiout;
67994074 13449 struct explicit_location explicit_loc;
0fb4aa4b 13450
5d9310c4 13451 tpmarker = &markers[0];
0fb4aa4b 13452
5d9310c4 13453 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13454
13455 warning (_("marker for static tracepoint %d (%s) not "
13456 "found at previous line number"),
5d9310c4 13457 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13458
51abb421 13459 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13460 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13461 uiout->text ("Now in ");
0fb4aa4b
PA
13462 if (sym)
13463 {
987012b8 13464 uiout->field_string ("func", sym->print_name (),
e43b10e1 13465 function_name_style.style ());
112e8700 13466 uiout->text (" at ");
0fb4aa4b 13467 }
112e8700 13468 uiout->field_string ("file",
cbe56571 13469 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13470 file_name_style.style ());
112e8700 13471 uiout->text (":");
0fb4aa4b 13472
112e8700 13473 if (uiout->is_mi_like_p ())
0fb4aa4b 13474 {
0b0865da 13475 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13476
112e8700 13477 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13478 }
13479
381befee 13480 uiout->field_signed ("line", sal2.line);
112e8700 13481 uiout->text ("\n");
0fb4aa4b 13482
80e1d417 13483 b->loc->line_number = sal2.line;
2f202fde 13484 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13485
d28cd78a 13486 b->location.reset (NULL);
67994074
KS
13487 initialize_explicit_location (&explicit_loc);
13488 explicit_loc.source_filename
00e52e53 13489 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13490 explicit_loc.line_offset.offset = b->loc->line_number;
13491 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13492 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13493
13494 /* Might be nice to check if function changed, and warn if
13495 so. */
0fb4aa4b
PA
13496 }
13497 }
13498 return sal;
13499}
13500
8d3788bd
VP
13501/* Returns 1 iff locations A and B are sufficiently same that
13502 we don't need to report breakpoint as changed. */
13503
13504static int
13505locations_are_equal (struct bp_location *a, struct bp_location *b)
13506{
13507 while (a && b)
13508 {
13509 if (a->address != b->address)
13510 return 0;
13511
13512 if (a->shlib_disabled != b->shlib_disabled)
13513 return 0;
13514
13515 if (a->enabled != b->enabled)
13516 return 0;
13517
b5fa468f
TBA
13518 if (a->disabled_by_cond != b->disabled_by_cond)
13519 return 0;
13520
8d3788bd
VP
13521 a = a->next;
13522 b = b->next;
13523 }
13524
13525 if ((a == NULL) != (b == NULL))
13526 return 0;
13527
13528 return 1;
13529}
13530
c2f4122d
PA
13531/* Split all locations of B that are bound to PSPACE out of B's
13532 location list to a separate list and return that list's head. If
13533 PSPACE is NULL, hoist out all locations of B. */
13534
13535static struct bp_location *
13536hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13537{
13538 struct bp_location head;
13539 struct bp_location *i = b->loc;
13540 struct bp_location **i_link = &b->loc;
13541 struct bp_location *hoisted = &head;
13542
13543 if (pspace == NULL)
13544 {
13545 i = b->loc;
13546 b->loc = NULL;
13547 return i;
13548 }
13549
13550 head.next = NULL;
13551
13552 while (i != NULL)
13553 {
13554 if (i->pspace == pspace)
13555 {
13556 *i_link = i->next;
13557 i->next = NULL;
13558 hoisted->next = i;
13559 hoisted = i;
13560 }
13561 else
13562 i_link = &i->next;
13563 i = *i_link;
13564 }
13565
13566 return head.next;
13567}
13568
13569/* Create new breakpoint locations for B (a hardware or software
13570 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13571 zero, then B is a ranged breakpoint. Only recreates locations for
13572 FILTER_PSPACE. Locations of other program spaces are left
13573 untouched. */
f1310107 13574
0e30163f 13575void
0d381245 13576update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13577 struct program_space *filter_pspace,
6c5b2ebe
PA
13578 gdb::array_view<const symtab_and_line> sals,
13579 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13580{
c2f4122d 13581 struct bp_location *existing_locations;
0d381245 13582
6c5b2ebe 13583 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13584 {
13585 /* Ranged breakpoints have only one start location and one end
13586 location. */
13587 b->enable_state = bp_disabled;
f8eba3c6
TT
13588 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13589 "multiple locations found\n"),
13590 b->number);
13591 return;
13592 }
f1310107 13593
4a64f543
MS
13594 /* If there's no new locations, and all existing locations are
13595 pending, don't do anything. This optimizes the common case where
13596 all locations are in the same shared library, that was unloaded.
13597 We'd like to retain the location, so that when the library is
13598 loaded again, we don't loose the enabled/disabled status of the
13599 individual locations. */
6c5b2ebe 13600 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13601 return;
13602
c2f4122d 13603 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13604
6c5b2ebe 13605 for (const auto &sal : sals)
fe3f5fa8 13606 {
f8eba3c6
TT
13607 struct bp_location *new_loc;
13608
6c5b2ebe 13609 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13610
6c5b2ebe 13611 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13612
0d381245
VP
13613 /* Reparse conditions, they might contain references to the
13614 old symtab. */
13615 if (b->cond_string != NULL)
13616 {
bbc13ae3 13617 const char *s;
fe3f5fa8 13618
0d381245 13619 s = b->cond_string;
a70b8144 13620 try
0d381245 13621 {
6c5b2ebe
PA
13622 new_loc->cond = parse_exp_1 (&s, sal.pc,
13623 block_for_pc (sal.pc),
0d381245
VP
13624 0);
13625 }
230d2906 13626 catch (const gdb_exception_error &e)
0d381245 13627 {
b5fa468f 13628 new_loc->disabled_by_cond = true;
0d381245
VP
13629 }
13630 }
fe3f5fa8 13631
6c5b2ebe 13632 if (!sals_end.empty ())
f1310107 13633 {
6c5b2ebe 13634 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13635
6c5b2ebe 13636 new_loc->length = end - sals[0].pc + 1;
f1310107 13637 }
0d381245 13638 }
fe3f5fa8 13639
4a64f543
MS
13640 /* If possible, carry over 'disable' status from existing
13641 breakpoints. */
0d381245
VP
13642 {
13643 struct bp_location *e = existing_locations;
776592bf
DE
13644 /* If there are multiple breakpoints with the same function name,
13645 e.g. for inline functions, comparing function names won't work.
13646 Instead compare pc addresses; this is just a heuristic as things
13647 may have moved, but in practice it gives the correct answer
13648 often enough until a better solution is found. */
13649 int have_ambiguous_names = ambiguous_names_p (b->loc);
13650
0d381245
VP
13651 for (; e; e = e->next)
13652 {
b5fa468f 13653 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 13654 {
776592bf
DE
13655 if (have_ambiguous_names)
13656 {
40cb8ca5 13657 for (bp_location *l : b->locations ())
7f32a4d5
PA
13658 {
13659 /* Ignore software vs hardware location type at
13660 this point, because with "set breakpoint
13661 auto-hw", after a re-set, locations that were
13662 hardware can end up as software, or vice versa.
13663 As mentioned above, this is an heuristic and in
13664 practice should give the correct answer often
13665 enough. */
13666 if (breakpoint_locations_match (e, l, true))
13667 {
b5fa468f
TBA
13668 l->enabled = e->enabled;
13669 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13670 break;
13671 }
13672 }
776592bf
DE
13673 }
13674 else
13675 {
40cb8ca5 13676 for (bp_location *l : b->locations ())
776592bf
DE
13677 if (l->function_name
13678 && strcmp (e->function_name, l->function_name) == 0)
13679 {
b5fa468f
TBA
13680 l->enabled = e->enabled;
13681 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13682 break;
13683 }
13684 }
0d381245
VP
13685 }
13686 }
13687 }
fe3f5fa8 13688
8d3788bd 13689 if (!locations_are_equal (existing_locations, b->loc))
76727919 13690 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13691}
13692
f00aae0f 13693/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13694 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13695
6c5b2ebe 13696static std::vector<symtab_and_line>
f00aae0f 13697location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13698 struct program_space *search_pspace, int *found)
ef23e705 13699{
cc06b668 13700 struct gdb_exception exception;
ef23e705 13701
983af33b 13702 gdb_assert (b->ops != NULL);
ef23e705 13703
6c5b2ebe
PA
13704 std::vector<symtab_and_line> sals;
13705
a70b8144 13706 try
ef23e705 13707 {
6c5b2ebe 13708 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13709 }
94aeb44b 13710 catch (gdb_exception_error &e)
ef23e705
TJB
13711 {
13712 int not_found_and_ok = 0;
492d29ea 13713
ef23e705
TJB
13714 /* For pending breakpoints, it's expected that parsing will
13715 fail until the right shared library is loaded. User has
13716 already told to create pending breakpoints and don't need
13717 extra messages. If breakpoint is in bp_shlib_disabled
13718 state, then user already saw the message about that
13719 breakpoint being disabled, and don't want to see more
13720 errors. */
58438ac1 13721 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13722 && (b->condition_not_parsed
13723 || (b->loc != NULL
13724 && search_pspace != NULL
13725 && b->loc->pspace != search_pspace)
ef23e705 13726 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13727 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13728 || b->enable_state == bp_disabled))
13729 not_found_and_ok = 1;
13730
13731 if (!not_found_and_ok)
13732 {
13733 /* We surely don't want to warn about the same breakpoint
13734 10 times. One solution, implemented here, is disable
13735 the breakpoint on error. Another solution would be to
13736 have separate 'warning emitted' flag. Since this
13737 happens only when a binary has changed, I don't know
13738 which approach is better. */
13739 b->enable_state = bp_disabled;
eedc3f4f 13740 throw;
ef23e705 13741 }
94aeb44b
TT
13742
13743 exception = std::move (e);
ef23e705
TJB
13744 }
13745
492d29ea 13746 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13747 {
6c5b2ebe
PA
13748 for (auto &sal : sals)
13749 resolve_sal_pc (&sal);
f00aae0f 13750 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13751 {
ed1d1739
KS
13752 char *cond_string, *extra_string;
13753 int thread, task;
ef23e705 13754
b5fa468f
TBA
13755 find_condition_and_thread_for_sals (sals, b->extra_string,
13756 &cond_string, &thread,
13757 &task, &extra_string);
f00aae0f 13758 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13759 if (cond_string)
13760 b->cond_string = cond_string;
13761 b->thread = thread;
13762 b->task = task;
e7e0cddf 13763 if (extra_string)
f00aae0f
KS
13764 {
13765 xfree (b->extra_string);
13766 b->extra_string = extra_string;
13767 }
ef23e705
TJB
13768 b->condition_not_parsed = 0;
13769 }
13770
983af33b 13771 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13772 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13773
58438ac1
TT
13774 *found = 1;
13775 }
13776 else
13777 *found = 0;
ef23e705
TJB
13778
13779 return sals;
13780}
13781
348d480f
PA
13782/* The default re_set method, for typical hardware or software
13783 breakpoints. Reevaluate the breakpoint and recreate its
13784 locations. */
13785
13786static void
28010a5d 13787breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13788{
c2f4122d 13789 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13790 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13791
6c5b2ebe
PA
13792 int found;
13793 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13794 filter_pspace, &found);
ef23e705 13795 if (found)
6c5b2ebe 13796 expanded = std::move (sals);
ef23e705 13797
f00aae0f 13798 if (b->location_range_end != NULL)
f1310107 13799 {
6c5b2ebe
PA
13800 std::vector<symtab_and_line> sals_end
13801 = location_to_sals (b, b->location_range_end.get (),
13802 filter_pspace, &found);
f1310107 13803 if (found)
6c5b2ebe 13804 expanded_end = std::move (sals_end);
f1310107
TJB
13805 }
13806
c2f4122d 13807 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13808}
13809
983af33b
SDJ
13810/* Default method for creating SALs from an address string. It basically
13811 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13812
13813static void
626d2320 13814create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13815 struct linespec_result *canonical,
13816 enum bptype type_wanted)
983af33b 13817{
f00aae0f 13818 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13819}
13820
13821/* Call create_breakpoints_sal for the given arguments. This is the default
13822 function for the `create_breakpoints_sal' method of
13823 breakpoint_ops. */
13824
13825static void
13826create_breakpoints_sal_default (struct gdbarch *gdbarch,
13827 struct linespec_result *canonical,
e1e01040
PA
13828 gdb::unique_xmalloc_ptr<char> cond_string,
13829 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13830 enum bptype type_wanted,
13831 enum bpdisp disposition,
13832 int thread,
13833 int task, int ignore_count,
13834 const struct breakpoint_ops *ops,
13835 int from_tty, int enabled,
44f238bb 13836 int internal, unsigned flags)
983af33b 13837{
e1e01040
PA
13838 create_breakpoints_sal (gdbarch, canonical,
13839 std::move (cond_string),
13840 std::move (extra_string),
983af33b
SDJ
13841 type_wanted, disposition,
13842 thread, task, ignore_count, ops, from_tty,
44f238bb 13843 enabled, internal, flags);
983af33b
SDJ
13844}
13845
13846/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13847 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13848
6c5b2ebe 13849static std::vector<symtab_and_line>
f00aae0f 13850decode_location_default (struct breakpoint *b,
626d2320 13851 struct event_location *location,
6c5b2ebe 13852 struct program_space *search_pspace)
983af33b
SDJ
13853{
13854 struct linespec_result canonical;
13855
c2f4122d 13856 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13857 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13858 b->filter.get ());
983af33b
SDJ
13859
13860 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13861 gdb_assert (canonical.lsals.size () < 2);
983af33b 13862
6c5b2ebe 13863 if (!canonical.lsals.empty ())
983af33b 13864 {
6c5b2ebe
PA
13865 const linespec_sals &lsal = canonical.lsals[0];
13866 return std::move (lsal.sals);
983af33b 13867 }
6c5b2ebe 13868 return {};
983af33b
SDJ
13869}
13870
bf469271 13871/* Reset a breakpoint. */
c906108c 13872
bf469271
PA
13873static void
13874breakpoint_re_set_one (breakpoint *b)
c906108c 13875{
fdf44873
TT
13876 input_radix = b->input_radix;
13877 set_language (b->language);
c906108c 13878
348d480f 13879 b->ops->re_set (b);
c906108c
SS
13880}
13881
c2f4122d
PA
13882/* Re-set breakpoint locations for the current program space.
13883 Locations bound to other program spaces are left untouched. */
13884
c906108c 13885void
69de3c6a 13886breakpoint_re_set (void)
c906108c 13887{
c5aa993b 13888 {
fdf44873
TT
13889 scoped_restore_current_language save_language;
13890 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13891 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13892
8e817061
JB
13893 /* breakpoint_re_set_one sets the current_language to the language
13894 of the breakpoint it is resetting (see prepare_re_set_context)
13895 before re-evaluating the breakpoint's location. This change can
13896 unfortunately get undone by accident if the language_mode is set
13897 to auto, and we either switch frames, or more likely in this context,
13898 we select the current frame.
13899
13900 We prevent this by temporarily turning the language_mode to
13901 language_mode_manual. We restore it once all breakpoints
13902 have been reset. */
13903 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13904 language_mode = language_mode_manual;
13905
5ed8105e
PA
13906 /* Note: we must not try to insert locations until after all
13907 breakpoints have been re-set. Otherwise, e.g., when re-setting
13908 breakpoint 1, we'd insert the locations of breakpoint 2, which
13909 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13910
1428b37a 13911 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 13912 {
a70b8144 13913 try
bf469271
PA
13914 {
13915 breakpoint_re_set_one (b);
13916 }
230d2906 13917 catch (const gdb_exception &ex)
bf469271
PA
13918 {
13919 exception_fprintf (gdb_stderr, ex,
13920 "Error in re-setting breakpoint %d: ",
13921 b->number);
13922 }
5ed8105e 13923 }
5ed8105e
PA
13924
13925 jit_breakpoint_re_set ();
13926 }
6c95b8df 13927
af02033e
PP
13928 create_overlay_event_breakpoint ();
13929 create_longjmp_master_breakpoint ();
13930 create_std_terminate_master_breakpoint ();
186c406b 13931 create_exception_master_breakpoint ();
2a7f3dff
PA
13932
13933 /* Now we can insert. */
13934 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13935}
13936\f
c906108c
SS
13937/* Reset the thread number of this breakpoint:
13938
13939 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13940 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13941void
fba45db2 13942breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13943{
13944 if (b->thread != -1)
13945 {
00431a78 13946 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13947
13948 /* We're being called after following a fork. The new fork is
13949 selected as current, and unless this was a vfork will have a
13950 different program space from the original thread. Reset that
13951 as well. */
13952 b->loc->pspace = current_program_space;
c906108c
SS
13953 }
13954}
13955
03ac34d5
MS
13956/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13957 If from_tty is nonzero, it prints a message to that effect,
13958 which ends with a period (no newline). */
13959
c906108c 13960void
fba45db2 13961set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13962{
c906108c
SS
13963 if (count < 0)
13964 count = 0;
13965
43892fdf 13966 for (breakpoint *b : all_breakpoints ())
c906108c 13967 if (b->number == bptnum)
01add95b
SM
13968 {
13969 if (is_tracepoint (b))
13970 {
13971 if (from_tty && count != 0)
13972 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13973 bptnum);
13974 return;
13975 }
13976
13977 b->ignore_count = count;
13978 if (from_tty)
13979 {
13980 if (count == 0)
13981 printf_filtered (_("Will stop next time "
13982 "breakpoint %d is reached."),
13983 bptnum);
13984 else if (count == 1)
13985 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13986 bptnum);
13987 else
13988 printf_filtered (_("Will ignore next %d "
13989 "crossings of breakpoint %d."),
13990 count, bptnum);
13991 }
13992 gdb::observers::breakpoint_modified.notify (b);
13993 return;
13994 }
c906108c 13995
8a3fe4f8 13996 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13997}
13998
c906108c
SS
13999/* Command to set ignore-count of breakpoint N to COUNT. */
14000
14001static void
0b39b52e 14002ignore_command (const char *args, int from_tty)
c906108c 14003{
0b39b52e 14004 const char *p = args;
52f0bd74 14005 int num;
c906108c
SS
14006
14007 if (p == 0)
e2e0b3e5 14008 error_no_arg (_("a breakpoint number"));
c5aa993b 14009
c906108c 14010 num = get_number (&p);
5c44784c 14011 if (num == 0)
8a3fe4f8 14012 error (_("bad breakpoint number: '%s'"), args);
c906108c 14013 if (*p == 0)
8a3fe4f8 14014 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14015
14016 set_ignore_count (num,
14017 longest_to_int (value_as_long (parse_and_eval (p))),
14018 from_tty);
221ea385
KS
14019 if (from_tty)
14020 printf_filtered ("\n");
c906108c
SS
14021}
14022\f
d0fe4701
XR
14023
14024/* Call FUNCTION on each of the breakpoints with numbers in the range
14025 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14026
14027static void
d0fe4701
XR
14028map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14029 gdb::function_view<void (breakpoint *)> function)
c906108c 14030{
d0fe4701
XR
14031 if (bp_num_range.first == 0)
14032 {
14033 warning (_("bad breakpoint number at or near '%d'"),
14034 bp_num_range.first);
14035 }
14036 else
c906108c 14037 {
d0fe4701 14038 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14039 {
d0fe4701
XR
14040 bool match = false;
14041
1428b37a 14042 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 14043 if (b->number == i)
5c44784c 14044 {
bfd28288 14045 match = true;
48649e1b 14046 function (b);
11cf8741 14047 break;
5c44784c 14048 }
bfd28288 14049 if (!match)
d0fe4701 14050 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14051 }
c906108c
SS
14052 }
14053}
14054
d0fe4701
XR
14055/* Call FUNCTION on each of the breakpoints whose numbers are given in
14056 ARGS. */
14057
14058static void
14059map_breakpoint_numbers (const char *args,
14060 gdb::function_view<void (breakpoint *)> function)
14061{
14062 if (args == NULL || *args == '\0')
14063 error_no_arg (_("one or more breakpoint numbers"));
14064
14065 number_or_range_parser parser (args);
14066
14067 while (!parser.finished ())
14068 {
14069 int num = parser.get_number ();
14070 map_breakpoint_number_range (std::make_pair (num, num), function);
14071 }
14072}
14073
14074/* Return the breakpoint location structure corresponding to the
14075 BP_NUM and LOC_NUM values. */
14076
0d381245 14077static struct bp_location *
d0fe4701 14078find_location_by_number (int bp_num, int loc_num)
0d381245 14079{
43892fdf 14080 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
14081
14082 if (!b || b->number != bp_num)
d0fe4701 14083 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14084
0d381245 14085 if (loc_num == 0)
d0fe4701 14086 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14087
d0fe4701 14088 int n = 0;
40cb8ca5 14089 for (bp_location *loc : b->locations ())
d0fe4701
XR
14090 if (++n == loc_num)
14091 return loc;
14092
14093 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14094}
14095
95e95a6d
PA
14096/* Modes of operation for extract_bp_num. */
14097enum class extract_bp_kind
14098{
14099 /* Extracting a breakpoint number. */
14100 bp,
14101
14102 /* Extracting a location number. */
14103 loc,
14104};
14105
14106/* Extract a breakpoint or location number (as determined by KIND)
14107 from the string starting at START. TRAILER is a character which
14108 can be found after the number. If you don't want a trailer, use
14109 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14110 string. This always returns a positive integer. */
14111
14112static int
14113extract_bp_num (extract_bp_kind kind, const char *start,
14114 int trailer, const char **end_out = NULL)
14115{
14116 const char *end = start;
14117 int num = get_number_trailer (&end, trailer);
14118 if (num < 0)
14119 error (kind == extract_bp_kind::bp
14120 ? _("Negative breakpoint number '%.*s'")
14121 : _("Negative breakpoint location number '%.*s'"),
14122 int (end - start), start);
14123 if (num == 0)
14124 error (kind == extract_bp_kind::bp
14125 ? _("Bad breakpoint number '%.*s'")
14126 : _("Bad breakpoint location number '%.*s'"),
14127 int (end - start), start);
14128
14129 if (end_out != NULL)
14130 *end_out = end;
14131 return num;
14132}
14133
14134/* Extract a breakpoint or location range (as determined by KIND) in
14135 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14136 representing the (inclusive) range. The returned pair's elements
14137 are always positive integers. */
14138
14139static std::pair<int, int>
14140extract_bp_or_bp_range (extract_bp_kind kind,
14141 const std::string &arg,
14142 std::string::size_type arg_offset)
14143{
14144 std::pair<int, int> range;
14145 const char *bp_loc = &arg[arg_offset];
14146 std::string::size_type dash = arg.find ('-', arg_offset);
14147 if (dash != std::string::npos)
14148 {
14149 /* bp_loc is a range (x-z). */
14150 if (arg.length () == dash + 1)
14151 error (kind == extract_bp_kind::bp
14152 ? _("Bad breakpoint number at or near: '%s'")
14153 : _("Bad breakpoint location number at or near: '%s'"),
14154 bp_loc);
14155
14156 const char *end;
14157 const char *start_first = bp_loc;
14158 const char *start_second = &arg[dash + 1];
14159 range.first = extract_bp_num (kind, start_first, '-');
14160 range.second = extract_bp_num (kind, start_second, '\0', &end);
14161
14162 if (range.first > range.second)
14163 error (kind == extract_bp_kind::bp
14164 ? _("Inverted breakpoint range at '%.*s'")
14165 : _("Inverted breakpoint location range at '%.*s'"),
14166 int (end - start_first), start_first);
14167 }
14168 else
14169 {
14170 /* bp_loc is a single value. */
14171 range.first = extract_bp_num (kind, bp_loc, '\0');
14172 range.second = range.first;
14173 }
14174 return range;
14175}
14176
d0fe4701
XR
14177/* Extract the breakpoint/location range specified by ARG. Returns
14178 the breakpoint range in BP_NUM_RANGE, and the location range in
14179 BP_LOC_RANGE.
14180
14181 ARG may be in any of the following forms:
14182
14183 x where 'x' is a breakpoint number.
14184 x-y where 'x' and 'y' specify a breakpoint numbers range.
14185 x.y where 'x' is a breakpoint number and 'y' a location number.
14186 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14187 location number range.
14188*/
14189
cc638e86 14190static void
d0fe4701
XR
14191extract_bp_number_and_location (const std::string &arg,
14192 std::pair<int, int> &bp_num_range,
14193 std::pair<int, int> &bp_loc_range)
14194{
14195 std::string::size_type dot = arg.find ('.');
14196
14197 if (dot != std::string::npos)
14198 {
14199 /* Handle 'x.y' and 'x.y-z' cases. */
14200
14201 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14202 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14203
95e95a6d
PA
14204 bp_num_range.first
14205 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14206 bp_num_range.second = bp_num_range.first;
d0fe4701 14207
95e95a6d
PA
14208 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14209 arg, dot + 1);
d0fe4701
XR
14210 }
14211 else
14212 {
14213 /* Handle x and x-y cases. */
d0fe4701 14214
95e95a6d 14215 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14216 bp_loc_range.first = 0;
14217 bp_loc_range.second = 0;
14218 }
d0fe4701
XR
14219}
14220
14221/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14222 specifies whether to enable or disable. */
14223
14224static void
14225enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14226{
14227 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14228 if (loc != NULL)
14229 {
b5fa468f
TBA
14230 if (loc->disabled_by_cond && enable)
14231 error (_("Breakpoint %d's condition is invalid at location %d, "
14232 "cannot enable."), bp_num, loc_num);
14233
d0fe4701
XR
14234 if (loc->enabled != enable)
14235 {
14236 loc->enabled = enable;
14237 mark_breakpoint_location_modified (loc);
14238 }
14239 if (target_supports_enable_disable_tracepoint ()
14240 && current_trace_status ()->running && loc->owner
14241 && is_tracepoint (loc->owner))
14242 target_disable_tracepoint (loc);
14243 }
14244 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14245
14246 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14247}
14248
14249/* Enable or disable a range of breakpoint locations. BP_NUM is the
14250 number of the breakpoint, and BP_LOC_RANGE specifies the
14251 (inclusive) range of location numbers of that breakpoint to
14252 enable/disable. ENABLE specifies whether to enable or disable the
14253 location. */
14254
14255static void
14256enable_disable_breakpoint_location_range (int bp_num,
14257 std::pair<int, int> &bp_loc_range,
14258 bool enable)
14259{
14260 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14261 enable_disable_bp_num_loc (bp_num, i, enable);
14262}
0d381245 14263
1900040c
MS
14264/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14265 If from_tty is nonzero, it prints a message to that effect,
14266 which ends with a period (no newline). */
14267
c906108c 14268void
fba45db2 14269disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14270{
14271 /* Never disable a watchpoint scope breakpoint; we want to
14272 hit them when we leave scope so we can delete both the
14273 watchpoint and its scope breakpoint at that time. */
14274 if (bpt->type == bp_watchpoint_scope)
14275 return;
14276
b5de0fa7 14277 bpt->enable_state = bp_disabled;
c906108c 14278
b775012e
LM
14279 /* Mark breakpoint locations modified. */
14280 mark_breakpoint_modified (bpt);
14281
d248b706
KY
14282 if (target_supports_enable_disable_tracepoint ()
14283 && current_trace_status ()->running && is_tracepoint (bpt))
14284 {
40cb8ca5 14285 for (bp_location *location : bpt->locations ())
d248b706
KY
14286 target_disable_tracepoint (location);
14287 }
14288
44702360 14289 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14290
76727919 14291 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14292}
14293
d0fe4701
XR
14294/* Enable or disable the breakpoint(s) or breakpoint location(s)
14295 specified in ARGS. ARGS may be in any of the formats handled by
14296 extract_bp_number_and_location. ENABLE specifies whether to enable
14297 or disable the breakpoints/locations. */
14298
c906108c 14299static void
d0fe4701 14300enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14301{
c906108c 14302 if (args == 0)
46c6471b 14303 {
43892fdf 14304 for (breakpoint *bpt : all_breakpoints ())
46c6471b 14305 if (user_breakpoint_p (bpt))
d0fe4701
XR
14306 {
14307 if (enable)
14308 enable_breakpoint (bpt);
14309 else
14310 disable_breakpoint (bpt);
14311 }
46c6471b 14312 }
9eaabc75 14313 else
0d381245 14314 {
cb791d59 14315 std::string num = extract_arg (&args);
9eaabc75 14316
cb791d59 14317 while (!num.empty ())
d248b706 14318 {
d0fe4701 14319 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14320
cc638e86
PA
14321 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14322
14323 if (bp_loc_range.first == bp_loc_range.second
14324 && bp_loc_range.first == 0)
d0fe4701 14325 {
cc638e86
PA
14326 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14327 map_breakpoint_number_range (bp_num_range,
14328 enable
14329 ? enable_breakpoint
14330 : disable_breakpoint);
14331 }
14332 else
14333 {
14334 /* Handle breakpoint ids with formats 'x.y' or
14335 'x.y-z'. */
14336 enable_disable_breakpoint_location_range
14337 (bp_num_range.first, bp_loc_range, enable);
b775012e 14338 }
9eaabc75 14339 num = extract_arg (&args);
d248b706 14340 }
0d381245 14341 }
c906108c
SS
14342}
14343
d0fe4701
XR
14344/* The disable command disables the specified breakpoints/locations
14345 (or all defined breakpoints) so they're no longer effective in
14346 stopping the inferior. ARGS may be in any of the forms defined in
14347 extract_bp_number_and_location. */
14348
14349static void
14350disable_command (const char *args, int from_tty)
14351{
14352 enable_disable_command (args, from_tty, false);
14353}
14354
c906108c 14355static void
816338b5
SS
14356enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14357 int count)
c906108c 14358{
afe38095 14359 int target_resources_ok;
c906108c
SS
14360
14361 if (bpt->type == bp_hardware_breakpoint)
14362 {
14363 int i;
c5aa993b 14364 i = hw_breakpoint_used_count ();
53a5351d 14365 target_resources_ok =
d92524f1 14366 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14367 i + 1, 0);
c906108c 14368 if (target_resources_ok == 0)
8a3fe4f8 14369 error (_("No hardware breakpoint support in the target."));
c906108c 14370 else if (target_resources_ok < 0)
8a3fe4f8 14371 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14372 }
14373
cc60f2e3 14374 if (is_watchpoint (bpt))
c906108c 14375 {
d07205c2 14376 /* Initialize it just to avoid a GCC false warning. */
f486487f 14377 enum enable_state orig_enable_state = bp_disabled;
dde02812 14378
a70b8144 14379 try
c906108c 14380 {
3a5c3e22
PA
14381 struct watchpoint *w = (struct watchpoint *) bpt;
14382
1e718ff1
TJB
14383 orig_enable_state = bpt->enable_state;
14384 bpt->enable_state = bp_enabled;
3a5c3e22 14385 update_watchpoint (w, 1 /* reparse */);
c906108c 14386 }
230d2906 14387 catch (const gdb_exception &e)
c5aa993b 14388 {
1e718ff1 14389 bpt->enable_state = orig_enable_state;
dde02812
ES
14390 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14391 bpt->number);
14392 return;
c5aa993b 14393 }
c906108c 14394 }
0101ce28 14395
b775012e
LM
14396 bpt->enable_state = bp_enabled;
14397
14398 /* Mark breakpoint locations modified. */
14399 mark_breakpoint_modified (bpt);
14400
d248b706
KY
14401 if (target_supports_enable_disable_tracepoint ()
14402 && current_trace_status ()->running && is_tracepoint (bpt))
14403 {
40cb8ca5 14404 for (bp_location *location : bpt->locations ())
d248b706
KY
14405 target_enable_tracepoint (location);
14406 }
14407
b4c291bb 14408 bpt->disposition = disposition;
816338b5 14409 bpt->enable_count = count;
44702360 14410 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14411
76727919 14412 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14413}
14414
fe3f5fa8 14415
c906108c 14416void
fba45db2 14417enable_breakpoint (struct breakpoint *bpt)
c906108c 14418{
816338b5 14419 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14420}
14421
d0fe4701
XR
14422/* The enable command enables the specified breakpoints/locations (or
14423 all defined breakpoints) so they once again become (or continue to
14424 be) effective in stopping the inferior. ARGS may be in any of the
14425 forms defined in extract_bp_number_and_location. */
c906108c 14426
c906108c 14427static void
981a3fb3 14428enable_command (const char *args, int from_tty)
c906108c 14429{
d0fe4701 14430 enable_disable_command (args, from_tty, true);
c906108c
SS
14431}
14432
c906108c 14433static void
4495129a 14434enable_once_command (const char *args, int from_tty)
c906108c 14435{
48649e1b
TT
14436 map_breakpoint_numbers
14437 (args, [&] (breakpoint *b)
14438 {
14439 iterate_over_related_breakpoints
14440 (b, [&] (breakpoint *bpt)
14441 {
14442 enable_breakpoint_disp (bpt, disp_disable, 1);
14443 });
14444 });
816338b5
SS
14445}
14446
14447static void
4495129a 14448enable_count_command (const char *args, int from_tty)
816338b5 14449{
b9d61307
SM
14450 int count;
14451
14452 if (args == NULL)
14453 error_no_arg (_("hit count"));
14454
14455 count = get_number (&args);
816338b5 14456
48649e1b
TT
14457 map_breakpoint_numbers
14458 (args, [&] (breakpoint *b)
14459 {
14460 iterate_over_related_breakpoints
14461 (b, [&] (breakpoint *bpt)
14462 {
14463 enable_breakpoint_disp (bpt, disp_disable, count);
14464 });
14465 });
c906108c
SS
14466}
14467
c906108c 14468static void
4495129a 14469enable_delete_command (const char *args, int from_tty)
c906108c 14470{
48649e1b
TT
14471 map_breakpoint_numbers
14472 (args, [&] (breakpoint *b)
14473 {
14474 iterate_over_related_breakpoints
14475 (b, [&] (breakpoint *bpt)
14476 {
14477 enable_breakpoint_disp (bpt, disp_del, 1);
14478 });
14479 });
c906108c
SS
14480}
14481\f
1f3b5d1b
PP
14482/* Invalidate last known value of any hardware watchpoint if
14483 the memory which that value represents has been written to by
14484 GDB itself. */
14485
14486static void
8de0566d
YQ
14487invalidate_bp_value_on_memory_change (struct inferior *inferior,
14488 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14489 const bfd_byte *data)
14490{
43892fdf 14491 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 14492 if (bp->enable_state == bp_enabled
3a5c3e22 14493 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14494 {
3a5c3e22 14495 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14496
850645cf 14497 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 14498 {
40cb8ca5 14499 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
14500 if (loc->loc_type == bp_loc_hardware_watchpoint
14501 && loc->address + loc->length > addr
14502 && addr + len > loc->address)
14503 {
3a5c3e22 14504 wp->val = NULL;
4c1d86d9 14505 wp->val_valid = false;
3a5c3e22
PA
14506 }
14507 }
1f3b5d1b
PP
14508 }
14509}
14510
8181d85f
DJ
14511/* Create and insert a breakpoint for software single step. */
14512
14513void
6c95b8df 14514insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14515 const address_space *aspace,
4a64f543 14516 CORE_ADDR next_pc)
8181d85f 14517{
7c16b83e
PA
14518 struct thread_info *tp = inferior_thread ();
14519 struct symtab_and_line sal;
14520 CORE_ADDR pc = next_pc;
8181d85f 14521
34b7e8a6
PA
14522 if (tp->control.single_step_breakpoints == NULL)
14523 {
14524 tp->control.single_step_breakpoints
5d5658a1 14525 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14526 }
8181d85f 14527
7c16b83e
PA
14528 sal = find_pc_line (pc, 0);
14529 sal.pc = pc;
14530 sal.section = find_pc_overlay (pc);
14531 sal.explicit_pc = 1;
34b7e8a6 14532 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14533
7c16b83e 14534 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14535}
14536
93f9a11f
YQ
14537/* Insert single step breakpoints according to the current state. */
14538
14539int
14540insert_single_step_breakpoints (struct gdbarch *gdbarch)
14541{
f5ea389a 14542 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14543 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14544
f5ea389a 14545 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14546
a0ff9e1a 14547 if (!next_pcs.empty ())
93f9a11f 14548 {
f5ea389a 14549 struct frame_info *frame = get_current_frame ();
8b86c959 14550 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14551
a0ff9e1a 14552 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14553 insert_single_step_breakpoint (gdbarch, aspace, pc);
14554
93f9a11f
YQ
14555 return 1;
14556 }
14557 else
14558 return 0;
14559}
14560
34b7e8a6 14561/* See breakpoint.h. */
f02253f1
HZ
14562
14563int
7c16b83e 14564breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14565 const address_space *aspace,
7c16b83e 14566 CORE_ADDR pc)
1aafd4da 14567{
40cb8ca5 14568 for (bp_location *loc : bp->locations ())
7c16b83e
PA
14569 if (loc->inserted
14570 && breakpoint_location_address_match (loc, aspace, pc))
14571 return 1;
1aafd4da 14572
7c16b83e 14573 return 0;
ef370185
JB
14574}
14575
14576/* Check whether a software single-step breakpoint is inserted at
14577 PC. */
14578
14579int
accd0bcd 14580single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14581 CORE_ADDR pc)
14582{
43892fdf 14583 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
14584 {
14585 if (bpt->type == bp_single_step
14586 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14587 return 1;
14588 }
14589 return 0;
1aafd4da
UW
14590}
14591
1042e4c0
SS
14592/* Tracepoint-specific operations. */
14593
14594/* Set tracepoint count to NUM. */
14595static void
14596set_tracepoint_count (int num)
14597{
14598 tracepoint_count = num;
4fa62494 14599 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14600}
14601
70221824 14602static void
0b39b52e 14603trace_command (const char *arg, int from_tty)
1042e4c0 14604{
ffc2605c
TT
14605 event_location_up location = string_to_event_location (&arg,
14606 current_language);
bac7c5cf
GB
14607 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14608 (location.get (), true /* is_tracepoint */);
55aa24fb 14609
558a9d82 14610 create_breakpoint (get_current_arch (),
ffc2605c 14611 location.get (),
10a636cc 14612 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14613 0 /* tempflag */,
14614 bp_tracepoint /* type_wanted */,
14615 0 /* Ignore count */,
14616 pending_break_support,
14617 ops,
14618 from_tty,
14619 1 /* enabled */,
14620 0 /* internal */, 0);
1042e4c0
SS
14621}
14622
70221824 14623static void
0b39b52e 14624ftrace_command (const char *arg, int from_tty)
7a697b8d 14625{
ffc2605c
TT
14626 event_location_up location = string_to_event_location (&arg,
14627 current_language);
558a9d82 14628 create_breakpoint (get_current_arch (),
ffc2605c 14629 location.get (),
10a636cc 14630 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14631 0 /* tempflag */,
14632 bp_fast_tracepoint /* type_wanted */,
14633 0 /* Ignore count */,
14634 pending_break_support,
14635 &tracepoint_breakpoint_ops,
14636 from_tty,
14637 1 /* enabled */,
14638 0 /* internal */, 0);
0fb4aa4b
PA
14639}
14640
14641/* strace command implementation. Creates a static tracepoint. */
14642
70221824 14643static void
0b39b52e 14644strace_command (const char *arg, int from_tty)
0fb4aa4b 14645{
983af33b 14646 struct breakpoint_ops *ops;
ffc2605c 14647 event_location_up location;
983af33b
SDJ
14648
14649 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14650 or with a normal static tracepoint. */
61012eef 14651 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14652 {
14653 ops = &strace_marker_breakpoint_ops;
a20714ff 14654 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14655 }
983af33b 14656 else
f00aae0f
KS
14657 {
14658 ops = &tracepoint_breakpoint_ops;
14659 location = string_to_event_location (&arg, current_language);
14660 }
983af33b 14661
558a9d82 14662 create_breakpoint (get_current_arch (),
ffc2605c 14663 location.get (),
10a636cc 14664 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14665 0 /* tempflag */,
14666 bp_static_tracepoint /* type_wanted */,
14667 0 /* Ignore count */,
14668 pending_break_support,
14669 ops,
14670 from_tty,
14671 1 /* enabled */,
14672 0 /* internal */, 0);
7a697b8d
SS
14673}
14674
409873ef
SS
14675/* Set up a fake reader function that gets command lines from a linked
14676 list that was acquired during tracepoint uploading. */
14677
14678static struct uploaded_tp *this_utp;
3149d8c1 14679static int next_cmd;
409873ef
SS
14680
14681static char *
14682read_uploaded_action (void)
14683{
a18ba4e4 14684 char *rslt = nullptr;
409873ef 14685
a18ba4e4
SM
14686 if (next_cmd < this_utp->cmd_strings.size ())
14687 {
67aa1f3c 14688 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14689 next_cmd++;
14690 }
409873ef
SS
14691
14692 return rslt;
14693}
14694
00bf0b85
SS
14695/* Given information about a tracepoint as recorded on a target (which
14696 can be either a live system or a trace file), attempt to create an
14697 equivalent GDB tracepoint. This is not a reliable process, since
14698 the target does not necessarily have all the information used when
14699 the tracepoint was originally defined. */
14700
d9b3f62e 14701struct tracepoint *
00bf0b85 14702create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14703{
f2fc3015
TT
14704 const char *addr_str;
14705 char small_buf[100];
d9b3f62e 14706 struct tracepoint *tp;
fd9b8c24 14707
409873ef 14708 if (utp->at_string)
67aa1f3c 14709 addr_str = utp->at_string.get ();
409873ef
SS
14710 else
14711 {
14712 /* In the absence of a source location, fall back to raw
14713 address. Since there is no way to confirm that the address
14714 means the same thing as when the trace was started, warn the
14715 user. */
3e43a32a
MS
14716 warning (_("Uploaded tracepoint %d has no "
14717 "source location, using raw address"),
409873ef 14718 utp->number);
8c042590 14719 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14720 addr_str = small_buf;
14721 }
14722
14723 /* There's not much we can do with a sequence of bytecodes. */
14724 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14725 warning (_("Uploaded tracepoint %d condition "
14726 "has no source form, ignoring it"),
409873ef 14727 utp->number);
d5551862 14728
ffc2605c
TT
14729 event_location_up location = string_to_event_location (&addr_str,
14730 current_language);
8cdf0e15 14731 if (!create_breakpoint (get_current_arch (),
ffc2605c 14732 location.get (),
67aa1f3c 14733 utp->cond_string.get (), -1, addr_str,
10a636cc 14734 false /* force_condition */,
e7e0cddf 14735 0 /* parse cond/thread */,
8cdf0e15 14736 0 /* tempflag */,
0fb4aa4b 14737 utp->type /* type_wanted */,
8cdf0e15
VP
14738 0 /* Ignore count */,
14739 pending_break_support,
348d480f 14740 &tracepoint_breakpoint_ops,
8cdf0e15 14741 0 /* from_tty */,
84f4c1fe 14742 utp->enabled /* enabled */,
44f238bb
PA
14743 0 /* internal */,
14744 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14745 return NULL;
fd9b8c24 14746
409873ef 14747 /* Get the tracepoint we just created. */
fd9b8c24
PA
14748 tp = get_tracepoint (tracepoint_count);
14749 gdb_assert (tp != NULL);
d5551862 14750
00bf0b85
SS
14751 if (utp->pass > 0)
14752 {
8c042590 14753 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14754 tp->number);
00bf0b85 14755
409873ef 14756 trace_pass_command (small_buf, 0);
00bf0b85
SS
14757 }
14758
409873ef
SS
14759 /* If we have uploaded versions of the original commands, set up a
14760 special-purpose "reader" function and call the usual command line
14761 reader, then pass the result to the breakpoint command-setting
14762 function. */
a18ba4e4 14763 if (!utp->cmd_strings.empty ())
00bf0b85 14764 {
12973681 14765 counted_command_line cmd_list;
00bf0b85 14766
409873ef 14767 this_utp = utp;
3149d8c1 14768 next_cmd = 0;
d5551862 14769
60b3cef2 14770 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14771
c1fc2657 14772 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14773 }
a18ba4e4
SM
14774 else if (!utp->actions.empty ()
14775 || !utp->step_actions.empty ())
3e43a32a
MS
14776 warning (_("Uploaded tracepoint %d actions "
14777 "have no source form, ignoring them"),
409873ef 14778 utp->number);
00bf0b85 14779
f196051f 14780 /* Copy any status information that might be available. */
c1fc2657 14781 tp->hit_count = utp->hit_count;
f196051f
SS
14782 tp->traceframe_usage = utp->traceframe_usage;
14783
00bf0b85 14784 return tp;
d9b3f62e 14785}
00bf0b85 14786
1042e4c0
SS
14787/* Print information on tracepoint number TPNUM_EXP, or all if
14788 omitted. */
14789
14790static void
1d12d88f 14791info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14792{
79a45e25 14793 struct ui_out *uiout = current_uiout;
e5a67952 14794 int num_printed;
1042e4c0 14795
5c458ae8 14796 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14797
14798 if (num_printed == 0)
1042e4c0 14799 {
e5a67952 14800 if (args == NULL || *args == '\0')
112e8700 14801 uiout->message ("No tracepoints.\n");
d77f58be 14802 else
112e8700 14803 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14804 }
ad443146
SS
14805
14806 default_collect_info ();
1042e4c0
SS
14807}
14808
4a64f543 14809/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14810 Not supported by all targets. */
14811static void
5fed81ff 14812enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14813{
14814 enable_command (args, from_tty);
14815}
14816
4a64f543 14817/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14818 Not supported by all targets. */
14819static void
5fed81ff 14820disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14821{
14822 disable_command (args, from_tty);
14823}
14824
4a64f543 14825/* Remove a tracepoint (or all if no argument). */
1042e4c0 14826static void
4495129a 14827delete_trace_command (const char *arg, int from_tty)
1042e4c0 14828{
1042e4c0
SS
14829 dont_repeat ();
14830
14831 if (arg == 0)
14832 {
14833 int breaks_to_delete = 0;
14834
14835 /* Delete all breakpoints if no argument.
dda83cd7
SM
14836 Do not delete internal or call-dummy breakpoints, these
14837 have to be deleted with an explicit breakpoint number
4a64f543 14838 argument. */
f6d17b2b 14839 for (breakpoint *tp : all_tracepoints ())
1428b37a 14840 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
14841 {
14842 breaks_to_delete = 1;
14843 break;
14844 }
1042e4c0
SS
14845
14846 /* Ask user only if there are some breakpoints to delete. */
14847 if (!from_tty
14848 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14849 {
1428b37a 14850 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14851 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14852 delete_breakpoint (b);
1042e4c0
SS
14853 }
14854 }
14855 else
48649e1b 14856 map_breakpoint_numbers
b926417a 14857 (arg, [&] (breakpoint *br)
48649e1b 14858 {
b926417a 14859 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14860 });
1042e4c0
SS
14861}
14862
197f0a60
TT
14863/* Helper function for trace_pass_command. */
14864
14865static void
d9b3f62e 14866trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14867{
d9b3f62e 14868 tp->pass_count = count;
76727919 14869 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14870 if (from_tty)
14871 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14872 tp->number, count);
197f0a60
TT
14873}
14874
1042e4c0
SS
14875/* Set passcount for tracepoint.
14876
14877 First command argument is passcount, second is tracepoint number.
14878 If tracepoint number omitted, apply to most recently defined.
14879 Also accepts special argument "all". */
14880
14881static void
0b39b52e 14882trace_pass_command (const char *args, int from_tty)
1042e4c0 14883{
d9b3f62e 14884 struct tracepoint *t1;
0b39b52e 14885 ULONGEST count;
1042e4c0
SS
14886
14887 if (args == 0 || *args == 0)
3e43a32a
MS
14888 error (_("passcount command requires an "
14889 "argument (count + optional TP num)"));
1042e4c0 14890
0b39b52e 14891 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14892
529480d0 14893 args = skip_spaces (args);
1042e4c0
SS
14894 if (*args && strncasecmp (args, "all", 3) == 0)
14895 {
14896 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14897 if (*args)
14898 error (_("Junk at end of arguments."));
1042e4c0 14899
f6d17b2b 14900 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14901 {
14902 t1 = (struct tracepoint *) b;
14903 trace_pass_set_count (t1, count, from_tty);
14904 }
197f0a60
TT
14905 }
14906 else if (*args == '\0')
1042e4c0 14907 {
5fa1d40e 14908 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14909 if (t1)
197f0a60
TT
14910 trace_pass_set_count (t1, count, from_tty);
14911 }
14912 else
14913 {
bfd28288
PA
14914 number_or_range_parser parser (args);
14915 while (!parser.finished ())
1042e4c0 14916 {
bfd28288 14917 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14918 if (t1)
14919 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14920 }
14921 }
1042e4c0
SS
14922}
14923
d9b3f62e 14924struct tracepoint *
1042e4c0
SS
14925get_tracepoint (int num)
14926{
f6d17b2b 14927 for (breakpoint *t : all_tracepoints ())
1042e4c0 14928 if (t->number == num)
d9b3f62e 14929 return (struct tracepoint *) t;
1042e4c0
SS
14930
14931 return NULL;
14932}
14933
d5551862
SS
14934/* Find the tracepoint with the given target-side number (which may be
14935 different from the tracepoint number after disconnecting and
14936 reconnecting). */
14937
d9b3f62e 14938struct tracepoint *
d5551862
SS
14939get_tracepoint_by_number_on_target (int num)
14940{
f6d17b2b 14941 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14942 {
14943 struct tracepoint *t = (struct tracepoint *) b;
14944
14945 if (t->number_on_target == num)
14946 return t;
14947 }
d5551862
SS
14948
14949 return NULL;
14950}
14951
1042e4c0 14952/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14953 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14954 If the argument is missing, the most recent tracepoint
14955 (tracepoint_count) is returned. */
14956
d9b3f62e 14957struct tracepoint *
0b39b52e 14958get_tracepoint_by_number (const char **arg,
bfd28288 14959 number_or_range_parser *parser)
1042e4c0 14960{
1042e4c0 14961 int tpnum;
0b39b52e 14962 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14963
bfd28288 14964 if (parser != NULL)
197f0a60 14965 {
bfd28288
PA
14966 gdb_assert (!parser->finished ());
14967 tpnum = parser->get_number ();
197f0a60
TT
14968 }
14969 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14970 tpnum = tracepoint_count;
1042e4c0 14971 else
197f0a60 14972 tpnum = get_number (arg);
1042e4c0
SS
14973
14974 if (tpnum <= 0)
14975 {
14976 if (instring && *instring)
14977 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14978 instring);
14979 else
5fa1d40e 14980 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14981 return NULL;
14982 }
14983
f6d17b2b 14984 for (breakpoint *t : all_tracepoints ())
1042e4c0 14985 if (t->number == tpnum)
d9b3f62e 14986 return (struct tracepoint *) t;
1042e4c0 14987
1042e4c0
SS
14988 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14989 return NULL;
14990}
14991
d9b3f62e
PA
14992void
14993print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14994{
14995 if (b->thread != -1)
14996 fprintf_unfiltered (fp, " thread %d", b->thread);
14997
14998 if (b->task != 0)
14999 fprintf_unfiltered (fp, " task %d", b->task);
15000
15001 fprintf_unfiltered (fp, "\n");
15002}
15003
6149aea9
PA
15004/* Save information on user settable breakpoints (watchpoints, etc) to
15005 a new script file named FILENAME. If FILTER is non-NULL, call it
15006 on each breakpoint and only include the ones for which it returns
f2478a7e 15007 true. */
6149aea9 15008
1042e4c0 15009static void
4495129a 15010save_breakpoints (const char *filename, int from_tty,
f2478a7e 15011 bool (*filter) (const struct breakpoint *))
1042e4c0 15012{
6149aea9 15013 int any = 0;
6149aea9 15014 int extra_trace_bits = 0;
1042e4c0 15015
6149aea9
PA
15016 if (filename == 0 || *filename == 0)
15017 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15018
15019 /* See if we have anything to save. */
43892fdf 15020 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15021 {
15022 /* Skip internal and momentary breakpoints. */
15023 if (!user_breakpoint_p (tp))
15024 continue;
6149aea9 15025
01add95b
SM
15026 /* If we have a filter, only save the breakpoints it accepts. */
15027 if (filter && !filter (tp))
15028 continue;
6149aea9 15029
01add95b 15030 any = 1;
6149aea9 15031
01add95b
SM
15032 if (is_tracepoint (tp))
15033 {
15034 extra_trace_bits = 1;
6149aea9 15035
01add95b
SM
15036 /* We can stop searching. */
15037 break;
15038 }
15039 }
6149aea9
PA
15040
15041 if (!any)
1042e4c0 15042 {
6149aea9 15043 warning (_("Nothing to save."));
1042e4c0
SS
15044 return;
15045 }
15046
ee0c3293 15047 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15048
15049 stdio_file fp;
15050
ee0c3293 15051 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15052 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15053 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15054
6149aea9 15055 if (extra_trace_bits)
d7e74731 15056 save_trace_state_variables (&fp);
8bf6485c 15057
43892fdf 15058 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15059 {
15060 /* Skip internal and momentary breakpoints. */
15061 if (!user_breakpoint_p (tp))
15062 continue;
8bf6485c 15063
01add95b
SM
15064 /* If we have a filter, only save the breakpoints it accepts. */
15065 if (filter && !filter (tp))
15066 continue;
6149aea9 15067
01add95b 15068 tp->ops->print_recreate (tp, &fp);
1042e4c0 15069
01add95b
SM
15070 /* Note, we can't rely on tp->number for anything, as we can't
15071 assume the recreated breakpoint numbers will match. Use $bpnum
15072 instead. */
6149aea9 15073
01add95b
SM
15074 if (tp->cond_string)
15075 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9 15076
01add95b
SM
15077 if (tp->ignore_count)
15078 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15079
01add95b
SM
15080 if (tp->type != bp_dprintf && tp->commands)
15081 {
15082 fp.puts (" commands\n");
1042e4c0 15083
01add95b
SM
15084 current_uiout->redirect (&fp);
15085 try
15086 {
15087 print_command_lines (current_uiout, tp->commands.get (), 2);
15088 }
15089 catch (const gdb_exception &ex)
15090 {
15091 current_uiout->redirect (NULL);
15092 throw;
15093 }
6149aea9 15094
01add95b
SM
15095 current_uiout->redirect (NULL);
15096 fp.puts (" end\n");
15097 }
6149aea9 15098
01add95b
SM
15099 if (tp->enable_state == bp_disabled)
15100 fp.puts ("disable $bpnum\n");
6149aea9 15101
01add95b
SM
15102 /* If this is a multi-location breakpoint, check if the locations
15103 should be individually disabled. Watchpoint locations are
15104 special, and not user visible. */
15105 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15106 {
15107 int n = 1;
40cb8ca5 15108
01add95b
SM
15109 for (bp_location *loc : tp->locations ())
15110 {
15111 if (!loc->enabled)
15112 fp.printf ("disable $bpnum.%d\n", n);
15113
15114 n++;
15115 }
15116 }
15117 }
8bf6485c 15118
6149aea9 15119 if (extra_trace_bits && *default_collect)
d7e74731 15120 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15121
1042e4c0 15122 if (from_tty)
ee0c3293 15123 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15124}
15125
15126/* The `save breakpoints' command. */
15127
15128static void
4495129a 15129save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15130{
15131 save_breakpoints (args, from_tty, NULL);
15132}
15133
15134/* The `save tracepoints' command. */
15135
15136static void
4495129a 15137save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15138{
15139 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15140}
15141
c906108c 15142\f
629500fa
KS
15143/* This help string is used to consolidate all the help string for specifying
15144 locations used by several commands. */
15145
15146#define LOCATION_HELP_STRING \
15147"Linespecs are colon-separated lists of location parameters, such as\n\
15148source filename, function name, label name, and line number.\n\
15149Example: To specify the start of a label named \"the_top\" in the\n\
15150function \"fact\" in the file \"factorial.c\", use\n\
15151\"factorial.c:fact:the_top\".\n\
15152\n\
15153Address locations begin with \"*\" and specify an exact address in the\n\
15154program. Example: To specify the fourth byte past the start function\n\
15155\"main\", use \"*main + 4\".\n\
15156\n\
15157Explicit locations are similar to linespecs but use an option/argument\n\
15158syntax to specify location parameters.\n\
15159Example: To specify the start of the label named \"the_top\" in the\n\
15160function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15161-function fact -label the_top\".\n\
15162\n\
15163By default, a specified function is matched against the program's\n\
15164functions in all scopes. For C++, this means in all namespaces and\n\
15165classes. For Ada, this means in all packages. E.g., in C++,\n\
15166\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15167\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15168specified name as a complete fully-qualified name instead."
629500fa 15169
4a64f543
MS
15170/* This help string is used for the break, hbreak, tbreak and thbreak
15171 commands. It is defined as a macro to prevent duplication.
15172 COMMAND should be a string constant containing the name of the
15173 command. */
629500fa 15174
31e2b00f 15175#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15176command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15177\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15178PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15179probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15180guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15181`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15182LOCATION may be a linespec, address, or explicit location as described\n\
15183below.\n\
15184\n\
dc10affe
PA
15185With no LOCATION, uses current execution address of the selected\n\
15186stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15187\n\
15188THREADNUM is the number from \"info threads\".\n\
15189CONDITION is a boolean expression.\n\
733d554a
TBA
15190\n\
15191With the \"-force-condition\" flag, the condition is defined even when\n\
15192it is invalid for all current locations.\n\
89549d7f 15193\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15194Multiple breakpoints at one place are permitted, and useful if their\n\
15195conditions are different.\n\
31e2b00f
AS
15196\n\
15197Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15198
44feb3ce
TT
15199/* List of subcommands for "catch". */
15200static struct cmd_list_element *catch_cmdlist;
15201
15202/* List of subcommands for "tcatch". */
15203static struct cmd_list_element *tcatch_cmdlist;
15204
9ac4176b 15205void
a121b7c1 15206add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15207 cmd_const_sfunc_ftype *sfunc,
625e8578 15208 completer_ftype *completer,
44feb3ce
TT
15209 void *user_data_catch,
15210 void *user_data_tcatch)
15211{
15212 struct cmd_list_element *command;
15213
0450cc4c 15214 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15215 &catch_cmdlist);
15216 set_cmd_sfunc (command, sfunc);
15217 set_cmd_context (command, user_data_catch);
a96d9b2e 15218 set_cmd_completer (command, completer);
44feb3ce 15219
0450cc4c 15220 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15221 &tcatch_cmdlist);
15222 set_cmd_sfunc (command, sfunc);
15223 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15224 set_cmd_completer (command, completer);
44feb3ce
TT
15225}
15226
0574c78f
GB
15227/* Zero if any of the breakpoint's locations could be a location where
15228 functions have been inlined, nonzero otherwise. */
15229
15230static int
15231is_non_inline_function (struct breakpoint *b)
15232{
15233 /* The shared library event breakpoint is set on the address of a
15234 non-inline function. */
15235 if (b->type == bp_shlib_event)
15236 return 1;
15237
15238 return 0;
15239}
15240
15241/* Nonzero if the specified PC cannot be a location where functions
15242 have been inlined. */
15243
15244int
accd0bcd 15245pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15246 const struct target_waitstatus *ws)
0574c78f 15247{
43892fdf 15248 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
15249 {
15250 if (!is_non_inline_function (b))
15251 continue;
15252
40cb8ca5 15253 for (bp_location *bl : b->locations ())
0574c78f
GB
15254 {
15255 if (!bl->shlib_disabled
09ac7c10 15256 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15257 return 1;
15258 }
15259 }
15260
15261 return 0;
15262}
15263
2f202fde
JK
15264/* Remove any references to OBJFILE which is going to be freed. */
15265
15266void
15267breakpoint_free_objfile (struct objfile *objfile)
15268{
48d7020b 15269 for (bp_location *loc : all_bp_locations ())
eb822aa6 15270 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15271 loc->symtab = NULL;
15272}
15273
2060206e
PA
15274void
15275initialize_breakpoint_ops (void)
15276{
15277 static int initialized = 0;
15278
15279 struct breakpoint_ops *ops;
15280
15281 if (initialized)
15282 return;
15283 initialized = 1;
15284
15285 /* The breakpoint_ops structure to be inherit by all kinds of
15286 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15287 internal and momentary breakpoints, etc.). */
15288 ops = &bkpt_base_breakpoint_ops;
15289 *ops = base_breakpoint_ops;
15290 ops->re_set = bkpt_re_set;
15291 ops->insert_location = bkpt_insert_location;
15292 ops->remove_location = bkpt_remove_location;
15293 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15294 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15295 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15296 ops->decode_location = bkpt_decode_location;
2060206e
PA
15297
15298 /* The breakpoint_ops structure to be used in regular breakpoints. */
15299 ops = &bkpt_breakpoint_ops;
15300 *ops = bkpt_base_breakpoint_ops;
15301 ops->re_set = bkpt_re_set;
15302 ops->resources_needed = bkpt_resources_needed;
15303 ops->print_it = bkpt_print_it;
15304 ops->print_mention = bkpt_print_mention;
15305 ops->print_recreate = bkpt_print_recreate;
15306
15307 /* Ranged breakpoints. */
15308 ops = &ranged_breakpoint_ops;
15309 *ops = bkpt_breakpoint_ops;
15310 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15311 ops->resources_needed = resources_needed_ranged_breakpoint;
15312 ops->print_it = print_it_ranged_breakpoint;
15313 ops->print_one = print_one_ranged_breakpoint;
15314 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15315 ops->print_mention = print_mention_ranged_breakpoint;
15316 ops->print_recreate = print_recreate_ranged_breakpoint;
15317
15318 /* Internal breakpoints. */
15319 ops = &internal_breakpoint_ops;
15320 *ops = bkpt_base_breakpoint_ops;
15321 ops->re_set = internal_bkpt_re_set;
15322 ops->check_status = internal_bkpt_check_status;
15323 ops->print_it = internal_bkpt_print_it;
15324 ops->print_mention = internal_bkpt_print_mention;
15325
15326 /* Momentary breakpoints. */
15327 ops = &momentary_breakpoint_ops;
15328 *ops = bkpt_base_breakpoint_ops;
15329 ops->re_set = momentary_bkpt_re_set;
15330 ops->check_status = momentary_bkpt_check_status;
15331 ops->print_it = momentary_bkpt_print_it;
15332 ops->print_mention = momentary_bkpt_print_mention;
15333
55aa24fb
SDJ
15334 /* Probe breakpoints. */
15335 ops = &bkpt_probe_breakpoint_ops;
15336 *ops = bkpt_breakpoint_ops;
15337 ops->insert_location = bkpt_probe_insert_location;
15338 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15339 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15340 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15341
2060206e
PA
15342 /* Watchpoints. */
15343 ops = &watchpoint_breakpoint_ops;
15344 *ops = base_breakpoint_ops;
15345 ops->re_set = re_set_watchpoint;
15346 ops->insert_location = insert_watchpoint;
15347 ops->remove_location = remove_watchpoint;
15348 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15349 ops->check_status = check_status_watchpoint;
15350 ops->resources_needed = resources_needed_watchpoint;
15351 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15352 ops->print_it = print_it_watchpoint;
15353 ops->print_mention = print_mention_watchpoint;
15354 ops->print_recreate = print_recreate_watchpoint;
427cd150 15355 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15356
15357 /* Masked watchpoints. */
15358 ops = &masked_watchpoint_breakpoint_ops;
15359 *ops = watchpoint_breakpoint_ops;
15360 ops->insert_location = insert_masked_watchpoint;
15361 ops->remove_location = remove_masked_watchpoint;
15362 ops->resources_needed = resources_needed_masked_watchpoint;
15363 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15364 ops->print_it = print_it_masked_watchpoint;
15365 ops->print_one_detail = print_one_detail_masked_watchpoint;
15366 ops->print_mention = print_mention_masked_watchpoint;
15367 ops->print_recreate = print_recreate_masked_watchpoint;
15368
15369 /* Tracepoints. */
15370 ops = &tracepoint_breakpoint_ops;
15371 *ops = base_breakpoint_ops;
15372 ops->re_set = tracepoint_re_set;
15373 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15374 ops->print_one_detail = tracepoint_print_one_detail;
15375 ops->print_mention = tracepoint_print_mention;
15376 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15377 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15378 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15379 ops->decode_location = tracepoint_decode_location;
983af33b 15380
55aa24fb
SDJ
15381 /* Probe tracepoints. */
15382 ops = &tracepoint_probe_breakpoint_ops;
15383 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15384 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15385 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15386
983af33b
SDJ
15387 /* Static tracepoints with marker (`-m'). */
15388 ops = &strace_marker_breakpoint_ops;
15389 *ops = tracepoint_breakpoint_ops;
5f700d83 15390 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15391 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15392 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15393
15394 /* Fork catchpoints. */
15395 ops = &catch_fork_breakpoint_ops;
15396 *ops = base_breakpoint_ops;
15397 ops->insert_location = insert_catch_fork;
15398 ops->remove_location = remove_catch_fork;
15399 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15400 ops->print_it = print_it_catch_fork;
15401 ops->print_one = print_one_catch_fork;
15402 ops->print_mention = print_mention_catch_fork;
15403 ops->print_recreate = print_recreate_catch_fork;
15404
15405 /* Vfork catchpoints. */
15406 ops = &catch_vfork_breakpoint_ops;
15407 *ops = base_breakpoint_ops;
15408 ops->insert_location = insert_catch_vfork;
15409 ops->remove_location = remove_catch_vfork;
15410 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15411 ops->print_it = print_it_catch_vfork;
15412 ops->print_one = print_one_catch_vfork;
15413 ops->print_mention = print_mention_catch_vfork;
15414 ops->print_recreate = print_recreate_catch_vfork;
15415
15416 /* Exec catchpoints. */
15417 ops = &catch_exec_breakpoint_ops;
15418 *ops = base_breakpoint_ops;
2060206e
PA
15419 ops->insert_location = insert_catch_exec;
15420 ops->remove_location = remove_catch_exec;
15421 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15422 ops->print_it = print_it_catch_exec;
15423 ops->print_one = print_one_catch_exec;
15424 ops->print_mention = print_mention_catch_exec;
15425 ops->print_recreate = print_recreate_catch_exec;
15426
edcc5120
TT
15427 /* Solib-related catchpoints. */
15428 ops = &catch_solib_breakpoint_ops;
15429 *ops = base_breakpoint_ops;
edcc5120
TT
15430 ops->insert_location = insert_catch_solib;
15431 ops->remove_location = remove_catch_solib;
15432 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15433 ops->check_status = check_status_catch_solib;
15434 ops->print_it = print_it_catch_solib;
15435 ops->print_one = print_one_catch_solib;
15436 ops->print_mention = print_mention_catch_solib;
15437 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15438
15439 ops = &dprintf_breakpoint_ops;
15440 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15441 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15442 ops->resources_needed = bkpt_resources_needed;
15443 ops->print_it = bkpt_print_it;
15444 ops->print_mention = bkpt_print_mention;
2d9442cc 15445 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15446 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15447 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15448}
15449
8bfd80db
YQ
15450/* Chain containing all defined "enable breakpoint" subcommands. */
15451
15452static struct cmd_list_element *enablebreaklist = NULL;
15453
8588b356
SM
15454/* See breakpoint.h. */
15455
15456cmd_list_element *commands_cmd_element = nullptr;
15457
6c265988 15458void _initialize_breakpoint ();
c906108c 15459void
6c265988 15460_initialize_breakpoint ()
c906108c
SS
15461{
15462 struct cmd_list_element *c;
15463
2060206e
PA
15464 initialize_breakpoint_ops ();
15465
c90e7d63
SM
15466 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15467 "breakpoint");
15468 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15469 "breakpoint");
15470 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15471 "breakpoint");
84acb35a 15472
c906108c
SS
15473 breakpoint_chain = 0;
15474 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15475 before a breakpoint is set. */
15476 breakpoint_count = 0;
15477
1042e4c0
SS
15478 tracepoint_count = 0;
15479
1bedd215
AC
15480 add_com ("ignore", class_breakpoint, ignore_command, _("\
15481Set ignore-count of breakpoint number N to COUNT.\n\
15482Usage is `ignore N COUNT'."));
c906108c 15483
8588b356
SM
15484 commands_cmd_element = add_com ("commands", class_breakpoint,
15485 commands_command, _("\
18da0c51
MG
15486Set commands to be executed when the given breakpoints are hit.\n\
15487Give a space-separated breakpoint list as argument after \"commands\".\n\
15488A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15489(e.g. `5-7').\n\
c906108c
SS
15490With no argument, the targeted breakpoint is the last one set.\n\
15491The commands themselves follow starting on the next line.\n\
15492Type a line containing \"end\" to indicate the end of them.\n\
15493Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15494then no output is printed when it is hit, except what the commands print."));
c906108c 15495
b1d4d8d1
TBA
15496 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15497 static std::string condition_command_help
15498 = gdb::option::build_help (_("\
1bedd215 15499Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15500Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15501is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15502\n\
15503Options:\n\
15504%OPTIONS%"), cc_opts);
15505
15506 c = add_com ("condition", class_breakpoint, condition_command,
15507 condition_command_help.c_str ());
15508 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15509
1bedd215 15510 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15511Set a temporary breakpoint.\n\
c906108c
SS
15512Like \"break\" except the breakpoint is only temporary,\n\
15513so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15514by using \"enable delete\" on the breakpoint number.\n\
15515\n"
15516BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15517 set_cmd_completer (c, location_completer);
c94fdfd0 15518
1bedd215 15519 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15520Set a hardware assisted breakpoint.\n\
c906108c 15521Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15522some target hardware may not have this support.\n\
15523\n"
15524BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15525 set_cmd_completer (c, location_completer);
c906108c 15526
1bedd215 15527 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15528Set a temporary hardware assisted breakpoint.\n\
c906108c 15529Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15530so it will be deleted when hit.\n\
15531\n"
15532BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15533 set_cmd_completer (c, location_completer);
c906108c 15534
3947f654
SM
15535 cmd_list_element *enable_cmd
15536 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15537Enable all or some breakpoints.\n\
15538Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15539Give breakpoint numbers (separated by spaces) as arguments.\n\
15540With no subcommand, breakpoints are enabled until you command otherwise.\n\
15541This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15542With a subcommand you can enable temporarily."),
3947f654 15543 &enablelist, 1, &cmdlist);
c906108c 15544
3947f654 15545 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 15546
84951ab5 15547 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15548Enable all or some breakpoints.\n\
15549Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15550Give breakpoint numbers (separated by spaces) as arguments.\n\
15551This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15552May be abbreviated to simply \"enable\"."),
2f822da5 15553 &enablebreaklist, 1, &enablelist);
c906108c 15554
1a966eab 15555 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15556Enable some breakpoints for one hit.\n\
15557Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15558If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15559 &enablebreaklist);
15560
1a966eab 15561 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15562Enable some breakpoints and delete when hit.\n\
15563Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15564If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15565 &enablebreaklist);
15566
816338b5 15567 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15568Enable some breakpoints for COUNT hits.\n\
15569Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15570If a breakpoint is hit while enabled in this fashion,\n\
15571the count is decremented; when it reaches zero, the breakpoint is disabled."),
15572 &enablebreaklist);
15573
1a966eab 15574 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15575Enable some breakpoints and delete when hit.\n\
15576Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15577If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15578 &enablelist);
15579
1a966eab 15580 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15581Enable some breakpoints for one hit.\n\
15582Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15583If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15584 &enablelist);
15585
15586 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15587Enable some breakpoints for COUNT hits.\n\
15588Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15589If a breakpoint is hit while enabled in this fashion,\n\
15590the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15591 &enablelist);
15592
3947f654
SM
15593 cmd_list_element *disable_cmd
15594 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15595Disable all or some breakpoints.\n\
15596Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15597Arguments are breakpoint numbers with spaces in between.\n\
15598To disable all breakpoints, give no argument.\n\
64b9b334 15599A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
15600 &disablelist, 1, &cmdlist);
15601 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15602 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 15603
57b4f16e 15604 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15605Disable all or some breakpoints.\n\
15606Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15607Arguments are breakpoint numbers with spaces in between.\n\
15608To disable all breakpoints, give no argument.\n\
64b9b334 15609A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15610This command may be abbreviated \"disable\"."),
c906108c
SS
15611 &disablelist);
15612
3947f654
SM
15613 cmd_list_element *delete_cmd
15614 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15615Delete all or some breakpoints.\n\
15616Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15617Arguments are breakpoint numbers with spaces in between.\n\
15618To delete all breakpoints, give no argument.\n\
15619\n\
590042fc 15620Also a prefix command for deletion of other GDB objects."),
3947f654
SM
15621 &deletelist, 1, &cmdlist);
15622 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15623 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 15624
57b4f16e 15625 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15626Delete all or some breakpoints or auto-display expressions.\n\
15627Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15628Arguments are breakpoint numbers with spaces in between.\n\
15629To delete all breakpoints, give no argument.\n\
1a966eab 15630This command may be abbreviated \"delete\"."),
c906108c
SS
15631 &deletelist);
15632
3947f654
SM
15633 cmd_list_element *clear_cmd
15634 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15635Clear breakpoint at specified location.\n\
15636Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15637\n\
15638With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15639is executing in.\n"
89549d7f 15640"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15641See also the \"delete\" command which clears breakpoints by number."));
3947f654 15642 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 15643
3947f654
SM
15644 cmd_list_element *break_cmd
15645 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15646Set breakpoint at specified location.\n"
31e2b00f 15647BREAK_ARGS_HELP ("break")));
3947f654 15648 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 15649
3947f654
SM
15650 add_com_alias ("b", break_cmd, class_run, 1);
15651 add_com_alias ("br", break_cmd, class_run, 1);
15652 add_com_alias ("bre", break_cmd, class_run, 1);
15653 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 15654
c906108c
SS
15655 if (dbx_commands)
15656 {
1bedd215
AC
15657 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15658Break in function/address or break at a line in the current file."),
2f822da5 15659 &stoplist, 1, &cmdlist);
c5aa993b 15660 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15661 _("Break in function or address."), &stoplist);
c5aa993b 15662 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15663 _("Break at a line in the current file."), &stoplist);
11db9430 15664 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15665Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15666The \"Type\" column indicates one of:\n\
15667\tbreakpoint - normal breakpoint\n\
15668\twatchpoint - watchpoint\n\
15669The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15670the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15671breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15672address and file/line number respectively.\n\
15673\n\
15674Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15675are set to the address of the last breakpoint listed unless the command\n\
15676is prefixed with \"server \".\n\n\
c906108c 15677Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15678breakpoint set."));
c906108c
SS
15679 }
15680
e0f25bd9
SM
15681 cmd_list_element *info_breakpoints_cmd
15682 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15683Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15684The \"Type\" column indicates one of:\n\
15685\tbreakpoint - normal breakpoint\n\
15686\twatchpoint - watchpoint\n\
15687The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15688the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15689breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15690address and file/line number respectively.\n\
15691\n\
15692Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15693are set to the address of the last breakpoint listed unless the command\n\
15694is prefixed with \"server \".\n\n\
c906108c 15695Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15696breakpoint set."));
c906108c 15697
e0f25bd9 15698 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 15699
1a966eab
AC
15700 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15701Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15702The \"Type\" column indicates one of:\n\
15703\tbreakpoint - normal breakpoint\n\
15704\twatchpoint - watchpoint\n\
15705\tlongjmp - internal breakpoint used to step through longjmp()\n\
15706\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15707\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15708\tfinish - internal breakpoint used by the \"finish\" command\n\
15709The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15710the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15711breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15712address and file/line number respectively.\n\
15713\n\
15714Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15715are set to the address of the last breakpoint listed unless the command\n\
15716is prefixed with \"server \".\n\n\
c906108c 15717Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15718breakpoint set."),
c906108c
SS
15719 &maintenanceinfolist);
15720
3b6acaee 15721 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15722Set catchpoints to catch events."),
2f822da5 15723 &catch_cmdlist,
3b6acaee 15724 0/*allow-unknown*/, &cmdlist);
44feb3ce 15725
3b6acaee 15726 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15727Set temporary catchpoints to catch events."),
2f822da5 15728 &tcatch_cmdlist,
3b6acaee 15729 0/*allow-unknown*/, &cmdlist);
44feb3ce 15730
44feb3ce
TT
15731 add_catch_command ("fork", _("Catch calls to fork."),
15732 catch_fork_command_1,
dda83cd7 15733 NULL,
44feb3ce
TT
15734 (void *) (uintptr_t) catch_fork_permanent,
15735 (void *) (uintptr_t) catch_fork_temporary);
15736 add_catch_command ("vfork", _("Catch calls to vfork."),
15737 catch_fork_command_1,
dda83cd7 15738 NULL,
44feb3ce
TT
15739 (void *) (uintptr_t) catch_vfork_permanent,
15740 (void *) (uintptr_t) catch_vfork_temporary);
15741 add_catch_command ("exec", _("Catch calls to exec."),
15742 catch_exec_command_1,
dda83cd7 15743 NULL,
a96d9b2e
SDJ
15744 CATCH_PERMANENT,
15745 CATCH_TEMPORARY);
edcc5120
TT
15746 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15747Usage: catch load [REGEX]\n\
15748If REGEX is given, only stop for libraries matching the regular expression."),
15749 catch_load_command_1,
15750 NULL,
15751 CATCH_PERMANENT,
15752 CATCH_TEMPORARY);
15753 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15754Usage: catch unload [REGEX]\n\
15755If REGEX is given, only stop for libraries matching the regular expression."),
15756 catch_unload_command_1,
15757 NULL,
15758 CATCH_PERMANENT,
15759 CATCH_TEMPORARY);
c5aa993b 15760
a15a5258
AB
15761 const auto opts = make_watch_options_def_group (nullptr);
15762
15763 static const std::string watch_help = gdb::option::build_help (_("\
15764Set a watchpoint for EXPRESSION.\n\
15765Usage: watch [-location] EXPRESSION\n\
15766\n\
15767Options:\n\
15768%OPTIONS%\n\
15769\n\
c906108c 15770A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15771an expression changes."), opts);
15772 c = add_com ("watch", class_breakpoint, watch_command,
15773 watch_help.c_str ());
15774 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15775
15776 static const std::string rwatch_help = gdb::option::build_help (_("\
15777Set a read watchpoint for EXPRESSION.\n\
15778Usage: rwatch [-location] EXPRESSION\n\
15779\n\
15780Options:\n\
15781%OPTIONS%\n\
15782\n\
15783A read watchpoint stops execution of your program whenever the value of\n\
15784an expression is read."), opts);
15785 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15786 rwatch_help.c_str ());
15787 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15788
15789 static const std::string awatch_help = gdb::option::build_help (_("\
15790Set an access watchpoint for EXPRESSION.\n\
15791Usage: awatch [-location] EXPRESSION\n\
15792\n\
15793Options:\n\
15794%OPTIONS%\n\
15795\n\
15796An access watchpoint stops execution of your program whenever the value\n\
15797of an expression is either read or written."), opts);
15798 c = add_com ("awatch", class_breakpoint, awatch_command,
15799 awatch_help.c_str ());
15800 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15801
11db9430 15802 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15803Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15804
920d2a44
AC
15805 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15806 respond to changes - contrary to the description. */
85c07804
AC
15807 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15808 &can_use_hw_watchpoints, _("\
15809Set debugger's willingness to use watchpoint hardware."), _("\
15810Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15811If zero, gdb will not use hardware for new watchpoints, even if\n\
15812such is available. (However, any hardware watchpoints that were\n\
15813created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15814hardware.)"),
15815 NULL,
920d2a44 15816 show_can_use_hw_watchpoints,
85c07804 15817 &setlist, &showlist);
c906108c
SS
15818
15819 can_use_hw_watchpoints = 1;
fa8d40ab 15820
1042e4c0
SS
15821 /* Tracepoint manipulation commands. */
15822
3947f654
SM
15823 cmd_list_element *trace_cmd
15824 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15825Set a tracepoint at specified location.\n\
1042e4c0
SS
15826\n"
15827BREAK_ARGS_HELP ("trace") "\n\
15828Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 15829 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 15830
3947f654
SM
15831 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15832 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15833 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15834 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 15835
7a697b8d 15836 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15837Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15838\n"
15839BREAK_ARGS_HELP ("ftrace") "\n\
15840Do \"help tracepoints\" for info on other tracepoint commands."));
15841 set_cmd_completer (c, location_completer);
15842
0fb4aa4b 15843 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15844Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15845\n\
15846strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15847LOCATION may be a linespec, explicit, or address location (described below) \n\
15848or -m MARKER_ID.\n\n\
15849If a marker id is specified, probe the marker with that name. With\n\
15850no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15851Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15852This collects arbitrary user data passed in the probe point call to the\n\
15853tracing library. You can inspect it when analyzing the trace buffer,\n\
15854by printing the $_sdata variable like any other convenience variable.\n\
15855\n\
15856CONDITION is a boolean expression.\n\
89549d7f 15857\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15858Multiple tracepoints at one place are permitted, and useful if their\n\
15859conditions are different.\n\
0fb4aa4b
PA
15860\n\
15861Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15862Do \"help tracepoints\" for info on other tracepoint commands."));
15863 set_cmd_completer (c, location_completer);
15864
e0f25bd9
SM
15865 cmd_list_element *info_tracepoints_cmd
15866 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15867Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15868Convenience variable \"$tpnum\" contains the number of the\n\
15869last tracepoint set."));
15870
e0f25bd9 15871 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 15872
5e84b7ee
SM
15873 cmd_list_element *delete_tracepoints_cmd
15874 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
15875Delete specified tracepoints.\n\
15876Arguments are tracepoint numbers, separated by spaces.\n\
15877No argument means delete all tracepoints."),
15878 &deletelist);
5e84b7ee 15879 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
15880
15881 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15882Disable specified tracepoints.\n\
15883Arguments are tracepoint numbers, separated by spaces.\n\
15884No argument means disable all tracepoints."),
15885 &disablelist);
15886 deprecate_cmd (c, "disable");
15887
15888 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15889Enable specified tracepoints.\n\
15890Arguments are tracepoint numbers, separated by spaces.\n\
15891No argument means enable all tracepoints."),
15892 &enablelist);
15893 deprecate_cmd (c, "enable");
15894
15895 add_com ("passcount", class_trace, trace_pass_command, _("\
15896Set the passcount for a tracepoint.\n\
15897The trace will end when the tracepoint has been passed 'count' times.\n\
15898Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15899if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15900
0743fc83
TT
15901 add_basic_prefix_cmd ("save", class_breakpoint,
15902 _("Save breakpoint definitions as a script."),
2f822da5 15903 &save_cmdlist,
0743fc83 15904 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15905
15906 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15907Save current breakpoint definitions as a script.\n\
cce7e648 15908This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15909catchpoints, tracepoints). Use the 'source' command in another debug\n\
15910session to restore them."),
15911 &save_cmdlist);
15912 set_cmd_completer (c, filename_completer);
15913
3947f654
SM
15914 cmd_list_element *save_tracepoints_cmd
15915 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15916Save current tracepoint definitions as a script.\n\
6149aea9
PA
15917Use the 'source' command in another debug session to restore them."),
15918 &save_cmdlist);
3947f654 15919 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 15920
3947f654 15921 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
15922 deprecate_cmd (c, "save tracepoints");
15923
3b6acaee 15924 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15925Breakpoint specific settings.\n\
fa8d40ab 15926Configure various breakpoint-specific variables such as\n\
590042fc 15927pending breakpoint behavior."),
2f822da5 15928 &breakpoint_set_cmdlist,
3b6acaee
TT
15929 0/*allow-unknown*/, &setlist);
15930 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15931Breakpoint specific settings.\n\
fa8d40ab 15932Configure various breakpoint-specific variables such as\n\
590042fc 15933pending breakpoint behavior."),
2f822da5 15934 &breakpoint_show_cmdlist,
3b6acaee 15935 0/*allow-unknown*/, &showlist);
fa8d40ab 15936
7915a72c
AC
15937 add_setshow_auto_boolean_cmd ("pending", no_class,
15938 &pending_break_support, _("\
15939Set debugger's behavior regarding pending breakpoints."), _("\
15940Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15941If on, an unrecognized breakpoint location will cause gdb to create a\n\
15942pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15943an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15944user-query to see if a pending breakpoint should be created."),
2c5b56ce 15945 NULL,
920d2a44 15946 show_pending_break_support,
6e1d7d6c
AC
15947 &breakpoint_set_cmdlist,
15948 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15949
15950 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15951
15952 add_setshow_boolean_cmd ("auto-hw", no_class,
15953 &automatic_hardware_breakpoints, _("\
15954Set automatic usage of hardware breakpoints."), _("\
15955Show automatic usage of hardware breakpoints."), _("\
15956If set, the debugger will automatically use hardware breakpoints for\n\
15957breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15958a warning will be emitted for such breakpoints."),
15959 NULL,
15960 show_automatic_hardware_breakpoints,
15961 &breakpoint_set_cmdlist,
15962 &breakpoint_show_cmdlist);
74960c60 15963
a25a5a45
PA
15964 add_setshow_boolean_cmd ("always-inserted", class_support,
15965 &always_inserted_mode, _("\
74960c60
VP
15966Set mode for inserting breakpoints."), _("\
15967Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15968When this mode is on, breakpoints are inserted immediately as soon as\n\
15969they're created, kept inserted even when execution stops, and removed\n\
15970only when the user deletes them. When this mode is off (the default),\n\
15971breakpoints are inserted only when execution continues, and removed\n\
15972when execution stops."),
72d0e2c5
YQ
15973 NULL,
15974 &show_always_inserted_mode,
15975 &breakpoint_set_cmdlist,
15976 &breakpoint_show_cmdlist);
f1310107 15977
b775012e
LM
15978 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15979 condition_evaluation_enums,
15980 &condition_evaluation_mode_1, _("\
15981Set mode of breakpoint condition evaluation."), _("\
15982Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15983When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15984evaluated on the host's side by GDB. When it is set to \"target\",\n\
15985breakpoint conditions will be downloaded to the target (if the target\n\
15986supports such feature) and conditions will be evaluated on the target's side.\n\
15987If this is set to \"auto\" (default), this will be automatically set to\n\
15988\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 15989be set to \"host\"."),
b775012e
LM
15990 &set_condition_evaluation_mode,
15991 &show_condition_evaluation_mode,
15992 &breakpoint_set_cmdlist,
15993 &breakpoint_show_cmdlist);
15994
f1310107
TJB
15995 add_com ("break-range", class_breakpoint, break_range_command, _("\
15996Set a breakpoint for an address range.\n\
15997break-range START-LOCATION, END-LOCATION\n\
15998where START-LOCATION and END-LOCATION can be one of the following:\n\
15999 LINENUM, for that line in the current file,\n\
16000 FILE:LINENUM, for that line in that file,\n\
16001 +OFFSET, for that number of lines after the current line\n\
dda83cd7 16002 or the start of the range\n\
f1310107
TJB
16003 FUNCTION, for the first line in that function,\n\
16004 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16005 *ADDRESS, for the instruction at that address.\n\
16006\n\
16007The breakpoint will stop execution of the inferior whenever it executes\n\
16008an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16009range (including START-LOCATION and END-LOCATION)."));
16010
e7e0cddf 16011 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16012Set a dynamic printf at specified location.\n\
e7e0cddf 16013dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16014location may be a linespec, explicit, or address location.\n"
16015"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16016 set_cmd_completer (c, location_completer);
16017
16018 add_setshow_enum_cmd ("dprintf-style", class_support,
16019 dprintf_style_enums, &dprintf_style, _("\
16020Set the style of usage for dynamic printf."), _("\
16021Show the style of usage for dynamic printf."), _("\
16022This setting chooses how GDB will do a dynamic printf.\n\
16023If the value is \"gdb\", then the printing is done by GDB to its own\n\
16024console, as with the \"printf\" command.\n\
16025If the value is \"call\", the print is done by calling a function in your\n\
16026program; by default printf(), but you can choose a different function or\n\
16027output stream by setting dprintf-function and dprintf-channel."),
16028 update_dprintf_commands, NULL,
16029 &setlist, &showlist);
16030
16031 dprintf_function = xstrdup ("printf");
16032 add_setshow_string_cmd ("dprintf-function", class_support,
16033 &dprintf_function, _("\
590042fc
PW
16034Set the function to use for dynamic printf."), _("\
16035Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16036 update_dprintf_commands, NULL,
16037 &setlist, &showlist);
16038
16039 dprintf_channel = xstrdup ("");
16040 add_setshow_string_cmd ("dprintf-channel", class_support,
16041 &dprintf_channel, _("\
590042fc
PW
16042Set the channel to use for dynamic printf."), _("\
16043Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16044 update_dprintf_commands, NULL,
16045 &setlist, &showlist);
16046
d3ce09f5
SS
16047 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16048 &disconnected_dprintf, _("\
16049Set whether dprintf continues after GDB disconnects."), _("\
16050Show whether dprintf continues after GDB disconnects."), _("\
16051Use this to let dprintf commands continue to hit and produce output\n\
16052even if GDB disconnects or detaches from the target."),
16053 NULL,
16054 NULL,
16055 &setlist, &showlist);
16056
16057 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16058Target agent only formatted printing, like the C \"printf\" function.\n\
16059Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16060This supports most C printf format specifications, like %s, %d, etc.\n\
16061This is useful for formatted output in user-defined commands."));
d3ce09f5 16062
491144b5 16063 automatic_hardware_breakpoints = true;
f3b1572e 16064
c90e7d63
SM
16065 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16066 "breakpoint");
16067 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16068 "breakpoint");
c906108c 16069}
This page took 3.654527 seconds and 4 git commands to generate.