[gdb/testsuite] Fix info-types-c.exp
[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
PA
2700
2701 /* In some cases, we might not be able to insert a
2702 breakpoint in a shared library that has already been
2703 removed, but we have not yet processed the shlib unload
2704 event. Unfortunately, some targets that implement
076855f9
PA
2705 breakpoint insertion themselves can't tell why the
2706 breakpoint insertion failed (e.g., the remote target
2707 doesn't define error codes), so we must treat generic
2708 errors as memory errors. */
688fca4f
PA
2709 if (bp_excpt.reason == RETURN_ERROR
2710 && (bp_excpt.error == GENERIC_ERROR
2711 || bp_excpt.error == MEMORY_ERROR)
076855f9 2712 && bl->loc_type == bp_loc_software_breakpoint
08351840 2713 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2714 || shared_objfile_contains_address_p (bl->pspace,
2715 bl->address)))
879bfdc2 2716 {
4a64f543 2717 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2718 bl->shlib_disabled = 1;
76727919 2719 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2720 if (!*disabled_breaks)
2721 {
2722 fprintf_unfiltered (tmp_error_stream,
2723 "Cannot insert breakpoint %d.\n",
2724 bl->owner->number);
2725 fprintf_unfiltered (tmp_error_stream,
2726 "Temporarily disabling shared "
2727 "library breakpoints:\n");
2728 }
2729 *disabled_breaks = 1;
879bfdc2 2730 fprintf_unfiltered (tmp_error_stream,
35df4500 2731 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2732 return 0;
879bfdc2
DJ
2733 }
2734 else
879bfdc2 2735 {
35df4500 2736 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2737 {
0000e5cc 2738 *hw_breakpoint_error = 1;
688fca4f 2739 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Cannot insert hardware breakpoint %d%s",
2742 bl->owner->number,
688fca4f 2743 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2744 if (bp_excpt.message != NULL)
2745 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2746 bp_excpt.what ());
879bfdc2
DJ
2747 }
2748 else
2749 {
688fca4f 2750 if (bp_excpt.message == NULL)
0000e5cc 2751 {
1ccbe998 2752 std::string message
0000e5cc
PA
2753 = memory_error_message (TARGET_XFER_E_IO,
2754 bl->gdbarch, bl->address);
0000e5cc
PA
2755
2756 fprintf_unfiltered (tmp_error_stream,
2757 "Cannot insert breakpoint %d.\n"
2758 "%s\n",
1ccbe998 2759 bl->owner->number, message.c_str ());
0000e5cc
PA
2760 }
2761 else
2762 {
2763 fprintf_unfiltered (tmp_error_stream,
2764 "Cannot insert breakpoint %d: %s\n",
2765 bl->owner->number,
3d6e9d23 2766 bp_excpt.what ());
0000e5cc 2767 }
879bfdc2 2768 }
0000e5cc 2769 return 1;
879bfdc2
DJ
2770
2771 }
2772 }
2773 else
35df4500 2774 bl->inserted = 1;
879bfdc2 2775
0000e5cc 2776 return 0;
879bfdc2
DJ
2777 }
2778
35df4500 2779 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2780 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2781 watchpoints. It's not clear that it's necessary... */
35df4500 2782 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2783 {
0000e5cc
PA
2784 int val;
2785
77b06cd7
TJB
2786 gdb_assert (bl->owner->ops != NULL
2787 && bl->owner->ops->insert_location != NULL);
2788
2789 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2790
2791 /* If trying to set a read-watchpoint, and it turns out it's not
2792 supported, try emulating one with an access watchpoint. */
35df4500 2793 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2794 {
85d721b8
PA
2795 /* But don't try to insert it, if there's already another
2796 hw_access location that would be considered a duplicate
2797 of this one. */
48d7020b 2798 for (bp_location *loc : all_bp_locations ())
35df4500 2799 if (loc != bl
85d721b8 2800 && loc->watchpoint_type == hw_access
35df4500 2801 && watchpoint_locations_match (bl, loc))
85d721b8 2802 {
35df4500
TJB
2803 bl->duplicate = 1;
2804 bl->inserted = 1;
2805 bl->target_info = loc->target_info;
2806 bl->watchpoint_type = hw_access;
85d721b8
PA
2807 val = 0;
2808 break;
2809 }
2810
2811 if (val == 1)
2812 {
77b06cd7
TJB
2813 bl->watchpoint_type = hw_access;
2814 val = bl->owner->ops->insert_location (bl);
2815
2816 if (val)
2817 /* Back to the original value. */
2818 bl->watchpoint_type = hw_read;
85d721b8
PA
2819 }
2820 }
2821
35df4500 2822 bl->inserted = (val == 0);
879bfdc2
DJ
2823 }
2824
35df4500 2825 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2826 {
0000e5cc
PA
2827 int val;
2828
77b06cd7
TJB
2829 gdb_assert (bl->owner->ops != NULL
2830 && bl->owner->ops->insert_location != NULL);
2831
2832 val = bl->owner->ops->insert_location (bl);
2833 if (val)
2834 {
2835 bl->owner->enable_state = bp_disabled;
2836
2837 if (val == 1)
2838 warning (_("\
2839Error inserting catchpoint %d: Your system does not support this type\n\
2840of catchpoint."), bl->owner->number);
2841 else
2842 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2843 }
2844
2845 bl->inserted = (val == 0);
1640b821
DJ
2846
2847 /* We've already printed an error message if there was a problem
2848 inserting this catchpoint, and we've disabled the catchpoint,
2849 so just return success. */
2850 return 0;
879bfdc2
DJ
2851 }
2852
2853 return 0;
2854}
2855
6c95b8df
PA
2856/* This function is called when program space PSPACE is about to be
2857 deleted. It takes care of updating breakpoints to not reference
2858 PSPACE anymore. */
2859
2860void
2861breakpoint_program_space_exit (struct program_space *pspace)
2862{
6c95b8df 2863 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2864 for (breakpoint *b : all_breakpoints_safe ())
2865 if (b->pspace == pspace)
2866 delete_breakpoint (b);
6c95b8df
PA
2867
2868 /* Breakpoints set through other program spaces could have locations
2869 bound to PSPACE as well. Remove those. */
48d7020b 2870 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2871 {
2872 struct bp_location *tmp;
2873
2874 if (loc->pspace == pspace)
2875 {
2bdf28a0 2876 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2877 if (loc->owner->loc == loc)
2878 loc->owner->loc = loc->next;
2879 else
2880 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2881 if (tmp->next == loc)
2882 {
2883 tmp->next = loc->next;
2884 break;
2885 }
2886 }
2887 }
2888
2889 /* Now update the global location list to permanently delete the
2890 removed locations above. */
44702360 2891 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2892}
2893
74960c60
VP
2894/* Make sure all breakpoints are inserted in inferior.
2895 Throws exception on any error.
2896 A breakpoint that is already inserted won't be inserted
2897 again, so calling this function twice is safe. */
2898void
2899insert_breakpoints (void)
2900{
43892fdf 2901 for (breakpoint *bpt : all_breakpoints ())
74960c60 2902 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2903 {
2904 struct watchpoint *w = (struct watchpoint *) bpt;
2905
2906 update_watchpoint (w, 0 /* don't reparse. */);
2907 }
74960c60 2908
04086b45
PA
2909 /* Updating watchpoints creates new locations, so update the global
2910 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2911 ignore breakpoints_always_inserted_mode. Also,
2912 update_global_location_list tries to "upgrade" software
2913 breakpoints to hardware breakpoints to handle "set breakpoint
2914 auto-hw", so we need to call it even if we don't have new
2915 locations. */
04086b45 2916 update_global_location_list (UGLL_INSERT);
74960c60
VP
2917}
2918
b775012e
LM
2919/* This is used when we need to synch breakpoint conditions between GDB and the
2920 target. It is the case with deleting and disabling of breakpoints when using
2921 always-inserted mode. */
2922
2923static void
2924update_inserted_breakpoint_locations (void)
2925{
b775012e
LM
2926 int error_flag = 0;
2927 int val = 0;
2928 int disabled_breaks = 0;
2929 int hw_breakpoint_error = 0;
dd61ec5c 2930 int hw_bp_details_reported = 0;
b775012e 2931
d7e74731 2932 string_file tmp_error_stream;
b775012e
LM
2933
2934 /* Explicitly mark the warning -- this will only be printed if
2935 there was an error. */
d7e74731 2936 tmp_error_stream.puts ("Warning:\n");
b775012e 2937
5ed8105e 2938 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 2939
48d7020b 2940 for (bp_location *bl : all_bp_locations ())
b775012e
LM
2941 {
2942 /* We only want to update software breakpoints and hardware
2943 breakpoints. */
2944 if (!is_breakpoint (bl->owner))
2945 continue;
2946
2947 /* We only want to update locations that are already inserted
2948 and need updating. This is to avoid unwanted insertion during
2949 deletion of breakpoints. */
4daf1902 2950 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2951 continue;
2952
2953 switch_to_program_space_and_thread (bl->pspace);
2954
2955 /* For targets that support global breakpoints, there's no need
2956 to select an inferior to insert breakpoint to. In fact, even
2957 if we aren't attached to any process yet, we should still
2958 insert breakpoints. */
f5656ead 2959 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2960 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2961 continue;
2962
d7e74731 2963 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2964 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2965 if (val)
2966 error_flag = val;
2967 }
2968
2969 if (error_flag)
2970 {
223ffa71 2971 target_terminal::ours_for_output ();
b775012e
LM
2972 error_stream (tmp_error_stream);
2973 }
b775012e
LM
2974}
2975
c30eee59 2976/* Used when starting or continuing the program. */
c906108c 2977
74960c60
VP
2978static void
2979insert_breakpoint_locations (void)
c906108c 2980{
eacd795a 2981 int error_flag = 0;
c906108c 2982 int val = 0;
3fbb6ffa 2983 int disabled_breaks = 0;
81d0cc19 2984 int hw_breakpoint_error = 0;
dd61ec5c 2985 int hw_bp_error_explained_already = 0;
c906108c 2986
d7e74731
PA
2987 string_file tmp_error_stream;
2988
81d0cc19
GS
2989 /* Explicitly mark the warning -- this will only be printed if
2990 there was an error. */
d7e74731 2991 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2992
5ed8105e 2993 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2994
48d7020b 2995 for (bp_location *bl : all_bp_locations ())
879bfdc2 2996 {
b775012e 2997 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2998 continue;
2999
4a64f543
MS
3000 /* There is no point inserting thread-specific breakpoints if
3001 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3002 has BL->OWNER always non-NULL. */
35df4500 3003 if (bl->owner->thread != -1
5d5658a1 3004 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3005 continue;
3006
35df4500 3007 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3008
3009 /* For targets that support global breakpoints, there's no need
3010 to select an inferior to insert breakpoint to. In fact, even
3011 if we aren't attached to any process yet, we should still
3012 insert breakpoints. */
f5656ead 3013 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3014 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3015 continue;
3016
d7e74731 3017 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3018 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3019 if (val)
eacd795a 3020 error_flag = val;
879bfdc2 3021 }
c906108c 3022
4a64f543
MS
3023 /* If we failed to insert all locations of a watchpoint, remove
3024 them, as half-inserted watchpoint is of limited use. */
43892fdf 3025 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3026 {
3027 int some_failed = 0;
a5606eee
VP
3028
3029 if (!is_hardware_watchpoint (bpt))
3030 continue;
3031
d6b74ac4 3032 if (!breakpoint_enabled (bpt))
a5606eee 3033 continue;
74960c60
VP
3034
3035 if (bpt->disposition == disp_del_at_next_stop)
3036 continue;
a5606eee 3037
40cb8ca5 3038 for (bp_location *loc : bpt->locations ())
56710373 3039 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3040 {
3041 some_failed = 1;
3042 break;
3043 }
40cb8ca5 3044
a5606eee
VP
3045 if (some_failed)
3046 {
40cb8ca5 3047 for (bp_location *loc : bpt->locations ())
a5606eee 3048 if (loc->inserted)
834c0d03 3049 remove_breakpoint (loc);
a5606eee
VP
3050
3051 hw_breakpoint_error = 1;
d7e74731
PA
3052 tmp_error_stream.printf ("Could not insert "
3053 "hardware watchpoint %d.\n",
3054 bpt->number);
eacd795a 3055 error_flag = -1;
a5606eee
VP
3056 }
3057 }
3058
eacd795a 3059 if (error_flag)
81d0cc19
GS
3060 {
3061 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3062 message about possibly exhausted resources. */
dd61ec5c 3063 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3064 {
d7e74731 3065 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3066You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3067 }
223ffa71 3068 target_terminal::ours_for_output ();
81d0cc19
GS
3069 error_stream (tmp_error_stream);
3070 }
c906108c
SS
3071}
3072
c30eee59
TJB
3073/* Used when the program stops.
3074 Returns zero if successful, or non-zero if there was a problem
3075 removing a breakpoint location. */
3076
c906108c 3077int
fba45db2 3078remove_breakpoints (void)
c906108c 3079{
3a1bae8e 3080 int val = 0;
c906108c 3081
48d7020b 3082 for (bp_location *bl : all_bp_locations ())
1e4d1764 3083 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3084 val |= remove_breakpoint (bl);
01add95b 3085
3a1bae8e 3086 return val;
c906108c
SS
3087}
3088
49fa26b0
PA
3089/* When a thread exits, remove breakpoints that are related to
3090 that thread. */
3091
3092static void
3093remove_threaded_breakpoints (struct thread_info *tp, int silent)
3094{
1428b37a 3095 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3096 {
5d5658a1 3097 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3098 {
3099 b->disposition = disp_del_at_next_stop;
3100
3101 printf_filtered (_("\
43792cf0
PA
3102Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3103 b->number, print_thread_id (tp));
49fa26b0
PA
3104
3105 /* Hide it from the user. */
3106 b->number = 0;
3107 }
3108 }
3109}
3110
f3869b1a 3111/* See breakpoint.h. */
6c95b8df 3112
f3869b1a 3113void
00431a78 3114remove_breakpoints_inf (inferior *inf)
6c95b8df 3115{
6c95b8df 3116 int val;
6c95b8df 3117
48d7020b 3118 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3119 {
3120 if (bl->pspace != inf->pspace)
3121 continue;
6c95b8df 3122
01add95b
SM
3123 if (bl->inserted && !bl->target_info.persist)
3124 {
3125 val = remove_breakpoint (bl);
3126 if (val != 0)
3127 return;
3128 }
3129 }
6c95b8df
PA
3130}
3131
e58b0e63
PA
3132static int internal_breakpoint_number = -1;
3133
84f4c1fe
PM
3134/* Set the breakpoint number of B, depending on the value of INTERNAL.
3135 If INTERNAL is non-zero, the breakpoint number will be populated
3136 from internal_breakpoint_number and that variable decremented.
e5dd4106 3137 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3138 breakpoint_count and that value incremented. Internal breakpoints
3139 do not set the internal var bpnum. */
3140static void
3141set_breakpoint_number (int internal, struct breakpoint *b)
3142{
3143 if (internal)
3144 b->number = internal_breakpoint_number--;
3145 else
3146 {
3147 set_breakpoint_count (breakpoint_count + 1);
3148 b->number = breakpoint_count;
3149 }
3150}
3151
e62c965a 3152static struct breakpoint *
a6d9a66e 3153create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3154 CORE_ADDR address, enum bptype type,
c0a91b2b 3155 const struct breakpoint_ops *ops)
e62c965a 3156{
51abb421 3157 symtab_and_line sal;
e62c965a
PP
3158 sal.pc = address;
3159 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3160 sal.pspace = current_program_space;
e62c965a 3161
51abb421 3162 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3163 b->number = internal_breakpoint_number--;
3164 b->disposition = disp_donttouch;
3165
3166 return b;
3167}
3168
17450429
PP
3169static const char *const longjmp_names[] =
3170 {
3171 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3172 };
3173#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3174
3175/* Per-objfile data private to breakpoint.c. */
3176struct breakpoint_objfile_data
3177{
3178 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3179 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3180
3181 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3182 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3183
28106bc2 3184 /* True if we have looked for longjmp probes. */
43dce439 3185 int longjmp_searched = 0;
28106bc2 3186
45461e0d
SM
3187 /* SystemTap probe points for longjmp (if any). These are non-owning
3188 references. */
3189 std::vector<probe *> longjmp_probes;
28106bc2 3190
17450429 3191 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3192 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3193
3194 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3195 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3196
3197 /* True if we have looked for exception probes. */
43dce439 3198 int exception_searched = 0;
28106bc2 3199
45461e0d
SM
3200 /* SystemTap probe points for unwinding (if any). These are non-owning
3201 references. */
3202 std::vector<probe *> exception_probes;
17450429
PP
3203};
3204
51d3063a
TT
3205static const struct objfile_key<breakpoint_objfile_data>
3206 breakpoint_objfile_key;
17450429
PP
3207
3208/* Minimal symbol not found sentinel. */
3209static struct minimal_symbol msym_not_found;
3210
3211/* Returns TRUE if MSYM point to the "not found" sentinel. */
3212
3213static int
3214msym_not_found_p (const struct minimal_symbol *msym)
3215{
3216 return msym == &msym_not_found;
3217}
3218
3219/* Return per-objfile data needed by breakpoint.c.
3220 Allocate the data if necessary. */
3221
3222static struct breakpoint_objfile_data *
3223get_breakpoint_objfile_data (struct objfile *objfile)
3224{
3225 struct breakpoint_objfile_data *bp_objfile_data;
3226
51d3063a 3227 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3228 if (bp_objfile_data == NULL)
51d3063a 3229 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3230 return bp_objfile_data;
3231}
3232
e62c965a 3233static void
af02033e 3234create_overlay_event_breakpoint (void)
e62c965a 3235{
af02033e 3236 const char *const func_name = "_ovly_debug_event";
e62c965a 3237
2030c079 3238 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3239 {
3240 struct breakpoint *b;
17450429
PP
3241 struct breakpoint_objfile_data *bp_objfile_data;
3242 CORE_ADDR addr;
67994074 3243 struct explicit_location explicit_loc;
69de3c6a 3244
17450429
PP
3245 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3246
3b7344d5 3247 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3248 continue;
3249
3b7344d5 3250 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3251 {
3b7344d5 3252 struct bound_minimal_symbol m;
17450429
PP
3253
3254 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3255 if (m.minsym == NULL)
17450429
PP
3256 {
3257 /* Avoid future lookups in this objfile. */
3b7344d5 3258 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3259 continue;
3260 }
3261 bp_objfile_data->overlay_msym = m;
3262 }
e62c965a 3263
77e371c0 3264 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3265 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3266 bp_overlay_event,
06edf0c0 3267 &internal_breakpoint_ops);
67994074
KS
3268 initialize_explicit_location (&explicit_loc);
3269 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3270 b->location = new_explicit_location (&explicit_loc);
e62c965a 3271
69de3c6a 3272 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3273 {
3274 b->enable_state = bp_enabled;
3275 overlay_events_enabled = 1;
3276 }
69de3c6a
PP
3277 else
3278 {
dda83cd7
SM
3279 b->enable_state = bp_disabled;
3280 overlay_events_enabled = 0;
69de3c6a 3281 }
e62c965a 3282 }
e62c965a
PP
3283}
3284
2a7f6487
TV
3285/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3286 true if a breakpoint was installed. */
3287
3288static bool
3289create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3290{
2a7f6487
TV
3291 struct gdbarch *gdbarch = objfile->arch ();
3292 struct breakpoint_objfile_data *bp_objfile_data
3293 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3294
2a7f6487 3295 if (!bp_objfile_data->longjmp_searched)
94c93c35 3296 {
2a7f6487
TV
3297 std::vector<probe *> ret
3298 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3299
2a7f6487 3300 if (!ret.empty ())
94c93c35 3301 {
2a7f6487
TV
3302 /* We are only interested in checking one element. */
3303 probe *p = ret[0];
0fd8e87f 3304
2a7f6487
TV
3305 if (!p->can_evaluate_arguments ())
3306 {
3307 /* We cannot use the probe interface here,
3308 because it does not know how to evaluate
3309 arguments. */
3310 ret.clear ();
3311 }
3312 }
3313 bp_objfile_data->longjmp_probes = ret;
3314 bp_objfile_data->longjmp_searched = 1;
3315 }
0fd8e87f 3316
2a7f6487
TV
3317 if (bp_objfile_data->longjmp_probes.empty ())
3318 return false;
17450429 3319
2a7f6487
TV
3320 for (probe *p : bp_objfile_data->longjmp_probes)
3321 {
3322 struct breakpoint *b;
25f9533e 3323
2a7f6487
TV
3324 b = create_internal_breakpoint (gdbarch,
3325 p->get_relocated_address (objfile),
3326 bp_longjmp_master,
3327 &internal_breakpoint_ops);
3328 b->location = new_probe_location ("-probe-stap libc:longjmp");
3329 b->enable_state = bp_disabled;
3330 }
aed57c53 3331
2a7f6487
TV
3332 return true;
3333}
25f9533e 3334
2a7f6487
TV
3335/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3336 Return true if at least one breakpoint was installed. */
3337
3338static bool
3339create_longjmp_master_breakpoint_names (objfile *objfile)
3340{
3341 struct gdbarch *gdbarch = objfile->arch ();
3342 if (!gdbarch_get_longjmp_target_p (gdbarch))
3343 return false;
3344
3345 struct breakpoint_objfile_data *bp_objfile_data
3346 = get_breakpoint_objfile_data (objfile);
3347 unsigned int installed_bp = 0;
3348
3349 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3350 {
3351 struct breakpoint *b;
3352 const char *func_name;
3353 CORE_ADDR addr;
3354 struct explicit_location explicit_loc;
3355
3356 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3357 continue;
28106bc2 3358
2a7f6487
TV
3359 func_name = longjmp_names[i];
3360 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3361 {
3362 struct bound_minimal_symbol m;
3363
3364 m = lookup_minimal_symbol_text (func_name, objfile);
3365 if (m.minsym == NULL)
3366 {
3367 /* Prevent future lookups in this objfile. */
3368 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3369 continue;
3370 }
2a7f6487
TV
3371 bp_objfile_data->longjmp_msym[i] = m;
3372 }
28106bc2 3373
2a7f6487
TV
3374 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3375 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3376 &internal_breakpoint_ops);
3377 initialize_explicit_location (&explicit_loc);
3378 explicit_loc.function_name = ASTRDUP (func_name);
3379 b->location = new_explicit_location (&explicit_loc);
3380 b->enable_state = bp_disabled;
3381 installed_bp++;
3382 }
28106bc2 3383
2a7f6487
TV
3384 return installed_bp > 0;
3385}
0fd8e87f 3386
2a7f6487 3387/* Create a master longjmp breakpoint. */
17450429 3388
2a7f6487
TV
3389static void
3390create_longjmp_master_breakpoint (void)
3391{
3392 scoped_restore_current_program_space restore_pspace;
aed57c53 3393
2a7f6487
TV
3394 for (struct program_space *pspace : program_spaces)
3395 {
3396 set_current_program_space (pspace);
17450429 3397
2a7f6487
TV
3398 for (objfile *obj : current_program_space->objfiles ())
3399 {
3400 /* Skip separate debug object, it's handled in the loop below. */
3401 if (obj->separate_debug_objfile_backlink != nullptr)
3402 continue;
3403
3404 /* Try a probe kind breakpoint on main objfile. */
3405 if (create_longjmp_master_breakpoint_probe (obj))
3406 continue;
3407
3408 /* Try longjmp_names kind breakpoints on main and separate_debug
3409 objfiles. */
3410 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3411 if (create_longjmp_master_breakpoint_names (debug_objfile))
3412 break;
94c93c35
TT
3413 }
3414 }
0fd8e87f
UW
3415}
3416
af02033e 3417/* Create a master std::terminate breakpoint. */
aa7d318d 3418static void
af02033e 3419create_std_terminate_master_breakpoint (void)
aa7d318d 3420{
af02033e 3421 const char *const func_name = "std::terminate()";
aa7d318d 3422
5ed8105e 3423 scoped_restore_current_program_space restore_pspace;
aa7d318d 3424
94c93c35
TT
3425 for (struct program_space *pspace : program_spaces)
3426 {
3427 CORE_ADDR addr;
17450429 3428
94c93c35 3429 set_current_program_space (pspace);
17450429 3430
94c93c35
TT
3431 for (objfile *objfile : current_program_space->objfiles ())
3432 {
3433 struct breakpoint *b;
3434 struct breakpoint_objfile_data *bp_objfile_data;
3435 struct explicit_location explicit_loc;
aa7d318d 3436
94c93c35 3437 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3438
94c93c35
TT
3439 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3440 continue;
17450429 3441
94c93c35
TT
3442 if (bp_objfile_data->terminate_msym.minsym == NULL)
3443 {
3444 struct bound_minimal_symbol m;
17450429 3445
94c93c35
TT
3446 m = lookup_minimal_symbol (func_name, NULL, objfile);
3447 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3448 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3449 {
3450 /* Prevent future lookups in this objfile. */
3451 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3452 continue;
3453 }
3454 bp_objfile_data->terminate_msym = m;
3455 }
aa7d318d 3456
94c93c35
TT
3457 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3458 b = create_internal_breakpoint (objfile->arch (), addr,
3459 bp_std_terminate_master,
3460 &internal_breakpoint_ops);
3461 initialize_explicit_location (&explicit_loc);
3462 explicit_loc.function_name = ASTRDUP (func_name);
3463 b->location = new_explicit_location (&explicit_loc);
3464 b->enable_state = bp_disabled;
3465 }
3466 }
aa7d318d
TT
3467}
3468
1940319c
TV
3469/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3470 probe. Return true if a breakpoint was installed. */
186c406b 3471
1940319c
TV
3472static bool
3473create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3474{
1940319c
TV
3475 struct breakpoint *b;
3476 struct gdbarch *gdbarch;
3477 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3478
1940319c 3479 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3480
1940319c
TV
3481 /* We prefer the SystemTap probe point if it exists. */
3482 if (!bp_objfile_data->exception_searched)
3483 {
3484 std::vector<probe *> ret
3485 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3486
1940319c 3487 if (!ret.empty ())
28106bc2 3488 {
1940319c
TV
3489 /* We are only interested in checking one element. */
3490 probe *p = ret[0];
25f9533e 3491
1940319c 3492 if (!p->can_evaluate_arguments ())
25f9533e 3493 {
1940319c
TV
3494 /* We cannot use the probe interface here, because it does
3495 not know how to evaluate arguments. */
3496 ret.clear ();
25f9533e 3497 }
28106bc2 3498 }
1940319c
TV
3499 bp_objfile_data->exception_probes = ret;
3500 bp_objfile_data->exception_searched = 1;
3501 }
28106bc2 3502
1940319c
TV
3503 if (bp_objfile_data->exception_probes.empty ())
3504 return false;
45461e0d 3505
1940319c 3506 gdbarch = objfile->arch ();
28106bc2 3507
1940319c
TV
3508 for (probe *p : bp_objfile_data->exception_probes)
3509 {
3510 b = create_internal_breakpoint (gdbarch,
3511 p->get_relocated_address (objfile),
3512 bp_exception_master,
3513 &internal_breakpoint_ops);
3514 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3515 b->enable_state = bp_disabled;
3516 }
28106bc2 3517
1940319c
TV
3518 return true;
3519}
28106bc2 3520
1940319c
TV
3521/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3522 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3523
1940319c
TV
3524static bool
3525create_exception_master_breakpoint_hook (objfile *objfile)
3526{
3527 const char *const func_name = "_Unwind_DebugHook";
3528 struct breakpoint *b;
3529 struct gdbarch *gdbarch;
3530 struct breakpoint_objfile_data *bp_objfile_data;
3531 CORE_ADDR addr;
3532 struct explicit_location explicit_loc;
186c406b 3533
1940319c 3534 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3535
1940319c
TV
3536 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3537 return false;
17450429 3538
1940319c
TV
3539 gdbarch = objfile->arch ();
3540
3541 if (bp_objfile_data->exception_msym.minsym == NULL)
3542 {
3543 struct bound_minimal_symbol debug_hook;
3544
3545 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3546 if (debug_hook.minsym == NULL)
3547 {
3548 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3549 return false;
186c406b 3550 }
17450429 3551
1940319c
TV
3552 bp_objfile_data->exception_msym = debug_hook;
3553 }
3554
3555 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3556 addr = gdbarch_convert_from_func_ptr_addr
3557 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3558 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3559 &internal_breakpoint_ops);
3560 initialize_explicit_location (&explicit_loc);
3561 explicit_loc.function_name = ASTRDUP (func_name);
3562 b->location = new_explicit_location (&explicit_loc);
3563 b->enable_state = bp_disabled;
3564
3565 return true;
3566}
3567
3568/* Install a master breakpoint on the unwinder's debug hook. */
3569
3570static void
3571create_exception_master_breakpoint (void)
3572{
3573 for (objfile *obj : current_program_space->objfiles ())
3574 {
3575 /* Skip separate debug object. */
3576 if (obj->separate_debug_objfile_backlink)
3577 continue;
3578
3579 /* Try a probe kind breakpoint. */
3580 if (create_exception_master_breakpoint_probe (obj))
3581 continue;
3582
7c6944ab
TV
3583 /* Iterate over main and separate debug objects and try an
3584 _Unwind_DebugHook kind breakpoint. */
3585 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3586 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3587 break;
186c406b 3588 }
186c406b
TT
3589}
3590
9ef9e6a6
KS
3591/* Does B have a location spec? */
3592
3593static int
3594breakpoint_event_location_empty_p (const struct breakpoint *b)
3595{
d28cd78a 3596 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3597}
3598
c906108c 3599void
fba45db2 3600update_breakpoints_after_exec (void)
c906108c 3601{
25b22b0a
PA
3602 /* We're about to delete breakpoints from GDB's lists. If the
3603 INSERTED flag is true, GDB will try to lift the breakpoints by
3604 writing the breakpoints' "shadow contents" back into memory. The
3605 "shadow contents" are NOT valid after an exec, so GDB should not
3606 do that. Instead, the target is responsible from marking
3607 breakpoints out as soon as it detects an exec. We don't do that
3608 here instead, because there may be other attempts to delete
3609 breakpoints after detecting an exec and before reaching here. */
48d7020b 3610 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3611 if (bploc->pspace == current_program_space)
3612 gdb_assert (!bploc->inserted);
c906108c 3613
1428b37a 3614 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3615 {
3616 if (b->pspace != current_program_space)
c5aa993b 3617 continue;
c906108c 3618
01add95b
SM
3619 /* Solib breakpoints must be explicitly reset after an exec(). */
3620 if (b->type == bp_shlib_event)
3621 {
3622 delete_breakpoint (b);
3623 continue;
3624 }
4efc6507 3625
01add95b
SM
3626 /* JIT breakpoints must be explicitly reset after an exec(). */
3627 if (b->type == bp_jit_event)
3628 {
3629 delete_breakpoint (b);
3630 continue;
3631 }
c4093a6a 3632
01add95b
SM
3633 /* Thread event breakpoints must be set anew after an exec(),
3634 as must overlay event and longjmp master breakpoints. */
3635 if (b->type == bp_thread_event || b->type == bp_overlay_event
3636 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3637 || b->type == bp_exception_master)
3638 {
3639 delete_breakpoint (b);
3640 continue;
3641 }
c5aa993b 3642
01add95b
SM
3643 /* Step-resume breakpoints are meaningless after an exec(). */
3644 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3645 {
3646 delete_breakpoint (b);
3647 continue;
3648 }
7c16b83e 3649
01add95b
SM
3650 /* Just like single-step breakpoints. */
3651 if (b->type == bp_single_step)
3652 {
3653 delete_breakpoint (b);
3654 continue;
3655 }
611c83ae 3656
01add95b
SM
3657 /* Longjmp and longjmp-resume breakpoints are also meaningless
3658 after an exec. */
3659 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3660 || b->type == bp_longjmp_call_dummy
3661 || b->type == bp_exception || b->type == bp_exception_resume)
3662 {
3663 delete_breakpoint (b);
3664 continue;
3665 }
ce78b96d 3666
01add95b
SM
3667 if (b->type == bp_catchpoint)
3668 {
3669 /* For now, none of the bp_catchpoint breakpoints need to
3670 do anything at this point. In the future, if some of
3671 the catchpoints need to something, we will need to add
3672 a new method, and call this method from here. */
3673 continue;
3674 }
c5aa993b 3675
01add95b
SM
3676 /* bp_finish is a special case. The only way we ought to be able
3677 to see one of these when an exec() has happened, is if the user
3678 caught a vfork, and then said "finish". Ordinarily a finish just
3679 carries them to the call-site of the current callee, by setting
3680 a temporary bp there and resuming. But in this case, the finish
3681 will carry them entirely through the vfork & exec.
3682
3683 We don't want to allow a bp_finish to remain inserted now. But
3684 we can't safely delete it, 'cause finish_command has a handle to
3685 the bp on a bpstat, and will later want to delete it. There's a
3686 chance (and I've seen it happen) that if we delete the bp_finish
3687 here, that its storage will get reused by the time finish_command
3688 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3689 We really must allow finish_command to delete a bp_finish.
3690
3691 In the absence of a general solution for the "how do we know
3692 it's safe to delete something others may have handles to?"
3693 problem, what we'll do here is just uninsert the bp_finish, and
3694 let finish_command delete it.
3695
3696 (We know the bp_finish is "doomed" in the sense that it's
3697 momentary, and will be deleted as soon as finish_command sees
3698 the inferior stopped. So it doesn't matter that the bp's
3699 address is probably bogus in the new a.out, unlike e.g., the
3700 solib breakpoints.) */
3701
3702 if (b->type == bp_finish)
3703 {
3704 continue;
3705 }
3706
3707 /* Without a symbolic address, we have little hope of the
3708 pre-exec() address meaning the same thing in the post-exec()
3709 a.out. */
3710 if (breakpoint_event_location_empty_p (b))
3711 {
3712 delete_breakpoint (b);
3713 continue;
3714 }
3715 }
c906108c
SS
3716}
3717
3718int
d80ee84f 3719detach_breakpoints (ptid_t ptid)
c906108c 3720{
3a1bae8e 3721 int val = 0;
2989a365 3722 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3723 struct inferior *inf = current_inferior ();
c5aa993b 3724
e99b03dc 3725 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3726 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3727
6c95b8df 3728 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3729 inferior_ptid = ptid;
48d7020b 3730 for (bp_location *bl : all_bp_locations ())
01add95b
SM
3731 {
3732 if (bl->pspace != inf->pspace)
3733 continue;
3734
3735 /* This function must physically remove breakpoints locations
3736 from the specified ptid, without modifying the breakpoint
3737 package's state. Locations of type bp_loc_other are only
3738 maintained at GDB side. So, there is no need to remove
3739 these bp_loc_other locations. Moreover, removing these
3740 would modify the breakpoint package's state. */
3741 if (bl->loc_type == bp_loc_other)
3742 continue;
3743
3744 if (bl->inserted)
3745 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3746 }
d03285ec 3747
3a1bae8e 3748 return val;
c906108c
SS
3749}
3750
35df4500 3751/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3752 Note that this is used to detach breakpoints from a child fork.
3753 When we get here, the child isn't in the inferior list, and neither
3754 do we have objects to represent its address space --- we should
35df4500 3755 *not* look at bl->pspace->aspace here. */
6c95b8df 3756
c906108c 3757static int
b2b6a7da 3758remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3759{
3760 int val;
c5aa993b 3761
35df4500
TJB
3762 /* BL is never in moribund_locations by our callers. */
3763 gdb_assert (bl->owner != NULL);
2bdf28a0 3764
74960c60
VP
3765 /* The type of none suggests that owner is actually deleted.
3766 This should not ever happen. */
35df4500 3767 gdb_assert (bl->owner->type != bp_none);
0bde7532 3768
35df4500
TJB
3769 if (bl->loc_type == bp_loc_software_breakpoint
3770 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3771 {
c02f5703
MS
3772 /* "Normal" instruction breakpoint: either the standard
3773 trap-instruction bp (bp_breakpoint), or a
3774 bp_hardware_breakpoint. */
3775
3776 /* First check to see if we have to handle an overlay. */
3777 if (overlay_debugging == ovly_off
35df4500
TJB
3778 || bl->section == NULL
3779 || !(section_is_overlay (bl->section)))
c02f5703
MS
3780 {
3781 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3782
3783 /* If we're trying to uninsert a memory breakpoint that we
3784 know is set in a dynamic object that is marked
3785 shlib_disabled, then either the dynamic object was
3786 removed with "remove-symbol-file" or with
3787 "nosharedlibrary". In the former case, we don't know
3788 whether another dynamic object might have loaded over the
3789 breakpoint's address -- the user might well let us know
3790 about it next with add-symbol-file (the whole point of
d03de421 3791 add-symbol-file is letting the user manually maintain a
08351840
PA
3792 list of dynamically loaded objects). If we have the
3793 breakpoint's shadow memory, that is, this is a software
3794 breakpoint managed by GDB, check whether the breakpoint
3795 is still inserted in memory, to avoid overwriting wrong
3796 code with stale saved shadow contents. Note that HW
3797 breakpoints don't have shadow memory, as they're
3798 implemented using a mechanism that is not dependent on
3799 being able to modify the target's memory, and as such
3800 they should always be removed. */
3801 if (bl->shlib_disabled
3802 && bl->target_info.shadow_len != 0
3803 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3804 val = 0;
3805 else
73971819 3806 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3807 }
c906108c
SS
3808 else
3809 {
4a64f543 3810 /* This breakpoint is in an overlay section.
c02f5703
MS
3811 Did we set a breakpoint at the LMA? */
3812 if (!overlay_events_enabled)
3813 {
3814 /* Yes -- overlay event support is not active, so we
3815 should have set a breakpoint at the LMA. Remove it.
3816 */
c02f5703
MS
3817 /* Ignore any failures: if the LMA is in ROM, we will
3818 have already warned when we failed to insert it. */
35df4500
TJB
3819 if (bl->loc_type == bp_loc_hardware_breakpoint)
3820 target_remove_hw_breakpoint (bl->gdbarch,
3821 &bl->overlay_target_info);
c02f5703 3822 else
35df4500 3823 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3824 &bl->overlay_target_info,
3825 reason);
c02f5703
MS
3826 }
3827 /* Did we set a breakpoint at the VMA?
3828 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3829 if (bl->inserted)
c906108c 3830 {
c02f5703
MS
3831 /* Yes -- remove it. Previously we did not bother to
3832 remove the breakpoint if the section had been
3833 unmapped, but let's not rely on that being safe. We
3834 don't know what the overlay manager might do. */
aa67235e
UW
3835
3836 /* However, we should remove *software* breakpoints only
3837 if the section is still mapped, or else we overwrite
3838 wrong code with the saved shadow contents. */
348d480f
PA
3839 if (bl->loc_type == bp_loc_hardware_breakpoint
3840 || section_is_mapped (bl->section))
73971819 3841 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3842 else
3843 val = 0;
c906108c 3844 }
c02f5703
MS
3845 else
3846 {
3847 /* No -- not inserted, so no need to remove. No error. */
3848 val = 0;
3849 }
c906108c 3850 }
879d1e6b 3851
08351840
PA
3852 /* In some cases, we might not be able to remove a breakpoint in
3853 a shared library that has already been removed, but we have
3854 not yet processed the shlib unload event. Similarly for an
3855 unloaded add-symbol-file object - the user might not yet have
3856 had the chance to remove-symbol-file it. shlib_disabled will
3857 be set if the library/object has already been removed, but
3858 the breakpoint hasn't been uninserted yet, e.g., after
3859 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3860 always-inserted mode. */
076855f9 3861 if (val
08351840
PA
3862 && (bl->loc_type == bp_loc_software_breakpoint
3863 && (bl->shlib_disabled
3864 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3865 || shared_objfile_contains_address_p (bl->pspace,
3866 bl->address))))
879d1e6b
UW
3867 val = 0;
3868
c906108c
SS
3869 if (val)
3870 return val;
b2b6a7da 3871 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3872 }
35df4500 3873 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3874 {
77b06cd7
TJB
3875 gdb_assert (bl->owner->ops != NULL
3876 && bl->owner->ops->remove_location != NULL);
3877
b2b6a7da 3878 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3879 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3880
c906108c 3881 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3882 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3883 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3884 bl->owner->number);
c906108c 3885 }
35df4500 3886 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3887 && breakpoint_enabled (bl->owner)
3888 && !bl->duplicate)
ce78b96d 3889 {
77b06cd7
TJB
3890 gdb_assert (bl->owner->ops != NULL
3891 && bl->owner->ops->remove_location != NULL);
ce78b96d 3892
73971819 3893 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3894 if (val)
3895 return val;
77b06cd7 3896
b2b6a7da 3897 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3898 }
c906108c
SS
3899
3900 return 0;
3901}
3902
6c95b8df 3903static int
834c0d03 3904remove_breakpoint (struct bp_location *bl)
6c95b8df 3905{
35df4500
TJB
3906 /* BL is never in moribund_locations by our callers. */
3907 gdb_assert (bl->owner != NULL);
2bdf28a0 3908
6c95b8df
PA
3909 /* The type of none suggests that owner is actually deleted.
3910 This should not ever happen. */
35df4500 3911 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3912
5ed8105e 3913 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3914
35df4500 3915 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3916
5ed8105e 3917 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3918}
3919
c906108c
SS
3920/* Clear the "inserted" flag in all breakpoints. */
3921
25b22b0a 3922void
fba45db2 3923mark_breakpoints_out (void)
c906108c 3924{
48d7020b 3925 for (bp_location *bl : all_bp_locations ())
66c4b3e8 3926 if (bl->pspace == current_program_space)
35df4500 3927 bl->inserted = 0;
c906108c
SS
3928}
3929
53a5351d
JM
3930/* Clear the "inserted" flag in all breakpoints and delete any
3931 breakpoints which should go away between runs of the program.
c906108c
SS
3932
3933 Plus other such housekeeping that has to be done for breakpoints
3934 between runs.
3935
53a5351d
JM
3936 Note: this function gets called at the end of a run (by
3937 generic_mourn_inferior) and when a run begins (by
4a64f543 3938 init_wait_for_inferior). */
c906108c
SS
3939
3940
3941
3942void
fba45db2 3943breakpoint_init_inferior (enum inf_context context)
c906108c 3944{
6c95b8df 3945 struct program_space *pspace = current_program_space;
c906108c 3946
50c71eaf
PA
3947 /* If breakpoint locations are shared across processes, then there's
3948 nothing to do. */
f5656ead 3949 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3950 return;
3951
1a853c52 3952 mark_breakpoints_out ();
075f6582 3953
1428b37a 3954 for (breakpoint *b : all_breakpoints_safe ())
01add95b
SM
3955 {
3956 if (b->loc && b->loc->pspace != pspace)
3957 continue;
6c95b8df 3958
01add95b
SM
3959 switch (b->type)
3960 {
3961 case bp_call_dummy:
3962 case bp_longjmp_call_dummy:
c906108c 3963
01add95b
SM
3964 /* If the call dummy breakpoint is at the entry point it will
3965 cause problems when the inferior is rerun, so we better get
3966 rid of it. */
ab92d69b 3967
01add95b 3968 case bp_watchpoint_scope:
ab92d69b 3969
01add95b 3970 /* Also get rid of scope breakpoints. */
ab92d69b 3971
01add95b 3972 case bp_shlib_event:
ab92d69b 3973
01add95b
SM
3974 /* Also remove solib event breakpoints. Their addresses may
3975 have changed since the last time we ran the program.
3976 Actually we may now be debugging against different target;
3977 and so the solib backend that installed this breakpoint may
3978 not be used in by the target. E.g.,
ab92d69b 3979
01add95b
SM
3980 (gdb) file prog-linux
3981 (gdb) run # native linux target
3982 ...
3983 (gdb) kill
3984 (gdb) file prog-win.exe
3985 (gdb) tar rem :9999 # remote Windows gdbserver.
3986 */
c906108c 3987
01add95b 3988 case bp_step_resume:
f59f708a 3989
01add95b 3990 /* Also remove step-resume breakpoints. */
f59f708a 3991
01add95b 3992 case bp_single_step:
7c16b83e 3993
01add95b 3994 /* Also remove single-step breakpoints. */
7c16b83e 3995
01add95b
SM
3996 delete_breakpoint (b);
3997 break;
c906108c 3998
01add95b
SM
3999 case bp_watchpoint:
4000 case bp_hardware_watchpoint:
4001 case bp_read_watchpoint:
4002 case bp_access_watchpoint:
4003 {
4004 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4005
01add95b
SM
4006 /* Likewise for watchpoints on local expressions. */
4007 if (w->exp_valid_block != NULL)
4008 delete_breakpoint (b);
4009 else
4010 {
4011 /* Get rid of existing locations, which are no longer
4012 valid. New ones will be created in
4013 update_watchpoint, when the inferior is restarted.
4014 The next update_global_location_list call will
4015 garbage collect them. */
4016 b->loc = NULL;
4017
4018 if (context == inf_starting)
4019 {
4020 /* Reset val field to force reread of starting value in
4021 insert_breakpoints. */
4022 w->val.reset (nullptr);
4023 w->val_valid = false;
4024 }
4025 }
4026 }
4027 break;
4028 default:
4029 break;
3a5c3e22 4030 }
01add95b 4031 }
1c5cfe86
PA
4032
4033 /* Get rid of the moribund locations. */
1123588c 4034 for (bp_location *bl : moribund_locations)
35df4500 4035 decref_bp_location (&bl);
1123588c 4036 moribund_locations.clear ();
c906108c
SS
4037}
4038
6c95b8df
PA
4039/* These functions concern about actual breakpoints inserted in the
4040 target --- to e.g. check if we need to do decr_pc adjustment or if
4041 we need to hop over the bkpt --- so we check for address space
4042 match, not program space. */
4043
c2c6d25f
JM
4044/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4045 exists at PC. It returns ordinary_breakpoint_here if it's an
4046 ordinary breakpoint, or permanent_breakpoint_here if it's a
4047 permanent breakpoint.
4048 - When continuing from a location with an ordinary breakpoint, we
4049 actually single step once before calling insert_breakpoints.
e5dd4106 4050 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4051 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4052 the target, to advance the PC past the breakpoint. */
c906108c 4053
c2c6d25f 4054enum breakpoint_here
accd0bcd 4055breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4056{
c2c6d25f 4057 int any_breakpoint_here = 0;
c906108c 4058
48d7020b 4059 for (bp_location *bl : all_bp_locations ())
075f6582 4060 {
35df4500
TJB
4061 if (bl->loc_type != bp_loc_software_breakpoint
4062 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4063 continue;
4064
f1310107 4065 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4066 if ((breakpoint_enabled (bl->owner)
1a853c52 4067 || bl->permanent)
f1310107 4068 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4069 {
4070 if (overlay_debugging
35df4500
TJB
4071 && section_is_overlay (bl->section)
4072 && !section_is_mapped (bl->section))
075f6582 4073 continue; /* unmapped overlay -- can't be a match */
1a853c52 4074 else if (bl->permanent)
075f6582
DJ
4075 return permanent_breakpoint_here;
4076 else
4077 any_breakpoint_here = 1;
4078 }
4079 }
c906108c 4080
f486487f 4081 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4082}
4083
d35ae833
PA
4084/* See breakpoint.h. */
4085
4086int
accd0bcd 4087breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4088 CORE_ADDR addr, ULONGEST len)
4089{
48d7020b 4090 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4091 {
4092 if (bl->loc_type != bp_loc_software_breakpoint
4093 && bl->loc_type != bp_loc_hardware_breakpoint)
4094 continue;
4095
4096 if ((breakpoint_enabled (bl->owner)
4097 || bl->permanent)
4098 && breakpoint_location_address_range_overlap (bl, aspace,
4099 addr, len))
4100 {
4101 if (overlay_debugging
4102 && section_is_overlay (bl->section)
4103 && !section_is_mapped (bl->section))
4104 {
4105 /* Unmapped overlay -- can't be a match. */
4106 continue;
4107 }
4108
4109 return 1;
4110 }
4111 }
4112
4113 return 0;
4114}
4115
1c5cfe86
PA
4116/* Return true if there's a moribund breakpoint at PC. */
4117
4118int
accd0bcd 4119moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4120{
1123588c 4121 for (bp_location *loc : moribund_locations)
f1310107 4122 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4123 return 1;
4124
4125 return 0;
4126}
c2c6d25f 4127
f7ce857f
PA
4128/* Returns non-zero iff BL is inserted at PC, in address space
4129 ASPACE. */
4130
4131static int
4132bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4133 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4134{
4135 if (bl->inserted
4136 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4137 aspace, pc))
4138 {
4139 if (overlay_debugging
4140 && section_is_overlay (bl->section)
4141 && !section_is_mapped (bl->section))
4142 return 0; /* unmapped overlay -- can't be a match */
4143 else
4144 return 1;
4145 }
4146 return 0;
4147}
4148
a1fd2fa5 4149/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4150
4151int
accd0bcd 4152breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4153{
e0d9a270 4154 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4155 {
35df4500
TJB
4156 if (bl->loc_type != bp_loc_software_breakpoint
4157 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4158 continue;
4159
f7ce857f
PA
4160 if (bp_location_inserted_here_p (bl, aspace, pc))
4161 return 1;
c5aa993b 4162 }
c36b740a
VP
4163 return 0;
4164}
4165
a1fd2fa5
PA
4166/* This function returns non-zero iff there is a software breakpoint
4167 inserted at PC. */
c36b740a
VP
4168
4169int
accd0bcd 4170software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4171 CORE_ADDR pc)
4fa8626c 4172{
e0d9a270 4173 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4174 {
35df4500 4175 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4176 continue;
4177
f7ce857f
PA
4178 if (bp_location_inserted_here_p (bl, aspace, pc))
4179 return 1;
4fa8626c
DJ
4180 }
4181
4182 return 0;
9c02b525
PA
4183}
4184
4185/* See breakpoint.h. */
4186
4187int
accd0bcd 4188hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4189 CORE_ADDR pc)
4190{
e0d9a270 4191 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4192 {
9c02b525
PA
4193 if (bl->loc_type != bp_loc_hardware_breakpoint)
4194 continue;
4195
4196 if (bp_location_inserted_here_p (bl, aspace, pc))
4197 return 1;
4198 }
4199
4200 return 0;
4fa8626c
DJ
4201}
4202
9093389c 4203int
accd0bcd 4204hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4205 CORE_ADDR addr, ULONGEST len)
4206{
43892fdf 4207 for (breakpoint *bpt : all_breakpoints ())
9093389c 4208 {
9093389c
PA
4209 if (bpt->type != bp_hardware_watchpoint
4210 && bpt->type != bp_access_watchpoint)
4211 continue;
4212
4213 if (!breakpoint_enabled (bpt))
4214 continue;
4215
40cb8ca5 4216 for (bp_location *loc : bpt->locations ())
9093389c
PA
4217 if (loc->pspace->aspace == aspace && loc->inserted)
4218 {
4219 CORE_ADDR l, h;
4220
4221 /* Check for intersection. */
768adc05
PA
4222 l = std::max<CORE_ADDR> (loc->address, addr);
4223 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4224 if (l < h)
4225 return 1;
4226 }
4227 }
4228 return 0;
4229}
c5aa993b 4230
f2478a7e 4231/* See breakpoint.h. */
c906108c 4232
f2478a7e
SM
4233bool
4234is_catchpoint (struct breakpoint *b)
c906108c 4235{
f2478a7e 4236 return (b->type == bp_catchpoint);
c906108c
SS
4237}
4238
c906108c
SS
4239/* Clear a bpstat so that it says we are not at any breakpoint.
4240 Also free any storage that is part of a bpstat. */
4241
4242void
fba45db2 4243bpstat_clear (bpstat *bsp)
c906108c
SS
4244{
4245 bpstat p;
4246 bpstat q;
4247
4248 if (bsp == 0)
4249 return;
4250 p = *bsp;
4251 while (p != NULL)
4252 {
4253 q = p->next;
04afa70c 4254 delete p;
c906108c
SS
4255 p = q;
4256 }
4257 *bsp = NULL;
4258}
4259
04afa70c
TT
4260bpstats::bpstats (const bpstats &other)
4261 : next (NULL),
4262 bp_location_at (other.bp_location_at),
4263 breakpoint_at (other.breakpoint_at),
4264 commands (other.commands),
04afa70c
TT
4265 print (other.print),
4266 stop (other.stop),
4267 print_it (other.print_it)
4268{
850645cf
TT
4269 if (other.old_val != NULL)
4270 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4271}
4272
c906108c
SS
4273/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4274 is part of the bpstat is copied as well. */
4275
4276bpstat
fba45db2 4277bpstat_copy (bpstat bs)
c906108c
SS
4278{
4279 bpstat p = NULL;
4280 bpstat tmp;
4281 bpstat retval = NULL;
4282
4283 if (bs == NULL)
4284 return bs;
4285
4286 for (; bs != NULL; bs = bs->next)
4287 {
04afa70c 4288 tmp = new bpstats (*bs);
31cc81e9 4289
c906108c
SS
4290 if (p == NULL)
4291 /* This is the first thing in the chain. */
4292 retval = tmp;
4293 else
4294 p->next = tmp;
4295 p = tmp;
4296 }
4297 p->next = NULL;
4298 return retval;
4299}
4300
4a64f543 4301/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4302
4303bpstat
fba45db2 4304bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4305{
c5aa993b
JM
4306 if (bsp == NULL)
4307 return NULL;
c906108c 4308
c5aa993b
JM
4309 for (; bsp != NULL; bsp = bsp->next)
4310 {
f431efe5 4311 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4312 return bsp;
4313 }
c906108c
SS
4314 return NULL;
4315}
4316
ab04a2af
TT
4317/* See breakpoint.h. */
4318
4c462cb0 4319bool
427cd150 4320bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4321{
ab04a2af
TT
4322 for (; bsp != NULL; bsp = bsp->next)
4323 {
427cd150
TT
4324 if (bsp->breakpoint_at == NULL)
4325 {
4326 /* A moribund location can never explain a signal other than
4327 GDB_SIGNAL_TRAP. */
4328 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4329 return true;
427cd150
TT
4330 }
4331 else
47591c29
PA
4332 {
4333 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4334 sig))
4c462cb0 4335 return true;
47591c29 4336 }
ab04a2af
TT
4337 }
4338
4c462cb0 4339 return false;
ab04a2af
TT
4340}
4341
4a64f543
MS
4342/* Put in *NUM the breakpoint number of the first breakpoint we are
4343 stopped at. *BSP upon return is a bpstat which points to the
4344 remaining breakpoints stopped at (but which is not guaranteed to be
4345 good for anything but further calls to bpstat_num).
4346
8671a17b
PA
4347 Return 0 if passed a bpstat which does not indicate any breakpoints.
4348 Return -1 if stopped at a breakpoint that has been deleted since
4349 we set it.
4350 Return 1 otherwise. */
c906108c
SS
4351
4352int
8671a17b 4353bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4354{
4355 struct breakpoint *b;
4356
4357 if ((*bsp) == NULL)
4358 return 0; /* No more breakpoint values */
8671a17b 4359
4a64f543
MS
4360 /* We assume we'll never have several bpstats that correspond to a
4361 single breakpoint -- otherwise, this function might return the
4362 same number more than once and this will look ugly. */
f431efe5 4363 b = (*bsp)->breakpoint_at;
8671a17b
PA
4364 *bsp = (*bsp)->next;
4365 if (b == NULL)
4366 return -1; /* breakpoint that's been deleted since */
4367
4368 *num = b->number; /* We have its number */
4369 return 1;
c906108c
SS
4370}
4371
e93ca019 4372/* See breakpoint.h. */
c906108c
SS
4373
4374void
e93ca019 4375bpstat_clear_actions (void)
c906108c 4376{
e93ca019
JK
4377 bpstat bs;
4378
00431a78 4379 if (inferior_ptid == null_ptid)
e93ca019
JK
4380 return;
4381
00431a78 4382 thread_info *tp = inferior_thread ();
e93ca019 4383 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4384 {
d1b0a7bf 4385 bs->commands = NULL;
850645cf 4386 bs->old_val.reset (nullptr);
c906108c
SS
4387 }
4388}
4389
f3b1572e
PA
4390/* Called when a command is about to proceed the inferior. */
4391
4392static void
4393breakpoint_about_to_proceed (void)
4394{
d7e15655 4395 if (inferior_ptid != null_ptid)
f3b1572e
PA
4396 {
4397 struct thread_info *tp = inferior_thread ();
4398
4399 /* Allow inferior function calls in breakpoint commands to not
4400 interrupt the command list. When the call finishes
4401 successfully, the inferior will be standing at the same
4402 breakpoint as if nothing happened. */
16c381f0 4403 if (tp->control.in_infcall)
f3b1572e
PA
4404 return;
4405 }
4406
4407 breakpoint_proceeded = 1;
4408}
4409
abf85f46
JK
4410/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4411 or its equivalent. */
4412
4413static int
4414command_line_is_silent (struct command_line *cmd)
4415{
4f45d445 4416 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4417}
4418
4a64f543
MS
4419/* Execute all the commands associated with all the breakpoints at
4420 this location. Any of these commands could cause the process to
4421 proceed beyond this point, etc. We look out for such changes by
4422 checking the global "breakpoint_proceeded" after each command.
c906108c 4423
347bddb7
PA
4424 Returns true if a breakpoint command resumed the inferior. In that
4425 case, it is the caller's responsibility to recall it again with the
4426 bpstat of the current thread. */
4427
4428static int
4429bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4430{
4431 bpstat bs;
347bddb7 4432 int again = 0;
c906108c
SS
4433
4434 /* Avoid endless recursion if a `source' command is contained
4435 in bs->commands. */
4436 if (executing_breakpoint_commands)
347bddb7 4437 return 0;
c906108c 4438
81b1e71c
TT
4439 scoped_restore save_executing
4440 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4441
1ac32117 4442 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4443
4a64f543 4444 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4445 bs = *bsp;
4446
4447 breakpoint_proceeded = 0;
4448 for (; bs != NULL; bs = bs->next)
4449 {
d1b0a7bf 4450 struct command_line *cmd = NULL;
6c50ab1c
JB
4451
4452 /* Take ownership of the BSP's command tree, if it has one.
4453
dda83cd7
SM
4454 The command tree could legitimately contain commands like
4455 'step' and 'next', which call clear_proceed_status, which
4456 frees stop_bpstat's command tree. To make sure this doesn't
4457 free the tree we're executing out from under us, we need to
4458 take ownership of the tree ourselves. Since a given bpstat's
4459 commands are only executed once, we don't need to copy it; we
4460 can clear the pointer in the bpstat, and make sure we free
4461 the tree when we're done. */
d1b0a7bf 4462 counted_command_line ccmd = bs->commands;
9add0f1b 4463 bs->commands = NULL;
d1b0a7bf
TT
4464 if (ccmd != NULL)
4465 cmd = ccmd.get ();
abf85f46
JK
4466 if (command_line_is_silent (cmd))
4467 {
4468 /* The action has been already done by bpstat_stop_status. */
4469 cmd = cmd->next;
4470 }
6c50ab1c 4471
c906108c
SS
4472 while (cmd != NULL)
4473 {
4474 execute_control_command (cmd);
4475
4476 if (breakpoint_proceeded)
4477 break;
4478 else
4479 cmd = cmd->next;
4480 }
6c50ab1c 4481
c906108c 4482 if (breakpoint_proceeded)
32c1e744 4483 {
cb814510 4484 if (current_ui->async)
347bddb7
PA
4485 /* If we are in async mode, then the target might be still
4486 running, not stopped at any breakpoint, so nothing for
4487 us to do here -- just return to the event loop. */
4488 ;
32c1e744
VP
4489 else
4490 /* In sync mode, when execute_control_command returns
4491 we're already standing on the next breakpoint.
347bddb7
PA
4492 Breakpoint commands for that stop were not run, since
4493 execute_command does not run breakpoint commands --
4494 only command_line_handler does, but that one is not
4495 involved in execution of breakpoint commands. So, we
4496 can now execute breakpoint commands. It should be
4497 noted that making execute_command do bpstat actions is
4498 not an option -- in this case we'll have recursive
4499 invocation of bpstat for each breakpoint with a
4500 command, and can easily blow up GDB stack. Instead, we
4501 return true, which will trigger the caller to recall us
4502 with the new stop_bpstat. */
4503 again = 1;
4504 break;
32c1e744 4505 }
c906108c 4506 }
347bddb7
PA
4507 return again;
4508}
4509
00431a78
PA
4510/* Helper for bpstat_do_actions. Get the current thread, if there's
4511 one, is alive and has execution. Return NULL otherwise. */
4512
4513static thread_info *
4514get_bpstat_thread ()
4515{
55f6301a 4516 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4517 return NULL;
4518
4519 thread_info *tp = inferior_thread ();
4520 if (tp->state == THREAD_EXITED || tp->executing)
4521 return NULL;
4522 return tp;
4523}
4524
347bddb7
PA
4525void
4526bpstat_do_actions (void)
4527{
694c6bf5 4528 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4529 thread_info *tp;
353d1d73 4530
347bddb7 4531 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4532 while ((tp = get_bpstat_thread ()) != NULL)
4533 {
4534 /* Since in sync mode, bpstat_do_actions may resume the
4535 inferior, and only return when it is stopped at the next
4536 breakpoint, we keep doing breakpoint actions until it returns
4537 false to indicate the inferior was not resumed. */
4538 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4539 break;
4540 }
353d1d73 4541
694c6bf5 4542 cleanup_if_error.release ();
c906108c
SS
4543}
4544
fa4727a6
DJ
4545/* Print out the (old or new) value associated with a watchpoint. */
4546
4547static void
4548watchpoint_value_print (struct value *val, struct ui_file *stream)
4549{
4550 if (val == NULL)
7f6aba03 4551 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4552 else
79a45b7d
TT
4553 {
4554 struct value_print_options opts;
4555 get_user_print_options (&opts);
4556 value_print (val, stream, &opts);
4557 }
fa4727a6
DJ
4558}
4559
f303dbd6
PA
4560/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4561 debugging multiple threads. */
4562
4563void
4564maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4565{
112e8700 4566 if (uiout->is_mi_like_p ())
f303dbd6
PA
4567 return;
4568
112e8700 4569 uiout->text ("\n");
f303dbd6
PA
4570
4571 if (show_thread_that_caused_stop ())
4572 {
4573 const char *name;
4574 struct thread_info *thr = inferior_thread ();
4575
112e8700 4576 uiout->text ("Thread ");
33eca680 4577 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4578
4579 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4580 if (name != NULL)
4581 {
112e8700 4582 uiout->text (" \"");
33eca680 4583 uiout->field_string ("name", name);
112e8700 4584 uiout->text ("\"");
f303dbd6
PA
4585 }
4586
112e8700 4587 uiout->text (" hit ");
f303dbd6
PA
4588 }
4589}
4590
e514a9d6 4591/* Generic routine for printing messages indicating why we
4a64f543 4592 stopped. The behavior of this function depends on the value
e514a9d6
JM
4593 'print_it' in the bpstat structure. Under some circumstances we
4594 may decide not to print anything here and delegate the task to
4a64f543 4595 normal_stop(). */
e514a9d6
JM
4596
4597static enum print_stop_action
4598print_bp_stop_message (bpstat bs)
4599{
4600 switch (bs->print_it)
4601 {
4602 case print_it_noop:
4a64f543 4603 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4604 return PRINT_UNKNOWN;
4605 break;
4606
4607 case print_it_done:
4608 /* We still want to print the frame, but we already printed the
dda83cd7 4609 relevant messages. */
e514a9d6
JM
4610 return PRINT_SRC_AND_LOC;
4611 break;
4612
4613 case print_it_normal:
4f8d1dc6 4614 {
f431efe5
PA
4615 struct breakpoint *b = bs->breakpoint_at;
4616
1a6a67de
TJB
4617 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4618 which has since been deleted. */
4619 if (b == NULL)
4620 return PRINT_UNKNOWN;
4621
348d480f
PA
4622 /* Normal case. Call the breakpoint's print_it method. */
4623 return b->ops->print_it (bs);
4f8d1dc6 4624 }
348d480f 4625 break;
3086aeae 4626
e514a9d6 4627 default:
8e65ff28 4628 internal_error (__FILE__, __LINE__,
e2e0b3e5 4629 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4630 break;
c906108c 4631 }
c906108c
SS
4632}
4633
edcc5120
TT
4634/* A helper function that prints a shared library stopped event. */
4635
4636static void
4637print_solib_event (int is_catchpoint)
4638{
6fb16ce6 4639 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4640 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4641
4642 if (!is_catchpoint)
4643 {
4644 if (any_added || any_deleted)
112e8700 4645 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4646 else
112e8700
SM
4647 current_uiout->text (_("Stopped due to shared library event (no "
4648 "libraries added or removed)\n"));
edcc5120
TT
4649 }
4650
112e8700
SM
4651 if (current_uiout->is_mi_like_p ())
4652 current_uiout->field_string ("reason",
4653 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4654
4655 if (any_deleted)
4656 {
112e8700 4657 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4658 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4659 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4660 {
6fb16ce6
SM
4661 const std::string &name = current_program_space->deleted_solibs[ix];
4662
edcc5120 4663 if (ix > 0)
112e8700
SM
4664 current_uiout->text (" ");
4665 current_uiout->field_string ("library", name);
4666 current_uiout->text ("\n");
edcc5120 4667 }
edcc5120
TT
4668 }
4669
4670 if (any_added)
4671 {
112e8700 4672 current_uiout->text (_(" Inferior loaded "));
10f489e5 4673 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4674 bool first = true;
52941706 4675 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4676 {
bcb430e4 4677 if (!first)
112e8700 4678 current_uiout->text (" ");
bcb430e4 4679 first = false;
112e8700
SM
4680 current_uiout->field_string ("library", iter->so_name);
4681 current_uiout->text ("\n");
edcc5120 4682 }
edcc5120
TT
4683 }
4684}
4685
e514a9d6
JM
4686/* Print a message indicating what happened. This is called from
4687 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4688 list - a list of the eventpoints that caused this stop. KIND is
4689 the target_waitkind for the stopping event. This
e514a9d6
JM
4690 routine calls the generic print routine for printing a message
4691 about reasons for stopping. This will print (for example) the
4692 "Breakpoint n," part of the output. The return value of this
4693 routine is one of:
c906108c 4694
4a64f543 4695 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4696 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4697 code to print the location. An example is
c5aa993b
JM
4698 "Breakpoint 1, " which should be followed by
4699 the location.
917317f4 4700 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4701 to also print the location part of the message.
4702 An example is the catch/throw messages, which
4a64f543 4703 don't require a location appended to the end.
917317f4 4704 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4705 further info to be printed. */
c906108c 4706
917317f4 4707enum print_stop_action
36dfb11c 4708bpstat_print (bpstat bs, int kind)
c906108c 4709{
f486487f 4710 enum print_stop_action val;
c5aa993b 4711
c906108c 4712 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4713 (Currently all watchpoints go on the bpstat whether hit or not.
4714 That probably could (should) be changed, provided care is taken
c906108c 4715 with respect to bpstat_explains_signal). */
e514a9d6
JM
4716 for (; bs; bs = bs->next)
4717 {
4718 val = print_bp_stop_message (bs);
4719 if (val == PRINT_SRC_ONLY
4720 || val == PRINT_SRC_AND_LOC
4721 || val == PRINT_NOTHING)
4722 return val;
4723 }
c906108c 4724
36dfb11c
TT
4725 /* If we had hit a shared library event breakpoint,
4726 print_bp_stop_message would print out this message. If we hit an
4727 OS-level shared library event, do the same thing. */
4728 if (kind == TARGET_WAITKIND_LOADED)
4729 {
edcc5120 4730 print_solib_event (0);
36dfb11c
TT
4731 return PRINT_NOTHING;
4732 }
4733
e514a9d6 4734 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4735 with and nothing was printed. */
917317f4 4736 return PRINT_UNKNOWN;
c906108c
SS
4737}
4738
bf469271 4739/* Evaluate the boolean expression EXP and return the result. */
c906108c 4740
bf469271
PA
4741static bool
4742breakpoint_cond_eval (expression *exp)
c906108c 4743{
278cd55f 4744 struct value *mark = value_mark ();
bf469271 4745 bool res = value_true (evaluate_expression (exp));
cc59ec59 4746
c906108c 4747 value_free_to_mark (mark);
bf469271 4748 return res;
c906108c
SS
4749}
4750
5760d0ab 4751/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4752
04afa70c
TT
4753bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4754 : next (NULL),
b6433ede 4755 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4756 breakpoint_at (bl->owner),
4757 commands (NULL),
04afa70c
TT
4758 print (0),
4759 stop (0),
4760 print_it (print_it_normal)
c906108c 4761{
04afa70c
TT
4762 **bs_link_pointer = this;
4763 *bs_link_pointer = &next;
4764}
4765
4766bpstats::bpstats ()
4767 : next (NULL),
04afa70c
TT
4768 breakpoint_at (NULL),
4769 commands (NULL),
04afa70c
TT
4770 print (0),
4771 stop (0),
4772 print_it (print_it_normal)
4773{
c906108c
SS
4774}
4775\f
d983da9c
DJ
4776/* The target has stopped with waitstatus WS. Check if any hardware
4777 watchpoints have triggered, according to the target. */
4778
4779int
4780watchpoints_triggered (struct target_waitstatus *ws)
4781{
57810aa7 4782 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4783 CORE_ADDR addr;
d983da9c
DJ
4784
4785 if (!stopped_by_watchpoint)
4786 {
4787 /* We were not stopped by a watchpoint. Mark all watchpoints
4788 as not triggered. */
43892fdf 4789 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4790 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4791 {
4792 struct watchpoint *w = (struct watchpoint *) b;
4793
4794 w->watchpoint_triggered = watch_triggered_no;
4795 }
d983da9c
DJ
4796
4797 return 0;
4798 }
4799
328d42d8 4800 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4801 {
4802 /* We were stopped by a watchpoint, but we don't know where.
4803 Mark all watchpoints as unknown. */
43892fdf 4804 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4805 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4806 {
4807 struct watchpoint *w = (struct watchpoint *) b;
4808
4809 w->watchpoint_triggered = watch_triggered_unknown;
4810 }
d983da9c 4811
3c4797ba 4812 return 1;
d983da9c
DJ
4813 }
4814
4815 /* The target could report the data address. Mark watchpoints
4816 affected by this data address as triggered, and all others as not
4817 triggered. */
4818
43892fdf 4819 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4820 if (is_hardware_watchpoint (b))
d983da9c 4821 {
3a5c3e22 4822 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4823
3a5c3e22 4824 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4825 for (bp_location *loc : b->locations ())
9c06b0b4 4826 {
3a5c3e22 4827 if (is_masked_watchpoint (b))
9c06b0b4 4828 {
3a5c3e22
PA
4829 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4830 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4831
4832 if (newaddr == start)
4833 {
3a5c3e22 4834 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4835 break;
4836 }
4837 }
4838 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4839 else if (target_watchpoint_addr_within_range
4840 (current_inferior ()->top_target (), addr, loc->address,
4841 loc->length))
9c06b0b4 4842 {
3a5c3e22 4843 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4844 break;
4845 }
4846 }
d983da9c
DJ
4847 }
4848
4849 return 1;
4850}
4851
bf469271
PA
4852/* Possible return values for watchpoint_check. */
4853enum wp_check_result
4854 {
4855 /* The watchpoint has been deleted. */
4856 WP_DELETED = 1,
4857
4858 /* The value has changed. */
4859 WP_VALUE_CHANGED = 2,
4860
4861 /* The value has not changed. */
4862 WP_VALUE_NOT_CHANGED = 3,
4863
4864 /* Ignore this watchpoint, no matter if the value changed or not. */
4865 WP_IGNORE = 4,
4866 };
c906108c
SS
4867
4868#define BP_TEMPFLAG 1
4869#define BP_HARDWAREFLAG 2
4870
4a64f543 4871/* Evaluate watchpoint condition expression and check if its value
bf469271 4872 changed. */
553e4c11 4873
bf469271
PA
4874static wp_check_result
4875watchpoint_check (bpstat bs)
c906108c 4876{
3a5c3e22 4877 struct watchpoint *b;
c906108c
SS
4878 struct frame_info *fr;
4879 int within_current_scope;
4880
f431efe5 4881 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4882 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4883 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4884
f6bc2008
PA
4885 /* If this is a local watchpoint, we only want to check if the
4886 watchpoint frame is in scope if the current thread is the thread
4887 that was used to create the watchpoint. */
4888 if (!watchpoint_in_thread_scope (b))
60e1c644 4889 return WP_IGNORE;
f6bc2008 4890
c906108c
SS
4891 if (b->exp_valid_block == NULL)
4892 within_current_scope = 1;
4893 else
4894 {
edb3359d
DJ
4895 struct frame_info *frame = get_current_frame ();
4896 struct gdbarch *frame_arch = get_frame_arch (frame);
4897 CORE_ADDR frame_pc = get_frame_pc (frame);
4898
c9cf6e20 4899 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4900 still in the function but the stack frame has already been
4901 invalidated. Since we can't rely on the values of local
4902 variables after the stack has been destroyed, we are treating
4903 the watchpoint in that state as `not changed' without further
4904 checking. Don't mark watchpoints as changed if the current
4905 frame is in an epilogue - even if they are in some other
4906 frame, our view of the stack is likely to be wrong and
4907 frame_find_by_id could error out. */
c9cf6e20 4908 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4909 return WP_IGNORE;
a0f49112 4910
101dcfbe 4911 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4912 within_current_scope = (fr != NULL);
69fbadd5
DJ
4913
4914 /* If we've gotten confused in the unwinder, we might have
4915 returned a frame that can't describe this variable. */
edb3359d
DJ
4916 if (within_current_scope)
4917 {
4918 struct symbol *function;
4919
4920 function = get_frame_function (fr);
4921 if (function == NULL
4922 || !contained_in (b->exp_valid_block,
4923 SYMBOL_BLOCK_VALUE (function)))
4924 within_current_scope = 0;
4925 }
69fbadd5 4926
edb3359d 4927 if (within_current_scope)
c906108c
SS
4928 /* If we end up stopping, the current frame will get selected
4929 in normal_stop. So this call to select_frame won't affect
4930 the user. */
0f7d239c 4931 select_frame (fr);
c906108c 4932 }
c5aa993b 4933
c906108c
SS
4934 if (within_current_scope)
4935 {
4a64f543 4936 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
4937 time before we return to the command level and call
4938 free_all_values. We can't call free_all_values because we
4939 might be in the middle of evaluating a function call. */
c906108c 4940
9c06b0b4 4941 struct value *mark;
fa4727a6
DJ
4942 struct value *new_val;
4943
c1fc2657 4944 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4945 /* Since we don't know the exact trigger address (from
4946 stopped_data_address), just tell the user we've triggered
4947 a mask watchpoint. */
4948 return WP_VALUE_CHANGED;
4949
4950 mark = value_mark ();
1eaebe02 4951 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 4952 NULL, NULL, false);
218d2fc6 4953
bb9d5f81
PP
4954 if (b->val_bitsize != 0)
4955 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4956
4a64f543
MS
4957 /* We use value_equal_contents instead of value_equal because
4958 the latter coerces an array to a pointer, thus comparing just
4959 the address of the array instead of its contents. This is
4960 not what we want. */
fa4727a6 4961 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4962 || (b->val != NULL && !value_equal_contents (b->val.get (),
4963 new_val)))
c906108c 4964 {
c906108c 4965 bs->old_val = b->val;
850645cf 4966 b->val = release_value (new_val);
4c1d86d9 4967 b->val_valid = true;
850645cf
TT
4968 if (new_val != NULL)
4969 value_free_to_mark (mark);
c906108c
SS
4970 return WP_VALUE_CHANGED;
4971 }
4972 else
4973 {
60e1c644 4974 /* Nothing changed. */
c906108c 4975 value_free_to_mark (mark);
c906108c
SS
4976 return WP_VALUE_NOT_CHANGED;
4977 }
4978 }
4979 else
4980 {
4981 /* This seems like the only logical thing to do because
dda83cd7
SM
4982 if we temporarily ignored the watchpoint, then when
4983 we reenter the block in which it is valid it contains
4984 garbage (in the case of a function, it may have two
4985 garbage values, one before and one after the prologue).
4986 So we can't even detect the first assignment to it and
4987 watch after that (since the garbage may or may not equal
4988 the first value assigned). */
348d480f
PA
4989 /* We print all the stop information in
4990 breakpoint_ops->print_it, but in this case, by the time we
4991 call breakpoint_ops->print_it this bp will be deleted
4992 already. So we have no choice but print the information
4993 here. */
468afe6c 4994
0e454242 4995 SWITCH_THRU_ALL_UIS ()
dda83cd7 4996 {
468afe6c
PA
4997 struct ui_out *uiout = current_uiout;
4998
112e8700
SM
4999 if (uiout->is_mi_like_p ())
5000 uiout->field_string
5001 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5002 uiout->message ("\nWatchpoint %pF deleted because the program has "
5003 "left the block in\n"
5004 "which its expression is valid.\n",
5005 signed_field ("wpnum", b->number));
468afe6c 5006 }
4ce44c66 5007
cdac0397 5008 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5009 b->commands = NULL;
d0fb5eae 5010 watchpoint_del_at_next_stop (b);
c906108c
SS
5011
5012 return WP_DELETED;
5013 }
5014}
5015
18a18393 5016/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5017 breakpoint location BL. This function does not check if we should
5018 stop, only if BL explains the stop. */
5019
18a18393 5020static int
6c95b8df 5021bpstat_check_location (const struct bp_location *bl,
accd0bcd 5022 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5023 const struct target_waitstatus *ws)
18a18393
VP
5024{
5025 struct breakpoint *b = bl->owner;
5026
348d480f 5027 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5028 gdb_assert (b != NULL);
5029
bd522513 5030 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5031}
5032
3a5c3e22
PA
5033/* Determine if the watched values have actually changed, and we
5034 should stop. If not, set BS->stop to 0. */
5035
18a18393
VP
5036static void
5037bpstat_check_watchpoint (bpstat bs)
5038{
2bdf28a0 5039 const struct bp_location *bl;
3a5c3e22 5040 struct watchpoint *b;
2bdf28a0
JK
5041
5042 /* BS is built for existing struct breakpoint. */
b6433ede 5043 bl = bs->bp_location_at.get ();
2bdf28a0 5044 gdb_assert (bl != NULL);
3a5c3e22 5045 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5046 gdb_assert (b != NULL);
18a18393 5047
18a18393 5048 {
18a18393
VP
5049 int must_check_value = 0;
5050
c1fc2657 5051 if (b->type == bp_watchpoint)
18a18393
VP
5052 /* For a software watchpoint, we must always check the
5053 watched value. */
5054 must_check_value = 1;
5055 else if (b->watchpoint_triggered == watch_triggered_yes)
5056 /* We have a hardware watchpoint (read, write, or access)
5057 and the target earlier reported an address watched by
5058 this watchpoint. */
5059 must_check_value = 1;
5060 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5061 && b->type == bp_hardware_watchpoint)
18a18393
VP
5062 /* We were stopped by a hardware watchpoint, but the target could
5063 not report the data address. We must check the watchpoint's
5064 value. Access and read watchpoints are out of luck; without
5065 a data address, we can't figure it out. */
5066 must_check_value = 1;
3a5c3e22 5067
18a18393
VP
5068 if (must_check_value)
5069 {
bf469271
PA
5070 wp_check_result e;
5071
a70b8144 5072 try
bf469271
PA
5073 {
5074 e = watchpoint_check (bs);
5075 }
230d2906 5076 catch (const gdb_exception &ex)
bf469271
PA
5077 {
5078 exception_fprintf (gdb_stderr, ex,
5079 "Error evaluating expression "
5080 "for watchpoint %d\n",
5081 b->number);
5082
5083 SWITCH_THRU_ALL_UIS ()
5084 {
5085 printf_filtered (_("Watchpoint %d deleted.\n"),
5086 b->number);
5087 }
5088 watchpoint_del_at_next_stop (b);
5089 e = WP_DELETED;
5090 }
bf469271 5091
18a18393
VP
5092 switch (e)
5093 {
5094 case WP_DELETED:
5095 /* We've already printed what needs to be printed. */
5096 bs->print_it = print_it_done;
5097 /* Stop. */
5098 break;
60e1c644
PA
5099 case WP_IGNORE:
5100 bs->print_it = print_it_noop;
5101 bs->stop = 0;
5102 break;
18a18393 5103 case WP_VALUE_CHANGED:
c1fc2657 5104 if (b->type == bp_read_watchpoint)
18a18393 5105 {
85d721b8
PA
5106 /* There are two cases to consider here:
5107
4a64f543 5108 1. We're watching the triggered memory for reads.
85d721b8
PA
5109 In that case, trust the target, and always report
5110 the watchpoint hit to the user. Even though
5111 reads don't cause value changes, the value may
5112 have changed since the last time it was read, and
5113 since we're not trapping writes, we will not see
5114 those, and as such we should ignore our notion of
5115 old value.
5116
4a64f543 5117 2. We're watching the triggered memory for both
85d721b8
PA
5118 reads and writes. There are two ways this may
5119 happen:
5120
4a64f543 5121 2.1. This is a target that can't break on data
85d721b8
PA
5122 reads only, but can break on accesses (reads or
5123 writes), such as e.g., x86. We detect this case
5124 at the time we try to insert read watchpoints.
5125
4a64f543 5126 2.2. Otherwise, the target supports read
85d721b8
PA
5127 watchpoints, but, the user set an access or write
5128 watchpoint watching the same memory as this read
5129 watchpoint.
5130
5131 If we're watching memory writes as well as reads,
5132 ignore watchpoint hits when we find that the
5133 value hasn't changed, as reads don't cause
5134 changes. This still gives false positives when
5135 the program writes the same value to memory as
5136 what there was already in memory (we will confuse
5137 it for a read), but it's much better than
5138 nothing. */
5139
5140 int other_write_watchpoint = 0;
5141
5142 if (bl->watchpoint_type == hw_read)
5143 {
43892fdf 5144 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5145 if (other_b->type == bp_hardware_watchpoint
5146 || other_b->type == bp_access_watchpoint)
85d721b8 5147 {
3a5c3e22
PA
5148 struct watchpoint *other_w =
5149 (struct watchpoint *) other_b;
5150
5151 if (other_w->watchpoint_triggered
5152 == watch_triggered_yes)
5153 {
5154 other_write_watchpoint = 1;
5155 break;
5156 }
85d721b8
PA
5157 }
5158 }
5159
5160 if (other_write_watchpoint
5161 || bl->watchpoint_type == hw_access)
5162 {
5163 /* We're watching the same memory for writes,
5164 and the value changed since the last time we
5165 updated it, so this trap must be for a write.
5166 Ignore it. */
5167 bs->print_it = print_it_noop;
5168 bs->stop = 0;
5169 }
18a18393
VP
5170 }
5171 break;
5172 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5173 if (b->type == bp_hardware_watchpoint
5174 || b->type == bp_watchpoint)
18a18393
VP
5175 {
5176 /* Don't stop: write watchpoints shouldn't fire if
5177 the value hasn't changed. */
5178 bs->print_it = print_it_noop;
5179 bs->stop = 0;
5180 }
5181 /* Stop. */
5182 break;
5183 default:
5184 /* Can't happen. */
18a18393
VP
5185 break;
5186 }
5187 }
5188 else /* must_check_value == 0 */
5189 {
5190 /* This is a case where some watchpoint(s) triggered, but
5191 not at the address of this watchpoint, or else no
5192 watchpoint triggered after all. So don't print
5193 anything for this watchpoint. */
5194 bs->print_it = print_it_noop;
5195 bs->stop = 0;
5196 }
5197 }
5198}
5199
7d4df6a4
DE
5200/* For breakpoints that are currently marked as telling gdb to stop,
5201 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5202 of breakpoint referred to by BS. If we should not stop for this
5203 breakpoint, set BS->stop to 0. */
f431efe5 5204
18a18393 5205static void
00431a78 5206bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5207{
2bdf28a0
JK
5208 const struct bp_location *bl;
5209 struct breakpoint *b;
bf469271
PA
5210 /* Assume stop. */
5211 bool condition_result = true;
7d4df6a4
DE
5212 struct expression *cond;
5213
5214 gdb_assert (bs->stop);
2bdf28a0
JK
5215
5216 /* BS is built for existing struct breakpoint. */
b6433ede 5217 bl = bs->bp_location_at.get ();
2bdf28a0 5218 gdb_assert (bl != NULL);
f431efe5 5219 b = bs->breakpoint_at;
2bdf28a0 5220 gdb_assert (b != NULL);
18a18393 5221
b775012e
LM
5222 /* Even if the target evaluated the condition on its end and notified GDB, we
5223 need to do so again since GDB does not know if we stopped due to a
5224 breakpoint or a single step breakpoint. */
5225
18a18393 5226 if (frame_id_p (b->frame_id)
edb3359d 5227 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5228 {
7d4df6a4
DE
5229 bs->stop = 0;
5230 return;
5231 }
60e1c644 5232
12ab52e9
PA
5233 /* If this is a thread/task-specific breakpoint, don't waste cpu
5234 evaluating the condition if this isn't the specified
5235 thread/task. */
00431a78
PA
5236 if ((b->thread != -1 && b->thread != thread->global_num)
5237 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5238 {
5239 bs->stop = 0;
5240 return;
5241 }
5242
6dddc817
DE
5243 /* Evaluate extension language breakpoints that have a "stop" method
5244 implemented. */
5245 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5246
7d4df6a4
DE
5247 if (is_watchpoint (b))
5248 {
5249 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5250
4d01a485 5251 cond = w->cond_exp.get ();
7d4df6a4
DE
5252 }
5253 else
4d01a485 5254 cond = bl->cond.get ();
60e1c644 5255
7d4df6a4
DE
5256 if (cond && b->disposition != disp_del_at_next_stop)
5257 {
5258 int within_current_scope = 1;
5259 struct watchpoint * w;
60e1c644 5260
7d4df6a4
DE
5261 /* We use value_mark and value_free_to_mark because it could
5262 be a long time before we return to the command level and
5263 call free_all_values. We can't call free_all_values
5264 because we might be in the middle of evaluating a
5265 function call. */
5266 struct value *mark = value_mark ();
5267
5268 if (is_watchpoint (b))
5269 w = (struct watchpoint *) b;
5270 else
5271 w = NULL;
5272
5273 /* Need to select the frame, with all that implies so that
5274 the conditions will have the right context. Because we
5275 use the frame, we will not see an inlined function's
5276 variables when we arrive at a breakpoint at the start
5277 of the inlined function; the current frame will be the
5278 call site. */
5279 if (w == NULL || w->cond_exp_valid_block == NULL)
5280 select_frame (get_current_frame ());
5281 else
18a18393 5282 {
7d4df6a4
DE
5283 struct frame_info *frame;
5284
5285 /* For local watchpoint expressions, which particular
5286 instance of a local is being watched matters, so we
5287 keep track of the frame to evaluate the expression
5288 in. To evaluate the condition however, it doesn't
5289 really matter which instantiation of the function
5290 where the condition makes sense triggers the
5291 watchpoint. This allows an expression like "watch
5292 global if q > 10" set in `func', catch writes to
5293 global on all threads that call `func', or catch
5294 writes on all recursive calls of `func' by a single
5295 thread. We simply always evaluate the condition in
5296 the innermost frame that's executing where it makes
5297 sense to evaluate the condition. It seems
5298 intuitive. */
5299 frame = block_innermost_frame (w->cond_exp_valid_block);
5300 if (frame != NULL)
5301 select_frame (frame);
5302 else
5303 within_current_scope = 0;
18a18393 5304 }
7d4df6a4 5305 if (within_current_scope)
bf469271 5306 {
a70b8144 5307 try
bf469271
PA
5308 {
5309 condition_result = breakpoint_cond_eval (cond);
5310 }
230d2906 5311 catch (const gdb_exception &ex)
bf469271
PA
5312 {
5313 exception_fprintf (gdb_stderr, ex,
5314 "Error in testing breakpoint condition:\n");
5315 }
bf469271 5316 }
7d4df6a4 5317 else
18a18393 5318 {
7d4df6a4
DE
5319 warning (_("Watchpoint condition cannot be tested "
5320 "in the current scope"));
5321 /* If we failed to set the right context for this
5322 watchpoint, unconditionally report it. */
18a18393 5323 }
7d4df6a4
DE
5324 /* FIXME-someday, should give breakpoint #. */
5325 value_free_to_mark (mark);
18a18393 5326 }
7d4df6a4 5327
bf469271 5328 if (cond && !condition_result)
7d4df6a4
DE
5329 {
5330 bs->stop = 0;
5331 }
7d4df6a4
DE
5332 else if (b->ignore_count > 0)
5333 {
5334 b->ignore_count--;
5335 bs->stop = 0;
5336 /* Increase the hit count even though we don't stop. */
5337 ++(b->hit_count);
76727919 5338 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5339 }
18a18393
VP
5340}
5341
1cf4d951
PA
5342/* Returns true if we need to track moribund locations of LOC's type
5343 on the current target. */
5344
5345static int
5346need_moribund_for_location_type (struct bp_location *loc)
5347{
5348 return ((loc->loc_type == bp_loc_software_breakpoint
5349 && !target_supports_stopped_by_sw_breakpoint ())
5350 || (loc->loc_type == bp_loc_hardware_breakpoint
5351 && !target_supports_stopped_by_hw_breakpoint ()));
5352}
5353
ddfe970e 5354/* See breakpoint.h. */
c906108c
SS
5355
5356bpstat
ddfe970e 5357build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5358 const struct target_waitstatus *ws)
c906108c 5359{
5760d0ab 5360 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5361
43892fdf 5362 for (breakpoint *b : all_breakpoints ())
429374b8 5363 {
1a853c52 5364 if (!breakpoint_enabled (b))
429374b8 5365 continue;
a5606eee 5366
40cb8ca5 5367 for (bp_location *bl : b->locations ())
429374b8 5368 {
4a64f543
MS
5369 /* For hardware watchpoints, we look only at the first
5370 location. The watchpoint_check function will work on the
5371 entire expression, not the individual locations. For
5372 read watchpoints, the watchpoints_triggered function has
5373 checked all locations already. */
429374b8
JK
5374 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5375 break;
18a18393 5376
b5fa468f 5377 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5378 continue;
c5aa993b 5379
09ac7c10 5380 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5381 continue;
c5aa993b 5382
4a64f543
MS
5383 /* Come here if it's a watchpoint, or if the break address
5384 matches. */
c5aa993b 5385
ddfe970e
KS
5386 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5387 explain stop. */
c5aa993b 5388
f431efe5
PA
5389 /* Assume we stop. Should we find a watchpoint that is not
5390 actually triggered, or if the condition of the breakpoint
5391 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5392 bs->stop = 1;
5393 bs->print = 1;
d983da9c 5394
f431efe5
PA
5395 /* If this is a scope breakpoint, mark the associated
5396 watchpoint as triggered so that we will handle the
5397 out-of-scope event. We'll get to the watchpoint next
5398 iteration. */
d0fb5eae 5399 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5400 {
5401 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5402
5403 w->watchpoint_triggered = watch_triggered_yes;
5404 }
f431efe5
PA
5405 }
5406 }
5407
7c16b83e 5408 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5409 if (!target_supports_stopped_by_sw_breakpoint ()
5410 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5411 {
1123588c 5412 for (bp_location *loc : moribund_locations)
f431efe5 5413 {
1cf4d951
PA
5414 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5415 && need_moribund_for_location_type (loc))
5416 {
ddfe970e 5417 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5418 /* For hits of moribund locations, we should just proceed. */
5419 bs->stop = 0;
5420 bs->print = 0;
5421 bs->print_it = print_it_noop;
5422 }
f431efe5
PA
5423 }
5424 }
5425
ddfe970e
KS
5426 return bs_head;
5427}
5428
5429/* See breakpoint.h. */
5430
5431bpstat
5432bpstat_stop_status (const address_space *aspace,
00431a78 5433 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5434 const struct target_waitstatus *ws,
5435 bpstat stop_chain)
5436{
5437 struct breakpoint *b = NULL;
5438 /* First item of allocated bpstat's. */
5439 bpstat bs_head = stop_chain;
5440 bpstat bs;
5441 int need_remove_insert;
5442 int removed_any;
5443
5444 /* First, build the bpstat chain with locations that explain a
5445 target stop, while being careful to not set the target running,
5446 as that may invalidate locations (in particular watchpoint
5447 locations are recreated). Resuming will happen here with
5448 breakpoint conditions or watchpoint expressions that include
5449 inferior function calls. */
5450 if (bs_head == NULL)
5451 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5452
edcc5120
TT
5453 /* A bit of special processing for shlib breakpoints. We need to
5454 process solib loading here, so that the lists of loaded and
5455 unloaded libraries are correct before we handle "catch load" and
5456 "catch unload". */
5457 for (bs = bs_head; bs != NULL; bs = bs->next)
5458 {
5d268276 5459 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5460 {
5461 handle_solib_event ();
5462 break;
5463 }
5464 }
5465
f431efe5
PA
5466 /* Now go through the locations that caused the target to stop, and
5467 check whether we're interested in reporting this stop to higher
5468 layers, or whether we should resume the target transparently. */
5469
5470 removed_any = 0;
5471
5760d0ab 5472 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5473 {
5474 if (!bs->stop)
5475 continue;
5476
f431efe5 5477 b = bs->breakpoint_at;
348d480f
PA
5478 b->ops->check_status (bs);
5479 if (bs->stop)
28010a5d 5480 {
00431a78 5481 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5482
429374b8
JK
5483 if (bs->stop)
5484 {
5485 ++(b->hit_count);
76727919 5486 gdb::observers::breakpoint_modified.notify (b);
c906108c 5487
4a64f543 5488 /* We will stop here. */
429374b8
JK
5489 if (b->disposition == disp_disable)
5490 {
816338b5 5491 --(b->enable_count);
1a853c52 5492 if (b->enable_count <= 0)
429374b8 5493 b->enable_state = bp_disabled;
f431efe5 5494 removed_any = 1;
429374b8
JK
5495 }
5496 if (b->silent)
5497 bs->print = 0;
5498 bs->commands = b->commands;
abf85f46 5499 if (command_line_is_silent (bs->commands
d1b0a7bf 5500 ? bs->commands.get () : NULL))
abf85f46 5501 bs->print = 0;
9d6e6e84
HZ
5502
5503 b->ops->after_condition_true (bs);
429374b8
JK
5504 }
5505
348d480f 5506 }
a9b3a50f
PA
5507
5508 /* Print nothing for this entry if we don't stop or don't
5509 print. */
5510 if (!bs->stop || !bs->print)
5511 bs->print_it = print_it_noop;
429374b8 5512 }
876fa593 5513
d983da9c
DJ
5514 /* If we aren't stopping, the value of some hardware watchpoint may
5515 not have changed, but the intermediate memory locations we are
5516 watching may have. Don't bother if we're stopping; this will get
5517 done later. */
d832cb68 5518 need_remove_insert = 0;
5760d0ab
JK
5519 if (! bpstat_causes_stop (bs_head))
5520 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5521 if (!bs->stop
f431efe5
PA
5522 && bs->breakpoint_at
5523 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5524 {
3a5c3e22
PA
5525 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5526
5527 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5528 need_remove_insert = 1;
d983da9c
DJ
5529 }
5530
d832cb68 5531 if (need_remove_insert)
44702360 5532 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5533 else if (removed_any)
44702360 5534 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5535
5760d0ab 5536 return bs_head;
c906108c 5537}
628fe4e4
JK
5538
5539static void
61c26be8 5540handle_jit_event (CORE_ADDR address)
628fe4e4 5541{
628fe4e4
JK
5542 struct gdbarch *gdbarch;
5543
1eb8556f 5544 infrun_debug_printf ("handling bp_jit_event");
243a9253 5545
628fe4e4
JK
5546 /* Switch terminal for any messages produced by
5547 breakpoint_re_set. */
223ffa71 5548 target_terminal::ours_for_output ();
628fe4e4 5549
61c26be8
MS
5550 gdbarch = get_frame_arch (get_current_frame ());
5551 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5552 thus it is expected that its objectfile can be found through
5553 minimal symbol lookup. If it doesn't work (and assert fails), it
5554 most likely means that `jit_breakpoint_re_set` was changes and this
5555 function needs to be updated too. */
5556 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5557 gdb_assert (jit_bp_sym.objfile != nullptr);
5558 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5559
223ffa71 5560 target_terminal::inferior ();
628fe4e4
JK
5561}
5562
5563/* Prepare WHAT final decision for infrun. */
5564
5565/* Decide what infrun needs to do with this bpstat. */
5566
c906108c 5567struct bpstat_what
0e30163f 5568bpstat_what (bpstat bs_head)
c906108c 5569{
c906108c 5570 struct bpstat_what retval;
0e30163f 5571 bpstat bs;
c906108c 5572
628fe4e4 5573 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5574 retval.call_dummy = STOP_NONE;
e2d0f980 5575 retval.is_longjmp = false;
628fe4e4 5576
0e30163f 5577 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5578 {
628fe4e4
JK
5579 /* Extract this BS's action. After processing each BS, we check
5580 if its action overrides all we've seem so far. */
5581 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5582 enum bptype bptype;
5583
c906108c 5584 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5585 {
5586 /* I suspect this can happen if it was a momentary
5587 breakpoint which has since been deleted. */
5588 bptype = bp_none;
5589 }
20874c92 5590 else
f431efe5 5591 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5592
5593 switch (bptype)
c906108c
SS
5594 {
5595 case bp_none:
628fe4e4 5596 break;
c906108c
SS
5597 case bp_breakpoint:
5598 case bp_hardware_breakpoint:
7c16b83e 5599 case bp_single_step:
c906108c
SS
5600 case bp_until:
5601 case bp_finish:
a9b3a50f 5602 case bp_shlib_event:
c906108c
SS
5603 if (bs->stop)
5604 {
5605 if (bs->print)
628fe4e4 5606 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5607 else
628fe4e4 5608 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5609 }
5610 else
628fe4e4 5611 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5612 break;
5613 case bp_watchpoint:
5614 case bp_hardware_watchpoint:
5615 case bp_read_watchpoint:
5616 case bp_access_watchpoint:
5617 if (bs->stop)
5618 {
5619 if (bs->print)
628fe4e4 5620 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5621 else
628fe4e4 5622 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5623 }
5624 else
628fe4e4
JK
5625 {
5626 /* There was a watchpoint, but we're not stopping.
5627 This requires no further action. */
5628 }
c906108c
SS
5629 break;
5630 case bp_longjmp:
e2e4d78b 5631 case bp_longjmp_call_dummy:
186c406b 5632 case bp_exception:
0a39bb32
PA
5633 if (bs->stop)
5634 {
5635 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5636 retval.is_longjmp = bptype != bp_exception;
5637 }
5638 else
5639 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5640 break;
5641 case bp_longjmp_resume:
186c406b 5642 case bp_exception_resume:
0a39bb32
PA
5643 if (bs->stop)
5644 {
5645 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5646 retval.is_longjmp = bptype == bp_longjmp_resume;
5647 }
5648 else
5649 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5650 break;
5651 case bp_step_resume:
5652 if (bs->stop)
628fe4e4
JK
5653 this_action = BPSTAT_WHAT_STEP_RESUME;
5654 else
c906108c 5655 {
628fe4e4
JK
5656 /* It is for the wrong frame. */
5657 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5658 }
c906108c 5659 break;
2c03e5be
PA
5660 case bp_hp_step_resume:
5661 if (bs->stop)
5662 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5663 else
5664 {
5665 /* It is for the wrong frame. */
5666 this_action = BPSTAT_WHAT_SINGLE;
5667 }
5668 break;
c906108c 5669 case bp_watchpoint_scope:
c4093a6a 5670 case bp_thread_event:
1900040c 5671 case bp_overlay_event:
0fd8e87f 5672 case bp_longjmp_master:
aa7d318d 5673 case bp_std_terminate_master:
186c406b 5674 case bp_exception_master:
628fe4e4 5675 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5676 break;
ce78b96d 5677 case bp_catchpoint:
c5aa993b
JM
5678 if (bs->stop)
5679 {
5680 if (bs->print)
628fe4e4 5681 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5682 else
628fe4e4 5683 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5684 }
5685 else
628fe4e4 5686 {
cb1e4e32
PA
5687 /* Some catchpoints are implemented with breakpoints.
5688 For those, we need to step over the breakpoint. */
5689 if (bs->bp_location_at->loc_type != bp_loc_other)
5690 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5691 }
5692 break;
628fe4e4 5693 case bp_jit_event:
628fe4e4 5694 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5695 break;
c906108c 5696 case bp_call_dummy:
53a5351d
JM
5697 /* Make sure the action is stop (silent or noisy),
5698 so infrun.c pops the dummy frame. */
aa7d318d 5699 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5700 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5701 break;
5702 case bp_std_terminate:
5703 /* Make sure the action is stop (silent or noisy),
5704 so infrun.c pops the dummy frame. */
aa7d318d 5705 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5706 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5707 break;
1042e4c0 5708 case bp_tracepoint:
7a697b8d 5709 case bp_fast_tracepoint:
0fb4aa4b 5710 case bp_static_tracepoint:
1042e4c0
SS
5711 /* Tracepoint hits should not be reported back to GDB, and
5712 if one got through somehow, it should have been filtered
5713 out already. */
5714 internal_error (__FILE__, __LINE__,
7a697b8d 5715 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5716 break;
5717 case bp_gnu_ifunc_resolver:
5718 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5719 this_action = BPSTAT_WHAT_SINGLE;
5720 break;
5721 case bp_gnu_ifunc_resolver_return:
5722 /* The breakpoint will be removed, execution will restart from the
5723 PC of the former breakpoint. */
5724 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5725 break;
e7e0cddf
SS
5726
5727 case bp_dprintf:
a11cfd87
HZ
5728 if (bs->stop)
5729 this_action = BPSTAT_WHAT_STOP_SILENT;
5730 else
5731 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5732 break;
5733
628fe4e4
JK
5734 default:
5735 internal_error (__FILE__, __LINE__,
5736 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5737 }
628fe4e4 5738
325fac50 5739 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5740 }
628fe4e4 5741
243a9253
PA
5742 return retval;
5743}
628fe4e4 5744
243a9253
PA
5745void
5746bpstat_run_callbacks (bpstat bs_head)
5747{
5748 bpstat bs;
628fe4e4 5749
0e30163f
JK
5750 for (bs = bs_head; bs != NULL; bs = bs->next)
5751 {
5752 struct breakpoint *b = bs->breakpoint_at;
5753
5754 if (b == NULL)
5755 continue;
5756 switch (b->type)
5757 {
243a9253 5758 case bp_jit_event:
61c26be8 5759 handle_jit_event (bs->bp_location_at->address);
243a9253 5760 break;
0e30163f
JK
5761 case bp_gnu_ifunc_resolver:
5762 gnu_ifunc_resolver_stop (b);
5763 break;
5764 case bp_gnu_ifunc_resolver_return:
5765 gnu_ifunc_resolver_return_stop (b);
5766 break;
5767 }
5768 }
c906108c
SS
5769}
5770
4c462cb0 5771/* See breakpoint.h. */
c906108c 5772
4c462cb0
SM
5773bool
5774bpstat_should_step ()
c906108c 5775{
43892fdf 5776 for (breakpoint *b : all_breakpoints ())
717a8278 5777 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5778 return true;
43892fdf 5779
4c462cb0 5780 return false;
c906108c
SS
5781}
5782
4c462cb0
SM
5783/* See breakpoint.h. */
5784
5785bool
67822962
PA
5786bpstat_causes_stop (bpstat bs)
5787{
5788 for (; bs != NULL; bs = bs->next)
5789 if (bs->stop)
4c462cb0 5790 return true;
67822962 5791
4c462cb0 5792 return false;
67822962
PA
5793}
5794
c906108c 5795\f
c5aa993b 5796
170b53b2
UW
5797/* Compute a string of spaces suitable to indent the next line
5798 so it starts at the position corresponding to the table column
5799 named COL_NAME in the currently active table of UIOUT. */
5800
5801static char *
5802wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5803{
5804 static char wrap_indent[80];
5805 int i, total_width, width, align;
c5209615 5806 const char *text;
170b53b2
UW
5807
5808 total_width = 0;
112e8700 5809 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5810 {
5811 if (strcmp (text, col_name) == 0)
5812 {
5813 gdb_assert (total_width < sizeof wrap_indent);
5814 memset (wrap_indent, ' ', total_width);
5815 wrap_indent[total_width] = 0;
5816
5817 return wrap_indent;
5818 }
5819
5820 total_width += width + 1;
5821 }
5822
5823 return NULL;
5824}
5825
b775012e
LM
5826/* Determine if the locations of this breakpoint will have their conditions
5827 evaluated by the target, host or a mix of both. Returns the following:
5828
5829 "host": Host evals condition.
5830 "host or target": Host or Target evals condition.
5831 "target": Target evals condition.
5832*/
5833
5834static const char *
5835bp_condition_evaluator (struct breakpoint *b)
5836{
b775012e
LM
5837 char host_evals = 0;
5838 char target_evals = 0;
5839
5840 if (!b)
5841 return NULL;
5842
5843 if (!is_breakpoint (b))
5844 return NULL;
5845
5846 if (gdb_evaluates_breakpoint_condition_p ()
5847 || !target_supports_evaluation_of_breakpoint_conditions ())
5848 return condition_evaluation_host;
5849
40cb8ca5 5850 for (bp_location *bl : b->locations ())
b775012e
LM
5851 {
5852 if (bl->cond_bytecode)
5853 target_evals++;
5854 else
5855 host_evals++;
5856 }
5857
5858 if (host_evals && target_evals)
5859 return condition_evaluation_both;
5860 else if (target_evals)
5861 return condition_evaluation_target;
5862 else
5863 return condition_evaluation_host;
5864}
5865
5866/* Determine the breakpoint location's condition evaluator. This is
5867 similar to bp_condition_evaluator, but for locations. */
5868
5869static const char *
5870bp_location_condition_evaluator (struct bp_location *bl)
5871{
5872 if (bl && !is_breakpoint (bl->owner))
5873 return NULL;
5874
5875 if (gdb_evaluates_breakpoint_condition_p ()
5876 || !target_supports_evaluation_of_breakpoint_conditions ())
5877 return condition_evaluation_host;
5878
5879 if (bl && bl->cond_bytecode)
5880 return condition_evaluation_target;
5881 else
5882 return condition_evaluation_host;
5883}
5884
859825b8
JK
5885/* Print the LOC location out of the list of B->LOC locations. */
5886
170b53b2
UW
5887static void
5888print_breakpoint_location (struct breakpoint *b,
5889 struct bp_location *loc)
0d381245 5890{
79a45e25 5891 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5892
5893 scoped_restore_current_program_space restore_pspace;
6c95b8df 5894
859825b8
JK
5895 if (loc != NULL && loc->shlib_disabled)
5896 loc = NULL;
5897
6c95b8df
PA
5898 if (loc != NULL)
5899 set_current_program_space (loc->pspace);
5900
56435ebe 5901 if (b->display_canonical)
d28cd78a 5902 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5903 else if (loc && loc->symtab)
0d381245 5904 {
4a27f119
KS
5905 const struct symbol *sym = loc->symbol;
5906
0d381245
VP
5907 if (sym)
5908 {
112e8700 5909 uiout->text ("in ");
987012b8 5910 uiout->field_string ("func", sym->print_name (),
e43b10e1 5911 function_name_style.style ());
112e8700
SM
5912 uiout->text (" ");
5913 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5914 uiout->text ("at ");
0d381245 5915 }
112e8700 5916 uiout->field_string ("file",
cbe56571 5917 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5918 file_name_style.style ());
112e8700 5919 uiout->text (":");
05cba821 5920
112e8700
SM
5921 if (uiout->is_mi_like_p ())
5922 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5923
381befee 5924 uiout->field_signed ("line", loc->line_number);
0d381245 5925 }
859825b8 5926 else if (loc)
0d381245 5927 {
d7e74731 5928 string_file stb;
170b53b2 5929
d7e74731 5930 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5931 demangle, "");
112e8700 5932 uiout->field_stream ("at", stb);
0d381245 5933 }
859825b8 5934 else
f00aae0f 5935 {
d28cd78a
TT
5936 uiout->field_string ("pending",
5937 event_location_to_string (b->location.get ()));
f00aae0f
KS
5938 /* If extra_string is available, it could be holding a condition
5939 or dprintf arguments. In either case, make sure it is printed,
5940 too, but only for non-MI streams. */
112e8700 5941 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5942 {
5943 if (b->type == bp_dprintf)
112e8700 5944 uiout->text (",");
f00aae0f 5945 else
112e8700
SM
5946 uiout->text (" ");
5947 uiout->text (b->extra_string);
f00aae0f
KS
5948 }
5949 }
6c95b8df 5950
b775012e
LM
5951 if (loc && is_breakpoint (b)
5952 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5953 && bp_condition_evaluator (b) == condition_evaluation_both)
5954 {
112e8700
SM
5955 uiout->text (" (");
5956 uiout->field_string ("evaluated-by",
b775012e 5957 bp_location_condition_evaluator (loc));
112e8700 5958 uiout->text (")");
b775012e 5959 }
0d381245
VP
5960}
5961
269b11a2
PA
5962static const char *
5963bptype_string (enum bptype type)
c906108c 5964{
c4093a6a
JM
5965 struct ep_type_description
5966 {
5967 enum bptype type;
a121b7c1 5968 const char *description;
c4093a6a
JM
5969 };
5970 static struct ep_type_description bptypes[] =
c906108c 5971 {
c5aa993b
JM
5972 {bp_none, "?deleted?"},
5973 {bp_breakpoint, "breakpoint"},
c906108c 5974 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5975 {bp_single_step, "sw single-step"},
c5aa993b
JM
5976 {bp_until, "until"},
5977 {bp_finish, "finish"},
5978 {bp_watchpoint, "watchpoint"},
c906108c 5979 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5980 {bp_read_watchpoint, "read watchpoint"},
5981 {bp_access_watchpoint, "acc watchpoint"},
5982 {bp_longjmp, "longjmp"},
5983 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5984 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5985 {bp_exception, "exception"},
5986 {bp_exception_resume, "exception resume"},
c5aa993b 5987 {bp_step_resume, "step resume"},
2c03e5be 5988 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5989 {bp_watchpoint_scope, "watchpoint scope"},
5990 {bp_call_dummy, "call dummy"},
aa7d318d 5991 {bp_std_terminate, "std::terminate"},
c5aa993b 5992 {bp_shlib_event, "shlib events"},
c4093a6a 5993 {bp_thread_event, "thread events"},
1900040c 5994 {bp_overlay_event, "overlay events"},
0fd8e87f 5995 {bp_longjmp_master, "longjmp master"},
aa7d318d 5996 {bp_std_terminate_master, "std::terminate master"},
186c406b 5997 {bp_exception_master, "exception master"},
ce78b96d 5998 {bp_catchpoint, "catchpoint"},
1042e4c0 5999 {bp_tracepoint, "tracepoint"},
7a697b8d 6000 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6001 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6002 {bp_dprintf, "dprintf"},
4efc6507 6003 {bp_jit_event, "jit events"},
0e30163f
JK
6004 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6005 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6006 };
269b11a2
PA
6007
6008 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6009 || ((int) type != bptypes[(int) type].type))
6010 internal_error (__FILE__, __LINE__,
6011 _("bptypes table does not describe type #%d."),
6012 (int) type);
6013
6014 return bptypes[(int) type].description;
6015}
6016
998580f1
MK
6017/* For MI, output a field named 'thread-groups' with a list as the value.
6018 For CLI, prefix the list with the string 'inf'. */
6019
6020static void
6021output_thread_groups (struct ui_out *uiout,
6022 const char *field_name,
5c632425 6023 const std::vector<int> &inf_nums,
998580f1
MK
6024 int mi_only)
6025{
112e8700 6026 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6027
6028 /* For backward compatibility, don't display inferiors in CLI unless
6029 there are several. Always display them for MI. */
6030 if (!is_mi && mi_only)
6031 return;
6032
10f489e5 6033 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6034
5c632425 6035 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6036 {
6037 if (is_mi)
6038 {
6039 char mi_group[10];
6040
5c632425 6041 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6042 uiout->field_string (NULL, mi_group);
998580f1
MK
6043 }
6044 else
6045 {
6046 if (i == 0)
112e8700 6047 uiout->text (" inf ");
998580f1 6048 else
112e8700 6049 uiout->text (", ");
998580f1 6050
5c632425 6051 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6052 }
6053 }
998580f1
MK
6054}
6055
a38118e5
PA
6056/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6057 instead of going via breakpoint_ops::print_one. This makes "maint
6058 info breakpoints" show the software breakpoint locations of
6059 catchpoints, which are considered internal implementation
6060 detail. */
269b11a2
PA
6061
6062static void
6063print_one_breakpoint_location (struct breakpoint *b,
6064 struct bp_location *loc,
6065 int loc_number,
6066 struct bp_location **last_loc,
a38118e5 6067 int allflag, bool raw_loc)
269b11a2
PA
6068{
6069 struct command_line *l;
c2c6d25f 6070 static char bpenables[] = "nynny";
c906108c 6071
79a45e25 6072 struct ui_out *uiout = current_uiout;
0d381245
VP
6073 int header_of_multiple = 0;
6074 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6075 struct value_print_options opts;
6076
6077 get_user_print_options (&opts);
0d381245
VP
6078
6079 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6080 /* See comment in print_one_breakpoint concerning treatment of
6081 breakpoints with single disabled location. */
0d381245
VP
6082 if (loc == NULL
6083 && (b->loc != NULL
b5fa468f
TBA
6084 && (b->loc->next != NULL
6085 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6086 header_of_multiple = 1;
6087 if (loc == NULL)
6088 loc = b->loc;
6089
c4093a6a
JM
6090 annotate_record ();
6091
6092 /* 1 */
6093 annotate_field (0);
0d381245 6094 if (part_of_multiple)
528e1572 6095 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6096 else
381befee 6097 uiout->field_signed ("number", b->number);
c4093a6a
JM
6098
6099 /* 2 */
6100 annotate_field (1);
0d381245 6101 if (part_of_multiple)
112e8700 6102 uiout->field_skip ("type");
269b11a2 6103 else
112e8700 6104 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6105
6106 /* 3 */
6107 annotate_field (2);
0d381245 6108 if (part_of_multiple)
112e8700 6109 uiout->field_skip ("disp");
0d381245 6110 else
112e8700 6111 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6112
c4093a6a
JM
6113 /* 4 */
6114 annotate_field (3);
85c88e2a
TBA
6115 /* For locations that are disabled because of an invalid condition,
6116 display "N*" on CLI, where "*" refers to a footnote below the
6117 table. For MI, simply display a "N" without a footnote. */
6118 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6119 if (part_of_multiple)
85c88e2a 6120 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6121 : (loc->enabled ? "y" : "n")));
0d381245 6122 else
112e8700 6123 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6124
c4093a6a 6125 /* 5 and 6 */
a38118e5 6126 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6127 b->ops->print_one (b, last_loc);
3086aeae 6128 else
a38118e5
PA
6129 {
6130 if (is_watchpoint (b))
3a5c3e22
PA
6131 {
6132 struct watchpoint *w = (struct watchpoint *) b;
6133
6134 /* Field 4, the address, is omitted (which makes the columns
6135 not line up too nicely with the headers, but the effect
6136 is relatively readable). */
6137 if (opts.addressprint)
112e8700 6138 uiout->field_skip ("addr");
3a5c3e22 6139 annotate_field (5);
112e8700 6140 uiout->field_string ("what", w->exp_string);
3a5c3e22 6141 }
f06f1252
TT
6142 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6143 || is_ada_exception_catchpoint (b))
a38118e5
PA
6144 {
6145 if (opts.addressprint)
6146 {
6147 annotate_field (4);
6148 if (header_of_multiple)
7f6aba03
TT
6149 uiout->field_string ("addr", "<MULTIPLE>",
6150 metadata_style.style ());
a38118e5 6151 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6152 uiout->field_string ("addr", "<PENDING>",
6153 metadata_style.style ());
a38118e5
PA
6154 else
6155 uiout->field_core_addr ("addr",
6156 loc->gdbarch, loc->address);
6157 }
6158 annotate_field (5);
6159 if (!header_of_multiple)
6160 print_breakpoint_location (b, loc);
6161 if (b->loc)
6162 *last_loc = b->loc;
6163 }
6164 }
6c95b8df 6165
998580f1 6166 if (loc != NULL && !header_of_multiple)
6c95b8df 6167 {
5c632425 6168 std::vector<int> inf_nums;
998580f1 6169 int mi_only = 1;
6c95b8df 6170
08036331 6171 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6172 {
6173 if (inf->pspace == loc->pspace)
5c632425 6174 inf_nums.push_back (inf->num);
6c95b8df 6175 }
998580f1 6176
dda83cd7 6177 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6178 there are several. Always display for MI. */
6179 if (allflag
6180 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6181 && (program_spaces.size () > 1
998580f1
MK
6182 || number_of_inferiors () > 1)
6183 /* LOC is for existing B, it cannot be in
6184 moribund_locations and thus having NULL OWNER. */
6185 && loc->owner->type != bp_catchpoint))
6186 mi_only = 0;
5c632425 6187 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6188 }
6189
4a306c9a 6190 if (!part_of_multiple)
c4093a6a 6191 {
4a306c9a
JB
6192 if (b->thread != -1)
6193 {
6194 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6195 "stop only in" line a little further down. */
112e8700 6196 uiout->text (" thread ");
381befee 6197 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6198 }
6199 else if (b->task != 0)
6200 {
112e8700 6201 uiout->text (" task ");
381befee 6202 uiout->field_signed ("task", b->task);
4a306c9a 6203 }
c4093a6a 6204 }
f1310107 6205
112e8700 6206 uiout->text ("\n");
f1310107 6207
348d480f 6208 if (!part_of_multiple)
f1310107
TJB
6209 b->ops->print_one_detail (b, uiout);
6210
0d381245 6211 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6212 {
6213 annotate_field (6);
112e8700 6214 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6215 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6216 the frame ID. */
112e8700 6217 uiout->field_core_addr ("frame",
5af949e3 6218 b->gdbarch, b->frame_id.stack_addr);
112e8700 6219 uiout->text ("\n");
c4093a6a
JM
6220 }
6221
28010a5d 6222 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6223 {
6224 annotate_field (7);
d77f58be 6225 if (is_tracepoint (b))
112e8700 6226 uiout->text ("\ttrace only if ");
1042e4c0 6227 else
112e8700
SM
6228 uiout->text ("\tstop only if ");
6229 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6230
6231 /* Print whether the target is doing the breakpoint's condition
6232 evaluation. If GDB is doing the evaluation, don't print anything. */
6233 if (is_breakpoint (b)
6234 && breakpoint_condition_evaluation_mode ()
6235 == condition_evaluation_target)
6236 {
6a831f06
PA
6237 uiout->message (" (%pF evals)",
6238 string_field ("evaluated-by",
6239 bp_condition_evaluator (b)));
b775012e 6240 }
112e8700 6241 uiout->text ("\n");
0101ce28
JJ
6242 }
6243
0d381245 6244 if (!part_of_multiple && b->thread != -1)
c4093a6a 6245 {
4a64f543 6246 /* FIXME should make an annotation for this. */
112e8700
SM
6247 uiout->text ("\tstop only in thread ");
6248 if (uiout->is_mi_like_p ())
381befee 6249 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6250 else
6251 {
6252 struct thread_info *thr = find_thread_global_id (b->thread);
6253
112e8700 6254 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6255 }
112e8700 6256 uiout->text ("\n");
c4093a6a
JM
6257 }
6258
556ec64d
YQ
6259 if (!part_of_multiple)
6260 {
6261 if (b->hit_count)
31f56a27
YQ
6262 {
6263 /* FIXME should make an annotation for this. */
6264 if (is_catchpoint (b))
112e8700 6265 uiout->text ("\tcatchpoint");
31f56a27 6266 else if (is_tracepoint (b))
112e8700 6267 uiout->text ("\ttracepoint");
31f56a27 6268 else
112e8700
SM
6269 uiout->text ("\tbreakpoint");
6270 uiout->text (" already hit ");
381befee 6271 uiout->field_signed ("times", b->hit_count);
31f56a27 6272 if (b->hit_count == 1)
112e8700 6273 uiout->text (" time\n");
31f56a27 6274 else
112e8700 6275 uiout->text (" times\n");
31f56a27 6276 }
556ec64d
YQ
6277 else
6278 {
31f56a27 6279 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6280 if (uiout->is_mi_like_p ())
381befee 6281 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6282 }
6283 }
8b93c638 6284
0d381245 6285 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6286 {
6287 annotate_field (8);
6a831f06
PA
6288 uiout->message ("\tignore next %pF hits\n",
6289 signed_field ("ignore", b->ignore_count));
c4093a6a 6290 }
059fb39f 6291
816338b5
SS
6292 /* Note that an enable count of 1 corresponds to "enable once"
6293 behavior, which is reported by the combination of enablement and
6294 disposition, so we don't need to mention it here. */
6295 if (!part_of_multiple && b->enable_count > 1)
6296 {
6297 annotate_field (8);
112e8700 6298 uiout->text ("\tdisable after ");
816338b5
SS
6299 /* Tweak the wording to clarify that ignore and enable counts
6300 are distinct, and have additive effect. */
6301 if (b->ignore_count)
112e8700 6302 uiout->text ("additional ");
816338b5 6303 else
112e8700 6304 uiout->text ("next ");
381befee 6305 uiout->field_signed ("enable", b->enable_count);
112e8700 6306 uiout->text (" hits\n");
816338b5
SS
6307 }
6308
f196051f
SS
6309 if (!part_of_multiple && is_tracepoint (b))
6310 {
6311 struct tracepoint *tp = (struct tracepoint *) b;
6312
6313 if (tp->traceframe_usage)
6314 {
112e8700 6315 uiout->text ("\ttrace buffer usage ");
381befee 6316 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6317 uiout->text (" bytes\n");
f196051f
SS
6318 }
6319 }
d3ce09f5 6320
d1b0a7bf 6321 l = b->commands ? b->commands.get () : NULL;
059fb39f 6322 if (!part_of_multiple && l)
c4093a6a
JM
6323 {
6324 annotate_field (9);
2e783024 6325 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6326 print_command_lines (uiout, l, 4);
c4093a6a 6327 }
d24317b4 6328
d9b3f62e 6329 if (is_tracepoint (b))
1042e4c0 6330 {
d9b3f62e
PA
6331 struct tracepoint *t = (struct tracepoint *) b;
6332
6333 if (!part_of_multiple && t->pass_count)
6334 {
6335 annotate_field (10);
112e8700 6336 uiout->text ("\tpass count ");
381befee 6337 uiout->field_signed ("pass", t->pass_count);
112e8700 6338 uiout->text (" \n");
d9b3f62e 6339 }
f2a8bc8a
YQ
6340
6341 /* Don't display it when tracepoint or tracepoint location is
6342 pending. */
6343 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6344 {
6345 annotate_field (11);
6346
112e8700
SM
6347 if (uiout->is_mi_like_p ())
6348 uiout->field_string ("installed",
f2a8bc8a
YQ
6349 loc->inserted ? "y" : "n");
6350 else
6351 {
6352 if (loc->inserted)
112e8700 6353 uiout->text ("\t");
f2a8bc8a 6354 else
112e8700
SM
6355 uiout->text ("\tnot ");
6356 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6357 }
6358 }
1042e4c0
SS
6359 }
6360
112e8700 6361 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6362 {
3a5c3e22
PA
6363 if (is_watchpoint (b))
6364 {
6365 struct watchpoint *w = (struct watchpoint *) b;
6366
112e8700 6367 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6368 }
f00aae0f 6369 else if (b->location != NULL
d28cd78a 6370 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6371 uiout->field_string ("original-location",
d28cd78a 6372 event_location_to_string (b->location.get ()));
d24317b4 6373 }
c4093a6a 6374}
c5aa993b 6375
13674803
SM
6376/* See breakpoint.h. */
6377
6378bool fix_multi_location_breakpoint_output_globally = false;
6379
0d381245
VP
6380static void
6381print_one_breakpoint (struct breakpoint *b,
4a64f543 6382 struct bp_location **last_loc,
6c95b8df 6383 int allflag)
0d381245 6384{
79a45e25 6385 struct ui_out *uiout = current_uiout;
13674803
SM
6386 bool use_fixed_output
6387 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6388 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6389
b4be1b06 6390 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6391 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6392
b4be1b06
SM
6393 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6394 are outside. */
6395 if (!use_fixed_output)
6396 bkpt_tuple_emitter.reset ();
0d381245
VP
6397
6398 /* If this breakpoint has custom print function,
6399 it's already printed. Otherwise, print individual
6400 locations, if any. */
a38118e5
PA
6401 if (b->ops == NULL
6402 || b->ops->print_one == NULL
6403 || allflag)
0d381245 6404 {
4a64f543
MS
6405 /* If breakpoint has a single location that is disabled, we
6406 print it as if it had several locations, since otherwise it's
6407 hard to represent "breakpoint enabled, location disabled"
6408 situation.
6409
6410 Note that while hardware watchpoints have several locations
a38118e5
PA
6411 internally, that's not a property exposed to users.
6412
6413 Likewise, while catchpoints may be implemented with
6414 breakpoints (e.g., catch throw), that's not a property
6415 exposed to users. We do however display the internal
6416 breakpoint locations with "maint info breakpoints". */
6417 if (!is_hardware_watchpoint (b)
f06f1252
TT
6418 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6419 || is_ada_exception_catchpoint (b))
a38118e5 6420 && (allflag
b5fa468f
TBA
6421 || (b->loc && (b->loc->next
6422 || !b->loc->enabled
6423 || b->loc->disabled_by_cond))))
0d381245 6424 {
b4be1b06
SM
6425 gdb::optional<ui_out_emit_list> locations_list;
6426
6427 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6428 MI record. For later versions, place breakpoint locations in a
6429 list. */
6430 if (uiout->is_mi_like_p () && use_fixed_output)
6431 locations_list.emplace (uiout, "locations");
8d3788bd 6432
b4be1b06 6433 int n = 1;
40cb8ca5 6434 for (bp_location *loc : b->locations ())
8d3788bd 6435 {
b4be1b06 6436 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6437 print_one_breakpoint_location (b, loc, n, last_loc,
6438 allflag, allflag);
40cb8ca5 6439 n++;
8d3788bd 6440 }
0d381245
VP
6441 }
6442 }
6443}
6444
a6d9a66e
UW
6445static int
6446breakpoint_address_bits (struct breakpoint *b)
6447{
6448 int print_address_bits = 0;
a6d9a66e 6449
c6d81124
PA
6450 /* Software watchpoints that aren't watching memory don't have an
6451 address to print. */
6452 if (is_no_memory_software_watchpoint (b))
6453 return 0;
6454
40cb8ca5 6455 for (bp_location *loc : b->locations ())
a6d9a66e 6456 {
c7437ca6
PA
6457 int addr_bit;
6458
c7437ca6 6459 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6460 if (addr_bit > print_address_bits)
6461 print_address_bits = addr_bit;
6462 }
6463
6464 return print_address_bits;
6465}
0d381245 6466
65630365 6467/* See breakpoint.h. */
c5aa993b 6468
65630365
PA
6469void
6470print_breakpoint (breakpoint *b)
c4093a6a 6471{
a6d9a66e 6472 struct bp_location *dummy_loc = NULL;
65630365 6473 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6474}
c5aa993b 6475
09d682a4
TT
6476/* Return true if this breakpoint was set by the user, false if it is
6477 internal or momentary. */
6478
6479int
6480user_breakpoint_p (struct breakpoint *b)
6481{
46c6471b 6482 return b->number > 0;
09d682a4
TT
6483}
6484
93daf339
TT
6485/* See breakpoint.h. */
6486
6487int
6488pending_breakpoint_p (struct breakpoint *b)
6489{
6490 return b->loc == NULL;
6491}
6492
5c458ae8
SM
6493/* Print information on breakpoints (including watchpoints and tracepoints).
6494
6495 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6496 understood by number_or_range_parser. Only breakpoints included in this
6497 list are then printed.
6498
6499 If SHOW_INTERNAL is true, print internal breakpoints.
6500
6501 If FILTER is non-NULL, call it on each breakpoint and only include the
6502 ones for which it returns true.
6503
6504 Return the total number of breakpoints listed. */
c906108c 6505
d77f58be 6506static int
5c458ae8 6507breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6508 bool (*filter) (const struct breakpoint *))
c4093a6a 6509{
a6d9a66e 6510 struct bp_location *last_loc = NULL;
7f3b0473 6511 int nr_printable_breakpoints;
79a45b7d 6512 struct value_print_options opts;
a6d9a66e 6513 int print_address_bits = 0;
269b11a2 6514 int print_type_col_width = 14;
79a45e25 6515 struct ui_out *uiout = current_uiout;
b5fa468f 6516 bool has_disabled_by_cond_location = false;
269b11a2 6517
79a45b7d
TT
6518 get_user_print_options (&opts);
6519
4a64f543
MS
6520 /* Compute the number of rows in the table, as well as the size
6521 required for address fields. */
7f3b0473 6522 nr_printable_breakpoints = 0;
43892fdf 6523 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6524 {
6525 /* If we have a filter, only list the breakpoints it accepts. */
6526 if (filter && !filter (b))
6527 continue;
6528
5c458ae8 6529 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6530 accept. Skip the others. */
5c458ae8 6531 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6532 {
5c458ae8 6533 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6534 continue;
5c458ae8 6535 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6536 continue;
6537 }
269b11a2 6538
5c458ae8 6539 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6540 {
6541 int addr_bit, type_len;
a6d9a66e 6542
e5a67952
MS
6543 addr_bit = breakpoint_address_bits (b);
6544 if (addr_bit > print_address_bits)
6545 print_address_bits = addr_bit;
269b11a2 6546
e5a67952
MS
6547 type_len = strlen (bptype_string (b->type));
6548 if (type_len > print_type_col_width)
6549 print_type_col_width = type_len;
6550
6551 nr_printable_breakpoints++;
6552 }
6553 }
7f3b0473 6554
4a2b031d
TT
6555 {
6556 ui_out_emit_table table_emitter (uiout,
6557 opts.addressprint ? 6 : 5,
6558 nr_printable_breakpoints,
6559 "BreakpointTable");
6560
6561 if (nr_printable_breakpoints > 0)
6562 annotate_breakpoints_headers ();
6563 if (nr_printable_breakpoints > 0)
6564 annotate_field (0);
6565 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6566 if (nr_printable_breakpoints > 0)
6567 annotate_field (1);
6568 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6569 if (nr_printable_breakpoints > 0)
6570 annotate_field (2);
6571 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6572 if (nr_printable_breakpoints > 0)
6573 annotate_field (3);
6574 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6575 if (opts.addressprint)
6576 {
6577 if (nr_printable_breakpoints > 0)
6578 annotate_field (4);
6579 if (print_address_bits <= 32)
6580 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6581 else
6582 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6583 }
6584 if (nr_printable_breakpoints > 0)
6585 annotate_field (5);
6586 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6587 uiout->table_body ();
6588 if (nr_printable_breakpoints > 0)
6589 annotate_breakpoints_table ();
6590
43892fdf 6591 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6592 {
6593 QUIT;
6594 /* If we have a filter, only list the breakpoints it accepts. */
6595 if (filter && !filter (b))
6596 continue;
e5a67952 6597
5c458ae8 6598 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6599 accept. Skip the others. */
e5a67952 6600
5c458ae8 6601 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6602 {
5c458ae8 6603 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6604 {
5c458ae8 6605 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6606 continue;
6607 }
6608 else /* all others */
6609 {
5c458ae8 6610 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6611 continue;
6612 }
6613 }
6614 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6615 show_internal is set. */
6616 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6617 {
6618 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6619 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6620 if (loc->disabled_by_cond)
6621 has_disabled_by_cond_location = true;
6622 }
4a2b031d
TT
6623 }
6624 }
698384cd 6625
7f3b0473 6626 if (nr_printable_breakpoints == 0)
c906108c 6627 {
4a64f543
MS
6628 /* If there's a filter, let the caller decide how to report
6629 empty list. */
d77f58be
SS
6630 if (!filter)
6631 {
5c458ae8 6632 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6633 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6634 else
112e8700 6635 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6636 bp_num_list);
d77f58be 6637 }
c906108c
SS
6638 }
6639 else
c4093a6a 6640 {
a6d9a66e
UW
6641 if (last_loc && !server_command)
6642 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6643
85c88e2a 6644 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6645 uiout->message (_("(*): Breakpoint condition is invalid at this "
6646 "location.\n"));
c4093a6a 6647 }
c906108c 6648
4a64f543 6649 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6650 there have been breakpoints? */
c906108c 6651 annotate_breakpoints_table_end ();
d77f58be
SS
6652
6653 return nr_printable_breakpoints;
c906108c
SS
6654}
6655
ad443146
SS
6656/* Display the value of default-collect in a way that is generally
6657 compatible with the breakpoint list. */
6658
6659static void
6660default_collect_info (void)
6661{
79a45e25
PA
6662 struct ui_out *uiout = current_uiout;
6663
ad443146
SS
6664 /* If it has no value (which is frequently the case), say nothing; a
6665 message like "No default-collect." gets in user's face when it's
6666 not wanted. */
6667 if (!*default_collect)
6668 return;
6669
6670 /* The following phrase lines up nicely with per-tracepoint collect
6671 actions. */
112e8700
SM
6672 uiout->text ("default collect ");
6673 uiout->field_string ("default-collect", default_collect);
6674 uiout->text (" \n");
ad443146
SS
6675}
6676
c906108c 6677static void
0b39b52e 6678info_breakpoints_command (const char *args, int from_tty)
c906108c 6679{
5c458ae8 6680 breakpoint_1 (args, false, NULL);
ad443146
SS
6681
6682 default_collect_info ();
d77f58be
SS
6683}
6684
6685static void
1d12d88f 6686info_watchpoints_command (const char *args, int from_tty)
d77f58be 6687{
5c458ae8 6688 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6689 struct ui_out *uiout = current_uiout;
d77f58be
SS
6690
6691 if (num_printed == 0)
6692 {
e5a67952 6693 if (args == NULL || *args == '\0')
112e8700 6694 uiout->message ("No watchpoints.\n");
d77f58be 6695 else
112e8700 6696 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6697 }
c906108c
SS
6698}
6699
7a292a7a 6700static void
4495129a 6701maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6702{
5c458ae8 6703 breakpoint_1 (args, true, NULL);
ad443146
SS
6704
6705 default_collect_info ();
c906108c
SS
6706}
6707
0d381245 6708static int
714835d5 6709breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6710 struct program_space *pspace,
714835d5 6711 CORE_ADDR pc, struct obj_section *section)
0d381245 6712{
40cb8ca5 6713 for (bp_location *bl : b->locations ())
0d381245 6714 {
6c95b8df
PA
6715 if (bl->pspace == pspace
6716 && bl->address == pc
0d381245
VP
6717 && (!overlay_debugging || bl->section == section))
6718 return 1;
6719 }
6720 return 0;
6721}
6722
672f9b60 6723/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6724 concerns with logical breakpoints, so we match program spaces, not
6725 address spaces. */
c906108c
SS
6726
6727static void
6c95b8df
PA
6728describe_other_breakpoints (struct gdbarch *gdbarch,
6729 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6730 struct obj_section *section, int thread)
c906108c 6731{
52f0bd74 6732 int others = 0;
c906108c 6733
43892fdf 6734 for (breakpoint *b : all_breakpoints ())
672f9b60 6735 others += (user_breakpoint_p (b)
dda83cd7 6736 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6737
c906108c
SS
6738 if (others > 0)
6739 {
a3f17187
AC
6740 if (others == 1)
6741 printf_filtered (_("Note: breakpoint "));
6742 else /* if (others == ???) */
6743 printf_filtered (_("Note: breakpoints "));
43892fdf 6744 for (breakpoint *b : all_breakpoints ())
672f9b60 6745 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6746 {
6747 others--;
6748 printf_filtered ("%d", b->number);
6749 if (b->thread == -1 && thread != -1)
6750 printf_filtered (" (all threads)");
6751 else if (b->thread != -1)
6752 printf_filtered (" (thread %d)", b->thread);
6753 printf_filtered ("%s%s ",
059fb39f 6754 ((b->enable_state == bp_disabled
f8eba3c6 6755 || b->enable_state == bp_call_disabled)
0d381245 6756 ? " (disabled)"
0d381245
VP
6757 : ""),
6758 (others > 1) ? ","
6759 : ((others == 1) ? " and" : ""));
6760 }
6a831f06
PA
6761 current_uiout->message (_("also set at pc %ps.\n"),
6762 styled_string (address_style.style (),
6763 paddress (gdbarch, pc)));
c906108c
SS
6764 }
6765}
6766\f
c906108c 6767
cb1e4e32
PA
6768/* Return true iff it is meaningful to use the address member of LOC.
6769 For some breakpoint types, the locations' address members are
6770 irrelevant and it makes no sense to attempt to compare them to
6771 other addresses (or use them for any other purpose either).
2d134ed3 6772
cb1e4e32
PA
6773 More specifically, software watchpoints and catchpoints that are
6774 not backed by breakpoints always have a zero valued location
6775 address and we don't want to mark breakpoints of any of these types
6776 to be a duplicate of an actual breakpoint location at address
6777 zero. */
e4f237da 6778
cb1e4e32
PA
6779static bool
6780bl_address_is_meaningful (bp_location *loc)
e4f237da 6781{
cb1e4e32 6782 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6783}
6784
6785/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6786 true if LOC1 and LOC2 represent the same watchpoint location. */
6787
6788static int
4a64f543
MS
6789watchpoint_locations_match (struct bp_location *loc1,
6790 struct bp_location *loc2)
2d134ed3 6791{
3a5c3e22
PA
6792 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6793 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6794
6795 /* Both of them must exist. */
6796 gdb_assert (w1 != NULL);
6797 gdb_assert (w2 != NULL);
2bdf28a0 6798
4a64f543
MS
6799 /* If the target can evaluate the condition expression in hardware,
6800 then we we need to insert both watchpoints even if they are at
6801 the same place. Otherwise the watchpoint will only trigger when
6802 the condition of whichever watchpoint was inserted evaluates to
6803 true, not giving a chance for GDB to check the condition of the
6804 other watchpoint. */
3a5c3e22 6805 if ((w1->cond_exp
4a64f543
MS
6806 && target_can_accel_watchpoint_condition (loc1->address,
6807 loc1->length,
0cf6dd15 6808 loc1->watchpoint_type,
4d01a485 6809 w1->cond_exp.get ()))
3a5c3e22 6810 || (w2->cond_exp
4a64f543
MS
6811 && target_can_accel_watchpoint_condition (loc2->address,
6812 loc2->length,
0cf6dd15 6813 loc2->watchpoint_type,
4d01a485 6814 w2->cond_exp.get ())))
0cf6dd15
TJB
6815 return 0;
6816
85d721b8
PA
6817 /* Note that this checks the owner's type, not the location's. In
6818 case the target does not support read watchpoints, but does
6819 support access watchpoints, we'll have bp_read_watchpoint
6820 watchpoints with hw_access locations. Those should be considered
6821 duplicates of hw_read locations. The hw_read locations will
6822 become hw_access locations later. */
2d134ed3
PA
6823 return (loc1->owner->type == loc2->owner->type
6824 && loc1->pspace->aspace == loc2->pspace->aspace
6825 && loc1->address == loc2->address
6826 && loc1->length == loc2->length);
e4f237da
KB
6827}
6828
31e77af2 6829/* See breakpoint.h. */
6c95b8df 6830
31e77af2 6831int
accd0bcd
YQ
6832breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6833 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6834{
f5656ead 6835 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6836 || aspace1 == aspace2)
6837 && addr1 == addr2);
6838}
6839
f1310107
TJB
6840/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6841 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6842 matches ASPACE2. On targets that have global breakpoints, the address
6843 space doesn't really matter. */
6844
6845static int
accd0bcd
YQ
6846breakpoint_address_match_range (const address_space *aspace1,
6847 CORE_ADDR addr1,
6848 int len1, const address_space *aspace2,
f1310107
TJB
6849 CORE_ADDR addr2)
6850{
f5656ead 6851 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6852 || aspace1 == aspace2)
6853 && addr2 >= addr1 && addr2 < addr1 + len1);
6854}
6855
6856/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6857 a ranged breakpoint. In most targets, a match happens only if ASPACE
6858 matches the breakpoint's address space. On targets that have global
6859 breakpoints, the address space doesn't really matter. */
6860
6861static int
6862breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6863 const address_space *aspace,
f1310107
TJB
6864 CORE_ADDR addr)
6865{
6866 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6867 aspace, addr)
6868 || (bl->length
6869 && breakpoint_address_match_range (bl->pspace->aspace,
6870 bl->address, bl->length,
6871 aspace, addr)));
6872}
6873
d35ae833
PA
6874/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6875 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6876 match happens only if ASPACE matches the breakpoint's address
6877 space. On targets that have global breakpoints, the address space
6878 doesn't really matter. */
6879
6880static int
6881breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6882 const address_space *aspace,
d35ae833
PA
6883 CORE_ADDR addr, int len)
6884{
6885 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6886 || bl->pspace->aspace == aspace)
6887 {
6888 int bl_len = bl->length != 0 ? bl->length : 1;
6889
6890 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6891 return 1;
6892 }
6893 return 0;
6894}
6895
1e4d1764
YQ
6896/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6897 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6898 true, otherwise returns false. */
6899
6900static int
6901tracepoint_locations_match (struct bp_location *loc1,
6902 struct bp_location *loc2)
6903{
6904 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6905 /* Since tracepoint locations are never duplicated with others', tracepoint
6906 locations at the same address of different tracepoints are regarded as
6907 different locations. */
6908 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6909 else
6910 return 0;
6911}
6912
2d134ed3 6913/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6914 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6915 the same location. If SW_HW_BPS_MATCH is true, then software
6916 breakpoint locations and hardware breakpoint locations match,
6917 otherwise they don't. */
2d134ed3
PA
6918
6919static int
7f32a4d5
PA
6920breakpoint_locations_match (struct bp_location *loc1,
6921 struct bp_location *loc2,
6922 bool sw_hw_bps_match)
2d134ed3 6923{
2bdf28a0
JK
6924 int hw_point1, hw_point2;
6925
6926 /* Both of them must not be in moribund_locations. */
6927 gdb_assert (loc1->owner != NULL);
6928 gdb_assert (loc2->owner != NULL);
6929
6930 hw_point1 = is_hardware_watchpoint (loc1->owner);
6931 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6932
6933 if (hw_point1 != hw_point2)
6934 return 0;
6935 else if (hw_point1)
6936 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6937 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6938 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6939 else
7f32a4d5
PA
6940 /* We compare bp_location.length in order to cover ranged
6941 breakpoints. Keep this in sync with
6942 bp_location_is_less_than. */
f1310107
TJB
6943 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6944 loc2->pspace->aspace, loc2->address)
7f32a4d5 6945 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6946 && loc1->length == loc2->length);
2d134ed3
PA
6947}
6948
76897487
KB
6949static void
6950breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 6951 int bnum, int have_bnum)
76897487 6952{
f63fbe86
MS
6953 /* The longest string possibly returned by hex_string_custom
6954 is 50 chars. These must be at least that big for safety. */
6955 char astr1[64];
6956 char astr2[64];
76897487 6957
bb599908
PH
6958 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6959 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6960 if (have_bnum)
8a3fe4f8 6961 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 6962 bnum, astr1, astr2);
76897487 6963 else
8a3fe4f8 6964 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6965}
6966
4a64f543
MS
6967/* Adjust a breakpoint's address to account for architectural
6968 constraints on breakpoint placement. Return the adjusted address.
6969 Note: Very few targets require this kind of adjustment. For most
6970 targets, this function is simply the identity function. */
76897487
KB
6971
6972static CORE_ADDR
a6d9a66e
UW
6973adjust_breakpoint_address (struct gdbarch *gdbarch,
6974 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6975{
a0de8c21
YQ
6976 if (bptype == bp_watchpoint
6977 || bptype == bp_hardware_watchpoint
6978 || bptype == bp_read_watchpoint
6979 || bptype == bp_access_watchpoint
6980 || bptype == bp_catchpoint)
88f7da05
KB
6981 {
6982 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 6983 have their addresses modified. */
88f7da05
KB
6984 return bpaddr;
6985 }
7c16b83e
PA
6986 else if (bptype == bp_single_step)
6987 {
6988 /* Single-step breakpoints should not have their addresses
6989 modified. If there's any architectural constrain that
6990 applies to this address, then it should have already been
6991 taken into account when the breakpoint was created in the
6992 first place. If we didn't do this, stepping through e.g.,
6993 Thumb-2 IT blocks would break. */
6994 return bpaddr;
6995 }
76897487
KB
6996 else
6997 {
a0de8c21
YQ
6998 CORE_ADDR adjusted_bpaddr = bpaddr;
6999
7000 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7001 {
7002 /* Some targets have architectural constraints on the placement
7003 of breakpoint instructions. Obtain the adjusted address. */
7004 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7005 }
76897487 7006
a0de8c21 7007 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7008
7009 /* An adjusted breakpoint address can significantly alter
dda83cd7 7010 a user's expectations. Print a warning if an adjustment
76897487
KB
7011 is required. */
7012 if (adjusted_bpaddr != bpaddr)
7013 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7014
7015 return adjusted_bpaddr;
7016 }
7017}
7018
cb1e4e32
PA
7019static bp_loc_type
7020bp_location_from_bp_type (bptype type)
7cc221ef 7021{
cb1e4e32 7022 switch (type)
e049a4b5
DJ
7023 {
7024 case bp_breakpoint:
7c16b83e 7025 case bp_single_step:
e049a4b5
DJ
7026 case bp_until:
7027 case bp_finish:
7028 case bp_longjmp:
7029 case bp_longjmp_resume:
e2e4d78b 7030 case bp_longjmp_call_dummy:
186c406b
TT
7031 case bp_exception:
7032 case bp_exception_resume:
e049a4b5 7033 case bp_step_resume:
2c03e5be 7034 case bp_hp_step_resume:
e049a4b5
DJ
7035 case bp_watchpoint_scope:
7036 case bp_call_dummy:
aa7d318d 7037 case bp_std_terminate:
e049a4b5
DJ
7038 case bp_shlib_event:
7039 case bp_thread_event:
7040 case bp_overlay_event:
4efc6507 7041 case bp_jit_event:
0fd8e87f 7042 case bp_longjmp_master:
aa7d318d 7043 case bp_std_terminate_master:
186c406b 7044 case bp_exception_master:
0e30163f
JK
7045 case bp_gnu_ifunc_resolver:
7046 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7047 case bp_dprintf:
cb1e4e32 7048 return bp_loc_software_breakpoint;
e049a4b5 7049 case bp_hardware_breakpoint:
cb1e4e32 7050 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7051 case bp_hardware_watchpoint:
7052 case bp_read_watchpoint:
7053 case bp_access_watchpoint:
cb1e4e32 7054 return bp_loc_hardware_watchpoint;
e049a4b5 7055 case bp_watchpoint:
ce78b96d 7056 case bp_catchpoint:
15c3d785
PA
7057 case bp_tracepoint:
7058 case bp_fast_tracepoint:
0fb4aa4b 7059 case bp_static_tracepoint:
cb1e4e32 7060 return bp_loc_other;
e049a4b5 7061 default:
e2e0b3e5 7062 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7063 }
cb1e4e32
PA
7064}
7065
7066bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7067{
7068 this->owner = owner;
7069 this->cond_bytecode = NULL;
7070 this->shlib_disabled = 0;
7071 this->enabled = 1;
b5fa468f 7072 this->disabled_by_cond = false;
cb1e4e32
PA
7073
7074 this->loc_type = type;
e049a4b5 7075
cb1e4e32
PA
7076 if (this->loc_type == bp_loc_software_breakpoint
7077 || this->loc_type == bp_loc_hardware_breakpoint)
7078 mark_breakpoint_location_modified (this);
7079
b6433ede 7080 incref ();
cb1e4e32
PA
7081}
7082
7083bp_location::bp_location (breakpoint *owner)
7084 : bp_location::bp_location (owner,
7085 bp_location_from_bp_type (owner->type))
7086{
28010a5d
PA
7087}
7088
7089/* Allocate a struct bp_location. */
7090
7091static struct bp_location *
7092allocate_bp_location (struct breakpoint *bpt)
7093{
348d480f
PA
7094 return bpt->ops->allocate_location (bpt);
7095}
7cc221ef 7096
f431efe5
PA
7097/* Decrement reference count. If the reference count reaches 0,
7098 destroy the bp_location. Sets *BLP to NULL. */
7099
7100static void
7101decref_bp_location (struct bp_location **blp)
7102{
b6433ede 7103 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7104 *blp = NULL;
7105}
7106
346774a9 7107/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7108
b270e6f9
TT
7109static breakpoint *
7110add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7111{
346774a9 7112 struct breakpoint *b1;
b270e6f9 7113 struct breakpoint *result = b.get ();
c906108c 7114
346774a9
PA
7115 /* Add this breakpoint to the end of the chain so that a list of
7116 breakpoints will come out in order of increasing numbers. */
7117
7118 b1 = breakpoint_chain;
7119 if (b1 == 0)
b270e6f9 7120 breakpoint_chain = b.release ();
346774a9
PA
7121 else
7122 {
7123 while (b1->next)
7124 b1 = b1->next;
b270e6f9 7125 b1->next = b.release ();
346774a9 7126 }
b270e6f9
TT
7127
7128 return result;
346774a9
PA
7129}
7130
7131/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7132
7133static void
7134init_raw_breakpoint_without_location (struct breakpoint *b,
7135 struct gdbarch *gdbarch,
28010a5d 7136 enum bptype bptype,
c0a91b2b 7137 const struct breakpoint_ops *ops)
346774a9 7138{
348d480f
PA
7139 gdb_assert (ops != NULL);
7140
28010a5d 7141 b->ops = ops;
4d28f7a8 7142 b->type = bptype;
a6d9a66e 7143 b->gdbarch = gdbarch;
c906108c
SS
7144 b->language = current_language->la_language;
7145 b->input_radix = input_radix;
d0fb5eae 7146 b->related_breakpoint = b;
346774a9
PA
7147}
7148
7149/* Helper to set_raw_breakpoint below. Creates a breakpoint
7150 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7151
7152static struct breakpoint *
7153set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7154 enum bptype bptype,
c0a91b2b 7155 const struct breakpoint_ops *ops)
346774a9 7156{
3b0871f4 7157 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7158
3b0871f4 7159 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7160 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7161}
7162
0ba852ab 7163/* Initialize loc->function_name. */
0e30163f 7164
0d381245 7165static void
0ba852ab 7166set_breakpoint_location_function (struct bp_location *loc)
0d381245 7167{
2bdf28a0
JK
7168 gdb_assert (loc->owner != NULL);
7169
0d381245 7170 if (loc->owner->type == bp_breakpoint
1042e4c0 7171 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7172 || is_tracepoint (loc->owner))
0d381245 7173 {
2c02bd72 7174 const char *function_name;
0e30163f 7175
3467ec66 7176 if (loc->msymbol != NULL
f50776aa 7177 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7178 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7179 {
7180 struct breakpoint *b = loc->owner;
7181
c9d95fa3 7182 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7183
7184 if (b->type == bp_breakpoint && b->loc == loc
7185 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7186 {
7187 /* Create only the whole new breakpoint of this type but do not
7188 mess more complicated breakpoints with multiple locations. */
7189 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7190 /* Remember the resolver's address for use by the return
dda83cd7 7191 breakpoint. */
3467ec66 7192 loc->related_address = loc->address;
0e30163f
JK
7193 }
7194 }
3467ec66
PA
7195 else
7196 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7197
2c02bd72
DE
7198 if (function_name)
7199 loc->function_name = xstrdup (function_name);
0d381245
VP
7200 }
7201}
7202
a6d9a66e 7203/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7204struct gdbarch *
a6d9a66e
UW
7205get_sal_arch (struct symtab_and_line sal)
7206{
7207 if (sal.section)
08feed99 7208 return sal.section->objfile->arch ();
a6d9a66e 7209 if (sal.symtab)
08feed99 7210 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7211
7212 return NULL;
7213}
7214
346774a9
PA
7215/* Low level routine for partially initializing a breakpoint of type
7216 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7217 file name, and line number are provided by SAL.
0d381245
VP
7218
7219 It is expected that the caller will complete the initialization of
7220 the newly created breakpoint struct as well as output any status
c56053d2 7221 information regarding the creation of a new breakpoint. */
0d381245 7222
346774a9
PA
7223static void
7224init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7225 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7226 const struct breakpoint_ops *ops)
0d381245 7227{
28010a5d 7228 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7229
3742cc8b 7230 add_location_to_breakpoint (b, &sal);
0d381245 7231
6c95b8df
PA
7232 if (bptype != bp_catchpoint)
7233 gdb_assert (sal.pspace != NULL);
7234
f8eba3c6
TT
7235 /* Store the program space that was used to set the breakpoint,
7236 except for ordinary breakpoints, which are independent of the
7237 program space. */
7238 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7239 b->pspace = sal.pspace;
346774a9 7240}
c906108c 7241
346774a9
PA
7242/* set_raw_breakpoint is a low level routine for allocating and
7243 partially initializing a breakpoint of type BPTYPE. The newly
7244 created breakpoint's address, section, source file name, and line
7245 number are provided by SAL. The newly created and partially
7246 initialized breakpoint is added to the breakpoint chain and
7247 is also returned as the value of this function.
7248
7249 It is expected that the caller will complete the initialization of
7250 the newly created breakpoint struct as well as output any status
7251 information regarding the creation of a new breakpoint. In
7252 particular, set_raw_breakpoint does NOT set the breakpoint
7253 number! Care should be taken to not allow an error to occur
7254 prior to completing the initialization of the breakpoint. If this
7255 should happen, a bogus breakpoint will be left on the chain. */
7256
7257struct breakpoint *
7258set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7259 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7260 const struct breakpoint_ops *ops)
346774a9 7261{
3b0871f4 7262 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7263
3b0871f4 7264 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7265 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7266}
7267
53a5351d 7268/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7269 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7270 initiated the operation. */
c906108c
SS
7271
7272void
186c406b 7273set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7274{
5d5658a1 7275 int thread = tp->global_num;
0fd8e87f
UW
7276
7277 /* To avoid having to rescan all objfile symbols at every step,
7278 we maintain a list of continually-inserted but always disabled
7279 longjmp "master" breakpoints. Here, we simply create momentary
7280 clones of those and enable them for the requested thread. */
1428b37a 7281 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7282 if (b->pspace == current_program_space
186c406b
TT
7283 && (b->type == bp_longjmp_master
7284 || b->type == bp_exception_master))
0fd8e87f 7285 {
06edf0c0
PA
7286 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7287 struct breakpoint *clone;
cc59ec59 7288
e2e4d78b
JK
7289 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7290 after their removal. */
06edf0c0 7291 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7292 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7293 clone->thread = thread;
7294 }
186c406b
TT
7295
7296 tp->initiating_frame = frame;
c906108c
SS
7297}
7298
611c83ae 7299/* Delete all longjmp breakpoints from THREAD. */
c906108c 7300void
611c83ae 7301delete_longjmp_breakpoint (int thread)
c906108c 7302{
1428b37a 7303 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7304 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7305 {
7306 if (b->thread == thread)
7307 delete_breakpoint (b);
7308 }
c906108c
SS
7309}
7310
f59f708a
PA
7311void
7312delete_longjmp_breakpoint_at_next_stop (int thread)
7313{
1428b37a 7314 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7315 if (b->type == bp_longjmp || b->type == bp_exception)
7316 {
7317 if (b->thread == thread)
7318 b->disposition = disp_del_at_next_stop;
7319 }
7320}
7321
e2e4d78b
JK
7322/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7323 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7324 pointer to any of them. Return NULL if this system cannot place longjmp
7325 breakpoints. */
7326
7327struct breakpoint *
7328set_longjmp_breakpoint_for_call_dummy (void)
7329{
43892fdf 7330 breakpoint *retval = nullptr;
e2e4d78b 7331
43892fdf 7332 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7333 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7334 {
7335 struct breakpoint *new_b;
7336
7337 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7338 &momentary_breakpoint_ops,
7339 1);
00431a78 7340 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7341
7342 /* Link NEW_B into the chain of RETVAL breakpoints. */
7343
7344 gdb_assert (new_b->related_breakpoint == new_b);
7345 if (retval == NULL)
7346 retval = new_b;
7347 new_b->related_breakpoint = retval;
7348 while (retval->related_breakpoint != new_b->related_breakpoint)
7349 retval = retval->related_breakpoint;
7350 retval->related_breakpoint = new_b;
7351 }
7352
7353 return retval;
7354}
7355
7356/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7357 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7358 stack.
7359
b4b3e2de
AB
7360 If the unwind fails then there is not sufficient information to discard
7361 dummy frames. In this case, elide the clean up and the dummy frames will
7362 be cleaned up next time this function is called from a location where
7363 unwinding is possible. */
e2e4d78b
JK
7364
7365void
b67a2c6f 7366check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7367{
7368 struct breakpoint *b, *b_tmp;
7369
7370 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7371 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7372 {
7373 struct breakpoint *dummy_b = b->related_breakpoint;
7374
b4b3e2de
AB
7375 /* Find the bp_call_dummy breakpoint in the list of breakpoints
7376 chained off b->related_breakpoint. */
e2e4d78b
JK
7377 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7378 dummy_b = dummy_b->related_breakpoint;
b4b3e2de
AB
7379
7380 /* If there was no bp_call_dummy breakpoint then there's nothing
7381 more to do. Or, if the dummy frame associated with the
7382 bp_call_dummy is still on the stack then we need to leave this
7383 bp_call_dummy in place. */
e2e4d78b
JK
7384 if (dummy_b->type != bp_call_dummy
7385 || frame_find_by_id (dummy_b->frame_id) != NULL)
7386 continue;
b4b3e2de
AB
7387
7388 /* We didn't find the dummy frame on the stack, this could be
7389 because we have longjmp'd to a stack frame that is previous to
7390 the dummy frame, or it could be because the stack unwind is
7391 broken at some point between the longjmp frame and the dummy
7392 frame.
7393
7394 Next we figure out why the stack unwind stopped. If it looks
7395 like the unwind is complete then we assume the dummy frame has
7396 been jumped over, however, if the unwind stopped for an
7397 unexpected reason then we assume the stack unwind is currently
7398 broken, and that we will (eventually) return to the dummy
7399 frame.
7400
7401 It might be tempting to consider using frame_id_inner here, but
7402 that is not safe. There is no guarantee that the stack frames
7403 we are looking at here are even on the same stack as the
7404 original dummy frame, hence frame_id_inner can't be used. See
7405 the comments on frame_id_inner for more details. */
7406 bool unwind_finished_unexpectedly = false;
7407 for (struct frame_info *fi = get_current_frame (); fi != nullptr; )
7408 {
7409 struct frame_info *prev = get_prev_frame (fi);
7410 if (prev == nullptr)
7411 {
7412 /* FI is the last stack frame. Why did this frame not
7413 unwind further? */
7414 auto stop_reason = get_frame_unwind_stop_reason (fi);
7415 if (stop_reason != UNWIND_NO_REASON
7416 && stop_reason != UNWIND_OUTERMOST)
7417 unwind_finished_unexpectedly = true;
7418 }
7419 fi = prev;
7420 }
7421 if (unwind_finished_unexpectedly)
7422 continue;
7423
00431a78 7424 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7425
7426 while (b->related_breakpoint != b)
7427 {
7428 if (b_tmp == b->related_breakpoint)
7429 b_tmp = b->related_breakpoint->next;
7430 delete_breakpoint (b->related_breakpoint);
7431 }
7432 delete_breakpoint (b);
7433 }
7434}
7435
1900040c
MS
7436void
7437enable_overlay_breakpoints (void)
7438{
43892fdf 7439 for (breakpoint *b : all_breakpoints ())
1900040c 7440 if (b->type == bp_overlay_event)
01add95b
SM
7441 {
7442 b->enable_state = bp_enabled;
7443 update_global_location_list (UGLL_MAY_INSERT);
7444 overlay_events_enabled = 1;
7445 }
1900040c
MS
7446}
7447
7448void
7449disable_overlay_breakpoints (void)
7450{
43892fdf 7451 for (breakpoint *b : all_breakpoints ())
1900040c 7452 if (b->type == bp_overlay_event)
01add95b
SM
7453 {
7454 b->enable_state = bp_disabled;
7455 update_global_location_list (UGLL_DONT_INSERT);
7456 overlay_events_enabled = 0;
7457 }
1900040c
MS
7458}
7459
aa7d318d
TT
7460/* Set an active std::terminate breakpoint for each std::terminate
7461 master breakpoint. */
7462void
7463set_std_terminate_breakpoint (void)
7464{
1428b37a 7465 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7466 if (b->pspace == current_program_space
7467 && b->type == bp_std_terminate_master)
7468 {
06edf0c0 7469 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7470 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7471 }
7472}
7473
7474/* Delete all the std::terminate breakpoints. */
7475void
7476delete_std_terminate_breakpoint (void)
7477{
1428b37a 7478 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7479 if (b->type == bp_std_terminate)
7480 delete_breakpoint (b);
7481}
7482
c4093a6a 7483struct breakpoint *
a6d9a66e 7484create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7485{
7486 struct breakpoint *b;
c4093a6a 7487
06edf0c0
PA
7488 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7489 &internal_breakpoint_ops);
7490
b5de0fa7 7491 b->enable_state = bp_enabled;
f00aae0f 7492 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7493 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7494
44702360 7495 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7496
c4093a6a
JM
7497 return b;
7498}
7499
0101ce28
JJ
7500struct lang_and_radix
7501 {
7502 enum language lang;
7503 int radix;
7504 };
7505
4efc6507
DE
7506/* Create a breakpoint for JIT code registration and unregistration. */
7507
7508struct breakpoint *
7509create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7510{
2a7f3dff
PA
7511 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7512 &internal_breakpoint_ops);
4efc6507 7513}
0101ce28 7514
03673fc7
PP
7515/* Remove JIT code registration and unregistration breakpoint(s). */
7516
7517void
7518remove_jit_event_breakpoints (void)
7519{
1428b37a 7520 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7521 if (b->type == bp_jit_event
7522 && b->loc->pspace == current_program_space)
7523 delete_breakpoint (b);
7524}
7525
cae688ec
JJ
7526void
7527remove_solib_event_breakpoints (void)
7528{
1428b37a 7529 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7530 if (b->type == bp_shlib_event
7531 && b->loc->pspace == current_program_space)
cae688ec
JJ
7532 delete_breakpoint (b);
7533}
7534
f37f681c
PA
7535/* See breakpoint.h. */
7536
7537void
7538remove_solib_event_breakpoints_at_next_stop (void)
7539{
1428b37a 7540 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7541 if (b->type == bp_shlib_event
7542 && b->loc->pspace == current_program_space)
7543 b->disposition = disp_del_at_next_stop;
7544}
7545
04086b45
PA
7546/* Helper for create_solib_event_breakpoint /
7547 create_and_insert_solib_event_breakpoint. Allows specifying which
7548 INSERT_MODE to pass through to update_global_location_list. */
7549
7550static struct breakpoint *
7551create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7552 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7553{
7554 struct breakpoint *b;
7555
06edf0c0
PA
7556 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7557 &internal_breakpoint_ops);
04086b45 7558 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7559 return b;
7560}
7561
04086b45
PA
7562struct breakpoint *
7563create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7564{
7565 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7566}
7567
f37f681c
PA
7568/* See breakpoint.h. */
7569
7570struct breakpoint *
7571create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7572{
7573 struct breakpoint *b;
7574
04086b45
PA
7575 /* Explicitly tell update_global_location_list to insert
7576 locations. */
7577 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7578 if (!b->loc->inserted)
7579 {
7580 delete_breakpoint (b);
7581 return NULL;
7582 }
7583 return b;
7584}
7585
cae688ec
JJ
7586/* Disable any breakpoints that are on code in shared libraries. Only
7587 apply to enabled breakpoints, disabled ones can just stay disabled. */
7588
7589void
cb851954 7590disable_breakpoints_in_shlibs (void)
cae688ec 7591{
48d7020b 7592 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7593 {
7594 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7595 struct breakpoint *b = loc->owner;
7596
7597 /* We apply the check to all breakpoints, including disabled for
7598 those with loc->duplicate set. This is so that when breakpoint
7599 becomes enabled, or the duplicate is removed, gdb will try to
7600 insert all breakpoints. If we don't set shlib_disabled here,
7601 we'll try to insert those breakpoints and fail. */
7602 if (((b->type == bp_breakpoint)
7603 || (b->type == bp_jit_event)
7604 || (b->type == bp_hardware_breakpoint)
7605 || (is_tracepoint (b)))
7606 && loc->pspace == current_program_space
7607 && !loc->shlib_disabled
7608 && solib_name_from_address (loc->pspace, loc->address)
7609 )
7610 {
7611 loc->shlib_disabled = 1;
7612 }
7613 }
cae688ec
JJ
7614}
7615
63644780
NB
7616/* Disable any breakpoints and tracepoints that are in SOLIB upon
7617 notification of unloaded_shlib. Only apply to enabled breakpoints,
7618 disabled ones can just stay disabled. */
84acb35a 7619
75149521 7620static void
84acb35a
JJ
7621disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7622{
84acb35a
JJ
7623 int disabled_shlib_breaks = 0;
7624
48d7020b 7625 for (bp_location *loc : all_bp_locations ())
01add95b
SM
7626 {
7627 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7628 struct breakpoint *b = loc->owner;
7629
7630 if (solib->pspace == loc->pspace
7631 && !loc->shlib_disabled
7632 && (((b->type == bp_breakpoint
7633 || b->type == bp_jit_event
7634 || b->type == bp_hardware_breakpoint)
7635 && (loc->loc_type == bp_loc_hardware_breakpoint
7636 || loc->loc_type == bp_loc_software_breakpoint))
7637 || is_tracepoint (b))
7638 && solib_contains_address_p (solib, loc->address))
7639 {
7640 loc->shlib_disabled = 1;
7641 /* At this point, we cannot rely on remove_breakpoint
7642 succeeding so we must mark the breakpoint as not inserted
7643 to prevent future errors occurring in remove_breakpoints. */
7644 loc->inserted = 0;
7645
7646 /* This may cause duplicate notifications for the same breakpoint. */
7647 gdb::observers::breakpoint_modified.notify (b);
7648
7649 if (!disabled_shlib_breaks)
7650 {
7651 target_terminal::ours_for_output ();
7652 warning (_("Temporarily disabling breakpoints "
7653 "for unloaded shared library \"%s\""),
7654 solib->so_name);
7655 }
7656 disabled_shlib_breaks = 1;
7657 }
7658 }
84acb35a
JJ
7659}
7660
63644780
NB
7661/* Disable any breakpoints and tracepoints in OBJFILE upon
7662 notification of free_objfile. Only apply to enabled breakpoints,
7663 disabled ones can just stay disabled. */
7664
7665static void
7666disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7667{
63644780
NB
7668 if (objfile == NULL)
7669 return;
7670
d03de421
PA
7671 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7672 managed by the user with add-symbol-file/remove-symbol-file.
7673 Similarly to how breakpoints in shared libraries are handled in
7674 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7675 shlib_disabled so they end up uninserted on the next global
7676 location list update. Shared libraries not loaded by the user
7677 aren't handled here -- they're already handled in
7678 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7679 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7680 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7681 main objfile). */
7682 if ((objfile->flags & OBJF_SHARED) == 0
7683 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7684 return;
7685
43892fdf 7686 for (breakpoint *b : all_breakpoints ())
63644780 7687 {
63644780
NB
7688 int bp_modified = 0;
7689
7690 if (!is_breakpoint (b) && !is_tracepoint (b))
7691 continue;
7692
40cb8ca5 7693 for (bp_location *loc : b->locations ())
63644780
NB
7694 {
7695 CORE_ADDR loc_addr = loc->address;
7696
7697 if (loc->loc_type != bp_loc_hardware_breakpoint
7698 && loc->loc_type != bp_loc_software_breakpoint)
7699 continue;
7700
7701 if (loc->shlib_disabled != 0)
7702 continue;
7703
7704 if (objfile->pspace != loc->pspace)
7705 continue;
7706
7707 if (loc->loc_type != bp_loc_hardware_breakpoint
7708 && loc->loc_type != bp_loc_software_breakpoint)
7709 continue;
7710
7711 if (is_addr_in_objfile (loc_addr, objfile))
7712 {
7713 loc->shlib_disabled = 1;
08351840
PA
7714 /* At this point, we don't know whether the object was
7715 unmapped from the inferior or not, so leave the
7716 inserted flag alone. We'll handle failure to
7717 uninsert quietly, in case the object was indeed
7718 unmapped. */
63644780
NB
7719
7720 mark_breakpoint_location_modified (loc);
7721
7722 bp_modified = 1;
7723 }
7724 }
7725
7726 if (bp_modified)
76727919 7727 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7728 }
7729}
7730
ce78b96d
JB
7731/* FORK & VFORK catchpoints. */
7732
e29a4733 7733/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7734 catchpoint. A breakpoint is really of this type iff its ops pointer points
7735 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7736
c1fc2657 7737struct fork_catchpoint : public breakpoint
e29a4733 7738{
e29a4733
PA
7739 /* Process id of a child process whose forking triggered this
7740 catchpoint. This field is only valid immediately after this
7741 catchpoint has triggered. */
7742 ptid_t forked_inferior_pid;
7743};
7744
4a64f543
MS
7745/* Implement the "insert" breakpoint_ops method for fork
7746 catchpoints. */
ce78b96d 7747
77b06cd7
TJB
7748static int
7749insert_catch_fork (struct bp_location *bl)
ce78b96d 7750{
e99b03dc 7751 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7752}
7753
4a64f543
MS
7754/* Implement the "remove" breakpoint_ops method for fork
7755 catchpoints. */
ce78b96d
JB
7756
7757static int
73971819 7758remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7759{
e99b03dc 7760 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7761}
7762
7763/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7764 catchpoints. */
7765
7766static int
f1310107 7767breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7768 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7769 const struct target_waitstatus *ws)
ce78b96d 7770{
e29a4733
PA
7771 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7772
f90263c1
TT
7773 if (ws->kind != TARGET_WAITKIND_FORKED)
7774 return 0;
7775
7776 c->forked_inferior_pid = ws->value.related_pid;
7777 return 1;
ce78b96d
JB
7778}
7779
4a64f543
MS
7780/* Implement the "print_it" breakpoint_ops method for fork
7781 catchpoints. */
ce78b96d
JB
7782
7783static enum print_stop_action
348d480f 7784print_it_catch_fork (bpstat bs)
ce78b96d 7785{
36dfb11c 7786 struct ui_out *uiout = current_uiout;
348d480f
PA
7787 struct breakpoint *b = bs->breakpoint_at;
7788 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7789
ce78b96d 7790 annotate_catchpoint (b->number);
f303dbd6 7791 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7792 if (b->disposition == disp_del)
112e8700 7793 uiout->text ("Temporary catchpoint ");
36dfb11c 7794 else
112e8700
SM
7795 uiout->text ("Catchpoint ");
7796 if (uiout->is_mi_like_p ())
36dfb11c 7797 {
112e8700
SM
7798 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7799 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7800 }
381befee 7801 uiout->field_signed ("bkptno", b->number);
112e8700 7802 uiout->text (" (forked process ");
381befee 7803 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7804 uiout->text ("), ");
ce78b96d
JB
7805 return PRINT_SRC_AND_LOC;
7806}
7807
4a64f543
MS
7808/* Implement the "print_one" breakpoint_ops method for fork
7809 catchpoints. */
ce78b96d
JB
7810
7811static void
a6d9a66e 7812print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7813{
e29a4733 7814 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7815 struct value_print_options opts;
79a45e25 7816 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7817
7818 get_user_print_options (&opts);
7819
4a64f543
MS
7820 /* Field 4, the address, is omitted (which makes the columns not
7821 line up too nicely with the headers, but the effect is relatively
7822 readable). */
79a45b7d 7823 if (opts.addressprint)
112e8700 7824 uiout->field_skip ("addr");
ce78b96d 7825 annotate_field (5);
112e8700 7826 uiout->text ("fork");
d7e15655 7827 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7828 {
112e8700 7829 uiout->text (", process ");
381befee 7830 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7831 uiout->spaces (1);
ce78b96d 7832 }
8ac3646f 7833
112e8700
SM
7834 if (uiout->is_mi_like_p ())
7835 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7836}
7837
7838/* Implement the "print_mention" breakpoint_ops method for fork
7839 catchpoints. */
7840
7841static void
7842print_mention_catch_fork (struct breakpoint *b)
7843{
7844 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7845}
7846
6149aea9
PA
7847/* Implement the "print_recreate" breakpoint_ops method for fork
7848 catchpoints. */
7849
7850static void
7851print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7852{
7853 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7854 print_recreate_thread (b, fp);
6149aea9
PA
7855}
7856
ce78b96d
JB
7857/* The breakpoint_ops structure to be used in fork catchpoints. */
7858
2060206e 7859static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7860
4a64f543
MS
7861/* Implement the "insert" breakpoint_ops method for vfork
7862 catchpoints. */
ce78b96d 7863
77b06cd7
TJB
7864static int
7865insert_catch_vfork (struct bp_location *bl)
ce78b96d 7866{
e99b03dc 7867 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7868}
7869
4a64f543
MS
7870/* Implement the "remove" breakpoint_ops method for vfork
7871 catchpoints. */
ce78b96d
JB
7872
7873static int
73971819 7874remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7875{
e99b03dc 7876 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7877}
7878
7879/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7880 catchpoints. */
7881
7882static int
f1310107 7883breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7884 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7885 const struct target_waitstatus *ws)
ce78b96d 7886{
e29a4733
PA
7887 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7888
f90263c1
TT
7889 if (ws->kind != TARGET_WAITKIND_VFORKED)
7890 return 0;
7891
7892 c->forked_inferior_pid = ws->value.related_pid;
7893 return 1;
ce78b96d
JB
7894}
7895
4a64f543
MS
7896/* Implement the "print_it" breakpoint_ops method for vfork
7897 catchpoints. */
ce78b96d
JB
7898
7899static enum print_stop_action
348d480f 7900print_it_catch_vfork (bpstat bs)
ce78b96d 7901{
36dfb11c 7902 struct ui_out *uiout = current_uiout;
348d480f 7903 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7904 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7905
ce78b96d 7906 annotate_catchpoint (b->number);
f303dbd6 7907 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7908 if (b->disposition == disp_del)
112e8700 7909 uiout->text ("Temporary catchpoint ");
36dfb11c 7910 else
112e8700
SM
7911 uiout->text ("Catchpoint ");
7912 if (uiout->is_mi_like_p ())
36dfb11c 7913 {
112e8700
SM
7914 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7915 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7916 }
381befee 7917 uiout->field_signed ("bkptno", b->number);
112e8700 7918 uiout->text (" (vforked process ");
381befee 7919 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7920 uiout->text ("), ");
ce78b96d
JB
7921 return PRINT_SRC_AND_LOC;
7922}
7923
4a64f543
MS
7924/* Implement the "print_one" breakpoint_ops method for vfork
7925 catchpoints. */
ce78b96d
JB
7926
7927static void
a6d9a66e 7928print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7929{
e29a4733 7930 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7931 struct value_print_options opts;
79a45e25 7932 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7933
7934 get_user_print_options (&opts);
4a64f543
MS
7935 /* Field 4, the address, is omitted (which makes the columns not
7936 line up too nicely with the headers, but the effect is relatively
7937 readable). */
79a45b7d 7938 if (opts.addressprint)
112e8700 7939 uiout->field_skip ("addr");
ce78b96d 7940 annotate_field (5);
112e8700 7941 uiout->text ("vfork");
d7e15655 7942 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7943 {
112e8700 7944 uiout->text (", process ");
381befee 7945 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7946 uiout->spaces (1);
ce78b96d 7947 }
8ac3646f 7948
112e8700
SM
7949 if (uiout->is_mi_like_p ())
7950 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7951}
7952
7953/* Implement the "print_mention" breakpoint_ops method for vfork
7954 catchpoints. */
7955
7956static void
7957print_mention_catch_vfork (struct breakpoint *b)
7958{
7959 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7960}
7961
6149aea9
PA
7962/* Implement the "print_recreate" breakpoint_ops method for vfork
7963 catchpoints. */
7964
7965static void
7966print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7967{
7968 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7969 print_recreate_thread (b, fp);
6149aea9
PA
7970}
7971
ce78b96d
JB
7972/* The breakpoint_ops structure to be used in vfork catchpoints. */
7973
2060206e 7974static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7975
edcc5120 7976/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7977 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7978 CATCH_SOLIB_BREAKPOINT_OPS. */
7979
c1fc2657 7980struct solib_catchpoint : public breakpoint
edcc5120 7981{
c1fc2657 7982 ~solib_catchpoint () override;
edcc5120
TT
7983
7984 /* True for "catch load", false for "catch unload". */
b650a282 7985 bool is_load;
edcc5120
TT
7986
7987 /* Regular expression to match, if any. COMPILED is only valid when
7988 REGEX is non-NULL. */
7989 char *regex;
2d7cc5c7 7990 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7991};
7992
c1fc2657 7993solib_catchpoint::~solib_catchpoint ()
edcc5120 7994{
c1fc2657 7995 xfree (this->regex);
edcc5120
TT
7996}
7997
7998static int
7999insert_catch_solib (struct bp_location *ignore)
8000{
8001 return 0;
8002}
8003
8004static int
73971819 8005remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8006{
8007 return 0;
8008}
8009
8010static int
8011breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8012 const address_space *aspace,
edcc5120
TT
8013 CORE_ADDR bp_addr,
8014 const struct target_waitstatus *ws)
8015{
8016 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
edcc5120
TT
8017
8018 if (ws->kind == TARGET_WAITKIND_LOADED)
8019 return 1;
8020
43892fdf 8021 for (breakpoint *other : all_breakpoints ())
01add95b
SM
8022 {
8023 if (other == bl->owner)
8024 continue;
edcc5120 8025
01add95b
SM
8026 if (other->type != bp_shlib_event)
8027 continue;
edcc5120 8028
01add95b
SM
8029 if (self->pspace != NULL && other->pspace != self->pspace)
8030 continue;
edcc5120 8031
01add95b
SM
8032 for (bp_location *other_bl : other->locations ())
8033 {
8034 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8035 return 1;
8036 }
8037 }
edcc5120
TT
8038
8039 return 0;
8040}
8041
8042static void
8043check_status_catch_solib (struct bpstats *bs)
8044{
8045 struct solib_catchpoint *self
8046 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8047
8048 if (self->is_load)
8049 {
52941706 8050 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8051 {
8052 if (!self->regex
2d7cc5c7 8053 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8054 return;
8055 }
8056 }
8057 else
8058 {
6fb16ce6 8059 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8060 {
8061 if (!self->regex
6fb16ce6 8062 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8063 return;
8064 }
8065 }
8066
8067 bs->stop = 0;
8068 bs->print_it = print_it_noop;
8069}
8070
8071static enum print_stop_action
8072print_it_catch_solib (bpstat bs)
8073{
8074 struct breakpoint *b = bs->breakpoint_at;
8075 struct ui_out *uiout = current_uiout;
8076
8077 annotate_catchpoint (b->number);
f303dbd6 8078 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8079 if (b->disposition == disp_del)
112e8700 8080 uiout->text ("Temporary catchpoint ");
edcc5120 8081 else
112e8700 8082 uiout->text ("Catchpoint ");
381befee 8083 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8084 uiout->text ("\n");
8085 if (uiout->is_mi_like_p ())
8086 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8087 print_solib_event (1);
8088 return PRINT_SRC_AND_LOC;
8089}
8090
8091static void
8092print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8093{
8094 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8095 struct value_print_options opts;
8096 struct ui_out *uiout = current_uiout;
edcc5120
TT
8097
8098 get_user_print_options (&opts);
8099 /* Field 4, the address, is omitted (which makes the columns not
8100 line up too nicely with the headers, but the effect is relatively
8101 readable). */
8102 if (opts.addressprint)
8103 {
8104 annotate_field (4);
112e8700 8105 uiout->field_skip ("addr");
edcc5120
TT
8106 }
8107
528e1572 8108 std::string msg;
edcc5120
TT
8109 annotate_field (5);
8110 if (self->is_load)
8111 {
8112 if (self->regex)
528e1572 8113 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8114 else
528e1572 8115 msg = _("load of library");
edcc5120
TT
8116 }
8117 else
8118 {
8119 if (self->regex)
528e1572 8120 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8121 else
528e1572 8122 msg = _("unload of library");
edcc5120 8123 }
112e8700 8124 uiout->field_string ("what", msg);
8ac3646f 8125
112e8700
SM
8126 if (uiout->is_mi_like_p ())
8127 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8128}
8129
8130static void
8131print_mention_catch_solib (struct breakpoint *b)
8132{
8133 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8134
8135 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8136 self->is_load ? "load" : "unload");
8137}
8138
8139static void
8140print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8141{
8142 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8143
8144 fprintf_unfiltered (fp, "%s %s",
8145 b->disposition == disp_del ? "tcatch" : "catch",
8146 self->is_load ? "load" : "unload");
8147 if (self->regex)
8148 fprintf_unfiltered (fp, " %s", self->regex);
8149 fprintf_unfiltered (fp, "\n");
8150}
8151
8152static struct breakpoint_ops catch_solib_breakpoint_ops;
8153
b650a282 8154/* See breakpoint.h. */
edcc5120 8155
91985142 8156void
b650a282 8157add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8158{
edcc5120 8159 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8160
edcc5120
TT
8161 if (!arg)
8162 arg = "";
f1735a53 8163 arg = skip_spaces (arg);
edcc5120 8164
36bd8eaa 8165 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8166
8167 if (*arg != '\0')
8168 {
2d7cc5c7
PA
8169 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8170 _("Invalid regexp")));
edcc5120
TT
8171 c->regex = xstrdup (arg);
8172 }
8173
8174 c->is_load = is_load;
36bd8eaa 8175 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8176 &catch_solib_breakpoint_ops);
8177
c1fc2657 8178 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8179
b270e6f9 8180 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8181}
8182
91985142
MG
8183/* A helper function that does all the work for "catch load" and
8184 "catch unload". */
8185
8186static void
eb4c3f4a 8187catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8188 struct cmd_list_element *command)
8189{
91985142 8190 const int enabled = 1;
b650a282 8191 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8192
b650a282 8193 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8194}
8195
edcc5120 8196static void
eb4c3f4a 8197catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8198 struct cmd_list_element *command)
8199{
8200 catch_load_or_unload (arg, from_tty, 1, command);
8201}
8202
8203static void
eb4c3f4a 8204catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8205 struct cmd_list_element *command)
8206{
8207 catch_load_or_unload (arg, from_tty, 0, command);
8208}
8209
b650a282 8210/* See breakpoint.h. */
ce78b96d 8211
ab04a2af 8212void
346774a9 8213init_catchpoint (struct breakpoint *b,
b650a282 8214 struct gdbarch *gdbarch, bool temp,
63160a43 8215 const char *cond_string,
c0a91b2b 8216 const struct breakpoint_ops *ops)
c906108c 8217{
51abb421 8218 symtab_and_line sal;
6c95b8df 8219 sal.pspace = current_program_space;
c5aa993b 8220
28010a5d 8221 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8222
1b36a34b 8223 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8224 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8225}
8226
28010a5d 8227void
b270e6f9 8228install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8229{
b270e6f9 8230 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8231 set_breakpoint_number (internal, b);
558a9d82
YQ
8232 if (is_tracepoint (b))
8233 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8234 if (!internal)
8235 mention (b);
76727919 8236 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8237
8238 if (update_gll)
44702360 8239 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8240}
8241
9b70b993 8242static void
a6d9a66e 8243create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8244 bool temp, const char *cond_string,
dda83cd7 8245 const struct breakpoint_ops *ops)
c906108c 8246{
b270e6f9 8247 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8248
b650a282 8249 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8250
8251 c->forked_inferior_pid = null_ptid;
8252
b270e6f9 8253 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8254}
8255
fe798b75
JB
8256/* Exec catchpoints. */
8257
b4d90040 8258/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8259 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8260 CATCH_EXEC_BREAKPOINT_OPS. */
8261
c1fc2657 8262struct exec_catchpoint : public breakpoint
b4d90040 8263{
c1fc2657 8264 ~exec_catchpoint () override;
b4d90040
PA
8265
8266 /* Filename of a program whose exec triggered this catchpoint.
8267 This field is only valid immediately after this catchpoint has
8268 triggered. */
8269 char *exec_pathname;
8270};
8271
c1fc2657 8272/* Exec catchpoint destructor. */
b4d90040 8273
c1fc2657 8274exec_catchpoint::~exec_catchpoint ()
b4d90040 8275{
c1fc2657 8276 xfree (this->exec_pathname);
b4d90040
PA
8277}
8278
77b06cd7
TJB
8279static int
8280insert_catch_exec (struct bp_location *bl)
c906108c 8281{
e99b03dc 8282 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8283}
c906108c 8284
fe798b75 8285static int
73971819 8286remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8287{
e99b03dc 8288 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8289}
c906108c 8290
fe798b75 8291static int
f1310107 8292breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8293 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8294 const struct target_waitstatus *ws)
fe798b75 8295{
b4d90040
PA
8296 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8297
f90263c1
TT
8298 if (ws->kind != TARGET_WAITKIND_EXECD)
8299 return 0;
8300
8301 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8302 return 1;
fe798b75 8303}
c906108c 8304
fe798b75 8305static enum print_stop_action
348d480f 8306print_it_catch_exec (bpstat bs)
fe798b75 8307{
36dfb11c 8308 struct ui_out *uiout = current_uiout;
348d480f 8309 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8310 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8311
fe798b75 8312 annotate_catchpoint (b->number);
f303dbd6 8313 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8314 if (b->disposition == disp_del)
112e8700 8315 uiout->text ("Temporary catchpoint ");
36dfb11c 8316 else
112e8700
SM
8317 uiout->text ("Catchpoint ");
8318 if (uiout->is_mi_like_p ())
36dfb11c 8319 {
112e8700
SM
8320 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8321 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8322 }
381befee 8323 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8324 uiout->text (" (exec'd ");
8325 uiout->field_string ("new-exec", c->exec_pathname);
8326 uiout->text ("), ");
36dfb11c 8327
fe798b75 8328 return PRINT_SRC_AND_LOC;
c906108c
SS
8329}
8330
fe798b75 8331static void
a6d9a66e 8332print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8333{
b4d90040 8334 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8335 struct value_print_options opts;
79a45e25 8336 struct ui_out *uiout = current_uiout;
fe798b75
JB
8337
8338 get_user_print_options (&opts);
8339
8340 /* Field 4, the address, is omitted (which makes the columns
8341 not line up too nicely with the headers, but the effect
8342 is relatively readable). */
8343 if (opts.addressprint)
112e8700 8344 uiout->field_skip ("addr");
fe798b75 8345 annotate_field (5);
112e8700 8346 uiout->text ("exec");
b4d90040 8347 if (c->exec_pathname != NULL)
fe798b75 8348 {
112e8700
SM
8349 uiout->text (", program \"");
8350 uiout->field_string ("what", c->exec_pathname);
8351 uiout->text ("\" ");
fe798b75 8352 }
8ac3646f 8353
112e8700
SM
8354 if (uiout->is_mi_like_p ())
8355 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8356}
8357
8358static void
8359print_mention_catch_exec (struct breakpoint *b)
8360{
8361 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8362}
8363
6149aea9
PA
8364/* Implement the "print_recreate" breakpoint_ops method for exec
8365 catchpoints. */
8366
8367static void
8368print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8369{
8370 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8371 print_recreate_thread (b, fp);
6149aea9
PA
8372}
8373
2060206e 8374static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8375
c906108c 8376static int
fba45db2 8377hw_breakpoint_used_count (void)
c906108c 8378{
c906108c
SS
8379 int i = 0;
8380
43892fdf 8381 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8382 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8383 for (bp_location *bl : b->locations ())
f1310107
TJB
8384 {
8385 /* Special types of hardware breakpoints may use more than
8386 one register. */
348d480f 8387 i += b->ops->resources_needed (bl);
f1310107 8388 }
c906108c
SS
8389
8390 return i;
8391}
8392
a1398e0c
PA
8393/* Returns the resources B would use if it were a hardware
8394 watchpoint. */
8395
c906108c 8396static int
a1398e0c 8397hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8398{
c906108c
SS
8399 int i = 0;
8400
a1398e0c
PA
8401 if (!breakpoint_enabled (b))
8402 return 0;
8403
40cb8ca5 8404 for (bp_location *bl : b->locations ())
a1398e0c
PA
8405 {
8406 /* Special types of hardware watchpoints may use more than
8407 one register. */
8408 i += b->ops->resources_needed (bl);
8409 }
8410
8411 return i;
8412}
8413
8414/* Returns the sum the used resources of all hardware watchpoints of
8415 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8416 the sum of the used resources of all hardware watchpoints of other
8417 types _not_ TYPE. */
8418
8419static int
8420hw_watchpoint_used_count_others (struct breakpoint *except,
8421 enum bptype type, int *other_type_used)
8422{
8423 int i = 0;
a1398e0c 8424
c906108c 8425 *other_type_used = 0;
43892fdf 8426 for (breakpoint *b : all_breakpoints ())
e09342b5 8427 {
a1398e0c
PA
8428 if (b == except)
8429 continue;
e09342b5
TJB
8430 if (!breakpoint_enabled (b))
8431 continue;
8432
a1398e0c
PA
8433 if (b->type == type)
8434 i += hw_watchpoint_use_count (b);
8435 else if (is_hardware_watchpoint (b))
8436 *other_type_used = 1;
e09342b5
TJB
8437 }
8438
c906108c
SS
8439 return i;
8440}
8441
c906108c 8442void
fba45db2 8443disable_watchpoints_before_interactive_call_start (void)
c906108c 8444{
43892fdf 8445 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8446 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8447 {
b5de0fa7 8448 b->enable_state = bp_call_disabled;
44702360 8449 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8450 }
c906108c
SS
8451}
8452
8453void
fba45db2 8454enable_watchpoints_after_interactive_call_stop (void)
c906108c 8455{
43892fdf 8456 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8457 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8458 {
b5de0fa7 8459 b->enable_state = bp_enabled;
44702360 8460 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8461 }
c906108c
SS
8462}
8463
8bea4e01
UW
8464void
8465disable_breakpoints_before_startup (void)
8466{
6c95b8df 8467 current_program_space->executing_startup = 1;
44702360 8468 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8469}
8470
8471void
8472enable_breakpoints_after_startup (void)
8473{
6c95b8df 8474 current_program_space->executing_startup = 0;
f8eba3c6 8475 breakpoint_re_set ();
8bea4e01
UW
8476}
8477
7c16b83e
PA
8478/* Create a new single-step breakpoint for thread THREAD, with no
8479 locations. */
c906108c 8480
7c16b83e
PA
8481static struct breakpoint *
8482new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8483{
b270e6f9 8484 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8485
b270e6f9 8486 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8487 &momentary_breakpoint_ops);
8488
8489 b->disposition = disp_donttouch;
8490 b->frame_id = null_frame_id;
8491
8492 b->thread = thread;
8493 gdb_assert (b->thread != 0);
8494
b270e6f9 8495 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8496}
8497
8498/* Set a momentary breakpoint of type TYPE at address specified by
8499 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8500 frame. */
c906108c 8501
454dafbd 8502breakpoint_up
a6d9a66e
UW
8503set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8504 struct frame_id frame_id, enum bptype type)
c906108c 8505{
52f0bd74 8506 struct breakpoint *b;
edb3359d 8507
193facb3
JK
8508 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8509 tail-called one. */
8510 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8511
06edf0c0 8512 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8513 b->enable_state = bp_enabled;
8514 b->disposition = disp_donttouch;
818dd999 8515 b->frame_id = frame_id;
c906108c 8516
00431a78 8517 b->thread = inferior_thread ()->global_num;
c906108c 8518
44702360 8519 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8520
454dafbd 8521 return breakpoint_up (b);
c906108c 8522}
611c83ae 8523
06edf0c0 8524/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8525 The new breakpoint will have type TYPE, use OPS as its
8526 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8527
06edf0c0
PA
8528static struct breakpoint *
8529momentary_breakpoint_from_master (struct breakpoint *orig,
8530 enum bptype type,
a1aa2221
LM
8531 const struct breakpoint_ops *ops,
8532 int loc_enabled)
e58b0e63
PA
8533{
8534 struct breakpoint *copy;
8535
06edf0c0 8536 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8537 copy->loc = allocate_bp_location (copy);
0ba852ab 8538 set_breakpoint_location_function (copy->loc);
e58b0e63 8539
a6d9a66e 8540 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8541 copy->loc->requested_address = orig->loc->requested_address;
8542 copy->loc->address = orig->loc->address;
8543 copy->loc->section = orig->loc->section;
6c95b8df 8544 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8545 copy->loc->probe = orig->loc->probe;
f8eba3c6 8546 copy->loc->line_number = orig->loc->line_number;
2f202fde 8547 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8548 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8549 copy->frame_id = orig->frame_id;
8550 copy->thread = orig->thread;
6c95b8df 8551 copy->pspace = orig->pspace;
e58b0e63
PA
8552
8553 copy->enable_state = bp_enabled;
8554 copy->disposition = disp_donttouch;
8555 copy->number = internal_breakpoint_number--;
8556
44702360 8557 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8558 return copy;
8559}
8560
06edf0c0
PA
8561/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8562 ORIG is NULL. */
8563
8564struct breakpoint *
8565clone_momentary_breakpoint (struct breakpoint *orig)
8566{
8567 /* If there's nothing to clone, then return nothing. */
8568 if (orig == NULL)
8569 return NULL;
8570
a1aa2221 8571 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8572}
8573
454dafbd 8574breakpoint_up
a6d9a66e
UW
8575set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8576 enum bptype type)
611c83ae
PA
8577{
8578 struct symtab_and_line sal;
8579
8580 sal = find_pc_line (pc, 0);
8581 sal.pc = pc;
8582 sal.section = find_pc_overlay (pc);
8583 sal.explicit_pc = 1;
8584
a6d9a66e 8585 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8586}
c906108c 8587\f
c5aa993b 8588
c906108c
SS
8589/* Tell the user we have just set a breakpoint B. */
8590
8591static void
fba45db2 8592mention (struct breakpoint *b)
c906108c 8593{
348d480f 8594 b->ops->print_mention (b);
2d33446d 8595 current_uiout->text ("\n");
c906108c 8596}
c906108c 8597\f
c5aa993b 8598
5133a315 8599static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8600
7f32a4d5
PA
8601/* Handle "set breakpoint auto-hw on".
8602
8603 If the explicitly specified breakpoint type is not hardware
8604 breakpoint, check the memory map to see whether the breakpoint
8605 address is in read-only memory.
8606
8607 - location type is not hardware breakpoint, memory is read-only.
8608 We change the type of the location to hardware breakpoint.
8609
8610 - location type is hardware breakpoint, memory is read-write. This
8611 means we've previously made the location hardware one, but then the
8612 memory map changed, so we undo.
8613*/
8614
8615static void
8616handle_automatic_hardware_breakpoints (bp_location *bl)
8617{
8618 if (automatic_hardware_breakpoints
8619 && bl->owner->type != bp_hardware_breakpoint
8620 && (bl->loc_type == bp_loc_software_breakpoint
8621 || bl->loc_type == bp_loc_hardware_breakpoint))
8622 {
8623 /* When breakpoints are removed, remove_breakpoints will use
8624 location types we've just set here, the only possible problem
8625 is that memory map has changed during running program, but
8626 it's not going to work anyway with current gdb. */
8627 mem_region *mr = lookup_mem_region (bl->address);
8628
8629 if (mr != nullptr)
8630 {
8631 enum bp_loc_type new_type;
8632
8633 if (mr->attrib.mode != MEM_RW)
8634 new_type = bp_loc_hardware_breakpoint;
8635 else
8636 new_type = bp_loc_software_breakpoint;
8637
8638 if (new_type != bl->loc_type)
8639 {
8640 static bool said = false;
8641
8642 bl->loc_type = new_type;
8643 if (!said)
8644 {
8645 fprintf_filtered (gdb_stdout,
8646 _("Note: automatically using "
8647 "hardware breakpoints for "
8648 "read-only addresses.\n"));
8649 said = true;
8650 }
8651 }
8652 }
8653 }
8654}
8655
0d381245 8656static struct bp_location *
39d61571 8657add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8658 const struct symtab_and_line *sal)
8659{
8660 struct bp_location *loc, **tmp;
3742cc8b
YQ
8661 CORE_ADDR adjusted_address;
8662 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8663
8664 if (loc_gdbarch == NULL)
8665 loc_gdbarch = b->gdbarch;
8666
8667 /* Adjust the breakpoint's address prior to allocating a location.
8668 Once we call allocate_bp_location(), that mostly uninitialized
8669 location will be placed on the location chain. Adjustment of the
8670 breakpoint may cause target_read_memory() to be called and we do
8671 not want its scan of the location chain to find a breakpoint and
8672 location that's only been partially initialized. */
8673 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8674 sal->pc, b->type);
0d381245 8675
d30113d4 8676 /* Sort the locations by their ADDRESS. */
39d61571 8677 loc = allocate_bp_location (b);
d30113d4
JK
8678 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8679 tmp = &((*tmp)->next))
0d381245 8680 ;
d30113d4 8681 loc->next = *tmp;
0d381245 8682 *tmp = loc;
3742cc8b 8683
0d381245 8684 loc->requested_address = sal->pc;
3742cc8b 8685 loc->address = adjusted_address;
6c95b8df 8686 loc->pspace = sal->pspace;
935676c9 8687 loc->probe.prob = sal->prob;
729662a5 8688 loc->probe.objfile = sal->objfile;
6c95b8df 8689 gdb_assert (loc->pspace != NULL);
0d381245 8690 loc->section = sal->section;
3742cc8b 8691 loc->gdbarch = loc_gdbarch;
f8eba3c6 8692 loc->line_number = sal->line;
2f202fde 8693 loc->symtab = sal->symtab;
4a27f119 8694 loc->symbol = sal->symbol;
3467ec66
PA
8695 loc->msymbol = sal->msymbol;
8696 loc->objfile = sal->objfile;
f8eba3c6 8697
0ba852ab 8698 set_breakpoint_location_function (loc);
1a853c52 8699
6ae88661
LM
8700 /* While by definition, permanent breakpoints are already present in the
8701 code, we don't mark the location as inserted. Normally one would expect
8702 that GDB could rely on that breakpoint instruction to stop the program,
8703 thus removing the need to insert its own breakpoint, except that executing
8704 the breakpoint instruction can kill the target instead of reporting a
8705 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8706 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8707 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8708 breakpoint be inserted normally results in QEMU knowing about the GDB
8709 breakpoint, and thus trap before the breakpoint instruction is executed.
8710 (If GDB later needs to continue execution past the permanent breakpoint,
8711 it manually increments the PC, thus avoiding executing the breakpoint
8712 instruction.) */
1a853c52 8713 if (bp_loc_is_permanent (loc))
6ae88661 8714 loc->permanent = 1;
1a853c52 8715
0d381245
VP
8716 return loc;
8717}
514f746b
AR
8718\f
8719
5133a315
LM
8720/* Return true if LOC is pointing to a permanent breakpoint,
8721 return false otherwise. */
1cf4d951 8722
5133a315 8723static bool
1cf4d951
PA
8724bp_loc_is_permanent (struct bp_location *loc)
8725{
514f746b
AR
8726 gdb_assert (loc != NULL);
8727
cb1e4e32
PA
8728 /* If we have a non-breakpoint-backed catchpoint or a software
8729 watchpoint, just return 0. We should not attempt to read from
8730 the addresses the locations of these breakpoint types point to.
5133a315 8731 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8732 memory. */
cb1e4e32 8733 if (!bl_address_is_meaningful (loc))
5133a315 8734 return false;
244558af 8735
5ed8105e 8736 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8737 switch_to_program_space_and_thread (loc->pspace);
5133a315 8738 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8739}
8740
e7e0cddf
SS
8741/* Build a command list for the dprintf corresponding to the current
8742 settings of the dprintf style options. */
8743
8744static void
8745update_dprintf_command_list (struct breakpoint *b)
8746{
8747 char *dprintf_args = b->extra_string;
8748 char *printf_line = NULL;
8749
8750 if (!dprintf_args)
8751 return;
8752
8753 dprintf_args = skip_spaces (dprintf_args);
8754
8755 /* Allow a comma, as it may have terminated a location, but don't
8756 insist on it. */
8757 if (*dprintf_args == ',')
8758 ++dprintf_args;
8759 dprintf_args = skip_spaces (dprintf_args);
8760
8761 if (*dprintf_args != '"')
8762 error (_("Bad format string, missing '\"'."));
8763
d3ce09f5 8764 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8765 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8766 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8767 {
8768 if (!dprintf_function)
8769 error (_("No function supplied for dprintf call"));
8770
8771 if (dprintf_channel && strlen (dprintf_channel) > 0)
8772 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8773 dprintf_function,
8774 dprintf_channel,
8775 dprintf_args);
8776 else
8777 printf_line = xstrprintf ("call (void) %s (%s)",
8778 dprintf_function,
8779 dprintf_args);
8780 }
d3ce09f5
SS
8781 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8782 {
8783 if (target_can_run_breakpoint_commands ())
8784 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8785 else
8786 {
8787 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8788 printf_line = xstrprintf ("printf %s", dprintf_args);
8789 }
8790 }
e7e0cddf
SS
8791 else
8792 internal_error (__FILE__, __LINE__,
8793 _("Invalid dprintf style."));
8794
f28045c2 8795 gdb_assert (printf_line != NULL);
e7e0cddf 8796
12973681
TT
8797 /* Manufacture a printf sequence. */
8798 struct command_line *printf_cmd_line
8799 = new struct command_line (simple_control, printf_line);
8800 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8801 command_lines_deleter ()));
e7e0cddf
SS
8802}
8803
8804/* Update all dprintf commands, making their command lists reflect
8805 current style settings. */
8806
8807static void
eb4c3f4a 8808update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8809 struct cmd_list_element *c)
8810{
43892fdf
SM
8811 for (breakpoint *b : all_breakpoints ())
8812 if (b->type == bp_dprintf)
e7e0cddf 8813 update_dprintf_command_list (b);
e7e0cddf 8814}
c3f6f71d 8815
f00aae0f
KS
8816/* Create a breakpoint with SAL as location. Use LOCATION
8817 as a description of the location, and COND_STRING
b35a8b2f
DE
8818 as condition expression. If LOCATION is NULL then create an
8819 "address location" from the address in the SAL. */
018d34a4
VP
8820
8821static void
d9b3f62e 8822init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8823 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8824 event_location_up &&location,
e1e01040
PA
8825 gdb::unique_xmalloc_ptr<char> filter,
8826 gdb::unique_xmalloc_ptr<char> cond_string,
8827 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8828 enum bptype type, enum bpdisp disposition,
8829 int thread, int task, int ignore_count,
c0a91b2b 8830 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8831 int enabled, int internal, unsigned flags,
8832 int display_canonical)
018d34a4 8833{
0d381245 8834 int i;
018d34a4
VP
8835
8836 if (type == bp_hardware_breakpoint)
8837 {
fbbd034e
AS
8838 int target_resources_ok;
8839
8840 i = hw_breakpoint_used_count ();
8841 target_resources_ok =
8842 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8843 i + 1, 0);
8844 if (target_resources_ok == 0)
8845 error (_("No hardware breakpoint support in the target."));
8846 else if (target_resources_ok < 0)
8847 error (_("Hardware breakpoints used exceeds limit."));
8848 }
8849
6c5b2ebe 8850 gdb_assert (!sals.empty ());
6c95b8df 8851
6c5b2ebe 8852 for (const auto &sal : sals)
0d381245 8853 {
0d381245
VP
8854 struct bp_location *loc;
8855
8856 if (from_tty)
5af949e3
UW
8857 {
8858 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8859 if (!loc_gdbarch)
8860 loc_gdbarch = gdbarch;
8861
8862 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8863 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8864 }
0d381245 8865
6c5b2ebe 8866 if (&sal == &sals[0])
0d381245 8867 {
d9b3f62e 8868 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8869 b->thread = thread;
4a306c9a 8870 b->task = task;
855a6e68 8871
e1e01040
PA
8872 b->cond_string = cond_string.release ();
8873 b->extra_string = extra_string.release ();
0d381245 8874 b->ignore_count = ignore_count;
41447f92 8875 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8876 b->disposition = disposition;
6c95b8df 8877
44f238bb
PA
8878 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8879 b->loc->inserted = 1;
8880
0fb4aa4b
PA
8881 if (type == bp_static_tracepoint)
8882 {
d9b3f62e 8883 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8884 struct static_tracepoint_marker marker;
8885
983af33b 8886 if (strace_marker_p (b))
0fb4aa4b
PA
8887 {
8888 /* We already know the marker exists, otherwise, we
8889 wouldn't see a sal for it. */
d28cd78a
TT
8890 const char *p
8891 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8892 const char *endp;
0fb4aa4b 8893
f1735a53 8894 p = skip_spaces (p);
0fb4aa4b 8895
f1735a53 8896 endp = skip_to_space (p);
0fb4aa4b 8897
5d9310c4 8898 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8899
3e43a32a
MS
8900 printf_filtered (_("Probed static tracepoint "
8901 "marker \"%s\"\n"),
5d9310c4 8902 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8903 }
8904 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8905 {
5d9310c4 8906 t->static_trace_marker_id = std::move (marker.str_id);
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
3e43a32a
MS
8913 warning (_("Couldn't determine the static "
8914 "tracepoint marker to probe"));
0fb4aa4b
PA
8915 }
8916
0d381245
VP
8917 loc = b->loc;
8918 }
8919 else
018d34a4 8920 {
39d61571 8921 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8922 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8923 loc->inserted = 1;
0d381245
VP
8924 }
8925
b5fa468f
TBA
8926 /* Do not set breakpoint locations conditions yet. As locations
8927 are inserted, they get sorted based on their addresses. Let
8928 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8929
8930 /* Dynamic printf requires and uses additional arguments on the
8931 command line, otherwise it's an error. */
8932 if (type == bp_dprintf)
8933 {
8934 if (b->extra_string)
8935 update_dprintf_command_list (b);
8936 else
8937 error (_("Format string required"));
8938 }
8939 else if (b->extra_string)
588ae58c 8940 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8941 }
018d34a4 8942
b5fa468f
TBA
8943
8944 /* The order of the locations is now stable. Set the location
8945 condition using the location's number. */
8946 int loc_num = 1;
40cb8ca5 8947 for (bp_location *loc : b->locations ())
b5fa468f
TBA
8948 {
8949 if (b->cond_string != nullptr)
8950 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8951 loc_num);
8952
8953 ++loc_num;
8954 }
8955
56435ebe 8956 b->display_canonical = display_canonical;
f00aae0f 8957 if (location != NULL)
d28cd78a 8958 b->location = std::move (location);
018d34a4 8959 else
d28cd78a 8960 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8961 b->filter = std::move (filter);
d9b3f62e 8962}
018d34a4 8963
d9b3f62e
PA
8964static void
8965create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8966 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8967 event_location_up &&location,
e1e01040
PA
8968 gdb::unique_xmalloc_ptr<char> filter,
8969 gdb::unique_xmalloc_ptr<char> cond_string,
8970 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8971 enum bptype type, enum bpdisp disposition,
8972 int thread, int task, int ignore_count,
c0a91b2b 8973 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8974 int enabled, int internal, unsigned flags,
8975 int display_canonical)
d9b3f62e 8976{
a5e364af 8977 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8978
a5e364af 8979 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8980 sals, std::move (location),
e1e01040
PA
8981 std::move (filter),
8982 std::move (cond_string),
8983 std::move (extra_string),
d9b3f62e
PA
8984 type, disposition,
8985 thread, task, ignore_count,
8986 ops, from_tty,
44f238bb
PA
8987 enabled, internal, flags,
8988 display_canonical);
d9b3f62e 8989
b270e6f9 8990 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8991}
8992
8993/* Add SALS.nelts breakpoints to the breakpoint table. For each
8994 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8995 value. COND_STRING, if not NULL, specified the condition to be
8996 used for all breakpoints. Essentially the only case where
8997 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8998 function. In that case, it's still not possible to specify
8999 separate conditions for different overloaded functions, so
9000 we take just a single condition string.
9001
c3f6f71d 9002 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9003 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9004 array contents). If the function fails (error() is called), the
9005 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9006 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9007
9008static void
8cdf0e15 9009create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9010 struct linespec_result *canonical,
e1e01040
PA
9011 gdb::unique_xmalloc_ptr<char> cond_string,
9012 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9013 enum bptype type, enum bpdisp disposition,
9014 int thread, int task, int ignore_count,
c0a91b2b 9015 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9016 int enabled, int internal, unsigned flags)
c906108c 9017{
f8eba3c6 9018 if (canonical->pre_expanded)
6c5b2ebe 9019 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9020
6c5b2ebe 9021 for (const auto &lsal : canonical->lsals)
c3f6f71d 9022 {
f00aae0f 9023 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9024 'break', without arguments. */
ffc2605c 9025 event_location_up location
f00aae0f 9026 = (canonical->location != NULL
8e9e35b1 9027 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9028 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9029 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9030
6c5b2ebe 9031 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9032 std::move (location),
e1e01040
PA
9033 std::move (filter_string),
9034 std::move (cond_string),
9035 std::move (extra_string),
e7e0cddf 9036 type, disposition,
84f4c1fe 9037 thread, task, ignore_count, ops,
44f238bb 9038 from_tty, enabled, internal, flags,
56435ebe 9039 canonical->special_display);
c3f6f71d 9040 }
c3f6f71d 9041}
c906108c 9042
f00aae0f 9043/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9044 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9045 addresses found. LOCATION points to the end of the SAL (for
9046 linespec locations).
9998af43
TJB
9047
9048 The array and the line spec strings are allocated on the heap, it is
9049 the caller's responsibility to free them. */
c906108c 9050
b9362cc7 9051static void
626d2320 9052parse_breakpoint_sals (struct event_location *location,
58438ac1 9053 struct linespec_result *canonical)
c3f6f71d 9054{
f00aae0f
KS
9055 struct symtab_and_line cursal;
9056
9057 if (event_location_type (location) == LINESPEC_LOCATION)
9058 {
a20714ff 9059 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9060
a20714ff 9061 if (spec == NULL)
f00aae0f
KS
9062 {
9063 /* The last displayed codepoint, if it's valid, is our default
9064 breakpoint address. */
9065 if (last_displayed_sal_is_valid ())
9066 {
f00aae0f
KS
9067 /* Set sal's pspace, pc, symtab, and line to the values
9068 corresponding to the last call to print_frame_info.
9069 Be sure to reinitialize LINE with NOTCURRENT == 0
9070 as the breakpoint line number is inappropriate otherwise.
9071 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9072 symtab_and_line sal = get_last_displayed_sal ();
9073 CORE_ADDR pc = sal.pc;
9074
f00aae0f
KS
9075 sal = find_pc_line (pc, 0);
9076
9077 /* "break" without arguments is equivalent to "break *PC"
9078 where PC is the last displayed codepoint's address. So
9079 make sure to set sal.explicit_pc to prevent GDB from
9080 trying to expand the list of sals to include all other
9081 instances with the same symtab and line. */
9082 sal.pc = pc;
9083 sal.explicit_pc = 1;
9084
6c5b2ebe
PA
9085 struct linespec_sals lsal;
9086 lsal.sals = {sal};
f00aae0f
KS
9087 lsal.canonical = NULL;
9088
6c5b2ebe 9089 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9090 return;
9091 }
9092 else
9093 error (_("No default breakpoint address now."));
c906108c 9094 }
c906108c 9095 }
f00aae0f
KS
9096
9097 /* Force almost all breakpoints to be in terms of the
9098 current_source_symtab (which is decode_line_1's default).
9099 This should produce the results we want almost all of the
9100 time while leaving default_breakpoint_* alone.
9101
9102 ObjC: However, don't match an Objective-C method name which
9103 may have a '+' or '-' succeeded by a '['. */
9104 cursal = get_current_source_symtab_and_line ();
9105 if (last_displayed_sal_is_valid ())
c906108c 9106 {
a20714ff 9107 const char *spec = NULL;
cc80f267 9108
f00aae0f 9109 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9110 spec = get_linespec_location (location)->spec_string;
cc80f267 9111
f00aae0f 9112 if (!cursal.symtab
a20714ff
PA
9113 || (spec != NULL
9114 && strchr ("+-", spec[0]) != NULL
9115 && spec[1] != '['))
f00aae0f 9116 {
c2f4122d 9117 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9118 get_last_displayed_symtab (),
9119 get_last_displayed_line (),
9120 canonical, NULL, NULL);
9121 return;
9122 }
c906108c 9123 }
f00aae0f 9124
c2f4122d 9125 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9126 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9127}
c906108c 9128
c906108c 9129
c3f6f71d 9130/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9131 inserted as a breakpoint. If it can't throw an error. */
c906108c 9132
b9362cc7 9133static void
6c5b2ebe 9134breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9135{
6c5b2ebe
PA
9136 for (auto &sal : sals)
9137 resolve_sal_pc (&sal);
c3f6f71d
JM
9138}
9139
7a697b8d
SS
9140/* Fast tracepoints may have restrictions on valid locations. For
9141 instance, a fast tracepoint using a jump instead of a trap will
9142 likely have to overwrite more bytes than a trap would, and so can
9143 only be placed where the instruction is longer than the jump, or a
9144 multi-instruction sequence does not have a jump into the middle of
9145 it, etc. */
9146
9147static void
9148check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9149 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9150{
6c5b2ebe 9151 for (const auto &sal : sals)
7a697b8d 9152 {
f8eba3c6
TT
9153 struct gdbarch *sarch;
9154
6c5b2ebe 9155 sarch = get_sal_arch (sal);
f8eba3c6
TT
9156 /* We fall back to GDBARCH if there is no architecture
9157 associated with SAL. */
9158 if (sarch == NULL)
9159 sarch = gdbarch;
281d762b
TT
9160 std::string msg;
9161 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9162 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9163 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9164 }
9165}
9166
018d34a4
VP
9167/* Given TOK, a string specification of condition and thread, as
9168 accepted by the 'break' command, extract the condition
9169 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9170 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9171 If no condition is found, *COND_STRING is set to NULL.
9172 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9173
9174static void
bbc13ae3 9175find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9176 char **cond_string, int *thread, int *task,
9177 char **rest)
018d34a4
VP
9178{
9179 *cond_string = NULL;
9180 *thread = -1;
ed1d1739
KS
9181 *task = 0;
9182 *rest = NULL;
733d554a 9183 bool force = false;
ed1d1739 9184
018d34a4
VP
9185 while (tok && *tok)
9186 {
bbc13ae3 9187 const char *end_tok;
018d34a4 9188 int toklen;
bbc13ae3
KS
9189 const char *cond_start = NULL;
9190 const char *cond_end = NULL;
cc59ec59 9191
f1735a53 9192 tok = skip_spaces (tok);
e7e0cddf
SS
9193
9194 if ((*tok == '"' || *tok == ',') && rest)
9195 {
9196 *rest = savestring (tok, strlen (tok));
9197 return;
9198 }
9199
f1735a53 9200 end_tok = skip_to_space (tok);
d634f2de 9201
018d34a4 9202 toklen = end_tok - tok;
d634f2de 9203
018d34a4
VP
9204 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9205 {
9206 tok = cond_start = end_tok + 1;
733d554a
TBA
9207 try
9208 {
9209 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9210 }
9211 catch (const gdb_exception_error &)
9212 {
9213 if (!force)
9214 throw;
9215 else
9216 tok = tok + strlen (tok);
9217 }
018d34a4 9218 cond_end = tok;
d634f2de 9219 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9220 }
733d554a
TBA
9221 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9222 {
5759831a 9223 tok = tok + toklen;
733d554a
TBA
9224 force = true;
9225 }
018d34a4
VP
9226 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9227 {
5d5658a1
PA
9228 const char *tmptok;
9229 struct thread_info *thr;
d634f2de 9230
018d34a4 9231 tok = end_tok + 1;
5d5658a1 9232 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9233 if (tok == tmptok)
9234 error (_("Junk after thread keyword."));
5d5658a1 9235 *thread = thr->global_num;
bbc13ae3 9236 tok = tmptok;
018d34a4 9237 }
4a306c9a
JB
9238 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9239 {
9240 char *tmptok;
9241
9242 tok = end_tok + 1;
bbc13ae3 9243 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9244 if (tok == tmptok)
9245 error (_("Junk after task keyword."));
9246 if (!valid_task_id (*task))
b6199126 9247 error (_("Unknown task %d."), *task);
bbc13ae3 9248 tok = tmptok;
4a306c9a 9249 }
e7e0cddf
SS
9250 else if (rest)
9251 {
9252 *rest = savestring (tok, strlen (tok));
ccab2054 9253 return;
e7e0cddf 9254 }
018d34a4
VP
9255 else
9256 error (_("Junk at end of arguments."));
9257 }
9258}
9259
b5fa468f
TBA
9260/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9261 succeeds. The parsed values are written to COND_STRING, THREAD,
9262 TASK, and REST. See the comment of 'find_condition_and_thread'
9263 for the description of these parameters and INPUT. */
9264
9265static void
9266find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9267 const char *input, char **cond_string,
9268 int *thread, int *task, char **rest)
9269{
9270 int num_failures = 0;
9271 for (auto &sal : sals)
9272 {
9273 char *cond = nullptr;
9274 int thread_id = 0;
9275 int task_id = 0;
9276 char *remaining = nullptr;
9277
9278 /* Here we want to parse 'arg' to separate condition from thread
9279 number. But because parsing happens in a context and the
9280 contexts of sals might be different, try each until there is
9281 success. Finding one successful parse is sufficient for our
9282 goal. When setting the breakpoint we'll re-parse the
9283 condition in the context of each sal. */
9284 try
9285 {
9286 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9287 &task_id, &remaining);
9288 *cond_string = cond;
9289 *thread = thread_id;
9290 *task = task_id;
9291 *rest = remaining;
9292 break;
9293 }
9294 catch (const gdb_exception_error &e)
9295 {
9296 num_failures++;
9297 /* If no sal remains, do not continue. */
9298 if (num_failures == sals.size ())
9299 throw;
9300 }
9301 }
9302}
9303
0fb4aa4b
PA
9304/* Decode a static tracepoint marker spec. */
9305
6c5b2ebe 9306static std::vector<symtab_and_line>
f00aae0f 9307decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9308{
f00aae0f
KS
9309 const char *p = &(*arg_p)[3];
9310 const char *endp;
0fb4aa4b 9311
f1735a53 9312 p = skip_spaces (p);
0fb4aa4b 9313
f1735a53 9314 endp = skip_to_space (p);
0fb4aa4b 9315
81b1e71c 9316 std::string marker_str (p, endp - p);
0fb4aa4b 9317
5d9310c4
SM
9318 std::vector<static_tracepoint_marker> markers
9319 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9320 if (markers.empty ())
81b1e71c
TT
9321 error (_("No known static tracepoint marker named %s"),
9322 marker_str.c_str ());
0fb4aa4b 9323
6c5b2ebe 9324 std::vector<symtab_and_line> sals;
5d9310c4 9325 sals.reserve (markers.size ());
0fb4aa4b 9326
5d9310c4 9327 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9328 {
5d9310c4
SM
9329 symtab_and_line sal = find_pc_line (marker.address, 0);
9330 sal.pc = marker.address;
6c5b2ebe 9331 sals.push_back (sal);
5d9310c4 9332 }
0fb4aa4b 9333
0fb4aa4b
PA
9334 *arg_p = endp;
9335 return sals;
9336}
9337
bac7c5cf
GB
9338/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9339 according to IS_TRACEPOINT. */
9340
9341static const struct breakpoint_ops *
9342breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9343 bool is_tracepoint)
9344{
9345 if (is_tracepoint)
9346 {
9347 if (location_type == PROBE_LOCATION)
9348 return &tracepoint_probe_breakpoint_ops;
9349 else
9350 return &tracepoint_breakpoint_ops;
9351 }
9352 else
9353 {
9354 if (location_type == PROBE_LOCATION)
9355 return &bkpt_probe_breakpoint_ops;
9356 else
9357 return &bkpt_breakpoint_ops;
9358 }
9359}
9360
9361/* See breakpoint.h. */
9362
9363const struct breakpoint_ops *
9364breakpoint_ops_for_event_location (const struct event_location *location,
9365 bool is_tracepoint)
9366{
9367 if (location != nullptr)
9368 return breakpoint_ops_for_event_location_type
9369 (event_location_type (location), is_tracepoint);
9370 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9371}
9372
f00aae0f 9373/* See breakpoint.h. */
0101ce28 9374
8cdf0e15
VP
9375int
9376create_breakpoint (struct gdbarch *gdbarch,
626d2320 9377 struct event_location *location,
e1e01040
PA
9378 const char *cond_string,
9379 int thread, const char *extra_string,
10a636cc 9380 bool force_condition, int parse_extra,
0fb4aa4b 9381 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9382 int ignore_count,
9383 enum auto_boolean pending_break_support,
c0a91b2b 9384 const struct breakpoint_ops *ops,
44f238bb
PA
9385 int from_tty, int enabled, int internal,
9386 unsigned flags)
c3f6f71d 9387{
7efd8fc2 9388 struct linespec_result canonical;
0101ce28 9389 int pending = 0;
4a306c9a 9390 int task = 0;
86b17b60 9391 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9392
348d480f
PA
9393 gdb_assert (ops != NULL);
9394
f00aae0f
KS
9395 /* If extra_string isn't useful, set it to NULL. */
9396 if (extra_string != NULL && *extra_string == '\0')
9397 extra_string = NULL;
9398
a70b8144 9399 try
b78a6381 9400 {
f00aae0f 9401 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9402 }
230d2906 9403 catch (const gdb_exception_error &e)
0101ce28 9404 {
492d29ea
PA
9405 /* If caller is interested in rc value from parse, set
9406 value. */
9407 if (e.error == NOT_FOUND_ERROR)
0101ce28 9408 {
05ff989b
AC
9409 /* If pending breakpoint support is turned off, throw
9410 error. */
fa8d40ab
JJ
9411
9412 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9413 throw;
723a2275
VP
9414
9415 exception_print (gdb_stderr, e);
fa8d40ab 9416
dda83cd7 9417 /* If pending breakpoint support is auto query and the user
05ff989b 9418 selects no, then simply return the error code. */
059fb39f 9419 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9420 && !nquery (_("Make %s pending on future shared library load? "),
9421 bptype_string (type_wanted)))
fd9b8c24 9422 return 0;
fa8d40ab 9423
05ff989b
AC
9424 /* At this point, either the user was queried about setting
9425 a pending breakpoint and selected yes, or pending
9426 breakpoint behavior is on and thus a pending breakpoint
9427 is defaulted on behalf of the user. */
f00aae0f 9428 pending = 1;
0101ce28 9429 }
492d29ea 9430 else
eedc3f4f 9431 throw;
0101ce28 9432 }
492d29ea 9433
6c5b2ebe 9434 if (!pending && canonical.lsals.empty ())
492d29ea 9435 return 0;
c3f6f71d 9436
c3f6f71d
JM
9437 /* Resolve all line numbers to PC's and verify that the addresses
9438 are ok for the target. */
0101ce28 9439 if (!pending)
f8eba3c6 9440 {
6c5b2ebe
PA
9441 for (auto &lsal : canonical.lsals)
9442 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9443 }
c3f6f71d 9444
7a697b8d 9445 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9446 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9447 {
6c5b2ebe
PA
9448 for (const auto &lsal : canonical.lsals)
9449 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9450 }
7a697b8d 9451
c3f6f71d
JM
9452 /* Verify that condition can be parsed, before setting any
9453 breakpoints. Allocate a separate condition expression for each
4a64f543 9454 breakpoint. */
0101ce28 9455 if (!pending)
c3f6f71d 9456 {
e1e01040
PA
9457 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9458 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9459
f00aae0f 9460 if (parse_extra)
dda83cd7 9461 {
0878d0fa 9462 char *rest;
e1e01040 9463 char *cond;
52d361e1 9464
6c5b2ebe 9465 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9466
b5fa468f
TBA
9467 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9468 &cond, &thread, &task, &rest);
e1e01040
PA
9469 cond_string_copy.reset (cond);
9470 extra_string_copy.reset (rest);
dda83cd7 9471 }
2f069f6f 9472 else
dda83cd7 9473 {
f00aae0f
KS
9474 if (type_wanted != bp_dprintf
9475 && extra_string != NULL && *extra_string != '\0')
9476 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9477
10a636cc
TBA
9478 /* Check the validity of the condition. We should error out
9479 if the condition is invalid at all of the locations and
9480 if it is not forced. In the PARSE_EXTRA case above, this
9481 check is done when parsing the EXTRA_STRING. */
9482 if (cond_string != nullptr && !force_condition)
9483 {
9484 int num_failures = 0;
9485 const linespec_sals &lsal = canonical.lsals[0];
9486 for (const auto &sal : lsal.sals)
9487 {
9488 const char *cond = cond_string;
9489 try
9490 {
9491 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9492 /* One success is sufficient to keep going. */
9493 break;
9494 }
9495 catch (const gdb_exception_error &)
9496 {
9497 num_failures++;
9498 /* If this is the last sal, error out. */
9499 if (num_failures == lsal.sals.size ())
9500 throw;
9501 }
9502 }
9503 }
9504
0878d0fa
YQ
9505 /* Create a private copy of condition string. */
9506 if (cond_string)
e1e01040 9507 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9508 /* Create a private copy of any extra string. */
9509 if (extra_string)
e1e01040 9510 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9511 }
0fb4aa4b 9512
52d361e1 9513 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9514 std::move (cond_string_copy),
9515 std::move (extra_string_copy),
9516 type_wanted,
d9b3f62e
PA
9517 tempflag ? disp_del : disp_donttouch,
9518 thread, task, ignore_count, ops,
44f238bb 9519 from_tty, enabled, internal, flags);
c906108c 9520 }
0101ce28
JJ
9521 else
9522 {
a5e364af 9523 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9524
a5e364af 9525 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9526 b->location = copy_event_location (location);
bfccc43c 9527
f00aae0f
KS
9528 if (parse_extra)
9529 b->cond_string = NULL;
e12c7713
MK
9530 else
9531 {
9532 /* Create a private copy of condition string. */
e1e01040 9533 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9534 b->thread = thread;
e12c7713 9535 }
f00aae0f
KS
9536
9537 /* Create a private copy of any extra string. */
e1e01040 9538 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9539 b->ignore_count = ignore_count;
0101ce28 9540 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9541 b->condition_not_parsed = 1;
41447f92 9542 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9543 if ((type_wanted != bp_breakpoint
dda83cd7 9544 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9545 b->pspace = current_program_space;
8bea4e01 9546
b270e6f9 9547 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9548 }
9549
6c5b2ebe 9550 if (canonical.lsals.size () > 1)
95a42b64 9551 {
3e43a32a
MS
9552 warning (_("Multiple breakpoints were set.\nUse the "
9553 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9554 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9555 }
9556
44702360 9557 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9558
9559 return 1;
c3f6f71d 9560}
c906108c 9561
348d480f 9562/* Set a breakpoint.
72b2ff0e
VP
9563 ARG is a string describing breakpoint address,
9564 condition, and thread.
9565 FLAG specifies if a breakpoint is hardware on,
9566 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9567 and BP_TEMPFLAG. */
348d480f 9568
98deb0da 9569static void
f2fc3015 9570break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9571{
72b2ff0e 9572 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9573 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9574 ? bp_hardware_breakpoint
9575 : bp_breakpoint);
f00aae0f 9576
ffc2605c 9577 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9578 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9579 (location.get (), false /* is_tracepoint */);
c3f6f71d 9580
8cdf0e15 9581 create_breakpoint (get_current_arch (),
ffc2605c 9582 location.get (),
10a636cc 9583 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9584 tempflag, type_wanted,
8cdf0e15
VP
9585 0 /* Ignore count */,
9586 pending_break_support,
55aa24fb 9587 ops,
8cdf0e15 9588 from_tty,
84f4c1fe 9589 1 /* enabled */,
44f238bb
PA
9590 0 /* internal */,
9591 0);
c906108c
SS
9592}
9593
c906108c
SS
9594/* Helper function for break_command_1 and disassemble_command. */
9595
9596void
fba45db2 9597resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9598{
9599 CORE_ADDR pc;
9600
9601 if (sal->pc == 0 && sal->symtab != NULL)
9602 {
9603 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9604 error (_("No line %d in file \"%s\"."),
05cba821 9605 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9606 sal->pc = pc;
6a048695 9607
4a64f543 9608 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9609 number, then skip the function prologue if necessary. */
6a048695 9610 if (sal->explicit_line)
059acae7 9611 skip_prologue_sal (sal);
c906108c
SS
9612 }
9613
9614 if (sal->section == 0 && sal->symtab != NULL)
9615 {
346d1dfe 9616 const struct blockvector *bv;
3977b71f 9617 const struct block *b;
c5aa993b 9618 struct symbol *sym;
c906108c 9619
43f3e411
DE
9620 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9621 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9622 if (bv != NULL)
9623 {
7f0df278 9624 sym = block_linkage_function (b);
c906108c
SS
9625 if (sym != NULL)
9626 {
eb822aa6 9627 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9628 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9629 }
9630 else
9631 {
4a64f543 9632 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9633 just have to look harder. This case can be executed
9634 if we have line numbers but no functions (as can
9635 happen in assembly source). */
c906108c 9636
5ed8105e 9637 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9638 switch_to_program_space_and_thread (sal->pspace);
c906108c 9639
5ed8105e 9640 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9641 if (msym.minsym)
1db66e34 9642 sal->section = msym.obj_section ();
c906108c
SS
9643 }
9644 }
9645 }
9646}
9647
9648void
0b39b52e 9649break_command (const char *arg, int from_tty)
c906108c 9650{
db107f19 9651 break_command_1 (arg, 0, from_tty);
c906108c
SS
9652}
9653
c906108c 9654void
0b39b52e 9655tbreak_command (const char *arg, int from_tty)
c906108c 9656{
db107f19 9657 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9658}
9659
c906108c 9660static void
0b39b52e 9661hbreak_command (const char *arg, int from_tty)
c906108c 9662{
db107f19 9663 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9664}
9665
9666static void
0b39b52e 9667thbreak_command (const char *arg, int from_tty)
c906108c 9668{
db107f19 9669 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9670}
9671
9672static void
ee7ddd71 9673stop_command (const char *arg, int from_tty)
c906108c 9674{
a3f17187 9675 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9676Usage: stop in <function | address>\n\
a3f17187 9677 stop at <line>\n"));
c906108c
SS
9678}
9679
9680static void
4495129a 9681stopin_command (const char *arg, int from_tty)
c906108c
SS
9682{
9683 int badInput = 0;
9684
cafb3438 9685 if (arg == NULL)
c906108c
SS
9686 badInput = 1;
9687 else if (*arg != '*')
9688 {
4495129a 9689 const char *argptr = arg;
c906108c
SS
9690 int hasColon = 0;
9691
4a64f543 9692 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9693 say it is bad, otherwise, it should be an address or
9694 function/method name. */
c906108c 9695 while (*argptr && !hasColon)
c5aa993b
JM
9696 {
9697 hasColon = (*argptr == ':');
9698 argptr++;
9699 }
c906108c
SS
9700
9701 if (hasColon)
c5aa993b 9702 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9703 else
c5aa993b 9704 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9705 }
9706
9707 if (badInput)
a3f17187 9708 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9709 else
db107f19 9710 break_command_1 (arg, 0, from_tty);
c906108c
SS
9711}
9712
9713static void
4495129a 9714stopat_command (const char *arg, int from_tty)
c906108c
SS
9715{
9716 int badInput = 0;
9717
cafb3438 9718 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9719 badInput = 1;
9720 else
9721 {
4495129a 9722 const char *argptr = arg;
c906108c
SS
9723 int hasColon = 0;
9724
4a64f543 9725 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9726 it is probably a line number. */
c906108c 9727 while (*argptr && !hasColon)
c5aa993b
JM
9728 {
9729 hasColon = (*argptr == ':');
9730 argptr++;
9731 }
c906108c
SS
9732
9733 if (hasColon)
c5aa993b 9734 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9735 else
c5aa993b 9736 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9737 }
9738
9739 if (badInput)
65e65158 9740 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9741 else
db107f19 9742 break_command_1 (arg, 0, from_tty);
c906108c
SS
9743}
9744
e7e0cddf
SS
9745/* The dynamic printf command is mostly like a regular breakpoint, but
9746 with a prewired command list consisting of a single output command,
9747 built from extra arguments supplied on the dprintf command
9748 line. */
9749
da821c7b 9750static void
0b39b52e 9751dprintf_command (const char *arg, int from_tty)
e7e0cddf 9752{
ffc2605c 9753 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9754
9755 /* If non-NULL, ARG should have been advanced past the location;
9756 the next character must be ','. */
9757 if (arg != NULL)
9758 {
9759 if (arg[0] != ',' || arg[1] == '\0')
9760 error (_("Format string required"));
9761 else
9762 {
9763 /* Skip the comma. */
9764 ++arg;
9765 }
9766 }
9767
e7e0cddf 9768 create_breakpoint (get_current_arch (),
ffc2605c 9769 location.get (),
10a636cc 9770 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9771 0, bp_dprintf,
9772 0 /* Ignore count */,
9773 pending_break_support,
9774 &dprintf_breakpoint_ops,
9775 from_tty,
9776 1 /* enabled */,
9777 0 /* internal */,
9778 0);
9779}
9780
d3ce09f5 9781static void
0b39b52e 9782agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9783{
9784 error (_("May only run agent-printf on the target"));
9785}
9786
f1310107
TJB
9787/* Implement the "breakpoint_hit" breakpoint_ops method for
9788 ranged breakpoints. */
9789
9790static int
9791breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9792 const address_space *aspace,
09ac7c10
TT
9793 CORE_ADDR bp_addr,
9794 const struct target_waitstatus *ws)
f1310107 9795{
09ac7c10 9796 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9797 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9798 return 0;
9799
f1310107
TJB
9800 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9801 bl->length, aspace, bp_addr);
9802}
9803
9804/* Implement the "resources_needed" breakpoint_ops method for
9805 ranged breakpoints. */
9806
9807static int
9808resources_needed_ranged_breakpoint (const struct bp_location *bl)
9809{
9810 return target_ranged_break_num_registers ();
9811}
9812
9813/* Implement the "print_it" breakpoint_ops method for
9814 ranged breakpoints. */
9815
9816static enum print_stop_action
348d480f 9817print_it_ranged_breakpoint (bpstat bs)
f1310107 9818{
348d480f 9819 struct breakpoint *b = bs->breakpoint_at;
f1310107 9820 struct bp_location *bl = b->loc;
79a45e25 9821 struct ui_out *uiout = current_uiout;
f1310107
TJB
9822
9823 gdb_assert (b->type == bp_hardware_breakpoint);
9824
9825 /* Ranged breakpoints have only one location. */
9826 gdb_assert (bl && bl->next == NULL);
9827
9828 annotate_breakpoint (b->number);
f303dbd6
PA
9829
9830 maybe_print_thread_hit_breakpoint (uiout);
9831
f1310107 9832 if (b->disposition == disp_del)
112e8700 9833 uiout->text ("Temporary ranged breakpoint ");
f1310107 9834 else
112e8700
SM
9835 uiout->text ("Ranged breakpoint ");
9836 if (uiout->is_mi_like_p ())
f1310107 9837 {
112e8700 9838 uiout->field_string ("reason",
f1310107 9839 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9840 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9841 }
381befee 9842 uiout->field_signed ("bkptno", b->number);
112e8700 9843 uiout->text (", ");
f1310107
TJB
9844
9845 return PRINT_SRC_AND_LOC;
9846}
9847
9848/* Implement the "print_one" breakpoint_ops method for
9849 ranged breakpoints. */
9850
9851static void
9852print_one_ranged_breakpoint (struct breakpoint *b,
9853 struct bp_location **last_loc)
9854{
9855 struct bp_location *bl = b->loc;
9856 struct value_print_options opts;
79a45e25 9857 struct ui_out *uiout = current_uiout;
f1310107
TJB
9858
9859 /* Ranged breakpoints have only one location. */
9860 gdb_assert (bl && bl->next == NULL);
9861
9862 get_user_print_options (&opts);
9863
9864 if (opts.addressprint)
9865 /* We don't print the address range here, it will be printed later
9866 by print_one_detail_ranged_breakpoint. */
112e8700 9867 uiout->field_skip ("addr");
f1310107
TJB
9868 annotate_field (5);
9869 print_breakpoint_location (b, bl);
9870 *last_loc = bl;
9871}
9872
9873/* Implement the "print_one_detail" breakpoint_ops method for
9874 ranged breakpoints. */
9875
9876static void
9877print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9878 struct ui_out *uiout)
9879{
9880 CORE_ADDR address_start, address_end;
9881 struct bp_location *bl = b->loc;
d7e74731 9882 string_file stb;
f1310107
TJB
9883
9884 gdb_assert (bl);
9885
9886 address_start = bl->address;
9887 address_end = address_start + bl->length - 1;
9888
112e8700 9889 uiout->text ("\taddress range: ");
d7e74731
PA
9890 stb.printf ("[%s, %s]",
9891 print_core_address (bl->gdbarch, address_start),
9892 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9893 uiout->field_stream ("addr", stb);
9894 uiout->text ("\n");
f1310107
TJB
9895}
9896
9897/* Implement the "print_mention" breakpoint_ops method for
9898 ranged breakpoints. */
9899
9900static void
9901print_mention_ranged_breakpoint (struct breakpoint *b)
9902{
9903 struct bp_location *bl = b->loc;
79a45e25 9904 struct ui_out *uiout = current_uiout;
f1310107
TJB
9905
9906 gdb_assert (bl);
9907 gdb_assert (b->type == bp_hardware_breakpoint);
9908
2d33446d
TT
9909 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9910 b->number, paddress (bl->gdbarch, bl->address),
9911 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9912}
9913
9914/* Implement the "print_recreate" breakpoint_ops method for
9915 ranged breakpoints. */
9916
9917static void
9918print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9919{
f00aae0f 9920 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9921 event_location_to_string (b->location.get ()),
9922 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9923 print_recreate_thread (b, fp);
f1310107
TJB
9924}
9925
9926/* The breakpoint_ops structure to be used in ranged breakpoints. */
9927
2060206e 9928static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9929
9930/* Find the address where the end of the breakpoint range should be
9931 placed, given the SAL of the end of the range. This is so that if
9932 the user provides a line number, the end of the range is set to the
9933 last instruction of the given line. */
9934
9935static CORE_ADDR
9936find_breakpoint_range_end (struct symtab_and_line sal)
9937{
9938 CORE_ADDR end;
9939
9940 /* If the user provided a PC value, use it. Otherwise,
9941 find the address of the end of the given location. */
9942 if (sal.explicit_pc)
9943 end = sal.pc;
9944 else
9945 {
9946 int ret;
9947 CORE_ADDR start;
9948
9949 ret = find_line_pc_range (sal, &start, &end);
9950 if (!ret)
9951 error (_("Could not find location of the end of the range."));
9952
9953 /* find_line_pc_range returns the start of the next line. */
9954 end--;
9955 }
9956
9957 return end;
9958}
9959
9960/* Implement the "break-range" CLI command. */
9961
9962static void
0b39b52e 9963break_range_command (const char *arg, int from_tty)
f1310107 9964{
f2fc3015 9965 const char *arg_start;
f1310107
TJB
9966 struct linespec_result canonical_start, canonical_end;
9967 int bp_count, can_use_bp, length;
9968 CORE_ADDR end;
9969 struct breakpoint *b;
f1310107
TJB
9970
9971 /* We don't support software ranged breakpoints. */
9972 if (target_ranged_break_num_registers () < 0)
9973 error (_("This target does not support hardware ranged breakpoints."));
9974
9975 bp_count = hw_breakpoint_used_count ();
9976 bp_count += target_ranged_break_num_registers ();
9977 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9978 bp_count, 0);
9979 if (can_use_bp < 0)
9980 error (_("Hardware breakpoints used exceeds limit."));
9981
f8eba3c6 9982 arg = skip_spaces (arg);
f1310107
TJB
9983 if (arg == NULL || arg[0] == '\0')
9984 error(_("No address range specified."));
9985
f8eba3c6 9986 arg_start = arg;
ffc2605c
TT
9987 event_location_up start_location = string_to_event_location (&arg,
9988 current_language);
9989 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9990
9991 if (arg[0] != ',')
9992 error (_("Too few arguments."));
6c5b2ebe 9993 else if (canonical_start.lsals.empty ())
f1310107 9994 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9995
6c5b2ebe 9996 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9997
6c5b2ebe
PA
9998 if (canonical_start.lsals.size () > 1
9999 || lsal_start.sals.size () != 1)
f1310107
TJB
10000 error (_("Cannot create a ranged breakpoint with multiple locations."));
10001
6c5b2ebe 10002 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10003 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10004
10005 arg++; /* Skip the comma. */
f8eba3c6 10006 arg = skip_spaces (arg);
f1310107
TJB
10007
10008 /* Parse the end location. */
10009
f1310107
TJB
10010 arg_start = arg;
10011
f8eba3c6 10012 /* We call decode_line_full directly here instead of using
f1310107
TJB
10013 parse_breakpoint_sals because we need to specify the start location's
10014 symtab and line as the default symtab and line for the end of the
10015 range. This makes it possible to have ranges like "foo.c:27, +14",
10016 where +14 means 14 lines from the start location. */
ffc2605c
TT
10017 event_location_up end_location = string_to_event_location (&arg,
10018 current_language);
10019 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10020 sal_start.symtab, sal_start.line,
10021 &canonical_end, NULL, NULL);
10022
6c5b2ebe 10023 if (canonical_end.lsals.empty ())
f1310107 10024 error (_("Could not find location of the end of the range."));
f8eba3c6 10025
6c5b2ebe
PA
10026 const linespec_sals &lsal_end = canonical_end.lsals[0];
10027 if (canonical_end.lsals.size () > 1
10028 || lsal_end.sals.size () != 1)
f1310107
TJB
10029 error (_("Cannot create a ranged breakpoint with multiple locations."));
10030
6c5b2ebe 10031 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10032
10033 end = find_breakpoint_range_end (sal_end);
10034 if (sal_start.pc > end)
177b42fe 10035 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10036
10037 length = end - sal_start.pc + 1;
10038 if (length < 0)
10039 /* Length overflowed. */
10040 error (_("Address range too large."));
10041 else if (length == 1)
10042 {
10043 /* This range is simple enough to be handled by
10044 the `hbreak' command. */
81b1e71c 10045 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10046
10047 return;
10048 }
10049
10050 /* Now set up the breakpoint. */
10051 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10052 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10053 set_breakpoint_count (breakpoint_count + 1);
10054 b->number = breakpoint_count;
10055 b->disposition = disp_donttouch;
d28cd78a
TT
10056 b->location = std::move (start_location);
10057 b->location_range_end = std::move (end_location);
f1310107
TJB
10058 b->loc->length = length;
10059
f1310107 10060 mention (b);
76727919 10061 gdb::observers::breakpoint_created.notify (b);
44702360 10062 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10063}
10064
4a64f543
MS
10065/* Return non-zero if EXP is verified as constant. Returned zero
10066 means EXP is variable. Also the constant detection may fail for
10067 some constant expressions and in such case still falsely return
10068 zero. */
2e6e3d9c 10069
dab72643 10070static bool
65d79d4b
SDJ
10071watchpoint_exp_is_const (const struct expression *exp)
10072{
1eaebe02 10073 return exp->op->constant_p ();
65d79d4b
SDJ
10074}
10075
c1fc2657 10076/* Watchpoint destructor. */
3a5c3e22 10077
c1fc2657 10078watchpoint::~watchpoint ()
3a5c3e22 10079{
c1fc2657
SM
10080 xfree (this->exp_string);
10081 xfree (this->exp_string_reparse);
3a5c3e22
PA
10082}
10083
348d480f
PA
10084/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10085
10086static void
10087re_set_watchpoint (struct breakpoint *b)
10088{
3a5c3e22
PA
10089 struct watchpoint *w = (struct watchpoint *) b;
10090
348d480f
PA
10091 /* Watchpoint can be either on expression using entirely global
10092 variables, or it can be on local variables.
10093
10094 Watchpoints of the first kind are never auto-deleted, and even
10095 persist across program restarts. Since they can use variables
10096 from shared libraries, we need to reparse expression as libraries
10097 are loaded and unloaded.
10098
10099 Watchpoints on local variables can also change meaning as result
10100 of solib event. For example, if a watchpoint uses both a local
10101 and a global variables in expression, it's a local watchpoint,
10102 but unloading of a shared library will make the expression
10103 invalid. This is not a very common use case, but we still
10104 re-evaluate expression, to avoid surprises to the user.
10105
10106 Note that for local watchpoints, we re-evaluate it only if
10107 watchpoints frame id is still valid. If it's not, it means the
10108 watchpoint is out of scope and will be deleted soon. In fact,
10109 I'm not sure we'll ever be called in this case.
10110
10111 If a local watchpoint's frame id is still valid, then
3a5c3e22 10112 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10113
3a5c3e22
PA
10114 Don't do anything about disabled watchpoints, since they will be
10115 reevaluated again when enabled. */
10116 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10117}
10118
77b06cd7
TJB
10119/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10120
10121static int
10122insert_watchpoint (struct bp_location *bl)
10123{
3a5c3e22
PA
10124 struct watchpoint *w = (struct watchpoint *) bl->owner;
10125 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10126
10127 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10128 w->cond_exp.get ());
77b06cd7
TJB
10129}
10130
10131/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10132
10133static int
73971819 10134remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10135{
3a5c3e22
PA
10136 struct watchpoint *w = (struct watchpoint *) bl->owner;
10137 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10138
10139 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10140 w->cond_exp.get ());
e09342b5
TJB
10141}
10142
e09342b5 10143static int
348d480f 10144breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10145 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10146 const struct target_waitstatus *ws)
e09342b5 10147{
348d480f 10148 struct breakpoint *b = bl->owner;
3a5c3e22 10149 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10150
348d480f
PA
10151 /* Continuable hardware watchpoints are treated as non-existent if the
10152 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10153 some data address). Otherwise gdb won't stop on a break instruction
10154 in the code (not from a breakpoint) when a hardware watchpoint has
10155 been defined. Also skip watchpoints which we know did not trigger
10156 (did not match the data address). */
10157 if (is_hardware_watchpoint (b)
3a5c3e22 10158 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10159 return 0;
9c06b0b4 10160
348d480f 10161 return 1;
9c06b0b4
TJB
10162}
10163
348d480f
PA
10164static void
10165check_status_watchpoint (bpstat bs)
9c06b0b4 10166{
348d480f 10167 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10168
348d480f 10169 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10170}
10171
10172/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10173 hardware watchpoints. */
9c06b0b4
TJB
10174
10175static int
348d480f 10176resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10177{
3a5c3e22
PA
10178 struct watchpoint *w = (struct watchpoint *) bl->owner;
10179 int length = w->exact? 1 : bl->length;
348d480f
PA
10180
10181 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10182}
10183
10184/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10185 hardware watchpoints. */
9c06b0b4
TJB
10186
10187static int
348d480f 10188works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10189{
efa80663
PA
10190 /* Read and access watchpoints only work with hardware support. */
10191 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10192}
10193
9c06b0b4 10194static enum print_stop_action
348d480f 10195print_it_watchpoint (bpstat bs)
9c06b0b4 10196{
348d480f 10197 struct breakpoint *b;
348d480f 10198 enum print_stop_action result;
3a5c3e22 10199 struct watchpoint *w;
79a45e25 10200 struct ui_out *uiout = current_uiout;
348d480f
PA
10201
10202 gdb_assert (bs->bp_location_at != NULL);
10203
348d480f 10204 b = bs->breakpoint_at;
3a5c3e22 10205 w = (struct watchpoint *) b;
348d480f 10206
f303dbd6
PA
10207 annotate_watchpoint (b->number);
10208 maybe_print_thread_hit_breakpoint (uiout);
10209
d7e74731
PA
10210 string_file stb;
10211
76f9c9cf 10212 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10213 switch (b->type)
10214 {
348d480f 10215 case bp_watchpoint:
9c06b0b4 10216 case bp_hardware_watchpoint:
112e8700
SM
10217 if (uiout->is_mi_like_p ())
10218 uiout->field_string
10219 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10220 mention (b);
76f9c9cf 10221 tuple_emitter.emplace (uiout, "value");
112e8700 10222 uiout->text ("\nOld value = ");
850645cf 10223 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10224 uiout->field_stream ("old", stb);
10225 uiout->text ("\nNew value = ");
850645cf 10226 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10227 uiout->field_stream ("new", stb);
10228 uiout->text ("\n");
348d480f
PA
10229 /* More than one watchpoint may have been triggered. */
10230 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10231 break;
10232
10233 case bp_read_watchpoint:
112e8700
SM
10234 if (uiout->is_mi_like_p ())
10235 uiout->field_string
10236 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10237 mention (b);
76f9c9cf 10238 tuple_emitter.emplace (uiout, "value");
112e8700 10239 uiout->text ("\nValue = ");
850645cf 10240 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10241 uiout->field_stream ("value", stb);
10242 uiout->text ("\n");
348d480f 10243 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10244 break;
10245
10246 case bp_access_watchpoint:
348d480f
PA
10247 if (bs->old_val != NULL)
10248 {
112e8700
SM
10249 if (uiout->is_mi_like_p ())
10250 uiout->field_string
10251 ("reason",
348d480f
PA
10252 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10253 mention (b);
76f9c9cf 10254 tuple_emitter.emplace (uiout, "value");
112e8700 10255 uiout->text ("\nOld value = ");
850645cf 10256 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10257 uiout->field_stream ("old", stb);
10258 uiout->text ("\nNew value = ");
348d480f
PA
10259 }
10260 else
10261 {
10262 mention (b);
112e8700
SM
10263 if (uiout->is_mi_like_p ())
10264 uiout->field_string
10265 ("reason",
348d480f 10266 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10267 tuple_emitter.emplace (uiout, "value");
112e8700 10268 uiout->text ("\nValue = ");
348d480f 10269 }
850645cf 10270 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10271 uiout->field_stream ("new", stb);
10272 uiout->text ("\n");
348d480f 10273 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10274 break;
10275 default:
348d480f 10276 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10277 }
10278
348d480f
PA
10279 return result;
10280}
10281
10282/* Implement the "print_mention" breakpoint_ops method for hardware
10283 watchpoints. */
10284
10285static void
10286print_mention_watchpoint (struct breakpoint *b)
10287{
3a5c3e22 10288 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10289 struct ui_out *uiout = current_uiout;
46b9c129 10290 const char *tuple_name;
348d480f
PA
10291
10292 switch (b->type)
10293 {
10294 case bp_watchpoint:
112e8700 10295 uiout->text ("Watchpoint ");
46b9c129 10296 tuple_name = "wpt";
348d480f
PA
10297 break;
10298 case bp_hardware_watchpoint:
112e8700 10299 uiout->text ("Hardware watchpoint ");
46b9c129 10300 tuple_name = "wpt";
348d480f
PA
10301 break;
10302 case bp_read_watchpoint:
112e8700 10303 uiout->text ("Hardware read watchpoint ");
46b9c129 10304 tuple_name = "hw-rwpt";
348d480f
PA
10305 break;
10306 case bp_access_watchpoint:
112e8700 10307 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10308 tuple_name = "hw-awpt";
348d480f
PA
10309 break;
10310 default:
10311 internal_error (__FILE__, __LINE__,
10312 _("Invalid hardware watchpoint type."));
10313 }
10314
46b9c129 10315 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10316 uiout->field_signed ("number", b->number);
112e8700
SM
10317 uiout->text (": ");
10318 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10319}
10320
10321/* Implement the "print_recreate" breakpoint_ops method for
10322 watchpoints. */
10323
10324static void
10325print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10326{
3a5c3e22
PA
10327 struct watchpoint *w = (struct watchpoint *) b;
10328
348d480f
PA
10329 switch (b->type)
10330 {
10331 case bp_watchpoint:
10332 case bp_hardware_watchpoint:
10333 fprintf_unfiltered (fp, "watch");
10334 break;
10335 case bp_read_watchpoint:
10336 fprintf_unfiltered (fp, "rwatch");
10337 break;
10338 case bp_access_watchpoint:
10339 fprintf_unfiltered (fp, "awatch");
10340 break;
10341 default:
10342 internal_error (__FILE__, __LINE__,
10343 _("Invalid watchpoint type."));
10344 }
10345
3a5c3e22 10346 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10347 print_recreate_thread (b, fp);
348d480f
PA
10348}
10349
427cd150
TT
10350/* Implement the "explains_signal" breakpoint_ops method for
10351 watchpoints. */
10352
47591c29 10353static int
427cd150
TT
10354explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10355{
10356 /* A software watchpoint cannot cause a signal other than
10357 GDB_SIGNAL_TRAP. */
10358 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10359 return 0;
427cd150 10360
47591c29 10361 return 1;
427cd150
TT
10362}
10363
348d480f
PA
10364/* The breakpoint_ops structure to be used in hardware watchpoints. */
10365
2060206e 10366static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10367
10368/* Implement the "insert" breakpoint_ops method for
10369 masked hardware watchpoints. */
10370
10371static int
10372insert_masked_watchpoint (struct bp_location *bl)
10373{
3a5c3e22
PA
10374 struct watchpoint *w = (struct watchpoint *) bl->owner;
10375
10376 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10377 bl->watchpoint_type);
10378}
10379
10380/* Implement the "remove" breakpoint_ops method for
10381 masked hardware watchpoints. */
10382
10383static int
73971819 10384remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10385{
3a5c3e22
PA
10386 struct watchpoint *w = (struct watchpoint *) bl->owner;
10387
10388 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10389 bl->watchpoint_type);
348d480f
PA
10390}
10391
10392/* Implement the "resources_needed" breakpoint_ops method for
10393 masked hardware watchpoints. */
10394
10395static int
10396resources_needed_masked_watchpoint (const struct bp_location *bl)
10397{
3a5c3e22
PA
10398 struct watchpoint *w = (struct watchpoint *) bl->owner;
10399
10400 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10401}
10402
10403/* Implement the "works_in_software_mode" breakpoint_ops method for
10404 masked hardware watchpoints. */
10405
10406static int
10407works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10408{
10409 return 0;
10410}
10411
10412/* Implement the "print_it" breakpoint_ops method for
10413 masked hardware watchpoints. */
10414
10415static enum print_stop_action
10416print_it_masked_watchpoint (bpstat bs)
10417{
10418 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10419 struct ui_out *uiout = current_uiout;
348d480f
PA
10420
10421 /* Masked watchpoints have only one location. */
10422 gdb_assert (b->loc && b->loc->next == NULL);
10423
f303dbd6
PA
10424 annotate_watchpoint (b->number);
10425 maybe_print_thread_hit_breakpoint (uiout);
10426
348d480f
PA
10427 switch (b->type)
10428 {
10429 case bp_hardware_watchpoint:
112e8700
SM
10430 if (uiout->is_mi_like_p ())
10431 uiout->field_string
10432 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10433 break;
10434
10435 case bp_read_watchpoint:
112e8700
SM
10436 if (uiout->is_mi_like_p ())
10437 uiout->field_string
10438 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10439 break;
10440
10441 case bp_access_watchpoint:
112e8700
SM
10442 if (uiout->is_mi_like_p ())
10443 uiout->field_string
10444 ("reason",
348d480f
PA
10445 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10446 break;
10447 default:
10448 internal_error (__FILE__, __LINE__,
10449 _("Invalid hardware watchpoint type."));
10450 }
10451
10452 mention (b);
112e8700 10453 uiout->text (_("\n\
9c06b0b4
TJB
10454Check the underlying instruction at PC for the memory\n\
10455address and value which triggered this watchpoint.\n"));
112e8700 10456 uiout->text ("\n");
9c06b0b4
TJB
10457
10458 /* More than one watchpoint may have been triggered. */
10459 return PRINT_UNKNOWN;
10460}
10461
10462/* Implement the "print_one_detail" breakpoint_ops method for
10463 masked hardware watchpoints. */
10464
10465static void
10466print_one_detail_masked_watchpoint (const struct breakpoint *b,
10467 struct ui_out *uiout)
10468{
3a5c3e22
PA
10469 struct watchpoint *w = (struct watchpoint *) b;
10470
9c06b0b4
TJB
10471 /* Masked watchpoints have only one location. */
10472 gdb_assert (b->loc && b->loc->next == NULL);
10473
112e8700
SM
10474 uiout->text ("\tmask ");
10475 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10476 uiout->text ("\n");
9c06b0b4
TJB
10477}
10478
10479/* Implement the "print_mention" breakpoint_ops method for
10480 masked hardware watchpoints. */
10481
10482static void
10483print_mention_masked_watchpoint (struct breakpoint *b)
10484{
3a5c3e22 10485 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10486 struct ui_out *uiout = current_uiout;
46b9c129 10487 const char *tuple_name;
9c06b0b4
TJB
10488
10489 switch (b->type)
10490 {
10491 case bp_hardware_watchpoint:
112e8700 10492 uiout->text ("Masked hardware watchpoint ");
46b9c129 10493 tuple_name = "wpt";
9c06b0b4
TJB
10494 break;
10495 case bp_read_watchpoint:
112e8700 10496 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10497 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10498 break;
10499 case bp_access_watchpoint:
112e8700 10500 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10501 tuple_name = "hw-awpt";
9c06b0b4
TJB
10502 break;
10503 default:
10504 internal_error (__FILE__, __LINE__,
10505 _("Invalid hardware watchpoint type."));
10506 }
10507
46b9c129 10508 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10509 uiout->field_signed ("number", b->number);
112e8700
SM
10510 uiout->text (": ");
10511 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10512}
10513
10514/* Implement the "print_recreate" breakpoint_ops method for
10515 masked hardware watchpoints. */
10516
10517static void
10518print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10519{
3a5c3e22 10520 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10521
10522 switch (b->type)
10523 {
10524 case bp_hardware_watchpoint:
10525 fprintf_unfiltered (fp, "watch");
10526 break;
10527 case bp_read_watchpoint:
10528 fprintf_unfiltered (fp, "rwatch");
10529 break;
10530 case bp_access_watchpoint:
10531 fprintf_unfiltered (fp, "awatch");
10532 break;
10533 default:
10534 internal_error (__FILE__, __LINE__,
10535 _("Invalid hardware watchpoint type."));
10536 }
10537
53807e9f
TT
10538 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10539 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10540 print_recreate_thread (b, fp);
9c06b0b4
TJB
10541}
10542
10543/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10544
2060206e 10545static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10546
10547/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10548
f2478a7e 10549static bool
9c06b0b4
TJB
10550is_masked_watchpoint (const struct breakpoint *b)
10551{
10552 return b->ops == &masked_watchpoint_breakpoint_ops;
10553}
10554
53a5351d 10555/* accessflag: hw_write: watch write,
dda83cd7 10556 hw_read: watch read,
53a5351d 10557 hw_access: watch access (read or write) */
c906108c 10558static void
bbc13ae3 10559watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10560 bool just_location, bool internal)
c906108c 10561{
c1fc2657 10562 struct breakpoint *scope_breakpoint = NULL;
270140bd 10563 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10564 struct value *result;
bb9d5f81 10565 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10566 const char *exp_start = NULL;
10567 const char *exp_end = NULL;
10568 const char *tok, *end_tok;
9c06b0b4 10569 int toklen = -1;
bbc13ae3
KS
10570 const char *cond_start = NULL;
10571 const char *cond_end = NULL;
c906108c 10572 enum bptype bp_type;
37e4754d 10573 int thread = -1;
9c06b0b4
TJB
10574 /* Flag to indicate whether we are going to use masks for
10575 the hardware watchpoint. */
2e362716 10576 bool use_mask = false;
9c06b0b4 10577 CORE_ADDR mask = 0;
c906108c 10578
37e4754d
LM
10579 /* Make sure that we actually have parameters to parse. */
10580 if (arg != NULL && arg[0] != '\0')
10581 {
bbc13ae3
KS
10582 const char *value_start;
10583
10584 exp_end = arg + strlen (arg);
37e4754d 10585
9c06b0b4
TJB
10586 /* Look for "parameter value" pairs at the end
10587 of the arguments string. */
bbc13ae3 10588 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10589 {
10590 /* Skip whitespace at the end of the argument list. */
10591 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10592 tok--;
10593
10594 /* Find the beginning of the last token.
10595 This is the value of the parameter. */
10596 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10597 tok--;
10598 value_start = tok + 1;
10599
10600 /* Skip whitespace. */
10601 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10602 tok--;
10603
10604 end_tok = tok;
10605
10606 /* Find the beginning of the second to last token.
10607 This is the parameter itself. */
10608 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10609 tok--;
10610 tok++;
10611 toklen = end_tok - tok + 1;
10612
61012eef 10613 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10614 {
5d5658a1 10615 struct thread_info *thr;
9c06b0b4
TJB
10616 /* At this point we've found a "thread" token, which means
10617 the user is trying to set a watchpoint that triggers
10618 only in a specific thread. */
5d5658a1 10619 const char *endp;
37e4754d 10620
9c06b0b4
TJB
10621 if (thread != -1)
10622 error(_("You can specify only one thread."));
37e4754d 10623
9c06b0b4 10624 /* Extract the thread ID from the next token. */
5d5658a1 10625 thr = parse_thread_id (value_start, &endp);
37e4754d 10626
5d5658a1 10627 /* Check if the user provided a valid thread ID. */
9c06b0b4 10628 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10629 invalid_thread_id_error (value_start);
9c06b0b4 10630
5d5658a1 10631 thread = thr->global_num;
9c06b0b4 10632 }
61012eef 10633 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10634 {
10635 /* We've found a "mask" token, which means the user wants to
10636 create a hardware watchpoint that is going to have the mask
10637 facility. */
10638 struct value *mask_value, *mark;
37e4754d 10639
9c06b0b4
TJB
10640 if (use_mask)
10641 error(_("You can specify only one mask."));
37e4754d 10642
2e362716 10643 use_mask = just_location = true;
37e4754d 10644
9c06b0b4
TJB
10645 mark = value_mark ();
10646 mask_value = parse_to_comma_and_eval (&value_start);
10647 mask = value_as_address (mask_value);
10648 value_free_to_mark (mark);
10649 }
10650 else
10651 /* We didn't recognize what we found. We should stop here. */
10652 break;
37e4754d 10653
9c06b0b4
TJB
10654 /* Truncate the string and get rid of the "parameter value" pair before
10655 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10656 exp_end = tok;
9c06b0b4 10657 }
37e4754d 10658 }
bbc13ae3
KS
10659 else
10660 exp_end = arg;
37e4754d 10661
bbc13ae3
KS
10662 /* Parse the rest of the arguments. From here on out, everything
10663 is in terms of a newly allocated string instead of the original
10664 ARG. */
81b1e71c
TT
10665 std::string expression (arg, exp_end - arg);
10666 exp_start = arg = expression.c_str ();
699bd4cf
TT
10667 innermost_block_tracker tracker;
10668 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10669 exp_end = arg;
fa8a61dc
TT
10670 /* Remove trailing whitespace from the expression before saving it.
10671 This makes the eventual display of the expression string a bit
10672 prettier. */
10673 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10674 --exp_end;
10675
65d79d4b 10676 /* Checking if the expression is not constant. */
4d01a485 10677 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10678 {
10679 int len;
10680
10681 len = exp_end - exp_start;
10682 while (len > 0 && isspace (exp_start[len - 1]))
10683 len--;
10684 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10685 }
10686
699bd4cf 10687 exp_valid_block = tracker.block ();
b926417a 10688 struct value *mark = value_mark ();
850645cf 10689 struct value *val_as_value = nullptr;
1eaebe02
TT
10690 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10691 just_location);
06a64a0b 10692
850645cf 10693 if (val_as_value != NULL && just_location)
bb9d5f81 10694 {
850645cf
TT
10695 saved_bitpos = value_bitpos (val_as_value);
10696 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10697 }
10698
850645cf 10699 value_ref_ptr val;
06a64a0b
TT
10700 if (just_location)
10701 {
9c06b0b4
TJB
10702 int ret;
10703
06a64a0b 10704 exp_valid_block = NULL;
850645cf 10705 val = release_value (value_addr (result));
06a64a0b 10706 value_free_to_mark (mark);
9c06b0b4
TJB
10707
10708 if (use_mask)
10709 {
850645cf 10710 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10711 mask);
10712 if (ret == -1)
10713 error (_("This target does not support masked watchpoints."));
10714 else if (ret == -2)
10715 error (_("Invalid mask or memory region."));
10716 }
06a64a0b 10717 }
850645cf
TT
10718 else if (val_as_value != NULL)
10719 val = release_value (val_as_value);
c906108c 10720
f1735a53
TT
10721 tok = skip_spaces (arg);
10722 end_tok = skip_to_space (tok);
c906108c
SS
10723
10724 toklen = end_tok - tok;
10725 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10726 {
10727 tok = cond_start = end_tok + 1;
699bd4cf
TT
10728 innermost_block_tracker if_tracker;
10729 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10730
10731 /* The watchpoint expression may not be local, but the condition
10732 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10733 cond_exp_valid_block = if_tracker.block ();
60e1c644 10734
c906108c
SS
10735 cond_end = tok;
10736 }
10737 if (*tok)
8a3fe4f8 10738 error (_("Junk at end of command."));
c906108c 10739
441d7c93
PA
10740 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10741
10742 /* Save this because create_internal_breakpoint below invalidates
10743 'wp_frame'. */
10744 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10745
10746 /* If the expression is "local", then set up a "watchpoint scope"
10747 breakpoint at the point where we've left the scope of the watchpoint
10748 expression. Create the scope breakpoint before the watchpoint, so
10749 that we will encounter it first in bpstat_stop_status. */
441d7c93 10750 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10751 {
441d7c93
PA
10752 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10753
10754 if (frame_id_p (caller_frame_id))
edb3359d 10755 {
441d7c93
PA
10756 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10757 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10758
24b21115 10759 scope_breakpoint
441d7c93 10760 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10761 bp_watchpoint_scope,
10762 &momentary_breakpoint_ops);
d983da9c 10763
441d7c93
PA
10764 /* create_internal_breakpoint could invalidate WP_FRAME. */
10765 wp_frame = NULL;
10766
edb3359d 10767 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10768
edb3359d
DJ
10769 /* Automatically delete the breakpoint when it hits. */
10770 scope_breakpoint->disposition = disp_del;
d983da9c 10771
edb3359d 10772 /* Only break in the proper frame (help with recursion). */
441d7c93 10773 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10774
edb3359d 10775 /* Set the address at which we will stop. */
441d7c93
PA
10776 scope_breakpoint->loc->gdbarch = caller_arch;
10777 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10778 scope_breakpoint->loc->address
a6d9a66e
UW
10779 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10780 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10781 scope_breakpoint->type);
10782 }
d983da9c
DJ
10783 }
10784
e8369a73
AB
10785 /* Now set up the breakpoint. We create all watchpoints as hardware
10786 watchpoints here even if hardware watchpoints are turned off, a call
10787 to update_watchpoint later in this function will cause the type to
10788 drop back to bp_watchpoint (software watchpoint) if required. */
10789
10790 if (accessflag == hw_read)
10791 bp_type = bp_read_watchpoint;
10792 else if (accessflag == hw_access)
10793 bp_type = bp_access_watchpoint;
10794 else
10795 bp_type = bp_hardware_watchpoint;
3a5c3e22 10796
b270e6f9 10797 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10798
348d480f 10799 if (use_mask)
b270e6f9 10800 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10801 &masked_watchpoint_breakpoint_ops);
348d480f 10802 else
b270e6f9 10803 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10804 &watchpoint_breakpoint_ops);
c1fc2657
SM
10805 w->thread = thread;
10806 w->disposition = disp_donttouch;
10807 w->pspace = current_program_space;
b22e99fd 10808 w->exp = std::move (exp);
3a5c3e22
PA
10809 w->exp_valid_block = exp_valid_block;
10810 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10811 if (just_location)
10812 {
850645cf
TT
10813 struct type *t = value_type (val.get ());
10814 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10815
43cc5389 10816 w->exp_string_reparse
f16a9f57 10817 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10818
3a5c3e22 10819 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10820 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10821 }
10822 else
3a5c3e22 10823 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10824
10825 if (use_mask)
10826 {
3a5c3e22 10827 w->hw_wp_mask = mask;
9c06b0b4
TJB
10828 }
10829 else
10830 {
3a5c3e22 10831 w->val = val;
bb9d5f81
PP
10832 w->val_bitpos = saved_bitpos;
10833 w->val_bitsize = saved_bitsize;
4c1d86d9 10834 w->val_valid = true;
9c06b0b4 10835 }
77b06cd7 10836
c906108c 10837 if (cond_start)
c1fc2657 10838 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10839 else
c1fc2657 10840 w->cond_string = 0;
c5aa993b 10841
441d7c93 10842 if (frame_id_p (watchpoint_frame))
f6bc2008 10843 {
441d7c93 10844 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10845 w->watchpoint_thread = inferior_ptid;
f6bc2008 10846 }
c906108c 10847 else
f6bc2008 10848 {
3a5c3e22
PA
10849 w->watchpoint_frame = null_frame_id;
10850 w->watchpoint_thread = null_ptid;
f6bc2008 10851 }
c906108c 10852
d983da9c 10853 if (scope_breakpoint != NULL)
c906108c 10854 {
d983da9c
DJ
10855 /* The scope breakpoint is related to the watchpoint. We will
10856 need to act on them together. */
c1fc2657 10857 w->related_breakpoint = scope_breakpoint;
b270e6f9 10858 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10859 }
d983da9c 10860
06a64a0b
TT
10861 if (!just_location)
10862 value_free_to_mark (mark);
2d134ed3 10863
b270e6f9
TT
10864 /* Finally update the new watchpoint. This creates the locations
10865 that should be inserted. */
10866 update_watchpoint (w.get (), 1);
a9634178 10867
b270e6f9 10868 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10869}
10870
e09342b5 10871/* Return count of debug registers needed to watch the given expression.
e09342b5 10872 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10873
c906108c 10874static int
a6535de1 10875can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10876{
10877 int found_memory_cnt = 0;
10878
10879 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10880 if (!can_use_hw_watchpoints)
c906108c 10881 return 0;
c5aa993b 10882
a6535de1
TT
10883 gdb_assert (!vals.empty ());
10884 struct value *head = vals[0].get ();
10885
5c44784c
JM
10886 /* Make sure that the value of the expression depends only upon
10887 memory contents, and values computed from them within GDB. If we
10888 find any register references or function calls, we can't use a
10889 hardware watchpoint.
10890
10891 The idea here is that evaluating an expression generates a series
10892 of values, one holding the value of every subexpression. (The
10893 expression a*b+c has five subexpressions: a, b, a*b, c, and
10894 a*b+c.) GDB's values hold almost enough information to establish
10895 the criteria given above --- they identify memory lvalues,
10896 register lvalues, computed values, etcetera. So we can evaluate
10897 the expression, and then scan the chain of values that leaves
10898 behind to decide whether we can detect any possible change to the
10899 expression's final value using only hardware watchpoints.
10900
10901 However, I don't think that the values returned by inferior
10902 function calls are special in any way. So this function may not
10903 notice that an expression involving an inferior function call
10904 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10905 for (const value_ref_ptr &iter : vals)
c906108c 10906 {
a6535de1
TT
10907 struct value *v = iter.get ();
10908
5c44784c 10909 if (VALUE_LVAL (v) == lval_memory)
c906108c 10910 {
8464be76
DJ
10911 if (v != head && value_lazy (v))
10912 /* A lazy memory lvalue in the chain is one that GDB never
10913 needed to fetch; we either just used its address (e.g.,
10914 `a' in `a.b') or we never needed it at all (e.g., `a'
10915 in `a,b'). This doesn't apply to HEAD; if that is
10916 lazy then it was not readable, but watch it anyway. */
5c44784c 10917 ;
53a5351d 10918 else
5c44784c
JM
10919 {
10920 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10921 it with hardware watchpoints. */
df407dfe 10922 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10923
10924 /* We only watch structs and arrays if user asked for it
10925 explicitly, never if they just happen to appear in a
10926 middle of some value chain. */
10927 if (v == head
78134374
SM
10928 || (vtype->code () != TYPE_CODE_STRUCT
10929 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10930 {
42ae5230 10931 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10932 int len;
10933 int num_regs;
10934
a9634178 10935 len = (target_exact_watchpoints
e09342b5
TJB
10936 && is_scalar_type_recursive (vtype))?
10937 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10938
e09342b5
TJB
10939 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10940 if (!num_regs)
2e70b7b9
MS
10941 return 0;
10942 else
e09342b5 10943 found_memory_cnt += num_regs;
2e70b7b9 10944 }
5c44784c 10945 }
c5aa993b 10946 }
5086187c
AC
10947 else if (VALUE_LVAL (v) != not_lval
10948 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10949 return 0; /* These are values from the history (e.g., $1). */
5086187c 10950 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10951 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10952 }
10953
10954 /* The expression itself looks suitable for using a hardware
10955 watchpoint, but give the target machine a chance to reject it. */
10956 return found_memory_cnt;
10957}
10958
8b93c638 10959void
2e362716 10960watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10961{
84f4c1fe 10962 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10963}
10964
a15a5258
AB
10965/* Options for the watch, awatch, and rwatch commands. */
10966
10967struct watch_options
10968{
10969 /* For -location. */
10970 bool location = false;
10971};
10972
10973/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10974
10975 Historically GDB always accepted both '-location' and '-l' flags for
10976 these commands (both flags being synonyms). When converting to the
10977 newer option scheme only '-location' is added here. That's fine (for
10978 backward compatibility) as any non-ambiguous prefix of a flag will be
10979 accepted, so '-l', '-loc', are now all accepted.
10980
10981 What this means is that, if in the future, we add any new flag here
10982 that starts with '-l' then this will break backward compatibility, so
10983 please, don't do that! */
10984
10985static const gdb::option::option_def watch_option_defs[] = {
10986 gdb::option::flag_option_def<watch_options> {
10987 "location",
10988 [] (watch_options *opt) { return &opt->location; },
10989 N_("\
10990This evaluates EXPRESSION and watches the memory to which is refers.\n\
10991-l can be used as a short form of -location."),
10992 },
10993};
10994
10995/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10996 commands. */
10997
10998static gdb::option::option_def_group
10999make_watch_options_def_group (watch_options *opts)
11000{
11001 return {{watch_option_defs}, opts};
11002}
11003
06a64a0b
TT
11004/* A helper function that looks for the "-location" argument and then
11005 calls watch_command_1. */
11006
11007static void
0b39b52e 11008watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 11009{
a15a5258
AB
11010 watch_options opts;
11011 auto grp = make_watch_options_def_group (&opts);
11012 gdb::option::process_options
11013 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
11014 if (arg != nullptr && *arg == '\0')
11015 arg = nullptr;
11016
11017 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
11018}
06a64a0b 11019
a15a5258
AB
11020/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
11021static void
11022watch_command_completer (struct cmd_list_element *ignore,
11023 completion_tracker &tracker,
11024 const char *text, const char * /*word*/)
11025{
11026 const auto group = make_watch_options_def_group (nullptr);
11027 if (gdb::option::complete_options
11028 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
11029 return;
06a64a0b 11030
a15a5258
AB
11031 const char *word = advance_to_expression_complete_word_point (tracker, text);
11032 expression_completer (ignore, tracker, text, word);
8b93c638 11033}
8926118c 11034
c5aa993b 11035static void
0b39b52e 11036watch_command (const char *arg, int from_tty)
c906108c 11037{
06a64a0b 11038 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11039}
11040
8b93c638 11041void
2e362716 11042rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11043{
84f4c1fe 11044 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11045}
8926118c 11046
c5aa993b 11047static void
0b39b52e 11048rwatch_command (const char *arg, int from_tty)
c906108c 11049{
06a64a0b 11050 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11051}
11052
8b93c638 11053void
2e362716 11054awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11055{
84f4c1fe 11056 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11057}
8926118c 11058
c5aa993b 11059static void
0b39b52e 11060awatch_command (const char *arg, int from_tty)
c906108c 11061{
06a64a0b 11062 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11063}
c906108c 11064\f
c5aa993b 11065
cfc31633
PA
11066/* Data for the FSM that manages the until(location)/advance commands
11067 in infcmd.c. Here because it uses the mechanisms of
11068 breakpoints. */
c906108c 11069
46e3ed7f 11070struct until_break_fsm : public thread_fsm
bfec99b2 11071{
46e3ed7f 11072 /* The thread that was current when the command was executed. */
cfc31633
PA
11073 int thread;
11074
df631783
PA
11075 /* The breakpoint set at the return address in the caller frame,
11076 plus breakpoints at all the destination locations. */
11077 std::vector<breakpoint_up> breakpoints;
cfc31633 11078
46e3ed7f 11079 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11080 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11081 : thread_fsm (cmd_interp),
11082 thread (thread),
df631783 11083 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11084 {
11085 }
cfc31633 11086
46e3ed7f
TT
11087 void clean_up (struct thread_info *thread) override;
11088 bool should_stop (struct thread_info *thread) override;
11089 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11090};
11091
cfc31633
PA
11092/* Implementation of the 'should_stop' FSM method for the
11093 until(location)/advance commands. */
11094
46e3ed7f
TT
11095bool
11096until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11097{
df631783
PA
11098 for (const breakpoint_up &bp : breakpoints)
11099 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11100 bp.get ()) != NULL)
11101 {
11102 set_finished ();
11103 break;
11104 }
cfc31633 11105
46e3ed7f 11106 return true;
cfc31633
PA
11107}
11108
11109/* Implementation of the 'clean_up' FSM method for the
11110 until(location)/advance commands. */
11111
46e3ed7f
TT
11112void
11113until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11114{
cfc31633 11115 /* Clean up our temporary breakpoints. */
df631783 11116 breakpoints.clear ();
46e3ed7f 11117 delete_longjmp_breakpoint (thread);
cfc31633
PA
11118}
11119
11120/* Implementation of the 'async_reply_reason' FSM method for the
11121 until(location)/advance commands. */
11122
46e3ed7f
TT
11123enum async_reply_reason
11124until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11125{
11126 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11127}
11128
c906108c 11129void
f2fc3015 11130until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11131{
8556afb4
PA
11132 struct frame_info *frame;
11133 struct gdbarch *frame_gdbarch;
11134 struct frame_id stack_frame_id;
11135 struct frame_id caller_frame_id;
186c406b
TT
11136 int thread;
11137 struct thread_info *tp;
c906108c 11138
70509625 11139 clear_proceed_status (0);
c906108c
SS
11140
11141 /* Set a breakpoint where the user wants it and at return from
4a64f543 11142 this function. */
c5aa993b 11143
ffc2605c 11144 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11145
6c5b2ebe
PA
11146 std::vector<symtab_and_line> sals
11147 = (last_displayed_sal_is_valid ()
11148 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11149 get_last_displayed_symtab (),
11150 get_last_displayed_line ())
11151 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11152 NULL, NULL, 0));
c5aa993b 11153
df631783 11154 if (sals.empty ())
8a3fe4f8 11155 error (_("Couldn't get information on specified line."));
c5aa993b 11156
c906108c 11157 if (*arg)
8a3fe4f8 11158 error (_("Junk at end of arguments."));
c5aa993b 11159
186c406b 11160 tp = inferior_thread ();
5d5658a1 11161 thread = tp->global_num;
186c406b 11162
8556afb4
PA
11163 /* Note linespec handling above invalidates the frame chain.
11164 Installing a breakpoint also invalidates the frame chain (as it
11165 may need to switch threads), so do any frame handling before
11166 that. */
11167
11168 frame = get_selected_frame (NULL);
11169 frame_gdbarch = get_frame_arch (frame);
11170 stack_frame_id = get_stack_frame_id (frame);
11171 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11172
ae66c1fc
EZ
11173 /* Keep within the current frame, or in frames called by the current
11174 one. */
edb3359d 11175
df631783 11176 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11177
11178 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11179
883bc8d1 11180 if (frame_id_p (caller_frame_id))
c906108c 11181 {
883bc8d1 11182 struct symtab_and_line sal2;
cfc31633 11183 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11184
11185 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11186 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11187 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11188
11189 breakpoint_up caller_breakpoint
11190 = set_momentary_breakpoint (caller_gdbarch, sal2,
11191 caller_frame_id, bp_until);
11192 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11193
883bc8d1 11194 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11195 lj_deleter.emplace (thread);
c906108c 11196 }
c5aa993b 11197
c70a6932
JK
11198 /* set_momentary_breakpoint could invalidate FRAME. */
11199 frame = NULL;
11200
df631783
PA
11201 /* If the user told us to continue until a specified location, we
11202 don't specify a frame at which we need to stop. Otherwise,
11203 specify the selected frame, because we want to stop only at the
11204 very same frame. */
11205 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11206
11207 for (symtab_and_line &sal : sals)
11208 {
11209 resolve_sal_pc (&sal);
11210
11211 breakpoint_up location_breakpoint
11212 = set_momentary_breakpoint (frame_gdbarch, sal,
11213 stop_frame_id, bp_until);
11214 breakpoints.emplace_back (std::move (location_breakpoint));
11215 }
883bc8d1 11216
46e3ed7f 11217 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11218 std::move (breakpoints));
f107f563 11219
5419bdae
TT
11220 if (lj_deleter)
11221 lj_deleter->release ();
f107f563 11222
cfc31633 11223 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11224}
ae66c1fc 11225
c906108c
SS
11226/* This function attempts to parse an optional "if <cond>" clause
11227 from the arg string. If one is not found, it returns NULL.
c5aa993b 11228
c906108c
SS
11229 Else, it returns a pointer to the condition string. (It does not
11230 attempt to evaluate the string against a particular block.) And,
11231 it updates arg to point to the first character following the parsed
4a64f543 11232 if clause in the arg string. */
53a5351d 11233
63160a43
PA
11234const char *
11235ep_parse_optional_if_clause (const char **arg)
c906108c 11236{
63160a43 11237 const char *cond_string;
c5aa993b
JM
11238
11239 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11240 return NULL;
c5aa993b 11241
4a64f543 11242 /* Skip the "if" keyword. */
c906108c 11243 (*arg) += 2;
c5aa993b 11244
c906108c 11245 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11246 condition string. */
f1735a53 11247 *arg = skip_spaces (*arg);
c906108c 11248 cond_string = *arg;
c5aa993b 11249
4a64f543
MS
11250 /* Assume that the condition occupies the remainder of the arg
11251 string. */
c906108c 11252 (*arg) += strlen (cond_string);
c5aa993b 11253
c906108c
SS
11254 return cond_string;
11255}
c5aa993b 11256
c906108c
SS
11257/* Commands to deal with catching events, such as signals, exceptions,
11258 process start/exit, etc. */
c5aa993b
JM
11259
11260typedef enum
11261{
44feb3ce
TT
11262 catch_fork_temporary, catch_vfork_temporary,
11263 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11264}
11265catch_fork_kind;
11266
c906108c 11267static void
eb4c3f4a 11268catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11269 struct cmd_list_element *command)
c906108c 11270{
a6d9a66e 11271 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11272 const char *cond_string = NULL;
44feb3ce 11273 catch_fork_kind fork_kind;
44feb3ce
TT
11274
11275 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11276 bool temp = (fork_kind == catch_fork_temporary
11277 || fork_kind == catch_vfork_temporary);
c5aa993b 11278
44feb3ce
TT
11279 if (!arg)
11280 arg = "";
f1735a53 11281 arg = skip_spaces (arg);
c5aa993b 11282
c906108c 11283 /* The allowed syntax is:
c5aa993b
JM
11284 catch [v]fork
11285 catch [v]fork if <cond>
11286
4a64f543 11287 First, check if there's an if clause. */
c906108c 11288 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11289
c906108c 11290 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11291 error (_("Junk at end of arguments."));
c5aa993b 11292
c906108c 11293 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11294 and enable reporting of such events. */
c5aa993b
JM
11295 switch (fork_kind)
11296 {
44feb3ce
TT
11297 case catch_fork_temporary:
11298 case catch_fork_permanent:
b650a282 11299 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11300 &catch_fork_breakpoint_ops);
c906108c 11301 break;
44feb3ce
TT
11302 case catch_vfork_temporary:
11303 case catch_vfork_permanent:
b650a282 11304 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11305 &catch_vfork_breakpoint_ops);
c906108c 11306 break;
c5aa993b 11307 default:
8a3fe4f8 11308 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11309 break;
c5aa993b 11310 }
c906108c
SS
11311}
11312
11313static void
eb4c3f4a 11314catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11315 struct cmd_list_element *command)
c906108c 11316{
a6d9a66e 11317 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11318 const char *cond_string = NULL;
b650a282 11319 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11320
11321 if (!arg)
11322 arg = "";
f1735a53 11323 arg = skip_spaces (arg);
c906108c
SS
11324
11325 /* The allowed syntax is:
c5aa993b
JM
11326 catch exec
11327 catch exec if <cond>
c906108c 11328
4a64f543 11329 First, check if there's an if clause. */
c906108c
SS
11330 cond_string = ep_parse_optional_if_clause (&arg);
11331
11332 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11333 error (_("Junk at end of arguments."));
c906108c 11334
b270e6f9 11335 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11336 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11337 &catch_exec_breakpoint_ops);
11338 c->exec_pathname = NULL;
11339
b270e6f9 11340 install_breakpoint (0, std::move (c), 1);
c906108c 11341}
c5aa993b 11342
9ac4176b 11343void
28010a5d
PA
11344init_ada_exception_breakpoint (struct breakpoint *b,
11345 struct gdbarch *gdbarch,
11346 struct symtab_and_line sal,
f2fc3015 11347 const char *addr_string,
c0a91b2b 11348 const struct breakpoint_ops *ops,
28010a5d 11349 int tempflag,
349774ef 11350 int enabled,
28010a5d 11351 int from_tty)
f7f9143b 11352{
f7f9143b
JB
11353 if (from_tty)
11354 {
5af949e3
UW
11355 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11356 if (!loc_gdbarch)
11357 loc_gdbarch = gdbarch;
11358
6c95b8df
PA
11359 describe_other_breakpoints (loc_gdbarch,
11360 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11361 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11362 version for exception catchpoints, because two catchpoints
11363 used for different exception names will use the same address.
11364 In this case, a "breakpoint ... also set at..." warning is
11365 unproductive. Besides, the warning phrasing is also a bit
11366 inappropriate, we should use the word catchpoint, and tell
11367 the user what type of catchpoint it is. The above is good
11368 enough for now, though. */
f7f9143b
JB
11369 }
11370
f06f1252 11371 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11372
349774ef 11373 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11374 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11375 b->location = string_to_event_location (&addr_string,
11376 language_def (language_ada));
f7f9143b 11377 b->language = language_ada;
f7f9143b
JB
11378}
11379
c906108c
SS
11380\f
11381
81b1e71c 11382/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11383
11384static int
81b1e71c 11385compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11386{
81b1e71c
TT
11387 uintptr_t ua = (uintptr_t) a;
11388 uintptr_t ub = (uintptr_t) b;
8a2c437b 11389
81b1e71c 11390 if (a->number < b->number)
8a2c437b 11391 return -1;
81b1e71c 11392 else if (a->number > b->number)
8a2c437b
TT
11393 return 1;
11394
11395 /* Now sort by address, in case we see, e..g, two breakpoints with
11396 the number 0. */
11397 if (ua < ub)
11398 return -1;
94b0e70d 11399 return ua > ub ? 1 : 0;
8a2c437b
TT
11400}
11401
80f8a6eb 11402/* Delete breakpoints by address or line. */
c906108c
SS
11403
11404static void
0b39b52e 11405clear_command (const char *arg, int from_tty)
c906108c 11406{
c906108c 11407 int default_match;
c906108c 11408
6c5b2ebe
PA
11409 std::vector<symtab_and_line> decoded_sals;
11410 symtab_and_line last_sal;
11411 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11412 if (arg)
11413 {
6c5b2ebe
PA
11414 decoded_sals
11415 = decode_line_with_current_source (arg,
11416 (DECODE_LINE_FUNFIRSTLINE
11417 | DECODE_LINE_LIST_MODE));
c906108c 11418 default_match = 0;
6c5b2ebe 11419 sals = decoded_sals;
c906108c
SS
11420 }
11421 else
11422 {
1bfeeb0f
JL
11423 /* Set sal's line, symtab, pc, and pspace to the values
11424 corresponding to the last call to print_frame_info. If the
11425 codepoint is not valid, this will set all the fields to 0. */
51abb421 11426 last_sal = get_last_displayed_sal ();
6c5b2ebe 11427 if (last_sal.symtab == 0)
8a3fe4f8 11428 error (_("No source file specified."));
c906108c 11429
c906108c 11430 default_match = 1;
6c5b2ebe 11431 sals = last_sal;
c906108c
SS
11432 }
11433
4a64f543
MS
11434 /* We don't call resolve_sal_pc here. That's not as bad as it
11435 seems, because all existing breakpoints typically have both
11436 file/line and pc set. So, if clear is given file/line, we can
11437 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11438
11439 We only support clearing given the address explicitly
11440 present in breakpoint table. Say, we've set breakpoint
4a64f543 11441 at file:line. There were several PC values for that file:line,
ed0616c6 11442 due to optimization, all in one block.
4a64f543
MS
11443
11444 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11445 PC corresponding to the same file:line, the breakpoint won't
11446 be cleared. We probably can still clear the breakpoint, but
11447 since the other PC value is never presented to user, user
11448 can only find it by guessing, and it does not seem important
11449 to support that. */
11450
4a64f543
MS
11451 /* For each line spec given, delete bps which correspond to it. Do
11452 it in two passes, solely to preserve the current behavior that
11453 from_tty is forced true if we delete more than one
11454 breakpoint. */
c906108c 11455
81b1e71c 11456 std::vector<struct breakpoint *> found;
6c5b2ebe 11457 for (const auto &sal : sals)
c906108c 11458 {
05cba821
JK
11459 const char *sal_fullname;
11460
c906108c 11461 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11462 If line given (pc == 0), clear all bpts on specified line.
11463 If defaulting, clear all bpts on default line
11464 or at default pc.
c5aa993b 11465
dda83cd7 11466 defaulting sal.pc != 0 tests to do
c5aa993b 11467
dda83cd7
SM
11468 0 1 pc
11469 1 1 pc _and_ line
11470 0 0 line
11471 1 0 <can't happen> */
c906108c 11472
05cba821
JK
11473 sal_fullname = (sal.symtab == NULL
11474 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11475
4a64f543 11476 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 11477 for (breakpoint *b : all_breakpoints ())
c5aa993b 11478 {
0d381245 11479 int match = 0;
4a64f543 11480 /* Are we going to delete b? */
cc60f2e3 11481 if (b->type != bp_none && !is_watchpoint (b))
0d381245 11482 {
40cb8ca5 11483 for (bp_location *loc : b->locations ())
0d381245 11484 {
f8eba3c6
TT
11485 /* If the user specified file:line, don't allow a PC
11486 match. This matches historical gdb behavior. */
11487 int pc_match = (!sal.explicit_line
11488 && sal.pc
11489 && (loc->pspace == sal.pspace)
11490 && (loc->address == sal.pc)
11491 && (!section_is_overlay (loc->section)
11492 || loc->section == sal.section));
4aac40c8
TT
11493 int line_match = 0;
11494
11495 if ((default_match || sal.explicit_line)
2f202fde 11496 && loc->symtab != NULL
05cba821 11497 && sal_fullname != NULL
4aac40c8 11498 && sal.pspace == loc->pspace
05cba821
JK
11499 && loc->line_number == sal.line
11500 && filename_cmp (symtab_to_fullname (loc->symtab),
11501 sal_fullname) == 0)
11502 line_match = 1;
4aac40c8 11503
0d381245
VP
11504 if (pc_match || line_match)
11505 {
11506 match = 1;
11507 break;
11508 }
11509 }
11510 }
11511
11512 if (match)
81b1e71c 11513 found.push_back (b);
c906108c 11514 }
80f8a6eb 11515 }
8a2c437b 11516
80f8a6eb 11517 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11518 if (found.empty ())
80f8a6eb
MS
11519 {
11520 if (arg)
8a3fe4f8 11521 error (_("No breakpoint at %s."), arg);
80f8a6eb 11522 else
8a3fe4f8 11523 error (_("No breakpoint at this line."));
80f8a6eb 11524 }
c906108c 11525
8a2c437b 11526 /* Remove duplicates from the vec. */
81b1e71c 11527 std::sort (found.begin (), found.end (),
b926417a 11528 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11529 {
b926417a 11530 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11531 });
11532 found.erase (std::unique (found.begin (), found.end (),
b926417a 11533 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11534 {
b926417a 11535 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11536 }),
11537 found.end ());
8a2c437b 11538
81b1e71c 11539 if (found.size () > 1)
4a64f543 11540 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11541 if (from_tty)
a3f17187 11542 {
81b1e71c 11543 if (found.size () == 1)
a3f17187
AC
11544 printf_unfiltered (_("Deleted breakpoint "));
11545 else
11546 printf_unfiltered (_("Deleted breakpoints "));
11547 }
d6e956e5 11548
81b1e71c 11549 for (breakpoint *iter : found)
80f8a6eb 11550 {
c5aa993b 11551 if (from_tty)
81b1e71c
TT
11552 printf_unfiltered ("%d ", iter->number);
11553 delete_breakpoint (iter);
c906108c 11554 }
80f8a6eb
MS
11555 if (from_tty)
11556 putchar_unfiltered ('\n');
c906108c
SS
11557}
11558\f
11559/* Delete breakpoint in BS if they are `delete' breakpoints and
11560 all breakpoints that are marked for deletion, whether hit or not.
11561 This is called after any breakpoint is hit, or after errors. */
11562
11563void
fba45db2 11564breakpoint_auto_delete (bpstat bs)
c906108c 11565{
c906108c 11566 for (; bs; bs = bs->next)
f431efe5
PA
11567 if (bs->breakpoint_at
11568 && bs->breakpoint_at->disposition == disp_del
c906108c 11569 && bs->stop)
f431efe5 11570 delete_breakpoint (bs->breakpoint_at);
c906108c 11571
1428b37a 11572 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 11573 if (b->disposition == disp_del_at_next_stop)
c5aa993b 11574 delete_breakpoint (b);
c906108c
SS
11575}
11576
4a64f543 11577/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11578 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11579 bl_address_is_meaningful says), secondarily by ordering first
11580 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11581 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11582
11583static int
39ef2f62 11584bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11585{
876fa593 11586 if (a->address != b->address)
39ef2f62 11587 return a->address < b->address;
876fa593 11588
dea2aa5f
LM
11589 /* Sort locations at the same address by their pspace number, keeping
11590 locations of the same inferior (in a multi-inferior environment)
11591 grouped. */
11592
11593 if (a->pspace->num != b->pspace->num)
39ef2f62 11594 return a->pspace->num < b->pspace->num;
dea2aa5f 11595
876fa593 11596 /* Sort permanent breakpoints first. */
1a853c52 11597 if (a->permanent != b->permanent)
39ef2f62 11598 return a->permanent > b->permanent;
876fa593 11599
7f32a4d5
PA
11600 /* Sort by type in order to make duplicate determination easier.
11601 See update_global_location_list. This is kept in sync with
11602 breakpoint_locations_match. */
11603 if (a->loc_type < b->loc_type)
11604 return true;
11605
11606 /* Likewise, for range-breakpoints, sort by length. */
11607 if (a->loc_type == bp_loc_hardware_breakpoint
11608 && b->loc_type == bp_loc_hardware_breakpoint
11609 && a->length < b->length)
11610 return true;
11611
c56a97f9
JK
11612 /* Make the internal GDB representation stable across GDB runs
11613 where A and B memory inside GDB can differ. Breakpoint locations of
11614 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11615
11616 if (a->owner->number != b->owner->number)
39ef2f62 11617 return a->owner->number < b->owner->number;
876fa593 11618
39ef2f62 11619 return a < b;
876fa593
JK
11620}
11621
f5336ca5
PA
11622/* Set bp_locations_placed_address_before_address_max and
11623 bp_locations_shadow_len_after_address_max according to the current
11624 content of the bp_locations array. */
f7545552
TT
11625
11626static void
f5336ca5 11627bp_locations_target_extensions_update (void)
f7545552 11628{
f5336ca5
PA
11629 bp_locations_placed_address_before_address_max = 0;
11630 bp_locations_shadow_len_after_address_max = 0;
876fa593 11631
48d7020b 11632 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11633 {
11634 CORE_ADDR start, end, addr;
11635
11636 if (!bp_location_has_shadow (bl))
11637 continue;
11638
11639 start = bl->target_info.placed_address;
11640 end = start + bl->target_info.shadow_len;
11641
11642 gdb_assert (bl->address >= start);
11643 addr = bl->address - start;
f5336ca5
PA
11644 if (addr > bp_locations_placed_address_before_address_max)
11645 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11646
11647 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11648
11649 gdb_assert (bl->address < end);
11650 addr = end - bl->address;
f5336ca5
PA
11651 if (addr > bp_locations_shadow_len_after_address_max)
11652 bp_locations_shadow_len_after_address_max = addr;
876fa593 11653 }
f7545552
TT
11654}
11655
1e4d1764
YQ
11656/* Download tracepoint locations if they haven't been. */
11657
11658static void
11659download_tracepoint_locations (void)
11660{
dd2e65cc 11661 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11662
5ed8105e 11663 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11664
f6d17b2b 11665 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11666 {
11667 struct tracepoint *t;
f2a8bc8a 11668 int bp_location_downloaded = 0;
1e4d1764 11669
7ed2c994 11670 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11671 ? !may_insert_fast_tracepoints
11672 : !may_insert_tracepoints))
11673 continue;
11674
dd2e65cc
YQ
11675 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11676 {
11677 if (target_can_download_tracepoint ())
11678 can_download_tracepoint = TRIBOOL_TRUE;
11679 else
11680 can_download_tracepoint = TRIBOOL_FALSE;
11681 }
11682
11683 if (can_download_tracepoint == TRIBOOL_FALSE)
11684 break;
11685
40cb8ca5 11686 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11687 {
11688 /* In tracepoint, locations are _never_ duplicated, so
11689 should_be_inserted is equivalent to
11690 unduplicated_should_be_inserted. */
11691 if (!should_be_inserted (bl) || bl->inserted)
11692 continue;
1e4d1764 11693
7ed2c994 11694 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11695
7ed2c994 11696 target_download_tracepoint (bl);
1e4d1764 11697
7ed2c994 11698 bl->inserted = 1;
f2a8bc8a 11699 bp_location_downloaded = 1;
7ed2c994
YQ
11700 }
11701 t = (struct tracepoint *) b;
11702 t->number_on_target = b->number;
f2a8bc8a 11703 if (bp_location_downloaded)
76727919 11704 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11705 }
1e4d1764
YQ
11706}
11707
934709f0
PW
11708/* Swap the insertion/duplication state between two locations. */
11709
11710static void
11711swap_insertion (struct bp_location *left, struct bp_location *right)
11712{
11713 const int left_inserted = left->inserted;
11714 const int left_duplicate = left->duplicate;
b775012e 11715 const int left_needs_update = left->needs_update;
934709f0
PW
11716 const struct bp_target_info left_target_info = left->target_info;
11717
1e4d1764
YQ
11718 /* Locations of tracepoints can never be duplicated. */
11719 if (is_tracepoint (left->owner))
11720 gdb_assert (!left->duplicate);
11721 if (is_tracepoint (right->owner))
11722 gdb_assert (!right->duplicate);
11723
934709f0
PW
11724 left->inserted = right->inserted;
11725 left->duplicate = right->duplicate;
b775012e 11726 left->needs_update = right->needs_update;
934709f0
PW
11727 left->target_info = right->target_info;
11728 right->inserted = left_inserted;
11729 right->duplicate = left_duplicate;
b775012e 11730 right->needs_update = left_needs_update;
934709f0
PW
11731 right->target_info = left_target_info;
11732}
11733
b775012e
LM
11734/* Force the re-insertion of the locations at ADDRESS. This is called
11735 once a new/deleted/modified duplicate location is found and we are evaluating
11736 conditions on the target's side. Such conditions need to be updated on
11737 the target. */
11738
11739static void
11740force_breakpoint_reinsertion (struct bp_location *bl)
11741{
b775012e
LM
11742 CORE_ADDR address = 0;
11743 int pspace_num;
11744
11745 address = bl->address;
11746 pspace_num = bl->pspace->num;
11747
11748 /* This is only meaningful if the target is
11749 evaluating conditions and if the user has
11750 opted for condition evaluation on the target's
11751 side. */
11752 if (gdb_evaluates_breakpoint_condition_p ()
11753 || !target_supports_evaluation_of_breakpoint_conditions ())
11754 return;
11755
11756 /* Flag all breakpoint locations with this address and
11757 the same program space as the location
11758 as "its condition has changed". We need to
11759 update the conditions on the target's side. */
e0d9a270 11760 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11761 {
b775012e
LM
11762 if (!is_breakpoint (loc->owner)
11763 || pspace_num != loc->pspace->num)
11764 continue;
11765
11766 /* Flag the location appropriately. We use a different state to
11767 let everyone know that we already updated the set of locations
11768 with addr bl->address and program space bl->pspace. This is so
11769 we don't have to keep calling these functions just to mark locations
11770 that have already been marked. */
11771 loc->condition_changed = condition_updated;
11772
11773 /* Free the agent expression bytecode as well. We will compute
11774 it later on. */
833177a4 11775 loc->cond_bytecode.reset ();
b775012e
LM
11776 }
11777}
7f32a4d5 11778
44702360
PA
11779/* Called whether new breakpoints are created, or existing breakpoints
11780 deleted, to update the global location list and recompute which
11781 locations are duplicate of which.
b775012e 11782
04086b45
PA
11783 The INSERT_MODE flag determines whether locations may not, may, or
11784 shall be inserted now. See 'enum ugll_insert_mode' for more
11785 info. */
b60e7edf 11786
0d381245 11787static void
44702360 11788update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11789{
b775012e
LM
11790 /* Last breakpoint location address that was marked for update. */
11791 CORE_ADDR last_addr = 0;
11792 /* Last breakpoint location program space that was marked for update. */
11793 int last_pspace_num = -1;
f7545552 11794
2d134ed3
PA
11795 /* Used in the duplicates detection below. When iterating over all
11796 bp_locations, points to the first bp_location of a given address.
11797 Breakpoints and watchpoints of different types are never
11798 duplicates of each other. Keep one pointer for each type of
11799 breakpoint/watchpoint, so we only need to loop over all locations
11800 once. */
11801 struct bp_location *bp_loc_first; /* breakpoint */
11802 struct bp_location *wp_loc_first; /* hardware watchpoint */
11803 struct bp_location *awp_loc_first; /* access watchpoint */
11804 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11805
f5336ca5
PA
11806 /* Saved former bp_locations array which we compare against the newly
11807 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11808 std::vector<bp_location *> old_locations = std::move (bp_locations);
11809 bp_locations.clear ();
876fa593 11810
43892fdf 11811 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11812 for (bp_location *loc : b->locations ())
5d51cd5d 11813 bp_locations.push_back (loc);
7f32a4d5
PA
11814
11815 /* See if we need to "upgrade" a software breakpoint to a hardware
11816 breakpoint. Do this before deciding whether locations are
11817 duplicates. Also do this before sorting because sorting order
11818 depends on location type. */
5d51cd5d
SM
11819 for (bp_location *loc : bp_locations)
11820 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11821 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11822
5d51cd5d 11823 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11824 bp_location_is_less_than);
876fa593 11825
f5336ca5 11826 bp_locations_target_extensions_update ();
74960c60 11827
4a64f543
MS
11828 /* Identify bp_location instances that are no longer present in the
11829 new list, and therefore should be freed. Note that it's not
11830 necessary that those locations should be removed from inferior --
11831 if there's another location at the same address (previously
11832 marked as duplicate), we don't need to remove/insert the
11833 location.
876fa593 11834
4a64f543
MS
11835 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11836 and former bp_location array state respectively. */
876fa593 11837
5d51cd5d
SM
11838 size_t loc_i = 0;
11839 for (bp_location *old_loc : old_locations)
74960c60 11840 {
e5dd4106 11841 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11842 not, we have to free it. */
c7d46a38 11843 int found_object = 0;
20874c92
VP
11844 /* Tells if the location should remain inserted in the target. */
11845 int keep_in_target = 0;
11846 int removed = 0;
876fa593 11847
4a64f543
MS
11848 /* Skip LOCP entries which will definitely never be needed.
11849 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11850 while (loc_i < bp_locations.size ()
11851 && bp_locations[loc_i]->address < old_loc->address)
11852 loc_i++;
c7d46a38 11853
5d51cd5d
SM
11854 for (size_t loc2_i = loc_i;
11855 (loc2_i < bp_locations.size ()
11856 && bp_locations[loc2_i]->address == old_loc->address);
11857 loc2_i++)
c7d46a38 11858 {
b775012e
LM
11859 /* Check if this is a new/duplicated location or a duplicated
11860 location that had its condition modified. If so, we want to send
11861 its condition to the target if evaluation of conditions is taking
11862 place there. */
5d51cd5d 11863 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11864 && (last_addr != old_loc->address
11865 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11866 {
5d51cd5d 11867 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11868 last_pspace_num = old_loc->pspace->num;
c7d46a38 11869 }
b775012e 11870
5d51cd5d 11871 if (bp_locations[loc2_i] == old_loc)
b775012e 11872 found_object = 1;
c7d46a38 11873 }
74960c60 11874
b775012e
LM
11875 /* We have already handled this address, update it so that we don't
11876 have to go through updates again. */
11877 last_addr = old_loc->address;
11878
11879 /* Target-side condition evaluation: Handle deleted locations. */
11880 if (!found_object)
11881 force_breakpoint_reinsertion (old_loc);
11882
4a64f543
MS
11883 /* If this location is no longer present, and inserted, look if
11884 there's maybe a new location at the same address. If so,
11885 mark that one inserted, and don't remove this one. This is
11886 needed so that we don't have a time window where a breakpoint
11887 at certain location is not inserted. */
74960c60 11888
876fa593 11889 if (old_loc->inserted)
0d381245 11890 {
4a64f543
MS
11891 /* If the location is inserted now, we might have to remove
11892 it. */
74960c60 11893
876fa593 11894 if (found_object && should_be_inserted (old_loc))
74960c60 11895 {
4a64f543
MS
11896 /* The location is still present in the location list,
11897 and still should be inserted. Don't do anything. */
20874c92 11898 keep_in_target = 1;
74960c60
VP
11899 }
11900 else
11901 {
b775012e
LM
11902 /* This location still exists, but it won't be kept in the
11903 target since it may have been disabled. We proceed to
11904 remove its target-side condition. */
11905
4a64f543
MS
11906 /* The location is either no longer present, or got
11907 disabled. See if there's another location at the
11908 same address, in which case we don't need to remove
11909 this one from the target. */
876fa593 11910
2bdf28a0 11911 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11912 if (bl_address_is_meaningful (old_loc))
876fa593 11913 {
5d51cd5d
SM
11914 for (size_t loc2_i = loc_i;
11915 (loc2_i < bp_locations.size ()
11916 && bp_locations[loc2_i]->address == old_loc->address);
11917 loc2_i++)
876fa593 11918 {
5d51cd5d 11919 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11920
7f32a4d5
PA
11921 if (loc2 == old_loc)
11922 continue;
11923
2d134ed3 11924 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11925 {
85d721b8
PA
11926 /* Read watchpoint locations are switched to
11927 access watchpoints, if the former are not
11928 supported, but the latter are. */
11929 if (is_hardware_watchpoint (old_loc->owner))
11930 {
11931 gdb_assert (is_hardware_watchpoint (loc2->owner));
11932 loc2->watchpoint_type = old_loc->watchpoint_type;
11933 }
11934
934709f0
PW
11935 /* loc2 is a duplicated location. We need to check
11936 if it should be inserted in case it will be
11937 unduplicated. */
7f32a4d5 11938 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11939 {
934709f0 11940 swap_insertion (old_loc, loc2);
c7d46a38
PA
11941 keep_in_target = 1;
11942 break;
11943 }
876fa593
JK
11944 }
11945 }
11946 }
74960c60
VP
11947 }
11948
20874c92
VP
11949 if (!keep_in_target)
11950 {
834c0d03 11951 if (remove_breakpoint (old_loc))
20874c92 11952 {
4a64f543
MS
11953 /* This is just about all we can do. We could keep
11954 this location on the global list, and try to
11955 remove it next time, but there's no particular
11956 reason why we will succeed next time.
20874c92 11957
4a64f543
MS
11958 Note that at this point, old_loc->owner is still
11959 valid, as delete_breakpoint frees the breakpoint
11960 only after calling us. */
3e43a32a
MS
11961 printf_filtered (_("warning: Error removing "
11962 "breakpoint %d\n"),
876fa593 11963 old_loc->owner->number);
20874c92
VP
11964 }
11965 removed = 1;
11966 }
0d381245 11967 }
74960c60
VP
11968
11969 if (!found_object)
1c5cfe86 11970 {
fbea99ea 11971 if (removed && target_is_non_stop_p ()
1cf4d951 11972 && need_moribund_for_location_type (old_loc))
20874c92 11973 {
db82e815
PA
11974 /* This location was removed from the target. In
11975 non-stop mode, a race condition is possible where
11976 we've removed a breakpoint, but stop events for that
11977 breakpoint are already queued and will arrive later.
11978 We apply an heuristic to be able to distinguish such
11979 SIGTRAPs from other random SIGTRAPs: we keep this
11980 breakpoint location for a bit, and will retire it
11981 after we see some number of events. The theory here
11982 is that reporting of events should, "on the average",
11983 be fair, so after a while we'll see events from all
11984 threads that have anything of interest, and no longer
11985 need to keep this breakpoint location around. We
11986 don't hold locations forever so to reduce chances of
11987 mistaking a non-breakpoint SIGTRAP for a breakpoint
11988 SIGTRAP.
11989
11990 The heuristic failing can be disastrous on
11991 decr_pc_after_break targets.
11992
11993 On decr_pc_after_break targets, like e.g., x86-linux,
11994 if we fail to recognize a late breakpoint SIGTRAP,
11995 because events_till_retirement has reached 0 too
11996 soon, we'll fail to do the PC adjustment, and report
11997 a random SIGTRAP to the user. When the user resumes
11998 the inferior, it will most likely immediately crash
2dec564e 11999 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12000 corrupted, because of being resumed e.g., in the
12001 middle of a multi-byte instruction, or skipped a
12002 one-byte instruction. This was actually seen happen
12003 on native x86-linux, and should be less rare on
12004 targets that do not support new thread events, like
12005 remote, due to the heuristic depending on
12006 thread_count.
12007
12008 Mistaking a random SIGTRAP for a breakpoint trap
12009 causes similar symptoms (PC adjustment applied when
12010 it shouldn't), but then again, playing with SIGTRAPs
12011 behind the debugger's back is asking for trouble.
12012
12013 Since hardware watchpoint traps are always
12014 distinguishable from other traps, so we don't need to
12015 apply keep hardware watchpoint moribund locations
12016 around. We simply always ignore hardware watchpoint
12017 traps we can no longer explain. */
12018
5b6d1e4f
PA
12019 process_stratum_target *proc_target = nullptr;
12020 for (inferior *inf : all_inferiors ())
12021 if (inf->pspace == old_loc->pspace)
12022 {
12023 proc_target = inf->process_target ();
12024 break;
12025 }
12026 if (proc_target != nullptr)
12027 old_loc->events_till_retirement
12028 = 3 * (thread_count (proc_target) + 1);
12029 else
12030 old_loc->events_till_retirement = 1;
876fa593 12031 old_loc->owner = NULL;
20874c92 12032
1123588c 12033 moribund_locations.push_back (old_loc);
1c5cfe86
PA
12034 }
12035 else
f431efe5
PA
12036 {
12037 old_loc->owner = NULL;
12038 decref_bp_location (&old_loc);
12039 }
20874c92 12040 }
74960c60 12041 }
1c5cfe86 12042
348d480f
PA
12043 /* Rescan breakpoints at the same address and section, marking the
12044 first one as "first" and any others as "duplicates". This is so
12045 that the bpt instruction is only inserted once. If we have a
12046 permanent breakpoint at the same place as BPT, make that one the
12047 official one, and the rest as duplicates. Permanent breakpoints
12048 are sorted first for the same address.
12049
12050 Do the same for hardware watchpoints, but also considering the
12051 watchpoint's type (regular/access/read) and length. */
12052
12053 bp_loc_first = NULL;
12054 wp_loc_first = NULL;
12055 awp_loc_first = NULL;
12056 rwp_loc_first = NULL;
40cb8ca5 12057
48d7020b 12058 for (bp_location *loc : all_bp_locations ())
348d480f
PA
12059 {
12060 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12061 non-NULL. */
348d480f 12062 struct bp_location **loc_first_p;
43892fdf 12063 breakpoint *b = loc->owner;
348d480f 12064
6f380991 12065 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12066 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12067 /* Don't detect duplicate for tracepoint locations because they are
12068 never duplicated. See the comments in field `duplicate' of
12069 `struct bp_location'. */
348d480f 12070 || is_tracepoint (b))
b775012e
LM
12071 {
12072 /* Clear the condition modification flag. */
12073 loc->condition_changed = condition_unchanged;
12074 continue;
12075 }
348d480f 12076
348d480f
PA
12077 if (b->type == bp_hardware_watchpoint)
12078 loc_first_p = &wp_loc_first;
12079 else if (b->type == bp_read_watchpoint)
12080 loc_first_p = &rwp_loc_first;
12081 else if (b->type == bp_access_watchpoint)
12082 loc_first_p = &awp_loc_first;
12083 else
12084 loc_first_p = &bp_loc_first;
12085
12086 if (*loc_first_p == NULL
12087 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12088 || !breakpoint_locations_match (loc, *loc_first_p))
12089 {
12090 *loc_first_p = loc;
12091 loc->duplicate = 0;
b775012e
LM
12092
12093 if (is_breakpoint (loc->owner) && loc->condition_changed)
12094 {
12095 loc->needs_update = 1;
12096 /* Clear the condition modification flag. */
12097 loc->condition_changed = condition_unchanged;
12098 }
348d480f
PA
12099 continue;
12100 }
12101
934709f0
PW
12102
12103 /* This and the above ensure the invariant that the first location
12104 is not duplicated, and is the inserted one.
12105 All following are marked as duplicated, and are not inserted. */
12106 if (loc->inserted)
12107 swap_insertion (loc, *loc_first_p);
348d480f
PA
12108 loc->duplicate = 1;
12109
b775012e
LM
12110 /* Clear the condition modification flag. */
12111 loc->condition_changed = condition_unchanged;
348d480f
PA
12112 }
12113
a25a5a45 12114 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12115 {
04086b45 12116 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12117 insert_breakpoint_locations ();
12118 else
12119 {
44702360
PA
12120 /* Even though the caller told us to not insert new
12121 locations, we may still need to update conditions on the
12122 target's side of breakpoints that were already inserted
12123 if the target is evaluating breakpoint conditions. We
b775012e
LM
12124 only update conditions for locations that are marked
12125 "needs_update". */
12126 update_inserted_breakpoint_locations ();
12127 }
12128 }
348d480f 12129
04086b45 12130 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12131 download_tracepoint_locations ();
348d480f
PA
12132}
12133
12134void
12135breakpoint_retire_moribund (void)
12136{
1123588c
TT
12137 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12138 {
12139 struct bp_location *loc = moribund_locations[ix];
12140 if (--(loc->events_till_retirement) == 0)
12141 {
12142 decref_bp_location (&loc);
12143 unordered_remove (moribund_locations, ix);
12144 --ix;
12145 }
12146 }
348d480f
PA
12147}
12148
12149static void
44702360 12150update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12151{
348d480f 12152
a70b8144 12153 try
492d29ea
PA
12154 {
12155 update_global_location_list (insert_mode);
12156 }
230d2906 12157 catch (const gdb_exception_error &e)
492d29ea
PA
12158 {
12159 }
348d480f
PA
12160}
12161
12162/* Clear BKP from a BPS. */
12163
12164static void
12165bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12166{
12167 bpstat bs;
12168
12169 for (bs = bps; bs; bs = bs->next)
12170 if (bs->breakpoint_at == bpt)
12171 {
12172 bs->breakpoint_at = NULL;
12173 bs->old_val = NULL;
12174 /* bs->commands will be freed later. */
12175 }
12176}
12177
12178/* Callback for iterate_over_threads. */
12179static int
12180bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12181{
9a3c8263 12182 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12183
12184 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12185 return 0;
12186}
12187
12188/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12189 callbacks. */
12190
12191static void
12192say_where (struct breakpoint *b)
12193{
12194 struct value_print_options opts;
12195
12196 get_user_print_options (&opts);
12197
12198 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12199 single string. */
12200 if (b->loc == NULL)
12201 {
f00aae0f
KS
12202 /* For pending locations, the output differs slightly based
12203 on b->extra_string. If this is non-NULL, it contains either
12204 a condition or dprintf arguments. */
12205 if (b->extra_string == NULL)
12206 {
12207 printf_filtered (_(" (%s) pending."),
d28cd78a 12208 event_location_to_string (b->location.get ()));
f00aae0f
KS
12209 }
12210 else if (b->type == bp_dprintf)
12211 {
12212 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12213 event_location_to_string (b->location.get ()),
f00aae0f
KS
12214 b->extra_string);
12215 }
12216 else
12217 {
12218 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12219 event_location_to_string (b->location.get ()),
f00aae0f
KS
12220 b->extra_string);
12221 }
348d480f
PA
12222 }
12223 else
12224 {
2f202fde 12225 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12226 printf_filtered (" at %ps",
12227 styled_string (address_style.style (),
12228 paddress (b->loc->gdbarch,
12229 b->loc->address)));
2f202fde 12230 if (b->loc->symtab != NULL)
f8eba3c6
TT
12231 {
12232 /* If there is a single location, we can print the location
12233 more nicely. */
12234 if (b->loc->next == NULL)
0bb296cb 12235 {
6a831f06
PA
12236 const char *filename
12237 = symtab_to_filename_for_display (b->loc->symtab);
12238 printf_filtered (": file %ps, line %d.",
12239 styled_string (file_name_style.style (),
12240 filename),
0bb296cb
TT
12241 b->loc->line_number);
12242 }
f8eba3c6
TT
12243 else
12244 /* This is not ideal, but each location may have a
12245 different file name, and this at least reflects the
12246 real situation somewhat. */
f00aae0f 12247 printf_filtered (": %s.",
d28cd78a 12248 event_location_to_string (b->location.get ()));
f8eba3c6 12249 }
348d480f
PA
12250
12251 if (b->loc->next)
12252 {
12253 struct bp_location *loc = b->loc;
12254 int n = 0;
12255 for (; loc; loc = loc->next)
12256 ++n;
12257 printf_filtered (" (%d locations)", n);
12258 }
12259 }
12260}
12261
5f486660 12262bp_location::~bp_location ()
348d480f 12263{
5f486660 12264 xfree (function_name);
348d480f
PA
12265}
12266
c1fc2657 12267/* Destructor for the breakpoint base class. */
348d480f 12268
c1fc2657 12269breakpoint::~breakpoint ()
348d480f 12270{
c1fc2657
SM
12271 xfree (this->cond_string);
12272 xfree (this->extra_string);
348d480f
PA
12273}
12274
40cb8ca5
SM
12275/* See breakpoint.h. */
12276
12277bp_locations_range breakpoint::locations ()
12278{
12279 return bp_locations_range (this->loc);
12280}
12281
2060206e
PA
12282static struct bp_location *
12283base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12284{
5f486660 12285 return new bp_location (self);
348d480f
PA
12286}
12287
2060206e
PA
12288static void
12289base_breakpoint_re_set (struct breakpoint *b)
12290{
12291 /* Nothing to re-set. */
12292}
12293
12294#define internal_error_pure_virtual_called() \
12295 gdb_assert_not_reached ("pure virtual function called")
12296
12297static int
12298base_breakpoint_insert_location (struct bp_location *bl)
12299{
12300 internal_error_pure_virtual_called ();
12301}
12302
12303static int
73971819
PA
12304base_breakpoint_remove_location (struct bp_location *bl,
12305 enum remove_bp_reason reason)
2060206e
PA
12306{
12307 internal_error_pure_virtual_called ();
12308}
12309
12310static int
12311base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12312 const address_space *aspace,
09ac7c10
TT
12313 CORE_ADDR bp_addr,
12314 const struct target_waitstatus *ws)
2060206e
PA
12315{
12316 internal_error_pure_virtual_called ();
12317}
12318
12319static void
12320base_breakpoint_check_status (bpstat bs)
12321{
12322 /* Always stop. */
12323}
12324
12325/* A "works_in_software_mode" breakpoint_ops method that just internal
12326 errors. */
12327
12328static int
12329base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12330{
12331 internal_error_pure_virtual_called ();
12332}
12333
12334/* A "resources_needed" breakpoint_ops method that just internal
12335 errors. */
12336
12337static int
12338base_breakpoint_resources_needed (const struct bp_location *bl)
12339{
12340 internal_error_pure_virtual_called ();
12341}
12342
12343static enum print_stop_action
12344base_breakpoint_print_it (bpstat bs)
12345{
12346 internal_error_pure_virtual_called ();
12347}
12348
12349static void
12350base_breakpoint_print_one_detail (const struct breakpoint *self,
12351 struct ui_out *uiout)
12352{
12353 /* nothing */
12354}
12355
12356static void
12357base_breakpoint_print_mention (struct breakpoint *b)
12358{
12359 internal_error_pure_virtual_called ();
12360}
12361
12362static void
12363base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12364{
12365 internal_error_pure_virtual_called ();
12366}
12367
983af33b 12368static void
f00aae0f 12369base_breakpoint_create_sals_from_location
626d2320 12370 (struct event_location *location,
f00aae0f
KS
12371 struct linespec_result *canonical,
12372 enum bptype type_wanted)
983af33b
SDJ
12373{
12374 internal_error_pure_virtual_called ();
12375}
12376
12377static void
12378base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12379 struct linespec_result *c,
e1e01040
PA
12380 gdb::unique_xmalloc_ptr<char> cond_string,
12381 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12382 enum bptype type_wanted,
12383 enum bpdisp disposition,
12384 int thread,
12385 int task, int ignore_count,
12386 const struct breakpoint_ops *o,
12387 int from_tty, int enabled,
44f238bb 12388 int internal, unsigned flags)
983af33b
SDJ
12389{
12390 internal_error_pure_virtual_called ();
12391}
12392
6c5b2ebe 12393static std::vector<symtab_and_line>
f00aae0f 12394base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12395 struct event_location *location,
6c5b2ebe 12396 struct program_space *search_pspace)
983af33b
SDJ
12397{
12398 internal_error_pure_virtual_called ();
12399}
12400
ab04a2af
TT
12401/* The default 'explains_signal' method. */
12402
47591c29 12403static int
427cd150 12404base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12405{
47591c29 12406 return 1;
ab04a2af
TT
12407}
12408
9d6e6e84
HZ
12409/* The default "after_condition_true" method. */
12410
12411static void
12412base_breakpoint_after_condition_true (struct bpstats *bs)
12413{
12414 /* Nothing to do. */
12415}
12416
ab04a2af 12417struct breakpoint_ops base_breakpoint_ops =
2060206e 12418{
2060206e
PA
12419 base_breakpoint_allocate_location,
12420 base_breakpoint_re_set,
12421 base_breakpoint_insert_location,
12422 base_breakpoint_remove_location,
12423 base_breakpoint_breakpoint_hit,
12424 base_breakpoint_check_status,
12425 base_breakpoint_resources_needed,
12426 base_breakpoint_works_in_software_mode,
12427 base_breakpoint_print_it,
12428 NULL,
12429 base_breakpoint_print_one_detail,
12430 base_breakpoint_print_mention,
983af33b 12431 base_breakpoint_print_recreate,
5f700d83 12432 base_breakpoint_create_sals_from_location,
983af33b 12433 base_breakpoint_create_breakpoints_sal,
5f700d83 12434 base_breakpoint_decode_location,
9d6e6e84
HZ
12435 base_breakpoint_explains_signal,
12436 base_breakpoint_after_condition_true,
2060206e
PA
12437};
12438
12439/* Default breakpoint_ops methods. */
12440
12441static void
348d480f
PA
12442bkpt_re_set (struct breakpoint *b)
12443{
06edf0c0 12444 /* FIXME: is this still reachable? */
9ef9e6a6 12445 if (breakpoint_event_location_empty_p (b))
06edf0c0 12446 {
f00aae0f 12447 /* Anything without a location can't be re-set. */
348d480f 12448 delete_breakpoint (b);
06edf0c0 12449 return;
348d480f 12450 }
06edf0c0
PA
12451
12452 breakpoint_re_set_default (b);
348d480f
PA
12453}
12454
2060206e 12455static int
348d480f
PA
12456bkpt_insert_location (struct bp_location *bl)
12457{
cd6c3b4f
YQ
12458 CORE_ADDR addr = bl->target_info.reqstd_address;
12459
579c6ad9 12460 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12461 bl->target_info.placed_address = addr;
12462
348d480f 12463 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12464 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12465 else
7c16b83e 12466 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12467}
12468
2060206e 12469static int
73971819 12470bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12471{
12472 if (bl->loc_type == bp_loc_hardware_breakpoint)
12473 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12474 else
73971819 12475 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12476}
12477
2060206e 12478static int
348d480f 12479bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12480 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12481 const struct target_waitstatus *ws)
348d480f 12482{
09ac7c10 12483 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12484 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12485 return 0;
12486
348d480f
PA
12487 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12488 aspace, bp_addr))
12489 return 0;
12490
12491 if (overlay_debugging /* unmapped overlay section */
12492 && section_is_overlay (bl->section)
12493 && !section_is_mapped (bl->section))
12494 return 0;
12495
12496 return 1;
12497}
12498
cd1608cc
PA
12499static int
12500dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12501 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12502 const struct target_waitstatus *ws)
12503{
12504 if (dprintf_style == dprintf_style_agent
12505 && target_can_run_breakpoint_commands ())
12506 {
12507 /* An agent-style dprintf never causes a stop. If we see a trap
12508 for this address it must be for a breakpoint that happens to
12509 be set at the same address. */
12510 return 0;
12511 }
12512
12513 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12514}
12515
2060206e 12516static int
348d480f
PA
12517bkpt_resources_needed (const struct bp_location *bl)
12518{
12519 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12520
12521 return 1;
12522}
12523
2060206e 12524static enum print_stop_action
348d480f
PA
12525bkpt_print_it (bpstat bs)
12526{
348d480f
PA
12527 struct breakpoint *b;
12528 const struct bp_location *bl;
001c8c33 12529 int bp_temp;
79a45e25 12530 struct ui_out *uiout = current_uiout;
348d480f
PA
12531
12532 gdb_assert (bs->bp_location_at != NULL);
12533
b6433ede 12534 bl = bs->bp_location_at.get ();
348d480f
PA
12535 b = bs->breakpoint_at;
12536
001c8c33
PA
12537 bp_temp = b->disposition == disp_del;
12538 if (bl->address != bl->requested_address)
12539 breakpoint_adjustment_warning (bl->requested_address,
12540 bl->address,
12541 b->number, 1);
12542 annotate_breakpoint (b->number);
f303dbd6
PA
12543 maybe_print_thread_hit_breakpoint (uiout);
12544
112e8700 12545 if (uiout->is_mi_like_p ())
348d480f 12546 {
112e8700 12547 uiout->field_string ("reason",
001c8c33 12548 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12549 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12550 }
6a831f06
PA
12551 if (bp_temp)
12552 uiout->message ("Temporary breakpoint %pF, ",
12553 signed_field ("bkptno", b->number));
12554 else
12555 uiout->message ("Breakpoint %pF, ",
12556 signed_field ("bkptno", b->number));
06edf0c0 12557
001c8c33 12558 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12559}
12560
2060206e 12561static void
06edf0c0
PA
12562bkpt_print_mention (struct breakpoint *b)
12563{
112e8700 12564 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12565 return;
12566
12567 switch (b->type)
12568 {
12569 case bp_breakpoint:
12570 case bp_gnu_ifunc_resolver:
12571 if (b->disposition == disp_del)
12572 printf_filtered (_("Temporary breakpoint"));
12573 else
12574 printf_filtered (_("Breakpoint"));
12575 printf_filtered (_(" %d"), b->number);
12576 if (b->type == bp_gnu_ifunc_resolver)
12577 printf_filtered (_(" at gnu-indirect-function resolver"));
12578 break;
12579 case bp_hardware_breakpoint:
12580 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12581 break;
e7e0cddf
SS
12582 case bp_dprintf:
12583 printf_filtered (_("Dprintf %d"), b->number);
12584 break;
06edf0c0
PA
12585 }
12586
12587 say_where (b);
12588}
12589
2060206e 12590static void
06edf0c0
PA
12591bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12592{
12593 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12594 fprintf_unfiltered (fp, "tbreak");
12595 else if (tp->type == bp_breakpoint)
12596 fprintf_unfiltered (fp, "break");
12597 else if (tp->type == bp_hardware_breakpoint
12598 && tp->disposition == disp_del)
12599 fprintf_unfiltered (fp, "thbreak");
12600 else if (tp->type == bp_hardware_breakpoint)
12601 fprintf_unfiltered (fp, "hbreak");
12602 else
12603 internal_error (__FILE__, __LINE__,
12604 _("unhandled breakpoint type %d"), (int) tp->type);
12605
f00aae0f 12606 fprintf_unfiltered (fp, " %s",
d28cd78a 12607 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12608
12609 /* Print out extra_string if this breakpoint is pending. It might
12610 contain, for example, conditions that were set by the user. */
12611 if (tp->loc == NULL && tp->extra_string != NULL)
12612 fprintf_unfiltered (fp, " %s", tp->extra_string);
12613
dd11a36c 12614 print_recreate_thread (tp, fp);
06edf0c0
PA
12615}
12616
983af33b 12617static void
626d2320 12618bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12619 struct linespec_result *canonical,
12620 enum bptype type_wanted)
983af33b 12621{
f00aae0f 12622 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12623}
12624
12625static void
12626bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12627 struct linespec_result *canonical,
e1e01040
PA
12628 gdb::unique_xmalloc_ptr<char> cond_string,
12629 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12630 enum bptype type_wanted,
12631 enum bpdisp disposition,
12632 int thread,
12633 int task, int ignore_count,
12634 const struct breakpoint_ops *ops,
12635 int from_tty, int enabled,
44f238bb 12636 int internal, unsigned flags)
983af33b 12637{
023fa29b 12638 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12639 std::move (cond_string),
12640 std::move (extra_string),
e7e0cddf 12641 type_wanted,
983af33b
SDJ
12642 disposition, thread, task,
12643 ignore_count, ops, from_tty,
44f238bb 12644 enabled, internal, flags);
983af33b
SDJ
12645}
12646
6c5b2ebe 12647static std::vector<symtab_and_line>
f00aae0f 12648bkpt_decode_location (struct breakpoint *b,
626d2320 12649 struct event_location *location,
6c5b2ebe 12650 struct program_space *search_pspace)
983af33b 12651{
6c5b2ebe 12652 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12653}
12654
06edf0c0
PA
12655/* Virtual table for internal breakpoints. */
12656
12657static void
12658internal_bkpt_re_set (struct breakpoint *b)
12659{
12660 switch (b->type)
12661 {
12662 /* Delete overlay event and longjmp master breakpoints; they
12663 will be reset later by breakpoint_re_set. */
12664 case bp_overlay_event:
12665 case bp_longjmp_master:
12666 case bp_std_terminate_master:
12667 case bp_exception_master:
12668 delete_breakpoint (b);
12669 break;
12670
12671 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12672 starts and we really don't want to touch it. */
06edf0c0
PA
12673 case bp_shlib_event:
12674
12675 /* Like bp_shlib_event, this breakpoint type is special. Once
12676 it is set up, we do not want to touch it. */
12677 case bp_thread_event:
12678 break;
12679 }
12680}
12681
12682static void
12683internal_bkpt_check_status (bpstat bs)
12684{
a9b3a50f
PA
12685 if (bs->breakpoint_at->type == bp_shlib_event)
12686 {
12687 /* If requested, stop when the dynamic linker notifies GDB of
12688 events. This allows the user to get control and place
12689 breakpoints in initializer routines for dynamically loaded
12690 objects (among other things). */
12691 bs->stop = stop_on_solib_events;
12692 bs->print = stop_on_solib_events;
12693 }
12694 else
12695 bs->stop = 0;
06edf0c0
PA
12696}
12697
12698static enum print_stop_action
12699internal_bkpt_print_it (bpstat bs)
12700{
06edf0c0 12701 struct breakpoint *b;
06edf0c0 12702
06edf0c0
PA
12703 b = bs->breakpoint_at;
12704
06edf0c0
PA
12705 switch (b->type)
12706 {
348d480f
PA
12707 case bp_shlib_event:
12708 /* Did we stop because the user set the stop_on_solib_events
12709 variable? (If so, we report this as a generic, "Stopped due
12710 to shlib event" message.) */
edcc5120 12711 print_solib_event (0);
348d480f
PA
12712 break;
12713
12714 case bp_thread_event:
12715 /* Not sure how we will get here.
12716 GDB should not stop for these breakpoints. */
12717 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12718 break;
12719
12720 case bp_overlay_event:
12721 /* By analogy with the thread event, GDB should not stop for these. */
12722 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12723 break;
12724
12725 case bp_longjmp_master:
12726 /* These should never be enabled. */
12727 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12728 break;
12729
12730 case bp_std_terminate_master:
12731 /* These should never be enabled. */
12732 printf_filtered (_("std::terminate Master Breakpoint: "
12733 "gdb should not stop!\n"));
348d480f
PA
12734 break;
12735
12736 case bp_exception_master:
12737 /* These should never be enabled. */
12738 printf_filtered (_("Exception Master Breakpoint: "
12739 "gdb should not stop!\n"));
06edf0c0
PA
12740 break;
12741 }
12742
001c8c33 12743 return PRINT_NOTHING;
06edf0c0
PA
12744}
12745
12746static void
12747internal_bkpt_print_mention (struct breakpoint *b)
12748{
12749 /* Nothing to mention. These breakpoints are internal. */
12750}
12751
06edf0c0
PA
12752/* Virtual table for momentary breakpoints */
12753
12754static void
12755momentary_bkpt_re_set (struct breakpoint *b)
12756{
12757 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12758 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12759 Otherwise these should have been blown away via the cleanup chain
12760 or by breakpoint_init_inferior when we rerun the executable. */
12761}
12762
12763static void
12764momentary_bkpt_check_status (bpstat bs)
12765{
12766 /* Nothing. The point of these breakpoints is causing a stop. */
12767}
12768
12769static enum print_stop_action
12770momentary_bkpt_print_it (bpstat bs)
12771{
001c8c33 12772 return PRINT_UNKNOWN;
348d480f
PA
12773}
12774
06edf0c0
PA
12775static void
12776momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12777{
06edf0c0 12778 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12779}
12780
e2e4d78b
JK
12781/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12782
12783 It gets cleared already on the removal of the first one of such placed
12784 breakpoints. This is OK as they get all removed altogether. */
12785
c1fc2657 12786longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12787{
c1fc2657 12788 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12789
c1fc2657 12790 if (tp != NULL)
e2e4d78b 12791 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12792}
12793
55aa24fb
SDJ
12794/* Specific methods for probe breakpoints. */
12795
12796static int
12797bkpt_probe_insert_location (struct bp_location *bl)
12798{
12799 int v = bkpt_insert_location (bl);
12800
12801 if (v == 0)
12802 {
12803 /* The insertion was successful, now let's set the probe's semaphore
12804 if needed. */
935676c9 12805 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12806 }
12807
12808 return v;
12809}
12810
12811static int
73971819
PA
12812bkpt_probe_remove_location (struct bp_location *bl,
12813 enum remove_bp_reason reason)
55aa24fb
SDJ
12814{
12815 /* Let's clear the semaphore before removing the location. */
935676c9 12816 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12817
73971819 12818 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12819}
12820
12821static void
626d2320 12822bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12823 struct linespec_result *canonical,
f00aae0f 12824 enum bptype type_wanted)
55aa24fb
SDJ
12825{
12826 struct linespec_sals lsal;
12827
c2f4122d 12828 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12829 lsal.canonical
12830 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12831 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12832}
12833
6c5b2ebe 12834static std::vector<symtab_and_line>
f00aae0f 12835bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12836 struct event_location *location,
6c5b2ebe 12837 struct program_space *search_pspace)
55aa24fb 12838{
6c5b2ebe
PA
12839 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12840 if (sals.empty ())
55aa24fb 12841 error (_("probe not found"));
6c5b2ebe 12842 return sals;
55aa24fb
SDJ
12843}
12844
348d480f 12845/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12846
348d480f
PA
12847static void
12848tracepoint_re_set (struct breakpoint *b)
12849{
12850 breakpoint_re_set_default (b);
12851}
876fa593 12852
348d480f
PA
12853static int
12854tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12855 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12856 const struct target_waitstatus *ws)
348d480f
PA
12857{
12858 /* By definition, the inferior does not report stops at
12859 tracepoints. */
12860 return 0;
74960c60
VP
12861}
12862
12863static void
348d480f
PA
12864tracepoint_print_one_detail (const struct breakpoint *self,
12865 struct ui_out *uiout)
74960c60 12866{
d9b3f62e 12867 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12868 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12869 {
12870 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12871
6a831f06
PA
12872 uiout->message ("\tmarker id is %pF\n",
12873 string_field ("static-tracepoint-marker-string-id",
12874 tp->static_trace_marker_id.c_str ()));
348d480f 12875 }
0d381245
VP
12876}
12877
a474d7c2 12878static void
348d480f 12879tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12880{
112e8700 12881 if (current_uiout->is_mi_like_p ())
348d480f 12882 return;
cc59ec59 12883
348d480f
PA
12884 switch (b->type)
12885 {
12886 case bp_tracepoint:
12887 printf_filtered (_("Tracepoint"));
12888 printf_filtered (_(" %d"), b->number);
12889 break;
12890 case bp_fast_tracepoint:
12891 printf_filtered (_("Fast tracepoint"));
12892 printf_filtered (_(" %d"), b->number);
12893 break;
12894 case bp_static_tracepoint:
12895 printf_filtered (_("Static tracepoint"));
12896 printf_filtered (_(" %d"), b->number);
12897 break;
12898 default:
12899 internal_error (__FILE__, __LINE__,
12900 _("unhandled tracepoint type %d"), (int) b->type);
12901 }
12902
12903 say_where (b);
a474d7c2
PA
12904}
12905
348d480f 12906static void
d9b3f62e 12907tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12908{
d9b3f62e
PA
12909 struct tracepoint *tp = (struct tracepoint *) self;
12910
12911 if (self->type == bp_fast_tracepoint)
348d480f 12912 fprintf_unfiltered (fp, "ftrace");
c93e8391 12913 else if (self->type == bp_static_tracepoint)
348d480f 12914 fprintf_unfiltered (fp, "strace");
d9b3f62e 12915 else if (self->type == bp_tracepoint)
348d480f
PA
12916 fprintf_unfiltered (fp, "trace");
12917 else
12918 internal_error (__FILE__, __LINE__,
d9b3f62e 12919 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12920
f00aae0f 12921 fprintf_unfiltered (fp, " %s",
d28cd78a 12922 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12923 print_recreate_thread (self, fp);
12924
12925 if (tp->pass_count)
12926 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12927}
12928
983af33b 12929static void
626d2320 12930tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12931 struct linespec_result *canonical,
12932 enum bptype type_wanted)
983af33b 12933{
f00aae0f 12934 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12935}
12936
12937static void
12938tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12939 struct linespec_result *canonical,
e1e01040
PA
12940 gdb::unique_xmalloc_ptr<char> cond_string,
12941 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12942 enum bptype type_wanted,
12943 enum bpdisp disposition,
12944 int thread,
12945 int task, int ignore_count,
12946 const struct breakpoint_ops *ops,
12947 int from_tty, int enabled,
44f238bb 12948 int internal, unsigned flags)
983af33b 12949{
023fa29b 12950 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12951 std::move (cond_string),
12952 std::move (extra_string),
e7e0cddf 12953 type_wanted,
983af33b
SDJ
12954 disposition, thread, task,
12955 ignore_count, ops, from_tty,
44f238bb 12956 enabled, internal, flags);
983af33b
SDJ
12957}
12958
6c5b2ebe 12959static std::vector<symtab_and_line>
f00aae0f 12960tracepoint_decode_location (struct breakpoint *b,
626d2320 12961 struct event_location *location,
6c5b2ebe 12962 struct program_space *search_pspace)
983af33b 12963{
6c5b2ebe 12964 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12965}
12966
2060206e 12967struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12968
bac7c5cf 12969/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12970
12971static void
f00aae0f 12972tracepoint_probe_create_sals_from_location
626d2320 12973 (struct event_location *location,
f00aae0f
KS
12974 struct linespec_result *canonical,
12975 enum bptype type_wanted)
55aa24fb
SDJ
12976{
12977 /* We use the same method for breakpoint on probes. */
f00aae0f 12978 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12979}
12980
6c5b2ebe 12981static std::vector<symtab_and_line>
f00aae0f 12982tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 12983 struct event_location *location,
6c5b2ebe 12984 struct program_space *search_pspace)
55aa24fb
SDJ
12985{
12986 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12987 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12988}
12989
5c2b4418
HZ
12990/* Dprintf breakpoint_ops methods. */
12991
12992static void
12993dprintf_re_set (struct breakpoint *b)
12994{
12995 breakpoint_re_set_default (b);
12996
f00aae0f
KS
12997 /* extra_string should never be non-NULL for dprintf. */
12998 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12999
13000 /* 1 - connect to target 1, that can run breakpoint commands.
13001 2 - create a dprintf, which resolves fine.
13002 3 - disconnect from target 1
13003 4 - connect to target 2, that can NOT run breakpoint commands.
13004
13005 After steps #3/#4, you'll want the dprintf command list to
13006 be updated, because target 1 and 2 may well return different
13007 answers for target_can_run_breakpoint_commands().
13008 Given absence of finer grained resetting, we get to do
13009 it all the time. */
13010 if (b->extra_string != NULL)
13011 update_dprintf_command_list (b);
13012}
13013
2d9442cc
HZ
13014/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13015
13016static void
13017dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13018{
f00aae0f 13019 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13020 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13021 tp->extra_string);
13022 print_recreate_thread (tp, fp);
13023}
13024
9d6e6e84
HZ
13025/* Implement the "after_condition_true" breakpoint_ops method for
13026 dprintf.
13027
13028 dprintf's are implemented with regular commands in their command
13029 list, but we run the commands here instead of before presenting the
13030 stop to the user, as dprintf's don't actually cause a stop. This
13031 also makes it so that the commands of multiple dprintfs at the same
13032 address are all handled. */
13033
13034static void
13035dprintf_after_condition_true (struct bpstats *bs)
13036{
04afa70c 13037 struct bpstats tmp_bs;
9d6e6e84
HZ
13038 struct bpstats *tmp_bs_p = &tmp_bs;
13039
13040 /* dprintf's never cause a stop. This wasn't set in the
13041 check_status hook instead because that would make the dprintf's
13042 condition not be evaluated. */
13043 bs->stop = 0;
13044
13045 /* Run the command list here. Take ownership of it instead of
13046 copying. We never want these commands to run later in
13047 bpstat_do_actions, if a breakpoint that causes a stop happens to
13048 be set at same address as this dprintf, or even if running the
13049 commands here throws. */
13050 tmp_bs.commands = bs->commands;
13051 bs->commands = NULL;
9d6e6e84
HZ
13052
13053 bpstat_do_actions_1 (&tmp_bs_p);
13054
13055 /* 'tmp_bs.commands' will usually be NULL by now, but
13056 bpstat_do_actions_1 may return early without processing the whole
13057 list. */
9d6e6e84
HZ
13058}
13059
983af33b
SDJ
13060/* The breakpoint_ops structure to be used on static tracepoints with
13061 markers (`-m'). */
13062
13063static void
626d2320 13064strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13065 struct linespec_result *canonical,
f00aae0f 13066 enum bptype type_wanted)
983af33b
SDJ
13067{
13068 struct linespec_sals lsal;
f00aae0f 13069 const char *arg_start, *arg;
983af33b 13070
a20714ff 13071 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13072 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13073
f2fc3015
TT
13074 std::string str (arg_start, arg - arg_start);
13075 const char *ptr = str.c_str ();
a20714ff
PA
13076 canonical->location
13077 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13078
8e9e35b1
TT
13079 lsal.canonical
13080 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13081 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13082}
13083
13084static void
13085strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13086 struct linespec_result *canonical,
e1e01040
PA
13087 gdb::unique_xmalloc_ptr<char> cond_string,
13088 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13089 enum bptype type_wanted,
13090 enum bpdisp disposition,
13091 int thread,
13092 int task, int ignore_count,
13093 const struct breakpoint_ops *ops,
13094 int from_tty, int enabled,
44f238bb 13095 int internal, unsigned flags)
983af33b 13096{
6c5b2ebe 13097 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13098
13099 /* If the user is creating a static tracepoint by marker id
13100 (strace -m MARKER_ID), then store the sals index, so that
13101 breakpoint_re_set can try to match up which of the newly
13102 found markers corresponds to this one, and, don't try to
13103 expand multiple locations for each sal, given than SALS
13104 already should contain all sals for MARKER_ID. */
13105
6c5b2ebe 13106 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13107 {
6c5b2ebe
PA
13108 event_location_up location
13109 = copy_event_location (canonical->location.get ());
983af33b 13110
b270e6f9 13111 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13112 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13113 std::move (location), NULL,
e1e01040
PA
13114 std::move (cond_string),
13115 std::move (extra_string),
e7e0cddf 13116 type_wanted, disposition,
983af33b 13117 thread, task, ignore_count, ops,
44f238bb 13118 from_tty, enabled, internal, flags,
983af33b
SDJ
13119 canonical->special_display);
13120 /* Given that its possible to have multiple markers with
13121 the same string id, if the user is creating a static
13122 tracepoint by marker id ("strace -m MARKER_ID"), then
13123 store the sals index, so that breakpoint_re_set can
13124 try to match up which of the newly found markers
13125 corresponds to this one */
13126 tp->static_trace_marker_id_idx = i;
13127
b270e6f9 13128 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13129 }
13130}
13131
6c5b2ebe 13132static std::vector<symtab_and_line>
f00aae0f 13133strace_marker_decode_location (struct breakpoint *b,
626d2320 13134 struct event_location *location,
6c5b2ebe 13135 struct program_space *search_pspace)
983af33b
SDJ
13136{
13137 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13138 const char *s = get_linespec_location (location)->spec_string;
983af33b 13139
6c5b2ebe
PA
13140 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13141 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13142 {
6c5b2ebe
PA
13143 sals[0] = sals[tp->static_trace_marker_id_idx];
13144 sals.resize (1);
13145 return sals;
983af33b
SDJ
13146 }
13147 else
5d9310c4 13148 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13149}
13150
13151static struct breakpoint_ops strace_marker_breakpoint_ops;
13152
13153static int
13154strace_marker_p (struct breakpoint *b)
13155{
13156 return b->ops == &strace_marker_breakpoint_ops;
13157}
13158
53a5351d 13159/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13160 structures. */
c906108c
SS
13161
13162void
fba45db2 13163delete_breakpoint (struct breakpoint *bpt)
c906108c 13164{
8a3fe4f8 13165 gdb_assert (bpt != NULL);
c906108c 13166
4a64f543
MS
13167 /* Has this bp already been deleted? This can happen because
13168 multiple lists can hold pointers to bp's. bpstat lists are
13169 especial culprits.
13170
13171 One example of this happening is a watchpoint's scope bp. When
13172 the scope bp triggers, we notice that the watchpoint is out of
13173 scope, and delete it. We also delete its scope bp. But the
13174 scope bp is marked "auto-deleting", and is already on a bpstat.
13175 That bpstat is then checked for auto-deleting bp's, which are
13176 deleted.
13177
13178 A real solution to this problem might involve reference counts in
13179 bp's, and/or giving them pointers back to their referencing
13180 bpstat's, and teaching delete_breakpoint to only free a bp's
13181 storage when no more references were extent. A cheaper bandaid
13182 was chosen. */
c906108c
SS
13183 if (bpt->type == bp_none)
13184 return;
13185
4a64f543
MS
13186 /* At least avoid this stale reference until the reference counting
13187 of breakpoints gets resolved. */
d0fb5eae 13188 if (bpt->related_breakpoint != bpt)
e5a0a904 13189 {
d0fb5eae 13190 struct breakpoint *related;
3a5c3e22 13191 struct watchpoint *w;
d0fb5eae
JK
13192
13193 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13194 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13195 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13196 w = (struct watchpoint *) bpt;
13197 else
13198 w = NULL;
13199 if (w != NULL)
13200 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13201
13202 /* Unlink bpt from the bpt->related_breakpoint ring. */
13203 for (related = bpt; related->related_breakpoint != bpt;
13204 related = related->related_breakpoint);
13205 related->related_breakpoint = bpt->related_breakpoint;
13206 bpt->related_breakpoint = bpt;
e5a0a904
JK
13207 }
13208
a9634178
TJB
13209 /* watch_command_1 creates a watchpoint but only sets its number if
13210 update_watchpoint succeeds in creating its bp_locations. If there's
13211 a problem in that process, we'll be asked to delete the half-created
13212 watchpoint. In that case, don't announce the deletion. */
13213 if (bpt->number)
76727919 13214 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13215
c906108c
SS
13216 if (breakpoint_chain == bpt)
13217 breakpoint_chain = bpt->next;
13218
43892fdf 13219 for (breakpoint *b : all_breakpoints ())
c906108c 13220 if (b->next == bpt)
01add95b
SM
13221 {
13222 b->next = bpt->next;
13223 break;
13224 }
c906108c 13225
f431efe5
PA
13226 /* Be sure no bpstat's are pointing at the breakpoint after it's
13227 been freed. */
13228 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13229 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13230 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13231 commands are associated with the bpstat; if we remove it here,
13232 then the later call to bpstat_do_actions (&stop_bpstat); in
13233 event-top.c won't do anything, and temporary breakpoints with
13234 commands won't work. */
13235
13236 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13237
4a64f543
MS
13238 /* Now that breakpoint is removed from breakpoint list, update the
13239 global location list. This will remove locations that used to
13240 belong to this breakpoint. Do this before freeing the breakpoint
13241 itself, since remove_breakpoint looks at location's owner. It
13242 might be better design to have location completely
13243 self-contained, but it's not the case now. */
44702360 13244 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13245
4a64f543
MS
13246 /* On the chance that someone will soon try again to delete this
13247 same bp, we mark it as deleted before freeing its storage. */
c906108c 13248 bpt->type = bp_none;
4d01a485 13249 delete bpt;
c906108c
SS
13250}
13251
51be5b68
PA
13252/* Iterator function to call a user-provided callback function once
13253 for each of B and its related breakpoints. */
13254
13255static void
13256iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13257 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13258{
13259 struct breakpoint *related;
13260
13261 related = b;
13262 do
13263 {
13264 struct breakpoint *next;
13265
13266 /* FUNCTION may delete RELATED. */
13267 next = related->related_breakpoint;
13268
13269 if (next == related)
13270 {
13271 /* RELATED is the last ring entry. */
48649e1b 13272 function (related);
51be5b68
PA
13273
13274 /* FUNCTION may have deleted it, so we'd never reach back to
13275 B. There's nothing left to do anyway, so just break
13276 out. */
13277 break;
13278 }
13279 else
48649e1b 13280 function (related);
51be5b68
PA
13281
13282 related = next;
13283 }
13284 while (related != b);
13285}
95a42b64 13286
4495129a 13287static void
981a3fb3 13288delete_command (const char *arg, int from_tty)
c906108c 13289{
ea9365bb
TT
13290 dont_repeat ();
13291
c906108c
SS
13292 if (arg == 0)
13293 {
13294 int breaks_to_delete = 0;
13295
46c6471b 13296 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13297 internal breakpoints, these have to be deleted with an
13298 explicit breakpoint number argument. */
43892fdf 13299 for (breakpoint *b : all_breakpoints ())
46c6471b 13300 if (user_breakpoint_p (b))
973d738b
DJ
13301 {
13302 breaks_to_delete = 1;
13303 break;
13304 }
c906108c
SS
13305
13306 /* Ask user only if there are some breakpoints to delete. */
13307 if (!from_tty
e2e0b3e5 13308 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
13309 for (breakpoint *b : all_breakpoints_safe ())
13310 if (user_breakpoint_p (b))
13311 delete_breakpoint (b);
c906108c
SS
13312 }
13313 else
48649e1b 13314 map_breakpoint_numbers
b926417a 13315 (arg, [&] (breakpoint *br)
48649e1b 13316 {
b926417a 13317 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13318 });
c906108c
SS
13319}
13320
c2f4122d
PA
13321/* Return true if all locations of B bound to PSPACE are pending. If
13322 PSPACE is NULL, all locations of all program spaces are
13323 considered. */
13324
0d381245 13325static int
c2f4122d 13326all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13327{
40cb8ca5 13328 for (bp_location *loc : b->locations ())
c2f4122d
PA
13329 if ((pspace == NULL
13330 || loc->pspace == pspace)
13331 && !loc->shlib_disabled
8645ff69 13332 && !loc->pspace->executing_startup)
0d381245
VP
13333 return 0;
13334 return 1;
fe3f5fa8
VP
13335}
13336
776592bf
DE
13337/* Subroutine of update_breakpoint_locations to simplify it.
13338 Return non-zero if multiple fns in list LOC have the same name.
13339 Null names are ignored. */
13340
13341static int
13342ambiguous_names_p (struct bp_location *loc)
13343{
13344 struct bp_location *l;
2698f5ea 13345 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 13346 xcalloc, xfree));
776592bf
DE
13347
13348 for (l = loc; l != NULL; l = l->next)
13349 {
13350 const char **slot;
13351 const char *name = l->function_name;
13352
13353 /* Allow for some names to be NULL, ignore them. */
13354 if (name == NULL)
13355 continue;
13356
c1fb9836 13357 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13358 INSERT);
4a64f543
MS
13359 /* NOTE: We can assume slot != NULL here because xcalloc never
13360 returns NULL. */
776592bf 13361 if (*slot != NULL)
c1fb9836 13362 return 1;
776592bf
DE
13363 *slot = name;
13364 }
13365
776592bf
DE
13366 return 0;
13367}
13368
0fb4aa4b
PA
13369/* When symbols change, it probably means the sources changed as well,
13370 and it might mean the static tracepoint markers are no longer at
13371 the same address or line numbers they used to be at last we
13372 checked. Losing your static tracepoints whenever you rebuild is
13373 undesirable. This function tries to resync/rematch gdb static
13374 tracepoints with the markers on the target, for static tracepoints
13375 that have not been set by marker id. Static tracepoint that have
13376 been set by marker id are reset by marker id in breakpoint_re_set.
13377 The heuristic is:
13378
13379 1) For a tracepoint set at a specific address, look for a marker at
13380 the old PC. If one is found there, assume to be the same marker.
13381 If the name / string id of the marker found is different from the
13382 previous known name, assume that means the user renamed the marker
13383 in the sources, and output a warning.
13384
13385 2) For a tracepoint set at a given line number, look for a marker
13386 at the new address of the old line number. If one is found there,
13387 assume to be the same marker. If the name / string id of the
13388 marker found is different from the previous known name, assume that
13389 means the user renamed the marker in the sources, and output a
13390 warning.
13391
13392 3) If a marker is no longer found at the same address or line, it
13393 may mean the marker no longer exists. But it may also just mean
13394 the code changed a bit. Maybe the user added a few lines of code
13395 that made the marker move up or down (in line number terms). Ask
13396 the target for info about the marker with the string id as we knew
13397 it. If found, update line number and address in the matching
13398 static tracepoint. This will get confused if there's more than one
13399 marker with the same ID (possible in UST, although unadvised
13400 precisely because it confuses tools). */
13401
13402static struct symtab_and_line
13403update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13404{
d9b3f62e 13405 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13406 struct static_tracepoint_marker marker;
13407 CORE_ADDR pc;
0fb4aa4b
PA
13408
13409 pc = sal.pc;
13410 if (sal.line)
13411 find_line_pc (sal.symtab, sal.line, &pc);
13412
13413 if (target_static_tracepoint_marker_at (pc, &marker))
13414 {
5d9310c4 13415 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13416 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13417 b->number, tp->static_trace_marker_id.c_str (),
13418 marker.str_id.c_str ());
0fb4aa4b 13419
5d9310c4 13420 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13421
13422 return sal;
13423 }
13424
13425 /* Old marker wasn't found on target at lineno. Try looking it up
13426 by string ID. */
13427 if (!sal.explicit_pc
13428 && sal.line != 0
13429 && sal.symtab != NULL
5d9310c4 13430 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13431 {
5d9310c4
SM
13432 std::vector<static_tracepoint_marker> markers
13433 = target_static_tracepoint_markers_by_strid
13434 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13435
5d9310c4 13436 if (!markers.empty ())
0fb4aa4b 13437 {
0fb4aa4b 13438 struct symbol *sym;
80e1d417 13439 struct static_tracepoint_marker *tpmarker;
79a45e25 13440 struct ui_out *uiout = current_uiout;
67994074 13441 struct explicit_location explicit_loc;
0fb4aa4b 13442
5d9310c4 13443 tpmarker = &markers[0];
0fb4aa4b 13444
5d9310c4 13445 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13446
13447 warning (_("marker for static tracepoint %d (%s) not "
13448 "found at previous line number"),
5d9310c4 13449 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13450
51abb421 13451 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13452 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13453 uiout->text ("Now in ");
0fb4aa4b
PA
13454 if (sym)
13455 {
987012b8 13456 uiout->field_string ("func", sym->print_name (),
e43b10e1 13457 function_name_style.style ());
112e8700 13458 uiout->text (" at ");
0fb4aa4b 13459 }
112e8700 13460 uiout->field_string ("file",
cbe56571 13461 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13462 file_name_style.style ());
112e8700 13463 uiout->text (":");
0fb4aa4b 13464
112e8700 13465 if (uiout->is_mi_like_p ())
0fb4aa4b 13466 {
0b0865da 13467 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13468
112e8700 13469 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13470 }
13471
381befee 13472 uiout->field_signed ("line", sal2.line);
112e8700 13473 uiout->text ("\n");
0fb4aa4b 13474
80e1d417 13475 b->loc->line_number = sal2.line;
2f202fde 13476 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13477
d28cd78a 13478 b->location.reset (NULL);
67994074
KS
13479 initialize_explicit_location (&explicit_loc);
13480 explicit_loc.source_filename
00e52e53 13481 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13482 explicit_loc.line_offset.offset = b->loc->line_number;
13483 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13484 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13485
13486 /* Might be nice to check if function changed, and warn if
13487 so. */
0fb4aa4b
PA
13488 }
13489 }
13490 return sal;
13491}
13492
8d3788bd
VP
13493/* Returns 1 iff locations A and B are sufficiently same that
13494 we don't need to report breakpoint as changed. */
13495
13496static int
13497locations_are_equal (struct bp_location *a, struct bp_location *b)
13498{
13499 while (a && b)
13500 {
13501 if (a->address != b->address)
13502 return 0;
13503
13504 if (a->shlib_disabled != b->shlib_disabled)
13505 return 0;
13506
13507 if (a->enabled != b->enabled)
13508 return 0;
13509
b5fa468f
TBA
13510 if (a->disabled_by_cond != b->disabled_by_cond)
13511 return 0;
13512
8d3788bd
VP
13513 a = a->next;
13514 b = b->next;
13515 }
13516
13517 if ((a == NULL) != (b == NULL))
13518 return 0;
13519
13520 return 1;
13521}
13522
c2f4122d
PA
13523/* Split all locations of B that are bound to PSPACE out of B's
13524 location list to a separate list and return that list's head. If
13525 PSPACE is NULL, hoist out all locations of B. */
13526
13527static struct bp_location *
13528hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13529{
13530 struct bp_location head;
13531 struct bp_location *i = b->loc;
13532 struct bp_location **i_link = &b->loc;
13533 struct bp_location *hoisted = &head;
13534
13535 if (pspace == NULL)
13536 {
13537 i = b->loc;
13538 b->loc = NULL;
13539 return i;
13540 }
13541
13542 head.next = NULL;
13543
13544 while (i != NULL)
13545 {
13546 if (i->pspace == pspace)
13547 {
13548 *i_link = i->next;
13549 i->next = NULL;
13550 hoisted->next = i;
13551 hoisted = i;
13552 }
13553 else
13554 i_link = &i->next;
13555 i = *i_link;
13556 }
13557
13558 return head.next;
13559}
13560
13561/* Create new breakpoint locations for B (a hardware or software
13562 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13563 zero, then B is a ranged breakpoint. Only recreates locations for
13564 FILTER_PSPACE. Locations of other program spaces are left
13565 untouched. */
f1310107 13566
0e30163f 13567void
0d381245 13568update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13569 struct program_space *filter_pspace,
6c5b2ebe
PA
13570 gdb::array_view<const symtab_and_line> sals,
13571 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13572{
c2f4122d 13573 struct bp_location *existing_locations;
0d381245 13574
6c5b2ebe 13575 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13576 {
13577 /* Ranged breakpoints have only one start location and one end
13578 location. */
13579 b->enable_state = bp_disabled;
f8eba3c6
TT
13580 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13581 "multiple locations found\n"),
13582 b->number);
13583 return;
13584 }
f1310107 13585
4a64f543
MS
13586 /* If there's no new locations, and all existing locations are
13587 pending, don't do anything. This optimizes the common case where
13588 all locations are in the same shared library, that was unloaded.
13589 We'd like to retain the location, so that when the library is
13590 loaded again, we don't loose the enabled/disabled status of the
13591 individual locations. */
6c5b2ebe 13592 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13593 return;
13594
c2f4122d 13595 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13596
6c5b2ebe 13597 for (const auto &sal : sals)
fe3f5fa8 13598 {
f8eba3c6
TT
13599 struct bp_location *new_loc;
13600
6c5b2ebe 13601 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13602
6c5b2ebe 13603 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13604
0d381245
VP
13605 /* Reparse conditions, they might contain references to the
13606 old symtab. */
13607 if (b->cond_string != NULL)
13608 {
bbc13ae3 13609 const char *s;
fe3f5fa8 13610
0d381245 13611 s = b->cond_string;
a70b8144 13612 try
0d381245 13613 {
6c5b2ebe
PA
13614 new_loc->cond = parse_exp_1 (&s, sal.pc,
13615 block_for_pc (sal.pc),
0d381245
VP
13616 0);
13617 }
230d2906 13618 catch (const gdb_exception_error &e)
0d381245 13619 {
b5fa468f 13620 new_loc->disabled_by_cond = true;
0d381245
VP
13621 }
13622 }
fe3f5fa8 13623
6c5b2ebe 13624 if (!sals_end.empty ())
f1310107 13625 {
6c5b2ebe 13626 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13627
6c5b2ebe 13628 new_loc->length = end - sals[0].pc + 1;
f1310107 13629 }
0d381245 13630 }
fe3f5fa8 13631
4a64f543
MS
13632 /* If possible, carry over 'disable' status from existing
13633 breakpoints. */
0d381245
VP
13634 {
13635 struct bp_location *e = existing_locations;
776592bf
DE
13636 /* If there are multiple breakpoints with the same function name,
13637 e.g. for inline functions, comparing function names won't work.
13638 Instead compare pc addresses; this is just a heuristic as things
13639 may have moved, but in practice it gives the correct answer
13640 often enough until a better solution is found. */
13641 int have_ambiguous_names = ambiguous_names_p (b->loc);
13642
0d381245
VP
13643 for (; e; e = e->next)
13644 {
b5fa468f 13645 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 13646 {
776592bf
DE
13647 if (have_ambiguous_names)
13648 {
40cb8ca5 13649 for (bp_location *l : b->locations ())
7f32a4d5
PA
13650 {
13651 /* Ignore software vs hardware location type at
13652 this point, because with "set breakpoint
13653 auto-hw", after a re-set, locations that were
13654 hardware can end up as software, or vice versa.
13655 As mentioned above, this is an heuristic and in
13656 practice should give the correct answer often
13657 enough. */
13658 if (breakpoint_locations_match (e, l, true))
13659 {
b5fa468f
TBA
13660 l->enabled = e->enabled;
13661 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13662 break;
13663 }
13664 }
776592bf
DE
13665 }
13666 else
13667 {
40cb8ca5 13668 for (bp_location *l : b->locations ())
776592bf
DE
13669 if (l->function_name
13670 && strcmp (e->function_name, l->function_name) == 0)
13671 {
b5fa468f
TBA
13672 l->enabled = e->enabled;
13673 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13674 break;
13675 }
13676 }
0d381245
VP
13677 }
13678 }
13679 }
fe3f5fa8 13680
8d3788bd 13681 if (!locations_are_equal (existing_locations, b->loc))
76727919 13682 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13683}
13684
f00aae0f 13685/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13686 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13687
6c5b2ebe 13688static std::vector<symtab_and_line>
f00aae0f 13689location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13690 struct program_space *search_pspace, int *found)
ef23e705 13691{
cc06b668 13692 struct gdb_exception exception;
ef23e705 13693
983af33b 13694 gdb_assert (b->ops != NULL);
ef23e705 13695
6c5b2ebe
PA
13696 std::vector<symtab_and_line> sals;
13697
a70b8144 13698 try
ef23e705 13699 {
6c5b2ebe 13700 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13701 }
94aeb44b 13702 catch (gdb_exception_error &e)
ef23e705
TJB
13703 {
13704 int not_found_and_ok = 0;
492d29ea 13705
ef23e705
TJB
13706 /* For pending breakpoints, it's expected that parsing will
13707 fail until the right shared library is loaded. User has
13708 already told to create pending breakpoints and don't need
13709 extra messages. If breakpoint is in bp_shlib_disabled
13710 state, then user already saw the message about that
13711 breakpoint being disabled, and don't want to see more
13712 errors. */
58438ac1 13713 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13714 && (b->condition_not_parsed
13715 || (b->loc != NULL
13716 && search_pspace != NULL
13717 && b->loc->pspace != search_pspace)
ef23e705 13718 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13719 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13720 || b->enable_state == bp_disabled))
13721 not_found_and_ok = 1;
13722
13723 if (!not_found_and_ok)
13724 {
13725 /* We surely don't want to warn about the same breakpoint
13726 10 times. One solution, implemented here, is disable
13727 the breakpoint on error. Another solution would be to
13728 have separate 'warning emitted' flag. Since this
13729 happens only when a binary has changed, I don't know
13730 which approach is better. */
13731 b->enable_state = bp_disabled;
eedc3f4f 13732 throw;
ef23e705 13733 }
94aeb44b
TT
13734
13735 exception = std::move (e);
ef23e705
TJB
13736 }
13737
492d29ea 13738 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13739 {
6c5b2ebe
PA
13740 for (auto &sal : sals)
13741 resolve_sal_pc (&sal);
f00aae0f 13742 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13743 {
ed1d1739
KS
13744 char *cond_string, *extra_string;
13745 int thread, task;
ef23e705 13746
b5fa468f
TBA
13747 find_condition_and_thread_for_sals (sals, b->extra_string,
13748 &cond_string, &thread,
13749 &task, &extra_string);
f00aae0f 13750 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13751 if (cond_string)
13752 b->cond_string = cond_string;
13753 b->thread = thread;
13754 b->task = task;
e7e0cddf 13755 if (extra_string)
f00aae0f
KS
13756 {
13757 xfree (b->extra_string);
13758 b->extra_string = extra_string;
13759 }
ef23e705
TJB
13760 b->condition_not_parsed = 0;
13761 }
13762
983af33b 13763 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13764 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13765
58438ac1
TT
13766 *found = 1;
13767 }
13768 else
13769 *found = 0;
ef23e705
TJB
13770
13771 return sals;
13772}
13773
348d480f
PA
13774/* The default re_set method, for typical hardware or software
13775 breakpoints. Reevaluate the breakpoint and recreate its
13776 locations. */
13777
13778static void
28010a5d 13779breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13780{
c2f4122d 13781 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13782 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13783
6c5b2ebe
PA
13784 int found;
13785 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13786 filter_pspace, &found);
ef23e705 13787 if (found)
6c5b2ebe 13788 expanded = std::move (sals);
ef23e705 13789
f00aae0f 13790 if (b->location_range_end != NULL)
f1310107 13791 {
6c5b2ebe
PA
13792 std::vector<symtab_and_line> sals_end
13793 = location_to_sals (b, b->location_range_end.get (),
13794 filter_pspace, &found);
f1310107 13795 if (found)
6c5b2ebe 13796 expanded_end = std::move (sals_end);
f1310107
TJB
13797 }
13798
c2f4122d 13799 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13800}
13801
983af33b
SDJ
13802/* Default method for creating SALs from an address string. It basically
13803 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13804
13805static void
626d2320 13806create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13807 struct linespec_result *canonical,
13808 enum bptype type_wanted)
983af33b 13809{
f00aae0f 13810 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13811}
13812
13813/* Call create_breakpoints_sal for the given arguments. This is the default
13814 function for the `create_breakpoints_sal' method of
13815 breakpoint_ops. */
13816
13817static void
13818create_breakpoints_sal_default (struct gdbarch *gdbarch,
13819 struct linespec_result *canonical,
e1e01040
PA
13820 gdb::unique_xmalloc_ptr<char> cond_string,
13821 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13822 enum bptype type_wanted,
13823 enum bpdisp disposition,
13824 int thread,
13825 int task, int ignore_count,
13826 const struct breakpoint_ops *ops,
13827 int from_tty, int enabled,
44f238bb 13828 int internal, unsigned flags)
983af33b 13829{
e1e01040
PA
13830 create_breakpoints_sal (gdbarch, canonical,
13831 std::move (cond_string),
13832 std::move (extra_string),
983af33b
SDJ
13833 type_wanted, disposition,
13834 thread, task, ignore_count, ops, from_tty,
44f238bb 13835 enabled, internal, flags);
983af33b
SDJ
13836}
13837
13838/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13839 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13840
6c5b2ebe 13841static std::vector<symtab_and_line>
f00aae0f 13842decode_location_default (struct breakpoint *b,
626d2320 13843 struct event_location *location,
6c5b2ebe 13844 struct program_space *search_pspace)
983af33b
SDJ
13845{
13846 struct linespec_result canonical;
13847
c2f4122d 13848 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13849 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13850 b->filter.get ());
983af33b
SDJ
13851
13852 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13853 gdb_assert (canonical.lsals.size () < 2);
983af33b 13854
6c5b2ebe 13855 if (!canonical.lsals.empty ())
983af33b 13856 {
6c5b2ebe
PA
13857 const linespec_sals &lsal = canonical.lsals[0];
13858 return std::move (lsal.sals);
983af33b 13859 }
6c5b2ebe 13860 return {};
983af33b
SDJ
13861}
13862
bf469271 13863/* Reset a breakpoint. */
c906108c 13864
bf469271
PA
13865static void
13866breakpoint_re_set_one (breakpoint *b)
c906108c 13867{
fdf44873
TT
13868 input_radix = b->input_radix;
13869 set_language (b->language);
c906108c 13870
348d480f 13871 b->ops->re_set (b);
c906108c
SS
13872}
13873
c2f4122d
PA
13874/* Re-set breakpoint locations for the current program space.
13875 Locations bound to other program spaces are left untouched. */
13876
c906108c 13877void
69de3c6a 13878breakpoint_re_set (void)
c906108c 13879{
c5aa993b 13880 {
fdf44873
TT
13881 scoped_restore_current_language save_language;
13882 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13883 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13884
8e817061
JB
13885 /* breakpoint_re_set_one sets the current_language to the language
13886 of the breakpoint it is resetting (see prepare_re_set_context)
13887 before re-evaluating the breakpoint's location. This change can
13888 unfortunately get undone by accident if the language_mode is set
13889 to auto, and we either switch frames, or more likely in this context,
13890 we select the current frame.
13891
13892 We prevent this by temporarily turning the language_mode to
13893 language_mode_manual. We restore it once all breakpoints
13894 have been reset. */
13895 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13896 language_mode = language_mode_manual;
13897
5ed8105e
PA
13898 /* Note: we must not try to insert locations until after all
13899 breakpoints have been re-set. Otherwise, e.g., when re-setting
13900 breakpoint 1, we'd insert the locations of breakpoint 2, which
13901 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13902
1428b37a 13903 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 13904 {
a70b8144 13905 try
bf469271
PA
13906 {
13907 breakpoint_re_set_one (b);
13908 }
230d2906 13909 catch (const gdb_exception &ex)
bf469271
PA
13910 {
13911 exception_fprintf (gdb_stderr, ex,
13912 "Error in re-setting breakpoint %d: ",
13913 b->number);
13914 }
5ed8105e 13915 }
5ed8105e
PA
13916
13917 jit_breakpoint_re_set ();
13918 }
6c95b8df 13919
af02033e
PP
13920 create_overlay_event_breakpoint ();
13921 create_longjmp_master_breakpoint ();
13922 create_std_terminate_master_breakpoint ();
186c406b 13923 create_exception_master_breakpoint ();
2a7f3dff
PA
13924
13925 /* Now we can insert. */
13926 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13927}
13928\f
c906108c
SS
13929/* Reset the thread number of this breakpoint:
13930
13931 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13932 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13933void
fba45db2 13934breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13935{
13936 if (b->thread != -1)
13937 {
00431a78 13938 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13939
13940 /* We're being called after following a fork. The new fork is
13941 selected as current, and unless this was a vfork will have a
13942 different program space from the original thread. Reset that
13943 as well. */
13944 b->loc->pspace = current_program_space;
c906108c
SS
13945 }
13946}
13947
03ac34d5
MS
13948/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13949 If from_tty is nonzero, it prints a message to that effect,
13950 which ends with a period (no newline). */
13951
c906108c 13952void
fba45db2 13953set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13954{
c906108c
SS
13955 if (count < 0)
13956 count = 0;
13957
43892fdf 13958 for (breakpoint *b : all_breakpoints ())
c906108c 13959 if (b->number == bptnum)
01add95b
SM
13960 {
13961 if (is_tracepoint (b))
13962 {
13963 if (from_tty && count != 0)
13964 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13965 bptnum);
13966 return;
13967 }
13968
13969 b->ignore_count = count;
13970 if (from_tty)
13971 {
13972 if (count == 0)
13973 printf_filtered (_("Will stop next time "
13974 "breakpoint %d is reached."),
13975 bptnum);
13976 else if (count == 1)
13977 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13978 bptnum);
13979 else
13980 printf_filtered (_("Will ignore next %d "
13981 "crossings of breakpoint %d."),
13982 count, bptnum);
13983 }
13984 gdb::observers::breakpoint_modified.notify (b);
13985 return;
13986 }
c906108c 13987
8a3fe4f8 13988 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13989}
13990
c906108c
SS
13991/* Command to set ignore-count of breakpoint N to COUNT. */
13992
13993static void
0b39b52e 13994ignore_command (const char *args, int from_tty)
c906108c 13995{
0b39b52e 13996 const char *p = args;
52f0bd74 13997 int num;
c906108c
SS
13998
13999 if (p == 0)
e2e0b3e5 14000 error_no_arg (_("a breakpoint number"));
c5aa993b 14001
c906108c 14002 num = get_number (&p);
5c44784c 14003 if (num == 0)
8a3fe4f8 14004 error (_("bad breakpoint number: '%s'"), args);
c906108c 14005 if (*p == 0)
8a3fe4f8 14006 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14007
14008 set_ignore_count (num,
14009 longest_to_int (value_as_long (parse_and_eval (p))),
14010 from_tty);
221ea385
KS
14011 if (from_tty)
14012 printf_filtered ("\n");
c906108c
SS
14013}
14014\f
d0fe4701
XR
14015
14016/* Call FUNCTION on each of the breakpoints with numbers in the range
14017 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14018
14019static void
d0fe4701
XR
14020map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14021 gdb::function_view<void (breakpoint *)> function)
c906108c 14022{
d0fe4701
XR
14023 if (bp_num_range.first == 0)
14024 {
14025 warning (_("bad breakpoint number at or near '%d'"),
14026 bp_num_range.first);
14027 }
14028 else
c906108c 14029 {
d0fe4701 14030 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14031 {
d0fe4701
XR
14032 bool match = false;
14033
1428b37a 14034 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 14035 if (b->number == i)
5c44784c 14036 {
bfd28288 14037 match = true;
48649e1b 14038 function (b);
11cf8741 14039 break;
5c44784c 14040 }
bfd28288 14041 if (!match)
d0fe4701 14042 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14043 }
c906108c
SS
14044 }
14045}
14046
d0fe4701
XR
14047/* Call FUNCTION on each of the breakpoints whose numbers are given in
14048 ARGS. */
14049
14050static void
14051map_breakpoint_numbers (const char *args,
14052 gdb::function_view<void (breakpoint *)> function)
14053{
14054 if (args == NULL || *args == '\0')
14055 error_no_arg (_("one or more breakpoint numbers"));
14056
14057 number_or_range_parser parser (args);
14058
14059 while (!parser.finished ())
14060 {
14061 int num = parser.get_number ();
14062 map_breakpoint_number_range (std::make_pair (num, num), function);
14063 }
14064}
14065
14066/* Return the breakpoint location structure corresponding to the
14067 BP_NUM and LOC_NUM values. */
14068
0d381245 14069static struct bp_location *
d0fe4701 14070find_location_by_number (int bp_num, int loc_num)
0d381245 14071{
43892fdf 14072 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
14073
14074 if (!b || b->number != bp_num)
d0fe4701 14075 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14076
0d381245 14077 if (loc_num == 0)
d0fe4701 14078 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14079
d0fe4701 14080 int n = 0;
40cb8ca5 14081 for (bp_location *loc : b->locations ())
d0fe4701
XR
14082 if (++n == loc_num)
14083 return loc;
14084
14085 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14086}
14087
95e95a6d
PA
14088/* Modes of operation for extract_bp_num. */
14089enum class extract_bp_kind
14090{
14091 /* Extracting a breakpoint number. */
14092 bp,
14093
14094 /* Extracting a location number. */
14095 loc,
14096};
14097
14098/* Extract a breakpoint or location number (as determined by KIND)
14099 from the string starting at START. TRAILER is a character which
14100 can be found after the number. If you don't want a trailer, use
14101 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14102 string. This always returns a positive integer. */
14103
14104static int
14105extract_bp_num (extract_bp_kind kind, const char *start,
14106 int trailer, const char **end_out = NULL)
14107{
14108 const char *end = start;
14109 int num = get_number_trailer (&end, trailer);
14110 if (num < 0)
14111 error (kind == extract_bp_kind::bp
14112 ? _("Negative breakpoint number '%.*s'")
14113 : _("Negative breakpoint location number '%.*s'"),
14114 int (end - start), start);
14115 if (num == 0)
14116 error (kind == extract_bp_kind::bp
14117 ? _("Bad breakpoint number '%.*s'")
14118 : _("Bad breakpoint location number '%.*s'"),
14119 int (end - start), start);
14120
14121 if (end_out != NULL)
14122 *end_out = end;
14123 return num;
14124}
14125
14126/* Extract a breakpoint or location range (as determined by KIND) in
14127 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14128 representing the (inclusive) range. The returned pair's elements
14129 are always positive integers. */
14130
14131static std::pair<int, int>
14132extract_bp_or_bp_range (extract_bp_kind kind,
14133 const std::string &arg,
14134 std::string::size_type arg_offset)
14135{
14136 std::pair<int, int> range;
14137 const char *bp_loc = &arg[arg_offset];
14138 std::string::size_type dash = arg.find ('-', arg_offset);
14139 if (dash != std::string::npos)
14140 {
14141 /* bp_loc is a range (x-z). */
14142 if (arg.length () == dash + 1)
14143 error (kind == extract_bp_kind::bp
14144 ? _("Bad breakpoint number at or near: '%s'")
14145 : _("Bad breakpoint location number at or near: '%s'"),
14146 bp_loc);
14147
14148 const char *end;
14149 const char *start_first = bp_loc;
14150 const char *start_second = &arg[dash + 1];
14151 range.first = extract_bp_num (kind, start_first, '-');
14152 range.second = extract_bp_num (kind, start_second, '\0', &end);
14153
14154 if (range.first > range.second)
14155 error (kind == extract_bp_kind::bp
14156 ? _("Inverted breakpoint range at '%.*s'")
14157 : _("Inverted breakpoint location range at '%.*s'"),
14158 int (end - start_first), start_first);
14159 }
14160 else
14161 {
14162 /* bp_loc is a single value. */
14163 range.first = extract_bp_num (kind, bp_loc, '\0');
14164 range.second = range.first;
14165 }
14166 return range;
14167}
14168
d0fe4701
XR
14169/* Extract the breakpoint/location range specified by ARG. Returns
14170 the breakpoint range in BP_NUM_RANGE, and the location range in
14171 BP_LOC_RANGE.
14172
14173 ARG may be in any of the following forms:
14174
14175 x where 'x' is a breakpoint number.
14176 x-y where 'x' and 'y' specify a breakpoint numbers range.
14177 x.y where 'x' is a breakpoint number and 'y' a location number.
14178 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14179 location number range.
14180*/
14181
cc638e86 14182static void
d0fe4701
XR
14183extract_bp_number_and_location (const std::string &arg,
14184 std::pair<int, int> &bp_num_range,
14185 std::pair<int, int> &bp_loc_range)
14186{
14187 std::string::size_type dot = arg.find ('.');
14188
14189 if (dot != std::string::npos)
14190 {
14191 /* Handle 'x.y' and 'x.y-z' cases. */
14192
14193 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14194 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14195
95e95a6d
PA
14196 bp_num_range.first
14197 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14198 bp_num_range.second = bp_num_range.first;
d0fe4701 14199
95e95a6d
PA
14200 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14201 arg, dot + 1);
d0fe4701
XR
14202 }
14203 else
14204 {
14205 /* Handle x and x-y cases. */
d0fe4701 14206
95e95a6d 14207 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14208 bp_loc_range.first = 0;
14209 bp_loc_range.second = 0;
14210 }
d0fe4701
XR
14211}
14212
14213/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14214 specifies whether to enable or disable. */
14215
14216static void
14217enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14218{
14219 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14220 if (loc != NULL)
14221 {
b5fa468f
TBA
14222 if (loc->disabled_by_cond && enable)
14223 error (_("Breakpoint %d's condition is invalid at location %d, "
14224 "cannot enable."), bp_num, loc_num);
14225
d0fe4701
XR
14226 if (loc->enabled != enable)
14227 {
14228 loc->enabled = enable;
14229 mark_breakpoint_location_modified (loc);
14230 }
14231 if (target_supports_enable_disable_tracepoint ()
14232 && current_trace_status ()->running && loc->owner
14233 && is_tracepoint (loc->owner))
14234 target_disable_tracepoint (loc);
14235 }
14236 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14237
14238 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14239}
14240
14241/* Enable or disable a range of breakpoint locations. BP_NUM is the
14242 number of the breakpoint, and BP_LOC_RANGE specifies the
14243 (inclusive) range of location numbers of that breakpoint to
14244 enable/disable. ENABLE specifies whether to enable or disable the
14245 location. */
14246
14247static void
14248enable_disable_breakpoint_location_range (int bp_num,
14249 std::pair<int, int> &bp_loc_range,
14250 bool enable)
14251{
14252 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14253 enable_disable_bp_num_loc (bp_num, i, enable);
14254}
0d381245 14255
1900040c
MS
14256/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14257 If from_tty is nonzero, it prints a message to that effect,
14258 which ends with a period (no newline). */
14259
c906108c 14260void
fba45db2 14261disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14262{
14263 /* Never disable a watchpoint scope breakpoint; we want to
14264 hit them when we leave scope so we can delete both the
14265 watchpoint and its scope breakpoint at that time. */
14266 if (bpt->type == bp_watchpoint_scope)
14267 return;
14268
b5de0fa7 14269 bpt->enable_state = bp_disabled;
c906108c 14270
b775012e
LM
14271 /* Mark breakpoint locations modified. */
14272 mark_breakpoint_modified (bpt);
14273
d248b706
KY
14274 if (target_supports_enable_disable_tracepoint ()
14275 && current_trace_status ()->running && is_tracepoint (bpt))
14276 {
40cb8ca5 14277 for (bp_location *location : bpt->locations ())
d248b706
KY
14278 target_disable_tracepoint (location);
14279 }
14280
44702360 14281 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14282
76727919 14283 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14284}
14285
d0fe4701
XR
14286/* Enable or disable the breakpoint(s) or breakpoint location(s)
14287 specified in ARGS. ARGS may be in any of the formats handled by
14288 extract_bp_number_and_location. ENABLE specifies whether to enable
14289 or disable the breakpoints/locations. */
14290
c906108c 14291static void
d0fe4701 14292enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14293{
c906108c 14294 if (args == 0)
46c6471b 14295 {
43892fdf 14296 for (breakpoint *bpt : all_breakpoints ())
46c6471b 14297 if (user_breakpoint_p (bpt))
d0fe4701
XR
14298 {
14299 if (enable)
14300 enable_breakpoint (bpt);
14301 else
14302 disable_breakpoint (bpt);
14303 }
46c6471b 14304 }
9eaabc75 14305 else
0d381245 14306 {
cb791d59 14307 std::string num = extract_arg (&args);
9eaabc75 14308
cb791d59 14309 while (!num.empty ())
d248b706 14310 {
d0fe4701 14311 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14312
cc638e86
PA
14313 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14314
14315 if (bp_loc_range.first == bp_loc_range.second
14316 && bp_loc_range.first == 0)
d0fe4701 14317 {
cc638e86
PA
14318 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14319 map_breakpoint_number_range (bp_num_range,
14320 enable
14321 ? enable_breakpoint
14322 : disable_breakpoint);
14323 }
14324 else
14325 {
14326 /* Handle breakpoint ids with formats 'x.y' or
14327 'x.y-z'. */
14328 enable_disable_breakpoint_location_range
14329 (bp_num_range.first, bp_loc_range, enable);
b775012e 14330 }
9eaabc75 14331 num = extract_arg (&args);
d248b706 14332 }
0d381245 14333 }
c906108c
SS
14334}
14335
d0fe4701
XR
14336/* The disable command disables the specified breakpoints/locations
14337 (or all defined breakpoints) so they're no longer effective in
14338 stopping the inferior. ARGS may be in any of the forms defined in
14339 extract_bp_number_and_location. */
14340
14341static void
14342disable_command (const char *args, int from_tty)
14343{
14344 enable_disable_command (args, from_tty, false);
14345}
14346
c906108c 14347static void
816338b5
SS
14348enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14349 int count)
c906108c 14350{
afe38095 14351 int target_resources_ok;
c906108c
SS
14352
14353 if (bpt->type == bp_hardware_breakpoint)
14354 {
14355 int i;
c5aa993b 14356 i = hw_breakpoint_used_count ();
53a5351d 14357 target_resources_ok =
d92524f1 14358 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14359 i + 1, 0);
c906108c 14360 if (target_resources_ok == 0)
8a3fe4f8 14361 error (_("No hardware breakpoint support in the target."));
c906108c 14362 else if (target_resources_ok < 0)
8a3fe4f8 14363 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14364 }
14365
cc60f2e3 14366 if (is_watchpoint (bpt))
c906108c 14367 {
d07205c2 14368 /* Initialize it just to avoid a GCC false warning. */
f486487f 14369 enum enable_state orig_enable_state = bp_disabled;
dde02812 14370
a70b8144 14371 try
c906108c 14372 {
3a5c3e22
PA
14373 struct watchpoint *w = (struct watchpoint *) bpt;
14374
1e718ff1
TJB
14375 orig_enable_state = bpt->enable_state;
14376 bpt->enable_state = bp_enabled;
3a5c3e22 14377 update_watchpoint (w, 1 /* reparse */);
c906108c 14378 }
230d2906 14379 catch (const gdb_exception &e)
c5aa993b 14380 {
1e718ff1 14381 bpt->enable_state = orig_enable_state;
dde02812
ES
14382 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14383 bpt->number);
14384 return;
c5aa993b 14385 }
c906108c 14386 }
0101ce28 14387
b775012e
LM
14388 bpt->enable_state = bp_enabled;
14389
14390 /* Mark breakpoint locations modified. */
14391 mark_breakpoint_modified (bpt);
14392
d248b706
KY
14393 if (target_supports_enable_disable_tracepoint ()
14394 && current_trace_status ()->running && is_tracepoint (bpt))
14395 {
40cb8ca5 14396 for (bp_location *location : bpt->locations ())
d248b706
KY
14397 target_enable_tracepoint (location);
14398 }
14399
b4c291bb 14400 bpt->disposition = disposition;
816338b5 14401 bpt->enable_count = count;
44702360 14402 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14403
76727919 14404 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14405}
14406
fe3f5fa8 14407
c906108c 14408void
fba45db2 14409enable_breakpoint (struct breakpoint *bpt)
c906108c 14410{
816338b5 14411 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14412}
14413
d0fe4701
XR
14414/* The enable command enables the specified breakpoints/locations (or
14415 all defined breakpoints) so they once again become (or continue to
14416 be) effective in stopping the inferior. ARGS may be in any of the
14417 forms defined in extract_bp_number_and_location. */
c906108c 14418
c906108c 14419static void
981a3fb3 14420enable_command (const char *args, int from_tty)
c906108c 14421{
d0fe4701 14422 enable_disable_command (args, from_tty, true);
c906108c
SS
14423}
14424
c906108c 14425static void
4495129a 14426enable_once_command (const char *args, int from_tty)
c906108c 14427{
48649e1b
TT
14428 map_breakpoint_numbers
14429 (args, [&] (breakpoint *b)
14430 {
14431 iterate_over_related_breakpoints
14432 (b, [&] (breakpoint *bpt)
14433 {
14434 enable_breakpoint_disp (bpt, disp_disable, 1);
14435 });
14436 });
816338b5
SS
14437}
14438
14439static void
4495129a 14440enable_count_command (const char *args, int from_tty)
816338b5 14441{
b9d61307
SM
14442 int count;
14443
14444 if (args == NULL)
14445 error_no_arg (_("hit count"));
14446
14447 count = get_number (&args);
816338b5 14448
48649e1b
TT
14449 map_breakpoint_numbers
14450 (args, [&] (breakpoint *b)
14451 {
14452 iterate_over_related_breakpoints
14453 (b, [&] (breakpoint *bpt)
14454 {
14455 enable_breakpoint_disp (bpt, disp_disable, count);
14456 });
14457 });
c906108c
SS
14458}
14459
c906108c 14460static void
4495129a 14461enable_delete_command (const char *args, int from_tty)
c906108c 14462{
48649e1b
TT
14463 map_breakpoint_numbers
14464 (args, [&] (breakpoint *b)
14465 {
14466 iterate_over_related_breakpoints
14467 (b, [&] (breakpoint *bpt)
14468 {
14469 enable_breakpoint_disp (bpt, disp_del, 1);
14470 });
14471 });
c906108c
SS
14472}
14473\f
1f3b5d1b
PP
14474/* Invalidate last known value of any hardware watchpoint if
14475 the memory which that value represents has been written to by
14476 GDB itself. */
14477
14478static void
8de0566d
YQ
14479invalidate_bp_value_on_memory_change (struct inferior *inferior,
14480 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14481 const bfd_byte *data)
14482{
43892fdf 14483 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 14484 if (bp->enable_state == bp_enabled
3a5c3e22 14485 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14486 {
3a5c3e22 14487 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14488
850645cf 14489 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 14490 {
40cb8ca5 14491 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
14492 if (loc->loc_type == bp_loc_hardware_watchpoint
14493 && loc->address + loc->length > addr
14494 && addr + len > loc->address)
14495 {
3a5c3e22 14496 wp->val = NULL;
4c1d86d9 14497 wp->val_valid = false;
3a5c3e22
PA
14498 }
14499 }
1f3b5d1b
PP
14500 }
14501}
14502
8181d85f
DJ
14503/* Create and insert a breakpoint for software single step. */
14504
14505void
6c95b8df 14506insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14507 const address_space *aspace,
4a64f543 14508 CORE_ADDR next_pc)
8181d85f 14509{
7c16b83e
PA
14510 struct thread_info *tp = inferior_thread ();
14511 struct symtab_and_line sal;
14512 CORE_ADDR pc = next_pc;
8181d85f 14513
34b7e8a6
PA
14514 if (tp->control.single_step_breakpoints == NULL)
14515 {
14516 tp->control.single_step_breakpoints
5d5658a1 14517 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14518 }
8181d85f 14519
7c16b83e
PA
14520 sal = find_pc_line (pc, 0);
14521 sal.pc = pc;
14522 sal.section = find_pc_overlay (pc);
14523 sal.explicit_pc = 1;
34b7e8a6 14524 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14525
7c16b83e 14526 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14527}
14528
93f9a11f
YQ
14529/* Insert single step breakpoints according to the current state. */
14530
14531int
14532insert_single_step_breakpoints (struct gdbarch *gdbarch)
14533{
f5ea389a 14534 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14535 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14536
f5ea389a 14537 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14538
a0ff9e1a 14539 if (!next_pcs.empty ())
93f9a11f 14540 {
f5ea389a 14541 struct frame_info *frame = get_current_frame ();
8b86c959 14542 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14543
a0ff9e1a 14544 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14545 insert_single_step_breakpoint (gdbarch, aspace, pc);
14546
93f9a11f
YQ
14547 return 1;
14548 }
14549 else
14550 return 0;
14551}
14552
34b7e8a6 14553/* See breakpoint.h. */
f02253f1
HZ
14554
14555int
7c16b83e 14556breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14557 const address_space *aspace,
7c16b83e 14558 CORE_ADDR pc)
1aafd4da 14559{
40cb8ca5 14560 for (bp_location *loc : bp->locations ())
7c16b83e
PA
14561 if (loc->inserted
14562 && breakpoint_location_address_match (loc, aspace, pc))
14563 return 1;
1aafd4da 14564
7c16b83e 14565 return 0;
ef370185
JB
14566}
14567
14568/* Check whether a software single-step breakpoint is inserted at
14569 PC. */
14570
14571int
accd0bcd 14572single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14573 CORE_ADDR pc)
14574{
43892fdf 14575 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
14576 {
14577 if (bpt->type == bp_single_step
14578 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14579 return 1;
14580 }
14581 return 0;
1aafd4da
UW
14582}
14583
1042e4c0
SS
14584/* Tracepoint-specific operations. */
14585
14586/* Set tracepoint count to NUM. */
14587static void
14588set_tracepoint_count (int num)
14589{
14590 tracepoint_count = num;
4fa62494 14591 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14592}
14593
70221824 14594static void
0b39b52e 14595trace_command (const char *arg, int from_tty)
1042e4c0 14596{
ffc2605c
TT
14597 event_location_up location = string_to_event_location (&arg,
14598 current_language);
bac7c5cf
GB
14599 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14600 (location.get (), true /* is_tracepoint */);
55aa24fb 14601
558a9d82 14602 create_breakpoint (get_current_arch (),
ffc2605c 14603 location.get (),
10a636cc 14604 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14605 0 /* tempflag */,
14606 bp_tracepoint /* type_wanted */,
14607 0 /* Ignore count */,
14608 pending_break_support,
14609 ops,
14610 from_tty,
14611 1 /* enabled */,
14612 0 /* internal */, 0);
1042e4c0
SS
14613}
14614
70221824 14615static void
0b39b52e 14616ftrace_command (const char *arg, int from_tty)
7a697b8d 14617{
ffc2605c
TT
14618 event_location_up location = string_to_event_location (&arg,
14619 current_language);
558a9d82 14620 create_breakpoint (get_current_arch (),
ffc2605c 14621 location.get (),
10a636cc 14622 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14623 0 /* tempflag */,
14624 bp_fast_tracepoint /* type_wanted */,
14625 0 /* Ignore count */,
14626 pending_break_support,
14627 &tracepoint_breakpoint_ops,
14628 from_tty,
14629 1 /* enabled */,
14630 0 /* internal */, 0);
0fb4aa4b
PA
14631}
14632
14633/* strace command implementation. Creates a static tracepoint. */
14634
70221824 14635static void
0b39b52e 14636strace_command (const char *arg, int from_tty)
0fb4aa4b 14637{
983af33b 14638 struct breakpoint_ops *ops;
ffc2605c 14639 event_location_up location;
983af33b
SDJ
14640
14641 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14642 or with a normal static tracepoint. */
61012eef 14643 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14644 {
14645 ops = &strace_marker_breakpoint_ops;
a20714ff 14646 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14647 }
983af33b 14648 else
f00aae0f
KS
14649 {
14650 ops = &tracepoint_breakpoint_ops;
14651 location = string_to_event_location (&arg, current_language);
14652 }
983af33b 14653
558a9d82 14654 create_breakpoint (get_current_arch (),
ffc2605c 14655 location.get (),
10a636cc 14656 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14657 0 /* tempflag */,
14658 bp_static_tracepoint /* type_wanted */,
14659 0 /* Ignore count */,
14660 pending_break_support,
14661 ops,
14662 from_tty,
14663 1 /* enabled */,
14664 0 /* internal */, 0);
7a697b8d
SS
14665}
14666
409873ef
SS
14667/* Set up a fake reader function that gets command lines from a linked
14668 list that was acquired during tracepoint uploading. */
14669
14670static struct uploaded_tp *this_utp;
3149d8c1 14671static int next_cmd;
409873ef
SS
14672
14673static char *
14674read_uploaded_action (void)
14675{
a18ba4e4 14676 char *rslt = nullptr;
409873ef 14677
a18ba4e4
SM
14678 if (next_cmd < this_utp->cmd_strings.size ())
14679 {
67aa1f3c 14680 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14681 next_cmd++;
14682 }
409873ef
SS
14683
14684 return rslt;
14685}
14686
00bf0b85
SS
14687/* Given information about a tracepoint as recorded on a target (which
14688 can be either a live system or a trace file), attempt to create an
14689 equivalent GDB tracepoint. This is not a reliable process, since
14690 the target does not necessarily have all the information used when
14691 the tracepoint was originally defined. */
14692
d9b3f62e 14693struct tracepoint *
00bf0b85 14694create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14695{
f2fc3015
TT
14696 const char *addr_str;
14697 char small_buf[100];
d9b3f62e 14698 struct tracepoint *tp;
fd9b8c24 14699
409873ef 14700 if (utp->at_string)
67aa1f3c 14701 addr_str = utp->at_string.get ();
409873ef
SS
14702 else
14703 {
14704 /* In the absence of a source location, fall back to raw
14705 address. Since there is no way to confirm that the address
14706 means the same thing as when the trace was started, warn the
14707 user. */
3e43a32a
MS
14708 warning (_("Uploaded tracepoint %d has no "
14709 "source location, using raw address"),
409873ef 14710 utp->number);
8c042590 14711 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14712 addr_str = small_buf;
14713 }
14714
14715 /* There's not much we can do with a sequence of bytecodes. */
14716 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14717 warning (_("Uploaded tracepoint %d condition "
14718 "has no source form, ignoring it"),
409873ef 14719 utp->number);
d5551862 14720
ffc2605c
TT
14721 event_location_up location = string_to_event_location (&addr_str,
14722 current_language);
8cdf0e15 14723 if (!create_breakpoint (get_current_arch (),
ffc2605c 14724 location.get (),
67aa1f3c 14725 utp->cond_string.get (), -1, addr_str,
10a636cc 14726 false /* force_condition */,
e7e0cddf 14727 0 /* parse cond/thread */,
8cdf0e15 14728 0 /* tempflag */,
0fb4aa4b 14729 utp->type /* type_wanted */,
8cdf0e15
VP
14730 0 /* Ignore count */,
14731 pending_break_support,
348d480f 14732 &tracepoint_breakpoint_ops,
8cdf0e15 14733 0 /* from_tty */,
84f4c1fe 14734 utp->enabled /* enabled */,
44f238bb
PA
14735 0 /* internal */,
14736 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14737 return NULL;
fd9b8c24 14738
409873ef 14739 /* Get the tracepoint we just created. */
fd9b8c24
PA
14740 tp = get_tracepoint (tracepoint_count);
14741 gdb_assert (tp != NULL);
d5551862 14742
00bf0b85
SS
14743 if (utp->pass > 0)
14744 {
8c042590 14745 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14746 tp->number);
00bf0b85 14747
409873ef 14748 trace_pass_command (small_buf, 0);
00bf0b85
SS
14749 }
14750
409873ef
SS
14751 /* If we have uploaded versions of the original commands, set up a
14752 special-purpose "reader" function and call the usual command line
14753 reader, then pass the result to the breakpoint command-setting
14754 function. */
a18ba4e4 14755 if (!utp->cmd_strings.empty ())
00bf0b85 14756 {
12973681 14757 counted_command_line cmd_list;
00bf0b85 14758
409873ef 14759 this_utp = utp;
3149d8c1 14760 next_cmd = 0;
d5551862 14761
60b3cef2 14762 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14763
c1fc2657 14764 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14765 }
a18ba4e4
SM
14766 else if (!utp->actions.empty ()
14767 || !utp->step_actions.empty ())
3e43a32a
MS
14768 warning (_("Uploaded tracepoint %d actions "
14769 "have no source form, ignoring them"),
409873ef 14770 utp->number);
00bf0b85 14771
f196051f 14772 /* Copy any status information that might be available. */
c1fc2657 14773 tp->hit_count = utp->hit_count;
f196051f
SS
14774 tp->traceframe_usage = utp->traceframe_usage;
14775
00bf0b85 14776 return tp;
d9b3f62e 14777}
00bf0b85 14778
1042e4c0
SS
14779/* Print information on tracepoint number TPNUM_EXP, or all if
14780 omitted. */
14781
14782static void
1d12d88f 14783info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14784{
79a45e25 14785 struct ui_out *uiout = current_uiout;
e5a67952 14786 int num_printed;
1042e4c0 14787
5c458ae8 14788 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14789
14790 if (num_printed == 0)
1042e4c0 14791 {
e5a67952 14792 if (args == NULL || *args == '\0')
112e8700 14793 uiout->message ("No tracepoints.\n");
d77f58be 14794 else
112e8700 14795 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14796 }
ad443146
SS
14797
14798 default_collect_info ();
1042e4c0
SS
14799}
14800
4a64f543 14801/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14802 Not supported by all targets. */
14803static void
5fed81ff 14804enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14805{
14806 enable_command (args, from_tty);
14807}
14808
4a64f543 14809/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14810 Not supported by all targets. */
14811static void
5fed81ff 14812disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14813{
14814 disable_command (args, from_tty);
14815}
14816
4a64f543 14817/* Remove a tracepoint (or all if no argument). */
1042e4c0 14818static void
4495129a 14819delete_trace_command (const char *arg, int from_tty)
1042e4c0 14820{
1042e4c0
SS
14821 dont_repeat ();
14822
14823 if (arg == 0)
14824 {
14825 int breaks_to_delete = 0;
14826
14827 /* Delete all breakpoints if no argument.
dda83cd7
SM
14828 Do not delete internal or call-dummy breakpoints, these
14829 have to be deleted with an explicit breakpoint number
4a64f543 14830 argument. */
f6d17b2b 14831 for (breakpoint *tp : all_tracepoints ())
1428b37a 14832 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
14833 {
14834 breaks_to_delete = 1;
14835 break;
14836 }
1042e4c0
SS
14837
14838 /* Ask user only if there are some breakpoints to delete. */
14839 if (!from_tty
14840 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14841 {
1428b37a 14842 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14843 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14844 delete_breakpoint (b);
1042e4c0
SS
14845 }
14846 }
14847 else
48649e1b 14848 map_breakpoint_numbers
b926417a 14849 (arg, [&] (breakpoint *br)
48649e1b 14850 {
b926417a 14851 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14852 });
1042e4c0
SS
14853}
14854
197f0a60
TT
14855/* Helper function for trace_pass_command. */
14856
14857static void
d9b3f62e 14858trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14859{
d9b3f62e 14860 tp->pass_count = count;
76727919 14861 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14862 if (from_tty)
14863 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14864 tp->number, count);
197f0a60
TT
14865}
14866
1042e4c0
SS
14867/* Set passcount for tracepoint.
14868
14869 First command argument is passcount, second is tracepoint number.
14870 If tracepoint number omitted, apply to most recently defined.
14871 Also accepts special argument "all". */
14872
14873static void
0b39b52e 14874trace_pass_command (const char *args, int from_tty)
1042e4c0 14875{
d9b3f62e 14876 struct tracepoint *t1;
0b39b52e 14877 ULONGEST count;
1042e4c0
SS
14878
14879 if (args == 0 || *args == 0)
3e43a32a
MS
14880 error (_("passcount command requires an "
14881 "argument (count + optional TP num)"));
1042e4c0 14882
0b39b52e 14883 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14884
529480d0 14885 args = skip_spaces (args);
1042e4c0
SS
14886 if (*args && strncasecmp (args, "all", 3) == 0)
14887 {
14888 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14889 if (*args)
14890 error (_("Junk at end of arguments."));
1042e4c0 14891
f6d17b2b 14892 for (breakpoint *b : all_tracepoints ())
01add95b
SM
14893 {
14894 t1 = (struct tracepoint *) b;
14895 trace_pass_set_count (t1, count, from_tty);
14896 }
197f0a60
TT
14897 }
14898 else if (*args == '\0')
1042e4c0 14899 {
5fa1d40e 14900 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14901 if (t1)
197f0a60
TT
14902 trace_pass_set_count (t1, count, from_tty);
14903 }
14904 else
14905 {
bfd28288
PA
14906 number_or_range_parser parser (args);
14907 while (!parser.finished ())
1042e4c0 14908 {
bfd28288 14909 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14910 if (t1)
14911 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14912 }
14913 }
1042e4c0
SS
14914}
14915
d9b3f62e 14916struct tracepoint *
1042e4c0
SS
14917get_tracepoint (int num)
14918{
f6d17b2b 14919 for (breakpoint *t : all_tracepoints ())
1042e4c0 14920 if (t->number == num)
d9b3f62e 14921 return (struct tracepoint *) t;
1042e4c0
SS
14922
14923 return NULL;
14924}
14925
d5551862
SS
14926/* Find the tracepoint with the given target-side number (which may be
14927 different from the tracepoint number after disconnecting and
14928 reconnecting). */
14929
d9b3f62e 14930struct tracepoint *
d5551862
SS
14931get_tracepoint_by_number_on_target (int num)
14932{
f6d17b2b 14933 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14934 {
14935 struct tracepoint *t = (struct tracepoint *) b;
14936
14937 if (t->number_on_target == num)
14938 return t;
14939 }
d5551862
SS
14940
14941 return NULL;
14942}
14943
1042e4c0 14944/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14945 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14946 If the argument is missing, the most recent tracepoint
14947 (tracepoint_count) is returned. */
14948
d9b3f62e 14949struct tracepoint *
0b39b52e 14950get_tracepoint_by_number (const char **arg,
bfd28288 14951 number_or_range_parser *parser)
1042e4c0 14952{
1042e4c0 14953 int tpnum;
0b39b52e 14954 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14955
bfd28288 14956 if (parser != NULL)
197f0a60 14957 {
bfd28288
PA
14958 gdb_assert (!parser->finished ());
14959 tpnum = parser->get_number ();
197f0a60
TT
14960 }
14961 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14962 tpnum = tracepoint_count;
1042e4c0 14963 else
197f0a60 14964 tpnum = get_number (arg);
1042e4c0
SS
14965
14966 if (tpnum <= 0)
14967 {
14968 if (instring && *instring)
14969 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14970 instring);
14971 else
5fa1d40e 14972 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14973 return NULL;
14974 }
14975
f6d17b2b 14976 for (breakpoint *t : all_tracepoints ())
1042e4c0 14977 if (t->number == tpnum)
d9b3f62e 14978 return (struct tracepoint *) t;
1042e4c0 14979
1042e4c0
SS
14980 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14981 return NULL;
14982}
14983
d9b3f62e
PA
14984void
14985print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14986{
14987 if (b->thread != -1)
14988 fprintf_unfiltered (fp, " thread %d", b->thread);
14989
14990 if (b->task != 0)
14991 fprintf_unfiltered (fp, " task %d", b->task);
14992
14993 fprintf_unfiltered (fp, "\n");
14994}
14995
6149aea9
PA
14996/* Save information on user settable breakpoints (watchpoints, etc) to
14997 a new script file named FILENAME. If FILTER is non-NULL, call it
14998 on each breakpoint and only include the ones for which it returns
f2478a7e 14999 true. */
6149aea9 15000
1042e4c0 15001static void
4495129a 15002save_breakpoints (const char *filename, int from_tty,
f2478a7e 15003 bool (*filter) (const struct breakpoint *))
1042e4c0 15004{
6149aea9 15005 int any = 0;
6149aea9 15006 int extra_trace_bits = 0;
1042e4c0 15007
6149aea9
PA
15008 if (filename == 0 || *filename == 0)
15009 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15010
15011 /* See if we have anything to save. */
43892fdf 15012 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15013 {
15014 /* Skip internal and momentary breakpoints. */
15015 if (!user_breakpoint_p (tp))
15016 continue;
6149aea9 15017
01add95b
SM
15018 /* If we have a filter, only save the breakpoints it accepts. */
15019 if (filter && !filter (tp))
15020 continue;
6149aea9 15021
01add95b 15022 any = 1;
6149aea9 15023
01add95b
SM
15024 if (is_tracepoint (tp))
15025 {
15026 extra_trace_bits = 1;
6149aea9 15027
01add95b
SM
15028 /* We can stop searching. */
15029 break;
15030 }
15031 }
6149aea9
PA
15032
15033 if (!any)
1042e4c0 15034 {
6149aea9 15035 warning (_("Nothing to save."));
1042e4c0
SS
15036 return;
15037 }
15038
ee0c3293 15039 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15040
15041 stdio_file fp;
15042
ee0c3293 15043 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15044 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15045 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15046
6149aea9 15047 if (extra_trace_bits)
d7e74731 15048 save_trace_state_variables (&fp);
8bf6485c 15049
43892fdf 15050 for (breakpoint *tp : all_breakpoints ())
01add95b
SM
15051 {
15052 /* Skip internal and momentary breakpoints. */
15053 if (!user_breakpoint_p (tp))
15054 continue;
8bf6485c 15055
01add95b
SM
15056 /* If we have a filter, only save the breakpoints it accepts. */
15057 if (filter && !filter (tp))
15058 continue;
6149aea9 15059
01add95b 15060 tp->ops->print_recreate (tp, &fp);
1042e4c0 15061
01add95b
SM
15062 /* Note, we can't rely on tp->number for anything, as we can't
15063 assume the recreated breakpoint numbers will match. Use $bpnum
15064 instead. */
6149aea9 15065
01add95b
SM
15066 if (tp->cond_string)
15067 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9 15068
01add95b
SM
15069 if (tp->ignore_count)
15070 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15071
01add95b
SM
15072 if (tp->type != bp_dprintf && tp->commands)
15073 {
15074 fp.puts (" commands\n");
1042e4c0 15075
01add95b
SM
15076 current_uiout->redirect (&fp);
15077 try
15078 {
15079 print_command_lines (current_uiout, tp->commands.get (), 2);
15080 }
15081 catch (const gdb_exception &ex)
15082 {
15083 current_uiout->redirect (NULL);
15084 throw;
15085 }
6149aea9 15086
01add95b
SM
15087 current_uiout->redirect (NULL);
15088 fp.puts (" end\n");
15089 }
6149aea9 15090
01add95b
SM
15091 if (tp->enable_state == bp_disabled)
15092 fp.puts ("disable $bpnum\n");
6149aea9 15093
01add95b
SM
15094 /* If this is a multi-location breakpoint, check if the locations
15095 should be individually disabled. Watchpoint locations are
15096 special, and not user visible. */
15097 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15098 {
15099 int n = 1;
40cb8ca5 15100
01add95b
SM
15101 for (bp_location *loc : tp->locations ())
15102 {
15103 if (!loc->enabled)
15104 fp.printf ("disable $bpnum.%d\n", n);
15105
15106 n++;
15107 }
15108 }
15109 }
8bf6485c 15110
6149aea9 15111 if (extra_trace_bits && *default_collect)
d7e74731 15112 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15113
1042e4c0 15114 if (from_tty)
ee0c3293 15115 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15116}
15117
15118/* The `save breakpoints' command. */
15119
15120static void
4495129a 15121save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15122{
15123 save_breakpoints (args, from_tty, NULL);
15124}
15125
15126/* The `save tracepoints' command. */
15127
15128static void
4495129a 15129save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15130{
15131 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15132}
15133
c906108c 15134\f
629500fa
KS
15135/* This help string is used to consolidate all the help string for specifying
15136 locations used by several commands. */
15137
15138#define LOCATION_HELP_STRING \
15139"Linespecs are colon-separated lists of location parameters, such as\n\
15140source filename, function name, label name, and line number.\n\
15141Example: To specify the start of a label named \"the_top\" in the\n\
15142function \"fact\" in the file \"factorial.c\", use\n\
15143\"factorial.c:fact:the_top\".\n\
15144\n\
15145Address locations begin with \"*\" and specify an exact address in the\n\
15146program. Example: To specify the fourth byte past the start function\n\
15147\"main\", use \"*main + 4\".\n\
15148\n\
15149Explicit locations are similar to linespecs but use an option/argument\n\
15150syntax to specify location parameters.\n\
15151Example: To specify the start of the label named \"the_top\" in the\n\
15152function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15153-function fact -label the_top\".\n\
15154\n\
15155By default, a specified function is matched against the program's\n\
15156functions in all scopes. For C++, this means in all namespaces and\n\
15157classes. For Ada, this means in all packages. E.g., in C++,\n\
15158\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15159\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15160specified name as a complete fully-qualified name instead."
629500fa 15161
4a64f543
MS
15162/* This help string is used for the break, hbreak, tbreak and thbreak
15163 commands. It is defined as a macro to prevent duplication.
15164 COMMAND should be a string constant containing the name of the
15165 command. */
629500fa 15166
31e2b00f 15167#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15168command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15169\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15170PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15171probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15172guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15173`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15174LOCATION may be a linespec, address, or explicit location as described\n\
15175below.\n\
15176\n\
dc10affe
PA
15177With no LOCATION, uses current execution address of the selected\n\
15178stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15179\n\
15180THREADNUM is the number from \"info threads\".\n\
15181CONDITION is a boolean expression.\n\
733d554a
TBA
15182\n\
15183With the \"-force-condition\" flag, the condition is defined even when\n\
15184it is invalid for all current locations.\n\
89549d7f 15185\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15186Multiple breakpoints at one place are permitted, and useful if their\n\
15187conditions are different.\n\
31e2b00f
AS
15188\n\
15189Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15190
44feb3ce
TT
15191/* List of subcommands for "catch". */
15192static struct cmd_list_element *catch_cmdlist;
15193
15194/* List of subcommands for "tcatch". */
15195static struct cmd_list_element *tcatch_cmdlist;
15196
9ac4176b 15197void
a121b7c1 15198add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15199 cmd_const_sfunc_ftype *sfunc,
625e8578 15200 completer_ftype *completer,
44feb3ce
TT
15201 void *user_data_catch,
15202 void *user_data_tcatch)
15203{
15204 struct cmd_list_element *command;
15205
0450cc4c 15206 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15207 &catch_cmdlist);
15208 set_cmd_sfunc (command, sfunc);
15209 set_cmd_context (command, user_data_catch);
a96d9b2e 15210 set_cmd_completer (command, completer);
44feb3ce 15211
0450cc4c 15212 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15213 &tcatch_cmdlist);
15214 set_cmd_sfunc (command, sfunc);
15215 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15216 set_cmd_completer (command, completer);
44feb3ce
TT
15217}
15218
0574c78f
GB
15219/* Zero if any of the breakpoint's locations could be a location where
15220 functions have been inlined, nonzero otherwise. */
15221
15222static int
15223is_non_inline_function (struct breakpoint *b)
15224{
15225 /* The shared library event breakpoint is set on the address of a
15226 non-inline function. */
15227 if (b->type == bp_shlib_event)
15228 return 1;
15229
15230 return 0;
15231}
15232
15233/* Nonzero if the specified PC cannot be a location where functions
15234 have been inlined. */
15235
15236int
accd0bcd 15237pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15238 const struct target_waitstatus *ws)
0574c78f 15239{
43892fdf 15240 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
15241 {
15242 if (!is_non_inline_function (b))
15243 continue;
15244
40cb8ca5 15245 for (bp_location *bl : b->locations ())
0574c78f
GB
15246 {
15247 if (!bl->shlib_disabled
09ac7c10 15248 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15249 return 1;
15250 }
15251 }
15252
15253 return 0;
15254}
15255
2f202fde
JK
15256/* Remove any references to OBJFILE which is going to be freed. */
15257
15258void
15259breakpoint_free_objfile (struct objfile *objfile)
15260{
48d7020b 15261 for (bp_location *loc : all_bp_locations ())
eb822aa6 15262 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15263 loc->symtab = NULL;
15264}
15265
2060206e
PA
15266void
15267initialize_breakpoint_ops (void)
15268{
15269 static int initialized = 0;
15270
15271 struct breakpoint_ops *ops;
15272
15273 if (initialized)
15274 return;
15275 initialized = 1;
15276
15277 /* The breakpoint_ops structure to be inherit by all kinds of
15278 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15279 internal and momentary breakpoints, etc.). */
15280 ops = &bkpt_base_breakpoint_ops;
15281 *ops = base_breakpoint_ops;
15282 ops->re_set = bkpt_re_set;
15283 ops->insert_location = bkpt_insert_location;
15284 ops->remove_location = bkpt_remove_location;
15285 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15286 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15287 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15288 ops->decode_location = bkpt_decode_location;
2060206e
PA
15289
15290 /* The breakpoint_ops structure to be used in regular breakpoints. */
15291 ops = &bkpt_breakpoint_ops;
15292 *ops = bkpt_base_breakpoint_ops;
15293 ops->re_set = bkpt_re_set;
15294 ops->resources_needed = bkpt_resources_needed;
15295 ops->print_it = bkpt_print_it;
15296 ops->print_mention = bkpt_print_mention;
15297 ops->print_recreate = bkpt_print_recreate;
15298
15299 /* Ranged breakpoints. */
15300 ops = &ranged_breakpoint_ops;
15301 *ops = bkpt_breakpoint_ops;
15302 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15303 ops->resources_needed = resources_needed_ranged_breakpoint;
15304 ops->print_it = print_it_ranged_breakpoint;
15305 ops->print_one = print_one_ranged_breakpoint;
15306 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15307 ops->print_mention = print_mention_ranged_breakpoint;
15308 ops->print_recreate = print_recreate_ranged_breakpoint;
15309
15310 /* Internal breakpoints. */
15311 ops = &internal_breakpoint_ops;
15312 *ops = bkpt_base_breakpoint_ops;
15313 ops->re_set = internal_bkpt_re_set;
15314 ops->check_status = internal_bkpt_check_status;
15315 ops->print_it = internal_bkpt_print_it;
15316 ops->print_mention = internal_bkpt_print_mention;
15317
15318 /* Momentary breakpoints. */
15319 ops = &momentary_breakpoint_ops;
15320 *ops = bkpt_base_breakpoint_ops;
15321 ops->re_set = momentary_bkpt_re_set;
15322 ops->check_status = momentary_bkpt_check_status;
15323 ops->print_it = momentary_bkpt_print_it;
15324 ops->print_mention = momentary_bkpt_print_mention;
15325
55aa24fb
SDJ
15326 /* Probe breakpoints. */
15327 ops = &bkpt_probe_breakpoint_ops;
15328 *ops = bkpt_breakpoint_ops;
15329 ops->insert_location = bkpt_probe_insert_location;
15330 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15331 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15332 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15333
2060206e
PA
15334 /* Watchpoints. */
15335 ops = &watchpoint_breakpoint_ops;
15336 *ops = base_breakpoint_ops;
15337 ops->re_set = re_set_watchpoint;
15338 ops->insert_location = insert_watchpoint;
15339 ops->remove_location = remove_watchpoint;
15340 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15341 ops->check_status = check_status_watchpoint;
15342 ops->resources_needed = resources_needed_watchpoint;
15343 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15344 ops->print_it = print_it_watchpoint;
15345 ops->print_mention = print_mention_watchpoint;
15346 ops->print_recreate = print_recreate_watchpoint;
427cd150 15347 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15348
15349 /* Masked watchpoints. */
15350 ops = &masked_watchpoint_breakpoint_ops;
15351 *ops = watchpoint_breakpoint_ops;
15352 ops->insert_location = insert_masked_watchpoint;
15353 ops->remove_location = remove_masked_watchpoint;
15354 ops->resources_needed = resources_needed_masked_watchpoint;
15355 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15356 ops->print_it = print_it_masked_watchpoint;
15357 ops->print_one_detail = print_one_detail_masked_watchpoint;
15358 ops->print_mention = print_mention_masked_watchpoint;
15359 ops->print_recreate = print_recreate_masked_watchpoint;
15360
15361 /* Tracepoints. */
15362 ops = &tracepoint_breakpoint_ops;
15363 *ops = base_breakpoint_ops;
15364 ops->re_set = tracepoint_re_set;
15365 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15366 ops->print_one_detail = tracepoint_print_one_detail;
15367 ops->print_mention = tracepoint_print_mention;
15368 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15369 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15370 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15371 ops->decode_location = tracepoint_decode_location;
983af33b 15372
55aa24fb
SDJ
15373 /* Probe tracepoints. */
15374 ops = &tracepoint_probe_breakpoint_ops;
15375 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15376 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15377 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15378
983af33b
SDJ
15379 /* Static tracepoints with marker (`-m'). */
15380 ops = &strace_marker_breakpoint_ops;
15381 *ops = tracepoint_breakpoint_ops;
5f700d83 15382 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15383 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15384 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15385
15386 /* Fork catchpoints. */
15387 ops = &catch_fork_breakpoint_ops;
15388 *ops = base_breakpoint_ops;
15389 ops->insert_location = insert_catch_fork;
15390 ops->remove_location = remove_catch_fork;
15391 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15392 ops->print_it = print_it_catch_fork;
15393 ops->print_one = print_one_catch_fork;
15394 ops->print_mention = print_mention_catch_fork;
15395 ops->print_recreate = print_recreate_catch_fork;
15396
15397 /* Vfork catchpoints. */
15398 ops = &catch_vfork_breakpoint_ops;
15399 *ops = base_breakpoint_ops;
15400 ops->insert_location = insert_catch_vfork;
15401 ops->remove_location = remove_catch_vfork;
15402 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15403 ops->print_it = print_it_catch_vfork;
15404 ops->print_one = print_one_catch_vfork;
15405 ops->print_mention = print_mention_catch_vfork;
15406 ops->print_recreate = print_recreate_catch_vfork;
15407
15408 /* Exec catchpoints. */
15409 ops = &catch_exec_breakpoint_ops;
15410 *ops = base_breakpoint_ops;
2060206e
PA
15411 ops->insert_location = insert_catch_exec;
15412 ops->remove_location = remove_catch_exec;
15413 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15414 ops->print_it = print_it_catch_exec;
15415 ops->print_one = print_one_catch_exec;
15416 ops->print_mention = print_mention_catch_exec;
15417 ops->print_recreate = print_recreate_catch_exec;
15418
edcc5120
TT
15419 /* Solib-related catchpoints. */
15420 ops = &catch_solib_breakpoint_ops;
15421 *ops = base_breakpoint_ops;
edcc5120
TT
15422 ops->insert_location = insert_catch_solib;
15423 ops->remove_location = remove_catch_solib;
15424 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15425 ops->check_status = check_status_catch_solib;
15426 ops->print_it = print_it_catch_solib;
15427 ops->print_one = print_one_catch_solib;
15428 ops->print_mention = print_mention_catch_solib;
15429 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15430
15431 ops = &dprintf_breakpoint_ops;
15432 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15433 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15434 ops->resources_needed = bkpt_resources_needed;
15435 ops->print_it = bkpt_print_it;
15436 ops->print_mention = bkpt_print_mention;
2d9442cc 15437 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15438 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15439 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15440}
15441
8bfd80db
YQ
15442/* Chain containing all defined "enable breakpoint" subcommands. */
15443
15444static struct cmd_list_element *enablebreaklist = NULL;
15445
8588b356
SM
15446/* See breakpoint.h. */
15447
15448cmd_list_element *commands_cmd_element = nullptr;
15449
6c265988 15450void _initialize_breakpoint ();
c906108c 15451void
6c265988 15452_initialize_breakpoint ()
c906108c
SS
15453{
15454 struct cmd_list_element *c;
15455
2060206e
PA
15456 initialize_breakpoint_ops ();
15457
c90e7d63
SM
15458 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15459 "breakpoint");
15460 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15461 "breakpoint");
15462 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15463 "breakpoint");
84acb35a 15464
c906108c
SS
15465 breakpoint_chain = 0;
15466 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15467 before a breakpoint is set. */
15468 breakpoint_count = 0;
15469
1042e4c0
SS
15470 tracepoint_count = 0;
15471
1bedd215
AC
15472 add_com ("ignore", class_breakpoint, ignore_command, _("\
15473Set ignore-count of breakpoint number N to COUNT.\n\
15474Usage is `ignore N COUNT'."));
c906108c 15475
8588b356
SM
15476 commands_cmd_element = add_com ("commands", class_breakpoint,
15477 commands_command, _("\
18da0c51
MG
15478Set commands to be executed when the given breakpoints are hit.\n\
15479Give a space-separated breakpoint list as argument after \"commands\".\n\
15480A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15481(e.g. `5-7').\n\
c906108c
SS
15482With no argument, the targeted breakpoint is the last one set.\n\
15483The commands themselves follow starting on the next line.\n\
15484Type a line containing \"end\" to indicate the end of them.\n\
15485Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15486then no output is printed when it is hit, except what the commands print."));
c906108c 15487
b1d4d8d1
TBA
15488 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15489 static std::string condition_command_help
15490 = gdb::option::build_help (_("\
1bedd215 15491Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15492Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15493is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15494\n\
15495Options:\n\
15496%OPTIONS%"), cc_opts);
15497
15498 c = add_com ("condition", class_breakpoint, condition_command,
15499 condition_command_help.c_str ());
15500 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15501
1bedd215 15502 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15503Set a temporary breakpoint.\n\
c906108c
SS
15504Like \"break\" except the breakpoint is only temporary,\n\
15505so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15506by using \"enable delete\" on the breakpoint number.\n\
15507\n"
15508BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15509 set_cmd_completer (c, location_completer);
c94fdfd0 15510
1bedd215 15511 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15512Set a hardware assisted breakpoint.\n\
c906108c 15513Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15514some target hardware may not have this support.\n\
15515\n"
15516BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15517 set_cmd_completer (c, location_completer);
c906108c 15518
1bedd215 15519 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15520Set a temporary hardware assisted breakpoint.\n\
c906108c 15521Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15522so it will be deleted when hit.\n\
15523\n"
15524BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15525 set_cmd_completer (c, location_completer);
c906108c 15526
3947f654
SM
15527 cmd_list_element *enable_cmd
15528 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15529Enable all or some breakpoints.\n\
15530Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15531Give breakpoint numbers (separated by spaces) as arguments.\n\
15532With no subcommand, breakpoints are enabled until you command otherwise.\n\
15533This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15534With a subcommand you can enable temporarily."),
3947f654 15535 &enablelist, 1, &cmdlist);
c906108c 15536
3947f654 15537 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 15538
84951ab5 15539 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15540Enable all or some breakpoints.\n\
15541Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15542Give breakpoint numbers (separated by spaces) as arguments.\n\
15543This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15544May be abbreviated to simply \"enable\"."),
2f822da5 15545 &enablebreaklist, 1, &enablelist);
c906108c 15546
1a966eab 15547 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15548Enable some breakpoints for one hit.\n\
15549Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15550If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15551 &enablebreaklist);
15552
1a966eab 15553 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15554Enable some breakpoints and delete when hit.\n\
15555Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15556If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15557 &enablebreaklist);
15558
816338b5 15559 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15560Enable some breakpoints for COUNT hits.\n\
15561Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15562If a breakpoint is hit while enabled in this fashion,\n\
15563the count is decremented; when it reaches zero, the breakpoint is disabled."),
15564 &enablebreaklist);
15565
1a966eab 15566 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15567Enable some breakpoints and delete when hit.\n\
15568Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15569If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15570 &enablelist);
15571
1a966eab 15572 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15573Enable some breakpoints for one hit.\n\
15574Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15575If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15576 &enablelist);
15577
15578 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15579Enable some breakpoints for COUNT hits.\n\
15580Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15581If a breakpoint is hit while enabled in this fashion,\n\
15582the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15583 &enablelist);
15584
3947f654
SM
15585 cmd_list_element *disable_cmd
15586 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15587Disable all or some breakpoints.\n\
15588Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15589Arguments are breakpoint numbers with spaces in between.\n\
15590To disable all breakpoints, give no argument.\n\
64b9b334 15591A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
15592 &disablelist, 1, &cmdlist);
15593 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15594 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 15595
57b4f16e 15596 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15597Disable all or some breakpoints.\n\
15598Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15599Arguments are breakpoint numbers with spaces in between.\n\
15600To disable all breakpoints, give no argument.\n\
64b9b334 15601A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15602This command may be abbreviated \"disable\"."),
c906108c
SS
15603 &disablelist);
15604
3947f654
SM
15605 cmd_list_element *delete_cmd
15606 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15607Delete all or some breakpoints.\n\
15608Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15609Arguments are breakpoint numbers with spaces in between.\n\
15610To delete all breakpoints, give no argument.\n\
15611\n\
590042fc 15612Also a prefix command for deletion of other GDB objects."),
3947f654
SM
15613 &deletelist, 1, &cmdlist);
15614 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15615 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 15616
57b4f16e 15617 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15618Delete all or some breakpoints or auto-display expressions.\n\
15619Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15620Arguments are breakpoint numbers with spaces in between.\n\
15621To delete all breakpoints, give no argument.\n\
1a966eab 15622This command may be abbreviated \"delete\"."),
c906108c
SS
15623 &deletelist);
15624
3947f654
SM
15625 cmd_list_element *clear_cmd
15626 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15627Clear breakpoint at specified location.\n\
15628Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15629\n\
15630With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15631is executing in.\n"
89549d7f 15632"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15633See also the \"delete\" command which clears breakpoints by number."));
3947f654 15634 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 15635
3947f654
SM
15636 cmd_list_element *break_cmd
15637 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15638Set breakpoint at specified location.\n"
31e2b00f 15639BREAK_ARGS_HELP ("break")));
3947f654 15640 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 15641
3947f654
SM
15642 add_com_alias ("b", break_cmd, class_run, 1);
15643 add_com_alias ("br", break_cmd, class_run, 1);
15644 add_com_alias ("bre", break_cmd, class_run, 1);
15645 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 15646
c906108c
SS
15647 if (dbx_commands)
15648 {
1bedd215
AC
15649 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15650Break in function/address or break at a line in the current file."),
2f822da5 15651 &stoplist, 1, &cmdlist);
c5aa993b 15652 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15653 _("Break in function or address."), &stoplist);
c5aa993b 15654 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15655 _("Break at a line in the current file."), &stoplist);
11db9430 15656 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15657Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15658The \"Type\" column indicates one of:\n\
15659\tbreakpoint - normal breakpoint\n\
15660\twatchpoint - watchpoint\n\
15661The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15662the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15663breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15664address and file/line number respectively.\n\
15665\n\
15666Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15667are set to the address of the last breakpoint listed unless the command\n\
15668is prefixed with \"server \".\n\n\
c906108c 15669Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15670breakpoint set."));
c906108c
SS
15671 }
15672
e0f25bd9
SM
15673 cmd_list_element *info_breakpoints_cmd
15674 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15675Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15676The \"Type\" column indicates one of:\n\
15677\tbreakpoint - normal breakpoint\n\
15678\twatchpoint - watchpoint\n\
15679The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15680the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15681breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15682address and file/line number respectively.\n\
15683\n\
15684Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15685are set to the address of the last breakpoint listed unless the command\n\
15686is prefixed with \"server \".\n\n\
c906108c 15687Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15688breakpoint set."));
c906108c 15689
e0f25bd9 15690 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 15691
1a966eab
AC
15692 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15693Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15694The \"Type\" column indicates one of:\n\
15695\tbreakpoint - normal breakpoint\n\
15696\twatchpoint - watchpoint\n\
15697\tlongjmp - internal breakpoint used to step through longjmp()\n\
15698\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15699\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15700\tfinish - internal breakpoint used by the \"finish\" command\n\
15701The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15702the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15703breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15704address and file/line number respectively.\n\
15705\n\
15706Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15707are set to the address of the last breakpoint listed unless the command\n\
15708is prefixed with \"server \".\n\n\
c906108c 15709Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15710breakpoint set."),
c906108c
SS
15711 &maintenanceinfolist);
15712
3b6acaee 15713 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15714Set catchpoints to catch events."),
2f822da5 15715 &catch_cmdlist,
3b6acaee 15716 0/*allow-unknown*/, &cmdlist);
44feb3ce 15717
3b6acaee 15718 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15719Set temporary catchpoints to catch events."),
2f822da5 15720 &tcatch_cmdlist,
3b6acaee 15721 0/*allow-unknown*/, &cmdlist);
44feb3ce 15722
44feb3ce
TT
15723 add_catch_command ("fork", _("Catch calls to fork."),
15724 catch_fork_command_1,
dda83cd7 15725 NULL,
44feb3ce
TT
15726 (void *) (uintptr_t) catch_fork_permanent,
15727 (void *) (uintptr_t) catch_fork_temporary);
15728 add_catch_command ("vfork", _("Catch calls to vfork."),
15729 catch_fork_command_1,
dda83cd7 15730 NULL,
44feb3ce
TT
15731 (void *) (uintptr_t) catch_vfork_permanent,
15732 (void *) (uintptr_t) catch_vfork_temporary);
15733 add_catch_command ("exec", _("Catch calls to exec."),
15734 catch_exec_command_1,
dda83cd7 15735 NULL,
a96d9b2e
SDJ
15736 CATCH_PERMANENT,
15737 CATCH_TEMPORARY);
edcc5120
TT
15738 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15739Usage: catch load [REGEX]\n\
15740If REGEX is given, only stop for libraries matching the regular expression."),
15741 catch_load_command_1,
15742 NULL,
15743 CATCH_PERMANENT,
15744 CATCH_TEMPORARY);
15745 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15746Usage: catch unload [REGEX]\n\
15747If REGEX is given, only stop for libraries matching the regular expression."),
15748 catch_unload_command_1,
15749 NULL,
15750 CATCH_PERMANENT,
15751 CATCH_TEMPORARY);
c5aa993b 15752
a15a5258
AB
15753 const auto opts = make_watch_options_def_group (nullptr);
15754
15755 static const std::string watch_help = gdb::option::build_help (_("\
15756Set a watchpoint for EXPRESSION.\n\
15757Usage: watch [-location] EXPRESSION\n\
15758\n\
15759Options:\n\
15760%OPTIONS%\n\
15761\n\
c906108c 15762A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15763an expression changes."), opts);
15764 c = add_com ("watch", class_breakpoint, watch_command,
15765 watch_help.c_str ());
15766 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15767
15768 static const std::string rwatch_help = gdb::option::build_help (_("\
15769Set a read watchpoint for EXPRESSION.\n\
15770Usage: rwatch [-location] EXPRESSION\n\
15771\n\
15772Options:\n\
15773%OPTIONS%\n\
15774\n\
15775A read watchpoint stops execution of your program whenever the value of\n\
15776an expression is read."), opts);
15777 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15778 rwatch_help.c_str ());
15779 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15780
15781 static const std::string awatch_help = gdb::option::build_help (_("\
15782Set an access watchpoint for EXPRESSION.\n\
15783Usage: awatch [-location] EXPRESSION\n\
15784\n\
15785Options:\n\
15786%OPTIONS%\n\
15787\n\
15788An access watchpoint stops execution of your program whenever the value\n\
15789of an expression is either read or written."), opts);
15790 c = add_com ("awatch", class_breakpoint, awatch_command,
15791 awatch_help.c_str ());
15792 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15793
11db9430 15794 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15795Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15796
920d2a44
AC
15797 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15798 respond to changes - contrary to the description. */
85c07804
AC
15799 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15800 &can_use_hw_watchpoints, _("\
15801Set debugger's willingness to use watchpoint hardware."), _("\
15802Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15803If zero, gdb will not use hardware for new watchpoints, even if\n\
15804such is available. (However, any hardware watchpoints that were\n\
15805created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15806hardware.)"),
15807 NULL,
920d2a44 15808 show_can_use_hw_watchpoints,
85c07804 15809 &setlist, &showlist);
c906108c
SS
15810
15811 can_use_hw_watchpoints = 1;
fa8d40ab 15812
1042e4c0
SS
15813 /* Tracepoint manipulation commands. */
15814
3947f654
SM
15815 cmd_list_element *trace_cmd
15816 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15817Set a tracepoint at specified location.\n\
1042e4c0
SS
15818\n"
15819BREAK_ARGS_HELP ("trace") "\n\
15820Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 15821 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 15822
3947f654
SM
15823 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15824 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15825 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15826 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 15827
7a697b8d 15828 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15829Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15830\n"
15831BREAK_ARGS_HELP ("ftrace") "\n\
15832Do \"help tracepoints\" for info on other tracepoint commands."));
15833 set_cmd_completer (c, location_completer);
15834
0fb4aa4b 15835 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15836Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15837\n\
15838strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15839LOCATION may be a linespec, explicit, or address location (described below) \n\
15840or -m MARKER_ID.\n\n\
15841If a marker id is specified, probe the marker with that name. With\n\
15842no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15843Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15844This collects arbitrary user data passed in the probe point call to the\n\
15845tracing library. You can inspect it when analyzing the trace buffer,\n\
15846by printing the $_sdata variable like any other convenience variable.\n\
15847\n\
15848CONDITION is a boolean expression.\n\
89549d7f 15849\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15850Multiple tracepoints at one place are permitted, and useful if their\n\
15851conditions are different.\n\
0fb4aa4b
PA
15852\n\
15853Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15854Do \"help tracepoints\" for info on other tracepoint commands."));
15855 set_cmd_completer (c, location_completer);
15856
e0f25bd9
SM
15857 cmd_list_element *info_tracepoints_cmd
15858 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15859Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15860Convenience variable \"$tpnum\" contains the number of the\n\
15861last tracepoint set."));
15862
e0f25bd9 15863 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 15864
5e84b7ee
SM
15865 cmd_list_element *delete_tracepoints_cmd
15866 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
15867Delete specified tracepoints.\n\
15868Arguments are tracepoint numbers, separated by spaces.\n\
15869No argument means delete all tracepoints."),
15870 &deletelist);
5e84b7ee 15871 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
15872
15873 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15874Disable specified tracepoints.\n\
15875Arguments are tracepoint numbers, separated by spaces.\n\
15876No argument means disable all tracepoints."),
15877 &disablelist);
15878 deprecate_cmd (c, "disable");
15879
15880 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15881Enable specified tracepoints.\n\
15882Arguments are tracepoint numbers, separated by spaces.\n\
15883No argument means enable all tracepoints."),
15884 &enablelist);
15885 deprecate_cmd (c, "enable");
15886
15887 add_com ("passcount", class_trace, trace_pass_command, _("\
15888Set the passcount for a tracepoint.\n\
15889The trace will end when the tracepoint has been passed 'count' times.\n\
15890Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15891if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15892
0743fc83
TT
15893 add_basic_prefix_cmd ("save", class_breakpoint,
15894 _("Save breakpoint definitions as a script."),
2f822da5 15895 &save_cmdlist,
0743fc83 15896 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15897
15898 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15899Save current breakpoint definitions as a script.\n\
cce7e648 15900This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15901catchpoints, tracepoints). Use the 'source' command in another debug\n\
15902session to restore them."),
15903 &save_cmdlist);
15904 set_cmd_completer (c, filename_completer);
15905
3947f654
SM
15906 cmd_list_element *save_tracepoints_cmd
15907 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15908Save current tracepoint definitions as a script.\n\
6149aea9
PA
15909Use the 'source' command in another debug session to restore them."),
15910 &save_cmdlist);
3947f654 15911 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 15912
3947f654 15913 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
15914 deprecate_cmd (c, "save tracepoints");
15915
3b6acaee 15916 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15917Breakpoint specific settings.\n\
fa8d40ab 15918Configure various breakpoint-specific variables such as\n\
590042fc 15919pending breakpoint behavior."),
2f822da5 15920 &breakpoint_set_cmdlist,
3b6acaee
TT
15921 0/*allow-unknown*/, &setlist);
15922 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15923Breakpoint specific settings.\n\
fa8d40ab 15924Configure various breakpoint-specific variables such as\n\
590042fc 15925pending breakpoint behavior."),
2f822da5 15926 &breakpoint_show_cmdlist,
3b6acaee 15927 0/*allow-unknown*/, &showlist);
fa8d40ab 15928
7915a72c
AC
15929 add_setshow_auto_boolean_cmd ("pending", no_class,
15930 &pending_break_support, _("\
15931Set debugger's behavior regarding pending breakpoints."), _("\
15932Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15933If on, an unrecognized breakpoint location will cause gdb to create a\n\
15934pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15935an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15936user-query to see if a pending breakpoint should be created."),
2c5b56ce 15937 NULL,
920d2a44 15938 show_pending_break_support,
6e1d7d6c
AC
15939 &breakpoint_set_cmdlist,
15940 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15941
15942 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15943
15944 add_setshow_boolean_cmd ("auto-hw", no_class,
15945 &automatic_hardware_breakpoints, _("\
15946Set automatic usage of hardware breakpoints."), _("\
15947Show automatic usage of hardware breakpoints."), _("\
15948If set, the debugger will automatically use hardware breakpoints for\n\
15949breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15950a warning will be emitted for such breakpoints."),
15951 NULL,
15952 show_automatic_hardware_breakpoints,
15953 &breakpoint_set_cmdlist,
15954 &breakpoint_show_cmdlist);
74960c60 15955
a25a5a45
PA
15956 add_setshow_boolean_cmd ("always-inserted", class_support,
15957 &always_inserted_mode, _("\
74960c60
VP
15958Set mode for inserting breakpoints."), _("\
15959Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15960When this mode is on, breakpoints are inserted immediately as soon as\n\
15961they're created, kept inserted even when execution stops, and removed\n\
15962only when the user deletes them. When this mode is off (the default),\n\
15963breakpoints are inserted only when execution continues, and removed\n\
15964when execution stops."),
72d0e2c5
YQ
15965 NULL,
15966 &show_always_inserted_mode,
15967 &breakpoint_set_cmdlist,
15968 &breakpoint_show_cmdlist);
f1310107 15969
b775012e
LM
15970 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15971 condition_evaluation_enums,
15972 &condition_evaluation_mode_1, _("\
15973Set mode of breakpoint condition evaluation."), _("\
15974Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15975When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15976evaluated on the host's side by GDB. When it is set to \"target\",\n\
15977breakpoint conditions will be downloaded to the target (if the target\n\
15978supports such feature) and conditions will be evaluated on the target's side.\n\
15979If this is set to \"auto\" (default), this will be automatically set to\n\
15980\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 15981be set to \"host\"."),
b775012e
LM
15982 &set_condition_evaluation_mode,
15983 &show_condition_evaluation_mode,
15984 &breakpoint_set_cmdlist,
15985 &breakpoint_show_cmdlist);
15986
f1310107
TJB
15987 add_com ("break-range", class_breakpoint, break_range_command, _("\
15988Set a breakpoint for an address range.\n\
15989break-range START-LOCATION, END-LOCATION\n\
15990where START-LOCATION and END-LOCATION can be one of the following:\n\
15991 LINENUM, for that line in the current file,\n\
15992 FILE:LINENUM, for that line in that file,\n\
15993 +OFFSET, for that number of lines after the current line\n\
dda83cd7 15994 or the start of the range\n\
f1310107
TJB
15995 FUNCTION, for the first line in that function,\n\
15996 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15997 *ADDRESS, for the instruction at that address.\n\
15998\n\
15999The breakpoint will stop execution of the inferior whenever it executes\n\
16000an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16001range (including START-LOCATION and END-LOCATION)."));
16002
e7e0cddf 16003 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16004Set a dynamic printf at specified location.\n\
e7e0cddf 16005dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16006location may be a linespec, explicit, or address location.\n"
16007"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16008 set_cmd_completer (c, location_completer);
16009
16010 add_setshow_enum_cmd ("dprintf-style", class_support,
16011 dprintf_style_enums, &dprintf_style, _("\
16012Set the style of usage for dynamic printf."), _("\
16013Show the style of usage for dynamic printf."), _("\
16014This setting chooses how GDB will do a dynamic printf.\n\
16015If the value is \"gdb\", then the printing is done by GDB to its own\n\
16016console, as with the \"printf\" command.\n\
16017If the value is \"call\", the print is done by calling a function in your\n\
16018program; by default printf(), but you can choose a different function or\n\
16019output stream by setting dprintf-function and dprintf-channel."),
16020 update_dprintf_commands, NULL,
16021 &setlist, &showlist);
16022
16023 dprintf_function = xstrdup ("printf");
16024 add_setshow_string_cmd ("dprintf-function", class_support,
16025 &dprintf_function, _("\
590042fc
PW
16026Set the function to use for dynamic printf."), _("\
16027Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
16028 update_dprintf_commands, NULL,
16029 &setlist, &showlist);
16030
16031 dprintf_channel = xstrdup ("");
16032 add_setshow_string_cmd ("dprintf-channel", class_support,
16033 &dprintf_channel, _("\
590042fc
PW
16034Set the channel to use for dynamic printf."), _("\
16035Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16036 update_dprintf_commands, NULL,
16037 &setlist, &showlist);
16038
d3ce09f5
SS
16039 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16040 &disconnected_dprintf, _("\
16041Set whether dprintf continues after GDB disconnects."), _("\
16042Show whether dprintf continues after GDB disconnects."), _("\
16043Use this to let dprintf commands continue to hit and produce output\n\
16044even if GDB disconnects or detaches from the target."),
16045 NULL,
16046 NULL,
16047 &setlist, &showlist);
16048
16049 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16050Target agent only formatted printing, like the C \"printf\" function.\n\
16051Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16052This supports most C printf format specifications, like %s, %d, etc.\n\
16053This is useful for formatted output in user-defined commands."));
d3ce09f5 16054
491144b5 16055 automatic_hardware_breakpoints = true;
f3b1572e 16056
c90e7d63
SM
16057 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16058 "breakpoint");
16059 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16060 "breakpoint");
c906108c 16061}
This page took 4.069933 seconds and 4 git commands to generate.