Remove "do_nothing"
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
61baf725 3 Copyright (C) 1986-2017 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"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5 69#include "format.h"
cfc31633 70#include "thread-fsm.h"
5d5658a1 71#include "tid-parse.h"
d3ce09f5 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
325fac50 82#include <algorithm>
5ed8105e 83#include "progspace-and-thread.h"
6c5b2ebe 84#include "common/array-view.h"
76f9c9cf 85#include "common/gdb_optional.h"
104c1213 86
e7e8980f
YQ
87/* Enums for exception-handling support. */
88enum exception_event_kind
89{
90 EX_EVENT_THROW,
591f19e8 91 EX_EVENT_RETHROW,
e7e8980f
YQ
92 EX_EVENT_CATCH
93};
94
4a64f543 95/* Prototypes for local functions. */
c906108c 96
a14ed312 97static void disable_command (char *, int);
c906108c 98
a14ed312 99static void enable_command (char *, int);
c906108c 100
896b6bda 101static void map_breakpoint_numbers (const char *,
48649e1b 102 gdb::function_view<void (breakpoint *)>);
c906108c 103
a14ed312 104static void ignore_command (char *, int);
c906108c 105
4efb68b1 106static int breakpoint_re_set_one (void *);
c906108c 107
348d480f
PA
108static void breakpoint_re_set_default (struct breakpoint *);
109
f00aae0f
KS
110static void
111 create_sals_from_location_default (const struct event_location *location,
112 struct linespec_result *canonical,
113 enum bptype type_wanted);
983af33b
SDJ
114
115static void create_breakpoints_sal_default (struct gdbarch *,
116 struct linespec_result *,
e1e01040
PA
117 gdb::unique_xmalloc_ptr<char>,
118 gdb::unique_xmalloc_ptr<char>,
119 enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b 124
6c5b2ebe
PA
125static std::vector<symtab_and_line> decode_location_default
126 (struct breakpoint *b, const struct event_location *location,
127 struct program_space *search_pspace);
983af33b 128
a14ed312 129static void clear_command (char *, int);
c906108c 130
a14ed312 131static void catch_command (char *, int);
c906108c 132
a9634178 133static int can_use_hardware_watchpoint (struct value *);
c906108c 134
a14ed312 135static void mention (struct breakpoint *);
c906108c 136
348d480f
PA
137static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
138 enum bptype,
c0a91b2b 139 const struct breakpoint_ops *);
3742cc8b
YQ
140static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
141 const struct symtab_and_line *);
142
4a64f543
MS
143/* This function is used in gdbtk sources and thus can not be made
144 static. */
63c252f8 145struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 146 struct symtab_and_line,
c0a91b2b
TT
147 enum bptype,
148 const struct breakpoint_ops *);
c906108c 149
06edf0c0
PA
150static struct breakpoint *
151 momentary_breakpoint_from_master (struct breakpoint *orig,
152 enum bptype type,
a1aa2221
LM
153 const struct breakpoint_ops *ops,
154 int loc_enabled);
06edf0c0 155
76897487
KB
156static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
157
a6d9a66e
UW
158static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
159 CORE_ADDR bpaddr,
88f7da05 160 enum bptype bptype);
76897487 161
6c95b8df
PA
162static void describe_other_breakpoints (struct gdbarch *,
163 struct program_space *, CORE_ADDR,
5af949e3 164 struct obj_section *, int);
c906108c 165
85d721b8
PA
166static int watchpoint_locations_match (struct bp_location *loc1,
167 struct bp_location *loc2);
168
f1310107
TJB
169static int breakpoint_location_address_match (struct bp_location *bl,
170 struct address_space *aspace,
171 CORE_ADDR addr);
172
d35ae833
PA
173static int breakpoint_location_address_range_overlap (struct bp_location *,
174 struct address_space *,
175 CORE_ADDR, int);
176
11db9430 177static void info_breakpoints_command (char *, int);
c906108c 178
11db9430 179static void info_watchpoints_command (char *, int);
d77f58be 180
4efb68b1 181static int breakpoint_cond_eval (void *);
c906108c 182
4efb68b1 183static void cleanup_executing_breakpoints (void *);
c906108c 184
a14ed312 185static void commands_command (char *, int);
c906108c 186
a14ed312 187static void condition_command (char *, int);
c906108c 188
834c0d03 189static int remove_breakpoint (struct bp_location *);
b2b6a7da 190static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 191
e514a9d6 192static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 193
4efb68b1 194static int watchpoint_check (void *);
c906108c 195
a14ed312 196static int hw_breakpoint_used_count (void);
c906108c 197
a1398e0c
PA
198static int hw_watchpoint_use_count (struct breakpoint *);
199
200static int hw_watchpoint_used_count_others (struct breakpoint *except,
201 enum bptype type,
202 int *other_type_used);
c906108c 203
a14ed312 204static void hbreak_command (char *, int);
c906108c 205
a14ed312 206static void thbreak_command (char *, int);
c906108c 207
816338b5
SS
208static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
209 int count);
c906108c 210
a14ed312 211static void stop_command (char *arg, int from_tty);
7a292a7a 212
a14ed312 213static void tcatch_command (char *arg, int from_tty);
7a292a7a 214
fe3f5fa8 215static void free_bp_location (struct bp_location *loc);
f431efe5
PA
216static void incref_bp_location (struct bp_location *loc);
217static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 218
39d61571 219static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 220
44702360
PA
221/* update_global_location_list's modes of operation wrt to whether to
222 insert locations now. */
223enum ugll_insert_mode
224{
225 /* Don't insert any breakpoint locations into the inferior, only
226 remove already-inserted locations that no longer should be
227 inserted. Functions that delete a breakpoint or breakpoints
228 should specify this mode, so that deleting a breakpoint doesn't
229 have the side effect of inserting the locations of other
230 breakpoints that are marked not-inserted, but should_be_inserted
231 returns true on them.
232
233 This behavior is useful is situations close to tear-down -- e.g.,
234 after an exec, while the target still has execution, but
235 breakpoint shadows of the previous executable image should *NOT*
236 be restored to the new image; or before detaching, where the
237 target still has execution and wants to delete breakpoints from
238 GDB's lists, and all breakpoints had already been removed from
239 the inferior. */
240 UGLL_DONT_INSERT,
241
a25a5a45
PA
242 /* May insert breakpoints iff breakpoints_should_be_inserted_now
243 claims breakpoints should be inserted now. */
04086b45
PA
244 UGLL_MAY_INSERT,
245
a25a5a45
PA
246 /* Insert locations now, irrespective of
247 breakpoints_should_be_inserted_now. E.g., say all threads are
248 stopped right now, and the user did "continue". We need to
249 insert breakpoints _before_ resuming the target, but
250 UGLL_MAY_INSERT wouldn't insert them, because
251 breakpoints_should_be_inserted_now returns false at that point,
252 as no thread is running yet. */
04086b45 253 UGLL_INSERT
44702360
PA
254};
255
256static void update_global_location_list (enum ugll_insert_mode);
a5606eee 257
44702360 258static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 259
d77f58be 260static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
261
262static void insert_breakpoint_locations (void);
a5606eee 263
11db9430 264static void info_tracepoints_command (char *, int);
1042e4c0 265
1042e4c0
SS
266static void enable_trace_command (char *, int);
267
268static void disable_trace_command (char *, int);
269
270static void trace_pass_command (char *, int);
271
558a9d82
YQ
272static void set_tracepoint_count (int num);
273
9c06b0b4
TJB
274static int is_masked_watchpoint (const struct breakpoint *b);
275
b775012e
LM
276static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
277
983af33b
SDJ
278/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
279 otherwise. */
280
281static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 282
2060206e
PA
283/* The breakpoint_ops structure to be inherited by all breakpoint_ops
284 that are implemented on top of software or hardware breakpoints
285 (user breakpoints, internal and momentary breakpoints, etc.). */
286static struct breakpoint_ops bkpt_base_breakpoint_ops;
287
288/* Internal breakpoints class type. */
06edf0c0 289static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
290
291/* Momentary breakpoints class type. */
06edf0c0
PA
292static struct breakpoint_ops momentary_breakpoint_ops;
293
2060206e
PA
294/* The breakpoint_ops structure to be used in regular user created
295 breakpoints. */
296struct breakpoint_ops bkpt_breakpoint_ops;
297
55aa24fb
SDJ
298/* Breakpoints set on probes. */
299static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
e7e0cddf 301/* Dynamic printf class type. */
c5867ab6 302struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 303
d3ce09f5
SS
304/* The style in which to perform a dynamic printf. This is a user
305 option because different output options have different tradeoffs;
306 if GDB does the printing, there is better error handling if there
307 is a problem with any of the arguments, but using an inferior
308 function lets you have special-purpose printers and sending of
309 output to the same place as compiled-in print functions. */
310
311static const char dprintf_style_gdb[] = "gdb";
312static const char dprintf_style_call[] = "call";
313static const char dprintf_style_agent[] = "agent";
314static const char *const dprintf_style_enums[] = {
315 dprintf_style_gdb,
316 dprintf_style_call,
317 dprintf_style_agent,
318 NULL
319};
320static const char *dprintf_style = dprintf_style_gdb;
321
322/* The function to use for dynamic printf if the preferred style is to
323 call into the inferior. The value is simply a string that is
324 copied into the command, so it can be anything that GDB can
325 evaluate to a callable address, not necessarily a function name. */
326
bde6261a 327static char *dprintf_function;
d3ce09f5
SS
328
329/* The channel to use for dynamic printf if the preferred style is to
330 call into the inferior; if a nonempty string, it will be passed to
331 the call as the first argument, with the format string as the
332 second. As with the dprintf function, this can be anything that
333 GDB knows how to evaluate, so in addition to common choices like
334 "stderr", this could be an app-specific expression like
335 "mystreams[curlogger]". */
336
bde6261a 337static char *dprintf_channel;
d3ce09f5
SS
338
339/* True if dprintf commands should continue to operate even if GDB
340 has disconnected. */
341static int disconnected_dprintf = 1;
342
5cea2a26
PA
343struct command_line *
344breakpoint_commands (struct breakpoint *b)
345{
d1b0a7bf 346 return b->commands ? b->commands.get () : NULL;
5cea2a26 347}
3daf8fe5 348
f3b1572e
PA
349/* Flag indicating that a command has proceeded the inferior past the
350 current breakpoint. */
351
352static int breakpoint_proceeded;
353
956a9fb9 354const char *
2cec12e5
AR
355bpdisp_text (enum bpdisp disp)
356{
4a64f543
MS
357 /* NOTE: the following values are a part of MI protocol and
358 represent values of 'disp' field returned when inferior stops at
359 a breakpoint. */
bc043ef3 360 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 361
2cec12e5
AR
362 return bpdisps[(int) disp];
363}
c906108c 364
4a64f543 365/* Prototypes for exported functions. */
c906108c 366/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 367 if such is available. */
c906108c
SS
368static int can_use_hw_watchpoints;
369
920d2a44
AC
370static void
371show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
372 struct cmd_list_element *c,
373 const char *value)
374{
3e43a32a
MS
375 fprintf_filtered (file,
376 _("Debugger's willingness to use "
377 "watchpoint hardware is %s.\n"),
920d2a44
AC
378 value);
379}
380
fa8d40ab
JJ
381/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
382 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 383 for unrecognized breakpoint locations.
fa8d40ab
JJ
384 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
385static enum auto_boolean pending_break_support;
920d2a44
AC
386static void
387show_pending_break_support (struct ui_file *file, int from_tty,
388 struct cmd_list_element *c,
389 const char *value)
390{
3e43a32a
MS
391 fprintf_filtered (file,
392 _("Debugger's behavior regarding "
393 "pending breakpoints is %s.\n"),
920d2a44
AC
394 value);
395}
fa8d40ab 396
765dc015 397/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 398 set with "break" but falling in read-only memory.
765dc015
VP
399 If 0, gdb will warn about such breakpoints, but won't automatically
400 use hardware breakpoints. */
401static int automatic_hardware_breakpoints;
402static void
403show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406{
3e43a32a
MS
407 fprintf_filtered (file,
408 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
409 value);
410}
411
a25a5a45
PA
412/* If on, GDB keeps breakpoints inserted even if the inferior is
413 stopped, and immediately inserts any new breakpoints as soon as
414 they're created. If off (default), GDB keeps breakpoints off of
415 the target as long as possible. That is, it delays inserting
416 breakpoints until the next resume, and removes them again when the
417 target fully stops. This is a bit safer in case GDB crashes while
418 processing user input. */
419static int always_inserted_mode = 0;
72d0e2c5 420
33e5cbd6 421static void
74960c60 422show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 423 struct cmd_list_element *c, const char *value)
74960c60 424{
a25a5a45
PA
425 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
426 value);
74960c60
VP
427}
428
b57bacec
PA
429/* See breakpoint.h. */
430
33e5cbd6 431int
a25a5a45 432breakpoints_should_be_inserted_now (void)
33e5cbd6 433{
a25a5a45
PA
434 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
435 {
436 /* If breakpoints are global, they should be inserted even if no
437 thread under gdb's control is running, or even if there are
438 no threads under GDB's control yet. */
439 return 1;
440 }
441 else if (target_has_execution)
442 {
372316f1
PA
443 struct thread_info *tp;
444
a25a5a45
PA
445 if (always_inserted_mode)
446 {
447 /* The user wants breakpoints inserted even if all threads
448 are stopped. */
449 return 1;
450 }
451
b57bacec
PA
452 if (threads_are_executing ())
453 return 1;
372316f1
PA
454
455 /* Don't remove breakpoints yet if, even though all threads are
456 stopped, we still have events to process. */
457 ALL_NON_EXITED_THREADS (tp)
458 if (tp->resumed
459 && tp->suspend.waitstatus_pending_p)
460 return 1;
a25a5a45
PA
461 }
462 return 0;
33e5cbd6 463}
765dc015 464
b775012e
LM
465static const char condition_evaluation_both[] = "host or target";
466
467/* Modes for breakpoint condition evaluation. */
468static const char condition_evaluation_auto[] = "auto";
469static const char condition_evaluation_host[] = "host";
470static const char condition_evaluation_target[] = "target";
471static const char *const condition_evaluation_enums[] = {
472 condition_evaluation_auto,
473 condition_evaluation_host,
474 condition_evaluation_target,
475 NULL
476};
477
478/* Global that holds the current mode for breakpoint condition evaluation. */
479static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
480
481/* Global that we use to display information to the user (gets its value from
482 condition_evaluation_mode_1. */
483static const char *condition_evaluation_mode = condition_evaluation_auto;
484
485/* Translate a condition evaluation mode MODE into either "host"
486 or "target". This is used mostly to translate from "auto" to the
487 real setting that is being used. It returns the translated
488 evaluation mode. */
489
490static const char *
491translate_condition_evaluation_mode (const char *mode)
492{
493 if (mode == condition_evaluation_auto)
494 {
495 if (target_supports_evaluation_of_breakpoint_conditions ())
496 return condition_evaluation_target;
497 else
498 return condition_evaluation_host;
499 }
500 else
501 return mode;
502}
503
504/* Discovers what condition_evaluation_auto translates to. */
505
506static const char *
507breakpoint_condition_evaluation_mode (void)
508{
509 return translate_condition_evaluation_mode (condition_evaluation_mode);
510}
511
512/* Return true if GDB should evaluate breakpoint conditions or false
513 otherwise. */
514
515static int
516gdb_evaluates_breakpoint_condition_p (void)
517{
518 const char *mode = breakpoint_condition_evaluation_mode ();
519
520 return (mode == condition_evaluation_host);
521}
522
c906108c
SS
523/* Are we executing breakpoint commands? */
524static int executing_breakpoint_commands;
525
c02f5703
MS
526/* Are overlay event breakpoints enabled? */
527static int overlay_events_enabled;
528
e09342b5
TJB
529/* See description in breakpoint.h. */
530int target_exact_watchpoints = 0;
531
c906108c 532/* Walk the following statement or block through all breakpoints.
e5dd4106 533 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 534 current breakpoint. */
c906108c 535
5c44784c 536#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 537
5c44784c
JM
538#define ALL_BREAKPOINTS_SAFE(B,TMP) \
539 for (B = breakpoint_chain; \
540 B ? (TMP=B->next, 1): 0; \
541 B = TMP)
c906108c 542
4a64f543
MS
543/* Similar iterator for the low-level breakpoints. SAFE variant is
544 not provided so update_global_location_list must not be called
545 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 546
876fa593 547#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
548 for (BP_TMP = bp_locations; \
549 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 550 BP_TMP++)
7cc221ef 551
b775012e
LM
552/* Iterates through locations with address ADDRESS for the currently selected
553 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
554 to where the loop should start from.
555 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
556 appropriate location to start with. */
557
558#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
559 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
560 BP_LOCP_TMP = BP_LOCP_START; \
561 BP_LOCP_START \
f5336ca5 562 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
563 && (*BP_LOCP_TMP)->address == ADDRESS); \
564 BP_LOCP_TMP++)
565
1042e4c0
SS
566/* Iterator for tracepoints only. */
567
568#define ALL_TRACEPOINTS(B) \
569 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 570 if (is_tracepoint (B))
1042e4c0 571
7cc221ef 572/* Chains of all breakpoints defined. */
c906108c
SS
573
574struct breakpoint *breakpoint_chain;
575
f5336ca5 576/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 577
f5336ca5 578static struct bp_location **bp_locations;
876fa593 579
f5336ca5 580/* Number of elements of BP_LOCATIONS. */
876fa593 581
f5336ca5 582static unsigned bp_locations_count;
876fa593 583
4a64f543 584/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 585 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 586 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 587 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 588 an address you need to read. */
876fa593 589
f5336ca5 590static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 591
4a64f543
MS
592/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
593 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
594 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
595 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 596 scan for shadow bytes for an address you need to read. */
876fa593 597
f5336ca5 598static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 599
4a64f543 600/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
601 from the bp_locations array, but for which a hit may still be
602 reported by a target. */
20874c92
VP
603VEC(bp_location_p) *moribund_locations = NULL;
604
c906108c
SS
605/* Number of last breakpoint made. */
606
95a42b64
TT
607static int breakpoint_count;
608
86b17b60
PA
609/* The value of `breakpoint_count' before the last command that
610 created breakpoints. If the last (break-like) command created more
611 than one breakpoint, then the difference between BREAKPOINT_COUNT
612 and PREV_BREAKPOINT_COUNT is more than one. */
613static int prev_breakpoint_count;
c906108c 614
1042e4c0
SS
615/* Number of last tracepoint made. */
616
95a42b64 617static int tracepoint_count;
1042e4c0 618
6149aea9
PA
619static struct cmd_list_element *breakpoint_set_cmdlist;
620static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 621struct cmd_list_element *save_cmdlist;
6149aea9 622
badd37ce
SDJ
623/* See declaration at breakpoint.h. */
624
625struct breakpoint *
626breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
627 void *user_data)
628{
629 struct breakpoint *b = NULL;
630
631 ALL_BREAKPOINTS (b)
632 {
633 if (func (b, user_data) != 0)
634 break;
635 }
636
637 return b;
638}
639
468d015d
JJ
640/* Return whether a breakpoint is an active enabled breakpoint. */
641static int
642breakpoint_enabled (struct breakpoint *b)
643{
0d381245 644 return (b->enable_state == bp_enabled);
468d015d
JJ
645}
646
c906108c
SS
647/* Set breakpoint count to NUM. */
648
95a42b64 649static void
fba45db2 650set_breakpoint_count (int num)
c906108c 651{
86b17b60 652 prev_breakpoint_count = breakpoint_count;
c906108c 653 breakpoint_count = num;
4fa62494 654 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
655}
656
86b17b60
PA
657/* Used by `start_rbreak_breakpoints' below, to record the current
658 breakpoint count before "rbreak" creates any breakpoint. */
659static int rbreak_start_breakpoint_count;
660
95a42b64
TT
661/* Called at the start an "rbreak" command to record the first
662 breakpoint made. */
86b17b60 663
95a42b64
TT
664void
665start_rbreak_breakpoints (void)
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
95a42b64
TT
673void
674end_rbreak_breakpoints (void)
675{
86b17b60 676 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
677}
678
4a64f543 679/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
680
681void
fba45db2 682clear_breakpoint_hit_counts (void)
c906108c
SS
683{
684 struct breakpoint *b;
685
686 ALL_BREAKPOINTS (b)
687 b->hit_count = 0;
688}
689
c906108c 690\f
48cb2d85
VP
691/* Return the breakpoint with the specified number, or NULL
692 if the number does not refer to an existing breakpoint. */
693
694struct breakpoint *
695get_breakpoint (int num)
696{
697 struct breakpoint *b;
698
699 ALL_BREAKPOINTS (b)
700 if (b->number == num)
701 return b;
702
703 return NULL;
704}
5c44784c 705
c906108c 706\f
adc36818 707
b775012e
LM
708/* Mark locations as "conditions have changed" in case the target supports
709 evaluating conditions on its side. */
710
711static void
712mark_breakpoint_modified (struct breakpoint *b)
713{
714 struct bp_location *loc;
715
716 /* This is only meaningful if the target is
717 evaluating conditions and if the user has
718 opted for condition evaluation on the target's
719 side. */
720 if (gdb_evaluates_breakpoint_condition_p ()
721 || !target_supports_evaluation_of_breakpoint_conditions ())
722 return;
723
724 if (!is_breakpoint (b))
725 return;
726
727 for (loc = b->loc; loc; loc = loc->next)
728 loc->condition_changed = condition_modified;
729}
730
731/* Mark location as "conditions have changed" in case the target supports
732 evaluating conditions on its side. */
733
734static void
735mark_breakpoint_location_modified (struct bp_location *loc)
736{
737 /* This is only meaningful if the target is
738 evaluating conditions and if the user has
739 opted for condition evaluation on the target's
740 side. */
741 if (gdb_evaluates_breakpoint_condition_p ()
742 || !target_supports_evaluation_of_breakpoint_conditions ())
743
744 return;
745
746 if (!is_breakpoint (loc->owner))
747 return;
748
749 loc->condition_changed = condition_modified;
750}
751
752/* Sets the condition-evaluation mode using the static global
753 condition_evaluation_mode. */
754
755static void
756set_condition_evaluation_mode (char *args, int from_tty,
757 struct cmd_list_element *c)
758{
b775012e
LM
759 const char *old_mode, *new_mode;
760
761 if ((condition_evaluation_mode_1 == condition_evaluation_target)
762 && !target_supports_evaluation_of_breakpoint_conditions ())
763 {
764 condition_evaluation_mode_1 = condition_evaluation_mode;
765 warning (_("Target does not support breakpoint condition evaluation.\n"
766 "Using host evaluation mode instead."));
767 return;
768 }
769
770 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
771 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
772
abf1152a
JK
773 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
774 settings was "auto". */
775 condition_evaluation_mode = condition_evaluation_mode_1;
776
b775012e
LM
777 /* Only update the mode if the user picked a different one. */
778 if (new_mode != old_mode)
779 {
780 struct bp_location *loc, **loc_tmp;
781 /* If the user switched to a different evaluation mode, we
782 need to synch the changes with the target as follows:
783
784 "host" -> "target": Send all (valid) conditions to the target.
785 "target" -> "host": Remove all the conditions from the target.
786 */
787
b775012e
LM
788 if (new_mode == condition_evaluation_target)
789 {
790 /* Mark everything modified and synch conditions with the
791 target. */
792 ALL_BP_LOCATIONS (loc, loc_tmp)
793 mark_breakpoint_location_modified (loc);
794 }
795 else
796 {
797 /* Manually mark non-duplicate locations to synch conditions
798 with the target. We do this to remove all the conditions the
799 target knows about. */
800 ALL_BP_LOCATIONS (loc, loc_tmp)
801 if (is_breakpoint (loc->owner) && loc->inserted)
802 loc->needs_update = 1;
803 }
804
805 /* Do the update. */
44702360 806 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
807 }
808
809 return;
810}
811
812/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
813 what "auto" is translating to. */
814
815static void
816show_condition_evaluation_mode (struct ui_file *file, int from_tty,
817 struct cmd_list_element *c, const char *value)
818{
819 if (condition_evaluation_mode == condition_evaluation_auto)
820 fprintf_filtered (file,
821 _("Breakpoint condition evaluation "
822 "mode is %s (currently %s).\n"),
823 value,
824 breakpoint_condition_evaluation_mode ());
825 else
826 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
827 value);
828}
829
830/* A comparison function for bp_location AP and BP that is used by
831 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 832 the more general bp_locations_compare function. */
b775012e
LM
833
834static int
f5336ca5 835bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 836{
9a3c8263
SM
837 const struct bp_location *a = *(const struct bp_location **) ap;
838 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
839
840 if (a->address == b->address)
841 return 0;
842 else
843 return ((a->address > b->address) - (a->address < b->address));
844}
845
846/* Helper function to skip all bp_locations with addresses
847 less than ADDRESS. It returns the first bp_location that
848 is greater than or equal to ADDRESS. If none is found, just
849 return NULL. */
850
851static struct bp_location **
852get_first_locp_gte_addr (CORE_ADDR address)
853{
854 struct bp_location dummy_loc;
855 struct bp_location *dummy_locp = &dummy_loc;
856 struct bp_location **locp_found = NULL;
857
858 /* Initialize the dummy location's address field. */
b775012e
LM
859 dummy_loc.address = address;
860
861 /* Find a close match to the first location at ADDRESS. */
9a3c8263 862 locp_found = ((struct bp_location **)
f5336ca5 863 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 864 sizeof (struct bp_location **),
f5336ca5 865 bp_locations_compare_addrs));
b775012e
LM
866
867 /* Nothing was found, nothing left to do. */
868 if (locp_found == NULL)
869 return NULL;
870
871 /* We may have found a location that is at ADDRESS but is not the first in the
872 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 873 while ((locp_found - 1) >= bp_locations
b775012e
LM
874 && (*(locp_found - 1))->address == address)
875 locp_found--;
876
877 return locp_found;
878}
879
adc36818 880void
7a26bd4d 881set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
882 int from_tty)
883{
3a5c3e22
PA
884 xfree (b->cond_string);
885 b->cond_string = NULL;
adc36818 886
3a5c3e22 887 if (is_watchpoint (b))
adc36818 888 {
3a5c3e22
PA
889 struct watchpoint *w = (struct watchpoint *) b;
890
4d01a485 891 w->cond_exp.reset ();
3a5c3e22
PA
892 }
893 else
894 {
895 struct bp_location *loc;
896
897 for (loc = b->loc; loc; loc = loc->next)
898 {
4d01a485 899 loc->cond.reset ();
b775012e
LM
900
901 /* No need to free the condition agent expression
902 bytecode (if we have one). We will handle this
903 when we go through update_global_location_list. */
3a5c3e22 904 }
adc36818 905 }
adc36818
PM
906
907 if (*exp == 0)
908 {
909 if (from_tty)
910 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
911 }
912 else
913 {
bbc13ae3 914 const char *arg = exp;
cc59ec59 915
adc36818
PM
916 /* I don't know if it matters whether this is the string the user
917 typed in or the decompiled expression. */
918 b->cond_string = xstrdup (arg);
919 b->condition_not_parsed = 0;
920
921 if (is_watchpoint (b))
922 {
3a5c3e22
PA
923 struct watchpoint *w = (struct watchpoint *) b;
924
adc36818
PM
925 innermost_block = NULL;
926 arg = exp;
1bb9788d 927 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
928 if (*arg)
929 error (_("Junk at end of expression"));
3a5c3e22 930 w->cond_exp_valid_block = innermost_block;
adc36818
PM
931 }
932 else
933 {
3a5c3e22
PA
934 struct bp_location *loc;
935
adc36818
PM
936 for (loc = b->loc; loc; loc = loc->next)
937 {
938 arg = exp;
939 loc->cond =
1bb9788d
TT
940 parse_exp_1 (&arg, loc->address,
941 block_for_pc (loc->address), 0);
adc36818
PM
942 if (*arg)
943 error (_("Junk at end of expression"));
944 }
945 }
946 }
b775012e
LM
947 mark_breakpoint_modified (b);
948
8d3788bd 949 observer_notify_breakpoint_modified (b);
adc36818
PM
950}
951
d55637df
TT
952/* Completion for the "condition" command. */
953
eb3ff9a5 954static void
6f937416 955condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 956 completion_tracker &tracker,
6f937416 957 const char *text, const char *word)
d55637df 958{
6f937416 959 const char *space;
d55637df 960
f1735a53
TT
961 text = skip_spaces (text);
962 space = skip_to_space (text);
d55637df
TT
963 if (*space == '\0')
964 {
965 int len;
966 struct breakpoint *b;
967 VEC (char_ptr) *result = NULL;
968
969 if (text[0] == '$')
970 {
971 /* We don't support completion of history indices. */
eb3ff9a5
PA
972 if (!isdigit (text[1]))
973 complete_internalvar (tracker, &text[1]);
974 return;
d55637df
TT
975 }
976
977 /* We're completing the breakpoint number. */
978 len = strlen (text);
979
980 ALL_BREAKPOINTS (b)
58ce7251
SDJ
981 {
982 char number[50];
983
984 xsnprintf (number, sizeof (number), "%d", b->number);
985
986 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
987 {
988 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
989 tracker.add_completion (std::move (copy));
990 }
58ce7251 991 }
d55637df 992
eb3ff9a5 993 return;
d55637df
TT
994 }
995
996 /* We're completing the expression part. */
f1735a53 997 text = skip_spaces (space);
eb3ff9a5 998 expression_completer (cmd, tracker, text, word);
d55637df
TT
999}
1000
c906108c
SS
1001/* condition N EXP -- set break condition of breakpoint N to EXP. */
1002
1003static void
fba45db2 1004condition_command (char *arg, int from_tty)
c906108c 1005{
52f0bd74 1006 struct breakpoint *b;
c906108c 1007 char *p;
52f0bd74 1008 int bnum;
c906108c
SS
1009
1010 if (arg == 0)
e2e0b3e5 1011 error_no_arg (_("breakpoint number"));
c906108c
SS
1012
1013 p = arg;
1014 bnum = get_number (&p);
5c44784c 1015 if (bnum == 0)
8a3fe4f8 1016 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1017
1018 ALL_BREAKPOINTS (b)
1019 if (b->number == bnum)
2f069f6f 1020 {
6dddc817
DE
1021 /* Check if this breakpoint has a "stop" method implemented in an
1022 extension language. This method and conditions entered into GDB
1023 from the CLI are mutually exclusive. */
1024 const struct extension_language_defn *extlang
1025 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1026
1027 if (extlang != NULL)
1028 {
1029 error (_("Only one stop condition allowed. There is currently"
1030 " a %s stop condition defined for this breakpoint."),
1031 ext_lang_capitalized_name (extlang));
1032 }
2566ad2d 1033 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1034
1035 if (is_breakpoint (b))
44702360 1036 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1037
2f069f6f
JB
1038 return;
1039 }
c906108c 1040
8a3fe4f8 1041 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1042}
1043
a7bdde9e
VP
1044/* Check that COMMAND do not contain commands that are suitable
1045 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1046 Throw if any such commands is found. */
1047
a7bdde9e
VP
1048static void
1049check_no_tracepoint_commands (struct command_line *commands)
1050{
1051 struct command_line *c;
cc59ec59 1052
a7bdde9e
VP
1053 for (c = commands; c; c = c->next)
1054 {
1055 int i;
1056
1057 if (c->control_type == while_stepping_control)
3e43a32a
MS
1058 error (_("The 'while-stepping' command can "
1059 "only be used for tracepoints"));
a7bdde9e
VP
1060
1061 for (i = 0; i < c->body_count; ++i)
1062 check_no_tracepoint_commands ((c->body_list)[i]);
1063
1064 /* Not that command parsing removes leading whitespace and comment
4a64f543 1065 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1066 command directly. */
1067 if (strstr (c->line, "collect ") == c->line)
1068 error (_("The 'collect' command can only be used for tracepoints"));
1069
51661e93
VP
1070 if (strstr (c->line, "teval ") == c->line)
1071 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1072 }
1073}
1074
c1fc2657 1075struct longjmp_breakpoint : public breakpoint
3b0871f4 1076{
c1fc2657 1077 ~longjmp_breakpoint () override;
3b0871f4
SM
1078};
1079
d77f58be
SS
1080/* Encapsulate tests for different types of tracepoints. */
1081
3b0871f4
SM
1082static bool
1083is_tracepoint_type (bptype type)
d9b3f62e
PA
1084{
1085 return (type == bp_tracepoint
1086 || type == bp_fast_tracepoint
1087 || type == bp_static_tracepoint);
1088}
1089
3b0871f4
SM
1090static bool
1091is_longjmp_type (bptype type)
1092{
1093 return type == bp_longjmp || type == bp_exception;
1094}
1095
a7bdde9e 1096int
d77f58be 1097is_tracepoint (const struct breakpoint *b)
a7bdde9e 1098{
d9b3f62e 1099 return is_tracepoint_type (b->type);
a7bdde9e 1100}
d9b3f62e 1101
a5e364af
SM
1102/* Factory function to create an appropriate instance of breakpoint given
1103 TYPE. */
1104
1105static std::unique_ptr<breakpoint>
1106new_breakpoint_from_type (bptype type)
1107{
1108 breakpoint *b;
1109
1110 if (is_tracepoint_type (type))
c1fc2657 1111 b = new tracepoint ();
3b0871f4 1112 else if (is_longjmp_type (type))
c1fc2657 1113 b = new longjmp_breakpoint ();
a5e364af
SM
1114 else
1115 b = new breakpoint ();
1116
1117 return std::unique_ptr<breakpoint> (b);
1118}
1119
e5dd4106 1120/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1121 breakpoint. This function will throw an exception if a problem is
1122 found. */
48cb2d85 1123
95a42b64
TT
1124static void
1125validate_commands_for_breakpoint (struct breakpoint *b,
1126 struct command_line *commands)
48cb2d85 1127{
d77f58be 1128 if (is_tracepoint (b))
a7bdde9e 1129 {
c9a6ce02 1130 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1131 struct command_line *c;
1132 struct command_line *while_stepping = 0;
c9a6ce02
PA
1133
1134 /* Reset the while-stepping step count. The previous commands
1135 might have included a while-stepping action, while the new
1136 ones might not. */
1137 t->step_count = 0;
1138
1139 /* We need to verify that each top-level element of commands is
1140 valid for tracepoints, that there's at most one
1141 while-stepping element, and that the while-stepping's body
1142 has valid tracing commands excluding nested while-stepping.
1143 We also need to validate the tracepoint action line in the
1144 context of the tracepoint --- validate_actionline actually
1145 has side effects, like setting the tracepoint's
1146 while-stepping STEP_COUNT, in addition to checking if the
1147 collect/teval actions parse and make sense in the
1148 tracepoint's context. */
a7bdde9e
VP
1149 for (c = commands; c; c = c->next)
1150 {
a7bdde9e
VP
1151 if (c->control_type == while_stepping_control)
1152 {
1153 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1154 error (_("The 'while-stepping' command "
1155 "cannot be used for fast tracepoint"));
0fb4aa4b 1156 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1157 error (_("The 'while-stepping' command "
1158 "cannot be used for static tracepoint"));
a7bdde9e
VP
1159
1160 if (while_stepping)
3e43a32a
MS
1161 error (_("The 'while-stepping' command "
1162 "can be used only once"));
a7bdde9e
VP
1163 else
1164 while_stepping = c;
1165 }
c9a6ce02
PA
1166
1167 validate_actionline (c->line, b);
a7bdde9e
VP
1168 }
1169 if (while_stepping)
1170 {
1171 struct command_line *c2;
1172
1173 gdb_assert (while_stepping->body_count == 1);
1174 c2 = while_stepping->body_list[0];
1175 for (; c2; c2 = c2->next)
1176 {
a7bdde9e
VP
1177 if (c2->control_type == while_stepping_control)
1178 error (_("The 'while-stepping' command cannot be nested"));
1179 }
1180 }
1181 }
1182 else
1183 {
1184 check_no_tracepoint_commands (commands);
1185 }
95a42b64
TT
1186}
1187
0fb4aa4b
PA
1188/* Return a vector of all the static tracepoints set at ADDR. The
1189 caller is responsible for releasing the vector. */
1190
1191VEC(breakpoint_p) *
1192static_tracepoints_here (CORE_ADDR addr)
1193{
1194 struct breakpoint *b;
1195 VEC(breakpoint_p) *found = 0;
1196 struct bp_location *loc;
1197
1198 ALL_BREAKPOINTS (b)
1199 if (b->type == bp_static_tracepoint)
1200 {
1201 for (loc = b->loc; loc; loc = loc->next)
1202 if (loc->address == addr)
1203 VEC_safe_push(breakpoint_p, found, b);
1204 }
1205
1206 return found;
1207}
1208
95a42b64 1209/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1210 validate that only allowed commands are included. */
95a42b64
TT
1211
1212void
4a64f543 1213breakpoint_set_commands (struct breakpoint *b,
93921405 1214 command_line_up &&commands)
95a42b64 1215{
93921405 1216 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1217
d1b0a7bf 1218 b->commands = std::move (commands);
8d3788bd 1219 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1220}
1221
45a43567
TT
1222/* Set the internal `silent' flag on the breakpoint. Note that this
1223 is not the same as the "silent" that may appear in the breakpoint's
1224 commands. */
1225
1226void
1227breakpoint_set_silent (struct breakpoint *b, int silent)
1228{
1229 int old_silent = b->silent;
1230
1231 b->silent = silent;
1232 if (old_silent != silent)
8d3788bd 1233 observer_notify_breakpoint_modified (b);
45a43567
TT
1234}
1235
1236/* Set the thread for this breakpoint. If THREAD is -1, make the
1237 breakpoint work for any thread. */
1238
1239void
1240breakpoint_set_thread (struct breakpoint *b, int thread)
1241{
1242 int old_thread = b->thread;
1243
1244 b->thread = thread;
1245 if (old_thread != thread)
8d3788bd 1246 observer_notify_breakpoint_modified (b);
45a43567
TT
1247}
1248
1249/* Set the task for this breakpoint. If TASK is 0, make the
1250 breakpoint work for any task. */
1251
1252void
1253breakpoint_set_task (struct breakpoint *b, int task)
1254{
1255 int old_task = b->task;
1256
1257 b->task = task;
1258 if (old_task != task)
8d3788bd 1259 observer_notify_breakpoint_modified (b);
45a43567
TT
1260}
1261
95a42b64
TT
1262void
1263check_tracepoint_command (char *line, void *closure)
a7bdde9e 1264{
9a3c8263 1265 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1266
6f937416 1267 validate_actionline (line, b);
a7bdde9e
VP
1268}
1269
95a42b64 1270static void
896b6bda 1271commands_command_1 (const char *arg, int from_tty,
4a64f543 1272 struct command_line *control)
95a42b64 1273{
d1b0a7bf 1274 counted_command_line cmd;
95a42b64 1275
896b6bda
PA
1276 std::string new_arg;
1277
95a42b64
TT
1278 if (arg == NULL || !*arg)
1279 {
86b17b60 1280 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1281 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1282 breakpoint_count);
95a42b64 1283 else if (breakpoint_count > 0)
896b6bda 1284 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1285 arg = new_arg.c_str ();
1286 }
1287
1288 map_breakpoint_numbers
1289 (arg, [&] (breakpoint *b)
1290 {
1291 if (cmd == NULL)
1292 {
48649e1b 1293 if (control != NULL)
d1b0a7bf 1294 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1295 else
1296 {
1297 struct cleanup *old_chain;
1298 char *str;
1299
1300 str = xstrprintf (_("Type commands for breakpoint(s) "
1301 "%s, one per line."),
1302 arg);
1303
1304 old_chain = make_cleanup (xfree, str);
1305
d1b0a7bf
TT
1306 cmd = read_command_lines (str,
1307 from_tty, 1,
1308 (is_tracepoint (b)
1309 ? check_tracepoint_command : 0),
1310 b);
48649e1b
TT
1311
1312 do_cleanups (old_chain);
1313 }
48649e1b
TT
1314 }
1315
1316 /* If a breakpoint was on the list more than once, we don't need to
1317 do anything. */
1318 if (b->commands != cmd)
1319 {
d1b0a7bf 1320 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1321 b->commands = cmd;
1322 observer_notify_breakpoint_modified (b);
1323 }
1324 });
95a42b64 1325
48649e1b 1326 if (cmd == NULL)
95a42b64 1327 error (_("No breakpoints specified."));
95a42b64
TT
1328}
1329
1330static void
1331commands_command (char *arg, int from_tty)
1332{
1333 commands_command_1 (arg, from_tty, NULL);
c906108c 1334}
40c03ae8
EZ
1335
1336/* Like commands_command, but instead of reading the commands from
1337 input stream, takes them from an already parsed command structure.
1338
1339 This is used by cli-script.c to DTRT with breakpoint commands
1340 that are part of if and while bodies. */
1341enum command_control_type
896b6bda 1342commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1343{
95a42b64
TT
1344 commands_command_1 (arg, 0, cmd);
1345 return simple_control;
40c03ae8 1346}
876fa593
JK
1347
1348/* Return non-zero if BL->TARGET_INFO contains valid information. */
1349
1350static int
1351bp_location_has_shadow (struct bp_location *bl)
1352{
1353 if (bl->loc_type != bp_loc_software_breakpoint)
1354 return 0;
1355 if (!bl->inserted)
1356 return 0;
1357 if (bl->target_info.shadow_len == 0)
e5dd4106 1358 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1359 return 0;
1360 return 1;
1361}
1362
9d497a19
PA
1363/* Update BUF, which is LEN bytes read from the target address
1364 MEMADDR, by replacing a memory breakpoint with its shadowed
1365 contents.
1366
1367 If READBUF is not NULL, this buffer must not overlap with the of
1368 the breakpoint location's shadow_contents buffer. Otherwise, a
1369 failed assertion internal error will be raised. */
1370
1371static void
1372one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1373 const gdb_byte *writebuf_org,
1374 ULONGEST memaddr, LONGEST len,
1375 struct bp_target_info *target_info,
1376 struct gdbarch *gdbarch)
1377{
1378 /* Now do full processing of the found relevant range of elements. */
1379 CORE_ADDR bp_addr = 0;
1380 int bp_size = 0;
1381 int bptoffset = 0;
1382
1383 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1384 current_program_space->aspace, 0))
1385 {
1386 /* The breakpoint is inserted in a different address space. */
1387 return;
1388 }
1389
1390 /* Addresses and length of the part of the breakpoint that
1391 we need to copy. */
1392 bp_addr = target_info->placed_address;
1393 bp_size = target_info->shadow_len;
1394
1395 if (bp_addr + bp_size <= memaddr)
1396 {
1397 /* The breakpoint is entirely before the chunk of memory we are
1398 reading. */
1399 return;
1400 }
1401
1402 if (bp_addr >= memaddr + len)
1403 {
1404 /* The breakpoint is entirely after the chunk of memory we are
1405 reading. */
1406 return;
1407 }
1408
1409 /* Offset within shadow_contents. */
1410 if (bp_addr < memaddr)
1411 {
1412 /* Only copy the second part of the breakpoint. */
1413 bp_size -= memaddr - bp_addr;
1414 bptoffset = memaddr - bp_addr;
1415 bp_addr = memaddr;
1416 }
1417
1418 if (bp_addr + bp_size > memaddr + len)
1419 {
1420 /* Only copy the first part of the breakpoint. */
1421 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1422 }
1423
1424 if (readbuf != NULL)
1425 {
1426 /* Verify that the readbuf buffer does not overlap with the
1427 shadow_contents buffer. */
1428 gdb_assert (target_info->shadow_contents >= readbuf + len
1429 || readbuf >= (target_info->shadow_contents
1430 + target_info->shadow_len));
1431
1432 /* Update the read buffer with this inserted breakpoint's
1433 shadow. */
1434 memcpy (readbuf + bp_addr - memaddr,
1435 target_info->shadow_contents + bptoffset, bp_size);
1436 }
1437 else
1438 {
1439 const unsigned char *bp;
0d5ed153
MR
1440 CORE_ADDR addr = target_info->reqstd_address;
1441 int placed_size;
9d497a19
PA
1442
1443 /* Update the shadow with what we want to write to memory. */
1444 memcpy (target_info->shadow_contents + bptoffset,
1445 writebuf_org + bp_addr - memaddr, bp_size);
1446
1447 /* Determine appropriate breakpoint contents and size for this
1448 address. */
0d5ed153 1449 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1450
1451 /* Update the final write buffer with this inserted
1452 breakpoint's INSN. */
1453 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1454 }
1455}
1456
8defab1a 1457/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1458 by replacing any memory breakpoints with their shadowed contents.
1459
35c63cd8
JB
1460 If READBUF is not NULL, this buffer must not overlap with any of
1461 the breakpoint location's shadow_contents buffers. Otherwise,
1462 a failed assertion internal error will be raised.
1463
876fa593 1464 The range of shadowed area by each bp_location is:
f5336ca5
PA
1465 bl->address - bp_locations_placed_address_before_address_max
1466 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1467 The range we were requested to resolve shadows for is:
1468 memaddr ... memaddr + len
1469 Thus the safe cutoff boundaries for performance optimization are
35df4500 1470 memaddr + len <= (bl->address
f5336ca5 1471 - bp_locations_placed_address_before_address_max)
876fa593 1472 and:
f5336ca5 1473 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1474
8defab1a 1475void
f0ba3972
PA
1476breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1477 const gdb_byte *writebuf_org,
1478 ULONGEST memaddr, LONGEST len)
c906108c 1479{
4a64f543
MS
1480 /* Left boundary, right boundary and median element of our binary
1481 search. */
876fa593
JK
1482 unsigned bc_l, bc_r, bc;
1483
4a64f543
MS
1484 /* Find BC_L which is a leftmost element which may affect BUF
1485 content. It is safe to report lower value but a failure to
1486 report higher one. */
876fa593
JK
1487
1488 bc_l = 0;
f5336ca5 1489 bc_r = bp_locations_count;
876fa593
JK
1490 while (bc_l + 1 < bc_r)
1491 {
35df4500 1492 struct bp_location *bl;
876fa593
JK
1493
1494 bc = (bc_l + bc_r) / 2;
f5336ca5 1495 bl = bp_locations[bc];
876fa593 1496
4a64f543
MS
1497 /* Check first BL->ADDRESS will not overflow due to the added
1498 constant. Then advance the left boundary only if we are sure
1499 the BC element can in no way affect the BUF content (MEMADDR
1500 to MEMADDR + LEN range).
876fa593 1501
f5336ca5 1502 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1503 offset so that we cannot miss a breakpoint with its shadow
1504 range tail still reaching MEMADDR. */
c5aa993b 1505
f5336ca5 1506 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1507 >= bl->address)
f5336ca5 1508 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1509 <= memaddr))
876fa593
JK
1510 bc_l = bc;
1511 else
1512 bc_r = bc;
1513 }
1514
128070bb
PA
1515 /* Due to the binary search above, we need to make sure we pick the
1516 first location that's at BC_L's address. E.g., if there are
1517 multiple locations at the same address, BC_L may end up pointing
1518 at a duplicate location, and miss the "master"/"inserted"
1519 location. Say, given locations L1, L2 and L3 at addresses A and
1520 B:
1521
1522 L1@A, L2@A, L3@B, ...
1523
1524 BC_L could end up pointing at location L2, while the "master"
1525 location could be L1. Since the `loc->inserted' flag is only set
1526 on "master" locations, we'd forget to restore the shadow of L1
1527 and L2. */
1528 while (bc_l > 0
f5336ca5 1529 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1530 bc_l--;
1531
876fa593
JK
1532 /* Now do full processing of the found relevant range of elements. */
1533
f5336ca5 1534 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1535 {
f5336ca5 1536 struct bp_location *bl = bp_locations[bc];
876fa593 1537
35df4500
TJB
1538 /* bp_location array has BL->OWNER always non-NULL. */
1539 if (bl->owner->type == bp_none)
8a3fe4f8 1540 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1541 bl->owner->number);
ffce0d52 1542
e5dd4106 1543 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1544 content. */
1545
f5336ca5
PA
1546 if (bl->address >= bp_locations_placed_address_before_address_max
1547 && memaddr + len <= (bl->address
1548 - bp_locations_placed_address_before_address_max))
876fa593
JK
1549 break;
1550
35df4500 1551 if (!bp_location_has_shadow (bl))
c5aa993b 1552 continue;
6c95b8df 1553
9d497a19
PA
1554 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1555 memaddr, len, &bl->target_info, bl->gdbarch);
1556 }
c906108c 1557}
9d497a19 1558
c906108c 1559\f
c5aa993b 1560
b775012e
LM
1561/* Return true if BPT is either a software breakpoint or a hardware
1562 breakpoint. */
1563
1564int
1565is_breakpoint (const struct breakpoint *bpt)
1566{
1567 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1568 || bpt->type == bp_hardware_breakpoint
1569 || bpt->type == bp_dprintf);
b775012e
LM
1570}
1571
60e1c644
PA
1572/* Return true if BPT is of any hardware watchpoint kind. */
1573
a5606eee 1574static int
d77f58be 1575is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1576{
1577 return (bpt->type == bp_hardware_watchpoint
1578 || bpt->type == bp_read_watchpoint
1579 || bpt->type == bp_access_watchpoint);
1580}
7270d8f2 1581
60e1c644
PA
1582/* Return true if BPT is of any watchpoint kind, hardware or
1583 software. */
1584
3a5c3e22 1585int
d77f58be 1586is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1587{
1588 return (is_hardware_watchpoint (bpt)
1589 || bpt->type == bp_watchpoint);
1590}
1591
3a5c3e22
PA
1592/* Returns true if the current thread and its running state are safe
1593 to evaluate or update watchpoint B. Watchpoints on local
1594 expressions need to be evaluated in the context of the thread that
1595 was current when the watchpoint was created, and, that thread needs
1596 to be stopped to be able to select the correct frame context.
1597 Watchpoints on global expressions can be evaluated on any thread,
1598 and in any state. It is presently left to the target allowing
1599 memory accesses when threads are running. */
f6bc2008
PA
1600
1601static int
3a5c3e22 1602watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1603{
c1fc2657 1604 return (b->pspace == current_program_space
d0d8b0c6
JK
1605 && (ptid_equal (b->watchpoint_thread, null_ptid)
1606 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1607 && !is_executing (inferior_ptid))));
f6bc2008
PA
1608}
1609
d0fb5eae
JK
1610/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1611 associated bp_watchpoint_scope breakpoint. */
1612
1613static void
3a5c3e22 1614watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1615{
c1fc2657 1616 if (w->related_breakpoint != w)
d0fb5eae 1617 {
c1fc2657
SM
1618 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1619 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1620 w->related_breakpoint->disposition = disp_del_at_next_stop;
1621 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1622 w->related_breakpoint = w;
d0fb5eae 1623 }
c1fc2657 1624 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1625}
1626
bb9d5f81
PP
1627/* Extract a bitfield value from value VAL using the bit parameters contained in
1628 watchpoint W. */
1629
1630static struct value *
1631extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1632{
1633 struct value *bit_val;
1634
1635 if (val == NULL)
1636 return NULL;
1637
1638 bit_val = allocate_value (value_type (val));
1639
1640 unpack_value_bitfield (bit_val,
1641 w->val_bitpos,
1642 w->val_bitsize,
1643 value_contents_for_printing (val),
1644 value_offset (val),
1645 val);
1646
1647 return bit_val;
1648}
1649
c6d81124
PA
1650/* Allocate a dummy location and add it to B, which must be a software
1651 watchpoint. This is required because even if a software watchpoint
1652 is not watching any memory, bpstat_stop_status requires a location
1653 to be able to report stops. */
1654
1655static void
1656software_watchpoint_add_no_memory_location (struct breakpoint *b,
1657 struct program_space *pspace)
1658{
1659 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1660
1661 b->loc = allocate_bp_location (b);
1662 b->loc->pspace = pspace;
1663 b->loc->address = -1;
1664 b->loc->length = -1;
1665}
1666
1667/* Returns true if B is a software watchpoint that is not watching any
1668 memory (e.g., "watch $pc"). */
1669
1670static int
1671is_no_memory_software_watchpoint (struct breakpoint *b)
1672{
1673 return (b->type == bp_watchpoint
1674 && b->loc != NULL
1675 && b->loc->next == NULL
1676 && b->loc->address == -1
1677 && b->loc->length == -1);
1678}
1679
567e1b4e
JB
1680/* Assuming that B is a watchpoint:
1681 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1682 - Evaluate expression and store the result in B->val
567e1b4e
JB
1683 - Evaluate the condition if there is one, and store the result
1684 in b->loc->cond.
a5606eee
VP
1685 - Update the list of values that must be watched in B->loc.
1686
4a64f543
MS
1687 If the watchpoint disposition is disp_del_at_next_stop, then do
1688 nothing. If this is local watchpoint that is out of scope, delete
1689 it.
1690
1691 Even with `set breakpoint always-inserted on' the watchpoints are
1692 removed + inserted on each stop here. Normal breakpoints must
1693 never be removed because they might be missed by a running thread
1694 when debugging in non-stop mode. On the other hand, hardware
1695 watchpoints (is_hardware_watchpoint; processed here) are specific
1696 to each LWP since they are stored in each LWP's hardware debug
1697 registers. Therefore, such LWP must be stopped first in order to
1698 be able to modify its hardware watchpoints.
1699
1700 Hardware watchpoints must be reset exactly once after being
1701 presented to the user. It cannot be done sooner, because it would
1702 reset the data used to present the watchpoint hit to the user. And
1703 it must not be done later because it could display the same single
1704 watchpoint hit during multiple GDB stops. Note that the latter is
1705 relevant only to the hardware watchpoint types bp_read_watchpoint
1706 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1707 not user-visible - its hit is suppressed if the memory content has
1708 not changed.
1709
1710 The following constraints influence the location where we can reset
1711 hardware watchpoints:
1712
1713 * target_stopped_by_watchpoint and target_stopped_data_address are
1714 called several times when GDB stops.
1715
1716 [linux]
1717 * Multiple hardware watchpoints can be hit at the same time,
1718 causing GDB to stop. GDB only presents one hardware watchpoint
1719 hit at a time as the reason for stopping, and all the other hits
1720 are presented later, one after the other, each time the user
1721 requests the execution to be resumed. Execution is not resumed
1722 for the threads still having pending hit event stored in
1723 LWP_INFO->STATUS. While the watchpoint is already removed from
1724 the inferior on the first stop the thread hit event is kept being
1725 reported from its cached value by linux_nat_stopped_data_address
1726 until the real thread resume happens after the watchpoint gets
1727 presented and thus its LWP_INFO->STATUS gets reset.
1728
1729 Therefore the hardware watchpoint hit can get safely reset on the
1730 watchpoint removal from inferior. */
a79d3c27 1731
b40ce68a 1732static void
3a5c3e22 1733update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1734{
a5606eee 1735 int within_current_scope;
a5606eee 1736 struct frame_id saved_frame_id;
66076460 1737 int frame_saved;
a5606eee 1738
f6bc2008
PA
1739 /* If this is a local watchpoint, we only want to check if the
1740 watchpoint frame is in scope if the current thread is the thread
1741 that was used to create the watchpoint. */
1742 if (!watchpoint_in_thread_scope (b))
1743 return;
1744
c1fc2657 1745 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1746 return;
1747
66076460 1748 frame_saved = 0;
a5606eee
VP
1749
1750 /* Determine if the watchpoint is within scope. */
1751 if (b->exp_valid_block == NULL)
1752 within_current_scope = 1;
1753 else
1754 {
b5db5dfc
UW
1755 struct frame_info *fi = get_current_frame ();
1756 struct gdbarch *frame_arch = get_frame_arch (fi);
1757 CORE_ADDR frame_pc = get_frame_pc (fi);
1758
c9cf6e20
MG
1759 /* If we're at a point where the stack has been destroyed
1760 (e.g. in a function epilogue), unwinding may not work
1761 properly. Do not attempt to recreate locations at this
b5db5dfc 1762 point. See similar comments in watchpoint_check. */
c9cf6e20 1763 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1764 return;
66076460
DJ
1765
1766 /* Save the current frame's ID so we can restore it after
1767 evaluating the watchpoint expression on its own frame. */
1768 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1769 took a frame parameter, so that we didn't have to change the
1770 selected frame. */
1771 frame_saved = 1;
1772 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1773
a5606eee
VP
1774 fi = frame_find_by_id (b->watchpoint_frame);
1775 within_current_scope = (fi != NULL);
1776 if (within_current_scope)
1777 select_frame (fi);
1778 }
1779
b5db5dfc
UW
1780 /* We don't free locations. They are stored in the bp_location array
1781 and update_global_location_list will eventually delete them and
1782 remove breakpoints if needed. */
c1fc2657 1783 b->loc = NULL;
b5db5dfc 1784
a5606eee
VP
1785 if (within_current_scope && reparse)
1786 {
bbc13ae3 1787 const char *s;
d63d0675 1788
4d01a485 1789 b->exp.reset ();
d63d0675 1790 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1791 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1792 /* If the meaning of expression itself changed, the old value is
1793 no longer relevant. We don't want to report a watchpoint hit
1794 to the user when the old value and the new value may actually
1795 be completely different objects. */
1796 value_free (b->val);
fa4727a6
DJ
1797 b->val = NULL;
1798 b->val_valid = 0;
60e1c644
PA
1799
1800 /* Note that unlike with breakpoints, the watchpoint's condition
1801 expression is stored in the breakpoint object, not in the
1802 locations (re)created below. */
c1fc2657 1803 if (b->cond_string != NULL)
60e1c644 1804 {
4d01a485 1805 b->cond_exp.reset ();
60e1c644 1806
c1fc2657 1807 s = b->cond_string;
1bb9788d 1808 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1809 }
a5606eee 1810 }
a5606eee
VP
1811
1812 /* If we failed to parse the expression, for example because
1813 it refers to a global variable in a not-yet-loaded shared library,
1814 don't try to insert watchpoint. We don't automatically delete
1815 such watchpoint, though, since failure to parse expression
1816 is different from out-of-scope watchpoint. */
e8369a73 1817 if (!target_has_execution)
2d134ed3
PA
1818 {
1819 /* Without execution, memory can't change. No use to try and
1820 set watchpoint locations. The watchpoint will be reset when
1821 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1822 if (!can_use_hw_watchpoints)
1823 {
c1fc2657
SM
1824 if (b->ops->works_in_software_mode (b))
1825 b->type = bp_watchpoint;
e8369a73 1826 else
638aa5a1
AB
1827 error (_("Can't set read/access watchpoint when "
1828 "hardware watchpoints are disabled."));
e8369a73 1829 }
2d134ed3
PA
1830 }
1831 else if (within_current_scope && b->exp)
a5606eee 1832 {
0cf6dd15 1833 int pc = 0;
fa4727a6 1834 struct value *val_chain, *v, *result, *next;
2d134ed3 1835 struct program_space *frame_pspace;
a5606eee 1836
4d01a485 1837 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1838
a5606eee
VP
1839 /* Avoid setting b->val if it's already set. The meaning of
1840 b->val is 'the last value' user saw, and we should update
1841 it only if we reported that last value to user. As it
9c06b0b4
TJB
1842 happens, the code that reports it updates b->val directly.
1843 We don't keep track of the memory value for masked
1844 watchpoints. */
c1fc2657 1845 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1846 {
bb9d5f81
PP
1847 if (b->val_bitsize != 0)
1848 {
1849 v = extract_bitfield_from_watchpoint_value (b, v);
1850 if (v != NULL)
1851 release_value (v);
1852 }
fa4727a6
DJ
1853 b->val = v;
1854 b->val_valid = 1;
1855 }
a5606eee 1856
2d134ed3
PA
1857 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1858
a5606eee 1859 /* Look at each value on the value chain. */
9fa40276 1860 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1861 {
1862 /* If it's a memory location, and GDB actually needed
1863 its contents to evaluate the expression, then we
fa4727a6
DJ
1864 must watch it. If the first value returned is
1865 still lazy, that means an error occurred reading it;
1866 watch it anyway in case it becomes readable. */
a5606eee 1867 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1868 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1869 {
1870 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1871
a5606eee
VP
1872 /* We only watch structs and arrays if user asked
1873 for it explicitly, never if they just happen to
1874 appear in the middle of some value chain. */
fa4727a6 1875 if (v == result
a5606eee
VP
1876 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1877 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1878 {
1879 CORE_ADDR addr;
f486487f 1880 enum target_hw_bp_type type;
a5606eee 1881 struct bp_location *loc, **tmp;
bb9d5f81
PP
1882 int bitpos = 0, bitsize = 0;
1883
1884 if (value_bitsize (v) != 0)
1885 {
1886 /* Extract the bit parameters out from the bitfield
1887 sub-expression. */
1888 bitpos = value_bitpos (v);
1889 bitsize = value_bitsize (v);
1890 }
1891 else if (v == result && b->val_bitsize != 0)
1892 {
1893 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1894 lvalue whose bit parameters are saved in the fields
1895 VAL_BITPOS and VAL_BITSIZE. */
1896 bitpos = b->val_bitpos;
1897 bitsize = b->val_bitsize;
1898 }
a5606eee 1899
42ae5230 1900 addr = value_address (v);
bb9d5f81
PP
1901 if (bitsize != 0)
1902 {
1903 /* Skip the bytes that don't contain the bitfield. */
1904 addr += bitpos / 8;
1905 }
1906
a5606eee 1907 type = hw_write;
c1fc2657 1908 if (b->type == bp_read_watchpoint)
a5606eee 1909 type = hw_read;
c1fc2657 1910 else if (b->type == bp_access_watchpoint)
a5606eee 1911 type = hw_access;
3a5c3e22 1912
c1fc2657
SM
1913 loc = allocate_bp_location (b);
1914 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1915 ;
1916 *tmp = loc;
a6d9a66e 1917 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1918
1919 loc->pspace = frame_pspace;
a5606eee 1920 loc->address = addr;
bb9d5f81
PP
1921
1922 if (bitsize != 0)
1923 {
1924 /* Just cover the bytes that make up the bitfield. */
1925 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1926 }
1927 else
1928 loc->length = TYPE_LENGTH (value_type (v));
1929
a5606eee
VP
1930 loc->watchpoint_type = type;
1931 }
1932 }
9fa40276
TJB
1933 }
1934
1935 /* Change the type of breakpoint between hardware assisted or
1936 an ordinary watchpoint depending on the hardware support
1937 and free hardware slots. REPARSE is set when the inferior
1938 is started. */
a9634178 1939 if (reparse)
9fa40276 1940 {
e09342b5 1941 int reg_cnt;
9fa40276
TJB
1942 enum bp_loc_type loc_type;
1943 struct bp_location *bl;
a5606eee 1944
a9634178 1945 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1946
1947 if (reg_cnt)
9fa40276
TJB
1948 {
1949 int i, target_resources_ok, other_type_used;
a1398e0c 1950 enum bptype type;
9fa40276 1951
a9634178
TJB
1952 /* Use an exact watchpoint when there's only one memory region to be
1953 watched, and only one debug register is needed to watch it. */
1954 b->exact = target_exact_watchpoints && reg_cnt == 1;
1955
9fa40276 1956 /* We need to determine how many resources are already
e09342b5
TJB
1957 used for all other hardware watchpoints plus this one
1958 to see if we still have enough resources to also fit
a1398e0c
PA
1959 this watchpoint in as well. */
1960
1961 /* If this is a software watchpoint, we try to turn it
1962 to a hardware one -- count resources as if B was of
1963 hardware watchpoint type. */
c1fc2657 1964 type = b->type;
a1398e0c
PA
1965 if (type == bp_watchpoint)
1966 type = bp_hardware_watchpoint;
1967
1968 /* This watchpoint may or may not have been placed on
1969 the list yet at this point (it won't be in the list
1970 if we're trying to create it for the first time,
1971 through watch_command), so always account for it
1972 manually. */
1973
1974 /* Count resources used by all watchpoints except B. */
c1fc2657 1975 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1976
1977 /* Add in the resources needed for B. */
c1fc2657 1978 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1979
1980 target_resources_ok
1981 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1982 if (target_resources_ok <= 0)
a9634178 1983 {
c1fc2657 1984 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1985
1986 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1987 error (_("Target does not support this type of "
1988 "hardware watchpoint."));
9c06b0b4
TJB
1989 else if (target_resources_ok < 0 && !sw_mode)
1990 error (_("There are not enough available hardware "
1991 "resources for this watchpoint."));
a1398e0c
PA
1992
1993 /* Downgrade to software watchpoint. */
c1fc2657 1994 b->type = bp_watchpoint;
a1398e0c
PA
1995 }
1996 else
1997 {
1998 /* If this was a software watchpoint, we've just
1999 found we have enough resources to turn it to a
2000 hardware watchpoint. Otherwise, this is a
2001 nop. */
c1fc2657 2002 b->type = type;
a9634178 2003 }
9fa40276 2004 }
c1fc2657 2005 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2006 {
2007 if (!can_use_hw_watchpoints)
2008 error (_("Can't set read/access watchpoint when "
2009 "hardware watchpoints are disabled."));
2010 else
2011 error (_("Expression cannot be implemented with "
2012 "read/access watchpoint."));
2013 }
9fa40276 2014 else
c1fc2657 2015 b->type = bp_watchpoint;
9fa40276 2016
c1fc2657 2017 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2018 : bp_loc_hardware_watchpoint);
c1fc2657 2019 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2020 bl->loc_type = loc_type;
2021 }
2022
2023 for (v = val_chain; v; v = next)
2024 {
a5606eee
VP
2025 next = value_next (v);
2026 if (v != b->val)
2027 value_free (v);
2028 }
2029
c7437ca6
PA
2030 /* If a software watchpoint is not watching any memory, then the
2031 above left it without any location set up. But,
2032 bpstat_stop_status requires a location to be able to report
2033 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2034 if (b->type == bp_watchpoint && b->loc == NULL)
2035 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2036 }
2037 else if (!within_current_scope)
7270d8f2 2038 {
ac74f770
MS
2039 printf_filtered (_("\
2040Watchpoint %d deleted because the program has left the block\n\
2041in which its expression is valid.\n"),
c1fc2657 2042 b->number);
d0fb5eae 2043 watchpoint_del_at_next_stop (b);
7270d8f2 2044 }
a5606eee
VP
2045
2046 /* Restore the selected frame. */
66076460
DJ
2047 if (frame_saved)
2048 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2049}
2050
a5606eee 2051
74960c60 2052/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2053 inserted in the inferior. We don't differentiate the type of BL's owner
2054 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2055 breakpoint_ops is not defined, because in insert_bp_location,
2056 tracepoint's insert_location will not be called. */
74960c60 2057static int
35df4500 2058should_be_inserted (struct bp_location *bl)
74960c60 2059{
35df4500 2060 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2061 return 0;
2062
35df4500 2063 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2064 return 0;
2065
35df4500 2066 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2067 return 0;
2068
f8eba3c6
TT
2069 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2070 return 0;
2071
56710373
PA
2072 /* This is set for example, when we're attached to the parent of a
2073 vfork, and have detached from the child. The child is running
2074 free, and we expect it to do an exec or exit, at which point the
2075 OS makes the parent schedulable again (and the target reports
2076 that the vfork is done). Until the child is done with the shared
2077 memory region, do not insert breakpoints in the parent, otherwise
2078 the child could still trip on the parent's breakpoints. Since
2079 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2080 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2081 return 0;
2082
31e77af2 2083 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2084 location, except if the breakpoint is a single-step breakpoint,
2085 and the breakpoint's thread is the thread which is stepping past
2086 a breakpoint. */
31e77af2
PA
2087 if ((bl->loc_type == bp_loc_software_breakpoint
2088 || bl->loc_type == bp_loc_hardware_breakpoint)
2089 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2090 bl->address)
2091 /* The single-step breakpoint may be inserted at the location
2092 we're trying to step if the instruction branches to itself.
2093 However, the instruction won't be executed at all and it may
2094 break the semantics of the instruction, for example, the
2095 instruction is a conditional branch or updates some flags.
2096 We can't fix it unless GDB is able to emulate the instruction
2097 or switch to displaced stepping. */
2098 && !(bl->owner->type == bp_single_step
2099 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2100 {
2101 if (debug_infrun)
2102 {
2103 fprintf_unfiltered (gdb_stdlog,
2104 "infrun: skipping breakpoint: "
2105 "stepping past insn at: %s\n",
2106 paddress (bl->gdbarch, bl->address));
2107 }
2108 return 0;
2109 }
31e77af2 2110
963f9c80
PA
2111 /* Don't insert watchpoints if we're trying to step past the
2112 instruction that triggered one. */
2113 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2114 && stepping_past_nonsteppable_watchpoint ())
2115 {
2116 if (debug_infrun)
2117 {
2118 fprintf_unfiltered (gdb_stdlog,
2119 "infrun: stepping past non-steppable watchpoint. "
2120 "skipping watchpoint at %s:%d\n",
2121 paddress (bl->gdbarch, bl->address),
2122 bl->length);
2123 }
2124 return 0;
2125 }
2126
74960c60
VP
2127 return 1;
2128}
2129
934709f0
PW
2130/* Same as should_be_inserted but does the check assuming
2131 that the location is not duplicated. */
2132
2133static int
2134unduplicated_should_be_inserted (struct bp_location *bl)
2135{
2136 int result;
2137 const int save_duplicate = bl->duplicate;
2138
2139 bl->duplicate = 0;
2140 result = should_be_inserted (bl);
2141 bl->duplicate = save_duplicate;
2142 return result;
2143}
2144
b775012e
LM
2145/* Parses a conditional described by an expression COND into an
2146 agent expression bytecode suitable for evaluation
2147 by the bytecode interpreter. Return NULL if there was
2148 any error during parsing. */
2149
833177a4 2150static agent_expr_up
b775012e
LM
2151parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2152{
833177a4 2153 if (cond == NULL)
b775012e
LM
2154 return NULL;
2155
833177a4
PA
2156 agent_expr_up aexpr;
2157
b775012e
LM
2158 /* We don't want to stop processing, so catch any errors
2159 that may show up. */
492d29ea 2160 TRY
b775012e 2161 {
036e657b 2162 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2163 }
2164
492d29ea 2165 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2166 {
2167 /* If we got here, it means the condition could not be parsed to a valid
2168 bytecode expression and thus can't be evaluated on the target's side.
2169 It's no use iterating through the conditions. */
b775012e 2170 }
492d29ea 2171 END_CATCH
b775012e
LM
2172
2173 /* We have a valid agent expression. */
2174 return aexpr;
2175}
2176
2177/* Based on location BL, create a list of breakpoint conditions to be
2178 passed on to the target. If we have duplicated locations with different
2179 conditions, we will add such conditions to the list. The idea is that the
2180 target will evaluate the list of conditions and will only notify GDB when
2181 one of them is true. */
2182
2183static void
2184build_target_condition_list (struct bp_location *bl)
2185{
2186 struct bp_location **locp = NULL, **loc2p;
2187 int null_condition_or_parse_error = 0;
2188 int modified = bl->needs_update;
2189 struct bp_location *loc;
2190
8b4f3082 2191 /* Release conditions left over from a previous insert. */
3cde5c42 2192 bl->target_info.conditions.clear ();
8b4f3082 2193
b775012e
LM
2194 /* This is only meaningful if the target is
2195 evaluating conditions and if the user has
2196 opted for condition evaluation on the target's
2197 side. */
2198 if (gdb_evaluates_breakpoint_condition_p ()
2199 || !target_supports_evaluation_of_breakpoint_conditions ())
2200 return;
2201
2202 /* Do a first pass to check for locations with no assigned
2203 conditions or conditions that fail to parse to a valid agent expression
2204 bytecode. If any of these happen, then it's no use to send conditions
2205 to the target since this location will always trigger and generate a
2206 response back to GDB. */
2207 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2208 {
2209 loc = (*loc2p);
2210 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2211 {
2212 if (modified)
2213 {
b775012e
LM
2214 /* Re-parse the conditions since something changed. In that
2215 case we already freed the condition bytecodes (see
2216 force_breakpoint_reinsertion). We just
2217 need to parse the condition to bytecodes again. */
833177a4
PA
2218 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2219 loc->cond.get ());
b775012e
LM
2220 }
2221
2222 /* If we have a NULL bytecode expression, it means something
2223 went wrong or we have a null condition expression. */
2224 if (!loc->cond_bytecode)
2225 {
2226 null_condition_or_parse_error = 1;
2227 break;
2228 }
2229 }
2230 }
2231
2232 /* If any of these happened, it means we will have to evaluate the conditions
2233 for the location's address on gdb's side. It is no use keeping bytecodes
2234 for all the other duplicate locations, thus we free all of them here.
2235
2236 This is so we have a finer control over which locations' conditions are
2237 being evaluated by GDB or the remote stub. */
2238 if (null_condition_or_parse_error)
2239 {
2240 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2241 {
2242 loc = (*loc2p);
2243 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2244 {
2245 /* Only go as far as the first NULL bytecode is
2246 located. */
2247 if (!loc->cond_bytecode)
2248 return;
2249
833177a4 2250 loc->cond_bytecode.reset ();
b775012e
LM
2251 }
2252 }
2253 }
2254
2255 /* No NULL conditions or failed bytecode generation. Build a condition list
2256 for this location's address. */
2257 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2258 {
2259 loc = (*loc2p);
2260 if (loc->cond
2261 && is_breakpoint (loc->owner)
2262 && loc->pspace->num == bl->pspace->num
2263 && loc->owner->enable_state == bp_enabled
2264 && loc->enabled)
3cde5c42
PA
2265 {
2266 /* Add the condition to the vector. This will be used later
2267 to send the conditions to the target. */
2268 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2269 }
b775012e
LM
2270 }
2271
2272 return;
2273}
2274
d3ce09f5
SS
2275/* Parses a command described by string CMD into an agent expression
2276 bytecode suitable for evaluation by the bytecode interpreter.
2277 Return NULL if there was any error during parsing. */
2278
833177a4 2279static agent_expr_up
d3ce09f5
SS
2280parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2281{
2282 struct cleanup *old_cleanups = 0;
4d01a485 2283 struct expression **argvec;
bbc13ae3
KS
2284 const char *cmdrest;
2285 const char *format_start, *format_end;
d3ce09f5
SS
2286 struct format_piece *fpieces;
2287 int nargs;
2288 struct gdbarch *gdbarch = get_current_arch ();
2289
833177a4 2290 if (cmd == NULL)
d3ce09f5
SS
2291 return NULL;
2292
2293 cmdrest = cmd;
2294
2295 if (*cmdrest == ',')
2296 ++cmdrest;
f1735a53 2297 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2298
2299 if (*cmdrest++ != '"')
2300 error (_("No format string following the location"));
2301
2302 format_start = cmdrest;
2303
2304 fpieces = parse_format_string (&cmdrest);
2305
2306 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2307
2308 format_end = cmdrest;
2309
2310 if (*cmdrest++ != '"')
2311 error (_("Bad format string, non-terminated '\"'."));
2312
f1735a53 2313 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2314
2315 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2316 error (_("Invalid argument syntax"));
2317
2318 if (*cmdrest == ',')
2319 cmdrest++;
f1735a53 2320 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2321
2322 /* For each argument, make an expression. */
2323
2324 argvec = (struct expression **) alloca (strlen (cmd)
2325 * sizeof (struct expression *));
2326
2327 nargs = 0;
2328 while (*cmdrest != '\0')
2329 {
bbc13ae3 2330 const char *cmd1;
d3ce09f5
SS
2331
2332 cmd1 = cmdrest;
4d01a485
PA
2333 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2334 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2335 cmdrest = cmd1;
2336 if (*cmdrest == ',')
2337 ++cmdrest;
2338 }
2339
833177a4
PA
2340 agent_expr_up aexpr;
2341
d3ce09f5
SS
2342 /* We don't want to stop processing, so catch any errors
2343 that may show up. */
492d29ea 2344 TRY
d3ce09f5 2345 {
036e657b
JB
2346 aexpr = gen_printf (scope, gdbarch, 0, 0,
2347 format_start, format_end - format_start,
2348 fpieces, nargs, argvec);
d3ce09f5 2349 }
492d29ea 2350 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2351 {
2352 /* If we got here, it means the command could not be parsed to a valid
2353 bytecode expression and thus can't be evaluated on the target's side.
2354 It's no use iterating through the other commands. */
d3ce09f5 2355 }
492d29ea
PA
2356 END_CATCH
2357
2358 do_cleanups (old_cleanups);
d3ce09f5 2359
d3ce09f5
SS
2360 /* We have a valid agent expression, return it. */
2361 return aexpr;
2362}
2363
2364/* Based on location BL, create a list of breakpoint commands to be
2365 passed on to the target. If we have duplicated locations with
2366 different commands, we will add any such to the list. */
2367
2368static void
2369build_target_command_list (struct bp_location *bl)
2370{
2371 struct bp_location **locp = NULL, **loc2p;
2372 int null_command_or_parse_error = 0;
2373 int modified = bl->needs_update;
2374 struct bp_location *loc;
2375
3cde5c42
PA
2376 /* Clear commands left over from a previous insert. */
2377 bl->target_info.tcommands.clear ();
8b4f3082 2378
41fac0cf 2379 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2380 return;
2381
41fac0cf
PA
2382 /* For now, limit to agent-style dprintf breakpoints. */
2383 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2384 return;
2385
41fac0cf
PA
2386 /* For now, if we have any duplicate location that isn't a dprintf,
2387 don't install the target-side commands, as that would make the
2388 breakpoint not be reported to the core, and we'd lose
2389 control. */
2390 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2391 {
2392 loc = (*loc2p);
2393 if (is_breakpoint (loc->owner)
2394 && loc->pspace->num == bl->pspace->num
2395 && loc->owner->type != bp_dprintf)
2396 return;
2397 }
2398
d3ce09f5
SS
2399 /* Do a first pass to check for locations with no assigned
2400 conditions or conditions that fail to parse to a valid agent expression
2401 bytecode. If any of these happen, then it's no use to send conditions
2402 to the target since this location will always trigger and generate a
2403 response back to GDB. */
2404 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2405 {
2406 loc = (*loc2p);
2407 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2408 {
2409 if (modified)
2410 {
d3ce09f5
SS
2411 /* Re-parse the commands since something changed. In that
2412 case we already freed the command bytecodes (see
2413 force_breakpoint_reinsertion). We just
2414 need to parse the command to bytecodes again. */
833177a4
PA
2415 loc->cmd_bytecode
2416 = parse_cmd_to_aexpr (bl->address,
2417 loc->owner->extra_string);
d3ce09f5
SS
2418 }
2419
2420 /* If we have a NULL bytecode expression, it means something
2421 went wrong or we have a null command expression. */
2422 if (!loc->cmd_bytecode)
2423 {
2424 null_command_or_parse_error = 1;
2425 break;
2426 }
2427 }
2428 }
2429
2430 /* If anything failed, then we're not doing target-side commands,
2431 and so clean up. */
2432 if (null_command_or_parse_error)
2433 {
2434 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2435 {
2436 loc = (*loc2p);
2437 if (is_breakpoint (loc->owner)
2438 && loc->pspace->num == bl->pspace->num)
2439 {
2440 /* Only go as far as the first NULL bytecode is
2441 located. */
40fb6c5e 2442 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2443 return;
2444
833177a4 2445 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2446 }
2447 }
2448 }
2449
2450 /* No NULL commands or failed bytecode generation. Build a command list
2451 for this location's address. */
2452 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2453 {
2454 loc = (*loc2p);
2455 if (loc->owner->extra_string
2456 && is_breakpoint (loc->owner)
2457 && loc->pspace->num == bl->pspace->num
2458 && loc->owner->enable_state == bp_enabled
2459 && loc->enabled)
3cde5c42
PA
2460 {
2461 /* Add the command to the vector. This will be used later
2462 to send the commands to the target. */
2463 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2464 }
d3ce09f5
SS
2465 }
2466
2467 bl->target_info.persist = 0;
2468 /* Maybe flag this location as persistent. */
2469 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2470 bl->target_info.persist = 1;
2471}
2472
833b7ab5
YQ
2473/* Return the kind of breakpoint on address *ADDR. Get the kind
2474 of breakpoint according to ADDR except single-step breakpoint.
2475 Get the kind of single-step breakpoint according to the current
2476 registers state. */
cd6c3b4f
YQ
2477
2478static int
2479breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2480{
833b7ab5
YQ
2481 if (bl->owner->type == bp_single_step)
2482 {
2483 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2484 struct regcache *regcache;
2485
2486 regcache = get_thread_regcache (thr->ptid);
2487
2488 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2489 regcache, addr);
2490 }
2491 else
2492 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2493}
2494
35df4500
TJB
2495/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2496 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2497 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2498 Returns 0 for success, 1 if the bp_location type is not supported or
2499 -1 for failure.
879bfdc2 2500
4a64f543
MS
2501 NOTE drow/2003-09-09: This routine could be broken down to an
2502 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2503static int
35df4500 2504insert_bp_location (struct bp_location *bl,
26bb91f3 2505 struct ui_file *tmp_error_stream,
3fbb6ffa 2506 int *disabled_breaks,
dd61ec5c
MW
2507 int *hw_breakpoint_error,
2508 int *hw_bp_error_explained_already)
879bfdc2 2509{
0000e5cc
PA
2510 enum errors bp_err = GDB_NO_ERROR;
2511 const char *bp_err_message = NULL;
879bfdc2 2512
b775012e 2513 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2514 return 0;
2515
35c63cd8
JB
2516 /* Note we don't initialize bl->target_info, as that wipes out
2517 the breakpoint location's shadow_contents if the breakpoint
2518 is still inserted at that location. This in turn breaks
2519 target_read_memory which depends on these buffers when
2520 a memory read is requested at the breakpoint location:
2521 Once the target_info has been wiped, we fail to see that
2522 we have a breakpoint inserted at that address and thus
2523 read the breakpoint instead of returning the data saved in
2524 the breakpoint location's shadow contents. */
0d5ed153 2525 bl->target_info.reqstd_address = bl->address;
35df4500 2526 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2527 bl->target_info.length = bl->length;
8181d85f 2528
b775012e
LM
2529 /* When working with target-side conditions, we must pass all the conditions
2530 for the same breakpoint address down to the target since GDB will not
2531 insert those locations. With a list of breakpoint conditions, the target
2532 can decide when to stop and notify GDB. */
2533
2534 if (is_breakpoint (bl->owner))
2535 {
2536 build_target_condition_list (bl);
d3ce09f5
SS
2537 build_target_command_list (bl);
2538 /* Reset the modification marker. */
b775012e
LM
2539 bl->needs_update = 0;
2540 }
2541
35df4500
TJB
2542 if (bl->loc_type == bp_loc_software_breakpoint
2543 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2544 {
35df4500 2545 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2546 {
2547 /* If the explicitly specified breakpoint type
2548 is not hardware breakpoint, check the memory map to see
2549 if the breakpoint address is in read only memory or not.
4a64f543 2550
765dc015
VP
2551 Two important cases are:
2552 - location type is not hardware breakpoint, memory
2553 is readonly. We change the type of the location to
2554 hardware breakpoint.
4a64f543
MS
2555 - location type is hardware breakpoint, memory is
2556 read-write. This means we've previously made the
2557 location hardware one, but then the memory map changed,
2558 so we undo.
765dc015 2559
4a64f543
MS
2560 When breakpoints are removed, remove_breakpoints will use
2561 location types we've just set here, the only possible
2562 problem is that memory map has changed during running
2563 program, but it's not going to work anyway with current
2564 gdb. */
765dc015 2565 struct mem_region *mr
0d5ed153 2566 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2567
2568 if (mr)
2569 {
2570 if (automatic_hardware_breakpoints)
2571 {
765dc015
VP
2572 enum bp_loc_type new_type;
2573
2574 if (mr->attrib.mode != MEM_RW)
2575 new_type = bp_loc_hardware_breakpoint;
2576 else
2577 new_type = bp_loc_software_breakpoint;
2578
35df4500 2579 if (new_type != bl->loc_type)
765dc015
VP
2580 {
2581 static int said = 0;
cc59ec59 2582
35df4500 2583 bl->loc_type = new_type;
765dc015
VP
2584 if (!said)
2585 {
3e43a32a
MS
2586 fprintf_filtered (gdb_stdout,
2587 _("Note: automatically using "
2588 "hardware breakpoints for "
2589 "read-only addresses.\n"));
765dc015
VP
2590 said = 1;
2591 }
2592 }
2593 }
35df4500 2594 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2595 && mr->attrib.mode != MEM_RW)
2596 {
2597 fprintf_unfiltered (tmp_error_stream,
2598 _("Cannot insert breakpoint %d.\n"
2599 "Cannot set software breakpoint "
2600 "at read-only address %s\n"),
2601 bl->owner->number,
2602 paddress (bl->gdbarch, bl->address));
2603 return 1;
2604 }
765dc015
VP
2605 }
2606 }
2607
879bfdc2
DJ
2608 /* First check to see if we have to handle an overlay. */
2609 if (overlay_debugging == ovly_off
35df4500
TJB
2610 || bl->section == NULL
2611 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2612 {
2613 /* No overlay handling: just set the breakpoint. */
492d29ea 2614 TRY
dd61ec5c 2615 {
0000e5cc
PA
2616 int val;
2617
dd61ec5c 2618 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2619 if (val)
2620 bp_err = GENERIC_ERROR;
dd61ec5c 2621 }
492d29ea 2622 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2623 {
0000e5cc
PA
2624 bp_err = e.error;
2625 bp_err_message = e.message;
dd61ec5c 2626 }
492d29ea 2627 END_CATCH
879bfdc2
DJ
2628 }
2629 else
2630 {
4a64f543 2631 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2632 Shall we set a breakpoint at the LMA? */
2633 if (!overlay_events_enabled)
2634 {
2635 /* Yes -- overlay event support is not active,
2636 so we must try to set a breakpoint at the LMA.
2637 This will not work for a hardware breakpoint. */
35df4500 2638 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2639 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2640 bl->owner->number);
879bfdc2
DJ
2641 else
2642 {
35df4500
TJB
2643 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2644 bl->section);
879bfdc2 2645 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2646 bl->overlay_target_info = bl->target_info;
0d5ed153 2647 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2648
2649 /* No overlay handling: just set the breakpoint. */
492d29ea 2650 TRY
0000e5cc
PA
2651 {
2652 int val;
2653
579c6ad9 2654 bl->overlay_target_info.kind
cd6c3b4f
YQ
2655 = breakpoint_kind (bl, &addr);
2656 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2657 val = target_insert_breakpoint (bl->gdbarch,
2658 &bl->overlay_target_info);
2659 if (val)
2660 bp_err = GENERIC_ERROR;
2661 }
492d29ea 2662 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2663 {
2664 bp_err = e.error;
2665 bp_err_message = e.message;
2666 }
492d29ea 2667 END_CATCH
0000e5cc
PA
2668
2669 if (bp_err != GDB_NO_ERROR)
99361f52 2670 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2671 "Overlay breakpoint %d "
2672 "failed: in ROM?\n",
35df4500 2673 bl->owner->number);
879bfdc2
DJ
2674 }
2675 }
2676 /* Shall we set a breakpoint at the VMA? */
35df4500 2677 if (section_is_mapped (bl->section))
879bfdc2
DJ
2678 {
2679 /* Yes. This overlay section is mapped into memory. */
492d29ea 2680 TRY
dd61ec5c 2681 {
0000e5cc
PA
2682 int val;
2683
dd61ec5c 2684 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2685 if (val)
2686 bp_err = GENERIC_ERROR;
dd61ec5c 2687 }
492d29ea 2688 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2689 {
0000e5cc
PA
2690 bp_err = e.error;
2691 bp_err_message = e.message;
dd61ec5c 2692 }
492d29ea 2693 END_CATCH
879bfdc2
DJ
2694 }
2695 else
2696 {
2697 /* No. This breakpoint will not be inserted.
2698 No error, but do not mark the bp as 'inserted'. */
2699 return 0;
2700 }
2701 }
2702
0000e5cc 2703 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2704 {
2705 /* Can't set the breakpoint. */
0000e5cc
PA
2706
2707 /* In some cases, we might not be able to insert a
2708 breakpoint in a shared library that has already been
2709 removed, but we have not yet processed the shlib unload
2710 event. Unfortunately, some targets that implement
076855f9
PA
2711 breakpoint insertion themselves can't tell why the
2712 breakpoint insertion failed (e.g., the remote target
2713 doesn't define error codes), so we must treat generic
2714 errors as memory errors. */
0000e5cc 2715 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2716 && bl->loc_type == bp_loc_software_breakpoint
08351840 2717 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2718 || shared_objfile_contains_address_p (bl->pspace,
2719 bl->address)))
879bfdc2 2720 {
4a64f543 2721 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2722 bl->shlib_disabled = 1;
8d3788bd 2723 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2724 if (!*disabled_breaks)
2725 {
2726 fprintf_unfiltered (tmp_error_stream,
2727 "Cannot insert breakpoint %d.\n",
2728 bl->owner->number);
2729 fprintf_unfiltered (tmp_error_stream,
2730 "Temporarily disabling shared "
2731 "library breakpoints:\n");
2732 }
2733 *disabled_breaks = 1;
879bfdc2 2734 fprintf_unfiltered (tmp_error_stream,
35df4500 2735 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2736 return 0;
879bfdc2
DJ
2737 }
2738 else
879bfdc2 2739 {
35df4500 2740 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2741 {
0000e5cc
PA
2742 *hw_breakpoint_error = 1;
2743 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2744 fprintf_unfiltered (tmp_error_stream,
2745 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2746 bl->owner->number, bp_err_message ? ":" : ".\n");
2747 if (bp_err_message != NULL)
2748 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2749 }
2750 else
2751 {
0000e5cc
PA
2752 if (bp_err_message == NULL)
2753 {
1ccbe998 2754 std::string message
0000e5cc
PA
2755 = memory_error_message (TARGET_XFER_E_IO,
2756 bl->gdbarch, bl->address);
0000e5cc
PA
2757
2758 fprintf_unfiltered (tmp_error_stream,
2759 "Cannot insert breakpoint %d.\n"
2760 "%s\n",
1ccbe998 2761 bl->owner->number, message.c_str ());
0000e5cc
PA
2762 }
2763 else
2764 {
2765 fprintf_unfiltered (tmp_error_stream,
2766 "Cannot insert breakpoint %d: %s\n",
2767 bl->owner->number,
2768 bp_err_message);
2769 }
879bfdc2 2770 }
0000e5cc 2771 return 1;
879bfdc2
DJ
2772
2773 }
2774 }
2775 else
35df4500 2776 bl->inserted = 1;
879bfdc2 2777
0000e5cc 2778 return 0;
879bfdc2
DJ
2779 }
2780
35df4500 2781 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2782 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2783 watchpoints. It's not clear that it's necessary... */
35df4500 2784 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2785 {
0000e5cc
PA
2786 int val;
2787
77b06cd7
TJB
2788 gdb_assert (bl->owner->ops != NULL
2789 && bl->owner->ops->insert_location != NULL);
2790
2791 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2792
2793 /* If trying to set a read-watchpoint, and it turns out it's not
2794 supported, try emulating one with an access watchpoint. */
35df4500 2795 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2796 {
2797 struct bp_location *loc, **loc_temp;
2798
2799 /* But don't try to insert it, if there's already another
2800 hw_access location that would be considered a duplicate
2801 of this one. */
2802 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2803 if (loc != bl
85d721b8 2804 && loc->watchpoint_type == hw_access
35df4500 2805 && watchpoint_locations_match (bl, loc))
85d721b8 2806 {
35df4500
TJB
2807 bl->duplicate = 1;
2808 bl->inserted = 1;
2809 bl->target_info = loc->target_info;
2810 bl->watchpoint_type = hw_access;
85d721b8
PA
2811 val = 0;
2812 break;
2813 }
2814
2815 if (val == 1)
2816 {
77b06cd7
TJB
2817 bl->watchpoint_type = hw_access;
2818 val = bl->owner->ops->insert_location (bl);
2819
2820 if (val)
2821 /* Back to the original value. */
2822 bl->watchpoint_type = hw_read;
85d721b8
PA
2823 }
2824 }
2825
35df4500 2826 bl->inserted = (val == 0);
879bfdc2
DJ
2827 }
2828
35df4500 2829 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2830 {
0000e5cc
PA
2831 int val;
2832
77b06cd7
TJB
2833 gdb_assert (bl->owner->ops != NULL
2834 && bl->owner->ops->insert_location != NULL);
2835
2836 val = bl->owner->ops->insert_location (bl);
2837 if (val)
2838 {
2839 bl->owner->enable_state = bp_disabled;
2840
2841 if (val == 1)
2842 warning (_("\
2843Error inserting catchpoint %d: Your system does not support this type\n\
2844of catchpoint."), bl->owner->number);
2845 else
2846 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2847 }
2848
2849 bl->inserted = (val == 0);
1640b821
DJ
2850
2851 /* We've already printed an error message if there was a problem
2852 inserting this catchpoint, and we've disabled the catchpoint,
2853 so just return success. */
2854 return 0;
879bfdc2
DJ
2855 }
2856
2857 return 0;
2858}
2859
6c95b8df
PA
2860/* This function is called when program space PSPACE is about to be
2861 deleted. It takes care of updating breakpoints to not reference
2862 PSPACE anymore. */
2863
2864void
2865breakpoint_program_space_exit (struct program_space *pspace)
2866{
2867 struct breakpoint *b, *b_temp;
876fa593 2868 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2869
2870 /* Remove any breakpoint that was set through this program space. */
2871 ALL_BREAKPOINTS_SAFE (b, b_temp)
2872 {
2873 if (b->pspace == pspace)
2874 delete_breakpoint (b);
2875 }
2876
2877 /* Breakpoints set through other program spaces could have locations
2878 bound to PSPACE as well. Remove those. */
876fa593 2879 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2880 {
2881 struct bp_location *tmp;
2882
2883 if (loc->pspace == pspace)
2884 {
2bdf28a0 2885 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2886 if (loc->owner->loc == loc)
2887 loc->owner->loc = loc->next;
2888 else
2889 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2890 if (tmp->next == loc)
2891 {
2892 tmp->next = loc->next;
2893 break;
2894 }
2895 }
2896 }
2897
2898 /* Now update the global location list to permanently delete the
2899 removed locations above. */
44702360 2900 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2901}
2902
74960c60
VP
2903/* Make sure all breakpoints are inserted in inferior.
2904 Throws exception on any error.
2905 A breakpoint that is already inserted won't be inserted
2906 again, so calling this function twice is safe. */
2907void
2908insert_breakpoints (void)
2909{
2910 struct breakpoint *bpt;
2911
2912 ALL_BREAKPOINTS (bpt)
2913 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2914 {
2915 struct watchpoint *w = (struct watchpoint *) bpt;
2916
2917 update_watchpoint (w, 0 /* don't reparse. */);
2918 }
74960c60 2919
04086b45
PA
2920 /* Updating watchpoints creates new locations, so update the global
2921 location list. Explicitly tell ugll to insert locations and
2922 ignore breakpoints_always_inserted_mode. */
2923 update_global_location_list (UGLL_INSERT);
74960c60
VP
2924}
2925
20388dd6
YQ
2926/* Invoke CALLBACK for each of bp_location. */
2927
2928void
2929iterate_over_bp_locations (walk_bp_location_callback callback)
2930{
2931 struct bp_location *loc, **loc_tmp;
2932
2933 ALL_BP_LOCATIONS (loc, loc_tmp)
2934 {
2935 callback (loc, NULL);
2936 }
2937}
2938
b775012e
LM
2939/* This is used when we need to synch breakpoint conditions between GDB and the
2940 target. It is the case with deleting and disabling of breakpoints when using
2941 always-inserted mode. */
2942
2943static void
2944update_inserted_breakpoint_locations (void)
2945{
2946 struct bp_location *bl, **blp_tmp;
2947 int error_flag = 0;
2948 int val = 0;
2949 int disabled_breaks = 0;
2950 int hw_breakpoint_error = 0;
dd61ec5c 2951 int hw_bp_details_reported = 0;
b775012e 2952
d7e74731 2953 string_file tmp_error_stream;
b775012e
LM
2954
2955 /* Explicitly mark the warning -- this will only be printed if
2956 there was an error. */
d7e74731 2957 tmp_error_stream.puts ("Warning:\n");
b775012e 2958
5ed8105e 2959 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2960
2961 ALL_BP_LOCATIONS (bl, blp_tmp)
2962 {
2963 /* We only want to update software breakpoints and hardware
2964 breakpoints. */
2965 if (!is_breakpoint (bl->owner))
2966 continue;
2967
2968 /* We only want to update locations that are already inserted
2969 and need updating. This is to avoid unwanted insertion during
2970 deletion of breakpoints. */
2971 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2972 continue;
2973
2974 switch_to_program_space_and_thread (bl->pspace);
2975
2976 /* For targets that support global breakpoints, there's no need
2977 to select an inferior to insert breakpoint to. In fact, even
2978 if we aren't attached to any process yet, we should still
2979 insert breakpoints. */
f5656ead 2980 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2981 && ptid_equal (inferior_ptid, null_ptid))
2982 continue;
2983
d7e74731 2984 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2985 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2986 if (val)
2987 error_flag = val;
2988 }
2989
2990 if (error_flag)
2991 {
223ffa71 2992 target_terminal::ours_for_output ();
b775012e
LM
2993 error_stream (tmp_error_stream);
2994 }
b775012e
LM
2995}
2996
c30eee59 2997/* Used when starting or continuing the program. */
c906108c 2998
74960c60
VP
2999static void
3000insert_breakpoint_locations (void)
c906108c 3001{
a5606eee 3002 struct breakpoint *bpt;
35df4500 3003 struct bp_location *bl, **blp_tmp;
eacd795a 3004 int error_flag = 0;
c906108c 3005 int val = 0;
3fbb6ffa 3006 int disabled_breaks = 0;
81d0cc19 3007 int hw_breakpoint_error = 0;
dd61ec5c 3008 int hw_bp_error_explained_already = 0;
c906108c 3009
d7e74731
PA
3010 string_file tmp_error_stream;
3011
81d0cc19
GS
3012 /* Explicitly mark the warning -- this will only be printed if
3013 there was an error. */
d7e74731 3014 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3015
5ed8105e 3016 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3017
35df4500 3018 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3019 {
b775012e 3020 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3021 continue;
3022
4a64f543
MS
3023 /* There is no point inserting thread-specific breakpoints if
3024 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3025 has BL->OWNER always non-NULL. */
35df4500 3026 if (bl->owner->thread != -1
5d5658a1 3027 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3028 continue;
3029
35df4500 3030 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3031
3032 /* For targets that support global breakpoints, there's no need
3033 to select an inferior to insert breakpoint to. In fact, even
3034 if we aren't attached to any process yet, we should still
3035 insert breakpoints. */
f5656ead 3036 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3037 && ptid_equal (inferior_ptid, null_ptid))
3038 continue;
3039
d7e74731 3040 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3041 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3042 if (val)
eacd795a 3043 error_flag = val;
879bfdc2 3044 }
c906108c 3045
4a64f543
MS
3046 /* If we failed to insert all locations of a watchpoint, remove
3047 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3048 ALL_BREAKPOINTS (bpt)
3049 {
3050 int some_failed = 0;
3051 struct bp_location *loc;
3052
3053 if (!is_hardware_watchpoint (bpt))
3054 continue;
3055
d6b74ac4 3056 if (!breakpoint_enabled (bpt))
a5606eee 3057 continue;
74960c60
VP
3058
3059 if (bpt->disposition == disp_del_at_next_stop)
3060 continue;
a5606eee
VP
3061
3062 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3063 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3064 {
3065 some_failed = 1;
3066 break;
3067 }
3068 if (some_failed)
3069 {
3070 for (loc = bpt->loc; loc; loc = loc->next)
3071 if (loc->inserted)
834c0d03 3072 remove_breakpoint (loc);
a5606eee
VP
3073
3074 hw_breakpoint_error = 1;
d7e74731
PA
3075 tmp_error_stream.printf ("Could not insert "
3076 "hardware watchpoint %d.\n",
3077 bpt->number);
eacd795a 3078 error_flag = -1;
a5606eee
VP
3079 }
3080 }
3081
eacd795a 3082 if (error_flag)
81d0cc19
GS
3083 {
3084 /* If a hardware breakpoint or watchpoint was inserted, add a
3085 message about possibly exhausted resources. */
dd61ec5c 3086 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3087 {
d7e74731 3088 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3089You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3090 }
223ffa71 3091 target_terminal::ours_for_output ();
81d0cc19
GS
3092 error_stream (tmp_error_stream);
3093 }
c906108c
SS
3094}
3095
c30eee59
TJB
3096/* Used when the program stops.
3097 Returns zero if successful, or non-zero if there was a problem
3098 removing a breakpoint location. */
3099
c906108c 3100int
fba45db2 3101remove_breakpoints (void)
c906108c 3102{
35df4500 3103 struct bp_location *bl, **blp_tmp;
3a1bae8e 3104 int val = 0;
c906108c 3105
35df4500 3106 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3107 {
1e4d1764 3108 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3109 val |= remove_breakpoint (bl);
c5aa993b 3110 }
3a1bae8e 3111 return val;
c906108c
SS
3112}
3113
49fa26b0
PA
3114/* When a thread exits, remove breakpoints that are related to
3115 that thread. */
3116
3117static void
3118remove_threaded_breakpoints (struct thread_info *tp, int silent)
3119{
3120 struct breakpoint *b, *b_tmp;
3121
3122 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3123 {
5d5658a1 3124 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3125 {
3126 b->disposition = disp_del_at_next_stop;
3127
3128 printf_filtered (_("\
43792cf0
PA
3129Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3130 b->number, print_thread_id (tp));
49fa26b0
PA
3131
3132 /* Hide it from the user. */
3133 b->number = 0;
3134 }
3135 }
3136}
3137
6c95b8df
PA
3138/* Remove breakpoints of process PID. */
3139
3140int
3141remove_breakpoints_pid (int pid)
3142{
35df4500 3143 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3144 int val;
3145 struct inferior *inf = find_inferior_pid (pid);
3146
35df4500 3147 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3148 {
35df4500 3149 if (bl->pspace != inf->pspace)
6c95b8df
PA
3150 continue;
3151
fc126975 3152 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3153 {
834c0d03 3154 val = remove_breakpoint (bl);
6c95b8df
PA
3155 if (val != 0)
3156 return val;
3157 }
3158 }
3159 return 0;
3160}
3161
c906108c 3162int
fba45db2 3163reattach_breakpoints (int pid)
c906108c 3164{
35df4500 3165 struct bp_location *bl, **blp_tmp;
c906108c 3166 int val;
dd61ec5c 3167 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3168 struct inferior *inf;
3169 struct thread_info *tp;
3170
3171 tp = any_live_thread_of_process (pid);
3172 if (tp == NULL)
3173 return 1;
3174
3175 inf = find_inferior_pid (pid);
6c95b8df 3176
2989a365 3177 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3178 inferior_ptid = tp->ptid;
a4954f26 3179
d7e74731 3180 string_file tmp_error_stream;
c906108c 3181
35df4500 3182 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3183 {
35df4500 3184 if (bl->pspace != inf->pspace)
6c95b8df
PA
3185 continue;
3186
35df4500 3187 if (bl->inserted)
c5aa993b 3188 {
35df4500 3189 bl->inserted = 0;
d7e74731 3190 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b 3191 if (val != 0)
2989a365 3192 return val;
c5aa993b
JM
3193 }
3194 }
c906108c
SS
3195 return 0;
3196}
3197
e58b0e63
PA
3198static int internal_breakpoint_number = -1;
3199
84f4c1fe
PM
3200/* Set the breakpoint number of B, depending on the value of INTERNAL.
3201 If INTERNAL is non-zero, the breakpoint number will be populated
3202 from internal_breakpoint_number and that variable decremented.
e5dd4106 3203 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3204 breakpoint_count and that value incremented. Internal breakpoints
3205 do not set the internal var bpnum. */
3206static void
3207set_breakpoint_number (int internal, struct breakpoint *b)
3208{
3209 if (internal)
3210 b->number = internal_breakpoint_number--;
3211 else
3212 {
3213 set_breakpoint_count (breakpoint_count + 1);
3214 b->number = breakpoint_count;
3215 }
3216}
3217
e62c965a 3218static struct breakpoint *
a6d9a66e 3219create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3220 CORE_ADDR address, enum bptype type,
c0a91b2b 3221 const struct breakpoint_ops *ops)
e62c965a 3222{
51abb421 3223 symtab_and_line sal;
e62c965a
PP
3224 sal.pc = address;
3225 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3226 sal.pspace = current_program_space;
e62c965a 3227
51abb421 3228 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3229 b->number = internal_breakpoint_number--;
3230 b->disposition = disp_donttouch;
3231
3232 return b;
3233}
3234
17450429
PP
3235static const char *const longjmp_names[] =
3236 {
3237 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3238 };
3239#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3240
3241/* Per-objfile data private to breakpoint.c. */
3242struct breakpoint_objfile_data
3243{
3244 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3245 struct bound_minimal_symbol overlay_msym;
17450429
PP
3246
3247 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3248 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3249
28106bc2
SDJ
3250 /* True if we have looked for longjmp probes. */
3251 int longjmp_searched;
3252
3253 /* SystemTap probe points for longjmp (if any). */
3254 VEC (probe_p) *longjmp_probes;
3255
17450429 3256 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3257 struct bound_minimal_symbol terminate_msym;
17450429
PP
3258
3259 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3260 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3261
3262 /* True if we have looked for exception probes. */
3263 int exception_searched;
3264
3265 /* SystemTap probe points for unwinding (if any). */
3266 VEC (probe_p) *exception_probes;
17450429
PP
3267};
3268
3269static const struct objfile_data *breakpoint_objfile_key;
3270
3271/* Minimal symbol not found sentinel. */
3272static struct minimal_symbol msym_not_found;
3273
3274/* Returns TRUE if MSYM point to the "not found" sentinel. */
3275
3276static int
3277msym_not_found_p (const struct minimal_symbol *msym)
3278{
3279 return msym == &msym_not_found;
3280}
3281
3282/* Return per-objfile data needed by breakpoint.c.
3283 Allocate the data if necessary. */
3284
3285static struct breakpoint_objfile_data *
3286get_breakpoint_objfile_data (struct objfile *objfile)
3287{
3288 struct breakpoint_objfile_data *bp_objfile_data;
3289
9a3c8263
SM
3290 bp_objfile_data = ((struct breakpoint_objfile_data *)
3291 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3292 if (bp_objfile_data == NULL)
3293 {
8d749320
SM
3294 bp_objfile_data =
3295 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3296
3297 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3298 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3299 }
3300 return bp_objfile_data;
3301}
3302
28106bc2
SDJ
3303static void
3304free_breakpoint_probes (struct objfile *obj, void *data)
3305{
9a3c8263
SM
3306 struct breakpoint_objfile_data *bp_objfile_data
3307 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3308
3309 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3310 VEC_free (probe_p, bp_objfile_data->exception_probes);
3311}
3312
e62c965a 3313static void
af02033e 3314create_overlay_event_breakpoint (void)
e62c965a 3315{
69de3c6a 3316 struct objfile *objfile;
af02033e 3317 const char *const func_name = "_ovly_debug_event";
e62c965a 3318
69de3c6a
PP
3319 ALL_OBJFILES (objfile)
3320 {
3321 struct breakpoint *b;
17450429
PP
3322 struct breakpoint_objfile_data *bp_objfile_data;
3323 CORE_ADDR addr;
67994074 3324 struct explicit_location explicit_loc;
69de3c6a 3325
17450429
PP
3326 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3327
3b7344d5 3328 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3329 continue;
3330
3b7344d5 3331 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3332 {
3b7344d5 3333 struct bound_minimal_symbol m;
17450429
PP
3334
3335 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3336 if (m.minsym == NULL)
17450429
PP
3337 {
3338 /* Avoid future lookups in this objfile. */
3b7344d5 3339 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3340 continue;
3341 }
3342 bp_objfile_data->overlay_msym = m;
3343 }
e62c965a 3344
77e371c0 3345 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3346 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3347 bp_overlay_event,
3348 &internal_breakpoint_ops);
67994074
KS
3349 initialize_explicit_location (&explicit_loc);
3350 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3351 b->location = new_explicit_location (&explicit_loc);
e62c965a 3352
69de3c6a
PP
3353 if (overlay_debugging == ovly_auto)
3354 {
3355 b->enable_state = bp_enabled;
3356 overlay_events_enabled = 1;
3357 }
3358 else
3359 {
3360 b->enable_state = bp_disabled;
3361 overlay_events_enabled = 0;
3362 }
e62c965a 3363 }
e62c965a
PP
3364}
3365
0fd8e87f 3366static void
af02033e 3367create_longjmp_master_breakpoint (void)
0fd8e87f 3368{
6c95b8df 3369 struct program_space *pspace;
6c95b8df 3370
5ed8105e 3371 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3372
6c95b8df 3373 ALL_PSPACES (pspace)
af02033e
PP
3374 {
3375 struct objfile *objfile;
3376
3377 set_current_program_space (pspace);
3378
3379 ALL_OBJFILES (objfile)
0fd8e87f 3380 {
af02033e
PP
3381 int i;
3382 struct gdbarch *gdbarch;
17450429 3383 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3384
af02033e 3385 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3386
17450429
PP
3387 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3388
28106bc2
SDJ
3389 if (!bp_objfile_data->longjmp_searched)
3390 {
25f9533e
SDJ
3391 VEC (probe_p) *ret;
3392
3393 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3394 if (ret != NULL)
3395 {
3396 /* We are only interested in checking one element. */
3397 struct probe *p = VEC_index (probe_p, ret, 0);
3398
3399 if (!can_evaluate_probe_arguments (p))
3400 {
3401 /* We cannot use the probe interface here, because it does
3402 not know how to evaluate arguments. */
3403 VEC_free (probe_p, ret);
3404 ret = NULL;
3405 }
3406 }
3407 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3408 bp_objfile_data->longjmp_searched = 1;
3409 }
3410
3411 if (bp_objfile_data->longjmp_probes != NULL)
3412 {
3413 int i;
3414 struct probe *probe;
3415 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3416
3417 for (i = 0;
3418 VEC_iterate (probe_p,
3419 bp_objfile_data->longjmp_probes,
3420 i, probe);
3421 ++i)
3422 {
3423 struct breakpoint *b;
3424
729662a5
TT
3425 b = create_internal_breakpoint (gdbarch,
3426 get_probe_address (probe,
3427 objfile),
28106bc2
SDJ
3428 bp_longjmp_master,
3429 &internal_breakpoint_ops);
d28cd78a 3430 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3431 b->enable_state = bp_disabled;
3432 }
3433
3434 continue;
3435 }
3436
0569175e
TSD
3437 if (!gdbarch_get_longjmp_target_p (gdbarch))
3438 continue;
3439
17450429 3440 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3441 {
3442 struct breakpoint *b;
af02033e 3443 const char *func_name;
17450429 3444 CORE_ADDR addr;
67994074 3445 struct explicit_location explicit_loc;
6c95b8df 3446
3b7344d5 3447 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3448 continue;
0fd8e87f 3449
17450429 3450 func_name = longjmp_names[i];
3b7344d5 3451 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3452 {
3b7344d5 3453 struct bound_minimal_symbol m;
17450429
PP
3454
3455 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3456 if (m.minsym == NULL)
17450429
PP
3457 {
3458 /* Prevent future lookups in this objfile. */
3b7344d5 3459 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3460 continue;
3461 }
3462 bp_objfile_data->longjmp_msym[i] = m;
3463 }
3464
77e371c0 3465 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3466 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3467 &internal_breakpoint_ops);
67994074
KS
3468 initialize_explicit_location (&explicit_loc);
3469 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3470 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3471 b->enable_state = bp_disabled;
3472 }
0fd8e87f 3473 }
af02033e 3474 }
0fd8e87f
UW
3475}
3476
af02033e 3477/* Create a master std::terminate breakpoint. */
aa7d318d 3478static void
af02033e 3479create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3480{
3481 struct program_space *pspace;
af02033e 3482 const char *const func_name = "std::terminate()";
aa7d318d 3483
5ed8105e 3484 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3485
3486 ALL_PSPACES (pspace)
17450429
PP
3487 {
3488 struct objfile *objfile;
3489 CORE_ADDR addr;
3490
3491 set_current_program_space (pspace);
3492
aa7d318d
TT
3493 ALL_OBJFILES (objfile)
3494 {
3495 struct breakpoint *b;
17450429 3496 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3497 struct explicit_location explicit_loc;
aa7d318d 3498
17450429 3499 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3500
3b7344d5 3501 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3502 continue;
3503
3b7344d5 3504 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3505 {
3b7344d5 3506 struct bound_minimal_symbol m;
17450429
PP
3507
3508 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3509 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3510 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3511 {
3512 /* Prevent future lookups in this objfile. */
3b7344d5 3513 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3514 continue;
3515 }
3516 bp_objfile_data->terminate_msym = m;
3517 }
aa7d318d 3518
77e371c0 3519 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3520 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3521 bp_std_terminate_master,
3522 &internal_breakpoint_ops);
67994074
KS
3523 initialize_explicit_location (&explicit_loc);
3524 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3525 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3526 b->enable_state = bp_disabled;
3527 }
17450429 3528 }
aa7d318d
TT
3529}
3530
186c406b
TT
3531/* Install a master breakpoint on the unwinder's debug hook. */
3532
70221824 3533static void
186c406b
TT
3534create_exception_master_breakpoint (void)
3535{
3536 struct objfile *objfile;
17450429 3537 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3538
3539 ALL_OBJFILES (objfile)
3540 {
17450429
PP
3541 struct breakpoint *b;
3542 struct gdbarch *gdbarch;
3543 struct breakpoint_objfile_data *bp_objfile_data;
3544 CORE_ADDR addr;
67994074 3545 struct explicit_location explicit_loc;
17450429
PP
3546
3547 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3548
28106bc2
SDJ
3549 /* We prefer the SystemTap probe point if it exists. */
3550 if (!bp_objfile_data->exception_searched)
3551 {
25f9533e
SDJ
3552 VEC (probe_p) *ret;
3553
3554 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3555
3556 if (ret != NULL)
3557 {
3558 /* We are only interested in checking one element. */
3559 struct probe *p = VEC_index (probe_p, ret, 0);
3560
3561 if (!can_evaluate_probe_arguments (p))
3562 {
3563 /* We cannot use the probe interface here, because it does
3564 not know how to evaluate arguments. */
3565 VEC_free (probe_p, ret);
3566 ret = NULL;
3567 }
3568 }
3569 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3570 bp_objfile_data->exception_searched = 1;
3571 }
3572
3573 if (bp_objfile_data->exception_probes != NULL)
3574 {
3575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3576 int i;
3577 struct probe *probe;
3578
3579 for (i = 0;
3580 VEC_iterate (probe_p,
3581 bp_objfile_data->exception_probes,
3582 i, probe);
3583 ++i)
3584 {
3585 struct breakpoint *b;
3586
729662a5
TT
3587 b = create_internal_breakpoint (gdbarch,
3588 get_probe_address (probe,
3589 objfile),
28106bc2
SDJ
3590 bp_exception_master,
3591 &internal_breakpoint_ops);
d28cd78a 3592 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3593 b->enable_state = bp_disabled;
3594 }
3595
3596 continue;
3597 }
3598
3599 /* Otherwise, try the hook function. */
3600
3b7344d5 3601 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3602 continue;
3603
3604 gdbarch = get_objfile_arch (objfile);
186c406b 3605
3b7344d5 3606 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3607 {
3b7344d5 3608 struct bound_minimal_symbol debug_hook;
186c406b 3609
17450429 3610 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3611 if (debug_hook.minsym == NULL)
17450429 3612 {
3b7344d5 3613 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3614 continue;
3615 }
3616
3617 bp_objfile_data->exception_msym = debug_hook;
186c406b 3618 }
17450429 3619
77e371c0 3620 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3621 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3622 &current_target);
06edf0c0
PA
3623 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3624 &internal_breakpoint_ops);
67994074
KS
3625 initialize_explicit_location (&explicit_loc);
3626 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3627 b->location = new_explicit_location (&explicit_loc);
17450429 3628 b->enable_state = bp_disabled;
186c406b 3629 }
186c406b
TT
3630}
3631
9ef9e6a6
KS
3632/* Does B have a location spec? */
3633
3634static int
3635breakpoint_event_location_empty_p (const struct breakpoint *b)
3636{
d28cd78a 3637 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3638}
3639
c906108c 3640void
fba45db2 3641update_breakpoints_after_exec (void)
c906108c 3642{
35df4500 3643 struct breakpoint *b, *b_tmp;
876fa593 3644 struct bp_location *bploc, **bplocp_tmp;
c906108c 3645
25b22b0a
PA
3646 /* We're about to delete breakpoints from GDB's lists. If the
3647 INSERTED flag is true, GDB will try to lift the breakpoints by
3648 writing the breakpoints' "shadow contents" back into memory. The
3649 "shadow contents" are NOT valid after an exec, so GDB should not
3650 do that. Instead, the target is responsible from marking
3651 breakpoints out as soon as it detects an exec. We don't do that
3652 here instead, because there may be other attempts to delete
3653 breakpoints after detecting an exec and before reaching here. */
876fa593 3654 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3655 if (bploc->pspace == current_program_space)
3656 gdb_assert (!bploc->inserted);
c906108c 3657
35df4500 3658 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3659 {
6c95b8df
PA
3660 if (b->pspace != current_program_space)
3661 continue;
3662
4a64f543 3663 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3664 if (b->type == bp_shlib_event)
3665 {
3666 delete_breakpoint (b);
3667 continue;
3668 }
c906108c 3669
4a64f543 3670 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3671 if (b->type == bp_jit_event)
3672 {
3673 delete_breakpoint (b);
3674 continue;
3675 }
3676
1900040c 3677 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3678 as must overlay event and longjmp master breakpoints. */
3679 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3680 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3681 || b->type == bp_exception_master)
c4093a6a
JM
3682 {
3683 delete_breakpoint (b);
3684 continue;
3685 }
3686
4a64f543 3687 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3688 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3689 {
3690 delete_breakpoint (b);
3691 continue;
3692 }
3693
7c16b83e
PA
3694 /* Just like single-step breakpoints. */
3695 if (b->type == bp_single_step)
3696 {
3697 delete_breakpoint (b);
3698 continue;
3699 }
3700
611c83ae
PA
3701 /* Longjmp and longjmp-resume breakpoints are also meaningless
3702 after an exec. */
186c406b 3703 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3704 || b->type == bp_longjmp_call_dummy
186c406b 3705 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3706 {
3707 delete_breakpoint (b);
3708 continue;
3709 }
3710
ce78b96d
JB
3711 if (b->type == bp_catchpoint)
3712 {
3713 /* For now, none of the bp_catchpoint breakpoints need to
3714 do anything at this point. In the future, if some of
3715 the catchpoints need to something, we will need to add
3716 a new method, and call this method from here. */
3717 continue;
3718 }
3719
c5aa993b
JM
3720 /* bp_finish is a special case. The only way we ought to be able
3721 to see one of these when an exec() has happened, is if the user
3722 caught a vfork, and then said "finish". Ordinarily a finish just
3723 carries them to the call-site of the current callee, by setting
3724 a temporary bp there and resuming. But in this case, the finish
3725 will carry them entirely through the vfork & exec.
3726
3727 We don't want to allow a bp_finish to remain inserted now. But
3728 we can't safely delete it, 'cause finish_command has a handle to
3729 the bp on a bpstat, and will later want to delete it. There's a
3730 chance (and I've seen it happen) that if we delete the bp_finish
3731 here, that its storage will get reused by the time finish_command
3732 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3733 We really must allow finish_command to delete a bp_finish.
3734
e5dd4106 3735 In the absence of a general solution for the "how do we know
53a5351d
JM
3736 it's safe to delete something others may have handles to?"
3737 problem, what we'll do here is just uninsert the bp_finish, and
3738 let finish_command delete it.
3739
3740 (We know the bp_finish is "doomed" in the sense that it's
3741 momentary, and will be deleted as soon as finish_command sees
3742 the inferior stopped. So it doesn't matter that the bp's
3743 address is probably bogus in the new a.out, unlike e.g., the
3744 solib breakpoints.) */
c5aa993b 3745
c5aa993b
JM
3746 if (b->type == bp_finish)
3747 {
3748 continue;
3749 }
3750
3751 /* Without a symbolic address, we have little hope of the
3752 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3753 a.out. */
9ef9e6a6 3754 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3755 {
3756 delete_breakpoint (b);
3757 continue;
3758 }
c5aa993b 3759 }
c906108c
SS
3760}
3761
3762int
d80ee84f 3763detach_breakpoints (ptid_t ptid)
c906108c 3764{
35df4500 3765 struct bp_location *bl, **blp_tmp;
3a1bae8e 3766 int val = 0;
2989a365 3767 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3768 struct inferior *inf = current_inferior ();
c5aa993b 3769
dfd4cc63 3770 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3771 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3772
6c95b8df 3773 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3774 inferior_ptid = ptid;
35df4500 3775 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3776 {
35df4500 3777 if (bl->pspace != inf->pspace)
6c95b8df
PA
3778 continue;
3779
bd9673a4
PW
3780 /* This function must physically remove breakpoints locations
3781 from the specified ptid, without modifying the breakpoint
3782 package's state. Locations of type bp_loc_other are only
3783 maintained at GDB side. So, there is no need to remove
3784 these bp_loc_other locations. Moreover, removing these
3785 would modify the breakpoint package's state. */
3786 if (bl->loc_type == bp_loc_other)
3787 continue;
3788
35df4500 3789 if (bl->inserted)
b2b6a7da 3790 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3791 }
d03285ec 3792
3a1bae8e 3793 return val;
c906108c
SS
3794}
3795
35df4500 3796/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3797 Note that this is used to detach breakpoints from a child fork.
3798 When we get here, the child isn't in the inferior list, and neither
3799 do we have objects to represent its address space --- we should
35df4500 3800 *not* look at bl->pspace->aspace here. */
6c95b8df 3801
c906108c 3802static int
b2b6a7da 3803remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3804{
3805 int val;
c5aa993b 3806
35df4500
TJB
3807 /* BL is never in moribund_locations by our callers. */
3808 gdb_assert (bl->owner != NULL);
2bdf28a0 3809
74960c60
VP
3810 /* The type of none suggests that owner is actually deleted.
3811 This should not ever happen. */
35df4500 3812 gdb_assert (bl->owner->type != bp_none);
0bde7532 3813
35df4500
TJB
3814 if (bl->loc_type == bp_loc_software_breakpoint
3815 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3816 {
c02f5703
MS
3817 /* "Normal" instruction breakpoint: either the standard
3818 trap-instruction bp (bp_breakpoint), or a
3819 bp_hardware_breakpoint. */
3820
3821 /* First check to see if we have to handle an overlay. */
3822 if (overlay_debugging == ovly_off
35df4500
TJB
3823 || bl->section == NULL
3824 || !(section_is_overlay (bl->section)))
c02f5703
MS
3825 {
3826 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3827
3828 /* If we're trying to uninsert a memory breakpoint that we
3829 know is set in a dynamic object that is marked
3830 shlib_disabled, then either the dynamic object was
3831 removed with "remove-symbol-file" or with
3832 "nosharedlibrary". In the former case, we don't know
3833 whether another dynamic object might have loaded over the
3834 breakpoint's address -- the user might well let us know
3835 about it next with add-symbol-file (the whole point of
d03de421 3836 add-symbol-file is letting the user manually maintain a
08351840
PA
3837 list of dynamically loaded objects). If we have the
3838 breakpoint's shadow memory, that is, this is a software
3839 breakpoint managed by GDB, check whether the breakpoint
3840 is still inserted in memory, to avoid overwriting wrong
3841 code with stale saved shadow contents. Note that HW
3842 breakpoints don't have shadow memory, as they're
3843 implemented using a mechanism that is not dependent on
3844 being able to modify the target's memory, and as such
3845 they should always be removed. */
3846 if (bl->shlib_disabled
3847 && bl->target_info.shadow_len != 0
3848 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3849 val = 0;
3850 else
73971819 3851 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3852 }
c906108c
SS
3853 else
3854 {
4a64f543 3855 /* This breakpoint is in an overlay section.
c02f5703
MS
3856 Did we set a breakpoint at the LMA? */
3857 if (!overlay_events_enabled)
3858 {
3859 /* Yes -- overlay event support is not active, so we
3860 should have set a breakpoint at the LMA. Remove it.
3861 */
c02f5703
MS
3862 /* Ignore any failures: if the LMA is in ROM, we will
3863 have already warned when we failed to insert it. */
35df4500
TJB
3864 if (bl->loc_type == bp_loc_hardware_breakpoint)
3865 target_remove_hw_breakpoint (bl->gdbarch,
3866 &bl->overlay_target_info);
c02f5703 3867 else
35df4500 3868 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3869 &bl->overlay_target_info,
3870 reason);
c02f5703
MS
3871 }
3872 /* Did we set a breakpoint at the VMA?
3873 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3874 if (bl->inserted)
c906108c 3875 {
c02f5703
MS
3876 /* Yes -- remove it. Previously we did not bother to
3877 remove the breakpoint if the section had been
3878 unmapped, but let's not rely on that being safe. We
3879 don't know what the overlay manager might do. */
aa67235e
UW
3880
3881 /* However, we should remove *software* breakpoints only
3882 if the section is still mapped, or else we overwrite
3883 wrong code with the saved shadow contents. */
348d480f
PA
3884 if (bl->loc_type == bp_loc_hardware_breakpoint
3885 || section_is_mapped (bl->section))
73971819 3886 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3887 else
3888 val = 0;
c906108c 3889 }
c02f5703
MS
3890 else
3891 {
3892 /* No -- not inserted, so no need to remove. No error. */
3893 val = 0;
3894 }
c906108c 3895 }
879d1e6b 3896
08351840
PA
3897 /* In some cases, we might not be able to remove a breakpoint in
3898 a shared library that has already been removed, but we have
3899 not yet processed the shlib unload event. Similarly for an
3900 unloaded add-symbol-file object - the user might not yet have
3901 had the chance to remove-symbol-file it. shlib_disabled will
3902 be set if the library/object has already been removed, but
3903 the breakpoint hasn't been uninserted yet, e.g., after
3904 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3905 always-inserted mode. */
076855f9 3906 if (val
08351840
PA
3907 && (bl->loc_type == bp_loc_software_breakpoint
3908 && (bl->shlib_disabled
3909 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3910 || shared_objfile_contains_address_p (bl->pspace,
3911 bl->address))))
879d1e6b
UW
3912 val = 0;
3913
c906108c
SS
3914 if (val)
3915 return val;
b2b6a7da 3916 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3917 }
35df4500 3918 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3919 {
77b06cd7
TJB
3920 gdb_assert (bl->owner->ops != NULL
3921 && bl->owner->ops->remove_location != NULL);
3922
b2b6a7da 3923 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3924 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3925
c906108c 3926 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3927 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3928 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3929 bl->owner->number);
c906108c 3930 }
35df4500
TJB
3931 else if (bl->owner->type == bp_catchpoint
3932 && breakpoint_enabled (bl->owner)
3933 && !bl->duplicate)
ce78b96d 3934 {
77b06cd7
TJB
3935 gdb_assert (bl->owner->ops != NULL
3936 && bl->owner->ops->remove_location != NULL);
ce78b96d 3937
73971819 3938 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3939 if (val)
3940 return val;
77b06cd7 3941
b2b6a7da 3942 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3943 }
c906108c
SS
3944
3945 return 0;
3946}
3947
6c95b8df 3948static int
834c0d03 3949remove_breakpoint (struct bp_location *bl)
6c95b8df 3950{
35df4500
TJB
3951 /* BL is never in moribund_locations by our callers. */
3952 gdb_assert (bl->owner != NULL);
2bdf28a0 3953
6c95b8df
PA
3954 /* The type of none suggests that owner is actually deleted.
3955 This should not ever happen. */
35df4500 3956 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3957
5ed8105e 3958 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3959
35df4500 3960 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3961
5ed8105e 3962 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3963}
3964
c906108c
SS
3965/* Clear the "inserted" flag in all breakpoints. */
3966
25b22b0a 3967void
fba45db2 3968mark_breakpoints_out (void)
c906108c 3969{
35df4500 3970 struct bp_location *bl, **blp_tmp;
c906108c 3971
35df4500 3972 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3973 if (bl->pspace == current_program_space)
35df4500 3974 bl->inserted = 0;
c906108c
SS
3975}
3976
53a5351d
JM
3977/* Clear the "inserted" flag in all breakpoints and delete any
3978 breakpoints which should go away between runs of the program.
c906108c
SS
3979
3980 Plus other such housekeeping that has to be done for breakpoints
3981 between runs.
3982
53a5351d
JM
3983 Note: this function gets called at the end of a run (by
3984 generic_mourn_inferior) and when a run begins (by
4a64f543 3985 init_wait_for_inferior). */
c906108c
SS
3986
3987
3988
3989void
fba45db2 3990breakpoint_init_inferior (enum inf_context context)
c906108c 3991{
35df4500 3992 struct breakpoint *b, *b_tmp;
870f88f7 3993 struct bp_location *bl;
1c5cfe86 3994 int ix;
6c95b8df 3995 struct program_space *pspace = current_program_space;
c906108c 3996
50c71eaf
PA
3997 /* If breakpoint locations are shared across processes, then there's
3998 nothing to do. */
f5656ead 3999 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4000 return;
4001
1a853c52 4002 mark_breakpoints_out ();
075f6582 4003
35df4500 4004 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4005 {
6c95b8df
PA
4006 if (b->loc && b->loc->pspace != pspace)
4007 continue;
4008
c5aa993b
JM
4009 switch (b->type)
4010 {
4011 case bp_call_dummy:
e2e4d78b 4012 case bp_longjmp_call_dummy:
c906108c 4013
c5aa993b 4014 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4015 cause problems when the inferior is rerun, so we better get
4016 rid of it. */
4017
4018 case bp_watchpoint_scope:
4019
4020 /* Also get rid of scope breakpoints. */
4021
4022 case bp_shlib_event:
4023
4024 /* Also remove solib event breakpoints. Their addresses may
4025 have changed since the last time we ran the program.
4026 Actually we may now be debugging against different target;
4027 and so the solib backend that installed this breakpoint may
4028 not be used in by the target. E.g.,
4029
4030 (gdb) file prog-linux
4031 (gdb) run # native linux target
4032 ...
4033 (gdb) kill
4034 (gdb) file prog-win.exe
4035 (gdb) tar rem :9999 # remote Windows gdbserver.
4036 */
c906108c 4037
f59f708a
PA
4038 case bp_step_resume:
4039
4040 /* Also remove step-resume breakpoints. */
4041
7c16b83e
PA
4042 case bp_single_step:
4043
4044 /* Also remove single-step breakpoints. */
4045
c5aa993b
JM
4046 delete_breakpoint (b);
4047 break;
c906108c 4048
c5aa993b
JM
4049 case bp_watchpoint:
4050 case bp_hardware_watchpoint:
4051 case bp_read_watchpoint:
4052 case bp_access_watchpoint:
3a5c3e22
PA
4053 {
4054 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4055
3a5c3e22
PA
4056 /* Likewise for watchpoints on local expressions. */
4057 if (w->exp_valid_block != NULL)
4058 delete_breakpoint (b);
63000888 4059 else
3a5c3e22 4060 {
63000888
PA
4061 /* Get rid of existing locations, which are no longer
4062 valid. New ones will be created in
4063 update_watchpoint, when the inferior is restarted.
4064 The next update_global_location_list call will
4065 garbage collect them. */
4066 b->loc = NULL;
4067
4068 if (context == inf_starting)
4069 {
4070 /* Reset val field to force reread of starting value in
4071 insert_breakpoints. */
4072 if (w->val)
4073 value_free (w->val);
4074 w->val = NULL;
4075 w->val_valid = 0;
4076 }
4077 }
3a5c3e22 4078 }
c5aa993b
JM
4079 break;
4080 default:
c5aa993b
JM
4081 break;
4082 }
4083 }
1c5cfe86
PA
4084
4085 /* Get rid of the moribund locations. */
35df4500
TJB
4086 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4087 decref_bp_location (&bl);
1c5cfe86 4088 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4089}
4090
6c95b8df
PA
4091/* These functions concern about actual breakpoints inserted in the
4092 target --- to e.g. check if we need to do decr_pc adjustment or if
4093 we need to hop over the bkpt --- so we check for address space
4094 match, not program space. */
4095
c2c6d25f
JM
4096/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4097 exists at PC. It returns ordinary_breakpoint_here if it's an
4098 ordinary breakpoint, or permanent_breakpoint_here if it's a
4099 permanent breakpoint.
4100 - When continuing from a location with an ordinary breakpoint, we
4101 actually single step once before calling insert_breakpoints.
e5dd4106 4102 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4103 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4104 the target, to advance the PC past the breakpoint. */
c906108c 4105
c2c6d25f 4106enum breakpoint_here
6c95b8df 4107breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4108{
35df4500 4109 struct bp_location *bl, **blp_tmp;
c2c6d25f 4110 int any_breakpoint_here = 0;
c906108c 4111
35df4500 4112 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4113 {
35df4500
TJB
4114 if (bl->loc_type != bp_loc_software_breakpoint
4115 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4116 continue;
4117
f1310107 4118 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4119 if ((breakpoint_enabled (bl->owner)
1a853c52 4120 || bl->permanent)
f1310107 4121 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4122 {
4123 if (overlay_debugging
35df4500
TJB
4124 && section_is_overlay (bl->section)
4125 && !section_is_mapped (bl->section))
075f6582 4126 continue; /* unmapped overlay -- can't be a match */
1a853c52 4127 else if (bl->permanent)
075f6582
DJ
4128 return permanent_breakpoint_here;
4129 else
4130 any_breakpoint_here = 1;
4131 }
4132 }
c906108c 4133
f486487f 4134 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4135}
4136
d35ae833
PA
4137/* See breakpoint.h. */
4138
4139int
4140breakpoint_in_range_p (struct address_space *aspace,
4141 CORE_ADDR addr, ULONGEST len)
4142{
4143 struct bp_location *bl, **blp_tmp;
4144
4145 ALL_BP_LOCATIONS (bl, blp_tmp)
4146 {
4147 if (bl->loc_type != bp_loc_software_breakpoint
4148 && bl->loc_type != bp_loc_hardware_breakpoint)
4149 continue;
4150
4151 if ((breakpoint_enabled (bl->owner)
4152 || bl->permanent)
4153 && breakpoint_location_address_range_overlap (bl, aspace,
4154 addr, len))
4155 {
4156 if (overlay_debugging
4157 && section_is_overlay (bl->section)
4158 && !section_is_mapped (bl->section))
4159 {
4160 /* Unmapped overlay -- can't be a match. */
4161 continue;
4162 }
4163
4164 return 1;
4165 }
4166 }
4167
4168 return 0;
4169}
4170
1c5cfe86
PA
4171/* Return true if there's a moribund breakpoint at PC. */
4172
4173int
6c95b8df 4174moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4175{
4176 struct bp_location *loc;
4177 int ix;
4178
4179 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4180 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4181 return 1;
4182
4183 return 0;
4184}
c2c6d25f 4185
f7ce857f
PA
4186/* Returns non-zero iff BL is inserted at PC, in address space
4187 ASPACE. */
4188
4189static int
4190bp_location_inserted_here_p (struct bp_location *bl,
4191 struct address_space *aspace, CORE_ADDR pc)
4192{
4193 if (bl->inserted
4194 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4195 aspace, pc))
4196 {
4197 if (overlay_debugging
4198 && section_is_overlay (bl->section)
4199 && !section_is_mapped (bl->section))
4200 return 0; /* unmapped overlay -- can't be a match */
4201 else
4202 return 1;
4203 }
4204 return 0;
4205}
4206
a1fd2fa5 4207/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4208
4209int
a1fd2fa5 4210breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4211{
f7ce857f 4212 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4213
f7ce857f 4214 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4215 {
f7ce857f
PA
4216 struct bp_location *bl = *blp;
4217
35df4500
TJB
4218 if (bl->loc_type != bp_loc_software_breakpoint
4219 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4220 continue;
4221
f7ce857f
PA
4222 if (bp_location_inserted_here_p (bl, aspace, pc))
4223 return 1;
c5aa993b 4224 }
c36b740a
VP
4225 return 0;
4226}
4227
a1fd2fa5
PA
4228/* This function returns non-zero iff there is a software breakpoint
4229 inserted at PC. */
c36b740a
VP
4230
4231int
a1fd2fa5
PA
4232software_breakpoint_inserted_here_p (struct address_space *aspace,
4233 CORE_ADDR pc)
4fa8626c 4234{
f7ce857f 4235 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4236
f7ce857f 4237 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4238 {
f7ce857f
PA
4239 struct bp_location *bl = *blp;
4240
35df4500 4241 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4242 continue;
4243
f7ce857f
PA
4244 if (bp_location_inserted_here_p (bl, aspace, pc))
4245 return 1;
4fa8626c
DJ
4246 }
4247
4248 return 0;
9c02b525
PA
4249}
4250
4251/* See breakpoint.h. */
4252
4253int
4254hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4255 CORE_ADDR pc)
4256{
4257 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4258
4259 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4260 {
4261 struct bp_location *bl = *blp;
4262
4263 if (bl->loc_type != bp_loc_hardware_breakpoint)
4264 continue;
4265
4266 if (bp_location_inserted_here_p (bl, aspace, pc))
4267 return 1;
4268 }
4269
4270 return 0;
4fa8626c
DJ
4271}
4272
9093389c
PA
4273int
4274hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4275 CORE_ADDR addr, ULONGEST len)
4276{
4277 struct breakpoint *bpt;
4278
4279 ALL_BREAKPOINTS (bpt)
4280 {
4281 struct bp_location *loc;
4282
4283 if (bpt->type != bp_hardware_watchpoint
4284 && bpt->type != bp_access_watchpoint)
4285 continue;
4286
4287 if (!breakpoint_enabled (bpt))
4288 continue;
4289
4290 for (loc = bpt->loc; loc; loc = loc->next)
4291 if (loc->pspace->aspace == aspace && loc->inserted)
4292 {
4293 CORE_ADDR l, h;
4294
4295 /* Check for intersection. */
768adc05
PA
4296 l = std::max<CORE_ADDR> (loc->address, addr);
4297 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4298 if (l < h)
4299 return 1;
4300 }
4301 }
4302 return 0;
4303}
c906108c 4304\f
c5aa993b 4305
c906108c
SS
4306/* bpstat stuff. External routines' interfaces are documented
4307 in breakpoint.h. */
4308
4309int
c326b90e 4310is_catchpoint (struct breakpoint *ep)
c906108c 4311{
533be4dd 4312 return (ep->type == bp_catchpoint);
c906108c
SS
4313}
4314
f431efe5
PA
4315/* Frees any storage that is part of a bpstat. Does not walk the
4316 'next' chain. */
4317
04afa70c 4318bpstats::~bpstats ()
198757a8 4319{
04afa70c
TT
4320 if (old_val != NULL)
4321 value_free (old_val);
04afa70c
TT
4322 if (bp_location_at != NULL)
4323 decref_bp_location (&bp_location_at);
198757a8
VP
4324}
4325
c906108c
SS
4326/* Clear a bpstat so that it says we are not at any breakpoint.
4327 Also free any storage that is part of a bpstat. */
4328
4329void
fba45db2 4330bpstat_clear (bpstat *bsp)
c906108c
SS
4331{
4332 bpstat p;
4333 bpstat q;
4334
4335 if (bsp == 0)
4336 return;
4337 p = *bsp;
4338 while (p != NULL)
4339 {
4340 q = p->next;
04afa70c 4341 delete p;
c906108c
SS
4342 p = q;
4343 }
4344 *bsp = NULL;
4345}
4346
04afa70c
TT
4347bpstats::bpstats (const bpstats &other)
4348 : next (NULL),
4349 bp_location_at (other.bp_location_at),
4350 breakpoint_at (other.breakpoint_at),
4351 commands (other.commands),
4352 old_val (other.old_val),
4353 print (other.print),
4354 stop (other.stop),
4355 print_it (other.print_it)
4356{
4357 if (old_val != NULL)
4358 {
4359 old_val = value_copy (old_val);
4360 release_value (old_val);
4361 }
4362 incref_bp_location (bp_location_at);
04afa70c
TT
4363}
4364
c906108c
SS
4365/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4366 is part of the bpstat is copied as well. */
4367
4368bpstat
fba45db2 4369bpstat_copy (bpstat bs)
c906108c
SS
4370{
4371 bpstat p = NULL;
4372 bpstat tmp;
4373 bpstat retval = NULL;
4374
4375 if (bs == NULL)
4376 return bs;
4377
4378 for (; bs != NULL; bs = bs->next)
4379 {
04afa70c 4380 tmp = new bpstats (*bs);
31cc81e9 4381
c906108c
SS
4382 if (p == NULL)
4383 /* This is the first thing in the chain. */
4384 retval = tmp;
4385 else
4386 p->next = tmp;
4387 p = tmp;
4388 }
4389 p->next = NULL;
4390 return retval;
4391}
4392
4a64f543 4393/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4394
4395bpstat
fba45db2 4396bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4397{
c5aa993b
JM
4398 if (bsp == NULL)
4399 return NULL;
c906108c 4400
c5aa993b
JM
4401 for (; bsp != NULL; bsp = bsp->next)
4402 {
f431efe5 4403 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4404 return bsp;
4405 }
c906108c
SS
4406 return NULL;
4407}
4408
ab04a2af
TT
4409/* See breakpoint.h. */
4410
47591c29 4411int
427cd150 4412bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4413{
ab04a2af
TT
4414 for (; bsp != NULL; bsp = bsp->next)
4415 {
427cd150
TT
4416 if (bsp->breakpoint_at == NULL)
4417 {
4418 /* A moribund location can never explain a signal other than
4419 GDB_SIGNAL_TRAP. */
4420 if (sig == GDB_SIGNAL_TRAP)
47591c29 4421 return 1;
427cd150
TT
4422 }
4423 else
47591c29
PA
4424 {
4425 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4426 sig))
4427 return 1;
4428 }
ab04a2af
TT
4429 }
4430
47591c29 4431 return 0;
ab04a2af
TT
4432}
4433
4a64f543
MS
4434/* Put in *NUM the breakpoint number of the first breakpoint we are
4435 stopped at. *BSP upon return is a bpstat which points to the
4436 remaining breakpoints stopped at (but which is not guaranteed to be
4437 good for anything but further calls to bpstat_num).
4438
8671a17b
PA
4439 Return 0 if passed a bpstat which does not indicate any breakpoints.
4440 Return -1 if stopped at a breakpoint that has been deleted since
4441 we set it.
4442 Return 1 otherwise. */
c906108c
SS
4443
4444int
8671a17b 4445bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4446{
4447 struct breakpoint *b;
4448
4449 if ((*bsp) == NULL)
4450 return 0; /* No more breakpoint values */
8671a17b 4451
4a64f543
MS
4452 /* We assume we'll never have several bpstats that correspond to a
4453 single breakpoint -- otherwise, this function might return the
4454 same number more than once and this will look ugly. */
f431efe5 4455 b = (*bsp)->breakpoint_at;
8671a17b
PA
4456 *bsp = (*bsp)->next;
4457 if (b == NULL)
4458 return -1; /* breakpoint that's been deleted since */
4459
4460 *num = b->number; /* We have its number */
4461 return 1;
c906108c
SS
4462}
4463
e93ca019 4464/* See breakpoint.h. */
c906108c
SS
4465
4466void
e93ca019 4467bpstat_clear_actions (void)
c906108c 4468{
e93ca019
JK
4469 struct thread_info *tp;
4470 bpstat bs;
4471
4472 if (ptid_equal (inferior_ptid, null_ptid))
4473 return;
4474
4475 tp = find_thread_ptid (inferior_ptid);
4476 if (tp == NULL)
4477 return;
4478
4479 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4480 {
d1b0a7bf 4481 bs->commands = NULL;
abf85f46 4482
c906108c
SS
4483 if (bs->old_val != NULL)
4484 {
4485 value_free (bs->old_val);
4486 bs->old_val = NULL;
4487 }
4488 }
4489}
4490
f3b1572e
PA
4491/* Called when a command is about to proceed the inferior. */
4492
4493static void
4494breakpoint_about_to_proceed (void)
4495{
4496 if (!ptid_equal (inferior_ptid, null_ptid))
4497 {
4498 struct thread_info *tp = inferior_thread ();
4499
4500 /* Allow inferior function calls in breakpoint commands to not
4501 interrupt the command list. When the call finishes
4502 successfully, the inferior will be standing at the same
4503 breakpoint as if nothing happened. */
16c381f0 4504 if (tp->control.in_infcall)
f3b1572e
PA
4505 return;
4506 }
4507
4508 breakpoint_proceeded = 1;
4509}
4510
4a64f543
MS
4511/* Stub for cleaning up our state if we error-out of a breakpoint
4512 command. */
c906108c 4513static void
4efb68b1 4514cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4515{
4516 executing_breakpoint_commands = 0;
4517}
4518
abf85f46
JK
4519/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4520 or its equivalent. */
4521
4522static int
4523command_line_is_silent (struct command_line *cmd)
4524{
4f45d445 4525 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4526}
4527
4a64f543
MS
4528/* Execute all the commands associated with all the breakpoints at
4529 this location. Any of these commands could cause the process to
4530 proceed beyond this point, etc. We look out for such changes by
4531 checking the global "breakpoint_proceeded" after each command.
c906108c 4532
347bddb7
PA
4533 Returns true if a breakpoint command resumed the inferior. In that
4534 case, it is the caller's responsibility to recall it again with the
4535 bpstat of the current thread. */
4536
4537static int
4538bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4539{
4540 bpstat bs;
4541 struct cleanup *old_chain;
347bddb7 4542 int again = 0;
c906108c
SS
4543
4544 /* Avoid endless recursion if a `source' command is contained
4545 in bs->commands. */
4546 if (executing_breakpoint_commands)
347bddb7 4547 return 0;
c906108c
SS
4548
4549 executing_breakpoint_commands = 1;
4550 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4551
1ac32117 4552 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4553
4a64f543 4554 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4555 bs = *bsp;
4556
4557 breakpoint_proceeded = 0;
4558 for (; bs != NULL; bs = bs->next)
4559 {
d1b0a7bf 4560 struct command_line *cmd = NULL;
6c50ab1c
JB
4561
4562 /* Take ownership of the BSP's command tree, if it has one.
4563
4564 The command tree could legitimately contain commands like
4565 'step' and 'next', which call clear_proceed_status, which
4566 frees stop_bpstat's command tree. To make sure this doesn't
4567 free the tree we're executing out from under us, we need to
4568 take ownership of the tree ourselves. Since a given bpstat's
4569 commands are only executed once, we don't need to copy it; we
4570 can clear the pointer in the bpstat, and make sure we free
4571 the tree when we're done. */
d1b0a7bf 4572 counted_command_line ccmd = bs->commands;
9add0f1b 4573 bs->commands = NULL;
d1b0a7bf
TT
4574 if (ccmd != NULL)
4575 cmd = ccmd.get ();
abf85f46
JK
4576 if (command_line_is_silent (cmd))
4577 {
4578 /* The action has been already done by bpstat_stop_status. */
4579 cmd = cmd->next;
4580 }
6c50ab1c 4581
c906108c
SS
4582 while (cmd != NULL)
4583 {
4584 execute_control_command (cmd);
4585
4586 if (breakpoint_proceeded)
4587 break;
4588 else
4589 cmd = cmd->next;
4590 }
6c50ab1c 4591
c906108c 4592 if (breakpoint_proceeded)
32c1e744 4593 {
cb814510 4594 if (current_ui->async)
347bddb7
PA
4595 /* If we are in async mode, then the target might be still
4596 running, not stopped at any breakpoint, so nothing for
4597 us to do here -- just return to the event loop. */
4598 ;
32c1e744
VP
4599 else
4600 /* In sync mode, when execute_control_command returns
4601 we're already standing on the next breakpoint.
347bddb7
PA
4602 Breakpoint commands for that stop were not run, since
4603 execute_command does not run breakpoint commands --
4604 only command_line_handler does, but that one is not
4605 involved in execution of breakpoint commands. So, we
4606 can now execute breakpoint commands. It should be
4607 noted that making execute_command do bpstat actions is
4608 not an option -- in this case we'll have recursive
4609 invocation of bpstat for each breakpoint with a
4610 command, and can easily blow up GDB stack. Instead, we
4611 return true, which will trigger the caller to recall us
4612 with the new stop_bpstat. */
4613 again = 1;
4614 break;
32c1e744 4615 }
c906108c 4616 }
c2b8ed2c 4617 do_cleanups (old_chain);
347bddb7
PA
4618 return again;
4619}
4620
4621void
4622bpstat_do_actions (void)
4623{
353d1d73
JK
4624 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4625
347bddb7
PA
4626 /* Do any commands attached to breakpoint we are stopped at. */
4627 while (!ptid_equal (inferior_ptid, null_ptid)
4628 && target_has_execution
4629 && !is_exited (inferior_ptid)
4630 && !is_executing (inferior_ptid))
4631 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4632 and only return when it is stopped at the next breakpoint, we
4633 keep doing breakpoint actions until it returns false to
4634 indicate the inferior was not resumed. */
16c381f0 4635 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4636 break;
353d1d73
JK
4637
4638 discard_cleanups (cleanup_if_error);
c906108c
SS
4639}
4640
fa4727a6
DJ
4641/* Print out the (old or new) value associated with a watchpoint. */
4642
4643static void
4644watchpoint_value_print (struct value *val, struct ui_file *stream)
4645{
4646 if (val == NULL)
4647 fprintf_unfiltered (stream, _("<unreadable>"));
4648 else
79a45b7d
TT
4649 {
4650 struct value_print_options opts;
4651 get_user_print_options (&opts);
4652 value_print (val, stream, &opts);
4653 }
fa4727a6
DJ
4654}
4655
f303dbd6
PA
4656/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4657 debugging multiple threads. */
4658
4659void
4660maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4661{
112e8700 4662 if (uiout->is_mi_like_p ())
f303dbd6
PA
4663 return;
4664
112e8700 4665 uiout->text ("\n");
f303dbd6
PA
4666
4667 if (show_thread_that_caused_stop ())
4668 {
4669 const char *name;
4670 struct thread_info *thr = inferior_thread ();
4671
112e8700
SM
4672 uiout->text ("Thread ");
4673 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4674
4675 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4676 if (name != NULL)
4677 {
112e8700
SM
4678 uiout->text (" \"");
4679 uiout->field_fmt ("name", "%s", name);
4680 uiout->text ("\"");
f303dbd6
PA
4681 }
4682
112e8700 4683 uiout->text (" hit ");
f303dbd6
PA
4684 }
4685}
4686
e514a9d6 4687/* Generic routine for printing messages indicating why we
4a64f543 4688 stopped. The behavior of this function depends on the value
e514a9d6
JM
4689 'print_it' in the bpstat structure. Under some circumstances we
4690 may decide not to print anything here and delegate the task to
4a64f543 4691 normal_stop(). */
e514a9d6
JM
4692
4693static enum print_stop_action
4694print_bp_stop_message (bpstat bs)
4695{
4696 switch (bs->print_it)
4697 {
4698 case print_it_noop:
4a64f543 4699 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4700 return PRINT_UNKNOWN;
4701 break;
4702
4703 case print_it_done:
4704 /* We still want to print the frame, but we already printed the
4a64f543 4705 relevant messages. */
e514a9d6
JM
4706 return PRINT_SRC_AND_LOC;
4707 break;
4708
4709 case print_it_normal:
4f8d1dc6 4710 {
f431efe5
PA
4711 struct breakpoint *b = bs->breakpoint_at;
4712
1a6a67de
TJB
4713 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4714 which has since been deleted. */
4715 if (b == NULL)
4716 return PRINT_UNKNOWN;
4717
348d480f
PA
4718 /* Normal case. Call the breakpoint's print_it method. */
4719 return b->ops->print_it (bs);
4f8d1dc6 4720 }
348d480f 4721 break;
3086aeae 4722
e514a9d6 4723 default:
8e65ff28 4724 internal_error (__FILE__, __LINE__,
e2e0b3e5 4725 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4726 break;
c906108c 4727 }
c906108c
SS
4728}
4729
edcc5120
TT
4730/* A helper function that prints a shared library stopped event. */
4731
4732static void
4733print_solib_event (int is_catchpoint)
4734{
4735 int any_deleted
4736 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4737 int any_added
4738 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4739
4740 if (!is_catchpoint)
4741 {
4742 if (any_added || any_deleted)
112e8700 4743 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4744 else
112e8700
SM
4745 current_uiout->text (_("Stopped due to shared library event (no "
4746 "libraries added or removed)\n"));
edcc5120
TT
4747 }
4748
112e8700
SM
4749 if (current_uiout->is_mi_like_p ())
4750 current_uiout->field_string ("reason",
4751 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4752
4753 if (any_deleted)
4754 {
edcc5120
TT
4755 char *name;
4756 int ix;
4757
112e8700 4758 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4759 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4760 for (ix = 0;
4761 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4762 ix, name);
4763 ++ix)
4764 {
4765 if (ix > 0)
112e8700
SM
4766 current_uiout->text (" ");
4767 current_uiout->field_string ("library", name);
4768 current_uiout->text ("\n");
edcc5120 4769 }
edcc5120
TT
4770 }
4771
4772 if (any_added)
4773 {
4774 struct so_list *iter;
4775 int ix;
edcc5120 4776
112e8700 4777 current_uiout->text (_(" Inferior loaded "));
10f489e5 4778 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4779 for (ix = 0;
4780 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4781 ix, iter);
4782 ++ix)
4783 {
4784 if (ix > 0)
112e8700
SM
4785 current_uiout->text (" ");
4786 current_uiout->field_string ("library", iter->so_name);
4787 current_uiout->text ("\n");
edcc5120 4788 }
edcc5120
TT
4789 }
4790}
4791
e514a9d6
JM
4792/* Print a message indicating what happened. This is called from
4793 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4794 list - a list of the eventpoints that caused this stop. KIND is
4795 the target_waitkind for the stopping event. This
e514a9d6
JM
4796 routine calls the generic print routine for printing a message
4797 about reasons for stopping. This will print (for example) the
4798 "Breakpoint n," part of the output. The return value of this
4799 routine is one of:
c906108c 4800
4a64f543 4801 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4802 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4803 code to print the location. An example is
c5aa993b
JM
4804 "Breakpoint 1, " which should be followed by
4805 the location.
917317f4 4806 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4807 to also print the location part of the message.
4808 An example is the catch/throw messages, which
4a64f543 4809 don't require a location appended to the end.
917317f4 4810 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4811 further info to be printed. */
c906108c 4812
917317f4 4813enum print_stop_action
36dfb11c 4814bpstat_print (bpstat bs, int kind)
c906108c 4815{
f486487f 4816 enum print_stop_action val;
c5aa993b 4817
c906108c 4818 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4819 (Currently all watchpoints go on the bpstat whether hit or not.
4820 That probably could (should) be changed, provided care is taken
c906108c 4821 with respect to bpstat_explains_signal). */
e514a9d6
JM
4822 for (; bs; bs = bs->next)
4823 {
4824 val = print_bp_stop_message (bs);
4825 if (val == PRINT_SRC_ONLY
4826 || val == PRINT_SRC_AND_LOC
4827 || val == PRINT_NOTHING)
4828 return val;
4829 }
c906108c 4830
36dfb11c
TT
4831 /* If we had hit a shared library event breakpoint,
4832 print_bp_stop_message would print out this message. If we hit an
4833 OS-level shared library event, do the same thing. */
4834 if (kind == TARGET_WAITKIND_LOADED)
4835 {
edcc5120 4836 print_solib_event (0);
36dfb11c
TT
4837 return PRINT_NOTHING;
4838 }
4839
e514a9d6 4840 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4841 with and nothing was printed. */
917317f4 4842 return PRINT_UNKNOWN;
c906108c
SS
4843}
4844
c42bd95a
DE
4845/* Evaluate the expression EXP and return 1 if value is zero.
4846 This returns the inverse of the condition because it is called
4847 from catch_errors which returns 0 if an exception happened, and if an
4848 exception happens we want execution to stop.
4a64f543 4849 The argument is a "struct expression *" that has been cast to a
c42bd95a 4850 "void *" to make it pass through catch_errors. */
c906108c
SS
4851
4852static int
4efb68b1 4853breakpoint_cond_eval (void *exp)
c906108c 4854{
278cd55f 4855 struct value *mark = value_mark ();
c5aa993b 4856 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4857
c906108c
SS
4858 value_free_to_mark (mark);
4859 return i;
4860}
4861
5760d0ab 4862/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4863
04afa70c
TT
4864bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4865 : next (NULL),
4866 bp_location_at (bl),
4867 breakpoint_at (bl->owner),
4868 commands (NULL),
4869 old_val (NULL),
4870 print (0),
4871 stop (0),
4872 print_it (print_it_normal)
c906108c 4873{
f431efe5 4874 incref_bp_location (bl);
04afa70c
TT
4875 **bs_link_pointer = this;
4876 *bs_link_pointer = &next;
4877}
4878
4879bpstats::bpstats ()
4880 : next (NULL),
4881 bp_location_at (NULL),
4882 breakpoint_at (NULL),
4883 commands (NULL),
4884 old_val (NULL),
4885 print (0),
4886 stop (0),
4887 print_it (print_it_normal)
4888{
c906108c
SS
4889}
4890\f
d983da9c
DJ
4891/* The target has stopped with waitstatus WS. Check if any hardware
4892 watchpoints have triggered, according to the target. */
4893
4894int
4895watchpoints_triggered (struct target_waitstatus *ws)
4896{
d92524f1 4897 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4898 CORE_ADDR addr;
4899 struct breakpoint *b;
4900
4901 if (!stopped_by_watchpoint)
4902 {
4903 /* We were not stopped by a watchpoint. Mark all watchpoints
4904 as not triggered. */
4905 ALL_BREAKPOINTS (b)
cc60f2e3 4906 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4907 {
4908 struct watchpoint *w = (struct watchpoint *) b;
4909
4910 w->watchpoint_triggered = watch_triggered_no;
4911 }
d983da9c
DJ
4912
4913 return 0;
4914 }
4915
4916 if (!target_stopped_data_address (&current_target, &addr))
4917 {
4918 /* We were stopped by a watchpoint, but we don't know where.
4919 Mark all watchpoints as unknown. */
4920 ALL_BREAKPOINTS (b)
cc60f2e3 4921 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4922 {
4923 struct watchpoint *w = (struct watchpoint *) b;
4924
4925 w->watchpoint_triggered = watch_triggered_unknown;
4926 }
d983da9c 4927
3c4797ba 4928 return 1;
d983da9c
DJ
4929 }
4930
4931 /* The target could report the data address. Mark watchpoints
4932 affected by this data address as triggered, and all others as not
4933 triggered. */
4934
4935 ALL_BREAKPOINTS (b)
cc60f2e3 4936 if (is_hardware_watchpoint (b))
d983da9c 4937 {
3a5c3e22 4938 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4939 struct bp_location *loc;
d983da9c 4940
3a5c3e22 4941 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4942 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4943 {
3a5c3e22 4944 if (is_masked_watchpoint (b))
9c06b0b4 4945 {
3a5c3e22
PA
4946 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4947 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4948
4949 if (newaddr == start)
4950 {
3a5c3e22 4951 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4952 break;
4953 }
4954 }
4955 /* Exact match not required. Within range is sufficient. */
4956 else if (target_watchpoint_addr_within_range (&current_target,
4957 addr, loc->address,
4958 loc->length))
4959 {
3a5c3e22 4960 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4961 break;
4962 }
4963 }
d983da9c
DJ
4964 }
4965
4966 return 1;
4967}
4968
c906108c
SS
4969/* Possible return values for watchpoint_check (this can't be an enum
4970 because of check_errors). */
4971/* The watchpoint has been deleted. */
4972#define WP_DELETED 1
4973/* The value has changed. */
4974#define WP_VALUE_CHANGED 2
4975/* The value has not changed. */
4976#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4977/* Ignore this watchpoint, no matter if the value changed or not. */
4978#define WP_IGNORE 4
c906108c
SS
4979
4980#define BP_TEMPFLAG 1
4981#define BP_HARDWAREFLAG 2
4982
4a64f543
MS
4983/* Evaluate watchpoint condition expression and check if its value
4984 changed.
553e4c11
JB
4985
4986 P should be a pointer to struct bpstat, but is defined as a void *
4987 in order for this function to be usable with catch_errors. */
c906108c
SS
4988
4989static int
4efb68b1 4990watchpoint_check (void *p)
c906108c
SS
4991{
4992 bpstat bs = (bpstat) p;
3a5c3e22 4993 struct watchpoint *b;
c906108c
SS
4994 struct frame_info *fr;
4995 int within_current_scope;
4996
f431efe5 4997 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4998 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4999 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5000
f6bc2008
PA
5001 /* If this is a local watchpoint, we only want to check if the
5002 watchpoint frame is in scope if the current thread is the thread
5003 that was used to create the watchpoint. */
5004 if (!watchpoint_in_thread_scope (b))
60e1c644 5005 return WP_IGNORE;
f6bc2008 5006
c906108c
SS
5007 if (b->exp_valid_block == NULL)
5008 within_current_scope = 1;
5009 else
5010 {
edb3359d
DJ
5011 struct frame_info *frame = get_current_frame ();
5012 struct gdbarch *frame_arch = get_frame_arch (frame);
5013 CORE_ADDR frame_pc = get_frame_pc (frame);
5014
c9cf6e20 5015 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5016 still in the function but the stack frame has already been
5017 invalidated. Since we can't rely on the values of local
5018 variables after the stack has been destroyed, we are treating
5019 the watchpoint in that state as `not changed' without further
5020 checking. Don't mark watchpoints as changed if the current
5021 frame is in an epilogue - even if they are in some other
5022 frame, our view of the stack is likely to be wrong and
5023 frame_find_by_id could error out. */
c9cf6e20 5024 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5025 return WP_IGNORE;
a0f49112 5026
101dcfbe 5027 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5028 within_current_scope = (fr != NULL);
69fbadd5
DJ
5029
5030 /* If we've gotten confused in the unwinder, we might have
5031 returned a frame that can't describe this variable. */
edb3359d
DJ
5032 if (within_current_scope)
5033 {
5034 struct symbol *function;
5035
5036 function = get_frame_function (fr);
5037 if (function == NULL
5038 || !contained_in (b->exp_valid_block,
5039 SYMBOL_BLOCK_VALUE (function)))
5040 within_current_scope = 0;
5041 }
69fbadd5 5042
edb3359d 5043 if (within_current_scope)
c906108c
SS
5044 /* If we end up stopping, the current frame will get selected
5045 in normal_stop. So this call to select_frame won't affect
5046 the user. */
0f7d239c 5047 select_frame (fr);
c906108c 5048 }
c5aa993b 5049
c906108c
SS
5050 if (within_current_scope)
5051 {
4a64f543
MS
5052 /* We use value_{,free_to_}mark because it could be a *long*
5053 time before we return to the command level and call
5054 free_all_values. We can't call free_all_values because we
5055 might be in the middle of evaluating a function call. */
c906108c 5056
0cf6dd15 5057 int pc = 0;
9c06b0b4 5058 struct value *mark;
fa4727a6
DJ
5059 struct value *new_val;
5060
c1fc2657 5061 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5062 /* Since we don't know the exact trigger address (from
5063 stopped_data_address), just tell the user we've triggered
5064 a mask watchpoint. */
5065 return WP_VALUE_CHANGED;
5066
5067 mark = value_mark ();
4d01a485 5068 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5069
bb9d5f81
PP
5070 if (b->val_bitsize != 0)
5071 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5072
4a64f543
MS
5073 /* We use value_equal_contents instead of value_equal because
5074 the latter coerces an array to a pointer, thus comparing just
5075 the address of the array instead of its contents. This is
5076 not what we want. */
fa4727a6 5077 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5078 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5079 {
fa4727a6
DJ
5080 if (new_val != NULL)
5081 {
5082 release_value (new_val);
5083 value_free_to_mark (mark);
5084 }
c906108c
SS
5085 bs->old_val = b->val;
5086 b->val = new_val;
fa4727a6 5087 b->val_valid = 1;
c906108c
SS
5088 return WP_VALUE_CHANGED;
5089 }
5090 else
5091 {
60e1c644 5092 /* Nothing changed. */
c906108c 5093 value_free_to_mark (mark);
c906108c
SS
5094 return WP_VALUE_NOT_CHANGED;
5095 }
5096 }
5097 else
5098 {
5099 /* This seems like the only logical thing to do because
c5aa993b
JM
5100 if we temporarily ignored the watchpoint, then when
5101 we reenter the block in which it is valid it contains
5102 garbage (in the case of a function, it may have two
5103 garbage values, one before and one after the prologue).
5104 So we can't even detect the first assignment to it and
5105 watch after that (since the garbage may or may not equal
5106 the first value assigned). */
348d480f
PA
5107 /* We print all the stop information in
5108 breakpoint_ops->print_it, but in this case, by the time we
5109 call breakpoint_ops->print_it this bp will be deleted
5110 already. So we have no choice but print the information
5111 here. */
468afe6c 5112
0e454242 5113 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5114 {
5115 struct ui_out *uiout = current_uiout;
5116
112e8700
SM
5117 if (uiout->is_mi_like_p ())
5118 uiout->field_string
5119 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5120 uiout->text ("\nWatchpoint ");
c1fc2657 5121 uiout->field_int ("wpnum", b->number);
112e8700 5122 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5123 "which its expression is valid.\n");
5124 }
4ce44c66 5125
cdac0397 5126 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5127 b->commands = NULL;
d0fb5eae 5128 watchpoint_del_at_next_stop (b);
c906108c
SS
5129
5130 return WP_DELETED;
5131 }
5132}
5133
18a18393 5134/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5135 breakpoint location BL. This function does not check if we should
5136 stop, only if BL explains the stop. */
5137
18a18393 5138static int
6c95b8df 5139bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5140 struct address_space *aspace, CORE_ADDR bp_addr,
5141 const struct target_waitstatus *ws)
18a18393
VP
5142{
5143 struct breakpoint *b = bl->owner;
5144
348d480f 5145 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5146 gdb_assert (b != NULL);
5147
09ac7c10 5148 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5149}
5150
3a5c3e22
PA
5151/* Determine if the watched values have actually changed, and we
5152 should stop. If not, set BS->stop to 0. */
5153
18a18393
VP
5154static void
5155bpstat_check_watchpoint (bpstat bs)
5156{
2bdf28a0 5157 const struct bp_location *bl;
3a5c3e22 5158 struct watchpoint *b;
2bdf28a0
JK
5159
5160 /* BS is built for existing struct breakpoint. */
f431efe5 5161 bl = bs->bp_location_at;
2bdf28a0 5162 gdb_assert (bl != NULL);
3a5c3e22 5163 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5164 gdb_assert (b != NULL);
18a18393 5165
18a18393 5166 {
18a18393
VP
5167 int must_check_value = 0;
5168
c1fc2657 5169 if (b->type == bp_watchpoint)
18a18393
VP
5170 /* For a software watchpoint, we must always check the
5171 watched value. */
5172 must_check_value = 1;
5173 else if (b->watchpoint_triggered == watch_triggered_yes)
5174 /* We have a hardware watchpoint (read, write, or access)
5175 and the target earlier reported an address watched by
5176 this watchpoint. */
5177 must_check_value = 1;
5178 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5179 && b->type == bp_hardware_watchpoint)
18a18393
VP
5180 /* We were stopped by a hardware watchpoint, but the target could
5181 not report the data address. We must check the watchpoint's
5182 value. Access and read watchpoints are out of luck; without
5183 a data address, we can't figure it out. */
5184 must_check_value = 1;
3a5c3e22 5185
18a18393
VP
5186 if (must_check_value)
5187 {
3e43a32a
MS
5188 char *message
5189 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
c1fc2657 5190 b->number);
18a18393
VP
5191 struct cleanup *cleanups = make_cleanup (xfree, message);
5192 int e = catch_errors (watchpoint_check, bs, message,
5193 RETURN_MASK_ALL);
5194 do_cleanups (cleanups);
5195 switch (e)
5196 {
5197 case WP_DELETED:
5198 /* We've already printed what needs to be printed. */
5199 bs->print_it = print_it_done;
5200 /* Stop. */
5201 break;
60e1c644
PA
5202 case WP_IGNORE:
5203 bs->print_it = print_it_noop;
5204 bs->stop = 0;
5205 break;
18a18393 5206 case WP_VALUE_CHANGED:
c1fc2657 5207 if (b->type == bp_read_watchpoint)
18a18393 5208 {
85d721b8
PA
5209 /* There are two cases to consider here:
5210
4a64f543 5211 1. We're watching the triggered memory for reads.
85d721b8
PA
5212 In that case, trust the target, and always report
5213 the watchpoint hit to the user. Even though
5214 reads don't cause value changes, the value may
5215 have changed since the last time it was read, and
5216 since we're not trapping writes, we will not see
5217 those, and as such we should ignore our notion of
5218 old value.
5219
4a64f543 5220 2. We're watching the triggered memory for both
85d721b8
PA
5221 reads and writes. There are two ways this may
5222 happen:
5223
4a64f543 5224 2.1. This is a target that can't break on data
85d721b8
PA
5225 reads only, but can break on accesses (reads or
5226 writes), such as e.g., x86. We detect this case
5227 at the time we try to insert read watchpoints.
5228
4a64f543 5229 2.2. Otherwise, the target supports read
85d721b8
PA
5230 watchpoints, but, the user set an access or write
5231 watchpoint watching the same memory as this read
5232 watchpoint.
5233
5234 If we're watching memory writes as well as reads,
5235 ignore watchpoint hits when we find that the
5236 value hasn't changed, as reads don't cause
5237 changes. This still gives false positives when
5238 the program writes the same value to memory as
5239 what there was already in memory (we will confuse
5240 it for a read), but it's much better than
5241 nothing. */
5242
5243 int other_write_watchpoint = 0;
5244
5245 if (bl->watchpoint_type == hw_read)
5246 {
5247 struct breakpoint *other_b;
5248
5249 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5250 if (other_b->type == bp_hardware_watchpoint
5251 || other_b->type == bp_access_watchpoint)
85d721b8 5252 {
3a5c3e22
PA
5253 struct watchpoint *other_w =
5254 (struct watchpoint *) other_b;
5255
5256 if (other_w->watchpoint_triggered
5257 == watch_triggered_yes)
5258 {
5259 other_write_watchpoint = 1;
5260 break;
5261 }
85d721b8
PA
5262 }
5263 }
5264
5265 if (other_write_watchpoint
5266 || bl->watchpoint_type == hw_access)
5267 {
5268 /* We're watching the same memory for writes,
5269 and the value changed since the last time we
5270 updated it, so this trap must be for a write.
5271 Ignore it. */
5272 bs->print_it = print_it_noop;
5273 bs->stop = 0;
5274 }
18a18393
VP
5275 }
5276 break;
5277 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5278 if (b->type == bp_hardware_watchpoint
5279 || b->type == bp_watchpoint)
18a18393
VP
5280 {
5281 /* Don't stop: write watchpoints shouldn't fire if
5282 the value hasn't changed. */
5283 bs->print_it = print_it_noop;
5284 bs->stop = 0;
5285 }
5286 /* Stop. */
5287 break;
5288 default:
5289 /* Can't happen. */
5290 case 0:
5291 /* Error from catch_errors. */
468afe6c 5292 {
0e454242 5293 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5294 {
5295 printf_filtered (_("Watchpoint %d deleted.\n"),
c1fc2657 5296 b->number);
468afe6c
PA
5297 }
5298 watchpoint_del_at_next_stop (b);
5299 /* We've already printed what needs to be printed. */
5300 bs->print_it = print_it_done;
5301 }
18a18393
VP
5302 break;
5303 }
5304 }
5305 else /* must_check_value == 0 */
5306 {
5307 /* This is a case where some watchpoint(s) triggered, but
5308 not at the address of this watchpoint, or else no
5309 watchpoint triggered after all. So don't print
5310 anything for this watchpoint. */
5311 bs->print_it = print_it_noop;
5312 bs->stop = 0;
5313 }
5314 }
5315}
5316
7d4df6a4
DE
5317/* For breakpoints that are currently marked as telling gdb to stop,
5318 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5319 of breakpoint referred to by BS. If we should not stop for this
5320 breakpoint, set BS->stop to 0. */
f431efe5 5321
18a18393
VP
5322static void
5323bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5324{
2bdf28a0
JK
5325 const struct bp_location *bl;
5326 struct breakpoint *b;
7d4df6a4
DE
5327 int value_is_zero = 0;
5328 struct expression *cond;
5329
5330 gdb_assert (bs->stop);
2bdf28a0
JK
5331
5332 /* BS is built for existing struct breakpoint. */
f431efe5 5333 bl = bs->bp_location_at;
2bdf28a0 5334 gdb_assert (bl != NULL);
f431efe5 5335 b = bs->breakpoint_at;
2bdf28a0 5336 gdb_assert (b != NULL);
18a18393 5337
b775012e
LM
5338 /* Even if the target evaluated the condition on its end and notified GDB, we
5339 need to do so again since GDB does not know if we stopped due to a
5340 breakpoint or a single step breakpoint. */
5341
18a18393 5342 if (frame_id_p (b->frame_id)
edb3359d 5343 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5344 {
7d4df6a4
DE
5345 bs->stop = 0;
5346 return;
5347 }
60e1c644 5348
12ab52e9
PA
5349 /* If this is a thread/task-specific breakpoint, don't waste cpu
5350 evaluating the condition if this isn't the specified
5351 thread/task. */
5d5658a1 5352 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5353 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5354
6c1b0f7b
DE
5355 {
5356 bs->stop = 0;
5357 return;
5358 }
5359
6dddc817
DE
5360 /* Evaluate extension language breakpoints that have a "stop" method
5361 implemented. */
5362 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5363
7d4df6a4
DE
5364 if (is_watchpoint (b))
5365 {
5366 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5367
4d01a485 5368 cond = w->cond_exp.get ();
7d4df6a4
DE
5369 }
5370 else
4d01a485 5371 cond = bl->cond.get ();
60e1c644 5372
7d4df6a4
DE
5373 if (cond && b->disposition != disp_del_at_next_stop)
5374 {
5375 int within_current_scope = 1;
5376 struct watchpoint * w;
60e1c644 5377
7d4df6a4
DE
5378 /* We use value_mark and value_free_to_mark because it could
5379 be a long time before we return to the command level and
5380 call free_all_values. We can't call free_all_values
5381 because we might be in the middle of evaluating a
5382 function call. */
5383 struct value *mark = value_mark ();
5384
5385 if (is_watchpoint (b))
5386 w = (struct watchpoint *) b;
5387 else
5388 w = NULL;
5389
5390 /* Need to select the frame, with all that implies so that
5391 the conditions will have the right context. Because we
5392 use the frame, we will not see an inlined function's
5393 variables when we arrive at a breakpoint at the start
5394 of the inlined function; the current frame will be the
5395 call site. */
5396 if (w == NULL || w->cond_exp_valid_block == NULL)
5397 select_frame (get_current_frame ());
5398 else
18a18393 5399 {
7d4df6a4
DE
5400 struct frame_info *frame;
5401
5402 /* For local watchpoint expressions, which particular
5403 instance of a local is being watched matters, so we
5404 keep track of the frame to evaluate the expression
5405 in. To evaluate the condition however, it doesn't
5406 really matter which instantiation of the function
5407 where the condition makes sense triggers the
5408 watchpoint. This allows an expression like "watch
5409 global if q > 10" set in `func', catch writes to
5410 global on all threads that call `func', or catch
5411 writes on all recursive calls of `func' by a single
5412 thread. We simply always evaluate the condition in
5413 the innermost frame that's executing where it makes
5414 sense to evaluate the condition. It seems
5415 intuitive. */
5416 frame = block_innermost_frame (w->cond_exp_valid_block);
5417 if (frame != NULL)
5418 select_frame (frame);
5419 else
5420 within_current_scope = 0;
18a18393 5421 }
7d4df6a4
DE
5422 if (within_current_scope)
5423 value_is_zero
5424 = catch_errors (breakpoint_cond_eval, cond,
5425 "Error in testing breakpoint condition:\n",
5426 RETURN_MASK_ALL);
5427 else
18a18393 5428 {
7d4df6a4
DE
5429 warning (_("Watchpoint condition cannot be tested "
5430 "in the current scope"));
5431 /* If we failed to set the right context for this
5432 watchpoint, unconditionally report it. */
5433 value_is_zero = 0;
18a18393 5434 }
7d4df6a4
DE
5435 /* FIXME-someday, should give breakpoint #. */
5436 value_free_to_mark (mark);
18a18393 5437 }
7d4df6a4
DE
5438
5439 if (cond && value_is_zero)
5440 {
5441 bs->stop = 0;
5442 }
7d4df6a4
DE
5443 else if (b->ignore_count > 0)
5444 {
5445 b->ignore_count--;
5446 bs->stop = 0;
5447 /* Increase the hit count even though we don't stop. */
5448 ++(b->hit_count);
5449 observer_notify_breakpoint_modified (b);
5450 }
18a18393
VP
5451}
5452
1cf4d951
PA
5453/* Returns true if we need to track moribund locations of LOC's type
5454 on the current target. */
5455
5456static int
5457need_moribund_for_location_type (struct bp_location *loc)
5458{
5459 return ((loc->loc_type == bp_loc_software_breakpoint
5460 && !target_supports_stopped_by_sw_breakpoint ())
5461 || (loc->loc_type == bp_loc_hardware_breakpoint
5462 && !target_supports_stopped_by_hw_breakpoint ()));
5463}
5464
18a18393 5465
9709f61c 5466/* Get a bpstat associated with having just stopped at address
d983da9c 5467 BP_ADDR in thread PTID.
c906108c 5468
d983da9c 5469 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5470 don't understand this stop. Result is a chain of bpstat's such
5471 that:
c906108c 5472
c5aa993b 5473 if we don't understand the stop, the result is a null pointer.
c906108c 5474
c5aa993b 5475 if we understand why we stopped, the result is not null.
c906108c 5476
c5aa993b
JM
5477 Each element of the chain refers to a particular breakpoint or
5478 watchpoint at which we have stopped. (We may have stopped for
5479 several reasons concurrently.)
c906108c 5480
c5aa993b
JM
5481 Each element of the chain has valid next, breakpoint_at,
5482 commands, FIXME??? fields. */
c906108c
SS
5483
5484bpstat
6c95b8df 5485bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5486 CORE_ADDR bp_addr, ptid_t ptid,
5487 const struct target_waitstatus *ws)
c906108c 5488{
0d381245 5489 struct breakpoint *b = NULL;
afe38095 5490 struct bp_location *bl;
20874c92 5491 struct bp_location *loc;
5760d0ab
JK
5492 /* First item of allocated bpstat's. */
5493 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5494 /* Pointer to the last thing in the chain currently. */
5760d0ab 5495 bpstat bs;
20874c92 5496 int ix;
429374b8 5497 int need_remove_insert;
f431efe5 5498 int removed_any;
c906108c 5499
f431efe5
PA
5500 /* First, build the bpstat chain with locations that explain a
5501 target stop, while being careful to not set the target running,
5502 as that may invalidate locations (in particular watchpoint
5503 locations are recreated). Resuming will happen here with
5504 breakpoint conditions or watchpoint expressions that include
5505 inferior function calls. */
c5aa993b 5506
429374b8
JK
5507 ALL_BREAKPOINTS (b)
5508 {
1a853c52 5509 if (!breakpoint_enabled (b))
429374b8 5510 continue;
a5606eee 5511
429374b8
JK
5512 for (bl = b->loc; bl != NULL; bl = bl->next)
5513 {
4a64f543
MS
5514 /* For hardware watchpoints, we look only at the first
5515 location. The watchpoint_check function will work on the
5516 entire expression, not the individual locations. For
5517 read watchpoints, the watchpoints_triggered function has
5518 checked all locations already. */
429374b8
JK
5519 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5520 break;
18a18393 5521
f6592439 5522 if (!bl->enabled || bl->shlib_disabled)
429374b8 5523 continue;
c5aa993b 5524
09ac7c10 5525 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5526 continue;
c5aa993b 5527
4a64f543
MS
5528 /* Come here if it's a watchpoint, or if the break address
5529 matches. */
c5aa993b 5530
04afa70c 5531 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5532 explain stop. */
c5aa993b 5533
f431efe5
PA
5534 /* Assume we stop. Should we find a watchpoint that is not
5535 actually triggered, or if the condition of the breakpoint
5536 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5537 bs->stop = 1;
5538 bs->print = 1;
d983da9c 5539
f431efe5
PA
5540 /* If this is a scope breakpoint, mark the associated
5541 watchpoint as triggered so that we will handle the
5542 out-of-scope event. We'll get to the watchpoint next
5543 iteration. */
d0fb5eae 5544 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5545 {
5546 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5547
5548 w->watchpoint_triggered = watch_triggered_yes;
5549 }
f431efe5
PA
5550 }
5551 }
5552
7c16b83e 5553 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5554 if (!target_supports_stopped_by_sw_breakpoint ()
5555 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5556 {
1cf4d951 5557 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5558 {
1cf4d951
PA
5559 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5560 && need_moribund_for_location_type (loc))
5561 {
04afa70c 5562 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5563 /* For hits of moribund locations, we should just proceed. */
5564 bs->stop = 0;
5565 bs->print = 0;
5566 bs->print_it = print_it_noop;
5567 }
f431efe5
PA
5568 }
5569 }
5570
edcc5120
TT
5571 /* A bit of special processing for shlib breakpoints. We need to
5572 process solib loading here, so that the lists of loaded and
5573 unloaded libraries are correct before we handle "catch load" and
5574 "catch unload". */
5575 for (bs = bs_head; bs != NULL; bs = bs->next)
5576 {
5d268276 5577 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5578 {
5579 handle_solib_event ();
5580 break;
5581 }
5582 }
5583
f431efe5
PA
5584 /* Now go through the locations that caused the target to stop, and
5585 check whether we're interested in reporting this stop to higher
5586 layers, or whether we should resume the target transparently. */
5587
5588 removed_any = 0;
5589
5760d0ab 5590 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5591 {
5592 if (!bs->stop)
5593 continue;
5594
f431efe5 5595 b = bs->breakpoint_at;
348d480f
PA
5596 b->ops->check_status (bs);
5597 if (bs->stop)
28010a5d 5598 {
348d480f 5599 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5600
429374b8
JK
5601 if (bs->stop)
5602 {
5603 ++(b->hit_count);
8d3788bd 5604 observer_notify_breakpoint_modified (b);
c906108c 5605
4a64f543 5606 /* We will stop here. */
429374b8
JK
5607 if (b->disposition == disp_disable)
5608 {
816338b5 5609 --(b->enable_count);
1a853c52 5610 if (b->enable_count <= 0)
429374b8 5611 b->enable_state = bp_disabled;
f431efe5 5612 removed_any = 1;
429374b8
JK
5613 }
5614 if (b->silent)
5615 bs->print = 0;
5616 bs->commands = b->commands;
abf85f46 5617 if (command_line_is_silent (bs->commands
d1b0a7bf 5618 ? bs->commands.get () : NULL))
abf85f46 5619 bs->print = 0;
9d6e6e84
HZ
5620
5621 b->ops->after_condition_true (bs);
429374b8
JK
5622 }
5623
348d480f 5624 }
a9b3a50f
PA
5625
5626 /* Print nothing for this entry if we don't stop or don't
5627 print. */
5628 if (!bs->stop || !bs->print)
5629 bs->print_it = print_it_noop;
429374b8 5630 }
876fa593 5631
d983da9c
DJ
5632 /* If we aren't stopping, the value of some hardware watchpoint may
5633 not have changed, but the intermediate memory locations we are
5634 watching may have. Don't bother if we're stopping; this will get
5635 done later. */
d832cb68 5636 need_remove_insert = 0;
5760d0ab
JK
5637 if (! bpstat_causes_stop (bs_head))
5638 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5639 if (!bs->stop
f431efe5
PA
5640 && bs->breakpoint_at
5641 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5642 {
3a5c3e22
PA
5643 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5644
5645 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5646 need_remove_insert = 1;
d983da9c
DJ
5647 }
5648
d832cb68 5649 if (need_remove_insert)
44702360 5650 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5651 else if (removed_any)
44702360 5652 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5653
5760d0ab 5654 return bs_head;
c906108c 5655}
628fe4e4
JK
5656
5657static void
5658handle_jit_event (void)
5659{
5660 struct frame_info *frame;
5661 struct gdbarch *gdbarch;
5662
243a9253
PA
5663 if (debug_infrun)
5664 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5665
628fe4e4
JK
5666 /* Switch terminal for any messages produced by
5667 breakpoint_re_set. */
223ffa71 5668 target_terminal::ours_for_output ();
628fe4e4
JK
5669
5670 frame = get_current_frame ();
5671 gdbarch = get_frame_arch (frame);
5672
5673 jit_event_handler (gdbarch);
5674
223ffa71 5675 target_terminal::inferior ();
628fe4e4
JK
5676}
5677
5678/* Prepare WHAT final decision for infrun. */
5679
5680/* Decide what infrun needs to do with this bpstat. */
5681
c906108c 5682struct bpstat_what
0e30163f 5683bpstat_what (bpstat bs_head)
c906108c 5684{
c906108c 5685 struct bpstat_what retval;
0e30163f 5686 bpstat bs;
c906108c 5687
628fe4e4 5688 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5689 retval.call_dummy = STOP_NONE;
186c406b 5690 retval.is_longjmp = 0;
628fe4e4 5691
0e30163f 5692 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5693 {
628fe4e4
JK
5694 /* Extract this BS's action. After processing each BS, we check
5695 if its action overrides all we've seem so far. */
5696 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5697 enum bptype bptype;
5698
c906108c 5699 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5700 {
5701 /* I suspect this can happen if it was a momentary
5702 breakpoint which has since been deleted. */
5703 bptype = bp_none;
5704 }
20874c92 5705 else
f431efe5 5706 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5707
5708 switch (bptype)
c906108c
SS
5709 {
5710 case bp_none:
628fe4e4 5711 break;
c906108c
SS
5712 case bp_breakpoint:
5713 case bp_hardware_breakpoint:
7c16b83e 5714 case bp_single_step:
c906108c
SS
5715 case bp_until:
5716 case bp_finish:
a9b3a50f 5717 case bp_shlib_event:
c906108c
SS
5718 if (bs->stop)
5719 {
5720 if (bs->print)
628fe4e4 5721 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5722 else
628fe4e4 5723 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5724 }
5725 else
628fe4e4 5726 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5727 break;
5728 case bp_watchpoint:
5729 case bp_hardware_watchpoint:
5730 case bp_read_watchpoint:
5731 case bp_access_watchpoint:
5732 if (bs->stop)
5733 {
5734 if (bs->print)
628fe4e4 5735 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5736 else
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5738 }
5739 else
628fe4e4
JK
5740 {
5741 /* There was a watchpoint, but we're not stopping.
5742 This requires no further action. */
5743 }
c906108c
SS
5744 break;
5745 case bp_longjmp:
e2e4d78b 5746 case bp_longjmp_call_dummy:
186c406b 5747 case bp_exception:
0a39bb32
PA
5748 if (bs->stop)
5749 {
5750 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5751 retval.is_longjmp = bptype != bp_exception;
5752 }
5753 else
5754 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5755 break;
5756 case bp_longjmp_resume:
186c406b 5757 case bp_exception_resume:
0a39bb32
PA
5758 if (bs->stop)
5759 {
5760 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5761 retval.is_longjmp = bptype == bp_longjmp_resume;
5762 }
5763 else
5764 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5765 break;
5766 case bp_step_resume:
5767 if (bs->stop)
628fe4e4
JK
5768 this_action = BPSTAT_WHAT_STEP_RESUME;
5769 else
c906108c 5770 {
628fe4e4
JK
5771 /* It is for the wrong frame. */
5772 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5773 }
c906108c 5774 break;
2c03e5be
PA
5775 case bp_hp_step_resume:
5776 if (bs->stop)
5777 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5778 else
5779 {
5780 /* It is for the wrong frame. */
5781 this_action = BPSTAT_WHAT_SINGLE;
5782 }
5783 break;
c906108c 5784 case bp_watchpoint_scope:
c4093a6a 5785 case bp_thread_event:
1900040c 5786 case bp_overlay_event:
0fd8e87f 5787 case bp_longjmp_master:
aa7d318d 5788 case bp_std_terminate_master:
186c406b 5789 case bp_exception_master:
628fe4e4 5790 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5791 break;
ce78b96d 5792 case bp_catchpoint:
c5aa993b
JM
5793 if (bs->stop)
5794 {
5795 if (bs->print)
628fe4e4 5796 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5797 else
628fe4e4 5798 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5799 }
5800 else
628fe4e4
JK
5801 {
5802 /* There was a catchpoint, but we're not stopping.
5803 This requires no further action. */
5804 }
5805 break;
628fe4e4 5806 case bp_jit_event:
628fe4e4 5807 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5808 break;
c906108c 5809 case bp_call_dummy:
53a5351d
JM
5810 /* Make sure the action is stop (silent or noisy),
5811 so infrun.c pops the dummy frame. */
aa7d318d 5812 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5813 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5814 break;
5815 case bp_std_terminate:
5816 /* Make sure the action is stop (silent or noisy),
5817 so infrun.c pops the dummy frame. */
aa7d318d 5818 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5819 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5820 break;
1042e4c0 5821 case bp_tracepoint:
7a697b8d 5822 case bp_fast_tracepoint:
0fb4aa4b 5823 case bp_static_tracepoint:
1042e4c0
SS
5824 /* Tracepoint hits should not be reported back to GDB, and
5825 if one got through somehow, it should have been filtered
5826 out already. */
5827 internal_error (__FILE__, __LINE__,
7a697b8d 5828 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5829 break;
5830 case bp_gnu_ifunc_resolver:
5831 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5832 this_action = BPSTAT_WHAT_SINGLE;
5833 break;
5834 case bp_gnu_ifunc_resolver_return:
5835 /* The breakpoint will be removed, execution will restart from the
5836 PC of the former breakpoint. */
5837 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5838 break;
e7e0cddf
SS
5839
5840 case bp_dprintf:
a11cfd87
HZ
5841 if (bs->stop)
5842 this_action = BPSTAT_WHAT_STOP_SILENT;
5843 else
5844 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5845 break;
5846
628fe4e4
JK
5847 default:
5848 internal_error (__FILE__, __LINE__,
5849 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5850 }
628fe4e4 5851
325fac50 5852 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5853 }
628fe4e4 5854
243a9253
PA
5855 return retval;
5856}
628fe4e4 5857
243a9253
PA
5858void
5859bpstat_run_callbacks (bpstat bs_head)
5860{
5861 bpstat bs;
628fe4e4 5862
0e30163f
JK
5863 for (bs = bs_head; bs != NULL; bs = bs->next)
5864 {
5865 struct breakpoint *b = bs->breakpoint_at;
5866
5867 if (b == NULL)
5868 continue;
5869 switch (b->type)
5870 {
243a9253
PA
5871 case bp_jit_event:
5872 handle_jit_event ();
5873 break;
0e30163f
JK
5874 case bp_gnu_ifunc_resolver:
5875 gnu_ifunc_resolver_stop (b);
5876 break;
5877 case bp_gnu_ifunc_resolver_return:
5878 gnu_ifunc_resolver_return_stop (b);
5879 break;
5880 }
5881 }
c906108c
SS
5882}
5883
5884/* Nonzero if we should step constantly (e.g. watchpoints on machines
5885 without hardware support). This isn't related to a specific bpstat,
5886 just to things like whether watchpoints are set. */
5887
c5aa993b 5888int
fba45db2 5889bpstat_should_step (void)
c906108c
SS
5890{
5891 struct breakpoint *b;
cc59ec59 5892
c906108c 5893 ALL_BREAKPOINTS (b)
717a8278 5894 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5895 return 1;
c906108c
SS
5896 return 0;
5897}
5898
67822962
PA
5899int
5900bpstat_causes_stop (bpstat bs)
5901{
5902 for (; bs != NULL; bs = bs->next)
5903 if (bs->stop)
5904 return 1;
5905
5906 return 0;
5907}
5908
c906108c 5909\f
c5aa993b 5910
170b53b2
UW
5911/* Compute a string of spaces suitable to indent the next line
5912 so it starts at the position corresponding to the table column
5913 named COL_NAME in the currently active table of UIOUT. */
5914
5915static char *
5916wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5917{
5918 static char wrap_indent[80];
5919 int i, total_width, width, align;
c5209615 5920 const char *text;
170b53b2
UW
5921
5922 total_width = 0;
112e8700 5923 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5924 {
5925 if (strcmp (text, col_name) == 0)
5926 {
5927 gdb_assert (total_width < sizeof wrap_indent);
5928 memset (wrap_indent, ' ', total_width);
5929 wrap_indent[total_width] = 0;
5930
5931 return wrap_indent;
5932 }
5933
5934 total_width += width + 1;
5935 }
5936
5937 return NULL;
5938}
5939
b775012e
LM
5940/* Determine if the locations of this breakpoint will have their conditions
5941 evaluated by the target, host or a mix of both. Returns the following:
5942
5943 "host": Host evals condition.
5944 "host or target": Host or Target evals condition.
5945 "target": Target evals condition.
5946*/
5947
5948static const char *
5949bp_condition_evaluator (struct breakpoint *b)
5950{
5951 struct bp_location *bl;
5952 char host_evals = 0;
5953 char target_evals = 0;
5954
5955 if (!b)
5956 return NULL;
5957
5958 if (!is_breakpoint (b))
5959 return NULL;
5960
5961 if (gdb_evaluates_breakpoint_condition_p ()
5962 || !target_supports_evaluation_of_breakpoint_conditions ())
5963 return condition_evaluation_host;
5964
5965 for (bl = b->loc; bl; bl = bl->next)
5966 {
5967 if (bl->cond_bytecode)
5968 target_evals++;
5969 else
5970 host_evals++;
5971 }
5972
5973 if (host_evals && target_evals)
5974 return condition_evaluation_both;
5975 else if (target_evals)
5976 return condition_evaluation_target;
5977 else
5978 return condition_evaluation_host;
5979}
5980
5981/* Determine the breakpoint location's condition evaluator. This is
5982 similar to bp_condition_evaluator, but for locations. */
5983
5984static const char *
5985bp_location_condition_evaluator (struct bp_location *bl)
5986{
5987 if (bl && !is_breakpoint (bl->owner))
5988 return NULL;
5989
5990 if (gdb_evaluates_breakpoint_condition_p ()
5991 || !target_supports_evaluation_of_breakpoint_conditions ())
5992 return condition_evaluation_host;
5993
5994 if (bl && bl->cond_bytecode)
5995 return condition_evaluation_target;
5996 else
5997 return condition_evaluation_host;
5998}
5999
859825b8
JK
6000/* Print the LOC location out of the list of B->LOC locations. */
6001
170b53b2
UW
6002static void
6003print_breakpoint_location (struct breakpoint *b,
6004 struct bp_location *loc)
0d381245 6005{
79a45e25 6006 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6007
6008 scoped_restore_current_program_space restore_pspace;
6c95b8df 6009
859825b8
JK
6010 if (loc != NULL && loc->shlib_disabled)
6011 loc = NULL;
6012
6c95b8df
PA
6013 if (loc != NULL)
6014 set_current_program_space (loc->pspace);
6015
56435ebe 6016 if (b->display_canonical)
d28cd78a 6017 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6018 else if (loc && loc->symtab)
0d381245
VP
6019 {
6020 struct symbol *sym
6021 = find_pc_sect_function (loc->address, loc->section);
6022 if (sym)
6023 {
112e8700
SM
6024 uiout->text ("in ");
6025 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6026 uiout->text (" ");
6027 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6028 uiout->text ("at ");
0d381245 6029 }
112e8700 6030 uiout->field_string ("file",
05cba821 6031 symtab_to_filename_for_display (loc->symtab));
112e8700 6032 uiout->text (":");
05cba821 6033
112e8700
SM
6034 if (uiout->is_mi_like_p ())
6035 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6036
112e8700 6037 uiout->field_int ("line", loc->line_number);
0d381245 6038 }
859825b8 6039 else if (loc)
0d381245 6040 {
d7e74731 6041 string_file stb;
170b53b2 6042
d7e74731 6043 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6044 demangle, "");
112e8700 6045 uiout->field_stream ("at", stb);
0d381245 6046 }
859825b8 6047 else
f00aae0f 6048 {
d28cd78a
TT
6049 uiout->field_string ("pending",
6050 event_location_to_string (b->location.get ()));
f00aae0f
KS
6051 /* If extra_string is available, it could be holding a condition
6052 or dprintf arguments. In either case, make sure it is printed,
6053 too, but only for non-MI streams. */
112e8700 6054 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6055 {
6056 if (b->type == bp_dprintf)
112e8700 6057 uiout->text (",");
f00aae0f 6058 else
112e8700
SM
6059 uiout->text (" ");
6060 uiout->text (b->extra_string);
f00aae0f
KS
6061 }
6062 }
6c95b8df 6063
b775012e
LM
6064 if (loc && is_breakpoint (b)
6065 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6066 && bp_condition_evaluator (b) == condition_evaluation_both)
6067 {
112e8700
SM
6068 uiout->text (" (");
6069 uiout->field_string ("evaluated-by",
b775012e 6070 bp_location_condition_evaluator (loc));
112e8700 6071 uiout->text (")");
b775012e 6072 }
0d381245
VP
6073}
6074
269b11a2
PA
6075static const char *
6076bptype_string (enum bptype type)
c906108c 6077{
c4093a6a
JM
6078 struct ep_type_description
6079 {
6080 enum bptype type;
a121b7c1 6081 const char *description;
c4093a6a
JM
6082 };
6083 static struct ep_type_description bptypes[] =
c906108c 6084 {
c5aa993b
JM
6085 {bp_none, "?deleted?"},
6086 {bp_breakpoint, "breakpoint"},
c906108c 6087 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6088 {bp_single_step, "sw single-step"},
c5aa993b
JM
6089 {bp_until, "until"},
6090 {bp_finish, "finish"},
6091 {bp_watchpoint, "watchpoint"},
c906108c 6092 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6093 {bp_read_watchpoint, "read watchpoint"},
6094 {bp_access_watchpoint, "acc watchpoint"},
6095 {bp_longjmp, "longjmp"},
6096 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6097 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6098 {bp_exception, "exception"},
6099 {bp_exception_resume, "exception resume"},
c5aa993b 6100 {bp_step_resume, "step resume"},
2c03e5be 6101 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6102 {bp_watchpoint_scope, "watchpoint scope"},
6103 {bp_call_dummy, "call dummy"},
aa7d318d 6104 {bp_std_terminate, "std::terminate"},
c5aa993b 6105 {bp_shlib_event, "shlib events"},
c4093a6a 6106 {bp_thread_event, "thread events"},
1900040c 6107 {bp_overlay_event, "overlay events"},
0fd8e87f 6108 {bp_longjmp_master, "longjmp master"},
aa7d318d 6109 {bp_std_terminate_master, "std::terminate master"},
186c406b 6110 {bp_exception_master, "exception master"},
ce78b96d 6111 {bp_catchpoint, "catchpoint"},
1042e4c0 6112 {bp_tracepoint, "tracepoint"},
7a697b8d 6113 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6114 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6115 {bp_dprintf, "dprintf"},
4efc6507 6116 {bp_jit_event, "jit events"},
0e30163f
JK
6117 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6118 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6119 };
269b11a2
PA
6120
6121 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6122 || ((int) type != bptypes[(int) type].type))
6123 internal_error (__FILE__, __LINE__,
6124 _("bptypes table does not describe type #%d."),
6125 (int) type);
6126
6127 return bptypes[(int) type].description;
6128}
6129
998580f1
MK
6130/* For MI, output a field named 'thread-groups' with a list as the value.
6131 For CLI, prefix the list with the string 'inf'. */
6132
6133static void
6134output_thread_groups (struct ui_out *uiout,
6135 const char *field_name,
6136 VEC(int) *inf_num,
6137 int mi_only)
6138{
112e8700 6139 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6140 int inf;
6141 int i;
6142
6143 /* For backward compatibility, don't display inferiors in CLI unless
6144 there are several. Always display them for MI. */
6145 if (!is_mi && mi_only)
6146 return;
6147
10f489e5 6148 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6149
998580f1
MK
6150 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6151 {
6152 if (is_mi)
6153 {
6154 char mi_group[10];
6155
6156 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6157 uiout->field_string (NULL, mi_group);
998580f1
MK
6158 }
6159 else
6160 {
6161 if (i == 0)
112e8700 6162 uiout->text (" inf ");
998580f1 6163 else
112e8700 6164 uiout->text (", ");
998580f1 6165
112e8700 6166 uiout->text (plongest (inf));
998580f1
MK
6167 }
6168 }
998580f1
MK
6169}
6170
269b11a2
PA
6171/* Print B to gdb_stdout. */
6172
6173static void
6174print_one_breakpoint_location (struct breakpoint *b,
6175 struct bp_location *loc,
6176 int loc_number,
6177 struct bp_location **last_loc,
269b11a2
PA
6178 int allflag)
6179{
6180 struct command_line *l;
c2c6d25f 6181 static char bpenables[] = "nynny";
c906108c 6182
79a45e25 6183 struct ui_out *uiout = current_uiout;
0d381245
VP
6184 int header_of_multiple = 0;
6185 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6186 struct value_print_options opts;
6187
6188 get_user_print_options (&opts);
0d381245
VP
6189
6190 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6191 /* See comment in print_one_breakpoint concerning treatment of
6192 breakpoints with single disabled location. */
0d381245
VP
6193 if (loc == NULL
6194 && (b->loc != NULL
6195 && (b->loc->next != NULL || !b->loc->enabled)))
6196 header_of_multiple = 1;
6197 if (loc == NULL)
6198 loc = b->loc;
6199
c4093a6a
JM
6200 annotate_record ();
6201
6202 /* 1 */
6203 annotate_field (0);
0d381245
VP
6204 if (part_of_multiple)
6205 {
6206 char *formatted;
0c6773c1 6207 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6208 uiout->field_string ("number", formatted);
0d381245
VP
6209 xfree (formatted);
6210 }
6211 else
6212 {
112e8700 6213 uiout->field_int ("number", b->number);
0d381245 6214 }
c4093a6a
JM
6215
6216 /* 2 */
6217 annotate_field (1);
0d381245 6218 if (part_of_multiple)
112e8700 6219 uiout->field_skip ("type");
269b11a2 6220 else
112e8700 6221 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6222
6223 /* 3 */
6224 annotate_field (2);
0d381245 6225 if (part_of_multiple)
112e8700 6226 uiout->field_skip ("disp");
0d381245 6227 else
112e8700 6228 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6229
c4093a6a
JM
6230
6231 /* 4 */
6232 annotate_field (3);
0d381245 6233 if (part_of_multiple)
112e8700 6234 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6235 else
112e8700
SM
6236 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6237 uiout->spaces (2);
0d381245 6238
c4093a6a
JM
6239
6240 /* 5 and 6 */
3086aeae 6241 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6242 {
4a64f543
MS
6243 /* Although the print_one can possibly print all locations,
6244 calling it here is not likely to get any nice result. So,
6245 make sure there's just one location. */
0d381245 6246 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6247 b->ops->print_one (b, last_loc);
0d381245 6248 }
3086aeae
DJ
6249 else
6250 switch (b->type)
6251 {
6252 case bp_none:
6253 internal_error (__FILE__, __LINE__,
e2e0b3e5 6254 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6255 break;
c906108c 6256
3086aeae
DJ
6257 case bp_watchpoint:
6258 case bp_hardware_watchpoint:
6259 case bp_read_watchpoint:
6260 case bp_access_watchpoint:
3a5c3e22
PA
6261 {
6262 struct watchpoint *w = (struct watchpoint *) b;
6263
6264 /* Field 4, the address, is omitted (which makes the columns
6265 not line up too nicely with the headers, but the effect
6266 is relatively readable). */
6267 if (opts.addressprint)
112e8700 6268 uiout->field_skip ("addr");
3a5c3e22 6269 annotate_field (5);
112e8700 6270 uiout->field_string ("what", w->exp_string);
3a5c3e22 6271 }
3086aeae
DJ
6272 break;
6273
3086aeae
DJ
6274 case bp_breakpoint:
6275 case bp_hardware_breakpoint:
7c16b83e 6276 case bp_single_step:
3086aeae
DJ
6277 case bp_until:
6278 case bp_finish:
6279 case bp_longjmp:
6280 case bp_longjmp_resume:
e2e4d78b 6281 case bp_longjmp_call_dummy:
186c406b
TT
6282 case bp_exception:
6283 case bp_exception_resume:
3086aeae 6284 case bp_step_resume:
2c03e5be 6285 case bp_hp_step_resume:
3086aeae
DJ
6286 case bp_watchpoint_scope:
6287 case bp_call_dummy:
aa7d318d 6288 case bp_std_terminate:
3086aeae
DJ
6289 case bp_shlib_event:
6290 case bp_thread_event:
6291 case bp_overlay_event:
0fd8e87f 6292 case bp_longjmp_master:
aa7d318d 6293 case bp_std_terminate_master:
186c406b 6294 case bp_exception_master:
1042e4c0 6295 case bp_tracepoint:
7a697b8d 6296 case bp_fast_tracepoint:
0fb4aa4b 6297 case bp_static_tracepoint:
e7e0cddf 6298 case bp_dprintf:
4efc6507 6299 case bp_jit_event:
0e30163f
JK
6300 case bp_gnu_ifunc_resolver:
6301 case bp_gnu_ifunc_resolver_return:
79a45b7d 6302 if (opts.addressprint)
3086aeae
DJ
6303 {
6304 annotate_field (4);
54e52265 6305 if (header_of_multiple)
112e8700 6306 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6307 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6308 uiout->field_string ("addr", "<PENDING>");
0101ce28 6309 else
112e8700 6310 uiout->field_core_addr ("addr",
5af949e3 6311 loc->gdbarch, loc->address);
3086aeae
DJ
6312 }
6313 annotate_field (5);
0d381245 6314 if (!header_of_multiple)
170b53b2 6315 print_breakpoint_location (b, loc);
0d381245 6316 if (b->loc)
a6d9a66e 6317 *last_loc = b->loc;
3086aeae
DJ
6318 break;
6319 }
c906108c 6320
6c95b8df 6321
998580f1 6322 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6323 {
6324 struct inferior *inf;
998580f1
MK
6325 VEC(int) *inf_num = NULL;
6326 int mi_only = 1;
6c95b8df 6327
998580f1 6328 ALL_INFERIORS (inf)
6c95b8df
PA
6329 {
6330 if (inf->pspace == loc->pspace)
998580f1 6331 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6332 }
998580f1
MK
6333
6334 /* For backward compatibility, don't display inferiors in CLI unless
6335 there are several. Always display for MI. */
6336 if (allflag
6337 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6338 && (number_of_program_spaces () > 1
6339 || number_of_inferiors () > 1)
6340 /* LOC is for existing B, it cannot be in
6341 moribund_locations and thus having NULL OWNER. */
6342 && loc->owner->type != bp_catchpoint))
6343 mi_only = 0;
6344 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6345 VEC_free (int, inf_num);
6c95b8df
PA
6346 }
6347
4a306c9a 6348 if (!part_of_multiple)
c4093a6a 6349 {
4a306c9a
JB
6350 if (b->thread != -1)
6351 {
6352 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6353 "stop only in" line a little further down. */
112e8700
SM
6354 uiout->text (" thread ");
6355 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6356 }
6357 else if (b->task != 0)
6358 {
112e8700
SM
6359 uiout->text (" task ");
6360 uiout->field_int ("task", b->task);
4a306c9a 6361 }
c4093a6a 6362 }
f1310107 6363
112e8700 6364 uiout->text ("\n");
f1310107 6365
348d480f 6366 if (!part_of_multiple)
f1310107
TJB
6367 b->ops->print_one_detail (b, uiout);
6368
0d381245 6369 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6370 {
6371 annotate_field (6);
112e8700 6372 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6373 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6374 the frame ID. */
112e8700 6375 uiout->field_core_addr ("frame",
5af949e3 6376 b->gdbarch, b->frame_id.stack_addr);
112e8700 6377 uiout->text ("\n");
c4093a6a
JM
6378 }
6379
28010a5d 6380 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6381 {
6382 annotate_field (7);
d77f58be 6383 if (is_tracepoint (b))
112e8700 6384 uiout->text ("\ttrace only if ");
1042e4c0 6385 else
112e8700
SM
6386 uiout->text ("\tstop only if ");
6387 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6388
6389 /* Print whether the target is doing the breakpoint's condition
6390 evaluation. If GDB is doing the evaluation, don't print anything. */
6391 if (is_breakpoint (b)
6392 && breakpoint_condition_evaluation_mode ()
6393 == condition_evaluation_target)
6394 {
112e8700
SM
6395 uiout->text (" (");
6396 uiout->field_string ("evaluated-by",
b775012e 6397 bp_condition_evaluator (b));
112e8700 6398 uiout->text (" evals)");
b775012e 6399 }
112e8700 6400 uiout->text ("\n");
0101ce28
JJ
6401 }
6402
0d381245 6403 if (!part_of_multiple && b->thread != -1)
c4093a6a 6404 {
4a64f543 6405 /* FIXME should make an annotation for this. */
112e8700
SM
6406 uiout->text ("\tstop only in thread ");
6407 if (uiout->is_mi_like_p ())
6408 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6409 else
6410 {
6411 struct thread_info *thr = find_thread_global_id (b->thread);
6412
112e8700 6413 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6414 }
112e8700 6415 uiout->text ("\n");
c4093a6a
JM
6416 }
6417
556ec64d
YQ
6418 if (!part_of_multiple)
6419 {
6420 if (b->hit_count)
31f56a27
YQ
6421 {
6422 /* FIXME should make an annotation for this. */
6423 if (is_catchpoint (b))
112e8700 6424 uiout->text ("\tcatchpoint");
31f56a27 6425 else if (is_tracepoint (b))
112e8700 6426 uiout->text ("\ttracepoint");
31f56a27 6427 else
112e8700
SM
6428 uiout->text ("\tbreakpoint");
6429 uiout->text (" already hit ");
6430 uiout->field_int ("times", b->hit_count);
31f56a27 6431 if (b->hit_count == 1)
112e8700 6432 uiout->text (" time\n");
31f56a27 6433 else
112e8700 6434 uiout->text (" times\n");
31f56a27 6435 }
556ec64d
YQ
6436 else
6437 {
31f56a27 6438 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6439 if (uiout->is_mi_like_p ())
6440 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6441 }
6442 }
8b93c638 6443
0d381245 6444 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6445 {
6446 annotate_field (8);
112e8700
SM
6447 uiout->text ("\tignore next ");
6448 uiout->field_int ("ignore", b->ignore_count);
6449 uiout->text (" hits\n");
c4093a6a 6450 }
059fb39f 6451
816338b5
SS
6452 /* Note that an enable count of 1 corresponds to "enable once"
6453 behavior, which is reported by the combination of enablement and
6454 disposition, so we don't need to mention it here. */
6455 if (!part_of_multiple && b->enable_count > 1)
6456 {
6457 annotate_field (8);
112e8700 6458 uiout->text ("\tdisable after ");
816338b5
SS
6459 /* Tweak the wording to clarify that ignore and enable counts
6460 are distinct, and have additive effect. */
6461 if (b->ignore_count)
112e8700 6462 uiout->text ("additional ");
816338b5 6463 else
112e8700
SM
6464 uiout->text ("next ");
6465 uiout->field_int ("enable", b->enable_count);
6466 uiout->text (" hits\n");
816338b5
SS
6467 }
6468
f196051f
SS
6469 if (!part_of_multiple && is_tracepoint (b))
6470 {
6471 struct tracepoint *tp = (struct tracepoint *) b;
6472
6473 if (tp->traceframe_usage)
6474 {
112e8700
SM
6475 uiout->text ("\ttrace buffer usage ");
6476 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6477 uiout->text (" bytes\n");
f196051f
SS
6478 }
6479 }
d3ce09f5 6480
d1b0a7bf 6481 l = b->commands ? b->commands.get () : NULL;
059fb39f 6482 if (!part_of_multiple && l)
c4093a6a
JM
6483 {
6484 annotate_field (9);
2e783024 6485 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6486 print_command_lines (uiout, l, 4);
c4093a6a 6487 }
d24317b4 6488
d9b3f62e 6489 if (is_tracepoint (b))
1042e4c0 6490 {
d9b3f62e
PA
6491 struct tracepoint *t = (struct tracepoint *) b;
6492
6493 if (!part_of_multiple && t->pass_count)
6494 {
6495 annotate_field (10);
112e8700
SM
6496 uiout->text ("\tpass count ");
6497 uiout->field_int ("pass", t->pass_count);
6498 uiout->text (" \n");
d9b3f62e 6499 }
f2a8bc8a
YQ
6500
6501 /* Don't display it when tracepoint or tracepoint location is
6502 pending. */
6503 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6504 {
6505 annotate_field (11);
6506
112e8700
SM
6507 if (uiout->is_mi_like_p ())
6508 uiout->field_string ("installed",
f2a8bc8a
YQ
6509 loc->inserted ? "y" : "n");
6510 else
6511 {
6512 if (loc->inserted)
112e8700 6513 uiout->text ("\t");
f2a8bc8a 6514 else
112e8700
SM
6515 uiout->text ("\tnot ");
6516 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6517 }
6518 }
1042e4c0
SS
6519 }
6520
112e8700 6521 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6522 {
3a5c3e22
PA
6523 if (is_watchpoint (b))
6524 {
6525 struct watchpoint *w = (struct watchpoint *) b;
6526
112e8700 6527 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6528 }
f00aae0f 6529 else if (b->location != NULL
d28cd78a 6530 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6531 uiout->field_string ("original-location",
d28cd78a 6532 event_location_to_string (b->location.get ()));
d24317b4 6533 }
c4093a6a 6534}
c5aa993b 6535
0d381245
VP
6536static void
6537print_one_breakpoint (struct breakpoint *b,
4a64f543 6538 struct bp_location **last_loc,
6c95b8df 6539 int allflag)
0d381245 6540{
79a45e25 6541 struct ui_out *uiout = current_uiout;
8d3788bd 6542
2e783024
TT
6543 {
6544 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6545
2e783024
TT
6546 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6547 }
0d381245
VP
6548
6549 /* If this breakpoint has custom print function,
6550 it's already printed. Otherwise, print individual
6551 locations, if any. */
6552 if (b->ops == NULL || b->ops->print_one == NULL)
6553 {
4a64f543
MS
6554 /* If breakpoint has a single location that is disabled, we
6555 print it as if it had several locations, since otherwise it's
6556 hard to represent "breakpoint enabled, location disabled"
6557 situation.
6558
6559 Note that while hardware watchpoints have several locations
a3be7890 6560 internally, that's not a property exposed to user. */
0d381245 6561 if (b->loc
a5606eee 6562 && !is_hardware_watchpoint (b)
8d3788bd 6563 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6564 {
6565 struct bp_location *loc;
6566 int n = 1;
8d3788bd 6567
0d381245 6568 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6569 {
2e783024 6570 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6571 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6572 }
0d381245
VP
6573 }
6574 }
6575}
6576
a6d9a66e
UW
6577static int
6578breakpoint_address_bits (struct breakpoint *b)
6579{
6580 int print_address_bits = 0;
6581 struct bp_location *loc;
6582
c6d81124
PA
6583 /* Software watchpoints that aren't watching memory don't have an
6584 address to print. */
6585 if (is_no_memory_software_watchpoint (b))
6586 return 0;
6587
a6d9a66e
UW
6588 for (loc = b->loc; loc; loc = loc->next)
6589 {
c7437ca6
PA
6590 int addr_bit;
6591
c7437ca6 6592 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6593 if (addr_bit > print_address_bits)
6594 print_address_bits = addr_bit;
6595 }
6596
6597 return print_address_bits;
6598}
0d381245 6599
c4093a6a
JM
6600struct captured_breakpoint_query_args
6601 {
6602 int bnum;
6603 };
c5aa993b 6604
c4093a6a 6605static int
2b65245e 6606do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6607{
9a3c8263
SM
6608 struct captured_breakpoint_query_args *args
6609 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6610 struct breakpoint *b;
a6d9a66e 6611 struct bp_location *dummy_loc = NULL;
cc59ec59 6612
c4093a6a
JM
6613 ALL_BREAKPOINTS (b)
6614 {
6615 if (args->bnum == b->number)
c5aa993b 6616 {
12c5a436 6617 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6618 return GDB_RC_OK;
c5aa993b 6619 }
c4093a6a
JM
6620 }
6621 return GDB_RC_NONE;
6622}
c5aa993b 6623
c4093a6a 6624enum gdb_rc
4a64f543
MS
6625gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6626 char **error_message)
c4093a6a
JM
6627{
6628 struct captured_breakpoint_query_args args;
cc59ec59 6629
c4093a6a
JM
6630 args.bnum = bnum;
6631 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6632 an error. */
b0b13bb4
DJ
6633 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6634 error_message, RETURN_MASK_ALL) < 0)
6635 return GDB_RC_FAIL;
6636 else
6637 return GDB_RC_OK;
c4093a6a 6638}
c5aa993b 6639
09d682a4
TT
6640/* Return true if this breakpoint was set by the user, false if it is
6641 internal or momentary. */
6642
6643int
6644user_breakpoint_p (struct breakpoint *b)
6645{
46c6471b 6646 return b->number > 0;
09d682a4
TT
6647}
6648
93daf339
TT
6649/* See breakpoint.h. */
6650
6651int
6652pending_breakpoint_p (struct breakpoint *b)
6653{
6654 return b->loc == NULL;
6655}
6656
7f3b0473 6657/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6658 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6659 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6660 FILTER is non-NULL, call it on each breakpoint and only include the
6661 ones for which it returns non-zero. Return the total number of
6662 breakpoints listed. */
c906108c 6663
d77f58be 6664static int
4495129a 6665breakpoint_1 (const char *args, int allflag,
4a64f543 6666 int (*filter) (const struct breakpoint *))
c4093a6a 6667{
52f0bd74 6668 struct breakpoint *b;
a6d9a66e 6669 struct bp_location *last_loc = NULL;
7f3b0473 6670 int nr_printable_breakpoints;
79a45b7d 6671 struct value_print_options opts;
a6d9a66e 6672 int print_address_bits = 0;
269b11a2 6673 int print_type_col_width = 14;
79a45e25 6674 struct ui_out *uiout = current_uiout;
269b11a2 6675
79a45b7d
TT
6676 get_user_print_options (&opts);
6677
4a64f543
MS
6678 /* Compute the number of rows in the table, as well as the size
6679 required for address fields. */
7f3b0473
AC
6680 nr_printable_breakpoints = 0;
6681 ALL_BREAKPOINTS (b)
e5a67952
MS
6682 {
6683 /* If we have a filter, only list the breakpoints it accepts. */
6684 if (filter && !filter (b))
6685 continue;
6686
6687 /* If we have an "args" string, it is a list of breakpoints to
6688 accept. Skip the others. */
6689 if (args != NULL && *args != '\0')
6690 {
6691 if (allflag && parse_and_eval_long (args) != b->number)
6692 continue;
6693 if (!allflag && !number_is_in_list (args, b->number))
6694 continue;
6695 }
269b11a2 6696
e5a67952
MS
6697 if (allflag || user_breakpoint_p (b))
6698 {
6699 int addr_bit, type_len;
a6d9a66e 6700
e5a67952
MS
6701 addr_bit = breakpoint_address_bits (b);
6702 if (addr_bit > print_address_bits)
6703 print_address_bits = addr_bit;
269b11a2 6704
e5a67952
MS
6705 type_len = strlen (bptype_string (b->type));
6706 if (type_len > print_type_col_width)
6707 print_type_col_width = type_len;
6708
6709 nr_printable_breakpoints++;
6710 }
6711 }
7f3b0473 6712
4a2b031d
TT
6713 {
6714 ui_out_emit_table table_emitter (uiout,
6715 opts.addressprint ? 6 : 5,
6716 nr_printable_breakpoints,
6717 "BreakpointTable");
6718
6719 if (nr_printable_breakpoints > 0)
6720 annotate_breakpoints_headers ();
6721 if (nr_printable_breakpoints > 0)
6722 annotate_field (0);
6723 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6724 if (nr_printable_breakpoints > 0)
6725 annotate_field (1);
6726 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6727 if (nr_printable_breakpoints > 0)
6728 annotate_field (2);
6729 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6730 if (nr_printable_breakpoints > 0)
6731 annotate_field (3);
6732 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6733 if (opts.addressprint)
6734 {
6735 if (nr_printable_breakpoints > 0)
6736 annotate_field (4);
6737 if (print_address_bits <= 32)
6738 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6739 else
6740 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6741 }
6742 if (nr_printable_breakpoints > 0)
6743 annotate_field (5);
6744 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6745 uiout->table_body ();
6746 if (nr_printable_breakpoints > 0)
6747 annotate_breakpoints_table ();
6748
6749 ALL_BREAKPOINTS (b)
6750 {
6751 QUIT;
6752 /* If we have a filter, only list the breakpoints it accepts. */
6753 if (filter && !filter (b))
6754 continue;
e5a67952 6755
4a2b031d
TT
6756 /* If we have an "args" string, it is a list of breakpoints to
6757 accept. Skip the others. */
e5a67952 6758
4a2b031d
TT
6759 if (args != NULL && *args != '\0')
6760 {
6761 if (allflag) /* maintenance info breakpoint */
6762 {
6763 if (parse_and_eval_long (args) != b->number)
6764 continue;
6765 }
6766 else /* all others */
6767 {
6768 if (!number_is_in_list (args, b->number))
6769 continue;
6770 }
6771 }
6772 /* We only print out user settable breakpoints unless the
6773 allflag is set. */
6774 if (allflag || user_breakpoint_p (b))
6775 print_one_breakpoint (b, &last_loc, allflag);
6776 }
6777 }
698384cd 6778
7f3b0473 6779 if (nr_printable_breakpoints == 0)
c906108c 6780 {
4a64f543
MS
6781 /* If there's a filter, let the caller decide how to report
6782 empty list. */
d77f58be
SS
6783 if (!filter)
6784 {
e5a67952 6785 if (args == NULL || *args == '\0')
112e8700 6786 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6787 else
112e8700 6788 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6789 args);
d77f58be 6790 }
c906108c
SS
6791 }
6792 else
c4093a6a 6793 {
a6d9a66e
UW
6794 if (last_loc && !server_command)
6795 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6796 }
c906108c 6797
4a64f543 6798 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6799 there have been breakpoints? */
c906108c 6800 annotate_breakpoints_table_end ();
d77f58be
SS
6801
6802 return nr_printable_breakpoints;
c906108c
SS
6803}
6804
ad443146
SS
6805/* Display the value of default-collect in a way that is generally
6806 compatible with the breakpoint list. */
6807
6808static void
6809default_collect_info (void)
6810{
79a45e25
PA
6811 struct ui_out *uiout = current_uiout;
6812
ad443146
SS
6813 /* If it has no value (which is frequently the case), say nothing; a
6814 message like "No default-collect." gets in user's face when it's
6815 not wanted. */
6816 if (!*default_collect)
6817 return;
6818
6819 /* The following phrase lines up nicely with per-tracepoint collect
6820 actions. */
112e8700
SM
6821 uiout->text ("default collect ");
6822 uiout->field_string ("default-collect", default_collect);
6823 uiout->text (" \n");
ad443146
SS
6824}
6825
c906108c 6826static void
11db9430 6827info_breakpoints_command (char *args, int from_tty)
c906108c 6828{
e5a67952 6829 breakpoint_1 (args, 0, NULL);
ad443146
SS
6830
6831 default_collect_info ();
d77f58be
SS
6832}
6833
6834static void
11db9430 6835info_watchpoints_command (char *args, int from_tty)
d77f58be 6836{
e5a67952 6837 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6838 struct ui_out *uiout = current_uiout;
d77f58be
SS
6839
6840 if (num_printed == 0)
6841 {
e5a67952 6842 if (args == NULL || *args == '\0')
112e8700 6843 uiout->message ("No watchpoints.\n");
d77f58be 6844 else
112e8700 6845 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6846 }
c906108c
SS
6847}
6848
7a292a7a 6849static void
4495129a 6850maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6851{
e5a67952 6852 breakpoint_1 (args, 1, NULL);
ad443146
SS
6853
6854 default_collect_info ();
c906108c
SS
6855}
6856
0d381245 6857static int
714835d5 6858breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6859 struct program_space *pspace,
714835d5 6860 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6861{
6862 struct bp_location *bl = b->loc;
cc59ec59 6863
0d381245
VP
6864 for (; bl; bl = bl->next)
6865 {
6c95b8df
PA
6866 if (bl->pspace == pspace
6867 && bl->address == pc
0d381245
VP
6868 && (!overlay_debugging || bl->section == section))
6869 return 1;
6870 }
6871 return 0;
6872}
6873
672f9b60 6874/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6875 concerns with logical breakpoints, so we match program spaces, not
6876 address spaces. */
c906108c
SS
6877
6878static void
6c95b8df
PA
6879describe_other_breakpoints (struct gdbarch *gdbarch,
6880 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6881 struct obj_section *section, int thread)
c906108c 6882{
52f0bd74
AC
6883 int others = 0;
6884 struct breakpoint *b;
c906108c
SS
6885
6886 ALL_BREAKPOINTS (b)
672f9b60
KP
6887 others += (user_breakpoint_p (b)
6888 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6889 if (others > 0)
6890 {
a3f17187
AC
6891 if (others == 1)
6892 printf_filtered (_("Note: breakpoint "));
6893 else /* if (others == ???) */
6894 printf_filtered (_("Note: breakpoints "));
c906108c 6895 ALL_BREAKPOINTS (b)
672f9b60 6896 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6897 {
6898 others--;
6899 printf_filtered ("%d", b->number);
6900 if (b->thread == -1 && thread != -1)
6901 printf_filtered (" (all threads)");
6902 else if (b->thread != -1)
6903 printf_filtered (" (thread %d)", b->thread);
6904 printf_filtered ("%s%s ",
059fb39f 6905 ((b->enable_state == bp_disabled
f8eba3c6 6906 || b->enable_state == bp_call_disabled)
0d381245 6907 ? " (disabled)"
0d381245
VP
6908 : ""),
6909 (others > 1) ? ","
6910 : ((others == 1) ? " and" : ""));
6911 }
a3f17187 6912 printf_filtered (_("also set at pc "));
5af949e3 6913 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6914 printf_filtered (".\n");
6915 }
6916}
6917\f
c906108c 6918
e4f237da 6919/* Return true iff it is meaningful to use the address member of
244558af
LM
6920 BPT locations. For some breakpoint types, the locations' address members
6921 are irrelevant and it makes no sense to attempt to compare them to other
6922 addresses (or use them for any other purpose either).
e4f237da 6923
4a64f543 6924 More specifically, each of the following breakpoint types will
244558af 6925 always have a zero valued location address and we don't want to mark
4a64f543 6926 breakpoints of any of these types to be a duplicate of an actual
244558af 6927 breakpoint location at address zero:
e4f237da
KB
6928
6929 bp_watchpoint
2d134ed3
PA
6930 bp_catchpoint
6931
6932*/
e4f237da
KB
6933
6934static int
6935breakpoint_address_is_meaningful (struct breakpoint *bpt)
6936{
6937 enum bptype type = bpt->type;
6938
2d134ed3
PA
6939 return (type != bp_watchpoint && type != bp_catchpoint);
6940}
6941
6942/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6943 true if LOC1 and LOC2 represent the same watchpoint location. */
6944
6945static int
4a64f543
MS
6946watchpoint_locations_match (struct bp_location *loc1,
6947 struct bp_location *loc2)
2d134ed3 6948{
3a5c3e22
PA
6949 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6950 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6951
6952 /* Both of them must exist. */
6953 gdb_assert (w1 != NULL);
6954 gdb_assert (w2 != NULL);
2bdf28a0 6955
4a64f543
MS
6956 /* If the target can evaluate the condition expression in hardware,
6957 then we we need to insert both watchpoints even if they are at
6958 the same place. Otherwise the watchpoint will only trigger when
6959 the condition of whichever watchpoint was inserted evaluates to
6960 true, not giving a chance for GDB to check the condition of the
6961 other watchpoint. */
3a5c3e22 6962 if ((w1->cond_exp
4a64f543
MS
6963 && target_can_accel_watchpoint_condition (loc1->address,
6964 loc1->length,
0cf6dd15 6965 loc1->watchpoint_type,
4d01a485 6966 w1->cond_exp.get ()))
3a5c3e22 6967 || (w2->cond_exp
4a64f543
MS
6968 && target_can_accel_watchpoint_condition (loc2->address,
6969 loc2->length,
0cf6dd15 6970 loc2->watchpoint_type,
4d01a485 6971 w2->cond_exp.get ())))
0cf6dd15
TJB
6972 return 0;
6973
85d721b8
PA
6974 /* Note that this checks the owner's type, not the location's. In
6975 case the target does not support read watchpoints, but does
6976 support access watchpoints, we'll have bp_read_watchpoint
6977 watchpoints with hw_access locations. Those should be considered
6978 duplicates of hw_read locations. The hw_read locations will
6979 become hw_access locations later. */
2d134ed3
PA
6980 return (loc1->owner->type == loc2->owner->type
6981 && loc1->pspace->aspace == loc2->pspace->aspace
6982 && loc1->address == loc2->address
6983 && loc1->length == loc2->length);
e4f237da
KB
6984}
6985
31e77af2 6986/* See breakpoint.h. */
6c95b8df 6987
31e77af2 6988int
6c95b8df
PA
6989breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6990 struct address_space *aspace2, CORE_ADDR addr2)
6991{
f5656ead 6992 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6993 || aspace1 == aspace2)
6994 && addr1 == addr2);
6995}
6996
f1310107
TJB
6997/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6998 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6999 matches ASPACE2. On targets that have global breakpoints, the address
7000 space doesn't really matter. */
7001
7002static int
7003breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7004 int len1, struct address_space *aspace2,
7005 CORE_ADDR addr2)
7006{
f5656ead 7007 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7008 || aspace1 == aspace2)
7009 && addr2 >= addr1 && addr2 < addr1 + len1);
7010}
7011
7012/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7013 a ranged breakpoint. In most targets, a match happens only if ASPACE
7014 matches the breakpoint's address space. On targets that have global
7015 breakpoints, the address space doesn't really matter. */
7016
7017static int
7018breakpoint_location_address_match (struct bp_location *bl,
7019 struct address_space *aspace,
7020 CORE_ADDR addr)
7021{
7022 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7023 aspace, addr)
7024 || (bl->length
7025 && breakpoint_address_match_range (bl->pspace->aspace,
7026 bl->address, bl->length,
7027 aspace, addr)));
7028}
7029
d35ae833
PA
7030/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7031 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7032 match happens only if ASPACE matches the breakpoint's address
7033 space. On targets that have global breakpoints, the address space
7034 doesn't really matter. */
7035
7036static int
7037breakpoint_location_address_range_overlap (struct bp_location *bl,
7038 struct address_space *aspace,
7039 CORE_ADDR addr, int len)
7040{
7041 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7042 || bl->pspace->aspace == aspace)
7043 {
7044 int bl_len = bl->length != 0 ? bl->length : 1;
7045
7046 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7047 return 1;
7048 }
7049 return 0;
7050}
7051
1e4d1764
YQ
7052/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7053 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7054 true, otherwise returns false. */
7055
7056static int
7057tracepoint_locations_match (struct bp_location *loc1,
7058 struct bp_location *loc2)
7059{
7060 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7061 /* Since tracepoint locations are never duplicated with others', tracepoint
7062 locations at the same address of different tracepoints are regarded as
7063 different locations. */
7064 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7065 else
7066 return 0;
7067}
7068
2d134ed3
PA
7069/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7070 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7071 represent the same location. */
7072
7073static int
4a64f543
MS
7074breakpoint_locations_match (struct bp_location *loc1,
7075 struct bp_location *loc2)
2d134ed3 7076{
2bdf28a0
JK
7077 int hw_point1, hw_point2;
7078
7079 /* Both of them must not be in moribund_locations. */
7080 gdb_assert (loc1->owner != NULL);
7081 gdb_assert (loc2->owner != NULL);
7082
7083 hw_point1 = is_hardware_watchpoint (loc1->owner);
7084 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7085
7086 if (hw_point1 != hw_point2)
7087 return 0;
7088 else if (hw_point1)
7089 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7090 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7091 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7092 else
f1310107
TJB
7093 /* We compare bp_location.length in order to cover ranged breakpoints. */
7094 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7095 loc2->pspace->aspace, loc2->address)
7096 && loc1->length == loc2->length);
2d134ed3
PA
7097}
7098
76897487
KB
7099static void
7100breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7101 int bnum, int have_bnum)
7102{
f63fbe86
MS
7103 /* The longest string possibly returned by hex_string_custom
7104 is 50 chars. These must be at least that big for safety. */
7105 char astr1[64];
7106 char astr2[64];
76897487 7107
bb599908
PH
7108 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7109 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7110 if (have_bnum)
8a3fe4f8 7111 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7112 bnum, astr1, astr2);
7113 else
8a3fe4f8 7114 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7115}
7116
4a64f543
MS
7117/* Adjust a breakpoint's address to account for architectural
7118 constraints on breakpoint placement. Return the adjusted address.
7119 Note: Very few targets require this kind of adjustment. For most
7120 targets, this function is simply the identity function. */
76897487
KB
7121
7122static CORE_ADDR
a6d9a66e
UW
7123adjust_breakpoint_address (struct gdbarch *gdbarch,
7124 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7125{
a6d9a66e 7126 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7127 {
7128 /* Very few targets need any kind of breakpoint adjustment. */
7129 return bpaddr;
7130 }
88f7da05
KB
7131 else if (bptype == bp_watchpoint
7132 || bptype == bp_hardware_watchpoint
7133 || bptype == bp_read_watchpoint
7134 || bptype == bp_access_watchpoint
fe798b75 7135 || bptype == bp_catchpoint)
88f7da05
KB
7136 {
7137 /* Watchpoints and the various bp_catch_* eventpoints should not
7138 have their addresses modified. */
7139 return bpaddr;
7140 }
7c16b83e
PA
7141 else if (bptype == bp_single_step)
7142 {
7143 /* Single-step breakpoints should not have their addresses
7144 modified. If there's any architectural constrain that
7145 applies to this address, then it should have already been
7146 taken into account when the breakpoint was created in the
7147 first place. If we didn't do this, stepping through e.g.,
7148 Thumb-2 IT blocks would break. */
7149 return bpaddr;
7150 }
76897487
KB
7151 else
7152 {
7153 CORE_ADDR adjusted_bpaddr;
7154
7155 /* Some targets have architectural constraints on the placement
7156 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7157 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7158
7159 /* An adjusted breakpoint address can significantly alter
7160 a user's expectations. Print a warning if an adjustment
7161 is required. */
7162 if (adjusted_bpaddr != bpaddr)
7163 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7164
7165 return adjusted_bpaddr;
7166 }
7167}
7168
5625a286 7169bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7170{
5625a286 7171 bp_location *loc = this;
7cc221ef 7172
348d480f
PA
7173 gdb_assert (ops != NULL);
7174
28010a5d
PA
7175 loc->ops = ops;
7176 loc->owner = owner;
b775012e 7177 loc->cond_bytecode = NULL;
0d381245
VP
7178 loc->shlib_disabled = 0;
7179 loc->enabled = 1;
e049a4b5 7180
28010a5d 7181 switch (owner->type)
e049a4b5
DJ
7182 {
7183 case bp_breakpoint:
7c16b83e 7184 case bp_single_step:
e049a4b5
DJ
7185 case bp_until:
7186 case bp_finish:
7187 case bp_longjmp:
7188 case bp_longjmp_resume:
e2e4d78b 7189 case bp_longjmp_call_dummy:
186c406b
TT
7190 case bp_exception:
7191 case bp_exception_resume:
e049a4b5 7192 case bp_step_resume:
2c03e5be 7193 case bp_hp_step_resume:
e049a4b5
DJ
7194 case bp_watchpoint_scope:
7195 case bp_call_dummy:
aa7d318d 7196 case bp_std_terminate:
e049a4b5
DJ
7197 case bp_shlib_event:
7198 case bp_thread_event:
7199 case bp_overlay_event:
4efc6507 7200 case bp_jit_event:
0fd8e87f 7201 case bp_longjmp_master:
aa7d318d 7202 case bp_std_terminate_master:
186c406b 7203 case bp_exception_master:
0e30163f
JK
7204 case bp_gnu_ifunc_resolver:
7205 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7206 case bp_dprintf:
e049a4b5 7207 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7208 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7209 break;
7210 case bp_hardware_breakpoint:
7211 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7212 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7213 break;
7214 case bp_hardware_watchpoint:
7215 case bp_read_watchpoint:
7216 case bp_access_watchpoint:
7217 loc->loc_type = bp_loc_hardware_watchpoint;
7218 break;
7219 case bp_watchpoint:
ce78b96d 7220 case bp_catchpoint:
15c3d785
PA
7221 case bp_tracepoint:
7222 case bp_fast_tracepoint:
0fb4aa4b 7223 case bp_static_tracepoint:
e049a4b5
DJ
7224 loc->loc_type = bp_loc_other;
7225 break;
7226 default:
e2e0b3e5 7227 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7228 }
7229
f431efe5 7230 loc->refc = 1;
28010a5d
PA
7231}
7232
7233/* Allocate a struct bp_location. */
7234
7235static struct bp_location *
7236allocate_bp_location (struct breakpoint *bpt)
7237{
348d480f
PA
7238 return bpt->ops->allocate_location (bpt);
7239}
7cc221ef 7240
f431efe5
PA
7241static void
7242free_bp_location (struct bp_location *loc)
fe3f5fa8 7243{
348d480f 7244 loc->ops->dtor (loc);
4d01a485 7245 delete loc;
fe3f5fa8
VP
7246}
7247
f431efe5
PA
7248/* Increment reference count. */
7249
7250static void
7251incref_bp_location (struct bp_location *bl)
7252{
7253 ++bl->refc;
7254}
7255
7256/* Decrement reference count. If the reference count reaches 0,
7257 destroy the bp_location. Sets *BLP to NULL. */
7258
7259static void
7260decref_bp_location (struct bp_location **blp)
7261{
0807b50c
PA
7262 gdb_assert ((*blp)->refc > 0);
7263
f431efe5
PA
7264 if (--(*blp)->refc == 0)
7265 free_bp_location (*blp);
7266 *blp = NULL;
7267}
7268
346774a9 7269/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7270
b270e6f9
TT
7271static breakpoint *
7272add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7273{
346774a9 7274 struct breakpoint *b1;
b270e6f9 7275 struct breakpoint *result = b.get ();
c906108c 7276
346774a9
PA
7277 /* Add this breakpoint to the end of the chain so that a list of
7278 breakpoints will come out in order of increasing numbers. */
7279
7280 b1 = breakpoint_chain;
7281 if (b1 == 0)
b270e6f9 7282 breakpoint_chain = b.release ();
346774a9
PA
7283 else
7284 {
7285 while (b1->next)
7286 b1 = b1->next;
b270e6f9 7287 b1->next = b.release ();
346774a9 7288 }
b270e6f9
TT
7289
7290 return result;
346774a9
PA
7291}
7292
7293/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7294
7295static void
7296init_raw_breakpoint_without_location (struct breakpoint *b,
7297 struct gdbarch *gdbarch,
28010a5d 7298 enum bptype bptype,
c0a91b2b 7299 const struct breakpoint_ops *ops)
346774a9 7300{
348d480f
PA
7301 gdb_assert (ops != NULL);
7302
28010a5d 7303 b->ops = ops;
4d28f7a8 7304 b->type = bptype;
a6d9a66e 7305 b->gdbarch = gdbarch;
c906108c
SS
7306 b->language = current_language->la_language;
7307 b->input_radix = input_radix;
d0fb5eae 7308 b->related_breakpoint = b;
346774a9
PA
7309}
7310
7311/* Helper to set_raw_breakpoint below. Creates a breakpoint
7312 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7313
7314static struct breakpoint *
7315set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7316 enum bptype bptype,
c0a91b2b 7317 const struct breakpoint_ops *ops)
346774a9 7318{
3b0871f4 7319 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7320
3b0871f4 7321 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7322 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7323}
7324
0e30163f
JK
7325/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7326 resolutions should be made as the user specified the location explicitly
7327 enough. */
7328
0d381245 7329static void
0e30163f 7330set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7331{
2bdf28a0
JK
7332 gdb_assert (loc->owner != NULL);
7333
0d381245 7334 if (loc->owner->type == bp_breakpoint
1042e4c0 7335 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7336 || is_tracepoint (loc->owner))
0d381245 7337 {
0e30163f 7338 int is_gnu_ifunc;
2c02bd72 7339 const char *function_name;
6a3a010b 7340 CORE_ADDR func_addr;
0e30163f 7341
2c02bd72 7342 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7343 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7344
7345 if (is_gnu_ifunc && !explicit_loc)
7346 {
7347 struct breakpoint *b = loc->owner;
7348
7349 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7350 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7351 &loc->requested_address))
7352 {
7353 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7354 loc->address = adjust_breakpoint_address (loc->gdbarch,
7355 loc->requested_address,
7356 b->type);
7357 }
7358 else if (b->type == bp_breakpoint && b->loc == loc
7359 && loc->next == NULL && b->related_breakpoint == b)
7360 {
7361 /* Create only the whole new breakpoint of this type but do not
7362 mess more complicated breakpoints with multiple locations. */
7363 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7364 /* Remember the resolver's address for use by the return
7365 breakpoint. */
7366 loc->related_address = func_addr;
0e30163f
JK
7367 }
7368 }
7369
2c02bd72
DE
7370 if (function_name)
7371 loc->function_name = xstrdup (function_name);
0d381245
VP
7372 }
7373}
7374
a6d9a66e 7375/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7376struct gdbarch *
a6d9a66e
UW
7377get_sal_arch (struct symtab_and_line sal)
7378{
7379 if (sal.section)
7380 return get_objfile_arch (sal.section->objfile);
7381 if (sal.symtab)
eb822aa6 7382 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7383
7384 return NULL;
7385}
7386
346774a9
PA
7387/* Low level routine for partially initializing a breakpoint of type
7388 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7389 file name, and line number are provided by SAL.
0d381245
VP
7390
7391 It is expected that the caller will complete the initialization of
7392 the newly created breakpoint struct as well as output any status
c56053d2 7393 information regarding the creation of a new breakpoint. */
0d381245 7394
346774a9
PA
7395static void
7396init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7397 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7398 const struct breakpoint_ops *ops)
0d381245 7399{
28010a5d 7400 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7401
3742cc8b 7402 add_location_to_breakpoint (b, &sal);
0d381245 7403
6c95b8df
PA
7404 if (bptype != bp_catchpoint)
7405 gdb_assert (sal.pspace != NULL);
7406
f8eba3c6
TT
7407 /* Store the program space that was used to set the breakpoint,
7408 except for ordinary breakpoints, which are independent of the
7409 program space. */
7410 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7411 b->pspace = sal.pspace;
346774a9 7412}
c906108c 7413
346774a9
PA
7414/* set_raw_breakpoint is a low level routine for allocating and
7415 partially initializing a breakpoint of type BPTYPE. The newly
7416 created breakpoint's address, section, source file name, and line
7417 number are provided by SAL. The newly created and partially
7418 initialized breakpoint is added to the breakpoint chain and
7419 is also returned as the value of this function.
7420
7421 It is expected that the caller will complete the initialization of
7422 the newly created breakpoint struct as well as output any status
7423 information regarding the creation of a new breakpoint. In
7424 particular, set_raw_breakpoint does NOT set the breakpoint
7425 number! Care should be taken to not allow an error to occur
7426 prior to completing the initialization of the breakpoint. If this
7427 should happen, a bogus breakpoint will be left on the chain. */
7428
7429struct breakpoint *
7430set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7431 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7432 const struct breakpoint_ops *ops)
346774a9 7433{
3b0871f4 7434 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7435
3b0871f4 7436 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7437 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7438}
7439
53a5351d 7440/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7441 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7442 initiated the operation. */
c906108c
SS
7443
7444void
186c406b 7445set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7446{
35df4500 7447 struct breakpoint *b, *b_tmp;
5d5658a1 7448 int thread = tp->global_num;
0fd8e87f
UW
7449
7450 /* To avoid having to rescan all objfile symbols at every step,
7451 we maintain a list of continually-inserted but always disabled
7452 longjmp "master" breakpoints. Here, we simply create momentary
7453 clones of those and enable them for the requested thread. */
35df4500 7454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7455 if (b->pspace == current_program_space
186c406b
TT
7456 && (b->type == bp_longjmp_master
7457 || b->type == bp_exception_master))
0fd8e87f 7458 {
06edf0c0
PA
7459 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7460 struct breakpoint *clone;
cc59ec59 7461
e2e4d78b
JK
7462 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7463 after their removal. */
06edf0c0 7464 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7465 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7466 clone->thread = thread;
7467 }
186c406b
TT
7468
7469 tp->initiating_frame = frame;
c906108c
SS
7470}
7471
611c83ae 7472/* Delete all longjmp breakpoints from THREAD. */
c906108c 7473void
611c83ae 7474delete_longjmp_breakpoint (int thread)
c906108c 7475{
35df4500 7476 struct breakpoint *b, *b_tmp;
c906108c 7477
35df4500 7478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7479 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7480 {
7481 if (b->thread == thread)
7482 delete_breakpoint (b);
7483 }
c906108c
SS
7484}
7485
f59f708a
PA
7486void
7487delete_longjmp_breakpoint_at_next_stop (int thread)
7488{
7489 struct breakpoint *b, *b_tmp;
7490
7491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7492 if (b->type == bp_longjmp || b->type == bp_exception)
7493 {
7494 if (b->thread == thread)
7495 b->disposition = disp_del_at_next_stop;
7496 }
7497}
7498
e2e4d78b
JK
7499/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7500 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7501 pointer to any of them. Return NULL if this system cannot place longjmp
7502 breakpoints. */
7503
7504struct breakpoint *
7505set_longjmp_breakpoint_for_call_dummy (void)
7506{
7507 struct breakpoint *b, *retval = NULL;
7508
7509 ALL_BREAKPOINTS (b)
7510 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7511 {
7512 struct breakpoint *new_b;
7513
7514 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7515 &momentary_breakpoint_ops,
7516 1);
5d5658a1 7517 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7518
7519 /* Link NEW_B into the chain of RETVAL breakpoints. */
7520
7521 gdb_assert (new_b->related_breakpoint == new_b);
7522 if (retval == NULL)
7523 retval = new_b;
7524 new_b->related_breakpoint = retval;
7525 while (retval->related_breakpoint != new_b->related_breakpoint)
7526 retval = retval->related_breakpoint;
7527 retval->related_breakpoint = new_b;
7528 }
7529
7530 return retval;
7531}
7532
7533/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7534 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7535 stack.
7536
7537 You should call this function only at places where it is safe to currently
7538 unwind the whole stack. Failed stack unwind would discard live dummy
7539 frames. */
7540
7541void
b67a2c6f 7542check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7543{
7544 struct breakpoint *b, *b_tmp;
7545
7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7547 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7548 {
7549 struct breakpoint *dummy_b = b->related_breakpoint;
7550
7551 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7552 dummy_b = dummy_b->related_breakpoint;
7553 if (dummy_b->type != bp_call_dummy
7554 || frame_find_by_id (dummy_b->frame_id) != NULL)
7555 continue;
7556
b67a2c6f 7557 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7558
7559 while (b->related_breakpoint != b)
7560 {
7561 if (b_tmp == b->related_breakpoint)
7562 b_tmp = b->related_breakpoint->next;
7563 delete_breakpoint (b->related_breakpoint);
7564 }
7565 delete_breakpoint (b);
7566 }
7567}
7568
1900040c
MS
7569void
7570enable_overlay_breakpoints (void)
7571{
52f0bd74 7572 struct breakpoint *b;
1900040c
MS
7573
7574 ALL_BREAKPOINTS (b)
7575 if (b->type == bp_overlay_event)
7576 {
7577 b->enable_state = bp_enabled;
44702360 7578 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7579 overlay_events_enabled = 1;
1900040c
MS
7580 }
7581}
7582
7583void
7584disable_overlay_breakpoints (void)
7585{
52f0bd74 7586 struct breakpoint *b;
1900040c
MS
7587
7588 ALL_BREAKPOINTS (b)
7589 if (b->type == bp_overlay_event)
7590 {
7591 b->enable_state = bp_disabled;
44702360 7592 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7593 overlay_events_enabled = 0;
1900040c
MS
7594 }
7595}
7596
aa7d318d
TT
7597/* Set an active std::terminate breakpoint for each std::terminate
7598 master breakpoint. */
7599void
7600set_std_terminate_breakpoint (void)
7601{
35df4500 7602 struct breakpoint *b, *b_tmp;
aa7d318d 7603
35df4500 7604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7605 if (b->pspace == current_program_space
7606 && b->type == bp_std_terminate_master)
7607 {
06edf0c0 7608 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7609 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7610 }
7611}
7612
7613/* Delete all the std::terminate breakpoints. */
7614void
7615delete_std_terminate_breakpoint (void)
7616{
35df4500 7617 struct breakpoint *b, *b_tmp;
aa7d318d 7618
35df4500 7619 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7620 if (b->type == bp_std_terminate)
7621 delete_breakpoint (b);
7622}
7623
c4093a6a 7624struct breakpoint *
a6d9a66e 7625create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7626{
7627 struct breakpoint *b;
c4093a6a 7628
06edf0c0
PA
7629 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7630 &internal_breakpoint_ops);
7631
b5de0fa7 7632 b->enable_state = bp_enabled;
f00aae0f 7633 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7634 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7635
44702360 7636 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7637
c4093a6a
JM
7638 return b;
7639}
7640
0101ce28
JJ
7641struct lang_and_radix
7642 {
7643 enum language lang;
7644 int radix;
7645 };
7646
4efc6507
DE
7647/* Create a breakpoint for JIT code registration and unregistration. */
7648
7649struct breakpoint *
7650create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7651{
2a7f3dff
PA
7652 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7653 &internal_breakpoint_ops);
4efc6507 7654}
0101ce28 7655
03673fc7
PP
7656/* Remove JIT code registration and unregistration breakpoint(s). */
7657
7658void
7659remove_jit_event_breakpoints (void)
7660{
7661 struct breakpoint *b, *b_tmp;
7662
7663 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7664 if (b->type == bp_jit_event
7665 && b->loc->pspace == current_program_space)
7666 delete_breakpoint (b);
7667}
7668
cae688ec
JJ
7669void
7670remove_solib_event_breakpoints (void)
7671{
35df4500 7672 struct breakpoint *b, *b_tmp;
cae688ec 7673
35df4500 7674 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7675 if (b->type == bp_shlib_event
7676 && b->loc->pspace == current_program_space)
cae688ec
JJ
7677 delete_breakpoint (b);
7678}
7679
f37f681c
PA
7680/* See breakpoint.h. */
7681
7682void
7683remove_solib_event_breakpoints_at_next_stop (void)
7684{
7685 struct breakpoint *b, *b_tmp;
7686
7687 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7688 if (b->type == bp_shlib_event
7689 && b->loc->pspace == current_program_space)
7690 b->disposition = disp_del_at_next_stop;
7691}
7692
04086b45
PA
7693/* Helper for create_solib_event_breakpoint /
7694 create_and_insert_solib_event_breakpoint. Allows specifying which
7695 INSERT_MODE to pass through to update_global_location_list. */
7696
7697static struct breakpoint *
7698create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7699 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7700{
7701 struct breakpoint *b;
7702
06edf0c0
PA
7703 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7704 &internal_breakpoint_ops);
04086b45 7705 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7706 return b;
7707}
7708
04086b45
PA
7709struct breakpoint *
7710create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7711{
7712 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7713}
7714
f37f681c
PA
7715/* See breakpoint.h. */
7716
7717struct breakpoint *
7718create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7719{
7720 struct breakpoint *b;
7721
04086b45
PA
7722 /* Explicitly tell update_global_location_list to insert
7723 locations. */
7724 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7725 if (!b->loc->inserted)
7726 {
7727 delete_breakpoint (b);
7728 return NULL;
7729 }
7730 return b;
7731}
7732
cae688ec
JJ
7733/* Disable any breakpoints that are on code in shared libraries. Only
7734 apply to enabled breakpoints, disabled ones can just stay disabled. */
7735
7736void
cb851954 7737disable_breakpoints_in_shlibs (void)
cae688ec 7738{
876fa593 7739 struct bp_location *loc, **locp_tmp;
cae688ec 7740
876fa593 7741 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7742 {
2bdf28a0 7743 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7744 struct breakpoint *b = loc->owner;
2bdf28a0 7745
4a64f543
MS
7746 /* We apply the check to all breakpoints, including disabled for
7747 those with loc->duplicate set. This is so that when breakpoint
7748 becomes enabled, or the duplicate is removed, gdb will try to
7749 insert all breakpoints. If we don't set shlib_disabled here,
7750 we'll try to insert those breakpoints and fail. */
1042e4c0 7751 if (((b->type == bp_breakpoint)
508ccb1f 7752 || (b->type == bp_jit_event)
1042e4c0 7753 || (b->type == bp_hardware_breakpoint)
d77f58be 7754 || (is_tracepoint (b)))
6c95b8df 7755 && loc->pspace == current_program_space
0d381245 7756 && !loc->shlib_disabled
6c95b8df 7757 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7758 )
0d381245
VP
7759 {
7760 loc->shlib_disabled = 1;
7761 }
cae688ec
JJ
7762 }
7763}
7764
63644780
NB
7765/* Disable any breakpoints and tracepoints that are in SOLIB upon
7766 notification of unloaded_shlib. Only apply to enabled breakpoints,
7767 disabled ones can just stay disabled. */
84acb35a 7768
75149521 7769static void
84acb35a
JJ
7770disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7771{
876fa593 7772 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7773 int disabled_shlib_breaks = 0;
7774
876fa593 7775 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7776 {
2bdf28a0 7777 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7778 struct breakpoint *b = loc->owner;
cc59ec59 7779
1e4d1764 7780 if (solib->pspace == loc->pspace
e2dd7057 7781 && !loc->shlib_disabled
1e4d1764
YQ
7782 && (((b->type == bp_breakpoint
7783 || b->type == bp_jit_event
7784 || b->type == bp_hardware_breakpoint)
7785 && (loc->loc_type == bp_loc_hardware_breakpoint
7786 || loc->loc_type == bp_loc_software_breakpoint))
7787 || is_tracepoint (b))
e2dd7057 7788 && solib_contains_address_p (solib, loc->address))
84acb35a 7789 {
e2dd7057
PP
7790 loc->shlib_disabled = 1;
7791 /* At this point, we cannot rely on remove_breakpoint
7792 succeeding so we must mark the breakpoint as not inserted
7793 to prevent future errors occurring in remove_breakpoints. */
7794 loc->inserted = 0;
8d3788bd
VP
7795
7796 /* This may cause duplicate notifications for the same breakpoint. */
7797 observer_notify_breakpoint_modified (b);
7798
e2dd7057
PP
7799 if (!disabled_shlib_breaks)
7800 {
223ffa71 7801 target_terminal::ours_for_output ();
3e43a32a
MS
7802 warning (_("Temporarily disabling breakpoints "
7803 "for unloaded shared library \"%s\""),
e2dd7057 7804 solib->so_name);
84acb35a 7805 }
e2dd7057 7806 disabled_shlib_breaks = 1;
84acb35a
JJ
7807 }
7808 }
84acb35a
JJ
7809}
7810
63644780
NB
7811/* Disable any breakpoints and tracepoints in OBJFILE upon
7812 notification of free_objfile. Only apply to enabled breakpoints,
7813 disabled ones can just stay disabled. */
7814
7815static void
7816disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7817{
7818 struct breakpoint *b;
7819
7820 if (objfile == NULL)
7821 return;
7822
d03de421
PA
7823 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7824 managed by the user with add-symbol-file/remove-symbol-file.
7825 Similarly to how breakpoints in shared libraries are handled in
7826 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7827 shlib_disabled so they end up uninserted on the next global
7828 location list update. Shared libraries not loaded by the user
7829 aren't handled here -- they're already handled in
7830 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7831 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7832 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7833 main objfile). */
7834 if ((objfile->flags & OBJF_SHARED) == 0
7835 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7836 return;
7837
7838 ALL_BREAKPOINTS (b)
7839 {
7840 struct bp_location *loc;
7841 int bp_modified = 0;
7842
7843 if (!is_breakpoint (b) && !is_tracepoint (b))
7844 continue;
7845
7846 for (loc = b->loc; loc != NULL; loc = loc->next)
7847 {
7848 CORE_ADDR loc_addr = loc->address;
7849
7850 if (loc->loc_type != bp_loc_hardware_breakpoint
7851 && loc->loc_type != bp_loc_software_breakpoint)
7852 continue;
7853
7854 if (loc->shlib_disabled != 0)
7855 continue;
7856
7857 if (objfile->pspace != loc->pspace)
7858 continue;
7859
7860 if (loc->loc_type != bp_loc_hardware_breakpoint
7861 && loc->loc_type != bp_loc_software_breakpoint)
7862 continue;
7863
7864 if (is_addr_in_objfile (loc_addr, objfile))
7865 {
7866 loc->shlib_disabled = 1;
08351840
PA
7867 /* At this point, we don't know whether the object was
7868 unmapped from the inferior or not, so leave the
7869 inserted flag alone. We'll handle failure to
7870 uninsert quietly, in case the object was indeed
7871 unmapped. */
63644780
NB
7872
7873 mark_breakpoint_location_modified (loc);
7874
7875 bp_modified = 1;
7876 }
7877 }
7878
7879 if (bp_modified)
7880 observer_notify_breakpoint_modified (b);
7881 }
7882}
7883
ce78b96d
JB
7884/* FORK & VFORK catchpoints. */
7885
e29a4733 7886/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7887 catchpoint. A breakpoint is really of this type iff its ops pointer points
7888 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7889
c1fc2657 7890struct fork_catchpoint : public breakpoint
e29a4733 7891{
e29a4733
PA
7892 /* Process id of a child process whose forking triggered this
7893 catchpoint. This field is only valid immediately after this
7894 catchpoint has triggered. */
7895 ptid_t forked_inferior_pid;
7896};
7897
4a64f543
MS
7898/* Implement the "insert" breakpoint_ops method for fork
7899 catchpoints. */
ce78b96d 7900
77b06cd7
TJB
7901static int
7902insert_catch_fork (struct bp_location *bl)
ce78b96d 7903{
dfd4cc63 7904 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7905}
7906
4a64f543
MS
7907/* Implement the "remove" breakpoint_ops method for fork
7908 catchpoints. */
ce78b96d
JB
7909
7910static int
73971819 7911remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7912{
dfd4cc63 7913 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7914}
7915
7916/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7917 catchpoints. */
7918
7919static int
f1310107 7920breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7921 struct address_space *aspace, CORE_ADDR bp_addr,
7922 const struct target_waitstatus *ws)
ce78b96d 7923{
e29a4733
PA
7924 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7925
f90263c1
TT
7926 if (ws->kind != TARGET_WAITKIND_FORKED)
7927 return 0;
7928
7929 c->forked_inferior_pid = ws->value.related_pid;
7930 return 1;
ce78b96d
JB
7931}
7932
4a64f543
MS
7933/* Implement the "print_it" breakpoint_ops method for fork
7934 catchpoints. */
ce78b96d
JB
7935
7936static enum print_stop_action
348d480f 7937print_it_catch_fork (bpstat bs)
ce78b96d 7938{
36dfb11c 7939 struct ui_out *uiout = current_uiout;
348d480f
PA
7940 struct breakpoint *b = bs->breakpoint_at;
7941 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7942
ce78b96d 7943 annotate_catchpoint (b->number);
f303dbd6 7944 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7945 if (b->disposition == disp_del)
112e8700 7946 uiout->text ("Temporary catchpoint ");
36dfb11c 7947 else
112e8700
SM
7948 uiout->text ("Catchpoint ");
7949 if (uiout->is_mi_like_p ())
36dfb11c 7950 {
112e8700
SM
7951 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7952 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7953 }
112e8700
SM
7954 uiout->field_int ("bkptno", b->number);
7955 uiout->text (" (forked process ");
7956 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7957 uiout->text ("), ");
ce78b96d
JB
7958 return PRINT_SRC_AND_LOC;
7959}
7960
4a64f543
MS
7961/* Implement the "print_one" breakpoint_ops method for fork
7962 catchpoints. */
ce78b96d
JB
7963
7964static void
a6d9a66e 7965print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7966{
e29a4733 7967 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7968 struct value_print_options opts;
79a45e25 7969 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7970
7971 get_user_print_options (&opts);
7972
4a64f543
MS
7973 /* Field 4, the address, is omitted (which makes the columns not
7974 line up too nicely with the headers, but the effect is relatively
7975 readable). */
79a45b7d 7976 if (opts.addressprint)
112e8700 7977 uiout->field_skip ("addr");
ce78b96d 7978 annotate_field (5);
112e8700 7979 uiout->text ("fork");
e29a4733 7980 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7981 {
112e8700
SM
7982 uiout->text (", process ");
7983 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7984 uiout->spaces (1);
ce78b96d 7985 }
8ac3646f 7986
112e8700
SM
7987 if (uiout->is_mi_like_p ())
7988 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7989}
7990
7991/* Implement the "print_mention" breakpoint_ops method for fork
7992 catchpoints. */
7993
7994static void
7995print_mention_catch_fork (struct breakpoint *b)
7996{
7997 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7998}
7999
6149aea9
PA
8000/* Implement the "print_recreate" breakpoint_ops method for fork
8001 catchpoints. */
8002
8003static void
8004print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8005{
8006 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8007 print_recreate_thread (b, fp);
6149aea9
PA
8008}
8009
ce78b96d
JB
8010/* The breakpoint_ops structure to be used in fork catchpoints. */
8011
2060206e 8012static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8013
4a64f543
MS
8014/* Implement the "insert" breakpoint_ops method for vfork
8015 catchpoints. */
ce78b96d 8016
77b06cd7
TJB
8017static int
8018insert_catch_vfork (struct bp_location *bl)
ce78b96d 8019{
dfd4cc63 8020 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8021}
8022
4a64f543
MS
8023/* Implement the "remove" breakpoint_ops method for vfork
8024 catchpoints. */
ce78b96d
JB
8025
8026static int
73971819 8027remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8028{
dfd4cc63 8029 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8030}
8031
8032/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8033 catchpoints. */
8034
8035static int
f1310107 8036breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8037 struct address_space *aspace, CORE_ADDR bp_addr,
8038 const struct target_waitstatus *ws)
ce78b96d 8039{
e29a4733
PA
8040 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8041
f90263c1
TT
8042 if (ws->kind != TARGET_WAITKIND_VFORKED)
8043 return 0;
8044
8045 c->forked_inferior_pid = ws->value.related_pid;
8046 return 1;
ce78b96d
JB
8047}
8048
4a64f543
MS
8049/* Implement the "print_it" breakpoint_ops method for vfork
8050 catchpoints. */
ce78b96d
JB
8051
8052static enum print_stop_action
348d480f 8053print_it_catch_vfork (bpstat bs)
ce78b96d 8054{
36dfb11c 8055 struct ui_out *uiout = current_uiout;
348d480f 8056 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8057 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8058
ce78b96d 8059 annotate_catchpoint (b->number);
f303dbd6 8060 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8061 if (b->disposition == disp_del)
112e8700 8062 uiout->text ("Temporary catchpoint ");
36dfb11c 8063 else
112e8700
SM
8064 uiout->text ("Catchpoint ");
8065 if (uiout->is_mi_like_p ())
36dfb11c 8066 {
112e8700
SM
8067 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8068 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8069 }
112e8700
SM
8070 uiout->field_int ("bkptno", b->number);
8071 uiout->text (" (vforked process ");
8072 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8073 uiout->text ("), ");
ce78b96d
JB
8074 return PRINT_SRC_AND_LOC;
8075}
8076
4a64f543
MS
8077/* Implement the "print_one" breakpoint_ops method for vfork
8078 catchpoints. */
ce78b96d
JB
8079
8080static void
a6d9a66e 8081print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8082{
e29a4733 8083 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8084 struct value_print_options opts;
79a45e25 8085 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8086
8087 get_user_print_options (&opts);
4a64f543
MS
8088 /* Field 4, the address, is omitted (which makes the columns not
8089 line up too nicely with the headers, but the effect is relatively
8090 readable). */
79a45b7d 8091 if (opts.addressprint)
112e8700 8092 uiout->field_skip ("addr");
ce78b96d 8093 annotate_field (5);
112e8700 8094 uiout->text ("vfork");
e29a4733 8095 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8096 {
112e8700
SM
8097 uiout->text (", process ");
8098 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8099 uiout->spaces (1);
ce78b96d 8100 }
8ac3646f 8101
112e8700
SM
8102 if (uiout->is_mi_like_p ())
8103 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8104}
8105
8106/* Implement the "print_mention" breakpoint_ops method for vfork
8107 catchpoints. */
8108
8109static void
8110print_mention_catch_vfork (struct breakpoint *b)
8111{
8112 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8113}
8114
6149aea9
PA
8115/* Implement the "print_recreate" breakpoint_ops method for vfork
8116 catchpoints. */
8117
8118static void
8119print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8120{
8121 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8122 print_recreate_thread (b, fp);
6149aea9
PA
8123}
8124
ce78b96d
JB
8125/* The breakpoint_ops structure to be used in vfork catchpoints. */
8126
2060206e 8127static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8128
edcc5120 8129/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8130 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8131 CATCH_SOLIB_BREAKPOINT_OPS. */
8132
c1fc2657 8133struct solib_catchpoint : public breakpoint
edcc5120 8134{
c1fc2657 8135 ~solib_catchpoint () override;
edcc5120
TT
8136
8137 /* True for "catch load", false for "catch unload". */
8138 unsigned char is_load;
8139
8140 /* Regular expression to match, if any. COMPILED is only valid when
8141 REGEX is non-NULL. */
8142 char *regex;
2d7cc5c7 8143 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8144};
8145
c1fc2657 8146solib_catchpoint::~solib_catchpoint ()
edcc5120 8147{
c1fc2657 8148 xfree (this->regex);
edcc5120
TT
8149}
8150
8151static int
8152insert_catch_solib (struct bp_location *ignore)
8153{
8154 return 0;
8155}
8156
8157static int
73971819 8158remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8159{
8160 return 0;
8161}
8162
8163static int
8164breakpoint_hit_catch_solib (const struct bp_location *bl,
8165 struct address_space *aspace,
8166 CORE_ADDR bp_addr,
8167 const struct target_waitstatus *ws)
8168{
8169 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8170 struct breakpoint *other;
8171
8172 if (ws->kind == TARGET_WAITKIND_LOADED)
8173 return 1;
8174
8175 ALL_BREAKPOINTS (other)
8176 {
8177 struct bp_location *other_bl;
8178
8179 if (other == bl->owner)
8180 continue;
8181
8182 if (other->type != bp_shlib_event)
8183 continue;
8184
c1fc2657 8185 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8186 continue;
8187
8188 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8189 {
8190 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8191 return 1;
8192 }
8193 }
8194
8195 return 0;
8196}
8197
8198static void
8199check_status_catch_solib (struct bpstats *bs)
8200{
8201 struct solib_catchpoint *self
8202 = (struct solib_catchpoint *) bs->breakpoint_at;
8203 int ix;
8204
8205 if (self->is_load)
8206 {
8207 struct so_list *iter;
8208
8209 for (ix = 0;
8210 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8211 ix, iter);
8212 ++ix)
8213 {
8214 if (!self->regex
2d7cc5c7 8215 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8216 return;
8217 }
8218 }
8219 else
8220 {
8221 char *iter;
8222
8223 for (ix = 0;
8224 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8225 ix, iter);
8226 ++ix)
8227 {
8228 if (!self->regex
2d7cc5c7 8229 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8230 return;
8231 }
8232 }
8233
8234 bs->stop = 0;
8235 bs->print_it = print_it_noop;
8236}
8237
8238static enum print_stop_action
8239print_it_catch_solib (bpstat bs)
8240{
8241 struct breakpoint *b = bs->breakpoint_at;
8242 struct ui_out *uiout = current_uiout;
8243
8244 annotate_catchpoint (b->number);
f303dbd6 8245 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8246 if (b->disposition == disp_del)
112e8700 8247 uiout->text ("Temporary catchpoint ");
edcc5120 8248 else
112e8700
SM
8249 uiout->text ("Catchpoint ");
8250 uiout->field_int ("bkptno", b->number);
8251 uiout->text ("\n");
8252 if (uiout->is_mi_like_p ())
8253 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8254 print_solib_event (1);
8255 return PRINT_SRC_AND_LOC;
8256}
8257
8258static void
8259print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8260{
8261 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8262 struct value_print_options opts;
8263 struct ui_out *uiout = current_uiout;
8264 char *msg;
8265
8266 get_user_print_options (&opts);
8267 /* Field 4, the address, is omitted (which makes the columns not
8268 line up too nicely with the headers, but the effect is relatively
8269 readable). */
8270 if (opts.addressprint)
8271 {
8272 annotate_field (4);
112e8700 8273 uiout->field_skip ("addr");
edcc5120
TT
8274 }
8275
8276 annotate_field (5);
8277 if (self->is_load)
8278 {
8279 if (self->regex)
8280 msg = xstrprintf (_("load of library matching %s"), self->regex);
8281 else
8282 msg = xstrdup (_("load of library"));
8283 }
8284 else
8285 {
8286 if (self->regex)
8287 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8288 else
8289 msg = xstrdup (_("unload of library"));
8290 }
112e8700 8291 uiout->field_string ("what", msg);
edcc5120 8292 xfree (msg);
8ac3646f 8293
112e8700
SM
8294 if (uiout->is_mi_like_p ())
8295 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8296}
8297
8298static void
8299print_mention_catch_solib (struct breakpoint *b)
8300{
8301 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8302
8303 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8304 self->is_load ? "load" : "unload");
8305}
8306
8307static void
8308print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8309{
8310 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8311
8312 fprintf_unfiltered (fp, "%s %s",
8313 b->disposition == disp_del ? "tcatch" : "catch",
8314 self->is_load ? "load" : "unload");
8315 if (self->regex)
8316 fprintf_unfiltered (fp, " %s", self->regex);
8317 fprintf_unfiltered (fp, "\n");
8318}
8319
8320static struct breakpoint_ops catch_solib_breakpoint_ops;
8321
91985142
MG
8322/* Shared helper function (MI and CLI) for creating and installing
8323 a shared object event catchpoint. If IS_LOAD is non-zero then
8324 the events to be caught are load events, otherwise they are
8325 unload events. If IS_TEMP is non-zero the catchpoint is a
8326 temporary one. If ENABLED is non-zero the catchpoint is
8327 created in an enabled state. */
edcc5120 8328
91985142 8329void
a121b7c1 8330add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8331{
edcc5120 8332 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8333
edcc5120
TT
8334 if (!arg)
8335 arg = "";
f1735a53 8336 arg = skip_spaces (arg);
edcc5120 8337
36bd8eaa 8338 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8339
8340 if (*arg != '\0')
8341 {
2d7cc5c7
PA
8342 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8343 _("Invalid regexp")));
edcc5120
TT
8344 c->regex = xstrdup (arg);
8345 }
8346
8347 c->is_load = is_load;
36bd8eaa 8348 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8349 &catch_solib_breakpoint_ops);
8350
c1fc2657 8351 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8352
b270e6f9 8353 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8354}
8355
91985142
MG
8356/* A helper function that does all the work for "catch load" and
8357 "catch unload". */
8358
8359static void
8360catch_load_or_unload (char *arg, int from_tty, int is_load,
8361 struct cmd_list_element *command)
8362{
8363 int tempflag;
8364 const int enabled = 1;
8365
8366 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8367
8368 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8369}
8370
edcc5120
TT
8371static void
8372catch_load_command_1 (char *arg, int from_tty,
8373 struct cmd_list_element *command)
8374{
8375 catch_load_or_unload (arg, from_tty, 1, command);
8376}
8377
8378static void
8379catch_unload_command_1 (char *arg, int from_tty,
8380 struct cmd_list_element *command)
8381{
8382 catch_load_or_unload (arg, from_tty, 0, command);
8383}
8384
346774a9
PA
8385/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8386 is non-zero, then make the breakpoint temporary. If COND_STRING is
8387 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8388 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8389
ab04a2af 8390void
346774a9
PA
8391init_catchpoint (struct breakpoint *b,
8392 struct gdbarch *gdbarch, int tempflag,
63160a43 8393 const char *cond_string,
c0a91b2b 8394 const struct breakpoint_ops *ops)
c906108c 8395{
51abb421 8396 symtab_and_line sal;
6c95b8df 8397 sal.pspace = current_program_space;
c5aa993b 8398
28010a5d 8399 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8400
1b36a34b 8401 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8402 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8403}
8404
28010a5d 8405void
b270e6f9 8406install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8407{
b270e6f9 8408 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8409 set_breakpoint_number (internal, b);
558a9d82
YQ
8410 if (is_tracepoint (b))
8411 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8412 if (!internal)
8413 mention (b);
c56053d2 8414 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8415
8416 if (update_gll)
44702360 8417 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8418}
8419
9b70b993 8420static void
a6d9a66e 8421create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8422 int tempflag, const char *cond_string,
c0a91b2b 8423 const struct breakpoint_ops *ops)
c906108c 8424{
b270e6f9 8425 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8426
b270e6f9 8427 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8428
8429 c->forked_inferior_pid = null_ptid;
8430
b270e6f9 8431 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8432}
8433
fe798b75
JB
8434/* Exec catchpoints. */
8435
b4d90040 8436/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8437 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8438 CATCH_EXEC_BREAKPOINT_OPS. */
8439
c1fc2657 8440struct exec_catchpoint : public breakpoint
b4d90040 8441{
c1fc2657 8442 ~exec_catchpoint () override;
b4d90040
PA
8443
8444 /* Filename of a program whose exec triggered this catchpoint.
8445 This field is only valid immediately after this catchpoint has
8446 triggered. */
8447 char *exec_pathname;
8448};
8449
c1fc2657 8450/* Exec catchpoint destructor. */
b4d90040 8451
c1fc2657 8452exec_catchpoint::~exec_catchpoint ()
b4d90040 8453{
c1fc2657 8454 xfree (this->exec_pathname);
b4d90040
PA
8455}
8456
77b06cd7
TJB
8457static int
8458insert_catch_exec (struct bp_location *bl)
c906108c 8459{
dfd4cc63 8460 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8461}
c906108c 8462
fe798b75 8463static int
73971819 8464remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8465{
dfd4cc63 8466 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8467}
c906108c 8468
fe798b75 8469static int
f1310107 8470breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8471 struct address_space *aspace, CORE_ADDR bp_addr,
8472 const struct target_waitstatus *ws)
fe798b75 8473{
b4d90040
PA
8474 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8475
f90263c1
TT
8476 if (ws->kind != TARGET_WAITKIND_EXECD)
8477 return 0;
8478
8479 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8480 return 1;
fe798b75 8481}
c906108c 8482
fe798b75 8483static enum print_stop_action
348d480f 8484print_it_catch_exec (bpstat bs)
fe798b75 8485{
36dfb11c 8486 struct ui_out *uiout = current_uiout;
348d480f 8487 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8488 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8489
fe798b75 8490 annotate_catchpoint (b->number);
f303dbd6 8491 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8492 if (b->disposition == disp_del)
112e8700 8493 uiout->text ("Temporary catchpoint ");
36dfb11c 8494 else
112e8700
SM
8495 uiout->text ("Catchpoint ");
8496 if (uiout->is_mi_like_p ())
36dfb11c 8497 {
112e8700
SM
8498 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8499 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8500 }
112e8700
SM
8501 uiout->field_int ("bkptno", b->number);
8502 uiout->text (" (exec'd ");
8503 uiout->field_string ("new-exec", c->exec_pathname);
8504 uiout->text ("), ");
36dfb11c 8505
fe798b75 8506 return PRINT_SRC_AND_LOC;
c906108c
SS
8507}
8508
fe798b75 8509static void
a6d9a66e 8510print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8511{
b4d90040 8512 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8513 struct value_print_options opts;
79a45e25 8514 struct ui_out *uiout = current_uiout;
fe798b75
JB
8515
8516 get_user_print_options (&opts);
8517
8518 /* Field 4, the address, is omitted (which makes the columns
8519 not line up too nicely with the headers, but the effect
8520 is relatively readable). */
8521 if (opts.addressprint)
112e8700 8522 uiout->field_skip ("addr");
fe798b75 8523 annotate_field (5);
112e8700 8524 uiout->text ("exec");
b4d90040 8525 if (c->exec_pathname != NULL)
fe798b75 8526 {
112e8700
SM
8527 uiout->text (", program \"");
8528 uiout->field_string ("what", c->exec_pathname);
8529 uiout->text ("\" ");
fe798b75 8530 }
8ac3646f 8531
112e8700
SM
8532 if (uiout->is_mi_like_p ())
8533 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8534}
8535
8536static void
8537print_mention_catch_exec (struct breakpoint *b)
8538{
8539 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8540}
8541
6149aea9
PA
8542/* Implement the "print_recreate" breakpoint_ops method for exec
8543 catchpoints. */
8544
8545static void
8546print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8547{
8548 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8549 print_recreate_thread (b, fp);
6149aea9
PA
8550}
8551
2060206e 8552static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8553
c906108c 8554static int
fba45db2 8555hw_breakpoint_used_count (void)
c906108c 8556{
c906108c 8557 int i = 0;
f1310107
TJB
8558 struct breakpoint *b;
8559 struct bp_location *bl;
c906108c
SS
8560
8561 ALL_BREAKPOINTS (b)
c5aa993b 8562 {
d6b74ac4 8563 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8564 for (bl = b->loc; bl; bl = bl->next)
8565 {
8566 /* Special types of hardware breakpoints may use more than
8567 one register. */
348d480f 8568 i += b->ops->resources_needed (bl);
f1310107 8569 }
c5aa993b 8570 }
c906108c
SS
8571
8572 return i;
8573}
8574
a1398e0c
PA
8575/* Returns the resources B would use if it were a hardware
8576 watchpoint. */
8577
c906108c 8578static int
a1398e0c 8579hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8580{
c906108c 8581 int i = 0;
e09342b5 8582 struct bp_location *bl;
c906108c 8583
a1398e0c
PA
8584 if (!breakpoint_enabled (b))
8585 return 0;
8586
8587 for (bl = b->loc; bl; bl = bl->next)
8588 {
8589 /* Special types of hardware watchpoints may use more than
8590 one register. */
8591 i += b->ops->resources_needed (bl);
8592 }
8593
8594 return i;
8595}
8596
8597/* Returns the sum the used resources of all hardware watchpoints of
8598 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8599 the sum of the used resources of all hardware watchpoints of other
8600 types _not_ TYPE. */
8601
8602static int
8603hw_watchpoint_used_count_others (struct breakpoint *except,
8604 enum bptype type, int *other_type_used)
8605{
8606 int i = 0;
8607 struct breakpoint *b;
8608
c906108c
SS
8609 *other_type_used = 0;
8610 ALL_BREAKPOINTS (b)
e09342b5 8611 {
a1398e0c
PA
8612 if (b == except)
8613 continue;
e09342b5
TJB
8614 if (!breakpoint_enabled (b))
8615 continue;
8616
a1398e0c
PA
8617 if (b->type == type)
8618 i += hw_watchpoint_use_count (b);
8619 else if (is_hardware_watchpoint (b))
8620 *other_type_used = 1;
e09342b5
TJB
8621 }
8622
c906108c
SS
8623 return i;
8624}
8625
c906108c 8626void
fba45db2 8627disable_watchpoints_before_interactive_call_start (void)
c906108c 8628{
c5aa993b 8629 struct breakpoint *b;
c906108c
SS
8630
8631 ALL_BREAKPOINTS (b)
c5aa993b 8632 {
cc60f2e3 8633 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8634 {
b5de0fa7 8635 b->enable_state = bp_call_disabled;
44702360 8636 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8637 }
8638 }
c906108c
SS
8639}
8640
8641void
fba45db2 8642enable_watchpoints_after_interactive_call_stop (void)
c906108c 8643{
c5aa993b 8644 struct breakpoint *b;
c906108c
SS
8645
8646 ALL_BREAKPOINTS (b)
c5aa993b 8647 {
cc60f2e3 8648 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8649 {
b5de0fa7 8650 b->enable_state = bp_enabled;
44702360 8651 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8652 }
8653 }
c906108c
SS
8654}
8655
8bea4e01
UW
8656void
8657disable_breakpoints_before_startup (void)
8658{
6c95b8df 8659 current_program_space->executing_startup = 1;
44702360 8660 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8661}
8662
8663void
8664enable_breakpoints_after_startup (void)
8665{
6c95b8df 8666 current_program_space->executing_startup = 0;
f8eba3c6 8667 breakpoint_re_set ();
8bea4e01
UW
8668}
8669
7c16b83e
PA
8670/* Create a new single-step breakpoint for thread THREAD, with no
8671 locations. */
c906108c 8672
7c16b83e
PA
8673static struct breakpoint *
8674new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8675{
b270e6f9 8676 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8677
b270e6f9 8678 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8679 &momentary_breakpoint_ops);
8680
8681 b->disposition = disp_donttouch;
8682 b->frame_id = null_frame_id;
8683
8684 b->thread = thread;
8685 gdb_assert (b->thread != 0);
8686
b270e6f9 8687 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8688}
8689
8690/* Set a momentary breakpoint of type TYPE at address specified by
8691 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8692 frame. */
c906108c
SS
8693
8694struct breakpoint *
a6d9a66e
UW
8695set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8696 struct frame_id frame_id, enum bptype type)
c906108c 8697{
52f0bd74 8698 struct breakpoint *b;
edb3359d 8699
193facb3
JK
8700 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8701 tail-called one. */
8702 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8703
06edf0c0 8704 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8705 b->enable_state = bp_enabled;
8706 b->disposition = disp_donttouch;
818dd999 8707 b->frame_id = frame_id;
c906108c 8708
4a64f543
MS
8709 /* If we're debugging a multi-threaded program, then we want
8710 momentary breakpoints to be active in only a single thread of
8711 control. */
39f77062 8712 if (in_thread_list (inferior_ptid))
5d5658a1 8713 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8714
44702360 8715 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8716
c906108c
SS
8717 return b;
8718}
611c83ae 8719
06edf0c0 8720/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8721 The new breakpoint will have type TYPE, use OPS as its
8722 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8723
06edf0c0
PA
8724static struct breakpoint *
8725momentary_breakpoint_from_master (struct breakpoint *orig,
8726 enum bptype type,
a1aa2221
LM
8727 const struct breakpoint_ops *ops,
8728 int loc_enabled)
e58b0e63
PA
8729{
8730 struct breakpoint *copy;
8731
06edf0c0 8732 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8733 copy->loc = allocate_bp_location (copy);
0e30163f 8734 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8735
a6d9a66e 8736 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8737 copy->loc->requested_address = orig->loc->requested_address;
8738 copy->loc->address = orig->loc->address;
8739 copy->loc->section = orig->loc->section;
6c95b8df 8740 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8741 copy->loc->probe = orig->loc->probe;
f8eba3c6 8742 copy->loc->line_number = orig->loc->line_number;
2f202fde 8743 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8744 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8745 copy->frame_id = orig->frame_id;
8746 copy->thread = orig->thread;
6c95b8df 8747 copy->pspace = orig->pspace;
e58b0e63
PA
8748
8749 copy->enable_state = bp_enabled;
8750 copy->disposition = disp_donttouch;
8751 copy->number = internal_breakpoint_number--;
8752
44702360 8753 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8754 return copy;
8755}
8756
06edf0c0
PA
8757/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8758 ORIG is NULL. */
8759
8760struct breakpoint *
8761clone_momentary_breakpoint (struct breakpoint *orig)
8762{
8763 /* If there's nothing to clone, then return nothing. */
8764 if (orig == NULL)
8765 return NULL;
8766
a1aa2221 8767 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8768}
8769
611c83ae 8770struct breakpoint *
a6d9a66e
UW
8771set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8772 enum bptype type)
611c83ae
PA
8773{
8774 struct symtab_and_line sal;
8775
8776 sal = find_pc_line (pc, 0);
8777 sal.pc = pc;
8778 sal.section = find_pc_overlay (pc);
8779 sal.explicit_pc = 1;
8780
a6d9a66e 8781 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8782}
c906108c 8783\f
c5aa993b 8784
c906108c
SS
8785/* Tell the user we have just set a breakpoint B. */
8786
8787static void
fba45db2 8788mention (struct breakpoint *b)
c906108c 8789{
348d480f 8790 b->ops->print_mention (b);
112e8700 8791 if (current_uiout->is_mi_like_p ())
fb40c209 8792 return;
c906108c
SS
8793 printf_filtered ("\n");
8794}
c906108c 8795\f
c5aa993b 8796
1a853c52
PA
8797static int bp_loc_is_permanent (struct bp_location *loc);
8798
0d381245 8799static struct bp_location *
39d61571 8800add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8801 const struct symtab_and_line *sal)
8802{
8803 struct bp_location *loc, **tmp;
3742cc8b
YQ
8804 CORE_ADDR adjusted_address;
8805 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8806
8807 if (loc_gdbarch == NULL)
8808 loc_gdbarch = b->gdbarch;
8809
8810 /* Adjust the breakpoint's address prior to allocating a location.
8811 Once we call allocate_bp_location(), that mostly uninitialized
8812 location will be placed on the location chain. Adjustment of the
8813 breakpoint may cause target_read_memory() to be called and we do
8814 not want its scan of the location chain to find a breakpoint and
8815 location that's only been partially initialized. */
8816 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8817 sal->pc, b->type);
0d381245 8818
d30113d4 8819 /* Sort the locations by their ADDRESS. */
39d61571 8820 loc = allocate_bp_location (b);
d30113d4
JK
8821 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8822 tmp = &((*tmp)->next))
0d381245 8823 ;
d30113d4 8824 loc->next = *tmp;
0d381245 8825 *tmp = loc;
3742cc8b 8826
0d381245 8827 loc->requested_address = sal->pc;
3742cc8b 8828 loc->address = adjusted_address;
6c95b8df 8829 loc->pspace = sal->pspace;
729662a5
TT
8830 loc->probe.probe = sal->probe;
8831 loc->probe.objfile = sal->objfile;
6c95b8df 8832 gdb_assert (loc->pspace != NULL);
0d381245 8833 loc->section = sal->section;
3742cc8b 8834 loc->gdbarch = loc_gdbarch;
f8eba3c6 8835 loc->line_number = sal->line;
2f202fde 8836 loc->symtab = sal->symtab;
f8eba3c6 8837
0e30163f
JK
8838 set_breakpoint_location_function (loc,
8839 sal->explicit_pc || sal->explicit_line);
1a853c52 8840
6ae88661
LM
8841 /* While by definition, permanent breakpoints are already present in the
8842 code, we don't mark the location as inserted. Normally one would expect
8843 that GDB could rely on that breakpoint instruction to stop the program,
8844 thus removing the need to insert its own breakpoint, except that executing
8845 the breakpoint instruction can kill the target instead of reporting a
8846 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8847 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8848 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8849 breakpoint be inserted normally results in QEMU knowing about the GDB
8850 breakpoint, and thus trap before the breakpoint instruction is executed.
8851 (If GDB later needs to continue execution past the permanent breakpoint,
8852 it manually increments the PC, thus avoiding executing the breakpoint
8853 instruction.) */
1a853c52 8854 if (bp_loc_is_permanent (loc))
6ae88661 8855 loc->permanent = 1;
1a853c52 8856
0d381245
VP
8857 return loc;
8858}
514f746b
AR
8859\f
8860
1cf4d951 8861/* See breakpoint.h. */
514f746b 8862
1cf4d951
PA
8863int
8864program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8865{
8866 int len;
8867 CORE_ADDR addr;
1afeeb75 8868 const gdb_byte *bpoint;
514f746b
AR
8869 gdb_byte *target_mem;
8870
1cf4d951
PA
8871 addr = address;
8872 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8873
8874 /* Software breakpoints unsupported? */
8875 if (bpoint == NULL)
8876 return 0;
8877
224c3ddb 8878 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8879
8880 /* Enable the automatic memory restoration from breakpoints while
8881 we read the memory. Otherwise we could say about our temporary
8882 breakpoints they are permanent. */
cb85b21b
TT
8883 scoped_restore restore_memory
8884 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8885
8886 if (target_read_memory (address, target_mem, len) == 0
8887 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8888 return 1;
1cf4d951 8889
cb85b21b 8890 return 0;
1cf4d951
PA
8891}
8892
8893/* Return 1 if LOC is pointing to a permanent breakpoint,
8894 return 0 otherwise. */
8895
8896static int
8897bp_loc_is_permanent (struct bp_location *loc)
8898{
514f746b
AR
8899 gdb_assert (loc != NULL);
8900
244558af
LM
8901 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8902 attempt to read from the addresses the locations of these breakpoint types
8903 point to. program_breakpoint_here_p, below, will attempt to read
8904 memory. */
8905 if (!breakpoint_address_is_meaningful (loc->owner))
8906 return 0;
8907
5ed8105e 8908 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8909 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8910 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8911}
8912
e7e0cddf
SS
8913/* Build a command list for the dprintf corresponding to the current
8914 settings of the dprintf style options. */
8915
8916static void
8917update_dprintf_command_list (struct breakpoint *b)
8918{
8919 char *dprintf_args = b->extra_string;
8920 char *printf_line = NULL;
8921
8922 if (!dprintf_args)
8923 return;
8924
8925 dprintf_args = skip_spaces (dprintf_args);
8926
8927 /* Allow a comma, as it may have terminated a location, but don't
8928 insist on it. */
8929 if (*dprintf_args == ',')
8930 ++dprintf_args;
8931 dprintf_args = skip_spaces (dprintf_args);
8932
8933 if (*dprintf_args != '"')
8934 error (_("Bad format string, missing '\"'."));
8935
d3ce09f5 8936 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8937 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8938 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8939 {
8940 if (!dprintf_function)
8941 error (_("No function supplied for dprintf call"));
8942
8943 if (dprintf_channel && strlen (dprintf_channel) > 0)
8944 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8945 dprintf_function,
8946 dprintf_channel,
8947 dprintf_args);
8948 else
8949 printf_line = xstrprintf ("call (void) %s (%s)",
8950 dprintf_function,
8951 dprintf_args);
8952 }
d3ce09f5
SS
8953 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8954 {
8955 if (target_can_run_breakpoint_commands ())
8956 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8957 else
8958 {
8959 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8960 printf_line = xstrprintf ("printf %s", dprintf_args);
8961 }
8962 }
e7e0cddf
SS
8963 else
8964 internal_error (__FILE__, __LINE__,
8965 _("Invalid dprintf style."));
8966
f28045c2 8967 gdb_assert (printf_line != NULL);
9d6e6e84 8968 /* Manufacture a printf sequence. */
f28045c2 8969 {
8d749320 8970 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8971
f28045c2
YQ
8972 printf_cmd_line->control_type = simple_control;
8973 printf_cmd_line->body_count = 0;
8974 printf_cmd_line->body_list = NULL;
9d6e6e84 8975 printf_cmd_line->next = NULL;
f28045c2 8976 printf_cmd_line->line = printf_line;
e7e0cddf 8977
93921405 8978 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8979 }
e7e0cddf
SS
8980}
8981
8982/* Update all dprintf commands, making their command lists reflect
8983 current style settings. */
8984
8985static void
8986update_dprintf_commands (char *args, int from_tty,
8987 struct cmd_list_element *c)
8988{
8989 struct breakpoint *b;
8990
8991 ALL_BREAKPOINTS (b)
8992 {
8993 if (b->type == bp_dprintf)
8994 update_dprintf_command_list (b);
8995 }
8996}
c3f6f71d 8997
f00aae0f
KS
8998/* Create a breakpoint with SAL as location. Use LOCATION
8999 as a description of the location, and COND_STRING
b35a8b2f
DE
9000 as condition expression. If LOCATION is NULL then create an
9001 "address location" from the address in the SAL. */
018d34a4
VP
9002
9003static void
d9b3f62e 9004init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 9005 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9006 event_location_up &&location,
e1e01040
PA
9007 gdb::unique_xmalloc_ptr<char> filter,
9008 gdb::unique_xmalloc_ptr<char> cond_string,
9009 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9010 enum bptype type, enum bpdisp disposition,
9011 int thread, int task, int ignore_count,
c0a91b2b 9012 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9013 int enabled, int internal, unsigned flags,
9014 int display_canonical)
018d34a4 9015{
0d381245 9016 int i;
018d34a4
VP
9017
9018 if (type == bp_hardware_breakpoint)
9019 {
fbbd034e
AS
9020 int target_resources_ok;
9021
9022 i = hw_breakpoint_used_count ();
9023 target_resources_ok =
9024 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9025 i + 1, 0);
9026 if (target_resources_ok == 0)
9027 error (_("No hardware breakpoint support in the target."));
9028 else if (target_resources_ok < 0)
9029 error (_("Hardware breakpoints used exceeds limit."));
9030 }
9031
6c5b2ebe 9032 gdb_assert (!sals.empty ());
6c95b8df 9033
6c5b2ebe 9034 for (const auto &sal : sals)
0d381245 9035 {
0d381245
VP
9036 struct bp_location *loc;
9037
9038 if (from_tty)
5af949e3
UW
9039 {
9040 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9041 if (!loc_gdbarch)
9042 loc_gdbarch = gdbarch;
9043
9044 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9045 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9046 }
0d381245 9047
6c5b2ebe 9048 if (&sal == &sals[0])
0d381245 9049 {
d9b3f62e 9050 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9051 b->thread = thread;
4a306c9a 9052 b->task = task;
855a6e68 9053
e1e01040
PA
9054 b->cond_string = cond_string.release ();
9055 b->extra_string = extra_string.release ();
0d381245 9056 b->ignore_count = ignore_count;
41447f92 9057 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9058 b->disposition = disposition;
6c95b8df 9059
44f238bb
PA
9060 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9061 b->loc->inserted = 1;
9062
0fb4aa4b
PA
9063 if (type == bp_static_tracepoint)
9064 {
d9b3f62e 9065 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9066 struct static_tracepoint_marker marker;
9067
983af33b 9068 if (strace_marker_p (b))
0fb4aa4b
PA
9069 {
9070 /* We already know the marker exists, otherwise, we
9071 wouldn't see a sal for it. */
d28cd78a
TT
9072 const char *p
9073 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9074 const char *endp;
0fb4aa4b 9075 char *marker_str;
0fb4aa4b 9076
f1735a53 9077 p = skip_spaces (p);
0fb4aa4b 9078
f1735a53 9079 endp = skip_to_space (p);
0fb4aa4b
PA
9080
9081 marker_str = savestring (p, endp - p);
d9b3f62e 9082 t->static_trace_marker_id = marker_str;
0fb4aa4b 9083
3e43a32a
MS
9084 printf_filtered (_("Probed static tracepoint "
9085 "marker \"%s\"\n"),
d9b3f62e 9086 t->static_trace_marker_id);
0fb4aa4b
PA
9087 }
9088 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9089 {
d9b3f62e 9090 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9091 release_static_tracepoint_marker (&marker);
9092
3e43a32a
MS
9093 printf_filtered (_("Probed static tracepoint "
9094 "marker \"%s\"\n"),
d9b3f62e 9095 t->static_trace_marker_id);
0fb4aa4b
PA
9096 }
9097 else
3e43a32a
MS
9098 warning (_("Couldn't determine the static "
9099 "tracepoint marker to probe"));
0fb4aa4b
PA
9100 }
9101
0d381245
VP
9102 loc = b->loc;
9103 }
9104 else
018d34a4 9105 {
39d61571 9106 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9107 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9108 loc->inserted = 1;
0d381245
VP
9109 }
9110
9111 if (b->cond_string)
9112 {
bbc13ae3
KS
9113 const char *arg = b->cond_string;
9114
1bb9788d
TT
9115 loc->cond = parse_exp_1 (&arg, loc->address,
9116 block_for_pc (loc->address), 0);
0d381245 9117 if (*arg)
588ae58c 9118 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9119 }
e7e0cddf
SS
9120
9121 /* Dynamic printf requires and uses additional arguments on the
9122 command line, otherwise it's an error. */
9123 if (type == bp_dprintf)
9124 {
9125 if (b->extra_string)
9126 update_dprintf_command_list (b);
9127 else
9128 error (_("Format string required"));
9129 }
9130 else if (b->extra_string)
588ae58c 9131 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9132 }
018d34a4 9133
56435ebe 9134 b->display_canonical = display_canonical;
f00aae0f 9135 if (location != NULL)
d28cd78a 9136 b->location = std::move (location);
018d34a4 9137 else
d28cd78a 9138 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9139 b->filter = filter.release ();
d9b3f62e 9140}
018d34a4 9141
d9b3f62e
PA
9142static void
9143create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9144 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9145 event_location_up &&location,
e1e01040
PA
9146 gdb::unique_xmalloc_ptr<char> filter,
9147 gdb::unique_xmalloc_ptr<char> cond_string,
9148 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9149 enum bptype type, enum bpdisp disposition,
9150 int thread, int task, int ignore_count,
c0a91b2b 9151 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9152 int enabled, int internal, unsigned flags,
9153 int display_canonical)
d9b3f62e 9154{
a5e364af 9155 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9156
a5e364af 9157 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9158 sals, std::move (location),
e1e01040
PA
9159 std::move (filter),
9160 std::move (cond_string),
9161 std::move (extra_string),
d9b3f62e
PA
9162 type, disposition,
9163 thread, task, ignore_count,
9164 ops, from_tty,
44f238bb
PA
9165 enabled, internal, flags,
9166 display_canonical);
d9b3f62e 9167
b270e6f9 9168 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9169}
9170
9171/* Add SALS.nelts breakpoints to the breakpoint table. For each
9172 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9173 value. COND_STRING, if not NULL, specified the condition to be
9174 used for all breakpoints. Essentially the only case where
9175 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9176 function. In that case, it's still not possible to specify
9177 separate conditions for different overloaded functions, so
9178 we take just a single condition string.
9179
c3f6f71d 9180 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9181 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9182 array contents). If the function fails (error() is called), the
9183 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9184 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9185
9186static void
8cdf0e15 9187create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9188 struct linespec_result *canonical,
e1e01040
PA
9189 gdb::unique_xmalloc_ptr<char> cond_string,
9190 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9191 enum bptype type, enum bpdisp disposition,
9192 int thread, int task, int ignore_count,
c0a91b2b 9193 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9194 int enabled, int internal, unsigned flags)
c906108c 9195{
f8eba3c6 9196 if (canonical->pre_expanded)
6c5b2ebe 9197 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9198
6c5b2ebe 9199 for (const auto &lsal : canonical->lsals)
c3f6f71d 9200 {
f00aae0f 9201 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9202 'break', without arguments. */
ffc2605c 9203 event_location_up location
f00aae0f 9204 = (canonical->location != NULL
8e9e35b1 9205 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9206 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9207 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9208
6c5b2ebe 9209 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9210 std::move (location),
e1e01040
PA
9211 std::move (filter_string),
9212 std::move (cond_string),
9213 std::move (extra_string),
e7e0cddf 9214 type, disposition,
84f4c1fe 9215 thread, task, ignore_count, ops,
44f238bb 9216 from_tty, enabled, internal, flags,
56435ebe 9217 canonical->special_display);
c3f6f71d 9218 }
c3f6f71d 9219}
c906108c 9220
f00aae0f 9221/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9222 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9223 addresses found. LOCATION points to the end of the SAL (for
9224 linespec locations).
9998af43
TJB
9225
9226 The array and the line spec strings are allocated on the heap, it is
9227 the caller's responsibility to free them. */
c906108c 9228
b9362cc7 9229static void
f00aae0f 9230parse_breakpoint_sals (const struct event_location *location,
58438ac1 9231 struct linespec_result *canonical)
c3f6f71d 9232{
f00aae0f
KS
9233 struct symtab_and_line cursal;
9234
9235 if (event_location_type (location) == LINESPEC_LOCATION)
9236 {
9237 const char *address = get_linespec_location (location);
9238
9239 if (address == NULL)
9240 {
9241 /* The last displayed codepoint, if it's valid, is our default
9242 breakpoint address. */
9243 if (last_displayed_sal_is_valid ())
9244 {
f00aae0f
KS
9245 /* Set sal's pspace, pc, symtab, and line to the values
9246 corresponding to the last call to print_frame_info.
9247 Be sure to reinitialize LINE with NOTCURRENT == 0
9248 as the breakpoint line number is inappropriate otherwise.
9249 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9250 symtab_and_line sal = get_last_displayed_sal ();
9251 CORE_ADDR pc = sal.pc;
9252
f00aae0f
KS
9253 sal = find_pc_line (pc, 0);
9254
9255 /* "break" without arguments is equivalent to "break *PC"
9256 where PC is the last displayed codepoint's address. So
9257 make sure to set sal.explicit_pc to prevent GDB from
9258 trying to expand the list of sals to include all other
9259 instances with the same symtab and line. */
9260 sal.pc = pc;
9261 sal.explicit_pc = 1;
9262
6c5b2ebe
PA
9263 struct linespec_sals lsal;
9264 lsal.sals = {sal};
f00aae0f
KS
9265 lsal.canonical = NULL;
9266
6c5b2ebe 9267 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9268 return;
9269 }
9270 else
9271 error (_("No default breakpoint address now."));
c906108c 9272 }
c906108c 9273 }
f00aae0f
KS
9274
9275 /* Force almost all breakpoints to be in terms of the
9276 current_source_symtab (which is decode_line_1's default).
9277 This should produce the results we want almost all of the
9278 time while leaving default_breakpoint_* alone.
9279
9280 ObjC: However, don't match an Objective-C method name which
9281 may have a '+' or '-' succeeded by a '['. */
9282 cursal = get_current_source_symtab_and_line ();
9283 if (last_displayed_sal_is_valid ())
c906108c 9284 {
f00aae0f 9285 const char *address = NULL;
cc80f267 9286
f00aae0f
KS
9287 if (event_location_type (location) == LINESPEC_LOCATION)
9288 address = get_linespec_location (location);
cc80f267 9289
f00aae0f
KS
9290 if (!cursal.symtab
9291 || (address != NULL
9292 && strchr ("+-", address[0]) != NULL
9293 && address[1] != '['))
9294 {
c2f4122d 9295 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9296 get_last_displayed_symtab (),
9297 get_last_displayed_line (),
9298 canonical, NULL, NULL);
9299 return;
9300 }
c906108c 9301 }
f00aae0f 9302
c2f4122d 9303 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9304 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9305}
c906108c 9306
c906108c 9307
c3f6f71d 9308/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9309 inserted as a breakpoint. If it can't throw an error. */
c906108c 9310
b9362cc7 9311static void
6c5b2ebe 9312breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9313{
6c5b2ebe
PA
9314 for (auto &sal : sals)
9315 resolve_sal_pc (&sal);
c3f6f71d
JM
9316}
9317
7a697b8d
SS
9318/* Fast tracepoints may have restrictions on valid locations. For
9319 instance, a fast tracepoint using a jump instead of a trap will
9320 likely have to overwrite more bytes than a trap would, and so can
9321 only be placed where the instruction is longer than the jump, or a
9322 multi-instruction sequence does not have a jump into the middle of
9323 it, etc. */
9324
9325static void
9326check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9327 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9328{
6c5b2ebe 9329 int rslt;
7a697b8d
SS
9330 char *msg;
9331 struct cleanup *old_chain;
9332
6c5b2ebe 9333 for (const auto &sal : sals)
7a697b8d 9334 {
f8eba3c6
TT
9335 struct gdbarch *sarch;
9336
6c5b2ebe 9337 sarch = get_sal_arch (sal);
f8eba3c6
TT
9338 /* We fall back to GDBARCH if there is no architecture
9339 associated with SAL. */
9340 if (sarch == NULL)
9341 sarch = gdbarch;
6c5b2ebe 9342 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9343 old_chain = make_cleanup (xfree, msg);
9344
9345 if (!rslt)
53c3572a 9346 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9347 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9348
9349 do_cleanups (old_chain);
9350 }
9351}
9352
018d34a4
VP
9353/* Given TOK, a string specification of condition and thread, as
9354 accepted by the 'break' command, extract the condition
9355 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9356 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9357 If no condition is found, *COND_STRING is set to NULL.
9358 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9359
9360static void
bbc13ae3 9361find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9362 char **cond_string, int *thread, int *task,
9363 char **rest)
018d34a4
VP
9364{
9365 *cond_string = NULL;
9366 *thread = -1;
ed1d1739
KS
9367 *task = 0;
9368 *rest = NULL;
9369
018d34a4
VP
9370 while (tok && *tok)
9371 {
bbc13ae3 9372 const char *end_tok;
018d34a4 9373 int toklen;
bbc13ae3
KS
9374 const char *cond_start = NULL;
9375 const char *cond_end = NULL;
cc59ec59 9376
f1735a53 9377 tok = skip_spaces (tok);
e7e0cddf
SS
9378
9379 if ((*tok == '"' || *tok == ',') && rest)
9380 {
9381 *rest = savestring (tok, strlen (tok));
9382 return;
9383 }
9384
f1735a53 9385 end_tok = skip_to_space (tok);
d634f2de 9386
018d34a4 9387 toklen = end_tok - tok;
d634f2de 9388
018d34a4
VP
9389 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9390 {
9391 tok = cond_start = end_tok + 1;
4d01a485 9392 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9393 cond_end = tok;
d634f2de 9394 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9395 }
9396 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9397 {
5d5658a1
PA
9398 const char *tmptok;
9399 struct thread_info *thr;
d634f2de 9400
018d34a4 9401 tok = end_tok + 1;
5d5658a1 9402 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9403 if (tok == tmptok)
9404 error (_("Junk after thread keyword."));
5d5658a1 9405 *thread = thr->global_num;
bbc13ae3 9406 tok = tmptok;
018d34a4 9407 }
4a306c9a
JB
9408 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9409 {
9410 char *tmptok;
9411
9412 tok = end_tok + 1;
bbc13ae3 9413 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9414 if (tok == tmptok)
9415 error (_("Junk after task keyword."));
9416 if (!valid_task_id (*task))
b6199126 9417 error (_("Unknown task %d."), *task);
bbc13ae3 9418 tok = tmptok;
4a306c9a 9419 }
e7e0cddf
SS
9420 else if (rest)
9421 {
9422 *rest = savestring (tok, strlen (tok));
ccab2054 9423 return;
e7e0cddf 9424 }
018d34a4
VP
9425 else
9426 error (_("Junk at end of arguments."));
9427 }
9428}
9429
0fb4aa4b
PA
9430/* Decode a static tracepoint marker spec. */
9431
6c5b2ebe 9432static std::vector<symtab_and_line>
f00aae0f 9433decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9434{
9435 VEC(static_tracepoint_marker_p) *markers = NULL;
0fb4aa4b 9436 struct cleanup *old_chain;
f00aae0f
KS
9437 const char *p = &(*arg_p)[3];
9438 const char *endp;
0fb4aa4b
PA
9439 char *marker_str;
9440 int i;
9441
f1735a53 9442 p = skip_spaces (p);
0fb4aa4b 9443
f1735a53 9444 endp = skip_to_space (p);
0fb4aa4b
PA
9445
9446 marker_str = savestring (p, endp - p);
9447 old_chain = make_cleanup (xfree, marker_str);
9448
9449 markers = target_static_tracepoint_markers_by_strid (marker_str);
9450 if (VEC_empty(static_tracepoint_marker_p, markers))
9451 error (_("No known static tracepoint marker named %s"), marker_str);
9452
6c5b2ebe
PA
9453 std::vector<symtab_and_line> sals;
9454 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9455
6c5b2ebe 9456 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9457 {
9458 struct static_tracepoint_marker *marker;
9459
9460 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9461
51abb421 9462 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9463 sal.pc = marker->address;
9464 sals.push_back (sal);
0fb4aa4b
PA
9465
9466 release_static_tracepoint_marker (marker);
9467 }
9468
9469 do_cleanups (old_chain);
9470
9471 *arg_p = endp;
9472 return sals;
9473}
9474
f00aae0f 9475/* See breakpoint.h. */
0101ce28 9476
8cdf0e15
VP
9477int
9478create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9479 const struct event_location *location,
9480 const char *cond_string,
9481 int thread, const char *extra_string,
f00aae0f 9482 int parse_extra,
0fb4aa4b 9483 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9484 int ignore_count,
9485 enum auto_boolean pending_break_support,
c0a91b2b 9486 const struct breakpoint_ops *ops,
44f238bb
PA
9487 int from_tty, int enabled, int internal,
9488 unsigned flags)
c3f6f71d 9489{
7efd8fc2 9490 struct linespec_result canonical;
80c99de1 9491 struct cleanup *bkpt_chain = NULL;
0101ce28 9492 int pending = 0;
4a306c9a 9493 int task = 0;
86b17b60 9494 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9495
348d480f
PA
9496 gdb_assert (ops != NULL);
9497
f00aae0f
KS
9498 /* If extra_string isn't useful, set it to NULL. */
9499 if (extra_string != NULL && *extra_string == '\0')
9500 extra_string = NULL;
9501
492d29ea 9502 TRY
b78a6381 9503 {
f00aae0f 9504 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9505 }
492d29ea 9506 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9507 {
492d29ea
PA
9508 /* If caller is interested in rc value from parse, set
9509 value. */
9510 if (e.error == NOT_FOUND_ERROR)
0101ce28 9511 {
05ff989b
AC
9512 /* If pending breakpoint support is turned off, throw
9513 error. */
fa8d40ab
JJ
9514
9515 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9516 throw_exception (e);
9517
9518 exception_print (gdb_stderr, e);
fa8d40ab 9519
05ff989b
AC
9520 /* If pending breakpoint support is auto query and the user
9521 selects no, then simply return the error code. */
059fb39f 9522 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9523 && !nquery (_("Make %s pending on future shared library load? "),
9524 bptype_string (type_wanted)))
fd9b8c24 9525 return 0;
fa8d40ab 9526
05ff989b
AC
9527 /* At this point, either the user was queried about setting
9528 a pending breakpoint and selected yes, or pending
9529 breakpoint behavior is on and thus a pending breakpoint
9530 is defaulted on behalf of the user. */
f00aae0f 9531 pending = 1;
0101ce28 9532 }
492d29ea
PA
9533 else
9534 throw_exception (e);
0101ce28 9535 }
492d29ea
PA
9536 END_CATCH
9537
6c5b2ebe 9538 if (!pending && canonical.lsals.empty ())
492d29ea 9539 return 0;
c3f6f71d 9540
c3f6f71d
JM
9541 /* ----------------------------- SNIP -----------------------------
9542 Anything added to the cleanup chain beyond this point is assumed
9543 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9544 then the memory is not reclaimed. */
9545 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9546
c3f6f71d
JM
9547 /* Resolve all line numbers to PC's and verify that the addresses
9548 are ok for the target. */
0101ce28 9549 if (!pending)
f8eba3c6 9550 {
6c5b2ebe
PA
9551 for (auto &lsal : canonical.lsals)
9552 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9553 }
c3f6f71d 9554
7a697b8d 9555 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9556 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9557 {
6c5b2ebe
PA
9558 for (const auto &lsal : canonical.lsals)
9559 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9560 }
7a697b8d 9561
c3f6f71d
JM
9562 /* Verify that condition can be parsed, before setting any
9563 breakpoints. Allocate a separate condition expression for each
4a64f543 9564 breakpoint. */
0101ce28 9565 if (!pending)
c3f6f71d 9566 {
e1e01040
PA
9567 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9568 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9569
f00aae0f 9570 if (parse_extra)
72b2ff0e 9571 {
0878d0fa 9572 char *rest;
e1e01040 9573 char *cond;
52d361e1 9574
6c5b2ebe 9575 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9576
0878d0fa
YQ
9577 /* Here we only parse 'arg' to separate condition
9578 from thread number, so parsing in context of first
9579 sal is OK. When setting the breakpoint we'll
9580 re-parse it in context of each sal. */
9581
6c5b2ebe 9582 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9583 &cond, &thread, &task, &rest);
9584 cond_string_copy.reset (cond);
9585 extra_string_copy.reset (rest);
72b2ff0e 9586 }
2f069f6f 9587 else
72b2ff0e 9588 {
f00aae0f
KS
9589 if (type_wanted != bp_dprintf
9590 && extra_string != NULL && *extra_string != '\0')
9591 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9592
9593 /* Create a private copy of condition string. */
9594 if (cond_string)
e1e01040 9595 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9596 /* Create a private copy of any extra string. */
9597 if (extra_string)
e1e01040 9598 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9599 }
0fb4aa4b 9600
52d361e1 9601 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9602 std::move (cond_string_copy),
9603 std::move (extra_string_copy),
9604 type_wanted,
d9b3f62e
PA
9605 tempflag ? disp_del : disp_donttouch,
9606 thread, task, ignore_count, ops,
44f238bb 9607 from_tty, enabled, internal, flags);
c906108c 9608 }
0101ce28
JJ
9609 else
9610 {
a5e364af 9611 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9612
a5e364af 9613 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9614 b->location = copy_event_location (location);
bfccc43c 9615
f00aae0f
KS
9616 if (parse_extra)
9617 b->cond_string = NULL;
e12c7713
MK
9618 else
9619 {
9620 /* Create a private copy of condition string. */
e1e01040 9621 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9622 b->thread = thread;
e12c7713 9623 }
f00aae0f
KS
9624
9625 /* Create a private copy of any extra string. */
e1e01040 9626 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9627 b->ignore_count = ignore_count;
0101ce28 9628 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9629 b->condition_not_parsed = 1;
41447f92 9630 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9631 if ((type_wanted != bp_breakpoint
9632 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9633 b->pspace = current_program_space;
8bea4e01 9634
b270e6f9 9635 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9636 }
9637
6c5b2ebe 9638 if (canonical.lsals.size () > 1)
95a42b64 9639 {
3e43a32a
MS
9640 warning (_("Multiple breakpoints were set.\nUse the "
9641 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9642 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9643 }
9644
80c99de1
PA
9645 /* That's it. Discard the cleanups for data inserted into the
9646 breakpoint. */
9647 discard_cleanups (bkpt_chain);
217dc9e2 9648
80c99de1 9649 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9650 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9651
9652 return 1;
c3f6f71d 9653}
c906108c 9654
348d480f 9655/* Set a breakpoint.
72b2ff0e
VP
9656 ARG is a string describing breakpoint address,
9657 condition, and thread.
9658 FLAG specifies if a breakpoint is hardware on,
9659 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9660 and BP_TEMPFLAG. */
348d480f 9661
98deb0da 9662static void
f2fc3015 9663break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9664{
72b2ff0e 9665 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9666 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9667 ? bp_hardware_breakpoint
9668 : bp_breakpoint);
55aa24fb 9669 struct breakpoint_ops *ops;
f00aae0f 9670
ffc2605c 9671 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9672
9673 /* Matching breakpoints on probes. */
5b56227b 9674 if (location != NULL
ffc2605c 9675 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9676 ops = &bkpt_probe_breakpoint_ops;
9677 else
9678 ops = &bkpt_breakpoint_ops;
c3f6f71d 9679
8cdf0e15 9680 create_breakpoint (get_current_arch (),
ffc2605c 9681 location.get (),
f00aae0f 9682 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9683 tempflag, type_wanted,
8cdf0e15
VP
9684 0 /* Ignore count */,
9685 pending_break_support,
55aa24fb 9686 ops,
8cdf0e15 9687 from_tty,
84f4c1fe 9688 1 /* enabled */,
44f238bb
PA
9689 0 /* internal */,
9690 0);
c906108c
SS
9691}
9692
c906108c
SS
9693/* Helper function for break_command_1 and disassemble_command. */
9694
9695void
fba45db2 9696resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9697{
9698 CORE_ADDR pc;
9699
9700 if (sal->pc == 0 && sal->symtab != NULL)
9701 {
9702 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9703 error (_("No line %d in file \"%s\"."),
05cba821 9704 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9705 sal->pc = pc;
6a048695 9706
4a64f543
MS
9707 /* If this SAL corresponds to a breakpoint inserted using a line
9708 number, then skip the function prologue if necessary. */
6a048695 9709 if (sal->explicit_line)
059acae7 9710 skip_prologue_sal (sal);
c906108c
SS
9711 }
9712
9713 if (sal->section == 0 && sal->symtab != NULL)
9714 {
346d1dfe 9715 const struct blockvector *bv;
3977b71f 9716 const struct block *b;
c5aa993b 9717 struct symbol *sym;
c906108c 9718
43f3e411
DE
9719 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9720 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9721 if (bv != NULL)
9722 {
7f0df278 9723 sym = block_linkage_function (b);
c906108c
SS
9724 if (sym != NULL)
9725 {
eb822aa6
DE
9726 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9727 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9728 sym);
c906108c
SS
9729 }
9730 else
9731 {
4a64f543
MS
9732 /* It really is worthwhile to have the section, so we'll
9733 just have to look harder. This case can be executed
9734 if we have line numbers but no functions (as can
9735 happen in assembly source). */
c906108c 9736
5ed8105e 9737 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9738 switch_to_program_space_and_thread (sal->pspace);
c906108c 9739
5ed8105e 9740 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9741 if (msym.minsym)
efd66ac6 9742 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9743 }
9744 }
9745 }
9746}
9747
9748void
fba45db2 9749break_command (char *arg, int from_tty)
c906108c 9750{
db107f19 9751 break_command_1 (arg, 0, from_tty);
c906108c
SS
9752}
9753
c906108c 9754void
fba45db2 9755tbreak_command (char *arg, int from_tty)
c906108c 9756{
db107f19 9757 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9758}
9759
c906108c 9760static void
fba45db2 9761hbreak_command (char *arg, int from_tty)
c906108c 9762{
db107f19 9763 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9764}
9765
9766static void
fba45db2 9767thbreak_command (char *arg, int from_tty)
c906108c 9768{
db107f19 9769 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9770}
9771
9772static void
fba45db2 9773stop_command (char *arg, int from_tty)
c906108c 9774{
a3f17187 9775 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9776Usage: stop in <function | address>\n\
a3f17187 9777 stop at <line>\n"));
c906108c
SS
9778}
9779
9780static void
4495129a 9781stopin_command (const char *arg, int from_tty)
c906108c
SS
9782{
9783 int badInput = 0;
9784
c5aa993b 9785 if (arg == (char *) NULL)
c906108c
SS
9786 badInput = 1;
9787 else if (*arg != '*')
9788 {
4495129a 9789 const char *argptr = arg;
c906108c
SS
9790 int hasColon = 0;
9791
4a64f543 9792 /* Look for a ':'. If this is a line number specification, then
53a5351d 9793 say it is bad, otherwise, it should be an address or
4a64f543 9794 function/method name. */
c906108c 9795 while (*argptr && !hasColon)
c5aa993b
JM
9796 {
9797 hasColon = (*argptr == ':');
9798 argptr++;
9799 }
c906108c
SS
9800
9801 if (hasColon)
c5aa993b 9802 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9803 else
c5aa993b 9804 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9805 }
9806
9807 if (badInput)
a3f17187 9808 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9809 else
db107f19 9810 break_command_1 (arg, 0, from_tty);
c906108c
SS
9811}
9812
9813static void
4495129a 9814stopat_command (const char *arg, int from_tty)
c906108c
SS
9815{
9816 int badInput = 0;
9817
c5aa993b 9818 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9819 badInput = 1;
9820 else
9821 {
4495129a 9822 const char *argptr = arg;
c906108c
SS
9823 int hasColon = 0;
9824
4a64f543
MS
9825 /* Look for a ':'. If there is a '::' then get out, otherwise
9826 it is probably a line number. */
c906108c 9827 while (*argptr && !hasColon)
c5aa993b
JM
9828 {
9829 hasColon = (*argptr == ':');
9830 argptr++;
9831 }
c906108c
SS
9832
9833 if (hasColon)
c5aa993b 9834 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9835 else
c5aa993b 9836 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9837 }
9838
9839 if (badInput)
a3f17187 9840 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9841 else
db107f19 9842 break_command_1 (arg, 0, from_tty);
c906108c
SS
9843}
9844
e7e0cddf
SS
9845/* The dynamic printf command is mostly like a regular breakpoint, but
9846 with a prewired command list consisting of a single output command,
9847 built from extra arguments supplied on the dprintf command
9848 line. */
9849
da821c7b 9850static void
f2fc3015 9851dprintf_command (char *arg_in, int from_tty)
e7e0cddf 9852{
f2fc3015 9853 const char *arg = arg_in;
ffc2605c 9854 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9855
9856 /* If non-NULL, ARG should have been advanced past the location;
9857 the next character must be ','. */
9858 if (arg != NULL)
9859 {
9860 if (arg[0] != ',' || arg[1] == '\0')
9861 error (_("Format string required"));
9862 else
9863 {
9864 /* Skip the comma. */
9865 ++arg;
9866 }
9867 }
9868
e7e0cddf 9869 create_breakpoint (get_current_arch (),
ffc2605c 9870 location.get (),
f00aae0f 9871 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9872 0, bp_dprintf,
9873 0 /* Ignore count */,
9874 pending_break_support,
9875 &dprintf_breakpoint_ops,
9876 from_tty,
9877 1 /* enabled */,
9878 0 /* internal */,
9879 0);
9880}
9881
d3ce09f5
SS
9882static void
9883agent_printf_command (char *arg, int from_tty)
9884{
9885 error (_("May only run agent-printf on the target"));
9886}
9887
f1310107
TJB
9888/* Implement the "breakpoint_hit" breakpoint_ops method for
9889 ranged breakpoints. */
9890
9891static int
9892breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9893 struct address_space *aspace,
09ac7c10
TT
9894 CORE_ADDR bp_addr,
9895 const struct target_waitstatus *ws)
f1310107 9896{
09ac7c10 9897 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9898 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9899 return 0;
9900
f1310107
TJB
9901 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9902 bl->length, aspace, bp_addr);
9903}
9904
9905/* Implement the "resources_needed" breakpoint_ops method for
9906 ranged breakpoints. */
9907
9908static int
9909resources_needed_ranged_breakpoint (const struct bp_location *bl)
9910{
9911 return target_ranged_break_num_registers ();
9912}
9913
9914/* Implement the "print_it" breakpoint_ops method for
9915 ranged breakpoints. */
9916
9917static enum print_stop_action
348d480f 9918print_it_ranged_breakpoint (bpstat bs)
f1310107 9919{
348d480f 9920 struct breakpoint *b = bs->breakpoint_at;
f1310107 9921 struct bp_location *bl = b->loc;
79a45e25 9922 struct ui_out *uiout = current_uiout;
f1310107
TJB
9923
9924 gdb_assert (b->type == bp_hardware_breakpoint);
9925
9926 /* Ranged breakpoints have only one location. */
9927 gdb_assert (bl && bl->next == NULL);
9928
9929 annotate_breakpoint (b->number);
f303dbd6
PA
9930
9931 maybe_print_thread_hit_breakpoint (uiout);
9932
f1310107 9933 if (b->disposition == disp_del)
112e8700 9934 uiout->text ("Temporary ranged breakpoint ");
f1310107 9935 else
112e8700
SM
9936 uiout->text ("Ranged breakpoint ");
9937 if (uiout->is_mi_like_p ())
f1310107 9938 {
112e8700 9939 uiout->field_string ("reason",
f1310107 9940 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9941 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9942 }
112e8700
SM
9943 uiout->field_int ("bkptno", b->number);
9944 uiout->text (", ");
f1310107
TJB
9945
9946 return PRINT_SRC_AND_LOC;
9947}
9948
9949/* Implement the "print_one" breakpoint_ops method for
9950 ranged breakpoints. */
9951
9952static void
9953print_one_ranged_breakpoint (struct breakpoint *b,
9954 struct bp_location **last_loc)
9955{
9956 struct bp_location *bl = b->loc;
9957 struct value_print_options opts;
79a45e25 9958 struct ui_out *uiout = current_uiout;
f1310107
TJB
9959
9960 /* Ranged breakpoints have only one location. */
9961 gdb_assert (bl && bl->next == NULL);
9962
9963 get_user_print_options (&opts);
9964
9965 if (opts.addressprint)
9966 /* We don't print the address range here, it will be printed later
9967 by print_one_detail_ranged_breakpoint. */
112e8700 9968 uiout->field_skip ("addr");
f1310107
TJB
9969 annotate_field (5);
9970 print_breakpoint_location (b, bl);
9971 *last_loc = bl;
9972}
9973
9974/* Implement the "print_one_detail" breakpoint_ops method for
9975 ranged breakpoints. */
9976
9977static void
9978print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9979 struct ui_out *uiout)
9980{
9981 CORE_ADDR address_start, address_end;
9982 struct bp_location *bl = b->loc;
d7e74731 9983 string_file stb;
f1310107
TJB
9984
9985 gdb_assert (bl);
9986
9987 address_start = bl->address;
9988 address_end = address_start + bl->length - 1;
9989
112e8700 9990 uiout->text ("\taddress range: ");
d7e74731
PA
9991 stb.printf ("[%s, %s]",
9992 print_core_address (bl->gdbarch, address_start),
9993 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9994 uiout->field_stream ("addr", stb);
9995 uiout->text ("\n");
f1310107
TJB
9996}
9997
9998/* Implement the "print_mention" breakpoint_ops method for
9999 ranged breakpoints. */
10000
10001static void
10002print_mention_ranged_breakpoint (struct breakpoint *b)
10003{
10004 struct bp_location *bl = b->loc;
79a45e25 10005 struct ui_out *uiout = current_uiout;
f1310107
TJB
10006
10007 gdb_assert (bl);
10008 gdb_assert (b->type == bp_hardware_breakpoint);
10009
112e8700 10010 if (uiout->is_mi_like_p ())
f1310107
TJB
10011 return;
10012
10013 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10014 b->number, paddress (bl->gdbarch, bl->address),
10015 paddress (bl->gdbarch, bl->address + bl->length - 1));
10016}
10017
10018/* Implement the "print_recreate" breakpoint_ops method for
10019 ranged breakpoints. */
10020
10021static void
10022print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10023{
f00aae0f 10024 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10025 event_location_to_string (b->location.get ()),
10026 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10027 print_recreate_thread (b, fp);
f1310107
TJB
10028}
10029
10030/* The breakpoint_ops structure to be used in ranged breakpoints. */
10031
2060206e 10032static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10033
10034/* Find the address where the end of the breakpoint range should be
10035 placed, given the SAL of the end of the range. This is so that if
10036 the user provides a line number, the end of the range is set to the
10037 last instruction of the given line. */
10038
10039static CORE_ADDR
10040find_breakpoint_range_end (struct symtab_and_line sal)
10041{
10042 CORE_ADDR end;
10043
10044 /* If the user provided a PC value, use it. Otherwise,
10045 find the address of the end of the given location. */
10046 if (sal.explicit_pc)
10047 end = sal.pc;
10048 else
10049 {
10050 int ret;
10051 CORE_ADDR start;
10052
10053 ret = find_line_pc_range (sal, &start, &end);
10054 if (!ret)
10055 error (_("Could not find location of the end of the range."));
10056
10057 /* find_line_pc_range returns the start of the next line. */
10058 end--;
10059 }
10060
10061 return end;
10062}
10063
10064/* Implement the "break-range" CLI command. */
10065
10066static void
f2fc3015 10067break_range_command (char *arg_in, int from_tty)
f1310107 10068{
f2fc3015
TT
10069 const char *arg = arg_in;
10070 const char *arg_start;
10071 char *addr_string_start;
f1310107
TJB
10072 struct linespec_result canonical_start, canonical_end;
10073 int bp_count, can_use_bp, length;
10074 CORE_ADDR end;
10075 struct breakpoint *b;
f1310107
TJB
10076 struct cleanup *cleanup_bkpt;
10077
10078 /* We don't support software ranged breakpoints. */
10079 if (target_ranged_break_num_registers () < 0)
10080 error (_("This target does not support hardware ranged breakpoints."));
10081
10082 bp_count = hw_breakpoint_used_count ();
10083 bp_count += target_ranged_break_num_registers ();
10084 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10085 bp_count, 0);
10086 if (can_use_bp < 0)
10087 error (_("Hardware breakpoints used exceeds limit."));
10088
f8eba3c6 10089 arg = skip_spaces (arg);
f1310107
TJB
10090 if (arg == NULL || arg[0] == '\0')
10091 error(_("No address range specified."));
10092
f8eba3c6 10093 arg_start = arg;
ffc2605c
TT
10094 event_location_up start_location = string_to_event_location (&arg,
10095 current_language);
10096 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10097
10098 if (arg[0] != ',')
10099 error (_("Too few arguments."));
6c5b2ebe 10100 else if (canonical_start.lsals.empty ())
f1310107 10101 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10102
6c5b2ebe 10103 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10104
6c5b2ebe
PA
10105 if (canonical_start.lsals.size () > 1
10106 || lsal_start.sals.size () != 1)
f1310107
TJB
10107 error (_("Cannot create a ranged breakpoint with multiple locations."));
10108
6c5b2ebe 10109 const symtab_and_line &sal_start = lsal_start.sals[0];
f8eba3c6 10110 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10111 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10112
10113 arg++; /* Skip the comma. */
f8eba3c6 10114 arg = skip_spaces (arg);
f1310107
TJB
10115
10116 /* Parse the end location. */
10117
f1310107
TJB
10118 arg_start = arg;
10119
f8eba3c6 10120 /* We call decode_line_full directly here instead of using
f1310107
TJB
10121 parse_breakpoint_sals because we need to specify the start location's
10122 symtab and line as the default symtab and line for the end of the
10123 range. This makes it possible to have ranges like "foo.c:27, +14",
10124 where +14 means 14 lines from the start location. */
ffc2605c
TT
10125 event_location_up end_location = string_to_event_location (&arg,
10126 current_language);
10127 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10128 sal_start.symtab, sal_start.line,
10129 &canonical_end, NULL, NULL);
10130
6c5b2ebe 10131 if (canonical_end.lsals.empty ())
f1310107 10132 error (_("Could not find location of the end of the range."));
f8eba3c6 10133
6c5b2ebe
PA
10134 const linespec_sals &lsal_end = canonical_end.lsals[0];
10135 if (canonical_end.lsals.size () > 1
10136 || lsal_end.sals.size () != 1)
f1310107
TJB
10137 error (_("Cannot create a ranged breakpoint with multiple locations."));
10138
6c5b2ebe 10139 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10140
10141 end = find_breakpoint_range_end (sal_end);
10142 if (sal_start.pc > end)
177b42fe 10143 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10144
10145 length = end - sal_start.pc + 1;
10146 if (length < 0)
10147 /* Length overflowed. */
10148 error (_("Address range too large."));
10149 else if (length == 1)
10150 {
10151 /* This range is simple enough to be handled by
10152 the `hbreak' command. */
10153 hbreak_command (addr_string_start, 1);
10154
10155 do_cleanups (cleanup_bkpt);
10156
10157 return;
10158 }
10159
10160 /* Now set up the breakpoint. */
10161 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10162 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10163 set_breakpoint_count (breakpoint_count + 1);
10164 b->number = breakpoint_count;
10165 b->disposition = disp_donttouch;
d28cd78a
TT
10166 b->location = std::move (start_location);
10167 b->location_range_end = std::move (end_location);
f1310107
TJB
10168 b->loc->length = length;
10169
f8eba3c6 10170 do_cleanups (cleanup_bkpt);
f1310107
TJB
10171
10172 mention (b);
8d3788bd 10173 observer_notify_breakpoint_created (b);
44702360 10174 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10175}
10176
4a64f543
MS
10177/* Return non-zero if EXP is verified as constant. Returned zero
10178 means EXP is variable. Also the constant detection may fail for
10179 some constant expressions and in such case still falsely return
10180 zero. */
2e6e3d9c 10181
65d79d4b
SDJ
10182static int
10183watchpoint_exp_is_const (const struct expression *exp)
10184{
10185 int i = exp->nelts;
10186
10187 while (i > 0)
10188 {
10189 int oplenp, argsp;
10190
10191 /* We are only interested in the descriptor of each element. */
10192 operator_length (exp, i, &oplenp, &argsp);
10193 i -= oplenp;
10194
10195 switch (exp->elts[i].opcode)
10196 {
10197 case BINOP_ADD:
10198 case BINOP_SUB:
10199 case BINOP_MUL:
10200 case BINOP_DIV:
10201 case BINOP_REM:
10202 case BINOP_MOD:
10203 case BINOP_LSH:
10204 case BINOP_RSH:
10205 case BINOP_LOGICAL_AND:
10206 case BINOP_LOGICAL_OR:
10207 case BINOP_BITWISE_AND:
10208 case BINOP_BITWISE_IOR:
10209 case BINOP_BITWISE_XOR:
10210 case BINOP_EQUAL:
10211 case BINOP_NOTEQUAL:
10212 case BINOP_LESS:
10213 case BINOP_GTR:
10214 case BINOP_LEQ:
10215 case BINOP_GEQ:
10216 case BINOP_REPEAT:
10217 case BINOP_COMMA:
10218 case BINOP_EXP:
10219 case BINOP_MIN:
10220 case BINOP_MAX:
10221 case BINOP_INTDIV:
10222 case BINOP_CONCAT:
65d79d4b
SDJ
10223 case TERNOP_COND:
10224 case TERNOP_SLICE:
65d79d4b
SDJ
10225
10226 case OP_LONG:
10227 case OP_DOUBLE:
10228 case OP_DECFLOAT:
10229 case OP_LAST:
10230 case OP_COMPLEX:
10231 case OP_STRING:
65d79d4b
SDJ
10232 case OP_ARRAY:
10233 case OP_TYPE:
608b4967
TT
10234 case OP_TYPEOF:
10235 case OP_DECLTYPE:
6e72ca20 10236 case OP_TYPEID:
65d79d4b
SDJ
10237 case OP_NAME:
10238 case OP_OBJC_NSSTRING:
10239
10240 case UNOP_NEG:
10241 case UNOP_LOGICAL_NOT:
10242 case UNOP_COMPLEMENT:
10243 case UNOP_ADDR:
10244 case UNOP_HIGH:
aeaa2474 10245 case UNOP_CAST:
9eaf6705
TT
10246
10247 case UNOP_CAST_TYPE:
10248 case UNOP_REINTERPRET_CAST:
10249 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10250 /* Unary, binary and ternary operators: We have to check
10251 their operands. If they are constant, then so is the
10252 result of that operation. For instance, if A and B are
10253 determined to be constants, then so is "A + B".
10254
10255 UNOP_IND is one exception to the rule above, because the
10256 value of *ADDR is not necessarily a constant, even when
10257 ADDR is. */
65d79d4b
SDJ
10258 break;
10259
10260 case OP_VAR_VALUE:
10261 /* Check whether the associated symbol is a constant.
4a64f543 10262
65d79d4b 10263 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10264 possible that a buggy compiler could mark a variable as
10265 constant even when it is not, and TYPE_CONST would return
10266 true in this case, while SYMBOL_CLASS wouldn't.
10267
10268 We also have to check for function symbols because they
10269 are always constant. */
65d79d4b
SDJ
10270 {
10271 struct symbol *s = exp->elts[i + 2].symbol;
10272
10273 if (SYMBOL_CLASS (s) != LOC_BLOCK
10274 && SYMBOL_CLASS (s) != LOC_CONST
10275 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10276 return 0;
10277 break;
10278 }
10279
10280 /* The default action is to return 0 because we are using
10281 the optimistic approach here: If we don't know something,
10282 then it is not a constant. */
10283 default:
10284 return 0;
10285 }
10286 }
10287
10288 return 1;
10289}
10290
c1fc2657 10291/* Watchpoint destructor. */
3a5c3e22 10292
c1fc2657 10293watchpoint::~watchpoint ()
3a5c3e22 10294{
c1fc2657
SM
10295 xfree (this->exp_string);
10296 xfree (this->exp_string_reparse);
10297 value_free (this->val);
3a5c3e22
PA
10298}
10299
348d480f
PA
10300/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10301
10302static void
10303re_set_watchpoint (struct breakpoint *b)
10304{
3a5c3e22
PA
10305 struct watchpoint *w = (struct watchpoint *) b;
10306
348d480f
PA
10307 /* Watchpoint can be either on expression using entirely global
10308 variables, or it can be on local variables.
10309
10310 Watchpoints of the first kind are never auto-deleted, and even
10311 persist across program restarts. Since they can use variables
10312 from shared libraries, we need to reparse expression as libraries
10313 are loaded and unloaded.
10314
10315 Watchpoints on local variables can also change meaning as result
10316 of solib event. For example, if a watchpoint uses both a local
10317 and a global variables in expression, it's a local watchpoint,
10318 but unloading of a shared library will make the expression
10319 invalid. This is not a very common use case, but we still
10320 re-evaluate expression, to avoid surprises to the user.
10321
10322 Note that for local watchpoints, we re-evaluate it only if
10323 watchpoints frame id is still valid. If it's not, it means the
10324 watchpoint is out of scope and will be deleted soon. In fact,
10325 I'm not sure we'll ever be called in this case.
10326
10327 If a local watchpoint's frame id is still valid, then
3a5c3e22 10328 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10329
3a5c3e22
PA
10330 Don't do anything about disabled watchpoints, since they will be
10331 reevaluated again when enabled. */
10332 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10333}
10334
77b06cd7
TJB
10335/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10336
10337static int
10338insert_watchpoint (struct bp_location *bl)
10339{
3a5c3e22
PA
10340 struct watchpoint *w = (struct watchpoint *) bl->owner;
10341 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10342
10343 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10344 w->cond_exp.get ());
77b06cd7
TJB
10345}
10346
10347/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10348
10349static int
73971819 10350remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10351{
3a5c3e22
PA
10352 struct watchpoint *w = (struct watchpoint *) bl->owner;
10353 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10354
10355 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10356 w->cond_exp.get ());
e09342b5
TJB
10357}
10358
e09342b5 10359static int
348d480f 10360breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10361 struct address_space *aspace, CORE_ADDR bp_addr,
10362 const struct target_waitstatus *ws)
e09342b5 10363{
348d480f 10364 struct breakpoint *b = bl->owner;
3a5c3e22 10365 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10366
348d480f
PA
10367 /* Continuable hardware watchpoints are treated as non-existent if the
10368 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10369 some data address). Otherwise gdb won't stop on a break instruction
10370 in the code (not from a breakpoint) when a hardware watchpoint has
10371 been defined. Also skip watchpoints which we know did not trigger
10372 (did not match the data address). */
10373 if (is_hardware_watchpoint (b)
3a5c3e22 10374 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10375 return 0;
9c06b0b4 10376
348d480f 10377 return 1;
9c06b0b4
TJB
10378}
10379
348d480f
PA
10380static void
10381check_status_watchpoint (bpstat bs)
9c06b0b4 10382{
348d480f 10383 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10384
348d480f 10385 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10386}
10387
10388/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10389 hardware watchpoints. */
9c06b0b4
TJB
10390
10391static int
348d480f 10392resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10393{
3a5c3e22
PA
10394 struct watchpoint *w = (struct watchpoint *) bl->owner;
10395 int length = w->exact? 1 : bl->length;
348d480f
PA
10396
10397 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10398}
10399
10400/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10401 hardware watchpoints. */
9c06b0b4
TJB
10402
10403static int
348d480f 10404works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10405{
efa80663
PA
10406 /* Read and access watchpoints only work with hardware support. */
10407 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10408}
10409
9c06b0b4 10410static enum print_stop_action
348d480f 10411print_it_watchpoint (bpstat bs)
9c06b0b4 10412{
348d480f 10413 struct breakpoint *b;
348d480f 10414 enum print_stop_action result;
3a5c3e22 10415 struct watchpoint *w;
79a45e25 10416 struct ui_out *uiout = current_uiout;
348d480f
PA
10417
10418 gdb_assert (bs->bp_location_at != NULL);
10419
348d480f 10420 b = bs->breakpoint_at;
3a5c3e22 10421 w = (struct watchpoint *) b;
348d480f 10422
f303dbd6
PA
10423 annotate_watchpoint (b->number);
10424 maybe_print_thread_hit_breakpoint (uiout);
10425
d7e74731
PA
10426 string_file stb;
10427
76f9c9cf 10428 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10429 switch (b->type)
10430 {
348d480f 10431 case bp_watchpoint:
9c06b0b4 10432 case bp_hardware_watchpoint:
112e8700
SM
10433 if (uiout->is_mi_like_p ())
10434 uiout->field_string
10435 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10436 mention (b);
76f9c9cf 10437 tuple_emitter.emplace (uiout, "value");
112e8700 10438 uiout->text ("\nOld value = ");
d7e74731 10439 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10440 uiout->field_stream ("old", stb);
10441 uiout->text ("\nNew value = ");
d7e74731 10442 watchpoint_value_print (w->val, &stb);
112e8700
SM
10443 uiout->field_stream ("new", stb);
10444 uiout->text ("\n");
348d480f
PA
10445 /* More than one watchpoint may have been triggered. */
10446 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10447 break;
10448
10449 case bp_read_watchpoint:
112e8700
SM
10450 if (uiout->is_mi_like_p ())
10451 uiout->field_string
10452 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10453 mention (b);
76f9c9cf 10454 tuple_emitter.emplace (uiout, "value");
112e8700 10455 uiout->text ("\nValue = ");
d7e74731 10456 watchpoint_value_print (w->val, &stb);
112e8700
SM
10457 uiout->field_stream ("value", stb);
10458 uiout->text ("\n");
348d480f 10459 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10460 break;
10461
10462 case bp_access_watchpoint:
348d480f
PA
10463 if (bs->old_val != NULL)
10464 {
112e8700
SM
10465 if (uiout->is_mi_like_p ())
10466 uiout->field_string
10467 ("reason",
348d480f
PA
10468 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10469 mention (b);
76f9c9cf 10470 tuple_emitter.emplace (uiout, "value");
112e8700 10471 uiout->text ("\nOld value = ");
d7e74731 10472 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10473 uiout->field_stream ("old", stb);
10474 uiout->text ("\nNew value = ");
348d480f
PA
10475 }
10476 else
10477 {
10478 mention (b);
112e8700
SM
10479 if (uiout->is_mi_like_p ())
10480 uiout->field_string
10481 ("reason",
348d480f 10482 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10483 tuple_emitter.emplace (uiout, "value");
112e8700 10484 uiout->text ("\nValue = ");
348d480f 10485 }
d7e74731 10486 watchpoint_value_print (w->val, &stb);
112e8700
SM
10487 uiout->field_stream ("new", stb);
10488 uiout->text ("\n");
348d480f 10489 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10490 break;
10491 default:
348d480f 10492 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10493 }
10494
348d480f
PA
10495 return result;
10496}
10497
10498/* Implement the "print_mention" breakpoint_ops method for hardware
10499 watchpoints. */
10500
10501static void
10502print_mention_watchpoint (struct breakpoint *b)
10503{
3a5c3e22 10504 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10505 struct ui_out *uiout = current_uiout;
46b9c129 10506 const char *tuple_name;
348d480f
PA
10507
10508 switch (b->type)
10509 {
10510 case bp_watchpoint:
112e8700 10511 uiout->text ("Watchpoint ");
46b9c129 10512 tuple_name = "wpt";
348d480f
PA
10513 break;
10514 case bp_hardware_watchpoint:
112e8700 10515 uiout->text ("Hardware watchpoint ");
46b9c129 10516 tuple_name = "wpt";
348d480f
PA
10517 break;
10518 case bp_read_watchpoint:
112e8700 10519 uiout->text ("Hardware read watchpoint ");
46b9c129 10520 tuple_name = "hw-rwpt";
348d480f
PA
10521 break;
10522 case bp_access_watchpoint:
112e8700 10523 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10524 tuple_name = "hw-awpt";
348d480f
PA
10525 break;
10526 default:
10527 internal_error (__FILE__, __LINE__,
10528 _("Invalid hardware watchpoint type."));
10529 }
10530
46b9c129 10531 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10532 uiout->field_int ("number", b->number);
10533 uiout->text (": ");
10534 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10535}
10536
10537/* Implement the "print_recreate" breakpoint_ops method for
10538 watchpoints. */
10539
10540static void
10541print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10542{
3a5c3e22
PA
10543 struct watchpoint *w = (struct watchpoint *) b;
10544
348d480f
PA
10545 switch (b->type)
10546 {
10547 case bp_watchpoint:
10548 case bp_hardware_watchpoint:
10549 fprintf_unfiltered (fp, "watch");
10550 break;
10551 case bp_read_watchpoint:
10552 fprintf_unfiltered (fp, "rwatch");
10553 break;
10554 case bp_access_watchpoint:
10555 fprintf_unfiltered (fp, "awatch");
10556 break;
10557 default:
10558 internal_error (__FILE__, __LINE__,
10559 _("Invalid watchpoint type."));
10560 }
10561
3a5c3e22 10562 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10563 print_recreate_thread (b, fp);
348d480f
PA
10564}
10565
427cd150
TT
10566/* Implement the "explains_signal" breakpoint_ops method for
10567 watchpoints. */
10568
47591c29 10569static int
427cd150
TT
10570explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10571{
10572 /* A software watchpoint cannot cause a signal other than
10573 GDB_SIGNAL_TRAP. */
10574 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10575 return 0;
427cd150 10576
47591c29 10577 return 1;
427cd150
TT
10578}
10579
348d480f
PA
10580/* The breakpoint_ops structure to be used in hardware watchpoints. */
10581
2060206e 10582static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10583
10584/* Implement the "insert" breakpoint_ops method for
10585 masked hardware watchpoints. */
10586
10587static int
10588insert_masked_watchpoint (struct bp_location *bl)
10589{
3a5c3e22
PA
10590 struct watchpoint *w = (struct watchpoint *) bl->owner;
10591
10592 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10593 bl->watchpoint_type);
10594}
10595
10596/* Implement the "remove" breakpoint_ops method for
10597 masked hardware watchpoints. */
10598
10599static int
73971819 10600remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10601{
3a5c3e22
PA
10602 struct watchpoint *w = (struct watchpoint *) bl->owner;
10603
10604 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10605 bl->watchpoint_type);
10606}
10607
10608/* Implement the "resources_needed" breakpoint_ops method for
10609 masked hardware watchpoints. */
10610
10611static int
10612resources_needed_masked_watchpoint (const struct bp_location *bl)
10613{
3a5c3e22
PA
10614 struct watchpoint *w = (struct watchpoint *) bl->owner;
10615
10616 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10617}
10618
10619/* Implement the "works_in_software_mode" breakpoint_ops method for
10620 masked hardware watchpoints. */
10621
10622static int
10623works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10624{
10625 return 0;
10626}
10627
10628/* Implement the "print_it" breakpoint_ops method for
10629 masked hardware watchpoints. */
10630
10631static enum print_stop_action
10632print_it_masked_watchpoint (bpstat bs)
10633{
10634 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10635 struct ui_out *uiout = current_uiout;
348d480f
PA
10636
10637 /* Masked watchpoints have only one location. */
10638 gdb_assert (b->loc && b->loc->next == NULL);
10639
f303dbd6
PA
10640 annotate_watchpoint (b->number);
10641 maybe_print_thread_hit_breakpoint (uiout);
10642
348d480f
PA
10643 switch (b->type)
10644 {
10645 case bp_hardware_watchpoint:
112e8700
SM
10646 if (uiout->is_mi_like_p ())
10647 uiout->field_string
10648 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10649 break;
10650
10651 case bp_read_watchpoint:
112e8700
SM
10652 if (uiout->is_mi_like_p ())
10653 uiout->field_string
10654 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10655 break;
10656
10657 case bp_access_watchpoint:
112e8700
SM
10658 if (uiout->is_mi_like_p ())
10659 uiout->field_string
10660 ("reason",
348d480f
PA
10661 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10662 break;
10663 default:
10664 internal_error (__FILE__, __LINE__,
10665 _("Invalid hardware watchpoint type."));
10666 }
10667
10668 mention (b);
112e8700 10669 uiout->text (_("\n\
9c06b0b4
TJB
10670Check the underlying instruction at PC for the memory\n\
10671address and value which triggered this watchpoint.\n"));
112e8700 10672 uiout->text ("\n");
9c06b0b4
TJB
10673
10674 /* More than one watchpoint may have been triggered. */
10675 return PRINT_UNKNOWN;
10676}
10677
10678/* Implement the "print_one_detail" breakpoint_ops method for
10679 masked hardware watchpoints. */
10680
10681static void
10682print_one_detail_masked_watchpoint (const struct breakpoint *b,
10683 struct ui_out *uiout)
10684{
3a5c3e22
PA
10685 struct watchpoint *w = (struct watchpoint *) b;
10686
9c06b0b4
TJB
10687 /* Masked watchpoints have only one location. */
10688 gdb_assert (b->loc && b->loc->next == NULL);
10689
112e8700
SM
10690 uiout->text ("\tmask ");
10691 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10692 uiout->text ("\n");
9c06b0b4
TJB
10693}
10694
10695/* Implement the "print_mention" breakpoint_ops method for
10696 masked hardware watchpoints. */
10697
10698static void
10699print_mention_masked_watchpoint (struct breakpoint *b)
10700{
3a5c3e22 10701 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10702 struct ui_out *uiout = current_uiout;
46b9c129 10703 const char *tuple_name;
9c06b0b4
TJB
10704
10705 switch (b->type)
10706 {
10707 case bp_hardware_watchpoint:
112e8700 10708 uiout->text ("Masked hardware watchpoint ");
46b9c129 10709 tuple_name = "wpt";
9c06b0b4
TJB
10710 break;
10711 case bp_read_watchpoint:
112e8700 10712 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10713 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10714 break;
10715 case bp_access_watchpoint:
112e8700 10716 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10717 tuple_name = "hw-awpt";
9c06b0b4
TJB
10718 break;
10719 default:
10720 internal_error (__FILE__, __LINE__,
10721 _("Invalid hardware watchpoint type."));
10722 }
10723
46b9c129 10724 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10725 uiout->field_int ("number", b->number);
10726 uiout->text (": ");
10727 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10728}
10729
10730/* Implement the "print_recreate" breakpoint_ops method for
10731 masked hardware watchpoints. */
10732
10733static void
10734print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10735{
3a5c3e22 10736 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10737 char tmp[40];
10738
10739 switch (b->type)
10740 {
10741 case bp_hardware_watchpoint:
10742 fprintf_unfiltered (fp, "watch");
10743 break;
10744 case bp_read_watchpoint:
10745 fprintf_unfiltered (fp, "rwatch");
10746 break;
10747 case bp_access_watchpoint:
10748 fprintf_unfiltered (fp, "awatch");
10749 break;
10750 default:
10751 internal_error (__FILE__, __LINE__,
10752 _("Invalid hardware watchpoint type."));
10753 }
10754
3a5c3e22
PA
10755 sprintf_vma (tmp, w->hw_wp_mask);
10756 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10757 print_recreate_thread (b, fp);
9c06b0b4
TJB
10758}
10759
10760/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10761
2060206e 10762static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10763
10764/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10765
10766static int
10767is_masked_watchpoint (const struct breakpoint *b)
10768{
10769 return b->ops == &masked_watchpoint_breakpoint_ops;
10770}
10771
53a5351d
JM
10772/* accessflag: hw_write: watch write,
10773 hw_read: watch read,
10774 hw_access: watch access (read or write) */
c906108c 10775static void
bbc13ae3 10776watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10777 int just_location, int internal)
c906108c 10778{
c1fc2657 10779 struct breakpoint *scope_breakpoint = NULL;
270140bd 10780 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10781 struct value *val, *mark, *result;
bb9d5f81 10782 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10783 const char *exp_start = NULL;
10784 const char *exp_end = NULL;
10785 const char *tok, *end_tok;
9c06b0b4 10786 int toklen = -1;
bbc13ae3
KS
10787 const char *cond_start = NULL;
10788 const char *cond_end = NULL;
c906108c 10789 enum bptype bp_type;
37e4754d 10790 int thread = -1;
0cf6dd15 10791 int pc = 0;
9c06b0b4
TJB
10792 /* Flag to indicate whether we are going to use masks for
10793 the hardware watchpoint. */
10794 int use_mask = 0;
10795 CORE_ADDR mask = 0;
bbc13ae3
KS
10796 char *expression;
10797 struct cleanup *back_to;
c906108c 10798
37e4754d
LM
10799 /* Make sure that we actually have parameters to parse. */
10800 if (arg != NULL && arg[0] != '\0')
10801 {
bbc13ae3
KS
10802 const char *value_start;
10803
10804 exp_end = arg + strlen (arg);
37e4754d 10805
9c06b0b4
TJB
10806 /* Look for "parameter value" pairs at the end
10807 of the arguments string. */
bbc13ae3 10808 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10809 {
10810 /* Skip whitespace at the end of the argument list. */
10811 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10812 tok--;
10813
10814 /* Find the beginning of the last token.
10815 This is the value of the parameter. */
10816 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10817 tok--;
10818 value_start = tok + 1;
10819
10820 /* Skip whitespace. */
10821 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10822 tok--;
10823
10824 end_tok = tok;
10825
10826 /* Find the beginning of the second to last token.
10827 This is the parameter itself. */
10828 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10829 tok--;
10830 tok++;
10831 toklen = end_tok - tok + 1;
10832
61012eef 10833 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10834 {
5d5658a1 10835 struct thread_info *thr;
9c06b0b4
TJB
10836 /* At this point we've found a "thread" token, which means
10837 the user is trying to set a watchpoint that triggers
10838 only in a specific thread. */
5d5658a1 10839 const char *endp;
37e4754d 10840
9c06b0b4
TJB
10841 if (thread != -1)
10842 error(_("You can specify only one thread."));
37e4754d 10843
9c06b0b4 10844 /* Extract the thread ID from the next token. */
5d5658a1 10845 thr = parse_thread_id (value_start, &endp);
37e4754d 10846
5d5658a1 10847 /* Check if the user provided a valid thread ID. */
9c06b0b4 10848 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10849 invalid_thread_id_error (value_start);
9c06b0b4 10850
5d5658a1 10851 thread = thr->global_num;
9c06b0b4 10852 }
61012eef 10853 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10854 {
10855 /* We've found a "mask" token, which means the user wants to
10856 create a hardware watchpoint that is going to have the mask
10857 facility. */
10858 struct value *mask_value, *mark;
37e4754d 10859
9c06b0b4
TJB
10860 if (use_mask)
10861 error(_("You can specify only one mask."));
37e4754d 10862
9c06b0b4 10863 use_mask = just_location = 1;
37e4754d 10864
9c06b0b4
TJB
10865 mark = value_mark ();
10866 mask_value = parse_to_comma_and_eval (&value_start);
10867 mask = value_as_address (mask_value);
10868 value_free_to_mark (mark);
10869 }
10870 else
10871 /* We didn't recognize what we found. We should stop here. */
10872 break;
37e4754d 10873
9c06b0b4
TJB
10874 /* Truncate the string and get rid of the "parameter value" pair before
10875 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10876 exp_end = tok;
9c06b0b4 10877 }
37e4754d 10878 }
bbc13ae3
KS
10879 else
10880 exp_end = arg;
37e4754d 10881
bbc13ae3
KS
10882 /* Parse the rest of the arguments. From here on out, everything
10883 is in terms of a newly allocated string instead of the original
10884 ARG. */
c906108c 10885 innermost_block = NULL;
bbc13ae3
KS
10886 expression = savestring (arg, exp_end - arg);
10887 back_to = make_cleanup (xfree, expression);
10888 exp_start = arg = expression;
4d01a485 10889 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10890 exp_end = arg;
fa8a61dc
TT
10891 /* Remove trailing whitespace from the expression before saving it.
10892 This makes the eventual display of the expression string a bit
10893 prettier. */
10894 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10895 --exp_end;
10896
65d79d4b 10897 /* Checking if the expression is not constant. */
4d01a485 10898 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10899 {
10900 int len;
10901
10902 len = exp_end - exp_start;
10903 while (len > 0 && isspace (exp_start[len - 1]))
10904 len--;
10905 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10906 }
10907
c906108c
SS
10908 exp_valid_block = innermost_block;
10909 mark = value_mark ();
4d01a485 10910 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10911
bb9d5f81
PP
10912 if (val != NULL && just_location)
10913 {
10914 saved_bitpos = value_bitpos (val);
10915 saved_bitsize = value_bitsize (val);
10916 }
10917
06a64a0b
TT
10918 if (just_location)
10919 {
9c06b0b4
TJB
10920 int ret;
10921
06a64a0b 10922 exp_valid_block = NULL;
a1442452 10923 val = value_addr (result);
06a64a0b
TT
10924 release_value (val);
10925 value_free_to_mark (mark);
9c06b0b4
TJB
10926
10927 if (use_mask)
10928 {
10929 ret = target_masked_watch_num_registers (value_as_address (val),
10930 mask);
10931 if (ret == -1)
10932 error (_("This target does not support masked watchpoints."));
10933 else if (ret == -2)
10934 error (_("Invalid mask or memory region."));
10935 }
06a64a0b
TT
10936 }
10937 else if (val != NULL)
fa4727a6 10938 release_value (val);
c906108c 10939
f1735a53
TT
10940 tok = skip_spaces (arg);
10941 end_tok = skip_to_space (tok);
c906108c
SS
10942
10943 toklen = end_tok - tok;
10944 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10945 {
60e1c644 10946 innermost_block = NULL;
c906108c 10947 tok = cond_start = end_tok + 1;
4d01a485 10948 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10949
10950 /* The watchpoint expression may not be local, but the condition
10951 may still be. E.g.: `watch global if local > 0'. */
10952 cond_exp_valid_block = innermost_block;
10953
c906108c
SS
10954 cond_end = tok;
10955 }
10956 if (*tok)
8a3fe4f8 10957 error (_("Junk at end of command."));
c906108c 10958
441d7c93
PA
10959 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10960
10961 /* Save this because create_internal_breakpoint below invalidates
10962 'wp_frame'. */
10963 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10964
10965 /* If the expression is "local", then set up a "watchpoint scope"
10966 breakpoint at the point where we've left the scope of the watchpoint
10967 expression. Create the scope breakpoint before the watchpoint, so
10968 that we will encounter it first in bpstat_stop_status. */
441d7c93 10969 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10970 {
441d7c93
PA
10971 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10972
10973 if (frame_id_p (caller_frame_id))
edb3359d 10974 {
441d7c93
PA
10975 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10976 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10977
edb3359d 10978 scope_breakpoint
441d7c93 10979 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10980 bp_watchpoint_scope,
10981 &momentary_breakpoint_ops);
d983da9c 10982
441d7c93
PA
10983 /* create_internal_breakpoint could invalidate WP_FRAME. */
10984 wp_frame = NULL;
10985
edb3359d 10986 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10987
edb3359d
DJ
10988 /* Automatically delete the breakpoint when it hits. */
10989 scope_breakpoint->disposition = disp_del;
d983da9c 10990
edb3359d 10991 /* Only break in the proper frame (help with recursion). */
441d7c93 10992 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10993
edb3359d 10994 /* Set the address at which we will stop. */
441d7c93
PA
10995 scope_breakpoint->loc->gdbarch = caller_arch;
10996 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10997 scope_breakpoint->loc->address
a6d9a66e
UW
10998 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10999 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11000 scope_breakpoint->type);
11001 }
d983da9c
DJ
11002 }
11003
e8369a73
AB
11004 /* Now set up the breakpoint. We create all watchpoints as hardware
11005 watchpoints here even if hardware watchpoints are turned off, a call
11006 to update_watchpoint later in this function will cause the type to
11007 drop back to bp_watchpoint (software watchpoint) if required. */
11008
11009 if (accessflag == hw_read)
11010 bp_type = bp_read_watchpoint;
11011 else if (accessflag == hw_access)
11012 bp_type = bp_access_watchpoint;
11013 else
11014 bp_type = bp_hardware_watchpoint;
3a5c3e22 11015
b270e6f9 11016 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 11017
348d480f 11018 if (use_mask)
b270e6f9 11019 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11020 &masked_watchpoint_breakpoint_ops);
348d480f 11021 else
b270e6f9 11022 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11023 &watchpoint_breakpoint_ops);
c1fc2657
SM
11024 w->thread = thread;
11025 w->disposition = disp_donttouch;
11026 w->pspace = current_program_space;
b22e99fd 11027 w->exp = std::move (exp);
3a5c3e22
PA
11028 w->exp_valid_block = exp_valid_block;
11029 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11030 if (just_location)
11031 {
11032 struct type *t = value_type (val);
11033 CORE_ADDR addr = value_as_address (val);
06a64a0b 11034
43cc5389
TT
11035 w->exp_string_reparse
11036 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11037
3a5c3e22 11038 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11039 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11040 }
11041 else
3a5c3e22 11042 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11043
11044 if (use_mask)
11045 {
3a5c3e22 11046 w->hw_wp_mask = mask;
9c06b0b4
TJB
11047 }
11048 else
11049 {
3a5c3e22 11050 w->val = val;
bb9d5f81
PP
11051 w->val_bitpos = saved_bitpos;
11052 w->val_bitsize = saved_bitsize;
3a5c3e22 11053 w->val_valid = 1;
9c06b0b4 11054 }
77b06cd7 11055
c906108c 11056 if (cond_start)
c1fc2657 11057 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 11058 else
c1fc2657 11059 w->cond_string = 0;
c5aa993b 11060
441d7c93 11061 if (frame_id_p (watchpoint_frame))
f6bc2008 11062 {
441d7c93 11063 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11064 w->watchpoint_thread = inferior_ptid;
f6bc2008 11065 }
c906108c 11066 else
f6bc2008 11067 {
3a5c3e22
PA
11068 w->watchpoint_frame = null_frame_id;
11069 w->watchpoint_thread = null_ptid;
f6bc2008 11070 }
c906108c 11071
d983da9c 11072 if (scope_breakpoint != NULL)
c906108c 11073 {
d983da9c
DJ
11074 /* The scope breakpoint is related to the watchpoint. We will
11075 need to act on them together. */
c1fc2657 11076 w->related_breakpoint = scope_breakpoint;
b270e6f9 11077 scope_breakpoint->related_breakpoint = w.get ();
c906108c 11078 }
d983da9c 11079
06a64a0b
TT
11080 if (!just_location)
11081 value_free_to_mark (mark);
2d134ed3 11082
b270e6f9
TT
11083 /* Finally update the new watchpoint. This creates the locations
11084 that should be inserted. */
11085 update_watchpoint (w.get (), 1);
a9634178 11086
b270e6f9 11087 install_breakpoint (internal, std::move (w), 1);
bbc13ae3 11088 do_cleanups (back_to);
c906108c
SS
11089}
11090
e09342b5 11091/* Return count of debug registers needed to watch the given expression.
e09342b5 11092 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11093
c906108c 11094static int
a9634178 11095can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11096{
11097 int found_memory_cnt = 0;
2e70b7b9 11098 struct value *head = v;
c906108c
SS
11099
11100 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11101 if (!can_use_hw_watchpoints)
c906108c 11102 return 0;
c5aa993b 11103
5c44784c
JM
11104 /* Make sure that the value of the expression depends only upon
11105 memory contents, and values computed from them within GDB. If we
11106 find any register references or function calls, we can't use a
11107 hardware watchpoint.
11108
11109 The idea here is that evaluating an expression generates a series
11110 of values, one holding the value of every subexpression. (The
11111 expression a*b+c has five subexpressions: a, b, a*b, c, and
11112 a*b+c.) GDB's values hold almost enough information to establish
11113 the criteria given above --- they identify memory lvalues,
11114 register lvalues, computed values, etcetera. So we can evaluate
11115 the expression, and then scan the chain of values that leaves
11116 behind to decide whether we can detect any possible change to the
11117 expression's final value using only hardware watchpoints.
11118
11119 However, I don't think that the values returned by inferior
11120 function calls are special in any way. So this function may not
11121 notice that an expression involving an inferior function call
11122 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11123 for (; v; v = value_next (v))
c906108c 11124 {
5c44784c 11125 if (VALUE_LVAL (v) == lval_memory)
c906108c 11126 {
8464be76
DJ
11127 if (v != head && value_lazy (v))
11128 /* A lazy memory lvalue in the chain is one that GDB never
11129 needed to fetch; we either just used its address (e.g.,
11130 `a' in `a.b') or we never needed it at all (e.g., `a'
11131 in `a,b'). This doesn't apply to HEAD; if that is
11132 lazy then it was not readable, but watch it anyway. */
5c44784c 11133 ;
53a5351d 11134 else
5c44784c
JM
11135 {
11136 /* Ahh, memory we actually used! Check if we can cover
11137 it with hardware watchpoints. */
df407dfe 11138 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11139
11140 /* We only watch structs and arrays if user asked for it
11141 explicitly, never if they just happen to appear in a
11142 middle of some value chain. */
11143 if (v == head
11144 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11145 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11146 {
42ae5230 11147 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11148 int len;
11149 int num_regs;
11150
a9634178 11151 len = (target_exact_watchpoints
e09342b5
TJB
11152 && is_scalar_type_recursive (vtype))?
11153 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11154
e09342b5
TJB
11155 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11156 if (!num_regs)
2e70b7b9
MS
11157 return 0;
11158 else
e09342b5 11159 found_memory_cnt += num_regs;
2e70b7b9 11160 }
5c44784c 11161 }
c5aa993b 11162 }
5086187c
AC
11163 else if (VALUE_LVAL (v) != not_lval
11164 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11165 return 0; /* These are values from the history (e.g., $1). */
5086187c 11166 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11167 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11168 }
11169
11170 /* The expression itself looks suitable for using a hardware
11171 watchpoint, but give the target machine a chance to reject it. */
11172 return found_memory_cnt;
11173}
11174
8b93c638 11175void
f2fc3015 11176watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11177{
84f4c1fe 11178 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11179}
11180
06a64a0b
TT
11181/* A helper function that looks for the "-location" argument and then
11182 calls watch_command_1. */
11183
11184static void
11185watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11186{
11187 int just_location = 0;
11188
11189 if (arg
11190 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11191 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11192 {
e9cafbcc 11193 arg = skip_spaces (arg);
06a64a0b
TT
11194 just_location = 1;
11195 }
11196
84f4c1fe 11197 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11198}
8926118c 11199
c5aa993b 11200static void
fba45db2 11201watch_command (char *arg, int from_tty)
c906108c 11202{
06a64a0b 11203 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11204}
11205
8b93c638 11206void
f2fc3015 11207rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11208{
84f4c1fe 11209 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11210}
8926118c 11211
c5aa993b 11212static void
fba45db2 11213rwatch_command (char *arg, int from_tty)
c906108c 11214{
06a64a0b 11215 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11216}
11217
8b93c638 11218void
f2fc3015 11219awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11220{
84f4c1fe 11221 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11222}
8926118c 11223
c5aa993b 11224static void
fba45db2 11225awatch_command (char *arg, int from_tty)
c906108c 11226{
06a64a0b 11227 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11228}
c906108c 11229\f
c5aa993b 11230
cfc31633
PA
11231/* Data for the FSM that manages the until(location)/advance commands
11232 in infcmd.c. Here because it uses the mechanisms of
11233 breakpoints. */
c906108c 11234
cfc31633 11235struct until_break_fsm
bfec99b2 11236{
cfc31633
PA
11237 /* The base class. */
11238 struct thread_fsm thread_fsm;
11239
11240 /* The thread that as current when the command was executed. */
11241 int thread;
11242
11243 /* The breakpoint set at the destination location. */
11244 struct breakpoint *location_breakpoint;
11245
11246 /* Breakpoint set at the return address in the caller frame. May be
11247 NULL. */
11248 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11249};
11250
8980e177
PA
11251static void until_break_fsm_clean_up (struct thread_fsm *self,
11252 struct thread_info *thread);
11253static int until_break_fsm_should_stop (struct thread_fsm *self,
11254 struct thread_info *thread);
cfc31633
PA
11255static enum async_reply_reason
11256 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11257
11258/* until_break_fsm's vtable. */
11259
11260static struct thread_fsm_ops until_break_fsm_ops =
11261{
11262 NULL, /* dtor */
11263 until_break_fsm_clean_up,
11264 until_break_fsm_should_stop,
11265 NULL, /* return_value */
11266 until_break_fsm_async_reply_reason,
11267};
11268
11269/* Allocate a new until_break_command_fsm. */
11270
11271static struct until_break_fsm *
8980e177 11272new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11273 struct breakpoint *location_breakpoint,
11274 struct breakpoint *caller_breakpoint)
11275{
11276 struct until_break_fsm *sm;
11277
11278 sm = XCNEW (struct until_break_fsm);
8980e177 11279 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11280
11281 sm->thread = thread;
11282 sm->location_breakpoint = location_breakpoint;
11283 sm->caller_breakpoint = caller_breakpoint;
11284
11285 return sm;
11286}
11287
11288/* Implementation of the 'should_stop' FSM method for the
11289 until(location)/advance commands. */
11290
11291static int
8980e177
PA
11292until_break_fsm_should_stop (struct thread_fsm *self,
11293 struct thread_info *tp)
cfc31633
PA
11294{
11295 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11296
11297 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11298 sm->location_breakpoint) != NULL
11299 || (sm->caller_breakpoint != NULL
11300 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11301 sm->caller_breakpoint) != NULL))
11302 thread_fsm_set_finished (self);
11303
11304 return 1;
11305}
11306
11307/* Implementation of the 'clean_up' FSM method for the
11308 until(location)/advance commands. */
11309
c2c6d25f 11310static void
8980e177
PA
11311until_break_fsm_clean_up (struct thread_fsm *self,
11312 struct thread_info *thread)
43ff13b4 11313{
cfc31633 11314 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11315
cfc31633
PA
11316 /* Clean up our temporary breakpoints. */
11317 if (sm->location_breakpoint != NULL)
11318 {
11319 delete_breakpoint (sm->location_breakpoint);
11320 sm->location_breakpoint = NULL;
11321 }
11322 if (sm->caller_breakpoint != NULL)
11323 {
11324 delete_breakpoint (sm->caller_breakpoint);
11325 sm->caller_breakpoint = NULL;
11326 }
11327 delete_longjmp_breakpoint (sm->thread);
11328}
11329
11330/* Implementation of the 'async_reply_reason' FSM method for the
11331 until(location)/advance commands. */
11332
11333static enum async_reply_reason
11334until_break_fsm_async_reply_reason (struct thread_fsm *self)
11335{
11336 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11337}
11338
c906108c 11339void
f2fc3015 11340until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11341{
8556afb4
PA
11342 struct frame_info *frame;
11343 struct gdbarch *frame_gdbarch;
11344 struct frame_id stack_frame_id;
11345 struct frame_id caller_frame_id;
cfc31633
PA
11346 struct breakpoint *location_breakpoint;
11347 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11348 struct cleanup *old_chain;
186c406b
TT
11349 int thread;
11350 struct thread_info *tp;
cfc31633 11351 struct until_break_fsm *sm;
c906108c 11352
70509625 11353 clear_proceed_status (0);
c906108c
SS
11354
11355 /* Set a breakpoint where the user wants it and at return from
4a64f543 11356 this function. */
c5aa993b 11357
ffc2605c 11358 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11359
6c5b2ebe
PA
11360 std::vector<symtab_and_line> sals
11361 = (last_displayed_sal_is_valid ()
11362 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11363 get_last_displayed_symtab (),
11364 get_last_displayed_line ())
11365 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11366 NULL, (struct symtab *) NULL, 0));
c5aa993b 11367
6c5b2ebe 11368 if (sals.size () != 1)
8a3fe4f8 11369 error (_("Couldn't get information on specified line."));
c5aa993b 11370
6c5b2ebe 11371 symtab_and_line &sal = sals[0];
c5aa993b 11372
c906108c 11373 if (*arg)
8a3fe4f8 11374 error (_("Junk at end of arguments."));
c5aa993b 11375
c906108c 11376 resolve_sal_pc (&sal);
c5aa993b 11377
186c406b 11378 tp = inferior_thread ();
5d5658a1 11379 thread = tp->global_num;
186c406b 11380
883bc8d1
PA
11381 old_chain = make_cleanup (null_cleanup, NULL);
11382
8556afb4
PA
11383 /* Note linespec handling above invalidates the frame chain.
11384 Installing a breakpoint also invalidates the frame chain (as it
11385 may need to switch threads), so do any frame handling before
11386 that. */
11387
11388 frame = get_selected_frame (NULL);
11389 frame_gdbarch = get_frame_arch (frame);
11390 stack_frame_id = get_stack_frame_id (frame);
11391 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11392
ae66c1fc
EZ
11393 /* Keep within the current frame, or in frames called by the current
11394 one. */
edb3359d 11395
883bc8d1 11396 if (frame_id_p (caller_frame_id))
c906108c 11397 {
883bc8d1 11398 struct symtab_and_line sal2;
cfc31633 11399 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11400
11401 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11402 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11403 caller_gdbarch = frame_unwind_caller_arch (frame);
11404 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11405 sal2,
11406 caller_frame_id,
11407 bp_until);
11408 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11409
883bc8d1 11410 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11411 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11412 }
c5aa993b 11413
c70a6932
JK
11414 /* set_momentary_breakpoint could invalidate FRAME. */
11415 frame = NULL;
11416
883bc8d1
PA
11417 if (anywhere)
11418 /* If the user told us to continue until a specified location,
11419 we don't specify a frame at which we need to stop. */
cfc31633
PA
11420 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11421 null_frame_id, bp_until);
883bc8d1
PA
11422 else
11423 /* Otherwise, specify the selected frame, because we want to stop
11424 only at the very same frame. */
cfc31633
PA
11425 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11426 stack_frame_id, bp_until);
11427 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11428
8980e177 11429 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11430 location_breakpoint, caller_breakpoint);
cfc31633 11431 tp->thread_fsm = &sm->thread_fsm;
f107f563 11432
cfc31633 11433 discard_cleanups (old_chain);
f107f563 11434
cfc31633 11435 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11436}
ae66c1fc 11437
c906108c
SS
11438/* This function attempts to parse an optional "if <cond>" clause
11439 from the arg string. If one is not found, it returns NULL.
c5aa993b 11440
c906108c
SS
11441 Else, it returns a pointer to the condition string. (It does not
11442 attempt to evaluate the string against a particular block.) And,
11443 it updates arg to point to the first character following the parsed
4a64f543 11444 if clause in the arg string. */
53a5351d 11445
63160a43
PA
11446const char *
11447ep_parse_optional_if_clause (const char **arg)
c906108c 11448{
63160a43 11449 const char *cond_string;
c5aa993b
JM
11450
11451 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11452 return NULL;
c5aa993b 11453
4a64f543 11454 /* Skip the "if" keyword. */
c906108c 11455 (*arg) += 2;
c5aa993b 11456
c906108c 11457 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11458 condition string. */
f1735a53 11459 *arg = skip_spaces (*arg);
c906108c 11460 cond_string = *arg;
c5aa993b 11461
4a64f543
MS
11462 /* Assume that the condition occupies the remainder of the arg
11463 string. */
c906108c 11464 (*arg) += strlen (cond_string);
c5aa993b 11465
c906108c
SS
11466 return cond_string;
11467}
c5aa993b 11468
c906108c
SS
11469/* Commands to deal with catching events, such as signals, exceptions,
11470 process start/exit, etc. */
c5aa993b
JM
11471
11472typedef enum
11473{
44feb3ce
TT
11474 catch_fork_temporary, catch_vfork_temporary,
11475 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11476}
11477catch_fork_kind;
11478
c906108c 11479static void
63160a43 11480catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11481 struct cmd_list_element *command)
c906108c 11482{
63160a43 11483 const char *arg = arg_entry;
a6d9a66e 11484 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11485 const char *cond_string = NULL;
44feb3ce
TT
11486 catch_fork_kind fork_kind;
11487 int tempflag;
11488
11489 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11490 tempflag = (fork_kind == catch_fork_temporary
11491 || fork_kind == catch_vfork_temporary);
c5aa993b 11492
44feb3ce
TT
11493 if (!arg)
11494 arg = "";
f1735a53 11495 arg = skip_spaces (arg);
c5aa993b 11496
c906108c 11497 /* The allowed syntax is:
c5aa993b
JM
11498 catch [v]fork
11499 catch [v]fork if <cond>
11500
4a64f543 11501 First, check if there's an if clause. */
c906108c 11502 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11503
c906108c 11504 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11505 error (_("Junk at end of arguments."));
c5aa993b 11506
c906108c 11507 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11508 and enable reporting of such events. */
c5aa993b
JM
11509 switch (fork_kind)
11510 {
44feb3ce
TT
11511 case catch_fork_temporary:
11512 case catch_fork_permanent:
a6d9a66e 11513 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11514 &catch_fork_breakpoint_ops);
c906108c 11515 break;
44feb3ce
TT
11516 case catch_vfork_temporary:
11517 case catch_vfork_permanent:
a6d9a66e 11518 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11519 &catch_vfork_breakpoint_ops);
c906108c 11520 break;
c5aa993b 11521 default:
8a3fe4f8 11522 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11523 break;
c5aa993b 11524 }
c906108c
SS
11525}
11526
11527static void
63160a43 11528catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11529 struct cmd_list_element *command)
c906108c 11530{
63160a43 11531 const char *arg = arg_entry;
a6d9a66e 11532 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11533 int tempflag;
63160a43 11534 const char *cond_string = NULL;
c906108c 11535
44feb3ce
TT
11536 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11537
11538 if (!arg)
11539 arg = "";
f1735a53 11540 arg = skip_spaces (arg);
c906108c
SS
11541
11542 /* The allowed syntax is:
c5aa993b
JM
11543 catch exec
11544 catch exec if <cond>
c906108c 11545
4a64f543 11546 First, check if there's an if clause. */
c906108c
SS
11547 cond_string = ep_parse_optional_if_clause (&arg);
11548
11549 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11550 error (_("Junk at end of arguments."));
c906108c 11551
b270e6f9
TT
11552 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11553 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11554 &catch_exec_breakpoint_ops);
11555 c->exec_pathname = NULL;
11556
b270e6f9 11557 install_breakpoint (0, std::move (c), 1);
c906108c 11558}
c5aa993b 11559
9ac4176b 11560void
28010a5d
PA
11561init_ada_exception_breakpoint (struct breakpoint *b,
11562 struct gdbarch *gdbarch,
11563 struct symtab_and_line sal,
f2fc3015 11564 const char *addr_string,
c0a91b2b 11565 const struct breakpoint_ops *ops,
28010a5d 11566 int tempflag,
349774ef 11567 int enabled,
28010a5d 11568 int from_tty)
f7f9143b 11569{
f7f9143b
JB
11570 if (from_tty)
11571 {
5af949e3
UW
11572 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11573 if (!loc_gdbarch)
11574 loc_gdbarch = gdbarch;
11575
6c95b8df
PA
11576 describe_other_breakpoints (loc_gdbarch,
11577 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11578 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11579 version for exception catchpoints, because two catchpoints
11580 used for different exception names will use the same address.
11581 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11582 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11583 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11584 the user what type of catchpoint it is. The above is good
11585 enough for now, though. */
11586 }
11587
28010a5d 11588 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11589
349774ef 11590 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11591 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11592 b->location = string_to_event_location (&addr_string,
11593 language_def (language_ada));
f7f9143b 11594 b->language = language_ada;
f7f9143b
JB
11595}
11596
c906108c 11597static void
fba45db2 11598catch_command (char *arg, int from_tty)
c906108c 11599{
44feb3ce 11600 error (_("Catch requires an event name."));
c906108c
SS
11601}
11602\f
11603
11604static void
fba45db2 11605tcatch_command (char *arg, int from_tty)
c906108c 11606{
44feb3ce 11607 error (_("Catch requires an event name."));
c906108c
SS
11608}
11609
8a2c437b
TT
11610/* A qsort comparison function that sorts breakpoints in order. */
11611
11612static int
11613compare_breakpoints (const void *a, const void *b)
11614{
9a3c8263 11615 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11616 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11617 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11618 uintptr_t ub = (uintptr_t) *bb;
11619
11620 if ((*ba)->number < (*bb)->number)
11621 return -1;
11622 else if ((*ba)->number > (*bb)->number)
11623 return 1;
11624
11625 /* Now sort by address, in case we see, e..g, two breakpoints with
11626 the number 0. */
11627 if (ua < ub)
11628 return -1;
94b0e70d 11629 return ua > ub ? 1 : 0;
8a2c437b
TT
11630}
11631
80f8a6eb 11632/* Delete breakpoints by address or line. */
c906108c
SS
11633
11634static void
fba45db2 11635clear_command (char *arg, int from_tty)
c906108c 11636{
8a2c437b 11637 struct breakpoint *b, *prev;
d6e956e5
VP
11638 VEC(breakpoint_p) *found = 0;
11639 int ix;
c906108c 11640 int default_match;
c906108c 11641 int i;
8a2c437b 11642 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c 11643
6c5b2ebe
PA
11644 std::vector<symtab_and_line> decoded_sals;
11645 symtab_and_line last_sal;
11646 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11647 if (arg)
11648 {
6c5b2ebe
PA
11649 decoded_sals
11650 = decode_line_with_current_source (arg,
11651 (DECODE_LINE_FUNFIRSTLINE
11652 | DECODE_LINE_LIST_MODE));
c906108c 11653 default_match = 0;
6c5b2ebe 11654 sals = decoded_sals;
c906108c
SS
11655 }
11656 else
11657 {
1bfeeb0f
JL
11658 /* Set sal's line, symtab, pc, and pspace to the values
11659 corresponding to the last call to print_frame_info. If the
11660 codepoint is not valid, this will set all the fields to 0. */
51abb421 11661 last_sal = get_last_displayed_sal ();
6c5b2ebe 11662 if (last_sal.symtab == 0)
8a3fe4f8 11663 error (_("No source file specified."));
c906108c 11664
c906108c 11665 default_match = 1;
6c5b2ebe 11666 sals = last_sal;
c906108c
SS
11667 }
11668
4a64f543
MS
11669 /* We don't call resolve_sal_pc here. That's not as bad as it
11670 seems, because all existing breakpoints typically have both
11671 file/line and pc set. So, if clear is given file/line, we can
11672 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11673
11674 We only support clearing given the address explicitly
11675 present in breakpoint table. Say, we've set breakpoint
4a64f543 11676 at file:line. There were several PC values for that file:line,
ed0616c6 11677 due to optimization, all in one block.
4a64f543
MS
11678
11679 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11680 PC corresponding to the same file:line, the breakpoint won't
11681 be cleared. We probably can still clear the breakpoint, but
11682 since the other PC value is never presented to user, user
11683 can only find it by guessing, and it does not seem important
11684 to support that. */
11685
4a64f543
MS
11686 /* For each line spec given, delete bps which correspond to it. Do
11687 it in two passes, solely to preserve the current behavior that
11688 from_tty is forced true if we delete more than one
11689 breakpoint. */
c906108c 11690
80f8a6eb 11691 found = NULL;
8a2c437b 11692 make_cleanup (VEC_cleanup (breakpoint_p), &found);
6c5b2ebe 11693 for (const auto &sal : sals)
c906108c 11694 {
05cba821
JK
11695 const char *sal_fullname;
11696
c906108c 11697 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11698 If line given (pc == 0), clear all bpts on specified line.
11699 If defaulting, clear all bpts on default line
c906108c 11700 or at default pc.
c5aa993b
JM
11701
11702 defaulting sal.pc != 0 tests to do
11703
11704 0 1 pc
11705 1 1 pc _and_ line
11706 0 0 line
11707 1 0 <can't happen> */
c906108c 11708
05cba821
JK
11709 sal_fullname = (sal.symtab == NULL
11710 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11711
4a64f543 11712 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11713 ALL_BREAKPOINTS (b)
c5aa993b 11714 {
0d381245 11715 int match = 0;
4a64f543 11716 /* Are we going to delete b? */
cc60f2e3 11717 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11718 {
11719 struct bp_location *loc = b->loc;
11720 for (; loc; loc = loc->next)
11721 {
f8eba3c6
TT
11722 /* If the user specified file:line, don't allow a PC
11723 match. This matches historical gdb behavior. */
11724 int pc_match = (!sal.explicit_line
11725 && sal.pc
11726 && (loc->pspace == sal.pspace)
11727 && (loc->address == sal.pc)
11728 && (!section_is_overlay (loc->section)
11729 || loc->section == sal.section));
4aac40c8
TT
11730 int line_match = 0;
11731
11732 if ((default_match || sal.explicit_line)
2f202fde 11733 && loc->symtab != NULL
05cba821 11734 && sal_fullname != NULL
4aac40c8 11735 && sal.pspace == loc->pspace
05cba821
JK
11736 && loc->line_number == sal.line
11737 && filename_cmp (symtab_to_fullname (loc->symtab),
11738 sal_fullname) == 0)
11739 line_match = 1;
4aac40c8 11740
0d381245
VP
11741 if (pc_match || line_match)
11742 {
11743 match = 1;
11744 break;
11745 }
11746 }
11747 }
11748
11749 if (match)
d6e956e5 11750 VEC_safe_push(breakpoint_p, found, b);
c906108c 11751 }
80f8a6eb 11752 }
8a2c437b 11753
80f8a6eb 11754 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11755 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11756 {
11757 if (arg)
8a3fe4f8 11758 error (_("No breakpoint at %s."), arg);
80f8a6eb 11759 else
8a3fe4f8 11760 error (_("No breakpoint at this line."));
80f8a6eb 11761 }
c906108c 11762
8a2c437b
TT
11763 /* Remove duplicates from the vec. */
11764 qsort (VEC_address (breakpoint_p, found),
11765 VEC_length (breakpoint_p, found),
11766 sizeof (breakpoint_p),
11767 compare_breakpoints);
11768 prev = VEC_index (breakpoint_p, found, 0);
11769 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11770 {
11771 if (b == prev)
11772 {
11773 VEC_ordered_remove (breakpoint_p, found, ix);
11774 --ix;
11775 }
11776 }
11777
d6e956e5 11778 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11779 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11780 if (from_tty)
a3f17187 11781 {
d6e956e5 11782 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11783 printf_unfiltered (_("Deleted breakpoint "));
11784 else
11785 printf_unfiltered (_("Deleted breakpoints "));
11786 }
d6e956e5
VP
11787
11788 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11789 {
c5aa993b 11790 if (from_tty)
d6e956e5
VP
11791 printf_unfiltered ("%d ", b->number);
11792 delete_breakpoint (b);
c906108c 11793 }
80f8a6eb
MS
11794 if (from_tty)
11795 putchar_unfiltered ('\n');
8a2c437b
TT
11796
11797 do_cleanups (cleanups);
c906108c
SS
11798}
11799\f
11800/* Delete breakpoint in BS if they are `delete' breakpoints and
11801 all breakpoints that are marked for deletion, whether hit or not.
11802 This is called after any breakpoint is hit, or after errors. */
11803
11804void
fba45db2 11805breakpoint_auto_delete (bpstat bs)
c906108c 11806{
35df4500 11807 struct breakpoint *b, *b_tmp;
c906108c
SS
11808
11809 for (; bs; bs = bs->next)
f431efe5
PA
11810 if (bs->breakpoint_at
11811 && bs->breakpoint_at->disposition == disp_del
c906108c 11812 && bs->stop)
f431efe5 11813 delete_breakpoint (bs->breakpoint_at);
c906108c 11814
35df4500 11815 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11816 {
b5de0fa7 11817 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11818 delete_breakpoint (b);
11819 }
c906108c
SS
11820}
11821
4a64f543
MS
11822/* A comparison function for bp_location AP and BP being interfaced to
11823 qsort. Sort elements primarily by their ADDRESS (no matter what
11824 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11825 secondarily by ordering first permanent elements and
4a64f543 11826 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11827 qsort being an unstable algorithm. */
876fa593
JK
11828
11829static int
f5336ca5 11830bp_locations_compare (const void *ap, const void *bp)
876fa593 11831{
9a3c8263
SM
11832 const struct bp_location *a = *(const struct bp_location **) ap;
11833 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11834
11835 if (a->address != b->address)
11836 return (a->address > b->address) - (a->address < b->address);
11837
dea2aa5f
LM
11838 /* Sort locations at the same address by their pspace number, keeping
11839 locations of the same inferior (in a multi-inferior environment)
11840 grouped. */
11841
11842 if (a->pspace->num != b->pspace->num)
11843 return ((a->pspace->num > b->pspace->num)
11844 - (a->pspace->num < b->pspace->num));
11845
876fa593 11846 /* Sort permanent breakpoints first. */
1a853c52
PA
11847 if (a->permanent != b->permanent)
11848 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11849
c56a97f9
JK
11850 /* Make the internal GDB representation stable across GDB runs
11851 where A and B memory inside GDB can differ. Breakpoint locations of
11852 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11853
11854 if (a->owner->number != b->owner->number)
c56a97f9
JK
11855 return ((a->owner->number > b->owner->number)
11856 - (a->owner->number < b->owner->number));
876fa593
JK
11857
11858 return (a > b) - (a < b);
11859}
11860
f5336ca5
PA
11861/* Set bp_locations_placed_address_before_address_max and
11862 bp_locations_shadow_len_after_address_max according to the current
11863 content of the bp_locations array. */
f7545552
TT
11864
11865static void
f5336ca5 11866bp_locations_target_extensions_update (void)
f7545552 11867{
876fa593
JK
11868 struct bp_location *bl, **blp_tmp;
11869
f5336ca5
PA
11870 bp_locations_placed_address_before_address_max = 0;
11871 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11872
11873 ALL_BP_LOCATIONS (bl, blp_tmp)
11874 {
11875 CORE_ADDR start, end, addr;
11876
11877 if (!bp_location_has_shadow (bl))
11878 continue;
11879
11880 start = bl->target_info.placed_address;
11881 end = start + bl->target_info.shadow_len;
11882
11883 gdb_assert (bl->address >= start);
11884 addr = bl->address - start;
f5336ca5
PA
11885 if (addr > bp_locations_placed_address_before_address_max)
11886 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11887
11888 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11889
11890 gdb_assert (bl->address < end);
11891 addr = end - bl->address;
f5336ca5
PA
11892 if (addr > bp_locations_shadow_len_after_address_max)
11893 bp_locations_shadow_len_after_address_max = addr;
876fa593 11894 }
f7545552
TT
11895}
11896
1e4d1764
YQ
11897/* Download tracepoint locations if they haven't been. */
11898
11899static void
11900download_tracepoint_locations (void)
11901{
7ed2c994 11902 struct breakpoint *b;
dd2e65cc 11903 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11904
5ed8105e 11905 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11906
7ed2c994 11907 ALL_TRACEPOINTS (b)
1e4d1764 11908 {
7ed2c994 11909 struct bp_location *bl;
1e4d1764 11910 struct tracepoint *t;
f2a8bc8a 11911 int bp_location_downloaded = 0;
1e4d1764 11912
7ed2c994 11913 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11914 ? !may_insert_fast_tracepoints
11915 : !may_insert_tracepoints))
11916 continue;
11917
dd2e65cc
YQ
11918 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11919 {
11920 if (target_can_download_tracepoint ())
11921 can_download_tracepoint = TRIBOOL_TRUE;
11922 else
11923 can_download_tracepoint = TRIBOOL_FALSE;
11924 }
11925
11926 if (can_download_tracepoint == TRIBOOL_FALSE)
11927 break;
11928
7ed2c994
YQ
11929 for (bl = b->loc; bl; bl = bl->next)
11930 {
11931 /* In tracepoint, locations are _never_ duplicated, so
11932 should_be_inserted is equivalent to
11933 unduplicated_should_be_inserted. */
11934 if (!should_be_inserted (bl) || bl->inserted)
11935 continue;
1e4d1764 11936
7ed2c994 11937 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11938
7ed2c994 11939 target_download_tracepoint (bl);
1e4d1764 11940
7ed2c994 11941 bl->inserted = 1;
f2a8bc8a 11942 bp_location_downloaded = 1;
7ed2c994
YQ
11943 }
11944 t = (struct tracepoint *) b;
11945 t->number_on_target = b->number;
f2a8bc8a
YQ
11946 if (bp_location_downloaded)
11947 observer_notify_breakpoint_modified (b);
1e4d1764 11948 }
1e4d1764
YQ
11949}
11950
934709f0
PW
11951/* Swap the insertion/duplication state between two locations. */
11952
11953static void
11954swap_insertion (struct bp_location *left, struct bp_location *right)
11955{
11956 const int left_inserted = left->inserted;
11957 const int left_duplicate = left->duplicate;
b775012e 11958 const int left_needs_update = left->needs_update;
934709f0
PW
11959 const struct bp_target_info left_target_info = left->target_info;
11960
1e4d1764
YQ
11961 /* Locations of tracepoints can never be duplicated. */
11962 if (is_tracepoint (left->owner))
11963 gdb_assert (!left->duplicate);
11964 if (is_tracepoint (right->owner))
11965 gdb_assert (!right->duplicate);
11966
934709f0
PW
11967 left->inserted = right->inserted;
11968 left->duplicate = right->duplicate;
b775012e 11969 left->needs_update = right->needs_update;
934709f0
PW
11970 left->target_info = right->target_info;
11971 right->inserted = left_inserted;
11972 right->duplicate = left_duplicate;
b775012e 11973 right->needs_update = left_needs_update;
934709f0
PW
11974 right->target_info = left_target_info;
11975}
11976
b775012e
LM
11977/* Force the re-insertion of the locations at ADDRESS. This is called
11978 once a new/deleted/modified duplicate location is found and we are evaluating
11979 conditions on the target's side. Such conditions need to be updated on
11980 the target. */
11981
11982static void
11983force_breakpoint_reinsertion (struct bp_location *bl)
11984{
11985 struct bp_location **locp = NULL, **loc2p;
11986 struct bp_location *loc;
11987 CORE_ADDR address = 0;
11988 int pspace_num;
11989
11990 address = bl->address;
11991 pspace_num = bl->pspace->num;
11992
11993 /* This is only meaningful if the target is
11994 evaluating conditions and if the user has
11995 opted for condition evaluation on the target's
11996 side. */
11997 if (gdb_evaluates_breakpoint_condition_p ()
11998 || !target_supports_evaluation_of_breakpoint_conditions ())
11999 return;
12000
12001 /* Flag all breakpoint locations with this address and
12002 the same program space as the location
12003 as "its condition has changed". We need to
12004 update the conditions on the target's side. */
12005 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12006 {
12007 loc = *loc2p;
12008
12009 if (!is_breakpoint (loc->owner)
12010 || pspace_num != loc->pspace->num)
12011 continue;
12012
12013 /* Flag the location appropriately. We use a different state to
12014 let everyone know that we already updated the set of locations
12015 with addr bl->address and program space bl->pspace. This is so
12016 we don't have to keep calling these functions just to mark locations
12017 that have already been marked. */
12018 loc->condition_changed = condition_updated;
12019
12020 /* Free the agent expression bytecode as well. We will compute
12021 it later on. */
833177a4 12022 loc->cond_bytecode.reset ();
b775012e
LM
12023 }
12024}
44702360
PA
12025/* Called whether new breakpoints are created, or existing breakpoints
12026 deleted, to update the global location list and recompute which
12027 locations are duplicate of which.
b775012e 12028
04086b45
PA
12029 The INSERT_MODE flag determines whether locations may not, may, or
12030 shall be inserted now. See 'enum ugll_insert_mode' for more
12031 info. */
b60e7edf 12032
0d381245 12033static void
44702360 12034update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12035{
74960c60 12036 struct breakpoint *b;
876fa593 12037 struct bp_location **locp, *loc;
f7545552 12038 struct cleanup *cleanups;
b775012e
LM
12039 /* Last breakpoint location address that was marked for update. */
12040 CORE_ADDR last_addr = 0;
12041 /* Last breakpoint location program space that was marked for update. */
12042 int last_pspace_num = -1;
f7545552 12043
2d134ed3
PA
12044 /* Used in the duplicates detection below. When iterating over all
12045 bp_locations, points to the first bp_location of a given address.
12046 Breakpoints and watchpoints of different types are never
12047 duplicates of each other. Keep one pointer for each type of
12048 breakpoint/watchpoint, so we only need to loop over all locations
12049 once. */
12050 struct bp_location *bp_loc_first; /* breakpoint */
12051 struct bp_location *wp_loc_first; /* hardware watchpoint */
12052 struct bp_location *awp_loc_first; /* access watchpoint */
12053 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12054
f5336ca5
PA
12055 /* Saved former bp_locations array which we compare against the newly
12056 built bp_locations from the current state of ALL_BREAKPOINTS. */
12057 struct bp_location **old_locations, **old_locp;
12058 unsigned old_locations_count;
876fa593 12059
f5336ca5
PA
12060 old_locations = bp_locations;
12061 old_locations_count = bp_locations_count;
12062 bp_locations = NULL;
12063 bp_locations_count = 0;
12064 cleanups = make_cleanup (xfree, old_locations);
0d381245 12065
74960c60 12066 ALL_BREAKPOINTS (b)
876fa593 12067 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12068 bp_locations_count++;
876fa593 12069
f5336ca5
PA
12070 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12071 locp = bp_locations;
876fa593
JK
12072 ALL_BREAKPOINTS (b)
12073 for (loc = b->loc; loc; loc = loc->next)
12074 *locp++ = loc;
f5336ca5
PA
12075 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12076 bp_locations_compare);
876fa593 12077
f5336ca5 12078 bp_locations_target_extensions_update ();
74960c60 12079
4a64f543
MS
12080 /* Identify bp_location instances that are no longer present in the
12081 new list, and therefore should be freed. Note that it's not
12082 necessary that those locations should be removed from inferior --
12083 if there's another location at the same address (previously
12084 marked as duplicate), we don't need to remove/insert the
12085 location.
876fa593 12086
4a64f543
MS
12087 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12088 and former bp_location array state respectively. */
876fa593 12089
f5336ca5
PA
12090 locp = bp_locations;
12091 for (old_locp = old_locations;
12092 old_locp < old_locations + old_locations_count;
876fa593 12093 old_locp++)
74960c60 12094 {
876fa593 12095 struct bp_location *old_loc = *old_locp;
c7d46a38 12096 struct bp_location **loc2p;
876fa593 12097
e5dd4106 12098 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12099 not, we have to free it. */
c7d46a38 12100 int found_object = 0;
20874c92
VP
12101 /* Tells if the location should remain inserted in the target. */
12102 int keep_in_target = 0;
12103 int removed = 0;
876fa593 12104
4a64f543
MS
12105 /* Skip LOCP entries which will definitely never be needed.
12106 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12107 while (locp < bp_locations + bp_locations_count
c7d46a38 12108 && (*locp)->address < old_loc->address)
876fa593 12109 locp++;
c7d46a38
PA
12110
12111 for (loc2p = locp;
f5336ca5 12112 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12113 && (*loc2p)->address == old_loc->address);
12114 loc2p++)
12115 {
b775012e
LM
12116 /* Check if this is a new/duplicated location or a duplicated
12117 location that had its condition modified. If so, we want to send
12118 its condition to the target if evaluation of conditions is taking
12119 place there. */
12120 if ((*loc2p)->condition_changed == condition_modified
12121 && (last_addr != old_loc->address
12122 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12123 {
b775012e
LM
12124 force_breakpoint_reinsertion (*loc2p);
12125 last_pspace_num = old_loc->pspace->num;
c7d46a38 12126 }
b775012e
LM
12127
12128 if (*loc2p == old_loc)
12129 found_object = 1;
c7d46a38 12130 }
74960c60 12131
b775012e
LM
12132 /* We have already handled this address, update it so that we don't
12133 have to go through updates again. */
12134 last_addr = old_loc->address;
12135
12136 /* Target-side condition evaluation: Handle deleted locations. */
12137 if (!found_object)
12138 force_breakpoint_reinsertion (old_loc);
12139
4a64f543
MS
12140 /* If this location is no longer present, and inserted, look if
12141 there's maybe a new location at the same address. If so,
12142 mark that one inserted, and don't remove this one. This is
12143 needed so that we don't have a time window where a breakpoint
12144 at certain location is not inserted. */
74960c60 12145
876fa593 12146 if (old_loc->inserted)
0d381245 12147 {
4a64f543
MS
12148 /* If the location is inserted now, we might have to remove
12149 it. */
74960c60 12150
876fa593 12151 if (found_object && should_be_inserted (old_loc))
74960c60 12152 {
4a64f543
MS
12153 /* The location is still present in the location list,
12154 and still should be inserted. Don't do anything. */
20874c92 12155 keep_in_target = 1;
74960c60
VP
12156 }
12157 else
12158 {
b775012e
LM
12159 /* This location still exists, but it won't be kept in the
12160 target since it may have been disabled. We proceed to
12161 remove its target-side condition. */
12162
4a64f543
MS
12163 /* The location is either no longer present, or got
12164 disabled. See if there's another location at the
12165 same address, in which case we don't need to remove
12166 this one from the target. */
876fa593 12167
2bdf28a0 12168 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12169 if (breakpoint_address_is_meaningful (old_loc->owner))
12170 {
876fa593 12171 for (loc2p = locp;
f5336ca5 12172 (loc2p < bp_locations + bp_locations_count
c7d46a38 12173 && (*loc2p)->address == old_loc->address);
876fa593
JK
12174 loc2p++)
12175 {
12176 struct bp_location *loc2 = *loc2p;
12177
2d134ed3 12178 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12179 {
85d721b8
PA
12180 /* Read watchpoint locations are switched to
12181 access watchpoints, if the former are not
12182 supported, but the latter are. */
12183 if (is_hardware_watchpoint (old_loc->owner))
12184 {
12185 gdb_assert (is_hardware_watchpoint (loc2->owner));
12186 loc2->watchpoint_type = old_loc->watchpoint_type;
12187 }
12188
934709f0
PW
12189 /* loc2 is a duplicated location. We need to check
12190 if it should be inserted in case it will be
12191 unduplicated. */
12192 if (loc2 != old_loc
12193 && unduplicated_should_be_inserted (loc2))
c7d46a38 12194 {
934709f0 12195 swap_insertion (old_loc, loc2);
c7d46a38
PA
12196 keep_in_target = 1;
12197 break;
12198 }
876fa593
JK
12199 }
12200 }
12201 }
74960c60
VP
12202 }
12203
20874c92
VP
12204 if (!keep_in_target)
12205 {
834c0d03 12206 if (remove_breakpoint (old_loc))
20874c92 12207 {
4a64f543
MS
12208 /* This is just about all we can do. We could keep
12209 this location on the global list, and try to
12210 remove it next time, but there's no particular
12211 reason why we will succeed next time.
20874c92 12212
4a64f543
MS
12213 Note that at this point, old_loc->owner is still
12214 valid, as delete_breakpoint frees the breakpoint
12215 only after calling us. */
3e43a32a
MS
12216 printf_filtered (_("warning: Error removing "
12217 "breakpoint %d\n"),
876fa593 12218 old_loc->owner->number);
20874c92
VP
12219 }
12220 removed = 1;
12221 }
0d381245 12222 }
74960c60
VP
12223
12224 if (!found_object)
1c5cfe86 12225 {
fbea99ea 12226 if (removed && target_is_non_stop_p ()
1cf4d951 12227 && need_moribund_for_location_type (old_loc))
20874c92 12228 {
db82e815
PA
12229 /* This location was removed from the target. In
12230 non-stop mode, a race condition is possible where
12231 we've removed a breakpoint, but stop events for that
12232 breakpoint are already queued and will arrive later.
12233 We apply an heuristic to be able to distinguish such
12234 SIGTRAPs from other random SIGTRAPs: we keep this
12235 breakpoint location for a bit, and will retire it
12236 after we see some number of events. The theory here
12237 is that reporting of events should, "on the average",
12238 be fair, so after a while we'll see events from all
12239 threads that have anything of interest, and no longer
12240 need to keep this breakpoint location around. We
12241 don't hold locations forever so to reduce chances of
12242 mistaking a non-breakpoint SIGTRAP for a breakpoint
12243 SIGTRAP.
12244
12245 The heuristic failing can be disastrous on
12246 decr_pc_after_break targets.
12247
12248 On decr_pc_after_break targets, like e.g., x86-linux,
12249 if we fail to recognize a late breakpoint SIGTRAP,
12250 because events_till_retirement has reached 0 too
12251 soon, we'll fail to do the PC adjustment, and report
12252 a random SIGTRAP to the user. When the user resumes
12253 the inferior, it will most likely immediately crash
2dec564e 12254 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12255 corrupted, because of being resumed e.g., in the
12256 middle of a multi-byte instruction, or skipped a
12257 one-byte instruction. This was actually seen happen
12258 on native x86-linux, and should be less rare on
12259 targets that do not support new thread events, like
12260 remote, due to the heuristic depending on
12261 thread_count.
12262
12263 Mistaking a random SIGTRAP for a breakpoint trap
12264 causes similar symptoms (PC adjustment applied when
12265 it shouldn't), but then again, playing with SIGTRAPs
12266 behind the debugger's back is asking for trouble.
12267
12268 Since hardware watchpoint traps are always
12269 distinguishable from other traps, so we don't need to
12270 apply keep hardware watchpoint moribund locations
12271 around. We simply always ignore hardware watchpoint
12272 traps we can no longer explain. */
12273
876fa593
JK
12274 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12275 old_loc->owner = NULL;
20874c92 12276
876fa593 12277 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12278 }
12279 else
f431efe5
PA
12280 {
12281 old_loc->owner = NULL;
12282 decref_bp_location (&old_loc);
12283 }
20874c92 12284 }
74960c60 12285 }
1c5cfe86 12286
348d480f
PA
12287 /* Rescan breakpoints at the same address and section, marking the
12288 first one as "first" and any others as "duplicates". This is so
12289 that the bpt instruction is only inserted once. If we have a
12290 permanent breakpoint at the same place as BPT, make that one the
12291 official one, and the rest as duplicates. Permanent breakpoints
12292 are sorted first for the same address.
12293
12294 Do the same for hardware watchpoints, but also considering the
12295 watchpoint's type (regular/access/read) and length. */
12296
12297 bp_loc_first = NULL;
12298 wp_loc_first = NULL;
12299 awp_loc_first = NULL;
12300 rwp_loc_first = NULL;
12301 ALL_BP_LOCATIONS (loc, locp)
12302 {
12303 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12304 non-NULL. */
348d480f 12305 struct bp_location **loc_first_p;
d3fbdd86 12306 b = loc->owner;
348d480f 12307
6f380991 12308 if (!unduplicated_should_be_inserted (loc)
348d480f 12309 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12310 /* Don't detect duplicate for tracepoint locations because they are
12311 never duplicated. See the comments in field `duplicate' of
12312 `struct bp_location'. */
348d480f 12313 || is_tracepoint (b))
b775012e
LM
12314 {
12315 /* Clear the condition modification flag. */
12316 loc->condition_changed = condition_unchanged;
12317 continue;
12318 }
348d480f 12319
348d480f
PA
12320 if (b->type == bp_hardware_watchpoint)
12321 loc_first_p = &wp_loc_first;
12322 else if (b->type == bp_read_watchpoint)
12323 loc_first_p = &rwp_loc_first;
12324 else if (b->type == bp_access_watchpoint)
12325 loc_first_p = &awp_loc_first;
12326 else
12327 loc_first_p = &bp_loc_first;
12328
12329 if (*loc_first_p == NULL
12330 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12331 || !breakpoint_locations_match (loc, *loc_first_p))
12332 {
12333 *loc_first_p = loc;
12334 loc->duplicate = 0;
b775012e
LM
12335
12336 if (is_breakpoint (loc->owner) && loc->condition_changed)
12337 {
12338 loc->needs_update = 1;
12339 /* Clear the condition modification flag. */
12340 loc->condition_changed = condition_unchanged;
12341 }
348d480f
PA
12342 continue;
12343 }
12344
934709f0
PW
12345
12346 /* This and the above ensure the invariant that the first location
12347 is not duplicated, and is the inserted one.
12348 All following are marked as duplicated, and are not inserted. */
12349 if (loc->inserted)
12350 swap_insertion (loc, *loc_first_p);
348d480f
PA
12351 loc->duplicate = 1;
12352
b775012e
LM
12353 /* Clear the condition modification flag. */
12354 loc->condition_changed = condition_unchanged;
348d480f
PA
12355 }
12356
a25a5a45 12357 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12358 {
04086b45 12359 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12360 insert_breakpoint_locations ();
12361 else
12362 {
44702360
PA
12363 /* Even though the caller told us to not insert new
12364 locations, we may still need to update conditions on the
12365 target's side of breakpoints that were already inserted
12366 if the target is evaluating breakpoint conditions. We
b775012e
LM
12367 only update conditions for locations that are marked
12368 "needs_update". */
12369 update_inserted_breakpoint_locations ();
12370 }
12371 }
348d480f 12372
04086b45 12373 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12374 download_tracepoint_locations ();
12375
348d480f
PA
12376 do_cleanups (cleanups);
12377}
12378
12379void
12380breakpoint_retire_moribund (void)
12381{
12382 struct bp_location *loc;
12383 int ix;
12384
12385 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12386 if (--(loc->events_till_retirement) == 0)
12387 {
12388 decref_bp_location (&loc);
12389 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12390 --ix;
12391 }
12392}
12393
12394static void
44702360 12395update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12396{
348d480f 12397
492d29ea
PA
12398 TRY
12399 {
12400 update_global_location_list (insert_mode);
12401 }
12402 CATCH (e, RETURN_MASK_ERROR)
12403 {
12404 }
12405 END_CATCH
348d480f
PA
12406}
12407
12408/* Clear BKP from a BPS. */
12409
12410static void
12411bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12412{
12413 bpstat bs;
12414
12415 for (bs = bps; bs; bs = bs->next)
12416 if (bs->breakpoint_at == bpt)
12417 {
12418 bs->breakpoint_at = NULL;
12419 bs->old_val = NULL;
12420 /* bs->commands will be freed later. */
12421 }
12422}
12423
12424/* Callback for iterate_over_threads. */
12425static int
12426bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12427{
9a3c8263 12428 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12429
12430 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12431 return 0;
12432}
12433
12434/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12435 callbacks. */
12436
12437static void
12438say_where (struct breakpoint *b)
12439{
12440 struct value_print_options opts;
12441
12442 get_user_print_options (&opts);
12443
12444 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12445 single string. */
12446 if (b->loc == NULL)
12447 {
f00aae0f
KS
12448 /* For pending locations, the output differs slightly based
12449 on b->extra_string. If this is non-NULL, it contains either
12450 a condition or dprintf arguments. */
12451 if (b->extra_string == NULL)
12452 {
12453 printf_filtered (_(" (%s) pending."),
d28cd78a 12454 event_location_to_string (b->location.get ()));
f00aae0f
KS
12455 }
12456 else if (b->type == bp_dprintf)
12457 {
12458 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12459 event_location_to_string (b->location.get ()),
f00aae0f
KS
12460 b->extra_string);
12461 }
12462 else
12463 {
12464 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12465 event_location_to_string (b->location.get ()),
f00aae0f
KS
12466 b->extra_string);
12467 }
348d480f
PA
12468 }
12469 else
12470 {
2f202fde 12471 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12472 {
12473 printf_filtered (" at ");
12474 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12475 gdb_stdout);
12476 }
2f202fde 12477 if (b->loc->symtab != NULL)
f8eba3c6
TT
12478 {
12479 /* If there is a single location, we can print the location
12480 more nicely. */
12481 if (b->loc->next == NULL)
12482 printf_filtered (": file %s, line %d.",
05cba821
JK
12483 symtab_to_filename_for_display (b->loc->symtab),
12484 b->loc->line_number);
f8eba3c6
TT
12485 else
12486 /* This is not ideal, but each location may have a
12487 different file name, and this at least reflects the
12488 real situation somewhat. */
f00aae0f 12489 printf_filtered (": %s.",
d28cd78a 12490 event_location_to_string (b->location.get ()));
f8eba3c6 12491 }
348d480f
PA
12492
12493 if (b->loc->next)
12494 {
12495 struct bp_location *loc = b->loc;
12496 int n = 0;
12497 for (; loc; loc = loc->next)
12498 ++n;
12499 printf_filtered (" (%d locations)", n);
12500 }
12501 }
12502}
12503
348d480f
PA
12504/* Default bp_location_ops methods. */
12505
12506static void
12507bp_location_dtor (struct bp_location *self)
12508{
348d480f
PA
12509 xfree (self->function_name);
12510}
12511
12512static const struct bp_location_ops bp_location_ops =
12513{
12514 bp_location_dtor
12515};
12516
c1fc2657 12517/* Destructor for the breakpoint base class. */
348d480f 12518
c1fc2657 12519breakpoint::~breakpoint ()
348d480f 12520{
c1fc2657
SM
12521 xfree (this->cond_string);
12522 xfree (this->extra_string);
12523 xfree (this->filter);
348d480f
PA
12524}
12525
2060206e
PA
12526static struct bp_location *
12527base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12528{
5625a286 12529 return new bp_location (&bp_location_ops, self);
348d480f
PA
12530}
12531
2060206e
PA
12532static void
12533base_breakpoint_re_set (struct breakpoint *b)
12534{
12535 /* Nothing to re-set. */
12536}
12537
12538#define internal_error_pure_virtual_called() \
12539 gdb_assert_not_reached ("pure virtual function called")
12540
12541static int
12542base_breakpoint_insert_location (struct bp_location *bl)
12543{
12544 internal_error_pure_virtual_called ();
12545}
12546
12547static int
73971819
PA
12548base_breakpoint_remove_location (struct bp_location *bl,
12549 enum remove_bp_reason reason)
2060206e
PA
12550{
12551 internal_error_pure_virtual_called ();
12552}
12553
12554static int
12555base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12556 struct address_space *aspace,
09ac7c10
TT
12557 CORE_ADDR bp_addr,
12558 const struct target_waitstatus *ws)
2060206e
PA
12559{
12560 internal_error_pure_virtual_called ();
12561}
12562
12563static void
12564base_breakpoint_check_status (bpstat bs)
12565{
12566 /* Always stop. */
12567}
12568
12569/* A "works_in_software_mode" breakpoint_ops method that just internal
12570 errors. */
12571
12572static int
12573base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12574{
12575 internal_error_pure_virtual_called ();
12576}
12577
12578/* A "resources_needed" breakpoint_ops method that just internal
12579 errors. */
12580
12581static int
12582base_breakpoint_resources_needed (const struct bp_location *bl)
12583{
12584 internal_error_pure_virtual_called ();
12585}
12586
12587static enum print_stop_action
12588base_breakpoint_print_it (bpstat bs)
12589{
12590 internal_error_pure_virtual_called ();
12591}
12592
12593static void
12594base_breakpoint_print_one_detail (const struct breakpoint *self,
12595 struct ui_out *uiout)
12596{
12597 /* nothing */
12598}
12599
12600static void
12601base_breakpoint_print_mention (struct breakpoint *b)
12602{
12603 internal_error_pure_virtual_called ();
12604}
12605
12606static void
12607base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12608{
12609 internal_error_pure_virtual_called ();
12610}
12611
983af33b 12612static void
f00aae0f
KS
12613base_breakpoint_create_sals_from_location
12614 (const struct event_location *location,
12615 struct linespec_result *canonical,
12616 enum bptype type_wanted)
983af33b
SDJ
12617{
12618 internal_error_pure_virtual_called ();
12619}
12620
12621static void
12622base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12623 struct linespec_result *c,
e1e01040
PA
12624 gdb::unique_xmalloc_ptr<char> cond_string,
12625 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12626 enum bptype type_wanted,
12627 enum bpdisp disposition,
12628 int thread,
12629 int task, int ignore_count,
12630 const struct breakpoint_ops *o,
12631 int from_tty, int enabled,
44f238bb 12632 int internal, unsigned flags)
983af33b
SDJ
12633{
12634 internal_error_pure_virtual_called ();
12635}
12636
6c5b2ebe 12637static std::vector<symtab_and_line>
f00aae0f
KS
12638base_breakpoint_decode_location (struct breakpoint *b,
12639 const struct event_location *location,
6c5b2ebe 12640 struct program_space *search_pspace)
983af33b
SDJ
12641{
12642 internal_error_pure_virtual_called ();
12643}
12644
ab04a2af
TT
12645/* The default 'explains_signal' method. */
12646
47591c29 12647static int
427cd150 12648base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12649{
47591c29 12650 return 1;
ab04a2af
TT
12651}
12652
9d6e6e84
HZ
12653/* The default "after_condition_true" method. */
12654
12655static void
12656base_breakpoint_after_condition_true (struct bpstats *bs)
12657{
12658 /* Nothing to do. */
12659}
12660
ab04a2af 12661struct breakpoint_ops base_breakpoint_ops =
2060206e 12662{
2060206e
PA
12663 base_breakpoint_allocate_location,
12664 base_breakpoint_re_set,
12665 base_breakpoint_insert_location,
12666 base_breakpoint_remove_location,
12667 base_breakpoint_breakpoint_hit,
12668 base_breakpoint_check_status,
12669 base_breakpoint_resources_needed,
12670 base_breakpoint_works_in_software_mode,
12671 base_breakpoint_print_it,
12672 NULL,
12673 base_breakpoint_print_one_detail,
12674 base_breakpoint_print_mention,
983af33b 12675 base_breakpoint_print_recreate,
5f700d83 12676 base_breakpoint_create_sals_from_location,
983af33b 12677 base_breakpoint_create_breakpoints_sal,
5f700d83 12678 base_breakpoint_decode_location,
9d6e6e84
HZ
12679 base_breakpoint_explains_signal,
12680 base_breakpoint_after_condition_true,
2060206e
PA
12681};
12682
12683/* Default breakpoint_ops methods. */
12684
12685static void
348d480f
PA
12686bkpt_re_set (struct breakpoint *b)
12687{
06edf0c0 12688 /* FIXME: is this still reachable? */
9ef9e6a6 12689 if (breakpoint_event_location_empty_p (b))
06edf0c0 12690 {
f00aae0f 12691 /* Anything without a location can't be re-set. */
348d480f 12692 delete_breakpoint (b);
06edf0c0 12693 return;
348d480f 12694 }
06edf0c0
PA
12695
12696 breakpoint_re_set_default (b);
348d480f
PA
12697}
12698
2060206e 12699static int
348d480f
PA
12700bkpt_insert_location (struct bp_location *bl)
12701{
cd6c3b4f
YQ
12702 CORE_ADDR addr = bl->target_info.reqstd_address;
12703
579c6ad9 12704 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12705 bl->target_info.placed_address = addr;
12706
348d480f 12707 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12708 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12709 else
7c16b83e 12710 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12711}
12712
2060206e 12713static int
73971819 12714bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12715{
12716 if (bl->loc_type == bp_loc_hardware_breakpoint)
12717 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12718 else
73971819 12719 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12720}
12721
2060206e 12722static int
348d480f 12723bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12724 struct address_space *aspace, CORE_ADDR bp_addr,
12725 const struct target_waitstatus *ws)
348d480f 12726{
09ac7c10 12727 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12728 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12729 return 0;
12730
348d480f
PA
12731 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12732 aspace, bp_addr))
12733 return 0;
12734
12735 if (overlay_debugging /* unmapped overlay section */
12736 && section_is_overlay (bl->section)
12737 && !section_is_mapped (bl->section))
12738 return 0;
12739
12740 return 1;
12741}
12742
cd1608cc
PA
12743static int
12744dprintf_breakpoint_hit (const struct bp_location *bl,
12745 struct address_space *aspace, CORE_ADDR bp_addr,
12746 const struct target_waitstatus *ws)
12747{
12748 if (dprintf_style == dprintf_style_agent
12749 && target_can_run_breakpoint_commands ())
12750 {
12751 /* An agent-style dprintf never causes a stop. If we see a trap
12752 for this address it must be for a breakpoint that happens to
12753 be set at the same address. */
12754 return 0;
12755 }
12756
12757 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12758}
12759
2060206e 12760static int
348d480f
PA
12761bkpt_resources_needed (const struct bp_location *bl)
12762{
12763 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12764
12765 return 1;
12766}
12767
2060206e 12768static enum print_stop_action
348d480f
PA
12769bkpt_print_it (bpstat bs)
12770{
348d480f
PA
12771 struct breakpoint *b;
12772 const struct bp_location *bl;
001c8c33 12773 int bp_temp;
79a45e25 12774 struct ui_out *uiout = current_uiout;
348d480f
PA
12775
12776 gdb_assert (bs->bp_location_at != NULL);
12777
12778 bl = bs->bp_location_at;
12779 b = bs->breakpoint_at;
12780
001c8c33
PA
12781 bp_temp = b->disposition == disp_del;
12782 if (bl->address != bl->requested_address)
12783 breakpoint_adjustment_warning (bl->requested_address,
12784 bl->address,
12785 b->number, 1);
12786 annotate_breakpoint (b->number);
f303dbd6
PA
12787 maybe_print_thread_hit_breakpoint (uiout);
12788
001c8c33 12789 if (bp_temp)
112e8700 12790 uiout->text ("Temporary breakpoint ");
001c8c33 12791 else
112e8700
SM
12792 uiout->text ("Breakpoint ");
12793 if (uiout->is_mi_like_p ())
348d480f 12794 {
112e8700 12795 uiout->field_string ("reason",
001c8c33 12796 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12797 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12798 }
112e8700
SM
12799 uiout->field_int ("bkptno", b->number);
12800 uiout->text (", ");
06edf0c0 12801
001c8c33 12802 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12803}
12804
2060206e 12805static void
06edf0c0
PA
12806bkpt_print_mention (struct breakpoint *b)
12807{
112e8700 12808 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12809 return;
12810
12811 switch (b->type)
12812 {
12813 case bp_breakpoint:
12814 case bp_gnu_ifunc_resolver:
12815 if (b->disposition == disp_del)
12816 printf_filtered (_("Temporary breakpoint"));
12817 else
12818 printf_filtered (_("Breakpoint"));
12819 printf_filtered (_(" %d"), b->number);
12820 if (b->type == bp_gnu_ifunc_resolver)
12821 printf_filtered (_(" at gnu-indirect-function resolver"));
12822 break;
12823 case bp_hardware_breakpoint:
12824 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12825 break;
e7e0cddf
SS
12826 case bp_dprintf:
12827 printf_filtered (_("Dprintf %d"), b->number);
12828 break;
06edf0c0
PA
12829 }
12830
12831 say_where (b);
12832}
12833
2060206e 12834static void
06edf0c0
PA
12835bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12836{
12837 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12838 fprintf_unfiltered (fp, "tbreak");
12839 else if (tp->type == bp_breakpoint)
12840 fprintf_unfiltered (fp, "break");
12841 else if (tp->type == bp_hardware_breakpoint
12842 && tp->disposition == disp_del)
12843 fprintf_unfiltered (fp, "thbreak");
12844 else if (tp->type == bp_hardware_breakpoint)
12845 fprintf_unfiltered (fp, "hbreak");
12846 else
12847 internal_error (__FILE__, __LINE__,
12848 _("unhandled breakpoint type %d"), (int) tp->type);
12849
f00aae0f 12850 fprintf_unfiltered (fp, " %s",
d28cd78a 12851 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12852
12853 /* Print out extra_string if this breakpoint is pending. It might
12854 contain, for example, conditions that were set by the user. */
12855 if (tp->loc == NULL && tp->extra_string != NULL)
12856 fprintf_unfiltered (fp, " %s", tp->extra_string);
12857
dd11a36c 12858 print_recreate_thread (tp, fp);
06edf0c0
PA
12859}
12860
983af33b 12861static void
f00aae0f
KS
12862bkpt_create_sals_from_location (const struct event_location *location,
12863 struct linespec_result *canonical,
12864 enum bptype type_wanted)
983af33b 12865{
f00aae0f 12866 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12867}
12868
12869static void
12870bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12871 struct linespec_result *canonical,
e1e01040
PA
12872 gdb::unique_xmalloc_ptr<char> cond_string,
12873 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12874 enum bptype type_wanted,
12875 enum bpdisp disposition,
12876 int thread,
12877 int task, int ignore_count,
12878 const struct breakpoint_ops *ops,
12879 int from_tty, int enabled,
44f238bb 12880 int internal, unsigned flags)
983af33b 12881{
023fa29b 12882 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12883 std::move (cond_string),
12884 std::move (extra_string),
e7e0cddf 12885 type_wanted,
983af33b
SDJ
12886 disposition, thread, task,
12887 ignore_count, ops, from_tty,
44f238bb 12888 enabled, internal, flags);
983af33b
SDJ
12889}
12890
6c5b2ebe 12891static std::vector<symtab_and_line>
f00aae0f
KS
12892bkpt_decode_location (struct breakpoint *b,
12893 const struct event_location *location,
6c5b2ebe 12894 struct program_space *search_pspace)
983af33b 12895{
6c5b2ebe 12896 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12897}
12898
06edf0c0
PA
12899/* Virtual table for internal breakpoints. */
12900
12901static void
12902internal_bkpt_re_set (struct breakpoint *b)
12903{
12904 switch (b->type)
12905 {
12906 /* Delete overlay event and longjmp master breakpoints; they
12907 will be reset later by breakpoint_re_set. */
12908 case bp_overlay_event:
12909 case bp_longjmp_master:
12910 case bp_std_terminate_master:
12911 case bp_exception_master:
12912 delete_breakpoint (b);
12913 break;
12914
12915 /* This breakpoint is special, it's set up when the inferior
12916 starts and we really don't want to touch it. */
12917 case bp_shlib_event:
12918
12919 /* Like bp_shlib_event, this breakpoint type is special. Once
12920 it is set up, we do not want to touch it. */
12921 case bp_thread_event:
12922 break;
12923 }
12924}
12925
12926static void
12927internal_bkpt_check_status (bpstat bs)
12928{
a9b3a50f
PA
12929 if (bs->breakpoint_at->type == bp_shlib_event)
12930 {
12931 /* If requested, stop when the dynamic linker notifies GDB of
12932 events. This allows the user to get control and place
12933 breakpoints in initializer routines for dynamically loaded
12934 objects (among other things). */
12935 bs->stop = stop_on_solib_events;
12936 bs->print = stop_on_solib_events;
12937 }
12938 else
12939 bs->stop = 0;
06edf0c0
PA
12940}
12941
12942static enum print_stop_action
12943internal_bkpt_print_it (bpstat bs)
12944{
06edf0c0 12945 struct breakpoint *b;
06edf0c0 12946
06edf0c0
PA
12947 b = bs->breakpoint_at;
12948
06edf0c0
PA
12949 switch (b->type)
12950 {
348d480f
PA
12951 case bp_shlib_event:
12952 /* Did we stop because the user set the stop_on_solib_events
12953 variable? (If so, we report this as a generic, "Stopped due
12954 to shlib event" message.) */
edcc5120 12955 print_solib_event (0);
348d480f
PA
12956 break;
12957
12958 case bp_thread_event:
12959 /* Not sure how we will get here.
12960 GDB should not stop for these breakpoints. */
12961 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12962 break;
12963
12964 case bp_overlay_event:
12965 /* By analogy with the thread event, GDB should not stop for these. */
12966 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12967 break;
12968
12969 case bp_longjmp_master:
12970 /* These should never be enabled. */
12971 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12972 break;
12973
12974 case bp_std_terminate_master:
12975 /* These should never be enabled. */
12976 printf_filtered (_("std::terminate Master Breakpoint: "
12977 "gdb should not stop!\n"));
348d480f
PA
12978 break;
12979
12980 case bp_exception_master:
12981 /* These should never be enabled. */
12982 printf_filtered (_("Exception Master Breakpoint: "
12983 "gdb should not stop!\n"));
06edf0c0
PA
12984 break;
12985 }
12986
001c8c33 12987 return PRINT_NOTHING;
06edf0c0
PA
12988}
12989
12990static void
12991internal_bkpt_print_mention (struct breakpoint *b)
12992{
12993 /* Nothing to mention. These breakpoints are internal. */
12994}
12995
06edf0c0
PA
12996/* Virtual table for momentary breakpoints */
12997
12998static void
12999momentary_bkpt_re_set (struct breakpoint *b)
13000{
13001 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13002 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13003 Otherwise these should have been blown away via the cleanup chain
13004 or by breakpoint_init_inferior when we rerun the executable. */
13005}
13006
13007static void
13008momentary_bkpt_check_status (bpstat bs)
13009{
13010 /* Nothing. The point of these breakpoints is causing a stop. */
13011}
13012
13013static enum print_stop_action
13014momentary_bkpt_print_it (bpstat bs)
13015{
001c8c33 13016 return PRINT_UNKNOWN;
348d480f
PA
13017}
13018
06edf0c0
PA
13019static void
13020momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13021{
06edf0c0 13022 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13023}
13024
e2e4d78b
JK
13025/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13026
13027 It gets cleared already on the removal of the first one of such placed
13028 breakpoints. This is OK as they get all removed altogether. */
13029
c1fc2657 13030longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 13031{
c1fc2657 13032 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 13033
c1fc2657 13034 if (tp != NULL)
e2e4d78b 13035 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
13036}
13037
55aa24fb
SDJ
13038/* Specific methods for probe breakpoints. */
13039
13040static int
13041bkpt_probe_insert_location (struct bp_location *bl)
13042{
13043 int v = bkpt_insert_location (bl);
13044
13045 if (v == 0)
13046 {
13047 /* The insertion was successful, now let's set the probe's semaphore
13048 if needed. */
0ea5cda8
SDJ
13049 if (bl->probe.probe->pops->set_semaphore != NULL)
13050 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13051 bl->probe.objfile,
13052 bl->gdbarch);
55aa24fb
SDJ
13053 }
13054
13055 return v;
13056}
13057
13058static int
73971819
PA
13059bkpt_probe_remove_location (struct bp_location *bl,
13060 enum remove_bp_reason reason)
55aa24fb
SDJ
13061{
13062 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13063 if (bl->probe.probe->pops->clear_semaphore != NULL)
13064 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13065 bl->probe.objfile,
13066 bl->gdbarch);
55aa24fb 13067
73971819 13068 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13069}
13070
13071static void
f00aae0f 13072bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13073 struct linespec_result *canonical,
f00aae0f 13074 enum bptype type_wanted)
55aa24fb
SDJ
13075{
13076 struct linespec_sals lsal;
13077
c2f4122d 13078 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13079 lsal.canonical
13080 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13081 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
13082}
13083
6c5b2ebe 13084static std::vector<symtab_and_line>
f00aae0f
KS
13085bkpt_probe_decode_location (struct breakpoint *b,
13086 const struct event_location *location,
6c5b2ebe 13087 struct program_space *search_pspace)
55aa24fb 13088{
6c5b2ebe
PA
13089 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13090 if (sals.empty ())
55aa24fb 13091 error (_("probe not found"));
6c5b2ebe 13092 return sals;
55aa24fb
SDJ
13093}
13094
348d480f 13095/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13096
348d480f
PA
13097static void
13098tracepoint_re_set (struct breakpoint *b)
13099{
13100 breakpoint_re_set_default (b);
13101}
876fa593 13102
348d480f
PA
13103static int
13104tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13105 struct address_space *aspace, CORE_ADDR bp_addr,
13106 const struct target_waitstatus *ws)
348d480f
PA
13107{
13108 /* By definition, the inferior does not report stops at
13109 tracepoints. */
13110 return 0;
74960c60
VP
13111}
13112
13113static void
348d480f
PA
13114tracepoint_print_one_detail (const struct breakpoint *self,
13115 struct ui_out *uiout)
74960c60 13116{
d9b3f62e
PA
13117 struct tracepoint *tp = (struct tracepoint *) self;
13118 if (tp->static_trace_marker_id)
348d480f
PA
13119 {
13120 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13121
112e8700
SM
13122 uiout->text ("\tmarker id is ");
13123 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13124 tp->static_trace_marker_id);
112e8700 13125 uiout->text ("\n");
348d480f 13126 }
0d381245
VP
13127}
13128
a474d7c2 13129static void
348d480f 13130tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13131{
112e8700 13132 if (current_uiout->is_mi_like_p ())
348d480f 13133 return;
cc59ec59 13134
348d480f
PA
13135 switch (b->type)
13136 {
13137 case bp_tracepoint:
13138 printf_filtered (_("Tracepoint"));
13139 printf_filtered (_(" %d"), b->number);
13140 break;
13141 case bp_fast_tracepoint:
13142 printf_filtered (_("Fast tracepoint"));
13143 printf_filtered (_(" %d"), b->number);
13144 break;
13145 case bp_static_tracepoint:
13146 printf_filtered (_("Static tracepoint"));
13147 printf_filtered (_(" %d"), b->number);
13148 break;
13149 default:
13150 internal_error (__FILE__, __LINE__,
13151 _("unhandled tracepoint type %d"), (int) b->type);
13152 }
13153
13154 say_where (b);
a474d7c2
PA
13155}
13156
348d480f 13157static void
d9b3f62e 13158tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13159{
d9b3f62e
PA
13160 struct tracepoint *tp = (struct tracepoint *) self;
13161
13162 if (self->type == bp_fast_tracepoint)
348d480f 13163 fprintf_unfiltered (fp, "ftrace");
c93e8391 13164 else if (self->type == bp_static_tracepoint)
348d480f 13165 fprintf_unfiltered (fp, "strace");
d9b3f62e 13166 else if (self->type == bp_tracepoint)
348d480f
PA
13167 fprintf_unfiltered (fp, "trace");
13168 else
13169 internal_error (__FILE__, __LINE__,
d9b3f62e 13170 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13171
f00aae0f 13172 fprintf_unfiltered (fp, " %s",
d28cd78a 13173 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13174 print_recreate_thread (self, fp);
13175
13176 if (tp->pass_count)
13177 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13178}
13179
983af33b 13180static void
f00aae0f
KS
13181tracepoint_create_sals_from_location (const struct event_location *location,
13182 struct linespec_result *canonical,
13183 enum bptype type_wanted)
983af33b 13184{
f00aae0f 13185 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13186}
13187
13188static void
13189tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13190 struct linespec_result *canonical,
e1e01040
PA
13191 gdb::unique_xmalloc_ptr<char> cond_string,
13192 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13193 enum bptype type_wanted,
13194 enum bpdisp disposition,
13195 int thread,
13196 int task, int ignore_count,
13197 const struct breakpoint_ops *ops,
13198 int from_tty, int enabled,
44f238bb 13199 int internal, unsigned flags)
983af33b 13200{
023fa29b 13201 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13202 std::move (cond_string),
13203 std::move (extra_string),
e7e0cddf 13204 type_wanted,
983af33b
SDJ
13205 disposition, thread, task,
13206 ignore_count, ops, from_tty,
44f238bb 13207 enabled, internal, flags);
983af33b
SDJ
13208}
13209
6c5b2ebe 13210static std::vector<symtab_and_line>
f00aae0f
KS
13211tracepoint_decode_location (struct breakpoint *b,
13212 const struct event_location *location,
6c5b2ebe 13213 struct program_space *search_pspace)
983af33b 13214{
6c5b2ebe 13215 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13216}
13217
2060206e 13218struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13219
55aa24fb
SDJ
13220/* The breakpoint_ops structure to be use on tracepoints placed in a
13221 static probe. */
13222
13223static void
f00aae0f
KS
13224tracepoint_probe_create_sals_from_location
13225 (const struct event_location *location,
13226 struct linespec_result *canonical,
13227 enum bptype type_wanted)
55aa24fb
SDJ
13228{
13229 /* We use the same method for breakpoint on probes. */
f00aae0f 13230 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13231}
13232
6c5b2ebe 13233static std::vector<symtab_and_line>
f00aae0f
KS
13234tracepoint_probe_decode_location (struct breakpoint *b,
13235 const struct event_location *location,
6c5b2ebe 13236 struct program_space *search_pspace)
55aa24fb
SDJ
13237{
13238 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13239 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13240}
13241
13242static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13243
5c2b4418
HZ
13244/* Dprintf breakpoint_ops methods. */
13245
13246static void
13247dprintf_re_set (struct breakpoint *b)
13248{
13249 breakpoint_re_set_default (b);
13250
f00aae0f
KS
13251 /* extra_string should never be non-NULL for dprintf. */
13252 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13253
13254 /* 1 - connect to target 1, that can run breakpoint commands.
13255 2 - create a dprintf, which resolves fine.
13256 3 - disconnect from target 1
13257 4 - connect to target 2, that can NOT run breakpoint commands.
13258
13259 After steps #3/#4, you'll want the dprintf command list to
13260 be updated, because target 1 and 2 may well return different
13261 answers for target_can_run_breakpoint_commands().
13262 Given absence of finer grained resetting, we get to do
13263 it all the time. */
13264 if (b->extra_string != NULL)
13265 update_dprintf_command_list (b);
13266}
13267
2d9442cc
HZ
13268/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13269
13270static void
13271dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13272{
f00aae0f 13273 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13274 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13275 tp->extra_string);
13276 print_recreate_thread (tp, fp);
13277}
13278
9d6e6e84
HZ
13279/* Implement the "after_condition_true" breakpoint_ops method for
13280 dprintf.
13281
13282 dprintf's are implemented with regular commands in their command
13283 list, but we run the commands here instead of before presenting the
13284 stop to the user, as dprintf's don't actually cause a stop. This
13285 also makes it so that the commands of multiple dprintfs at the same
13286 address are all handled. */
13287
13288static void
13289dprintf_after_condition_true (struct bpstats *bs)
13290{
04afa70c 13291 struct bpstats tmp_bs;
9d6e6e84
HZ
13292 struct bpstats *tmp_bs_p = &tmp_bs;
13293
13294 /* dprintf's never cause a stop. This wasn't set in the
13295 check_status hook instead because that would make the dprintf's
13296 condition not be evaluated. */
13297 bs->stop = 0;
13298
13299 /* Run the command list here. Take ownership of it instead of
13300 copying. We never want these commands to run later in
13301 bpstat_do_actions, if a breakpoint that causes a stop happens to
13302 be set at same address as this dprintf, or even if running the
13303 commands here throws. */
13304 tmp_bs.commands = bs->commands;
13305 bs->commands = NULL;
9d6e6e84
HZ
13306
13307 bpstat_do_actions_1 (&tmp_bs_p);
13308
13309 /* 'tmp_bs.commands' will usually be NULL by now, but
13310 bpstat_do_actions_1 may return early without processing the whole
13311 list. */
9d6e6e84
HZ
13312}
13313
983af33b
SDJ
13314/* The breakpoint_ops structure to be used on static tracepoints with
13315 markers (`-m'). */
13316
13317static void
f00aae0f 13318strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13319 struct linespec_result *canonical,
f00aae0f 13320 enum bptype type_wanted)
983af33b
SDJ
13321{
13322 struct linespec_sals lsal;
f00aae0f 13323 const char *arg_start, *arg;
983af33b 13324
f00aae0f
KS
13325 arg = arg_start = get_linespec_location (location);
13326 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13327
f2fc3015
TT
13328 std::string str (arg_start, arg - arg_start);
13329 const char *ptr = str.c_str ();
13330 canonical->location = new_linespec_location (&ptr);
983af33b 13331
8e9e35b1
TT
13332 lsal.canonical
13333 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13334 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13335}
13336
13337static void
13338strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13339 struct linespec_result *canonical,
e1e01040
PA
13340 gdb::unique_xmalloc_ptr<char> cond_string,
13341 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13342 enum bptype type_wanted,
13343 enum bpdisp disposition,
13344 int thread,
13345 int task, int ignore_count,
13346 const struct breakpoint_ops *ops,
13347 int from_tty, int enabled,
44f238bb 13348 int internal, unsigned flags)
983af33b 13349{
6c5b2ebe 13350 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13351
13352 /* If the user is creating a static tracepoint by marker id
13353 (strace -m MARKER_ID), then store the sals index, so that
13354 breakpoint_re_set can try to match up which of the newly
13355 found markers corresponds to this one, and, don't try to
13356 expand multiple locations for each sal, given than SALS
13357 already should contain all sals for MARKER_ID. */
13358
6c5b2ebe 13359 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13360 {
6c5b2ebe
PA
13361 event_location_up location
13362 = copy_event_location (canonical->location.get ());
983af33b 13363
b270e6f9 13364 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13365 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13366 std::move (location), NULL,
e1e01040
PA
13367 std::move (cond_string),
13368 std::move (extra_string),
e7e0cddf 13369 type_wanted, disposition,
983af33b 13370 thread, task, ignore_count, ops,
44f238bb 13371 from_tty, enabled, internal, flags,
983af33b
SDJ
13372 canonical->special_display);
13373 /* Given that its possible to have multiple markers with
13374 the same string id, if the user is creating a static
13375 tracepoint by marker id ("strace -m MARKER_ID"), then
13376 store the sals index, so that breakpoint_re_set can
13377 try to match up which of the newly found markers
13378 corresponds to this one */
13379 tp->static_trace_marker_id_idx = i;
13380
b270e6f9 13381 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13382 }
13383}
13384
6c5b2ebe 13385static std::vector<symtab_and_line>
f00aae0f
KS
13386strace_marker_decode_location (struct breakpoint *b,
13387 const struct event_location *location,
6c5b2ebe 13388 struct program_space *search_pspace)
983af33b
SDJ
13389{
13390 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13391 const char *s = get_linespec_location (location);
983af33b 13392
6c5b2ebe
PA
13393 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13394 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13395 {
6c5b2ebe
PA
13396 sals[0] = sals[tp->static_trace_marker_id_idx];
13397 sals.resize (1);
13398 return sals;
983af33b
SDJ
13399 }
13400 else
13401 error (_("marker %s not found"), tp->static_trace_marker_id);
13402}
13403
13404static struct breakpoint_ops strace_marker_breakpoint_ops;
13405
13406static int
13407strace_marker_p (struct breakpoint *b)
13408{
13409 return b->ops == &strace_marker_breakpoint_ops;
13410}
13411
53a5351d 13412/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13413 structures. */
c906108c
SS
13414
13415void
fba45db2 13416delete_breakpoint (struct breakpoint *bpt)
c906108c 13417{
52f0bd74 13418 struct breakpoint *b;
c906108c 13419
8a3fe4f8 13420 gdb_assert (bpt != NULL);
c906108c 13421
4a64f543
MS
13422 /* Has this bp already been deleted? This can happen because
13423 multiple lists can hold pointers to bp's. bpstat lists are
13424 especial culprits.
13425
13426 One example of this happening is a watchpoint's scope bp. When
13427 the scope bp triggers, we notice that the watchpoint is out of
13428 scope, and delete it. We also delete its scope bp. But the
13429 scope bp is marked "auto-deleting", and is already on a bpstat.
13430 That bpstat is then checked for auto-deleting bp's, which are
13431 deleted.
13432
13433 A real solution to this problem might involve reference counts in
13434 bp's, and/or giving them pointers back to their referencing
13435 bpstat's, and teaching delete_breakpoint to only free a bp's
13436 storage when no more references were extent. A cheaper bandaid
13437 was chosen. */
c906108c
SS
13438 if (bpt->type == bp_none)
13439 return;
13440
4a64f543
MS
13441 /* At least avoid this stale reference until the reference counting
13442 of breakpoints gets resolved. */
d0fb5eae 13443 if (bpt->related_breakpoint != bpt)
e5a0a904 13444 {
d0fb5eae 13445 struct breakpoint *related;
3a5c3e22 13446 struct watchpoint *w;
d0fb5eae
JK
13447
13448 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13449 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13450 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13451 w = (struct watchpoint *) bpt;
13452 else
13453 w = NULL;
13454 if (w != NULL)
13455 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13456
13457 /* Unlink bpt from the bpt->related_breakpoint ring. */
13458 for (related = bpt; related->related_breakpoint != bpt;
13459 related = related->related_breakpoint);
13460 related->related_breakpoint = bpt->related_breakpoint;
13461 bpt->related_breakpoint = bpt;
e5a0a904
JK
13462 }
13463
a9634178
TJB
13464 /* watch_command_1 creates a watchpoint but only sets its number if
13465 update_watchpoint succeeds in creating its bp_locations. If there's
13466 a problem in that process, we'll be asked to delete the half-created
13467 watchpoint. In that case, don't announce the deletion. */
13468 if (bpt->number)
13469 observer_notify_breakpoint_deleted (bpt);
c906108c 13470
c906108c
SS
13471 if (breakpoint_chain == bpt)
13472 breakpoint_chain = bpt->next;
13473
c906108c
SS
13474 ALL_BREAKPOINTS (b)
13475 if (b->next == bpt)
c5aa993b
JM
13476 {
13477 b->next = bpt->next;
13478 break;
13479 }
c906108c 13480
f431efe5
PA
13481 /* Be sure no bpstat's are pointing at the breakpoint after it's
13482 been freed. */
13483 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13484 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13485 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13486 commands are associated with the bpstat; if we remove it here,
13487 then the later call to bpstat_do_actions (&stop_bpstat); in
13488 event-top.c won't do anything, and temporary breakpoints with
13489 commands won't work. */
13490
13491 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13492
4a64f543
MS
13493 /* Now that breakpoint is removed from breakpoint list, update the
13494 global location list. This will remove locations that used to
13495 belong to this breakpoint. Do this before freeing the breakpoint
13496 itself, since remove_breakpoint looks at location's owner. It
13497 might be better design to have location completely
13498 self-contained, but it's not the case now. */
44702360 13499 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13500
4a64f543
MS
13501 /* On the chance that someone will soon try again to delete this
13502 same bp, we mark it as deleted before freeing its storage. */
c906108c 13503 bpt->type = bp_none;
4d01a485 13504 delete bpt;
c906108c
SS
13505}
13506
4d6140d9
AC
13507static void
13508do_delete_breakpoint_cleanup (void *b)
13509{
9a3c8263 13510 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13511}
13512
13513struct cleanup *
13514make_cleanup_delete_breakpoint (struct breakpoint *b)
13515{
13516 return make_cleanup (do_delete_breakpoint_cleanup, b);
13517}
13518
51be5b68
PA
13519/* Iterator function to call a user-provided callback function once
13520 for each of B and its related breakpoints. */
13521
13522static void
13523iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13524 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13525{
13526 struct breakpoint *related;
13527
13528 related = b;
13529 do
13530 {
13531 struct breakpoint *next;
13532
13533 /* FUNCTION may delete RELATED. */
13534 next = related->related_breakpoint;
13535
13536 if (next == related)
13537 {
13538 /* RELATED is the last ring entry. */
48649e1b 13539 function (related);
51be5b68
PA
13540
13541 /* FUNCTION may have deleted it, so we'd never reach back to
13542 B. There's nothing left to do anyway, so just break
13543 out. */
13544 break;
13545 }
13546 else
48649e1b 13547 function (related);
51be5b68
PA
13548
13549 related = next;
13550 }
13551 while (related != b);
13552}
95a42b64 13553
4495129a 13554static void
fba45db2 13555delete_command (char *arg, int from_tty)
c906108c 13556{
35df4500 13557 struct breakpoint *b, *b_tmp;
c906108c 13558
ea9365bb
TT
13559 dont_repeat ();
13560
c906108c
SS
13561 if (arg == 0)
13562 {
13563 int breaks_to_delete = 0;
13564
46c6471b
PA
13565 /* Delete all breakpoints if no argument. Do not delete
13566 internal breakpoints, these have to be deleted with an
13567 explicit breakpoint number argument. */
c5aa993b 13568 ALL_BREAKPOINTS (b)
46c6471b 13569 if (user_breakpoint_p (b))
973d738b
DJ
13570 {
13571 breaks_to_delete = 1;
13572 break;
13573 }
c906108c
SS
13574
13575 /* Ask user only if there are some breakpoints to delete. */
13576 if (!from_tty
e2e0b3e5 13577 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13578 {
35df4500 13579 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13580 if (user_breakpoint_p (b))
c5aa993b 13581 delete_breakpoint (b);
c906108c
SS
13582 }
13583 }
13584 else
48649e1b
TT
13585 map_breakpoint_numbers
13586 (arg, [&] (breakpoint *b)
13587 {
13588 iterate_over_related_breakpoints (b, delete_breakpoint);
13589 });
c906108c
SS
13590}
13591
c2f4122d
PA
13592/* Return true if all locations of B bound to PSPACE are pending. If
13593 PSPACE is NULL, all locations of all program spaces are
13594 considered. */
13595
0d381245 13596static int
c2f4122d 13597all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13598{
c2f4122d
PA
13599 struct bp_location *loc;
13600
13601 for (loc = b->loc; loc != NULL; loc = loc->next)
13602 if ((pspace == NULL
13603 || loc->pspace == pspace)
13604 && !loc->shlib_disabled
8645ff69 13605 && !loc->pspace->executing_startup)
0d381245
VP
13606 return 0;
13607 return 1;
fe3f5fa8
VP
13608}
13609
776592bf
DE
13610/* Subroutine of update_breakpoint_locations to simplify it.
13611 Return non-zero if multiple fns in list LOC have the same name.
13612 Null names are ignored. */
13613
13614static int
13615ambiguous_names_p (struct bp_location *loc)
13616{
13617 struct bp_location *l;
13618 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13619 (int (*) (const void *,
13620 const void *)) streq,
776592bf
DE
13621 NULL, xcalloc, xfree);
13622
13623 for (l = loc; l != NULL; l = l->next)
13624 {
13625 const char **slot;
13626 const char *name = l->function_name;
13627
13628 /* Allow for some names to be NULL, ignore them. */
13629 if (name == NULL)
13630 continue;
13631
13632 slot = (const char **) htab_find_slot (htab, (const void *) name,
13633 INSERT);
4a64f543
MS
13634 /* NOTE: We can assume slot != NULL here because xcalloc never
13635 returns NULL. */
776592bf
DE
13636 if (*slot != NULL)
13637 {
13638 htab_delete (htab);
13639 return 1;
13640 }
13641 *slot = name;
13642 }
13643
13644 htab_delete (htab);
13645 return 0;
13646}
13647
0fb4aa4b
PA
13648/* When symbols change, it probably means the sources changed as well,
13649 and it might mean the static tracepoint markers are no longer at
13650 the same address or line numbers they used to be at last we
13651 checked. Losing your static tracepoints whenever you rebuild is
13652 undesirable. This function tries to resync/rematch gdb static
13653 tracepoints with the markers on the target, for static tracepoints
13654 that have not been set by marker id. Static tracepoint that have
13655 been set by marker id are reset by marker id in breakpoint_re_set.
13656 The heuristic is:
13657
13658 1) For a tracepoint set at a specific address, look for a marker at
13659 the old PC. If one is found there, assume to be the same marker.
13660 If the name / string id of the marker found is different from the
13661 previous known name, assume that means the user renamed the marker
13662 in the sources, and output a warning.
13663
13664 2) For a tracepoint set at a given line number, look for a marker
13665 at the new address of the old line number. If one is found there,
13666 assume to be the same marker. If the name / string id of the
13667 marker found is different from the previous known name, assume that
13668 means the user renamed the marker in the sources, and output a
13669 warning.
13670
13671 3) If a marker is no longer found at the same address or line, it
13672 may mean the marker no longer exists. But it may also just mean
13673 the code changed a bit. Maybe the user added a few lines of code
13674 that made the marker move up or down (in line number terms). Ask
13675 the target for info about the marker with the string id as we knew
13676 it. If found, update line number and address in the matching
13677 static tracepoint. This will get confused if there's more than one
13678 marker with the same ID (possible in UST, although unadvised
13679 precisely because it confuses tools). */
13680
13681static struct symtab_and_line
13682update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13683{
d9b3f62e 13684 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13685 struct static_tracepoint_marker marker;
13686 CORE_ADDR pc;
0fb4aa4b
PA
13687
13688 pc = sal.pc;
13689 if (sal.line)
13690 find_line_pc (sal.symtab, sal.line, &pc);
13691
13692 if (target_static_tracepoint_marker_at (pc, &marker))
13693 {
d9b3f62e 13694 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13695 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13696 b->number,
d9b3f62e 13697 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13698
d9b3f62e
PA
13699 xfree (tp->static_trace_marker_id);
13700 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13701 release_static_tracepoint_marker (&marker);
13702
13703 return sal;
13704 }
13705
13706 /* Old marker wasn't found on target at lineno. Try looking it up
13707 by string ID. */
13708 if (!sal.explicit_pc
13709 && sal.line != 0
13710 && sal.symtab != NULL
d9b3f62e 13711 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13712 {
13713 VEC(static_tracepoint_marker_p) *markers;
13714
13715 markers
d9b3f62e 13716 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13717
13718 if (!VEC_empty(static_tracepoint_marker_p, markers))
13719 {
0fb4aa4b 13720 struct symbol *sym;
80e1d417 13721 struct static_tracepoint_marker *tpmarker;
79a45e25 13722 struct ui_out *uiout = current_uiout;
67994074 13723 struct explicit_location explicit_loc;
0fb4aa4b 13724
80e1d417 13725 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13726
d9b3f62e 13727 xfree (tp->static_trace_marker_id);
80e1d417 13728 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13729
13730 warning (_("marker for static tracepoint %d (%s) not "
13731 "found at previous line number"),
d9b3f62e 13732 b->number, tp->static_trace_marker_id);
0fb4aa4b 13733
51abb421 13734 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13735 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13736 uiout->text ("Now in ");
0fb4aa4b
PA
13737 if (sym)
13738 {
112e8700
SM
13739 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13740 uiout->text (" at ");
0fb4aa4b 13741 }
112e8700 13742 uiout->field_string ("file",
05cba821 13743 symtab_to_filename_for_display (sal2.symtab));
112e8700 13744 uiout->text (":");
0fb4aa4b 13745
112e8700 13746 if (uiout->is_mi_like_p ())
0fb4aa4b 13747 {
0b0865da 13748 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13749
112e8700 13750 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13751 }
13752
112e8700
SM
13753 uiout->field_int ("line", sal2.line);
13754 uiout->text ("\n");
0fb4aa4b 13755
80e1d417 13756 b->loc->line_number = sal2.line;
2f202fde 13757 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13758
d28cd78a 13759 b->location.reset (NULL);
67994074
KS
13760 initialize_explicit_location (&explicit_loc);
13761 explicit_loc.source_filename
00e52e53 13762 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13763 explicit_loc.line_offset.offset = b->loc->line_number;
13764 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13765 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13766
13767 /* Might be nice to check if function changed, and warn if
13768 so. */
13769
80e1d417 13770 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13771 }
13772 }
13773 return sal;
13774}
13775
8d3788bd
VP
13776/* Returns 1 iff locations A and B are sufficiently same that
13777 we don't need to report breakpoint as changed. */
13778
13779static int
13780locations_are_equal (struct bp_location *a, struct bp_location *b)
13781{
13782 while (a && b)
13783 {
13784 if (a->address != b->address)
13785 return 0;
13786
13787 if (a->shlib_disabled != b->shlib_disabled)
13788 return 0;
13789
13790 if (a->enabled != b->enabled)
13791 return 0;
13792
13793 a = a->next;
13794 b = b->next;
13795 }
13796
13797 if ((a == NULL) != (b == NULL))
13798 return 0;
13799
13800 return 1;
13801}
13802
c2f4122d
PA
13803/* Split all locations of B that are bound to PSPACE out of B's
13804 location list to a separate list and return that list's head. If
13805 PSPACE is NULL, hoist out all locations of B. */
13806
13807static struct bp_location *
13808hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13809{
13810 struct bp_location head;
13811 struct bp_location *i = b->loc;
13812 struct bp_location **i_link = &b->loc;
13813 struct bp_location *hoisted = &head;
13814
13815 if (pspace == NULL)
13816 {
13817 i = b->loc;
13818 b->loc = NULL;
13819 return i;
13820 }
13821
13822 head.next = NULL;
13823
13824 while (i != NULL)
13825 {
13826 if (i->pspace == pspace)
13827 {
13828 *i_link = i->next;
13829 i->next = NULL;
13830 hoisted->next = i;
13831 hoisted = i;
13832 }
13833 else
13834 i_link = &i->next;
13835 i = *i_link;
13836 }
13837
13838 return head.next;
13839}
13840
13841/* Create new breakpoint locations for B (a hardware or software
13842 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13843 zero, then B is a ranged breakpoint. Only recreates locations for
13844 FILTER_PSPACE. Locations of other program spaces are left
13845 untouched. */
f1310107 13846
0e30163f 13847void
0d381245 13848update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13849 struct program_space *filter_pspace,
6c5b2ebe
PA
13850 gdb::array_view<const symtab_and_line> sals,
13851 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13852{
13853 int i;
c2f4122d 13854 struct bp_location *existing_locations;
0d381245 13855
6c5b2ebe 13856 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13857 {
13858 /* Ranged breakpoints have only one start location and one end
13859 location. */
13860 b->enable_state = bp_disabled;
f8eba3c6
TT
13861 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13862 "multiple locations found\n"),
13863 b->number);
13864 return;
13865 }
f1310107 13866
4a64f543
MS
13867 /* If there's no new locations, and all existing locations are
13868 pending, don't do anything. This optimizes the common case where
13869 all locations are in the same shared library, that was unloaded.
13870 We'd like to retain the location, so that when the library is
13871 loaded again, we don't loose the enabled/disabled status of the
13872 individual locations. */
6c5b2ebe 13873 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13874 return;
13875
c2f4122d 13876 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13877
6c5b2ebe 13878 for (const auto &sal : sals)
fe3f5fa8 13879 {
f8eba3c6
TT
13880 struct bp_location *new_loc;
13881
6c5b2ebe 13882 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13883
6c5b2ebe 13884 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13885
0d381245
VP
13886 /* Reparse conditions, they might contain references to the
13887 old symtab. */
13888 if (b->cond_string != NULL)
13889 {
bbc13ae3 13890 const char *s;
fe3f5fa8 13891
0d381245 13892 s = b->cond_string;
492d29ea 13893 TRY
0d381245 13894 {
6c5b2ebe
PA
13895 new_loc->cond = parse_exp_1 (&s, sal.pc,
13896 block_for_pc (sal.pc),
0d381245
VP
13897 0);
13898 }
492d29ea 13899 CATCH (e, RETURN_MASK_ERROR)
0d381245 13900 {
3e43a32a
MS
13901 warning (_("failed to reevaluate condition "
13902 "for breakpoint %d: %s"),
0d381245
VP
13903 b->number, e.message);
13904 new_loc->enabled = 0;
13905 }
492d29ea 13906 END_CATCH
0d381245 13907 }
fe3f5fa8 13908
6c5b2ebe 13909 if (!sals_end.empty ())
f1310107 13910 {
6c5b2ebe 13911 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13912
6c5b2ebe 13913 new_loc->length = end - sals[0].pc + 1;
f1310107 13914 }
0d381245 13915 }
fe3f5fa8 13916
4a64f543
MS
13917 /* If possible, carry over 'disable' status from existing
13918 breakpoints. */
0d381245
VP
13919 {
13920 struct bp_location *e = existing_locations;
776592bf
DE
13921 /* If there are multiple breakpoints with the same function name,
13922 e.g. for inline functions, comparing function names won't work.
13923 Instead compare pc addresses; this is just a heuristic as things
13924 may have moved, but in practice it gives the correct answer
13925 often enough until a better solution is found. */
13926 int have_ambiguous_names = ambiguous_names_p (b->loc);
13927
0d381245
VP
13928 for (; e; e = e->next)
13929 {
13930 if (!e->enabled && e->function_name)
13931 {
13932 struct bp_location *l = b->loc;
776592bf
DE
13933 if (have_ambiguous_names)
13934 {
13935 for (; l; l = l->next)
f1310107 13936 if (breakpoint_locations_match (e, l))
776592bf
DE
13937 {
13938 l->enabled = 0;
13939 break;
13940 }
13941 }
13942 else
13943 {
13944 for (; l; l = l->next)
13945 if (l->function_name
13946 && strcmp (e->function_name, l->function_name) == 0)
13947 {
13948 l->enabled = 0;
13949 break;
13950 }
13951 }
0d381245
VP
13952 }
13953 }
13954 }
fe3f5fa8 13955
8d3788bd
VP
13956 if (!locations_are_equal (existing_locations, b->loc))
13957 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13958}
13959
f00aae0f 13960/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13961 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13962
6c5b2ebe 13963static std::vector<symtab_and_line>
f00aae0f 13964location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13965 struct program_space *search_pspace, int *found)
ef23e705 13966{
492d29ea 13967 struct gdb_exception exception = exception_none;
ef23e705 13968
983af33b 13969 gdb_assert (b->ops != NULL);
ef23e705 13970
6c5b2ebe
PA
13971 std::vector<symtab_and_line> sals;
13972
492d29ea 13973 TRY
ef23e705 13974 {
6c5b2ebe 13975 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13976 }
492d29ea 13977 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13978 {
13979 int not_found_and_ok = 0;
492d29ea
PA
13980
13981 exception = e;
13982
ef23e705
TJB
13983 /* For pending breakpoints, it's expected that parsing will
13984 fail until the right shared library is loaded. User has
13985 already told to create pending breakpoints and don't need
13986 extra messages. If breakpoint is in bp_shlib_disabled
13987 state, then user already saw the message about that
13988 breakpoint being disabled, and don't want to see more
13989 errors. */
58438ac1 13990 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13991 && (b->condition_not_parsed
13992 || (b->loc != NULL
13993 && search_pspace != NULL
13994 && b->loc->pspace != search_pspace)
ef23e705 13995 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13996 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13997 || b->enable_state == bp_disabled))
13998 not_found_and_ok = 1;
13999
14000 if (!not_found_and_ok)
14001 {
14002 /* We surely don't want to warn about the same breakpoint
14003 10 times. One solution, implemented here, is disable
14004 the breakpoint on error. Another solution would be to
14005 have separate 'warning emitted' flag. Since this
14006 happens only when a binary has changed, I don't know
14007 which approach is better. */
14008 b->enable_state = bp_disabled;
14009 throw_exception (e);
14010 }
14011 }
492d29ea 14012 END_CATCH
ef23e705 14013
492d29ea 14014 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14015 {
6c5b2ebe
PA
14016 for (auto &sal : sals)
14017 resolve_sal_pc (&sal);
f00aae0f 14018 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14019 {
ed1d1739
KS
14020 char *cond_string, *extra_string;
14021 int thread, task;
ef23e705 14022
6c5b2ebe 14023 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
14024 &cond_string, &thread, &task,
14025 &extra_string);
f00aae0f 14026 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14027 if (cond_string)
14028 b->cond_string = cond_string;
14029 b->thread = thread;
14030 b->task = task;
e7e0cddf 14031 if (extra_string)
f00aae0f
KS
14032 {
14033 xfree (b->extra_string);
14034 b->extra_string = extra_string;
14035 }
ef23e705
TJB
14036 b->condition_not_parsed = 0;
14037 }
14038
983af33b 14039 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 14040 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 14041
58438ac1
TT
14042 *found = 1;
14043 }
14044 else
14045 *found = 0;
ef23e705
TJB
14046
14047 return sals;
14048}
14049
348d480f
PA
14050/* The default re_set method, for typical hardware or software
14051 breakpoints. Reevaluate the breakpoint and recreate its
14052 locations. */
14053
14054static void
28010a5d 14055breakpoint_re_set_default (struct breakpoint *b)
ef23e705 14056{
c2f4122d 14057 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 14058 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 14059
6c5b2ebe
PA
14060 int found;
14061 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14062 filter_pspace, &found);
ef23e705 14063 if (found)
6c5b2ebe 14064 expanded = std::move (sals);
ef23e705 14065
f00aae0f 14066 if (b->location_range_end != NULL)
f1310107 14067 {
6c5b2ebe
PA
14068 std::vector<symtab_and_line> sals_end
14069 = location_to_sals (b, b->location_range_end.get (),
14070 filter_pspace, &found);
f1310107 14071 if (found)
6c5b2ebe 14072 expanded_end = std::move (sals_end);
f1310107
TJB
14073 }
14074
c2f4122d 14075 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14076}
14077
983af33b
SDJ
14078/* Default method for creating SALs from an address string. It basically
14079 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14080
14081static void
f00aae0f
KS
14082create_sals_from_location_default (const struct event_location *location,
14083 struct linespec_result *canonical,
14084 enum bptype type_wanted)
983af33b 14085{
f00aae0f 14086 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14087}
14088
14089/* Call create_breakpoints_sal for the given arguments. This is the default
14090 function for the `create_breakpoints_sal' method of
14091 breakpoint_ops. */
14092
14093static void
14094create_breakpoints_sal_default (struct gdbarch *gdbarch,
14095 struct linespec_result *canonical,
e1e01040
PA
14096 gdb::unique_xmalloc_ptr<char> cond_string,
14097 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14098 enum bptype type_wanted,
14099 enum bpdisp disposition,
14100 int thread,
14101 int task, int ignore_count,
14102 const struct breakpoint_ops *ops,
14103 int from_tty, int enabled,
44f238bb 14104 int internal, unsigned flags)
983af33b 14105{
e1e01040
PA
14106 create_breakpoints_sal (gdbarch, canonical,
14107 std::move (cond_string),
14108 std::move (extra_string),
983af33b
SDJ
14109 type_wanted, disposition,
14110 thread, task, ignore_count, ops, from_tty,
44f238bb 14111 enabled, internal, flags);
983af33b
SDJ
14112}
14113
14114/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14115 default function for the `decode_location' method of breakpoint_ops. */
983af33b 14116
6c5b2ebe 14117static std::vector<symtab_and_line>
f00aae0f
KS
14118decode_location_default (struct breakpoint *b,
14119 const struct event_location *location,
6c5b2ebe 14120 struct program_space *search_pspace)
983af33b
SDJ
14121{
14122 struct linespec_result canonical;
14123
c2f4122d 14124 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14125 (struct symtab *) NULL, 0,
14126 &canonical, multiple_symbols_all,
14127 b->filter);
14128
14129 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 14130 gdb_assert (canonical.lsals.size () < 2);
983af33b 14131
6c5b2ebe 14132 if (!canonical.lsals.empty ())
983af33b 14133 {
6c5b2ebe
PA
14134 const linespec_sals &lsal = canonical.lsals[0];
14135 return std::move (lsal.sals);
983af33b 14136 }
6c5b2ebe 14137 return {};
983af33b
SDJ
14138}
14139
28010a5d
PA
14140/* Prepare the global context for a re-set of breakpoint B. */
14141
14142static struct cleanup *
14143prepare_re_set_context (struct breakpoint *b)
14144{
28010a5d 14145 input_radix = b->input_radix;
28010a5d
PA
14146 set_language (b->language);
14147
c2f4122d 14148 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14149}
14150
c906108c
SS
14151/* Reset a breakpoint given it's struct breakpoint * BINT.
14152 The value we return ends up being the return value from catch_errors.
14153 Unused in this case. */
14154
14155static int
4efb68b1 14156breakpoint_re_set_one (void *bint)
c906108c 14157{
4a64f543 14158 /* Get past catch_errs. */
53a5351d 14159 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14160 struct cleanup *cleanups;
c906108c 14161
348d480f
PA
14162 cleanups = prepare_re_set_context (b);
14163 b->ops->re_set (b);
14164 do_cleanups (cleanups);
c906108c
SS
14165 return 0;
14166}
14167
c2f4122d
PA
14168/* Re-set breakpoint locations for the current program space.
14169 Locations bound to other program spaces are left untouched. */
14170
c906108c 14171void
69de3c6a 14172breakpoint_re_set (void)
c906108c 14173{
35df4500 14174 struct breakpoint *b, *b_tmp;
c906108c
SS
14175 enum language save_language;
14176 int save_input_radix;
c5aa993b 14177
c906108c
SS
14178 save_language = current_language->la_language;
14179 save_input_radix = input_radix;
2a7f3dff 14180
c5aa993b 14181 {
5ed8105e 14182 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14183
5ed8105e
PA
14184 /* Note: we must not try to insert locations until after all
14185 breakpoints have been re-set. Otherwise, e.g., when re-setting
14186 breakpoint 1, we'd insert the locations of breakpoint 2, which
14187 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14188
5ed8105e
PA
14189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14190 {
14191 /* Format possible error msg. */
14192 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14193 b->number);
14194 struct cleanup *cleanups = make_cleanup (xfree, message);
14195 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14196 do_cleanups (cleanups);
14197 }
14198 set_language (save_language);
14199 input_radix = save_input_radix;
14200
14201 jit_breakpoint_re_set ();
14202 }
6c95b8df 14203
af02033e
PP
14204 create_overlay_event_breakpoint ();
14205 create_longjmp_master_breakpoint ();
14206 create_std_terminate_master_breakpoint ();
186c406b 14207 create_exception_master_breakpoint ();
2a7f3dff
PA
14208
14209 /* Now we can insert. */
14210 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14211}
14212\f
c906108c
SS
14213/* Reset the thread number of this breakpoint:
14214
14215 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14216 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14217void
fba45db2 14218breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14219{
14220 if (b->thread != -1)
14221 {
39f77062 14222 if (in_thread_list (inferior_ptid))
5d5658a1 14223 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14224
14225 /* We're being called after following a fork. The new fork is
14226 selected as current, and unless this was a vfork will have a
14227 different program space from the original thread. Reset that
14228 as well. */
14229 b->loc->pspace = current_program_space;
c906108c
SS
14230 }
14231}
14232
03ac34d5
MS
14233/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14234 If from_tty is nonzero, it prints a message to that effect,
14235 which ends with a period (no newline). */
14236
c906108c 14237void
fba45db2 14238set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14239{
52f0bd74 14240 struct breakpoint *b;
c906108c
SS
14241
14242 if (count < 0)
14243 count = 0;
14244
14245 ALL_BREAKPOINTS (b)
14246 if (b->number == bptnum)
c5aa993b 14247 {
d77f58be
SS
14248 if (is_tracepoint (b))
14249 {
14250 if (from_tty && count != 0)
14251 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14252 bptnum);
14253 return;
14254 }
14255
c5aa993b 14256 b->ignore_count = count;
221ea385
KS
14257 if (from_tty)
14258 {
14259 if (count == 0)
3e43a32a
MS
14260 printf_filtered (_("Will stop next time "
14261 "breakpoint %d is reached."),
221ea385
KS
14262 bptnum);
14263 else if (count == 1)
a3f17187 14264 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14265 bptnum);
14266 else
3e43a32a
MS
14267 printf_filtered (_("Will ignore next %d "
14268 "crossings of breakpoint %d."),
221ea385
KS
14269 count, bptnum);
14270 }
8d3788bd 14271 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14272 return;
14273 }
c906108c 14274
8a3fe4f8 14275 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14276}
14277
c906108c
SS
14278/* Command to set ignore-count of breakpoint N to COUNT. */
14279
14280static void
fba45db2 14281ignore_command (char *args, int from_tty)
c906108c
SS
14282{
14283 char *p = args;
52f0bd74 14284 int num;
c906108c
SS
14285
14286 if (p == 0)
e2e0b3e5 14287 error_no_arg (_("a breakpoint number"));
c5aa993b 14288
c906108c 14289 num = get_number (&p);
5c44784c 14290 if (num == 0)
8a3fe4f8 14291 error (_("bad breakpoint number: '%s'"), args);
c906108c 14292 if (*p == 0)
8a3fe4f8 14293 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14294
14295 set_ignore_count (num,
14296 longest_to_int (value_as_long (parse_and_eval (p))),
14297 from_tty);
221ea385
KS
14298 if (from_tty)
14299 printf_filtered ("\n");
c906108c
SS
14300}
14301\f
14302/* Call FUNCTION on each of the breakpoints
14303 whose numbers are given in ARGS. */
14304
14305static void
896b6bda 14306map_breakpoint_numbers (const char *args,
48649e1b 14307 gdb::function_view<void (breakpoint *)> function)
c906108c 14308{
52f0bd74
AC
14309 int num;
14310 struct breakpoint *b, *tmp;
c906108c 14311
b9d61307 14312 if (args == 0 || *args == '\0')
e2e0b3e5 14313 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14314
bfd28288 14315 number_or_range_parser parser (args);
197f0a60 14316
bfd28288 14317 while (!parser.finished ())
c906108c 14318 {
bfd28288
PA
14319 const char *p = parser.cur_tok ();
14320 bool match = false;
197f0a60 14321
bfd28288 14322 num = parser.get_number ();
5c44784c 14323 if (num == 0)
c5aa993b 14324 {
8a3fe4f8 14325 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14326 }
14327 else
14328 {
14329 ALL_BREAKPOINTS_SAFE (b, tmp)
14330 if (b->number == num)
14331 {
bfd28288 14332 match = true;
48649e1b 14333 function (b);
11cf8741 14334 break;
5c44784c 14335 }
bfd28288 14336 if (!match)
a3f17187 14337 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14338 }
c906108c
SS
14339 }
14340}
14341
0d381245 14342static struct bp_location *
cb791d59 14343find_location_by_number (const char *number)
0d381245 14344{
cb791d59 14345 const char *p1;
0d381245
VP
14346 int bp_num;
14347 int loc_num;
14348 struct breakpoint *b;
14349 struct bp_location *loc;
14350
0d381245 14351 p1 = number;
cb791d59
TT
14352 bp_num = get_number_trailer (&p1, '.');
14353 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14354 error (_("Bad breakpoint number '%s'"), number);
14355
14356 ALL_BREAKPOINTS (b)
14357 if (b->number == bp_num)
14358 {
14359 break;
14360 }
14361
14362 if (!b || b->number != bp_num)
14363 error (_("Bad breakpoint number '%s'"), number);
14364
cb791d59
TT
14365 /* Skip the dot. */
14366 ++p1;
14367 const char *save = p1;
197f0a60 14368 loc_num = get_number (&p1);
0d381245
VP
14369 if (loc_num == 0)
14370 error (_("Bad breakpoint location number '%s'"), number);
14371
14372 --loc_num;
14373 loc = b->loc;
14374 for (;loc_num && loc; --loc_num, loc = loc->next)
14375 ;
14376 if (!loc)
cb791d59 14377 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14378
14379 return loc;
14380}
14381
14382
1900040c
MS
14383/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14384 If from_tty is nonzero, it prints a message to that effect,
14385 which ends with a period (no newline). */
14386
c906108c 14387void
fba45db2 14388disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14389{
14390 /* Never disable a watchpoint scope breakpoint; we want to
14391 hit them when we leave scope so we can delete both the
14392 watchpoint and its scope breakpoint at that time. */
14393 if (bpt->type == bp_watchpoint_scope)
14394 return;
14395
b5de0fa7 14396 bpt->enable_state = bp_disabled;
c906108c 14397
b775012e
LM
14398 /* Mark breakpoint locations modified. */
14399 mark_breakpoint_modified (bpt);
14400
d248b706
KY
14401 if (target_supports_enable_disable_tracepoint ()
14402 && current_trace_status ()->running && is_tracepoint (bpt))
14403 {
14404 struct bp_location *location;
14405
14406 for (location = bpt->loc; location; location = location->next)
14407 target_disable_tracepoint (location);
14408 }
14409
44702360 14410 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14411
8d3788bd 14412 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14413}
14414
c906108c 14415static void
fba45db2 14416disable_command (char *args, int from_tty)
c906108c 14417{
c906108c 14418 if (args == 0)
46c6471b
PA
14419 {
14420 struct breakpoint *bpt;
14421
14422 ALL_BREAKPOINTS (bpt)
14423 if (user_breakpoint_p (bpt))
14424 disable_breakpoint (bpt);
14425 }
9eaabc75 14426 else
0d381245 14427 {
cb791d59 14428 std::string num = extract_arg (&args);
9eaabc75 14429
cb791d59 14430 while (!num.empty ())
d248b706 14431 {
cb791d59 14432 if (num.find ('.') != std::string::npos)
b775012e 14433 {
cb791d59 14434 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14435
14436 if (loc)
14437 {
14438 if (loc->enabled)
14439 {
14440 loc->enabled = 0;
14441 mark_breakpoint_location_modified (loc);
14442 }
14443 if (target_supports_enable_disable_tracepoint ()
14444 && current_trace_status ()->running && loc->owner
14445 && is_tracepoint (loc->owner))
14446 target_disable_tracepoint (loc);
14447 }
44702360 14448 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14449 }
9eaabc75 14450 else
48649e1b
TT
14451 map_breakpoint_numbers
14452 (num.c_str (), [&] (breakpoint *b)
14453 {
14454 iterate_over_related_breakpoints (b, disable_breakpoint);
14455 });
9eaabc75 14456 num = extract_arg (&args);
d248b706 14457 }
0d381245 14458 }
c906108c
SS
14459}
14460
14461static void
816338b5
SS
14462enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14463 int count)
c906108c 14464{
afe38095 14465 int target_resources_ok;
c906108c
SS
14466
14467 if (bpt->type == bp_hardware_breakpoint)
14468 {
14469 int i;
c5aa993b 14470 i = hw_breakpoint_used_count ();
53a5351d 14471 target_resources_ok =
d92524f1 14472 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14473 i + 1, 0);
c906108c 14474 if (target_resources_ok == 0)
8a3fe4f8 14475 error (_("No hardware breakpoint support in the target."));
c906108c 14476 else if (target_resources_ok < 0)
8a3fe4f8 14477 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14478 }
14479
cc60f2e3 14480 if (is_watchpoint (bpt))
c906108c 14481 {
d07205c2 14482 /* Initialize it just to avoid a GCC false warning. */
f486487f 14483 enum enable_state orig_enable_state = bp_disabled;
dde02812 14484
492d29ea 14485 TRY
c906108c 14486 {
3a5c3e22
PA
14487 struct watchpoint *w = (struct watchpoint *) bpt;
14488
1e718ff1
TJB
14489 orig_enable_state = bpt->enable_state;
14490 bpt->enable_state = bp_enabled;
3a5c3e22 14491 update_watchpoint (w, 1 /* reparse */);
c906108c 14492 }
492d29ea 14493 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14494 {
1e718ff1 14495 bpt->enable_state = orig_enable_state;
dde02812
ES
14496 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14497 bpt->number);
14498 return;
c5aa993b 14499 }
492d29ea 14500 END_CATCH
c906108c 14501 }
0101ce28 14502
b775012e
LM
14503 bpt->enable_state = bp_enabled;
14504
14505 /* Mark breakpoint locations modified. */
14506 mark_breakpoint_modified (bpt);
14507
d248b706
KY
14508 if (target_supports_enable_disable_tracepoint ()
14509 && current_trace_status ()->running && is_tracepoint (bpt))
14510 {
14511 struct bp_location *location;
14512
14513 for (location = bpt->loc; location; location = location->next)
14514 target_enable_tracepoint (location);
14515 }
14516
b4c291bb 14517 bpt->disposition = disposition;
816338b5 14518 bpt->enable_count = count;
44702360 14519 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14520
8d3788bd 14521 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14522}
14523
fe3f5fa8 14524
c906108c 14525void
fba45db2 14526enable_breakpoint (struct breakpoint *bpt)
c906108c 14527{
816338b5 14528 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14529}
14530
c906108c
SS
14531/* The enable command enables the specified breakpoints (or all defined
14532 breakpoints) so they once again become (or continue to be) effective
1272ad14 14533 in stopping the inferior. */
c906108c 14534
c906108c 14535static void
fba45db2 14536enable_command (char *args, int from_tty)
c906108c 14537{
c906108c 14538 if (args == 0)
46c6471b
PA
14539 {
14540 struct breakpoint *bpt;
14541
14542 ALL_BREAKPOINTS (bpt)
14543 if (user_breakpoint_p (bpt))
14544 enable_breakpoint (bpt);
14545 }
9eaabc75 14546 else
0d381245 14547 {
cb791d59 14548 std::string num = extract_arg (&args);
9eaabc75 14549
cb791d59 14550 while (!num.empty ())
d248b706 14551 {
cb791d59 14552 if (num.find ('.') != std::string::npos)
b775012e 14553 {
cb791d59 14554 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14555
14556 if (loc)
14557 {
14558 if (!loc->enabled)
14559 {
14560 loc->enabled = 1;
14561 mark_breakpoint_location_modified (loc);
14562 }
14563 if (target_supports_enable_disable_tracepoint ()
14564 && current_trace_status ()->running && loc->owner
14565 && is_tracepoint (loc->owner))
14566 target_enable_tracepoint (loc);
14567 }
44702360 14568 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14569 }
9eaabc75 14570 else
48649e1b
TT
14571 map_breakpoint_numbers
14572 (num.c_str (), [&] (breakpoint *b)
14573 {
14574 iterate_over_related_breakpoints (b, enable_breakpoint);
14575 });
9eaabc75 14576 num = extract_arg (&args);
d248b706 14577 }
0d381245 14578 }
c906108c
SS
14579}
14580
c906108c 14581static void
4495129a 14582enable_once_command (const char *args, int from_tty)
c906108c 14583{
48649e1b
TT
14584 map_breakpoint_numbers
14585 (args, [&] (breakpoint *b)
14586 {
14587 iterate_over_related_breakpoints
14588 (b, [&] (breakpoint *bpt)
14589 {
14590 enable_breakpoint_disp (bpt, disp_disable, 1);
14591 });
14592 });
816338b5
SS
14593}
14594
14595static void
4495129a 14596enable_count_command (const char *args, int from_tty)
816338b5 14597{
b9d61307
SM
14598 int count;
14599
14600 if (args == NULL)
14601 error_no_arg (_("hit count"));
14602
14603 count = get_number (&args);
816338b5 14604
48649e1b
TT
14605 map_breakpoint_numbers
14606 (args, [&] (breakpoint *b)
14607 {
14608 iterate_over_related_breakpoints
14609 (b, [&] (breakpoint *bpt)
14610 {
14611 enable_breakpoint_disp (bpt, disp_disable, count);
14612 });
14613 });
c906108c
SS
14614}
14615
c906108c 14616static void
4495129a 14617enable_delete_command (const char *args, int from_tty)
c906108c 14618{
48649e1b
TT
14619 map_breakpoint_numbers
14620 (args, [&] (breakpoint *b)
14621 {
14622 iterate_over_related_breakpoints
14623 (b, [&] (breakpoint *bpt)
14624 {
14625 enable_breakpoint_disp (bpt, disp_del, 1);
14626 });
14627 });
c906108c
SS
14628}
14629\f
fa8d40ab
JJ
14630static void
14631set_breakpoint_cmd (char *args, int from_tty)
14632{
14633}
14634
14635static void
14636show_breakpoint_cmd (char *args, int from_tty)
14637{
14638}
14639
1f3b5d1b
PP
14640/* Invalidate last known value of any hardware watchpoint if
14641 the memory which that value represents has been written to by
14642 GDB itself. */
14643
14644static void
8de0566d
YQ
14645invalidate_bp_value_on_memory_change (struct inferior *inferior,
14646 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14647 const bfd_byte *data)
14648{
14649 struct breakpoint *bp;
14650
14651 ALL_BREAKPOINTS (bp)
14652 if (bp->enable_state == bp_enabled
3a5c3e22 14653 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14654 {
3a5c3e22 14655 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14656
3a5c3e22
PA
14657 if (wp->val_valid && wp->val)
14658 {
14659 struct bp_location *loc;
14660
14661 for (loc = bp->loc; loc != NULL; loc = loc->next)
14662 if (loc->loc_type == bp_loc_hardware_watchpoint
14663 && loc->address + loc->length > addr
14664 && addr + len > loc->address)
14665 {
14666 value_free (wp->val);
14667 wp->val = NULL;
14668 wp->val_valid = 0;
14669 }
14670 }
1f3b5d1b
PP
14671 }
14672}
14673
8181d85f
DJ
14674/* Create and insert a breakpoint for software single step. */
14675
14676void
6c95b8df 14677insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14678 struct address_space *aspace,
14679 CORE_ADDR next_pc)
8181d85f 14680{
7c16b83e
PA
14681 struct thread_info *tp = inferior_thread ();
14682 struct symtab_and_line sal;
14683 CORE_ADDR pc = next_pc;
8181d85f 14684
34b7e8a6
PA
14685 if (tp->control.single_step_breakpoints == NULL)
14686 {
14687 tp->control.single_step_breakpoints
5d5658a1 14688 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14689 }
8181d85f 14690
7c16b83e
PA
14691 sal = find_pc_line (pc, 0);
14692 sal.pc = pc;
14693 sal.section = find_pc_overlay (pc);
14694 sal.explicit_pc = 1;
34b7e8a6 14695 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14696
7c16b83e 14697 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14698}
14699
93f9a11f
YQ
14700/* Insert single step breakpoints according to the current state. */
14701
14702int
14703insert_single_step_breakpoints (struct gdbarch *gdbarch)
14704{
f5ea389a 14705 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14706 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14707
f5ea389a 14708 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14709
a0ff9e1a 14710 if (!next_pcs.empty ())
93f9a11f 14711 {
f5ea389a 14712 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14713 struct address_space *aspace = get_frame_address_space (frame);
14714
a0ff9e1a 14715 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14716 insert_single_step_breakpoint (gdbarch, aspace, pc);
14717
93f9a11f
YQ
14718 return 1;
14719 }
14720 else
14721 return 0;
14722}
14723
34b7e8a6 14724/* See breakpoint.h. */
f02253f1
HZ
14725
14726int
7c16b83e
PA
14727breakpoint_has_location_inserted_here (struct breakpoint *bp,
14728 struct address_space *aspace,
14729 CORE_ADDR pc)
1aafd4da 14730{
7c16b83e 14731 struct bp_location *loc;
1aafd4da 14732
7c16b83e
PA
14733 for (loc = bp->loc; loc != NULL; loc = loc->next)
14734 if (loc->inserted
14735 && breakpoint_location_address_match (loc, aspace, pc))
14736 return 1;
1aafd4da 14737
7c16b83e 14738 return 0;
ef370185
JB
14739}
14740
14741/* Check whether a software single-step breakpoint is inserted at
14742 PC. */
14743
14744int
14745single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14746 CORE_ADDR pc)
14747{
34b7e8a6
PA
14748 struct breakpoint *bpt;
14749
14750 ALL_BREAKPOINTS (bpt)
14751 {
14752 if (bpt->type == bp_single_step
14753 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14754 return 1;
14755 }
14756 return 0;
1aafd4da
UW
14757}
14758
1042e4c0
SS
14759/* Tracepoint-specific operations. */
14760
14761/* Set tracepoint count to NUM. */
14762static void
14763set_tracepoint_count (int num)
14764{
14765 tracepoint_count = num;
4fa62494 14766 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14767}
14768
70221824 14769static void
f2fc3015 14770trace_command (char *arg_in, int from_tty)
1042e4c0 14771{
f2fc3015 14772 const char *arg = arg_in;
55aa24fb 14773 struct breakpoint_ops *ops;
55aa24fb 14774
ffc2605c
TT
14775 event_location_up location = string_to_event_location (&arg,
14776 current_language);
5b56227b 14777 if (location != NULL
ffc2605c 14778 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14779 ops = &tracepoint_probe_breakpoint_ops;
14780 else
14781 ops = &tracepoint_breakpoint_ops;
14782
558a9d82 14783 create_breakpoint (get_current_arch (),
ffc2605c 14784 location.get (),
f00aae0f 14785 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14786 0 /* tempflag */,
14787 bp_tracepoint /* type_wanted */,
14788 0 /* Ignore count */,
14789 pending_break_support,
14790 ops,
14791 from_tty,
14792 1 /* enabled */,
14793 0 /* internal */, 0);
1042e4c0
SS
14794}
14795
70221824 14796static void
f2fc3015 14797ftrace_command (char *arg_in, int from_tty)
7a697b8d 14798{
f2fc3015 14799 const char *arg = arg_in;
ffc2605c
TT
14800 event_location_up location = string_to_event_location (&arg,
14801 current_language);
558a9d82 14802 create_breakpoint (get_current_arch (),
ffc2605c 14803 location.get (),
f00aae0f 14804 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14805 0 /* tempflag */,
14806 bp_fast_tracepoint /* type_wanted */,
14807 0 /* Ignore count */,
14808 pending_break_support,
14809 &tracepoint_breakpoint_ops,
14810 from_tty,
14811 1 /* enabled */,
14812 0 /* internal */, 0);
0fb4aa4b
PA
14813}
14814
14815/* strace command implementation. Creates a static tracepoint. */
14816
70221824 14817static void
f2fc3015 14818strace_command (char *arg_in, int from_tty)
0fb4aa4b 14819{
f2fc3015 14820 const char *arg = arg_in;
983af33b 14821 struct breakpoint_ops *ops;
ffc2605c 14822 event_location_up location;
f00aae0f 14823 struct cleanup *back_to;
983af33b
SDJ
14824
14825 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14826 or with a normal static tracepoint. */
61012eef 14827 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14828 {
14829 ops = &strace_marker_breakpoint_ops;
14830 location = new_linespec_location (&arg);
14831 }
983af33b 14832 else
f00aae0f
KS
14833 {
14834 ops = &tracepoint_breakpoint_ops;
14835 location = string_to_event_location (&arg, current_language);
14836 }
983af33b 14837
558a9d82 14838 create_breakpoint (get_current_arch (),
ffc2605c 14839 location.get (),
f00aae0f 14840 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14841 0 /* tempflag */,
14842 bp_static_tracepoint /* type_wanted */,
14843 0 /* Ignore count */,
14844 pending_break_support,
14845 ops,
14846 from_tty,
14847 1 /* enabled */,
14848 0 /* internal */, 0);
7a697b8d
SS
14849}
14850
409873ef
SS
14851/* Set up a fake reader function that gets command lines from a linked
14852 list that was acquired during tracepoint uploading. */
14853
14854static struct uploaded_tp *this_utp;
3149d8c1 14855static int next_cmd;
409873ef
SS
14856
14857static char *
14858read_uploaded_action (void)
14859{
14860 char *rslt;
14861
3149d8c1 14862 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14863
3149d8c1 14864 next_cmd++;
409873ef
SS
14865
14866 return rslt;
14867}
14868
00bf0b85
SS
14869/* Given information about a tracepoint as recorded on a target (which
14870 can be either a live system or a trace file), attempt to create an
14871 equivalent GDB tracepoint. This is not a reliable process, since
14872 the target does not necessarily have all the information used when
14873 the tracepoint was originally defined. */
14874
d9b3f62e 14875struct tracepoint *
00bf0b85 14876create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14877{
f2fc3015
TT
14878 const char *addr_str;
14879 char small_buf[100];
d9b3f62e 14880 struct tracepoint *tp;
fd9b8c24 14881
409873ef
SS
14882 if (utp->at_string)
14883 addr_str = utp->at_string;
14884 else
14885 {
14886 /* In the absence of a source location, fall back to raw
14887 address. Since there is no way to confirm that the address
14888 means the same thing as when the trace was started, warn the
14889 user. */
3e43a32a
MS
14890 warning (_("Uploaded tracepoint %d has no "
14891 "source location, using raw address"),
409873ef 14892 utp->number);
8c042590 14893 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14894 addr_str = small_buf;
14895 }
14896
14897 /* There's not much we can do with a sequence of bytecodes. */
14898 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14899 warning (_("Uploaded tracepoint %d condition "
14900 "has no source form, ignoring it"),
409873ef 14901 utp->number);
d5551862 14902
ffc2605c
TT
14903 event_location_up location = string_to_event_location (&addr_str,
14904 current_language);
8cdf0e15 14905 if (!create_breakpoint (get_current_arch (),
ffc2605c 14906 location.get (),
f00aae0f 14907 utp->cond_string, -1, addr_str,
e7e0cddf 14908 0 /* parse cond/thread */,
8cdf0e15 14909 0 /* tempflag */,
0fb4aa4b 14910 utp->type /* type_wanted */,
8cdf0e15
VP
14911 0 /* Ignore count */,
14912 pending_break_support,
348d480f 14913 &tracepoint_breakpoint_ops,
8cdf0e15 14914 0 /* from_tty */,
84f4c1fe 14915 utp->enabled /* enabled */,
44f238bb
PA
14916 0 /* internal */,
14917 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14918 return NULL;
fd9b8c24 14919
409873ef 14920 /* Get the tracepoint we just created. */
fd9b8c24
PA
14921 tp = get_tracepoint (tracepoint_count);
14922 gdb_assert (tp != NULL);
d5551862 14923
00bf0b85
SS
14924 if (utp->pass > 0)
14925 {
8c042590 14926 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14927 tp->number);
00bf0b85 14928
409873ef 14929 trace_pass_command (small_buf, 0);
00bf0b85
SS
14930 }
14931
409873ef
SS
14932 /* If we have uploaded versions of the original commands, set up a
14933 special-purpose "reader" function and call the usual command line
14934 reader, then pass the result to the breakpoint command-setting
14935 function. */
3149d8c1 14936 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14937 {
93921405 14938 command_line_up cmd_list;
00bf0b85 14939
409873ef 14940 this_utp = utp;
3149d8c1 14941 next_cmd = 0;
d5551862 14942
409873ef
SS
14943 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14944
c1fc2657 14945 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14946 }
3149d8c1
SS
14947 else if (!VEC_empty (char_ptr, utp->actions)
14948 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14949 warning (_("Uploaded tracepoint %d actions "
14950 "have no source form, ignoring them"),
409873ef 14951 utp->number);
00bf0b85 14952
f196051f 14953 /* Copy any status information that might be available. */
c1fc2657 14954 tp->hit_count = utp->hit_count;
f196051f
SS
14955 tp->traceframe_usage = utp->traceframe_usage;
14956
00bf0b85 14957 return tp;
d9b3f62e 14958}
00bf0b85 14959
1042e4c0
SS
14960/* Print information on tracepoint number TPNUM_EXP, or all if
14961 omitted. */
14962
14963static void
11db9430 14964info_tracepoints_command (char *args, int from_tty)
1042e4c0 14965{
79a45e25 14966 struct ui_out *uiout = current_uiout;
e5a67952 14967 int num_printed;
1042e4c0 14968
e5a67952 14969 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14970
14971 if (num_printed == 0)
1042e4c0 14972 {
e5a67952 14973 if (args == NULL || *args == '\0')
112e8700 14974 uiout->message ("No tracepoints.\n");
d77f58be 14975 else
112e8700 14976 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14977 }
ad443146
SS
14978
14979 default_collect_info ();
1042e4c0
SS
14980}
14981
4a64f543 14982/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14983 Not supported by all targets. */
14984static void
14985enable_trace_command (char *args, int from_tty)
14986{
14987 enable_command (args, from_tty);
14988}
14989
4a64f543 14990/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14991 Not supported by all targets. */
14992static void
14993disable_trace_command (char *args, int from_tty)
14994{
14995 disable_command (args, from_tty);
14996}
14997
4a64f543 14998/* Remove a tracepoint (or all if no argument). */
1042e4c0 14999static void
4495129a 15000delete_trace_command (const char *arg, int from_tty)
1042e4c0 15001{
35df4500 15002 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15003
15004 dont_repeat ();
15005
15006 if (arg == 0)
15007 {
15008 int breaks_to_delete = 0;
15009
15010 /* Delete all breakpoints if no argument.
15011 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15012 have to be deleted with an explicit breakpoint number
15013 argument. */
1042e4c0 15014 ALL_TRACEPOINTS (b)
46c6471b 15015 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15016 {
15017 breaks_to_delete = 1;
15018 break;
15019 }
1042e4c0
SS
15020
15021 /* Ask user only if there are some breakpoints to delete. */
15022 if (!from_tty
15023 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15024 {
35df4500 15025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15026 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15027 delete_breakpoint (b);
1042e4c0
SS
15028 }
15029 }
15030 else
48649e1b
TT
15031 map_breakpoint_numbers
15032 (arg, [&] (breakpoint *b)
15033 {
15034 iterate_over_related_breakpoints (b, delete_breakpoint);
15035 });
1042e4c0
SS
15036}
15037
197f0a60
TT
15038/* Helper function for trace_pass_command. */
15039
15040static void
d9b3f62e 15041trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15042{
d9b3f62e 15043 tp->pass_count = count;
c1fc2657 15044 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15045 if (from_tty)
15046 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15047 tp->number, count);
197f0a60
TT
15048}
15049
1042e4c0
SS
15050/* Set passcount for tracepoint.
15051
15052 First command argument is passcount, second is tracepoint number.
15053 If tracepoint number omitted, apply to most recently defined.
15054 Also accepts special argument "all". */
15055
15056static void
15057trace_pass_command (char *args, int from_tty)
15058{
d9b3f62e 15059 struct tracepoint *t1;
1042e4c0 15060 unsigned int count;
1042e4c0
SS
15061
15062 if (args == 0 || *args == 0)
3e43a32a
MS
15063 error (_("passcount command requires an "
15064 "argument (count + optional TP num)"));
1042e4c0 15065
4a64f543 15066 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15067
529480d0 15068 args = skip_spaces (args);
1042e4c0
SS
15069 if (*args && strncasecmp (args, "all", 3) == 0)
15070 {
d9b3f62e
PA
15071 struct breakpoint *b;
15072
1042e4c0 15073 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15074 if (*args)
15075 error (_("Junk at end of arguments."));
1042e4c0 15076
d9b3f62e 15077 ALL_TRACEPOINTS (b)
197f0a60 15078 {
d9b3f62e 15079 t1 = (struct tracepoint *) b;
197f0a60
TT
15080 trace_pass_set_count (t1, count, from_tty);
15081 }
15082 }
15083 else if (*args == '\0')
1042e4c0 15084 {
5fa1d40e 15085 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15086 if (t1)
197f0a60
TT
15087 trace_pass_set_count (t1, count, from_tty);
15088 }
15089 else
15090 {
bfd28288
PA
15091 number_or_range_parser parser (args);
15092 while (!parser.finished ())
1042e4c0 15093 {
bfd28288 15094 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15095 if (t1)
15096 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15097 }
15098 }
1042e4c0
SS
15099}
15100
d9b3f62e 15101struct tracepoint *
1042e4c0
SS
15102get_tracepoint (int num)
15103{
15104 struct breakpoint *t;
15105
15106 ALL_TRACEPOINTS (t)
15107 if (t->number == num)
d9b3f62e 15108 return (struct tracepoint *) t;
1042e4c0
SS
15109
15110 return NULL;
15111}
15112
d5551862
SS
15113/* Find the tracepoint with the given target-side number (which may be
15114 different from the tracepoint number after disconnecting and
15115 reconnecting). */
15116
d9b3f62e 15117struct tracepoint *
d5551862
SS
15118get_tracepoint_by_number_on_target (int num)
15119{
d9b3f62e 15120 struct breakpoint *b;
d5551862 15121
d9b3f62e
PA
15122 ALL_TRACEPOINTS (b)
15123 {
15124 struct tracepoint *t = (struct tracepoint *) b;
15125
15126 if (t->number_on_target == num)
15127 return t;
15128 }
d5551862
SS
15129
15130 return NULL;
15131}
15132
1042e4c0 15133/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15134 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15135 If the argument is missing, the most recent tracepoint
15136 (tracepoint_count) is returned. */
15137
d9b3f62e 15138struct tracepoint *
197f0a60 15139get_tracepoint_by_number (char **arg,
bfd28288 15140 number_or_range_parser *parser)
1042e4c0 15141{
1042e4c0
SS
15142 struct breakpoint *t;
15143 int tpnum;
15144 char *instring = arg == NULL ? NULL : *arg;
15145
bfd28288 15146 if (parser != NULL)
197f0a60 15147 {
bfd28288
PA
15148 gdb_assert (!parser->finished ());
15149 tpnum = parser->get_number ();
197f0a60
TT
15150 }
15151 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15152 tpnum = tracepoint_count;
1042e4c0 15153 else
197f0a60 15154 tpnum = get_number (arg);
1042e4c0
SS
15155
15156 if (tpnum <= 0)
15157 {
15158 if (instring && *instring)
15159 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15160 instring);
15161 else
5fa1d40e 15162 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15163 return NULL;
15164 }
15165
15166 ALL_TRACEPOINTS (t)
15167 if (t->number == tpnum)
15168 {
d9b3f62e 15169 return (struct tracepoint *) t;
1042e4c0
SS
15170 }
15171
1042e4c0
SS
15172 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15173 return NULL;
15174}
15175
d9b3f62e
PA
15176void
15177print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15178{
15179 if (b->thread != -1)
15180 fprintf_unfiltered (fp, " thread %d", b->thread);
15181
15182 if (b->task != 0)
15183 fprintf_unfiltered (fp, " task %d", b->task);
15184
15185 fprintf_unfiltered (fp, "\n");
15186}
15187
6149aea9
PA
15188/* Save information on user settable breakpoints (watchpoints, etc) to
15189 a new script file named FILENAME. If FILTER is non-NULL, call it
15190 on each breakpoint and only include the ones for which it returns
15191 non-zero. */
15192
1042e4c0 15193static void
4495129a 15194save_breakpoints (const char *filename, int from_tty,
6149aea9 15195 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15196{
15197 struct breakpoint *tp;
6149aea9 15198 int any = 0;
6149aea9 15199 int extra_trace_bits = 0;
1042e4c0 15200
6149aea9
PA
15201 if (filename == 0 || *filename == 0)
15202 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15203
15204 /* See if we have anything to save. */
6149aea9 15205 ALL_BREAKPOINTS (tp)
1042e4c0 15206 {
6149aea9 15207 /* Skip internal and momentary breakpoints. */
09d682a4 15208 if (!user_breakpoint_p (tp))
6149aea9
PA
15209 continue;
15210
15211 /* If we have a filter, only save the breakpoints it accepts. */
15212 if (filter && !filter (tp))
15213 continue;
15214
15215 any = 1;
15216
15217 if (is_tracepoint (tp))
15218 {
15219 extra_trace_bits = 1;
15220
15221 /* We can stop searching. */
15222 break;
15223 }
1042e4c0 15224 }
6149aea9
PA
15225
15226 if (!any)
1042e4c0 15227 {
6149aea9 15228 warning (_("Nothing to save."));
1042e4c0
SS
15229 return;
15230 }
15231
ee0c3293 15232 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15233
15234 stdio_file fp;
15235
ee0c3293 15236 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15237 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15238 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15239
6149aea9 15240 if (extra_trace_bits)
d7e74731 15241 save_trace_state_variables (&fp);
8bf6485c 15242
6149aea9 15243 ALL_BREAKPOINTS (tp)
1042e4c0 15244 {
6149aea9 15245 /* Skip internal and momentary breakpoints. */
09d682a4 15246 if (!user_breakpoint_p (tp))
6149aea9 15247 continue;
8bf6485c 15248
6149aea9
PA
15249 /* If we have a filter, only save the breakpoints it accepts. */
15250 if (filter && !filter (tp))
15251 continue;
15252
d7e74731 15253 tp->ops->print_recreate (tp, &fp);
1042e4c0 15254
6149aea9
PA
15255 /* Note, we can't rely on tp->number for anything, as we can't
15256 assume the recreated breakpoint numbers will match. Use $bpnum
15257 instead. */
15258
15259 if (tp->cond_string)
d7e74731 15260 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15261
15262 if (tp->ignore_count)
d7e74731 15263 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15264
2d9442cc 15265 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15266 {
d7e74731 15267 fp.puts (" commands\n");
a7bdde9e 15268
d7e74731 15269 current_uiout->redirect (&fp);
492d29ea 15270 TRY
1042e4c0 15271 {
d1b0a7bf 15272 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15273 }
492d29ea
PA
15274 CATCH (ex, RETURN_MASK_ALL)
15275 {
112e8700 15276 current_uiout->redirect (NULL);
492d29ea
PA
15277 throw_exception (ex);
15278 }
15279 END_CATCH
1042e4c0 15280
112e8700 15281 current_uiout->redirect (NULL);
d7e74731 15282 fp.puts (" end\n");
1042e4c0 15283 }
6149aea9
PA
15284
15285 if (tp->enable_state == bp_disabled)
d7e74731 15286 fp.puts ("disable $bpnum\n");
6149aea9
PA
15287
15288 /* If this is a multi-location breakpoint, check if the locations
15289 should be individually disabled. Watchpoint locations are
15290 special, and not user visible. */
15291 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15292 {
15293 struct bp_location *loc;
15294 int n = 1;
15295
15296 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15297 if (!loc->enabled)
d7e74731 15298 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15299 }
1042e4c0 15300 }
8bf6485c 15301
6149aea9 15302 if (extra_trace_bits && *default_collect)
d7e74731 15303 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15304
1042e4c0 15305 if (from_tty)
ee0c3293 15306 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15307}
15308
15309/* The `save breakpoints' command. */
15310
15311static void
4495129a 15312save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15313{
15314 save_breakpoints (args, from_tty, NULL);
15315}
15316
15317/* The `save tracepoints' command. */
15318
15319static void
4495129a 15320save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15321{
15322 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15323}
15324
15325/* Create a vector of all tracepoints. */
15326
15327VEC(breakpoint_p) *
eeae04df 15328all_tracepoints (void)
1042e4c0
SS
15329{
15330 VEC(breakpoint_p) *tp_vec = 0;
15331 struct breakpoint *tp;
15332
15333 ALL_TRACEPOINTS (tp)
15334 {
15335 VEC_safe_push (breakpoint_p, tp_vec, tp);
15336 }
15337
15338 return tp_vec;
15339}
15340
c906108c 15341\f
629500fa
KS
15342/* This help string is used to consolidate all the help string for specifying
15343 locations used by several commands. */
15344
15345#define LOCATION_HELP_STRING \
15346"Linespecs are colon-separated lists of location parameters, such as\n\
15347source filename, function name, label name, and line number.\n\
15348Example: To specify the start of a label named \"the_top\" in the\n\
15349function \"fact\" in the file \"factorial.c\", use\n\
15350\"factorial.c:fact:the_top\".\n\
15351\n\
15352Address locations begin with \"*\" and specify an exact address in the\n\
15353program. Example: To specify the fourth byte past the start function\n\
15354\"main\", use \"*main + 4\".\n\
15355\n\
15356Explicit locations are similar to linespecs but use an option/argument\n\
15357syntax to specify location parameters.\n\
15358Example: To specify the start of the label named \"the_top\" in the\n\
15359function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15360-function fact -label the_top\".\n"
15361
4a64f543
MS
15362/* This help string is used for the break, hbreak, tbreak and thbreak
15363 commands. It is defined as a macro to prevent duplication.
15364 COMMAND should be a string constant containing the name of the
15365 command. */
629500fa 15366
31e2b00f 15367#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15368command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15369PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15370probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15371guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15372`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15373LOCATION may be a linespec, address, or explicit location as described\n\
15374below.\n\
15375\n\
dc10affe
PA
15376With no LOCATION, uses current execution address of the selected\n\
15377stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15378\n\
15379THREADNUM is the number from \"info threads\".\n\
15380CONDITION is a boolean expression.\n\
629500fa 15381\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15382Multiple breakpoints at one place are permitted, and useful if their\n\
15383conditions are different.\n\
31e2b00f
AS
15384\n\
15385Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15386
44feb3ce
TT
15387/* List of subcommands for "catch". */
15388static struct cmd_list_element *catch_cmdlist;
15389
15390/* List of subcommands for "tcatch". */
15391static struct cmd_list_element *tcatch_cmdlist;
15392
9ac4176b 15393void
a121b7c1 15394add_catch_command (const char *name, const char *docstring,
82ae6c8d 15395 cmd_sfunc_ftype *sfunc,
625e8578 15396 completer_ftype *completer,
44feb3ce
TT
15397 void *user_data_catch,
15398 void *user_data_tcatch)
15399{
15400 struct cmd_list_element *command;
15401
0450cc4c 15402 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15403 &catch_cmdlist);
15404 set_cmd_sfunc (command, sfunc);
15405 set_cmd_context (command, user_data_catch);
a96d9b2e 15406 set_cmd_completer (command, completer);
44feb3ce 15407
0450cc4c 15408 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15409 &tcatch_cmdlist);
15410 set_cmd_sfunc (command, sfunc);
15411 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15412 set_cmd_completer (command, completer);
44feb3ce
TT
15413}
15414
6149aea9
PA
15415static void
15416save_command (char *arg, int from_tty)
15417{
3e43a32a
MS
15418 printf_unfiltered (_("\"save\" must be followed by "
15419 "the name of a save subcommand.\n"));
635c7e8a 15420 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15421}
15422
84f4c1fe
PM
15423struct breakpoint *
15424iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15425 void *data)
15426{
35df4500 15427 struct breakpoint *b, *b_tmp;
84f4c1fe 15428
35df4500 15429 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15430 {
15431 if ((*callback) (b, data))
15432 return b;
15433 }
15434
15435 return NULL;
15436}
15437
0574c78f
GB
15438/* Zero if any of the breakpoint's locations could be a location where
15439 functions have been inlined, nonzero otherwise. */
15440
15441static int
15442is_non_inline_function (struct breakpoint *b)
15443{
15444 /* The shared library event breakpoint is set on the address of a
15445 non-inline function. */
15446 if (b->type == bp_shlib_event)
15447 return 1;
15448
15449 return 0;
15450}
15451
15452/* Nonzero if the specified PC cannot be a location where functions
15453 have been inlined. */
15454
15455int
09ac7c10
TT
15456pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15457 const struct target_waitstatus *ws)
0574c78f
GB
15458{
15459 struct breakpoint *b;
15460 struct bp_location *bl;
15461
15462 ALL_BREAKPOINTS (b)
15463 {
15464 if (!is_non_inline_function (b))
15465 continue;
15466
15467 for (bl = b->loc; bl != NULL; bl = bl->next)
15468 {
15469 if (!bl->shlib_disabled
09ac7c10 15470 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15471 return 1;
15472 }
15473 }
15474
15475 return 0;
15476}
15477
2f202fde
JK
15478/* Remove any references to OBJFILE which is going to be freed. */
15479
15480void
15481breakpoint_free_objfile (struct objfile *objfile)
15482{
15483 struct bp_location **locp, *loc;
15484
15485 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15486 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15487 loc->symtab = NULL;
15488}
15489
2060206e
PA
15490void
15491initialize_breakpoint_ops (void)
15492{
15493 static int initialized = 0;
15494
15495 struct breakpoint_ops *ops;
15496
15497 if (initialized)
15498 return;
15499 initialized = 1;
15500
15501 /* The breakpoint_ops structure to be inherit by all kinds of
15502 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15503 internal and momentary breakpoints, etc.). */
15504 ops = &bkpt_base_breakpoint_ops;
15505 *ops = base_breakpoint_ops;
15506 ops->re_set = bkpt_re_set;
15507 ops->insert_location = bkpt_insert_location;
15508 ops->remove_location = bkpt_remove_location;
15509 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15510 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15511 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15512 ops->decode_location = bkpt_decode_location;
2060206e
PA
15513
15514 /* The breakpoint_ops structure to be used in regular breakpoints. */
15515 ops = &bkpt_breakpoint_ops;
15516 *ops = bkpt_base_breakpoint_ops;
15517 ops->re_set = bkpt_re_set;
15518 ops->resources_needed = bkpt_resources_needed;
15519 ops->print_it = bkpt_print_it;
15520 ops->print_mention = bkpt_print_mention;
15521 ops->print_recreate = bkpt_print_recreate;
15522
15523 /* Ranged breakpoints. */
15524 ops = &ranged_breakpoint_ops;
15525 *ops = bkpt_breakpoint_ops;
15526 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15527 ops->resources_needed = resources_needed_ranged_breakpoint;
15528 ops->print_it = print_it_ranged_breakpoint;
15529 ops->print_one = print_one_ranged_breakpoint;
15530 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15531 ops->print_mention = print_mention_ranged_breakpoint;
15532 ops->print_recreate = print_recreate_ranged_breakpoint;
15533
15534 /* Internal breakpoints. */
15535 ops = &internal_breakpoint_ops;
15536 *ops = bkpt_base_breakpoint_ops;
15537 ops->re_set = internal_bkpt_re_set;
15538 ops->check_status = internal_bkpt_check_status;
15539 ops->print_it = internal_bkpt_print_it;
15540 ops->print_mention = internal_bkpt_print_mention;
15541
15542 /* Momentary breakpoints. */
15543 ops = &momentary_breakpoint_ops;
15544 *ops = bkpt_base_breakpoint_ops;
15545 ops->re_set = momentary_bkpt_re_set;
15546 ops->check_status = momentary_bkpt_check_status;
15547 ops->print_it = momentary_bkpt_print_it;
15548 ops->print_mention = momentary_bkpt_print_mention;
15549
55aa24fb
SDJ
15550 /* Probe breakpoints. */
15551 ops = &bkpt_probe_breakpoint_ops;
15552 *ops = bkpt_breakpoint_ops;
15553 ops->insert_location = bkpt_probe_insert_location;
15554 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15555 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15556 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15557
2060206e
PA
15558 /* Watchpoints. */
15559 ops = &watchpoint_breakpoint_ops;
15560 *ops = base_breakpoint_ops;
15561 ops->re_set = re_set_watchpoint;
15562 ops->insert_location = insert_watchpoint;
15563 ops->remove_location = remove_watchpoint;
15564 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15565 ops->check_status = check_status_watchpoint;
15566 ops->resources_needed = resources_needed_watchpoint;
15567 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15568 ops->print_it = print_it_watchpoint;
15569 ops->print_mention = print_mention_watchpoint;
15570 ops->print_recreate = print_recreate_watchpoint;
427cd150 15571 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15572
15573 /* Masked watchpoints. */
15574 ops = &masked_watchpoint_breakpoint_ops;
15575 *ops = watchpoint_breakpoint_ops;
15576 ops->insert_location = insert_masked_watchpoint;
15577 ops->remove_location = remove_masked_watchpoint;
15578 ops->resources_needed = resources_needed_masked_watchpoint;
15579 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15580 ops->print_it = print_it_masked_watchpoint;
15581 ops->print_one_detail = print_one_detail_masked_watchpoint;
15582 ops->print_mention = print_mention_masked_watchpoint;
15583 ops->print_recreate = print_recreate_masked_watchpoint;
15584
15585 /* Tracepoints. */
15586 ops = &tracepoint_breakpoint_ops;
15587 *ops = base_breakpoint_ops;
15588 ops->re_set = tracepoint_re_set;
15589 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15590 ops->print_one_detail = tracepoint_print_one_detail;
15591 ops->print_mention = tracepoint_print_mention;
15592 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15593 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15594 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15595 ops->decode_location = tracepoint_decode_location;
983af33b 15596
55aa24fb
SDJ
15597 /* Probe tracepoints. */
15598 ops = &tracepoint_probe_breakpoint_ops;
15599 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15600 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15601 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15602
983af33b
SDJ
15603 /* Static tracepoints with marker (`-m'). */
15604 ops = &strace_marker_breakpoint_ops;
15605 *ops = tracepoint_breakpoint_ops;
5f700d83 15606 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15607 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15608 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15609
15610 /* Fork catchpoints. */
15611 ops = &catch_fork_breakpoint_ops;
15612 *ops = base_breakpoint_ops;
15613 ops->insert_location = insert_catch_fork;
15614 ops->remove_location = remove_catch_fork;
15615 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15616 ops->print_it = print_it_catch_fork;
15617 ops->print_one = print_one_catch_fork;
15618 ops->print_mention = print_mention_catch_fork;
15619 ops->print_recreate = print_recreate_catch_fork;
15620
15621 /* Vfork catchpoints. */
15622 ops = &catch_vfork_breakpoint_ops;
15623 *ops = base_breakpoint_ops;
15624 ops->insert_location = insert_catch_vfork;
15625 ops->remove_location = remove_catch_vfork;
15626 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15627 ops->print_it = print_it_catch_vfork;
15628 ops->print_one = print_one_catch_vfork;
15629 ops->print_mention = print_mention_catch_vfork;
15630 ops->print_recreate = print_recreate_catch_vfork;
15631
15632 /* Exec catchpoints. */
15633 ops = &catch_exec_breakpoint_ops;
15634 *ops = base_breakpoint_ops;
2060206e
PA
15635 ops->insert_location = insert_catch_exec;
15636 ops->remove_location = remove_catch_exec;
15637 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15638 ops->print_it = print_it_catch_exec;
15639 ops->print_one = print_one_catch_exec;
15640 ops->print_mention = print_mention_catch_exec;
15641 ops->print_recreate = print_recreate_catch_exec;
15642
edcc5120
TT
15643 /* Solib-related catchpoints. */
15644 ops = &catch_solib_breakpoint_ops;
15645 *ops = base_breakpoint_ops;
edcc5120
TT
15646 ops->insert_location = insert_catch_solib;
15647 ops->remove_location = remove_catch_solib;
15648 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15649 ops->check_status = check_status_catch_solib;
15650 ops->print_it = print_it_catch_solib;
15651 ops->print_one = print_one_catch_solib;
15652 ops->print_mention = print_mention_catch_solib;
15653 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15654
15655 ops = &dprintf_breakpoint_ops;
15656 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15657 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15658 ops->resources_needed = bkpt_resources_needed;
15659 ops->print_it = bkpt_print_it;
15660 ops->print_mention = bkpt_print_mention;
2d9442cc 15661 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15662 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15663 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15664}
15665
8bfd80db
YQ
15666/* Chain containing all defined "enable breakpoint" subcommands. */
15667
15668static struct cmd_list_element *enablebreaklist = NULL;
15669
c906108c 15670void
fba45db2 15671_initialize_breakpoint (void)
c906108c
SS
15672{
15673 struct cmd_list_element *c;
15674
2060206e
PA
15675 initialize_breakpoint_ops ();
15676
84acb35a 15677 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15678 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15679 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15680
55aa24fb
SDJ
15681 breakpoint_objfile_key
15682 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15683
c906108c
SS
15684 breakpoint_chain = 0;
15685 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15686 before a breakpoint is set. */
15687 breakpoint_count = 0;
15688
1042e4c0
SS
15689 tracepoint_count = 0;
15690
1bedd215
AC
15691 add_com ("ignore", class_breakpoint, ignore_command, _("\
15692Set ignore-count of breakpoint number N to COUNT.\n\
15693Usage is `ignore N COUNT'."));
c906108c 15694
1bedd215 15695 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15696Set commands to be executed when the given breakpoints are hit.\n\
15697Give a space-separated breakpoint list as argument after \"commands\".\n\
15698A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15699(e.g. `5-7').\n\
c906108c
SS
15700With no argument, the targeted breakpoint is the last one set.\n\
15701The commands themselves follow starting on the next line.\n\
15702Type a line containing \"end\" to indicate the end of them.\n\
15703Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15704then no output is printed when it is hit, except what the commands print."));
c906108c 15705
d55637df 15706 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15707Specify breakpoint number N to break only if COND is true.\n\
c906108c 15708Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15709expression to be evaluated whenever breakpoint N is reached."));
d55637df 15710 set_cmd_completer (c, condition_completer);
c906108c 15711
1bedd215 15712 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15713Set a temporary breakpoint.\n\
c906108c
SS
15714Like \"break\" except the breakpoint is only temporary,\n\
15715so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15716by using \"enable delete\" on the breakpoint number.\n\
15717\n"
15718BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15719 set_cmd_completer (c, location_completer);
c94fdfd0 15720
1bedd215 15721 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15722Set a hardware assisted breakpoint.\n\
c906108c 15723Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15724some target hardware may not have this support.\n\
15725\n"
15726BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15727 set_cmd_completer (c, location_completer);
c906108c 15728
1bedd215 15729 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15730Set a temporary hardware assisted breakpoint.\n\
c906108c 15731Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15732so it will be deleted when hit.\n\
15733\n"
15734BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15735 set_cmd_completer (c, location_completer);
c906108c 15736
1bedd215
AC
15737 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15738Enable some breakpoints.\n\
c906108c
SS
15739Give breakpoint numbers (separated by spaces) as arguments.\n\
15740With no subcommand, breakpoints are enabled until you command otherwise.\n\
15741This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15742With a subcommand you can enable temporarily."),
c906108c 15743 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15744
15745 add_com_alias ("en", "enable", class_breakpoint, 1);
15746
84951ab5 15747 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15748Enable some breakpoints.\n\
c906108c
SS
15749Give breakpoint numbers (separated by spaces) as arguments.\n\
15750This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15751May be abbreviated to simply \"enable\".\n"),
c5aa993b 15752 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15753
1a966eab
AC
15754 add_cmd ("once", no_class, enable_once_command, _("\
15755Enable breakpoints for one hit. Give breakpoint numbers.\n\
15756If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15757 &enablebreaklist);
15758
1a966eab
AC
15759 add_cmd ("delete", no_class, enable_delete_command, _("\
15760Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15761If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15762 &enablebreaklist);
15763
816338b5
SS
15764 add_cmd ("count", no_class, enable_count_command, _("\
15765Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15766If a breakpoint is hit while enabled in this fashion,\n\
15767the count is decremented; when it reaches zero, the breakpoint is disabled."),
15768 &enablebreaklist);
15769
1a966eab
AC
15770 add_cmd ("delete", no_class, enable_delete_command, _("\
15771Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15772If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15773 &enablelist);
15774
1a966eab
AC
15775 add_cmd ("once", no_class, enable_once_command, _("\
15776Enable breakpoints for one hit. Give breakpoint numbers.\n\
15777If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15778 &enablelist);
15779
15780 add_cmd ("count", no_class, enable_count_command, _("\
15781Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15782If a breakpoint is hit while enabled in this fashion,\n\
15783the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15784 &enablelist);
15785
1bedd215
AC
15786 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15787Disable some breakpoints.\n\
c906108c
SS
15788Arguments are breakpoint numbers with spaces in between.\n\
15789To disable all breakpoints, give no argument.\n\
64b9b334 15790A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15791 &disablelist, "disable ", 1, &cmdlist);
15792 add_com_alias ("dis", "disable", class_breakpoint, 1);
15793 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15794
1a966eab
AC
15795 add_cmd ("breakpoints", class_alias, disable_command, _("\
15796Disable some breakpoints.\n\
c906108c
SS
15797Arguments are breakpoint numbers with spaces in between.\n\
15798To disable all breakpoints, give no argument.\n\
64b9b334 15799A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15800This command may be abbreviated \"disable\"."),
c906108c
SS
15801 &disablelist);
15802
1bedd215
AC
15803 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15804Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15805Arguments are breakpoint numbers with spaces in between.\n\
15806To delete all breakpoints, give no argument.\n\
15807\n\
15808Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15809The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15810 &deletelist, "delete ", 1, &cmdlist);
15811 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15812 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15813
1a966eab
AC
15814 add_cmd ("breakpoints", class_alias, delete_command, _("\
15815Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15816Arguments are breakpoint numbers with spaces in between.\n\
15817To delete all breakpoints, give no argument.\n\
1a966eab 15818This command may be abbreviated \"delete\"."),
c906108c
SS
15819 &deletelist);
15820
1bedd215 15821 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15822Clear breakpoint at specified location.\n\
15823Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15824\n\
15825With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15826is executing in.\n"
15827"\n" LOCATION_HELP_STRING "\n\
1bedd215 15828See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15829 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15830
1bedd215 15831 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15832Set breakpoint at specified location.\n"
31e2b00f 15833BREAK_ARGS_HELP ("break")));
5ba2abeb 15834 set_cmd_completer (c, location_completer);
c94fdfd0 15835
c906108c
SS
15836 add_com_alias ("b", "break", class_run, 1);
15837 add_com_alias ("br", "break", class_run, 1);
15838 add_com_alias ("bre", "break", class_run, 1);
15839 add_com_alias ("brea", "break", class_run, 1);
15840
c906108c
SS
15841 if (dbx_commands)
15842 {
1bedd215
AC
15843 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15844Break in function/address or break at a line in the current file."),
c5aa993b
JM
15845 &stoplist, "stop ", 1, &cmdlist);
15846 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15847 _("Break in function or address."), &stoplist);
c5aa993b 15848 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15849 _("Break at a line in the current file."), &stoplist);
11db9430 15850 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15851Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15852The \"Type\" column indicates one of:\n\
15853\tbreakpoint - normal breakpoint\n\
15854\twatchpoint - watchpoint\n\
15855The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15856the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15857breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15858address and file/line number respectively.\n\
15859\n\
15860Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15861are set to the address of the last breakpoint listed unless the command\n\
15862is prefixed with \"server \".\n\n\
c906108c 15863Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15864breakpoint set."));
c906108c
SS
15865 }
15866
11db9430 15867 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15868Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15869The \"Type\" column indicates one of:\n\
15870\tbreakpoint - normal breakpoint\n\
15871\twatchpoint - watchpoint\n\
15872The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15873the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15874breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15875address and file/line number respectively.\n\
15876\n\
15877Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15878are set to the address of the last breakpoint listed unless the command\n\
15879is prefixed with \"server \".\n\n\
c906108c 15880Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15881breakpoint set."));
c906108c 15882
6b04bdb7
MS
15883 add_info_alias ("b", "breakpoints", 1);
15884
1a966eab
AC
15885 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15886Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15887The \"Type\" column indicates one of:\n\
15888\tbreakpoint - normal breakpoint\n\
15889\twatchpoint - watchpoint\n\
15890\tlongjmp - internal breakpoint used to step through longjmp()\n\
15891\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15892\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15893\tfinish - internal breakpoint used by the \"finish\" command\n\
15894The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15895the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15896breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15897address and file/line number respectively.\n\
15898\n\
15899Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15900are set to the address of the last breakpoint listed unless the command\n\
15901is prefixed with \"server \".\n\n\
c906108c 15902Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15903breakpoint set."),
c906108c
SS
15904 &maintenanceinfolist);
15905
44feb3ce
TT
15906 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15907Set catchpoints to catch events."),
15908 &catch_cmdlist, "catch ",
15909 0/*allow-unknown*/, &cmdlist);
15910
15911 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15912Set temporary catchpoints to catch events."),
15913 &tcatch_cmdlist, "tcatch ",
15914 0/*allow-unknown*/, &cmdlist);
15915
44feb3ce
TT
15916 add_catch_command ("fork", _("Catch calls to fork."),
15917 catch_fork_command_1,
a96d9b2e 15918 NULL,
44feb3ce
TT
15919 (void *) (uintptr_t) catch_fork_permanent,
15920 (void *) (uintptr_t) catch_fork_temporary);
15921 add_catch_command ("vfork", _("Catch calls to vfork."),
15922 catch_fork_command_1,
a96d9b2e 15923 NULL,
44feb3ce
TT
15924 (void *) (uintptr_t) catch_vfork_permanent,
15925 (void *) (uintptr_t) catch_vfork_temporary);
15926 add_catch_command ("exec", _("Catch calls to exec."),
15927 catch_exec_command_1,
a96d9b2e
SDJ
15928 NULL,
15929 CATCH_PERMANENT,
15930 CATCH_TEMPORARY);
edcc5120
TT
15931 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15932Usage: catch load [REGEX]\n\
15933If REGEX is given, only stop for libraries matching the regular expression."),
15934 catch_load_command_1,
15935 NULL,
15936 CATCH_PERMANENT,
15937 CATCH_TEMPORARY);
15938 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15939Usage: catch unload [REGEX]\n\
15940If REGEX is given, only stop for libraries matching the regular expression."),
15941 catch_unload_command_1,
15942 NULL,
15943 CATCH_PERMANENT,
15944 CATCH_TEMPORARY);
c5aa993b 15945
1bedd215
AC
15946 c = add_com ("watch", class_breakpoint, watch_command, _("\
15947Set a watchpoint for an expression.\n\
06a64a0b 15948Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15949A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15950an expression changes.\n\
15951If -l or -location is given, this evaluates EXPRESSION and watches\n\
15952the memory to which it refers."));
65d12d83 15953 set_cmd_completer (c, expression_completer);
c906108c 15954
1bedd215
AC
15955 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15956Set a read watchpoint for an expression.\n\
06a64a0b 15957Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15958A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15959an expression is read.\n\
15960If -l or -location is given, this evaluates EXPRESSION and watches\n\
15961the memory to which it refers."));
65d12d83 15962 set_cmd_completer (c, expression_completer);
c906108c 15963
1bedd215
AC
15964 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15965Set a watchpoint for an expression.\n\
06a64a0b 15966Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15967A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15968an expression is either read or written.\n\
15969If -l or -location is given, this evaluates EXPRESSION and watches\n\
15970the memory to which it refers."));
65d12d83 15971 set_cmd_completer (c, expression_completer);
c906108c 15972
11db9430 15973 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15974Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15975
920d2a44
AC
15976 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15977 respond to changes - contrary to the description. */
85c07804
AC
15978 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15979 &can_use_hw_watchpoints, _("\
15980Set debugger's willingness to use watchpoint hardware."), _("\
15981Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15982If zero, gdb will not use hardware for new watchpoints, even if\n\
15983such is available. (However, any hardware watchpoints that were\n\
15984created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15985hardware.)"),
15986 NULL,
920d2a44 15987 show_can_use_hw_watchpoints,
85c07804 15988 &setlist, &showlist);
c906108c
SS
15989
15990 can_use_hw_watchpoints = 1;
fa8d40ab 15991
1042e4c0
SS
15992 /* Tracepoint manipulation commands. */
15993
15994 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15995Set a tracepoint at specified location.\n\
1042e4c0
SS
15996\n"
15997BREAK_ARGS_HELP ("trace") "\n\
15998Do \"help tracepoints\" for info on other tracepoint commands."));
15999 set_cmd_completer (c, location_completer);
16000
16001 add_com_alias ("tp", "trace", class_alias, 0);
16002 add_com_alias ("tr", "trace", class_alias, 1);
16003 add_com_alias ("tra", "trace", class_alias, 1);
16004 add_com_alias ("trac", "trace", class_alias, 1);
16005
7a697b8d 16006 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16007Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16008\n"
16009BREAK_ARGS_HELP ("ftrace") "\n\
16010Do \"help tracepoints\" for info on other tracepoint commands."));
16011 set_cmd_completer (c, location_completer);
16012
0fb4aa4b 16013 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16014Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16015\n\
16016strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16017LOCATION may be a linespec, explicit, or address location (described below) \n\
16018or -m MARKER_ID.\n\n\
16019If a marker id is specified, probe the marker with that name. With\n\
16020no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16021Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16022This collects arbitrary user data passed in the probe point call to the\n\
16023tracing library. You can inspect it when analyzing the trace buffer,\n\
16024by printing the $_sdata variable like any other convenience variable.\n\
16025\n\
16026CONDITION is a boolean expression.\n\
629500fa 16027\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16028Multiple tracepoints at one place are permitted, and useful if their\n\
16029conditions are different.\n\
0fb4aa4b
PA
16030\n\
16031Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16032Do \"help tracepoints\" for info on other tracepoint commands."));
16033 set_cmd_completer (c, location_completer);
16034
11db9430 16035 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 16036Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16037Convenience variable \"$tpnum\" contains the number of the\n\
16038last tracepoint set."));
16039
16040 add_info_alias ("tp", "tracepoints", 1);
16041
16042 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16043Delete specified tracepoints.\n\
16044Arguments are tracepoint numbers, separated by spaces.\n\
16045No argument means delete all tracepoints."),
16046 &deletelist);
7e20dfcd 16047 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16048
16049 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16050Disable specified tracepoints.\n\
16051Arguments are tracepoint numbers, separated by spaces.\n\
16052No argument means disable all tracepoints."),
16053 &disablelist);
16054 deprecate_cmd (c, "disable");
16055
16056 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16057Enable specified tracepoints.\n\
16058Arguments are tracepoint numbers, separated by spaces.\n\
16059No argument means enable all tracepoints."),
16060 &enablelist);
16061 deprecate_cmd (c, "enable");
16062
16063 add_com ("passcount", class_trace, trace_pass_command, _("\
16064Set the passcount for a tracepoint.\n\
16065The trace will end when the tracepoint has been passed 'count' times.\n\
16066Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16067if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16068
6149aea9
PA
16069 add_prefix_cmd ("save", class_breakpoint, save_command,
16070 _("Save breakpoint definitions as a script."),
16071 &save_cmdlist, "save ",
16072 0/*allow-unknown*/, &cmdlist);
16073
16074 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16075Save current breakpoint definitions as a script.\n\
cce7e648 16076This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16077catchpoints, tracepoints). Use the 'source' command in another debug\n\
16078session to restore them."),
16079 &save_cmdlist);
16080 set_cmd_completer (c, filename_completer);
16081
16082 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16083Save current tracepoint definitions as a script.\n\
6149aea9
PA
16084Use the 'source' command in another debug session to restore them."),
16085 &save_cmdlist);
1042e4c0
SS
16086 set_cmd_completer (c, filename_completer);
16087
6149aea9
PA
16088 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16089 deprecate_cmd (c, "save tracepoints");
16090
1bedd215 16091 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16092Breakpoint specific settings\n\
16093Configure various breakpoint-specific variables such as\n\
1bedd215 16094pending breakpoint behavior"),
fa8d40ab
JJ
16095 &breakpoint_set_cmdlist, "set breakpoint ",
16096 0/*allow-unknown*/, &setlist);
1bedd215 16097 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16098Breakpoint specific settings\n\
16099Configure various breakpoint-specific variables such as\n\
1bedd215 16100pending breakpoint behavior"),
fa8d40ab
JJ
16101 &breakpoint_show_cmdlist, "show breakpoint ",
16102 0/*allow-unknown*/, &showlist);
16103
7915a72c
AC
16104 add_setshow_auto_boolean_cmd ("pending", no_class,
16105 &pending_break_support, _("\
16106Set debugger's behavior regarding pending breakpoints."), _("\
16107Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16108If on, an unrecognized breakpoint location will cause gdb to create a\n\
16109pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16110an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16111user-query to see if a pending breakpoint should be created."),
2c5b56ce 16112 NULL,
920d2a44 16113 show_pending_break_support,
6e1d7d6c
AC
16114 &breakpoint_set_cmdlist,
16115 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16116
16117 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16118
16119 add_setshow_boolean_cmd ("auto-hw", no_class,
16120 &automatic_hardware_breakpoints, _("\
16121Set automatic usage of hardware breakpoints."), _("\
16122Show automatic usage of hardware breakpoints."), _("\
16123If set, the debugger will automatically use hardware breakpoints for\n\
16124breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16125a warning will be emitted for such breakpoints."),
16126 NULL,
16127 show_automatic_hardware_breakpoints,
16128 &breakpoint_set_cmdlist,
16129 &breakpoint_show_cmdlist);
74960c60 16130
a25a5a45
PA
16131 add_setshow_boolean_cmd ("always-inserted", class_support,
16132 &always_inserted_mode, _("\
74960c60
VP
16133Set mode for inserting breakpoints."), _("\
16134Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16135When this mode is on, breakpoints are inserted immediately as soon as\n\
16136they're created, kept inserted even when execution stops, and removed\n\
16137only when the user deletes them. When this mode is off (the default),\n\
16138breakpoints are inserted only when execution continues, and removed\n\
16139when execution stops."),
72d0e2c5
YQ
16140 NULL,
16141 &show_always_inserted_mode,
16142 &breakpoint_set_cmdlist,
16143 &breakpoint_show_cmdlist);
f1310107 16144
b775012e
LM
16145 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16146 condition_evaluation_enums,
16147 &condition_evaluation_mode_1, _("\
16148Set mode of breakpoint condition evaluation."), _("\
16149Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16150When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16151evaluated on the host's side by GDB. When it is set to \"target\",\n\
16152breakpoint conditions will be downloaded to the target (if the target\n\
16153supports such feature) and conditions will be evaluated on the target's side.\n\
16154If this is set to \"auto\" (default), this will be automatically set to\n\
16155\"target\" if it supports condition evaluation, otherwise it will\n\
16156be set to \"gdb\""),
16157 &set_condition_evaluation_mode,
16158 &show_condition_evaluation_mode,
16159 &breakpoint_set_cmdlist,
16160 &breakpoint_show_cmdlist);
16161
f1310107
TJB
16162 add_com ("break-range", class_breakpoint, break_range_command, _("\
16163Set a breakpoint for an address range.\n\
16164break-range START-LOCATION, END-LOCATION\n\
16165where START-LOCATION and END-LOCATION can be one of the following:\n\
16166 LINENUM, for that line in the current file,\n\
16167 FILE:LINENUM, for that line in that file,\n\
16168 +OFFSET, for that number of lines after the current line\n\
16169 or the start of the range\n\
16170 FUNCTION, for the first line in that function,\n\
16171 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16172 *ADDRESS, for the instruction at that address.\n\
16173\n\
16174The breakpoint will stop execution of the inferior whenever it executes\n\
16175an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16176range (including START-LOCATION and END-LOCATION)."));
16177
e7e0cddf 16178 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16179Set a dynamic printf at specified location.\n\
e7e0cddf 16180dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16181location may be a linespec, explicit, or address location.\n"
16182"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16183 set_cmd_completer (c, location_completer);
16184
16185 add_setshow_enum_cmd ("dprintf-style", class_support,
16186 dprintf_style_enums, &dprintf_style, _("\
16187Set the style of usage for dynamic printf."), _("\
16188Show the style of usage for dynamic printf."), _("\
16189This setting chooses how GDB will do a dynamic printf.\n\
16190If the value is \"gdb\", then the printing is done by GDB to its own\n\
16191console, as with the \"printf\" command.\n\
16192If the value is \"call\", the print is done by calling a function in your\n\
16193program; by default printf(), but you can choose a different function or\n\
16194output stream by setting dprintf-function and dprintf-channel."),
16195 update_dprintf_commands, NULL,
16196 &setlist, &showlist);
16197
16198 dprintf_function = xstrdup ("printf");
16199 add_setshow_string_cmd ("dprintf-function", class_support,
16200 &dprintf_function, _("\
16201Set the function to use for dynamic printf"), _("\
16202Show the function to use for dynamic printf"), NULL,
16203 update_dprintf_commands, NULL,
16204 &setlist, &showlist);
16205
16206 dprintf_channel = xstrdup ("");
16207 add_setshow_string_cmd ("dprintf-channel", class_support,
16208 &dprintf_channel, _("\
16209Set the channel to use for dynamic printf"), _("\
16210Show the channel to use for dynamic printf"), NULL,
16211 update_dprintf_commands, NULL,
16212 &setlist, &showlist);
16213
d3ce09f5
SS
16214 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16215 &disconnected_dprintf, _("\
16216Set whether dprintf continues after GDB disconnects."), _("\
16217Show whether dprintf continues after GDB disconnects."), _("\
16218Use this to let dprintf commands continue to hit and produce output\n\
16219even if GDB disconnects or detaches from the target."),
16220 NULL,
16221 NULL,
16222 &setlist, &showlist);
16223
16224 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16225agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16226(target agent only) This is useful for formatted output in user-defined commands."));
16227
765dc015 16228 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16229
16230 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16231 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16232}
This page took 3.215565 seconds and 4 git commands to generate.