gdb: remove iterate_over_breakpoints function
[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
48d7020b
SM
524static const std::vector<bp_location *> &
525all_bp_locations ()
526{
527 return bp_locations;
528}
529
e0d9a270
SM
530/* Range to iterate over breakpoint locations at a given address. */
531
532struct bp_locations_at_addr_range
533{
534 using iterator = std::vector<bp_location *>::iterator;
535
536 bp_locations_at_addr_range (CORE_ADDR addr)
537 {
538 struct compare
539 {
540 bool operator() (const bp_location *loc, CORE_ADDR addr_) const
541 { return loc->address < addr_; }
542
543 bool operator() (CORE_ADDR addr_, const bp_location *loc) const
544 { return addr_ < loc->address; }
545 };
546
547 auto it_pair = std::equal_range (bp_locations.begin (), bp_locations.end (),
548 addr, compare ());
549
550 m_begin = it_pair.first;
551 m_end = it_pair.second;
552 }
553
554 iterator begin () const
555 { return m_begin; }
556
557 iterator end () const
558 { return m_end; }
559
560private:
561 iterator m_begin;
562 iterator m_end;
563};
564
565/* Return a range to iterate over all breakpoint locations exactly at address
566 ADDR.
567
568 If it's needed to iterate multiple times on the same range, it's possible
569 to save the range in a local variable and use it multiple times:
570
571 auto range = all_bp_locations_at_addr (addr);
572
573 for (bp_location *loc : range)
574 // use loc
575
576 for (bp_location *loc : range)
577 // use loc
578
579 This saves a bit of time, as it avoids re-doing the binary searches to find
580 the range's boundaries. Just remember not to change the bp_locations vector
581 in the mean time, as it could make the range's iterators stale. */
582
583static bp_locations_at_addr_range
584all_bp_locations_at_addr (CORE_ADDR addr)
585{
586 return bp_locations_at_addr_range (addr);
587}
588
4a64f543 589/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 590 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 591 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 592 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 593 an address you need to read. */
876fa593 594
f5336ca5 595static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 596
4a64f543
MS
597/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
598 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
599 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
600 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 601 scan for shadow bytes for an address you need to read. */
876fa593 602
f5336ca5 603static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 604
4a64f543 605/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
606 from the bp_locations array, but for which a hit may still be
607 reported by a target. */
1123588c 608static std::vector<bp_location *> moribund_locations;
20874c92 609
c906108c
SS
610/* Number of last breakpoint made. */
611
95a42b64
TT
612static int breakpoint_count;
613
86b17b60
PA
614/* The value of `breakpoint_count' before the last command that
615 created breakpoints. If the last (break-like) command created more
616 than one breakpoint, then the difference between BREAKPOINT_COUNT
617 and PREV_BREAKPOINT_COUNT is more than one. */
618static int prev_breakpoint_count;
c906108c 619
1042e4c0
SS
620/* Number of last tracepoint made. */
621
95a42b64 622static int tracepoint_count;
1042e4c0 623
6149aea9
PA
624static struct cmd_list_element *breakpoint_set_cmdlist;
625static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 626struct cmd_list_element *save_cmdlist;
6149aea9 627
badd37ce
SDJ
628/* See declaration at breakpoint.h. */
629
630struct breakpoint *
631breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
632 void *user_data)
633{
43892fdf
SM
634 for (breakpoint *b : all_breakpoints ())
635 if (func (b, user_data) != 0)
636 return b;
badd37ce 637
43892fdf 638 return nullptr;
badd37ce
SDJ
639}
640
468d015d
JJ
641/* Return whether a breakpoint is an active enabled breakpoint. */
642static int
643breakpoint_enabled (struct breakpoint *b)
644{
0d381245 645 return (b->enable_state == bp_enabled);
468d015d
JJ
646}
647
c906108c
SS
648/* Set breakpoint count to NUM. */
649
95a42b64 650static void
fba45db2 651set_breakpoint_count (int num)
c906108c 652{
86b17b60 653 prev_breakpoint_count = breakpoint_count;
c906108c 654 breakpoint_count = num;
4fa62494 655 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
656}
657
86b17b60
PA
658/* Used by `start_rbreak_breakpoints' below, to record the current
659 breakpoint count before "rbreak" creates any breakpoint. */
660static int rbreak_start_breakpoint_count;
661
95a42b64
TT
662/* Called at the start an "rbreak" command to record the first
663 breakpoint made. */
86b17b60 664
c80049d3 665scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 666{
86b17b60 667 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
668}
669
670/* Called at the end of an "rbreak" command to record the last
671 breakpoint made. */
86b17b60 672
c80049d3 673scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 674{
86b17b60 675 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
676}
677
4a64f543 678/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
679
680void
fba45db2 681clear_breakpoint_hit_counts (void)
c906108c 682{
43892fdf 683 for (breakpoint *b : all_breakpoints ())
c906108c
SS
684 b->hit_count = 0;
685}
686
c906108c 687\f
48cb2d85
VP
688/* Return the breakpoint with the specified number, or NULL
689 if the number does not refer to an existing breakpoint. */
690
691struct breakpoint *
692get_breakpoint (int num)
693{
43892fdf 694 for (breakpoint *b : all_breakpoints ())
48cb2d85
VP
695 if (b->number == num)
696 return b;
697
43892fdf 698 return nullptr;
48cb2d85 699}
5c44784c 700
c906108c 701\f
adc36818 702
b775012e
LM
703/* Mark locations as "conditions have changed" in case the target supports
704 evaluating conditions on its side. */
705
706static void
707mark_breakpoint_modified (struct breakpoint *b)
708{
b775012e
LM
709 /* This is only meaningful if the target is
710 evaluating conditions and if the user has
711 opted for condition evaluation on the target's
712 side. */
713 if (gdb_evaluates_breakpoint_condition_p ()
714 || !target_supports_evaluation_of_breakpoint_conditions ())
715 return;
716
717 if (!is_breakpoint (b))
718 return;
719
40cb8ca5 720 for (bp_location *loc : b->locations ())
b775012e
LM
721 loc->condition_changed = condition_modified;
722}
723
724/* Mark location as "conditions have changed" in case the target supports
725 evaluating conditions on its side. */
726
727static void
728mark_breakpoint_location_modified (struct bp_location *loc)
729{
730 /* This is only meaningful if the target is
731 evaluating conditions and if the user has
732 opted for condition evaluation on the target's
733 side. */
734 if (gdb_evaluates_breakpoint_condition_p ()
735 || !target_supports_evaluation_of_breakpoint_conditions ())
736
737 return;
738
739 if (!is_breakpoint (loc->owner))
740 return;
741
742 loc->condition_changed = condition_modified;
743}
744
745/* Sets the condition-evaluation mode using the static global
746 condition_evaluation_mode. */
747
748static void
eb4c3f4a 749set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
750 struct cmd_list_element *c)
751{
b775012e
LM
752 const char *old_mode, *new_mode;
753
754 if ((condition_evaluation_mode_1 == condition_evaluation_target)
755 && !target_supports_evaluation_of_breakpoint_conditions ())
756 {
757 condition_evaluation_mode_1 = condition_evaluation_mode;
758 warning (_("Target does not support breakpoint condition evaluation.\n"
759 "Using host evaluation mode instead."));
760 return;
761 }
762
763 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
764 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
765
abf1152a
JK
766 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
767 settings was "auto". */
768 condition_evaluation_mode = condition_evaluation_mode_1;
769
b775012e
LM
770 /* Only update the mode if the user picked a different one. */
771 if (new_mode != old_mode)
772 {
b775012e
LM
773 /* If the user switched to a different evaluation mode, we
774 need to synch the changes with the target as follows:
775
776 "host" -> "target": Send all (valid) conditions to the target.
777 "target" -> "host": Remove all the conditions from the target.
778 */
779
b775012e
LM
780 if (new_mode == condition_evaluation_target)
781 {
782 /* Mark everything modified and synch conditions with the
783 target. */
48d7020b 784 for (bp_location *loc : all_bp_locations ())
b775012e
LM
785 mark_breakpoint_location_modified (loc);
786 }
787 else
788 {
789 /* Manually mark non-duplicate locations to synch conditions
790 with the target. We do this to remove all the conditions the
791 target knows about. */
48d7020b 792 for (bp_location *loc : all_bp_locations ())
b775012e
LM
793 if (is_breakpoint (loc->owner) && loc->inserted)
794 loc->needs_update = 1;
795 }
796
797 /* Do the update. */
44702360 798 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
799 }
800
801 return;
802}
803
804/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
805 what "auto" is translating to. */
806
807static void
808show_condition_evaluation_mode (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
810{
811 if (condition_evaluation_mode == condition_evaluation_auto)
812 fprintf_filtered (file,
813 _("Breakpoint condition evaluation "
814 "mode is %s (currently %s).\n"),
815 value,
816 breakpoint_condition_evaluation_mode ());
817 else
818 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
819 value);
820}
821
b5fa468f
TBA
822/* Parse COND_STRING in the context of LOC and set as the condition
823 expression of LOC. BP_NUM is the number of LOC's owner, LOC_NUM is
824 the number of LOC within its owner. In case of parsing error, mark
825 LOC as DISABLED_BY_COND. In case of success, unset DISABLED_BY_COND. */
826
827static void
828set_breakpoint_location_condition (const char *cond_string, bp_location *loc,
829 int bp_num, int loc_num)
830{
831 bool has_junk = false;
832 try
833 {
834 expression_up new_exp = parse_exp_1 (&cond_string, loc->address,
835 block_for_pc (loc->address), 0);
836 if (*cond_string != 0)
837 has_junk = true;
838 else
839 {
840 loc->cond = std::move (new_exp);
841 if (loc->disabled_by_cond && loc->enabled)
842 printf_filtered (_("Breakpoint %d's condition is now valid at "
843 "location %d, enabling.\n"),
844 bp_num, loc_num);
845
846 loc->disabled_by_cond = false;
847 }
848 }
849 catch (const gdb_exception_error &e)
850 {
851 if (loc->enabled)
852 {
853 /* Warn if a user-enabled location is now becoming disabled-by-cond.
854 BP_NUM is 0 if the breakpoint is being defined for the first
855 time using the "break ... if ..." command, and non-zero if
856 already defined. */
857 if (bp_num != 0)
858 warning (_("failed to validate condition at location %d.%d, "
859 "disabling:\n %s"), bp_num, loc_num, e.what ());
860 else
861 warning (_("failed to validate condition at location %d, "
862 "disabling:\n %s"), loc_num, e.what ());
863 }
864
865 loc->disabled_by_cond = true;
866 }
867
868 if (has_junk)
869 error (_("Garbage '%s' follows condition"), cond_string);
870}
871
adc36818 872void
7a26bd4d 873set_breakpoint_condition (struct breakpoint *b, const char *exp,
733d554a 874 int from_tty, bool force)
adc36818 875{
4c55e970 876 if (*exp == 0)
3a5c3e22 877 {
4c55e970
TBA
878 xfree (b->cond_string);
879 b->cond_string = nullptr;
3a5c3e22 880
4c55e970 881 if (is_watchpoint (b))
78319c15 882 static_cast<watchpoint *> (b)->cond_exp.reset ();
4c55e970
TBA
883 else
884 {
b5fa468f 885 int loc_num = 1;
40cb8ca5 886 for (bp_location *loc : b->locations ())
4c55e970
TBA
887 {
888 loc->cond.reset ();
b5fa468f
TBA
889 if (loc->disabled_by_cond && loc->enabled)
890 printf_filtered (_("Breakpoint %d's condition is now valid at "
891 "location %d, enabling.\n"),
892 b->number, loc_num);
893 loc->disabled_by_cond = false;
894 loc_num++;
4c55e970
TBA
895
896 /* No need to free the condition agent expression
897 bytecode (if we have one). We will handle this
898 when we go through update_global_location_list. */
899 }
900 }
1e620590 901
adc36818
PM
902 if (from_tty)
903 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
904 }
905 else
906 {
adc36818
PM
907 if (is_watchpoint (b))
908 {
699bd4cf 909 innermost_block_tracker tracker;
78319c15 910 const char *arg = exp;
4c55e970 911 expression_up new_exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
78319c15 912 if (*arg != 0)
adc36818 913 error (_("Junk at end of expression"));
78319c15 914 watchpoint *w = static_cast<watchpoint *> (b);
4c55e970 915 w->cond_exp = std::move (new_exp);
699bd4cf 916 w->cond_exp_valid_block = tracker.block ();
adc36818
PM
917 }
918 else
919 {
4c55e970
TBA
920 /* Parse and set condition expressions. We make two passes.
921 In the first, we parse the condition string to see if it
b5fa468f
TBA
922 is valid in at least one location. If so, the condition
923 would be accepted. So we go ahead and set the locations'
924 conditions. In case no valid case is found, we throw
4c55e970
TBA
925 the error and the condition string will be rejected.
926 This two-pass approach is taken to avoid setting the
927 state of locations in case of a reject. */
40cb8ca5 928 for (bp_location *loc : b->locations ())
4c55e970 929 {
b5fa468f
TBA
930 try
931 {
932 const char *arg = exp;
933 parse_exp_1 (&arg, loc->address,
934 block_for_pc (loc->address), 0);
935 if (*arg != 0)
936 error (_("Junk at end of expression"));
937 break;
938 }
939 catch (const gdb_exception_error &e)
940 {
941 /* Condition string is invalid. If this happens to
733d554a
TBA
942 be the last loc, abandon (if not forced) or continue
943 (if forced). */
944 if (loc->next == nullptr && !force)
b5fa468f
TBA
945 throw;
946 }
4c55e970
TBA
947 }
948
b5fa468f 949 /* If we reach here, the condition is valid at some locations. */
dda83cd7 950 int loc_num = 1;
40cb8ca5
SM
951 for (bp_location *loc : b->locations ())
952 {
953 set_breakpoint_location_condition (exp, loc, b->number, loc_num);
954 loc_num++;
955 }
adc36818 956 }
1e620590
TBA
957
958 /* We know that the new condition parsed successfully. The
959 condition string of the breakpoint can be safely updated. */
960 xfree (b->cond_string);
961 b->cond_string = xstrdup (exp);
962 b->condition_not_parsed = 0;
adc36818 963 }
b775012e
LM
964 mark_breakpoint_modified (b);
965
76727919 966 gdb::observers::breakpoint_modified.notify (b);
adc36818
PM
967}
968
79aabb73
TBA
969/* See breakpoint.h. */
970
971void
972set_breakpoint_condition (int bpnum, const char *exp, int from_tty,
973 bool force)
974{
43892fdf 975 for (breakpoint *b : all_breakpoints ())
79aabb73
TBA
976 if (b->number == bpnum)
977 {
978 /* Check if this breakpoint has a "stop" method implemented in an
979 extension language. This method and conditions entered into GDB
980 from the CLI are mutually exclusive. */
981 const struct extension_language_defn *extlang
982 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
983
984 if (extlang != NULL)
985 {
986 error (_("Only one stop condition allowed. There is currently"
987 " a %s stop condition defined for this breakpoint."),
988 ext_lang_capitalized_name (extlang));
989 }
990 set_breakpoint_condition (b, exp, from_tty, force);
991
992 if (is_breakpoint (b))
993 update_global_location_list (UGLL_MAY_INSERT);
994
995 return;
996 }
997
998 error (_("No breakpoint number %d."), bpnum);
999}
1000
b1d4d8d1
TBA
1001/* The options for the "condition" command. */
1002
1003struct condition_command_opts
1004{
1005 /* For "-force". */
1006 bool force_condition = false;
1007};
1008
1009static const gdb::option::option_def condition_command_option_defs[] = {
1010
1011 gdb::option::flag_option_def<condition_command_opts> {
1012 "force",
1013 [] (condition_command_opts *opts) { return &opts->force_condition; },
1014 N_("Set the condition even if it is invalid for all current locations."),
1015 },
1016
1017};
1018
1019/* Create an option_def_group for the "condition" options, with
1020 CC_OPTS as context. */
1021
1022static inline gdb::option::option_def_group
1023make_condition_command_options_def_group (condition_command_opts *cc_opts)
1024{
1025 return {{condition_command_option_defs}, cc_opts};
1026}
1027
d55637df
TT
1028/* Completion for the "condition" command. */
1029
eb3ff9a5 1030static void
6f937416 1031condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1032 completion_tracker &tracker,
b1d4d8d1 1033 const char *text, const char * /*word*/)
d55637df 1034{
b1d4d8d1
TBA
1035 bool has_no_arguments = (*text == '\0');
1036 condition_command_opts cc_opts;
1037 const auto group = make_condition_command_options_def_group (&cc_opts);
1038 if (gdb::option::complete_options
1039 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group))
1040 return;
d55637df 1041
f1735a53 1042 text = skip_spaces (text);
b1d4d8d1 1043 const char *space = skip_to_space (text);
d55637df
TT
1044 if (*space == '\0')
1045 {
1046 int len;
d55637df
TT
1047
1048 if (text[0] == '$')
1049 {
b1d4d8d1 1050 tracker.advance_custom_word_point_by (1);
d55637df 1051 /* We don't support completion of history indices. */
eb3ff9a5
PA
1052 if (!isdigit (text[1]))
1053 complete_internalvar (tracker, &text[1]);
1054 return;
d55637df
TT
1055 }
1056
b1d4d8d1
TBA
1057 /* Suggest the "-force" flag if no arguments are given. If
1058 arguments were passed, they either already include the flag,
1059 or we are beyond the point of suggesting it because it's
1060 positionally the first argument. */
1061 if (has_no_arguments)
1062 gdb::option::complete_on_all_options (tracker, group);
1063
d55637df
TT
1064 /* We're completing the breakpoint number. */
1065 len = strlen (text);
1066
43892fdf 1067 for (breakpoint *b : all_breakpoints ())
58ce7251
SDJ
1068 {
1069 char number[50];
1070
1071 xsnprintf (number, sizeof (number), "%d", b->number);
1072
1073 if (strncmp (number, text, len) == 0)
b02f78f9 1074 tracker.add_completion (make_unique_xstrdup (number));
58ce7251 1075 }
d55637df 1076
eb3ff9a5 1077 return;
d55637df
TT
1078 }
1079
b1d4d8d1
TBA
1080 /* We're completing the expression part. Skip the breakpoint num. */
1081 const char *exp_start = skip_spaces (space);
1082 tracker.advance_custom_word_point_by (exp_start - text);
1083 text = exp_start;
1084 const char *word = advance_to_expression_complete_word_point (tracker, text);
eb3ff9a5 1085 expression_completer (cmd, tracker, text, word);
d55637df
TT
1086}
1087
c906108c
SS
1088/* condition N EXP -- set break condition of breakpoint N to EXP. */
1089
1090static void
0b39b52e 1091condition_command (const char *arg, int from_tty)
c906108c 1092{
0b39b52e 1093 const char *p;
52f0bd74 1094 int bnum;
c906108c
SS
1095
1096 if (arg == 0)
e2e0b3e5 1097 error_no_arg (_("breakpoint number"));
c906108c
SS
1098
1099 p = arg;
733d554a
TBA
1100
1101 /* Check if the "-force" flag was passed. */
b1d4d8d1
TBA
1102 condition_command_opts cc_opts;
1103 const auto group = make_condition_command_options_def_group (&cc_opts);
1104 gdb::option::process_options
1105 (&p, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, group);
733d554a 1106
c906108c 1107 bnum = get_number (&p);
5c44784c 1108 if (bnum == 0)
8a3fe4f8 1109 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c 1110
79aabb73 1111 set_breakpoint_condition (bnum, p, from_tty, cc_opts.force_condition);
c906108c
SS
1112}
1113
a7bdde9e
VP
1114/* Check that COMMAND do not contain commands that are suitable
1115 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1116 Throw if any such commands is found. */
1117
a7bdde9e
VP
1118static void
1119check_no_tracepoint_commands (struct command_line *commands)
1120{
1121 struct command_line *c;
cc59ec59 1122
a7bdde9e
VP
1123 for (c = commands; c; c = c->next)
1124 {
a7bdde9e 1125 if (c->control_type == while_stepping_control)
3e43a32a
MS
1126 error (_("The 'while-stepping' command can "
1127 "only be used for tracepoints"));
a7bdde9e 1128
12973681
TT
1129 check_no_tracepoint_commands (c->body_list_0.get ());
1130 check_no_tracepoint_commands (c->body_list_1.get ());
a7bdde9e
VP
1131
1132 /* Not that command parsing removes leading whitespace and comment
4a64f543 1133 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1134 command directly. */
1135 if (strstr (c->line, "collect ") == c->line)
1136 error (_("The 'collect' command can only be used for tracepoints"));
1137
51661e93
VP
1138 if (strstr (c->line, "teval ") == c->line)
1139 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1140 }
1141}
1142
c1fc2657 1143struct longjmp_breakpoint : public breakpoint
3b0871f4 1144{
c1fc2657 1145 ~longjmp_breakpoint () override;
3b0871f4
SM
1146};
1147
d77f58be
SS
1148/* Encapsulate tests for different types of tracepoints. */
1149
3b0871f4
SM
1150static bool
1151is_tracepoint_type (bptype type)
d9b3f62e
PA
1152{
1153 return (type == bp_tracepoint
1154 || type == bp_fast_tracepoint
1155 || type == bp_static_tracepoint);
1156}
1157
3b0871f4
SM
1158static bool
1159is_longjmp_type (bptype type)
1160{
1161 return type == bp_longjmp || type == bp_exception;
1162}
1163
f2478a7e
SM
1164/* See breakpoint.h. */
1165
1166bool
d77f58be 1167is_tracepoint (const struct breakpoint *b)
a7bdde9e 1168{
d9b3f62e 1169 return is_tracepoint_type (b->type);
a7bdde9e 1170}
d9b3f62e 1171
a5e364af
SM
1172/* Factory function to create an appropriate instance of breakpoint given
1173 TYPE. */
1174
1175static std::unique_ptr<breakpoint>
1176new_breakpoint_from_type (bptype type)
1177{
1178 breakpoint *b;
1179
1180 if (is_tracepoint_type (type))
c1fc2657 1181 b = new tracepoint ();
3b0871f4 1182 else if (is_longjmp_type (type))
c1fc2657 1183 b = new longjmp_breakpoint ();
a5e364af
SM
1184 else
1185 b = new breakpoint ();
1186
1187 return std::unique_ptr<breakpoint> (b);
1188}
1189
e5dd4106 1190/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1191 breakpoint. This function will throw an exception if a problem is
1192 found. */
48cb2d85 1193
95a42b64
TT
1194static void
1195validate_commands_for_breakpoint (struct breakpoint *b,
1196 struct command_line *commands)
48cb2d85 1197{
d77f58be 1198 if (is_tracepoint (b))
a7bdde9e 1199 {
c9a6ce02 1200 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1201 struct command_line *c;
1202 struct command_line *while_stepping = 0;
c9a6ce02
PA
1203
1204 /* Reset the while-stepping step count. The previous commands
dda83cd7
SM
1205 might have included a while-stepping action, while the new
1206 ones might not. */
c9a6ce02
PA
1207 t->step_count = 0;
1208
1209 /* We need to verify that each top-level element of commands is
1210 valid for tracepoints, that there's at most one
1211 while-stepping element, and that the while-stepping's body
1212 has valid tracing commands excluding nested while-stepping.
1213 We also need to validate the tracepoint action line in the
1214 context of the tracepoint --- validate_actionline actually
1215 has side effects, like setting the tracepoint's
1216 while-stepping STEP_COUNT, in addition to checking if the
1217 collect/teval actions parse and make sense in the
1218 tracepoint's context. */
a7bdde9e
VP
1219 for (c = commands; c; c = c->next)
1220 {
a7bdde9e
VP
1221 if (c->control_type == while_stepping_control)
1222 {
1223 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for fast tracepoint"));
0fb4aa4b 1226 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1227 error (_("The 'while-stepping' command "
1228 "cannot be used for static tracepoint"));
a7bdde9e
VP
1229
1230 if (while_stepping)
3e43a32a
MS
1231 error (_("The 'while-stepping' command "
1232 "can be used only once"));
a7bdde9e
VP
1233 else
1234 while_stepping = c;
1235 }
c9a6ce02
PA
1236
1237 validate_actionline (c->line, b);
a7bdde9e
VP
1238 }
1239 if (while_stepping)
1240 {
1241 struct command_line *c2;
1242
12973681
TT
1243 gdb_assert (while_stepping->body_list_1 == nullptr);
1244 c2 = while_stepping->body_list_0.get ();
a7bdde9e
VP
1245 for (; c2; c2 = c2->next)
1246 {
a7bdde9e
VP
1247 if (c2->control_type == while_stepping_control)
1248 error (_("The 'while-stepping' command cannot be nested"));
1249 }
1250 }
1251 }
1252 else
1253 {
1254 check_no_tracepoint_commands (commands);
1255 }
95a42b64
TT
1256}
1257
0fb4aa4b
PA
1258/* Return a vector of all the static tracepoints set at ADDR. The
1259 caller is responsible for releasing the vector. */
1260
f51e0e20 1261std::vector<breakpoint *>
0fb4aa4b
PA
1262static_tracepoints_here (CORE_ADDR addr)
1263{
f51e0e20 1264 std::vector<breakpoint *> found;
0fb4aa4b 1265
43892fdf 1266 for (breakpoint *b : all_breakpoints ())
0fb4aa4b
PA
1267 if (b->type == bp_static_tracepoint)
1268 {
40cb8ca5 1269 for (bp_location *loc : b->locations ())
0fb4aa4b 1270 if (loc->address == addr)
f51e0e20 1271 found.push_back (b);
0fb4aa4b
PA
1272 }
1273
1274 return found;
1275}
1276
95a42b64 1277/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1278 validate that only allowed commands are included. */
95a42b64
TT
1279
1280void
4a64f543 1281breakpoint_set_commands (struct breakpoint *b,
12973681 1282 counted_command_line &&commands)
95a42b64 1283{
93921405 1284 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1285
d1b0a7bf 1286 b->commands = std::move (commands);
76727919 1287 gdb::observers::breakpoint_modified.notify (b);
48cb2d85
VP
1288}
1289
45a43567
TT
1290/* Set the internal `silent' flag on the breakpoint. Note that this
1291 is not the same as the "silent" that may appear in the breakpoint's
1292 commands. */
1293
1294void
1295breakpoint_set_silent (struct breakpoint *b, int silent)
1296{
1297 int old_silent = b->silent;
1298
1299 b->silent = silent;
1300 if (old_silent != silent)
76727919 1301 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1302}
1303
1304/* Set the thread for this breakpoint. If THREAD is -1, make the
1305 breakpoint work for any thread. */
1306
1307void
1308breakpoint_set_thread (struct breakpoint *b, int thread)
1309{
1310 int old_thread = b->thread;
1311
1312 b->thread = thread;
1313 if (old_thread != thread)
76727919 1314 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1315}
1316
1317/* Set the task for this breakpoint. If TASK is 0, make the
1318 breakpoint work for any task. */
1319
1320void
1321breakpoint_set_task (struct breakpoint *b, int task)
1322{
1323 int old_task = b->task;
1324
1325 b->task = task;
1326 if (old_task != task)
76727919 1327 gdb::observers::breakpoint_modified.notify (b);
45a43567
TT
1328}
1329
95a42b64 1330static void
896b6bda 1331commands_command_1 (const char *arg, int from_tty,
4a64f543 1332 struct command_line *control)
95a42b64 1333{
d1b0a7bf 1334 counted_command_line cmd;
999700cd
PW
1335 /* cmd_read will be true once we have read cmd. Note that cmd might still be
1336 NULL after the call to read_command_lines if the user provides an empty
1337 list of command by just typing "end". */
1338 bool cmd_read = false;
95a42b64 1339
896b6bda
PA
1340 std::string new_arg;
1341
95a42b64
TT
1342 if (arg == NULL || !*arg)
1343 {
da1df1db 1344 /* Argument not explicitly given. Synthesize it. */
86b17b60 1345 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1346 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1347 breakpoint_count);
95a42b64 1348 else if (breakpoint_count > 0)
896b6bda 1349 new_arg = string_printf ("%d", breakpoint_count);
48649e1b 1350 }
da1df1db
TBA
1351 else
1352 {
1353 /* Create a copy of ARG. This is needed because the "commands"
1354 command may be coming from a script. In that case, the read
1355 line buffer is going to be overwritten in the lambda of
1356 'map_breakpoint_numbers' below when reading the next line
1357 before we are are done parsing the breakpoint numbers. */
1358 new_arg = arg;
1359 }
1360 arg = new_arg.c_str ();
48649e1b
TT
1361
1362 map_breakpoint_numbers
1363 (arg, [&] (breakpoint *b)
1364 {
999700cd 1365 if (!cmd_read)
48649e1b 1366 {
999700cd 1367 gdb_assert (cmd == NULL);
48649e1b 1368 if (control != NULL)
12973681 1369 cmd = control->body_list_0;
48649e1b
TT
1370 else
1371 {
81b1e71c
TT
1372 std::string str
1373 = string_printf (_("Type commands for breakpoint(s) "
1374 "%s, one per line."),
1375 arg);
48649e1b 1376
60b3cef2
TT
1377 auto do_validate = [=] (const char *line)
1378 {
1379 validate_actionline (line, b);
1380 };
1381 gdb::function_view<void (const char *)> validator;
1382 if (is_tracepoint (b))
1383 validator = do_validate;
1384
1385 cmd = read_command_lines (str.c_str (), from_tty, 1, validator);
48649e1b 1386 }
999700cd 1387 cmd_read = true;
48649e1b
TT
1388 }
1389
1390 /* If a breakpoint was on the list more than once, we don't need to
1391 do anything. */
1392 if (b->commands != cmd)
1393 {
d1b0a7bf 1394 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b 1395 b->commands = cmd;
76727919 1396 gdb::observers::breakpoint_modified.notify (b);
48649e1b
TT
1397 }
1398 });
95a42b64
TT
1399}
1400
1401static void
0b39b52e 1402commands_command (const char *arg, int from_tty)
95a42b64
TT
1403{
1404 commands_command_1 (arg, from_tty, NULL);
c906108c 1405}
40c03ae8
EZ
1406
1407/* Like commands_command, but instead of reading the commands from
1408 input stream, takes them from an already parsed command structure.
1409
1410 This is used by cli-script.c to DTRT with breakpoint commands
1411 that are part of if and while bodies. */
1412enum command_control_type
896b6bda 1413commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1414{
95a42b64
TT
1415 commands_command_1 (arg, 0, cmd);
1416 return simple_control;
40c03ae8 1417}
876fa593
JK
1418
1419/* Return non-zero if BL->TARGET_INFO contains valid information. */
1420
1421static int
1422bp_location_has_shadow (struct bp_location *bl)
1423{
1424 if (bl->loc_type != bp_loc_software_breakpoint)
1425 return 0;
1426 if (!bl->inserted)
1427 return 0;
1428 if (bl->target_info.shadow_len == 0)
e5dd4106 1429 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1430 return 0;
1431 return 1;
1432}
1433
9d497a19
PA
1434/* Update BUF, which is LEN bytes read from the target address
1435 MEMADDR, by replacing a memory breakpoint with its shadowed
1436 contents.
1437
1438 If READBUF is not NULL, this buffer must not overlap with the of
1439 the breakpoint location's shadow_contents buffer. Otherwise, a
1440 failed assertion internal error will be raised. */
1441
1442static void
1443one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1444 const gdb_byte *writebuf_org,
1445 ULONGEST memaddr, LONGEST len,
1446 struct bp_target_info *target_info,
1447 struct gdbarch *gdbarch)
1448{
1449 /* Now do full processing of the found relevant range of elements. */
1450 CORE_ADDR bp_addr = 0;
1451 int bp_size = 0;
1452 int bptoffset = 0;
1453
1454 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1455 current_program_space->aspace, 0))
1456 {
1457 /* The breakpoint is inserted in a different address space. */
1458 return;
1459 }
1460
1461 /* Addresses and length of the part of the breakpoint that
1462 we need to copy. */
1463 bp_addr = target_info->placed_address;
1464 bp_size = target_info->shadow_len;
1465
1466 if (bp_addr + bp_size <= memaddr)
1467 {
1468 /* The breakpoint is entirely before the chunk of memory we are
1469 reading. */
1470 return;
1471 }
1472
1473 if (bp_addr >= memaddr + len)
1474 {
1475 /* The breakpoint is entirely after the chunk of memory we are
1476 reading. */
1477 return;
1478 }
1479
1480 /* Offset within shadow_contents. */
1481 if (bp_addr < memaddr)
1482 {
1483 /* Only copy the second part of the breakpoint. */
1484 bp_size -= memaddr - bp_addr;
1485 bptoffset = memaddr - bp_addr;
1486 bp_addr = memaddr;
1487 }
1488
1489 if (bp_addr + bp_size > memaddr + len)
1490 {
1491 /* Only copy the first part of the breakpoint. */
1492 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1493 }
1494
1495 if (readbuf != NULL)
1496 {
1497 /* Verify that the readbuf buffer does not overlap with the
1498 shadow_contents buffer. */
1499 gdb_assert (target_info->shadow_contents >= readbuf + len
1500 || readbuf >= (target_info->shadow_contents
1501 + target_info->shadow_len));
1502
1503 /* Update the read buffer with this inserted breakpoint's
1504 shadow. */
1505 memcpy (readbuf + bp_addr - memaddr,
1506 target_info->shadow_contents + bptoffset, bp_size);
1507 }
1508 else
1509 {
1510 const unsigned char *bp;
0d5ed153
MR
1511 CORE_ADDR addr = target_info->reqstd_address;
1512 int placed_size;
9d497a19
PA
1513
1514 /* Update the shadow with what we want to write to memory. */
1515 memcpy (target_info->shadow_contents + bptoffset,
1516 writebuf_org + bp_addr - memaddr, bp_size);
1517
1518 /* Determine appropriate breakpoint contents and size for this
1519 address. */
0d5ed153 1520 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1521
1522 /* Update the final write buffer with this inserted
1523 breakpoint's INSN. */
1524 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1525 }
1526}
1527
8defab1a 1528/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1529 by replacing any memory breakpoints with their shadowed contents.
1530
35c63cd8
JB
1531 If READBUF is not NULL, this buffer must not overlap with any of
1532 the breakpoint location's shadow_contents buffers. Otherwise,
1533 a failed assertion internal error will be raised.
1534
876fa593 1535 The range of shadowed area by each bp_location is:
f5336ca5
PA
1536 bl->address - bp_locations_placed_address_before_address_max
1537 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1538 The range we were requested to resolve shadows for is:
1539 memaddr ... memaddr + len
1540 Thus the safe cutoff boundaries for performance optimization are
35df4500 1541 memaddr + len <= (bl->address
f5336ca5 1542 - bp_locations_placed_address_before_address_max)
876fa593 1543 and:
f5336ca5 1544 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1545
8defab1a 1546void
f0ba3972
PA
1547breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1548 const gdb_byte *writebuf_org,
1549 ULONGEST memaddr, LONGEST len)
c906108c 1550{
4a64f543
MS
1551 /* Left boundary, right boundary and median element of our binary
1552 search. */
876fa593
JK
1553 unsigned bc_l, bc_r, bc;
1554
4a64f543
MS
1555 /* Find BC_L which is a leftmost element which may affect BUF
1556 content. It is safe to report lower value but a failure to
1557 report higher one. */
876fa593
JK
1558
1559 bc_l = 0;
5d51cd5d 1560 bc_r = bp_locations.size ();
876fa593
JK
1561 while (bc_l + 1 < bc_r)
1562 {
35df4500 1563 struct bp_location *bl;
876fa593
JK
1564
1565 bc = (bc_l + bc_r) / 2;
f5336ca5 1566 bl = bp_locations[bc];
876fa593 1567
4a64f543
MS
1568 /* Check first BL->ADDRESS will not overflow due to the added
1569 constant. Then advance the left boundary only if we are sure
1570 the BC element can in no way affect the BUF content (MEMADDR
1571 to MEMADDR + LEN range).
876fa593 1572
f5336ca5 1573 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1574 offset so that we cannot miss a breakpoint with its shadow
1575 range tail still reaching MEMADDR. */
c5aa993b 1576
f5336ca5 1577 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1578 >= bl->address)
f5336ca5 1579 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1580 <= memaddr))
876fa593
JK
1581 bc_l = bc;
1582 else
1583 bc_r = bc;
1584 }
1585
128070bb
PA
1586 /* Due to the binary search above, we need to make sure we pick the
1587 first location that's at BC_L's address. E.g., if there are
1588 multiple locations at the same address, BC_L may end up pointing
1589 at a duplicate location, and miss the "master"/"inserted"
1590 location. Say, given locations L1, L2 and L3 at addresses A and
1591 B:
1592
1593 L1@A, L2@A, L3@B, ...
1594
1595 BC_L could end up pointing at location L2, while the "master"
1596 location could be L1. Since the `loc->inserted' flag is only set
1597 on "master" locations, we'd forget to restore the shadow of L1
1598 and L2. */
1599 while (bc_l > 0
f5336ca5 1600 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1601 bc_l--;
1602
876fa593
JK
1603 /* Now do full processing of the found relevant range of elements. */
1604
5d51cd5d 1605 for (bc = bc_l; bc < bp_locations.size (); bc++)
c5aa993b 1606 {
f5336ca5 1607 struct bp_location *bl = bp_locations[bc];
876fa593 1608
35df4500
TJB
1609 /* bp_location array has BL->OWNER always non-NULL. */
1610 if (bl->owner->type == bp_none)
8a3fe4f8 1611 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1612 bl->owner->number);
ffce0d52 1613
e5dd4106 1614 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1615 content. */
1616
f5336ca5
PA
1617 if (bl->address >= bp_locations_placed_address_before_address_max
1618 && memaddr + len <= (bl->address
1619 - bp_locations_placed_address_before_address_max))
876fa593
JK
1620 break;
1621
35df4500 1622 if (!bp_location_has_shadow (bl))
c5aa993b 1623 continue;
6c95b8df 1624
9d497a19
PA
1625 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1626 memaddr, len, &bl->target_info, bl->gdbarch);
1627 }
c906108c 1628}
9d497a19 1629
f2478a7e 1630/* See breakpoint.h. */
b775012e 1631
f2478a7e 1632bool
b775012e
LM
1633is_breakpoint (const struct breakpoint *bpt)
1634{
1635 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1636 || bpt->type == bp_hardware_breakpoint
1637 || bpt->type == bp_dprintf);
b775012e
LM
1638}
1639
60e1c644
PA
1640/* Return true if BPT is of any hardware watchpoint kind. */
1641
f2478a7e 1642static bool
d77f58be 1643is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1644{
1645 return (bpt->type == bp_hardware_watchpoint
1646 || bpt->type == bp_read_watchpoint
1647 || bpt->type == bp_access_watchpoint);
1648}
7270d8f2 1649
f2478a7e 1650/* See breakpoint.h. */
60e1c644 1651
f2478a7e 1652bool
d77f58be 1653is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1654{
1655 return (is_hardware_watchpoint (bpt)
1656 || bpt->type == bp_watchpoint);
1657}
1658
3a5c3e22
PA
1659/* Returns true if the current thread and its running state are safe
1660 to evaluate or update watchpoint B. Watchpoints on local
1661 expressions need to be evaluated in the context of the thread that
1662 was current when the watchpoint was created, and, that thread needs
1663 to be stopped to be able to select the correct frame context.
1664 Watchpoints on global expressions can be evaluated on any thread,
1665 and in any state. It is presently left to the target allowing
1666 memory accesses when threads are running. */
f6bc2008
PA
1667
1668static int
3a5c3e22 1669watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1670{
c1fc2657 1671 return (b->pspace == current_program_space
d7e15655
TT
1672 && (b->watchpoint_thread == null_ptid
1673 || (inferior_ptid == b->watchpoint_thread
00431a78 1674 && !inferior_thread ()->executing)));
f6bc2008
PA
1675}
1676
d0fb5eae
JK
1677/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1678 associated bp_watchpoint_scope breakpoint. */
1679
1680static void
3a5c3e22 1681watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1682{
c1fc2657 1683 if (w->related_breakpoint != w)
d0fb5eae 1684 {
c1fc2657
SM
1685 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1686 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1687 w->related_breakpoint->disposition = disp_del_at_next_stop;
1688 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1689 w->related_breakpoint = w;
d0fb5eae 1690 }
c1fc2657 1691 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1692}
1693
bb9d5f81
PP
1694/* Extract a bitfield value from value VAL using the bit parameters contained in
1695 watchpoint W. */
1696
1697static struct value *
1698extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1699{
1700 struct value *bit_val;
1701
1702 if (val == NULL)
1703 return NULL;
1704
1705 bit_val = allocate_value (value_type (val));
1706
1707 unpack_value_bitfield (bit_val,
1708 w->val_bitpos,
1709 w->val_bitsize,
1710 value_contents_for_printing (val),
1711 value_offset (val),
1712 val);
1713
1714 return bit_val;
1715}
1716
c6d81124
PA
1717/* Allocate a dummy location and add it to B, which must be a software
1718 watchpoint. This is required because even if a software watchpoint
1719 is not watching any memory, bpstat_stop_status requires a location
1720 to be able to report stops. */
1721
1722static void
1723software_watchpoint_add_no_memory_location (struct breakpoint *b,
1724 struct program_space *pspace)
1725{
1726 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1727
1728 b->loc = allocate_bp_location (b);
1729 b->loc->pspace = pspace;
1730 b->loc->address = -1;
1731 b->loc->length = -1;
1732}
1733
1734/* Returns true if B is a software watchpoint that is not watching any
1735 memory (e.g., "watch $pc"). */
1736
f2478a7e 1737static bool
c6d81124
PA
1738is_no_memory_software_watchpoint (struct breakpoint *b)
1739{
1740 return (b->type == bp_watchpoint
1741 && b->loc != NULL
1742 && b->loc->next == NULL
1743 && b->loc->address == -1
1744 && b->loc->length == -1);
1745}
1746
567e1b4e
JB
1747/* Assuming that B is a watchpoint:
1748 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1749 - Evaluate expression and store the result in B->val
567e1b4e
JB
1750 - Evaluate the condition if there is one, and store the result
1751 in b->loc->cond.
a5606eee
VP
1752 - Update the list of values that must be watched in B->loc.
1753
4a64f543
MS
1754 If the watchpoint disposition is disp_del_at_next_stop, then do
1755 nothing. If this is local watchpoint that is out of scope, delete
1756 it.
1757
1758 Even with `set breakpoint always-inserted on' the watchpoints are
1759 removed + inserted on each stop here. Normal breakpoints must
1760 never be removed because they might be missed by a running thread
1761 when debugging in non-stop mode. On the other hand, hardware
1762 watchpoints (is_hardware_watchpoint; processed here) are specific
1763 to each LWP since they are stored in each LWP's hardware debug
1764 registers. Therefore, such LWP must be stopped first in order to
1765 be able to modify its hardware watchpoints.
1766
1767 Hardware watchpoints must be reset exactly once after being
1768 presented to the user. It cannot be done sooner, because it would
1769 reset the data used to present the watchpoint hit to the user. And
1770 it must not be done later because it could display the same single
1771 watchpoint hit during multiple GDB stops. Note that the latter is
1772 relevant only to the hardware watchpoint types bp_read_watchpoint
1773 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1774 not user-visible - its hit is suppressed if the memory content has
1775 not changed.
1776
1777 The following constraints influence the location where we can reset
1778 hardware watchpoints:
1779
1780 * target_stopped_by_watchpoint and target_stopped_data_address are
1781 called several times when GDB stops.
1782
1783 [linux]
1784 * Multiple hardware watchpoints can be hit at the same time,
1785 causing GDB to stop. GDB only presents one hardware watchpoint
1786 hit at a time as the reason for stopping, and all the other hits
1787 are presented later, one after the other, each time the user
1788 requests the execution to be resumed. Execution is not resumed
1789 for the threads still having pending hit event stored in
1790 LWP_INFO->STATUS. While the watchpoint is already removed from
1791 the inferior on the first stop the thread hit event is kept being
1792 reported from its cached value by linux_nat_stopped_data_address
1793 until the real thread resume happens after the watchpoint gets
1794 presented and thus its LWP_INFO->STATUS gets reset.
1795
1796 Therefore the hardware watchpoint hit can get safely reset on the
1797 watchpoint removal from inferior. */
a79d3c27 1798
b40ce68a 1799static void
3a5c3e22 1800update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1801{
a5606eee 1802 int within_current_scope;
a5606eee 1803 struct frame_id saved_frame_id;
66076460 1804 int frame_saved;
a5606eee 1805
f6bc2008
PA
1806 /* If this is a local watchpoint, we only want to check if the
1807 watchpoint frame is in scope if the current thread is the thread
1808 that was used to create the watchpoint. */
1809 if (!watchpoint_in_thread_scope (b))
1810 return;
1811
c1fc2657 1812 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1813 return;
1814
66076460 1815 frame_saved = 0;
a5606eee
VP
1816
1817 /* Determine if the watchpoint is within scope. */
1818 if (b->exp_valid_block == NULL)
1819 within_current_scope = 1;
1820 else
1821 {
b5db5dfc
UW
1822 struct frame_info *fi = get_current_frame ();
1823 struct gdbarch *frame_arch = get_frame_arch (fi);
1824 CORE_ADDR frame_pc = get_frame_pc (fi);
1825
c9cf6e20
MG
1826 /* If we're at a point where the stack has been destroyed
1827 (e.g. in a function epilogue), unwinding may not work
1828 properly. Do not attempt to recreate locations at this
b5db5dfc 1829 point. See similar comments in watchpoint_check. */
c9cf6e20 1830 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1831 return;
66076460
DJ
1832
1833 /* Save the current frame's ID so we can restore it after
dda83cd7 1834 evaluating the watchpoint expression on its own frame. */
66076460 1835 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
dda83cd7
SM
1836 took a frame parameter, so that we didn't have to change the
1837 selected frame. */
66076460
DJ
1838 frame_saved = 1;
1839 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1840
a5606eee
VP
1841 fi = frame_find_by_id (b->watchpoint_frame);
1842 within_current_scope = (fi != NULL);
1843 if (within_current_scope)
1844 select_frame (fi);
1845 }
1846
b5db5dfc
UW
1847 /* We don't free locations. They are stored in the bp_location array
1848 and update_global_location_list will eventually delete them and
1849 remove breakpoints if needed. */
c1fc2657 1850 b->loc = NULL;
b5db5dfc 1851
a5606eee
VP
1852 if (within_current_scope && reparse)
1853 {
bbc13ae3 1854 const char *s;
d63d0675 1855
4d01a485 1856 b->exp.reset ();
d63d0675 1857 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1858 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1859 /* If the meaning of expression itself changed, the old value is
1860 no longer relevant. We don't want to report a watchpoint hit
1861 to the user when the old value and the new value may actually
1862 be completely different objects. */
fa4727a6 1863 b->val = NULL;
4c1d86d9 1864 b->val_valid = false;
60e1c644
PA
1865
1866 /* Note that unlike with breakpoints, the watchpoint's condition
1867 expression is stored in the breakpoint object, not in the
1868 locations (re)created below. */
c1fc2657 1869 if (b->cond_string != NULL)
60e1c644 1870 {
4d01a485 1871 b->cond_exp.reset ();
60e1c644 1872
c1fc2657 1873 s = b->cond_string;
1bb9788d 1874 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1875 }
a5606eee 1876 }
a5606eee
VP
1877
1878 /* If we failed to parse the expression, for example because
1879 it refers to a global variable in a not-yet-loaded shared library,
1880 don't try to insert watchpoint. We don't automatically delete
1881 such watchpoint, though, since failure to parse expression
1882 is different from out-of-scope watchpoint. */
55f6301a 1883 if (!target_has_execution ())
2d134ed3
PA
1884 {
1885 /* Without execution, memory can't change. No use to try and
1886 set watchpoint locations. The watchpoint will be reset when
1887 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1888 if (!can_use_hw_watchpoints)
1889 {
c1fc2657
SM
1890 if (b->ops->works_in_software_mode (b))
1891 b->type = bp_watchpoint;
e8369a73 1892 else
638aa5a1
AB
1893 error (_("Can't set read/access watchpoint when "
1894 "hardware watchpoints are disabled."));
e8369a73 1895 }
2d134ed3
PA
1896 }
1897 else if (within_current_scope && b->exp)
a5606eee 1898 {
a6535de1 1899 std::vector<value_ref_ptr> val_chain;
8d49165d 1900 struct value *v, *result;
2d134ed3 1901 struct program_space *frame_pspace;
a5606eee 1902
1eaebe02 1903 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &v, &result,
413403fc 1904 &val_chain, false);
a5606eee 1905
a5606eee
VP
1906 /* Avoid setting b->val if it's already set. The meaning of
1907 b->val is 'the last value' user saw, and we should update
1908 it only if we reported that last value to user. As it
9c06b0b4
TJB
1909 happens, the code that reports it updates b->val directly.
1910 We don't keep track of the memory value for masked
1911 watchpoints. */
c1fc2657 1912 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1913 {
bb9d5f81 1914 if (b->val_bitsize != 0)
850645cf
TT
1915 v = extract_bitfield_from_watchpoint_value (b, v);
1916 b->val = release_value (v);
4c1d86d9 1917 b->val_valid = true;
fa4727a6 1918 }
a5606eee 1919
2d134ed3
PA
1920 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1921
a5606eee 1922 /* Look at each value on the value chain. */
a6535de1
TT
1923 gdb_assert (!val_chain.empty ());
1924 for (const value_ref_ptr &iter : val_chain)
a5606eee 1925 {
a6535de1
TT
1926 v = iter.get ();
1927
a5606eee
VP
1928 /* If it's a memory location, and GDB actually needed
1929 its contents to evaluate the expression, then we
fa4727a6
DJ
1930 must watch it. If the first value returned is
1931 still lazy, that means an error occurred reading it;
1932 watch it anyway in case it becomes readable. */
a5606eee 1933 if (VALUE_LVAL (v) == lval_memory
a6535de1 1934 && (v == val_chain[0] || ! value_lazy (v)))
a5606eee
VP
1935 {
1936 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1937
a5606eee
VP
1938 /* We only watch structs and arrays if user asked
1939 for it explicitly, never if they just happen to
1940 appear in the middle of some value chain. */
fa4727a6 1941 if (v == result
78134374
SM
1942 || (vtype->code () != TYPE_CODE_STRUCT
1943 && vtype->code () != TYPE_CODE_ARRAY))
a5606eee
VP
1944 {
1945 CORE_ADDR addr;
f486487f 1946 enum target_hw_bp_type type;
a5606eee 1947 struct bp_location *loc, **tmp;
bb9d5f81
PP
1948 int bitpos = 0, bitsize = 0;
1949
1950 if (value_bitsize (v) != 0)
1951 {
1952 /* Extract the bit parameters out from the bitfield
1953 sub-expression. */
1954 bitpos = value_bitpos (v);
1955 bitsize = value_bitsize (v);
1956 }
1957 else if (v == result && b->val_bitsize != 0)
1958 {
1959 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1960 lvalue whose bit parameters are saved in the fields
1961 VAL_BITPOS and VAL_BITSIZE. */
1962 bitpos = b->val_bitpos;
1963 bitsize = b->val_bitsize;
1964 }
a5606eee 1965
42ae5230 1966 addr = value_address (v);
bb9d5f81
PP
1967 if (bitsize != 0)
1968 {
1969 /* Skip the bytes that don't contain the bitfield. */
1970 addr += bitpos / 8;
1971 }
1972
a5606eee 1973 type = hw_write;
c1fc2657 1974 if (b->type == bp_read_watchpoint)
a5606eee 1975 type = hw_read;
c1fc2657 1976 else if (b->type == bp_access_watchpoint)
a5606eee 1977 type = hw_access;
3a5c3e22 1978
c1fc2657
SM
1979 loc = allocate_bp_location (b);
1980 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1981 ;
1982 *tmp = loc;
8ee511af 1983 loc->gdbarch = value_type (v)->arch ();
6c95b8df
PA
1984
1985 loc->pspace = frame_pspace;
f17d9474 1986 loc->address = address_significant (loc->gdbarch, addr);
bb9d5f81
PP
1987
1988 if (bitsize != 0)
1989 {
1990 /* Just cover the bytes that make up the bitfield. */
1991 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1992 }
1993 else
1994 loc->length = TYPE_LENGTH (value_type (v));
1995
a5606eee
VP
1996 loc->watchpoint_type = type;
1997 }
1998 }
9fa40276
TJB
1999 }
2000
2001 /* Change the type of breakpoint between hardware assisted or
2002 an ordinary watchpoint depending on the hardware support
2003 and free hardware slots. REPARSE is set when the inferior
2004 is started. */
a9634178 2005 if (reparse)
9fa40276 2006 {
e09342b5 2007 int reg_cnt;
9fa40276 2008 enum bp_loc_type loc_type;
a5606eee 2009
a9634178 2010 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2011
2012 if (reg_cnt)
9fa40276
TJB
2013 {
2014 int i, target_resources_ok, other_type_used;
a1398e0c 2015 enum bptype type;
9fa40276 2016
a9634178
TJB
2017 /* Use an exact watchpoint when there's only one memory region to be
2018 watched, and only one debug register is needed to watch it. */
2019 b->exact = target_exact_watchpoints && reg_cnt == 1;
2020
9fa40276 2021 /* We need to determine how many resources are already
e09342b5
TJB
2022 used for all other hardware watchpoints plus this one
2023 to see if we still have enough resources to also fit
a1398e0c
PA
2024 this watchpoint in as well. */
2025
2026 /* If this is a software watchpoint, we try to turn it
2027 to a hardware one -- count resources as if B was of
2028 hardware watchpoint type. */
c1fc2657 2029 type = b->type;
a1398e0c
PA
2030 if (type == bp_watchpoint)
2031 type = bp_hardware_watchpoint;
2032
2033 /* This watchpoint may or may not have been placed on
2034 the list yet at this point (it won't be in the list
2035 if we're trying to create it for the first time,
2036 through watch_command), so always account for it
2037 manually. */
2038
2039 /* Count resources used by all watchpoints except B. */
c1fc2657 2040 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2041
2042 /* Add in the resources needed for B. */
c1fc2657 2043 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2044
2045 target_resources_ok
2046 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2047 if (target_resources_ok <= 0)
a9634178 2048 {
c1fc2657 2049 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2050
2051 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2052 error (_("Target does not support this type of "
2053 "hardware watchpoint."));
9c06b0b4
TJB
2054 else if (target_resources_ok < 0 && !sw_mode)
2055 error (_("There are not enough available hardware "
2056 "resources for this watchpoint."));
a1398e0c
PA
2057
2058 /* Downgrade to software watchpoint. */
c1fc2657 2059 b->type = bp_watchpoint;
a1398e0c
PA
2060 }
2061 else
2062 {
2063 /* If this was a software watchpoint, we've just
2064 found we have enough resources to turn it to a
2065 hardware watchpoint. Otherwise, this is a
2066 nop. */
c1fc2657 2067 b->type = type;
a9634178 2068 }
9fa40276 2069 }
c1fc2657 2070 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2071 {
2072 if (!can_use_hw_watchpoints)
2073 error (_("Can't set read/access watchpoint when "
2074 "hardware watchpoints are disabled."));
2075 else
2076 error (_("Expression cannot be implemented with "
2077 "read/access watchpoint."));
2078 }
9fa40276 2079 else
c1fc2657 2080 b->type = bp_watchpoint;
9fa40276 2081
c1fc2657 2082 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2083 : bp_loc_hardware_watchpoint);
40cb8ca5 2084 for (bp_location *bl : b->locations ())
9fa40276
TJB
2085 bl->loc_type = loc_type;
2086 }
2087
c7437ca6
PA
2088 /* If a software watchpoint is not watching any memory, then the
2089 above left it without any location set up. But,
2090 bpstat_stop_status requires a location to be able to report
2091 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2092 if (b->type == bp_watchpoint && b->loc == NULL)
2093 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2094 }
2095 else if (!within_current_scope)
7270d8f2 2096 {
ac74f770
MS
2097 printf_filtered (_("\
2098Watchpoint %d deleted because the program has left the block\n\
2099in which its expression is valid.\n"),
c1fc2657 2100 b->number);
d0fb5eae 2101 watchpoint_del_at_next_stop (b);
7270d8f2 2102 }
a5606eee
VP
2103
2104 /* Restore the selected frame. */
66076460
DJ
2105 if (frame_saved)
2106 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2107}
2108
a5606eee 2109
74960c60 2110/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2111 inserted in the inferior. We don't differentiate the type of BL's owner
2112 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2113 breakpoint_ops is not defined, because in insert_bp_location,
2114 tracepoint's insert_location will not be called. */
74960c60 2115static int
35df4500 2116should_be_inserted (struct bp_location *bl)
74960c60 2117{
35df4500 2118 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2119 return 0;
2120
35df4500 2121 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2122 return 0;
2123
b5fa468f
TBA
2124 if (!bl->enabled || bl->disabled_by_cond
2125 || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2126 return 0;
2127
f8eba3c6
TT
2128 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2129 return 0;
2130
56710373
PA
2131 /* This is set for example, when we're attached to the parent of a
2132 vfork, and have detached from the child. The child is running
2133 free, and we expect it to do an exec or exit, at which point the
2134 OS makes the parent schedulable again (and the target reports
2135 that the vfork is done). Until the child is done with the shared
2136 memory region, do not insert breakpoints in the parent, otherwise
2137 the child could still trip on the parent's breakpoints. Since
2138 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2139 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2140 return 0;
2141
31e77af2 2142 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2143 location, except if the breakpoint is a single-step breakpoint,
2144 and the breakpoint's thread is the thread which is stepping past
2145 a breakpoint. */
31e77af2
PA
2146 if ((bl->loc_type == bp_loc_software_breakpoint
2147 || bl->loc_type == bp_loc_hardware_breakpoint)
2148 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2149 bl->address)
2150 /* The single-step breakpoint may be inserted at the location
2151 we're trying to step if the instruction branches to itself.
2152 However, the instruction won't be executed at all and it may
2153 break the semantics of the instruction, for example, the
2154 instruction is a conditional branch or updates some flags.
2155 We can't fix it unless GDB is able to emulate the instruction
2156 or switch to displaced stepping. */
2157 && !(bl->owner->type == bp_single_step
2158 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1 2159 {
1eb8556f
SM
2160 infrun_debug_printf ("skipping breakpoint: stepping past insn at: %s",
2161 paddress (bl->gdbarch, bl->address));
e558d7c1
PA
2162 return 0;
2163 }
31e77af2 2164
963f9c80
PA
2165 /* Don't insert watchpoints if we're trying to step past the
2166 instruction that triggered one. */
2167 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2168 && stepping_past_nonsteppable_watchpoint ())
2169 {
1eb8556f 2170 infrun_debug_printf ("stepping past non-steppable watchpoint. "
ae1f4d2d 2171 "skipping watchpoint at %s:%d",
1eb8556f 2172 paddress (bl->gdbarch, bl->address), bl->length);
963f9c80
PA
2173 return 0;
2174 }
2175
74960c60
VP
2176 return 1;
2177}
2178
934709f0
PW
2179/* Same as should_be_inserted but does the check assuming
2180 that the location is not duplicated. */
2181
2182static int
2183unduplicated_should_be_inserted (struct bp_location *bl)
2184{
2185 int result;
2186 const int save_duplicate = bl->duplicate;
2187
2188 bl->duplicate = 0;
2189 result = should_be_inserted (bl);
2190 bl->duplicate = save_duplicate;
2191 return result;
2192}
2193
b775012e
LM
2194/* Parses a conditional described by an expression COND into an
2195 agent expression bytecode suitable for evaluation
2196 by the bytecode interpreter. Return NULL if there was
2197 any error during parsing. */
2198
833177a4 2199static agent_expr_up
b775012e
LM
2200parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2201{
833177a4 2202 if (cond == NULL)
b775012e
LM
2203 return NULL;
2204
833177a4
PA
2205 agent_expr_up aexpr;
2206
b775012e
LM
2207 /* We don't want to stop processing, so catch any errors
2208 that may show up. */
a70b8144 2209 try
b775012e 2210 {
036e657b 2211 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2212 }
2213
230d2906 2214 catch (const gdb_exception_error &ex)
b775012e
LM
2215 {
2216 /* If we got here, it means the condition could not be parsed to a valid
2217 bytecode expression and thus can't be evaluated on the target's side.
2218 It's no use iterating through the conditions. */
b775012e
LM
2219 }
2220
2221 /* We have a valid agent expression. */
2222 return aexpr;
2223}
2224
2225/* Based on location BL, create a list of breakpoint conditions to be
2226 passed on to the target. If we have duplicated locations with different
2227 conditions, we will add such conditions to the list. The idea is that the
2228 target will evaluate the list of conditions and will only notify GDB when
2229 one of them is true. */
2230
2231static void
2232build_target_condition_list (struct bp_location *bl)
2233{
b775012e
LM
2234 int null_condition_or_parse_error = 0;
2235 int modified = bl->needs_update;
b775012e 2236
8b4f3082 2237 /* Release conditions left over from a previous insert. */
3cde5c42 2238 bl->target_info.conditions.clear ();
8b4f3082 2239
b775012e
LM
2240 /* This is only meaningful if the target is
2241 evaluating conditions and if the user has
2242 opted for condition evaluation on the target's
2243 side. */
2244 if (gdb_evaluates_breakpoint_condition_p ()
2245 || !target_supports_evaluation_of_breakpoint_conditions ())
2246 return;
2247
e0d9a270
SM
2248 auto loc_range = all_bp_locations_at_addr (bl->address);
2249
b775012e 2250 /* Do a first pass to check for locations with no assigned
7f32a4d5
PA
2251 conditions or conditions that fail to parse to a valid agent
2252 expression bytecode. If any of these happen, then it's no use to
2253 send conditions to the target since this location will always
2254 trigger and generate a response back to GDB. Note we consider
2255 all locations at the same address irrespective of type, i.e.,
2256 even if the locations aren't considered duplicates (e.g.,
2257 software breakpoint and hardware breakpoint at the same
2258 address). */
e0d9a270 2259 for (bp_location *loc : loc_range)
b775012e 2260 {
b775012e
LM
2261 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2262 {
2263 if (modified)
2264 {
b775012e
LM
2265 /* Re-parse the conditions since something changed. In that
2266 case we already freed the condition bytecodes (see
2267 force_breakpoint_reinsertion). We just
2268 need to parse the condition to bytecodes again. */
833177a4
PA
2269 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2270 loc->cond.get ());
b775012e
LM
2271 }
2272
2273 /* If we have a NULL bytecode expression, it means something
2274 went wrong or we have a null condition expression. */
2275 if (!loc->cond_bytecode)
2276 {
2277 null_condition_or_parse_error = 1;
2278 break;
2279 }
2280 }
2281 }
2282
2283 /* If any of these happened, it means we will have to evaluate the conditions
2284 for the location's address on gdb's side. It is no use keeping bytecodes
2285 for all the other duplicate locations, thus we free all of them here.
2286
2287 This is so we have a finer control over which locations' conditions are
2288 being evaluated by GDB or the remote stub. */
2289 if (null_condition_or_parse_error)
2290 {
e0d9a270 2291 for (bp_location *loc : loc_range)
b775012e 2292 {
b775012e
LM
2293 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2294 {
2295 /* Only go as far as the first NULL bytecode is
2296 located. */
2297 if (!loc->cond_bytecode)
2298 return;
2299
833177a4 2300 loc->cond_bytecode.reset ();
b775012e
LM
2301 }
2302 }
2303 }
2304
7f32a4d5
PA
2305 /* No NULL conditions or failed bytecode generation. Build a
2306 condition list for this location's address. If we have software
2307 and hardware locations at the same address, they aren't
2308 considered duplicates, but we still marge all the conditions
2309 anyway, as it's simpler, and doesn't really make a practical
2310 difference. */
e0d9a270
SM
2311 for (bp_location *loc : loc_range)
2312 if (loc->cond
2313 && is_breakpoint (loc->owner)
2314 && loc->pspace->num == bl->pspace->num
2315 && loc->owner->enable_state == bp_enabled
2316 && loc->enabled
2317 && !loc->disabled_by_cond)
2318 {
2319 /* Add the condition to the vector. This will be used later
2320 to send the conditions to the target. */
2321 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2322 }
b775012e
LM
2323
2324 return;
2325}
2326
d3ce09f5
SS
2327/* Parses a command described by string CMD into an agent expression
2328 bytecode suitable for evaluation by the bytecode interpreter.
2329 Return NULL if there was any error during parsing. */
2330
833177a4 2331static agent_expr_up
d3ce09f5
SS
2332parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2333{
bbc13ae3
KS
2334 const char *cmdrest;
2335 const char *format_start, *format_end;
d3ce09f5
SS
2336 struct gdbarch *gdbarch = get_current_arch ();
2337
833177a4 2338 if (cmd == NULL)
d3ce09f5
SS
2339 return NULL;
2340
2341 cmdrest = cmd;
2342
2343 if (*cmdrest == ',')
2344 ++cmdrest;
f1735a53 2345 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2346
2347 if (*cmdrest++ != '"')
2348 error (_("No format string following the location"));
2349
2350 format_start = cmdrest;
2351
8e481c3b 2352 format_pieces fpieces (&cmdrest);
d3ce09f5
SS
2353
2354 format_end = cmdrest;
2355
2356 if (*cmdrest++ != '"')
2357 error (_("Bad format string, non-terminated '\"'."));
2358
f1735a53 2359 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2360
2361 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2362 error (_("Invalid argument syntax"));
2363
2364 if (*cmdrest == ',')
2365 cmdrest++;
f1735a53 2366 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2367
2368 /* For each argument, make an expression. */
2369
8e481c3b 2370 std::vector<struct expression *> argvec;
d3ce09f5
SS
2371 while (*cmdrest != '\0')
2372 {
bbc13ae3 2373 const char *cmd1;
d3ce09f5
SS
2374
2375 cmd1 = cmdrest;
4d01a485 2376 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
8e481c3b 2377 argvec.push_back (expr.release ());
d3ce09f5
SS
2378 cmdrest = cmd1;
2379 if (*cmdrest == ',')
2380 ++cmdrest;
2381 }
2382
833177a4
PA
2383 agent_expr_up aexpr;
2384
d3ce09f5
SS
2385 /* We don't want to stop processing, so catch any errors
2386 that may show up. */
a70b8144 2387 try
d3ce09f5 2388 {
036e657b
JB
2389 aexpr = gen_printf (scope, gdbarch, 0, 0,
2390 format_start, format_end - format_start,
8e481c3b 2391 argvec.size (), argvec.data ());
d3ce09f5 2392 }
230d2906 2393 catch (const gdb_exception_error &ex)
d3ce09f5
SS
2394 {
2395 /* If we got here, it means the command could not be parsed to a valid
2396 bytecode expression and thus can't be evaluated on the target's side.
2397 It's no use iterating through the other commands. */
d3ce09f5 2398 }
492d29ea 2399
d3ce09f5
SS
2400 /* We have a valid agent expression, return it. */
2401 return aexpr;
2402}
2403
2404/* Based on location BL, create a list of breakpoint commands to be
2405 passed on to the target. If we have duplicated locations with
2406 different commands, we will add any such to the list. */
2407
2408static void
2409build_target_command_list (struct bp_location *bl)
2410{
d3ce09f5
SS
2411 int null_command_or_parse_error = 0;
2412 int modified = bl->needs_update;
d3ce09f5 2413
3cde5c42
PA
2414 /* Clear commands left over from a previous insert. */
2415 bl->target_info.tcommands.clear ();
8b4f3082 2416
41fac0cf 2417 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2418 return;
2419
41fac0cf
PA
2420 /* For now, limit to agent-style dprintf breakpoints. */
2421 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2422 return;
2423
e0d9a270
SM
2424 auto loc_range = all_bp_locations_at_addr (bl->address);
2425
7f32a4d5
PA
2426 /* For now, if we have any location at the same address that isn't a
2427 dprintf, don't install the target-side commands, as that would
2428 make the breakpoint not be reported to the core, and we'd lose
41fac0cf 2429 control. */
e0d9a270
SM
2430 for (bp_location *loc : loc_range)
2431 if (is_breakpoint (loc->owner)
2432 && loc->pspace->num == bl->pspace->num
2433 && loc->owner->type != bp_dprintf)
2434 return;
41fac0cf 2435
d3ce09f5
SS
2436 /* Do a first pass to check for locations with no assigned
2437 conditions or conditions that fail to parse to a valid agent expression
2438 bytecode. If any of these happen, then it's no use to send conditions
2439 to the target since this location will always trigger and generate a
2440 response back to GDB. */
e0d9a270 2441 for (bp_location *loc : loc_range)
d3ce09f5 2442 {
d3ce09f5
SS
2443 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2444 {
2445 if (modified)
2446 {
d3ce09f5
SS
2447 /* Re-parse the commands since something changed. In that
2448 case we already freed the command bytecodes (see
2449 force_breakpoint_reinsertion). We just
2450 need to parse the command to bytecodes again. */
833177a4
PA
2451 loc->cmd_bytecode
2452 = parse_cmd_to_aexpr (bl->address,
2453 loc->owner->extra_string);
d3ce09f5
SS
2454 }
2455
2456 /* If we have a NULL bytecode expression, it means something
2457 went wrong or we have a null command expression. */
2458 if (!loc->cmd_bytecode)
2459 {
2460 null_command_or_parse_error = 1;
2461 break;
2462 }
2463 }
2464 }
2465
2466 /* If anything failed, then we're not doing target-side commands,
2467 and so clean up. */
2468 if (null_command_or_parse_error)
2469 {
e0d9a270
SM
2470 for (bp_location *loc : loc_range)
2471 if (is_breakpoint (loc->owner)
2472 && loc->pspace->num == bl->pspace->num)
2473 {
2474 /* Only go as far as the first NULL bytecode is
2475 located. */
2476 if (loc->cmd_bytecode == NULL)
2477 return;
d3ce09f5 2478
e0d9a270
SM
2479 loc->cmd_bytecode.reset ();
2480 }
d3ce09f5
SS
2481 }
2482
7f32a4d5
PA
2483 /* No NULL commands or failed bytecode generation. Build a command
2484 list for all duplicate locations at this location's address.
2485 Note that here we must care for whether the breakpoint location
2486 types are considered duplicates, otherwise, say, if we have a
2487 software and hardware location at the same address, the target
2488 could end up running the commands twice. For the moment, we only
2489 support targets-side commands with dprintf, but it doesn't hurt
2490 to be pedantically correct in case that changes. */
e0d9a270
SM
2491 for (bp_location *loc : loc_range)
2492 if (breakpoint_locations_match (bl, loc)
2493 && loc->owner->extra_string
2494 && is_breakpoint (loc->owner)
2495 && loc->pspace->num == bl->pspace->num
2496 && loc->owner->enable_state == bp_enabled
2497 && loc->enabled
2498 && !loc->disabled_by_cond)
2499 {
2500 /* Add the command to the vector. This will be used later
2501 to send the commands to the target. */
2502 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2503 }
d3ce09f5
SS
2504
2505 bl->target_info.persist = 0;
2506 /* Maybe flag this location as persistent. */
2507 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2508 bl->target_info.persist = 1;
2509}
2510
833b7ab5
YQ
2511/* Return the kind of breakpoint on address *ADDR. Get the kind
2512 of breakpoint according to ADDR except single-step breakpoint.
2513 Get the kind of single-step breakpoint according to the current
2514 registers state. */
cd6c3b4f
YQ
2515
2516static int
2517breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2518{
833b7ab5
YQ
2519 if (bl->owner->type == bp_single_step)
2520 {
2521 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2522 struct regcache *regcache;
2523
00431a78 2524 regcache = get_thread_regcache (thr);
833b7ab5
YQ
2525
2526 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2527 regcache, addr);
2528 }
2529 else
2530 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2531}
2532
35df4500
TJB
2533/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2534 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2535 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2536 Returns 0 for success, 1 if the bp_location type is not supported or
2537 -1 for failure.
879bfdc2 2538
4a64f543
MS
2539 NOTE drow/2003-09-09: This routine could be broken down to an
2540 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2541static int
35df4500 2542insert_bp_location (struct bp_location *bl,
26bb91f3 2543 struct ui_file *tmp_error_stream,
3fbb6ffa 2544 int *disabled_breaks,
dd61ec5c
MW
2545 int *hw_breakpoint_error,
2546 int *hw_bp_error_explained_already)
879bfdc2 2547{
cc06b668 2548 gdb_exception bp_excpt;
879bfdc2 2549
b775012e 2550 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2551 return 0;
2552
35c63cd8
JB
2553 /* Note we don't initialize bl->target_info, as that wipes out
2554 the breakpoint location's shadow_contents if the breakpoint
2555 is still inserted at that location. This in turn breaks
2556 target_read_memory which depends on these buffers when
2557 a memory read is requested at the breakpoint location:
2558 Once the target_info has been wiped, we fail to see that
2559 we have a breakpoint inserted at that address and thus
2560 read the breakpoint instead of returning the data saved in
2561 the breakpoint location's shadow contents. */
0d5ed153 2562 bl->target_info.reqstd_address = bl->address;
35df4500 2563 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2564 bl->target_info.length = bl->length;
8181d85f 2565
b775012e
LM
2566 /* When working with target-side conditions, we must pass all the conditions
2567 for the same breakpoint address down to the target since GDB will not
2568 insert those locations. With a list of breakpoint conditions, the target
2569 can decide when to stop and notify GDB. */
2570
2571 if (is_breakpoint (bl->owner))
2572 {
2573 build_target_condition_list (bl);
d3ce09f5
SS
2574 build_target_command_list (bl);
2575 /* Reset the modification marker. */
b775012e
LM
2576 bl->needs_update = 0;
2577 }
2578
7f32a4d5
PA
2579 /* If "set breakpoint auto-hw" is "on" and a software breakpoint was
2580 set at a read-only address, then a breakpoint location will have
2581 been changed to hardware breakpoint before we get here. If it is
2582 "off" however, error out before actually trying to insert the
2583 breakpoint, with a nicer error message. */
35df4500 2584 if (bl->loc_type == bp_loc_software_breakpoint
7f32a4d5 2585 && !automatic_hardware_breakpoints)
879bfdc2 2586 {
7f32a4d5 2587 mem_region *mr = lookup_mem_region (bl->address);
cc59ec59 2588
7f32a4d5
PA
2589 if (mr != nullptr && mr->attrib.mode != MEM_RW)
2590 {
2591 fprintf_unfiltered (tmp_error_stream,
2592 _("Cannot insert breakpoint %d.\n"
2593 "Cannot set software breakpoint "
2594 "at read-only address %s\n"),
2595 bl->owner->number,
2596 paddress (bl->gdbarch, bl->address));
2597 return 1;
765dc015 2598 }
7f32a4d5
PA
2599 }
2600
2601 if (bl->loc_type == bp_loc_software_breakpoint
2602 || bl->loc_type == bp_loc_hardware_breakpoint)
2603 {
879bfdc2
DJ
2604 /* First check to see if we have to handle an overlay. */
2605 if (overlay_debugging == ovly_off
35df4500
TJB
2606 || bl->section == NULL
2607 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2608 {
2609 /* No overlay handling: just set the breakpoint. */
a70b8144 2610 try
dd61ec5c 2611 {
0000e5cc
PA
2612 int val;
2613
dd61ec5c 2614 val = bl->owner->ops->insert_location (bl);
0000e5cc 2615 if (val)
688fca4f 2616 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2617 }
94aeb44b 2618 catch (gdb_exception &e)
dd61ec5c 2619 {
94aeb44b 2620 bp_excpt = std::move (e);
dd61ec5c 2621 }
879bfdc2
DJ
2622 }
2623 else
2624 {
4a64f543 2625 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2626 Shall we set a breakpoint at the LMA? */
2627 if (!overlay_events_enabled)
2628 {
2629 /* Yes -- overlay event support is not active,
2630 so we must try to set a breakpoint at the LMA.
2631 This will not work for a hardware breakpoint. */
35df4500 2632 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2633 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2634 bl->owner->number);
879bfdc2
DJ
2635 else
2636 {
35df4500
TJB
2637 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2638 bl->section);
879bfdc2 2639 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2640 bl->overlay_target_info = bl->target_info;
0d5ed153 2641 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2642
2643 /* No overlay handling: just set the breakpoint. */
a70b8144 2644 try
0000e5cc
PA
2645 {
2646 int val;
2647
579c6ad9 2648 bl->overlay_target_info.kind
cd6c3b4f
YQ
2649 = breakpoint_kind (bl, &addr);
2650 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2651 val = target_insert_breakpoint (bl->gdbarch,
2652 &bl->overlay_target_info);
2653 if (val)
688fca4f
PA
2654 bp_excpt
2655 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2656 }
94aeb44b 2657 catch (gdb_exception &e)
0000e5cc 2658 {
94aeb44b 2659 bp_excpt = std::move (e);
0000e5cc
PA
2660 }
2661
688fca4f 2662 if (bp_excpt.reason != 0)
99361f52 2663 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2664 "Overlay breakpoint %d "
2665 "failed: in ROM?\n",
35df4500 2666 bl->owner->number);
879bfdc2
DJ
2667 }
2668 }
2669 /* Shall we set a breakpoint at the VMA? */
35df4500 2670 if (section_is_mapped (bl->section))
879bfdc2
DJ
2671 {
2672 /* Yes. This overlay section is mapped into memory. */
a70b8144 2673 try
dda83cd7 2674 {
0000e5cc
PA
2675 int val;
2676
dda83cd7 2677 val = bl->owner->ops->insert_location (bl);
0000e5cc 2678 if (val)
688fca4f 2679 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dda83cd7 2680 }
94aeb44b 2681 catch (gdb_exception &e)
dda83cd7 2682 {
94aeb44b 2683 bp_excpt = std::move (e);
dda83cd7 2684 }
879bfdc2
DJ
2685 }
2686 else
2687 {
2688 /* No. This breakpoint will not be inserted.
2689 No error, but do not mark the bp as 'inserted'. */
2690 return 0;
2691 }
2692 }
2693
688fca4f 2694 if (bp_excpt.reason != 0)
879bfdc2
DJ
2695 {
2696 /* Can't set the breakpoint. */
0000e5cc
PA
2697
2698 /* In some cases, we might not be able to insert a
2699 breakpoint in a shared library that has already been
2700 removed, but we have not yet processed the shlib unload
2701 event. Unfortunately, some targets that implement
076855f9
PA
2702 breakpoint insertion themselves can't tell why the
2703 breakpoint insertion failed (e.g., the remote target
2704 doesn't define error codes), so we must treat generic
2705 errors as memory errors. */
688fca4f
PA
2706 if (bp_excpt.reason == RETURN_ERROR
2707 && (bp_excpt.error == GENERIC_ERROR
2708 || bp_excpt.error == MEMORY_ERROR)
076855f9 2709 && bl->loc_type == bp_loc_software_breakpoint
08351840 2710 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2711 || shared_objfile_contains_address_p (bl->pspace,
2712 bl->address)))
879bfdc2 2713 {
4a64f543 2714 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2715 bl->shlib_disabled = 1;
76727919 2716 gdb::observers::breakpoint_modified.notify (bl->owner);
3fbb6ffa
TJB
2717 if (!*disabled_breaks)
2718 {
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert breakpoint %d.\n",
2721 bl->owner->number);
2722 fprintf_unfiltered (tmp_error_stream,
2723 "Temporarily disabling shared "
2724 "library breakpoints:\n");
2725 }
2726 *disabled_breaks = 1;
879bfdc2 2727 fprintf_unfiltered (tmp_error_stream,
35df4500 2728 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2729 return 0;
879bfdc2
DJ
2730 }
2731 else
879bfdc2 2732 {
35df4500 2733 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2734 {
0000e5cc 2735 *hw_breakpoint_error = 1;
688fca4f 2736 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dda83cd7
SM
2737 fprintf_unfiltered (tmp_error_stream,
2738 "Cannot insert hardware breakpoint %d%s",
2739 bl->owner->number,
688fca4f 2740 bp_excpt.message ? ":" : ".\n");
dda83cd7
SM
2741 if (bp_excpt.message != NULL)
2742 fprintf_unfiltered (tmp_error_stream, "%s.\n",
3d6e9d23 2743 bp_excpt.what ());
879bfdc2
DJ
2744 }
2745 else
2746 {
688fca4f 2747 if (bp_excpt.message == NULL)
0000e5cc 2748 {
1ccbe998 2749 std::string message
0000e5cc
PA
2750 = memory_error_message (TARGET_XFER_E_IO,
2751 bl->gdbarch, bl->address);
0000e5cc
PA
2752
2753 fprintf_unfiltered (tmp_error_stream,
2754 "Cannot insert breakpoint %d.\n"
2755 "%s\n",
1ccbe998 2756 bl->owner->number, message.c_str ());
0000e5cc
PA
2757 }
2758 else
2759 {
2760 fprintf_unfiltered (tmp_error_stream,
2761 "Cannot insert breakpoint %d: %s\n",
2762 bl->owner->number,
3d6e9d23 2763 bp_excpt.what ());
0000e5cc 2764 }
879bfdc2 2765 }
0000e5cc 2766 return 1;
879bfdc2
DJ
2767
2768 }
2769 }
2770 else
35df4500 2771 bl->inserted = 1;
879bfdc2 2772
0000e5cc 2773 return 0;
879bfdc2
DJ
2774 }
2775
35df4500 2776 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2777 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2778 watchpoints. It's not clear that it's necessary... */
35df4500 2779 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2780 {
0000e5cc
PA
2781 int val;
2782
77b06cd7
TJB
2783 gdb_assert (bl->owner->ops != NULL
2784 && bl->owner->ops->insert_location != NULL);
2785
2786 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2787
2788 /* If trying to set a read-watchpoint, and it turns out it's not
2789 supported, try emulating one with an access watchpoint. */
35df4500 2790 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8 2791 {
85d721b8
PA
2792 /* But don't try to insert it, if there's already another
2793 hw_access location that would be considered a duplicate
2794 of this one. */
48d7020b 2795 for (bp_location *loc : all_bp_locations ())
35df4500 2796 if (loc != bl
85d721b8 2797 && loc->watchpoint_type == hw_access
35df4500 2798 && watchpoint_locations_match (bl, loc))
85d721b8 2799 {
35df4500
TJB
2800 bl->duplicate = 1;
2801 bl->inserted = 1;
2802 bl->target_info = loc->target_info;
2803 bl->watchpoint_type = hw_access;
85d721b8
PA
2804 val = 0;
2805 break;
2806 }
2807
2808 if (val == 1)
2809 {
77b06cd7
TJB
2810 bl->watchpoint_type = hw_access;
2811 val = bl->owner->ops->insert_location (bl);
2812
2813 if (val)
2814 /* Back to the original value. */
2815 bl->watchpoint_type = hw_read;
85d721b8
PA
2816 }
2817 }
2818
35df4500 2819 bl->inserted = (val == 0);
879bfdc2
DJ
2820 }
2821
35df4500 2822 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2823 {
0000e5cc
PA
2824 int val;
2825
77b06cd7
TJB
2826 gdb_assert (bl->owner->ops != NULL
2827 && bl->owner->ops->insert_location != NULL);
2828
2829 val = bl->owner->ops->insert_location (bl);
2830 if (val)
2831 {
2832 bl->owner->enable_state = bp_disabled;
2833
2834 if (val == 1)
2835 warning (_("\
2836Error inserting catchpoint %d: Your system does not support this type\n\
2837of catchpoint."), bl->owner->number);
2838 else
2839 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2840 }
2841
2842 bl->inserted = (val == 0);
1640b821
DJ
2843
2844 /* We've already printed an error message if there was a problem
2845 inserting this catchpoint, and we've disabled the catchpoint,
2846 so just return success. */
2847 return 0;
879bfdc2
DJ
2848 }
2849
2850 return 0;
2851}
2852
6c95b8df
PA
2853/* This function is called when program space PSPACE is about to be
2854 deleted. It takes care of updating breakpoints to not reference
2855 PSPACE anymore. */
2856
2857void
2858breakpoint_program_space_exit (struct program_space *pspace)
2859{
6c95b8df 2860 /* Remove any breakpoint that was set through this program space. */
1428b37a
SM
2861 for (breakpoint *b : all_breakpoints_safe ())
2862 if (b->pspace == pspace)
2863 delete_breakpoint (b);
6c95b8df
PA
2864
2865 /* Breakpoints set through other program spaces could have locations
2866 bound to PSPACE as well. Remove those. */
48d7020b 2867 for (bp_location *loc : all_bp_locations ())
6c95b8df
PA
2868 {
2869 struct bp_location *tmp;
2870
2871 if (loc->pspace == pspace)
2872 {
2bdf28a0 2873 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2874 if (loc->owner->loc == loc)
2875 loc->owner->loc = loc->next;
2876 else
2877 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2878 if (tmp->next == loc)
2879 {
2880 tmp->next = loc->next;
2881 break;
2882 }
2883 }
2884 }
2885
2886 /* Now update the global location list to permanently delete the
2887 removed locations above. */
44702360 2888 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2889}
2890
74960c60
VP
2891/* Make sure all breakpoints are inserted in inferior.
2892 Throws exception on any error.
2893 A breakpoint that is already inserted won't be inserted
2894 again, so calling this function twice is safe. */
2895void
2896insert_breakpoints (void)
2897{
43892fdf 2898 for (breakpoint *bpt : all_breakpoints ())
74960c60 2899 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2900 {
2901 struct watchpoint *w = (struct watchpoint *) bpt;
2902
2903 update_watchpoint (w, 0 /* don't reparse. */);
2904 }
74960c60 2905
04086b45
PA
2906 /* Updating watchpoints creates new locations, so update the global
2907 location list. Explicitly tell ugll to insert locations and
7f32a4d5
PA
2908 ignore breakpoints_always_inserted_mode. Also,
2909 update_global_location_list tries to "upgrade" software
2910 breakpoints to hardware breakpoints to handle "set breakpoint
2911 auto-hw", so we need to call it even if we don't have new
2912 locations. */
04086b45 2913 update_global_location_list (UGLL_INSERT);
74960c60
VP
2914}
2915
20388dd6
YQ
2916/* Invoke CALLBACK for each of bp_location. */
2917
2918void
0406545d 2919iterate_over_bp_locations (gdb::function_view<void (bp_location *)> callback)
20388dd6 2920{
48d7020b
SM
2921 for (bp_location *loc : all_bp_locations ())
2922 callback (loc);
20388dd6
YQ
2923}
2924
b775012e
LM
2925/* This is used when we need to synch breakpoint conditions between GDB and the
2926 target. It is the case with deleting and disabling of breakpoints when using
2927 always-inserted mode. */
2928
2929static void
2930update_inserted_breakpoint_locations (void)
2931{
b775012e
LM
2932 int error_flag = 0;
2933 int val = 0;
2934 int disabled_breaks = 0;
2935 int hw_breakpoint_error = 0;
dd61ec5c 2936 int hw_bp_details_reported = 0;
b775012e 2937
d7e74731 2938 string_file tmp_error_stream;
b775012e
LM
2939
2940 /* Explicitly mark the warning -- this will only be printed if
2941 there was an error. */
d7e74731 2942 tmp_error_stream.puts ("Warning:\n");
b775012e 2943
5ed8105e 2944 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e 2945
48d7020b 2946 for (bp_location *bl : all_bp_locations ())
b775012e
LM
2947 {
2948 /* We only want to update software breakpoints and hardware
2949 breakpoints. */
2950 if (!is_breakpoint (bl->owner))
2951 continue;
2952
2953 /* We only want to update locations that are already inserted
2954 and need updating. This is to avoid unwanted insertion during
2955 deletion of breakpoints. */
4daf1902 2956 if (!bl->inserted || !bl->needs_update)
b775012e
LM
2957 continue;
2958
2959 switch_to_program_space_and_thread (bl->pspace);
2960
2961 /* For targets that support global breakpoints, there's no need
2962 to select an inferior to insert breakpoint to. In fact, even
2963 if we aren't attached to any process yet, we should still
2964 insert breakpoints. */
f5656ead 2965 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 2966 && (inferior_ptid == null_ptid || !target_has_execution ()))
b775012e
LM
2967 continue;
2968
d7e74731 2969 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2970 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2971 if (val)
2972 error_flag = val;
2973 }
2974
2975 if (error_flag)
2976 {
223ffa71 2977 target_terminal::ours_for_output ();
b775012e
LM
2978 error_stream (tmp_error_stream);
2979 }
b775012e
LM
2980}
2981
c30eee59 2982/* Used when starting or continuing the program. */
c906108c 2983
74960c60
VP
2984static void
2985insert_breakpoint_locations (void)
c906108c 2986{
eacd795a 2987 int error_flag = 0;
c906108c 2988 int val = 0;
3fbb6ffa 2989 int disabled_breaks = 0;
81d0cc19 2990 int hw_breakpoint_error = 0;
dd61ec5c 2991 int hw_bp_error_explained_already = 0;
c906108c 2992
d7e74731
PA
2993 string_file tmp_error_stream;
2994
81d0cc19
GS
2995 /* Explicitly mark the warning -- this will only be printed if
2996 there was an error. */
d7e74731 2997 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2998
5ed8105e 2999 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3000
48d7020b 3001 for (bp_location *bl : all_bp_locations ())
879bfdc2 3002 {
b775012e 3003 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3004 continue;
3005
4a64f543
MS
3006 /* There is no point inserting thread-specific breakpoints if
3007 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3008 has BL->OWNER always non-NULL. */
35df4500 3009 if (bl->owner->thread != -1
5d5658a1 3010 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3011 continue;
3012
35df4500 3013 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3014
3015 /* For targets that support global breakpoints, there's no need
3016 to select an inferior to insert breakpoint to. In fact, even
3017 if we aren't attached to any process yet, we should still
3018 insert breakpoints. */
f5656ead 3019 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
55f6301a 3020 && (inferior_ptid == null_ptid || !target_has_execution ()))
6c95b8df
PA
3021 continue;
3022
d7e74731 3023 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3024 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3025 if (val)
eacd795a 3026 error_flag = val;
879bfdc2 3027 }
c906108c 3028
4a64f543
MS
3029 /* If we failed to insert all locations of a watchpoint, remove
3030 them, as half-inserted watchpoint is of limited use. */
43892fdf 3031 for (breakpoint *bpt : all_breakpoints ())
a5606eee
VP
3032 {
3033 int some_failed = 0;
a5606eee
VP
3034
3035 if (!is_hardware_watchpoint (bpt))
3036 continue;
3037
d6b74ac4 3038 if (!breakpoint_enabled (bpt))
a5606eee 3039 continue;
74960c60
VP
3040
3041 if (bpt->disposition == disp_del_at_next_stop)
3042 continue;
a5606eee 3043
40cb8ca5 3044 for (bp_location *loc : bpt->locations ())
56710373 3045 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3046 {
3047 some_failed = 1;
3048 break;
3049 }
40cb8ca5 3050
a5606eee
VP
3051 if (some_failed)
3052 {
40cb8ca5 3053 for (bp_location *loc : bpt->locations ())
a5606eee 3054 if (loc->inserted)
834c0d03 3055 remove_breakpoint (loc);
a5606eee
VP
3056
3057 hw_breakpoint_error = 1;
d7e74731
PA
3058 tmp_error_stream.printf ("Could not insert "
3059 "hardware watchpoint %d.\n",
3060 bpt->number);
eacd795a 3061 error_flag = -1;
a5606eee
VP
3062 }
3063 }
3064
eacd795a 3065 if (error_flag)
81d0cc19
GS
3066 {
3067 /* If a hardware breakpoint or watchpoint was inserted, add a
dda83cd7 3068 message about possibly exhausted resources. */
dd61ec5c 3069 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3070 {
d7e74731 3071 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3072You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3073 }
223ffa71 3074 target_terminal::ours_for_output ();
81d0cc19
GS
3075 error_stream (tmp_error_stream);
3076 }
c906108c
SS
3077}
3078
c30eee59
TJB
3079/* Used when the program stops.
3080 Returns zero if successful, or non-zero if there was a problem
3081 removing a breakpoint location. */
3082
c906108c 3083int
fba45db2 3084remove_breakpoints (void)
c906108c 3085{
3a1bae8e 3086 int val = 0;
c906108c 3087
48d7020b 3088 for (bp_location *bl : all_bp_locations ())
c5aa993b 3089 {
1e4d1764 3090 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3091 val |= remove_breakpoint (bl);
c5aa993b 3092 }
3a1bae8e 3093 return val;
c906108c
SS
3094}
3095
49fa26b0
PA
3096/* When a thread exits, remove breakpoints that are related to
3097 that thread. */
3098
3099static void
3100remove_threaded_breakpoints (struct thread_info *tp, int silent)
3101{
1428b37a 3102 for (breakpoint *b : all_breakpoints_safe ())
49fa26b0 3103 {
5d5658a1 3104 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3105 {
3106 b->disposition = disp_del_at_next_stop;
3107
3108 printf_filtered (_("\
43792cf0
PA
3109Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3110 b->number, print_thread_id (tp));
49fa26b0
PA
3111
3112 /* Hide it from the user. */
3113 b->number = 0;
3114 }
3115 }
3116}
3117
f3869b1a 3118/* See breakpoint.h. */
6c95b8df 3119
f3869b1a 3120void
00431a78 3121remove_breakpoints_inf (inferior *inf)
6c95b8df 3122{
6c95b8df 3123 int val;
6c95b8df 3124
48d7020b 3125 for (bp_location *bl : all_bp_locations ())
6c95b8df 3126 {
35df4500 3127 if (bl->pspace != inf->pspace)
6c95b8df
PA
3128 continue;
3129
fc126975 3130 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3131 {
834c0d03 3132 val = remove_breakpoint (bl);
6c95b8df 3133 if (val != 0)
f3869b1a 3134 return;
6c95b8df
PA
3135 }
3136 }
6c95b8df
PA
3137}
3138
e58b0e63
PA
3139static int internal_breakpoint_number = -1;
3140
84f4c1fe
PM
3141/* Set the breakpoint number of B, depending on the value of INTERNAL.
3142 If INTERNAL is non-zero, the breakpoint number will be populated
3143 from internal_breakpoint_number and that variable decremented.
e5dd4106 3144 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3145 breakpoint_count and that value incremented. Internal breakpoints
3146 do not set the internal var bpnum. */
3147static void
3148set_breakpoint_number (int internal, struct breakpoint *b)
3149{
3150 if (internal)
3151 b->number = internal_breakpoint_number--;
3152 else
3153 {
3154 set_breakpoint_count (breakpoint_count + 1);
3155 b->number = breakpoint_count;
3156 }
3157}
3158
e62c965a 3159static struct breakpoint *
a6d9a66e 3160create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3161 CORE_ADDR address, enum bptype type,
c0a91b2b 3162 const struct breakpoint_ops *ops)
e62c965a 3163{
51abb421 3164 symtab_and_line sal;
e62c965a
PP
3165 sal.pc = address;
3166 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3167 sal.pspace = current_program_space;
e62c965a 3168
51abb421 3169 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3170 b->number = internal_breakpoint_number--;
3171 b->disposition = disp_donttouch;
3172
3173 return b;
3174}
3175
17450429
PP
3176static const char *const longjmp_names[] =
3177 {
3178 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3179 };
3180#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3181
3182/* Per-objfile data private to breakpoint.c. */
3183struct breakpoint_objfile_data
3184{
3185 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3186 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3187
3188 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3189 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3190
28106bc2 3191 /* True if we have looked for longjmp probes. */
43dce439 3192 int longjmp_searched = 0;
28106bc2 3193
45461e0d
SM
3194 /* SystemTap probe points for longjmp (if any). These are non-owning
3195 references. */
3196 std::vector<probe *> longjmp_probes;
28106bc2 3197
17450429 3198 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3199 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3200
3201 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3202 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3203
3204 /* True if we have looked for exception probes. */
43dce439 3205 int exception_searched = 0;
28106bc2 3206
45461e0d
SM
3207 /* SystemTap probe points for unwinding (if any). These are non-owning
3208 references. */
3209 std::vector<probe *> exception_probes;
17450429
PP
3210};
3211
51d3063a
TT
3212static const struct objfile_key<breakpoint_objfile_data>
3213 breakpoint_objfile_key;
17450429
PP
3214
3215/* Minimal symbol not found sentinel. */
3216static struct minimal_symbol msym_not_found;
3217
3218/* Returns TRUE if MSYM point to the "not found" sentinel. */
3219
3220static int
3221msym_not_found_p (const struct minimal_symbol *msym)
3222{
3223 return msym == &msym_not_found;
3224}
3225
3226/* Return per-objfile data needed by breakpoint.c.
3227 Allocate the data if necessary. */
3228
3229static struct breakpoint_objfile_data *
3230get_breakpoint_objfile_data (struct objfile *objfile)
3231{
3232 struct breakpoint_objfile_data *bp_objfile_data;
3233
51d3063a 3234 bp_objfile_data = breakpoint_objfile_key.get (objfile);
17450429 3235 if (bp_objfile_data == NULL)
51d3063a 3236 bp_objfile_data = breakpoint_objfile_key.emplace (objfile);
17450429
PP
3237 return bp_objfile_data;
3238}
3239
e62c965a 3240static void
af02033e 3241create_overlay_event_breakpoint (void)
e62c965a 3242{
af02033e 3243 const char *const func_name = "_ovly_debug_event";
e62c965a 3244
2030c079 3245 for (objfile *objfile : current_program_space->objfiles ())
69de3c6a
PP
3246 {
3247 struct breakpoint *b;
17450429
PP
3248 struct breakpoint_objfile_data *bp_objfile_data;
3249 CORE_ADDR addr;
67994074 3250 struct explicit_location explicit_loc;
69de3c6a 3251
17450429
PP
3252 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3253
3b7344d5 3254 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3255 continue;
3256
3b7344d5 3257 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3258 {
3b7344d5 3259 struct bound_minimal_symbol m;
17450429
PP
3260
3261 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3262 if (m.minsym == NULL)
17450429
PP
3263 {
3264 /* Avoid future lookups in this objfile. */
3b7344d5 3265 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3266 continue;
3267 }
3268 bp_objfile_data->overlay_msym = m;
3269 }
e62c965a 3270
77e371c0 3271 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
08feed99 3272 b = create_internal_breakpoint (objfile->arch (), addr,
dda83cd7 3273 bp_overlay_event,
06edf0c0 3274 &internal_breakpoint_ops);
67994074
KS
3275 initialize_explicit_location (&explicit_loc);
3276 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3277 b->location = new_explicit_location (&explicit_loc);
e62c965a 3278
69de3c6a 3279 if (overlay_debugging == ovly_auto)
dda83cd7
SM
3280 {
3281 b->enable_state = bp_enabled;
3282 overlay_events_enabled = 1;
3283 }
69de3c6a
PP
3284 else
3285 {
dda83cd7
SM
3286 b->enable_state = bp_disabled;
3287 overlay_events_enabled = 0;
69de3c6a 3288 }
e62c965a 3289 }
e62c965a
PP
3290}
3291
2a7f6487
TV
3292/* Install a master longjmp breakpoint for OBJFILE using a probe. Return
3293 true if a breakpoint was installed. */
3294
3295static bool
3296create_longjmp_master_breakpoint_probe (objfile *objfile)
0fd8e87f 3297{
2a7f6487
TV
3298 struct gdbarch *gdbarch = objfile->arch ();
3299 struct breakpoint_objfile_data *bp_objfile_data
3300 = get_breakpoint_objfile_data (objfile);
0fd8e87f 3301
2a7f6487 3302 if (!bp_objfile_data->longjmp_searched)
94c93c35 3303 {
2a7f6487
TV
3304 std::vector<probe *> ret
3305 = find_probes_in_objfile (objfile, "libc", "longjmp");
af02033e 3306
2a7f6487 3307 if (!ret.empty ())
94c93c35 3308 {
2a7f6487
TV
3309 /* We are only interested in checking one element. */
3310 probe *p = ret[0];
0fd8e87f 3311
2a7f6487
TV
3312 if (!p->can_evaluate_arguments ())
3313 {
3314 /* We cannot use the probe interface here,
3315 because it does not know how to evaluate
3316 arguments. */
3317 ret.clear ();
3318 }
3319 }
3320 bp_objfile_data->longjmp_probes = ret;
3321 bp_objfile_data->longjmp_searched = 1;
3322 }
0fd8e87f 3323
2a7f6487
TV
3324 if (bp_objfile_data->longjmp_probes.empty ())
3325 return false;
17450429 3326
2a7f6487
TV
3327 for (probe *p : bp_objfile_data->longjmp_probes)
3328 {
3329 struct breakpoint *b;
25f9533e 3330
2a7f6487
TV
3331 b = create_internal_breakpoint (gdbarch,
3332 p->get_relocated_address (objfile),
3333 bp_longjmp_master,
3334 &internal_breakpoint_ops);
3335 b->location = new_probe_location ("-probe-stap libc:longjmp");
3336 b->enable_state = bp_disabled;
3337 }
aed57c53 3338
2a7f6487
TV
3339 return true;
3340}
25f9533e 3341
2a7f6487
TV
3342/* Install master longjmp breakpoints for OBJFILE using longjmp_names.
3343 Return true if at least one breakpoint was installed. */
3344
3345static bool
3346create_longjmp_master_breakpoint_names (objfile *objfile)
3347{
3348 struct gdbarch *gdbarch = objfile->arch ();
3349 if (!gdbarch_get_longjmp_target_p (gdbarch))
3350 return false;
3351
3352 struct breakpoint_objfile_data *bp_objfile_data
3353 = get_breakpoint_objfile_data (objfile);
3354 unsigned int installed_bp = 0;
3355
3356 for (int i = 0; i < NUM_LONGJMP_NAMES; i++)
3357 {
3358 struct breakpoint *b;
3359 const char *func_name;
3360 CORE_ADDR addr;
3361 struct explicit_location explicit_loc;
3362
3363 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3364 continue;
28106bc2 3365
2a7f6487
TV
3366 func_name = longjmp_names[i];
3367 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3368 {
3369 struct bound_minimal_symbol m;
3370
3371 m = lookup_minimal_symbol_text (func_name, objfile);
3372 if (m.minsym == NULL)
3373 {
3374 /* Prevent future lookups in this objfile. */
3375 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
94c93c35
TT
3376 continue;
3377 }
2a7f6487
TV
3378 bp_objfile_data->longjmp_msym[i] = m;
3379 }
28106bc2 3380
2a7f6487
TV
3381 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3382 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3383 &internal_breakpoint_ops);
3384 initialize_explicit_location (&explicit_loc);
3385 explicit_loc.function_name = ASTRDUP (func_name);
3386 b->location = new_explicit_location (&explicit_loc);
3387 b->enable_state = bp_disabled;
3388 installed_bp++;
3389 }
28106bc2 3390
2a7f6487
TV
3391 return installed_bp > 0;
3392}
0fd8e87f 3393
2a7f6487 3394/* Create a master longjmp breakpoint. */
17450429 3395
2a7f6487
TV
3396static void
3397create_longjmp_master_breakpoint (void)
3398{
3399 scoped_restore_current_program_space restore_pspace;
aed57c53 3400
2a7f6487
TV
3401 for (struct program_space *pspace : program_spaces)
3402 {
3403 set_current_program_space (pspace);
17450429 3404
2a7f6487
TV
3405 for (objfile *obj : current_program_space->objfiles ())
3406 {
3407 /* Skip separate debug object, it's handled in the loop below. */
3408 if (obj->separate_debug_objfile_backlink != nullptr)
3409 continue;
3410
3411 /* Try a probe kind breakpoint on main objfile. */
3412 if (create_longjmp_master_breakpoint_probe (obj))
3413 continue;
3414
3415 /* Try longjmp_names kind breakpoints on main and separate_debug
3416 objfiles. */
3417 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3418 if (create_longjmp_master_breakpoint_names (debug_objfile))
3419 break;
94c93c35
TT
3420 }
3421 }
0fd8e87f
UW
3422}
3423
af02033e 3424/* Create a master std::terminate breakpoint. */
aa7d318d 3425static void
af02033e 3426create_std_terminate_master_breakpoint (void)
aa7d318d 3427{
af02033e 3428 const char *const func_name = "std::terminate()";
aa7d318d 3429
5ed8105e 3430 scoped_restore_current_program_space restore_pspace;
aa7d318d 3431
94c93c35
TT
3432 for (struct program_space *pspace : program_spaces)
3433 {
3434 CORE_ADDR addr;
17450429 3435
94c93c35 3436 set_current_program_space (pspace);
17450429 3437
94c93c35
TT
3438 for (objfile *objfile : current_program_space->objfiles ())
3439 {
3440 struct breakpoint *b;
3441 struct breakpoint_objfile_data *bp_objfile_data;
3442 struct explicit_location explicit_loc;
aa7d318d 3443
94c93c35 3444 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3445
94c93c35
TT
3446 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3447 continue;
17450429 3448
94c93c35
TT
3449 if (bp_objfile_data->terminate_msym.minsym == NULL)
3450 {
3451 struct bound_minimal_symbol m;
17450429 3452
94c93c35
TT
3453 m = lookup_minimal_symbol (func_name, NULL, objfile);
3454 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3455 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3456 {
3457 /* Prevent future lookups in this objfile. */
3458 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3459 continue;
3460 }
3461 bp_objfile_data->terminate_msym = m;
3462 }
aa7d318d 3463
94c93c35
TT
3464 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3465 b = create_internal_breakpoint (objfile->arch (), addr,
3466 bp_std_terminate_master,
3467 &internal_breakpoint_ops);
3468 initialize_explicit_location (&explicit_loc);
3469 explicit_loc.function_name = ASTRDUP (func_name);
3470 b->location = new_explicit_location (&explicit_loc);
3471 b->enable_state = bp_disabled;
3472 }
3473 }
aa7d318d
TT
3474}
3475
1940319c
TV
3476/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using a
3477 probe. Return true if a breakpoint was installed. */
186c406b 3478
1940319c
TV
3479static bool
3480create_exception_master_breakpoint_probe (objfile *objfile)
186c406b 3481{
1940319c
TV
3482 struct breakpoint *b;
3483 struct gdbarch *gdbarch;
3484 struct breakpoint_objfile_data *bp_objfile_data;
186c406b 3485
1940319c 3486 bp_objfile_data = get_breakpoint_objfile_data (objfile);
17450429 3487
1940319c
TV
3488 /* We prefer the SystemTap probe point if it exists. */
3489 if (!bp_objfile_data->exception_searched)
3490 {
3491 std::vector<probe *> ret
3492 = find_probes_in_objfile (objfile, "libgcc", "unwind");
17450429 3493
1940319c 3494 if (!ret.empty ())
28106bc2 3495 {
1940319c
TV
3496 /* We are only interested in checking one element. */
3497 probe *p = ret[0];
25f9533e 3498
1940319c 3499 if (!p->can_evaluate_arguments ())
25f9533e 3500 {
1940319c
TV
3501 /* We cannot use the probe interface here, because it does
3502 not know how to evaluate arguments. */
3503 ret.clear ();
25f9533e 3504 }
28106bc2 3505 }
1940319c
TV
3506 bp_objfile_data->exception_probes = ret;
3507 bp_objfile_data->exception_searched = 1;
3508 }
28106bc2 3509
1940319c
TV
3510 if (bp_objfile_data->exception_probes.empty ())
3511 return false;
45461e0d 3512
1940319c 3513 gdbarch = objfile->arch ();
28106bc2 3514
1940319c
TV
3515 for (probe *p : bp_objfile_data->exception_probes)
3516 {
3517 b = create_internal_breakpoint (gdbarch,
3518 p->get_relocated_address (objfile),
3519 bp_exception_master,
3520 &internal_breakpoint_ops);
3521 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3522 b->enable_state = bp_disabled;
3523 }
28106bc2 3524
1940319c
TV
3525 return true;
3526}
28106bc2 3527
1940319c
TV
3528/* Install a master breakpoint on the unwinder's debug hook for OBJFILE using
3529 _Unwind_DebugHook. Return true if a breakpoint was installed. */
17450429 3530
1940319c
TV
3531static bool
3532create_exception_master_breakpoint_hook (objfile *objfile)
3533{
3534 const char *const func_name = "_Unwind_DebugHook";
3535 struct breakpoint *b;
3536 struct gdbarch *gdbarch;
3537 struct breakpoint_objfile_data *bp_objfile_data;
3538 CORE_ADDR addr;
3539 struct explicit_location explicit_loc;
186c406b 3540
1940319c 3541 bp_objfile_data = get_breakpoint_objfile_data (objfile);
186c406b 3542
1940319c
TV
3543 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3544 return false;
17450429 3545
1940319c
TV
3546 gdbarch = objfile->arch ();
3547
3548 if (bp_objfile_data->exception_msym.minsym == NULL)
3549 {
3550 struct bound_minimal_symbol debug_hook;
3551
3552 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3553 if (debug_hook.minsym == NULL)
3554 {
3555 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3556 return false;
186c406b 3557 }
17450429 3558
1940319c
TV
3559 bp_objfile_data->exception_msym = debug_hook;
3560 }
3561
3562 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
328d42d8
SM
3563 addr = gdbarch_convert_from_func_ptr_addr
3564 (gdbarch, addr, current_inferior ()->top_target ());
1940319c
TV
3565 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3566 &internal_breakpoint_ops);
3567 initialize_explicit_location (&explicit_loc);
3568 explicit_loc.function_name = ASTRDUP (func_name);
3569 b->location = new_explicit_location (&explicit_loc);
3570 b->enable_state = bp_disabled;
3571
3572 return true;
3573}
3574
3575/* Install a master breakpoint on the unwinder's debug hook. */
3576
3577static void
3578create_exception_master_breakpoint (void)
3579{
3580 for (objfile *obj : current_program_space->objfiles ())
3581 {
3582 /* Skip separate debug object. */
3583 if (obj->separate_debug_objfile_backlink)
3584 continue;
3585
3586 /* Try a probe kind breakpoint. */
3587 if (create_exception_master_breakpoint_probe (obj))
3588 continue;
3589
7c6944ab
TV
3590 /* Iterate over main and separate debug objects and try an
3591 _Unwind_DebugHook kind breakpoint. */
3592 for (objfile *debug_objfile : obj->separate_debug_objfiles ())
3593 if (create_exception_master_breakpoint_hook (debug_objfile))
1940319c 3594 break;
186c406b 3595 }
186c406b
TT
3596}
3597
9ef9e6a6
KS
3598/* Does B have a location spec? */
3599
3600static int
3601breakpoint_event_location_empty_p (const struct breakpoint *b)
3602{
d28cd78a 3603 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3604}
3605
c906108c 3606void
fba45db2 3607update_breakpoints_after_exec (void)
c906108c 3608{
25b22b0a
PA
3609 /* We're about to delete breakpoints from GDB's lists. If the
3610 INSERTED flag is true, GDB will try to lift the breakpoints by
3611 writing the breakpoints' "shadow contents" back into memory. The
3612 "shadow contents" are NOT valid after an exec, so GDB should not
3613 do that. Instead, the target is responsible from marking
3614 breakpoints out as soon as it detects an exec. We don't do that
3615 here instead, because there may be other attempts to delete
3616 breakpoints after detecting an exec and before reaching here. */
48d7020b 3617 for (bp_location *bploc : all_bp_locations ())
6c95b8df
PA
3618 if (bploc->pspace == current_program_space)
3619 gdb_assert (!bploc->inserted);
c906108c 3620
1428b37a 3621 for (breakpoint *b : all_breakpoints_safe ())
c5aa993b 3622 {
6c95b8df
PA
3623 if (b->pspace != current_program_space)
3624 continue;
3625
4a64f543 3626 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3627 if (b->type == bp_shlib_event)
3628 {
3629 delete_breakpoint (b);
3630 continue;
3631 }
c906108c 3632
4a64f543 3633 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3634 if (b->type == bp_jit_event)
3635 {
3636 delete_breakpoint (b);
3637 continue;
3638 }
3639
1900040c 3640 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3641 as must overlay event and longjmp master breakpoints. */
3642 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3643 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3644 || b->type == bp_exception_master)
c4093a6a
JM
3645 {
3646 delete_breakpoint (b);
3647 continue;
3648 }
3649
4a64f543 3650 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3651 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3652 {
3653 delete_breakpoint (b);
3654 continue;
3655 }
3656
7c16b83e
PA
3657 /* Just like single-step breakpoints. */
3658 if (b->type == bp_single_step)
3659 {
3660 delete_breakpoint (b);
3661 continue;
3662 }
3663
611c83ae
PA
3664 /* Longjmp and longjmp-resume breakpoints are also meaningless
3665 after an exec. */
186c406b 3666 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3667 || b->type == bp_longjmp_call_dummy
186c406b 3668 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3669 {
3670 delete_breakpoint (b);
3671 continue;
3672 }
3673
ce78b96d
JB
3674 if (b->type == bp_catchpoint)
3675 {
dda83cd7
SM
3676 /* For now, none of the bp_catchpoint breakpoints need to
3677 do anything at this point. In the future, if some of
3678 the catchpoints need to something, we will need to add
3679 a new method, and call this method from here. */
3680 continue;
ce78b96d
JB
3681 }
3682
c5aa993b
JM
3683 /* bp_finish is a special case. The only way we ought to be able
3684 to see one of these when an exec() has happened, is if the user
3685 caught a vfork, and then said "finish". Ordinarily a finish just
3686 carries them to the call-site of the current callee, by setting
3687 a temporary bp there and resuming. But in this case, the finish
3688 will carry them entirely through the vfork & exec.
3689
3690 We don't want to allow a bp_finish to remain inserted now. But
3691 we can't safely delete it, 'cause finish_command has a handle to
3692 the bp on a bpstat, and will later want to delete it. There's a
3693 chance (and I've seen it happen) that if we delete the bp_finish
3694 here, that its storage will get reused by the time finish_command
3695 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3696 We really must allow finish_command to delete a bp_finish.
3697
e5dd4106 3698 In the absence of a general solution for the "how do we know
53a5351d
JM
3699 it's safe to delete something others may have handles to?"
3700 problem, what we'll do here is just uninsert the bp_finish, and
3701 let finish_command delete it.
3702
3703 (We know the bp_finish is "doomed" in the sense that it's
3704 momentary, and will be deleted as soon as finish_command sees
3705 the inferior stopped. So it doesn't matter that the bp's
3706 address is probably bogus in the new a.out, unlike e.g., the
3707 solib breakpoints.) */
c5aa993b 3708
c5aa993b
JM
3709 if (b->type == bp_finish)
3710 {
3711 continue;
3712 }
3713
3714 /* Without a symbolic address, we have little hope of the
3715 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3716 a.out. */
9ef9e6a6 3717 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3718 {
3719 delete_breakpoint (b);
3720 continue;
3721 }
c5aa993b 3722 }
c906108c
SS
3723}
3724
3725int
d80ee84f 3726detach_breakpoints (ptid_t ptid)
c906108c 3727{
3a1bae8e 3728 int val = 0;
2989a365 3729 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3730 struct inferior *inf = current_inferior ();
c5aa993b 3731
e99b03dc 3732 if (ptid.pid () == inferior_ptid.pid ())
8a3fe4f8 3733 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3734
6c95b8df 3735 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3736 inferior_ptid = ptid;
48d7020b 3737 for (bp_location *bl : all_bp_locations ())
c5aa993b 3738 {
35df4500 3739 if (bl->pspace != inf->pspace)
6c95b8df
PA
3740 continue;
3741
bd9673a4
PW
3742 /* This function must physically remove breakpoints locations
3743 from the specified ptid, without modifying the breakpoint
3744 package's state. Locations of type bp_loc_other are only
3745 maintained at GDB side. So, there is no need to remove
3746 these bp_loc_other locations. Moreover, removing these
3747 would modify the breakpoint package's state. */
3748 if (bl->loc_type == bp_loc_other)
3749 continue;
3750
35df4500 3751 if (bl->inserted)
b2b6a7da 3752 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3753 }
d03285ec 3754
3a1bae8e 3755 return val;
c906108c
SS
3756}
3757
35df4500 3758/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3759 Note that this is used to detach breakpoints from a child fork.
3760 When we get here, the child isn't in the inferior list, and neither
3761 do we have objects to represent its address space --- we should
35df4500 3762 *not* look at bl->pspace->aspace here. */
6c95b8df 3763
c906108c 3764static int
b2b6a7da 3765remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3766{
3767 int val;
c5aa993b 3768
35df4500
TJB
3769 /* BL is never in moribund_locations by our callers. */
3770 gdb_assert (bl->owner != NULL);
2bdf28a0 3771
74960c60
VP
3772 /* The type of none suggests that owner is actually deleted.
3773 This should not ever happen. */
35df4500 3774 gdb_assert (bl->owner->type != bp_none);
0bde7532 3775
35df4500
TJB
3776 if (bl->loc_type == bp_loc_software_breakpoint
3777 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3778 {
c02f5703
MS
3779 /* "Normal" instruction breakpoint: either the standard
3780 trap-instruction bp (bp_breakpoint), or a
3781 bp_hardware_breakpoint. */
3782
3783 /* First check to see if we have to handle an overlay. */
3784 if (overlay_debugging == ovly_off
35df4500
TJB
3785 || bl->section == NULL
3786 || !(section_is_overlay (bl->section)))
c02f5703
MS
3787 {
3788 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3789
3790 /* If we're trying to uninsert a memory breakpoint that we
3791 know is set in a dynamic object that is marked
3792 shlib_disabled, then either the dynamic object was
3793 removed with "remove-symbol-file" or with
3794 "nosharedlibrary". In the former case, we don't know
3795 whether another dynamic object might have loaded over the
3796 breakpoint's address -- the user might well let us know
3797 about it next with add-symbol-file (the whole point of
d03de421 3798 add-symbol-file is letting the user manually maintain a
08351840
PA
3799 list of dynamically loaded objects). If we have the
3800 breakpoint's shadow memory, that is, this is a software
3801 breakpoint managed by GDB, check whether the breakpoint
3802 is still inserted in memory, to avoid overwriting wrong
3803 code with stale saved shadow contents. Note that HW
3804 breakpoints don't have shadow memory, as they're
3805 implemented using a mechanism that is not dependent on
3806 being able to modify the target's memory, and as such
3807 they should always be removed. */
3808 if (bl->shlib_disabled
3809 && bl->target_info.shadow_len != 0
3810 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3811 val = 0;
3812 else
73971819 3813 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3814 }
c906108c
SS
3815 else
3816 {
4a64f543 3817 /* This breakpoint is in an overlay section.
c02f5703
MS
3818 Did we set a breakpoint at the LMA? */
3819 if (!overlay_events_enabled)
3820 {
3821 /* Yes -- overlay event support is not active, so we
3822 should have set a breakpoint at the LMA. Remove it.
3823 */
c02f5703
MS
3824 /* Ignore any failures: if the LMA is in ROM, we will
3825 have already warned when we failed to insert it. */
35df4500
TJB
3826 if (bl->loc_type == bp_loc_hardware_breakpoint)
3827 target_remove_hw_breakpoint (bl->gdbarch,
3828 &bl->overlay_target_info);
c02f5703 3829 else
35df4500 3830 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3831 &bl->overlay_target_info,
3832 reason);
c02f5703
MS
3833 }
3834 /* Did we set a breakpoint at the VMA?
3835 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3836 if (bl->inserted)
c906108c 3837 {
c02f5703
MS
3838 /* Yes -- remove it. Previously we did not bother to
3839 remove the breakpoint if the section had been
3840 unmapped, but let's not rely on that being safe. We
3841 don't know what the overlay manager might do. */
aa67235e
UW
3842
3843 /* However, we should remove *software* breakpoints only
3844 if the section is still mapped, or else we overwrite
3845 wrong code with the saved shadow contents. */
348d480f
PA
3846 if (bl->loc_type == bp_loc_hardware_breakpoint
3847 || section_is_mapped (bl->section))
73971819 3848 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3849 else
3850 val = 0;
c906108c 3851 }
c02f5703
MS
3852 else
3853 {
3854 /* No -- not inserted, so no need to remove. No error. */
3855 val = 0;
3856 }
c906108c 3857 }
879d1e6b 3858
08351840
PA
3859 /* In some cases, we might not be able to remove a breakpoint in
3860 a shared library that has already been removed, but we have
3861 not yet processed the shlib unload event. Similarly for an
3862 unloaded add-symbol-file object - the user might not yet have
3863 had the chance to remove-symbol-file it. shlib_disabled will
3864 be set if the library/object has already been removed, but
3865 the breakpoint hasn't been uninserted yet, e.g., after
3866 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3867 always-inserted mode. */
076855f9 3868 if (val
08351840
PA
3869 && (bl->loc_type == bp_loc_software_breakpoint
3870 && (bl->shlib_disabled
3871 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3872 || shared_objfile_contains_address_p (bl->pspace,
3873 bl->address))))
879d1e6b
UW
3874 val = 0;
3875
c906108c
SS
3876 if (val)
3877 return val;
b2b6a7da 3878 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3879 }
35df4500 3880 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3881 {
77b06cd7
TJB
3882 gdb_assert (bl->owner->ops != NULL
3883 && bl->owner->ops->remove_location != NULL);
3884
b2b6a7da 3885 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3886 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3887
c906108c 3888 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3889 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3890 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3891 bl->owner->number);
c906108c 3892 }
35df4500 3893 else if (bl->owner->type == bp_catchpoint
dda83cd7
SM
3894 && breakpoint_enabled (bl->owner)
3895 && !bl->duplicate)
ce78b96d 3896 {
77b06cd7
TJB
3897 gdb_assert (bl->owner->ops != NULL
3898 && bl->owner->ops->remove_location != NULL);
ce78b96d 3899
73971819 3900 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3901 if (val)
3902 return val;
77b06cd7 3903
b2b6a7da 3904 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3905 }
c906108c
SS
3906
3907 return 0;
3908}
3909
6c95b8df 3910static int
834c0d03 3911remove_breakpoint (struct bp_location *bl)
6c95b8df 3912{
35df4500
TJB
3913 /* BL is never in moribund_locations by our callers. */
3914 gdb_assert (bl->owner != NULL);
2bdf28a0 3915
6c95b8df
PA
3916 /* The type of none suggests that owner is actually deleted.
3917 This should not ever happen. */
35df4500 3918 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3919
5ed8105e 3920 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3921
35df4500 3922 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3923
5ed8105e 3924 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3925}
3926
c906108c
SS
3927/* Clear the "inserted" flag in all breakpoints. */
3928
25b22b0a 3929void
fba45db2 3930mark_breakpoints_out (void)
c906108c 3931{
48d7020b 3932 for (bp_location *bl : all_bp_locations ())
66c4b3e8 3933 if (bl->pspace == current_program_space)
35df4500 3934 bl->inserted = 0;
c906108c
SS
3935}
3936
53a5351d
JM
3937/* Clear the "inserted" flag in all breakpoints and delete any
3938 breakpoints which should go away between runs of the program.
c906108c
SS
3939
3940 Plus other such housekeeping that has to be done for breakpoints
3941 between runs.
3942
53a5351d
JM
3943 Note: this function gets called at the end of a run (by
3944 generic_mourn_inferior) and when a run begins (by
4a64f543 3945 init_wait_for_inferior). */
c906108c
SS
3946
3947
3948
3949void
fba45db2 3950breakpoint_init_inferior (enum inf_context context)
c906108c 3951{
6c95b8df 3952 struct program_space *pspace = current_program_space;
c906108c 3953
50c71eaf
PA
3954 /* If breakpoint locations are shared across processes, then there's
3955 nothing to do. */
f5656ead 3956 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3957 return;
3958
1a853c52 3959 mark_breakpoints_out ();
075f6582 3960
1428b37a 3961 for (breakpoint *b : all_breakpoints_safe ())
c5aa993b 3962 {
6c95b8df
PA
3963 if (b->loc && b->loc->pspace != pspace)
3964 continue;
3965
c5aa993b
JM
3966 switch (b->type)
3967 {
3968 case bp_call_dummy:
e2e4d78b 3969 case bp_longjmp_call_dummy:
c906108c 3970
c5aa993b 3971 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3972 cause problems when the inferior is rerun, so we better get
3973 rid of it. */
3974
3975 case bp_watchpoint_scope:
3976
3977 /* Also get rid of scope breakpoints. */
3978
3979 case bp_shlib_event:
3980
3981 /* Also remove solib event breakpoints. Their addresses may
3982 have changed since the last time we ran the program.
3983 Actually we may now be debugging against different target;
3984 and so the solib backend that installed this breakpoint may
3985 not be used in by the target. E.g.,
3986
3987 (gdb) file prog-linux
3988 (gdb) run # native linux target
3989 ...
3990 (gdb) kill
3991 (gdb) file prog-win.exe
3992 (gdb) tar rem :9999 # remote Windows gdbserver.
3993 */
c906108c 3994
f59f708a
PA
3995 case bp_step_resume:
3996
3997 /* Also remove step-resume breakpoints. */
3998
7c16b83e
PA
3999 case bp_single_step:
4000
4001 /* Also remove single-step breakpoints. */
4002
c5aa993b
JM
4003 delete_breakpoint (b);
4004 break;
c906108c 4005
c5aa993b
JM
4006 case bp_watchpoint:
4007 case bp_hardware_watchpoint:
4008 case bp_read_watchpoint:
4009 case bp_access_watchpoint:
3a5c3e22
PA
4010 {
4011 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4012
3a5c3e22
PA
4013 /* Likewise for watchpoints on local expressions. */
4014 if (w->exp_valid_block != NULL)
4015 delete_breakpoint (b);
63000888 4016 else
3a5c3e22 4017 {
63000888
PA
4018 /* Get rid of existing locations, which are no longer
4019 valid. New ones will be created in
4020 update_watchpoint, when the inferior is restarted.
4021 The next update_global_location_list call will
4022 garbage collect them. */
4023 b->loc = NULL;
4024
4025 if (context == inf_starting)
4026 {
4027 /* Reset val field to force reread of starting value in
4028 insert_breakpoints. */
850645cf 4029 w->val.reset (nullptr);
4c1d86d9 4030 w->val_valid = false;
63000888
PA
4031 }
4032 }
3a5c3e22 4033 }
c5aa993b
JM
4034 break;
4035 default:
c5aa993b
JM
4036 break;
4037 }
4038 }
1c5cfe86
PA
4039
4040 /* Get rid of the moribund locations. */
1123588c 4041 for (bp_location *bl : moribund_locations)
35df4500 4042 decref_bp_location (&bl);
1123588c 4043 moribund_locations.clear ();
c906108c
SS
4044}
4045
6c95b8df
PA
4046/* These functions concern about actual breakpoints inserted in the
4047 target --- to e.g. check if we need to do decr_pc adjustment or if
4048 we need to hop over the bkpt --- so we check for address space
4049 match, not program space. */
4050
c2c6d25f
JM
4051/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4052 exists at PC. It returns ordinary_breakpoint_here if it's an
4053 ordinary breakpoint, or permanent_breakpoint_here if it's a
4054 permanent breakpoint.
4055 - When continuing from a location with an ordinary breakpoint, we
4056 actually single step once before calling insert_breakpoints.
e5dd4106 4057 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4058 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4059 the target, to advance the PC past the breakpoint. */
c906108c 4060
c2c6d25f 4061enum breakpoint_here
accd0bcd 4062breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4063{
c2c6d25f 4064 int any_breakpoint_here = 0;
c906108c 4065
48d7020b 4066 for (bp_location *bl : all_bp_locations ())
075f6582 4067 {
35df4500
TJB
4068 if (bl->loc_type != bp_loc_software_breakpoint
4069 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4070 continue;
4071
f1310107 4072 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4073 if ((breakpoint_enabled (bl->owner)
1a853c52 4074 || bl->permanent)
f1310107 4075 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4076 {
4077 if (overlay_debugging
35df4500
TJB
4078 && section_is_overlay (bl->section)
4079 && !section_is_mapped (bl->section))
075f6582 4080 continue; /* unmapped overlay -- can't be a match */
1a853c52 4081 else if (bl->permanent)
075f6582
DJ
4082 return permanent_breakpoint_here;
4083 else
4084 any_breakpoint_here = 1;
4085 }
4086 }
c906108c 4087
f486487f 4088 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4089}
4090
d35ae833
PA
4091/* See breakpoint.h. */
4092
4093int
accd0bcd 4094breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4095 CORE_ADDR addr, ULONGEST len)
4096{
48d7020b 4097 for (bp_location *bl : all_bp_locations ())
d35ae833
PA
4098 {
4099 if (bl->loc_type != bp_loc_software_breakpoint
4100 && bl->loc_type != bp_loc_hardware_breakpoint)
4101 continue;
4102
4103 if ((breakpoint_enabled (bl->owner)
4104 || bl->permanent)
4105 && breakpoint_location_address_range_overlap (bl, aspace,
4106 addr, len))
4107 {
4108 if (overlay_debugging
4109 && section_is_overlay (bl->section)
4110 && !section_is_mapped (bl->section))
4111 {
4112 /* Unmapped overlay -- can't be a match. */
4113 continue;
4114 }
4115
4116 return 1;
4117 }
4118 }
4119
4120 return 0;
4121}
4122
1c5cfe86
PA
4123/* Return true if there's a moribund breakpoint at PC. */
4124
4125int
accd0bcd 4126moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86 4127{
1123588c 4128 for (bp_location *loc : moribund_locations)
f1310107 4129 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4130 return 1;
4131
4132 return 0;
4133}
c2c6d25f 4134
f7ce857f
PA
4135/* Returns non-zero iff BL is inserted at PC, in address space
4136 ASPACE. */
4137
4138static int
4139bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4140 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4141{
4142 if (bl->inserted
4143 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4144 aspace, pc))
4145 {
4146 if (overlay_debugging
4147 && section_is_overlay (bl->section)
4148 && !section_is_mapped (bl->section))
4149 return 0; /* unmapped overlay -- can't be a match */
4150 else
4151 return 1;
4152 }
4153 return 0;
4154}
4155
a1fd2fa5 4156/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4157
4158int
accd0bcd 4159breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4160{
e0d9a270 4161 for (bp_location *bl : all_bp_locations_at_addr (pc))
c5aa993b 4162 {
35df4500
TJB
4163 if (bl->loc_type != bp_loc_software_breakpoint
4164 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4165 continue;
4166
f7ce857f
PA
4167 if (bp_location_inserted_here_p (bl, aspace, pc))
4168 return 1;
c5aa993b 4169 }
c36b740a
VP
4170 return 0;
4171}
4172
a1fd2fa5
PA
4173/* This function returns non-zero iff there is a software breakpoint
4174 inserted at PC. */
c36b740a
VP
4175
4176int
accd0bcd 4177software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4178 CORE_ADDR pc)
4fa8626c 4179{
e0d9a270 4180 for (bp_location *bl : all_bp_locations_at_addr (pc))
4fa8626c 4181 {
35df4500 4182 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4183 continue;
4184
f7ce857f
PA
4185 if (bp_location_inserted_here_p (bl, aspace, pc))
4186 return 1;
4fa8626c
DJ
4187 }
4188
4189 return 0;
9c02b525
PA
4190}
4191
4192/* See breakpoint.h. */
4193
4194int
accd0bcd 4195hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4196 CORE_ADDR pc)
4197{
e0d9a270 4198 for (bp_location *bl : all_bp_locations_at_addr (pc))
9c02b525 4199 {
9c02b525
PA
4200 if (bl->loc_type != bp_loc_hardware_breakpoint)
4201 continue;
4202
4203 if (bp_location_inserted_here_p (bl, aspace, pc))
4204 return 1;
4205 }
4206
4207 return 0;
4fa8626c
DJ
4208}
4209
9093389c 4210int
accd0bcd 4211hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4212 CORE_ADDR addr, ULONGEST len)
4213{
43892fdf 4214 for (breakpoint *bpt : all_breakpoints ())
9093389c 4215 {
9093389c
PA
4216 if (bpt->type != bp_hardware_watchpoint
4217 && bpt->type != bp_access_watchpoint)
4218 continue;
4219
4220 if (!breakpoint_enabled (bpt))
4221 continue;
4222
40cb8ca5 4223 for (bp_location *loc : bpt->locations ())
9093389c
PA
4224 if (loc->pspace->aspace == aspace && loc->inserted)
4225 {
4226 CORE_ADDR l, h;
4227
4228 /* Check for intersection. */
768adc05
PA
4229 l = std::max<CORE_ADDR> (loc->address, addr);
4230 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4231 if (l < h)
4232 return 1;
4233 }
4234 }
4235 return 0;
4236}
c5aa993b 4237
f2478a7e 4238/* See breakpoint.h. */
c906108c 4239
f2478a7e
SM
4240bool
4241is_catchpoint (struct breakpoint *b)
c906108c 4242{
f2478a7e 4243 return (b->type == bp_catchpoint);
c906108c
SS
4244}
4245
c906108c
SS
4246/* Clear a bpstat so that it says we are not at any breakpoint.
4247 Also free any storage that is part of a bpstat. */
4248
4249void
fba45db2 4250bpstat_clear (bpstat *bsp)
c906108c
SS
4251{
4252 bpstat p;
4253 bpstat q;
4254
4255 if (bsp == 0)
4256 return;
4257 p = *bsp;
4258 while (p != NULL)
4259 {
4260 q = p->next;
04afa70c 4261 delete p;
c906108c
SS
4262 p = q;
4263 }
4264 *bsp = NULL;
4265}
4266
04afa70c
TT
4267bpstats::bpstats (const bpstats &other)
4268 : next (NULL),
4269 bp_location_at (other.bp_location_at),
4270 breakpoint_at (other.breakpoint_at),
4271 commands (other.commands),
04afa70c
TT
4272 print (other.print),
4273 stop (other.stop),
4274 print_it (other.print_it)
4275{
850645cf
TT
4276 if (other.old_val != NULL)
4277 old_val = release_value (value_copy (other.old_val.get ()));
04afa70c
TT
4278}
4279
c906108c
SS
4280/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4281 is part of the bpstat is copied as well. */
4282
4283bpstat
fba45db2 4284bpstat_copy (bpstat bs)
c906108c
SS
4285{
4286 bpstat p = NULL;
4287 bpstat tmp;
4288 bpstat retval = NULL;
4289
4290 if (bs == NULL)
4291 return bs;
4292
4293 for (; bs != NULL; bs = bs->next)
4294 {
04afa70c 4295 tmp = new bpstats (*bs);
31cc81e9 4296
c906108c
SS
4297 if (p == NULL)
4298 /* This is the first thing in the chain. */
4299 retval = tmp;
4300 else
4301 p->next = tmp;
4302 p = tmp;
4303 }
4304 p->next = NULL;
4305 return retval;
4306}
4307
4a64f543 4308/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4309
4310bpstat
fba45db2 4311bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4312{
c5aa993b
JM
4313 if (bsp == NULL)
4314 return NULL;
c906108c 4315
c5aa993b
JM
4316 for (; bsp != NULL; bsp = bsp->next)
4317 {
f431efe5 4318 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4319 return bsp;
4320 }
c906108c
SS
4321 return NULL;
4322}
4323
ab04a2af
TT
4324/* See breakpoint.h. */
4325
4c462cb0 4326bool
427cd150 4327bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4328{
ab04a2af
TT
4329 for (; bsp != NULL; bsp = bsp->next)
4330 {
427cd150
TT
4331 if (bsp->breakpoint_at == NULL)
4332 {
4333 /* A moribund location can never explain a signal other than
4334 GDB_SIGNAL_TRAP. */
4335 if (sig == GDB_SIGNAL_TRAP)
4c462cb0 4336 return true;
427cd150
TT
4337 }
4338 else
47591c29
PA
4339 {
4340 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4341 sig))
4c462cb0 4342 return true;
47591c29 4343 }
ab04a2af
TT
4344 }
4345
4c462cb0 4346 return false;
ab04a2af
TT
4347}
4348
4a64f543
MS
4349/* Put in *NUM the breakpoint number of the first breakpoint we are
4350 stopped at. *BSP upon return is a bpstat which points to the
4351 remaining breakpoints stopped at (but which is not guaranteed to be
4352 good for anything but further calls to bpstat_num).
4353
8671a17b
PA
4354 Return 0 if passed a bpstat which does not indicate any breakpoints.
4355 Return -1 if stopped at a breakpoint that has been deleted since
4356 we set it.
4357 Return 1 otherwise. */
c906108c
SS
4358
4359int
8671a17b 4360bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4361{
4362 struct breakpoint *b;
4363
4364 if ((*bsp) == NULL)
4365 return 0; /* No more breakpoint values */
8671a17b 4366
4a64f543
MS
4367 /* We assume we'll never have several bpstats that correspond to a
4368 single breakpoint -- otherwise, this function might return the
4369 same number more than once and this will look ugly. */
f431efe5 4370 b = (*bsp)->breakpoint_at;
8671a17b
PA
4371 *bsp = (*bsp)->next;
4372 if (b == NULL)
4373 return -1; /* breakpoint that's been deleted since */
4374
4375 *num = b->number; /* We have its number */
4376 return 1;
c906108c
SS
4377}
4378
e93ca019 4379/* See breakpoint.h. */
c906108c
SS
4380
4381void
e93ca019 4382bpstat_clear_actions (void)
c906108c 4383{
e93ca019
JK
4384 bpstat bs;
4385
00431a78 4386 if (inferior_ptid == null_ptid)
e93ca019
JK
4387 return;
4388
00431a78 4389 thread_info *tp = inferior_thread ();
e93ca019 4390 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4391 {
d1b0a7bf 4392 bs->commands = NULL;
850645cf 4393 bs->old_val.reset (nullptr);
c906108c
SS
4394 }
4395}
4396
f3b1572e
PA
4397/* Called when a command is about to proceed the inferior. */
4398
4399static void
4400breakpoint_about_to_proceed (void)
4401{
d7e15655 4402 if (inferior_ptid != null_ptid)
f3b1572e
PA
4403 {
4404 struct thread_info *tp = inferior_thread ();
4405
4406 /* Allow inferior function calls in breakpoint commands to not
4407 interrupt the command list. When the call finishes
4408 successfully, the inferior will be standing at the same
4409 breakpoint as if nothing happened. */
16c381f0 4410 if (tp->control.in_infcall)
f3b1572e
PA
4411 return;
4412 }
4413
4414 breakpoint_proceeded = 1;
4415}
4416
abf85f46
JK
4417/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4418 or its equivalent. */
4419
4420static int
4421command_line_is_silent (struct command_line *cmd)
4422{
4f45d445 4423 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4424}
4425
4a64f543
MS
4426/* Execute all the commands associated with all the breakpoints at
4427 this location. Any of these commands could cause the process to
4428 proceed beyond this point, etc. We look out for such changes by
4429 checking the global "breakpoint_proceeded" after each command.
c906108c 4430
347bddb7
PA
4431 Returns true if a breakpoint command resumed the inferior. In that
4432 case, it is the caller's responsibility to recall it again with the
4433 bpstat of the current thread. */
4434
4435static int
4436bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4437{
4438 bpstat bs;
347bddb7 4439 int again = 0;
c906108c
SS
4440
4441 /* Avoid endless recursion if a `source' command is contained
4442 in bs->commands. */
4443 if (executing_breakpoint_commands)
347bddb7 4444 return 0;
c906108c 4445
81b1e71c
TT
4446 scoped_restore save_executing
4447 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4448
1ac32117 4449 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4450
4a64f543 4451 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4452 bs = *bsp;
4453
4454 breakpoint_proceeded = 0;
4455 for (; bs != NULL; bs = bs->next)
4456 {
d1b0a7bf 4457 struct command_line *cmd = NULL;
6c50ab1c
JB
4458
4459 /* Take ownership of the BSP's command tree, if it has one.
4460
dda83cd7
SM
4461 The command tree could legitimately contain commands like
4462 'step' and 'next', which call clear_proceed_status, which
4463 frees stop_bpstat's command tree. To make sure this doesn't
4464 free the tree we're executing out from under us, we need to
4465 take ownership of the tree ourselves. Since a given bpstat's
4466 commands are only executed once, we don't need to copy it; we
4467 can clear the pointer in the bpstat, and make sure we free
4468 the tree when we're done. */
d1b0a7bf 4469 counted_command_line ccmd = bs->commands;
9add0f1b 4470 bs->commands = NULL;
d1b0a7bf
TT
4471 if (ccmd != NULL)
4472 cmd = ccmd.get ();
abf85f46
JK
4473 if (command_line_is_silent (cmd))
4474 {
4475 /* The action has been already done by bpstat_stop_status. */
4476 cmd = cmd->next;
4477 }
6c50ab1c 4478
c906108c
SS
4479 while (cmd != NULL)
4480 {
4481 execute_control_command (cmd);
4482
4483 if (breakpoint_proceeded)
4484 break;
4485 else
4486 cmd = cmd->next;
4487 }
6c50ab1c 4488
c906108c 4489 if (breakpoint_proceeded)
32c1e744 4490 {
cb814510 4491 if (current_ui->async)
347bddb7
PA
4492 /* If we are in async mode, then the target might be still
4493 running, not stopped at any breakpoint, so nothing for
4494 us to do here -- just return to the event loop. */
4495 ;
32c1e744
VP
4496 else
4497 /* In sync mode, when execute_control_command returns
4498 we're already standing on the next breakpoint.
347bddb7
PA
4499 Breakpoint commands for that stop were not run, since
4500 execute_command does not run breakpoint commands --
4501 only command_line_handler does, but that one is not
4502 involved in execution of breakpoint commands. So, we
4503 can now execute breakpoint commands. It should be
4504 noted that making execute_command do bpstat actions is
4505 not an option -- in this case we'll have recursive
4506 invocation of bpstat for each breakpoint with a
4507 command, and can easily blow up GDB stack. Instead, we
4508 return true, which will trigger the caller to recall us
4509 with the new stop_bpstat. */
4510 again = 1;
4511 break;
32c1e744 4512 }
c906108c 4513 }
347bddb7
PA
4514 return again;
4515}
4516
00431a78
PA
4517/* Helper for bpstat_do_actions. Get the current thread, if there's
4518 one, is alive and has execution. Return NULL otherwise. */
4519
4520static thread_info *
4521get_bpstat_thread ()
4522{
55f6301a 4523 if (inferior_ptid == null_ptid || !target_has_execution ())
00431a78
PA
4524 return NULL;
4525
4526 thread_info *tp = inferior_thread ();
4527 if (tp->state == THREAD_EXITED || tp->executing)
4528 return NULL;
4529 return tp;
4530}
4531
347bddb7
PA
4532void
4533bpstat_do_actions (void)
4534{
694c6bf5 4535 auto cleanup_if_error = make_scope_exit (bpstat_clear_actions);
00431a78 4536 thread_info *tp;
353d1d73 4537
347bddb7 4538 /* Do any commands attached to breakpoint we are stopped at. */
00431a78
PA
4539 while ((tp = get_bpstat_thread ()) != NULL)
4540 {
4541 /* Since in sync mode, bpstat_do_actions may resume the
4542 inferior, and only return when it is stopped at the next
4543 breakpoint, we keep doing breakpoint actions until it returns
4544 false to indicate the inferior was not resumed. */
4545 if (!bpstat_do_actions_1 (&tp->control.stop_bpstat))
4546 break;
4547 }
353d1d73 4548
694c6bf5 4549 cleanup_if_error.release ();
c906108c
SS
4550}
4551
fa4727a6
DJ
4552/* Print out the (old or new) value associated with a watchpoint. */
4553
4554static void
4555watchpoint_value_print (struct value *val, struct ui_file *stream)
4556{
4557 if (val == NULL)
7f6aba03 4558 fprintf_styled (stream, metadata_style.style (), _("<unreadable>"));
fa4727a6 4559 else
79a45b7d
TT
4560 {
4561 struct value_print_options opts;
4562 get_user_print_options (&opts);
4563 value_print (val, stream, &opts);
4564 }
fa4727a6
DJ
4565}
4566
f303dbd6
PA
4567/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4568 debugging multiple threads. */
4569
4570void
4571maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4572{
112e8700 4573 if (uiout->is_mi_like_p ())
f303dbd6
PA
4574 return;
4575
112e8700 4576 uiout->text ("\n");
f303dbd6
PA
4577
4578 if (show_thread_that_caused_stop ())
4579 {
4580 const char *name;
4581 struct thread_info *thr = inferior_thread ();
4582
112e8700 4583 uiout->text ("Thread ");
33eca680 4584 uiout->field_string ("thread-id", print_thread_id (thr));
f303dbd6
PA
4585
4586 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4587 if (name != NULL)
4588 {
112e8700 4589 uiout->text (" \"");
33eca680 4590 uiout->field_string ("name", name);
112e8700 4591 uiout->text ("\"");
f303dbd6
PA
4592 }
4593
112e8700 4594 uiout->text (" hit ");
f303dbd6
PA
4595 }
4596}
4597
e514a9d6 4598/* Generic routine for printing messages indicating why we
4a64f543 4599 stopped. The behavior of this function depends on the value
e514a9d6
JM
4600 'print_it' in the bpstat structure. Under some circumstances we
4601 may decide not to print anything here and delegate the task to
4a64f543 4602 normal_stop(). */
e514a9d6
JM
4603
4604static enum print_stop_action
4605print_bp_stop_message (bpstat bs)
4606{
4607 switch (bs->print_it)
4608 {
4609 case print_it_noop:
4a64f543 4610 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4611 return PRINT_UNKNOWN;
4612 break;
4613
4614 case print_it_done:
4615 /* We still want to print the frame, but we already printed the
dda83cd7 4616 relevant messages. */
e514a9d6
JM
4617 return PRINT_SRC_AND_LOC;
4618 break;
4619
4620 case print_it_normal:
4f8d1dc6 4621 {
f431efe5
PA
4622 struct breakpoint *b = bs->breakpoint_at;
4623
1a6a67de
TJB
4624 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4625 which has since been deleted. */
4626 if (b == NULL)
4627 return PRINT_UNKNOWN;
4628
348d480f
PA
4629 /* Normal case. Call the breakpoint's print_it method. */
4630 return b->ops->print_it (bs);
4f8d1dc6 4631 }
348d480f 4632 break;
3086aeae 4633
e514a9d6 4634 default:
8e65ff28 4635 internal_error (__FILE__, __LINE__,
e2e0b3e5 4636 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4637 break;
c906108c 4638 }
c906108c
SS
4639}
4640
edcc5120
TT
4641/* A helper function that prints a shared library stopped event. */
4642
4643static void
4644print_solib_event (int is_catchpoint)
4645{
6fb16ce6 4646 bool any_deleted = !current_program_space->deleted_solibs.empty ();
bcb430e4 4647 bool any_added = !current_program_space->added_solibs.empty ();
edcc5120
TT
4648
4649 if (!is_catchpoint)
4650 {
4651 if (any_added || any_deleted)
112e8700 4652 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4653 else
112e8700
SM
4654 current_uiout->text (_("Stopped due to shared library event (no "
4655 "libraries added or removed)\n"));
edcc5120
TT
4656 }
4657
112e8700
SM
4658 if (current_uiout->is_mi_like_p ())
4659 current_uiout->field_string ("reason",
4660 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4661
4662 if (any_deleted)
4663 {
112e8700 4664 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4665 ui_out_emit_list list_emitter (current_uiout, "removed");
6fb16ce6 4666 for (int ix = 0; ix < current_program_space->deleted_solibs.size (); ix++)
edcc5120 4667 {
6fb16ce6
SM
4668 const std::string &name = current_program_space->deleted_solibs[ix];
4669
edcc5120 4670 if (ix > 0)
112e8700
SM
4671 current_uiout->text (" ");
4672 current_uiout->field_string ("library", name);
4673 current_uiout->text ("\n");
edcc5120 4674 }
edcc5120
TT
4675 }
4676
4677 if (any_added)
4678 {
112e8700 4679 current_uiout->text (_(" Inferior loaded "));
10f489e5 4680 ui_out_emit_list list_emitter (current_uiout, "added");
bcb430e4 4681 bool first = true;
52941706 4682 for (so_list *iter : current_program_space->added_solibs)
edcc5120 4683 {
bcb430e4 4684 if (!first)
112e8700 4685 current_uiout->text (" ");
bcb430e4 4686 first = false;
112e8700
SM
4687 current_uiout->field_string ("library", iter->so_name);
4688 current_uiout->text ("\n");
edcc5120 4689 }
edcc5120
TT
4690 }
4691}
4692
e514a9d6
JM
4693/* Print a message indicating what happened. This is called from
4694 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4695 list - a list of the eventpoints that caused this stop. KIND is
4696 the target_waitkind for the stopping event. This
e514a9d6
JM
4697 routine calls the generic print routine for printing a message
4698 about reasons for stopping. This will print (for example) the
4699 "Breakpoint n," part of the output. The return value of this
4700 routine is one of:
c906108c 4701
4a64f543 4702 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4703 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4704 code to print the location. An example is
c5aa993b
JM
4705 "Breakpoint 1, " which should be followed by
4706 the location.
917317f4 4707 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4708 to also print the location part of the message.
4709 An example is the catch/throw messages, which
4a64f543 4710 don't require a location appended to the end.
917317f4 4711 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4712 further info to be printed. */
c906108c 4713
917317f4 4714enum print_stop_action
36dfb11c 4715bpstat_print (bpstat bs, int kind)
c906108c 4716{
f486487f 4717 enum print_stop_action val;
c5aa993b 4718
c906108c 4719 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4720 (Currently all watchpoints go on the bpstat whether hit or not.
4721 That probably could (should) be changed, provided care is taken
c906108c 4722 with respect to bpstat_explains_signal). */
e514a9d6
JM
4723 for (; bs; bs = bs->next)
4724 {
4725 val = print_bp_stop_message (bs);
4726 if (val == PRINT_SRC_ONLY
4727 || val == PRINT_SRC_AND_LOC
4728 || val == PRINT_NOTHING)
4729 return val;
4730 }
c906108c 4731
36dfb11c
TT
4732 /* If we had hit a shared library event breakpoint,
4733 print_bp_stop_message would print out this message. If we hit an
4734 OS-level shared library event, do the same thing. */
4735 if (kind == TARGET_WAITKIND_LOADED)
4736 {
edcc5120 4737 print_solib_event (0);
36dfb11c
TT
4738 return PRINT_NOTHING;
4739 }
4740
e514a9d6 4741 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4742 with and nothing was printed. */
917317f4 4743 return PRINT_UNKNOWN;
c906108c
SS
4744}
4745
bf469271 4746/* Evaluate the boolean expression EXP and return the result. */
c906108c 4747
bf469271
PA
4748static bool
4749breakpoint_cond_eval (expression *exp)
c906108c 4750{
278cd55f 4751 struct value *mark = value_mark ();
bf469271 4752 bool res = value_true (evaluate_expression (exp));
cc59ec59 4753
c906108c 4754 value_free_to_mark (mark);
bf469271 4755 return res;
c906108c
SS
4756}
4757
5760d0ab 4758/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4759
04afa70c
TT
4760bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4761 : next (NULL),
b6433ede 4762 bp_location_at (bp_location_ref_ptr::new_reference (bl)),
04afa70c
TT
4763 breakpoint_at (bl->owner),
4764 commands (NULL),
04afa70c
TT
4765 print (0),
4766 stop (0),
4767 print_it (print_it_normal)
c906108c 4768{
04afa70c
TT
4769 **bs_link_pointer = this;
4770 *bs_link_pointer = &next;
4771}
4772
4773bpstats::bpstats ()
4774 : next (NULL),
04afa70c
TT
4775 breakpoint_at (NULL),
4776 commands (NULL),
04afa70c
TT
4777 print (0),
4778 stop (0),
4779 print_it (print_it_normal)
4780{
c906108c
SS
4781}
4782\f
d983da9c
DJ
4783/* The target has stopped with waitstatus WS. Check if any hardware
4784 watchpoints have triggered, according to the target. */
4785
4786int
4787watchpoints_triggered (struct target_waitstatus *ws)
4788{
57810aa7 4789 bool stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c 4790 CORE_ADDR addr;
d983da9c
DJ
4791
4792 if (!stopped_by_watchpoint)
4793 {
4794 /* We were not stopped by a watchpoint. Mark all watchpoints
4795 as not triggered. */
43892fdf 4796 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4797 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4798 {
4799 struct watchpoint *w = (struct watchpoint *) b;
4800
4801 w->watchpoint_triggered = watch_triggered_no;
4802 }
d983da9c
DJ
4803
4804 return 0;
4805 }
4806
328d42d8 4807 if (!target_stopped_data_address (current_inferior ()->top_target (), &addr))
d983da9c
DJ
4808 {
4809 /* We were stopped by a watchpoint, but we don't know where.
4810 Mark all watchpoints as unknown. */
43892fdf 4811 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4812 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4813 {
4814 struct watchpoint *w = (struct watchpoint *) b;
4815
4816 w->watchpoint_triggered = watch_triggered_unknown;
4817 }
d983da9c 4818
3c4797ba 4819 return 1;
d983da9c
DJ
4820 }
4821
4822 /* The target could report the data address. Mark watchpoints
4823 affected by this data address as triggered, and all others as not
4824 triggered. */
4825
43892fdf 4826 for (breakpoint *b : all_breakpoints ())
cc60f2e3 4827 if (is_hardware_watchpoint (b))
d983da9c 4828 {
3a5c3e22 4829 struct watchpoint *w = (struct watchpoint *) b;
d983da9c 4830
3a5c3e22 4831 w->watchpoint_triggered = watch_triggered_no;
40cb8ca5 4832 for (bp_location *loc : b->locations ())
9c06b0b4 4833 {
3a5c3e22 4834 if (is_masked_watchpoint (b))
9c06b0b4 4835 {
3a5c3e22
PA
4836 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4837 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4838
4839 if (newaddr == start)
4840 {
3a5c3e22 4841 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4842 break;
4843 }
4844 }
4845 /* Exact match not required. Within range is sufficient. */
328d42d8
SM
4846 else if (target_watchpoint_addr_within_range
4847 (current_inferior ()->top_target (), addr, loc->address,
4848 loc->length))
9c06b0b4 4849 {
3a5c3e22 4850 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4851 break;
4852 }
4853 }
d983da9c
DJ
4854 }
4855
4856 return 1;
4857}
4858
bf469271
PA
4859/* Possible return values for watchpoint_check. */
4860enum wp_check_result
4861 {
4862 /* The watchpoint has been deleted. */
4863 WP_DELETED = 1,
4864
4865 /* The value has changed. */
4866 WP_VALUE_CHANGED = 2,
4867
4868 /* The value has not changed. */
4869 WP_VALUE_NOT_CHANGED = 3,
4870
4871 /* Ignore this watchpoint, no matter if the value changed or not. */
4872 WP_IGNORE = 4,
4873 };
c906108c
SS
4874
4875#define BP_TEMPFLAG 1
4876#define BP_HARDWAREFLAG 2
4877
4a64f543 4878/* Evaluate watchpoint condition expression and check if its value
bf469271 4879 changed. */
553e4c11 4880
bf469271
PA
4881static wp_check_result
4882watchpoint_check (bpstat bs)
c906108c 4883{
3a5c3e22 4884 struct watchpoint *b;
c906108c
SS
4885 struct frame_info *fr;
4886 int within_current_scope;
4887
f431efe5 4888 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4889 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4890 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4891
f6bc2008
PA
4892 /* If this is a local watchpoint, we only want to check if the
4893 watchpoint frame is in scope if the current thread is the thread
4894 that was used to create the watchpoint. */
4895 if (!watchpoint_in_thread_scope (b))
60e1c644 4896 return WP_IGNORE;
f6bc2008 4897
c906108c
SS
4898 if (b->exp_valid_block == NULL)
4899 within_current_scope = 1;
4900 else
4901 {
edb3359d
DJ
4902 struct frame_info *frame = get_current_frame ();
4903 struct gdbarch *frame_arch = get_frame_arch (frame);
4904 CORE_ADDR frame_pc = get_frame_pc (frame);
4905
c9cf6e20 4906 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4907 still in the function but the stack frame has already been
4908 invalidated. Since we can't rely on the values of local
4909 variables after the stack has been destroyed, we are treating
4910 the watchpoint in that state as `not changed' without further
4911 checking. Don't mark watchpoints as changed if the current
4912 frame is in an epilogue - even if they are in some other
4913 frame, our view of the stack is likely to be wrong and
4914 frame_find_by_id could error out. */
c9cf6e20 4915 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4916 return WP_IGNORE;
a0f49112 4917
101dcfbe 4918 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4919 within_current_scope = (fr != NULL);
69fbadd5
DJ
4920
4921 /* If we've gotten confused in the unwinder, we might have
4922 returned a frame that can't describe this variable. */
edb3359d
DJ
4923 if (within_current_scope)
4924 {
4925 struct symbol *function;
4926
4927 function = get_frame_function (fr);
4928 if (function == NULL
4929 || !contained_in (b->exp_valid_block,
4930 SYMBOL_BLOCK_VALUE (function)))
4931 within_current_scope = 0;
4932 }
69fbadd5 4933
edb3359d 4934 if (within_current_scope)
c906108c
SS
4935 /* If we end up stopping, the current frame will get selected
4936 in normal_stop. So this call to select_frame won't affect
4937 the user. */
0f7d239c 4938 select_frame (fr);
c906108c 4939 }
c5aa993b 4940
c906108c
SS
4941 if (within_current_scope)
4942 {
4a64f543 4943 /* We use value_{,free_to_}mark because it could be a *long*
dda83cd7
SM
4944 time before we return to the command level and call
4945 free_all_values. We can't call free_all_values because we
4946 might be in the middle of evaluating a function call. */
c906108c 4947
9c06b0b4 4948 struct value *mark;
fa4727a6
DJ
4949 struct value *new_val;
4950
c1fc2657 4951 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4952 /* Since we don't know the exact trigger address (from
4953 stopped_data_address), just tell the user we've triggered
4954 a mask watchpoint. */
4955 return WP_VALUE_CHANGED;
4956
4957 mark = value_mark ();
1eaebe02 4958 fetch_subexp_value (b->exp.get (), b->exp->op.get (), &new_val,
413403fc 4959 NULL, NULL, false);
218d2fc6 4960
bb9d5f81
PP
4961 if (b->val_bitsize != 0)
4962 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4963
4a64f543
MS
4964 /* We use value_equal_contents instead of value_equal because
4965 the latter coerces an array to a pointer, thus comparing just
4966 the address of the array instead of its contents. This is
4967 not what we want. */
fa4727a6 4968 if ((b->val != NULL) != (new_val != NULL)
850645cf
TT
4969 || (b->val != NULL && !value_equal_contents (b->val.get (),
4970 new_val)))
c906108c 4971 {
c906108c 4972 bs->old_val = b->val;
850645cf 4973 b->val = release_value (new_val);
4c1d86d9 4974 b->val_valid = true;
850645cf
TT
4975 if (new_val != NULL)
4976 value_free_to_mark (mark);
c906108c
SS
4977 return WP_VALUE_CHANGED;
4978 }
4979 else
4980 {
60e1c644 4981 /* Nothing changed. */
c906108c 4982 value_free_to_mark (mark);
c906108c
SS
4983 return WP_VALUE_NOT_CHANGED;
4984 }
4985 }
4986 else
4987 {
4988 /* This seems like the only logical thing to do because
dda83cd7
SM
4989 if we temporarily ignored the watchpoint, then when
4990 we reenter the block in which it is valid it contains
4991 garbage (in the case of a function, it may have two
4992 garbage values, one before and one after the prologue).
4993 So we can't even detect the first assignment to it and
4994 watch after that (since the garbage may or may not equal
4995 the first value assigned). */
348d480f
PA
4996 /* We print all the stop information in
4997 breakpoint_ops->print_it, but in this case, by the time we
4998 call breakpoint_ops->print_it this bp will be deleted
4999 already. So we have no choice but print the information
5000 here. */
468afe6c 5001
0e454242 5002 SWITCH_THRU_ALL_UIS ()
dda83cd7 5003 {
468afe6c
PA
5004 struct ui_out *uiout = current_uiout;
5005
112e8700
SM
5006 if (uiout->is_mi_like_p ())
5007 uiout->field_string
5008 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
6a831f06
PA
5009 uiout->message ("\nWatchpoint %pF deleted because the program has "
5010 "left the block in\n"
5011 "which its expression is valid.\n",
5012 signed_field ("wpnum", b->number));
468afe6c 5013 }
4ce44c66 5014
cdac0397 5015 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5016 b->commands = NULL;
d0fb5eae 5017 watchpoint_del_at_next_stop (b);
c906108c
SS
5018
5019 return WP_DELETED;
5020 }
5021}
5022
18a18393 5023/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5024 breakpoint location BL. This function does not check if we should
5025 stop, only if BL explains the stop. */
5026
18a18393 5027static int
6c95b8df 5028bpstat_check_location (const struct bp_location *bl,
accd0bcd 5029 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5030 const struct target_waitstatus *ws)
18a18393
VP
5031{
5032 struct breakpoint *b = bl->owner;
5033
348d480f 5034 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5035 gdb_assert (b != NULL);
5036
bd522513 5037 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5038}
5039
3a5c3e22
PA
5040/* Determine if the watched values have actually changed, and we
5041 should stop. If not, set BS->stop to 0. */
5042
18a18393
VP
5043static void
5044bpstat_check_watchpoint (bpstat bs)
5045{
2bdf28a0 5046 const struct bp_location *bl;
3a5c3e22 5047 struct watchpoint *b;
2bdf28a0
JK
5048
5049 /* BS is built for existing struct breakpoint. */
b6433ede 5050 bl = bs->bp_location_at.get ();
2bdf28a0 5051 gdb_assert (bl != NULL);
3a5c3e22 5052 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5053 gdb_assert (b != NULL);
18a18393 5054
18a18393 5055 {
18a18393
VP
5056 int must_check_value = 0;
5057
c1fc2657 5058 if (b->type == bp_watchpoint)
18a18393
VP
5059 /* For a software watchpoint, we must always check the
5060 watched value. */
5061 must_check_value = 1;
5062 else if (b->watchpoint_triggered == watch_triggered_yes)
5063 /* We have a hardware watchpoint (read, write, or access)
5064 and the target earlier reported an address watched by
5065 this watchpoint. */
5066 must_check_value = 1;
5067 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5068 && b->type == bp_hardware_watchpoint)
18a18393
VP
5069 /* We were stopped by a hardware watchpoint, but the target could
5070 not report the data address. We must check the watchpoint's
5071 value. Access and read watchpoints are out of luck; without
5072 a data address, we can't figure it out. */
5073 must_check_value = 1;
3a5c3e22 5074
18a18393
VP
5075 if (must_check_value)
5076 {
bf469271
PA
5077 wp_check_result e;
5078
a70b8144 5079 try
bf469271
PA
5080 {
5081 e = watchpoint_check (bs);
5082 }
230d2906 5083 catch (const gdb_exception &ex)
bf469271
PA
5084 {
5085 exception_fprintf (gdb_stderr, ex,
5086 "Error evaluating expression "
5087 "for watchpoint %d\n",
5088 b->number);
5089
5090 SWITCH_THRU_ALL_UIS ()
5091 {
5092 printf_filtered (_("Watchpoint %d deleted.\n"),
5093 b->number);
5094 }
5095 watchpoint_del_at_next_stop (b);
5096 e = WP_DELETED;
5097 }
bf469271 5098
18a18393
VP
5099 switch (e)
5100 {
5101 case WP_DELETED:
5102 /* We've already printed what needs to be printed. */
5103 bs->print_it = print_it_done;
5104 /* Stop. */
5105 break;
60e1c644
PA
5106 case WP_IGNORE:
5107 bs->print_it = print_it_noop;
5108 bs->stop = 0;
5109 break;
18a18393 5110 case WP_VALUE_CHANGED:
c1fc2657 5111 if (b->type == bp_read_watchpoint)
18a18393 5112 {
85d721b8
PA
5113 /* There are two cases to consider here:
5114
4a64f543 5115 1. We're watching the triggered memory for reads.
85d721b8
PA
5116 In that case, trust the target, and always report
5117 the watchpoint hit to the user. Even though
5118 reads don't cause value changes, the value may
5119 have changed since the last time it was read, and
5120 since we're not trapping writes, we will not see
5121 those, and as such we should ignore our notion of
5122 old value.
5123
4a64f543 5124 2. We're watching the triggered memory for both
85d721b8
PA
5125 reads and writes. There are two ways this may
5126 happen:
5127
4a64f543 5128 2.1. This is a target that can't break on data
85d721b8
PA
5129 reads only, but can break on accesses (reads or
5130 writes), such as e.g., x86. We detect this case
5131 at the time we try to insert read watchpoints.
5132
4a64f543 5133 2.2. Otherwise, the target supports read
85d721b8
PA
5134 watchpoints, but, the user set an access or write
5135 watchpoint watching the same memory as this read
5136 watchpoint.
5137
5138 If we're watching memory writes as well as reads,
5139 ignore watchpoint hits when we find that the
5140 value hasn't changed, as reads don't cause
5141 changes. This still gives false positives when
5142 the program writes the same value to memory as
5143 what there was already in memory (we will confuse
5144 it for a read), but it's much better than
5145 nothing. */
5146
5147 int other_write_watchpoint = 0;
5148
5149 if (bl->watchpoint_type == hw_read)
5150 {
43892fdf 5151 for (breakpoint *other_b : all_breakpoints ())
3a5c3e22
PA
5152 if (other_b->type == bp_hardware_watchpoint
5153 || other_b->type == bp_access_watchpoint)
85d721b8 5154 {
3a5c3e22
PA
5155 struct watchpoint *other_w =
5156 (struct watchpoint *) other_b;
5157
5158 if (other_w->watchpoint_triggered
5159 == watch_triggered_yes)
5160 {
5161 other_write_watchpoint = 1;
5162 break;
5163 }
85d721b8
PA
5164 }
5165 }
5166
5167 if (other_write_watchpoint
5168 || bl->watchpoint_type == hw_access)
5169 {
5170 /* We're watching the same memory for writes,
5171 and the value changed since the last time we
5172 updated it, so this trap must be for a write.
5173 Ignore it. */
5174 bs->print_it = print_it_noop;
5175 bs->stop = 0;
5176 }
18a18393
VP
5177 }
5178 break;
5179 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5180 if (b->type == bp_hardware_watchpoint
5181 || b->type == bp_watchpoint)
18a18393
VP
5182 {
5183 /* Don't stop: write watchpoints shouldn't fire if
5184 the value hasn't changed. */
5185 bs->print_it = print_it_noop;
5186 bs->stop = 0;
5187 }
5188 /* Stop. */
5189 break;
5190 default:
5191 /* Can't happen. */
18a18393
VP
5192 break;
5193 }
5194 }
5195 else /* must_check_value == 0 */
5196 {
5197 /* This is a case where some watchpoint(s) triggered, but
5198 not at the address of this watchpoint, or else no
5199 watchpoint triggered after all. So don't print
5200 anything for this watchpoint. */
5201 bs->print_it = print_it_noop;
5202 bs->stop = 0;
5203 }
5204 }
5205}
5206
7d4df6a4
DE
5207/* For breakpoints that are currently marked as telling gdb to stop,
5208 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5209 of breakpoint referred to by BS. If we should not stop for this
5210 breakpoint, set BS->stop to 0. */
f431efe5 5211
18a18393 5212static void
00431a78 5213bpstat_check_breakpoint_conditions (bpstat bs, thread_info *thread)
18a18393 5214{
2bdf28a0
JK
5215 const struct bp_location *bl;
5216 struct breakpoint *b;
bf469271
PA
5217 /* Assume stop. */
5218 bool condition_result = true;
7d4df6a4
DE
5219 struct expression *cond;
5220
5221 gdb_assert (bs->stop);
2bdf28a0
JK
5222
5223 /* BS is built for existing struct breakpoint. */
b6433ede 5224 bl = bs->bp_location_at.get ();
2bdf28a0 5225 gdb_assert (bl != NULL);
f431efe5 5226 b = bs->breakpoint_at;
2bdf28a0 5227 gdb_assert (b != NULL);
18a18393 5228
b775012e
LM
5229 /* Even if the target evaluated the condition on its end and notified GDB, we
5230 need to do so again since GDB does not know if we stopped due to a
5231 breakpoint or a single step breakpoint. */
5232
18a18393 5233 if (frame_id_p (b->frame_id)
edb3359d 5234 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5235 {
7d4df6a4
DE
5236 bs->stop = 0;
5237 return;
5238 }
60e1c644 5239
12ab52e9
PA
5240 /* If this is a thread/task-specific breakpoint, don't waste cpu
5241 evaluating the condition if this isn't the specified
5242 thread/task. */
00431a78
PA
5243 if ((b->thread != -1 && b->thread != thread->global_num)
5244 || (b->task != 0 && b->task != ada_get_task_number (thread)))
6c1b0f7b
DE
5245 {
5246 bs->stop = 0;
5247 return;
5248 }
5249
6dddc817
DE
5250 /* Evaluate extension language breakpoints that have a "stop" method
5251 implemented. */
5252 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5253
7d4df6a4
DE
5254 if (is_watchpoint (b))
5255 {
5256 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5257
4d01a485 5258 cond = w->cond_exp.get ();
7d4df6a4
DE
5259 }
5260 else
4d01a485 5261 cond = bl->cond.get ();
60e1c644 5262
7d4df6a4
DE
5263 if (cond && b->disposition != disp_del_at_next_stop)
5264 {
5265 int within_current_scope = 1;
5266 struct watchpoint * w;
60e1c644 5267
7d4df6a4
DE
5268 /* We use value_mark and value_free_to_mark because it could
5269 be a long time before we return to the command level and
5270 call free_all_values. We can't call free_all_values
5271 because we might be in the middle of evaluating a
5272 function call. */
5273 struct value *mark = value_mark ();
5274
5275 if (is_watchpoint (b))
5276 w = (struct watchpoint *) b;
5277 else
5278 w = NULL;
5279
5280 /* Need to select the frame, with all that implies so that
5281 the conditions will have the right context. Because we
5282 use the frame, we will not see an inlined function's
5283 variables when we arrive at a breakpoint at the start
5284 of the inlined function; the current frame will be the
5285 call site. */
5286 if (w == NULL || w->cond_exp_valid_block == NULL)
5287 select_frame (get_current_frame ());
5288 else
18a18393 5289 {
7d4df6a4
DE
5290 struct frame_info *frame;
5291
5292 /* For local watchpoint expressions, which particular
5293 instance of a local is being watched matters, so we
5294 keep track of the frame to evaluate the expression
5295 in. To evaluate the condition however, it doesn't
5296 really matter which instantiation of the function
5297 where the condition makes sense triggers the
5298 watchpoint. This allows an expression like "watch
5299 global if q > 10" set in `func', catch writes to
5300 global on all threads that call `func', or catch
5301 writes on all recursive calls of `func' by a single
5302 thread. We simply always evaluate the condition in
5303 the innermost frame that's executing where it makes
5304 sense to evaluate the condition. It seems
5305 intuitive. */
5306 frame = block_innermost_frame (w->cond_exp_valid_block);
5307 if (frame != NULL)
5308 select_frame (frame);
5309 else
5310 within_current_scope = 0;
18a18393 5311 }
7d4df6a4 5312 if (within_current_scope)
bf469271 5313 {
a70b8144 5314 try
bf469271
PA
5315 {
5316 condition_result = breakpoint_cond_eval (cond);
5317 }
230d2906 5318 catch (const gdb_exception &ex)
bf469271
PA
5319 {
5320 exception_fprintf (gdb_stderr, ex,
5321 "Error in testing breakpoint condition:\n");
5322 }
bf469271 5323 }
7d4df6a4 5324 else
18a18393 5325 {
7d4df6a4
DE
5326 warning (_("Watchpoint condition cannot be tested "
5327 "in the current scope"));
5328 /* If we failed to set the right context for this
5329 watchpoint, unconditionally report it. */
18a18393 5330 }
7d4df6a4
DE
5331 /* FIXME-someday, should give breakpoint #. */
5332 value_free_to_mark (mark);
18a18393 5333 }
7d4df6a4 5334
bf469271 5335 if (cond && !condition_result)
7d4df6a4
DE
5336 {
5337 bs->stop = 0;
5338 }
7d4df6a4
DE
5339 else if (b->ignore_count > 0)
5340 {
5341 b->ignore_count--;
5342 bs->stop = 0;
5343 /* Increase the hit count even though we don't stop. */
5344 ++(b->hit_count);
76727919 5345 gdb::observers::breakpoint_modified.notify (b);
7d4df6a4 5346 }
18a18393
VP
5347}
5348
1cf4d951
PA
5349/* Returns true if we need to track moribund locations of LOC's type
5350 on the current target. */
5351
5352static int
5353need_moribund_for_location_type (struct bp_location *loc)
5354{
5355 return ((loc->loc_type == bp_loc_software_breakpoint
5356 && !target_supports_stopped_by_sw_breakpoint ())
5357 || (loc->loc_type == bp_loc_hardware_breakpoint
5358 && !target_supports_stopped_by_hw_breakpoint ()));
5359}
5360
ddfe970e 5361/* See breakpoint.h. */
c906108c
SS
5362
5363bpstat
ddfe970e 5364build_bpstat_chain (const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5365 const struct target_waitstatus *ws)
c906108c 5366{
5760d0ab 5367 bpstat bs_head = NULL, *bs_link = &bs_head;
c5aa993b 5368
43892fdf 5369 for (breakpoint *b : all_breakpoints ())
429374b8 5370 {
1a853c52 5371 if (!breakpoint_enabled (b))
429374b8 5372 continue;
a5606eee 5373
40cb8ca5 5374 for (bp_location *bl : b->locations ())
429374b8 5375 {
4a64f543
MS
5376 /* For hardware watchpoints, we look only at the first
5377 location. The watchpoint_check function will work on the
5378 entire expression, not the individual locations. For
5379 read watchpoints, the watchpoints_triggered function has
5380 checked all locations already. */
429374b8
JK
5381 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5382 break;
18a18393 5383
b5fa468f 5384 if (!bl->enabled || bl->disabled_by_cond || bl->shlib_disabled)
429374b8 5385 continue;
c5aa993b 5386
09ac7c10 5387 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5388 continue;
c5aa993b 5389
4a64f543
MS
5390 /* Come here if it's a watchpoint, or if the break address
5391 matches. */
c5aa993b 5392
ddfe970e
KS
5393 bpstat bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
5394 explain stop. */
c5aa993b 5395
f431efe5
PA
5396 /* Assume we stop. Should we find a watchpoint that is not
5397 actually triggered, or if the condition of the breakpoint
5398 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5399 bs->stop = 1;
5400 bs->print = 1;
d983da9c 5401
f431efe5
PA
5402 /* If this is a scope breakpoint, mark the associated
5403 watchpoint as triggered so that we will handle the
5404 out-of-scope event. We'll get to the watchpoint next
5405 iteration. */
d0fb5eae 5406 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5407 {
5408 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5409
5410 w->watchpoint_triggered = watch_triggered_yes;
5411 }
f431efe5
PA
5412 }
5413 }
5414
7c16b83e 5415 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5416 if (!target_supports_stopped_by_sw_breakpoint ()
5417 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5418 {
1123588c 5419 for (bp_location *loc : moribund_locations)
f431efe5 5420 {
1cf4d951
PA
5421 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5422 && need_moribund_for_location_type (loc))
5423 {
ddfe970e 5424 bpstat bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5425 /* For hits of moribund locations, we should just proceed. */
5426 bs->stop = 0;
5427 bs->print = 0;
5428 bs->print_it = print_it_noop;
5429 }
f431efe5
PA
5430 }
5431 }
5432
ddfe970e
KS
5433 return bs_head;
5434}
5435
5436/* See breakpoint.h. */
5437
5438bpstat
5439bpstat_stop_status (const address_space *aspace,
00431a78 5440 CORE_ADDR bp_addr, thread_info *thread,
ddfe970e
KS
5441 const struct target_waitstatus *ws,
5442 bpstat stop_chain)
5443{
5444 struct breakpoint *b = NULL;
5445 /* First item of allocated bpstat's. */
5446 bpstat bs_head = stop_chain;
5447 bpstat bs;
5448 int need_remove_insert;
5449 int removed_any;
5450
5451 /* First, build the bpstat chain with locations that explain a
5452 target stop, while being careful to not set the target running,
5453 as that may invalidate locations (in particular watchpoint
5454 locations are recreated). Resuming will happen here with
5455 breakpoint conditions or watchpoint expressions that include
5456 inferior function calls. */
5457 if (bs_head == NULL)
5458 bs_head = build_bpstat_chain (aspace, bp_addr, ws);
5459
edcc5120
TT
5460 /* A bit of special processing for shlib breakpoints. We need to
5461 process solib loading here, so that the lists of loaded and
5462 unloaded libraries are correct before we handle "catch load" and
5463 "catch unload". */
5464 for (bs = bs_head; bs != NULL; bs = bs->next)
5465 {
5d268276 5466 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5467 {
5468 handle_solib_event ();
5469 break;
5470 }
5471 }
5472
f431efe5
PA
5473 /* Now go through the locations that caused the target to stop, and
5474 check whether we're interested in reporting this stop to higher
5475 layers, or whether we should resume the target transparently. */
5476
5477 removed_any = 0;
5478
5760d0ab 5479 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5480 {
5481 if (!bs->stop)
5482 continue;
5483
f431efe5 5484 b = bs->breakpoint_at;
348d480f
PA
5485 b->ops->check_status (bs);
5486 if (bs->stop)
28010a5d 5487 {
00431a78 5488 bpstat_check_breakpoint_conditions (bs, thread);
f431efe5 5489
429374b8
JK
5490 if (bs->stop)
5491 {
5492 ++(b->hit_count);
76727919 5493 gdb::observers::breakpoint_modified.notify (b);
c906108c 5494
4a64f543 5495 /* We will stop here. */
429374b8
JK
5496 if (b->disposition == disp_disable)
5497 {
816338b5 5498 --(b->enable_count);
1a853c52 5499 if (b->enable_count <= 0)
429374b8 5500 b->enable_state = bp_disabled;
f431efe5 5501 removed_any = 1;
429374b8
JK
5502 }
5503 if (b->silent)
5504 bs->print = 0;
5505 bs->commands = b->commands;
abf85f46 5506 if (command_line_is_silent (bs->commands
d1b0a7bf 5507 ? bs->commands.get () : NULL))
abf85f46 5508 bs->print = 0;
9d6e6e84
HZ
5509
5510 b->ops->after_condition_true (bs);
429374b8
JK
5511 }
5512
348d480f 5513 }
a9b3a50f
PA
5514
5515 /* Print nothing for this entry if we don't stop or don't
5516 print. */
5517 if (!bs->stop || !bs->print)
5518 bs->print_it = print_it_noop;
429374b8 5519 }
876fa593 5520
d983da9c
DJ
5521 /* If we aren't stopping, the value of some hardware watchpoint may
5522 not have changed, but the intermediate memory locations we are
5523 watching may have. Don't bother if we're stopping; this will get
5524 done later. */
d832cb68 5525 need_remove_insert = 0;
5760d0ab
JK
5526 if (! bpstat_causes_stop (bs_head))
5527 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5528 if (!bs->stop
f431efe5
PA
5529 && bs->breakpoint_at
5530 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5531 {
3a5c3e22
PA
5532 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5533
5534 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5535 need_remove_insert = 1;
d983da9c
DJ
5536 }
5537
d832cb68 5538 if (need_remove_insert)
44702360 5539 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5540 else if (removed_any)
44702360 5541 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5542
5760d0ab 5543 return bs_head;
c906108c 5544}
628fe4e4
JK
5545
5546static void
61c26be8 5547handle_jit_event (CORE_ADDR address)
628fe4e4 5548{
628fe4e4
JK
5549 struct gdbarch *gdbarch;
5550
1eb8556f 5551 infrun_debug_printf ("handling bp_jit_event");
243a9253 5552
628fe4e4
JK
5553 /* Switch terminal for any messages produced by
5554 breakpoint_re_set. */
223ffa71 5555 target_terminal::ours_for_output ();
628fe4e4 5556
61c26be8
MS
5557 gdbarch = get_frame_arch (get_current_frame ());
5558 /* This event is caused by a breakpoint set in `jit_breakpoint_re_set`,
5559 thus it is expected that its objectfile can be found through
5560 minimal symbol lookup. If it doesn't work (and assert fails), it
5561 most likely means that `jit_breakpoint_re_set` was changes and this
5562 function needs to be updated too. */
5563 bound_minimal_symbol jit_bp_sym = lookup_minimal_symbol_by_pc (address);
5564 gdb_assert (jit_bp_sym.objfile != nullptr);
5565 jit_event_handler (gdbarch, jit_bp_sym.objfile);
628fe4e4 5566
223ffa71 5567 target_terminal::inferior ();
628fe4e4
JK
5568}
5569
5570/* Prepare WHAT final decision for infrun. */
5571
5572/* Decide what infrun needs to do with this bpstat. */
5573
c906108c 5574struct bpstat_what
0e30163f 5575bpstat_what (bpstat bs_head)
c906108c 5576{
c906108c 5577 struct bpstat_what retval;
0e30163f 5578 bpstat bs;
c906108c 5579
628fe4e4 5580 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5581 retval.call_dummy = STOP_NONE;
e2d0f980 5582 retval.is_longjmp = false;
628fe4e4 5583
0e30163f 5584 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5585 {
628fe4e4
JK
5586 /* Extract this BS's action. After processing each BS, we check
5587 if its action overrides all we've seem so far. */
5588 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5589 enum bptype bptype;
5590
c906108c 5591 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5592 {
5593 /* I suspect this can happen if it was a momentary
5594 breakpoint which has since been deleted. */
5595 bptype = bp_none;
5596 }
20874c92 5597 else
f431efe5 5598 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5599
5600 switch (bptype)
c906108c
SS
5601 {
5602 case bp_none:
628fe4e4 5603 break;
c906108c
SS
5604 case bp_breakpoint:
5605 case bp_hardware_breakpoint:
7c16b83e 5606 case bp_single_step:
c906108c
SS
5607 case bp_until:
5608 case bp_finish:
a9b3a50f 5609 case bp_shlib_event:
c906108c
SS
5610 if (bs->stop)
5611 {
5612 if (bs->print)
628fe4e4 5613 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5614 else
628fe4e4 5615 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5616 }
5617 else
628fe4e4 5618 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5619 break;
5620 case bp_watchpoint:
5621 case bp_hardware_watchpoint:
5622 case bp_read_watchpoint:
5623 case bp_access_watchpoint:
5624 if (bs->stop)
5625 {
5626 if (bs->print)
628fe4e4 5627 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5628 else
628fe4e4 5629 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5630 }
5631 else
628fe4e4
JK
5632 {
5633 /* There was a watchpoint, but we're not stopping.
5634 This requires no further action. */
5635 }
c906108c
SS
5636 break;
5637 case bp_longjmp:
e2e4d78b 5638 case bp_longjmp_call_dummy:
186c406b 5639 case bp_exception:
0a39bb32
PA
5640 if (bs->stop)
5641 {
5642 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5643 retval.is_longjmp = bptype != bp_exception;
5644 }
5645 else
5646 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5647 break;
5648 case bp_longjmp_resume:
186c406b 5649 case bp_exception_resume:
0a39bb32
PA
5650 if (bs->stop)
5651 {
5652 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5653 retval.is_longjmp = bptype == bp_longjmp_resume;
5654 }
5655 else
5656 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5657 break;
5658 case bp_step_resume:
5659 if (bs->stop)
628fe4e4
JK
5660 this_action = BPSTAT_WHAT_STEP_RESUME;
5661 else
c906108c 5662 {
628fe4e4
JK
5663 /* It is for the wrong frame. */
5664 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5665 }
c906108c 5666 break;
2c03e5be
PA
5667 case bp_hp_step_resume:
5668 if (bs->stop)
5669 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5670 else
5671 {
5672 /* It is for the wrong frame. */
5673 this_action = BPSTAT_WHAT_SINGLE;
5674 }
5675 break;
c906108c 5676 case bp_watchpoint_scope:
c4093a6a 5677 case bp_thread_event:
1900040c 5678 case bp_overlay_event:
0fd8e87f 5679 case bp_longjmp_master:
aa7d318d 5680 case bp_std_terminate_master:
186c406b 5681 case bp_exception_master:
628fe4e4 5682 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5683 break;
ce78b96d 5684 case bp_catchpoint:
c5aa993b
JM
5685 if (bs->stop)
5686 {
5687 if (bs->print)
628fe4e4 5688 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5689 else
628fe4e4 5690 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5691 }
5692 else
628fe4e4 5693 {
cb1e4e32
PA
5694 /* Some catchpoints are implemented with breakpoints.
5695 For those, we need to step over the breakpoint. */
5696 if (bs->bp_location_at->loc_type != bp_loc_other)
5697 this_action = BPSTAT_WHAT_SINGLE;
628fe4e4
JK
5698 }
5699 break;
628fe4e4 5700 case bp_jit_event:
628fe4e4 5701 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5702 break;
c906108c 5703 case bp_call_dummy:
53a5351d
JM
5704 /* Make sure the action is stop (silent or noisy),
5705 so infrun.c pops the dummy frame. */
aa7d318d 5706 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5707 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5708 break;
5709 case bp_std_terminate:
5710 /* Make sure the action is stop (silent or noisy),
5711 so infrun.c pops the dummy frame. */
aa7d318d 5712 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5713 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5714 break;
1042e4c0 5715 case bp_tracepoint:
7a697b8d 5716 case bp_fast_tracepoint:
0fb4aa4b 5717 case bp_static_tracepoint:
1042e4c0
SS
5718 /* Tracepoint hits should not be reported back to GDB, and
5719 if one got through somehow, it should have been filtered
5720 out already. */
5721 internal_error (__FILE__, __LINE__,
7a697b8d 5722 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5723 break;
5724 case bp_gnu_ifunc_resolver:
5725 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5726 this_action = BPSTAT_WHAT_SINGLE;
5727 break;
5728 case bp_gnu_ifunc_resolver_return:
5729 /* The breakpoint will be removed, execution will restart from the
5730 PC of the former breakpoint. */
5731 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5732 break;
e7e0cddf
SS
5733
5734 case bp_dprintf:
a11cfd87
HZ
5735 if (bs->stop)
5736 this_action = BPSTAT_WHAT_STOP_SILENT;
5737 else
5738 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5739 break;
5740
628fe4e4
JK
5741 default:
5742 internal_error (__FILE__, __LINE__,
5743 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5744 }
628fe4e4 5745
325fac50 5746 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5747 }
628fe4e4 5748
243a9253
PA
5749 return retval;
5750}
628fe4e4 5751
243a9253
PA
5752void
5753bpstat_run_callbacks (bpstat bs_head)
5754{
5755 bpstat bs;
628fe4e4 5756
0e30163f
JK
5757 for (bs = bs_head; bs != NULL; bs = bs->next)
5758 {
5759 struct breakpoint *b = bs->breakpoint_at;
5760
5761 if (b == NULL)
5762 continue;
5763 switch (b->type)
5764 {
243a9253 5765 case bp_jit_event:
61c26be8 5766 handle_jit_event (bs->bp_location_at->address);
243a9253 5767 break;
0e30163f
JK
5768 case bp_gnu_ifunc_resolver:
5769 gnu_ifunc_resolver_stop (b);
5770 break;
5771 case bp_gnu_ifunc_resolver_return:
5772 gnu_ifunc_resolver_return_stop (b);
5773 break;
5774 }
5775 }
c906108c
SS
5776}
5777
4c462cb0 5778/* See breakpoint.h. */
c906108c 5779
4c462cb0
SM
5780bool
5781bpstat_should_step ()
c906108c 5782{
43892fdf 5783 for (breakpoint *b : all_breakpoints ())
717a8278 5784 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
4c462cb0 5785 return true;
43892fdf 5786
4c462cb0 5787 return false;
c906108c
SS
5788}
5789
4c462cb0
SM
5790/* See breakpoint.h. */
5791
5792bool
67822962
PA
5793bpstat_causes_stop (bpstat bs)
5794{
5795 for (; bs != NULL; bs = bs->next)
5796 if (bs->stop)
4c462cb0 5797 return true;
67822962 5798
4c462cb0 5799 return false;
67822962
PA
5800}
5801
c906108c 5802\f
c5aa993b 5803
170b53b2
UW
5804/* Compute a string of spaces suitable to indent the next line
5805 so it starts at the position corresponding to the table column
5806 named COL_NAME in the currently active table of UIOUT. */
5807
5808static char *
5809wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5810{
5811 static char wrap_indent[80];
5812 int i, total_width, width, align;
c5209615 5813 const char *text;
170b53b2
UW
5814
5815 total_width = 0;
112e8700 5816 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5817 {
5818 if (strcmp (text, col_name) == 0)
5819 {
5820 gdb_assert (total_width < sizeof wrap_indent);
5821 memset (wrap_indent, ' ', total_width);
5822 wrap_indent[total_width] = 0;
5823
5824 return wrap_indent;
5825 }
5826
5827 total_width += width + 1;
5828 }
5829
5830 return NULL;
5831}
5832
b775012e
LM
5833/* Determine if the locations of this breakpoint will have their conditions
5834 evaluated by the target, host or a mix of both. Returns the following:
5835
5836 "host": Host evals condition.
5837 "host or target": Host or Target evals condition.
5838 "target": Target evals condition.
5839*/
5840
5841static const char *
5842bp_condition_evaluator (struct breakpoint *b)
5843{
b775012e
LM
5844 char host_evals = 0;
5845 char target_evals = 0;
5846
5847 if (!b)
5848 return NULL;
5849
5850 if (!is_breakpoint (b))
5851 return NULL;
5852
5853 if (gdb_evaluates_breakpoint_condition_p ()
5854 || !target_supports_evaluation_of_breakpoint_conditions ())
5855 return condition_evaluation_host;
5856
40cb8ca5 5857 for (bp_location *bl : b->locations ())
b775012e
LM
5858 {
5859 if (bl->cond_bytecode)
5860 target_evals++;
5861 else
5862 host_evals++;
5863 }
5864
5865 if (host_evals && target_evals)
5866 return condition_evaluation_both;
5867 else if (target_evals)
5868 return condition_evaluation_target;
5869 else
5870 return condition_evaluation_host;
5871}
5872
5873/* Determine the breakpoint location's condition evaluator. This is
5874 similar to bp_condition_evaluator, but for locations. */
5875
5876static const char *
5877bp_location_condition_evaluator (struct bp_location *bl)
5878{
5879 if (bl && !is_breakpoint (bl->owner))
5880 return NULL;
5881
5882 if (gdb_evaluates_breakpoint_condition_p ()
5883 || !target_supports_evaluation_of_breakpoint_conditions ())
5884 return condition_evaluation_host;
5885
5886 if (bl && bl->cond_bytecode)
5887 return condition_evaluation_target;
5888 else
5889 return condition_evaluation_host;
5890}
5891
859825b8
JK
5892/* Print the LOC location out of the list of B->LOC locations. */
5893
170b53b2
UW
5894static void
5895print_breakpoint_location (struct breakpoint *b,
5896 struct bp_location *loc)
0d381245 5897{
79a45e25 5898 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5899
5900 scoped_restore_current_program_space restore_pspace;
6c95b8df 5901
859825b8
JK
5902 if (loc != NULL && loc->shlib_disabled)
5903 loc = NULL;
5904
6c95b8df
PA
5905 if (loc != NULL)
5906 set_current_program_space (loc->pspace);
5907
56435ebe 5908 if (b->display_canonical)
d28cd78a 5909 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5910 else if (loc && loc->symtab)
0d381245 5911 {
4a27f119
KS
5912 const struct symbol *sym = loc->symbol;
5913
0d381245
VP
5914 if (sym)
5915 {
112e8700 5916 uiout->text ("in ");
987012b8 5917 uiout->field_string ("func", sym->print_name (),
e43b10e1 5918 function_name_style.style ());
112e8700
SM
5919 uiout->text (" ");
5920 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5921 uiout->text ("at ");
0d381245 5922 }
112e8700 5923 uiout->field_string ("file",
cbe56571 5924 symtab_to_filename_for_display (loc->symtab),
e43b10e1 5925 file_name_style.style ());
112e8700 5926 uiout->text (":");
05cba821 5927
112e8700
SM
5928 if (uiout->is_mi_like_p ())
5929 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5930
381befee 5931 uiout->field_signed ("line", loc->line_number);
0d381245 5932 }
859825b8 5933 else if (loc)
0d381245 5934 {
d7e74731 5935 string_file stb;
170b53b2 5936
d7e74731 5937 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5938 demangle, "");
112e8700 5939 uiout->field_stream ("at", stb);
0d381245 5940 }
859825b8 5941 else
f00aae0f 5942 {
d28cd78a
TT
5943 uiout->field_string ("pending",
5944 event_location_to_string (b->location.get ()));
f00aae0f
KS
5945 /* If extra_string is available, it could be holding a condition
5946 or dprintf arguments. In either case, make sure it is printed,
5947 too, but only for non-MI streams. */
112e8700 5948 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5949 {
5950 if (b->type == bp_dprintf)
112e8700 5951 uiout->text (",");
f00aae0f 5952 else
112e8700
SM
5953 uiout->text (" ");
5954 uiout->text (b->extra_string);
f00aae0f
KS
5955 }
5956 }
6c95b8df 5957
b775012e
LM
5958 if (loc && is_breakpoint (b)
5959 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5960 && bp_condition_evaluator (b) == condition_evaluation_both)
5961 {
112e8700
SM
5962 uiout->text (" (");
5963 uiout->field_string ("evaluated-by",
b775012e 5964 bp_location_condition_evaluator (loc));
112e8700 5965 uiout->text (")");
b775012e 5966 }
0d381245
VP
5967}
5968
269b11a2
PA
5969static const char *
5970bptype_string (enum bptype type)
c906108c 5971{
c4093a6a
JM
5972 struct ep_type_description
5973 {
5974 enum bptype type;
a121b7c1 5975 const char *description;
c4093a6a
JM
5976 };
5977 static struct ep_type_description bptypes[] =
c906108c 5978 {
c5aa993b
JM
5979 {bp_none, "?deleted?"},
5980 {bp_breakpoint, "breakpoint"},
c906108c 5981 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5982 {bp_single_step, "sw single-step"},
c5aa993b
JM
5983 {bp_until, "until"},
5984 {bp_finish, "finish"},
5985 {bp_watchpoint, "watchpoint"},
c906108c 5986 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5987 {bp_read_watchpoint, "read watchpoint"},
5988 {bp_access_watchpoint, "acc watchpoint"},
5989 {bp_longjmp, "longjmp"},
5990 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5991 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5992 {bp_exception, "exception"},
5993 {bp_exception_resume, "exception resume"},
c5aa993b 5994 {bp_step_resume, "step resume"},
2c03e5be 5995 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5996 {bp_watchpoint_scope, "watchpoint scope"},
5997 {bp_call_dummy, "call dummy"},
aa7d318d 5998 {bp_std_terminate, "std::terminate"},
c5aa993b 5999 {bp_shlib_event, "shlib events"},
c4093a6a 6000 {bp_thread_event, "thread events"},
1900040c 6001 {bp_overlay_event, "overlay events"},
0fd8e87f 6002 {bp_longjmp_master, "longjmp master"},
aa7d318d 6003 {bp_std_terminate_master, "std::terminate master"},
186c406b 6004 {bp_exception_master, "exception master"},
ce78b96d 6005 {bp_catchpoint, "catchpoint"},
1042e4c0 6006 {bp_tracepoint, "tracepoint"},
7a697b8d 6007 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6008 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6009 {bp_dprintf, "dprintf"},
4efc6507 6010 {bp_jit_event, "jit events"},
0e30163f
JK
6011 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6012 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6013 };
269b11a2
PA
6014
6015 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6016 || ((int) type != bptypes[(int) type].type))
6017 internal_error (__FILE__, __LINE__,
6018 _("bptypes table does not describe type #%d."),
6019 (int) type);
6020
6021 return bptypes[(int) type].description;
6022}
6023
998580f1
MK
6024/* For MI, output a field named 'thread-groups' with a list as the value.
6025 For CLI, prefix the list with the string 'inf'. */
6026
6027static void
6028output_thread_groups (struct ui_out *uiout,
6029 const char *field_name,
5c632425 6030 const std::vector<int> &inf_nums,
998580f1
MK
6031 int mi_only)
6032{
112e8700 6033 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6034
6035 /* For backward compatibility, don't display inferiors in CLI unless
6036 there are several. Always display them for MI. */
6037 if (!is_mi && mi_only)
6038 return;
6039
10f489e5 6040 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6041
5c632425 6042 for (size_t i = 0; i < inf_nums.size (); i++)
998580f1
MK
6043 {
6044 if (is_mi)
6045 {
6046 char mi_group[10];
6047
5c632425 6048 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf_nums[i]);
112e8700 6049 uiout->field_string (NULL, mi_group);
998580f1
MK
6050 }
6051 else
6052 {
6053 if (i == 0)
112e8700 6054 uiout->text (" inf ");
998580f1 6055 else
112e8700 6056 uiout->text (", ");
998580f1 6057
5c632425 6058 uiout->text (plongest (inf_nums[i]));
998580f1
MK
6059 }
6060 }
998580f1
MK
6061}
6062
a38118e5
PA
6063/* Print B to gdb_stdout. If RAW_LOC, print raw breakpoint locations
6064 instead of going via breakpoint_ops::print_one. This makes "maint
6065 info breakpoints" show the software breakpoint locations of
6066 catchpoints, which are considered internal implementation
6067 detail. */
269b11a2
PA
6068
6069static void
6070print_one_breakpoint_location (struct breakpoint *b,
6071 struct bp_location *loc,
6072 int loc_number,
6073 struct bp_location **last_loc,
a38118e5 6074 int allflag, bool raw_loc)
269b11a2
PA
6075{
6076 struct command_line *l;
c2c6d25f 6077 static char bpenables[] = "nynny";
c906108c 6078
79a45e25 6079 struct ui_out *uiout = current_uiout;
0d381245
VP
6080 int header_of_multiple = 0;
6081 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6082 struct value_print_options opts;
6083
6084 get_user_print_options (&opts);
0d381245
VP
6085
6086 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6087 /* See comment in print_one_breakpoint concerning treatment of
6088 breakpoints with single disabled location. */
0d381245
VP
6089 if (loc == NULL
6090 && (b->loc != NULL
b5fa468f
TBA
6091 && (b->loc->next != NULL
6092 || !b->loc->enabled || b->loc->disabled_by_cond)))
0d381245
VP
6093 header_of_multiple = 1;
6094 if (loc == NULL)
6095 loc = b->loc;
6096
c4093a6a
JM
6097 annotate_record ();
6098
6099 /* 1 */
6100 annotate_field (0);
0d381245 6101 if (part_of_multiple)
528e1572 6102 uiout->field_fmt ("number", "%d.%d", b->number, loc_number);
0d381245 6103 else
381befee 6104 uiout->field_signed ("number", b->number);
c4093a6a
JM
6105
6106 /* 2 */
6107 annotate_field (1);
0d381245 6108 if (part_of_multiple)
112e8700 6109 uiout->field_skip ("type");
269b11a2 6110 else
112e8700 6111 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6112
6113 /* 3 */
6114 annotate_field (2);
0d381245 6115 if (part_of_multiple)
112e8700 6116 uiout->field_skip ("disp");
0d381245 6117 else
112e8700 6118 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6119
c4093a6a
JM
6120 /* 4 */
6121 annotate_field (3);
85c88e2a
TBA
6122 /* For locations that are disabled because of an invalid condition,
6123 display "N*" on CLI, where "*" refers to a footnote below the
6124 table. For MI, simply display a "N" without a footnote. */
6125 const char *N = (uiout->is_mi_like_p ()) ? "N" : "N*";
0d381245 6126 if (part_of_multiple)
85c88e2a 6127 uiout->field_string ("enabled", (loc->disabled_by_cond ? N
b5fa468f 6128 : (loc->enabled ? "y" : "n")));
0d381245 6129 else
112e8700 6130 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
0d381245 6131
c4093a6a 6132 /* 5 and 6 */
a38118e5 6133 if (!raw_loc && b->ops != NULL && b->ops->print_one != NULL)
b58a68fe 6134 b->ops->print_one (b, last_loc);
3086aeae 6135 else
a38118e5
PA
6136 {
6137 if (is_watchpoint (b))
3a5c3e22
PA
6138 {
6139 struct watchpoint *w = (struct watchpoint *) b;
6140
6141 /* Field 4, the address, is omitted (which makes the columns
6142 not line up too nicely with the headers, but the effect
6143 is relatively readable). */
6144 if (opts.addressprint)
112e8700 6145 uiout->field_skip ("addr");
3a5c3e22 6146 annotate_field (5);
112e8700 6147 uiout->field_string ("what", w->exp_string);
3a5c3e22 6148 }
f06f1252
TT
6149 else if (!is_catchpoint (b) || is_exception_catchpoint (b)
6150 || is_ada_exception_catchpoint (b))
a38118e5
PA
6151 {
6152 if (opts.addressprint)
6153 {
6154 annotate_field (4);
6155 if (header_of_multiple)
7f6aba03
TT
6156 uiout->field_string ("addr", "<MULTIPLE>",
6157 metadata_style.style ());
a38118e5 6158 else if (b->loc == NULL || loc->shlib_disabled)
7f6aba03
TT
6159 uiout->field_string ("addr", "<PENDING>",
6160 metadata_style.style ());
a38118e5
PA
6161 else
6162 uiout->field_core_addr ("addr",
6163 loc->gdbarch, loc->address);
6164 }
6165 annotate_field (5);
6166 if (!header_of_multiple)
6167 print_breakpoint_location (b, loc);
6168 if (b->loc)
6169 *last_loc = b->loc;
6170 }
6171 }
6c95b8df 6172
998580f1 6173 if (loc != NULL && !header_of_multiple)
6c95b8df 6174 {
5c632425 6175 std::vector<int> inf_nums;
998580f1 6176 int mi_only = 1;
6c95b8df 6177
08036331 6178 for (inferior *inf : all_inferiors ())
6c95b8df
PA
6179 {
6180 if (inf->pspace == loc->pspace)
5c632425 6181 inf_nums.push_back (inf->num);
6c95b8df 6182 }
998580f1 6183
dda83cd7 6184 /* For backward compatibility, don't display inferiors in CLI unless
998580f1
MK
6185 there are several. Always display for MI. */
6186 if (allflag
6187 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
94c93c35 6188 && (program_spaces.size () > 1
998580f1
MK
6189 || number_of_inferiors () > 1)
6190 /* LOC is for existing B, it cannot be in
6191 moribund_locations and thus having NULL OWNER. */
6192 && loc->owner->type != bp_catchpoint))
6193 mi_only = 0;
5c632425 6194 output_thread_groups (uiout, "thread-groups", inf_nums, mi_only);
6c95b8df
PA
6195 }
6196
4a306c9a 6197 if (!part_of_multiple)
c4093a6a 6198 {
4a306c9a
JB
6199 if (b->thread != -1)
6200 {
6201 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6202 "stop only in" line a little further down. */
112e8700 6203 uiout->text (" thread ");
381befee 6204 uiout->field_signed ("thread", b->thread);
4a306c9a
JB
6205 }
6206 else if (b->task != 0)
6207 {
112e8700 6208 uiout->text (" task ");
381befee 6209 uiout->field_signed ("task", b->task);
4a306c9a 6210 }
c4093a6a 6211 }
f1310107 6212
112e8700 6213 uiout->text ("\n");
f1310107 6214
348d480f 6215 if (!part_of_multiple)
f1310107
TJB
6216 b->ops->print_one_detail (b, uiout);
6217
0d381245 6218 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6219 {
6220 annotate_field (6);
112e8700 6221 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6222 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
dda83cd7 6223 the frame ID. */
112e8700 6224 uiout->field_core_addr ("frame",
5af949e3 6225 b->gdbarch, b->frame_id.stack_addr);
112e8700 6226 uiout->text ("\n");
c4093a6a
JM
6227 }
6228
28010a5d 6229 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6230 {
6231 annotate_field (7);
d77f58be 6232 if (is_tracepoint (b))
112e8700 6233 uiout->text ("\ttrace only if ");
1042e4c0 6234 else
112e8700
SM
6235 uiout->text ("\tstop only if ");
6236 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6237
6238 /* Print whether the target is doing the breakpoint's condition
6239 evaluation. If GDB is doing the evaluation, don't print anything. */
6240 if (is_breakpoint (b)
6241 && breakpoint_condition_evaluation_mode ()
6242 == condition_evaluation_target)
6243 {
6a831f06
PA
6244 uiout->message (" (%pF evals)",
6245 string_field ("evaluated-by",
6246 bp_condition_evaluator (b)));
b775012e 6247 }
112e8700 6248 uiout->text ("\n");
0101ce28
JJ
6249 }
6250
0d381245 6251 if (!part_of_multiple && b->thread != -1)
c4093a6a 6252 {
4a64f543 6253 /* FIXME should make an annotation for this. */
112e8700
SM
6254 uiout->text ("\tstop only in thread ");
6255 if (uiout->is_mi_like_p ())
381befee 6256 uiout->field_signed ("thread", b->thread);
5d5658a1
PA
6257 else
6258 {
6259 struct thread_info *thr = find_thread_global_id (b->thread);
6260
112e8700 6261 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6262 }
112e8700 6263 uiout->text ("\n");
c4093a6a
JM
6264 }
6265
556ec64d
YQ
6266 if (!part_of_multiple)
6267 {
6268 if (b->hit_count)
31f56a27
YQ
6269 {
6270 /* FIXME should make an annotation for this. */
6271 if (is_catchpoint (b))
112e8700 6272 uiout->text ("\tcatchpoint");
31f56a27 6273 else if (is_tracepoint (b))
112e8700 6274 uiout->text ("\ttracepoint");
31f56a27 6275 else
112e8700
SM
6276 uiout->text ("\tbreakpoint");
6277 uiout->text (" already hit ");
381befee 6278 uiout->field_signed ("times", b->hit_count);
31f56a27 6279 if (b->hit_count == 1)
112e8700 6280 uiout->text (" time\n");
31f56a27 6281 else
112e8700 6282 uiout->text (" times\n");
31f56a27 6283 }
556ec64d
YQ
6284 else
6285 {
31f56a27 6286 /* Output the count also if it is zero, but only if this is mi. */
112e8700 6287 if (uiout->is_mi_like_p ())
381befee 6288 uiout->field_signed ("times", b->hit_count);
556ec64d
YQ
6289 }
6290 }
8b93c638 6291
0d381245 6292 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6293 {
6294 annotate_field (8);
6a831f06
PA
6295 uiout->message ("\tignore next %pF hits\n",
6296 signed_field ("ignore", b->ignore_count));
c4093a6a 6297 }
059fb39f 6298
816338b5
SS
6299 /* Note that an enable count of 1 corresponds to "enable once"
6300 behavior, which is reported by the combination of enablement and
6301 disposition, so we don't need to mention it here. */
6302 if (!part_of_multiple && b->enable_count > 1)
6303 {
6304 annotate_field (8);
112e8700 6305 uiout->text ("\tdisable after ");
816338b5
SS
6306 /* Tweak the wording to clarify that ignore and enable counts
6307 are distinct, and have additive effect. */
6308 if (b->ignore_count)
112e8700 6309 uiout->text ("additional ");
816338b5 6310 else
112e8700 6311 uiout->text ("next ");
381befee 6312 uiout->field_signed ("enable", b->enable_count);
112e8700 6313 uiout->text (" hits\n");
816338b5
SS
6314 }
6315
f196051f
SS
6316 if (!part_of_multiple && is_tracepoint (b))
6317 {
6318 struct tracepoint *tp = (struct tracepoint *) b;
6319
6320 if (tp->traceframe_usage)
6321 {
112e8700 6322 uiout->text ("\ttrace buffer usage ");
381befee 6323 uiout->field_signed ("traceframe-usage", tp->traceframe_usage);
112e8700 6324 uiout->text (" bytes\n");
f196051f
SS
6325 }
6326 }
d3ce09f5 6327
d1b0a7bf 6328 l = b->commands ? b->commands.get () : NULL;
059fb39f 6329 if (!part_of_multiple && l)
c4093a6a
JM
6330 {
6331 annotate_field (9);
2e783024 6332 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6333 print_command_lines (uiout, l, 4);
c4093a6a 6334 }
d24317b4 6335
d9b3f62e 6336 if (is_tracepoint (b))
1042e4c0 6337 {
d9b3f62e
PA
6338 struct tracepoint *t = (struct tracepoint *) b;
6339
6340 if (!part_of_multiple && t->pass_count)
6341 {
6342 annotate_field (10);
112e8700 6343 uiout->text ("\tpass count ");
381befee 6344 uiout->field_signed ("pass", t->pass_count);
112e8700 6345 uiout->text (" \n");
d9b3f62e 6346 }
f2a8bc8a
YQ
6347
6348 /* Don't display it when tracepoint or tracepoint location is
6349 pending. */
6350 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6351 {
6352 annotate_field (11);
6353
112e8700
SM
6354 if (uiout->is_mi_like_p ())
6355 uiout->field_string ("installed",
f2a8bc8a
YQ
6356 loc->inserted ? "y" : "n");
6357 else
6358 {
6359 if (loc->inserted)
112e8700 6360 uiout->text ("\t");
f2a8bc8a 6361 else
112e8700
SM
6362 uiout->text ("\tnot ");
6363 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6364 }
6365 }
1042e4c0
SS
6366 }
6367
112e8700 6368 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6369 {
3a5c3e22
PA
6370 if (is_watchpoint (b))
6371 {
6372 struct watchpoint *w = (struct watchpoint *) b;
6373
112e8700 6374 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6375 }
f00aae0f 6376 else if (b->location != NULL
d28cd78a 6377 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6378 uiout->field_string ("original-location",
d28cd78a 6379 event_location_to_string (b->location.get ()));
d24317b4 6380 }
c4093a6a 6381}
c5aa993b 6382
13674803
SM
6383/* See breakpoint.h. */
6384
6385bool fix_multi_location_breakpoint_output_globally = false;
6386
0d381245
VP
6387static void
6388print_one_breakpoint (struct breakpoint *b,
4a64f543 6389 struct bp_location **last_loc,
6c95b8df 6390 int allflag)
0d381245 6391{
79a45e25 6392 struct ui_out *uiout = current_uiout;
13674803
SM
6393 bool use_fixed_output
6394 = (uiout->test_flags (fix_multi_location_breakpoint_output)
6395 || fix_multi_location_breakpoint_output_globally);
8d3788bd 6396
b4be1b06 6397 gdb::optional<ui_out_emit_tuple> bkpt_tuple_emitter (gdb::in_place, uiout, "bkpt");
a38118e5 6398 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag, false);
8d3788bd 6399
b4be1b06
SM
6400 /* The mi2 broken format: the main breakpoint tuple ends here, the locations
6401 are outside. */
6402 if (!use_fixed_output)
6403 bkpt_tuple_emitter.reset ();
0d381245
VP
6404
6405 /* If this breakpoint has custom print function,
6406 it's already printed. Otherwise, print individual
6407 locations, if any. */
a38118e5
PA
6408 if (b->ops == NULL
6409 || b->ops->print_one == NULL
6410 || allflag)
0d381245 6411 {
4a64f543
MS
6412 /* If breakpoint has a single location that is disabled, we
6413 print it as if it had several locations, since otherwise it's
6414 hard to represent "breakpoint enabled, location disabled"
6415 situation.
6416
6417 Note that while hardware watchpoints have several locations
a38118e5
PA
6418 internally, that's not a property exposed to users.
6419
6420 Likewise, while catchpoints may be implemented with
6421 breakpoints (e.g., catch throw), that's not a property
6422 exposed to users. We do however display the internal
6423 breakpoint locations with "maint info breakpoints". */
6424 if (!is_hardware_watchpoint (b)
f06f1252
TT
6425 && (!is_catchpoint (b) || is_exception_catchpoint (b)
6426 || is_ada_exception_catchpoint (b))
a38118e5 6427 && (allflag
b5fa468f
TBA
6428 || (b->loc && (b->loc->next
6429 || !b->loc->enabled
6430 || b->loc->disabled_by_cond))))
0d381245 6431 {
b4be1b06
SM
6432 gdb::optional<ui_out_emit_list> locations_list;
6433
6434 /* For MI version <= 2, keep the behavior where GDB outputs an invalid
6435 MI record. For later versions, place breakpoint locations in a
6436 list. */
6437 if (uiout->is_mi_like_p () && use_fixed_output)
6438 locations_list.emplace (uiout, "locations");
8d3788bd 6439
b4be1b06 6440 int n = 1;
40cb8ca5 6441 for (bp_location *loc : b->locations ())
8d3788bd 6442 {
b4be1b06 6443 ui_out_emit_tuple loc_tuple_emitter (uiout, NULL);
a38118e5
PA
6444 print_one_breakpoint_location (b, loc, n, last_loc,
6445 allflag, allflag);
40cb8ca5 6446 n++;
8d3788bd 6447 }
0d381245
VP
6448 }
6449 }
6450}
6451
a6d9a66e
UW
6452static int
6453breakpoint_address_bits (struct breakpoint *b)
6454{
6455 int print_address_bits = 0;
a6d9a66e 6456
c6d81124
PA
6457 /* Software watchpoints that aren't watching memory don't have an
6458 address to print. */
6459 if (is_no_memory_software_watchpoint (b))
6460 return 0;
6461
40cb8ca5 6462 for (bp_location *loc : b->locations ())
a6d9a66e 6463 {
c7437ca6
PA
6464 int addr_bit;
6465
c7437ca6 6466 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6467 if (addr_bit > print_address_bits)
6468 print_address_bits = addr_bit;
6469 }
6470
6471 return print_address_bits;
6472}
0d381245 6473
65630365 6474/* See breakpoint.h. */
c5aa993b 6475
65630365
PA
6476void
6477print_breakpoint (breakpoint *b)
c4093a6a 6478{
a6d9a66e 6479 struct bp_location *dummy_loc = NULL;
65630365 6480 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6481}
c5aa993b 6482
09d682a4
TT
6483/* Return true if this breakpoint was set by the user, false if it is
6484 internal or momentary. */
6485
6486int
6487user_breakpoint_p (struct breakpoint *b)
6488{
46c6471b 6489 return b->number > 0;
09d682a4
TT
6490}
6491
93daf339
TT
6492/* See breakpoint.h. */
6493
6494int
6495pending_breakpoint_p (struct breakpoint *b)
6496{
6497 return b->loc == NULL;
6498}
6499
5c458ae8
SM
6500/* Print information on breakpoints (including watchpoints and tracepoints).
6501
6502 If non-NULL, BP_NUM_LIST is a list of numbers and number ranges as
6503 understood by number_or_range_parser. Only breakpoints included in this
6504 list are then printed.
6505
6506 If SHOW_INTERNAL is true, print internal breakpoints.
6507
6508 If FILTER is non-NULL, call it on each breakpoint and only include the
6509 ones for which it returns true.
6510
6511 Return the total number of breakpoints listed. */
c906108c 6512
d77f58be 6513static int
5c458ae8 6514breakpoint_1 (const char *bp_num_list, bool show_internal,
f2478a7e 6515 bool (*filter) (const struct breakpoint *))
c4093a6a 6516{
a6d9a66e 6517 struct bp_location *last_loc = NULL;
7f3b0473 6518 int nr_printable_breakpoints;
79a45b7d 6519 struct value_print_options opts;
a6d9a66e 6520 int print_address_bits = 0;
269b11a2 6521 int print_type_col_width = 14;
79a45e25 6522 struct ui_out *uiout = current_uiout;
b5fa468f 6523 bool has_disabled_by_cond_location = false;
269b11a2 6524
79a45b7d
TT
6525 get_user_print_options (&opts);
6526
4a64f543
MS
6527 /* Compute the number of rows in the table, as well as the size
6528 required for address fields. */
7f3b0473 6529 nr_printable_breakpoints = 0;
43892fdf 6530 for (breakpoint *b : all_breakpoints ())
e5a67952
MS
6531 {
6532 /* If we have a filter, only list the breakpoints it accepts. */
6533 if (filter && !filter (b))
6534 continue;
6535
5c458ae8 6536 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
e5a67952 6537 accept. Skip the others. */
5c458ae8 6538 if (bp_num_list != NULL && *bp_num_list != '\0')
e5a67952 6539 {
5c458ae8 6540 if (show_internal && parse_and_eval_long (bp_num_list) != b->number)
e5a67952 6541 continue;
5c458ae8 6542 if (!show_internal && !number_is_in_list (bp_num_list, b->number))
e5a67952
MS
6543 continue;
6544 }
269b11a2 6545
5c458ae8 6546 if (show_internal || user_breakpoint_p (b))
e5a67952
MS
6547 {
6548 int addr_bit, type_len;
a6d9a66e 6549
e5a67952
MS
6550 addr_bit = breakpoint_address_bits (b);
6551 if (addr_bit > print_address_bits)
6552 print_address_bits = addr_bit;
269b11a2 6553
e5a67952
MS
6554 type_len = strlen (bptype_string (b->type));
6555 if (type_len > print_type_col_width)
6556 print_type_col_width = type_len;
6557
6558 nr_printable_breakpoints++;
6559 }
6560 }
7f3b0473 6561
4a2b031d
TT
6562 {
6563 ui_out_emit_table table_emitter (uiout,
6564 opts.addressprint ? 6 : 5,
6565 nr_printable_breakpoints,
6566 "BreakpointTable");
6567
6568 if (nr_printable_breakpoints > 0)
6569 annotate_breakpoints_headers ();
6570 if (nr_printable_breakpoints > 0)
6571 annotate_field (0);
6572 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6573 if (nr_printable_breakpoints > 0)
6574 annotate_field (1);
6575 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6576 if (nr_printable_breakpoints > 0)
6577 annotate_field (2);
6578 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6579 if (nr_printable_breakpoints > 0)
6580 annotate_field (3);
6581 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6582 if (opts.addressprint)
6583 {
6584 if (nr_printable_breakpoints > 0)
6585 annotate_field (4);
6586 if (print_address_bits <= 32)
6587 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6588 else
6589 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6590 }
6591 if (nr_printable_breakpoints > 0)
6592 annotate_field (5);
6593 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6594 uiout->table_body ();
6595 if (nr_printable_breakpoints > 0)
6596 annotate_breakpoints_table ();
6597
43892fdf 6598 for (breakpoint *b : all_breakpoints ())
4a2b031d
TT
6599 {
6600 QUIT;
6601 /* If we have a filter, only list the breakpoints it accepts. */
6602 if (filter && !filter (b))
6603 continue;
e5a67952 6604
5c458ae8 6605 /* If we have a BP_NUM_LIST string, it is a list of breakpoints to
4a2b031d 6606 accept. Skip the others. */
e5a67952 6607
5c458ae8 6608 if (bp_num_list != NULL && *bp_num_list != '\0')
4a2b031d 6609 {
5c458ae8 6610 if (show_internal) /* maintenance info breakpoint */
4a2b031d 6611 {
5c458ae8 6612 if (parse_and_eval_long (bp_num_list) != b->number)
4a2b031d
TT
6613 continue;
6614 }
6615 else /* all others */
6616 {
5c458ae8 6617 if (!number_is_in_list (bp_num_list, b->number))
4a2b031d
TT
6618 continue;
6619 }
6620 }
6621 /* We only print out user settable breakpoints unless the
5c458ae8
SM
6622 show_internal is set. */
6623 if (show_internal || user_breakpoint_p (b))
b5fa468f
TBA
6624 {
6625 print_one_breakpoint (b, &last_loc, show_internal);
40cb8ca5 6626 for (bp_location *loc : b->locations ())
b5fa468f
TBA
6627 if (loc->disabled_by_cond)
6628 has_disabled_by_cond_location = true;
6629 }
4a2b031d
TT
6630 }
6631 }
698384cd 6632
7f3b0473 6633 if (nr_printable_breakpoints == 0)
c906108c 6634 {
4a64f543
MS
6635 /* If there's a filter, let the caller decide how to report
6636 empty list. */
d77f58be
SS
6637 if (!filter)
6638 {
5c458ae8 6639 if (bp_num_list == NULL || *bp_num_list == '\0')
112e8700 6640 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6641 else
112e8700 6642 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
5c458ae8 6643 bp_num_list);
d77f58be 6644 }
c906108c
SS
6645 }
6646 else
c4093a6a 6647 {
a6d9a66e
UW
6648 if (last_loc && !server_command)
6649 set_next_address (last_loc->gdbarch, last_loc->address);
b5fa468f 6650
85c88e2a 6651 if (has_disabled_by_cond_location && !uiout->is_mi_like_p ())
b5fa468f
TBA
6652 uiout->message (_("(*): Breakpoint condition is invalid at this "
6653 "location.\n"));
c4093a6a 6654 }
c906108c 6655
4a64f543 6656 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6657 there have been breakpoints? */
c906108c 6658 annotate_breakpoints_table_end ();
d77f58be
SS
6659
6660 return nr_printable_breakpoints;
c906108c
SS
6661}
6662
ad443146
SS
6663/* Display the value of default-collect in a way that is generally
6664 compatible with the breakpoint list. */
6665
6666static void
6667default_collect_info (void)
6668{
79a45e25
PA
6669 struct ui_out *uiout = current_uiout;
6670
ad443146
SS
6671 /* If it has no value (which is frequently the case), say nothing; a
6672 message like "No default-collect." gets in user's face when it's
6673 not wanted. */
6674 if (!*default_collect)
6675 return;
6676
6677 /* The following phrase lines up nicely with per-tracepoint collect
6678 actions. */
112e8700
SM
6679 uiout->text ("default collect ");
6680 uiout->field_string ("default-collect", default_collect);
6681 uiout->text (" \n");
ad443146
SS
6682}
6683
c906108c 6684static void
0b39b52e 6685info_breakpoints_command (const char *args, int from_tty)
c906108c 6686{
5c458ae8 6687 breakpoint_1 (args, false, NULL);
ad443146
SS
6688
6689 default_collect_info ();
d77f58be
SS
6690}
6691
6692static void
1d12d88f 6693info_watchpoints_command (const char *args, int from_tty)
d77f58be 6694{
5c458ae8 6695 int num_printed = breakpoint_1 (args, false, is_watchpoint);
79a45e25 6696 struct ui_out *uiout = current_uiout;
d77f58be
SS
6697
6698 if (num_printed == 0)
6699 {
e5a67952 6700 if (args == NULL || *args == '\0')
112e8700 6701 uiout->message ("No watchpoints.\n");
d77f58be 6702 else
112e8700 6703 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6704 }
c906108c
SS
6705}
6706
7a292a7a 6707static void
4495129a 6708maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6709{
5c458ae8 6710 breakpoint_1 (args, true, NULL);
ad443146
SS
6711
6712 default_collect_info ();
c906108c
SS
6713}
6714
0d381245 6715static int
714835d5 6716breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6717 struct program_space *pspace,
714835d5 6718 CORE_ADDR pc, struct obj_section *section)
0d381245 6719{
40cb8ca5 6720 for (bp_location *bl : b->locations ())
0d381245 6721 {
6c95b8df
PA
6722 if (bl->pspace == pspace
6723 && bl->address == pc
0d381245
VP
6724 && (!overlay_debugging || bl->section == section))
6725 return 1;
6726 }
6727 return 0;
6728}
6729
672f9b60 6730/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6731 concerns with logical breakpoints, so we match program spaces, not
6732 address spaces. */
c906108c
SS
6733
6734static void
6c95b8df
PA
6735describe_other_breakpoints (struct gdbarch *gdbarch,
6736 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6737 struct obj_section *section, int thread)
c906108c 6738{
52f0bd74 6739 int others = 0;
c906108c 6740
43892fdf 6741 for (breakpoint *b : all_breakpoints ())
672f9b60 6742 others += (user_breakpoint_p (b)
dda83cd7 6743 && breakpoint_has_pc (b, pspace, pc, section));
43892fdf 6744
c906108c
SS
6745 if (others > 0)
6746 {
a3f17187
AC
6747 if (others == 1)
6748 printf_filtered (_("Note: breakpoint "));
6749 else /* if (others == ???) */
6750 printf_filtered (_("Note: breakpoints "));
43892fdf 6751 for (breakpoint *b : all_breakpoints ())
672f9b60 6752 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6753 {
6754 others--;
6755 printf_filtered ("%d", b->number);
6756 if (b->thread == -1 && thread != -1)
6757 printf_filtered (" (all threads)");
6758 else if (b->thread != -1)
6759 printf_filtered (" (thread %d)", b->thread);
6760 printf_filtered ("%s%s ",
059fb39f 6761 ((b->enable_state == bp_disabled
f8eba3c6 6762 || b->enable_state == bp_call_disabled)
0d381245 6763 ? " (disabled)"
0d381245
VP
6764 : ""),
6765 (others > 1) ? ","
6766 : ((others == 1) ? " and" : ""));
6767 }
6a831f06
PA
6768 current_uiout->message (_("also set at pc %ps.\n"),
6769 styled_string (address_style.style (),
6770 paddress (gdbarch, pc)));
c906108c
SS
6771 }
6772}
6773\f
c906108c 6774
cb1e4e32
PA
6775/* Return true iff it is meaningful to use the address member of LOC.
6776 For some breakpoint types, the locations' address members are
6777 irrelevant and it makes no sense to attempt to compare them to
6778 other addresses (or use them for any other purpose either).
2d134ed3 6779
cb1e4e32
PA
6780 More specifically, software watchpoints and catchpoints that are
6781 not backed by breakpoints always have a zero valued location
6782 address and we don't want to mark breakpoints of any of these types
6783 to be a duplicate of an actual breakpoint location at address
6784 zero. */
e4f237da 6785
cb1e4e32
PA
6786static bool
6787bl_address_is_meaningful (bp_location *loc)
e4f237da 6788{
cb1e4e32 6789 return loc->loc_type != bp_loc_other;
2d134ed3
PA
6790}
6791
6792/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6793 true if LOC1 and LOC2 represent the same watchpoint location. */
6794
6795static int
4a64f543
MS
6796watchpoint_locations_match (struct bp_location *loc1,
6797 struct bp_location *loc2)
2d134ed3 6798{
3a5c3e22
PA
6799 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6800 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6801
6802 /* Both of them must exist. */
6803 gdb_assert (w1 != NULL);
6804 gdb_assert (w2 != NULL);
2bdf28a0 6805
4a64f543
MS
6806 /* If the target can evaluate the condition expression in hardware,
6807 then we we need to insert both watchpoints even if they are at
6808 the same place. Otherwise the watchpoint will only trigger when
6809 the condition of whichever watchpoint was inserted evaluates to
6810 true, not giving a chance for GDB to check the condition of the
6811 other watchpoint. */
3a5c3e22 6812 if ((w1->cond_exp
4a64f543
MS
6813 && target_can_accel_watchpoint_condition (loc1->address,
6814 loc1->length,
0cf6dd15 6815 loc1->watchpoint_type,
4d01a485 6816 w1->cond_exp.get ()))
3a5c3e22 6817 || (w2->cond_exp
4a64f543
MS
6818 && target_can_accel_watchpoint_condition (loc2->address,
6819 loc2->length,
0cf6dd15 6820 loc2->watchpoint_type,
4d01a485 6821 w2->cond_exp.get ())))
0cf6dd15
TJB
6822 return 0;
6823
85d721b8
PA
6824 /* Note that this checks the owner's type, not the location's. In
6825 case the target does not support read watchpoints, but does
6826 support access watchpoints, we'll have bp_read_watchpoint
6827 watchpoints with hw_access locations. Those should be considered
6828 duplicates of hw_read locations. The hw_read locations will
6829 become hw_access locations later. */
2d134ed3
PA
6830 return (loc1->owner->type == loc2->owner->type
6831 && loc1->pspace->aspace == loc2->pspace->aspace
6832 && loc1->address == loc2->address
6833 && loc1->length == loc2->length);
e4f237da
KB
6834}
6835
31e77af2 6836/* See breakpoint.h. */
6c95b8df 6837
31e77af2 6838int
accd0bcd
YQ
6839breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6840 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6841{
f5656ead 6842 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6843 || aspace1 == aspace2)
6844 && addr1 == addr2);
6845}
6846
f1310107
TJB
6847/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6848 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6849 matches ASPACE2. On targets that have global breakpoints, the address
6850 space doesn't really matter. */
6851
6852static int
accd0bcd
YQ
6853breakpoint_address_match_range (const address_space *aspace1,
6854 CORE_ADDR addr1,
6855 int len1, const address_space *aspace2,
f1310107
TJB
6856 CORE_ADDR addr2)
6857{
f5656ead 6858 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6859 || aspace1 == aspace2)
6860 && addr2 >= addr1 && addr2 < addr1 + len1);
6861}
6862
6863/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6864 a ranged breakpoint. In most targets, a match happens only if ASPACE
6865 matches the breakpoint's address space. On targets that have global
6866 breakpoints, the address space doesn't really matter. */
6867
6868static int
6869breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6870 const address_space *aspace,
f1310107
TJB
6871 CORE_ADDR addr)
6872{
6873 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6874 aspace, addr)
6875 || (bl->length
6876 && breakpoint_address_match_range (bl->pspace->aspace,
6877 bl->address, bl->length,
6878 aspace, addr)));
6879}
6880
d35ae833
PA
6881/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6882 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6883 match happens only if ASPACE matches the breakpoint's address
6884 space. On targets that have global breakpoints, the address space
6885 doesn't really matter. */
6886
6887static int
6888breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6889 const address_space *aspace,
d35ae833
PA
6890 CORE_ADDR addr, int len)
6891{
6892 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6893 || bl->pspace->aspace == aspace)
6894 {
6895 int bl_len = bl->length != 0 ? bl->length : 1;
6896
6897 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6898 return 1;
6899 }
6900 return 0;
6901}
6902
1e4d1764
YQ
6903/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6904 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6905 true, otherwise returns false. */
6906
6907static int
6908tracepoint_locations_match (struct bp_location *loc1,
6909 struct bp_location *loc2)
6910{
6911 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6912 /* Since tracepoint locations are never duplicated with others', tracepoint
6913 locations at the same address of different tracepoints are regarded as
6914 different locations. */
6915 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6916 else
6917 return 0;
6918}
6919
2d134ed3 6920/* Assuming LOC1 and LOC2's types' have meaningful target addresses
cb1e4e32 6921 (bl_address_is_meaningful), returns true if LOC1 and LOC2 represent
7f32a4d5
PA
6922 the same location. If SW_HW_BPS_MATCH is true, then software
6923 breakpoint locations and hardware breakpoint locations match,
6924 otherwise they don't. */
2d134ed3
PA
6925
6926static int
7f32a4d5
PA
6927breakpoint_locations_match (struct bp_location *loc1,
6928 struct bp_location *loc2,
6929 bool sw_hw_bps_match)
2d134ed3 6930{
2bdf28a0
JK
6931 int hw_point1, hw_point2;
6932
6933 /* Both of them must not be in moribund_locations. */
6934 gdb_assert (loc1->owner != NULL);
6935 gdb_assert (loc2->owner != NULL);
6936
6937 hw_point1 = is_hardware_watchpoint (loc1->owner);
6938 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6939
6940 if (hw_point1 != hw_point2)
6941 return 0;
6942 else if (hw_point1)
6943 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6944 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6945 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6946 else
7f32a4d5
PA
6947 /* We compare bp_location.length in order to cover ranged
6948 breakpoints. Keep this in sync with
6949 bp_location_is_less_than. */
f1310107
TJB
6950 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6951 loc2->pspace->aspace, loc2->address)
7f32a4d5 6952 && (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
f1310107 6953 && loc1->length == loc2->length);
2d134ed3
PA
6954}
6955
76897487
KB
6956static void
6957breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
dda83cd7 6958 int bnum, int have_bnum)
76897487 6959{
f63fbe86
MS
6960 /* The longest string possibly returned by hex_string_custom
6961 is 50 chars. These must be at least that big for safety. */
6962 char astr1[64];
6963 char astr2[64];
76897487 6964
bb599908
PH
6965 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6966 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6967 if (have_bnum)
8a3fe4f8 6968 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
dda83cd7 6969 bnum, astr1, astr2);
76897487 6970 else
8a3fe4f8 6971 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6972}
6973
4a64f543
MS
6974/* Adjust a breakpoint's address to account for architectural
6975 constraints on breakpoint placement. Return the adjusted address.
6976 Note: Very few targets require this kind of adjustment. For most
6977 targets, this function is simply the identity function. */
76897487
KB
6978
6979static CORE_ADDR
a6d9a66e
UW
6980adjust_breakpoint_address (struct gdbarch *gdbarch,
6981 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6982{
a0de8c21
YQ
6983 if (bptype == bp_watchpoint
6984 || bptype == bp_hardware_watchpoint
6985 || bptype == bp_read_watchpoint
6986 || bptype == bp_access_watchpoint
6987 || bptype == bp_catchpoint)
88f7da05
KB
6988 {
6989 /* Watchpoints and the various bp_catch_* eventpoints should not
dda83cd7 6990 have their addresses modified. */
88f7da05
KB
6991 return bpaddr;
6992 }
7c16b83e
PA
6993 else if (bptype == bp_single_step)
6994 {
6995 /* Single-step breakpoints should not have their addresses
6996 modified. If there's any architectural constrain that
6997 applies to this address, then it should have already been
6998 taken into account when the breakpoint was created in the
6999 first place. If we didn't do this, stepping through e.g.,
7000 Thumb-2 IT blocks would break. */
7001 return bpaddr;
7002 }
76897487
KB
7003 else
7004 {
a0de8c21
YQ
7005 CORE_ADDR adjusted_bpaddr = bpaddr;
7006
7007 if (gdbarch_adjust_breakpoint_address_p (gdbarch))
7008 {
7009 /* Some targets have architectural constraints on the placement
7010 of breakpoint instructions. Obtain the adjusted address. */
7011 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7012 }
76897487 7013
a0de8c21 7014 adjusted_bpaddr = address_significant (gdbarch, adjusted_bpaddr);
76897487
KB
7015
7016 /* An adjusted breakpoint address can significantly alter
dda83cd7 7017 a user's expectations. Print a warning if an adjustment
76897487
KB
7018 is required. */
7019 if (adjusted_bpaddr != bpaddr)
7020 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7021
7022 return adjusted_bpaddr;
7023 }
7024}
7025
cb1e4e32
PA
7026static bp_loc_type
7027bp_location_from_bp_type (bptype type)
7cc221ef 7028{
cb1e4e32 7029 switch (type)
e049a4b5
DJ
7030 {
7031 case bp_breakpoint:
7c16b83e 7032 case bp_single_step:
e049a4b5
DJ
7033 case bp_until:
7034 case bp_finish:
7035 case bp_longjmp:
7036 case bp_longjmp_resume:
e2e4d78b 7037 case bp_longjmp_call_dummy:
186c406b
TT
7038 case bp_exception:
7039 case bp_exception_resume:
e049a4b5 7040 case bp_step_resume:
2c03e5be 7041 case bp_hp_step_resume:
e049a4b5
DJ
7042 case bp_watchpoint_scope:
7043 case bp_call_dummy:
aa7d318d 7044 case bp_std_terminate:
e049a4b5
DJ
7045 case bp_shlib_event:
7046 case bp_thread_event:
7047 case bp_overlay_event:
4efc6507 7048 case bp_jit_event:
0fd8e87f 7049 case bp_longjmp_master:
aa7d318d 7050 case bp_std_terminate_master:
186c406b 7051 case bp_exception_master:
0e30163f
JK
7052 case bp_gnu_ifunc_resolver:
7053 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7054 case bp_dprintf:
cb1e4e32 7055 return bp_loc_software_breakpoint;
e049a4b5 7056 case bp_hardware_breakpoint:
cb1e4e32 7057 return bp_loc_hardware_breakpoint;
e049a4b5
DJ
7058 case bp_hardware_watchpoint:
7059 case bp_read_watchpoint:
7060 case bp_access_watchpoint:
cb1e4e32 7061 return bp_loc_hardware_watchpoint;
e049a4b5 7062 case bp_watchpoint:
ce78b96d 7063 case bp_catchpoint:
15c3d785
PA
7064 case bp_tracepoint:
7065 case bp_fast_tracepoint:
0fb4aa4b 7066 case bp_static_tracepoint:
cb1e4e32 7067 return bp_loc_other;
e049a4b5 7068 default:
e2e0b3e5 7069 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5 7070 }
cb1e4e32
PA
7071}
7072
7073bp_location::bp_location (breakpoint *owner, bp_loc_type type)
7074{
7075 this->owner = owner;
7076 this->cond_bytecode = NULL;
7077 this->shlib_disabled = 0;
7078 this->enabled = 1;
b5fa468f 7079 this->disabled_by_cond = false;
cb1e4e32
PA
7080
7081 this->loc_type = type;
e049a4b5 7082
cb1e4e32
PA
7083 if (this->loc_type == bp_loc_software_breakpoint
7084 || this->loc_type == bp_loc_hardware_breakpoint)
7085 mark_breakpoint_location_modified (this);
7086
b6433ede 7087 incref ();
cb1e4e32
PA
7088}
7089
7090bp_location::bp_location (breakpoint *owner)
7091 : bp_location::bp_location (owner,
7092 bp_location_from_bp_type (owner->type))
7093{
28010a5d
PA
7094}
7095
7096/* Allocate a struct bp_location. */
7097
7098static struct bp_location *
7099allocate_bp_location (struct breakpoint *bpt)
7100{
348d480f
PA
7101 return bpt->ops->allocate_location (bpt);
7102}
7cc221ef 7103
f431efe5
PA
7104/* Decrement reference count. If the reference count reaches 0,
7105 destroy the bp_location. Sets *BLP to NULL. */
7106
7107static void
7108decref_bp_location (struct bp_location **blp)
7109{
b6433ede 7110 bp_location_ref_policy::decref (*blp);
f431efe5
PA
7111 *blp = NULL;
7112}
7113
346774a9 7114/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7115
b270e6f9
TT
7116static breakpoint *
7117add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7118{
346774a9 7119 struct breakpoint *b1;
b270e6f9 7120 struct breakpoint *result = b.get ();
c906108c 7121
346774a9
PA
7122 /* Add this breakpoint to the end of the chain so that a list of
7123 breakpoints will come out in order of increasing numbers. */
7124
7125 b1 = breakpoint_chain;
7126 if (b1 == 0)
b270e6f9 7127 breakpoint_chain = b.release ();
346774a9
PA
7128 else
7129 {
7130 while (b1->next)
7131 b1 = b1->next;
b270e6f9 7132 b1->next = b.release ();
346774a9 7133 }
b270e6f9
TT
7134
7135 return result;
346774a9
PA
7136}
7137
7138/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7139
7140static void
7141init_raw_breakpoint_without_location (struct breakpoint *b,
7142 struct gdbarch *gdbarch,
28010a5d 7143 enum bptype bptype,
c0a91b2b 7144 const struct breakpoint_ops *ops)
346774a9 7145{
348d480f
PA
7146 gdb_assert (ops != NULL);
7147
28010a5d 7148 b->ops = ops;
4d28f7a8 7149 b->type = bptype;
a6d9a66e 7150 b->gdbarch = gdbarch;
c906108c
SS
7151 b->language = current_language->la_language;
7152 b->input_radix = input_radix;
d0fb5eae 7153 b->related_breakpoint = b;
346774a9
PA
7154}
7155
7156/* Helper to set_raw_breakpoint below. Creates a breakpoint
7157 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7158
7159static struct breakpoint *
7160set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7161 enum bptype bptype,
c0a91b2b 7162 const struct breakpoint_ops *ops)
346774a9 7163{
3b0871f4 7164 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7165
3b0871f4 7166 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7167 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7168}
7169
0ba852ab 7170/* Initialize loc->function_name. */
0e30163f 7171
0d381245 7172static void
0ba852ab 7173set_breakpoint_location_function (struct bp_location *loc)
0d381245 7174{
2bdf28a0
JK
7175 gdb_assert (loc->owner != NULL);
7176
0d381245 7177 if (loc->owner->type == bp_breakpoint
1042e4c0 7178 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7179 || is_tracepoint (loc->owner))
0d381245 7180 {
2c02bd72 7181 const char *function_name;
0e30163f 7182
3467ec66 7183 if (loc->msymbol != NULL
f50776aa 7184 && (MSYMBOL_TYPE (loc->msymbol) == mst_text_gnu_ifunc
0ba852ab 7185 || MSYMBOL_TYPE (loc->msymbol) == mst_data_gnu_ifunc))
0e30163f
JK
7186 {
7187 struct breakpoint *b = loc->owner;
7188
c9d95fa3 7189 function_name = loc->msymbol->linkage_name ();
3467ec66
PA
7190
7191 if (b->type == bp_breakpoint && b->loc == loc
7192 && loc->next == NULL && b->related_breakpoint == b)
0e30163f
JK
7193 {
7194 /* Create only the whole new breakpoint of this type but do not
7195 mess more complicated breakpoints with multiple locations. */
7196 b->type = bp_gnu_ifunc_resolver;
6a3a010b 7197 /* Remember the resolver's address for use by the return
dda83cd7 7198 breakpoint. */
3467ec66 7199 loc->related_address = loc->address;
0e30163f
JK
7200 }
7201 }
3467ec66
PA
7202 else
7203 find_pc_partial_function (loc->address, &function_name, NULL, NULL);
0e30163f 7204
2c02bd72
DE
7205 if (function_name)
7206 loc->function_name = xstrdup (function_name);
0d381245
VP
7207 }
7208}
7209
a6d9a66e 7210/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7211struct gdbarch *
a6d9a66e
UW
7212get_sal_arch (struct symtab_and_line sal)
7213{
7214 if (sal.section)
08feed99 7215 return sal.section->objfile->arch ();
a6d9a66e 7216 if (sal.symtab)
08feed99 7217 return SYMTAB_OBJFILE (sal.symtab)->arch ();
a6d9a66e
UW
7218
7219 return NULL;
7220}
7221
346774a9
PA
7222/* Low level routine for partially initializing a breakpoint of type
7223 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7224 file name, and line number are provided by SAL.
0d381245
VP
7225
7226 It is expected that the caller will complete the initialization of
7227 the newly created breakpoint struct as well as output any status
c56053d2 7228 information regarding the creation of a new breakpoint. */
0d381245 7229
346774a9
PA
7230static void
7231init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7232 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7233 const struct breakpoint_ops *ops)
0d381245 7234{
28010a5d 7235 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7236
3742cc8b 7237 add_location_to_breakpoint (b, &sal);
0d381245 7238
6c95b8df
PA
7239 if (bptype != bp_catchpoint)
7240 gdb_assert (sal.pspace != NULL);
7241
f8eba3c6
TT
7242 /* Store the program space that was used to set the breakpoint,
7243 except for ordinary breakpoints, which are independent of the
7244 program space. */
7245 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7246 b->pspace = sal.pspace;
346774a9 7247}
c906108c 7248
346774a9
PA
7249/* set_raw_breakpoint is a low level routine for allocating and
7250 partially initializing a breakpoint of type BPTYPE. The newly
7251 created breakpoint's address, section, source file name, and line
7252 number are provided by SAL. The newly created and partially
7253 initialized breakpoint is added to the breakpoint chain and
7254 is also returned as the value of this function.
7255
7256 It is expected that the caller will complete the initialization of
7257 the newly created breakpoint struct as well as output any status
7258 information regarding the creation of a new breakpoint. In
7259 particular, set_raw_breakpoint does NOT set the breakpoint
7260 number! Care should be taken to not allow an error to occur
7261 prior to completing the initialization of the breakpoint. If this
7262 should happen, a bogus breakpoint will be left on the chain. */
7263
7264struct breakpoint *
7265set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7266 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7267 const struct breakpoint_ops *ops)
346774a9 7268{
3b0871f4 7269 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7270
3b0871f4 7271 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7272 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7273}
7274
53a5351d 7275/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7276 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7277 initiated the operation. */
c906108c
SS
7278
7279void
186c406b 7280set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7281{
5d5658a1 7282 int thread = tp->global_num;
0fd8e87f
UW
7283
7284 /* To avoid having to rescan all objfile symbols at every step,
7285 we maintain a list of continually-inserted but always disabled
7286 longjmp "master" breakpoints. Here, we simply create momentary
7287 clones of those and enable them for the requested thread. */
1428b37a 7288 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df 7289 if (b->pspace == current_program_space
186c406b
TT
7290 && (b->type == bp_longjmp_master
7291 || b->type == bp_exception_master))
0fd8e87f 7292 {
06edf0c0
PA
7293 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7294 struct breakpoint *clone;
cc59ec59 7295
e2e4d78b
JK
7296 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7297 after their removal. */
06edf0c0 7298 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7299 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7300 clone->thread = thread;
7301 }
186c406b
TT
7302
7303 tp->initiating_frame = frame;
c906108c
SS
7304}
7305
611c83ae 7306/* Delete all longjmp breakpoints from THREAD. */
c906108c 7307void
611c83ae 7308delete_longjmp_breakpoint (int thread)
c906108c 7309{
1428b37a 7310 for (breakpoint *b : all_breakpoints_safe ())
186c406b 7311 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7312 {
7313 if (b->thread == thread)
7314 delete_breakpoint (b);
7315 }
c906108c
SS
7316}
7317
f59f708a
PA
7318void
7319delete_longjmp_breakpoint_at_next_stop (int thread)
7320{
1428b37a 7321 for (breakpoint *b : all_breakpoints_safe ())
f59f708a
PA
7322 if (b->type == bp_longjmp || b->type == bp_exception)
7323 {
7324 if (b->thread == thread)
7325 b->disposition = disp_del_at_next_stop;
7326 }
7327}
7328
e2e4d78b
JK
7329/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7330 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7331 pointer to any of them. Return NULL if this system cannot place longjmp
7332 breakpoints. */
7333
7334struct breakpoint *
7335set_longjmp_breakpoint_for_call_dummy (void)
7336{
43892fdf 7337 breakpoint *retval = nullptr;
e2e4d78b 7338
43892fdf 7339 for (breakpoint *b : all_breakpoints ())
e2e4d78b
JK
7340 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7341 {
7342 struct breakpoint *new_b;
7343
7344 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7345 &momentary_breakpoint_ops,
7346 1);
00431a78 7347 new_b->thread = inferior_thread ()->global_num;
e2e4d78b
JK
7348
7349 /* Link NEW_B into the chain of RETVAL breakpoints. */
7350
7351 gdb_assert (new_b->related_breakpoint == new_b);
7352 if (retval == NULL)
7353 retval = new_b;
7354 new_b->related_breakpoint = retval;
7355 while (retval->related_breakpoint != new_b->related_breakpoint)
7356 retval = retval->related_breakpoint;
7357 retval->related_breakpoint = new_b;
7358 }
7359
7360 return retval;
7361}
7362
7363/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7364 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7365 stack.
7366
7367 You should call this function only at places where it is safe to currently
7368 unwind the whole stack. Failed stack unwind would discard live dummy
7369 frames. */
7370
7371void
b67a2c6f 7372check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7373{
7374 struct breakpoint *b, *b_tmp;
7375
7376 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7377 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7378 {
7379 struct breakpoint *dummy_b = b->related_breakpoint;
7380
7381 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7382 dummy_b = dummy_b->related_breakpoint;
7383 if (dummy_b->type != bp_call_dummy
7384 || frame_find_by_id (dummy_b->frame_id) != NULL)
7385 continue;
7386
00431a78 7387 dummy_frame_discard (dummy_b->frame_id, tp);
e2e4d78b
JK
7388
7389 while (b->related_breakpoint != b)
7390 {
7391 if (b_tmp == b->related_breakpoint)
7392 b_tmp = b->related_breakpoint->next;
7393 delete_breakpoint (b->related_breakpoint);
7394 }
7395 delete_breakpoint (b);
7396 }
7397}
7398
1900040c
MS
7399void
7400enable_overlay_breakpoints (void)
7401{
43892fdf 7402 for (breakpoint *b : all_breakpoints ())
1900040c
MS
7403 if (b->type == bp_overlay_event)
7404 {
7405 b->enable_state = bp_enabled;
44702360 7406 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7407 overlay_events_enabled = 1;
1900040c
MS
7408 }
7409}
7410
7411void
7412disable_overlay_breakpoints (void)
7413{
43892fdf 7414 for (breakpoint *b : all_breakpoints ())
1900040c
MS
7415 if (b->type == bp_overlay_event)
7416 {
7417 b->enable_state = bp_disabled;
44702360 7418 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7419 overlay_events_enabled = 0;
1900040c
MS
7420 }
7421}
7422
aa7d318d
TT
7423/* Set an active std::terminate breakpoint for each std::terminate
7424 master breakpoint. */
7425void
7426set_std_terminate_breakpoint (void)
7427{
1428b37a 7428 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7429 if (b->pspace == current_program_space
7430 && b->type == bp_std_terminate_master)
7431 {
06edf0c0 7432 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7433 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7434 }
7435}
7436
7437/* Delete all the std::terminate breakpoints. */
7438void
7439delete_std_terminate_breakpoint (void)
7440{
1428b37a 7441 for (breakpoint *b : all_breakpoints_safe ())
aa7d318d
TT
7442 if (b->type == bp_std_terminate)
7443 delete_breakpoint (b);
7444}
7445
c4093a6a 7446struct breakpoint *
a6d9a66e 7447create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7448{
7449 struct breakpoint *b;
c4093a6a 7450
06edf0c0
PA
7451 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7452 &internal_breakpoint_ops);
7453
b5de0fa7 7454 b->enable_state = bp_enabled;
f00aae0f 7455 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7456 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7457
44702360 7458 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7459
c4093a6a
JM
7460 return b;
7461}
7462
0101ce28
JJ
7463struct lang_and_radix
7464 {
7465 enum language lang;
7466 int radix;
7467 };
7468
4efc6507
DE
7469/* Create a breakpoint for JIT code registration and unregistration. */
7470
7471struct breakpoint *
7472create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7473{
2a7f3dff
PA
7474 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7475 &internal_breakpoint_ops);
4efc6507 7476}
0101ce28 7477
03673fc7
PP
7478/* Remove JIT code registration and unregistration breakpoint(s). */
7479
7480void
7481remove_jit_event_breakpoints (void)
7482{
1428b37a 7483 for (breakpoint *b : all_breakpoints_safe ())
03673fc7
PP
7484 if (b->type == bp_jit_event
7485 && b->loc->pspace == current_program_space)
7486 delete_breakpoint (b);
7487}
7488
cae688ec
JJ
7489void
7490remove_solib_event_breakpoints (void)
7491{
1428b37a 7492 for (breakpoint *b : all_breakpoints_safe ())
6c95b8df
PA
7493 if (b->type == bp_shlib_event
7494 && b->loc->pspace == current_program_space)
cae688ec
JJ
7495 delete_breakpoint (b);
7496}
7497
f37f681c
PA
7498/* See breakpoint.h. */
7499
7500void
7501remove_solib_event_breakpoints_at_next_stop (void)
7502{
1428b37a 7503 for (breakpoint *b : all_breakpoints_safe ())
f37f681c
PA
7504 if (b->type == bp_shlib_event
7505 && b->loc->pspace == current_program_space)
7506 b->disposition = disp_del_at_next_stop;
7507}
7508
04086b45
PA
7509/* Helper for create_solib_event_breakpoint /
7510 create_and_insert_solib_event_breakpoint. Allows specifying which
7511 INSERT_MODE to pass through to update_global_location_list. */
7512
7513static struct breakpoint *
7514create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7515 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7516{
7517 struct breakpoint *b;
7518
06edf0c0
PA
7519 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7520 &internal_breakpoint_ops);
04086b45 7521 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7522 return b;
7523}
7524
04086b45
PA
7525struct breakpoint *
7526create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7527{
7528 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7529}
7530
f37f681c
PA
7531/* See breakpoint.h. */
7532
7533struct breakpoint *
7534create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7535{
7536 struct breakpoint *b;
7537
04086b45
PA
7538 /* Explicitly tell update_global_location_list to insert
7539 locations. */
7540 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7541 if (!b->loc->inserted)
7542 {
7543 delete_breakpoint (b);
7544 return NULL;
7545 }
7546 return b;
7547}
7548
cae688ec
JJ
7549/* Disable any breakpoints that are on code in shared libraries. Only
7550 apply to enabled breakpoints, disabled ones can just stay disabled. */
7551
7552void
cb851954 7553disable_breakpoints_in_shlibs (void)
cae688ec 7554{
48d7020b 7555 for (bp_location *loc : all_bp_locations ())
cae688ec 7556 {
2bdf28a0 7557 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7558 struct breakpoint *b = loc->owner;
2bdf28a0 7559
4a64f543
MS
7560 /* We apply the check to all breakpoints, including disabled for
7561 those with loc->duplicate set. This is so that when breakpoint
7562 becomes enabled, or the duplicate is removed, gdb will try to
7563 insert all breakpoints. If we don't set shlib_disabled here,
7564 we'll try to insert those breakpoints and fail. */
1042e4c0 7565 if (((b->type == bp_breakpoint)
508ccb1f 7566 || (b->type == bp_jit_event)
1042e4c0 7567 || (b->type == bp_hardware_breakpoint)
d77f58be 7568 || (is_tracepoint (b)))
6c95b8df 7569 && loc->pspace == current_program_space
0d381245 7570 && !loc->shlib_disabled
6c95b8df 7571 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7572 )
0d381245
VP
7573 {
7574 loc->shlib_disabled = 1;
7575 }
cae688ec
JJ
7576 }
7577}
7578
63644780
NB
7579/* Disable any breakpoints and tracepoints that are in SOLIB upon
7580 notification of unloaded_shlib. Only apply to enabled breakpoints,
7581 disabled ones can just stay disabled. */
84acb35a 7582
75149521 7583static void
84acb35a
JJ
7584disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7585{
84acb35a
JJ
7586 int disabled_shlib_breaks = 0;
7587
48d7020b 7588 for (bp_location *loc : all_bp_locations ())
84acb35a 7589 {
2bdf28a0 7590 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7591 struct breakpoint *b = loc->owner;
cc59ec59 7592
1e4d1764 7593 if (solib->pspace == loc->pspace
e2dd7057 7594 && !loc->shlib_disabled
1e4d1764
YQ
7595 && (((b->type == bp_breakpoint
7596 || b->type == bp_jit_event
7597 || b->type == bp_hardware_breakpoint)
7598 && (loc->loc_type == bp_loc_hardware_breakpoint
7599 || loc->loc_type == bp_loc_software_breakpoint))
7600 || is_tracepoint (b))
e2dd7057 7601 && solib_contains_address_p (solib, loc->address))
84acb35a 7602 {
e2dd7057
PP
7603 loc->shlib_disabled = 1;
7604 /* At this point, we cannot rely on remove_breakpoint
7605 succeeding so we must mark the breakpoint as not inserted
7606 to prevent future errors occurring in remove_breakpoints. */
7607 loc->inserted = 0;
8d3788bd
VP
7608
7609 /* This may cause duplicate notifications for the same breakpoint. */
76727919 7610 gdb::observers::breakpoint_modified.notify (b);
8d3788bd 7611
e2dd7057
PP
7612 if (!disabled_shlib_breaks)
7613 {
223ffa71 7614 target_terminal::ours_for_output ();
3e43a32a
MS
7615 warning (_("Temporarily disabling breakpoints "
7616 "for unloaded shared library \"%s\""),
e2dd7057 7617 solib->so_name);
84acb35a 7618 }
e2dd7057 7619 disabled_shlib_breaks = 1;
84acb35a
JJ
7620 }
7621 }
84acb35a
JJ
7622}
7623
63644780
NB
7624/* Disable any breakpoints and tracepoints in OBJFILE upon
7625 notification of free_objfile. Only apply to enabled breakpoints,
7626 disabled ones can just stay disabled. */
7627
7628static void
7629disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7630{
63644780
NB
7631 if (objfile == NULL)
7632 return;
7633
d03de421
PA
7634 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7635 managed by the user with add-symbol-file/remove-symbol-file.
7636 Similarly to how breakpoints in shared libraries are handled in
7637 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7638 shlib_disabled so they end up uninserted on the next global
7639 location list update. Shared libraries not loaded by the user
7640 aren't handled here -- they're already handled in
7641 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7642 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7643 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7644 main objfile). */
7645 if ((objfile->flags & OBJF_SHARED) == 0
7646 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7647 return;
7648
43892fdf 7649 for (breakpoint *b : all_breakpoints ())
63644780 7650 {
63644780
NB
7651 int bp_modified = 0;
7652
7653 if (!is_breakpoint (b) && !is_tracepoint (b))
7654 continue;
7655
40cb8ca5 7656 for (bp_location *loc : b->locations ())
63644780
NB
7657 {
7658 CORE_ADDR loc_addr = loc->address;
7659
7660 if (loc->loc_type != bp_loc_hardware_breakpoint
7661 && loc->loc_type != bp_loc_software_breakpoint)
7662 continue;
7663
7664 if (loc->shlib_disabled != 0)
7665 continue;
7666
7667 if (objfile->pspace != loc->pspace)
7668 continue;
7669
7670 if (loc->loc_type != bp_loc_hardware_breakpoint
7671 && loc->loc_type != bp_loc_software_breakpoint)
7672 continue;
7673
7674 if (is_addr_in_objfile (loc_addr, objfile))
7675 {
7676 loc->shlib_disabled = 1;
08351840
PA
7677 /* At this point, we don't know whether the object was
7678 unmapped from the inferior or not, so leave the
7679 inserted flag alone. We'll handle failure to
7680 uninsert quietly, in case the object was indeed
7681 unmapped. */
63644780
NB
7682
7683 mark_breakpoint_location_modified (loc);
7684
7685 bp_modified = 1;
7686 }
7687 }
7688
7689 if (bp_modified)
76727919 7690 gdb::observers::breakpoint_modified.notify (b);
63644780
NB
7691 }
7692}
7693
ce78b96d
JB
7694/* FORK & VFORK catchpoints. */
7695
e29a4733 7696/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7697 catchpoint. A breakpoint is really of this type iff its ops pointer points
7698 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7699
c1fc2657 7700struct fork_catchpoint : public breakpoint
e29a4733 7701{
e29a4733
PA
7702 /* Process id of a child process whose forking triggered this
7703 catchpoint. This field is only valid immediately after this
7704 catchpoint has triggered. */
7705 ptid_t forked_inferior_pid;
7706};
7707
4a64f543
MS
7708/* Implement the "insert" breakpoint_ops method for fork
7709 catchpoints. */
ce78b96d 7710
77b06cd7
TJB
7711static int
7712insert_catch_fork (struct bp_location *bl)
ce78b96d 7713{
e99b03dc 7714 return target_insert_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7715}
7716
4a64f543
MS
7717/* Implement the "remove" breakpoint_ops method for fork
7718 catchpoints. */
ce78b96d
JB
7719
7720static int
73971819 7721remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7722{
e99b03dc 7723 return target_remove_fork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7724}
7725
7726/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7727 catchpoints. */
7728
7729static int
f1310107 7730breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7731 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7732 const struct target_waitstatus *ws)
ce78b96d 7733{
e29a4733
PA
7734 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7735
f90263c1
TT
7736 if (ws->kind != TARGET_WAITKIND_FORKED)
7737 return 0;
7738
7739 c->forked_inferior_pid = ws->value.related_pid;
7740 return 1;
ce78b96d
JB
7741}
7742
4a64f543
MS
7743/* Implement the "print_it" breakpoint_ops method for fork
7744 catchpoints. */
ce78b96d
JB
7745
7746static enum print_stop_action
348d480f 7747print_it_catch_fork (bpstat bs)
ce78b96d 7748{
36dfb11c 7749 struct ui_out *uiout = current_uiout;
348d480f
PA
7750 struct breakpoint *b = bs->breakpoint_at;
7751 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7752
ce78b96d 7753 annotate_catchpoint (b->number);
f303dbd6 7754 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7755 if (b->disposition == disp_del)
112e8700 7756 uiout->text ("Temporary catchpoint ");
36dfb11c 7757 else
112e8700
SM
7758 uiout->text ("Catchpoint ");
7759 if (uiout->is_mi_like_p ())
36dfb11c 7760 {
112e8700
SM
7761 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7762 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7763 }
381befee 7764 uiout->field_signed ("bkptno", b->number);
112e8700 7765 uiout->text (" (forked process ");
381befee 7766 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7767 uiout->text ("), ");
ce78b96d
JB
7768 return PRINT_SRC_AND_LOC;
7769}
7770
4a64f543
MS
7771/* Implement the "print_one" breakpoint_ops method for fork
7772 catchpoints. */
ce78b96d
JB
7773
7774static void
a6d9a66e 7775print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7776{
e29a4733 7777 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7778 struct value_print_options opts;
79a45e25 7779 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7780
7781 get_user_print_options (&opts);
7782
4a64f543
MS
7783 /* Field 4, the address, is omitted (which makes the columns not
7784 line up too nicely with the headers, but the effect is relatively
7785 readable). */
79a45b7d 7786 if (opts.addressprint)
112e8700 7787 uiout->field_skip ("addr");
ce78b96d 7788 annotate_field (5);
112e8700 7789 uiout->text ("fork");
d7e15655 7790 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7791 {
112e8700 7792 uiout->text (", process ");
381befee 7793 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7794 uiout->spaces (1);
ce78b96d 7795 }
8ac3646f 7796
112e8700
SM
7797 if (uiout->is_mi_like_p ())
7798 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7799}
7800
7801/* Implement the "print_mention" breakpoint_ops method for fork
7802 catchpoints. */
7803
7804static void
7805print_mention_catch_fork (struct breakpoint *b)
7806{
7807 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7808}
7809
6149aea9
PA
7810/* Implement the "print_recreate" breakpoint_ops method for fork
7811 catchpoints. */
7812
7813static void
7814print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7815{
7816 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7817 print_recreate_thread (b, fp);
6149aea9
PA
7818}
7819
ce78b96d
JB
7820/* The breakpoint_ops structure to be used in fork catchpoints. */
7821
2060206e 7822static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7823
4a64f543
MS
7824/* Implement the "insert" breakpoint_ops method for vfork
7825 catchpoints. */
ce78b96d 7826
77b06cd7
TJB
7827static int
7828insert_catch_vfork (struct bp_location *bl)
ce78b96d 7829{
e99b03dc 7830 return target_insert_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7831}
7832
4a64f543
MS
7833/* Implement the "remove" breakpoint_ops method for vfork
7834 catchpoints. */
ce78b96d
JB
7835
7836static int
73971819 7837remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7838{
e99b03dc 7839 return target_remove_vfork_catchpoint (inferior_ptid.pid ());
ce78b96d
JB
7840}
7841
7842/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7843 catchpoints. */
7844
7845static int
f1310107 7846breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7847 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7848 const struct target_waitstatus *ws)
ce78b96d 7849{
e29a4733
PA
7850 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7851
f90263c1
TT
7852 if (ws->kind != TARGET_WAITKIND_VFORKED)
7853 return 0;
7854
7855 c->forked_inferior_pid = ws->value.related_pid;
7856 return 1;
ce78b96d
JB
7857}
7858
4a64f543
MS
7859/* Implement the "print_it" breakpoint_ops method for vfork
7860 catchpoints. */
ce78b96d
JB
7861
7862static enum print_stop_action
348d480f 7863print_it_catch_vfork (bpstat bs)
ce78b96d 7864{
36dfb11c 7865 struct ui_out *uiout = current_uiout;
348d480f 7866 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7867 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7868
ce78b96d 7869 annotate_catchpoint (b->number);
f303dbd6 7870 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7871 if (b->disposition == disp_del)
112e8700 7872 uiout->text ("Temporary catchpoint ");
36dfb11c 7873 else
112e8700
SM
7874 uiout->text ("Catchpoint ");
7875 if (uiout->is_mi_like_p ())
36dfb11c 7876 {
112e8700
SM
7877 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7878 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7879 }
381befee 7880 uiout->field_signed ("bkptno", b->number);
112e8700 7881 uiout->text (" (vforked process ");
381befee 7882 uiout->field_signed ("newpid", c->forked_inferior_pid.pid ());
112e8700 7883 uiout->text ("), ");
ce78b96d
JB
7884 return PRINT_SRC_AND_LOC;
7885}
7886
4a64f543
MS
7887/* Implement the "print_one" breakpoint_ops method for vfork
7888 catchpoints. */
ce78b96d
JB
7889
7890static void
a6d9a66e 7891print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7892{
e29a4733 7893 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7894 struct value_print_options opts;
79a45e25 7895 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7896
7897 get_user_print_options (&opts);
4a64f543
MS
7898 /* Field 4, the address, is omitted (which makes the columns not
7899 line up too nicely with the headers, but the effect is relatively
7900 readable). */
79a45b7d 7901 if (opts.addressprint)
112e8700 7902 uiout->field_skip ("addr");
ce78b96d 7903 annotate_field (5);
112e8700 7904 uiout->text ("vfork");
d7e15655 7905 if (c->forked_inferior_pid != null_ptid)
ce78b96d 7906 {
112e8700 7907 uiout->text (", process ");
381befee 7908 uiout->field_signed ("what", c->forked_inferior_pid.pid ());
112e8700 7909 uiout->spaces (1);
ce78b96d 7910 }
8ac3646f 7911
112e8700
SM
7912 if (uiout->is_mi_like_p ())
7913 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7914}
7915
7916/* Implement the "print_mention" breakpoint_ops method for vfork
7917 catchpoints. */
7918
7919static void
7920print_mention_catch_vfork (struct breakpoint *b)
7921{
7922 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7923}
7924
6149aea9
PA
7925/* Implement the "print_recreate" breakpoint_ops method for vfork
7926 catchpoints. */
7927
7928static void
7929print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7930{
7931 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7932 print_recreate_thread (b, fp);
6149aea9
PA
7933}
7934
ce78b96d
JB
7935/* The breakpoint_ops structure to be used in vfork catchpoints. */
7936
2060206e 7937static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7938
edcc5120 7939/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7940 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7941 CATCH_SOLIB_BREAKPOINT_OPS. */
7942
c1fc2657 7943struct solib_catchpoint : public breakpoint
edcc5120 7944{
c1fc2657 7945 ~solib_catchpoint () override;
edcc5120
TT
7946
7947 /* True for "catch load", false for "catch unload". */
b650a282 7948 bool is_load;
edcc5120
TT
7949
7950 /* Regular expression to match, if any. COMPILED is only valid when
7951 REGEX is non-NULL. */
7952 char *regex;
2d7cc5c7 7953 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
7954};
7955
c1fc2657 7956solib_catchpoint::~solib_catchpoint ()
edcc5120 7957{
c1fc2657 7958 xfree (this->regex);
edcc5120
TT
7959}
7960
7961static int
7962insert_catch_solib (struct bp_location *ignore)
7963{
7964 return 0;
7965}
7966
7967static int
73971819 7968remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
7969{
7970 return 0;
7971}
7972
7973static int
7974breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 7975 const address_space *aspace,
edcc5120
TT
7976 CORE_ADDR bp_addr,
7977 const struct target_waitstatus *ws)
7978{
7979 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
edcc5120
TT
7980
7981 if (ws->kind == TARGET_WAITKIND_LOADED)
7982 return 1;
7983
43892fdf 7984 for (breakpoint *other : all_breakpoints ())
edcc5120 7985 {
edcc5120
TT
7986 if (other == bl->owner)
7987 continue;
7988
7989 if (other->type != bp_shlib_event)
7990 continue;
7991
c1fc2657 7992 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
7993 continue;
7994
40cb8ca5 7995 for (bp_location *other_bl : other->locations ())
edcc5120
TT
7996 {
7997 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7998 return 1;
7999 }
8000 }
8001
8002 return 0;
8003}
8004
8005static void
8006check_status_catch_solib (struct bpstats *bs)
8007{
8008 struct solib_catchpoint *self
8009 = (struct solib_catchpoint *) bs->breakpoint_at;
edcc5120
TT
8010
8011 if (self->is_load)
8012 {
52941706 8013 for (so_list *iter : current_program_space->added_solibs)
edcc5120
TT
8014 {
8015 if (!self->regex
2d7cc5c7 8016 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8017 return;
8018 }
8019 }
8020 else
8021 {
6fb16ce6 8022 for (const std::string &iter : current_program_space->deleted_solibs)
edcc5120
TT
8023 {
8024 if (!self->regex
6fb16ce6 8025 || self->compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
edcc5120
TT
8026 return;
8027 }
8028 }
8029
8030 bs->stop = 0;
8031 bs->print_it = print_it_noop;
8032}
8033
8034static enum print_stop_action
8035print_it_catch_solib (bpstat bs)
8036{
8037 struct breakpoint *b = bs->breakpoint_at;
8038 struct ui_out *uiout = current_uiout;
8039
8040 annotate_catchpoint (b->number);
f303dbd6 8041 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8042 if (b->disposition == disp_del)
112e8700 8043 uiout->text ("Temporary catchpoint ");
edcc5120 8044 else
112e8700 8045 uiout->text ("Catchpoint ");
381befee 8046 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8047 uiout->text ("\n");
8048 if (uiout->is_mi_like_p ())
8049 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8050 print_solib_event (1);
8051 return PRINT_SRC_AND_LOC;
8052}
8053
8054static void
8055print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8056{
8057 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8058 struct value_print_options opts;
8059 struct ui_out *uiout = current_uiout;
edcc5120
TT
8060
8061 get_user_print_options (&opts);
8062 /* Field 4, the address, is omitted (which makes the columns not
8063 line up too nicely with the headers, but the effect is relatively
8064 readable). */
8065 if (opts.addressprint)
8066 {
8067 annotate_field (4);
112e8700 8068 uiout->field_skip ("addr");
edcc5120
TT
8069 }
8070
528e1572 8071 std::string msg;
edcc5120
TT
8072 annotate_field (5);
8073 if (self->is_load)
8074 {
8075 if (self->regex)
528e1572 8076 msg = string_printf (_("load of library matching %s"), self->regex);
edcc5120 8077 else
528e1572 8078 msg = _("load of library");
edcc5120
TT
8079 }
8080 else
8081 {
8082 if (self->regex)
528e1572 8083 msg = string_printf (_("unload of library matching %s"), self->regex);
edcc5120 8084 else
528e1572 8085 msg = _("unload of library");
edcc5120 8086 }
112e8700 8087 uiout->field_string ("what", msg);
8ac3646f 8088
112e8700
SM
8089 if (uiout->is_mi_like_p ())
8090 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8091}
8092
8093static void
8094print_mention_catch_solib (struct breakpoint *b)
8095{
8096 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8097
8098 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8099 self->is_load ? "load" : "unload");
8100}
8101
8102static void
8103print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8104{
8105 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8106
8107 fprintf_unfiltered (fp, "%s %s",
8108 b->disposition == disp_del ? "tcatch" : "catch",
8109 self->is_load ? "load" : "unload");
8110 if (self->regex)
8111 fprintf_unfiltered (fp, " %s", self->regex);
8112 fprintf_unfiltered (fp, "\n");
8113}
8114
8115static struct breakpoint_ops catch_solib_breakpoint_ops;
8116
b650a282 8117/* See breakpoint.h. */
edcc5120 8118
91985142 8119void
b650a282 8120add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
edcc5120 8121{
edcc5120 8122 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8123
edcc5120
TT
8124 if (!arg)
8125 arg = "";
f1735a53 8126 arg = skip_spaces (arg);
edcc5120 8127
36bd8eaa 8128 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8129
8130 if (*arg != '\0')
8131 {
2d7cc5c7
PA
8132 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8133 _("Invalid regexp")));
edcc5120
TT
8134 c->regex = xstrdup (arg);
8135 }
8136
8137 c->is_load = is_load;
36bd8eaa 8138 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8139 &catch_solib_breakpoint_ops);
8140
c1fc2657 8141 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8142
b270e6f9 8143 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8144}
8145
91985142
MG
8146/* A helper function that does all the work for "catch load" and
8147 "catch unload". */
8148
8149static void
eb4c3f4a 8150catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8151 struct cmd_list_element *command)
8152{
91985142 8153 const int enabled = 1;
b650a282 8154 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
91985142 8155
b650a282 8156 add_solib_catchpoint (arg, is_load, temp, enabled);
91985142
MG
8157}
8158
edcc5120 8159static void
eb4c3f4a 8160catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8161 struct cmd_list_element *command)
8162{
8163 catch_load_or_unload (arg, from_tty, 1, command);
8164}
8165
8166static void
eb4c3f4a 8167catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8168 struct cmd_list_element *command)
8169{
8170 catch_load_or_unload (arg, from_tty, 0, command);
8171}
8172
b650a282 8173/* See breakpoint.h. */
ce78b96d 8174
ab04a2af 8175void
346774a9 8176init_catchpoint (struct breakpoint *b,
b650a282 8177 struct gdbarch *gdbarch, bool temp,
63160a43 8178 const char *cond_string,
c0a91b2b 8179 const struct breakpoint_ops *ops)
c906108c 8180{
51abb421 8181 symtab_and_line sal;
6c95b8df 8182 sal.pspace = current_program_space;
c5aa993b 8183
28010a5d 8184 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8185
1b36a34b 8186 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b650a282 8187 b->disposition = temp ? disp_del : disp_donttouch;
346774a9
PA
8188}
8189
28010a5d 8190void
b270e6f9 8191install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8192{
b270e6f9 8193 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8194 set_breakpoint_number (internal, b);
558a9d82
YQ
8195 if (is_tracepoint (b))
8196 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8197 if (!internal)
8198 mention (b);
76727919 8199 gdb::observers::breakpoint_created.notify (b);
3ea46bff
YQ
8200
8201 if (update_gll)
44702360 8202 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8203}
8204
9b70b993 8205static void
a6d9a66e 8206create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
b650a282 8207 bool temp, const char *cond_string,
dda83cd7 8208 const struct breakpoint_ops *ops)
c906108c 8209{
b270e6f9 8210 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8211
b650a282 8212 init_catchpoint (c.get (), gdbarch, temp, cond_string, ops);
e29a4733
PA
8213
8214 c->forked_inferior_pid = null_ptid;
8215
b270e6f9 8216 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8217}
8218
fe798b75
JB
8219/* Exec catchpoints. */
8220
b4d90040 8221/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8222 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8223 CATCH_EXEC_BREAKPOINT_OPS. */
8224
c1fc2657 8225struct exec_catchpoint : public breakpoint
b4d90040 8226{
c1fc2657 8227 ~exec_catchpoint () override;
b4d90040
PA
8228
8229 /* Filename of a program whose exec triggered this catchpoint.
8230 This field is only valid immediately after this catchpoint has
8231 triggered. */
8232 char *exec_pathname;
8233};
8234
c1fc2657 8235/* Exec catchpoint destructor. */
b4d90040 8236
c1fc2657 8237exec_catchpoint::~exec_catchpoint ()
b4d90040 8238{
c1fc2657 8239 xfree (this->exec_pathname);
b4d90040
PA
8240}
8241
77b06cd7
TJB
8242static int
8243insert_catch_exec (struct bp_location *bl)
c906108c 8244{
e99b03dc 8245 return target_insert_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8246}
c906108c 8247
fe798b75 8248static int
73971819 8249remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8250{
e99b03dc 8251 return target_remove_exec_catchpoint (inferior_ptid.pid ());
fe798b75 8252}
c906108c 8253
fe798b75 8254static int
f1310107 8255breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8256 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8257 const struct target_waitstatus *ws)
fe798b75 8258{
b4d90040
PA
8259 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8260
f90263c1
TT
8261 if (ws->kind != TARGET_WAITKIND_EXECD)
8262 return 0;
8263
8264 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8265 return 1;
fe798b75 8266}
c906108c 8267
fe798b75 8268static enum print_stop_action
348d480f 8269print_it_catch_exec (bpstat bs)
fe798b75 8270{
36dfb11c 8271 struct ui_out *uiout = current_uiout;
348d480f 8272 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8273 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8274
fe798b75 8275 annotate_catchpoint (b->number);
f303dbd6 8276 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8277 if (b->disposition == disp_del)
112e8700 8278 uiout->text ("Temporary catchpoint ");
36dfb11c 8279 else
112e8700
SM
8280 uiout->text ("Catchpoint ");
8281 if (uiout->is_mi_like_p ())
36dfb11c 8282 {
112e8700
SM
8283 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8284 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8285 }
381befee 8286 uiout->field_signed ("bkptno", b->number);
112e8700
SM
8287 uiout->text (" (exec'd ");
8288 uiout->field_string ("new-exec", c->exec_pathname);
8289 uiout->text ("), ");
36dfb11c 8290
fe798b75 8291 return PRINT_SRC_AND_LOC;
c906108c
SS
8292}
8293
fe798b75 8294static void
a6d9a66e 8295print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8296{
b4d90040 8297 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8298 struct value_print_options opts;
79a45e25 8299 struct ui_out *uiout = current_uiout;
fe798b75
JB
8300
8301 get_user_print_options (&opts);
8302
8303 /* Field 4, the address, is omitted (which makes the columns
8304 not line up too nicely with the headers, but the effect
8305 is relatively readable). */
8306 if (opts.addressprint)
112e8700 8307 uiout->field_skip ("addr");
fe798b75 8308 annotate_field (5);
112e8700 8309 uiout->text ("exec");
b4d90040 8310 if (c->exec_pathname != NULL)
fe798b75 8311 {
112e8700
SM
8312 uiout->text (", program \"");
8313 uiout->field_string ("what", c->exec_pathname);
8314 uiout->text ("\" ");
fe798b75 8315 }
8ac3646f 8316
112e8700
SM
8317 if (uiout->is_mi_like_p ())
8318 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8319}
8320
8321static void
8322print_mention_catch_exec (struct breakpoint *b)
8323{
8324 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8325}
8326
6149aea9
PA
8327/* Implement the "print_recreate" breakpoint_ops method for exec
8328 catchpoints. */
8329
8330static void
8331print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8332{
8333 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8334 print_recreate_thread (b, fp);
6149aea9
PA
8335}
8336
2060206e 8337static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8338
c906108c 8339static int
fba45db2 8340hw_breakpoint_used_count (void)
c906108c 8341{
c906108c
SS
8342 int i = 0;
8343
43892fdf 8344 for (breakpoint *b : all_breakpoints ())
d6b74ac4 8345 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
40cb8ca5 8346 for (bp_location *bl : b->locations ())
f1310107
TJB
8347 {
8348 /* Special types of hardware breakpoints may use more than
8349 one register. */
348d480f 8350 i += b->ops->resources_needed (bl);
f1310107 8351 }
c906108c
SS
8352
8353 return i;
8354}
8355
a1398e0c
PA
8356/* Returns the resources B would use if it were a hardware
8357 watchpoint. */
8358
c906108c 8359static int
a1398e0c 8360hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8361{
c906108c
SS
8362 int i = 0;
8363
a1398e0c
PA
8364 if (!breakpoint_enabled (b))
8365 return 0;
8366
40cb8ca5 8367 for (bp_location *bl : b->locations ())
a1398e0c
PA
8368 {
8369 /* Special types of hardware watchpoints may use more than
8370 one register. */
8371 i += b->ops->resources_needed (bl);
8372 }
8373
8374 return i;
8375}
8376
8377/* Returns the sum the used resources of all hardware watchpoints of
8378 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8379 the sum of the used resources of all hardware watchpoints of other
8380 types _not_ TYPE. */
8381
8382static int
8383hw_watchpoint_used_count_others (struct breakpoint *except,
8384 enum bptype type, int *other_type_used)
8385{
8386 int i = 0;
a1398e0c 8387
c906108c 8388 *other_type_used = 0;
43892fdf 8389 for (breakpoint *b : all_breakpoints ())
e09342b5 8390 {
a1398e0c
PA
8391 if (b == except)
8392 continue;
e09342b5
TJB
8393 if (!breakpoint_enabled (b))
8394 continue;
8395
a1398e0c
PA
8396 if (b->type == type)
8397 i += hw_watchpoint_use_count (b);
8398 else if (is_hardware_watchpoint (b))
8399 *other_type_used = 1;
e09342b5
TJB
8400 }
8401
c906108c
SS
8402 return i;
8403}
8404
c906108c 8405void
fba45db2 8406disable_watchpoints_before_interactive_call_start (void)
c906108c 8407{
43892fdf 8408 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8409 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8410 {
b5de0fa7 8411 b->enable_state = bp_call_disabled;
44702360 8412 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b 8413 }
c906108c
SS
8414}
8415
8416void
fba45db2 8417enable_watchpoints_after_interactive_call_stop (void)
c906108c 8418{
43892fdf 8419 for (breakpoint *b : all_breakpoints ())
cc60f2e3 8420 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8421 {
b5de0fa7 8422 b->enable_state = bp_enabled;
44702360 8423 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b 8424 }
c906108c
SS
8425}
8426
8bea4e01
UW
8427void
8428disable_breakpoints_before_startup (void)
8429{
6c95b8df 8430 current_program_space->executing_startup = 1;
44702360 8431 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8432}
8433
8434void
8435enable_breakpoints_after_startup (void)
8436{
6c95b8df 8437 current_program_space->executing_startup = 0;
f8eba3c6 8438 breakpoint_re_set ();
8bea4e01
UW
8439}
8440
7c16b83e
PA
8441/* Create a new single-step breakpoint for thread THREAD, with no
8442 locations. */
c906108c 8443
7c16b83e
PA
8444static struct breakpoint *
8445new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8446{
b270e6f9 8447 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8448
b270e6f9 8449 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8450 &momentary_breakpoint_ops);
8451
8452 b->disposition = disp_donttouch;
8453 b->frame_id = null_frame_id;
8454
8455 b->thread = thread;
8456 gdb_assert (b->thread != 0);
8457
b270e6f9 8458 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8459}
8460
8461/* Set a momentary breakpoint of type TYPE at address specified by
8462 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8463 frame. */
c906108c 8464
454dafbd 8465breakpoint_up
a6d9a66e
UW
8466set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8467 struct frame_id frame_id, enum bptype type)
c906108c 8468{
52f0bd74 8469 struct breakpoint *b;
edb3359d 8470
193facb3
JK
8471 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8472 tail-called one. */
8473 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8474
06edf0c0 8475 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8476 b->enable_state = bp_enabled;
8477 b->disposition = disp_donttouch;
818dd999 8478 b->frame_id = frame_id;
c906108c 8479
00431a78 8480 b->thread = inferior_thread ()->global_num;
c906108c 8481
44702360 8482 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8483
454dafbd 8484 return breakpoint_up (b);
c906108c 8485}
611c83ae 8486
06edf0c0 8487/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8488 The new breakpoint will have type TYPE, use OPS as its
8489 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8490
06edf0c0
PA
8491static struct breakpoint *
8492momentary_breakpoint_from_master (struct breakpoint *orig,
8493 enum bptype type,
a1aa2221
LM
8494 const struct breakpoint_ops *ops,
8495 int loc_enabled)
e58b0e63
PA
8496{
8497 struct breakpoint *copy;
8498
06edf0c0 8499 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8500 copy->loc = allocate_bp_location (copy);
0ba852ab 8501 set_breakpoint_location_function (copy->loc);
e58b0e63 8502
a6d9a66e 8503 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8504 copy->loc->requested_address = orig->loc->requested_address;
8505 copy->loc->address = orig->loc->address;
8506 copy->loc->section = orig->loc->section;
6c95b8df 8507 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8508 copy->loc->probe = orig->loc->probe;
f8eba3c6 8509 copy->loc->line_number = orig->loc->line_number;
2f202fde 8510 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8511 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8512 copy->frame_id = orig->frame_id;
8513 copy->thread = orig->thread;
6c95b8df 8514 copy->pspace = orig->pspace;
e58b0e63
PA
8515
8516 copy->enable_state = bp_enabled;
8517 copy->disposition = disp_donttouch;
8518 copy->number = internal_breakpoint_number--;
8519
44702360 8520 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8521 return copy;
8522}
8523
06edf0c0
PA
8524/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8525 ORIG is NULL. */
8526
8527struct breakpoint *
8528clone_momentary_breakpoint (struct breakpoint *orig)
8529{
8530 /* If there's nothing to clone, then return nothing. */
8531 if (orig == NULL)
8532 return NULL;
8533
a1aa2221 8534 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8535}
8536
454dafbd 8537breakpoint_up
a6d9a66e
UW
8538set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8539 enum bptype type)
611c83ae
PA
8540{
8541 struct symtab_and_line sal;
8542
8543 sal = find_pc_line (pc, 0);
8544 sal.pc = pc;
8545 sal.section = find_pc_overlay (pc);
8546 sal.explicit_pc = 1;
8547
a6d9a66e 8548 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8549}
c906108c 8550\f
c5aa993b 8551
c906108c
SS
8552/* Tell the user we have just set a breakpoint B. */
8553
8554static void
fba45db2 8555mention (struct breakpoint *b)
c906108c 8556{
348d480f 8557 b->ops->print_mention (b);
2d33446d 8558 current_uiout->text ("\n");
c906108c 8559}
c906108c 8560\f
c5aa993b 8561
5133a315 8562static bool bp_loc_is_permanent (struct bp_location *loc);
1a853c52 8563
7f32a4d5
PA
8564/* Handle "set breakpoint auto-hw on".
8565
8566 If the explicitly specified breakpoint type is not hardware
8567 breakpoint, check the memory map to see whether the breakpoint
8568 address is in read-only memory.
8569
8570 - location type is not hardware breakpoint, memory is read-only.
8571 We change the type of the location to hardware breakpoint.
8572
8573 - location type is hardware breakpoint, memory is read-write. This
8574 means we've previously made the location hardware one, but then the
8575 memory map changed, so we undo.
8576*/
8577
8578static void
8579handle_automatic_hardware_breakpoints (bp_location *bl)
8580{
8581 if (automatic_hardware_breakpoints
8582 && bl->owner->type != bp_hardware_breakpoint
8583 && (bl->loc_type == bp_loc_software_breakpoint
8584 || bl->loc_type == bp_loc_hardware_breakpoint))
8585 {
8586 /* When breakpoints are removed, remove_breakpoints will use
8587 location types we've just set here, the only possible problem
8588 is that memory map has changed during running program, but
8589 it's not going to work anyway with current gdb. */
8590 mem_region *mr = lookup_mem_region (bl->address);
8591
8592 if (mr != nullptr)
8593 {
8594 enum bp_loc_type new_type;
8595
8596 if (mr->attrib.mode != MEM_RW)
8597 new_type = bp_loc_hardware_breakpoint;
8598 else
8599 new_type = bp_loc_software_breakpoint;
8600
8601 if (new_type != bl->loc_type)
8602 {
8603 static bool said = false;
8604
8605 bl->loc_type = new_type;
8606 if (!said)
8607 {
8608 fprintf_filtered (gdb_stdout,
8609 _("Note: automatically using "
8610 "hardware breakpoints for "
8611 "read-only addresses.\n"));
8612 said = true;
8613 }
8614 }
8615 }
8616 }
8617}
8618
0d381245 8619static struct bp_location *
39d61571 8620add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8621 const struct symtab_and_line *sal)
8622{
8623 struct bp_location *loc, **tmp;
3742cc8b
YQ
8624 CORE_ADDR adjusted_address;
8625 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8626
8627 if (loc_gdbarch == NULL)
8628 loc_gdbarch = b->gdbarch;
8629
8630 /* Adjust the breakpoint's address prior to allocating a location.
8631 Once we call allocate_bp_location(), that mostly uninitialized
8632 location will be placed on the location chain. Adjustment of the
8633 breakpoint may cause target_read_memory() to be called and we do
8634 not want its scan of the location chain to find a breakpoint and
8635 location that's only been partially initialized. */
8636 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8637 sal->pc, b->type);
0d381245 8638
d30113d4 8639 /* Sort the locations by their ADDRESS. */
39d61571 8640 loc = allocate_bp_location (b);
d30113d4
JK
8641 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8642 tmp = &((*tmp)->next))
0d381245 8643 ;
d30113d4 8644 loc->next = *tmp;
0d381245 8645 *tmp = loc;
3742cc8b 8646
0d381245 8647 loc->requested_address = sal->pc;
3742cc8b 8648 loc->address = adjusted_address;
6c95b8df 8649 loc->pspace = sal->pspace;
935676c9 8650 loc->probe.prob = sal->prob;
729662a5 8651 loc->probe.objfile = sal->objfile;
6c95b8df 8652 gdb_assert (loc->pspace != NULL);
0d381245 8653 loc->section = sal->section;
3742cc8b 8654 loc->gdbarch = loc_gdbarch;
f8eba3c6 8655 loc->line_number = sal->line;
2f202fde 8656 loc->symtab = sal->symtab;
4a27f119 8657 loc->symbol = sal->symbol;
3467ec66
PA
8658 loc->msymbol = sal->msymbol;
8659 loc->objfile = sal->objfile;
f8eba3c6 8660
0ba852ab 8661 set_breakpoint_location_function (loc);
1a853c52 8662
6ae88661
LM
8663 /* While by definition, permanent breakpoints are already present in the
8664 code, we don't mark the location as inserted. Normally one would expect
8665 that GDB could rely on that breakpoint instruction to stop the program,
8666 thus removing the need to insert its own breakpoint, except that executing
8667 the breakpoint instruction can kill the target instead of reporting a
8668 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8669 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8670 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8671 breakpoint be inserted normally results in QEMU knowing about the GDB
8672 breakpoint, and thus trap before the breakpoint instruction is executed.
8673 (If GDB later needs to continue execution past the permanent breakpoint,
8674 it manually increments the PC, thus avoiding executing the breakpoint
8675 instruction.) */
1a853c52 8676 if (bp_loc_is_permanent (loc))
6ae88661 8677 loc->permanent = 1;
1a853c52 8678
0d381245
VP
8679 return loc;
8680}
514f746b
AR
8681\f
8682
5133a315
LM
8683/* Return true if LOC is pointing to a permanent breakpoint,
8684 return false otherwise. */
1cf4d951 8685
5133a315 8686static bool
1cf4d951
PA
8687bp_loc_is_permanent (struct bp_location *loc)
8688{
514f746b
AR
8689 gdb_assert (loc != NULL);
8690
cb1e4e32
PA
8691 /* If we have a non-breakpoint-backed catchpoint or a software
8692 watchpoint, just return 0. We should not attempt to read from
8693 the addresses the locations of these breakpoint types point to.
5133a315 8694 gdbarch_program_breakpoint_here_p, below, will attempt to read
244558af 8695 memory. */
cb1e4e32 8696 if (!bl_address_is_meaningful (loc))
5133a315 8697 return false;
244558af 8698
5ed8105e 8699 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8700 switch_to_program_space_and_thread (loc->pspace);
5133a315 8701 return gdbarch_program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8702}
8703
e7e0cddf
SS
8704/* Build a command list for the dprintf corresponding to the current
8705 settings of the dprintf style options. */
8706
8707static void
8708update_dprintf_command_list (struct breakpoint *b)
8709{
8710 char *dprintf_args = b->extra_string;
8711 char *printf_line = NULL;
8712
8713 if (!dprintf_args)
8714 return;
8715
8716 dprintf_args = skip_spaces (dprintf_args);
8717
8718 /* Allow a comma, as it may have terminated a location, but don't
8719 insist on it. */
8720 if (*dprintf_args == ',')
8721 ++dprintf_args;
8722 dprintf_args = skip_spaces (dprintf_args);
8723
8724 if (*dprintf_args != '"')
8725 error (_("Bad format string, missing '\"'."));
8726
d3ce09f5 8727 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8728 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8729 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8730 {
8731 if (!dprintf_function)
8732 error (_("No function supplied for dprintf call"));
8733
8734 if (dprintf_channel && strlen (dprintf_channel) > 0)
8735 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8736 dprintf_function,
8737 dprintf_channel,
8738 dprintf_args);
8739 else
8740 printf_line = xstrprintf ("call (void) %s (%s)",
8741 dprintf_function,
8742 dprintf_args);
8743 }
d3ce09f5
SS
8744 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8745 {
8746 if (target_can_run_breakpoint_commands ())
8747 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8748 else
8749 {
8750 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8751 printf_line = xstrprintf ("printf %s", dprintf_args);
8752 }
8753 }
e7e0cddf
SS
8754 else
8755 internal_error (__FILE__, __LINE__,
8756 _("Invalid dprintf style."));
8757
f28045c2 8758 gdb_assert (printf_line != NULL);
e7e0cddf 8759
12973681
TT
8760 /* Manufacture a printf sequence. */
8761 struct command_line *printf_cmd_line
8762 = new struct command_line (simple_control, printf_line);
8763 breakpoint_set_commands (b, counted_command_line (printf_cmd_line,
8764 command_lines_deleter ()));
e7e0cddf
SS
8765}
8766
8767/* Update all dprintf commands, making their command lists reflect
8768 current style settings. */
8769
8770static void
eb4c3f4a 8771update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8772 struct cmd_list_element *c)
8773{
43892fdf
SM
8774 for (breakpoint *b : all_breakpoints ())
8775 if (b->type == bp_dprintf)
e7e0cddf 8776 update_dprintf_command_list (b);
e7e0cddf 8777}
c3f6f71d 8778
f00aae0f
KS
8779/* Create a breakpoint with SAL as location. Use LOCATION
8780 as a description of the location, and COND_STRING
b35a8b2f
DE
8781 as condition expression. If LOCATION is NULL then create an
8782 "address location" from the address in the SAL. */
018d34a4
VP
8783
8784static void
d9b3f62e 8785init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8786 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8787 event_location_up &&location,
e1e01040
PA
8788 gdb::unique_xmalloc_ptr<char> filter,
8789 gdb::unique_xmalloc_ptr<char> cond_string,
8790 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8791 enum bptype type, enum bpdisp disposition,
8792 int thread, int task, int ignore_count,
c0a91b2b 8793 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8794 int enabled, int internal, unsigned flags,
8795 int display_canonical)
018d34a4 8796{
0d381245 8797 int i;
018d34a4
VP
8798
8799 if (type == bp_hardware_breakpoint)
8800 {
fbbd034e
AS
8801 int target_resources_ok;
8802
8803 i = hw_breakpoint_used_count ();
8804 target_resources_ok =
8805 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8806 i + 1, 0);
8807 if (target_resources_ok == 0)
8808 error (_("No hardware breakpoint support in the target."));
8809 else if (target_resources_ok < 0)
8810 error (_("Hardware breakpoints used exceeds limit."));
8811 }
8812
6c5b2ebe 8813 gdb_assert (!sals.empty ());
6c95b8df 8814
6c5b2ebe 8815 for (const auto &sal : sals)
0d381245 8816 {
0d381245
VP
8817 struct bp_location *loc;
8818
8819 if (from_tty)
5af949e3
UW
8820 {
8821 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8822 if (!loc_gdbarch)
8823 loc_gdbarch = gdbarch;
8824
8825 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8826 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8827 }
0d381245 8828
6c5b2ebe 8829 if (&sal == &sals[0])
0d381245 8830 {
d9b3f62e 8831 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8832 b->thread = thread;
4a306c9a 8833 b->task = task;
855a6e68 8834
e1e01040
PA
8835 b->cond_string = cond_string.release ();
8836 b->extra_string = extra_string.release ();
0d381245 8837 b->ignore_count = ignore_count;
41447f92 8838 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8839 b->disposition = disposition;
6c95b8df 8840
44f238bb
PA
8841 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8842 b->loc->inserted = 1;
8843
0fb4aa4b
PA
8844 if (type == bp_static_tracepoint)
8845 {
d9b3f62e 8846 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8847 struct static_tracepoint_marker marker;
8848
983af33b 8849 if (strace_marker_p (b))
0fb4aa4b
PA
8850 {
8851 /* We already know the marker exists, otherwise, we
8852 wouldn't see a sal for it. */
d28cd78a
TT
8853 const char *p
8854 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8855 const char *endp;
0fb4aa4b 8856
f1735a53 8857 p = skip_spaces (p);
0fb4aa4b 8858
f1735a53 8859 endp = skip_to_space (p);
0fb4aa4b 8860
5d9310c4 8861 t->static_trace_marker_id.assign (p, endp - p);
0fb4aa4b 8862
3e43a32a
MS
8863 printf_filtered (_("Probed static tracepoint "
8864 "marker \"%s\"\n"),
5d9310c4 8865 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8866 }
8867 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8868 {
5d9310c4 8869 t->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b 8870
3e43a32a
MS
8871 printf_filtered (_("Probed static tracepoint "
8872 "marker \"%s\"\n"),
5d9310c4 8873 t->static_trace_marker_id.c_str ());
0fb4aa4b
PA
8874 }
8875 else
3e43a32a
MS
8876 warning (_("Couldn't determine the static "
8877 "tracepoint marker to probe"));
0fb4aa4b
PA
8878 }
8879
0d381245
VP
8880 loc = b->loc;
8881 }
8882 else
018d34a4 8883 {
39d61571 8884 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8885 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8886 loc->inserted = 1;
0d381245
VP
8887 }
8888
b5fa468f
TBA
8889 /* Do not set breakpoint locations conditions yet. As locations
8890 are inserted, they get sorted based on their addresses. Let
8891 the list stabilize to have reliable location numbers. */
e7e0cddf
SS
8892
8893 /* Dynamic printf requires and uses additional arguments on the
8894 command line, otherwise it's an error. */
8895 if (type == bp_dprintf)
8896 {
8897 if (b->extra_string)
8898 update_dprintf_command_list (b);
8899 else
8900 error (_("Format string required"));
8901 }
8902 else if (b->extra_string)
588ae58c 8903 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8904 }
018d34a4 8905
b5fa468f
TBA
8906
8907 /* The order of the locations is now stable. Set the location
8908 condition using the location's number. */
8909 int loc_num = 1;
40cb8ca5 8910 for (bp_location *loc : b->locations ())
b5fa468f
TBA
8911 {
8912 if (b->cond_string != nullptr)
8913 set_breakpoint_location_condition (b->cond_string, loc, b->number,
8914 loc_num);
8915
8916 ++loc_num;
8917 }
8918
56435ebe 8919 b->display_canonical = display_canonical;
f00aae0f 8920 if (location != NULL)
d28cd78a 8921 b->location = std::move (location);
018d34a4 8922 else
d28cd78a 8923 b->location = new_address_location (b->loc->address, NULL, 0);
c0e8dcd8 8924 b->filter = std::move (filter);
d9b3f62e 8925}
018d34a4 8926
d9b3f62e
PA
8927static void
8928create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 8929 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8930 event_location_up &&location,
e1e01040
PA
8931 gdb::unique_xmalloc_ptr<char> filter,
8932 gdb::unique_xmalloc_ptr<char> cond_string,
8933 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8934 enum bptype type, enum bpdisp disposition,
8935 int thread, int task, int ignore_count,
c0a91b2b 8936 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8937 int enabled, int internal, unsigned flags,
8938 int display_canonical)
d9b3f62e 8939{
a5e364af 8940 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 8941
a5e364af 8942 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 8943 sals, std::move (location),
e1e01040
PA
8944 std::move (filter),
8945 std::move (cond_string),
8946 std::move (extra_string),
d9b3f62e
PA
8947 type, disposition,
8948 thread, task, ignore_count,
8949 ops, from_tty,
44f238bb
PA
8950 enabled, internal, flags,
8951 display_canonical);
d9b3f62e 8952
b270e6f9 8953 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
8954}
8955
8956/* Add SALS.nelts breakpoints to the breakpoint table. For each
8957 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8958 value. COND_STRING, if not NULL, specified the condition to be
8959 used for all breakpoints. Essentially the only case where
8960 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8961 function. In that case, it's still not possible to specify
8962 separate conditions for different overloaded functions, so
8963 we take just a single condition string.
8964
c3f6f71d 8965 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8966 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8967 array contents). If the function fails (error() is called), the
8968 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8969 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8970
8971static void
8cdf0e15 8972create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8973 struct linespec_result *canonical,
e1e01040
PA
8974 gdb::unique_xmalloc_ptr<char> cond_string,
8975 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
8976 enum bptype type, enum bpdisp disposition,
8977 int thread, int task, int ignore_count,
c0a91b2b 8978 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8979 int enabled, int internal, unsigned flags)
c906108c 8980{
f8eba3c6 8981 if (canonical->pre_expanded)
6c5b2ebe 8982 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 8983
6c5b2ebe 8984 for (const auto &lsal : canonical->lsals)
c3f6f71d 8985 {
f00aae0f 8986 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 8987 'break', without arguments. */
ffc2605c 8988 event_location_up location
f00aae0f 8989 = (canonical->location != NULL
8e9e35b1 8990 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 8991 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 8992 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 8993
6c5b2ebe 8994 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 8995 std::move (location),
e1e01040
PA
8996 std::move (filter_string),
8997 std::move (cond_string),
8998 std::move (extra_string),
e7e0cddf 8999 type, disposition,
84f4c1fe 9000 thread, task, ignore_count, ops,
44f238bb 9001 from_tty, enabled, internal, flags,
56435ebe 9002 canonical->special_display);
c3f6f71d 9003 }
c3f6f71d 9004}
c906108c 9005
f00aae0f 9006/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9007 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9008 addresses found. LOCATION points to the end of the SAL (for
9009 linespec locations).
9998af43
TJB
9010
9011 The array and the line spec strings are allocated on the heap, it is
9012 the caller's responsibility to free them. */
c906108c 9013
b9362cc7 9014static void
626d2320 9015parse_breakpoint_sals (struct event_location *location,
58438ac1 9016 struct linespec_result *canonical)
c3f6f71d 9017{
f00aae0f
KS
9018 struct symtab_and_line cursal;
9019
9020 if (event_location_type (location) == LINESPEC_LOCATION)
9021 {
a20714ff 9022 const char *spec = get_linespec_location (location)->spec_string;
f00aae0f 9023
a20714ff 9024 if (spec == NULL)
f00aae0f
KS
9025 {
9026 /* The last displayed codepoint, if it's valid, is our default
9027 breakpoint address. */
9028 if (last_displayed_sal_is_valid ())
9029 {
f00aae0f
KS
9030 /* Set sal's pspace, pc, symtab, and line to the values
9031 corresponding to the last call to print_frame_info.
9032 Be sure to reinitialize LINE with NOTCURRENT == 0
9033 as the breakpoint line number is inappropriate otherwise.
9034 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9035 symtab_and_line sal = get_last_displayed_sal ();
9036 CORE_ADDR pc = sal.pc;
9037
f00aae0f
KS
9038 sal = find_pc_line (pc, 0);
9039
9040 /* "break" without arguments is equivalent to "break *PC"
9041 where PC is the last displayed codepoint's address. So
9042 make sure to set sal.explicit_pc to prevent GDB from
9043 trying to expand the list of sals to include all other
9044 instances with the same symtab and line. */
9045 sal.pc = pc;
9046 sal.explicit_pc = 1;
9047
6c5b2ebe
PA
9048 struct linespec_sals lsal;
9049 lsal.sals = {sal};
f00aae0f
KS
9050 lsal.canonical = NULL;
9051
6c5b2ebe 9052 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9053 return;
9054 }
9055 else
9056 error (_("No default breakpoint address now."));
c906108c 9057 }
c906108c 9058 }
f00aae0f
KS
9059
9060 /* Force almost all breakpoints to be in terms of the
9061 current_source_symtab (which is decode_line_1's default).
9062 This should produce the results we want almost all of the
9063 time while leaving default_breakpoint_* alone.
9064
9065 ObjC: However, don't match an Objective-C method name which
9066 may have a '+' or '-' succeeded by a '['. */
9067 cursal = get_current_source_symtab_and_line ();
9068 if (last_displayed_sal_is_valid ())
c906108c 9069 {
a20714ff 9070 const char *spec = NULL;
cc80f267 9071
f00aae0f 9072 if (event_location_type (location) == LINESPEC_LOCATION)
a20714ff 9073 spec = get_linespec_location (location)->spec_string;
cc80f267 9074
f00aae0f 9075 if (!cursal.symtab
a20714ff
PA
9076 || (spec != NULL
9077 && strchr ("+-", spec[0]) != NULL
9078 && spec[1] != '['))
f00aae0f 9079 {
c2f4122d 9080 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9081 get_last_displayed_symtab (),
9082 get_last_displayed_line (),
9083 canonical, NULL, NULL);
9084 return;
9085 }
c906108c 9086 }
f00aae0f 9087
c2f4122d 9088 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9089 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9090}
c906108c 9091
c906108c 9092
c3f6f71d 9093/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9094 inserted as a breakpoint. If it can't throw an error. */
c906108c 9095
b9362cc7 9096static void
6c5b2ebe 9097breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9098{
6c5b2ebe
PA
9099 for (auto &sal : sals)
9100 resolve_sal_pc (&sal);
c3f6f71d
JM
9101}
9102
7a697b8d
SS
9103/* Fast tracepoints may have restrictions on valid locations. For
9104 instance, a fast tracepoint using a jump instead of a trap will
9105 likely have to overwrite more bytes than a trap would, and so can
9106 only be placed where the instruction is longer than the jump, or a
9107 multi-instruction sequence does not have a jump into the middle of
9108 it, etc. */
9109
9110static void
9111check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9112 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9113{
6c5b2ebe 9114 for (const auto &sal : sals)
7a697b8d 9115 {
f8eba3c6
TT
9116 struct gdbarch *sarch;
9117
6c5b2ebe 9118 sarch = get_sal_arch (sal);
f8eba3c6
TT
9119 /* We fall back to GDBARCH if there is no architecture
9120 associated with SAL. */
9121 if (sarch == NULL)
9122 sarch = gdbarch;
281d762b
TT
9123 std::string msg;
9124 if (!gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg))
53c3572a 9125 error (_("May not have a fast tracepoint at %s%s"),
281d762b 9126 paddress (sarch, sal.pc), msg.c_str ());
7a697b8d
SS
9127 }
9128}
9129
018d34a4
VP
9130/* Given TOK, a string specification of condition and thread, as
9131 accepted by the 'break' command, extract the condition
9132 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9133 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9134 If no condition is found, *COND_STRING is set to NULL.
9135 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9136
9137static void
bbc13ae3 9138find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9139 char **cond_string, int *thread, int *task,
9140 char **rest)
018d34a4
VP
9141{
9142 *cond_string = NULL;
9143 *thread = -1;
ed1d1739
KS
9144 *task = 0;
9145 *rest = NULL;
733d554a 9146 bool force = false;
ed1d1739 9147
018d34a4
VP
9148 while (tok && *tok)
9149 {
bbc13ae3 9150 const char *end_tok;
018d34a4 9151 int toklen;
bbc13ae3
KS
9152 const char *cond_start = NULL;
9153 const char *cond_end = NULL;
cc59ec59 9154
f1735a53 9155 tok = skip_spaces (tok);
e7e0cddf
SS
9156
9157 if ((*tok == '"' || *tok == ',') && rest)
9158 {
9159 *rest = savestring (tok, strlen (tok));
9160 return;
9161 }
9162
f1735a53 9163 end_tok = skip_to_space (tok);
d634f2de 9164
018d34a4 9165 toklen = end_tok - tok;
d634f2de 9166
018d34a4
VP
9167 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9168 {
9169 tok = cond_start = end_tok + 1;
733d554a
TBA
9170 try
9171 {
9172 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9173 }
9174 catch (const gdb_exception_error &)
9175 {
9176 if (!force)
9177 throw;
9178 else
9179 tok = tok + strlen (tok);
9180 }
018d34a4 9181 cond_end = tok;
d634f2de 9182 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4 9183 }
733d554a
TBA
9184 else if (toklen >= 1 && strncmp (tok, "-force-condition", toklen) == 0)
9185 {
5759831a 9186 tok = tok + toklen;
733d554a
TBA
9187 force = true;
9188 }
018d34a4
VP
9189 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9190 {
5d5658a1
PA
9191 const char *tmptok;
9192 struct thread_info *thr;
d634f2de 9193
018d34a4 9194 tok = end_tok + 1;
5d5658a1 9195 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9196 if (tok == tmptok)
9197 error (_("Junk after thread keyword."));
5d5658a1 9198 *thread = thr->global_num;
bbc13ae3 9199 tok = tmptok;
018d34a4 9200 }
4a306c9a
JB
9201 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9202 {
9203 char *tmptok;
9204
9205 tok = end_tok + 1;
bbc13ae3 9206 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9207 if (tok == tmptok)
9208 error (_("Junk after task keyword."));
9209 if (!valid_task_id (*task))
b6199126 9210 error (_("Unknown task %d."), *task);
bbc13ae3 9211 tok = tmptok;
4a306c9a 9212 }
e7e0cddf
SS
9213 else if (rest)
9214 {
9215 *rest = savestring (tok, strlen (tok));
ccab2054 9216 return;
e7e0cddf 9217 }
018d34a4
VP
9218 else
9219 error (_("Junk at end of arguments."));
9220 }
9221}
9222
b5fa468f
TBA
9223/* Call 'find_condition_and_thread' for each sal in SALS until a parse
9224 succeeds. The parsed values are written to COND_STRING, THREAD,
9225 TASK, and REST. See the comment of 'find_condition_and_thread'
9226 for the description of these parameters and INPUT. */
9227
9228static void
9229find_condition_and_thread_for_sals (const std::vector<symtab_and_line> &sals,
9230 const char *input, char **cond_string,
9231 int *thread, int *task, char **rest)
9232{
9233 int num_failures = 0;
9234 for (auto &sal : sals)
9235 {
9236 char *cond = nullptr;
9237 int thread_id = 0;
9238 int task_id = 0;
9239 char *remaining = nullptr;
9240
9241 /* Here we want to parse 'arg' to separate condition from thread
9242 number. But because parsing happens in a context and the
9243 contexts of sals might be different, try each until there is
9244 success. Finding one successful parse is sufficient for our
9245 goal. When setting the breakpoint we'll re-parse the
9246 condition in the context of each sal. */
9247 try
9248 {
9249 find_condition_and_thread (input, sal.pc, &cond, &thread_id,
9250 &task_id, &remaining);
9251 *cond_string = cond;
9252 *thread = thread_id;
9253 *task = task_id;
9254 *rest = remaining;
9255 break;
9256 }
9257 catch (const gdb_exception_error &e)
9258 {
9259 num_failures++;
9260 /* If no sal remains, do not continue. */
9261 if (num_failures == sals.size ())
9262 throw;
9263 }
9264 }
9265}
9266
0fb4aa4b
PA
9267/* Decode a static tracepoint marker spec. */
9268
6c5b2ebe 9269static std::vector<symtab_and_line>
f00aae0f 9270decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b 9271{
f00aae0f
KS
9272 const char *p = &(*arg_p)[3];
9273 const char *endp;
0fb4aa4b 9274
f1735a53 9275 p = skip_spaces (p);
0fb4aa4b 9276
f1735a53 9277 endp = skip_to_space (p);
0fb4aa4b 9278
81b1e71c 9279 std::string marker_str (p, endp - p);
0fb4aa4b 9280
5d9310c4
SM
9281 std::vector<static_tracepoint_marker> markers
9282 = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
9283 if (markers.empty ())
81b1e71c
TT
9284 error (_("No known static tracepoint marker named %s"),
9285 marker_str.c_str ());
0fb4aa4b 9286
6c5b2ebe 9287 std::vector<symtab_and_line> sals;
5d9310c4 9288 sals.reserve (markers.size ());
0fb4aa4b 9289
5d9310c4 9290 for (const static_tracepoint_marker &marker : markers)
0fb4aa4b 9291 {
5d9310c4
SM
9292 symtab_and_line sal = find_pc_line (marker.address, 0);
9293 sal.pc = marker.address;
6c5b2ebe 9294 sals.push_back (sal);
5d9310c4 9295 }
0fb4aa4b 9296
0fb4aa4b
PA
9297 *arg_p = endp;
9298 return sals;
9299}
9300
bac7c5cf
GB
9301/* Returns the breakpoint ops appropriate for use with with LOCATION_TYPE and
9302 according to IS_TRACEPOINT. */
9303
9304static const struct breakpoint_ops *
9305breakpoint_ops_for_event_location_type (enum event_location_type location_type,
9306 bool is_tracepoint)
9307{
9308 if (is_tracepoint)
9309 {
9310 if (location_type == PROBE_LOCATION)
9311 return &tracepoint_probe_breakpoint_ops;
9312 else
9313 return &tracepoint_breakpoint_ops;
9314 }
9315 else
9316 {
9317 if (location_type == PROBE_LOCATION)
9318 return &bkpt_probe_breakpoint_ops;
9319 else
9320 return &bkpt_breakpoint_ops;
9321 }
9322}
9323
9324/* See breakpoint.h. */
9325
9326const struct breakpoint_ops *
9327breakpoint_ops_for_event_location (const struct event_location *location,
9328 bool is_tracepoint)
9329{
9330 if (location != nullptr)
9331 return breakpoint_ops_for_event_location_type
9332 (event_location_type (location), is_tracepoint);
9333 return is_tracepoint ? &tracepoint_breakpoint_ops : &bkpt_breakpoint_ops;
9334}
9335
f00aae0f 9336/* See breakpoint.h. */
0101ce28 9337
8cdf0e15
VP
9338int
9339create_breakpoint (struct gdbarch *gdbarch,
626d2320 9340 struct event_location *location,
e1e01040
PA
9341 const char *cond_string,
9342 int thread, const char *extra_string,
10a636cc 9343 bool force_condition, int parse_extra,
0fb4aa4b 9344 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9345 int ignore_count,
9346 enum auto_boolean pending_break_support,
c0a91b2b 9347 const struct breakpoint_ops *ops,
44f238bb
PA
9348 int from_tty, int enabled, int internal,
9349 unsigned flags)
c3f6f71d 9350{
7efd8fc2 9351 struct linespec_result canonical;
0101ce28 9352 int pending = 0;
4a306c9a 9353 int task = 0;
86b17b60 9354 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9355
348d480f
PA
9356 gdb_assert (ops != NULL);
9357
f00aae0f
KS
9358 /* If extra_string isn't useful, set it to NULL. */
9359 if (extra_string != NULL && *extra_string == '\0')
9360 extra_string = NULL;
9361
a70b8144 9362 try
b78a6381 9363 {
f00aae0f 9364 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9365 }
230d2906 9366 catch (const gdb_exception_error &e)
0101ce28 9367 {
492d29ea
PA
9368 /* If caller is interested in rc value from parse, set
9369 value. */
9370 if (e.error == NOT_FOUND_ERROR)
0101ce28 9371 {
05ff989b
AC
9372 /* If pending breakpoint support is turned off, throw
9373 error. */
fa8d40ab
JJ
9374
9375 if (pending_break_support == AUTO_BOOLEAN_FALSE)
eedc3f4f 9376 throw;
723a2275
VP
9377
9378 exception_print (gdb_stderr, e);
fa8d40ab 9379
dda83cd7 9380 /* If pending breakpoint support is auto query and the user
05ff989b 9381 selects no, then simply return the error code. */
059fb39f 9382 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9383 && !nquery (_("Make %s pending on future shared library load? "),
9384 bptype_string (type_wanted)))
fd9b8c24 9385 return 0;
fa8d40ab 9386
05ff989b
AC
9387 /* At this point, either the user was queried about setting
9388 a pending breakpoint and selected yes, or pending
9389 breakpoint behavior is on and thus a pending breakpoint
9390 is defaulted on behalf of the user. */
f00aae0f 9391 pending = 1;
0101ce28 9392 }
492d29ea 9393 else
eedc3f4f 9394 throw;
0101ce28 9395 }
492d29ea 9396
6c5b2ebe 9397 if (!pending && canonical.lsals.empty ())
492d29ea 9398 return 0;
c3f6f71d 9399
c3f6f71d
JM
9400 /* Resolve all line numbers to PC's and verify that the addresses
9401 are ok for the target. */
0101ce28 9402 if (!pending)
f8eba3c6 9403 {
6c5b2ebe
PA
9404 for (auto &lsal : canonical.lsals)
9405 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9406 }
c3f6f71d 9407
7a697b8d 9408 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9409 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9410 {
6c5b2ebe
PA
9411 for (const auto &lsal : canonical.lsals)
9412 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9413 }
7a697b8d 9414
c3f6f71d
JM
9415 /* Verify that condition can be parsed, before setting any
9416 breakpoints. Allocate a separate condition expression for each
4a64f543 9417 breakpoint. */
0101ce28 9418 if (!pending)
c3f6f71d 9419 {
e1e01040
PA
9420 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9421 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9422
f00aae0f 9423 if (parse_extra)
dda83cd7 9424 {
0878d0fa 9425 char *rest;
e1e01040 9426 char *cond;
52d361e1 9427
6c5b2ebe 9428 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9429
b5fa468f
TBA
9430 find_condition_and_thread_for_sals (lsal.sals, extra_string,
9431 &cond, &thread, &task, &rest);
e1e01040
PA
9432 cond_string_copy.reset (cond);
9433 extra_string_copy.reset (rest);
dda83cd7 9434 }
2f069f6f 9435 else
dda83cd7 9436 {
f00aae0f
KS
9437 if (type_wanted != bp_dprintf
9438 && extra_string != NULL && *extra_string != '\0')
9439 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa 9440
10a636cc
TBA
9441 /* Check the validity of the condition. We should error out
9442 if the condition is invalid at all of the locations and
9443 if it is not forced. In the PARSE_EXTRA case above, this
9444 check is done when parsing the EXTRA_STRING. */
9445 if (cond_string != nullptr && !force_condition)
9446 {
9447 int num_failures = 0;
9448 const linespec_sals &lsal = canonical.lsals[0];
9449 for (const auto &sal : lsal.sals)
9450 {
9451 const char *cond = cond_string;
9452 try
9453 {
9454 parse_exp_1 (&cond, sal.pc, block_for_pc (sal.pc), 0);
9455 /* One success is sufficient to keep going. */
9456 break;
9457 }
9458 catch (const gdb_exception_error &)
9459 {
9460 num_failures++;
9461 /* If this is the last sal, error out. */
9462 if (num_failures == lsal.sals.size ())
9463 throw;
9464 }
9465 }
9466 }
9467
0878d0fa
YQ
9468 /* Create a private copy of condition string. */
9469 if (cond_string)
e1e01040 9470 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9471 /* Create a private copy of any extra string. */
9472 if (extra_string)
e1e01040 9473 extra_string_copy.reset (xstrdup (extra_string));
dda83cd7 9474 }
0fb4aa4b 9475
52d361e1 9476 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9477 std::move (cond_string_copy),
9478 std::move (extra_string_copy),
9479 type_wanted,
d9b3f62e
PA
9480 tempflag ? disp_del : disp_donttouch,
9481 thread, task, ignore_count, ops,
44f238bb 9482 from_tty, enabled, internal, flags);
c906108c 9483 }
0101ce28
JJ
9484 else
9485 {
a5e364af 9486 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9487
a5e364af 9488 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9489 b->location = copy_event_location (location);
bfccc43c 9490
f00aae0f
KS
9491 if (parse_extra)
9492 b->cond_string = NULL;
e12c7713
MK
9493 else
9494 {
9495 /* Create a private copy of condition string. */
e1e01040 9496 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9497 b->thread = thread;
e12c7713 9498 }
f00aae0f
KS
9499
9500 /* Create a private copy of any extra string. */
e1e01040 9501 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9502 b->ignore_count = ignore_count;
0101ce28 9503 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9504 b->condition_not_parsed = 1;
41447f92 9505 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2 9506 if ((type_wanted != bp_breakpoint
dda83cd7 9507 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9508 b->pspace = current_program_space;
8bea4e01 9509
b270e6f9 9510 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9511 }
9512
6c5b2ebe 9513 if (canonical.lsals.size () > 1)
95a42b64 9514 {
3e43a32a
MS
9515 warning (_("Multiple breakpoints were set.\nUse the "
9516 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9517 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9518 }
9519
44702360 9520 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9521
9522 return 1;
c3f6f71d 9523}
c906108c 9524
348d480f 9525/* Set a breakpoint.
72b2ff0e
VP
9526 ARG is a string describing breakpoint address,
9527 condition, and thread.
9528 FLAG specifies if a breakpoint is hardware on,
9529 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9530 and BP_TEMPFLAG. */
348d480f 9531
98deb0da 9532static void
f2fc3015 9533break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9534{
72b2ff0e 9535 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9536 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9537 ? bp_hardware_breakpoint
9538 : bp_breakpoint);
f00aae0f 9539
ffc2605c 9540 event_location_up location = string_to_event_location (&arg, current_language);
bac7c5cf
GB
9541 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
9542 (location.get (), false /* is_tracepoint */);
c3f6f71d 9543
8cdf0e15 9544 create_breakpoint (get_current_arch (),
ffc2605c 9545 location.get (),
10a636cc 9546 NULL, 0, arg, false, 1 /* parse arg */,
0fb4aa4b 9547 tempflag, type_wanted,
8cdf0e15
VP
9548 0 /* Ignore count */,
9549 pending_break_support,
55aa24fb 9550 ops,
8cdf0e15 9551 from_tty,
84f4c1fe 9552 1 /* enabled */,
44f238bb
PA
9553 0 /* internal */,
9554 0);
c906108c
SS
9555}
9556
c906108c
SS
9557/* Helper function for break_command_1 and disassemble_command. */
9558
9559void
fba45db2 9560resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9561{
9562 CORE_ADDR pc;
9563
9564 if (sal->pc == 0 && sal->symtab != NULL)
9565 {
9566 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9567 error (_("No line %d in file \"%s\"."),
05cba821 9568 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9569 sal->pc = pc;
6a048695 9570
4a64f543 9571 /* If this SAL corresponds to a breakpoint inserted using a line
dda83cd7 9572 number, then skip the function prologue if necessary. */
6a048695 9573 if (sal->explicit_line)
059acae7 9574 skip_prologue_sal (sal);
c906108c
SS
9575 }
9576
9577 if (sal->section == 0 && sal->symtab != NULL)
9578 {
346d1dfe 9579 const struct blockvector *bv;
3977b71f 9580 const struct block *b;
c5aa993b 9581 struct symbol *sym;
c906108c 9582
43f3e411
DE
9583 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9584 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9585 if (bv != NULL)
9586 {
7f0df278 9587 sym = block_linkage_function (b);
c906108c
SS
9588 if (sym != NULL)
9589 {
eb822aa6 9590 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
ebbc3a7d 9591 sal->section = sym->obj_section (SYMTAB_OBJFILE (sal->symtab));
c906108c
SS
9592 }
9593 else
9594 {
4a64f543 9595 /* It really is worthwhile to have the section, so we'll
dda83cd7
SM
9596 just have to look harder. This case can be executed
9597 if we have line numbers but no functions (as can
9598 happen in assembly source). */
c906108c 9599
5ed8105e 9600 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9601 switch_to_program_space_and_thread (sal->pspace);
c906108c 9602
5ed8105e 9603 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9604 if (msym.minsym)
1db66e34 9605 sal->section = msym.obj_section ();
c906108c
SS
9606 }
9607 }
9608 }
9609}
9610
9611void
0b39b52e 9612break_command (const char *arg, int from_tty)
c906108c 9613{
db107f19 9614 break_command_1 (arg, 0, from_tty);
c906108c
SS
9615}
9616
c906108c 9617void
0b39b52e 9618tbreak_command (const char *arg, int from_tty)
c906108c 9619{
db107f19 9620 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9621}
9622
c906108c 9623static void
0b39b52e 9624hbreak_command (const char *arg, int from_tty)
c906108c 9625{
db107f19 9626 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9627}
9628
9629static void
0b39b52e 9630thbreak_command (const char *arg, int from_tty)
c906108c 9631{
db107f19 9632 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9633}
9634
9635static void
ee7ddd71 9636stop_command (const char *arg, int from_tty)
c906108c 9637{
a3f17187 9638 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9639Usage: stop in <function | address>\n\
a3f17187 9640 stop at <line>\n"));
c906108c
SS
9641}
9642
9643static void
4495129a 9644stopin_command (const char *arg, int from_tty)
c906108c
SS
9645{
9646 int badInput = 0;
9647
cafb3438 9648 if (arg == NULL)
c906108c
SS
9649 badInput = 1;
9650 else if (*arg != '*')
9651 {
4495129a 9652 const char *argptr = arg;
c906108c
SS
9653 int hasColon = 0;
9654
4a64f543 9655 /* Look for a ':'. If this is a line number specification, then
dda83cd7
SM
9656 say it is bad, otherwise, it should be an address or
9657 function/method name. */
c906108c 9658 while (*argptr && !hasColon)
c5aa993b
JM
9659 {
9660 hasColon = (*argptr == ':');
9661 argptr++;
9662 }
c906108c
SS
9663
9664 if (hasColon)
c5aa993b 9665 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9666 else
c5aa993b 9667 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9668 }
9669
9670 if (badInput)
a3f17187 9671 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9672 else
db107f19 9673 break_command_1 (arg, 0, from_tty);
c906108c
SS
9674}
9675
9676static void
4495129a 9677stopat_command (const char *arg, int from_tty)
c906108c
SS
9678{
9679 int badInput = 0;
9680
cafb3438 9681 if (arg == NULL || *arg == '*') /* no line number */
c906108c
SS
9682 badInput = 1;
9683 else
9684 {
4495129a 9685 const char *argptr = arg;
c906108c
SS
9686 int hasColon = 0;
9687
4a64f543 9688 /* Look for a ':'. If there is a '::' then get out, otherwise
dda83cd7 9689 it is probably a line number. */
c906108c 9690 while (*argptr && !hasColon)
c5aa993b
JM
9691 {
9692 hasColon = (*argptr == ':');
9693 argptr++;
9694 }
c906108c
SS
9695
9696 if (hasColon)
c5aa993b 9697 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9698 else
c5aa993b 9699 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9700 }
9701
9702 if (badInput)
65e65158 9703 printf_filtered (_("Usage: stop at LINE\n"));
c906108c 9704 else
db107f19 9705 break_command_1 (arg, 0, from_tty);
c906108c
SS
9706}
9707
e7e0cddf
SS
9708/* The dynamic printf command is mostly like a regular breakpoint, but
9709 with a prewired command list consisting of a single output command,
9710 built from extra arguments supplied on the dprintf command
9711 line. */
9712
da821c7b 9713static void
0b39b52e 9714dprintf_command (const char *arg, int from_tty)
e7e0cddf 9715{
ffc2605c 9716 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9717
9718 /* If non-NULL, ARG should have been advanced past the location;
9719 the next character must be ','. */
9720 if (arg != NULL)
9721 {
9722 if (arg[0] != ',' || arg[1] == '\0')
9723 error (_("Format string required"));
9724 else
9725 {
9726 /* Skip the comma. */
9727 ++arg;
9728 }
9729 }
9730
e7e0cddf 9731 create_breakpoint (get_current_arch (),
ffc2605c 9732 location.get (),
10a636cc 9733 NULL, 0, arg, false, 1 /* parse arg */,
e7e0cddf
SS
9734 0, bp_dprintf,
9735 0 /* Ignore count */,
9736 pending_break_support,
9737 &dprintf_breakpoint_ops,
9738 from_tty,
9739 1 /* enabled */,
9740 0 /* internal */,
9741 0);
9742}
9743
d3ce09f5 9744static void
0b39b52e 9745agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9746{
9747 error (_("May only run agent-printf on the target"));
9748}
9749
f1310107
TJB
9750/* Implement the "breakpoint_hit" breakpoint_ops method for
9751 ranged breakpoints. */
9752
9753static int
9754breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9755 const address_space *aspace,
09ac7c10
TT
9756 CORE_ADDR bp_addr,
9757 const struct target_waitstatus *ws)
f1310107 9758{
09ac7c10 9759 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9760 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9761 return 0;
9762
f1310107
TJB
9763 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9764 bl->length, aspace, bp_addr);
9765}
9766
9767/* Implement the "resources_needed" breakpoint_ops method for
9768 ranged breakpoints. */
9769
9770static int
9771resources_needed_ranged_breakpoint (const struct bp_location *bl)
9772{
9773 return target_ranged_break_num_registers ();
9774}
9775
9776/* Implement the "print_it" breakpoint_ops method for
9777 ranged breakpoints. */
9778
9779static enum print_stop_action
348d480f 9780print_it_ranged_breakpoint (bpstat bs)
f1310107 9781{
348d480f 9782 struct breakpoint *b = bs->breakpoint_at;
f1310107 9783 struct bp_location *bl = b->loc;
79a45e25 9784 struct ui_out *uiout = current_uiout;
f1310107
TJB
9785
9786 gdb_assert (b->type == bp_hardware_breakpoint);
9787
9788 /* Ranged breakpoints have only one location. */
9789 gdb_assert (bl && bl->next == NULL);
9790
9791 annotate_breakpoint (b->number);
f303dbd6
PA
9792
9793 maybe_print_thread_hit_breakpoint (uiout);
9794
f1310107 9795 if (b->disposition == disp_del)
112e8700 9796 uiout->text ("Temporary ranged breakpoint ");
f1310107 9797 else
112e8700
SM
9798 uiout->text ("Ranged breakpoint ");
9799 if (uiout->is_mi_like_p ())
f1310107 9800 {
112e8700 9801 uiout->field_string ("reason",
f1310107 9802 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9803 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9804 }
381befee 9805 uiout->field_signed ("bkptno", b->number);
112e8700 9806 uiout->text (", ");
f1310107
TJB
9807
9808 return PRINT_SRC_AND_LOC;
9809}
9810
9811/* Implement the "print_one" breakpoint_ops method for
9812 ranged breakpoints. */
9813
9814static void
9815print_one_ranged_breakpoint (struct breakpoint *b,
9816 struct bp_location **last_loc)
9817{
9818 struct bp_location *bl = b->loc;
9819 struct value_print_options opts;
79a45e25 9820 struct ui_out *uiout = current_uiout;
f1310107
TJB
9821
9822 /* Ranged breakpoints have only one location. */
9823 gdb_assert (bl && bl->next == NULL);
9824
9825 get_user_print_options (&opts);
9826
9827 if (opts.addressprint)
9828 /* We don't print the address range here, it will be printed later
9829 by print_one_detail_ranged_breakpoint. */
112e8700 9830 uiout->field_skip ("addr");
f1310107
TJB
9831 annotate_field (5);
9832 print_breakpoint_location (b, bl);
9833 *last_loc = bl;
9834}
9835
9836/* Implement the "print_one_detail" breakpoint_ops method for
9837 ranged breakpoints. */
9838
9839static void
9840print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9841 struct ui_out *uiout)
9842{
9843 CORE_ADDR address_start, address_end;
9844 struct bp_location *bl = b->loc;
d7e74731 9845 string_file stb;
f1310107
TJB
9846
9847 gdb_assert (bl);
9848
9849 address_start = bl->address;
9850 address_end = address_start + bl->length - 1;
9851
112e8700 9852 uiout->text ("\taddress range: ");
d7e74731
PA
9853 stb.printf ("[%s, %s]",
9854 print_core_address (bl->gdbarch, address_start),
9855 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9856 uiout->field_stream ("addr", stb);
9857 uiout->text ("\n");
f1310107
TJB
9858}
9859
9860/* Implement the "print_mention" breakpoint_ops method for
9861 ranged breakpoints. */
9862
9863static void
9864print_mention_ranged_breakpoint (struct breakpoint *b)
9865{
9866 struct bp_location *bl = b->loc;
79a45e25 9867 struct ui_out *uiout = current_uiout;
f1310107
TJB
9868
9869 gdb_assert (bl);
9870 gdb_assert (b->type == bp_hardware_breakpoint);
9871
2d33446d
TT
9872 uiout->message (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9873 b->number, paddress (bl->gdbarch, bl->address),
9874 paddress (bl->gdbarch, bl->address + bl->length - 1));
f1310107
TJB
9875}
9876
9877/* Implement the "print_recreate" breakpoint_ops method for
9878 ranged breakpoints. */
9879
9880static void
9881print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9882{
f00aae0f 9883 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9884 event_location_to_string (b->location.get ()),
9885 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9886 print_recreate_thread (b, fp);
f1310107
TJB
9887}
9888
9889/* The breakpoint_ops structure to be used in ranged breakpoints. */
9890
2060206e 9891static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9892
9893/* Find the address where the end of the breakpoint range should be
9894 placed, given the SAL of the end of the range. This is so that if
9895 the user provides a line number, the end of the range is set to the
9896 last instruction of the given line. */
9897
9898static CORE_ADDR
9899find_breakpoint_range_end (struct symtab_and_line sal)
9900{
9901 CORE_ADDR end;
9902
9903 /* If the user provided a PC value, use it. Otherwise,
9904 find the address of the end of the given location. */
9905 if (sal.explicit_pc)
9906 end = sal.pc;
9907 else
9908 {
9909 int ret;
9910 CORE_ADDR start;
9911
9912 ret = find_line_pc_range (sal, &start, &end);
9913 if (!ret)
9914 error (_("Could not find location of the end of the range."));
9915
9916 /* find_line_pc_range returns the start of the next line. */
9917 end--;
9918 }
9919
9920 return end;
9921}
9922
9923/* Implement the "break-range" CLI command. */
9924
9925static void
0b39b52e 9926break_range_command (const char *arg, int from_tty)
f1310107 9927{
f2fc3015 9928 const char *arg_start;
f1310107
TJB
9929 struct linespec_result canonical_start, canonical_end;
9930 int bp_count, can_use_bp, length;
9931 CORE_ADDR end;
9932 struct breakpoint *b;
f1310107
TJB
9933
9934 /* We don't support software ranged breakpoints. */
9935 if (target_ranged_break_num_registers () < 0)
9936 error (_("This target does not support hardware ranged breakpoints."));
9937
9938 bp_count = hw_breakpoint_used_count ();
9939 bp_count += target_ranged_break_num_registers ();
9940 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9941 bp_count, 0);
9942 if (can_use_bp < 0)
9943 error (_("Hardware breakpoints used exceeds limit."));
9944
f8eba3c6 9945 arg = skip_spaces (arg);
f1310107
TJB
9946 if (arg == NULL || arg[0] == '\0')
9947 error(_("No address range specified."));
9948
f8eba3c6 9949 arg_start = arg;
ffc2605c
TT
9950 event_location_up start_location = string_to_event_location (&arg,
9951 current_language);
9952 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9953
9954 if (arg[0] != ',')
9955 error (_("Too few arguments."));
6c5b2ebe 9956 else if (canonical_start.lsals.empty ())
f1310107 9957 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9958
6c5b2ebe 9959 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9960
6c5b2ebe
PA
9961 if (canonical_start.lsals.size () > 1
9962 || lsal_start.sals.size () != 1)
f1310107
TJB
9963 error (_("Cannot create a ranged breakpoint with multiple locations."));
9964
6c5b2ebe 9965 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9966 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9967
9968 arg++; /* Skip the comma. */
f8eba3c6 9969 arg = skip_spaces (arg);
f1310107
TJB
9970
9971 /* Parse the end location. */
9972
f1310107
TJB
9973 arg_start = arg;
9974
f8eba3c6 9975 /* We call decode_line_full directly here instead of using
f1310107
TJB
9976 parse_breakpoint_sals because we need to specify the start location's
9977 symtab and line as the default symtab and line for the end of the
9978 range. This makes it possible to have ranges like "foo.c:27, +14",
9979 where +14 means 14 lines from the start location. */
ffc2605c
TT
9980 event_location_up end_location = string_to_event_location (&arg,
9981 current_language);
9982 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9983 sal_start.symtab, sal_start.line,
9984 &canonical_end, NULL, NULL);
9985
6c5b2ebe 9986 if (canonical_end.lsals.empty ())
f1310107 9987 error (_("Could not find location of the end of the range."));
f8eba3c6 9988
6c5b2ebe
PA
9989 const linespec_sals &lsal_end = canonical_end.lsals[0];
9990 if (canonical_end.lsals.size () > 1
9991 || lsal_end.sals.size () != 1)
f1310107
TJB
9992 error (_("Cannot create a ranged breakpoint with multiple locations."));
9993
6c5b2ebe 9994 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
9995
9996 end = find_breakpoint_range_end (sal_end);
9997 if (sal_start.pc > end)
177b42fe 9998 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9999
10000 length = end - sal_start.pc + 1;
10001 if (length < 0)
10002 /* Length overflowed. */
10003 error (_("Address range too large."));
10004 else if (length == 1)
10005 {
10006 /* This range is simple enough to be handled by
10007 the `hbreak' command. */
81b1e71c 10008 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10009
10010 return;
10011 }
10012
10013 /* Now set up the breakpoint. */
10014 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10015 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10016 set_breakpoint_count (breakpoint_count + 1);
10017 b->number = breakpoint_count;
10018 b->disposition = disp_donttouch;
d28cd78a
TT
10019 b->location = std::move (start_location);
10020 b->location_range_end = std::move (end_location);
f1310107
TJB
10021 b->loc->length = length;
10022
f1310107 10023 mention (b);
76727919 10024 gdb::observers::breakpoint_created.notify (b);
44702360 10025 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10026}
10027
4a64f543
MS
10028/* Return non-zero if EXP is verified as constant. Returned zero
10029 means EXP is variable. Also the constant detection may fail for
10030 some constant expressions and in such case still falsely return
10031 zero. */
2e6e3d9c 10032
dab72643 10033static bool
65d79d4b
SDJ
10034watchpoint_exp_is_const (const struct expression *exp)
10035{
1eaebe02 10036 return exp->op->constant_p ();
65d79d4b
SDJ
10037}
10038
c1fc2657 10039/* Watchpoint destructor. */
3a5c3e22 10040
c1fc2657 10041watchpoint::~watchpoint ()
3a5c3e22 10042{
c1fc2657
SM
10043 xfree (this->exp_string);
10044 xfree (this->exp_string_reparse);
3a5c3e22
PA
10045}
10046
348d480f
PA
10047/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10048
10049static void
10050re_set_watchpoint (struct breakpoint *b)
10051{
3a5c3e22
PA
10052 struct watchpoint *w = (struct watchpoint *) b;
10053
348d480f
PA
10054 /* Watchpoint can be either on expression using entirely global
10055 variables, or it can be on local variables.
10056
10057 Watchpoints of the first kind are never auto-deleted, and even
10058 persist across program restarts. Since they can use variables
10059 from shared libraries, we need to reparse expression as libraries
10060 are loaded and unloaded.
10061
10062 Watchpoints on local variables can also change meaning as result
10063 of solib event. For example, if a watchpoint uses both a local
10064 and a global variables in expression, it's a local watchpoint,
10065 but unloading of a shared library will make the expression
10066 invalid. This is not a very common use case, but we still
10067 re-evaluate expression, to avoid surprises to the user.
10068
10069 Note that for local watchpoints, we re-evaluate it only if
10070 watchpoints frame id is still valid. If it's not, it means the
10071 watchpoint is out of scope and will be deleted soon. In fact,
10072 I'm not sure we'll ever be called in this case.
10073
10074 If a local watchpoint's frame id is still valid, then
3a5c3e22 10075 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10076
3a5c3e22
PA
10077 Don't do anything about disabled watchpoints, since they will be
10078 reevaluated again when enabled. */
10079 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10080}
10081
77b06cd7
TJB
10082/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10083
10084static int
10085insert_watchpoint (struct bp_location *bl)
10086{
3a5c3e22
PA
10087 struct watchpoint *w = (struct watchpoint *) bl->owner;
10088 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10089
10090 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10091 w->cond_exp.get ());
77b06cd7
TJB
10092}
10093
10094/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10095
10096static int
73971819 10097remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10098{
3a5c3e22
PA
10099 struct watchpoint *w = (struct watchpoint *) bl->owner;
10100 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10101
10102 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10103 w->cond_exp.get ());
e09342b5
TJB
10104}
10105
e09342b5 10106static int
348d480f 10107breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10108 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10109 const struct target_waitstatus *ws)
e09342b5 10110{
348d480f 10111 struct breakpoint *b = bl->owner;
3a5c3e22 10112 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10113
348d480f
PA
10114 /* Continuable hardware watchpoints are treated as non-existent if the
10115 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10116 some data address). Otherwise gdb won't stop on a break instruction
10117 in the code (not from a breakpoint) when a hardware watchpoint has
10118 been defined. Also skip watchpoints which we know did not trigger
10119 (did not match the data address). */
10120 if (is_hardware_watchpoint (b)
3a5c3e22 10121 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10122 return 0;
9c06b0b4 10123
348d480f 10124 return 1;
9c06b0b4
TJB
10125}
10126
348d480f
PA
10127static void
10128check_status_watchpoint (bpstat bs)
9c06b0b4 10129{
348d480f 10130 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10131
348d480f 10132 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10133}
10134
10135/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10136 hardware watchpoints. */
9c06b0b4
TJB
10137
10138static int
348d480f 10139resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10140{
3a5c3e22
PA
10141 struct watchpoint *w = (struct watchpoint *) bl->owner;
10142 int length = w->exact? 1 : bl->length;
348d480f
PA
10143
10144 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10145}
10146
10147/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10148 hardware watchpoints. */
9c06b0b4
TJB
10149
10150static int
348d480f 10151works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10152{
efa80663
PA
10153 /* Read and access watchpoints only work with hardware support. */
10154 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10155}
10156
9c06b0b4 10157static enum print_stop_action
348d480f 10158print_it_watchpoint (bpstat bs)
9c06b0b4 10159{
348d480f 10160 struct breakpoint *b;
348d480f 10161 enum print_stop_action result;
3a5c3e22 10162 struct watchpoint *w;
79a45e25 10163 struct ui_out *uiout = current_uiout;
348d480f
PA
10164
10165 gdb_assert (bs->bp_location_at != NULL);
10166
348d480f 10167 b = bs->breakpoint_at;
3a5c3e22 10168 w = (struct watchpoint *) b;
348d480f 10169
f303dbd6
PA
10170 annotate_watchpoint (b->number);
10171 maybe_print_thread_hit_breakpoint (uiout);
10172
d7e74731
PA
10173 string_file stb;
10174
76f9c9cf 10175 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10176 switch (b->type)
10177 {
348d480f 10178 case bp_watchpoint:
9c06b0b4 10179 case bp_hardware_watchpoint:
112e8700
SM
10180 if (uiout->is_mi_like_p ())
10181 uiout->field_string
10182 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10183 mention (b);
76f9c9cf 10184 tuple_emitter.emplace (uiout, "value");
112e8700 10185 uiout->text ("\nOld value = ");
850645cf 10186 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10187 uiout->field_stream ("old", stb);
10188 uiout->text ("\nNew value = ");
850645cf 10189 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10190 uiout->field_stream ("new", stb);
10191 uiout->text ("\n");
348d480f
PA
10192 /* More than one watchpoint may have been triggered. */
10193 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10194 break;
10195
10196 case bp_read_watchpoint:
112e8700
SM
10197 if (uiout->is_mi_like_p ())
10198 uiout->field_string
10199 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10200 mention (b);
76f9c9cf 10201 tuple_emitter.emplace (uiout, "value");
112e8700 10202 uiout->text ("\nValue = ");
850645cf 10203 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10204 uiout->field_stream ("value", stb);
10205 uiout->text ("\n");
348d480f 10206 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10207 break;
10208
10209 case bp_access_watchpoint:
348d480f
PA
10210 if (bs->old_val != NULL)
10211 {
112e8700
SM
10212 if (uiout->is_mi_like_p ())
10213 uiout->field_string
10214 ("reason",
348d480f
PA
10215 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10216 mention (b);
76f9c9cf 10217 tuple_emitter.emplace (uiout, "value");
112e8700 10218 uiout->text ("\nOld value = ");
850645cf 10219 watchpoint_value_print (bs->old_val.get (), &stb);
112e8700
SM
10220 uiout->field_stream ("old", stb);
10221 uiout->text ("\nNew value = ");
348d480f
PA
10222 }
10223 else
10224 {
10225 mention (b);
112e8700
SM
10226 if (uiout->is_mi_like_p ())
10227 uiout->field_string
10228 ("reason",
348d480f 10229 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10230 tuple_emitter.emplace (uiout, "value");
112e8700 10231 uiout->text ("\nValue = ");
348d480f 10232 }
850645cf 10233 watchpoint_value_print (w->val.get (), &stb);
112e8700
SM
10234 uiout->field_stream ("new", stb);
10235 uiout->text ("\n");
348d480f 10236 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10237 break;
10238 default:
348d480f 10239 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10240 }
10241
348d480f
PA
10242 return result;
10243}
10244
10245/* Implement the "print_mention" breakpoint_ops method for hardware
10246 watchpoints. */
10247
10248static void
10249print_mention_watchpoint (struct breakpoint *b)
10250{
3a5c3e22 10251 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10252 struct ui_out *uiout = current_uiout;
46b9c129 10253 const char *tuple_name;
348d480f
PA
10254
10255 switch (b->type)
10256 {
10257 case bp_watchpoint:
112e8700 10258 uiout->text ("Watchpoint ");
46b9c129 10259 tuple_name = "wpt";
348d480f
PA
10260 break;
10261 case bp_hardware_watchpoint:
112e8700 10262 uiout->text ("Hardware watchpoint ");
46b9c129 10263 tuple_name = "wpt";
348d480f
PA
10264 break;
10265 case bp_read_watchpoint:
112e8700 10266 uiout->text ("Hardware read watchpoint ");
46b9c129 10267 tuple_name = "hw-rwpt";
348d480f
PA
10268 break;
10269 case bp_access_watchpoint:
112e8700 10270 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10271 tuple_name = "hw-awpt";
348d480f
PA
10272 break;
10273 default:
10274 internal_error (__FILE__, __LINE__,
10275 _("Invalid hardware watchpoint type."));
10276 }
10277
46b9c129 10278 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10279 uiout->field_signed ("number", b->number);
112e8700
SM
10280 uiout->text (": ");
10281 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10282}
10283
10284/* Implement the "print_recreate" breakpoint_ops method for
10285 watchpoints. */
10286
10287static void
10288print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10289{
3a5c3e22
PA
10290 struct watchpoint *w = (struct watchpoint *) b;
10291
348d480f
PA
10292 switch (b->type)
10293 {
10294 case bp_watchpoint:
10295 case bp_hardware_watchpoint:
10296 fprintf_unfiltered (fp, "watch");
10297 break;
10298 case bp_read_watchpoint:
10299 fprintf_unfiltered (fp, "rwatch");
10300 break;
10301 case bp_access_watchpoint:
10302 fprintf_unfiltered (fp, "awatch");
10303 break;
10304 default:
10305 internal_error (__FILE__, __LINE__,
10306 _("Invalid watchpoint type."));
10307 }
10308
3a5c3e22 10309 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10310 print_recreate_thread (b, fp);
348d480f
PA
10311}
10312
427cd150
TT
10313/* Implement the "explains_signal" breakpoint_ops method for
10314 watchpoints. */
10315
47591c29 10316static int
427cd150
TT
10317explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10318{
10319 /* A software watchpoint cannot cause a signal other than
10320 GDB_SIGNAL_TRAP. */
10321 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10322 return 0;
427cd150 10323
47591c29 10324 return 1;
427cd150
TT
10325}
10326
348d480f
PA
10327/* The breakpoint_ops structure to be used in hardware watchpoints. */
10328
2060206e 10329static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10330
10331/* Implement the "insert" breakpoint_ops method for
10332 masked hardware watchpoints. */
10333
10334static int
10335insert_masked_watchpoint (struct bp_location *bl)
10336{
3a5c3e22
PA
10337 struct watchpoint *w = (struct watchpoint *) bl->owner;
10338
10339 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10340 bl->watchpoint_type);
10341}
10342
10343/* Implement the "remove" breakpoint_ops method for
10344 masked hardware watchpoints. */
10345
10346static int
73971819 10347remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10348{
3a5c3e22
PA
10349 struct watchpoint *w = (struct watchpoint *) bl->owner;
10350
10351 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
dda83cd7 10352 bl->watchpoint_type);
348d480f
PA
10353}
10354
10355/* Implement the "resources_needed" breakpoint_ops method for
10356 masked hardware watchpoints. */
10357
10358static int
10359resources_needed_masked_watchpoint (const struct bp_location *bl)
10360{
3a5c3e22
PA
10361 struct watchpoint *w = (struct watchpoint *) bl->owner;
10362
10363 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10364}
10365
10366/* Implement the "works_in_software_mode" breakpoint_ops method for
10367 masked hardware watchpoints. */
10368
10369static int
10370works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10371{
10372 return 0;
10373}
10374
10375/* Implement the "print_it" breakpoint_ops method for
10376 masked hardware watchpoints. */
10377
10378static enum print_stop_action
10379print_it_masked_watchpoint (bpstat bs)
10380{
10381 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10382 struct ui_out *uiout = current_uiout;
348d480f
PA
10383
10384 /* Masked watchpoints have only one location. */
10385 gdb_assert (b->loc && b->loc->next == NULL);
10386
f303dbd6
PA
10387 annotate_watchpoint (b->number);
10388 maybe_print_thread_hit_breakpoint (uiout);
10389
348d480f
PA
10390 switch (b->type)
10391 {
10392 case bp_hardware_watchpoint:
112e8700
SM
10393 if (uiout->is_mi_like_p ())
10394 uiout->field_string
10395 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10396 break;
10397
10398 case bp_read_watchpoint:
112e8700
SM
10399 if (uiout->is_mi_like_p ())
10400 uiout->field_string
10401 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10402 break;
10403
10404 case bp_access_watchpoint:
112e8700
SM
10405 if (uiout->is_mi_like_p ())
10406 uiout->field_string
10407 ("reason",
348d480f
PA
10408 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10409 break;
10410 default:
10411 internal_error (__FILE__, __LINE__,
10412 _("Invalid hardware watchpoint type."));
10413 }
10414
10415 mention (b);
112e8700 10416 uiout->text (_("\n\
9c06b0b4
TJB
10417Check the underlying instruction at PC for the memory\n\
10418address and value which triggered this watchpoint.\n"));
112e8700 10419 uiout->text ("\n");
9c06b0b4
TJB
10420
10421 /* More than one watchpoint may have been triggered. */
10422 return PRINT_UNKNOWN;
10423}
10424
10425/* Implement the "print_one_detail" breakpoint_ops method for
10426 masked hardware watchpoints. */
10427
10428static void
10429print_one_detail_masked_watchpoint (const struct breakpoint *b,
10430 struct ui_out *uiout)
10431{
3a5c3e22
PA
10432 struct watchpoint *w = (struct watchpoint *) b;
10433
9c06b0b4
TJB
10434 /* Masked watchpoints have only one location. */
10435 gdb_assert (b->loc && b->loc->next == NULL);
10436
112e8700
SM
10437 uiout->text ("\tmask ");
10438 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10439 uiout->text ("\n");
9c06b0b4
TJB
10440}
10441
10442/* Implement the "print_mention" breakpoint_ops method for
10443 masked hardware watchpoints. */
10444
10445static void
10446print_mention_masked_watchpoint (struct breakpoint *b)
10447{
3a5c3e22 10448 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10449 struct ui_out *uiout = current_uiout;
46b9c129 10450 const char *tuple_name;
9c06b0b4
TJB
10451
10452 switch (b->type)
10453 {
10454 case bp_hardware_watchpoint:
112e8700 10455 uiout->text ("Masked hardware watchpoint ");
46b9c129 10456 tuple_name = "wpt";
9c06b0b4
TJB
10457 break;
10458 case bp_read_watchpoint:
112e8700 10459 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10460 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10461 break;
10462 case bp_access_watchpoint:
112e8700 10463 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10464 tuple_name = "hw-awpt";
9c06b0b4
TJB
10465 break;
10466 default:
10467 internal_error (__FILE__, __LINE__,
10468 _("Invalid hardware watchpoint type."));
10469 }
10470
46b9c129 10471 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
381befee 10472 uiout->field_signed ("number", b->number);
112e8700
SM
10473 uiout->text (": ");
10474 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10475}
10476
10477/* Implement the "print_recreate" breakpoint_ops method for
10478 masked hardware watchpoints. */
10479
10480static void
10481print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10482{
3a5c3e22 10483 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10484
10485 switch (b->type)
10486 {
10487 case bp_hardware_watchpoint:
10488 fprintf_unfiltered (fp, "watch");
10489 break;
10490 case bp_read_watchpoint:
10491 fprintf_unfiltered (fp, "rwatch");
10492 break;
10493 case bp_access_watchpoint:
10494 fprintf_unfiltered (fp, "awatch");
10495 break;
10496 default:
10497 internal_error (__FILE__, __LINE__,
10498 _("Invalid hardware watchpoint type."));
10499 }
10500
53807e9f
TT
10501 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string,
10502 phex (w->hw_wp_mask, sizeof (CORE_ADDR)));
d9b3f62e 10503 print_recreate_thread (b, fp);
9c06b0b4
TJB
10504}
10505
10506/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10507
2060206e 10508static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10509
10510/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10511
f2478a7e 10512static bool
9c06b0b4
TJB
10513is_masked_watchpoint (const struct breakpoint *b)
10514{
10515 return b->ops == &masked_watchpoint_breakpoint_ops;
10516}
10517
53a5351d 10518/* accessflag: hw_write: watch write,
dda83cd7 10519 hw_read: watch read,
53a5351d 10520 hw_access: watch access (read or write) */
c906108c 10521static void
bbc13ae3 10522watch_command_1 (const char *arg, int accessflag, int from_tty,
2e362716 10523 bool just_location, bool internal)
c906108c 10524{
c1fc2657 10525 struct breakpoint *scope_breakpoint = NULL;
270140bd 10526 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
b926417a 10527 struct value *result;
bb9d5f81 10528 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10529 const char *exp_start = NULL;
10530 const char *exp_end = NULL;
10531 const char *tok, *end_tok;
9c06b0b4 10532 int toklen = -1;
bbc13ae3
KS
10533 const char *cond_start = NULL;
10534 const char *cond_end = NULL;
c906108c 10535 enum bptype bp_type;
37e4754d 10536 int thread = -1;
9c06b0b4
TJB
10537 /* Flag to indicate whether we are going to use masks for
10538 the hardware watchpoint. */
2e362716 10539 bool use_mask = false;
9c06b0b4 10540 CORE_ADDR mask = 0;
c906108c 10541
37e4754d
LM
10542 /* Make sure that we actually have parameters to parse. */
10543 if (arg != NULL && arg[0] != '\0')
10544 {
bbc13ae3
KS
10545 const char *value_start;
10546
10547 exp_end = arg + strlen (arg);
37e4754d 10548
9c06b0b4
TJB
10549 /* Look for "parameter value" pairs at the end
10550 of the arguments string. */
bbc13ae3 10551 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10552 {
10553 /* Skip whitespace at the end of the argument list. */
10554 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10555 tok--;
10556
10557 /* Find the beginning of the last token.
10558 This is the value of the parameter. */
10559 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10560 tok--;
10561 value_start = tok + 1;
10562
10563 /* Skip whitespace. */
10564 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10565 tok--;
10566
10567 end_tok = tok;
10568
10569 /* Find the beginning of the second to last token.
10570 This is the parameter itself. */
10571 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10572 tok--;
10573 tok++;
10574 toklen = end_tok - tok + 1;
10575
61012eef 10576 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10577 {
5d5658a1 10578 struct thread_info *thr;
9c06b0b4
TJB
10579 /* At this point we've found a "thread" token, which means
10580 the user is trying to set a watchpoint that triggers
10581 only in a specific thread. */
5d5658a1 10582 const char *endp;
37e4754d 10583
9c06b0b4
TJB
10584 if (thread != -1)
10585 error(_("You can specify only one thread."));
37e4754d 10586
9c06b0b4 10587 /* Extract the thread ID from the next token. */
5d5658a1 10588 thr = parse_thread_id (value_start, &endp);
37e4754d 10589
5d5658a1 10590 /* Check if the user provided a valid thread ID. */
9c06b0b4 10591 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10592 invalid_thread_id_error (value_start);
9c06b0b4 10593
5d5658a1 10594 thread = thr->global_num;
9c06b0b4 10595 }
61012eef 10596 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10597 {
10598 /* We've found a "mask" token, which means the user wants to
10599 create a hardware watchpoint that is going to have the mask
10600 facility. */
10601 struct value *mask_value, *mark;
37e4754d 10602
9c06b0b4
TJB
10603 if (use_mask)
10604 error(_("You can specify only one mask."));
37e4754d 10605
2e362716 10606 use_mask = just_location = true;
37e4754d 10607
9c06b0b4
TJB
10608 mark = value_mark ();
10609 mask_value = parse_to_comma_and_eval (&value_start);
10610 mask = value_as_address (mask_value);
10611 value_free_to_mark (mark);
10612 }
10613 else
10614 /* We didn't recognize what we found. We should stop here. */
10615 break;
37e4754d 10616
9c06b0b4
TJB
10617 /* Truncate the string and get rid of the "parameter value" pair before
10618 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10619 exp_end = tok;
9c06b0b4 10620 }
37e4754d 10621 }
bbc13ae3
KS
10622 else
10623 exp_end = arg;
37e4754d 10624
bbc13ae3
KS
10625 /* Parse the rest of the arguments. From here on out, everything
10626 is in terms of a newly allocated string instead of the original
10627 ARG. */
81b1e71c
TT
10628 std::string expression (arg, exp_end - arg);
10629 exp_start = arg = expression.c_str ();
699bd4cf
TT
10630 innermost_block_tracker tracker;
10631 expression_up exp = parse_exp_1 (&arg, 0, 0, 0, &tracker);
c906108c 10632 exp_end = arg;
fa8a61dc
TT
10633 /* Remove trailing whitespace from the expression before saving it.
10634 This makes the eventual display of the expression string a bit
10635 prettier. */
10636 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10637 --exp_end;
10638
65d79d4b 10639 /* Checking if the expression is not constant. */
4d01a485 10640 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10641 {
10642 int len;
10643
10644 len = exp_end - exp_start;
10645 while (len > 0 && isspace (exp_start[len - 1]))
10646 len--;
10647 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10648 }
10649
699bd4cf 10650 exp_valid_block = tracker.block ();
b926417a 10651 struct value *mark = value_mark ();
850645cf 10652 struct value *val_as_value = nullptr;
1eaebe02
TT
10653 fetch_subexp_value (exp.get (), exp->op.get (), &val_as_value, &result, NULL,
10654 just_location);
06a64a0b 10655
850645cf 10656 if (val_as_value != NULL && just_location)
bb9d5f81 10657 {
850645cf
TT
10658 saved_bitpos = value_bitpos (val_as_value);
10659 saved_bitsize = value_bitsize (val_as_value);
bb9d5f81
PP
10660 }
10661
850645cf 10662 value_ref_ptr val;
06a64a0b
TT
10663 if (just_location)
10664 {
9c06b0b4
TJB
10665 int ret;
10666
06a64a0b 10667 exp_valid_block = NULL;
850645cf 10668 val = release_value (value_addr (result));
06a64a0b 10669 value_free_to_mark (mark);
9c06b0b4
TJB
10670
10671 if (use_mask)
10672 {
850645cf 10673 ret = target_masked_watch_num_registers (value_as_address (val.get ()),
9c06b0b4
TJB
10674 mask);
10675 if (ret == -1)
10676 error (_("This target does not support masked watchpoints."));
10677 else if (ret == -2)
10678 error (_("Invalid mask or memory region."));
10679 }
06a64a0b 10680 }
850645cf
TT
10681 else if (val_as_value != NULL)
10682 val = release_value (val_as_value);
c906108c 10683
f1735a53
TT
10684 tok = skip_spaces (arg);
10685 end_tok = skip_to_space (tok);
c906108c
SS
10686
10687 toklen = end_tok - tok;
10688 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10689 {
10690 tok = cond_start = end_tok + 1;
699bd4cf
TT
10691 innermost_block_tracker if_tracker;
10692 parse_exp_1 (&tok, 0, 0, 0, &if_tracker);
60e1c644
PA
10693
10694 /* The watchpoint expression may not be local, but the condition
10695 may still be. E.g.: `watch global if local > 0'. */
699bd4cf 10696 cond_exp_valid_block = if_tracker.block ();
60e1c644 10697
c906108c
SS
10698 cond_end = tok;
10699 }
10700 if (*tok)
8a3fe4f8 10701 error (_("Junk at end of command."));
c906108c 10702
441d7c93
PA
10703 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10704
10705 /* Save this because create_internal_breakpoint below invalidates
10706 'wp_frame'. */
10707 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10708
10709 /* If the expression is "local", then set up a "watchpoint scope"
10710 breakpoint at the point where we've left the scope of the watchpoint
10711 expression. Create the scope breakpoint before the watchpoint, so
10712 that we will encounter it first in bpstat_stop_status. */
441d7c93 10713 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10714 {
441d7c93
PA
10715 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10716
10717 if (frame_id_p (caller_frame_id))
edb3359d 10718 {
441d7c93
PA
10719 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10720 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10721
edb3359d 10722 scope_breakpoint
441d7c93 10723 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10724 bp_watchpoint_scope,
10725 &momentary_breakpoint_ops);
d983da9c 10726
441d7c93
PA
10727 /* create_internal_breakpoint could invalidate WP_FRAME. */
10728 wp_frame = NULL;
10729
edb3359d 10730 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10731
edb3359d
DJ
10732 /* Automatically delete the breakpoint when it hits. */
10733 scope_breakpoint->disposition = disp_del;
d983da9c 10734
edb3359d 10735 /* Only break in the proper frame (help with recursion). */
441d7c93 10736 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10737
edb3359d 10738 /* Set the address at which we will stop. */
441d7c93
PA
10739 scope_breakpoint->loc->gdbarch = caller_arch;
10740 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10741 scope_breakpoint->loc->address
a6d9a66e
UW
10742 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10743 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10744 scope_breakpoint->type);
10745 }
d983da9c
DJ
10746 }
10747
e8369a73
AB
10748 /* Now set up the breakpoint. We create all watchpoints as hardware
10749 watchpoints here even if hardware watchpoints are turned off, a call
10750 to update_watchpoint later in this function will cause the type to
10751 drop back to bp_watchpoint (software watchpoint) if required. */
10752
10753 if (accessflag == hw_read)
10754 bp_type = bp_read_watchpoint;
10755 else if (accessflag == hw_access)
10756 bp_type = bp_access_watchpoint;
10757 else
10758 bp_type = bp_hardware_watchpoint;
3a5c3e22 10759
b270e6f9 10760 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10761
348d480f 10762 if (use_mask)
b270e6f9 10763 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10764 &masked_watchpoint_breakpoint_ops);
348d480f 10765 else
b270e6f9 10766 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10767 &watchpoint_breakpoint_ops);
c1fc2657
SM
10768 w->thread = thread;
10769 w->disposition = disp_donttouch;
10770 w->pspace = current_program_space;
b22e99fd 10771 w->exp = std::move (exp);
3a5c3e22
PA
10772 w->exp_valid_block = exp_valid_block;
10773 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10774 if (just_location)
10775 {
850645cf
TT
10776 struct type *t = value_type (val.get ());
10777 CORE_ADDR addr = value_as_address (val.get ());
06a64a0b 10778
43cc5389 10779 w->exp_string_reparse
f16a9f57 10780 = current_language->watch_location_expression (t, addr).release ();
06a64a0b 10781
3a5c3e22 10782 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10783 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10784 }
10785 else
3a5c3e22 10786 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10787
10788 if (use_mask)
10789 {
3a5c3e22 10790 w->hw_wp_mask = mask;
9c06b0b4
TJB
10791 }
10792 else
10793 {
3a5c3e22 10794 w->val = val;
bb9d5f81
PP
10795 w->val_bitpos = saved_bitpos;
10796 w->val_bitsize = saved_bitsize;
4c1d86d9 10797 w->val_valid = true;
9c06b0b4 10798 }
77b06cd7 10799
c906108c 10800 if (cond_start)
c1fc2657 10801 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10802 else
c1fc2657 10803 w->cond_string = 0;
c5aa993b 10804
441d7c93 10805 if (frame_id_p (watchpoint_frame))
f6bc2008 10806 {
441d7c93 10807 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10808 w->watchpoint_thread = inferior_ptid;
f6bc2008 10809 }
c906108c 10810 else
f6bc2008 10811 {
3a5c3e22
PA
10812 w->watchpoint_frame = null_frame_id;
10813 w->watchpoint_thread = null_ptid;
f6bc2008 10814 }
c906108c 10815
d983da9c 10816 if (scope_breakpoint != NULL)
c906108c 10817 {
d983da9c
DJ
10818 /* The scope breakpoint is related to the watchpoint. We will
10819 need to act on them together. */
c1fc2657 10820 w->related_breakpoint = scope_breakpoint;
b270e6f9 10821 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10822 }
d983da9c 10823
06a64a0b
TT
10824 if (!just_location)
10825 value_free_to_mark (mark);
2d134ed3 10826
b270e6f9
TT
10827 /* Finally update the new watchpoint. This creates the locations
10828 that should be inserted. */
10829 update_watchpoint (w.get (), 1);
a9634178 10830
b270e6f9 10831 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10832}
10833
e09342b5 10834/* Return count of debug registers needed to watch the given expression.
e09342b5 10835 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10836
c906108c 10837static int
a6535de1 10838can_use_hardware_watchpoint (const std::vector<value_ref_ptr> &vals)
c906108c
SS
10839{
10840 int found_memory_cnt = 0;
10841
10842 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10843 if (!can_use_hw_watchpoints)
c906108c 10844 return 0;
c5aa993b 10845
a6535de1
TT
10846 gdb_assert (!vals.empty ());
10847 struct value *head = vals[0].get ();
10848
5c44784c
JM
10849 /* Make sure that the value of the expression depends only upon
10850 memory contents, and values computed from them within GDB. If we
10851 find any register references or function calls, we can't use a
10852 hardware watchpoint.
10853
10854 The idea here is that evaluating an expression generates a series
10855 of values, one holding the value of every subexpression. (The
10856 expression a*b+c has five subexpressions: a, b, a*b, c, and
10857 a*b+c.) GDB's values hold almost enough information to establish
10858 the criteria given above --- they identify memory lvalues,
10859 register lvalues, computed values, etcetera. So we can evaluate
10860 the expression, and then scan the chain of values that leaves
10861 behind to decide whether we can detect any possible change to the
10862 expression's final value using only hardware watchpoints.
10863
10864 However, I don't think that the values returned by inferior
10865 function calls are special in any way. So this function may not
10866 notice that an expression involving an inferior function call
10867 can't be watched with hardware watchpoints. FIXME. */
a6535de1 10868 for (const value_ref_ptr &iter : vals)
c906108c 10869 {
a6535de1
TT
10870 struct value *v = iter.get ();
10871
5c44784c 10872 if (VALUE_LVAL (v) == lval_memory)
c906108c 10873 {
8464be76
DJ
10874 if (v != head && value_lazy (v))
10875 /* A lazy memory lvalue in the chain is one that GDB never
10876 needed to fetch; we either just used its address (e.g.,
10877 `a' in `a.b') or we never needed it at all (e.g., `a'
10878 in `a,b'). This doesn't apply to HEAD; if that is
10879 lazy then it was not readable, but watch it anyway. */
5c44784c 10880 ;
53a5351d 10881 else
5c44784c
JM
10882 {
10883 /* Ahh, memory we actually used! Check if we can cover
dda83cd7 10884 it with hardware watchpoints. */
df407dfe 10885 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10886
10887 /* We only watch structs and arrays if user asked for it
10888 explicitly, never if they just happen to appear in a
10889 middle of some value chain. */
10890 if (v == head
78134374
SM
10891 || (vtype->code () != TYPE_CODE_STRUCT
10892 && vtype->code () != TYPE_CODE_ARRAY))
2e70b7b9 10893 {
42ae5230 10894 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10895 int len;
10896 int num_regs;
10897
a9634178 10898 len = (target_exact_watchpoints
e09342b5
TJB
10899 && is_scalar_type_recursive (vtype))?
10900 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10901
e09342b5
TJB
10902 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10903 if (!num_regs)
2e70b7b9
MS
10904 return 0;
10905 else
e09342b5 10906 found_memory_cnt += num_regs;
2e70b7b9 10907 }
5c44784c 10908 }
c5aa993b 10909 }
5086187c
AC
10910 else if (VALUE_LVAL (v) != not_lval
10911 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10912 return 0; /* These are values from the history (e.g., $1). */
5086187c 10913 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10914 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10915 }
10916
10917 /* The expression itself looks suitable for using a hardware
10918 watchpoint, but give the target machine a chance to reject it. */
10919 return found_memory_cnt;
10920}
10921
8b93c638 10922void
2e362716 10923watch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 10924{
84f4c1fe 10925 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10926}
10927
a15a5258
AB
10928/* Options for the watch, awatch, and rwatch commands. */
10929
10930struct watch_options
10931{
10932 /* For -location. */
10933 bool location = false;
10934};
10935
10936/* Definitions of options for the "watch", "awatch", and "rwatch" commands.
10937
10938 Historically GDB always accepted both '-location' and '-l' flags for
10939 these commands (both flags being synonyms). When converting to the
10940 newer option scheme only '-location' is added here. That's fine (for
10941 backward compatibility) as any non-ambiguous prefix of a flag will be
10942 accepted, so '-l', '-loc', are now all accepted.
10943
10944 What this means is that, if in the future, we add any new flag here
10945 that starts with '-l' then this will break backward compatibility, so
10946 please, don't do that! */
10947
10948static const gdb::option::option_def watch_option_defs[] = {
10949 gdb::option::flag_option_def<watch_options> {
10950 "location",
10951 [] (watch_options *opt) { return &opt->location; },
10952 N_("\
10953This evaluates EXPRESSION and watches the memory to which is refers.\n\
10954-l can be used as a short form of -location."),
10955 },
10956};
10957
10958/* Returns the option group used by 'watch', 'awatch', and 'rwatch'
10959 commands. */
10960
10961static gdb::option::option_def_group
10962make_watch_options_def_group (watch_options *opts)
10963{
10964 return {{watch_option_defs}, opts};
10965}
10966
06a64a0b
TT
10967/* A helper function that looks for the "-location" argument and then
10968 calls watch_command_1. */
10969
10970static void
0b39b52e 10971watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b 10972{
a15a5258
AB
10973 watch_options opts;
10974 auto grp = make_watch_options_def_group (&opts);
10975 gdb::option::process_options
10976 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp);
10977 if (arg != nullptr && *arg == '\0')
10978 arg = nullptr;
10979
10980 watch_command_1 (arg, accessflag, from_tty, opts.location, false);
10981}
06a64a0b 10982
a15a5258
AB
10983/* Command completion for 'watch', 'awatch', and 'rwatch' commands. */
10984static void
10985watch_command_completer (struct cmd_list_element *ignore,
10986 completion_tracker &tracker,
10987 const char *text, const char * /*word*/)
10988{
10989 const auto group = make_watch_options_def_group (nullptr);
10990 if (gdb::option::complete_options
10991 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
10992 return;
06a64a0b 10993
a15a5258
AB
10994 const char *word = advance_to_expression_complete_word_point (tracker, text);
10995 expression_completer (ignore, tracker, text, word);
8b93c638 10996}
8926118c 10997
c5aa993b 10998static void
0b39b52e 10999watch_command (const char *arg, int from_tty)
c906108c 11000{
06a64a0b 11001 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11002}
11003
8b93c638 11004void
2e362716 11005rwatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11006{
84f4c1fe 11007 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11008}
8926118c 11009
c5aa993b 11010static void
0b39b52e 11011rwatch_command (const char *arg, int from_tty)
c906108c 11012{
06a64a0b 11013 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11014}
11015
8b93c638 11016void
2e362716 11017awatch_command_wrapper (const char *arg, int from_tty, bool internal)
8b93c638 11018{
84f4c1fe 11019 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11020}
8926118c 11021
c5aa993b 11022static void
0b39b52e 11023awatch_command (const char *arg, int from_tty)
c906108c 11024{
06a64a0b 11025 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11026}
c906108c 11027\f
c5aa993b 11028
cfc31633
PA
11029/* Data for the FSM that manages the until(location)/advance commands
11030 in infcmd.c. Here because it uses the mechanisms of
11031 breakpoints. */
c906108c 11032
46e3ed7f 11033struct until_break_fsm : public thread_fsm
bfec99b2 11034{
46e3ed7f 11035 /* The thread that was current when the command was executed. */
cfc31633
PA
11036 int thread;
11037
df631783
PA
11038 /* The breakpoint set at the return address in the caller frame,
11039 plus breakpoints at all the destination locations. */
11040 std::vector<breakpoint_up> breakpoints;
cfc31633 11041
46e3ed7f 11042 until_break_fsm (struct interp *cmd_interp, int thread,
df631783 11043 std::vector<breakpoint_up> &&breakpoints)
46e3ed7f
TT
11044 : thread_fsm (cmd_interp),
11045 thread (thread),
df631783 11046 breakpoints (std::move (breakpoints))
46e3ed7f
TT
11047 {
11048 }
cfc31633 11049
46e3ed7f
TT
11050 void clean_up (struct thread_info *thread) override;
11051 bool should_stop (struct thread_info *thread) override;
11052 enum async_reply_reason do_async_reply_reason () override;
cfc31633
PA
11053};
11054
cfc31633
PA
11055/* Implementation of the 'should_stop' FSM method for the
11056 until(location)/advance commands. */
11057
46e3ed7f
TT
11058bool
11059until_break_fsm::should_stop (struct thread_info *tp)
cfc31633 11060{
df631783
PA
11061 for (const breakpoint_up &bp : breakpoints)
11062 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11063 bp.get ()) != NULL)
11064 {
11065 set_finished ();
11066 break;
11067 }
cfc31633 11068
46e3ed7f 11069 return true;
cfc31633
PA
11070}
11071
11072/* Implementation of the 'clean_up' FSM method for the
11073 until(location)/advance commands. */
11074
46e3ed7f
TT
11075void
11076until_break_fsm::clean_up (struct thread_info *)
43ff13b4 11077{
cfc31633 11078 /* Clean up our temporary breakpoints. */
df631783 11079 breakpoints.clear ();
46e3ed7f 11080 delete_longjmp_breakpoint (thread);
cfc31633
PA
11081}
11082
11083/* Implementation of the 'async_reply_reason' FSM method for the
11084 until(location)/advance commands. */
11085
46e3ed7f
TT
11086enum async_reply_reason
11087until_break_fsm::do_async_reply_reason ()
cfc31633
PA
11088{
11089 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11090}
11091
c906108c 11092void
f2fc3015 11093until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11094{
8556afb4
PA
11095 struct frame_info *frame;
11096 struct gdbarch *frame_gdbarch;
11097 struct frame_id stack_frame_id;
11098 struct frame_id caller_frame_id;
186c406b
TT
11099 int thread;
11100 struct thread_info *tp;
c906108c 11101
70509625 11102 clear_proceed_status (0);
c906108c
SS
11103
11104 /* Set a breakpoint where the user wants it and at return from
4a64f543 11105 this function. */
c5aa993b 11106
ffc2605c 11107 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11108
6c5b2ebe
PA
11109 std::vector<symtab_and_line> sals
11110 = (last_displayed_sal_is_valid ()
11111 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11112 get_last_displayed_symtab (),
11113 get_last_displayed_line ())
11114 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
cafb3438 11115 NULL, NULL, 0));
c5aa993b 11116
df631783 11117 if (sals.empty ())
8a3fe4f8 11118 error (_("Couldn't get information on specified line."));
c5aa993b 11119
c906108c 11120 if (*arg)
8a3fe4f8 11121 error (_("Junk at end of arguments."));
c5aa993b 11122
186c406b 11123 tp = inferior_thread ();
5d5658a1 11124 thread = tp->global_num;
186c406b 11125
8556afb4
PA
11126 /* Note linespec handling above invalidates the frame chain.
11127 Installing a breakpoint also invalidates the frame chain (as it
11128 may need to switch threads), so do any frame handling before
11129 that. */
11130
11131 frame = get_selected_frame (NULL);
11132 frame_gdbarch = get_frame_arch (frame);
11133 stack_frame_id = get_stack_frame_id (frame);
11134 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11135
ae66c1fc
EZ
11136 /* Keep within the current frame, or in frames called by the current
11137 one. */
edb3359d 11138
df631783 11139 std::vector<breakpoint_up> breakpoints;
5419bdae
TT
11140
11141 gdb::optional<delete_longjmp_breakpoint_cleanup> lj_deleter;
11142
883bc8d1 11143 if (frame_id_p (caller_frame_id))
c906108c 11144 {
883bc8d1 11145 struct symtab_and_line sal2;
cfc31633 11146 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11147
11148 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11149 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633 11150 caller_gdbarch = frame_unwind_caller_arch (frame);
df631783
PA
11151
11152 breakpoint_up caller_breakpoint
11153 = set_momentary_breakpoint (caller_gdbarch, sal2,
11154 caller_frame_id, bp_until);
11155 breakpoints.emplace_back (std::move (caller_breakpoint));
186c406b 11156
883bc8d1 11157 set_longjmp_breakpoint (tp, caller_frame_id);
5419bdae 11158 lj_deleter.emplace (thread);
c906108c 11159 }
c5aa993b 11160
c70a6932
JK
11161 /* set_momentary_breakpoint could invalidate FRAME. */
11162 frame = NULL;
11163
df631783
PA
11164 /* If the user told us to continue until a specified location, we
11165 don't specify a frame at which we need to stop. Otherwise,
11166 specify the selected frame, because we want to stop only at the
11167 very same frame. */
11168 frame_id stop_frame_id = anywhere ? null_frame_id : stack_frame_id;
11169
11170 for (symtab_and_line &sal : sals)
11171 {
11172 resolve_sal_pc (&sal);
11173
11174 breakpoint_up location_breakpoint
11175 = set_momentary_breakpoint (frame_gdbarch, sal,
11176 stop_frame_id, bp_until);
11177 breakpoints.emplace_back (std::move (location_breakpoint));
11178 }
883bc8d1 11179
46e3ed7f 11180 tp->thread_fsm = new until_break_fsm (command_interp (), tp->global_num,
df631783 11181 std::move (breakpoints));
f107f563 11182
5419bdae
TT
11183 if (lj_deleter)
11184 lj_deleter->release ();
f107f563 11185
cfc31633 11186 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11187}
ae66c1fc 11188
c906108c
SS
11189/* This function attempts to parse an optional "if <cond>" clause
11190 from the arg string. If one is not found, it returns NULL.
c5aa993b 11191
c906108c
SS
11192 Else, it returns a pointer to the condition string. (It does not
11193 attempt to evaluate the string against a particular block.) And,
11194 it updates arg to point to the first character following the parsed
4a64f543 11195 if clause in the arg string. */
53a5351d 11196
63160a43
PA
11197const char *
11198ep_parse_optional_if_clause (const char **arg)
c906108c 11199{
63160a43 11200 const char *cond_string;
c5aa993b
JM
11201
11202 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11203 return NULL;
c5aa993b 11204
4a64f543 11205 /* Skip the "if" keyword. */
c906108c 11206 (*arg) += 2;
c5aa993b 11207
c906108c 11208 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11209 condition string. */
f1735a53 11210 *arg = skip_spaces (*arg);
c906108c 11211 cond_string = *arg;
c5aa993b 11212
4a64f543
MS
11213 /* Assume that the condition occupies the remainder of the arg
11214 string. */
c906108c 11215 (*arg) += strlen (cond_string);
c5aa993b 11216
c906108c
SS
11217 return cond_string;
11218}
c5aa993b 11219
c906108c
SS
11220/* Commands to deal with catching events, such as signals, exceptions,
11221 process start/exit, etc. */
c5aa993b
JM
11222
11223typedef enum
11224{
44feb3ce
TT
11225 catch_fork_temporary, catch_vfork_temporary,
11226 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11227}
11228catch_fork_kind;
11229
c906108c 11230static void
eb4c3f4a 11231catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11232 struct cmd_list_element *command)
c906108c 11233{
a6d9a66e 11234 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11235 const char *cond_string = NULL;
44feb3ce 11236 catch_fork_kind fork_kind;
44feb3ce
TT
11237
11238 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
b650a282
SM
11239 bool temp = (fork_kind == catch_fork_temporary
11240 || fork_kind == catch_vfork_temporary);
c5aa993b 11241
44feb3ce
TT
11242 if (!arg)
11243 arg = "";
f1735a53 11244 arg = skip_spaces (arg);
c5aa993b 11245
c906108c 11246 /* The allowed syntax is:
c5aa993b
JM
11247 catch [v]fork
11248 catch [v]fork if <cond>
11249
4a64f543 11250 First, check if there's an if clause. */
c906108c 11251 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11252
c906108c 11253 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11254 error (_("Junk at end of arguments."));
c5aa993b 11255
c906108c 11256 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11257 and enable reporting of such events. */
c5aa993b
JM
11258 switch (fork_kind)
11259 {
44feb3ce
TT
11260 case catch_fork_temporary:
11261 case catch_fork_permanent:
b650a282 11262 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11263 &catch_fork_breakpoint_ops);
c906108c 11264 break;
44feb3ce
TT
11265 case catch_vfork_temporary:
11266 case catch_vfork_permanent:
b650a282 11267 create_fork_vfork_event_catchpoint (gdbarch, temp, cond_string,
dda83cd7 11268 &catch_vfork_breakpoint_ops);
c906108c 11269 break;
c5aa993b 11270 default:
8a3fe4f8 11271 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11272 break;
c5aa993b 11273 }
c906108c
SS
11274}
11275
11276static void
eb4c3f4a 11277catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11278 struct cmd_list_element *command)
c906108c 11279{
a6d9a66e 11280 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11281 const char *cond_string = NULL;
b650a282 11282 bool temp = get_cmd_context (command) == CATCH_TEMPORARY;
44feb3ce
TT
11283
11284 if (!arg)
11285 arg = "";
f1735a53 11286 arg = skip_spaces (arg);
c906108c
SS
11287
11288 /* The allowed syntax is:
c5aa993b
JM
11289 catch exec
11290 catch exec if <cond>
c906108c 11291
4a64f543 11292 First, check if there's an if clause. */
c906108c
SS
11293 cond_string = ep_parse_optional_if_clause (&arg);
11294
11295 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11296 error (_("Junk at end of arguments."));
c906108c 11297
b270e6f9 11298 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
b650a282 11299 init_catchpoint (c.get (), gdbarch, temp, cond_string,
b4d90040
PA
11300 &catch_exec_breakpoint_ops);
11301 c->exec_pathname = NULL;
11302
b270e6f9 11303 install_breakpoint (0, std::move (c), 1);
c906108c 11304}
c5aa993b 11305
9ac4176b 11306void
28010a5d
PA
11307init_ada_exception_breakpoint (struct breakpoint *b,
11308 struct gdbarch *gdbarch,
11309 struct symtab_and_line sal,
f2fc3015 11310 const char *addr_string,
c0a91b2b 11311 const struct breakpoint_ops *ops,
28010a5d 11312 int tempflag,
349774ef 11313 int enabled,
28010a5d 11314 int from_tty)
f7f9143b 11315{
f7f9143b
JB
11316 if (from_tty)
11317 {
5af949e3
UW
11318 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11319 if (!loc_gdbarch)
11320 loc_gdbarch = gdbarch;
11321
6c95b8df
PA
11322 describe_other_breakpoints (loc_gdbarch,
11323 sal.pspace, sal.pc, sal.section, -1);
f7f9143b 11324 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
dda83cd7
SM
11325 version for exception catchpoints, because two catchpoints
11326 used for different exception names will use the same address.
11327 In this case, a "breakpoint ... also set at..." warning is
11328 unproductive. Besides, the warning phrasing is also a bit
11329 inappropriate, we should use the word catchpoint, and tell
11330 the user what type of catchpoint it is. The above is good
11331 enough for now, though. */
f7f9143b
JB
11332 }
11333
f06f1252 11334 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
f7f9143b 11335
349774ef 11336 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11337 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11338 b->location = string_to_event_location (&addr_string,
11339 language_def (language_ada));
f7f9143b 11340 b->language = language_ada;
f7f9143b
JB
11341}
11342
c906108c
SS
11343\f
11344
81b1e71c 11345/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11346
11347static int
81b1e71c 11348compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11349{
81b1e71c
TT
11350 uintptr_t ua = (uintptr_t) a;
11351 uintptr_t ub = (uintptr_t) b;
8a2c437b 11352
81b1e71c 11353 if (a->number < b->number)
8a2c437b 11354 return -1;
81b1e71c 11355 else if (a->number > b->number)
8a2c437b
TT
11356 return 1;
11357
11358 /* Now sort by address, in case we see, e..g, two breakpoints with
11359 the number 0. */
11360 if (ua < ub)
11361 return -1;
94b0e70d 11362 return ua > ub ? 1 : 0;
8a2c437b
TT
11363}
11364
80f8a6eb 11365/* Delete breakpoints by address or line. */
c906108c
SS
11366
11367static void
0b39b52e 11368clear_command (const char *arg, int from_tty)
c906108c 11369{
c906108c 11370 int default_match;
c906108c 11371
6c5b2ebe
PA
11372 std::vector<symtab_and_line> decoded_sals;
11373 symtab_and_line last_sal;
11374 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11375 if (arg)
11376 {
6c5b2ebe
PA
11377 decoded_sals
11378 = decode_line_with_current_source (arg,
11379 (DECODE_LINE_FUNFIRSTLINE
11380 | DECODE_LINE_LIST_MODE));
c906108c 11381 default_match = 0;
6c5b2ebe 11382 sals = decoded_sals;
c906108c
SS
11383 }
11384 else
11385 {
1bfeeb0f
JL
11386 /* Set sal's line, symtab, pc, and pspace to the values
11387 corresponding to the last call to print_frame_info. If the
11388 codepoint is not valid, this will set all the fields to 0. */
51abb421 11389 last_sal = get_last_displayed_sal ();
6c5b2ebe 11390 if (last_sal.symtab == 0)
8a3fe4f8 11391 error (_("No source file specified."));
c906108c 11392
c906108c 11393 default_match = 1;
6c5b2ebe 11394 sals = last_sal;
c906108c
SS
11395 }
11396
4a64f543
MS
11397 /* We don't call resolve_sal_pc here. That's not as bad as it
11398 seems, because all existing breakpoints typically have both
11399 file/line and pc set. So, if clear is given file/line, we can
11400 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11401
11402 We only support clearing given the address explicitly
11403 present in breakpoint table. Say, we've set breakpoint
4a64f543 11404 at file:line. There were several PC values for that file:line,
ed0616c6 11405 due to optimization, all in one block.
4a64f543
MS
11406
11407 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11408 PC corresponding to the same file:line, the breakpoint won't
11409 be cleared. We probably can still clear the breakpoint, but
11410 since the other PC value is never presented to user, user
11411 can only find it by guessing, and it does not seem important
11412 to support that. */
11413
4a64f543
MS
11414 /* For each line spec given, delete bps which correspond to it. Do
11415 it in two passes, solely to preserve the current behavior that
11416 from_tty is forced true if we delete more than one
11417 breakpoint. */
c906108c 11418
81b1e71c 11419 std::vector<struct breakpoint *> found;
6c5b2ebe 11420 for (const auto &sal : sals)
c906108c 11421 {
05cba821
JK
11422 const char *sal_fullname;
11423
c906108c 11424 /* If exact pc given, clear bpts at that pc.
dda83cd7
SM
11425 If line given (pc == 0), clear all bpts on specified line.
11426 If defaulting, clear all bpts on default line
11427 or at default pc.
c5aa993b 11428
dda83cd7 11429 defaulting sal.pc != 0 tests to do
c5aa993b 11430
dda83cd7
SM
11431 0 1 pc
11432 1 1 pc _and_ line
11433 0 0 line
11434 1 0 <can't happen> */
c906108c 11435
05cba821
JK
11436 sal_fullname = (sal.symtab == NULL
11437 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11438
4a64f543 11439 /* Find all matching breakpoints and add them to 'found'. */
43892fdf 11440 for (breakpoint *b : all_breakpoints ())
c5aa993b 11441 {
0d381245 11442 int match = 0;
4a64f543 11443 /* Are we going to delete b? */
cc60f2e3 11444 if (b->type != bp_none && !is_watchpoint (b))
0d381245 11445 {
40cb8ca5 11446 for (bp_location *loc : b->locations ())
0d381245 11447 {
f8eba3c6
TT
11448 /* If the user specified file:line, don't allow a PC
11449 match. This matches historical gdb behavior. */
11450 int pc_match = (!sal.explicit_line
11451 && sal.pc
11452 && (loc->pspace == sal.pspace)
11453 && (loc->address == sal.pc)
11454 && (!section_is_overlay (loc->section)
11455 || loc->section == sal.section));
4aac40c8
TT
11456 int line_match = 0;
11457
11458 if ((default_match || sal.explicit_line)
2f202fde 11459 && loc->symtab != NULL
05cba821 11460 && sal_fullname != NULL
4aac40c8 11461 && sal.pspace == loc->pspace
05cba821
JK
11462 && loc->line_number == sal.line
11463 && filename_cmp (symtab_to_fullname (loc->symtab),
11464 sal_fullname) == 0)
11465 line_match = 1;
4aac40c8 11466
0d381245
VP
11467 if (pc_match || line_match)
11468 {
11469 match = 1;
11470 break;
11471 }
11472 }
11473 }
11474
11475 if (match)
81b1e71c 11476 found.push_back (b);
c906108c 11477 }
80f8a6eb 11478 }
8a2c437b 11479
80f8a6eb 11480 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11481 if (found.empty ())
80f8a6eb
MS
11482 {
11483 if (arg)
8a3fe4f8 11484 error (_("No breakpoint at %s."), arg);
80f8a6eb 11485 else
8a3fe4f8 11486 error (_("No breakpoint at this line."));
80f8a6eb 11487 }
c906108c 11488
8a2c437b 11489 /* Remove duplicates from the vec. */
81b1e71c 11490 std::sort (found.begin (), found.end (),
b926417a 11491 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11492 {
b926417a 11493 return compare_breakpoints (bp_a, bp_b) < 0;
81b1e71c
TT
11494 });
11495 found.erase (std::unique (found.begin (), found.end (),
b926417a 11496 [] (const breakpoint *bp_a, const breakpoint *bp_b)
81b1e71c 11497 {
b926417a 11498 return compare_breakpoints (bp_a, bp_b) == 0;
81b1e71c
TT
11499 }),
11500 found.end ());
8a2c437b 11501
81b1e71c 11502 if (found.size () > 1)
4a64f543 11503 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11504 if (from_tty)
a3f17187 11505 {
81b1e71c 11506 if (found.size () == 1)
a3f17187
AC
11507 printf_unfiltered (_("Deleted breakpoint "));
11508 else
11509 printf_unfiltered (_("Deleted breakpoints "));
11510 }
d6e956e5 11511
81b1e71c 11512 for (breakpoint *iter : found)
80f8a6eb 11513 {
c5aa993b 11514 if (from_tty)
81b1e71c
TT
11515 printf_unfiltered ("%d ", iter->number);
11516 delete_breakpoint (iter);
c906108c 11517 }
80f8a6eb
MS
11518 if (from_tty)
11519 putchar_unfiltered ('\n');
c906108c
SS
11520}
11521\f
11522/* Delete breakpoint in BS if they are `delete' breakpoints and
11523 all breakpoints that are marked for deletion, whether hit or not.
11524 This is called after any breakpoint is hit, or after errors. */
11525
11526void
fba45db2 11527breakpoint_auto_delete (bpstat bs)
c906108c 11528{
c906108c 11529 for (; bs; bs = bs->next)
f431efe5
PA
11530 if (bs->breakpoint_at
11531 && bs->breakpoint_at->disposition == disp_del
c906108c 11532 && bs->stop)
f431efe5 11533 delete_breakpoint (bs->breakpoint_at);
c906108c 11534
1428b37a 11535 for (breakpoint *b : all_breakpoints_safe ())
b5de0fa7 11536 if (b->disposition == disp_del_at_next_stop)
c5aa993b 11537 delete_breakpoint (b);
c906108c
SS
11538}
11539
4a64f543 11540/* A comparison function for bp_location AP and BP being interfaced to
39ef2f62 11541 std::sort. Sort elements primarily by their ADDRESS (no matter what
cb1e4e32
PA
11542 bl_address_is_meaningful says), secondarily by ordering first
11543 permanent elements and terciarily just ensuring the array is sorted
39ef2f62 11544 stable way despite std::sort being an unstable algorithm. */
876fa593
JK
11545
11546static int
39ef2f62 11547bp_location_is_less_than (const bp_location *a, const bp_location *b)
876fa593 11548{
876fa593 11549 if (a->address != b->address)
39ef2f62 11550 return a->address < b->address;
876fa593 11551
dea2aa5f
LM
11552 /* Sort locations at the same address by their pspace number, keeping
11553 locations of the same inferior (in a multi-inferior environment)
11554 grouped. */
11555
11556 if (a->pspace->num != b->pspace->num)
39ef2f62 11557 return a->pspace->num < b->pspace->num;
dea2aa5f 11558
876fa593 11559 /* Sort permanent breakpoints first. */
1a853c52 11560 if (a->permanent != b->permanent)
39ef2f62 11561 return a->permanent > b->permanent;
876fa593 11562
7f32a4d5
PA
11563 /* Sort by type in order to make duplicate determination easier.
11564 See update_global_location_list. This is kept in sync with
11565 breakpoint_locations_match. */
11566 if (a->loc_type < b->loc_type)
11567 return true;
11568
11569 /* Likewise, for range-breakpoints, sort by length. */
11570 if (a->loc_type == bp_loc_hardware_breakpoint
11571 && b->loc_type == bp_loc_hardware_breakpoint
11572 && a->length < b->length)
11573 return true;
11574
c56a97f9
JK
11575 /* Make the internal GDB representation stable across GDB runs
11576 where A and B memory inside GDB can differ. Breakpoint locations of
11577 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11578
11579 if (a->owner->number != b->owner->number)
39ef2f62 11580 return a->owner->number < b->owner->number;
876fa593 11581
39ef2f62 11582 return a < b;
876fa593
JK
11583}
11584
f5336ca5
PA
11585/* Set bp_locations_placed_address_before_address_max and
11586 bp_locations_shadow_len_after_address_max according to the current
11587 content of the bp_locations array. */
f7545552
TT
11588
11589static void
f5336ca5 11590bp_locations_target_extensions_update (void)
f7545552 11591{
f5336ca5
PA
11592 bp_locations_placed_address_before_address_max = 0;
11593 bp_locations_shadow_len_after_address_max = 0;
876fa593 11594
48d7020b 11595 for (bp_location *bl : all_bp_locations ())
876fa593
JK
11596 {
11597 CORE_ADDR start, end, addr;
11598
11599 if (!bp_location_has_shadow (bl))
11600 continue;
11601
11602 start = bl->target_info.placed_address;
11603 end = start + bl->target_info.shadow_len;
11604
11605 gdb_assert (bl->address >= start);
11606 addr = bl->address - start;
f5336ca5
PA
11607 if (addr > bp_locations_placed_address_before_address_max)
11608 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11609
11610 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11611
11612 gdb_assert (bl->address < end);
11613 addr = end - bl->address;
f5336ca5
PA
11614 if (addr > bp_locations_shadow_len_after_address_max)
11615 bp_locations_shadow_len_after_address_max = addr;
876fa593 11616 }
f7545552
TT
11617}
11618
1e4d1764
YQ
11619/* Download tracepoint locations if they haven't been. */
11620
11621static void
11622download_tracepoint_locations (void)
11623{
dd2e65cc 11624 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11625
5ed8105e 11626 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11627
f6d17b2b 11628 for (breakpoint *b : all_tracepoints ())
1e4d1764
YQ
11629 {
11630 struct tracepoint *t;
f2a8bc8a 11631 int bp_location_downloaded = 0;
1e4d1764 11632
7ed2c994 11633 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11634 ? !may_insert_fast_tracepoints
11635 : !may_insert_tracepoints))
11636 continue;
11637
dd2e65cc
YQ
11638 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11639 {
11640 if (target_can_download_tracepoint ())
11641 can_download_tracepoint = TRIBOOL_TRUE;
11642 else
11643 can_download_tracepoint = TRIBOOL_FALSE;
11644 }
11645
11646 if (can_download_tracepoint == TRIBOOL_FALSE)
11647 break;
11648
40cb8ca5 11649 for (bp_location *bl : b->locations ())
7ed2c994
YQ
11650 {
11651 /* In tracepoint, locations are _never_ duplicated, so
11652 should_be_inserted is equivalent to
11653 unduplicated_should_be_inserted. */
11654 if (!should_be_inserted (bl) || bl->inserted)
11655 continue;
1e4d1764 11656
7ed2c994 11657 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11658
7ed2c994 11659 target_download_tracepoint (bl);
1e4d1764 11660
7ed2c994 11661 bl->inserted = 1;
f2a8bc8a 11662 bp_location_downloaded = 1;
7ed2c994
YQ
11663 }
11664 t = (struct tracepoint *) b;
11665 t->number_on_target = b->number;
f2a8bc8a 11666 if (bp_location_downloaded)
76727919 11667 gdb::observers::breakpoint_modified.notify (b);
1e4d1764 11668 }
1e4d1764
YQ
11669}
11670
934709f0
PW
11671/* Swap the insertion/duplication state between two locations. */
11672
11673static void
11674swap_insertion (struct bp_location *left, struct bp_location *right)
11675{
11676 const int left_inserted = left->inserted;
11677 const int left_duplicate = left->duplicate;
b775012e 11678 const int left_needs_update = left->needs_update;
934709f0
PW
11679 const struct bp_target_info left_target_info = left->target_info;
11680
1e4d1764
YQ
11681 /* Locations of tracepoints can never be duplicated. */
11682 if (is_tracepoint (left->owner))
11683 gdb_assert (!left->duplicate);
11684 if (is_tracepoint (right->owner))
11685 gdb_assert (!right->duplicate);
11686
934709f0
PW
11687 left->inserted = right->inserted;
11688 left->duplicate = right->duplicate;
b775012e 11689 left->needs_update = right->needs_update;
934709f0
PW
11690 left->target_info = right->target_info;
11691 right->inserted = left_inserted;
11692 right->duplicate = left_duplicate;
b775012e 11693 right->needs_update = left_needs_update;
934709f0
PW
11694 right->target_info = left_target_info;
11695}
11696
b775012e
LM
11697/* Force the re-insertion of the locations at ADDRESS. This is called
11698 once a new/deleted/modified duplicate location is found and we are evaluating
11699 conditions on the target's side. Such conditions need to be updated on
11700 the target. */
11701
11702static void
11703force_breakpoint_reinsertion (struct bp_location *bl)
11704{
b775012e
LM
11705 CORE_ADDR address = 0;
11706 int pspace_num;
11707
11708 address = bl->address;
11709 pspace_num = bl->pspace->num;
11710
11711 /* This is only meaningful if the target is
11712 evaluating conditions and if the user has
11713 opted for condition evaluation on the target's
11714 side. */
11715 if (gdb_evaluates_breakpoint_condition_p ()
11716 || !target_supports_evaluation_of_breakpoint_conditions ())
11717 return;
11718
11719 /* Flag all breakpoint locations with this address and
11720 the same program space as the location
11721 as "its condition has changed". We need to
11722 update the conditions on the target's side. */
e0d9a270 11723 for (bp_location *loc : all_bp_locations_at_addr (address))
b775012e 11724 {
b775012e
LM
11725 if (!is_breakpoint (loc->owner)
11726 || pspace_num != loc->pspace->num)
11727 continue;
11728
11729 /* Flag the location appropriately. We use a different state to
11730 let everyone know that we already updated the set of locations
11731 with addr bl->address and program space bl->pspace. This is so
11732 we don't have to keep calling these functions just to mark locations
11733 that have already been marked. */
11734 loc->condition_changed = condition_updated;
11735
11736 /* Free the agent expression bytecode as well. We will compute
11737 it later on. */
833177a4 11738 loc->cond_bytecode.reset ();
b775012e
LM
11739 }
11740}
7f32a4d5 11741
44702360
PA
11742/* Called whether new breakpoints are created, or existing breakpoints
11743 deleted, to update the global location list and recompute which
11744 locations are duplicate of which.
b775012e 11745
04086b45
PA
11746 The INSERT_MODE flag determines whether locations may not, may, or
11747 shall be inserted now. See 'enum ugll_insert_mode' for more
11748 info. */
b60e7edf 11749
0d381245 11750static void
44702360 11751update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11752{
b775012e
LM
11753 /* Last breakpoint location address that was marked for update. */
11754 CORE_ADDR last_addr = 0;
11755 /* Last breakpoint location program space that was marked for update. */
11756 int last_pspace_num = -1;
f7545552 11757
2d134ed3
PA
11758 /* Used in the duplicates detection below. When iterating over all
11759 bp_locations, points to the first bp_location of a given address.
11760 Breakpoints and watchpoints of different types are never
11761 duplicates of each other. Keep one pointer for each type of
11762 breakpoint/watchpoint, so we only need to loop over all locations
11763 once. */
11764 struct bp_location *bp_loc_first; /* breakpoint */
11765 struct bp_location *wp_loc_first; /* hardware watchpoint */
11766 struct bp_location *awp_loc_first; /* access watchpoint */
11767 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11768
f5336ca5
PA
11769 /* Saved former bp_locations array which we compare against the newly
11770 built bp_locations from the current state of ALL_BREAKPOINTS. */
5d51cd5d
SM
11771 std::vector<bp_location *> old_locations = std::move (bp_locations);
11772 bp_locations.clear ();
876fa593 11773
43892fdf 11774 for (breakpoint *b : all_breakpoints ())
40cb8ca5 11775 for (bp_location *loc : b->locations ())
5d51cd5d 11776 bp_locations.push_back (loc);
7f32a4d5
PA
11777
11778 /* See if we need to "upgrade" a software breakpoint to a hardware
11779 breakpoint. Do this before deciding whether locations are
11780 duplicates. Also do this before sorting because sorting order
11781 depends on location type. */
5d51cd5d
SM
11782 for (bp_location *loc : bp_locations)
11783 if (!loc->inserted && should_be_inserted (loc))
7f32a4d5 11784 handle_automatic_hardware_breakpoints (loc);
7f32a4d5 11785
5d51cd5d 11786 std::sort (bp_locations.begin (), bp_locations.end (),
39ef2f62 11787 bp_location_is_less_than);
876fa593 11788
f5336ca5 11789 bp_locations_target_extensions_update ();
74960c60 11790
4a64f543
MS
11791 /* Identify bp_location instances that are no longer present in the
11792 new list, and therefore should be freed. Note that it's not
11793 necessary that those locations should be removed from inferior --
11794 if there's another location at the same address (previously
11795 marked as duplicate), we don't need to remove/insert the
11796 location.
876fa593 11797
4a64f543
MS
11798 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11799 and former bp_location array state respectively. */
876fa593 11800
5d51cd5d
SM
11801 size_t loc_i = 0;
11802 for (bp_location *old_loc : old_locations)
74960c60 11803 {
e5dd4106 11804 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11805 not, we have to free it. */
c7d46a38 11806 int found_object = 0;
20874c92
VP
11807 /* Tells if the location should remain inserted in the target. */
11808 int keep_in_target = 0;
11809 int removed = 0;
876fa593 11810
4a64f543
MS
11811 /* Skip LOCP entries which will definitely never be needed.
11812 Stop either at or being the one matching OLD_LOC. */
5d51cd5d
SM
11813 while (loc_i < bp_locations.size ()
11814 && bp_locations[loc_i]->address < old_loc->address)
11815 loc_i++;
c7d46a38 11816
5d51cd5d
SM
11817 for (size_t loc2_i = loc_i;
11818 (loc2_i < bp_locations.size ()
11819 && bp_locations[loc2_i]->address == old_loc->address);
11820 loc2_i++)
c7d46a38 11821 {
b775012e
LM
11822 /* Check if this is a new/duplicated location or a duplicated
11823 location that had its condition modified. If so, we want to send
11824 its condition to the target if evaluation of conditions is taking
11825 place there. */
5d51cd5d 11826 if (bp_locations[loc2_i]->condition_changed == condition_modified
b775012e
LM
11827 && (last_addr != old_loc->address
11828 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11829 {
5d51cd5d 11830 force_breakpoint_reinsertion (bp_locations[loc2_i]);
b775012e 11831 last_pspace_num = old_loc->pspace->num;
c7d46a38 11832 }
b775012e 11833
5d51cd5d 11834 if (bp_locations[loc2_i] == old_loc)
b775012e 11835 found_object = 1;
c7d46a38 11836 }
74960c60 11837
b775012e
LM
11838 /* We have already handled this address, update it so that we don't
11839 have to go through updates again. */
11840 last_addr = old_loc->address;
11841
11842 /* Target-side condition evaluation: Handle deleted locations. */
11843 if (!found_object)
11844 force_breakpoint_reinsertion (old_loc);
11845
4a64f543
MS
11846 /* If this location is no longer present, and inserted, look if
11847 there's maybe a new location at the same address. If so,
11848 mark that one inserted, and don't remove this one. This is
11849 needed so that we don't have a time window where a breakpoint
11850 at certain location is not inserted. */
74960c60 11851
876fa593 11852 if (old_loc->inserted)
0d381245 11853 {
4a64f543
MS
11854 /* If the location is inserted now, we might have to remove
11855 it. */
74960c60 11856
876fa593 11857 if (found_object && should_be_inserted (old_loc))
74960c60 11858 {
4a64f543
MS
11859 /* The location is still present in the location list,
11860 and still should be inserted. Don't do anything. */
20874c92 11861 keep_in_target = 1;
74960c60
VP
11862 }
11863 else
11864 {
b775012e
LM
11865 /* This location still exists, but it won't be kept in the
11866 target since it may have been disabled. We proceed to
11867 remove its target-side condition. */
11868
4a64f543
MS
11869 /* The location is either no longer present, or got
11870 disabled. See if there's another location at the
11871 same address, in which case we don't need to remove
11872 this one from the target. */
876fa593 11873
2bdf28a0 11874 /* OLD_LOC comes from existing struct breakpoint. */
cb1e4e32 11875 if (bl_address_is_meaningful (old_loc))
876fa593 11876 {
5d51cd5d
SM
11877 for (size_t loc2_i = loc_i;
11878 (loc2_i < bp_locations.size ()
11879 && bp_locations[loc2_i]->address == old_loc->address);
11880 loc2_i++)
876fa593 11881 {
5d51cd5d 11882 bp_location *loc2 = bp_locations[loc2_i];
876fa593 11883
7f32a4d5
PA
11884 if (loc2 == old_loc)
11885 continue;
11886
2d134ed3 11887 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 11888 {
85d721b8
PA
11889 /* Read watchpoint locations are switched to
11890 access watchpoints, if the former are not
11891 supported, but the latter are. */
11892 if (is_hardware_watchpoint (old_loc->owner))
11893 {
11894 gdb_assert (is_hardware_watchpoint (loc2->owner));
11895 loc2->watchpoint_type = old_loc->watchpoint_type;
11896 }
11897
934709f0
PW
11898 /* loc2 is a duplicated location. We need to check
11899 if it should be inserted in case it will be
11900 unduplicated. */
7f32a4d5 11901 if (unduplicated_should_be_inserted (loc2))
c7d46a38 11902 {
934709f0 11903 swap_insertion (old_loc, loc2);
c7d46a38
PA
11904 keep_in_target = 1;
11905 break;
11906 }
876fa593
JK
11907 }
11908 }
11909 }
74960c60
VP
11910 }
11911
20874c92
VP
11912 if (!keep_in_target)
11913 {
834c0d03 11914 if (remove_breakpoint (old_loc))
20874c92 11915 {
4a64f543
MS
11916 /* This is just about all we can do. We could keep
11917 this location on the global list, and try to
11918 remove it next time, but there's no particular
11919 reason why we will succeed next time.
20874c92 11920
4a64f543
MS
11921 Note that at this point, old_loc->owner is still
11922 valid, as delete_breakpoint frees the breakpoint
11923 only after calling us. */
3e43a32a
MS
11924 printf_filtered (_("warning: Error removing "
11925 "breakpoint %d\n"),
876fa593 11926 old_loc->owner->number);
20874c92
VP
11927 }
11928 removed = 1;
11929 }
0d381245 11930 }
74960c60
VP
11931
11932 if (!found_object)
1c5cfe86 11933 {
fbea99ea 11934 if (removed && target_is_non_stop_p ()
1cf4d951 11935 && need_moribund_for_location_type (old_loc))
20874c92 11936 {
db82e815
PA
11937 /* This location was removed from the target. In
11938 non-stop mode, a race condition is possible where
11939 we've removed a breakpoint, but stop events for that
11940 breakpoint are already queued and will arrive later.
11941 We apply an heuristic to be able to distinguish such
11942 SIGTRAPs from other random SIGTRAPs: we keep this
11943 breakpoint location for a bit, and will retire it
11944 after we see some number of events. The theory here
11945 is that reporting of events should, "on the average",
11946 be fair, so after a while we'll see events from all
11947 threads that have anything of interest, and no longer
11948 need to keep this breakpoint location around. We
11949 don't hold locations forever so to reduce chances of
11950 mistaking a non-breakpoint SIGTRAP for a breakpoint
11951 SIGTRAP.
11952
11953 The heuristic failing can be disastrous on
11954 decr_pc_after_break targets.
11955
11956 On decr_pc_after_break targets, like e.g., x86-linux,
11957 if we fail to recognize a late breakpoint SIGTRAP,
11958 because events_till_retirement has reached 0 too
11959 soon, we'll fail to do the PC adjustment, and report
11960 a random SIGTRAP to the user. When the user resumes
11961 the inferior, it will most likely immediately crash
2dec564e 11962 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
11963 corrupted, because of being resumed e.g., in the
11964 middle of a multi-byte instruction, or skipped a
11965 one-byte instruction. This was actually seen happen
11966 on native x86-linux, and should be less rare on
11967 targets that do not support new thread events, like
11968 remote, due to the heuristic depending on
11969 thread_count.
11970
11971 Mistaking a random SIGTRAP for a breakpoint trap
11972 causes similar symptoms (PC adjustment applied when
11973 it shouldn't), but then again, playing with SIGTRAPs
11974 behind the debugger's back is asking for trouble.
11975
11976 Since hardware watchpoint traps are always
11977 distinguishable from other traps, so we don't need to
11978 apply keep hardware watchpoint moribund locations
11979 around. We simply always ignore hardware watchpoint
11980 traps we can no longer explain. */
11981
5b6d1e4f
PA
11982 process_stratum_target *proc_target = nullptr;
11983 for (inferior *inf : all_inferiors ())
11984 if (inf->pspace == old_loc->pspace)
11985 {
11986 proc_target = inf->process_target ();
11987 break;
11988 }
11989 if (proc_target != nullptr)
11990 old_loc->events_till_retirement
11991 = 3 * (thread_count (proc_target) + 1);
11992 else
11993 old_loc->events_till_retirement = 1;
876fa593 11994 old_loc->owner = NULL;
20874c92 11995
1123588c 11996 moribund_locations.push_back (old_loc);
1c5cfe86
PA
11997 }
11998 else
f431efe5
PA
11999 {
12000 old_loc->owner = NULL;
12001 decref_bp_location (&old_loc);
12002 }
20874c92 12003 }
74960c60 12004 }
1c5cfe86 12005
348d480f
PA
12006 /* Rescan breakpoints at the same address and section, marking the
12007 first one as "first" and any others as "duplicates". This is so
12008 that the bpt instruction is only inserted once. If we have a
12009 permanent breakpoint at the same place as BPT, make that one the
12010 official one, and the rest as duplicates. Permanent breakpoints
12011 are sorted first for the same address.
12012
12013 Do the same for hardware watchpoints, but also considering the
12014 watchpoint's type (regular/access/read) and length. */
12015
12016 bp_loc_first = NULL;
12017 wp_loc_first = NULL;
12018 awp_loc_first = NULL;
12019 rwp_loc_first = NULL;
40cb8ca5 12020
48d7020b 12021 for (bp_location *loc : all_bp_locations ())
348d480f
PA
12022 {
12023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12024 non-NULL. */
348d480f 12025 struct bp_location **loc_first_p;
43892fdf 12026 breakpoint *b = loc->owner;
348d480f 12027
6f380991 12028 if (!unduplicated_should_be_inserted (loc)
cb1e4e32 12029 || !bl_address_is_meaningful (loc)
1e4d1764
YQ
12030 /* Don't detect duplicate for tracepoint locations because they are
12031 never duplicated. See the comments in field `duplicate' of
12032 `struct bp_location'. */
348d480f 12033 || is_tracepoint (b))
b775012e
LM
12034 {
12035 /* Clear the condition modification flag. */
12036 loc->condition_changed = condition_unchanged;
12037 continue;
12038 }
348d480f 12039
348d480f
PA
12040 if (b->type == bp_hardware_watchpoint)
12041 loc_first_p = &wp_loc_first;
12042 else if (b->type == bp_read_watchpoint)
12043 loc_first_p = &rwp_loc_first;
12044 else if (b->type == bp_access_watchpoint)
12045 loc_first_p = &awp_loc_first;
12046 else
12047 loc_first_p = &bp_loc_first;
12048
12049 if (*loc_first_p == NULL
12050 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12051 || !breakpoint_locations_match (loc, *loc_first_p))
12052 {
12053 *loc_first_p = loc;
12054 loc->duplicate = 0;
b775012e
LM
12055
12056 if (is_breakpoint (loc->owner) && loc->condition_changed)
12057 {
12058 loc->needs_update = 1;
12059 /* Clear the condition modification flag. */
12060 loc->condition_changed = condition_unchanged;
12061 }
348d480f
PA
12062 continue;
12063 }
12064
934709f0
PW
12065
12066 /* This and the above ensure the invariant that the first location
12067 is not duplicated, and is the inserted one.
12068 All following are marked as duplicated, and are not inserted. */
12069 if (loc->inserted)
12070 swap_insertion (loc, *loc_first_p);
348d480f
PA
12071 loc->duplicate = 1;
12072
b775012e
LM
12073 /* Clear the condition modification flag. */
12074 loc->condition_changed = condition_unchanged;
348d480f
PA
12075 }
12076
a25a5a45 12077 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12078 {
04086b45 12079 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12080 insert_breakpoint_locations ();
12081 else
12082 {
44702360
PA
12083 /* Even though the caller told us to not insert new
12084 locations, we may still need to update conditions on the
12085 target's side of breakpoints that were already inserted
12086 if the target is evaluating breakpoint conditions. We
b775012e
LM
12087 only update conditions for locations that are marked
12088 "needs_update". */
12089 update_inserted_breakpoint_locations ();
12090 }
12091 }
348d480f 12092
04086b45 12093 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12094 download_tracepoint_locations ();
348d480f
PA
12095}
12096
12097void
12098breakpoint_retire_moribund (void)
12099{
1123588c
TT
12100 for (int ix = 0; ix < moribund_locations.size (); ++ix)
12101 {
12102 struct bp_location *loc = moribund_locations[ix];
12103 if (--(loc->events_till_retirement) == 0)
12104 {
12105 decref_bp_location (&loc);
12106 unordered_remove (moribund_locations, ix);
12107 --ix;
12108 }
12109 }
348d480f
PA
12110}
12111
12112static void
44702360 12113update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12114{
348d480f 12115
a70b8144 12116 try
492d29ea
PA
12117 {
12118 update_global_location_list (insert_mode);
12119 }
230d2906 12120 catch (const gdb_exception_error &e)
492d29ea
PA
12121 {
12122 }
348d480f
PA
12123}
12124
12125/* Clear BKP from a BPS. */
12126
12127static void
12128bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12129{
12130 bpstat bs;
12131
12132 for (bs = bps; bs; bs = bs->next)
12133 if (bs->breakpoint_at == bpt)
12134 {
12135 bs->breakpoint_at = NULL;
12136 bs->old_val = NULL;
12137 /* bs->commands will be freed later. */
12138 }
12139}
12140
12141/* Callback for iterate_over_threads. */
12142static int
12143bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12144{
9a3c8263 12145 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12146
12147 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12148 return 0;
12149}
12150
12151/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12152 callbacks. */
12153
12154static void
12155say_where (struct breakpoint *b)
12156{
12157 struct value_print_options opts;
12158
12159 get_user_print_options (&opts);
12160
12161 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12162 single string. */
12163 if (b->loc == NULL)
12164 {
f00aae0f
KS
12165 /* For pending locations, the output differs slightly based
12166 on b->extra_string. If this is non-NULL, it contains either
12167 a condition or dprintf arguments. */
12168 if (b->extra_string == NULL)
12169 {
12170 printf_filtered (_(" (%s) pending."),
d28cd78a 12171 event_location_to_string (b->location.get ()));
f00aae0f
KS
12172 }
12173 else if (b->type == bp_dprintf)
12174 {
12175 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12176 event_location_to_string (b->location.get ()),
f00aae0f
KS
12177 b->extra_string);
12178 }
12179 else
12180 {
12181 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12182 event_location_to_string (b->location.get ()),
f00aae0f
KS
12183 b->extra_string);
12184 }
348d480f
PA
12185 }
12186 else
12187 {
2f202fde 12188 if (opts.addressprint || b->loc->symtab == NULL)
6a831f06
PA
12189 printf_filtered (" at %ps",
12190 styled_string (address_style.style (),
12191 paddress (b->loc->gdbarch,
12192 b->loc->address)));
2f202fde 12193 if (b->loc->symtab != NULL)
f8eba3c6
TT
12194 {
12195 /* If there is a single location, we can print the location
12196 more nicely. */
12197 if (b->loc->next == NULL)
0bb296cb 12198 {
6a831f06
PA
12199 const char *filename
12200 = symtab_to_filename_for_display (b->loc->symtab);
12201 printf_filtered (": file %ps, line %d.",
12202 styled_string (file_name_style.style (),
12203 filename),
0bb296cb
TT
12204 b->loc->line_number);
12205 }
f8eba3c6
TT
12206 else
12207 /* This is not ideal, but each location may have a
12208 different file name, and this at least reflects the
12209 real situation somewhat. */
f00aae0f 12210 printf_filtered (": %s.",
d28cd78a 12211 event_location_to_string (b->location.get ()));
f8eba3c6 12212 }
348d480f
PA
12213
12214 if (b->loc->next)
12215 {
12216 struct bp_location *loc = b->loc;
12217 int n = 0;
12218 for (; loc; loc = loc->next)
12219 ++n;
12220 printf_filtered (" (%d locations)", n);
12221 }
12222 }
12223}
12224
5f486660 12225bp_location::~bp_location ()
348d480f 12226{
5f486660 12227 xfree (function_name);
348d480f
PA
12228}
12229
c1fc2657 12230/* Destructor for the breakpoint base class. */
348d480f 12231
c1fc2657 12232breakpoint::~breakpoint ()
348d480f 12233{
c1fc2657
SM
12234 xfree (this->cond_string);
12235 xfree (this->extra_string);
348d480f
PA
12236}
12237
40cb8ca5
SM
12238/* See breakpoint.h. */
12239
12240bp_locations_range breakpoint::locations ()
12241{
12242 return bp_locations_range (this->loc);
12243}
12244
2060206e
PA
12245static struct bp_location *
12246base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12247{
5f486660 12248 return new bp_location (self);
348d480f
PA
12249}
12250
2060206e
PA
12251static void
12252base_breakpoint_re_set (struct breakpoint *b)
12253{
12254 /* Nothing to re-set. */
12255}
12256
12257#define internal_error_pure_virtual_called() \
12258 gdb_assert_not_reached ("pure virtual function called")
12259
12260static int
12261base_breakpoint_insert_location (struct bp_location *bl)
12262{
12263 internal_error_pure_virtual_called ();
12264}
12265
12266static int
73971819
PA
12267base_breakpoint_remove_location (struct bp_location *bl,
12268 enum remove_bp_reason reason)
2060206e
PA
12269{
12270 internal_error_pure_virtual_called ();
12271}
12272
12273static int
12274base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12275 const address_space *aspace,
09ac7c10
TT
12276 CORE_ADDR bp_addr,
12277 const struct target_waitstatus *ws)
2060206e
PA
12278{
12279 internal_error_pure_virtual_called ();
12280}
12281
12282static void
12283base_breakpoint_check_status (bpstat bs)
12284{
12285 /* Always stop. */
12286}
12287
12288/* A "works_in_software_mode" breakpoint_ops method that just internal
12289 errors. */
12290
12291static int
12292base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12293{
12294 internal_error_pure_virtual_called ();
12295}
12296
12297/* A "resources_needed" breakpoint_ops method that just internal
12298 errors. */
12299
12300static int
12301base_breakpoint_resources_needed (const struct bp_location *bl)
12302{
12303 internal_error_pure_virtual_called ();
12304}
12305
12306static enum print_stop_action
12307base_breakpoint_print_it (bpstat bs)
12308{
12309 internal_error_pure_virtual_called ();
12310}
12311
12312static void
12313base_breakpoint_print_one_detail (const struct breakpoint *self,
12314 struct ui_out *uiout)
12315{
12316 /* nothing */
12317}
12318
12319static void
12320base_breakpoint_print_mention (struct breakpoint *b)
12321{
12322 internal_error_pure_virtual_called ();
12323}
12324
12325static void
12326base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12327{
12328 internal_error_pure_virtual_called ();
12329}
12330
983af33b 12331static void
f00aae0f 12332base_breakpoint_create_sals_from_location
626d2320 12333 (struct event_location *location,
f00aae0f
KS
12334 struct linespec_result *canonical,
12335 enum bptype type_wanted)
983af33b
SDJ
12336{
12337 internal_error_pure_virtual_called ();
12338}
12339
12340static void
12341base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12342 struct linespec_result *c,
e1e01040
PA
12343 gdb::unique_xmalloc_ptr<char> cond_string,
12344 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12345 enum bptype type_wanted,
12346 enum bpdisp disposition,
12347 int thread,
12348 int task, int ignore_count,
12349 const struct breakpoint_ops *o,
12350 int from_tty, int enabled,
44f238bb 12351 int internal, unsigned flags)
983af33b
SDJ
12352{
12353 internal_error_pure_virtual_called ();
12354}
12355
6c5b2ebe 12356static std::vector<symtab_and_line>
f00aae0f 12357base_breakpoint_decode_location (struct breakpoint *b,
626d2320 12358 struct event_location *location,
6c5b2ebe 12359 struct program_space *search_pspace)
983af33b
SDJ
12360{
12361 internal_error_pure_virtual_called ();
12362}
12363
ab04a2af
TT
12364/* The default 'explains_signal' method. */
12365
47591c29 12366static int
427cd150 12367base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12368{
47591c29 12369 return 1;
ab04a2af
TT
12370}
12371
9d6e6e84
HZ
12372/* The default "after_condition_true" method. */
12373
12374static void
12375base_breakpoint_after_condition_true (struct bpstats *bs)
12376{
12377 /* Nothing to do. */
12378}
12379
ab04a2af 12380struct breakpoint_ops base_breakpoint_ops =
2060206e 12381{
2060206e
PA
12382 base_breakpoint_allocate_location,
12383 base_breakpoint_re_set,
12384 base_breakpoint_insert_location,
12385 base_breakpoint_remove_location,
12386 base_breakpoint_breakpoint_hit,
12387 base_breakpoint_check_status,
12388 base_breakpoint_resources_needed,
12389 base_breakpoint_works_in_software_mode,
12390 base_breakpoint_print_it,
12391 NULL,
12392 base_breakpoint_print_one_detail,
12393 base_breakpoint_print_mention,
983af33b 12394 base_breakpoint_print_recreate,
5f700d83 12395 base_breakpoint_create_sals_from_location,
983af33b 12396 base_breakpoint_create_breakpoints_sal,
5f700d83 12397 base_breakpoint_decode_location,
9d6e6e84
HZ
12398 base_breakpoint_explains_signal,
12399 base_breakpoint_after_condition_true,
2060206e
PA
12400};
12401
12402/* Default breakpoint_ops methods. */
12403
12404static void
348d480f
PA
12405bkpt_re_set (struct breakpoint *b)
12406{
06edf0c0 12407 /* FIXME: is this still reachable? */
9ef9e6a6 12408 if (breakpoint_event_location_empty_p (b))
06edf0c0 12409 {
f00aae0f 12410 /* Anything without a location can't be re-set. */
348d480f 12411 delete_breakpoint (b);
06edf0c0 12412 return;
348d480f 12413 }
06edf0c0
PA
12414
12415 breakpoint_re_set_default (b);
348d480f
PA
12416}
12417
2060206e 12418static int
348d480f
PA
12419bkpt_insert_location (struct bp_location *bl)
12420{
cd6c3b4f
YQ
12421 CORE_ADDR addr = bl->target_info.reqstd_address;
12422
579c6ad9 12423 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12424 bl->target_info.placed_address = addr;
12425
348d480f 12426 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12427 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12428 else
7c16b83e 12429 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12430}
12431
2060206e 12432static int
73971819 12433bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12434{
12435 if (bl->loc_type == bp_loc_hardware_breakpoint)
12436 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12437 else
73971819 12438 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12439}
12440
2060206e 12441static int
348d480f 12442bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12443 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12444 const struct target_waitstatus *ws)
348d480f 12445{
09ac7c10 12446 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12447 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12448 return 0;
12449
348d480f
PA
12450 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12451 aspace, bp_addr))
12452 return 0;
12453
12454 if (overlay_debugging /* unmapped overlay section */
12455 && section_is_overlay (bl->section)
12456 && !section_is_mapped (bl->section))
12457 return 0;
12458
12459 return 1;
12460}
12461
cd1608cc
PA
12462static int
12463dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12464 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12465 const struct target_waitstatus *ws)
12466{
12467 if (dprintf_style == dprintf_style_agent
12468 && target_can_run_breakpoint_commands ())
12469 {
12470 /* An agent-style dprintf never causes a stop. If we see a trap
12471 for this address it must be for a breakpoint that happens to
12472 be set at the same address. */
12473 return 0;
12474 }
12475
12476 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12477}
12478
2060206e 12479static int
348d480f
PA
12480bkpt_resources_needed (const struct bp_location *bl)
12481{
12482 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12483
12484 return 1;
12485}
12486
2060206e 12487static enum print_stop_action
348d480f
PA
12488bkpt_print_it (bpstat bs)
12489{
348d480f
PA
12490 struct breakpoint *b;
12491 const struct bp_location *bl;
001c8c33 12492 int bp_temp;
79a45e25 12493 struct ui_out *uiout = current_uiout;
348d480f
PA
12494
12495 gdb_assert (bs->bp_location_at != NULL);
12496
b6433ede 12497 bl = bs->bp_location_at.get ();
348d480f
PA
12498 b = bs->breakpoint_at;
12499
001c8c33
PA
12500 bp_temp = b->disposition == disp_del;
12501 if (bl->address != bl->requested_address)
12502 breakpoint_adjustment_warning (bl->requested_address,
12503 bl->address,
12504 b->number, 1);
12505 annotate_breakpoint (b->number);
f303dbd6
PA
12506 maybe_print_thread_hit_breakpoint (uiout);
12507
112e8700 12508 if (uiout->is_mi_like_p ())
348d480f 12509 {
112e8700 12510 uiout->field_string ("reason",
001c8c33 12511 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12512 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12513 }
6a831f06
PA
12514 if (bp_temp)
12515 uiout->message ("Temporary breakpoint %pF, ",
12516 signed_field ("bkptno", b->number));
12517 else
12518 uiout->message ("Breakpoint %pF, ",
12519 signed_field ("bkptno", b->number));
06edf0c0 12520
001c8c33 12521 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12522}
12523
2060206e 12524static void
06edf0c0
PA
12525bkpt_print_mention (struct breakpoint *b)
12526{
112e8700 12527 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12528 return;
12529
12530 switch (b->type)
12531 {
12532 case bp_breakpoint:
12533 case bp_gnu_ifunc_resolver:
12534 if (b->disposition == disp_del)
12535 printf_filtered (_("Temporary breakpoint"));
12536 else
12537 printf_filtered (_("Breakpoint"));
12538 printf_filtered (_(" %d"), b->number);
12539 if (b->type == bp_gnu_ifunc_resolver)
12540 printf_filtered (_(" at gnu-indirect-function resolver"));
12541 break;
12542 case bp_hardware_breakpoint:
12543 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12544 break;
e7e0cddf
SS
12545 case bp_dprintf:
12546 printf_filtered (_("Dprintf %d"), b->number);
12547 break;
06edf0c0
PA
12548 }
12549
12550 say_where (b);
12551}
12552
2060206e 12553static void
06edf0c0
PA
12554bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12555{
12556 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12557 fprintf_unfiltered (fp, "tbreak");
12558 else if (tp->type == bp_breakpoint)
12559 fprintf_unfiltered (fp, "break");
12560 else if (tp->type == bp_hardware_breakpoint
12561 && tp->disposition == disp_del)
12562 fprintf_unfiltered (fp, "thbreak");
12563 else if (tp->type == bp_hardware_breakpoint)
12564 fprintf_unfiltered (fp, "hbreak");
12565 else
12566 internal_error (__FILE__, __LINE__,
12567 _("unhandled breakpoint type %d"), (int) tp->type);
12568
f00aae0f 12569 fprintf_unfiltered (fp, " %s",
d28cd78a 12570 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12571
12572 /* Print out extra_string if this breakpoint is pending. It might
12573 contain, for example, conditions that were set by the user. */
12574 if (tp->loc == NULL && tp->extra_string != NULL)
12575 fprintf_unfiltered (fp, " %s", tp->extra_string);
12576
dd11a36c 12577 print_recreate_thread (tp, fp);
06edf0c0
PA
12578}
12579
983af33b 12580static void
626d2320 12581bkpt_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12582 struct linespec_result *canonical,
12583 enum bptype type_wanted)
983af33b 12584{
f00aae0f 12585 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12586}
12587
12588static void
12589bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12590 struct linespec_result *canonical,
e1e01040
PA
12591 gdb::unique_xmalloc_ptr<char> cond_string,
12592 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12593 enum bptype type_wanted,
12594 enum bpdisp disposition,
12595 int thread,
12596 int task, int ignore_count,
12597 const struct breakpoint_ops *ops,
12598 int from_tty, int enabled,
44f238bb 12599 int internal, unsigned flags)
983af33b 12600{
023fa29b 12601 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12602 std::move (cond_string),
12603 std::move (extra_string),
e7e0cddf 12604 type_wanted,
983af33b
SDJ
12605 disposition, thread, task,
12606 ignore_count, ops, from_tty,
44f238bb 12607 enabled, internal, flags);
983af33b
SDJ
12608}
12609
6c5b2ebe 12610static std::vector<symtab_and_line>
f00aae0f 12611bkpt_decode_location (struct breakpoint *b,
626d2320 12612 struct event_location *location,
6c5b2ebe 12613 struct program_space *search_pspace)
983af33b 12614{
6c5b2ebe 12615 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12616}
12617
06edf0c0
PA
12618/* Virtual table for internal breakpoints. */
12619
12620static void
12621internal_bkpt_re_set (struct breakpoint *b)
12622{
12623 switch (b->type)
12624 {
12625 /* Delete overlay event and longjmp master breakpoints; they
12626 will be reset later by breakpoint_re_set. */
12627 case bp_overlay_event:
12628 case bp_longjmp_master:
12629 case bp_std_terminate_master:
12630 case bp_exception_master:
12631 delete_breakpoint (b);
12632 break;
12633
12634 /* This breakpoint is special, it's set up when the inferior
dda83cd7 12635 starts and we really don't want to touch it. */
06edf0c0
PA
12636 case bp_shlib_event:
12637
12638 /* Like bp_shlib_event, this breakpoint type is special. Once
12639 it is set up, we do not want to touch it. */
12640 case bp_thread_event:
12641 break;
12642 }
12643}
12644
12645static void
12646internal_bkpt_check_status (bpstat bs)
12647{
a9b3a50f
PA
12648 if (bs->breakpoint_at->type == bp_shlib_event)
12649 {
12650 /* If requested, stop when the dynamic linker notifies GDB of
12651 events. This allows the user to get control and place
12652 breakpoints in initializer routines for dynamically loaded
12653 objects (among other things). */
12654 bs->stop = stop_on_solib_events;
12655 bs->print = stop_on_solib_events;
12656 }
12657 else
12658 bs->stop = 0;
06edf0c0
PA
12659}
12660
12661static enum print_stop_action
12662internal_bkpt_print_it (bpstat bs)
12663{
06edf0c0 12664 struct breakpoint *b;
06edf0c0 12665
06edf0c0
PA
12666 b = bs->breakpoint_at;
12667
06edf0c0
PA
12668 switch (b->type)
12669 {
348d480f
PA
12670 case bp_shlib_event:
12671 /* Did we stop because the user set the stop_on_solib_events
12672 variable? (If so, we report this as a generic, "Stopped due
12673 to shlib event" message.) */
edcc5120 12674 print_solib_event (0);
348d480f
PA
12675 break;
12676
12677 case bp_thread_event:
12678 /* Not sure how we will get here.
12679 GDB should not stop for these breakpoints. */
12680 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12681 break;
12682
12683 case bp_overlay_event:
12684 /* By analogy with the thread event, GDB should not stop for these. */
12685 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12686 break;
12687
12688 case bp_longjmp_master:
12689 /* These should never be enabled. */
12690 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12691 break;
12692
12693 case bp_std_terminate_master:
12694 /* These should never be enabled. */
12695 printf_filtered (_("std::terminate Master Breakpoint: "
12696 "gdb should not stop!\n"));
348d480f
PA
12697 break;
12698
12699 case bp_exception_master:
12700 /* These should never be enabled. */
12701 printf_filtered (_("Exception Master Breakpoint: "
12702 "gdb should not stop!\n"));
06edf0c0
PA
12703 break;
12704 }
12705
001c8c33 12706 return PRINT_NOTHING;
06edf0c0
PA
12707}
12708
12709static void
12710internal_bkpt_print_mention (struct breakpoint *b)
12711{
12712 /* Nothing to mention. These breakpoints are internal. */
12713}
12714
06edf0c0
PA
12715/* Virtual table for momentary breakpoints */
12716
12717static void
12718momentary_bkpt_re_set (struct breakpoint *b)
12719{
12720 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12721 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12722 Otherwise these should have been blown away via the cleanup chain
12723 or by breakpoint_init_inferior when we rerun the executable. */
12724}
12725
12726static void
12727momentary_bkpt_check_status (bpstat bs)
12728{
12729 /* Nothing. The point of these breakpoints is causing a stop. */
12730}
12731
12732static enum print_stop_action
12733momentary_bkpt_print_it (bpstat bs)
12734{
001c8c33 12735 return PRINT_UNKNOWN;
348d480f
PA
12736}
12737
06edf0c0
PA
12738static void
12739momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12740{
06edf0c0 12741 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12742}
12743
e2e4d78b
JK
12744/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12745
12746 It gets cleared already on the removal of the first one of such placed
12747 breakpoints. This is OK as they get all removed altogether. */
12748
c1fc2657 12749longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12750{
c1fc2657 12751 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12752
c1fc2657 12753 if (tp != NULL)
e2e4d78b 12754 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12755}
12756
55aa24fb
SDJ
12757/* Specific methods for probe breakpoints. */
12758
12759static int
12760bkpt_probe_insert_location (struct bp_location *bl)
12761{
12762 int v = bkpt_insert_location (bl);
12763
12764 if (v == 0)
12765 {
12766 /* The insertion was successful, now let's set the probe's semaphore
12767 if needed. */
935676c9 12768 bl->probe.prob->set_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb
SDJ
12769 }
12770
12771 return v;
12772}
12773
12774static int
73971819
PA
12775bkpt_probe_remove_location (struct bp_location *bl,
12776 enum remove_bp_reason reason)
55aa24fb
SDJ
12777{
12778 /* Let's clear the semaphore before removing the location. */
935676c9 12779 bl->probe.prob->clear_semaphore (bl->probe.objfile, bl->gdbarch);
55aa24fb 12780
73971819 12781 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12782}
12783
12784static void
626d2320 12785bkpt_probe_create_sals_from_location (struct event_location *location,
5f700d83 12786 struct linespec_result *canonical,
f00aae0f 12787 enum bptype type_wanted)
55aa24fb
SDJ
12788{
12789 struct linespec_sals lsal;
12790
c2f4122d 12791 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12792 lsal.canonical
12793 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12794 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12795}
12796
6c5b2ebe 12797static std::vector<symtab_and_line>
f00aae0f 12798bkpt_probe_decode_location (struct breakpoint *b,
626d2320 12799 struct event_location *location,
6c5b2ebe 12800 struct program_space *search_pspace)
55aa24fb 12801{
6c5b2ebe
PA
12802 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12803 if (sals.empty ())
55aa24fb 12804 error (_("probe not found"));
6c5b2ebe 12805 return sals;
55aa24fb
SDJ
12806}
12807
348d480f 12808/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12809
348d480f
PA
12810static void
12811tracepoint_re_set (struct breakpoint *b)
12812{
12813 breakpoint_re_set_default (b);
12814}
876fa593 12815
348d480f
PA
12816static int
12817tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12818 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12819 const struct target_waitstatus *ws)
348d480f
PA
12820{
12821 /* By definition, the inferior does not report stops at
12822 tracepoints. */
12823 return 0;
74960c60
VP
12824}
12825
12826static void
348d480f
PA
12827tracepoint_print_one_detail (const struct breakpoint *self,
12828 struct ui_out *uiout)
74960c60 12829{
d9b3f62e 12830 struct tracepoint *tp = (struct tracepoint *) self;
5d9310c4 12831 if (!tp->static_trace_marker_id.empty ())
348d480f
PA
12832 {
12833 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12834
6a831f06
PA
12835 uiout->message ("\tmarker id is %pF\n",
12836 string_field ("static-tracepoint-marker-string-id",
12837 tp->static_trace_marker_id.c_str ()));
348d480f 12838 }
0d381245
VP
12839}
12840
a474d7c2 12841static void
348d480f 12842tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12843{
112e8700 12844 if (current_uiout->is_mi_like_p ())
348d480f 12845 return;
cc59ec59 12846
348d480f
PA
12847 switch (b->type)
12848 {
12849 case bp_tracepoint:
12850 printf_filtered (_("Tracepoint"));
12851 printf_filtered (_(" %d"), b->number);
12852 break;
12853 case bp_fast_tracepoint:
12854 printf_filtered (_("Fast tracepoint"));
12855 printf_filtered (_(" %d"), b->number);
12856 break;
12857 case bp_static_tracepoint:
12858 printf_filtered (_("Static tracepoint"));
12859 printf_filtered (_(" %d"), b->number);
12860 break;
12861 default:
12862 internal_error (__FILE__, __LINE__,
12863 _("unhandled tracepoint type %d"), (int) b->type);
12864 }
12865
12866 say_where (b);
a474d7c2
PA
12867}
12868
348d480f 12869static void
d9b3f62e 12870tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12871{
d9b3f62e
PA
12872 struct tracepoint *tp = (struct tracepoint *) self;
12873
12874 if (self->type == bp_fast_tracepoint)
348d480f 12875 fprintf_unfiltered (fp, "ftrace");
c93e8391 12876 else if (self->type == bp_static_tracepoint)
348d480f 12877 fprintf_unfiltered (fp, "strace");
d9b3f62e 12878 else if (self->type == bp_tracepoint)
348d480f
PA
12879 fprintf_unfiltered (fp, "trace");
12880 else
12881 internal_error (__FILE__, __LINE__,
d9b3f62e 12882 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 12883
f00aae0f 12884 fprintf_unfiltered (fp, " %s",
d28cd78a 12885 event_location_to_string (self->location.get ()));
d9b3f62e
PA
12886 print_recreate_thread (self, fp);
12887
12888 if (tp->pass_count)
12889 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
12890}
12891
983af33b 12892static void
626d2320 12893tracepoint_create_sals_from_location (struct event_location *location,
f00aae0f
KS
12894 struct linespec_result *canonical,
12895 enum bptype type_wanted)
983af33b 12896{
f00aae0f 12897 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12898}
12899
12900static void
12901tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12902 struct linespec_result *canonical,
e1e01040
PA
12903 gdb::unique_xmalloc_ptr<char> cond_string,
12904 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12905 enum bptype type_wanted,
12906 enum bpdisp disposition,
12907 int thread,
12908 int task, int ignore_count,
12909 const struct breakpoint_ops *ops,
12910 int from_tty, int enabled,
44f238bb 12911 int internal, unsigned flags)
983af33b 12912{
023fa29b 12913 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12914 std::move (cond_string),
12915 std::move (extra_string),
e7e0cddf 12916 type_wanted,
983af33b
SDJ
12917 disposition, thread, task,
12918 ignore_count, ops, from_tty,
44f238bb 12919 enabled, internal, flags);
983af33b
SDJ
12920}
12921
6c5b2ebe 12922static std::vector<symtab_and_line>
f00aae0f 12923tracepoint_decode_location (struct breakpoint *b,
626d2320 12924 struct event_location *location,
6c5b2ebe 12925 struct program_space *search_pspace)
983af33b 12926{
6c5b2ebe 12927 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12928}
12929
2060206e 12930struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 12931
bac7c5cf 12932/* Virtual table for tracepoints on static probes. */
55aa24fb
SDJ
12933
12934static void
f00aae0f 12935tracepoint_probe_create_sals_from_location
626d2320 12936 (struct event_location *location,
f00aae0f
KS
12937 struct linespec_result *canonical,
12938 enum bptype type_wanted)
55aa24fb
SDJ
12939{
12940 /* We use the same method for breakpoint on probes. */
f00aae0f 12941 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
12942}
12943
6c5b2ebe 12944static std::vector<symtab_and_line>
f00aae0f 12945tracepoint_probe_decode_location (struct breakpoint *b,
626d2320 12946 struct event_location *location,
6c5b2ebe 12947 struct program_space *search_pspace)
55aa24fb
SDJ
12948{
12949 /* We use the same method for breakpoint on probes. */
6c5b2ebe 12950 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
12951}
12952
5c2b4418
HZ
12953/* Dprintf breakpoint_ops methods. */
12954
12955static void
12956dprintf_re_set (struct breakpoint *b)
12957{
12958 breakpoint_re_set_default (b);
12959
f00aae0f
KS
12960 /* extra_string should never be non-NULL for dprintf. */
12961 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
12962
12963 /* 1 - connect to target 1, that can run breakpoint commands.
12964 2 - create a dprintf, which resolves fine.
12965 3 - disconnect from target 1
12966 4 - connect to target 2, that can NOT run breakpoint commands.
12967
12968 After steps #3/#4, you'll want the dprintf command list to
12969 be updated, because target 1 and 2 may well return different
12970 answers for target_can_run_breakpoint_commands().
12971 Given absence of finer grained resetting, we get to do
12972 it all the time. */
12973 if (b->extra_string != NULL)
12974 update_dprintf_command_list (b);
12975}
12976
2d9442cc
HZ
12977/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
12978
12979static void
12980dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12981{
f00aae0f 12982 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 12983 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
12984 tp->extra_string);
12985 print_recreate_thread (tp, fp);
12986}
12987
9d6e6e84
HZ
12988/* Implement the "after_condition_true" breakpoint_ops method for
12989 dprintf.
12990
12991 dprintf's are implemented with regular commands in their command
12992 list, but we run the commands here instead of before presenting the
12993 stop to the user, as dprintf's don't actually cause a stop. This
12994 also makes it so that the commands of multiple dprintfs at the same
12995 address are all handled. */
12996
12997static void
12998dprintf_after_condition_true (struct bpstats *bs)
12999{
04afa70c 13000 struct bpstats tmp_bs;
9d6e6e84
HZ
13001 struct bpstats *tmp_bs_p = &tmp_bs;
13002
13003 /* dprintf's never cause a stop. This wasn't set in the
13004 check_status hook instead because that would make the dprintf's
13005 condition not be evaluated. */
13006 bs->stop = 0;
13007
13008 /* Run the command list here. Take ownership of it instead of
13009 copying. We never want these commands to run later in
13010 bpstat_do_actions, if a breakpoint that causes a stop happens to
13011 be set at same address as this dprintf, or even if running the
13012 commands here throws. */
13013 tmp_bs.commands = bs->commands;
13014 bs->commands = NULL;
9d6e6e84
HZ
13015
13016 bpstat_do_actions_1 (&tmp_bs_p);
13017
13018 /* 'tmp_bs.commands' will usually be NULL by now, but
13019 bpstat_do_actions_1 may return early without processing the whole
13020 list. */
9d6e6e84
HZ
13021}
13022
983af33b
SDJ
13023/* The breakpoint_ops structure to be used on static tracepoints with
13024 markers (`-m'). */
13025
13026static void
626d2320 13027strace_marker_create_sals_from_location (struct event_location *location,
5f700d83 13028 struct linespec_result *canonical,
f00aae0f 13029 enum bptype type_wanted)
983af33b
SDJ
13030{
13031 struct linespec_sals lsal;
f00aae0f 13032 const char *arg_start, *arg;
983af33b 13033
a20714ff 13034 arg = arg_start = get_linespec_location (location)->spec_string;
f00aae0f 13035 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13036
f2fc3015
TT
13037 std::string str (arg_start, arg - arg_start);
13038 const char *ptr = str.c_str ();
a20714ff
PA
13039 canonical->location
13040 = new_linespec_location (&ptr, symbol_name_match_type::FULL);
983af33b 13041
8e9e35b1
TT
13042 lsal.canonical
13043 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13044 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13045}
13046
13047static void
13048strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13049 struct linespec_result *canonical,
e1e01040
PA
13050 gdb::unique_xmalloc_ptr<char> cond_string,
13051 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13052 enum bptype type_wanted,
13053 enum bpdisp disposition,
13054 int thread,
13055 int task, int ignore_count,
13056 const struct breakpoint_ops *ops,
13057 int from_tty, int enabled,
44f238bb 13058 int internal, unsigned flags)
983af33b 13059{
6c5b2ebe 13060 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13061
13062 /* If the user is creating a static tracepoint by marker id
13063 (strace -m MARKER_ID), then store the sals index, so that
13064 breakpoint_re_set can try to match up which of the newly
13065 found markers corresponds to this one, and, don't try to
13066 expand multiple locations for each sal, given than SALS
13067 already should contain all sals for MARKER_ID. */
13068
6c5b2ebe 13069 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13070 {
6c5b2ebe
PA
13071 event_location_up location
13072 = copy_event_location (canonical->location.get ());
983af33b 13073
b270e6f9 13074 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13075 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13076 std::move (location), NULL,
e1e01040
PA
13077 std::move (cond_string),
13078 std::move (extra_string),
e7e0cddf 13079 type_wanted, disposition,
983af33b 13080 thread, task, ignore_count, ops,
44f238bb 13081 from_tty, enabled, internal, flags,
983af33b
SDJ
13082 canonical->special_display);
13083 /* Given that its possible to have multiple markers with
13084 the same string id, if the user is creating a static
13085 tracepoint by marker id ("strace -m MARKER_ID"), then
13086 store the sals index, so that breakpoint_re_set can
13087 try to match up which of the newly found markers
13088 corresponds to this one */
13089 tp->static_trace_marker_id_idx = i;
13090
b270e6f9 13091 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13092 }
13093}
13094
6c5b2ebe 13095static std::vector<symtab_and_line>
f00aae0f 13096strace_marker_decode_location (struct breakpoint *b,
626d2320 13097 struct event_location *location,
6c5b2ebe 13098 struct program_space *search_pspace)
983af33b
SDJ
13099{
13100 struct tracepoint *tp = (struct tracepoint *) b;
a20714ff 13101 const char *s = get_linespec_location (location)->spec_string;
983af33b 13102
6c5b2ebe
PA
13103 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13104 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13105 {
6c5b2ebe
PA
13106 sals[0] = sals[tp->static_trace_marker_id_idx];
13107 sals.resize (1);
13108 return sals;
983af33b
SDJ
13109 }
13110 else
5d9310c4 13111 error (_("marker %s not found"), tp->static_trace_marker_id.c_str ());
983af33b
SDJ
13112}
13113
13114static struct breakpoint_ops strace_marker_breakpoint_ops;
13115
13116static int
13117strace_marker_p (struct breakpoint *b)
13118{
13119 return b->ops == &strace_marker_breakpoint_ops;
13120}
13121
53a5351d 13122/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13123 structures. */
c906108c
SS
13124
13125void
fba45db2 13126delete_breakpoint (struct breakpoint *bpt)
c906108c 13127{
8a3fe4f8 13128 gdb_assert (bpt != NULL);
c906108c 13129
4a64f543
MS
13130 /* Has this bp already been deleted? This can happen because
13131 multiple lists can hold pointers to bp's. bpstat lists are
13132 especial culprits.
13133
13134 One example of this happening is a watchpoint's scope bp. When
13135 the scope bp triggers, we notice that the watchpoint is out of
13136 scope, and delete it. We also delete its scope bp. But the
13137 scope bp is marked "auto-deleting", and is already on a bpstat.
13138 That bpstat is then checked for auto-deleting bp's, which are
13139 deleted.
13140
13141 A real solution to this problem might involve reference counts in
13142 bp's, and/or giving them pointers back to their referencing
13143 bpstat's, and teaching delete_breakpoint to only free a bp's
13144 storage when no more references were extent. A cheaper bandaid
13145 was chosen. */
c906108c
SS
13146 if (bpt->type == bp_none)
13147 return;
13148
4a64f543
MS
13149 /* At least avoid this stale reference until the reference counting
13150 of breakpoints gets resolved. */
d0fb5eae 13151 if (bpt->related_breakpoint != bpt)
e5a0a904 13152 {
d0fb5eae 13153 struct breakpoint *related;
3a5c3e22 13154 struct watchpoint *w;
d0fb5eae
JK
13155
13156 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13157 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13158 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13159 w = (struct watchpoint *) bpt;
13160 else
13161 w = NULL;
13162 if (w != NULL)
13163 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13164
13165 /* Unlink bpt from the bpt->related_breakpoint ring. */
13166 for (related = bpt; related->related_breakpoint != bpt;
13167 related = related->related_breakpoint);
13168 related->related_breakpoint = bpt->related_breakpoint;
13169 bpt->related_breakpoint = bpt;
e5a0a904
JK
13170 }
13171
a9634178
TJB
13172 /* watch_command_1 creates a watchpoint but only sets its number if
13173 update_watchpoint succeeds in creating its bp_locations. If there's
13174 a problem in that process, we'll be asked to delete the half-created
13175 watchpoint. In that case, don't announce the deletion. */
13176 if (bpt->number)
76727919 13177 gdb::observers::breakpoint_deleted.notify (bpt);
c906108c 13178
c906108c
SS
13179 if (breakpoint_chain == bpt)
13180 breakpoint_chain = bpt->next;
13181
43892fdf 13182 for (breakpoint *b : all_breakpoints ())
c906108c 13183 if (b->next == bpt)
c5aa993b
JM
13184 {
13185 b->next = bpt->next;
13186 break;
13187 }
c906108c 13188
f431efe5
PA
13189 /* Be sure no bpstat's are pointing at the breakpoint after it's
13190 been freed. */
13191 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13192 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13193 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13194 commands are associated with the bpstat; if we remove it here,
13195 then the later call to bpstat_do_actions (&stop_bpstat); in
13196 event-top.c won't do anything, and temporary breakpoints with
13197 commands won't work. */
13198
13199 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13200
4a64f543
MS
13201 /* Now that breakpoint is removed from breakpoint list, update the
13202 global location list. This will remove locations that used to
13203 belong to this breakpoint. Do this before freeing the breakpoint
13204 itself, since remove_breakpoint looks at location's owner. It
13205 might be better design to have location completely
13206 self-contained, but it's not the case now. */
44702360 13207 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13208
4a64f543
MS
13209 /* On the chance that someone will soon try again to delete this
13210 same bp, we mark it as deleted before freeing its storage. */
c906108c 13211 bpt->type = bp_none;
4d01a485 13212 delete bpt;
c906108c
SS
13213}
13214
51be5b68
PA
13215/* Iterator function to call a user-provided callback function once
13216 for each of B and its related breakpoints. */
13217
13218static void
13219iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13220 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13221{
13222 struct breakpoint *related;
13223
13224 related = b;
13225 do
13226 {
13227 struct breakpoint *next;
13228
13229 /* FUNCTION may delete RELATED. */
13230 next = related->related_breakpoint;
13231
13232 if (next == related)
13233 {
13234 /* RELATED is the last ring entry. */
48649e1b 13235 function (related);
51be5b68
PA
13236
13237 /* FUNCTION may have deleted it, so we'd never reach back to
13238 B. There's nothing left to do anyway, so just break
13239 out. */
13240 break;
13241 }
13242 else
48649e1b 13243 function (related);
51be5b68
PA
13244
13245 related = next;
13246 }
13247 while (related != b);
13248}
95a42b64 13249
4495129a 13250static void
981a3fb3 13251delete_command (const char *arg, int from_tty)
c906108c 13252{
ea9365bb
TT
13253 dont_repeat ();
13254
c906108c
SS
13255 if (arg == 0)
13256 {
13257 int breaks_to_delete = 0;
13258
46c6471b 13259 /* Delete all breakpoints if no argument. Do not delete
dda83cd7
SM
13260 internal breakpoints, these have to be deleted with an
13261 explicit breakpoint number argument. */
43892fdf 13262 for (breakpoint *b : all_breakpoints ())
46c6471b 13263 if (user_breakpoint_p (b))
973d738b
DJ
13264 {
13265 breaks_to_delete = 1;
13266 break;
13267 }
c906108c
SS
13268
13269 /* Ask user only if there are some breakpoints to delete. */
13270 if (!from_tty
e2e0b3e5 13271 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
1428b37a
SM
13272 for (breakpoint *b : all_breakpoints_safe ())
13273 if (user_breakpoint_p (b))
13274 delete_breakpoint (b);
c906108c
SS
13275 }
13276 else
48649e1b 13277 map_breakpoint_numbers
b926417a 13278 (arg, [&] (breakpoint *br)
48649e1b 13279 {
b926417a 13280 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 13281 });
c906108c
SS
13282}
13283
c2f4122d
PA
13284/* Return true if all locations of B bound to PSPACE are pending. If
13285 PSPACE is NULL, all locations of all program spaces are
13286 considered. */
13287
0d381245 13288static int
c2f4122d 13289all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13290{
40cb8ca5 13291 for (bp_location *loc : b->locations ())
c2f4122d
PA
13292 if ((pspace == NULL
13293 || loc->pspace == pspace)
13294 && !loc->shlib_disabled
8645ff69 13295 && !loc->pspace->executing_startup)
0d381245
VP
13296 return 0;
13297 return 1;
fe3f5fa8
VP
13298}
13299
776592bf
DE
13300/* Subroutine of update_breakpoint_locations to simplify it.
13301 Return non-zero if multiple fns in list LOC have the same name.
13302 Null names are ignored. */
13303
13304static int
13305ambiguous_names_p (struct bp_location *loc)
13306{
13307 struct bp_location *l;
2698f5ea 13308 htab_up htab (htab_create_alloc (13, htab_hash_string, htab_eq_string, NULL,
c1fb9836 13309 xcalloc, xfree));
776592bf
DE
13310
13311 for (l = loc; l != NULL; l = l->next)
13312 {
13313 const char **slot;
13314 const char *name = l->function_name;
13315
13316 /* Allow for some names to be NULL, ignore them. */
13317 if (name == NULL)
13318 continue;
13319
c1fb9836 13320 slot = (const char **) htab_find_slot (htab.get (), (const void *) name,
776592bf 13321 INSERT);
4a64f543
MS
13322 /* NOTE: We can assume slot != NULL here because xcalloc never
13323 returns NULL. */
776592bf 13324 if (*slot != NULL)
c1fb9836 13325 return 1;
776592bf
DE
13326 *slot = name;
13327 }
13328
776592bf
DE
13329 return 0;
13330}
13331
0fb4aa4b
PA
13332/* When symbols change, it probably means the sources changed as well,
13333 and it might mean the static tracepoint markers are no longer at
13334 the same address or line numbers they used to be at last we
13335 checked. Losing your static tracepoints whenever you rebuild is
13336 undesirable. This function tries to resync/rematch gdb static
13337 tracepoints with the markers on the target, for static tracepoints
13338 that have not been set by marker id. Static tracepoint that have
13339 been set by marker id are reset by marker id in breakpoint_re_set.
13340 The heuristic is:
13341
13342 1) For a tracepoint set at a specific address, look for a marker at
13343 the old PC. If one is found there, assume to be the same marker.
13344 If the name / string id of the marker found is different from the
13345 previous known name, assume that means the user renamed the marker
13346 in the sources, and output a warning.
13347
13348 2) For a tracepoint set at a given line number, look for a marker
13349 at the new address of the old line number. If one is found there,
13350 assume to be the same marker. If the name / string id of the
13351 marker found is different from the previous known name, assume that
13352 means the user renamed the marker in the sources, and output a
13353 warning.
13354
13355 3) If a marker is no longer found at the same address or line, it
13356 may mean the marker no longer exists. But it may also just mean
13357 the code changed a bit. Maybe the user added a few lines of code
13358 that made the marker move up or down (in line number terms). Ask
13359 the target for info about the marker with the string id as we knew
13360 it. If found, update line number and address in the matching
13361 static tracepoint. This will get confused if there's more than one
13362 marker with the same ID (possible in UST, although unadvised
13363 precisely because it confuses tools). */
13364
13365static struct symtab_and_line
13366update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13367{
d9b3f62e 13368 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13369 struct static_tracepoint_marker marker;
13370 CORE_ADDR pc;
0fb4aa4b
PA
13371
13372 pc = sal.pc;
13373 if (sal.line)
13374 find_line_pc (sal.symtab, sal.line, &pc);
13375
13376 if (target_static_tracepoint_marker_at (pc, &marker))
13377 {
5d9310c4 13378 if (tp->static_trace_marker_id != marker.str_id)
0fb4aa4b 13379 warning (_("static tracepoint %d changed probed marker from %s to %s"),
5d9310c4
SM
13380 b->number, tp->static_trace_marker_id.c_str (),
13381 marker.str_id.c_str ());
0fb4aa4b 13382
5d9310c4 13383 tp->static_trace_marker_id = std::move (marker.str_id);
0fb4aa4b
PA
13384
13385 return sal;
13386 }
13387
13388 /* Old marker wasn't found on target at lineno. Try looking it up
13389 by string ID. */
13390 if (!sal.explicit_pc
13391 && sal.line != 0
13392 && sal.symtab != NULL
5d9310c4 13393 && !tp->static_trace_marker_id.empty ())
0fb4aa4b 13394 {
5d9310c4
SM
13395 std::vector<static_tracepoint_marker> markers
13396 = target_static_tracepoint_markers_by_strid
13397 (tp->static_trace_marker_id.c_str ());
0fb4aa4b 13398
5d9310c4 13399 if (!markers.empty ())
0fb4aa4b 13400 {
0fb4aa4b 13401 struct symbol *sym;
80e1d417 13402 struct static_tracepoint_marker *tpmarker;
79a45e25 13403 struct ui_out *uiout = current_uiout;
67994074 13404 struct explicit_location explicit_loc;
0fb4aa4b 13405
5d9310c4 13406 tpmarker = &markers[0];
0fb4aa4b 13407
5d9310c4 13408 tp->static_trace_marker_id = std::move (tpmarker->str_id);
0fb4aa4b
PA
13409
13410 warning (_("marker for static tracepoint %d (%s) not "
13411 "found at previous line number"),
5d9310c4 13412 b->number, tp->static_trace_marker_id.c_str ());
0fb4aa4b 13413
51abb421 13414 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13415 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13416 uiout->text ("Now in ");
0fb4aa4b
PA
13417 if (sym)
13418 {
987012b8 13419 uiout->field_string ("func", sym->print_name (),
e43b10e1 13420 function_name_style.style ());
112e8700 13421 uiout->text (" at ");
0fb4aa4b 13422 }
112e8700 13423 uiout->field_string ("file",
cbe56571 13424 symtab_to_filename_for_display (sal2.symtab),
e43b10e1 13425 file_name_style.style ());
112e8700 13426 uiout->text (":");
0fb4aa4b 13427
112e8700 13428 if (uiout->is_mi_like_p ())
0fb4aa4b 13429 {
0b0865da 13430 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13431
112e8700 13432 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13433 }
13434
381befee 13435 uiout->field_signed ("line", sal2.line);
112e8700 13436 uiout->text ("\n");
0fb4aa4b 13437
80e1d417 13438 b->loc->line_number = sal2.line;
2f202fde 13439 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13440
d28cd78a 13441 b->location.reset (NULL);
67994074
KS
13442 initialize_explicit_location (&explicit_loc);
13443 explicit_loc.source_filename
00e52e53 13444 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13445 explicit_loc.line_offset.offset = b->loc->line_number;
13446 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13447 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13448
13449 /* Might be nice to check if function changed, and warn if
13450 so. */
0fb4aa4b
PA
13451 }
13452 }
13453 return sal;
13454}
13455
8d3788bd
VP
13456/* Returns 1 iff locations A and B are sufficiently same that
13457 we don't need to report breakpoint as changed. */
13458
13459static int
13460locations_are_equal (struct bp_location *a, struct bp_location *b)
13461{
13462 while (a && b)
13463 {
13464 if (a->address != b->address)
13465 return 0;
13466
13467 if (a->shlib_disabled != b->shlib_disabled)
13468 return 0;
13469
13470 if (a->enabled != b->enabled)
13471 return 0;
13472
b5fa468f
TBA
13473 if (a->disabled_by_cond != b->disabled_by_cond)
13474 return 0;
13475
8d3788bd
VP
13476 a = a->next;
13477 b = b->next;
13478 }
13479
13480 if ((a == NULL) != (b == NULL))
13481 return 0;
13482
13483 return 1;
13484}
13485
c2f4122d
PA
13486/* Split all locations of B that are bound to PSPACE out of B's
13487 location list to a separate list and return that list's head. If
13488 PSPACE is NULL, hoist out all locations of B. */
13489
13490static struct bp_location *
13491hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13492{
13493 struct bp_location head;
13494 struct bp_location *i = b->loc;
13495 struct bp_location **i_link = &b->loc;
13496 struct bp_location *hoisted = &head;
13497
13498 if (pspace == NULL)
13499 {
13500 i = b->loc;
13501 b->loc = NULL;
13502 return i;
13503 }
13504
13505 head.next = NULL;
13506
13507 while (i != NULL)
13508 {
13509 if (i->pspace == pspace)
13510 {
13511 *i_link = i->next;
13512 i->next = NULL;
13513 hoisted->next = i;
13514 hoisted = i;
13515 }
13516 else
13517 i_link = &i->next;
13518 i = *i_link;
13519 }
13520
13521 return head.next;
13522}
13523
13524/* Create new breakpoint locations for B (a hardware or software
13525 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13526 zero, then B is a ranged breakpoint. Only recreates locations for
13527 FILTER_PSPACE. Locations of other program spaces are left
13528 untouched. */
f1310107 13529
0e30163f 13530void
0d381245 13531update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13532 struct program_space *filter_pspace,
6c5b2ebe
PA
13533 gdb::array_view<const symtab_and_line> sals,
13534 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8 13535{
c2f4122d 13536 struct bp_location *existing_locations;
0d381245 13537
6c5b2ebe 13538 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13539 {
13540 /* Ranged breakpoints have only one start location and one end
13541 location. */
13542 b->enable_state = bp_disabled;
f8eba3c6
TT
13543 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13544 "multiple locations found\n"),
13545 b->number);
13546 return;
13547 }
f1310107 13548
4a64f543
MS
13549 /* If there's no new locations, and all existing locations are
13550 pending, don't do anything. This optimizes the common case where
13551 all locations are in the same shared library, that was unloaded.
13552 We'd like to retain the location, so that when the library is
13553 loaded again, we don't loose the enabled/disabled status of the
13554 individual locations. */
6c5b2ebe 13555 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13556 return;
13557
c2f4122d 13558 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13559
6c5b2ebe 13560 for (const auto &sal : sals)
fe3f5fa8 13561 {
f8eba3c6
TT
13562 struct bp_location *new_loc;
13563
6c5b2ebe 13564 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13565
6c5b2ebe 13566 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13567
0d381245
VP
13568 /* Reparse conditions, they might contain references to the
13569 old symtab. */
13570 if (b->cond_string != NULL)
13571 {
bbc13ae3 13572 const char *s;
fe3f5fa8 13573
0d381245 13574 s = b->cond_string;
a70b8144 13575 try
0d381245 13576 {
6c5b2ebe
PA
13577 new_loc->cond = parse_exp_1 (&s, sal.pc,
13578 block_for_pc (sal.pc),
0d381245
VP
13579 0);
13580 }
230d2906 13581 catch (const gdb_exception_error &e)
0d381245 13582 {
b5fa468f 13583 new_loc->disabled_by_cond = true;
0d381245
VP
13584 }
13585 }
fe3f5fa8 13586
6c5b2ebe 13587 if (!sals_end.empty ())
f1310107 13588 {
6c5b2ebe 13589 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13590
6c5b2ebe 13591 new_loc->length = end - sals[0].pc + 1;
f1310107 13592 }
0d381245 13593 }
fe3f5fa8 13594
4a64f543
MS
13595 /* If possible, carry over 'disable' status from existing
13596 breakpoints. */
0d381245
VP
13597 {
13598 struct bp_location *e = existing_locations;
776592bf
DE
13599 /* If there are multiple breakpoints with the same function name,
13600 e.g. for inline functions, comparing function names won't work.
13601 Instead compare pc addresses; this is just a heuristic as things
13602 may have moved, but in practice it gives the correct answer
13603 often enough until a better solution is found. */
13604 int have_ambiguous_names = ambiguous_names_p (b->loc);
13605
0d381245
VP
13606 for (; e; e = e->next)
13607 {
b5fa468f 13608 if ((!e->enabled || e->disabled_by_cond) && e->function_name)
0d381245 13609 {
776592bf
DE
13610 if (have_ambiguous_names)
13611 {
40cb8ca5 13612 for (bp_location *l : b->locations ())
7f32a4d5
PA
13613 {
13614 /* Ignore software vs hardware location type at
13615 this point, because with "set breakpoint
13616 auto-hw", after a re-set, locations that were
13617 hardware can end up as software, or vice versa.
13618 As mentioned above, this is an heuristic and in
13619 practice should give the correct answer often
13620 enough. */
13621 if (breakpoint_locations_match (e, l, true))
13622 {
b5fa468f
TBA
13623 l->enabled = e->enabled;
13624 l->disabled_by_cond = e->disabled_by_cond;
7f32a4d5
PA
13625 break;
13626 }
13627 }
776592bf
DE
13628 }
13629 else
13630 {
40cb8ca5 13631 for (bp_location *l : b->locations ())
776592bf
DE
13632 if (l->function_name
13633 && strcmp (e->function_name, l->function_name) == 0)
13634 {
b5fa468f
TBA
13635 l->enabled = e->enabled;
13636 l->disabled_by_cond = e->disabled_by_cond;
776592bf
DE
13637 break;
13638 }
13639 }
0d381245
VP
13640 }
13641 }
13642 }
fe3f5fa8 13643
8d3788bd 13644 if (!locations_are_equal (existing_locations, b->loc))
76727919 13645 gdb::observers::breakpoint_modified.notify (b);
fe3f5fa8
VP
13646}
13647
f00aae0f 13648/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13649 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13650
6c5b2ebe 13651static std::vector<symtab_and_line>
f00aae0f 13652location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13653 struct program_space *search_pspace, int *found)
ef23e705 13654{
cc06b668 13655 struct gdb_exception exception;
ef23e705 13656
983af33b 13657 gdb_assert (b->ops != NULL);
ef23e705 13658
6c5b2ebe
PA
13659 std::vector<symtab_and_line> sals;
13660
a70b8144 13661 try
ef23e705 13662 {
6c5b2ebe 13663 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13664 }
94aeb44b 13665 catch (gdb_exception_error &e)
ef23e705
TJB
13666 {
13667 int not_found_and_ok = 0;
492d29ea 13668
ef23e705
TJB
13669 /* For pending breakpoints, it's expected that parsing will
13670 fail until the right shared library is loaded. User has
13671 already told to create pending breakpoints and don't need
13672 extra messages. If breakpoint is in bp_shlib_disabled
13673 state, then user already saw the message about that
13674 breakpoint being disabled, and don't want to see more
13675 errors. */
58438ac1 13676 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13677 && (b->condition_not_parsed
13678 || (b->loc != NULL
13679 && search_pspace != NULL
13680 && b->loc->pspace != search_pspace)
ef23e705 13681 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13682 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13683 || b->enable_state == bp_disabled))
13684 not_found_and_ok = 1;
13685
13686 if (!not_found_and_ok)
13687 {
13688 /* We surely don't want to warn about the same breakpoint
13689 10 times. One solution, implemented here, is disable
13690 the breakpoint on error. Another solution would be to
13691 have separate 'warning emitted' flag. Since this
13692 happens only when a binary has changed, I don't know
13693 which approach is better. */
13694 b->enable_state = bp_disabled;
eedc3f4f 13695 throw;
ef23e705 13696 }
94aeb44b
TT
13697
13698 exception = std::move (e);
ef23e705
TJB
13699 }
13700
492d29ea 13701 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13702 {
6c5b2ebe
PA
13703 for (auto &sal : sals)
13704 resolve_sal_pc (&sal);
f00aae0f 13705 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13706 {
ed1d1739
KS
13707 char *cond_string, *extra_string;
13708 int thread, task;
ef23e705 13709
b5fa468f
TBA
13710 find_condition_and_thread_for_sals (sals, b->extra_string,
13711 &cond_string, &thread,
13712 &task, &extra_string);
f00aae0f 13713 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13714 if (cond_string)
13715 b->cond_string = cond_string;
13716 b->thread = thread;
13717 b->task = task;
e7e0cddf 13718 if (extra_string)
f00aae0f
KS
13719 {
13720 xfree (b->extra_string);
13721 b->extra_string = extra_string;
13722 }
ef23e705
TJB
13723 b->condition_not_parsed = 0;
13724 }
13725
983af33b 13726 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13727 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13728
58438ac1
TT
13729 *found = 1;
13730 }
13731 else
13732 *found = 0;
ef23e705
TJB
13733
13734 return sals;
13735}
13736
348d480f
PA
13737/* The default re_set method, for typical hardware or software
13738 breakpoints. Reevaluate the breakpoint and recreate its
13739 locations. */
13740
13741static void
28010a5d 13742breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13743{
c2f4122d 13744 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13745 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13746
6c5b2ebe
PA
13747 int found;
13748 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13749 filter_pspace, &found);
ef23e705 13750 if (found)
6c5b2ebe 13751 expanded = std::move (sals);
ef23e705 13752
f00aae0f 13753 if (b->location_range_end != NULL)
f1310107 13754 {
6c5b2ebe
PA
13755 std::vector<symtab_and_line> sals_end
13756 = location_to_sals (b, b->location_range_end.get (),
13757 filter_pspace, &found);
f1310107 13758 if (found)
6c5b2ebe 13759 expanded_end = std::move (sals_end);
f1310107
TJB
13760 }
13761
c2f4122d 13762 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13763}
13764
983af33b
SDJ
13765/* Default method for creating SALs from an address string. It basically
13766 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13767
13768static void
626d2320 13769create_sals_from_location_default (struct event_location *location,
f00aae0f
KS
13770 struct linespec_result *canonical,
13771 enum bptype type_wanted)
983af33b 13772{
f00aae0f 13773 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13774}
13775
13776/* Call create_breakpoints_sal for the given arguments. This is the default
13777 function for the `create_breakpoints_sal' method of
13778 breakpoint_ops. */
13779
13780static void
13781create_breakpoints_sal_default (struct gdbarch *gdbarch,
13782 struct linespec_result *canonical,
e1e01040
PA
13783 gdb::unique_xmalloc_ptr<char> cond_string,
13784 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13785 enum bptype type_wanted,
13786 enum bpdisp disposition,
13787 int thread,
13788 int task, int ignore_count,
13789 const struct breakpoint_ops *ops,
13790 int from_tty, int enabled,
44f238bb 13791 int internal, unsigned flags)
983af33b 13792{
e1e01040
PA
13793 create_breakpoints_sal (gdbarch, canonical,
13794 std::move (cond_string),
13795 std::move (extra_string),
983af33b
SDJ
13796 type_wanted, disposition,
13797 thread, task, ignore_count, ops, from_tty,
44f238bb 13798 enabled, internal, flags);
983af33b
SDJ
13799}
13800
13801/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13802 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13803
6c5b2ebe 13804static std::vector<symtab_and_line>
f00aae0f 13805decode_location_default (struct breakpoint *b,
626d2320 13806 struct event_location *location,
6c5b2ebe 13807 struct program_space *search_pspace)
983af33b
SDJ
13808{
13809 struct linespec_result canonical;
13810
c2f4122d 13811 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
cafb3438 13812 NULL, 0, &canonical, multiple_symbols_all,
c0e8dcd8 13813 b->filter.get ());
983af33b
SDJ
13814
13815 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13816 gdb_assert (canonical.lsals.size () < 2);
983af33b 13817
6c5b2ebe 13818 if (!canonical.lsals.empty ())
983af33b 13819 {
6c5b2ebe
PA
13820 const linespec_sals &lsal = canonical.lsals[0];
13821 return std::move (lsal.sals);
983af33b 13822 }
6c5b2ebe 13823 return {};
983af33b
SDJ
13824}
13825
bf469271 13826/* Reset a breakpoint. */
c906108c 13827
bf469271
PA
13828static void
13829breakpoint_re_set_one (breakpoint *b)
c906108c 13830{
fdf44873
TT
13831 input_radix = b->input_radix;
13832 set_language (b->language);
c906108c 13833
348d480f 13834 b->ops->re_set (b);
c906108c
SS
13835}
13836
c2f4122d
PA
13837/* Re-set breakpoint locations for the current program space.
13838 Locations bound to other program spaces are left untouched. */
13839
c906108c 13840void
69de3c6a 13841breakpoint_re_set (void)
c906108c 13842{
c5aa993b 13843 {
fdf44873
TT
13844 scoped_restore_current_language save_language;
13845 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13846 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13847
8e817061
JB
13848 /* breakpoint_re_set_one sets the current_language to the language
13849 of the breakpoint it is resetting (see prepare_re_set_context)
13850 before re-evaluating the breakpoint's location. This change can
13851 unfortunately get undone by accident if the language_mode is set
13852 to auto, and we either switch frames, or more likely in this context,
13853 we select the current frame.
13854
13855 We prevent this by temporarily turning the language_mode to
13856 language_mode_manual. We restore it once all breakpoints
13857 have been reset. */
13858 scoped_restore save_language_mode = make_scoped_restore (&language_mode);
13859 language_mode = language_mode_manual;
13860
5ed8105e
PA
13861 /* Note: we must not try to insert locations until after all
13862 breakpoints have been re-set. Otherwise, e.g., when re-setting
13863 breakpoint 1, we'd insert the locations of breakpoint 2, which
13864 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13865
1428b37a 13866 for (breakpoint *b : all_breakpoints_safe ())
5ed8105e 13867 {
a70b8144 13868 try
bf469271
PA
13869 {
13870 breakpoint_re_set_one (b);
13871 }
230d2906 13872 catch (const gdb_exception &ex)
bf469271
PA
13873 {
13874 exception_fprintf (gdb_stderr, ex,
13875 "Error in re-setting breakpoint %d: ",
13876 b->number);
13877 }
5ed8105e 13878 }
5ed8105e
PA
13879
13880 jit_breakpoint_re_set ();
13881 }
6c95b8df 13882
af02033e
PP
13883 create_overlay_event_breakpoint ();
13884 create_longjmp_master_breakpoint ();
13885 create_std_terminate_master_breakpoint ();
186c406b 13886 create_exception_master_breakpoint ();
2a7f3dff
PA
13887
13888 /* Now we can insert. */
13889 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
13890}
13891\f
c906108c
SS
13892/* Reset the thread number of this breakpoint:
13893
13894 - If the breakpoint is for all threads, leave it as-is.
4a64f543 13895 - Else, reset it to the current thread for inferior_ptid. */
c906108c 13896void
fba45db2 13897breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
13898{
13899 if (b->thread != -1)
13900 {
00431a78 13901 b->thread = inferior_thread ()->global_num;
6c95b8df
PA
13902
13903 /* We're being called after following a fork. The new fork is
13904 selected as current, and unless this was a vfork will have a
13905 different program space from the original thread. Reset that
13906 as well. */
13907 b->loc->pspace = current_program_space;
c906108c
SS
13908 }
13909}
13910
03ac34d5
MS
13911/* Set ignore-count of breakpoint number BPTNUM to COUNT.
13912 If from_tty is nonzero, it prints a message to that effect,
13913 which ends with a period (no newline). */
13914
c906108c 13915void
fba45db2 13916set_ignore_count (int bptnum, int count, int from_tty)
c906108c 13917{
c906108c
SS
13918 if (count < 0)
13919 count = 0;
13920
43892fdf 13921 for (breakpoint *b : all_breakpoints ())
c906108c 13922 if (b->number == bptnum)
c5aa993b 13923 {
d77f58be
SS
13924 if (is_tracepoint (b))
13925 {
13926 if (from_tty && count != 0)
13927 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13928 bptnum);
13929 return;
13930 }
13931
c5aa993b 13932 b->ignore_count = count;
221ea385
KS
13933 if (from_tty)
13934 {
13935 if (count == 0)
3e43a32a
MS
13936 printf_filtered (_("Will stop next time "
13937 "breakpoint %d is reached."),
221ea385
KS
13938 bptnum);
13939 else if (count == 1)
a3f17187 13940 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
13941 bptnum);
13942 else
3e43a32a
MS
13943 printf_filtered (_("Will ignore next %d "
13944 "crossings of breakpoint %d."),
221ea385
KS
13945 count, bptnum);
13946 }
76727919 13947 gdb::observers::breakpoint_modified.notify (b);
c5aa993b
JM
13948 return;
13949 }
c906108c 13950
8a3fe4f8 13951 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
13952}
13953
c906108c
SS
13954/* Command to set ignore-count of breakpoint N to COUNT. */
13955
13956static void
0b39b52e 13957ignore_command (const char *args, int from_tty)
c906108c 13958{
0b39b52e 13959 const char *p = args;
52f0bd74 13960 int num;
c906108c
SS
13961
13962 if (p == 0)
e2e0b3e5 13963 error_no_arg (_("a breakpoint number"));
c5aa993b 13964
c906108c 13965 num = get_number (&p);
5c44784c 13966 if (num == 0)
8a3fe4f8 13967 error (_("bad breakpoint number: '%s'"), args);
c906108c 13968 if (*p == 0)
8a3fe4f8 13969 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
13970
13971 set_ignore_count (num,
13972 longest_to_int (value_as_long (parse_and_eval (p))),
13973 from_tty);
221ea385
KS
13974 if (from_tty)
13975 printf_filtered ("\n");
c906108c
SS
13976}
13977\f
d0fe4701
XR
13978
13979/* Call FUNCTION on each of the breakpoints with numbers in the range
13980 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
13981
13982static void
d0fe4701
XR
13983map_breakpoint_number_range (std::pair<int, int> bp_num_range,
13984 gdb::function_view<void (breakpoint *)> function)
c906108c 13985{
d0fe4701
XR
13986 if (bp_num_range.first == 0)
13987 {
13988 warning (_("bad breakpoint number at or near '%d'"),
13989 bp_num_range.first);
13990 }
13991 else
c906108c 13992 {
d0fe4701 13993 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 13994 {
d0fe4701
XR
13995 bool match = false;
13996
1428b37a 13997 for (breakpoint *b : all_breakpoints_safe ())
d0fe4701 13998 if (b->number == i)
5c44784c 13999 {
bfd28288 14000 match = true;
48649e1b 14001 function (b);
11cf8741 14002 break;
5c44784c 14003 }
bfd28288 14004 if (!match)
d0fe4701 14005 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14006 }
c906108c
SS
14007 }
14008}
14009
d0fe4701
XR
14010/* Call FUNCTION on each of the breakpoints whose numbers are given in
14011 ARGS. */
14012
14013static void
14014map_breakpoint_numbers (const char *args,
14015 gdb::function_view<void (breakpoint *)> function)
14016{
14017 if (args == NULL || *args == '\0')
14018 error_no_arg (_("one or more breakpoint numbers"));
14019
14020 number_or_range_parser parser (args);
14021
14022 while (!parser.finished ())
14023 {
14024 int num = parser.get_number ();
14025 map_breakpoint_number_range (std::make_pair (num, num), function);
14026 }
14027}
14028
14029/* Return the breakpoint location structure corresponding to the
14030 BP_NUM and LOC_NUM values. */
14031
0d381245 14032static struct bp_location *
d0fe4701 14033find_location_by_number (int bp_num, int loc_num)
0d381245 14034{
43892fdf 14035 breakpoint *b = get_breakpoint (bp_num);
0d381245
VP
14036
14037 if (!b || b->number != bp_num)
d0fe4701 14038 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14039
0d381245 14040 if (loc_num == 0)
d0fe4701 14041 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14042
d0fe4701 14043 int n = 0;
40cb8ca5 14044 for (bp_location *loc : b->locations ())
d0fe4701
XR
14045 if (++n == loc_num)
14046 return loc;
14047
14048 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14049}
14050
95e95a6d
PA
14051/* Modes of operation for extract_bp_num. */
14052enum class extract_bp_kind
14053{
14054 /* Extracting a breakpoint number. */
14055 bp,
14056
14057 /* Extracting a location number. */
14058 loc,
14059};
14060
14061/* Extract a breakpoint or location number (as determined by KIND)
14062 from the string starting at START. TRAILER is a character which
14063 can be found after the number. If you don't want a trailer, use
14064 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14065 string. This always returns a positive integer. */
14066
14067static int
14068extract_bp_num (extract_bp_kind kind, const char *start,
14069 int trailer, const char **end_out = NULL)
14070{
14071 const char *end = start;
14072 int num = get_number_trailer (&end, trailer);
14073 if (num < 0)
14074 error (kind == extract_bp_kind::bp
14075 ? _("Negative breakpoint number '%.*s'")
14076 : _("Negative breakpoint location number '%.*s'"),
14077 int (end - start), start);
14078 if (num == 0)
14079 error (kind == extract_bp_kind::bp
14080 ? _("Bad breakpoint number '%.*s'")
14081 : _("Bad breakpoint location number '%.*s'"),
14082 int (end - start), start);
14083
14084 if (end_out != NULL)
14085 *end_out = end;
14086 return num;
14087}
14088
14089/* Extract a breakpoint or location range (as determined by KIND) in
14090 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14091 representing the (inclusive) range. The returned pair's elements
14092 are always positive integers. */
14093
14094static std::pair<int, int>
14095extract_bp_or_bp_range (extract_bp_kind kind,
14096 const std::string &arg,
14097 std::string::size_type arg_offset)
14098{
14099 std::pair<int, int> range;
14100 const char *bp_loc = &arg[arg_offset];
14101 std::string::size_type dash = arg.find ('-', arg_offset);
14102 if (dash != std::string::npos)
14103 {
14104 /* bp_loc is a range (x-z). */
14105 if (arg.length () == dash + 1)
14106 error (kind == extract_bp_kind::bp
14107 ? _("Bad breakpoint number at or near: '%s'")
14108 : _("Bad breakpoint location number at or near: '%s'"),
14109 bp_loc);
14110
14111 const char *end;
14112 const char *start_first = bp_loc;
14113 const char *start_second = &arg[dash + 1];
14114 range.first = extract_bp_num (kind, start_first, '-');
14115 range.second = extract_bp_num (kind, start_second, '\0', &end);
14116
14117 if (range.first > range.second)
14118 error (kind == extract_bp_kind::bp
14119 ? _("Inverted breakpoint range at '%.*s'")
14120 : _("Inverted breakpoint location range at '%.*s'"),
14121 int (end - start_first), start_first);
14122 }
14123 else
14124 {
14125 /* bp_loc is a single value. */
14126 range.first = extract_bp_num (kind, bp_loc, '\0');
14127 range.second = range.first;
14128 }
14129 return range;
14130}
14131
d0fe4701
XR
14132/* Extract the breakpoint/location range specified by ARG. Returns
14133 the breakpoint range in BP_NUM_RANGE, and the location range in
14134 BP_LOC_RANGE.
14135
14136 ARG may be in any of the following forms:
14137
14138 x where 'x' is a breakpoint number.
14139 x-y where 'x' and 'y' specify a breakpoint numbers range.
14140 x.y where 'x' is a breakpoint number and 'y' a location number.
14141 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14142 location number range.
14143*/
14144
cc638e86 14145static void
d0fe4701
XR
14146extract_bp_number_and_location (const std::string &arg,
14147 std::pair<int, int> &bp_num_range,
14148 std::pair<int, int> &bp_loc_range)
14149{
14150 std::string::size_type dot = arg.find ('.');
14151
14152 if (dot != std::string::npos)
14153 {
14154 /* Handle 'x.y' and 'x.y-z' cases. */
14155
14156 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14157 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14158
95e95a6d
PA
14159 bp_num_range.first
14160 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14161 bp_num_range.second = bp_num_range.first;
d0fe4701 14162
95e95a6d
PA
14163 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14164 arg, dot + 1);
d0fe4701
XR
14165 }
14166 else
14167 {
14168 /* Handle x and x-y cases. */
d0fe4701 14169
95e95a6d 14170 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14171 bp_loc_range.first = 0;
14172 bp_loc_range.second = 0;
14173 }
d0fe4701
XR
14174}
14175
14176/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14177 specifies whether to enable or disable. */
14178
14179static void
14180enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14181{
14182 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14183 if (loc != NULL)
14184 {
b5fa468f
TBA
14185 if (loc->disabled_by_cond && enable)
14186 error (_("Breakpoint %d's condition is invalid at location %d, "
14187 "cannot enable."), bp_num, loc_num);
14188
d0fe4701
XR
14189 if (loc->enabled != enable)
14190 {
14191 loc->enabled = enable;
14192 mark_breakpoint_location_modified (loc);
14193 }
14194 if (target_supports_enable_disable_tracepoint ()
14195 && current_trace_status ()->running && loc->owner
14196 && is_tracepoint (loc->owner))
14197 target_disable_tracepoint (loc);
14198 }
14199 update_global_location_list (UGLL_DONT_INSERT);
d7154a8d
JV
14200
14201 gdb::observers::breakpoint_modified.notify (loc->owner);
d0fe4701
XR
14202}
14203
14204/* Enable or disable a range of breakpoint locations. BP_NUM is the
14205 number of the breakpoint, and BP_LOC_RANGE specifies the
14206 (inclusive) range of location numbers of that breakpoint to
14207 enable/disable. ENABLE specifies whether to enable or disable the
14208 location. */
14209
14210static void
14211enable_disable_breakpoint_location_range (int bp_num,
14212 std::pair<int, int> &bp_loc_range,
14213 bool enable)
14214{
14215 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14216 enable_disable_bp_num_loc (bp_num, i, enable);
14217}
0d381245 14218
1900040c
MS
14219/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14220 If from_tty is nonzero, it prints a message to that effect,
14221 which ends with a period (no newline). */
14222
c906108c 14223void
fba45db2 14224disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14225{
14226 /* Never disable a watchpoint scope breakpoint; we want to
14227 hit them when we leave scope so we can delete both the
14228 watchpoint and its scope breakpoint at that time. */
14229 if (bpt->type == bp_watchpoint_scope)
14230 return;
14231
b5de0fa7 14232 bpt->enable_state = bp_disabled;
c906108c 14233
b775012e
LM
14234 /* Mark breakpoint locations modified. */
14235 mark_breakpoint_modified (bpt);
14236
d248b706
KY
14237 if (target_supports_enable_disable_tracepoint ()
14238 && current_trace_status ()->running && is_tracepoint (bpt))
14239 {
40cb8ca5 14240 for (bp_location *location : bpt->locations ())
d248b706
KY
14241 target_disable_tracepoint (location);
14242 }
14243
44702360 14244 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14245
76727919 14246 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14247}
14248
d0fe4701
XR
14249/* Enable or disable the breakpoint(s) or breakpoint location(s)
14250 specified in ARGS. ARGS may be in any of the formats handled by
14251 extract_bp_number_and_location. ENABLE specifies whether to enable
14252 or disable the breakpoints/locations. */
14253
c906108c 14254static void
d0fe4701 14255enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14256{
c906108c 14257 if (args == 0)
46c6471b 14258 {
43892fdf 14259 for (breakpoint *bpt : all_breakpoints ())
46c6471b 14260 if (user_breakpoint_p (bpt))
d0fe4701
XR
14261 {
14262 if (enable)
14263 enable_breakpoint (bpt);
14264 else
14265 disable_breakpoint (bpt);
14266 }
46c6471b 14267 }
9eaabc75 14268 else
0d381245 14269 {
cb791d59 14270 std::string num = extract_arg (&args);
9eaabc75 14271
cb791d59 14272 while (!num.empty ())
d248b706 14273 {
d0fe4701 14274 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14275
cc638e86
PA
14276 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14277
14278 if (bp_loc_range.first == bp_loc_range.second
14279 && bp_loc_range.first == 0)
d0fe4701 14280 {
cc638e86
PA
14281 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14282 map_breakpoint_number_range (bp_num_range,
14283 enable
14284 ? enable_breakpoint
14285 : disable_breakpoint);
14286 }
14287 else
14288 {
14289 /* Handle breakpoint ids with formats 'x.y' or
14290 'x.y-z'. */
14291 enable_disable_breakpoint_location_range
14292 (bp_num_range.first, bp_loc_range, enable);
b775012e 14293 }
9eaabc75 14294 num = extract_arg (&args);
d248b706 14295 }
0d381245 14296 }
c906108c
SS
14297}
14298
d0fe4701
XR
14299/* The disable command disables the specified breakpoints/locations
14300 (or all defined breakpoints) so they're no longer effective in
14301 stopping the inferior. ARGS may be in any of the forms defined in
14302 extract_bp_number_and_location. */
14303
14304static void
14305disable_command (const char *args, int from_tty)
14306{
14307 enable_disable_command (args, from_tty, false);
14308}
14309
c906108c 14310static void
816338b5
SS
14311enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14312 int count)
c906108c 14313{
afe38095 14314 int target_resources_ok;
c906108c
SS
14315
14316 if (bpt->type == bp_hardware_breakpoint)
14317 {
14318 int i;
c5aa993b 14319 i = hw_breakpoint_used_count ();
53a5351d 14320 target_resources_ok =
d92524f1 14321 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14322 i + 1, 0);
c906108c 14323 if (target_resources_ok == 0)
8a3fe4f8 14324 error (_("No hardware breakpoint support in the target."));
c906108c 14325 else if (target_resources_ok < 0)
8a3fe4f8 14326 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14327 }
14328
cc60f2e3 14329 if (is_watchpoint (bpt))
c906108c 14330 {
d07205c2 14331 /* Initialize it just to avoid a GCC false warning. */
f486487f 14332 enum enable_state orig_enable_state = bp_disabled;
dde02812 14333
a70b8144 14334 try
c906108c 14335 {
3a5c3e22
PA
14336 struct watchpoint *w = (struct watchpoint *) bpt;
14337
1e718ff1
TJB
14338 orig_enable_state = bpt->enable_state;
14339 bpt->enable_state = bp_enabled;
3a5c3e22 14340 update_watchpoint (w, 1 /* reparse */);
c906108c 14341 }
230d2906 14342 catch (const gdb_exception &e)
c5aa993b 14343 {
1e718ff1 14344 bpt->enable_state = orig_enable_state;
dde02812
ES
14345 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14346 bpt->number);
14347 return;
c5aa993b 14348 }
c906108c 14349 }
0101ce28 14350
b775012e
LM
14351 bpt->enable_state = bp_enabled;
14352
14353 /* Mark breakpoint locations modified. */
14354 mark_breakpoint_modified (bpt);
14355
d248b706
KY
14356 if (target_supports_enable_disable_tracepoint ()
14357 && current_trace_status ()->running && is_tracepoint (bpt))
14358 {
40cb8ca5 14359 for (bp_location *location : bpt->locations ())
d248b706
KY
14360 target_enable_tracepoint (location);
14361 }
14362
b4c291bb 14363 bpt->disposition = disposition;
816338b5 14364 bpt->enable_count = count;
44702360 14365 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14366
76727919 14367 gdb::observers::breakpoint_modified.notify (bpt);
c906108c
SS
14368}
14369
fe3f5fa8 14370
c906108c 14371void
fba45db2 14372enable_breakpoint (struct breakpoint *bpt)
c906108c 14373{
816338b5 14374 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14375}
14376
d0fe4701
XR
14377/* The enable command enables the specified breakpoints/locations (or
14378 all defined breakpoints) so they once again become (or continue to
14379 be) effective in stopping the inferior. ARGS may be in any of the
14380 forms defined in extract_bp_number_and_location. */
c906108c 14381
c906108c 14382static void
981a3fb3 14383enable_command (const char *args, int from_tty)
c906108c 14384{
d0fe4701 14385 enable_disable_command (args, from_tty, true);
c906108c
SS
14386}
14387
c906108c 14388static void
4495129a 14389enable_once_command (const char *args, int from_tty)
c906108c 14390{
48649e1b
TT
14391 map_breakpoint_numbers
14392 (args, [&] (breakpoint *b)
14393 {
14394 iterate_over_related_breakpoints
14395 (b, [&] (breakpoint *bpt)
14396 {
14397 enable_breakpoint_disp (bpt, disp_disable, 1);
14398 });
14399 });
816338b5
SS
14400}
14401
14402static void
4495129a 14403enable_count_command (const char *args, int from_tty)
816338b5 14404{
b9d61307
SM
14405 int count;
14406
14407 if (args == NULL)
14408 error_no_arg (_("hit count"));
14409
14410 count = get_number (&args);
816338b5 14411
48649e1b
TT
14412 map_breakpoint_numbers
14413 (args, [&] (breakpoint *b)
14414 {
14415 iterate_over_related_breakpoints
14416 (b, [&] (breakpoint *bpt)
14417 {
14418 enable_breakpoint_disp (bpt, disp_disable, count);
14419 });
14420 });
c906108c
SS
14421}
14422
c906108c 14423static void
4495129a 14424enable_delete_command (const char *args, int from_tty)
c906108c 14425{
48649e1b
TT
14426 map_breakpoint_numbers
14427 (args, [&] (breakpoint *b)
14428 {
14429 iterate_over_related_breakpoints
14430 (b, [&] (breakpoint *bpt)
14431 {
14432 enable_breakpoint_disp (bpt, disp_del, 1);
14433 });
14434 });
c906108c
SS
14435}
14436\f
1f3b5d1b
PP
14437/* Invalidate last known value of any hardware watchpoint if
14438 the memory which that value represents has been written to by
14439 GDB itself. */
14440
14441static void
8de0566d
YQ
14442invalidate_bp_value_on_memory_change (struct inferior *inferior,
14443 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14444 const bfd_byte *data)
14445{
43892fdf 14446 for (breakpoint *bp : all_breakpoints ())
1f3b5d1b 14447 if (bp->enable_state == bp_enabled
3a5c3e22 14448 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14449 {
3a5c3e22 14450 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14451
850645cf 14452 if (wp->val_valid && wp->val != nullptr)
3a5c3e22 14453 {
40cb8ca5 14454 for (bp_location *loc : bp->locations ())
3a5c3e22
PA
14455 if (loc->loc_type == bp_loc_hardware_watchpoint
14456 && loc->address + loc->length > addr
14457 && addr + len > loc->address)
14458 {
3a5c3e22 14459 wp->val = NULL;
4c1d86d9 14460 wp->val_valid = false;
3a5c3e22
PA
14461 }
14462 }
1f3b5d1b
PP
14463 }
14464}
14465
8181d85f
DJ
14466/* Create and insert a breakpoint for software single step. */
14467
14468void
6c95b8df 14469insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14470 const address_space *aspace,
4a64f543 14471 CORE_ADDR next_pc)
8181d85f 14472{
7c16b83e
PA
14473 struct thread_info *tp = inferior_thread ();
14474 struct symtab_and_line sal;
14475 CORE_ADDR pc = next_pc;
8181d85f 14476
34b7e8a6
PA
14477 if (tp->control.single_step_breakpoints == NULL)
14478 {
14479 tp->control.single_step_breakpoints
5d5658a1 14480 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14481 }
8181d85f 14482
7c16b83e
PA
14483 sal = find_pc_line (pc, 0);
14484 sal.pc = pc;
14485 sal.section = find_pc_overlay (pc);
14486 sal.explicit_pc = 1;
34b7e8a6 14487 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14488
7c16b83e 14489 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14490}
14491
93f9a11f
YQ
14492/* Insert single step breakpoints according to the current state. */
14493
14494int
14495insert_single_step_breakpoints (struct gdbarch *gdbarch)
14496{
f5ea389a 14497 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14498 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14499
f5ea389a 14500 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14501
a0ff9e1a 14502 if (!next_pcs.empty ())
93f9a11f 14503 {
f5ea389a 14504 struct frame_info *frame = get_current_frame ();
8b86c959 14505 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14506
a0ff9e1a 14507 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14508 insert_single_step_breakpoint (gdbarch, aspace, pc);
14509
93f9a11f
YQ
14510 return 1;
14511 }
14512 else
14513 return 0;
14514}
14515
34b7e8a6 14516/* See breakpoint.h. */
f02253f1
HZ
14517
14518int
7c16b83e 14519breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14520 const address_space *aspace,
7c16b83e 14521 CORE_ADDR pc)
1aafd4da 14522{
40cb8ca5 14523 for (bp_location *loc : bp->locations ())
7c16b83e
PA
14524 if (loc->inserted
14525 && breakpoint_location_address_match (loc, aspace, pc))
14526 return 1;
1aafd4da 14527
7c16b83e 14528 return 0;
ef370185
JB
14529}
14530
14531/* Check whether a software single-step breakpoint is inserted at
14532 PC. */
14533
14534int
accd0bcd 14535single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14536 CORE_ADDR pc)
14537{
43892fdf 14538 for (breakpoint *bpt : all_breakpoints ())
34b7e8a6
PA
14539 {
14540 if (bpt->type == bp_single_step
14541 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14542 return 1;
14543 }
14544 return 0;
1aafd4da
UW
14545}
14546
1042e4c0
SS
14547/* Tracepoint-specific operations. */
14548
14549/* Set tracepoint count to NUM. */
14550static void
14551set_tracepoint_count (int num)
14552{
14553 tracepoint_count = num;
4fa62494 14554 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14555}
14556
70221824 14557static void
0b39b52e 14558trace_command (const char *arg, int from_tty)
1042e4c0 14559{
ffc2605c
TT
14560 event_location_up location = string_to_event_location (&arg,
14561 current_language);
bac7c5cf
GB
14562 const struct breakpoint_ops *ops = breakpoint_ops_for_event_location
14563 (location.get (), true /* is_tracepoint */);
55aa24fb 14564
558a9d82 14565 create_breakpoint (get_current_arch (),
ffc2605c 14566 location.get (),
10a636cc 14567 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14568 0 /* tempflag */,
14569 bp_tracepoint /* type_wanted */,
14570 0 /* Ignore count */,
14571 pending_break_support,
14572 ops,
14573 from_tty,
14574 1 /* enabled */,
14575 0 /* internal */, 0);
1042e4c0
SS
14576}
14577
70221824 14578static void
0b39b52e 14579ftrace_command (const char *arg, int from_tty)
7a697b8d 14580{
ffc2605c
TT
14581 event_location_up location = string_to_event_location (&arg,
14582 current_language);
558a9d82 14583 create_breakpoint (get_current_arch (),
ffc2605c 14584 location.get (),
10a636cc 14585 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14586 0 /* tempflag */,
14587 bp_fast_tracepoint /* type_wanted */,
14588 0 /* Ignore count */,
14589 pending_break_support,
14590 &tracepoint_breakpoint_ops,
14591 from_tty,
14592 1 /* enabled */,
14593 0 /* internal */, 0);
0fb4aa4b
PA
14594}
14595
14596/* strace command implementation. Creates a static tracepoint. */
14597
70221824 14598static void
0b39b52e 14599strace_command (const char *arg, int from_tty)
0fb4aa4b 14600{
983af33b 14601 struct breakpoint_ops *ops;
ffc2605c 14602 event_location_up location;
983af33b
SDJ
14603
14604 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14605 or with a normal static tracepoint. */
61012eef 14606 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14607 {
14608 ops = &strace_marker_breakpoint_ops;
a20714ff 14609 location = new_linespec_location (&arg, symbol_name_match_type::FULL);
f00aae0f 14610 }
983af33b 14611 else
f00aae0f
KS
14612 {
14613 ops = &tracepoint_breakpoint_ops;
14614 location = string_to_event_location (&arg, current_language);
14615 }
983af33b 14616
558a9d82 14617 create_breakpoint (get_current_arch (),
ffc2605c 14618 location.get (),
10a636cc 14619 NULL, 0, arg, false, 1 /* parse arg */,
558a9d82
YQ
14620 0 /* tempflag */,
14621 bp_static_tracepoint /* type_wanted */,
14622 0 /* Ignore count */,
14623 pending_break_support,
14624 ops,
14625 from_tty,
14626 1 /* enabled */,
14627 0 /* internal */, 0);
7a697b8d
SS
14628}
14629
409873ef
SS
14630/* Set up a fake reader function that gets command lines from a linked
14631 list that was acquired during tracepoint uploading. */
14632
14633static struct uploaded_tp *this_utp;
3149d8c1 14634static int next_cmd;
409873ef
SS
14635
14636static char *
14637read_uploaded_action (void)
14638{
a18ba4e4 14639 char *rslt = nullptr;
409873ef 14640
a18ba4e4
SM
14641 if (next_cmd < this_utp->cmd_strings.size ())
14642 {
67aa1f3c 14643 rslt = this_utp->cmd_strings[next_cmd].get ();
a18ba4e4
SM
14644 next_cmd++;
14645 }
409873ef
SS
14646
14647 return rslt;
14648}
14649
00bf0b85
SS
14650/* Given information about a tracepoint as recorded on a target (which
14651 can be either a live system or a trace file), attempt to create an
14652 equivalent GDB tracepoint. This is not a reliable process, since
14653 the target does not necessarily have all the information used when
14654 the tracepoint was originally defined. */
14655
d9b3f62e 14656struct tracepoint *
00bf0b85 14657create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14658{
f2fc3015
TT
14659 const char *addr_str;
14660 char small_buf[100];
d9b3f62e 14661 struct tracepoint *tp;
fd9b8c24 14662
409873ef 14663 if (utp->at_string)
67aa1f3c 14664 addr_str = utp->at_string.get ();
409873ef
SS
14665 else
14666 {
14667 /* In the absence of a source location, fall back to raw
14668 address. Since there is no way to confirm that the address
14669 means the same thing as when the trace was started, warn the
14670 user. */
3e43a32a
MS
14671 warning (_("Uploaded tracepoint %d has no "
14672 "source location, using raw address"),
409873ef 14673 utp->number);
8c042590 14674 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14675 addr_str = small_buf;
14676 }
14677
14678 /* There's not much we can do with a sequence of bytecodes. */
14679 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14680 warning (_("Uploaded tracepoint %d condition "
14681 "has no source form, ignoring it"),
409873ef 14682 utp->number);
d5551862 14683
ffc2605c
TT
14684 event_location_up location = string_to_event_location (&addr_str,
14685 current_language);
8cdf0e15 14686 if (!create_breakpoint (get_current_arch (),
ffc2605c 14687 location.get (),
67aa1f3c 14688 utp->cond_string.get (), -1, addr_str,
10a636cc 14689 false /* force_condition */,
e7e0cddf 14690 0 /* parse cond/thread */,
8cdf0e15 14691 0 /* tempflag */,
0fb4aa4b 14692 utp->type /* type_wanted */,
8cdf0e15
VP
14693 0 /* Ignore count */,
14694 pending_break_support,
348d480f 14695 &tracepoint_breakpoint_ops,
8cdf0e15 14696 0 /* from_tty */,
84f4c1fe 14697 utp->enabled /* enabled */,
44f238bb
PA
14698 0 /* internal */,
14699 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14700 return NULL;
fd9b8c24 14701
409873ef 14702 /* Get the tracepoint we just created. */
fd9b8c24
PA
14703 tp = get_tracepoint (tracepoint_count);
14704 gdb_assert (tp != NULL);
d5551862 14705
00bf0b85
SS
14706 if (utp->pass > 0)
14707 {
8c042590 14708 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14709 tp->number);
00bf0b85 14710
409873ef 14711 trace_pass_command (small_buf, 0);
00bf0b85
SS
14712 }
14713
409873ef
SS
14714 /* If we have uploaded versions of the original commands, set up a
14715 special-purpose "reader" function and call the usual command line
14716 reader, then pass the result to the breakpoint command-setting
14717 function. */
a18ba4e4 14718 if (!utp->cmd_strings.empty ())
00bf0b85 14719 {
12973681 14720 counted_command_line cmd_list;
00bf0b85 14721
409873ef 14722 this_utp = utp;
3149d8c1 14723 next_cmd = 0;
d5551862 14724
60b3cef2 14725 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL);
409873ef 14726
c1fc2657 14727 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14728 }
a18ba4e4
SM
14729 else if (!utp->actions.empty ()
14730 || !utp->step_actions.empty ())
3e43a32a
MS
14731 warning (_("Uploaded tracepoint %d actions "
14732 "have no source form, ignoring them"),
409873ef 14733 utp->number);
00bf0b85 14734
f196051f 14735 /* Copy any status information that might be available. */
c1fc2657 14736 tp->hit_count = utp->hit_count;
f196051f
SS
14737 tp->traceframe_usage = utp->traceframe_usage;
14738
00bf0b85 14739 return tp;
d9b3f62e 14740}
00bf0b85 14741
1042e4c0
SS
14742/* Print information on tracepoint number TPNUM_EXP, or all if
14743 omitted. */
14744
14745static void
1d12d88f 14746info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14747{
79a45e25 14748 struct ui_out *uiout = current_uiout;
e5a67952 14749 int num_printed;
1042e4c0 14750
5c458ae8 14751 num_printed = breakpoint_1 (args, false, is_tracepoint);
d77f58be
SS
14752
14753 if (num_printed == 0)
1042e4c0 14754 {
e5a67952 14755 if (args == NULL || *args == '\0')
112e8700 14756 uiout->message ("No tracepoints.\n");
d77f58be 14757 else
112e8700 14758 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14759 }
ad443146
SS
14760
14761 default_collect_info ();
1042e4c0
SS
14762}
14763
4a64f543 14764/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14765 Not supported by all targets. */
14766static void
5fed81ff 14767enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14768{
14769 enable_command (args, from_tty);
14770}
14771
4a64f543 14772/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14773 Not supported by all targets. */
14774static void
5fed81ff 14775disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14776{
14777 disable_command (args, from_tty);
14778}
14779
4a64f543 14780/* Remove a tracepoint (or all if no argument). */
1042e4c0 14781static void
4495129a 14782delete_trace_command (const char *arg, int from_tty)
1042e4c0 14783{
1042e4c0
SS
14784 dont_repeat ();
14785
14786 if (arg == 0)
14787 {
14788 int breaks_to_delete = 0;
14789
14790 /* Delete all breakpoints if no argument.
dda83cd7
SM
14791 Do not delete internal or call-dummy breakpoints, these
14792 have to be deleted with an explicit breakpoint number
4a64f543 14793 argument. */
f6d17b2b 14794 for (breakpoint *tp : all_tracepoints ())
1428b37a 14795 if (is_tracepoint (tp) && user_breakpoint_p (tp))
1042e4c0
SS
14796 {
14797 breaks_to_delete = 1;
14798 break;
14799 }
1042e4c0
SS
14800
14801 /* Ask user only if there are some breakpoints to delete. */
14802 if (!from_tty
14803 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14804 {
1428b37a 14805 for (breakpoint *b : all_breakpoints_safe ())
46c6471b 14806 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14807 delete_breakpoint (b);
1042e4c0
SS
14808 }
14809 }
14810 else
48649e1b 14811 map_breakpoint_numbers
b926417a 14812 (arg, [&] (breakpoint *br)
48649e1b 14813 {
b926417a 14814 iterate_over_related_breakpoints (br, delete_breakpoint);
48649e1b 14815 });
1042e4c0
SS
14816}
14817
197f0a60
TT
14818/* Helper function for trace_pass_command. */
14819
14820static void
d9b3f62e 14821trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14822{
d9b3f62e 14823 tp->pass_count = count;
76727919 14824 gdb::observers::breakpoint_modified.notify (tp);
197f0a60
TT
14825 if (from_tty)
14826 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14827 tp->number, count);
197f0a60
TT
14828}
14829
1042e4c0
SS
14830/* Set passcount for tracepoint.
14831
14832 First command argument is passcount, second is tracepoint number.
14833 If tracepoint number omitted, apply to most recently defined.
14834 Also accepts special argument "all". */
14835
14836static void
0b39b52e 14837trace_pass_command (const char *args, int from_tty)
1042e4c0 14838{
d9b3f62e 14839 struct tracepoint *t1;
0b39b52e 14840 ULONGEST count;
1042e4c0
SS
14841
14842 if (args == 0 || *args == 0)
3e43a32a
MS
14843 error (_("passcount command requires an "
14844 "argument (count + optional TP num)"));
1042e4c0 14845
0b39b52e 14846 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14847
529480d0 14848 args = skip_spaces (args);
1042e4c0
SS
14849 if (*args && strncasecmp (args, "all", 3) == 0)
14850 {
14851 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14852 if (*args)
14853 error (_("Junk at end of arguments."));
1042e4c0 14854
f6d17b2b 14855 for (breakpoint *b : all_tracepoints ())
197f0a60 14856 {
d9b3f62e 14857 t1 = (struct tracepoint *) b;
197f0a60
TT
14858 trace_pass_set_count (t1, count, from_tty);
14859 }
14860 }
14861 else if (*args == '\0')
1042e4c0 14862 {
5fa1d40e 14863 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14864 if (t1)
197f0a60
TT
14865 trace_pass_set_count (t1, count, from_tty);
14866 }
14867 else
14868 {
bfd28288
PA
14869 number_or_range_parser parser (args);
14870 while (!parser.finished ())
1042e4c0 14871 {
bfd28288 14872 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14873 if (t1)
14874 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14875 }
14876 }
1042e4c0
SS
14877}
14878
d9b3f62e 14879struct tracepoint *
1042e4c0
SS
14880get_tracepoint (int num)
14881{
f6d17b2b 14882 for (breakpoint *t : all_tracepoints ())
1042e4c0 14883 if (t->number == num)
d9b3f62e 14884 return (struct tracepoint *) t;
1042e4c0
SS
14885
14886 return NULL;
14887}
14888
d5551862
SS
14889/* Find the tracepoint with the given target-side number (which may be
14890 different from the tracepoint number after disconnecting and
14891 reconnecting). */
14892
d9b3f62e 14893struct tracepoint *
d5551862
SS
14894get_tracepoint_by_number_on_target (int num)
14895{
f6d17b2b 14896 for (breakpoint *b : all_tracepoints ())
d9b3f62e
PA
14897 {
14898 struct tracepoint *t = (struct tracepoint *) b;
14899
14900 if (t->number_on_target == num)
14901 return t;
14902 }
d5551862
SS
14903
14904 return NULL;
14905}
14906
1042e4c0 14907/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14908 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14909 If the argument is missing, the most recent tracepoint
14910 (tracepoint_count) is returned. */
14911
d9b3f62e 14912struct tracepoint *
0b39b52e 14913get_tracepoint_by_number (const char **arg,
bfd28288 14914 number_or_range_parser *parser)
1042e4c0 14915{
1042e4c0 14916 int tpnum;
0b39b52e 14917 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 14918
bfd28288 14919 if (parser != NULL)
197f0a60 14920 {
bfd28288
PA
14921 gdb_assert (!parser->finished ());
14922 tpnum = parser->get_number ();
197f0a60
TT
14923 }
14924 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14925 tpnum = tracepoint_count;
1042e4c0 14926 else
197f0a60 14927 tpnum = get_number (arg);
1042e4c0
SS
14928
14929 if (tpnum <= 0)
14930 {
14931 if (instring && *instring)
14932 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14933 instring);
14934 else
5fa1d40e 14935 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14936 return NULL;
14937 }
14938
f6d17b2b 14939 for (breakpoint *t : all_tracepoints ())
1042e4c0
SS
14940 if (t->number == tpnum)
14941 {
d9b3f62e 14942 return (struct tracepoint *) t;
1042e4c0
SS
14943 }
14944
1042e4c0
SS
14945 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
14946 return NULL;
14947}
14948
d9b3f62e
PA
14949void
14950print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
14951{
14952 if (b->thread != -1)
14953 fprintf_unfiltered (fp, " thread %d", b->thread);
14954
14955 if (b->task != 0)
14956 fprintf_unfiltered (fp, " task %d", b->task);
14957
14958 fprintf_unfiltered (fp, "\n");
14959}
14960
6149aea9
PA
14961/* Save information on user settable breakpoints (watchpoints, etc) to
14962 a new script file named FILENAME. If FILTER is non-NULL, call it
14963 on each breakpoint and only include the ones for which it returns
f2478a7e 14964 true. */
6149aea9 14965
1042e4c0 14966static void
4495129a 14967save_breakpoints (const char *filename, int from_tty,
f2478a7e 14968 bool (*filter) (const struct breakpoint *))
1042e4c0 14969{
6149aea9 14970 int any = 0;
6149aea9 14971 int extra_trace_bits = 0;
1042e4c0 14972
6149aea9
PA
14973 if (filename == 0 || *filename == 0)
14974 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
14975
14976 /* See if we have anything to save. */
43892fdf 14977 for (breakpoint *tp : all_breakpoints ())
1042e4c0 14978 {
6149aea9 14979 /* Skip internal and momentary breakpoints. */
09d682a4 14980 if (!user_breakpoint_p (tp))
6149aea9
PA
14981 continue;
14982
14983 /* If we have a filter, only save the breakpoints it accepts. */
14984 if (filter && !filter (tp))
14985 continue;
14986
14987 any = 1;
14988
14989 if (is_tracepoint (tp))
14990 {
14991 extra_trace_bits = 1;
14992
14993 /* We can stop searching. */
14994 break;
14995 }
1042e4c0 14996 }
6149aea9
PA
14997
14998 if (!any)
1042e4c0 14999 {
6149aea9 15000 warning (_("Nothing to save."));
1042e4c0
SS
15001 return;
15002 }
15003
ee0c3293 15004 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15005
15006 stdio_file fp;
15007
ee0c3293 15008 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15009 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15010 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15011
6149aea9 15012 if (extra_trace_bits)
d7e74731 15013 save_trace_state_variables (&fp);
8bf6485c 15014
43892fdf 15015 for (breakpoint *tp : all_breakpoints ())
1042e4c0 15016 {
6149aea9 15017 /* Skip internal and momentary breakpoints. */
09d682a4 15018 if (!user_breakpoint_p (tp))
6149aea9 15019 continue;
8bf6485c 15020
6149aea9
PA
15021 /* If we have a filter, only save the breakpoints it accepts. */
15022 if (filter && !filter (tp))
15023 continue;
15024
d7e74731 15025 tp->ops->print_recreate (tp, &fp);
1042e4c0 15026
6149aea9
PA
15027 /* Note, we can't rely on tp->number for anything, as we can't
15028 assume the recreated breakpoint numbers will match. Use $bpnum
15029 instead. */
15030
15031 if (tp->cond_string)
d7e74731 15032 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15033
15034 if (tp->ignore_count)
d7e74731 15035 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15036
2d9442cc 15037 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15038 {
d7e74731 15039 fp.puts (" commands\n");
a7bdde9e 15040
d7e74731 15041 current_uiout->redirect (&fp);
a70b8144 15042 try
1042e4c0 15043 {
d1b0a7bf 15044 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15045 }
230d2906 15046 catch (const gdb_exception &ex)
492d29ea 15047 {
112e8700 15048 current_uiout->redirect (NULL);
eedc3f4f 15049 throw;
492d29ea 15050 }
1042e4c0 15051
112e8700 15052 current_uiout->redirect (NULL);
d7e74731 15053 fp.puts (" end\n");
1042e4c0 15054 }
6149aea9
PA
15055
15056 if (tp->enable_state == bp_disabled)
d7e74731 15057 fp.puts ("disable $bpnum\n");
6149aea9
PA
15058
15059 /* If this is a multi-location breakpoint, check if the locations
15060 should be individually disabled. Watchpoint locations are
15061 special, and not user visible. */
15062 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15063 {
6149aea9
PA
15064 int n = 1;
15065
40cb8ca5
SM
15066 for (bp_location *loc : tp->locations ())
15067 {
15068 if (!loc->enabled)
15069 fp.printf ("disable $bpnum.%d\n", n);
15070
15071 n++;
15072 }
6149aea9 15073 }
1042e4c0 15074 }
8bf6485c 15075
6149aea9 15076 if (extra_trace_bits && *default_collect)
d7e74731 15077 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15078
1042e4c0 15079 if (from_tty)
ee0c3293 15080 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15081}
15082
15083/* The `save breakpoints' command. */
15084
15085static void
4495129a 15086save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15087{
15088 save_breakpoints (args, from_tty, NULL);
15089}
15090
15091/* The `save tracepoints' command. */
15092
15093static void
4495129a 15094save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15095{
15096 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15097}
15098
c906108c 15099\f
629500fa
KS
15100/* This help string is used to consolidate all the help string for specifying
15101 locations used by several commands. */
15102
15103#define LOCATION_HELP_STRING \
15104"Linespecs are colon-separated lists of location parameters, such as\n\
15105source filename, function name, label name, and line number.\n\
15106Example: To specify the start of a label named \"the_top\" in the\n\
15107function \"fact\" in the file \"factorial.c\", use\n\
15108\"factorial.c:fact:the_top\".\n\
15109\n\
15110Address locations begin with \"*\" and specify an exact address in the\n\
15111program. Example: To specify the fourth byte past the start function\n\
15112\"main\", use \"*main + 4\".\n\
15113\n\
15114Explicit locations are similar to linespecs but use an option/argument\n\
15115syntax to specify location parameters.\n\
15116Example: To specify the start of the label named \"the_top\" in the\n\
15117function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
a20714ff
PA
15118-function fact -label the_top\".\n\
15119\n\
15120By default, a specified function is matched against the program's\n\
15121functions in all scopes. For C++, this means in all namespaces and\n\
15122classes. For Ada, this means in all packages. E.g., in C++,\n\
15123\"func()\" matches \"A::func()\", \"A::B::func()\", etc. The\n\
15124\"-qualified\" flag overrides this behavior, making GDB interpret the\n\
89549d7f 15125specified name as a complete fully-qualified name instead."
629500fa 15126
4a64f543
MS
15127/* This help string is used for the break, hbreak, tbreak and thbreak
15128 commands. It is defined as a macro to prevent duplication.
15129 COMMAND should be a string constant containing the name of the
15130 command. */
629500fa 15131
31e2b00f 15132#define BREAK_ARGS_HELP(command) \
733d554a
TBA
15133command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM]\n\
15134\t[-force-condition] [if CONDITION]\n\
fb7b5af4
SDJ
15135PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15136probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15137guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15138`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15139LOCATION may be a linespec, address, or explicit location as described\n\
15140below.\n\
15141\n\
dc10affe
PA
15142With no LOCATION, uses current execution address of the selected\n\
15143stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15144\n\
15145THREADNUM is the number from \"info threads\".\n\
15146CONDITION is a boolean expression.\n\
733d554a
TBA
15147\n\
15148With the \"-force-condition\" flag, the condition is defined even when\n\
15149it is invalid for all current locations.\n\
89549d7f 15150\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15151Multiple breakpoints at one place are permitted, and useful if their\n\
15152conditions are different.\n\
31e2b00f
AS
15153\n\
15154Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15155
44feb3ce
TT
15156/* List of subcommands for "catch". */
15157static struct cmd_list_element *catch_cmdlist;
15158
15159/* List of subcommands for "tcatch". */
15160static struct cmd_list_element *tcatch_cmdlist;
15161
9ac4176b 15162void
a121b7c1 15163add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15164 cmd_const_sfunc_ftype *sfunc,
625e8578 15165 completer_ftype *completer,
44feb3ce
TT
15166 void *user_data_catch,
15167 void *user_data_tcatch)
15168{
15169 struct cmd_list_element *command;
15170
0450cc4c 15171 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15172 &catch_cmdlist);
15173 set_cmd_sfunc (command, sfunc);
15174 set_cmd_context (command, user_data_catch);
a96d9b2e 15175 set_cmd_completer (command, completer);
44feb3ce 15176
0450cc4c 15177 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15178 &tcatch_cmdlist);
15179 set_cmd_sfunc (command, sfunc);
15180 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15181 set_cmd_completer (command, completer);
44feb3ce
TT
15182}
15183
0574c78f
GB
15184/* Zero if any of the breakpoint's locations could be a location where
15185 functions have been inlined, nonzero otherwise. */
15186
15187static int
15188is_non_inline_function (struct breakpoint *b)
15189{
15190 /* The shared library event breakpoint is set on the address of a
15191 non-inline function. */
15192 if (b->type == bp_shlib_event)
15193 return 1;
15194
15195 return 0;
15196}
15197
15198/* Nonzero if the specified PC cannot be a location where functions
15199 have been inlined. */
15200
15201int
accd0bcd 15202pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15203 const struct target_waitstatus *ws)
0574c78f 15204{
43892fdf 15205 for (breakpoint *b : all_breakpoints ())
0574c78f
GB
15206 {
15207 if (!is_non_inline_function (b))
15208 continue;
15209
40cb8ca5 15210 for (bp_location *bl : b->locations ())
0574c78f
GB
15211 {
15212 if (!bl->shlib_disabled
09ac7c10 15213 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15214 return 1;
15215 }
15216 }
15217
15218 return 0;
15219}
15220
2f202fde
JK
15221/* Remove any references to OBJFILE which is going to be freed. */
15222
15223void
15224breakpoint_free_objfile (struct objfile *objfile)
15225{
48d7020b 15226 for (bp_location *loc : all_bp_locations ())
eb822aa6 15227 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15228 loc->symtab = NULL;
15229}
15230
2060206e
PA
15231void
15232initialize_breakpoint_ops (void)
15233{
15234 static int initialized = 0;
15235
15236 struct breakpoint_ops *ops;
15237
15238 if (initialized)
15239 return;
15240 initialized = 1;
15241
15242 /* The breakpoint_ops structure to be inherit by all kinds of
15243 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15244 internal and momentary breakpoints, etc.). */
15245 ops = &bkpt_base_breakpoint_ops;
15246 *ops = base_breakpoint_ops;
15247 ops->re_set = bkpt_re_set;
15248 ops->insert_location = bkpt_insert_location;
15249 ops->remove_location = bkpt_remove_location;
15250 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15251 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15252 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15253 ops->decode_location = bkpt_decode_location;
2060206e
PA
15254
15255 /* The breakpoint_ops structure to be used in regular breakpoints. */
15256 ops = &bkpt_breakpoint_ops;
15257 *ops = bkpt_base_breakpoint_ops;
15258 ops->re_set = bkpt_re_set;
15259 ops->resources_needed = bkpt_resources_needed;
15260 ops->print_it = bkpt_print_it;
15261 ops->print_mention = bkpt_print_mention;
15262 ops->print_recreate = bkpt_print_recreate;
15263
15264 /* Ranged breakpoints. */
15265 ops = &ranged_breakpoint_ops;
15266 *ops = bkpt_breakpoint_ops;
15267 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15268 ops->resources_needed = resources_needed_ranged_breakpoint;
15269 ops->print_it = print_it_ranged_breakpoint;
15270 ops->print_one = print_one_ranged_breakpoint;
15271 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15272 ops->print_mention = print_mention_ranged_breakpoint;
15273 ops->print_recreate = print_recreate_ranged_breakpoint;
15274
15275 /* Internal breakpoints. */
15276 ops = &internal_breakpoint_ops;
15277 *ops = bkpt_base_breakpoint_ops;
15278 ops->re_set = internal_bkpt_re_set;
15279 ops->check_status = internal_bkpt_check_status;
15280 ops->print_it = internal_bkpt_print_it;
15281 ops->print_mention = internal_bkpt_print_mention;
15282
15283 /* Momentary breakpoints. */
15284 ops = &momentary_breakpoint_ops;
15285 *ops = bkpt_base_breakpoint_ops;
15286 ops->re_set = momentary_bkpt_re_set;
15287 ops->check_status = momentary_bkpt_check_status;
15288 ops->print_it = momentary_bkpt_print_it;
15289 ops->print_mention = momentary_bkpt_print_mention;
15290
55aa24fb
SDJ
15291 /* Probe breakpoints. */
15292 ops = &bkpt_probe_breakpoint_ops;
15293 *ops = bkpt_breakpoint_ops;
15294 ops->insert_location = bkpt_probe_insert_location;
15295 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15296 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15297 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15298
2060206e
PA
15299 /* Watchpoints. */
15300 ops = &watchpoint_breakpoint_ops;
15301 *ops = base_breakpoint_ops;
15302 ops->re_set = re_set_watchpoint;
15303 ops->insert_location = insert_watchpoint;
15304 ops->remove_location = remove_watchpoint;
15305 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15306 ops->check_status = check_status_watchpoint;
15307 ops->resources_needed = resources_needed_watchpoint;
15308 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15309 ops->print_it = print_it_watchpoint;
15310 ops->print_mention = print_mention_watchpoint;
15311 ops->print_recreate = print_recreate_watchpoint;
427cd150 15312 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15313
15314 /* Masked watchpoints. */
15315 ops = &masked_watchpoint_breakpoint_ops;
15316 *ops = watchpoint_breakpoint_ops;
15317 ops->insert_location = insert_masked_watchpoint;
15318 ops->remove_location = remove_masked_watchpoint;
15319 ops->resources_needed = resources_needed_masked_watchpoint;
15320 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15321 ops->print_it = print_it_masked_watchpoint;
15322 ops->print_one_detail = print_one_detail_masked_watchpoint;
15323 ops->print_mention = print_mention_masked_watchpoint;
15324 ops->print_recreate = print_recreate_masked_watchpoint;
15325
15326 /* Tracepoints. */
15327 ops = &tracepoint_breakpoint_ops;
15328 *ops = base_breakpoint_ops;
15329 ops->re_set = tracepoint_re_set;
15330 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15331 ops->print_one_detail = tracepoint_print_one_detail;
15332 ops->print_mention = tracepoint_print_mention;
15333 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15334 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15335 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15336 ops->decode_location = tracepoint_decode_location;
983af33b 15337
55aa24fb
SDJ
15338 /* Probe tracepoints. */
15339 ops = &tracepoint_probe_breakpoint_ops;
15340 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15341 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15342 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15343
983af33b
SDJ
15344 /* Static tracepoints with marker (`-m'). */
15345 ops = &strace_marker_breakpoint_ops;
15346 *ops = tracepoint_breakpoint_ops;
5f700d83 15347 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15348 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15349 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15350
15351 /* Fork catchpoints. */
15352 ops = &catch_fork_breakpoint_ops;
15353 *ops = base_breakpoint_ops;
15354 ops->insert_location = insert_catch_fork;
15355 ops->remove_location = remove_catch_fork;
15356 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15357 ops->print_it = print_it_catch_fork;
15358 ops->print_one = print_one_catch_fork;
15359 ops->print_mention = print_mention_catch_fork;
15360 ops->print_recreate = print_recreate_catch_fork;
15361
15362 /* Vfork catchpoints. */
15363 ops = &catch_vfork_breakpoint_ops;
15364 *ops = base_breakpoint_ops;
15365 ops->insert_location = insert_catch_vfork;
15366 ops->remove_location = remove_catch_vfork;
15367 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15368 ops->print_it = print_it_catch_vfork;
15369 ops->print_one = print_one_catch_vfork;
15370 ops->print_mention = print_mention_catch_vfork;
15371 ops->print_recreate = print_recreate_catch_vfork;
15372
15373 /* Exec catchpoints. */
15374 ops = &catch_exec_breakpoint_ops;
15375 *ops = base_breakpoint_ops;
2060206e
PA
15376 ops->insert_location = insert_catch_exec;
15377 ops->remove_location = remove_catch_exec;
15378 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15379 ops->print_it = print_it_catch_exec;
15380 ops->print_one = print_one_catch_exec;
15381 ops->print_mention = print_mention_catch_exec;
15382 ops->print_recreate = print_recreate_catch_exec;
15383
edcc5120
TT
15384 /* Solib-related catchpoints. */
15385 ops = &catch_solib_breakpoint_ops;
15386 *ops = base_breakpoint_ops;
edcc5120
TT
15387 ops->insert_location = insert_catch_solib;
15388 ops->remove_location = remove_catch_solib;
15389 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15390 ops->check_status = check_status_catch_solib;
15391 ops->print_it = print_it_catch_solib;
15392 ops->print_one = print_one_catch_solib;
15393 ops->print_mention = print_mention_catch_solib;
15394 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15395
15396 ops = &dprintf_breakpoint_ops;
15397 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15398 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15399 ops->resources_needed = bkpt_resources_needed;
15400 ops->print_it = bkpt_print_it;
15401 ops->print_mention = bkpt_print_mention;
2d9442cc 15402 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15403 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15404 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15405}
15406
8bfd80db
YQ
15407/* Chain containing all defined "enable breakpoint" subcommands. */
15408
15409static struct cmd_list_element *enablebreaklist = NULL;
15410
8588b356
SM
15411/* See breakpoint.h. */
15412
15413cmd_list_element *commands_cmd_element = nullptr;
15414
6c265988 15415void _initialize_breakpoint ();
c906108c 15416void
6c265988 15417_initialize_breakpoint ()
c906108c
SS
15418{
15419 struct cmd_list_element *c;
15420
2060206e
PA
15421 initialize_breakpoint_ops ();
15422
c90e7d63
SM
15423 gdb::observers::solib_unloaded.attach (disable_breakpoints_in_unloaded_shlib,
15424 "breakpoint");
15425 gdb::observers::free_objfile.attach (disable_breakpoints_in_freed_objfile,
15426 "breakpoint");
15427 gdb::observers::memory_changed.attach (invalidate_bp_value_on_memory_change,
15428 "breakpoint");
84acb35a 15429
c906108c
SS
15430 breakpoint_chain = 0;
15431 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15432 before a breakpoint is set. */
15433 breakpoint_count = 0;
15434
1042e4c0
SS
15435 tracepoint_count = 0;
15436
1bedd215
AC
15437 add_com ("ignore", class_breakpoint, ignore_command, _("\
15438Set ignore-count of breakpoint number N to COUNT.\n\
15439Usage is `ignore N COUNT'."));
c906108c 15440
8588b356
SM
15441 commands_cmd_element = add_com ("commands", class_breakpoint,
15442 commands_command, _("\
18da0c51
MG
15443Set commands to be executed when the given breakpoints are hit.\n\
15444Give a space-separated breakpoint list as argument after \"commands\".\n\
15445A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15446(e.g. `5-7').\n\
c906108c
SS
15447With no argument, the targeted breakpoint is the last one set.\n\
15448The commands themselves follow starting on the next line.\n\
15449Type a line containing \"end\" to indicate the end of them.\n\
15450Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15451then no output is printed when it is hit, except what the commands print."));
c906108c 15452
b1d4d8d1
TBA
15453 const auto cc_opts = make_condition_command_options_def_group (nullptr);
15454 static std::string condition_command_help
15455 = gdb::option::build_help (_("\
1bedd215 15456Specify breakpoint number N to break only if COND is true.\n\
b1d4d8d1 15457Usage is `condition [OPTION] N COND', where N is an integer and COND\n\
733d554a 15458is an expression to be evaluated whenever breakpoint N is reached.\n\
b1d4d8d1
TBA
15459\n\
15460Options:\n\
15461%OPTIONS%"), cc_opts);
15462
15463 c = add_com ("condition", class_breakpoint, condition_command,
15464 condition_command_help.c_str ());
15465 set_cmd_completer_handle_brkchars (c, condition_completer);
c906108c 15466
1bedd215 15467 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15468Set a temporary breakpoint.\n\
c906108c
SS
15469Like \"break\" except the breakpoint is only temporary,\n\
15470so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15471by using \"enable delete\" on the breakpoint number.\n\
15472\n"
15473BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15474 set_cmd_completer (c, location_completer);
c94fdfd0 15475
1bedd215 15476 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15477Set a hardware assisted breakpoint.\n\
c906108c 15478Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15479some target hardware may not have this support.\n\
15480\n"
15481BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15482 set_cmd_completer (c, location_completer);
c906108c 15483
1bedd215 15484 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15485Set a temporary hardware assisted breakpoint.\n\
c906108c 15486Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15487so it will be deleted when hit.\n\
15488\n"
15489BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15490 set_cmd_completer (c, location_completer);
c906108c 15491
3947f654
SM
15492 cmd_list_element *enable_cmd
15493 = add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
590042fc
PW
15494Enable all or some breakpoints.\n\
15495Usage: enable [BREAKPOINTNUM]...\n\
c906108c
SS
15496Give breakpoint numbers (separated by spaces) as arguments.\n\
15497With no subcommand, breakpoints are enabled until you command otherwise.\n\
15498This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15499With a subcommand you can enable temporarily."),
3947f654 15500 &enablelist, 1, &cmdlist);
c906108c 15501
3947f654 15502 add_com_alias ("en", enable_cmd, class_breakpoint, 1);
c906108c 15503
84951ab5 15504 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
590042fc
PW
15505Enable all or some breakpoints.\n\
15506Usage: enable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15507Give breakpoint numbers (separated by spaces) as arguments.\n\
15508This is used to cancel the effect of the \"disable\" command.\n\
89549d7f 15509May be abbreviated to simply \"enable\"."),
2f822da5 15510 &enablebreaklist, 1, &enablelist);
c906108c 15511
1a966eab 15512 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15513Enable some breakpoints for one hit.\n\
15514Usage: enable breakpoints once BREAKPOINTNUM...\n\
1a966eab 15515If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15516 &enablebreaklist);
15517
1a966eab 15518 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15519Enable some breakpoints and delete when hit.\n\
15520Usage: enable breakpoints delete BREAKPOINTNUM...\n\
1a966eab 15521If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15522 &enablebreaklist);
15523
816338b5 15524 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15525Enable some breakpoints for COUNT hits.\n\
15526Usage: enable breakpoints count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15527If a breakpoint is hit while enabled in this fashion,\n\
15528the count is decremented; when it reaches zero, the breakpoint is disabled."),
15529 &enablebreaklist);
15530
1a966eab 15531 add_cmd ("delete", no_class, enable_delete_command, _("\
590042fc
PW
15532Enable some breakpoints and delete when hit.\n\
15533Usage: enable delete BREAKPOINTNUM...\n\
1a966eab 15534If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15535 &enablelist);
15536
1a966eab 15537 add_cmd ("once", no_class, enable_once_command, _("\
590042fc
PW
15538Enable some breakpoints for one hit.\n\
15539Usage: enable once BREAKPOINTNUM...\n\
1a966eab 15540If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15541 &enablelist);
15542
15543 add_cmd ("count", no_class, enable_count_command, _("\
590042fc
PW
15544Enable some breakpoints for COUNT hits.\n\
15545Usage: enable count COUNT BREAKPOINTNUM...\n\
816338b5
SS
15546If a breakpoint is hit while enabled in this fashion,\n\
15547the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15548 &enablelist);
15549
3947f654
SM
15550 cmd_list_element *disable_cmd
15551 = add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
590042fc
PW
15552Disable all or some breakpoints.\n\
15553Usage: disable [BREAKPOINTNUM]...\n\
c906108c
SS
15554Arguments are breakpoint numbers with spaces in between.\n\
15555To disable all breakpoints, give no argument.\n\
64b9b334 15556A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
3947f654
SM
15557 &disablelist, 1, &cmdlist);
15558 add_com_alias ("dis", disable_cmd, class_breakpoint, 1);
15559 add_com_alias ("disa", disable_cmd, class_breakpoint, 1);
c906108c 15560
57b4f16e 15561 add_cmd ("breakpoints", class_breakpoint, disable_command, _("\
590042fc
PW
15562Disable all or some breakpoints.\n\
15563Usage: disable breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15564Arguments are breakpoint numbers with spaces in between.\n\
15565To disable all breakpoints, give no argument.\n\
64b9b334 15566A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15567This command may be abbreviated \"disable\"."),
c906108c
SS
15568 &disablelist);
15569
3947f654
SM
15570 cmd_list_element *delete_cmd
15571 = add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
590042fc
PW
15572Delete all or some breakpoints.\n\
15573Usage: delete [BREAKPOINTNUM]...\n\
c906108c
SS
15574Arguments are breakpoint numbers with spaces in between.\n\
15575To delete all breakpoints, give no argument.\n\
15576\n\
590042fc 15577Also a prefix command for deletion of other GDB objects."),
3947f654
SM
15578 &deletelist, 1, &cmdlist);
15579 add_com_alias ("d", delete_cmd, class_breakpoint, 1);
15580 add_com_alias ("del", delete_cmd, class_breakpoint, 1);
c906108c 15581
57b4f16e 15582 add_cmd ("breakpoints", class_breakpoint, delete_command, _("\
590042fc
PW
15583Delete all or some breakpoints or auto-display expressions.\n\
15584Usage: delete breakpoints [BREAKPOINTNUM]...\n\
c906108c
SS
15585Arguments are breakpoint numbers with spaces in between.\n\
15586To delete all breakpoints, give no argument.\n\
1a966eab 15587This command may be abbreviated \"delete\"."),
c906108c
SS
15588 &deletelist);
15589
3947f654
SM
15590 cmd_list_element *clear_cmd
15591 = add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15592Clear breakpoint at specified location.\n\
15593Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15594\n\
15595With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa 15596is executing in.\n"
89549d7f 15597"\n" LOCATION_HELP_STRING "\n\n\
1bedd215 15598See also the \"delete\" command which clears breakpoints by number."));
3947f654 15599 add_com_alias ("cl", clear_cmd, class_breakpoint, 1);
c906108c 15600
3947f654
SM
15601 cmd_list_element *break_cmd
15602 = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15603Set breakpoint at specified location.\n"
31e2b00f 15604BREAK_ARGS_HELP ("break")));
3947f654 15605 set_cmd_completer (break_cmd, location_completer);
c94fdfd0 15606
3947f654
SM
15607 add_com_alias ("b", break_cmd, class_run, 1);
15608 add_com_alias ("br", break_cmd, class_run, 1);
15609 add_com_alias ("bre", break_cmd, class_run, 1);
15610 add_com_alias ("brea", break_cmd, class_run, 1);
c906108c 15611
c906108c
SS
15612 if (dbx_commands)
15613 {
1bedd215
AC
15614 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15615Break in function/address or break at a line in the current file."),
2f822da5 15616 &stoplist, 1, &cmdlist);
c5aa993b 15617 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15618 _("Break in function or address."), &stoplist);
c5aa993b 15619 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15620 _("Break at a line in the current file."), &stoplist);
11db9430 15621 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15622Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15623The \"Type\" column indicates one of:\n\
15624\tbreakpoint - normal breakpoint\n\
15625\twatchpoint - watchpoint\n\
15626The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15627the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15628breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15629address and file/line number respectively.\n\
15630\n\
15631Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15632are set to the address of the last breakpoint listed unless the command\n\
15633is prefixed with \"server \".\n\n\
c906108c 15634Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15635breakpoint set."));
c906108c
SS
15636 }
15637
e0f25bd9
SM
15638 cmd_list_element *info_breakpoints_cmd
15639 = add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15640Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15641The \"Type\" column indicates one of:\n\
15642\tbreakpoint - normal breakpoint\n\
15643\twatchpoint - watchpoint\n\
15644The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15645the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15646breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15647address and file/line number respectively.\n\
15648\n\
15649Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15650are set to the address of the last breakpoint listed unless the command\n\
15651is prefixed with \"server \".\n\n\
c906108c 15652Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15653breakpoint set."));
c906108c 15654
e0f25bd9 15655 add_info_alias ("b", info_breakpoints_cmd, 1);
6b04bdb7 15656
1a966eab
AC
15657 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15658Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15659The \"Type\" column indicates one of:\n\
15660\tbreakpoint - normal breakpoint\n\
15661\twatchpoint - watchpoint\n\
15662\tlongjmp - internal breakpoint used to step through longjmp()\n\
15663\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15664\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15665\tfinish - internal breakpoint used by the \"finish\" command\n\
15666The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15667the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15668breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15669address and file/line number respectively.\n\
15670\n\
15671Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15672are set to the address of the last breakpoint listed unless the command\n\
15673is prefixed with \"server \".\n\n\
c906108c 15674Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15675breakpoint set."),
c906108c
SS
15676 &maintenanceinfolist);
15677
3b6acaee 15678 add_basic_prefix_cmd ("catch", class_breakpoint, _("\
44feb3ce 15679Set catchpoints to catch events."),
2f822da5 15680 &catch_cmdlist,
3b6acaee 15681 0/*allow-unknown*/, &cmdlist);
44feb3ce 15682
3b6acaee 15683 add_basic_prefix_cmd ("tcatch", class_breakpoint, _("\
44feb3ce 15684Set temporary catchpoints to catch events."),
2f822da5 15685 &tcatch_cmdlist,
3b6acaee 15686 0/*allow-unknown*/, &cmdlist);
44feb3ce 15687
44feb3ce
TT
15688 add_catch_command ("fork", _("Catch calls to fork."),
15689 catch_fork_command_1,
dda83cd7 15690 NULL,
44feb3ce
TT
15691 (void *) (uintptr_t) catch_fork_permanent,
15692 (void *) (uintptr_t) catch_fork_temporary);
15693 add_catch_command ("vfork", _("Catch calls to vfork."),
15694 catch_fork_command_1,
dda83cd7 15695 NULL,
44feb3ce
TT
15696 (void *) (uintptr_t) catch_vfork_permanent,
15697 (void *) (uintptr_t) catch_vfork_temporary);
15698 add_catch_command ("exec", _("Catch calls to exec."),
15699 catch_exec_command_1,
dda83cd7 15700 NULL,
a96d9b2e
SDJ
15701 CATCH_PERMANENT,
15702 CATCH_TEMPORARY);
edcc5120
TT
15703 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15704Usage: catch load [REGEX]\n\
15705If REGEX is given, only stop for libraries matching the regular expression."),
15706 catch_load_command_1,
15707 NULL,
15708 CATCH_PERMANENT,
15709 CATCH_TEMPORARY);
15710 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15711Usage: catch unload [REGEX]\n\
15712If REGEX is given, only stop for libraries matching the regular expression."),
15713 catch_unload_command_1,
15714 NULL,
15715 CATCH_PERMANENT,
15716 CATCH_TEMPORARY);
c5aa993b 15717
a15a5258
AB
15718 const auto opts = make_watch_options_def_group (nullptr);
15719
15720 static const std::string watch_help = gdb::option::build_help (_("\
15721Set a watchpoint for EXPRESSION.\n\
15722Usage: watch [-location] EXPRESSION\n\
15723\n\
15724Options:\n\
15725%OPTIONS%\n\
15726\n\
c906108c 15727A watchpoint stops execution of your program whenever the value of\n\
a15a5258
AB
15728an expression changes."), opts);
15729 c = add_com ("watch", class_breakpoint, watch_command,
15730 watch_help.c_str ());
15731 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15732
15733 static const std::string rwatch_help = gdb::option::build_help (_("\
15734Set a read watchpoint for EXPRESSION.\n\
15735Usage: rwatch [-location] EXPRESSION\n\
15736\n\
15737Options:\n\
15738%OPTIONS%\n\
15739\n\
15740A read watchpoint stops execution of your program whenever the value of\n\
15741an expression is read."), opts);
15742 c = add_com ("rwatch", class_breakpoint, rwatch_command,
15743 rwatch_help.c_str ());
15744 set_cmd_completer_handle_brkchars (c, watch_command_completer);
15745
15746 static const std::string awatch_help = gdb::option::build_help (_("\
15747Set an access watchpoint for EXPRESSION.\n\
15748Usage: awatch [-location] EXPRESSION\n\
15749\n\
15750Options:\n\
15751%OPTIONS%\n\
15752\n\
15753An access watchpoint stops execution of your program whenever the value\n\
15754of an expression is either read or written."), opts);
15755 c = add_com ("awatch", class_breakpoint, awatch_command,
15756 awatch_help.c_str ());
15757 set_cmd_completer_handle_brkchars (c, watch_command_completer);
c906108c 15758
11db9430 15759 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15760Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15761
920d2a44
AC
15762 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15763 respond to changes - contrary to the description. */
85c07804
AC
15764 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15765 &can_use_hw_watchpoints, _("\
15766Set debugger's willingness to use watchpoint hardware."), _("\
15767Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15768If zero, gdb will not use hardware for new watchpoints, even if\n\
15769such is available. (However, any hardware watchpoints that were\n\
15770created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15771hardware.)"),
15772 NULL,
920d2a44 15773 show_can_use_hw_watchpoints,
85c07804 15774 &setlist, &showlist);
c906108c
SS
15775
15776 can_use_hw_watchpoints = 1;
fa8d40ab 15777
1042e4c0
SS
15778 /* Tracepoint manipulation commands. */
15779
3947f654
SM
15780 cmd_list_element *trace_cmd
15781 = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15782Set a tracepoint at specified location.\n\
1042e4c0
SS
15783\n"
15784BREAK_ARGS_HELP ("trace") "\n\
15785Do \"help tracepoints\" for info on other tracepoint commands."));
3947f654 15786 set_cmd_completer (trace_cmd, location_completer);
1042e4c0 15787
3947f654
SM
15788 add_com_alias ("tp", trace_cmd, class_breakpoint, 0);
15789 add_com_alias ("tr", trace_cmd, class_breakpoint, 1);
15790 add_com_alias ("tra", trace_cmd, class_breakpoint, 1);
15791 add_com_alias ("trac", trace_cmd, class_breakpoint, 1);
1042e4c0 15792
7a697b8d 15793 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15794Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15795\n"
15796BREAK_ARGS_HELP ("ftrace") "\n\
15797Do \"help tracepoints\" for info on other tracepoint commands."));
15798 set_cmd_completer (c, location_completer);
15799
0fb4aa4b 15800 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15801Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15802\n\
15803strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15804LOCATION may be a linespec, explicit, or address location (described below) \n\
15805or -m MARKER_ID.\n\n\
15806If a marker id is specified, probe the marker with that name. With\n\
15807no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15808Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15809This collects arbitrary user data passed in the probe point call to the\n\
15810tracing library. You can inspect it when analyzing the trace buffer,\n\
15811by printing the $_sdata variable like any other convenience variable.\n\
15812\n\
15813CONDITION is a boolean expression.\n\
89549d7f 15814\n" LOCATION_HELP_STRING "\n\n\
d41c0fc8
PA
15815Multiple tracepoints at one place are permitted, and useful if their\n\
15816conditions are different.\n\
0fb4aa4b
PA
15817\n\
15818Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15819Do \"help tracepoints\" for info on other tracepoint commands."));
15820 set_cmd_completer (c, location_completer);
15821
e0f25bd9
SM
15822 cmd_list_element *info_tracepoints_cmd
15823 = add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15824Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15825Convenience variable \"$tpnum\" contains the number of the\n\
15826last tracepoint set."));
15827
e0f25bd9 15828 add_info_alias ("tp", info_tracepoints_cmd, 1);
1042e4c0 15829
5e84b7ee
SM
15830 cmd_list_element *delete_tracepoints_cmd
15831 = add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
1042e4c0
SS
15832Delete specified tracepoints.\n\
15833Arguments are tracepoint numbers, separated by spaces.\n\
15834No argument means delete all tracepoints."),
15835 &deletelist);
5e84b7ee 15836 add_alias_cmd ("tr", delete_tracepoints_cmd, class_trace, 1, &deletelist);
1042e4c0
SS
15837
15838 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15839Disable specified tracepoints.\n\
15840Arguments are tracepoint numbers, separated by spaces.\n\
15841No argument means disable all tracepoints."),
15842 &disablelist);
15843 deprecate_cmd (c, "disable");
15844
15845 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15846Enable specified tracepoints.\n\
15847Arguments are tracepoint numbers, separated by spaces.\n\
15848No argument means enable all tracepoints."),
15849 &enablelist);
15850 deprecate_cmd (c, "enable");
15851
15852 add_com ("passcount", class_trace, trace_pass_command, _("\
15853Set the passcount for a tracepoint.\n\
15854The trace will end when the tracepoint has been passed 'count' times.\n\
15855Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15856if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15857
0743fc83
TT
15858 add_basic_prefix_cmd ("save", class_breakpoint,
15859 _("Save breakpoint definitions as a script."),
2f822da5 15860 &save_cmdlist,
0743fc83 15861 0/*allow-unknown*/, &cmdlist);
6149aea9
PA
15862
15863 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15864Save current breakpoint definitions as a script.\n\
cce7e648 15865This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15866catchpoints, tracepoints). Use the 'source' command in another debug\n\
15867session to restore them."),
15868 &save_cmdlist);
15869 set_cmd_completer (c, filename_completer);
15870
3947f654
SM
15871 cmd_list_element *save_tracepoints_cmd
15872 = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15873Save current tracepoint definitions as a script.\n\
6149aea9
PA
15874Use the 'source' command in another debug session to restore them."),
15875 &save_cmdlist);
3947f654 15876 set_cmd_completer (save_tracepoints_cmd, filename_completer);
1042e4c0 15877
3947f654 15878 c = add_com_alias ("save-tracepoints", save_tracepoints_cmd, class_trace, 0);
6149aea9
PA
15879 deprecate_cmd (c, "save tracepoints");
15880
3b6acaee 15881 add_basic_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15882Breakpoint specific settings.\n\
fa8d40ab 15883Configure various breakpoint-specific variables such as\n\
590042fc 15884pending breakpoint behavior."),
2f822da5 15885 &breakpoint_set_cmdlist,
3b6acaee
TT
15886 0/*allow-unknown*/, &setlist);
15887 add_show_prefix_cmd ("breakpoint", class_maintenance, _("\
590042fc 15888Breakpoint specific settings.\n\
fa8d40ab 15889Configure various breakpoint-specific variables such as\n\
590042fc 15890pending breakpoint behavior."),
2f822da5 15891 &breakpoint_show_cmdlist,
3b6acaee 15892 0/*allow-unknown*/, &showlist);
fa8d40ab 15893
7915a72c
AC
15894 add_setshow_auto_boolean_cmd ("pending", no_class,
15895 &pending_break_support, _("\
15896Set debugger's behavior regarding pending breakpoints."), _("\
15897Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15898If on, an unrecognized breakpoint location will cause gdb to create a\n\
15899pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15900an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15901user-query to see if a pending breakpoint should be created."),
2c5b56ce 15902 NULL,
920d2a44 15903 show_pending_break_support,
6e1d7d6c
AC
15904 &breakpoint_set_cmdlist,
15905 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15906
15907 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15908
15909 add_setshow_boolean_cmd ("auto-hw", no_class,
15910 &automatic_hardware_breakpoints, _("\
15911Set automatic usage of hardware breakpoints."), _("\
15912Show automatic usage of hardware breakpoints."), _("\
15913If set, the debugger will automatically use hardware breakpoints for\n\
15914breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15915a warning will be emitted for such breakpoints."),
15916 NULL,
15917 show_automatic_hardware_breakpoints,
15918 &breakpoint_set_cmdlist,
15919 &breakpoint_show_cmdlist);
74960c60 15920
a25a5a45
PA
15921 add_setshow_boolean_cmd ("always-inserted", class_support,
15922 &always_inserted_mode, _("\
74960c60
VP
15923Set mode for inserting breakpoints."), _("\
15924Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15925When this mode is on, breakpoints are inserted immediately as soon as\n\
15926they're created, kept inserted even when execution stops, and removed\n\
15927only when the user deletes them. When this mode is off (the default),\n\
15928breakpoints are inserted only when execution continues, and removed\n\
15929when execution stops."),
72d0e2c5
YQ
15930 NULL,
15931 &show_always_inserted_mode,
15932 &breakpoint_set_cmdlist,
15933 &breakpoint_show_cmdlist);
f1310107 15934
b775012e
LM
15935 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15936 condition_evaluation_enums,
15937 &condition_evaluation_mode_1, _("\
15938Set mode of breakpoint condition evaluation."), _("\
15939Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15940When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15941evaluated on the host's side by GDB. When it is set to \"target\",\n\
15942breakpoint conditions will be downloaded to the target (if the target\n\
15943supports such feature) and conditions will be evaluated on the target's side.\n\
15944If this is set to \"auto\" (default), this will be automatically set to\n\
15945\"target\" if it supports condition evaluation, otherwise it will\n\
7a4e8e7d 15946be set to \"host\"."),
b775012e
LM
15947 &set_condition_evaluation_mode,
15948 &show_condition_evaluation_mode,
15949 &breakpoint_set_cmdlist,
15950 &breakpoint_show_cmdlist);
15951
f1310107
TJB
15952 add_com ("break-range", class_breakpoint, break_range_command, _("\
15953Set a breakpoint for an address range.\n\
15954break-range START-LOCATION, END-LOCATION\n\
15955where START-LOCATION and END-LOCATION can be one of the following:\n\
15956 LINENUM, for that line in the current file,\n\
15957 FILE:LINENUM, for that line in that file,\n\
15958 +OFFSET, for that number of lines after the current line\n\
dda83cd7 15959 or the start of the range\n\
f1310107
TJB
15960 FUNCTION, for the first line in that function,\n\
15961 FILE:FUNCTION, to distinguish among like-named static functions.\n\
15962 *ADDRESS, for the instruction at that address.\n\
15963\n\
15964The breakpoint will stop execution of the inferior whenever it executes\n\
15965an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
15966range (including START-LOCATION and END-LOCATION)."));
15967
e7e0cddf 15968 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 15969Set a dynamic printf at specified location.\n\
e7e0cddf 15970dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
15971location may be a linespec, explicit, or address location.\n"
15972"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
15973 set_cmd_completer (c, location_completer);
15974
15975 add_setshow_enum_cmd ("dprintf-style", class_support,
15976 dprintf_style_enums, &dprintf_style, _("\
15977Set the style of usage for dynamic printf."), _("\
15978Show the style of usage for dynamic printf."), _("\
15979This setting chooses how GDB will do a dynamic printf.\n\
15980If the value is \"gdb\", then the printing is done by GDB to its own\n\
15981console, as with the \"printf\" command.\n\
15982If the value is \"call\", the print is done by calling a function in your\n\
15983program; by default printf(), but you can choose a different function or\n\
15984output stream by setting dprintf-function and dprintf-channel."),
15985 update_dprintf_commands, NULL,
15986 &setlist, &showlist);
15987
15988 dprintf_function = xstrdup ("printf");
15989 add_setshow_string_cmd ("dprintf-function", class_support,
15990 &dprintf_function, _("\
590042fc
PW
15991Set the function to use for dynamic printf."), _("\
15992Show the function to use for dynamic printf."), NULL,
e7e0cddf
SS
15993 update_dprintf_commands, NULL,
15994 &setlist, &showlist);
15995
15996 dprintf_channel = xstrdup ("");
15997 add_setshow_string_cmd ("dprintf-channel", class_support,
15998 &dprintf_channel, _("\
590042fc
PW
15999Set the channel to use for dynamic printf."), _("\
16000Show the channel to use for dynamic printf."), NULL,
e7e0cddf
SS
16001 update_dprintf_commands, NULL,
16002 &setlist, &showlist);
16003
d3ce09f5
SS
16004 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16005 &disconnected_dprintf, _("\
16006Set whether dprintf continues after GDB disconnects."), _("\
16007Show whether dprintf continues after GDB disconnects."), _("\
16008Use this to let dprintf commands continue to hit and produce output\n\
16009even if GDB disconnects or detaches from the target."),
16010 NULL,
16011 NULL,
16012 &setlist, &showlist);
16013
16014 add_com ("agent-printf", class_vars, agent_printf_command, _("\
590042fc
PW
16015Target agent only formatted printing, like the C \"printf\" function.\n\
16016Usage: agent-printf \"format string\", ARG1, ARG2, ARG3, ..., ARGN\n\
16017This supports most C printf format specifications, like %s, %d, etc.\n\
16018This is useful for formatted output in user-defined commands."));
d3ce09f5 16019
491144b5 16020 automatic_hardware_breakpoints = true;
f3b1572e 16021
c90e7d63
SM
16022 gdb::observers::about_to_proceed.attach (breakpoint_about_to_proceed,
16023 "breakpoint");
16024 gdb::observers::thread_exit.attach (remove_threaded_breakpoints,
16025 "breakpoint");
c906108c 16026}
This page took 4.007132 seconds and 4 git commands to generate.