Add native target for FreeBSD/arm.
[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"
8b93c638 47#include "ui-out.h"
e1507482 48#include "cli/cli-script.h"
fe898f56 49#include "block.h"
a77053c2 50#include "solib.h"
84acb35a
JJ
51#include "solist.h"
52#include "observer.h"
765dc015 53#include "memattr.h"
f7f9143b 54#include "ada-lang.h"
d1aa2f50 55#include "top.h"
79a45b7d 56#include "valprint.h"
4efc6507 57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
55aa24fb
SDJ
59#include "gdb_regex.h"
60#include "probe.h"
e9cafbcc 61#include "cli/cli-utils.h"
be34f849 62#include "continuations.h"
1bfeeb0f
JL
63#include "stack.h"
64#include "skip.h"
b775012e 65#include "ax-gdb.h"
e2e4d78b 66#include "dummy-frame.h"
5589af0e 67#include "interps.h"
d3ce09f5 68#include "format.h"
cfc31633 69#include "thread-fsm.h"
5d5658a1 70#include "tid-parse.h"
d3ce09f5 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
325fac50 81#include <algorithm>
5ed8105e 82#include "progspace-and-thread.h"
6c5b2ebe 83#include "common/array-view.h"
76f9c9cf 84#include "common/gdb_optional.h"
104c1213 85
e7e8980f
YQ
86/* Enums for exception-handling support. */
87enum exception_event_kind
88{
89 EX_EVENT_THROW,
591f19e8 90 EX_EVENT_RETHROW,
e7e8980f
YQ
91 EX_EVENT_CATCH
92};
93
4a64f543 94/* Prototypes for local functions. */
c906108c 95
a14ed312 96static void disable_command (char *, int);
c906108c 97
a14ed312 98static void enable_command (char *, int);
c906108c 99
896b6bda 100static void map_breakpoint_numbers (const char *,
48649e1b 101 gdb::function_view<void (breakpoint *)>);
c906108c 102
a14ed312 103static void ignore_command (char *, int);
c906108c 104
348d480f
PA
105static void breakpoint_re_set_default (struct breakpoint *);
106
f00aae0f
KS
107static void
108 create_sals_from_location_default (const struct event_location *location,
109 struct linespec_result *canonical,
110 enum bptype type_wanted);
983af33b
SDJ
111
112static void create_breakpoints_sal_default (struct gdbarch *,
113 struct linespec_result *,
e1e01040
PA
114 gdb::unique_xmalloc_ptr<char>,
115 gdb::unique_xmalloc_ptr<char>,
116 enum bptype,
983af33b
SDJ
117 enum bpdisp, int, int,
118 int,
119 const struct breakpoint_ops *,
44f238bb 120 int, int, int, unsigned);
983af33b 121
6c5b2ebe
PA
122static std::vector<symtab_and_line> decode_location_default
123 (struct breakpoint *b, const struct event_location *location,
124 struct program_space *search_pspace);
983af33b 125
a14ed312 126static void clear_command (char *, int);
c906108c 127
a14ed312 128static void catch_command (char *, int);
c906108c 129
a9634178 130static int can_use_hardware_watchpoint (struct value *);
c906108c 131
a14ed312 132static void mention (struct breakpoint *);
c906108c 133
348d480f
PA
134static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
135 enum bptype,
c0a91b2b 136 const struct breakpoint_ops *);
3742cc8b
YQ
137static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
138 const struct symtab_and_line *);
139
4a64f543
MS
140/* This function is used in gdbtk sources and thus can not be made
141 static. */
63c252f8 142struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 143 struct symtab_and_line,
c0a91b2b
TT
144 enum bptype,
145 const struct breakpoint_ops *);
c906108c 146
06edf0c0
PA
147static struct breakpoint *
148 momentary_breakpoint_from_master (struct breakpoint *orig,
149 enum bptype type,
a1aa2221
LM
150 const struct breakpoint_ops *ops,
151 int loc_enabled);
06edf0c0 152
76897487
KB
153static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
154
a6d9a66e
UW
155static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
156 CORE_ADDR bpaddr,
88f7da05 157 enum bptype bptype);
76897487 158
6c95b8df
PA
159static void describe_other_breakpoints (struct gdbarch *,
160 struct program_space *, CORE_ADDR,
5af949e3 161 struct obj_section *, int);
c906108c 162
85d721b8
PA
163static int watchpoint_locations_match (struct bp_location *loc1,
164 struct bp_location *loc2);
165
f1310107
TJB
166static int breakpoint_location_address_match (struct bp_location *bl,
167 struct address_space *aspace,
168 CORE_ADDR addr);
169
d35ae833
PA
170static int breakpoint_location_address_range_overlap (struct bp_location *,
171 struct address_space *,
172 CORE_ADDR, int);
173
11db9430 174static void info_breakpoints_command (char *, int);
c906108c 175
11db9430 176static void info_watchpoints_command (char *, int);
d77f58be 177
4efb68b1 178static void cleanup_executing_breakpoints (void *);
c906108c 179
a14ed312 180static void commands_command (char *, int);
c906108c 181
a14ed312 182static void condition_command (char *, int);
c906108c 183
834c0d03 184static int remove_breakpoint (struct bp_location *);
b2b6a7da 185static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 186
e514a9d6 187static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 188
a14ed312 189static int hw_breakpoint_used_count (void);
c906108c 190
a1398e0c
PA
191static int hw_watchpoint_use_count (struct breakpoint *);
192
193static int hw_watchpoint_used_count_others (struct breakpoint *except,
194 enum bptype type,
195 int *other_type_used);
c906108c 196
a14ed312 197static void hbreak_command (char *, int);
c906108c 198
a14ed312 199static void thbreak_command (char *, int);
c906108c 200
816338b5
SS
201static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
202 int count);
c906108c 203
a14ed312 204static void stop_command (char *arg, int from_tty);
7a292a7a 205
a14ed312 206static void tcatch_command (char *arg, int from_tty);
7a292a7a 207
fe3f5fa8 208static void free_bp_location (struct bp_location *loc);
f431efe5
PA
209static void incref_bp_location (struct bp_location *loc);
210static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 211
39d61571 212static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 213
44702360
PA
214/* update_global_location_list's modes of operation wrt to whether to
215 insert locations now. */
216enum ugll_insert_mode
217{
218 /* Don't insert any breakpoint locations into the inferior, only
219 remove already-inserted locations that no longer should be
220 inserted. Functions that delete a breakpoint or breakpoints
221 should specify this mode, so that deleting a breakpoint doesn't
222 have the side effect of inserting the locations of other
223 breakpoints that are marked not-inserted, but should_be_inserted
224 returns true on them.
225
226 This behavior is useful is situations close to tear-down -- e.g.,
227 after an exec, while the target still has execution, but
228 breakpoint shadows of the previous executable image should *NOT*
229 be restored to the new image; or before detaching, where the
230 target still has execution and wants to delete breakpoints from
231 GDB's lists, and all breakpoints had already been removed from
232 the inferior. */
233 UGLL_DONT_INSERT,
234
a25a5a45
PA
235 /* May insert breakpoints iff breakpoints_should_be_inserted_now
236 claims breakpoints should be inserted now. */
04086b45
PA
237 UGLL_MAY_INSERT,
238
a25a5a45
PA
239 /* Insert locations now, irrespective of
240 breakpoints_should_be_inserted_now. E.g., say all threads are
241 stopped right now, and the user did "continue". We need to
242 insert breakpoints _before_ resuming the target, but
243 UGLL_MAY_INSERT wouldn't insert them, because
244 breakpoints_should_be_inserted_now returns false at that point,
245 as no thread is running yet. */
04086b45 246 UGLL_INSERT
44702360
PA
247};
248
249static void update_global_location_list (enum ugll_insert_mode);
a5606eee 250
44702360 251static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 252
d77f58be 253static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
254
255static void insert_breakpoint_locations (void);
a5606eee 256
11db9430 257static void info_tracepoints_command (char *, int);
1042e4c0 258
1042e4c0
SS
259static void enable_trace_command (char *, int);
260
261static void disable_trace_command (char *, int);
262
263static void trace_pass_command (char *, int);
264
558a9d82
YQ
265static void set_tracepoint_count (int num);
266
9c06b0b4
TJB
267static int is_masked_watchpoint (const struct breakpoint *b);
268
b775012e
LM
269static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
270
983af33b
SDJ
271/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
272 otherwise. */
273
274static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 275
2060206e
PA
276/* The breakpoint_ops structure to be inherited by all breakpoint_ops
277 that are implemented on top of software or hardware breakpoints
278 (user breakpoints, internal and momentary breakpoints, etc.). */
279static struct breakpoint_ops bkpt_base_breakpoint_ops;
280
281/* Internal breakpoints class type. */
06edf0c0 282static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
283
284/* Momentary breakpoints class type. */
06edf0c0
PA
285static struct breakpoint_ops momentary_breakpoint_ops;
286
2060206e
PA
287/* The breakpoint_ops structure to be used in regular user created
288 breakpoints. */
289struct breakpoint_ops bkpt_breakpoint_ops;
290
55aa24fb
SDJ
291/* Breakpoints set on probes. */
292static struct breakpoint_ops bkpt_probe_breakpoint_ops;
293
e7e0cddf 294/* Dynamic printf class type. */
c5867ab6 295struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 296
d3ce09f5
SS
297/* The style in which to perform a dynamic printf. This is a user
298 option because different output options have different tradeoffs;
299 if GDB does the printing, there is better error handling if there
300 is a problem with any of the arguments, but using an inferior
301 function lets you have special-purpose printers and sending of
302 output to the same place as compiled-in print functions. */
303
304static const char dprintf_style_gdb[] = "gdb";
305static const char dprintf_style_call[] = "call";
306static const char dprintf_style_agent[] = "agent";
307static const char *const dprintf_style_enums[] = {
308 dprintf_style_gdb,
309 dprintf_style_call,
310 dprintf_style_agent,
311 NULL
312};
313static const char *dprintf_style = dprintf_style_gdb;
314
315/* The function to use for dynamic printf if the preferred style is to
316 call into the inferior. The value is simply a string that is
317 copied into the command, so it can be anything that GDB can
318 evaluate to a callable address, not necessarily a function name. */
319
bde6261a 320static char *dprintf_function;
d3ce09f5
SS
321
322/* The channel to use for dynamic printf if the preferred style is to
323 call into the inferior; if a nonempty string, it will be passed to
324 the call as the first argument, with the format string as the
325 second. As with the dprintf function, this can be anything that
326 GDB knows how to evaluate, so in addition to common choices like
327 "stderr", this could be an app-specific expression like
328 "mystreams[curlogger]". */
329
bde6261a 330static char *dprintf_channel;
d3ce09f5
SS
331
332/* True if dprintf commands should continue to operate even if GDB
333 has disconnected. */
334static int disconnected_dprintf = 1;
335
5cea2a26
PA
336struct command_line *
337breakpoint_commands (struct breakpoint *b)
338{
d1b0a7bf 339 return b->commands ? b->commands.get () : NULL;
5cea2a26 340}
3daf8fe5 341
f3b1572e
PA
342/* Flag indicating that a command has proceeded the inferior past the
343 current breakpoint. */
344
345static int breakpoint_proceeded;
346
956a9fb9 347const char *
2cec12e5
AR
348bpdisp_text (enum bpdisp disp)
349{
4a64f543
MS
350 /* NOTE: the following values are a part of MI protocol and
351 represent values of 'disp' field returned when inferior stops at
352 a breakpoint. */
bc043ef3 353 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 354
2cec12e5
AR
355 return bpdisps[(int) disp];
356}
c906108c 357
4a64f543 358/* Prototypes for exported functions. */
c906108c 359/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 360 if such is available. */
c906108c
SS
361static int can_use_hw_watchpoints;
362
920d2a44
AC
363static void
364show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
365 struct cmd_list_element *c,
366 const char *value)
367{
3e43a32a
MS
368 fprintf_filtered (file,
369 _("Debugger's willingness to use "
370 "watchpoint hardware is %s.\n"),
920d2a44
AC
371 value);
372}
373
fa8d40ab
JJ
374/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
375 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 376 for unrecognized breakpoint locations.
fa8d40ab
JJ
377 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
378static enum auto_boolean pending_break_support;
920d2a44
AC
379static void
380show_pending_break_support (struct ui_file *file, int from_tty,
381 struct cmd_list_element *c,
382 const char *value)
383{
3e43a32a
MS
384 fprintf_filtered (file,
385 _("Debugger's behavior regarding "
386 "pending breakpoints is %s.\n"),
920d2a44
AC
387 value);
388}
fa8d40ab 389
765dc015 390/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 391 set with "break" but falling in read-only memory.
765dc015
VP
392 If 0, gdb will warn about such breakpoints, but won't automatically
393 use hardware breakpoints. */
394static int automatic_hardware_breakpoints;
395static void
396show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
397 struct cmd_list_element *c,
398 const char *value)
399{
3e43a32a
MS
400 fprintf_filtered (file,
401 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
402 value);
403}
404
a25a5a45
PA
405/* If on, GDB keeps breakpoints inserted even if the inferior is
406 stopped, and immediately inserts any new breakpoints as soon as
407 they're created. If off (default), GDB keeps breakpoints off of
408 the target as long as possible. That is, it delays inserting
409 breakpoints until the next resume, and removes them again when the
410 target fully stops. This is a bit safer in case GDB crashes while
411 processing user input. */
412static int always_inserted_mode = 0;
72d0e2c5 413
33e5cbd6 414static void
74960c60 415show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 416 struct cmd_list_element *c, const char *value)
74960c60 417{
a25a5a45
PA
418 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
419 value);
74960c60
VP
420}
421
b57bacec
PA
422/* See breakpoint.h. */
423
33e5cbd6 424int
a25a5a45 425breakpoints_should_be_inserted_now (void)
33e5cbd6 426{
a25a5a45
PA
427 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
428 {
429 /* If breakpoints are global, they should be inserted even if no
430 thread under gdb's control is running, or even if there are
431 no threads under GDB's control yet. */
432 return 1;
433 }
434 else if (target_has_execution)
435 {
372316f1
PA
436 struct thread_info *tp;
437
a25a5a45
PA
438 if (always_inserted_mode)
439 {
440 /* The user wants breakpoints inserted even if all threads
441 are stopped. */
442 return 1;
443 }
444
b57bacec
PA
445 if (threads_are_executing ())
446 return 1;
372316f1
PA
447
448 /* Don't remove breakpoints yet if, even though all threads are
449 stopped, we still have events to process. */
450 ALL_NON_EXITED_THREADS (tp)
451 if (tp->resumed
452 && tp->suspend.waitstatus_pending_p)
453 return 1;
a25a5a45
PA
454 }
455 return 0;
33e5cbd6 456}
765dc015 457
b775012e
LM
458static const char condition_evaluation_both[] = "host or target";
459
460/* Modes for breakpoint condition evaluation. */
461static const char condition_evaluation_auto[] = "auto";
462static const char condition_evaluation_host[] = "host";
463static const char condition_evaluation_target[] = "target";
464static const char *const condition_evaluation_enums[] = {
465 condition_evaluation_auto,
466 condition_evaluation_host,
467 condition_evaluation_target,
468 NULL
469};
470
471/* Global that holds the current mode for breakpoint condition evaluation. */
472static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
473
474/* Global that we use to display information to the user (gets its value from
475 condition_evaluation_mode_1. */
476static const char *condition_evaluation_mode = condition_evaluation_auto;
477
478/* Translate a condition evaluation mode MODE into either "host"
479 or "target". This is used mostly to translate from "auto" to the
480 real setting that is being used. It returns the translated
481 evaluation mode. */
482
483static const char *
484translate_condition_evaluation_mode (const char *mode)
485{
486 if (mode == condition_evaluation_auto)
487 {
488 if (target_supports_evaluation_of_breakpoint_conditions ())
489 return condition_evaluation_target;
490 else
491 return condition_evaluation_host;
492 }
493 else
494 return mode;
495}
496
497/* Discovers what condition_evaluation_auto translates to. */
498
499static const char *
500breakpoint_condition_evaluation_mode (void)
501{
502 return translate_condition_evaluation_mode (condition_evaluation_mode);
503}
504
505/* Return true if GDB should evaluate breakpoint conditions or false
506 otherwise. */
507
508static int
509gdb_evaluates_breakpoint_condition_p (void)
510{
511 const char *mode = breakpoint_condition_evaluation_mode ();
512
513 return (mode == condition_evaluation_host);
514}
515
c906108c
SS
516/* Are we executing breakpoint commands? */
517static int executing_breakpoint_commands;
518
c02f5703
MS
519/* Are overlay event breakpoints enabled? */
520static int overlay_events_enabled;
521
e09342b5
TJB
522/* See description in breakpoint.h. */
523int target_exact_watchpoints = 0;
524
c906108c 525/* Walk the following statement or block through all breakpoints.
e5dd4106 526 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 527 current breakpoint. */
c906108c 528
5c44784c 529#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 530
5c44784c
JM
531#define ALL_BREAKPOINTS_SAFE(B,TMP) \
532 for (B = breakpoint_chain; \
533 B ? (TMP=B->next, 1): 0; \
534 B = TMP)
c906108c 535
4a64f543
MS
536/* Similar iterator for the low-level breakpoints. SAFE variant is
537 not provided so update_global_location_list must not be called
538 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 539
876fa593 540#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
541 for (BP_TMP = bp_locations; \
542 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 543 BP_TMP++)
7cc221ef 544
b775012e
LM
545/* Iterates through locations with address ADDRESS for the currently selected
546 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
547 to where the loop should start from.
548 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
549 appropriate location to start with. */
550
551#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
552 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
553 BP_LOCP_TMP = BP_LOCP_START; \
554 BP_LOCP_START \
f5336ca5 555 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
556 && (*BP_LOCP_TMP)->address == ADDRESS); \
557 BP_LOCP_TMP++)
558
1042e4c0
SS
559/* Iterator for tracepoints only. */
560
561#define ALL_TRACEPOINTS(B) \
562 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 563 if (is_tracepoint (B))
1042e4c0 564
7cc221ef 565/* Chains of all breakpoints defined. */
c906108c
SS
566
567struct breakpoint *breakpoint_chain;
568
f5336ca5 569/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 570
f5336ca5 571static struct bp_location **bp_locations;
876fa593 572
f5336ca5 573/* Number of elements of BP_LOCATIONS. */
876fa593 574
f5336ca5 575static unsigned bp_locations_count;
876fa593 576
4a64f543 577/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 578 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 579 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 580 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 581 an address you need to read. */
876fa593 582
f5336ca5 583static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 584
4a64f543
MS
585/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
586 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
587 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
588 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 589 scan for shadow bytes for an address you need to read. */
876fa593 590
f5336ca5 591static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 592
4a64f543 593/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
594 from the bp_locations array, but for which a hit may still be
595 reported by a target. */
20874c92
VP
596VEC(bp_location_p) *moribund_locations = NULL;
597
c906108c
SS
598/* Number of last breakpoint made. */
599
95a42b64
TT
600static int breakpoint_count;
601
86b17b60
PA
602/* The value of `breakpoint_count' before the last command that
603 created breakpoints. If the last (break-like) command created more
604 than one breakpoint, then the difference between BREAKPOINT_COUNT
605 and PREV_BREAKPOINT_COUNT is more than one. */
606static int prev_breakpoint_count;
c906108c 607
1042e4c0
SS
608/* Number of last tracepoint made. */
609
95a42b64 610static int tracepoint_count;
1042e4c0 611
6149aea9
PA
612static struct cmd_list_element *breakpoint_set_cmdlist;
613static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 614struct cmd_list_element *save_cmdlist;
6149aea9 615
badd37ce
SDJ
616/* See declaration at breakpoint.h. */
617
618struct breakpoint *
619breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
620 void *user_data)
621{
622 struct breakpoint *b = NULL;
623
624 ALL_BREAKPOINTS (b)
625 {
626 if (func (b, user_data) != 0)
627 break;
628 }
629
630 return b;
631}
632
468d015d
JJ
633/* Return whether a breakpoint is an active enabled breakpoint. */
634static int
635breakpoint_enabled (struct breakpoint *b)
636{
0d381245 637 return (b->enable_state == bp_enabled);
468d015d
JJ
638}
639
c906108c
SS
640/* Set breakpoint count to NUM. */
641
95a42b64 642static void
fba45db2 643set_breakpoint_count (int num)
c906108c 644{
86b17b60 645 prev_breakpoint_count = breakpoint_count;
c906108c 646 breakpoint_count = num;
4fa62494 647 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
648}
649
86b17b60
PA
650/* Used by `start_rbreak_breakpoints' below, to record the current
651 breakpoint count before "rbreak" creates any breakpoint. */
652static int rbreak_start_breakpoint_count;
653
95a42b64
TT
654/* Called at the start an "rbreak" command to record the first
655 breakpoint made. */
86b17b60 656
95a42b64
TT
657void
658start_rbreak_breakpoints (void)
659{
86b17b60 660 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
661}
662
663/* Called at the end of an "rbreak" command to record the last
664 breakpoint made. */
86b17b60 665
95a42b64
TT
666void
667end_rbreak_breakpoints (void)
668{
86b17b60 669 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
670}
671
4a64f543 672/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
673
674void
fba45db2 675clear_breakpoint_hit_counts (void)
c906108c
SS
676{
677 struct breakpoint *b;
678
679 ALL_BREAKPOINTS (b)
680 b->hit_count = 0;
681}
682
c906108c 683\f
48cb2d85
VP
684/* Return the breakpoint with the specified number, or NULL
685 if the number does not refer to an existing breakpoint. */
686
687struct breakpoint *
688get_breakpoint (int num)
689{
690 struct breakpoint *b;
691
692 ALL_BREAKPOINTS (b)
693 if (b->number == num)
694 return b;
695
696 return NULL;
697}
5c44784c 698
c906108c 699\f
adc36818 700
b775012e
LM
701/* Mark locations as "conditions have changed" in case the target supports
702 evaluating conditions on its side. */
703
704static void
705mark_breakpoint_modified (struct breakpoint *b)
706{
707 struct bp_location *loc;
708
709 /* This is only meaningful if the target is
710 evaluating conditions and if the user has
711 opted for condition evaluation on the target's
712 side. */
713 if (gdb_evaluates_breakpoint_condition_p ()
714 || !target_supports_evaluation_of_breakpoint_conditions ())
715 return;
716
717 if (!is_breakpoint (b))
718 return;
719
720 for (loc = b->loc; loc; loc = loc->next)
721 loc->condition_changed = condition_modified;
722}
723
724/* Mark location as "conditions have changed" in case the target supports
725 evaluating conditions on its side. */
726
727static void
728mark_breakpoint_location_modified (struct bp_location *loc)
729{
730 /* This is only meaningful if the target is
731 evaluating conditions and if the user has
732 opted for condition evaluation on the target's
733 side. */
734 if (gdb_evaluates_breakpoint_condition_p ()
735 || !target_supports_evaluation_of_breakpoint_conditions ())
736
737 return;
738
739 if (!is_breakpoint (loc->owner))
740 return;
741
742 loc->condition_changed = condition_modified;
743}
744
745/* Sets the condition-evaluation mode using the static global
746 condition_evaluation_mode. */
747
748static void
749set_condition_evaluation_mode (char *args, int from_tty,
750 struct cmd_list_element *c)
751{
b775012e
LM
752 const char *old_mode, *new_mode;
753
754 if ((condition_evaluation_mode_1 == condition_evaluation_target)
755 && !target_supports_evaluation_of_breakpoint_conditions ())
756 {
757 condition_evaluation_mode_1 = condition_evaluation_mode;
758 warning (_("Target does not support breakpoint condition evaluation.\n"
759 "Using host evaluation mode instead."));
760 return;
761 }
762
763 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
764 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
765
abf1152a
JK
766 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
767 settings was "auto". */
768 condition_evaluation_mode = condition_evaluation_mode_1;
769
b775012e
LM
770 /* Only update the mode if the user picked a different one. */
771 if (new_mode != old_mode)
772 {
773 struct bp_location *loc, **loc_tmp;
774 /* If the user switched to a different evaluation mode, we
775 need to synch the changes with the target as follows:
776
777 "host" -> "target": Send all (valid) conditions to the target.
778 "target" -> "host": Remove all the conditions from the target.
779 */
780
b775012e
LM
781 if (new_mode == condition_evaluation_target)
782 {
783 /* Mark everything modified and synch conditions with the
784 target. */
785 ALL_BP_LOCATIONS (loc, loc_tmp)
786 mark_breakpoint_location_modified (loc);
787 }
788 else
789 {
790 /* Manually mark non-duplicate locations to synch conditions
791 with the target. We do this to remove all the conditions the
792 target knows about. */
793 ALL_BP_LOCATIONS (loc, loc_tmp)
794 if (is_breakpoint (loc->owner) && loc->inserted)
795 loc->needs_update = 1;
796 }
797
798 /* Do the update. */
44702360 799 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
800 }
801
802 return;
803}
804
805/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
806 what "auto" is translating to. */
807
808static void
809show_condition_evaluation_mode (struct ui_file *file, int from_tty,
810 struct cmd_list_element *c, const char *value)
811{
812 if (condition_evaluation_mode == condition_evaluation_auto)
813 fprintf_filtered (file,
814 _("Breakpoint condition evaluation "
815 "mode is %s (currently %s).\n"),
816 value,
817 breakpoint_condition_evaluation_mode ());
818 else
819 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
820 value);
821}
822
823/* A comparison function for bp_location AP and BP that is used by
824 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 825 the more general bp_locations_compare function. */
b775012e
LM
826
827static int
f5336ca5 828bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 829{
9a3c8263
SM
830 const struct bp_location *a = *(const struct bp_location **) ap;
831 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
832
833 if (a->address == b->address)
834 return 0;
835 else
836 return ((a->address > b->address) - (a->address < b->address));
837}
838
839/* Helper function to skip all bp_locations with addresses
840 less than ADDRESS. It returns the first bp_location that
841 is greater than or equal to ADDRESS. If none is found, just
842 return NULL. */
843
844static struct bp_location **
845get_first_locp_gte_addr (CORE_ADDR address)
846{
847 struct bp_location dummy_loc;
848 struct bp_location *dummy_locp = &dummy_loc;
849 struct bp_location **locp_found = NULL;
850
851 /* Initialize the dummy location's address field. */
b775012e
LM
852 dummy_loc.address = address;
853
854 /* Find a close match to the first location at ADDRESS. */
9a3c8263 855 locp_found = ((struct bp_location **)
f5336ca5 856 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 857 sizeof (struct bp_location **),
f5336ca5 858 bp_locations_compare_addrs));
b775012e
LM
859
860 /* Nothing was found, nothing left to do. */
861 if (locp_found == NULL)
862 return NULL;
863
864 /* We may have found a location that is at ADDRESS but is not the first in the
865 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 866 while ((locp_found - 1) >= bp_locations
b775012e
LM
867 && (*(locp_found - 1))->address == address)
868 locp_found--;
869
870 return locp_found;
871}
872
adc36818 873void
7a26bd4d 874set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
875 int from_tty)
876{
3a5c3e22
PA
877 xfree (b->cond_string);
878 b->cond_string = NULL;
adc36818 879
3a5c3e22 880 if (is_watchpoint (b))
adc36818 881 {
3a5c3e22
PA
882 struct watchpoint *w = (struct watchpoint *) b;
883
4d01a485 884 w->cond_exp.reset ();
3a5c3e22
PA
885 }
886 else
887 {
888 struct bp_location *loc;
889
890 for (loc = b->loc; loc; loc = loc->next)
891 {
4d01a485 892 loc->cond.reset ();
b775012e
LM
893
894 /* No need to free the condition agent expression
895 bytecode (if we have one). We will handle this
896 when we go through update_global_location_list. */
3a5c3e22 897 }
adc36818 898 }
adc36818
PM
899
900 if (*exp == 0)
901 {
902 if (from_tty)
903 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
904 }
905 else
906 {
bbc13ae3 907 const char *arg = exp;
cc59ec59 908
adc36818
PM
909 /* I don't know if it matters whether this is the string the user
910 typed in or the decompiled expression. */
911 b->cond_string = xstrdup (arg);
912 b->condition_not_parsed = 0;
913
914 if (is_watchpoint (b))
915 {
3a5c3e22
PA
916 struct watchpoint *w = (struct watchpoint *) b;
917
adc36818
PM
918 innermost_block = NULL;
919 arg = exp;
1bb9788d 920 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
921 if (*arg)
922 error (_("Junk at end of expression"));
3a5c3e22 923 w->cond_exp_valid_block = innermost_block;
adc36818
PM
924 }
925 else
926 {
3a5c3e22
PA
927 struct bp_location *loc;
928
adc36818
PM
929 for (loc = b->loc; loc; loc = loc->next)
930 {
931 arg = exp;
932 loc->cond =
1bb9788d
TT
933 parse_exp_1 (&arg, loc->address,
934 block_for_pc (loc->address), 0);
adc36818
PM
935 if (*arg)
936 error (_("Junk at end of expression"));
937 }
938 }
939 }
b775012e
LM
940 mark_breakpoint_modified (b);
941
8d3788bd 942 observer_notify_breakpoint_modified (b);
adc36818
PM
943}
944
d55637df
TT
945/* Completion for the "condition" command. */
946
eb3ff9a5 947static void
6f937416 948condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 949 completion_tracker &tracker,
6f937416 950 const char *text, const char *word)
d55637df 951{
6f937416 952 const char *space;
d55637df 953
f1735a53
TT
954 text = skip_spaces (text);
955 space = skip_to_space (text);
d55637df
TT
956 if (*space == '\0')
957 {
958 int len;
959 struct breakpoint *b;
960 VEC (char_ptr) *result = NULL;
961
962 if (text[0] == '$')
963 {
964 /* We don't support completion of history indices. */
eb3ff9a5
PA
965 if (!isdigit (text[1]))
966 complete_internalvar (tracker, &text[1]);
967 return;
d55637df
TT
968 }
969
970 /* We're completing the breakpoint number. */
971 len = strlen (text);
972
973 ALL_BREAKPOINTS (b)
58ce7251
SDJ
974 {
975 char number[50];
976
977 xsnprintf (number, sizeof (number), "%d", b->number);
978
979 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
980 {
981 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
982 tracker.add_completion (std::move (copy));
983 }
58ce7251 984 }
d55637df 985
eb3ff9a5 986 return;
d55637df
TT
987 }
988
989 /* We're completing the expression part. */
f1735a53 990 text = skip_spaces (space);
eb3ff9a5 991 expression_completer (cmd, tracker, text, word);
d55637df
TT
992}
993
c906108c
SS
994/* condition N EXP -- set break condition of breakpoint N to EXP. */
995
996static void
fba45db2 997condition_command (char *arg, int from_tty)
c906108c 998{
52f0bd74 999 struct breakpoint *b;
c906108c 1000 char *p;
52f0bd74 1001 int bnum;
c906108c
SS
1002
1003 if (arg == 0)
e2e0b3e5 1004 error_no_arg (_("breakpoint number"));
c906108c
SS
1005
1006 p = arg;
1007 bnum = get_number (&p);
5c44784c 1008 if (bnum == 0)
8a3fe4f8 1009 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1010
1011 ALL_BREAKPOINTS (b)
1012 if (b->number == bnum)
2f069f6f 1013 {
6dddc817
DE
1014 /* Check if this breakpoint has a "stop" method implemented in an
1015 extension language. This method and conditions entered into GDB
1016 from the CLI are mutually exclusive. */
1017 const struct extension_language_defn *extlang
1018 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1019
1020 if (extlang != NULL)
1021 {
1022 error (_("Only one stop condition allowed. There is currently"
1023 " a %s stop condition defined for this breakpoint."),
1024 ext_lang_capitalized_name (extlang));
1025 }
2566ad2d 1026 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1027
1028 if (is_breakpoint (b))
44702360 1029 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1030
2f069f6f
JB
1031 return;
1032 }
c906108c 1033
8a3fe4f8 1034 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1035}
1036
a7bdde9e
VP
1037/* Check that COMMAND do not contain commands that are suitable
1038 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1039 Throw if any such commands is found. */
1040
a7bdde9e
VP
1041static void
1042check_no_tracepoint_commands (struct command_line *commands)
1043{
1044 struct command_line *c;
cc59ec59 1045
a7bdde9e
VP
1046 for (c = commands; c; c = c->next)
1047 {
1048 int i;
1049
1050 if (c->control_type == while_stepping_control)
3e43a32a
MS
1051 error (_("The 'while-stepping' command can "
1052 "only be used for tracepoints"));
a7bdde9e
VP
1053
1054 for (i = 0; i < c->body_count; ++i)
1055 check_no_tracepoint_commands ((c->body_list)[i]);
1056
1057 /* Not that command parsing removes leading whitespace and comment
4a64f543 1058 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1059 command directly. */
1060 if (strstr (c->line, "collect ") == c->line)
1061 error (_("The 'collect' command can only be used for tracepoints"));
1062
51661e93
VP
1063 if (strstr (c->line, "teval ") == c->line)
1064 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1065 }
1066}
1067
c1fc2657 1068struct longjmp_breakpoint : public breakpoint
3b0871f4 1069{
c1fc2657 1070 ~longjmp_breakpoint () override;
3b0871f4
SM
1071};
1072
d77f58be
SS
1073/* Encapsulate tests for different types of tracepoints. */
1074
3b0871f4
SM
1075static bool
1076is_tracepoint_type (bptype type)
d9b3f62e
PA
1077{
1078 return (type == bp_tracepoint
1079 || type == bp_fast_tracepoint
1080 || type == bp_static_tracepoint);
1081}
1082
3b0871f4
SM
1083static bool
1084is_longjmp_type (bptype type)
1085{
1086 return type == bp_longjmp || type == bp_exception;
1087}
1088
a7bdde9e 1089int
d77f58be 1090is_tracepoint (const struct breakpoint *b)
a7bdde9e 1091{
d9b3f62e 1092 return is_tracepoint_type (b->type);
a7bdde9e 1093}
d9b3f62e 1094
a5e364af
SM
1095/* Factory function to create an appropriate instance of breakpoint given
1096 TYPE. */
1097
1098static std::unique_ptr<breakpoint>
1099new_breakpoint_from_type (bptype type)
1100{
1101 breakpoint *b;
1102
1103 if (is_tracepoint_type (type))
c1fc2657 1104 b = new tracepoint ();
3b0871f4 1105 else if (is_longjmp_type (type))
c1fc2657 1106 b = new longjmp_breakpoint ();
a5e364af
SM
1107 else
1108 b = new breakpoint ();
1109
1110 return std::unique_ptr<breakpoint> (b);
1111}
1112
e5dd4106 1113/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1114 breakpoint. This function will throw an exception if a problem is
1115 found. */
48cb2d85 1116
95a42b64
TT
1117static void
1118validate_commands_for_breakpoint (struct breakpoint *b,
1119 struct command_line *commands)
48cb2d85 1120{
d77f58be 1121 if (is_tracepoint (b))
a7bdde9e 1122 {
c9a6ce02 1123 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1124 struct command_line *c;
1125 struct command_line *while_stepping = 0;
c9a6ce02
PA
1126
1127 /* Reset the while-stepping step count. The previous commands
1128 might have included a while-stepping action, while the new
1129 ones might not. */
1130 t->step_count = 0;
1131
1132 /* We need to verify that each top-level element of commands is
1133 valid for tracepoints, that there's at most one
1134 while-stepping element, and that the while-stepping's body
1135 has valid tracing commands excluding nested while-stepping.
1136 We also need to validate the tracepoint action line in the
1137 context of the tracepoint --- validate_actionline actually
1138 has side effects, like setting the tracepoint's
1139 while-stepping STEP_COUNT, in addition to checking if the
1140 collect/teval actions parse and make sense in the
1141 tracepoint's context. */
a7bdde9e
VP
1142 for (c = commands; c; c = c->next)
1143 {
a7bdde9e
VP
1144 if (c->control_type == while_stepping_control)
1145 {
1146 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1147 error (_("The 'while-stepping' command "
1148 "cannot be used for fast tracepoint"));
0fb4aa4b 1149 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1150 error (_("The 'while-stepping' command "
1151 "cannot be used for static tracepoint"));
a7bdde9e
VP
1152
1153 if (while_stepping)
3e43a32a
MS
1154 error (_("The 'while-stepping' command "
1155 "can be used only once"));
a7bdde9e
VP
1156 else
1157 while_stepping = c;
1158 }
c9a6ce02
PA
1159
1160 validate_actionline (c->line, b);
a7bdde9e
VP
1161 }
1162 if (while_stepping)
1163 {
1164 struct command_line *c2;
1165
1166 gdb_assert (while_stepping->body_count == 1);
1167 c2 = while_stepping->body_list[0];
1168 for (; c2; c2 = c2->next)
1169 {
a7bdde9e
VP
1170 if (c2->control_type == while_stepping_control)
1171 error (_("The 'while-stepping' command cannot be nested"));
1172 }
1173 }
1174 }
1175 else
1176 {
1177 check_no_tracepoint_commands (commands);
1178 }
95a42b64
TT
1179}
1180
0fb4aa4b
PA
1181/* Return a vector of all the static tracepoints set at ADDR. The
1182 caller is responsible for releasing the vector. */
1183
1184VEC(breakpoint_p) *
1185static_tracepoints_here (CORE_ADDR addr)
1186{
1187 struct breakpoint *b;
1188 VEC(breakpoint_p) *found = 0;
1189 struct bp_location *loc;
1190
1191 ALL_BREAKPOINTS (b)
1192 if (b->type == bp_static_tracepoint)
1193 {
1194 for (loc = b->loc; loc; loc = loc->next)
1195 if (loc->address == addr)
1196 VEC_safe_push(breakpoint_p, found, b);
1197 }
1198
1199 return found;
1200}
1201
95a42b64 1202/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1203 validate that only allowed commands are included. */
95a42b64
TT
1204
1205void
4a64f543 1206breakpoint_set_commands (struct breakpoint *b,
93921405 1207 command_line_up &&commands)
95a42b64 1208{
93921405 1209 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1210
d1b0a7bf 1211 b->commands = std::move (commands);
8d3788bd 1212 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1213}
1214
45a43567
TT
1215/* Set the internal `silent' flag on the breakpoint. Note that this
1216 is not the same as the "silent" that may appear in the breakpoint's
1217 commands. */
1218
1219void
1220breakpoint_set_silent (struct breakpoint *b, int silent)
1221{
1222 int old_silent = b->silent;
1223
1224 b->silent = silent;
1225 if (old_silent != silent)
8d3788bd 1226 observer_notify_breakpoint_modified (b);
45a43567
TT
1227}
1228
1229/* Set the thread for this breakpoint. If THREAD is -1, make the
1230 breakpoint work for any thread. */
1231
1232void
1233breakpoint_set_thread (struct breakpoint *b, int thread)
1234{
1235 int old_thread = b->thread;
1236
1237 b->thread = thread;
1238 if (old_thread != thread)
8d3788bd 1239 observer_notify_breakpoint_modified (b);
45a43567
TT
1240}
1241
1242/* Set the task for this breakpoint. If TASK is 0, make the
1243 breakpoint work for any task. */
1244
1245void
1246breakpoint_set_task (struct breakpoint *b, int task)
1247{
1248 int old_task = b->task;
1249
1250 b->task = task;
1251 if (old_task != task)
8d3788bd 1252 observer_notify_breakpoint_modified (b);
45a43567
TT
1253}
1254
95a42b64
TT
1255void
1256check_tracepoint_command (char *line, void *closure)
a7bdde9e 1257{
9a3c8263 1258 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1259
6f937416 1260 validate_actionline (line, b);
a7bdde9e
VP
1261}
1262
95a42b64 1263static void
896b6bda 1264commands_command_1 (const char *arg, int from_tty,
4a64f543 1265 struct command_line *control)
95a42b64 1266{
d1b0a7bf 1267 counted_command_line cmd;
95a42b64 1268
896b6bda
PA
1269 std::string new_arg;
1270
95a42b64
TT
1271 if (arg == NULL || !*arg)
1272 {
86b17b60 1273 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1274 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1275 breakpoint_count);
95a42b64 1276 else if (breakpoint_count > 0)
896b6bda 1277 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1278 arg = new_arg.c_str ();
1279 }
1280
1281 map_breakpoint_numbers
1282 (arg, [&] (breakpoint *b)
1283 {
1284 if (cmd == NULL)
1285 {
48649e1b 1286 if (control != NULL)
d1b0a7bf 1287 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1288 else
1289 {
1290 struct cleanup *old_chain;
1291 char *str;
1292
1293 str = xstrprintf (_("Type commands for breakpoint(s) "
1294 "%s, one per line."),
1295 arg);
1296
1297 old_chain = make_cleanup (xfree, str);
1298
d1b0a7bf
TT
1299 cmd = read_command_lines (str,
1300 from_tty, 1,
1301 (is_tracepoint (b)
1302 ? check_tracepoint_command : 0),
1303 b);
48649e1b
TT
1304
1305 do_cleanups (old_chain);
1306 }
48649e1b
TT
1307 }
1308
1309 /* If a breakpoint was on the list more than once, we don't need to
1310 do anything. */
1311 if (b->commands != cmd)
1312 {
d1b0a7bf 1313 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1314 b->commands = cmd;
1315 observer_notify_breakpoint_modified (b);
1316 }
1317 });
95a42b64 1318
48649e1b 1319 if (cmd == NULL)
95a42b64 1320 error (_("No breakpoints specified."));
95a42b64
TT
1321}
1322
1323static void
1324commands_command (char *arg, int from_tty)
1325{
1326 commands_command_1 (arg, from_tty, NULL);
c906108c 1327}
40c03ae8
EZ
1328
1329/* Like commands_command, but instead of reading the commands from
1330 input stream, takes them from an already parsed command structure.
1331
1332 This is used by cli-script.c to DTRT with breakpoint commands
1333 that are part of if and while bodies. */
1334enum command_control_type
896b6bda 1335commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1336{
95a42b64
TT
1337 commands_command_1 (arg, 0, cmd);
1338 return simple_control;
40c03ae8 1339}
876fa593
JK
1340
1341/* Return non-zero if BL->TARGET_INFO contains valid information. */
1342
1343static int
1344bp_location_has_shadow (struct bp_location *bl)
1345{
1346 if (bl->loc_type != bp_loc_software_breakpoint)
1347 return 0;
1348 if (!bl->inserted)
1349 return 0;
1350 if (bl->target_info.shadow_len == 0)
e5dd4106 1351 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1352 return 0;
1353 return 1;
1354}
1355
9d497a19
PA
1356/* Update BUF, which is LEN bytes read from the target address
1357 MEMADDR, by replacing a memory breakpoint with its shadowed
1358 contents.
1359
1360 If READBUF is not NULL, this buffer must not overlap with the of
1361 the breakpoint location's shadow_contents buffer. Otherwise, a
1362 failed assertion internal error will be raised. */
1363
1364static void
1365one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1366 const gdb_byte *writebuf_org,
1367 ULONGEST memaddr, LONGEST len,
1368 struct bp_target_info *target_info,
1369 struct gdbarch *gdbarch)
1370{
1371 /* Now do full processing of the found relevant range of elements. */
1372 CORE_ADDR bp_addr = 0;
1373 int bp_size = 0;
1374 int bptoffset = 0;
1375
1376 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1377 current_program_space->aspace, 0))
1378 {
1379 /* The breakpoint is inserted in a different address space. */
1380 return;
1381 }
1382
1383 /* Addresses and length of the part of the breakpoint that
1384 we need to copy. */
1385 bp_addr = target_info->placed_address;
1386 bp_size = target_info->shadow_len;
1387
1388 if (bp_addr + bp_size <= memaddr)
1389 {
1390 /* The breakpoint is entirely before the chunk of memory we are
1391 reading. */
1392 return;
1393 }
1394
1395 if (bp_addr >= memaddr + len)
1396 {
1397 /* The breakpoint is entirely after the chunk of memory we are
1398 reading. */
1399 return;
1400 }
1401
1402 /* Offset within shadow_contents. */
1403 if (bp_addr < memaddr)
1404 {
1405 /* Only copy the second part of the breakpoint. */
1406 bp_size -= memaddr - bp_addr;
1407 bptoffset = memaddr - bp_addr;
1408 bp_addr = memaddr;
1409 }
1410
1411 if (bp_addr + bp_size > memaddr + len)
1412 {
1413 /* Only copy the first part of the breakpoint. */
1414 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1415 }
1416
1417 if (readbuf != NULL)
1418 {
1419 /* Verify that the readbuf buffer does not overlap with the
1420 shadow_contents buffer. */
1421 gdb_assert (target_info->shadow_contents >= readbuf + len
1422 || readbuf >= (target_info->shadow_contents
1423 + target_info->shadow_len));
1424
1425 /* Update the read buffer with this inserted breakpoint's
1426 shadow. */
1427 memcpy (readbuf + bp_addr - memaddr,
1428 target_info->shadow_contents + bptoffset, bp_size);
1429 }
1430 else
1431 {
1432 const unsigned char *bp;
0d5ed153
MR
1433 CORE_ADDR addr = target_info->reqstd_address;
1434 int placed_size;
9d497a19
PA
1435
1436 /* Update the shadow with what we want to write to memory. */
1437 memcpy (target_info->shadow_contents + bptoffset,
1438 writebuf_org + bp_addr - memaddr, bp_size);
1439
1440 /* Determine appropriate breakpoint contents and size for this
1441 address. */
0d5ed153 1442 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1443
1444 /* Update the final write buffer with this inserted
1445 breakpoint's INSN. */
1446 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1447 }
1448}
1449
8defab1a 1450/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1451 by replacing any memory breakpoints with their shadowed contents.
1452
35c63cd8
JB
1453 If READBUF is not NULL, this buffer must not overlap with any of
1454 the breakpoint location's shadow_contents buffers. Otherwise,
1455 a failed assertion internal error will be raised.
1456
876fa593 1457 The range of shadowed area by each bp_location is:
f5336ca5
PA
1458 bl->address - bp_locations_placed_address_before_address_max
1459 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1460 The range we were requested to resolve shadows for is:
1461 memaddr ... memaddr + len
1462 Thus the safe cutoff boundaries for performance optimization are
35df4500 1463 memaddr + len <= (bl->address
f5336ca5 1464 - bp_locations_placed_address_before_address_max)
876fa593 1465 and:
f5336ca5 1466 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1467
8defab1a 1468void
f0ba3972
PA
1469breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1470 const gdb_byte *writebuf_org,
1471 ULONGEST memaddr, LONGEST len)
c906108c 1472{
4a64f543
MS
1473 /* Left boundary, right boundary and median element of our binary
1474 search. */
876fa593
JK
1475 unsigned bc_l, bc_r, bc;
1476
4a64f543
MS
1477 /* Find BC_L which is a leftmost element which may affect BUF
1478 content. It is safe to report lower value but a failure to
1479 report higher one. */
876fa593
JK
1480
1481 bc_l = 0;
f5336ca5 1482 bc_r = bp_locations_count;
876fa593
JK
1483 while (bc_l + 1 < bc_r)
1484 {
35df4500 1485 struct bp_location *bl;
876fa593
JK
1486
1487 bc = (bc_l + bc_r) / 2;
f5336ca5 1488 bl = bp_locations[bc];
876fa593 1489
4a64f543
MS
1490 /* Check first BL->ADDRESS will not overflow due to the added
1491 constant. Then advance the left boundary only if we are sure
1492 the BC element can in no way affect the BUF content (MEMADDR
1493 to MEMADDR + LEN range).
876fa593 1494
f5336ca5 1495 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1496 offset so that we cannot miss a breakpoint with its shadow
1497 range tail still reaching MEMADDR. */
c5aa993b 1498
f5336ca5 1499 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1500 >= bl->address)
f5336ca5 1501 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1502 <= memaddr))
876fa593
JK
1503 bc_l = bc;
1504 else
1505 bc_r = bc;
1506 }
1507
128070bb
PA
1508 /* Due to the binary search above, we need to make sure we pick the
1509 first location that's at BC_L's address. E.g., if there are
1510 multiple locations at the same address, BC_L may end up pointing
1511 at a duplicate location, and miss the "master"/"inserted"
1512 location. Say, given locations L1, L2 and L3 at addresses A and
1513 B:
1514
1515 L1@A, L2@A, L3@B, ...
1516
1517 BC_L could end up pointing at location L2, while the "master"
1518 location could be L1. Since the `loc->inserted' flag is only set
1519 on "master" locations, we'd forget to restore the shadow of L1
1520 and L2. */
1521 while (bc_l > 0
f5336ca5 1522 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1523 bc_l--;
1524
876fa593
JK
1525 /* Now do full processing of the found relevant range of elements. */
1526
f5336ca5 1527 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1528 {
f5336ca5 1529 struct bp_location *bl = bp_locations[bc];
876fa593 1530
35df4500
TJB
1531 /* bp_location array has BL->OWNER always non-NULL. */
1532 if (bl->owner->type == bp_none)
8a3fe4f8 1533 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1534 bl->owner->number);
ffce0d52 1535
e5dd4106 1536 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1537 content. */
1538
f5336ca5
PA
1539 if (bl->address >= bp_locations_placed_address_before_address_max
1540 && memaddr + len <= (bl->address
1541 - bp_locations_placed_address_before_address_max))
876fa593
JK
1542 break;
1543
35df4500 1544 if (!bp_location_has_shadow (bl))
c5aa993b 1545 continue;
6c95b8df 1546
9d497a19
PA
1547 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1548 memaddr, len, &bl->target_info, bl->gdbarch);
1549 }
c906108c 1550}
9d497a19 1551
c906108c 1552\f
c5aa993b 1553
b775012e
LM
1554/* Return true if BPT is either a software breakpoint or a hardware
1555 breakpoint. */
1556
1557int
1558is_breakpoint (const struct breakpoint *bpt)
1559{
1560 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1561 || bpt->type == bp_hardware_breakpoint
1562 || bpt->type == bp_dprintf);
b775012e
LM
1563}
1564
60e1c644
PA
1565/* Return true if BPT is of any hardware watchpoint kind. */
1566
a5606eee 1567static int
d77f58be 1568is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1569{
1570 return (bpt->type == bp_hardware_watchpoint
1571 || bpt->type == bp_read_watchpoint
1572 || bpt->type == bp_access_watchpoint);
1573}
7270d8f2 1574
60e1c644
PA
1575/* Return true if BPT is of any watchpoint kind, hardware or
1576 software. */
1577
3a5c3e22 1578int
d77f58be 1579is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1580{
1581 return (is_hardware_watchpoint (bpt)
1582 || bpt->type == bp_watchpoint);
1583}
1584
3a5c3e22
PA
1585/* Returns true if the current thread and its running state are safe
1586 to evaluate or update watchpoint B. Watchpoints on local
1587 expressions need to be evaluated in the context of the thread that
1588 was current when the watchpoint was created, and, that thread needs
1589 to be stopped to be able to select the correct frame context.
1590 Watchpoints on global expressions can be evaluated on any thread,
1591 and in any state. It is presently left to the target allowing
1592 memory accesses when threads are running. */
f6bc2008
PA
1593
1594static int
3a5c3e22 1595watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1596{
c1fc2657 1597 return (b->pspace == current_program_space
d0d8b0c6
JK
1598 && (ptid_equal (b->watchpoint_thread, null_ptid)
1599 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1600 && !is_executing (inferior_ptid))));
f6bc2008
PA
1601}
1602
d0fb5eae
JK
1603/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1604 associated bp_watchpoint_scope breakpoint. */
1605
1606static void
3a5c3e22 1607watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1608{
c1fc2657 1609 if (w->related_breakpoint != w)
d0fb5eae 1610 {
c1fc2657
SM
1611 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1612 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1613 w->related_breakpoint->disposition = disp_del_at_next_stop;
1614 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1615 w->related_breakpoint = w;
d0fb5eae 1616 }
c1fc2657 1617 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1618}
1619
bb9d5f81
PP
1620/* Extract a bitfield value from value VAL using the bit parameters contained in
1621 watchpoint W. */
1622
1623static struct value *
1624extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1625{
1626 struct value *bit_val;
1627
1628 if (val == NULL)
1629 return NULL;
1630
1631 bit_val = allocate_value (value_type (val));
1632
1633 unpack_value_bitfield (bit_val,
1634 w->val_bitpos,
1635 w->val_bitsize,
1636 value_contents_for_printing (val),
1637 value_offset (val),
1638 val);
1639
1640 return bit_val;
1641}
1642
c6d81124
PA
1643/* Allocate a dummy location and add it to B, which must be a software
1644 watchpoint. This is required because even if a software watchpoint
1645 is not watching any memory, bpstat_stop_status requires a location
1646 to be able to report stops. */
1647
1648static void
1649software_watchpoint_add_no_memory_location (struct breakpoint *b,
1650 struct program_space *pspace)
1651{
1652 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1653
1654 b->loc = allocate_bp_location (b);
1655 b->loc->pspace = pspace;
1656 b->loc->address = -1;
1657 b->loc->length = -1;
1658}
1659
1660/* Returns true if B is a software watchpoint that is not watching any
1661 memory (e.g., "watch $pc"). */
1662
1663static int
1664is_no_memory_software_watchpoint (struct breakpoint *b)
1665{
1666 return (b->type == bp_watchpoint
1667 && b->loc != NULL
1668 && b->loc->next == NULL
1669 && b->loc->address == -1
1670 && b->loc->length == -1);
1671}
1672
567e1b4e
JB
1673/* Assuming that B is a watchpoint:
1674 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1675 - Evaluate expression and store the result in B->val
567e1b4e
JB
1676 - Evaluate the condition if there is one, and store the result
1677 in b->loc->cond.
a5606eee
VP
1678 - Update the list of values that must be watched in B->loc.
1679
4a64f543
MS
1680 If the watchpoint disposition is disp_del_at_next_stop, then do
1681 nothing. If this is local watchpoint that is out of scope, delete
1682 it.
1683
1684 Even with `set breakpoint always-inserted on' the watchpoints are
1685 removed + inserted on each stop here. Normal breakpoints must
1686 never be removed because they might be missed by a running thread
1687 when debugging in non-stop mode. On the other hand, hardware
1688 watchpoints (is_hardware_watchpoint; processed here) are specific
1689 to each LWP since they are stored in each LWP's hardware debug
1690 registers. Therefore, such LWP must be stopped first in order to
1691 be able to modify its hardware watchpoints.
1692
1693 Hardware watchpoints must be reset exactly once after being
1694 presented to the user. It cannot be done sooner, because it would
1695 reset the data used to present the watchpoint hit to the user. And
1696 it must not be done later because it could display the same single
1697 watchpoint hit during multiple GDB stops. Note that the latter is
1698 relevant only to the hardware watchpoint types bp_read_watchpoint
1699 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1700 not user-visible - its hit is suppressed if the memory content has
1701 not changed.
1702
1703 The following constraints influence the location where we can reset
1704 hardware watchpoints:
1705
1706 * target_stopped_by_watchpoint and target_stopped_data_address are
1707 called several times when GDB stops.
1708
1709 [linux]
1710 * Multiple hardware watchpoints can be hit at the same time,
1711 causing GDB to stop. GDB only presents one hardware watchpoint
1712 hit at a time as the reason for stopping, and all the other hits
1713 are presented later, one after the other, each time the user
1714 requests the execution to be resumed. Execution is not resumed
1715 for the threads still having pending hit event stored in
1716 LWP_INFO->STATUS. While the watchpoint is already removed from
1717 the inferior on the first stop the thread hit event is kept being
1718 reported from its cached value by linux_nat_stopped_data_address
1719 until the real thread resume happens after the watchpoint gets
1720 presented and thus its LWP_INFO->STATUS gets reset.
1721
1722 Therefore the hardware watchpoint hit can get safely reset on the
1723 watchpoint removal from inferior. */
a79d3c27 1724
b40ce68a 1725static void
3a5c3e22 1726update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1727{
a5606eee 1728 int within_current_scope;
a5606eee 1729 struct frame_id saved_frame_id;
66076460 1730 int frame_saved;
a5606eee 1731
f6bc2008
PA
1732 /* If this is a local watchpoint, we only want to check if the
1733 watchpoint frame is in scope if the current thread is the thread
1734 that was used to create the watchpoint. */
1735 if (!watchpoint_in_thread_scope (b))
1736 return;
1737
c1fc2657 1738 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1739 return;
1740
66076460 1741 frame_saved = 0;
a5606eee
VP
1742
1743 /* Determine if the watchpoint is within scope. */
1744 if (b->exp_valid_block == NULL)
1745 within_current_scope = 1;
1746 else
1747 {
b5db5dfc
UW
1748 struct frame_info *fi = get_current_frame ();
1749 struct gdbarch *frame_arch = get_frame_arch (fi);
1750 CORE_ADDR frame_pc = get_frame_pc (fi);
1751
c9cf6e20
MG
1752 /* If we're at a point where the stack has been destroyed
1753 (e.g. in a function epilogue), unwinding may not work
1754 properly. Do not attempt to recreate locations at this
b5db5dfc 1755 point. See similar comments in watchpoint_check. */
c9cf6e20 1756 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1757 return;
66076460
DJ
1758
1759 /* Save the current frame's ID so we can restore it after
1760 evaluating the watchpoint expression on its own frame. */
1761 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1762 took a frame parameter, so that we didn't have to change the
1763 selected frame. */
1764 frame_saved = 1;
1765 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1766
a5606eee
VP
1767 fi = frame_find_by_id (b->watchpoint_frame);
1768 within_current_scope = (fi != NULL);
1769 if (within_current_scope)
1770 select_frame (fi);
1771 }
1772
b5db5dfc
UW
1773 /* We don't free locations. They are stored in the bp_location array
1774 and update_global_location_list will eventually delete them and
1775 remove breakpoints if needed. */
c1fc2657 1776 b->loc = NULL;
b5db5dfc 1777
a5606eee
VP
1778 if (within_current_scope && reparse)
1779 {
bbc13ae3 1780 const char *s;
d63d0675 1781
4d01a485 1782 b->exp.reset ();
d63d0675 1783 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1784 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1785 /* If the meaning of expression itself changed, the old value is
1786 no longer relevant. We don't want to report a watchpoint hit
1787 to the user when the old value and the new value may actually
1788 be completely different objects. */
1789 value_free (b->val);
fa4727a6
DJ
1790 b->val = NULL;
1791 b->val_valid = 0;
60e1c644
PA
1792
1793 /* Note that unlike with breakpoints, the watchpoint's condition
1794 expression is stored in the breakpoint object, not in the
1795 locations (re)created below. */
c1fc2657 1796 if (b->cond_string != NULL)
60e1c644 1797 {
4d01a485 1798 b->cond_exp.reset ();
60e1c644 1799
c1fc2657 1800 s = b->cond_string;
1bb9788d 1801 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1802 }
a5606eee 1803 }
a5606eee
VP
1804
1805 /* If we failed to parse the expression, for example because
1806 it refers to a global variable in a not-yet-loaded shared library,
1807 don't try to insert watchpoint. We don't automatically delete
1808 such watchpoint, though, since failure to parse expression
1809 is different from out-of-scope watchpoint. */
e8369a73 1810 if (!target_has_execution)
2d134ed3
PA
1811 {
1812 /* Without execution, memory can't change. No use to try and
1813 set watchpoint locations. The watchpoint will be reset when
1814 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1815 if (!can_use_hw_watchpoints)
1816 {
c1fc2657
SM
1817 if (b->ops->works_in_software_mode (b))
1818 b->type = bp_watchpoint;
e8369a73 1819 else
638aa5a1
AB
1820 error (_("Can't set read/access watchpoint when "
1821 "hardware watchpoints are disabled."));
e8369a73 1822 }
2d134ed3
PA
1823 }
1824 else if (within_current_scope && b->exp)
a5606eee 1825 {
0cf6dd15 1826 int pc = 0;
fa4727a6 1827 struct value *val_chain, *v, *result, *next;
2d134ed3 1828 struct program_space *frame_pspace;
a5606eee 1829
4d01a485 1830 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1831
a5606eee
VP
1832 /* Avoid setting b->val if it's already set. The meaning of
1833 b->val is 'the last value' user saw, and we should update
1834 it only if we reported that last value to user. As it
9c06b0b4
TJB
1835 happens, the code that reports it updates b->val directly.
1836 We don't keep track of the memory value for masked
1837 watchpoints. */
c1fc2657 1838 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1839 {
bb9d5f81
PP
1840 if (b->val_bitsize != 0)
1841 {
1842 v = extract_bitfield_from_watchpoint_value (b, v);
1843 if (v != NULL)
1844 release_value (v);
1845 }
fa4727a6
DJ
1846 b->val = v;
1847 b->val_valid = 1;
1848 }
a5606eee 1849
2d134ed3
PA
1850 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1851
a5606eee 1852 /* Look at each value on the value chain. */
9fa40276 1853 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1854 {
1855 /* If it's a memory location, and GDB actually needed
1856 its contents to evaluate the expression, then we
fa4727a6
DJ
1857 must watch it. If the first value returned is
1858 still lazy, that means an error occurred reading it;
1859 watch it anyway in case it becomes readable. */
a5606eee 1860 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1861 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1862 {
1863 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1864
a5606eee
VP
1865 /* We only watch structs and arrays if user asked
1866 for it explicitly, never if they just happen to
1867 appear in the middle of some value chain. */
fa4727a6 1868 if (v == result
a5606eee
VP
1869 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1870 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1871 {
1872 CORE_ADDR addr;
f486487f 1873 enum target_hw_bp_type type;
a5606eee 1874 struct bp_location *loc, **tmp;
bb9d5f81
PP
1875 int bitpos = 0, bitsize = 0;
1876
1877 if (value_bitsize (v) != 0)
1878 {
1879 /* Extract the bit parameters out from the bitfield
1880 sub-expression. */
1881 bitpos = value_bitpos (v);
1882 bitsize = value_bitsize (v);
1883 }
1884 else if (v == result && b->val_bitsize != 0)
1885 {
1886 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1887 lvalue whose bit parameters are saved in the fields
1888 VAL_BITPOS and VAL_BITSIZE. */
1889 bitpos = b->val_bitpos;
1890 bitsize = b->val_bitsize;
1891 }
a5606eee 1892
42ae5230 1893 addr = value_address (v);
bb9d5f81
PP
1894 if (bitsize != 0)
1895 {
1896 /* Skip the bytes that don't contain the bitfield. */
1897 addr += bitpos / 8;
1898 }
1899
a5606eee 1900 type = hw_write;
c1fc2657 1901 if (b->type == bp_read_watchpoint)
a5606eee 1902 type = hw_read;
c1fc2657 1903 else if (b->type == bp_access_watchpoint)
a5606eee 1904 type = hw_access;
3a5c3e22 1905
c1fc2657
SM
1906 loc = allocate_bp_location (b);
1907 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1908 ;
1909 *tmp = loc;
a6d9a66e 1910 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1911
1912 loc->pspace = frame_pspace;
a5606eee 1913 loc->address = addr;
bb9d5f81
PP
1914
1915 if (bitsize != 0)
1916 {
1917 /* Just cover the bytes that make up the bitfield. */
1918 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1919 }
1920 else
1921 loc->length = TYPE_LENGTH (value_type (v));
1922
a5606eee
VP
1923 loc->watchpoint_type = type;
1924 }
1925 }
9fa40276
TJB
1926 }
1927
1928 /* Change the type of breakpoint between hardware assisted or
1929 an ordinary watchpoint depending on the hardware support
1930 and free hardware slots. REPARSE is set when the inferior
1931 is started. */
a9634178 1932 if (reparse)
9fa40276 1933 {
e09342b5 1934 int reg_cnt;
9fa40276
TJB
1935 enum bp_loc_type loc_type;
1936 struct bp_location *bl;
a5606eee 1937
a9634178 1938 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1939
1940 if (reg_cnt)
9fa40276
TJB
1941 {
1942 int i, target_resources_ok, other_type_used;
a1398e0c 1943 enum bptype type;
9fa40276 1944
a9634178
TJB
1945 /* Use an exact watchpoint when there's only one memory region to be
1946 watched, and only one debug register is needed to watch it. */
1947 b->exact = target_exact_watchpoints && reg_cnt == 1;
1948
9fa40276 1949 /* We need to determine how many resources are already
e09342b5
TJB
1950 used for all other hardware watchpoints plus this one
1951 to see if we still have enough resources to also fit
a1398e0c
PA
1952 this watchpoint in as well. */
1953
1954 /* If this is a software watchpoint, we try to turn it
1955 to a hardware one -- count resources as if B was of
1956 hardware watchpoint type. */
c1fc2657 1957 type = b->type;
a1398e0c
PA
1958 if (type == bp_watchpoint)
1959 type = bp_hardware_watchpoint;
1960
1961 /* This watchpoint may or may not have been placed on
1962 the list yet at this point (it won't be in the list
1963 if we're trying to create it for the first time,
1964 through watch_command), so always account for it
1965 manually. */
1966
1967 /* Count resources used by all watchpoints except B. */
c1fc2657 1968 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1969
1970 /* Add in the resources needed for B. */
c1fc2657 1971 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1972
1973 target_resources_ok
1974 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1975 if (target_resources_ok <= 0)
a9634178 1976 {
c1fc2657 1977 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1978
1979 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1980 error (_("Target does not support this type of "
1981 "hardware watchpoint."));
9c06b0b4
TJB
1982 else if (target_resources_ok < 0 && !sw_mode)
1983 error (_("There are not enough available hardware "
1984 "resources for this watchpoint."));
a1398e0c
PA
1985
1986 /* Downgrade to software watchpoint. */
c1fc2657 1987 b->type = bp_watchpoint;
a1398e0c
PA
1988 }
1989 else
1990 {
1991 /* If this was a software watchpoint, we've just
1992 found we have enough resources to turn it to a
1993 hardware watchpoint. Otherwise, this is a
1994 nop. */
c1fc2657 1995 b->type = type;
a9634178 1996 }
9fa40276 1997 }
c1fc2657 1998 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1999 {
2000 if (!can_use_hw_watchpoints)
2001 error (_("Can't set read/access watchpoint when "
2002 "hardware watchpoints are disabled."));
2003 else
2004 error (_("Expression cannot be implemented with "
2005 "read/access watchpoint."));
2006 }
9fa40276 2007 else
c1fc2657 2008 b->type = bp_watchpoint;
9fa40276 2009
c1fc2657 2010 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2011 : bp_loc_hardware_watchpoint);
c1fc2657 2012 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2013 bl->loc_type = loc_type;
2014 }
2015
2016 for (v = val_chain; v; v = next)
2017 {
a5606eee
VP
2018 next = value_next (v);
2019 if (v != b->val)
2020 value_free (v);
2021 }
2022
c7437ca6
PA
2023 /* If a software watchpoint is not watching any memory, then the
2024 above left it without any location set up. But,
2025 bpstat_stop_status requires a location to be able to report
2026 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2027 if (b->type == bp_watchpoint && b->loc == NULL)
2028 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2029 }
2030 else if (!within_current_scope)
7270d8f2 2031 {
ac74f770
MS
2032 printf_filtered (_("\
2033Watchpoint %d deleted because the program has left the block\n\
2034in which its expression is valid.\n"),
c1fc2657 2035 b->number);
d0fb5eae 2036 watchpoint_del_at_next_stop (b);
7270d8f2 2037 }
a5606eee
VP
2038
2039 /* Restore the selected frame. */
66076460
DJ
2040 if (frame_saved)
2041 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2042}
2043
a5606eee 2044
74960c60 2045/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2046 inserted in the inferior. We don't differentiate the type of BL's owner
2047 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2048 breakpoint_ops is not defined, because in insert_bp_location,
2049 tracepoint's insert_location will not be called. */
74960c60 2050static int
35df4500 2051should_be_inserted (struct bp_location *bl)
74960c60 2052{
35df4500 2053 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2054 return 0;
2055
35df4500 2056 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2057 return 0;
2058
35df4500 2059 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2060 return 0;
2061
f8eba3c6
TT
2062 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2063 return 0;
2064
56710373
PA
2065 /* This is set for example, when we're attached to the parent of a
2066 vfork, and have detached from the child. The child is running
2067 free, and we expect it to do an exec or exit, at which point the
2068 OS makes the parent schedulable again (and the target reports
2069 that the vfork is done). Until the child is done with the shared
2070 memory region, do not insert breakpoints in the parent, otherwise
2071 the child could still trip on the parent's breakpoints. Since
2072 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2073 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2074 return 0;
2075
31e77af2 2076 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2077 location, except if the breakpoint is a single-step breakpoint,
2078 and the breakpoint's thread is the thread which is stepping past
2079 a breakpoint. */
31e77af2
PA
2080 if ((bl->loc_type == bp_loc_software_breakpoint
2081 || bl->loc_type == bp_loc_hardware_breakpoint)
2082 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2083 bl->address)
2084 /* The single-step breakpoint may be inserted at the location
2085 we're trying to step if the instruction branches to itself.
2086 However, the instruction won't be executed at all and it may
2087 break the semantics of the instruction, for example, the
2088 instruction is a conditional branch or updates some flags.
2089 We can't fix it unless GDB is able to emulate the instruction
2090 or switch to displaced stepping. */
2091 && !(bl->owner->type == bp_single_step
2092 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2093 {
2094 if (debug_infrun)
2095 {
2096 fprintf_unfiltered (gdb_stdlog,
2097 "infrun: skipping breakpoint: "
2098 "stepping past insn at: %s\n",
2099 paddress (bl->gdbarch, bl->address));
2100 }
2101 return 0;
2102 }
31e77af2 2103
963f9c80
PA
2104 /* Don't insert watchpoints if we're trying to step past the
2105 instruction that triggered one. */
2106 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2107 && stepping_past_nonsteppable_watchpoint ())
2108 {
2109 if (debug_infrun)
2110 {
2111 fprintf_unfiltered (gdb_stdlog,
2112 "infrun: stepping past non-steppable watchpoint. "
2113 "skipping watchpoint at %s:%d\n",
2114 paddress (bl->gdbarch, bl->address),
2115 bl->length);
2116 }
2117 return 0;
2118 }
2119
74960c60
VP
2120 return 1;
2121}
2122
934709f0
PW
2123/* Same as should_be_inserted but does the check assuming
2124 that the location is not duplicated. */
2125
2126static int
2127unduplicated_should_be_inserted (struct bp_location *bl)
2128{
2129 int result;
2130 const int save_duplicate = bl->duplicate;
2131
2132 bl->duplicate = 0;
2133 result = should_be_inserted (bl);
2134 bl->duplicate = save_duplicate;
2135 return result;
2136}
2137
b775012e
LM
2138/* Parses a conditional described by an expression COND into an
2139 agent expression bytecode suitable for evaluation
2140 by the bytecode interpreter. Return NULL if there was
2141 any error during parsing. */
2142
833177a4 2143static agent_expr_up
b775012e
LM
2144parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2145{
833177a4 2146 if (cond == NULL)
b775012e
LM
2147 return NULL;
2148
833177a4
PA
2149 agent_expr_up aexpr;
2150
b775012e
LM
2151 /* We don't want to stop processing, so catch any errors
2152 that may show up. */
492d29ea 2153 TRY
b775012e 2154 {
036e657b 2155 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2156 }
2157
492d29ea 2158 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2159 {
2160 /* If we got here, it means the condition could not be parsed to a valid
2161 bytecode expression and thus can't be evaluated on the target's side.
2162 It's no use iterating through the conditions. */
b775012e 2163 }
492d29ea 2164 END_CATCH
b775012e
LM
2165
2166 /* We have a valid agent expression. */
2167 return aexpr;
2168}
2169
2170/* Based on location BL, create a list of breakpoint conditions to be
2171 passed on to the target. If we have duplicated locations with different
2172 conditions, we will add such conditions to the list. The idea is that the
2173 target will evaluate the list of conditions and will only notify GDB when
2174 one of them is true. */
2175
2176static void
2177build_target_condition_list (struct bp_location *bl)
2178{
2179 struct bp_location **locp = NULL, **loc2p;
2180 int null_condition_or_parse_error = 0;
2181 int modified = bl->needs_update;
2182 struct bp_location *loc;
2183
8b4f3082 2184 /* Release conditions left over from a previous insert. */
3cde5c42 2185 bl->target_info.conditions.clear ();
8b4f3082 2186
b775012e
LM
2187 /* This is only meaningful if the target is
2188 evaluating conditions and if the user has
2189 opted for condition evaluation on the target's
2190 side. */
2191 if (gdb_evaluates_breakpoint_condition_p ()
2192 || !target_supports_evaluation_of_breakpoint_conditions ())
2193 return;
2194
2195 /* Do a first pass to check for locations with no assigned
2196 conditions or conditions that fail to parse to a valid agent expression
2197 bytecode. If any of these happen, then it's no use to send conditions
2198 to the target since this location will always trigger and generate a
2199 response back to GDB. */
2200 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2201 {
2202 loc = (*loc2p);
2203 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2204 {
2205 if (modified)
2206 {
b775012e
LM
2207 /* Re-parse the conditions since something changed. In that
2208 case we already freed the condition bytecodes (see
2209 force_breakpoint_reinsertion). We just
2210 need to parse the condition to bytecodes again. */
833177a4
PA
2211 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2212 loc->cond.get ());
b775012e
LM
2213 }
2214
2215 /* If we have a NULL bytecode expression, it means something
2216 went wrong or we have a null condition expression. */
2217 if (!loc->cond_bytecode)
2218 {
2219 null_condition_or_parse_error = 1;
2220 break;
2221 }
2222 }
2223 }
2224
2225 /* If any of these happened, it means we will have to evaluate the conditions
2226 for the location's address on gdb's side. It is no use keeping bytecodes
2227 for all the other duplicate locations, thus we free all of them here.
2228
2229 This is so we have a finer control over which locations' conditions are
2230 being evaluated by GDB or the remote stub. */
2231 if (null_condition_or_parse_error)
2232 {
2233 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2234 {
2235 loc = (*loc2p);
2236 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2237 {
2238 /* Only go as far as the first NULL bytecode is
2239 located. */
2240 if (!loc->cond_bytecode)
2241 return;
2242
833177a4 2243 loc->cond_bytecode.reset ();
b775012e
LM
2244 }
2245 }
2246 }
2247
2248 /* No NULL conditions or failed bytecode generation. Build a condition list
2249 for this location's address. */
2250 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2251 {
2252 loc = (*loc2p);
2253 if (loc->cond
2254 && is_breakpoint (loc->owner)
2255 && loc->pspace->num == bl->pspace->num
2256 && loc->owner->enable_state == bp_enabled
2257 && loc->enabled)
3cde5c42
PA
2258 {
2259 /* Add the condition to the vector. This will be used later
2260 to send the conditions to the target. */
2261 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2262 }
b775012e
LM
2263 }
2264
2265 return;
2266}
2267
d3ce09f5
SS
2268/* Parses a command described by string CMD into an agent expression
2269 bytecode suitable for evaluation by the bytecode interpreter.
2270 Return NULL if there was any error during parsing. */
2271
833177a4 2272static agent_expr_up
d3ce09f5
SS
2273parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2274{
2275 struct cleanup *old_cleanups = 0;
4d01a485 2276 struct expression **argvec;
bbc13ae3
KS
2277 const char *cmdrest;
2278 const char *format_start, *format_end;
d3ce09f5
SS
2279 struct format_piece *fpieces;
2280 int nargs;
2281 struct gdbarch *gdbarch = get_current_arch ();
2282
833177a4 2283 if (cmd == NULL)
d3ce09f5
SS
2284 return NULL;
2285
2286 cmdrest = cmd;
2287
2288 if (*cmdrest == ',')
2289 ++cmdrest;
f1735a53 2290 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2291
2292 if (*cmdrest++ != '"')
2293 error (_("No format string following the location"));
2294
2295 format_start = cmdrest;
2296
2297 fpieces = parse_format_string (&cmdrest);
2298
2299 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2300
2301 format_end = cmdrest;
2302
2303 if (*cmdrest++ != '"')
2304 error (_("Bad format string, non-terminated '\"'."));
2305
f1735a53 2306 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2307
2308 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2309 error (_("Invalid argument syntax"));
2310
2311 if (*cmdrest == ',')
2312 cmdrest++;
f1735a53 2313 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2314
2315 /* For each argument, make an expression. */
2316
2317 argvec = (struct expression **) alloca (strlen (cmd)
2318 * sizeof (struct expression *));
2319
2320 nargs = 0;
2321 while (*cmdrest != '\0')
2322 {
bbc13ae3 2323 const char *cmd1;
d3ce09f5
SS
2324
2325 cmd1 = cmdrest;
4d01a485
PA
2326 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2327 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2328 cmdrest = cmd1;
2329 if (*cmdrest == ',')
2330 ++cmdrest;
2331 }
2332
833177a4
PA
2333 agent_expr_up aexpr;
2334
d3ce09f5
SS
2335 /* We don't want to stop processing, so catch any errors
2336 that may show up. */
492d29ea 2337 TRY
d3ce09f5 2338 {
036e657b
JB
2339 aexpr = gen_printf (scope, gdbarch, 0, 0,
2340 format_start, format_end - format_start,
2341 fpieces, nargs, argvec);
d3ce09f5 2342 }
492d29ea 2343 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2344 {
2345 /* If we got here, it means the command could not be parsed to a valid
2346 bytecode expression and thus can't be evaluated on the target's side.
2347 It's no use iterating through the other commands. */
d3ce09f5 2348 }
492d29ea
PA
2349 END_CATCH
2350
2351 do_cleanups (old_cleanups);
d3ce09f5 2352
d3ce09f5
SS
2353 /* We have a valid agent expression, return it. */
2354 return aexpr;
2355}
2356
2357/* Based on location BL, create a list of breakpoint commands to be
2358 passed on to the target. If we have duplicated locations with
2359 different commands, we will add any such to the list. */
2360
2361static void
2362build_target_command_list (struct bp_location *bl)
2363{
2364 struct bp_location **locp = NULL, **loc2p;
2365 int null_command_or_parse_error = 0;
2366 int modified = bl->needs_update;
2367 struct bp_location *loc;
2368
3cde5c42
PA
2369 /* Clear commands left over from a previous insert. */
2370 bl->target_info.tcommands.clear ();
8b4f3082 2371
41fac0cf 2372 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2373 return;
2374
41fac0cf
PA
2375 /* For now, limit to agent-style dprintf breakpoints. */
2376 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2377 return;
2378
41fac0cf
PA
2379 /* For now, if we have any duplicate location that isn't a dprintf,
2380 don't install the target-side commands, as that would make the
2381 breakpoint not be reported to the core, and we'd lose
2382 control. */
2383 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2384 {
2385 loc = (*loc2p);
2386 if (is_breakpoint (loc->owner)
2387 && loc->pspace->num == bl->pspace->num
2388 && loc->owner->type != bp_dprintf)
2389 return;
2390 }
2391
d3ce09f5
SS
2392 /* Do a first pass to check for locations with no assigned
2393 conditions or conditions that fail to parse to a valid agent expression
2394 bytecode. If any of these happen, then it's no use to send conditions
2395 to the target since this location will always trigger and generate a
2396 response back to GDB. */
2397 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2398 {
2399 loc = (*loc2p);
2400 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2401 {
2402 if (modified)
2403 {
d3ce09f5
SS
2404 /* Re-parse the commands since something changed. In that
2405 case we already freed the command bytecodes (see
2406 force_breakpoint_reinsertion). We just
2407 need to parse the command to bytecodes again. */
833177a4
PA
2408 loc->cmd_bytecode
2409 = parse_cmd_to_aexpr (bl->address,
2410 loc->owner->extra_string);
d3ce09f5
SS
2411 }
2412
2413 /* If we have a NULL bytecode expression, it means something
2414 went wrong or we have a null command expression. */
2415 if (!loc->cmd_bytecode)
2416 {
2417 null_command_or_parse_error = 1;
2418 break;
2419 }
2420 }
2421 }
2422
2423 /* If anything failed, then we're not doing target-side commands,
2424 and so clean up. */
2425 if (null_command_or_parse_error)
2426 {
2427 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2428 {
2429 loc = (*loc2p);
2430 if (is_breakpoint (loc->owner)
2431 && loc->pspace->num == bl->pspace->num)
2432 {
2433 /* Only go as far as the first NULL bytecode is
2434 located. */
40fb6c5e 2435 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2436 return;
2437
833177a4 2438 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2439 }
2440 }
2441 }
2442
2443 /* No NULL commands or failed bytecode generation. Build a command list
2444 for this location's address. */
2445 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2446 {
2447 loc = (*loc2p);
2448 if (loc->owner->extra_string
2449 && is_breakpoint (loc->owner)
2450 && loc->pspace->num == bl->pspace->num
2451 && loc->owner->enable_state == bp_enabled
2452 && loc->enabled)
3cde5c42
PA
2453 {
2454 /* Add the command to the vector. This will be used later
2455 to send the commands to the target. */
2456 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2457 }
d3ce09f5
SS
2458 }
2459
2460 bl->target_info.persist = 0;
2461 /* Maybe flag this location as persistent. */
2462 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2463 bl->target_info.persist = 1;
2464}
2465
833b7ab5
YQ
2466/* Return the kind of breakpoint on address *ADDR. Get the kind
2467 of breakpoint according to ADDR except single-step breakpoint.
2468 Get the kind of single-step breakpoint according to the current
2469 registers state. */
cd6c3b4f
YQ
2470
2471static int
2472breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2473{
833b7ab5
YQ
2474 if (bl->owner->type == bp_single_step)
2475 {
2476 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2477 struct regcache *regcache;
2478
2479 regcache = get_thread_regcache (thr->ptid);
2480
2481 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2482 regcache, addr);
2483 }
2484 else
2485 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2486}
2487
35df4500
TJB
2488/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2489 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2490 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2491 Returns 0 for success, 1 if the bp_location type is not supported or
2492 -1 for failure.
879bfdc2 2493
4a64f543
MS
2494 NOTE drow/2003-09-09: This routine could be broken down to an
2495 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2496static int
35df4500 2497insert_bp_location (struct bp_location *bl,
26bb91f3 2498 struct ui_file *tmp_error_stream,
3fbb6ffa 2499 int *disabled_breaks,
dd61ec5c
MW
2500 int *hw_breakpoint_error,
2501 int *hw_bp_error_explained_already)
879bfdc2 2502{
0000e5cc
PA
2503 enum errors bp_err = GDB_NO_ERROR;
2504 const char *bp_err_message = NULL;
879bfdc2 2505
b775012e 2506 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2507 return 0;
2508
35c63cd8
JB
2509 /* Note we don't initialize bl->target_info, as that wipes out
2510 the breakpoint location's shadow_contents if the breakpoint
2511 is still inserted at that location. This in turn breaks
2512 target_read_memory which depends on these buffers when
2513 a memory read is requested at the breakpoint location:
2514 Once the target_info has been wiped, we fail to see that
2515 we have a breakpoint inserted at that address and thus
2516 read the breakpoint instead of returning the data saved in
2517 the breakpoint location's shadow contents. */
0d5ed153 2518 bl->target_info.reqstd_address = bl->address;
35df4500 2519 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2520 bl->target_info.length = bl->length;
8181d85f 2521
b775012e
LM
2522 /* When working with target-side conditions, we must pass all the conditions
2523 for the same breakpoint address down to the target since GDB will not
2524 insert those locations. With a list of breakpoint conditions, the target
2525 can decide when to stop and notify GDB. */
2526
2527 if (is_breakpoint (bl->owner))
2528 {
2529 build_target_condition_list (bl);
d3ce09f5
SS
2530 build_target_command_list (bl);
2531 /* Reset the modification marker. */
b775012e
LM
2532 bl->needs_update = 0;
2533 }
2534
35df4500
TJB
2535 if (bl->loc_type == bp_loc_software_breakpoint
2536 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2537 {
35df4500 2538 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2539 {
2540 /* If the explicitly specified breakpoint type
2541 is not hardware breakpoint, check the memory map to see
2542 if the breakpoint address is in read only memory or not.
4a64f543 2543
765dc015
VP
2544 Two important cases are:
2545 - location type is not hardware breakpoint, memory
2546 is readonly. We change the type of the location to
2547 hardware breakpoint.
4a64f543
MS
2548 - location type is hardware breakpoint, memory is
2549 read-write. This means we've previously made the
2550 location hardware one, but then the memory map changed,
2551 so we undo.
765dc015 2552
4a64f543
MS
2553 When breakpoints are removed, remove_breakpoints will use
2554 location types we've just set here, the only possible
2555 problem is that memory map has changed during running
2556 program, but it's not going to work anyway with current
2557 gdb. */
765dc015 2558 struct mem_region *mr
0d5ed153 2559 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2560
2561 if (mr)
2562 {
2563 if (automatic_hardware_breakpoints)
2564 {
765dc015
VP
2565 enum bp_loc_type new_type;
2566
2567 if (mr->attrib.mode != MEM_RW)
2568 new_type = bp_loc_hardware_breakpoint;
2569 else
2570 new_type = bp_loc_software_breakpoint;
2571
35df4500 2572 if (new_type != bl->loc_type)
765dc015
VP
2573 {
2574 static int said = 0;
cc59ec59 2575
35df4500 2576 bl->loc_type = new_type;
765dc015
VP
2577 if (!said)
2578 {
3e43a32a
MS
2579 fprintf_filtered (gdb_stdout,
2580 _("Note: automatically using "
2581 "hardware breakpoints for "
2582 "read-only addresses.\n"));
765dc015
VP
2583 said = 1;
2584 }
2585 }
2586 }
35df4500 2587 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2588 && mr->attrib.mode != MEM_RW)
2589 {
2590 fprintf_unfiltered (tmp_error_stream,
2591 _("Cannot insert breakpoint %d.\n"
2592 "Cannot set software breakpoint "
2593 "at read-only address %s\n"),
2594 bl->owner->number,
2595 paddress (bl->gdbarch, bl->address));
2596 return 1;
2597 }
765dc015
VP
2598 }
2599 }
2600
879bfdc2
DJ
2601 /* First check to see if we have to handle an overlay. */
2602 if (overlay_debugging == ovly_off
35df4500
TJB
2603 || bl->section == NULL
2604 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2605 {
2606 /* No overlay handling: just set the breakpoint. */
492d29ea 2607 TRY
dd61ec5c 2608 {
0000e5cc
PA
2609 int val;
2610
dd61ec5c 2611 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2612 if (val)
2613 bp_err = GENERIC_ERROR;
dd61ec5c 2614 }
492d29ea 2615 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2616 {
0000e5cc
PA
2617 bp_err = e.error;
2618 bp_err_message = e.message;
dd61ec5c 2619 }
492d29ea 2620 END_CATCH
879bfdc2
DJ
2621 }
2622 else
2623 {
4a64f543 2624 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2625 Shall we set a breakpoint at the LMA? */
2626 if (!overlay_events_enabled)
2627 {
2628 /* Yes -- overlay event support is not active,
2629 so we must try to set a breakpoint at the LMA.
2630 This will not work for a hardware breakpoint. */
35df4500 2631 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2632 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2633 bl->owner->number);
879bfdc2
DJ
2634 else
2635 {
35df4500
TJB
2636 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2637 bl->section);
879bfdc2 2638 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2639 bl->overlay_target_info = bl->target_info;
0d5ed153 2640 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2641
2642 /* No overlay handling: just set the breakpoint. */
492d29ea 2643 TRY
0000e5cc
PA
2644 {
2645 int val;
2646
579c6ad9 2647 bl->overlay_target_info.kind
cd6c3b4f
YQ
2648 = breakpoint_kind (bl, &addr);
2649 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2650 val = target_insert_breakpoint (bl->gdbarch,
2651 &bl->overlay_target_info);
2652 if (val)
2653 bp_err = GENERIC_ERROR;
2654 }
492d29ea 2655 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2656 {
2657 bp_err = e.error;
2658 bp_err_message = e.message;
2659 }
492d29ea 2660 END_CATCH
0000e5cc
PA
2661
2662 if (bp_err != GDB_NO_ERROR)
99361f52 2663 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2664 "Overlay breakpoint %d "
2665 "failed: in ROM?\n",
35df4500 2666 bl->owner->number);
879bfdc2
DJ
2667 }
2668 }
2669 /* Shall we set a breakpoint at the VMA? */
35df4500 2670 if (section_is_mapped (bl->section))
879bfdc2
DJ
2671 {
2672 /* Yes. This overlay section is mapped into memory. */
492d29ea 2673 TRY
dd61ec5c 2674 {
0000e5cc
PA
2675 int val;
2676
dd61ec5c 2677 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2678 if (val)
2679 bp_err = GENERIC_ERROR;
dd61ec5c 2680 }
492d29ea 2681 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2682 {
0000e5cc
PA
2683 bp_err = e.error;
2684 bp_err_message = e.message;
dd61ec5c 2685 }
492d29ea 2686 END_CATCH
879bfdc2
DJ
2687 }
2688 else
2689 {
2690 /* No. This breakpoint will not be inserted.
2691 No error, but do not mark the bp as 'inserted'. */
2692 return 0;
2693 }
2694 }
2695
0000e5cc 2696 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2697 {
2698 /* Can't set the breakpoint. */
0000e5cc
PA
2699
2700 /* In some cases, we might not be able to insert a
2701 breakpoint in a shared library that has already been
2702 removed, but we have not yet processed the shlib unload
2703 event. Unfortunately, some targets that implement
076855f9
PA
2704 breakpoint insertion themselves can't tell why the
2705 breakpoint insertion failed (e.g., the remote target
2706 doesn't define error codes), so we must treat generic
2707 errors as memory errors. */
0000e5cc 2708 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2709 && bl->loc_type == bp_loc_software_breakpoint
08351840 2710 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2711 || shared_objfile_contains_address_p (bl->pspace,
2712 bl->address)))
879bfdc2 2713 {
4a64f543 2714 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2715 bl->shlib_disabled = 1;
8d3788bd 2716 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2717 if (!*disabled_breaks)
2718 {
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert breakpoint %d.\n",
2721 bl->owner->number);
2722 fprintf_unfiltered (tmp_error_stream,
2723 "Temporarily disabling shared "
2724 "library breakpoints:\n");
2725 }
2726 *disabled_breaks = 1;
879bfdc2 2727 fprintf_unfiltered (tmp_error_stream,
35df4500 2728 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2729 return 0;
879bfdc2
DJ
2730 }
2731 else
879bfdc2 2732 {
35df4500 2733 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2734 {
0000e5cc
PA
2735 *hw_breakpoint_error = 1;
2736 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2737 fprintf_unfiltered (tmp_error_stream,
2738 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2739 bl->owner->number, bp_err_message ? ":" : ".\n");
2740 if (bp_err_message != NULL)
2741 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2742 }
2743 else
2744 {
0000e5cc
PA
2745 if (bp_err_message == NULL)
2746 {
1ccbe998 2747 std::string message
0000e5cc
PA
2748 = memory_error_message (TARGET_XFER_E_IO,
2749 bl->gdbarch, bl->address);
0000e5cc
PA
2750
2751 fprintf_unfiltered (tmp_error_stream,
2752 "Cannot insert breakpoint %d.\n"
2753 "%s\n",
1ccbe998 2754 bl->owner->number, message.c_str ());
0000e5cc
PA
2755 }
2756 else
2757 {
2758 fprintf_unfiltered (tmp_error_stream,
2759 "Cannot insert breakpoint %d: %s\n",
2760 bl->owner->number,
2761 bp_err_message);
2762 }
879bfdc2 2763 }
0000e5cc 2764 return 1;
879bfdc2
DJ
2765
2766 }
2767 }
2768 else
35df4500 2769 bl->inserted = 1;
879bfdc2 2770
0000e5cc 2771 return 0;
879bfdc2
DJ
2772 }
2773
35df4500 2774 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2775 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2776 watchpoints. It's not clear that it's necessary... */
35df4500 2777 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2778 {
0000e5cc
PA
2779 int val;
2780
77b06cd7
TJB
2781 gdb_assert (bl->owner->ops != NULL
2782 && bl->owner->ops->insert_location != NULL);
2783
2784 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2785
2786 /* If trying to set a read-watchpoint, and it turns out it's not
2787 supported, try emulating one with an access watchpoint. */
35df4500 2788 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2789 {
2790 struct bp_location *loc, **loc_temp;
2791
2792 /* But don't try to insert it, if there's already another
2793 hw_access location that would be considered a duplicate
2794 of this one. */
2795 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2796 if (loc != bl
85d721b8 2797 && loc->watchpoint_type == hw_access
35df4500 2798 && watchpoint_locations_match (bl, loc))
85d721b8 2799 {
35df4500
TJB
2800 bl->duplicate = 1;
2801 bl->inserted = 1;
2802 bl->target_info = loc->target_info;
2803 bl->watchpoint_type = hw_access;
85d721b8
PA
2804 val = 0;
2805 break;
2806 }
2807
2808 if (val == 1)
2809 {
77b06cd7
TJB
2810 bl->watchpoint_type = hw_access;
2811 val = bl->owner->ops->insert_location (bl);
2812
2813 if (val)
2814 /* Back to the original value. */
2815 bl->watchpoint_type = hw_read;
85d721b8
PA
2816 }
2817 }
2818
35df4500 2819 bl->inserted = (val == 0);
879bfdc2
DJ
2820 }
2821
35df4500 2822 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2823 {
0000e5cc
PA
2824 int val;
2825
77b06cd7
TJB
2826 gdb_assert (bl->owner->ops != NULL
2827 && bl->owner->ops->insert_location != NULL);
2828
2829 val = bl->owner->ops->insert_location (bl);
2830 if (val)
2831 {
2832 bl->owner->enable_state = bp_disabled;
2833
2834 if (val == 1)
2835 warning (_("\
2836Error inserting catchpoint %d: Your system does not support this type\n\
2837of catchpoint."), bl->owner->number);
2838 else
2839 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2840 }
2841
2842 bl->inserted = (val == 0);
1640b821
DJ
2843
2844 /* We've already printed an error message if there was a problem
2845 inserting this catchpoint, and we've disabled the catchpoint,
2846 so just return success. */
2847 return 0;
879bfdc2
DJ
2848 }
2849
2850 return 0;
2851}
2852
6c95b8df
PA
2853/* This function is called when program space PSPACE is about to be
2854 deleted. It takes care of updating breakpoints to not reference
2855 PSPACE anymore. */
2856
2857void
2858breakpoint_program_space_exit (struct program_space *pspace)
2859{
2860 struct breakpoint *b, *b_temp;
876fa593 2861 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2862
2863 /* Remove any breakpoint that was set through this program space. */
2864 ALL_BREAKPOINTS_SAFE (b, b_temp)
2865 {
2866 if (b->pspace == pspace)
2867 delete_breakpoint (b);
2868 }
2869
2870 /* Breakpoints set through other program spaces could have locations
2871 bound to PSPACE as well. Remove those. */
876fa593 2872 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2873 {
2874 struct bp_location *tmp;
2875
2876 if (loc->pspace == pspace)
2877 {
2bdf28a0 2878 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2879 if (loc->owner->loc == loc)
2880 loc->owner->loc = loc->next;
2881 else
2882 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2883 if (tmp->next == loc)
2884 {
2885 tmp->next = loc->next;
2886 break;
2887 }
2888 }
2889 }
2890
2891 /* Now update the global location list to permanently delete the
2892 removed locations above. */
44702360 2893 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2894}
2895
74960c60
VP
2896/* Make sure all breakpoints are inserted in inferior.
2897 Throws exception on any error.
2898 A breakpoint that is already inserted won't be inserted
2899 again, so calling this function twice is safe. */
2900void
2901insert_breakpoints (void)
2902{
2903 struct breakpoint *bpt;
2904
2905 ALL_BREAKPOINTS (bpt)
2906 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2907 {
2908 struct watchpoint *w = (struct watchpoint *) bpt;
2909
2910 update_watchpoint (w, 0 /* don't reparse. */);
2911 }
74960c60 2912
04086b45
PA
2913 /* Updating watchpoints creates new locations, so update the global
2914 location list. Explicitly tell ugll to insert locations and
2915 ignore breakpoints_always_inserted_mode. */
2916 update_global_location_list (UGLL_INSERT);
74960c60
VP
2917}
2918
20388dd6
YQ
2919/* Invoke CALLBACK for each of bp_location. */
2920
2921void
2922iterate_over_bp_locations (walk_bp_location_callback callback)
2923{
2924 struct bp_location *loc, **loc_tmp;
2925
2926 ALL_BP_LOCATIONS (loc, loc_tmp)
2927 {
2928 callback (loc, NULL);
2929 }
2930}
2931
b775012e
LM
2932/* This is used when we need to synch breakpoint conditions between GDB and the
2933 target. It is the case with deleting and disabling of breakpoints when using
2934 always-inserted mode. */
2935
2936static void
2937update_inserted_breakpoint_locations (void)
2938{
2939 struct bp_location *bl, **blp_tmp;
2940 int error_flag = 0;
2941 int val = 0;
2942 int disabled_breaks = 0;
2943 int hw_breakpoint_error = 0;
dd61ec5c 2944 int hw_bp_details_reported = 0;
b775012e 2945
d7e74731 2946 string_file tmp_error_stream;
b775012e
LM
2947
2948 /* Explicitly mark the warning -- this will only be printed if
2949 there was an error. */
d7e74731 2950 tmp_error_stream.puts ("Warning:\n");
b775012e 2951
5ed8105e 2952 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2953
2954 ALL_BP_LOCATIONS (bl, blp_tmp)
2955 {
2956 /* We only want to update software breakpoints and hardware
2957 breakpoints. */
2958 if (!is_breakpoint (bl->owner))
2959 continue;
2960
2961 /* We only want to update locations that are already inserted
2962 and need updating. This is to avoid unwanted insertion during
2963 deletion of breakpoints. */
2964 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2965 continue;
2966
2967 switch_to_program_space_and_thread (bl->pspace);
2968
2969 /* For targets that support global breakpoints, there's no need
2970 to select an inferior to insert breakpoint to. In fact, even
2971 if we aren't attached to any process yet, we should still
2972 insert breakpoints. */
f5656ead 2973 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2974 && ptid_equal (inferior_ptid, null_ptid))
2975 continue;
2976
d7e74731 2977 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2978 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2979 if (val)
2980 error_flag = val;
2981 }
2982
2983 if (error_flag)
2984 {
223ffa71 2985 target_terminal::ours_for_output ();
b775012e
LM
2986 error_stream (tmp_error_stream);
2987 }
b775012e
LM
2988}
2989
c30eee59 2990/* Used when starting or continuing the program. */
c906108c 2991
74960c60
VP
2992static void
2993insert_breakpoint_locations (void)
c906108c 2994{
a5606eee 2995 struct breakpoint *bpt;
35df4500 2996 struct bp_location *bl, **blp_tmp;
eacd795a 2997 int error_flag = 0;
c906108c 2998 int val = 0;
3fbb6ffa 2999 int disabled_breaks = 0;
81d0cc19 3000 int hw_breakpoint_error = 0;
dd61ec5c 3001 int hw_bp_error_explained_already = 0;
c906108c 3002
d7e74731
PA
3003 string_file tmp_error_stream;
3004
81d0cc19
GS
3005 /* Explicitly mark the warning -- this will only be printed if
3006 there was an error. */
d7e74731 3007 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3008
5ed8105e 3009 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3010
35df4500 3011 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3012 {
b775012e 3013 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3014 continue;
3015
4a64f543
MS
3016 /* There is no point inserting thread-specific breakpoints if
3017 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3018 has BL->OWNER always non-NULL. */
35df4500 3019 if (bl->owner->thread != -1
5d5658a1 3020 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3021 continue;
3022
35df4500 3023 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3024
3025 /* For targets that support global breakpoints, there's no need
3026 to select an inferior to insert breakpoint to. In fact, even
3027 if we aren't attached to any process yet, we should still
3028 insert breakpoints. */
f5656ead 3029 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3030 && ptid_equal (inferior_ptid, null_ptid))
3031 continue;
3032
d7e74731 3033 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3034 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3035 if (val)
eacd795a 3036 error_flag = val;
879bfdc2 3037 }
c906108c 3038
4a64f543
MS
3039 /* If we failed to insert all locations of a watchpoint, remove
3040 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3041 ALL_BREAKPOINTS (bpt)
3042 {
3043 int some_failed = 0;
3044 struct bp_location *loc;
3045
3046 if (!is_hardware_watchpoint (bpt))
3047 continue;
3048
d6b74ac4 3049 if (!breakpoint_enabled (bpt))
a5606eee 3050 continue;
74960c60
VP
3051
3052 if (bpt->disposition == disp_del_at_next_stop)
3053 continue;
a5606eee
VP
3054
3055 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3056 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3057 {
3058 some_failed = 1;
3059 break;
3060 }
3061 if (some_failed)
3062 {
3063 for (loc = bpt->loc; loc; loc = loc->next)
3064 if (loc->inserted)
834c0d03 3065 remove_breakpoint (loc);
a5606eee
VP
3066
3067 hw_breakpoint_error = 1;
d7e74731
PA
3068 tmp_error_stream.printf ("Could not insert "
3069 "hardware watchpoint %d.\n",
3070 bpt->number);
eacd795a 3071 error_flag = -1;
a5606eee
VP
3072 }
3073 }
3074
eacd795a 3075 if (error_flag)
81d0cc19
GS
3076 {
3077 /* If a hardware breakpoint or watchpoint was inserted, add a
3078 message about possibly exhausted resources. */
dd61ec5c 3079 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3080 {
d7e74731 3081 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3082You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3083 }
223ffa71 3084 target_terminal::ours_for_output ();
81d0cc19
GS
3085 error_stream (tmp_error_stream);
3086 }
c906108c
SS
3087}
3088
c30eee59
TJB
3089/* Used when the program stops.
3090 Returns zero if successful, or non-zero if there was a problem
3091 removing a breakpoint location. */
3092
c906108c 3093int
fba45db2 3094remove_breakpoints (void)
c906108c 3095{
35df4500 3096 struct bp_location *bl, **blp_tmp;
3a1bae8e 3097 int val = 0;
c906108c 3098
35df4500 3099 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3100 {
1e4d1764 3101 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3102 val |= remove_breakpoint (bl);
c5aa993b 3103 }
3a1bae8e 3104 return val;
c906108c
SS
3105}
3106
49fa26b0
PA
3107/* When a thread exits, remove breakpoints that are related to
3108 that thread. */
3109
3110static void
3111remove_threaded_breakpoints (struct thread_info *tp, int silent)
3112{
3113 struct breakpoint *b, *b_tmp;
3114
3115 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3116 {
5d5658a1 3117 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3118 {
3119 b->disposition = disp_del_at_next_stop;
3120
3121 printf_filtered (_("\
43792cf0
PA
3122Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3123 b->number, print_thread_id (tp));
49fa26b0
PA
3124
3125 /* Hide it from the user. */
3126 b->number = 0;
3127 }
3128 }
3129}
3130
6c95b8df
PA
3131/* Remove breakpoints of process PID. */
3132
3133int
3134remove_breakpoints_pid (int pid)
3135{
35df4500 3136 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3137 int val;
3138 struct inferior *inf = find_inferior_pid (pid);
3139
35df4500 3140 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3141 {
35df4500 3142 if (bl->pspace != inf->pspace)
6c95b8df
PA
3143 continue;
3144
fc126975 3145 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3146 {
834c0d03 3147 val = remove_breakpoint (bl);
6c95b8df
PA
3148 if (val != 0)
3149 return val;
3150 }
3151 }
3152 return 0;
3153}
3154
e58b0e63
PA
3155static int internal_breakpoint_number = -1;
3156
84f4c1fe
PM
3157/* Set the breakpoint number of B, depending on the value of INTERNAL.
3158 If INTERNAL is non-zero, the breakpoint number will be populated
3159 from internal_breakpoint_number and that variable decremented.
e5dd4106 3160 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3161 breakpoint_count and that value incremented. Internal breakpoints
3162 do not set the internal var bpnum. */
3163static void
3164set_breakpoint_number (int internal, struct breakpoint *b)
3165{
3166 if (internal)
3167 b->number = internal_breakpoint_number--;
3168 else
3169 {
3170 set_breakpoint_count (breakpoint_count + 1);
3171 b->number = breakpoint_count;
3172 }
3173}
3174
e62c965a 3175static struct breakpoint *
a6d9a66e 3176create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3177 CORE_ADDR address, enum bptype type,
c0a91b2b 3178 const struct breakpoint_ops *ops)
e62c965a 3179{
51abb421 3180 symtab_and_line sal;
e62c965a
PP
3181 sal.pc = address;
3182 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3183 sal.pspace = current_program_space;
e62c965a 3184
51abb421 3185 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3186 b->number = internal_breakpoint_number--;
3187 b->disposition = disp_donttouch;
3188
3189 return b;
3190}
3191
17450429
PP
3192static const char *const longjmp_names[] =
3193 {
3194 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3195 };
3196#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3197
3198/* Per-objfile data private to breakpoint.c. */
3199struct breakpoint_objfile_data
3200{
3201 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3202 struct bound_minimal_symbol overlay_msym;
17450429
PP
3203
3204 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3205 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3206
28106bc2
SDJ
3207 /* True if we have looked for longjmp probes. */
3208 int longjmp_searched;
3209
3210 /* SystemTap probe points for longjmp (if any). */
3211 VEC (probe_p) *longjmp_probes;
3212
17450429 3213 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3214 struct bound_minimal_symbol terminate_msym;
17450429
PP
3215
3216 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3217 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3218
3219 /* True if we have looked for exception probes. */
3220 int exception_searched;
3221
3222 /* SystemTap probe points for unwinding (if any). */
3223 VEC (probe_p) *exception_probes;
17450429
PP
3224};
3225
3226static const struct objfile_data *breakpoint_objfile_key;
3227
3228/* Minimal symbol not found sentinel. */
3229static struct minimal_symbol msym_not_found;
3230
3231/* Returns TRUE if MSYM point to the "not found" sentinel. */
3232
3233static int
3234msym_not_found_p (const struct minimal_symbol *msym)
3235{
3236 return msym == &msym_not_found;
3237}
3238
3239/* Return per-objfile data needed by breakpoint.c.
3240 Allocate the data if necessary. */
3241
3242static struct breakpoint_objfile_data *
3243get_breakpoint_objfile_data (struct objfile *objfile)
3244{
3245 struct breakpoint_objfile_data *bp_objfile_data;
3246
9a3c8263
SM
3247 bp_objfile_data = ((struct breakpoint_objfile_data *)
3248 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3249 if (bp_objfile_data == NULL)
3250 {
8d749320
SM
3251 bp_objfile_data =
3252 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3253
3254 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3255 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3256 }
3257 return bp_objfile_data;
3258}
3259
28106bc2
SDJ
3260static void
3261free_breakpoint_probes (struct objfile *obj, void *data)
3262{
9a3c8263
SM
3263 struct breakpoint_objfile_data *bp_objfile_data
3264 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3265
3266 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3267 VEC_free (probe_p, bp_objfile_data->exception_probes);
3268}
3269
e62c965a 3270static void
af02033e 3271create_overlay_event_breakpoint (void)
e62c965a 3272{
69de3c6a 3273 struct objfile *objfile;
af02033e 3274 const char *const func_name = "_ovly_debug_event";
e62c965a 3275
69de3c6a
PP
3276 ALL_OBJFILES (objfile)
3277 {
3278 struct breakpoint *b;
17450429
PP
3279 struct breakpoint_objfile_data *bp_objfile_data;
3280 CORE_ADDR addr;
67994074 3281 struct explicit_location explicit_loc;
69de3c6a 3282
17450429
PP
3283 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3284
3b7344d5 3285 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3286 continue;
3287
3b7344d5 3288 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3289 {
3b7344d5 3290 struct bound_minimal_symbol m;
17450429
PP
3291
3292 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3293 if (m.minsym == NULL)
17450429
PP
3294 {
3295 /* Avoid future lookups in this objfile. */
3b7344d5 3296 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3297 continue;
3298 }
3299 bp_objfile_data->overlay_msym = m;
3300 }
e62c965a 3301
77e371c0 3302 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3303 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3304 bp_overlay_event,
3305 &internal_breakpoint_ops);
67994074
KS
3306 initialize_explicit_location (&explicit_loc);
3307 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3308 b->location = new_explicit_location (&explicit_loc);
e62c965a 3309
69de3c6a
PP
3310 if (overlay_debugging == ovly_auto)
3311 {
3312 b->enable_state = bp_enabled;
3313 overlay_events_enabled = 1;
3314 }
3315 else
3316 {
3317 b->enable_state = bp_disabled;
3318 overlay_events_enabled = 0;
3319 }
e62c965a 3320 }
e62c965a
PP
3321}
3322
0fd8e87f 3323static void
af02033e 3324create_longjmp_master_breakpoint (void)
0fd8e87f 3325{
6c95b8df 3326 struct program_space *pspace;
6c95b8df 3327
5ed8105e 3328 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3329
6c95b8df 3330 ALL_PSPACES (pspace)
af02033e
PP
3331 {
3332 struct objfile *objfile;
3333
3334 set_current_program_space (pspace);
3335
3336 ALL_OBJFILES (objfile)
0fd8e87f 3337 {
af02033e
PP
3338 int i;
3339 struct gdbarch *gdbarch;
17450429 3340 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3341
af02033e 3342 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3343
17450429
PP
3344 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3345
28106bc2
SDJ
3346 if (!bp_objfile_data->longjmp_searched)
3347 {
25f9533e
SDJ
3348 VEC (probe_p) *ret;
3349
3350 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3351 if (ret != NULL)
3352 {
3353 /* We are only interested in checking one element. */
3354 struct probe *p = VEC_index (probe_p, ret, 0);
3355
3356 if (!can_evaluate_probe_arguments (p))
3357 {
3358 /* We cannot use the probe interface here, because it does
3359 not know how to evaluate arguments. */
3360 VEC_free (probe_p, ret);
3361 ret = NULL;
3362 }
3363 }
3364 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3365 bp_objfile_data->longjmp_searched = 1;
3366 }
3367
3368 if (bp_objfile_data->longjmp_probes != NULL)
3369 {
3370 int i;
3371 struct probe *probe;
3372 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3373
3374 for (i = 0;
3375 VEC_iterate (probe_p,
3376 bp_objfile_data->longjmp_probes,
3377 i, probe);
3378 ++i)
3379 {
3380 struct breakpoint *b;
3381
729662a5
TT
3382 b = create_internal_breakpoint (gdbarch,
3383 get_probe_address (probe,
3384 objfile),
28106bc2
SDJ
3385 bp_longjmp_master,
3386 &internal_breakpoint_ops);
d28cd78a 3387 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3388 b->enable_state = bp_disabled;
3389 }
3390
3391 continue;
3392 }
3393
0569175e
TSD
3394 if (!gdbarch_get_longjmp_target_p (gdbarch))
3395 continue;
3396
17450429 3397 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3398 {
3399 struct breakpoint *b;
af02033e 3400 const char *func_name;
17450429 3401 CORE_ADDR addr;
67994074 3402 struct explicit_location explicit_loc;
6c95b8df 3403
3b7344d5 3404 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3405 continue;
0fd8e87f 3406
17450429 3407 func_name = longjmp_names[i];
3b7344d5 3408 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3409 {
3b7344d5 3410 struct bound_minimal_symbol m;
17450429
PP
3411
3412 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3413 if (m.minsym == NULL)
17450429
PP
3414 {
3415 /* Prevent future lookups in this objfile. */
3b7344d5 3416 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3417 continue;
3418 }
3419 bp_objfile_data->longjmp_msym[i] = m;
3420 }
3421
77e371c0 3422 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3423 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3424 &internal_breakpoint_ops);
67994074
KS
3425 initialize_explicit_location (&explicit_loc);
3426 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3427 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3428 b->enable_state = bp_disabled;
3429 }
0fd8e87f 3430 }
af02033e 3431 }
0fd8e87f
UW
3432}
3433
af02033e 3434/* Create a master std::terminate breakpoint. */
aa7d318d 3435static void
af02033e 3436create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3437{
3438 struct program_space *pspace;
af02033e 3439 const char *const func_name = "std::terminate()";
aa7d318d 3440
5ed8105e 3441 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3442
3443 ALL_PSPACES (pspace)
17450429
PP
3444 {
3445 struct objfile *objfile;
3446 CORE_ADDR addr;
3447
3448 set_current_program_space (pspace);
3449
aa7d318d
TT
3450 ALL_OBJFILES (objfile)
3451 {
3452 struct breakpoint *b;
17450429 3453 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3454 struct explicit_location explicit_loc;
aa7d318d 3455
17450429 3456 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3457
3b7344d5 3458 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3459 continue;
3460
3b7344d5 3461 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3462 {
3b7344d5 3463 struct bound_minimal_symbol m;
17450429
PP
3464
3465 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3466 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3467 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3468 {
3469 /* Prevent future lookups in this objfile. */
3b7344d5 3470 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3471 continue;
3472 }
3473 bp_objfile_data->terminate_msym = m;
3474 }
aa7d318d 3475
77e371c0 3476 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3477 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3478 bp_std_terminate_master,
3479 &internal_breakpoint_ops);
67994074
KS
3480 initialize_explicit_location (&explicit_loc);
3481 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3482 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3483 b->enable_state = bp_disabled;
3484 }
17450429 3485 }
aa7d318d
TT
3486}
3487
186c406b
TT
3488/* Install a master breakpoint on the unwinder's debug hook. */
3489
70221824 3490static void
186c406b
TT
3491create_exception_master_breakpoint (void)
3492{
3493 struct objfile *objfile;
17450429 3494 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3495
3496 ALL_OBJFILES (objfile)
3497 {
17450429
PP
3498 struct breakpoint *b;
3499 struct gdbarch *gdbarch;
3500 struct breakpoint_objfile_data *bp_objfile_data;
3501 CORE_ADDR addr;
67994074 3502 struct explicit_location explicit_loc;
17450429
PP
3503
3504 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3505
28106bc2
SDJ
3506 /* We prefer the SystemTap probe point if it exists. */
3507 if (!bp_objfile_data->exception_searched)
3508 {
25f9533e
SDJ
3509 VEC (probe_p) *ret;
3510
3511 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3512
3513 if (ret != NULL)
3514 {
3515 /* We are only interested in checking one element. */
3516 struct probe *p = VEC_index (probe_p, ret, 0);
3517
3518 if (!can_evaluate_probe_arguments (p))
3519 {
3520 /* We cannot use the probe interface here, because it does
3521 not know how to evaluate arguments. */
3522 VEC_free (probe_p, ret);
3523 ret = NULL;
3524 }
3525 }
3526 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3527 bp_objfile_data->exception_searched = 1;
3528 }
3529
3530 if (bp_objfile_data->exception_probes != NULL)
3531 {
3532 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3533 int i;
3534 struct probe *probe;
3535
3536 for (i = 0;
3537 VEC_iterate (probe_p,
3538 bp_objfile_data->exception_probes,
3539 i, probe);
3540 ++i)
3541 {
3542 struct breakpoint *b;
3543
729662a5
TT
3544 b = create_internal_breakpoint (gdbarch,
3545 get_probe_address (probe,
3546 objfile),
28106bc2
SDJ
3547 bp_exception_master,
3548 &internal_breakpoint_ops);
d28cd78a 3549 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3550 b->enable_state = bp_disabled;
3551 }
3552
3553 continue;
3554 }
3555
3556 /* Otherwise, try the hook function. */
3557
3b7344d5 3558 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3559 continue;
3560
3561 gdbarch = get_objfile_arch (objfile);
186c406b 3562
3b7344d5 3563 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3564 {
3b7344d5 3565 struct bound_minimal_symbol debug_hook;
186c406b 3566
17450429 3567 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3568 if (debug_hook.minsym == NULL)
17450429 3569 {
3b7344d5 3570 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3571 continue;
3572 }
3573
3574 bp_objfile_data->exception_msym = debug_hook;
186c406b 3575 }
17450429 3576
77e371c0 3577 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3578 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3579 &current_target);
06edf0c0
PA
3580 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3581 &internal_breakpoint_ops);
67994074
KS
3582 initialize_explicit_location (&explicit_loc);
3583 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3584 b->location = new_explicit_location (&explicit_loc);
17450429 3585 b->enable_state = bp_disabled;
186c406b 3586 }
186c406b
TT
3587}
3588
9ef9e6a6
KS
3589/* Does B have a location spec? */
3590
3591static int
3592breakpoint_event_location_empty_p (const struct breakpoint *b)
3593{
d28cd78a 3594 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3595}
3596
c906108c 3597void
fba45db2 3598update_breakpoints_after_exec (void)
c906108c 3599{
35df4500 3600 struct breakpoint *b, *b_tmp;
876fa593 3601 struct bp_location *bploc, **bplocp_tmp;
c906108c 3602
25b22b0a
PA
3603 /* We're about to delete breakpoints from GDB's lists. If the
3604 INSERTED flag is true, GDB will try to lift the breakpoints by
3605 writing the breakpoints' "shadow contents" back into memory. The
3606 "shadow contents" are NOT valid after an exec, so GDB should not
3607 do that. Instead, the target is responsible from marking
3608 breakpoints out as soon as it detects an exec. We don't do that
3609 here instead, because there may be other attempts to delete
3610 breakpoints after detecting an exec and before reaching here. */
876fa593 3611 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3612 if (bploc->pspace == current_program_space)
3613 gdb_assert (!bploc->inserted);
c906108c 3614
35df4500 3615 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3616 {
6c95b8df
PA
3617 if (b->pspace != current_program_space)
3618 continue;
3619
4a64f543 3620 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3621 if (b->type == bp_shlib_event)
3622 {
3623 delete_breakpoint (b);
3624 continue;
3625 }
c906108c 3626
4a64f543 3627 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3628 if (b->type == bp_jit_event)
3629 {
3630 delete_breakpoint (b);
3631 continue;
3632 }
3633
1900040c 3634 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3635 as must overlay event and longjmp master breakpoints. */
3636 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3637 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3638 || b->type == bp_exception_master)
c4093a6a
JM
3639 {
3640 delete_breakpoint (b);
3641 continue;
3642 }
3643
4a64f543 3644 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3645 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3646 {
3647 delete_breakpoint (b);
3648 continue;
3649 }
3650
7c16b83e
PA
3651 /* Just like single-step breakpoints. */
3652 if (b->type == bp_single_step)
3653 {
3654 delete_breakpoint (b);
3655 continue;
3656 }
3657
611c83ae
PA
3658 /* Longjmp and longjmp-resume breakpoints are also meaningless
3659 after an exec. */
186c406b 3660 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3661 || b->type == bp_longjmp_call_dummy
186c406b 3662 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3663 {
3664 delete_breakpoint (b);
3665 continue;
3666 }
3667
ce78b96d
JB
3668 if (b->type == bp_catchpoint)
3669 {
3670 /* For now, none of the bp_catchpoint breakpoints need to
3671 do anything at this point. In the future, if some of
3672 the catchpoints need to something, we will need to add
3673 a new method, and call this method from here. */
3674 continue;
3675 }
3676
c5aa993b
JM
3677 /* bp_finish is a special case. The only way we ought to be able
3678 to see one of these when an exec() has happened, is if the user
3679 caught a vfork, and then said "finish". Ordinarily a finish just
3680 carries them to the call-site of the current callee, by setting
3681 a temporary bp there and resuming. But in this case, the finish
3682 will carry them entirely through the vfork & exec.
3683
3684 We don't want to allow a bp_finish to remain inserted now. But
3685 we can't safely delete it, 'cause finish_command has a handle to
3686 the bp on a bpstat, and will later want to delete it. There's a
3687 chance (and I've seen it happen) that if we delete the bp_finish
3688 here, that its storage will get reused by the time finish_command
3689 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3690 We really must allow finish_command to delete a bp_finish.
3691
e5dd4106 3692 In the absence of a general solution for the "how do we know
53a5351d
JM
3693 it's safe to delete something others may have handles to?"
3694 problem, what we'll do here is just uninsert the bp_finish, and
3695 let finish_command delete it.
3696
3697 (We know the bp_finish is "doomed" in the sense that it's
3698 momentary, and will be deleted as soon as finish_command sees
3699 the inferior stopped. So it doesn't matter that the bp's
3700 address is probably bogus in the new a.out, unlike e.g., the
3701 solib breakpoints.) */
c5aa993b 3702
c5aa993b
JM
3703 if (b->type == bp_finish)
3704 {
3705 continue;
3706 }
3707
3708 /* Without a symbolic address, we have little hope of the
3709 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3710 a.out. */
9ef9e6a6 3711 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3712 {
3713 delete_breakpoint (b);
3714 continue;
3715 }
c5aa993b 3716 }
c906108c
SS
3717}
3718
3719int
d80ee84f 3720detach_breakpoints (ptid_t ptid)
c906108c 3721{
35df4500 3722 struct bp_location *bl, **blp_tmp;
3a1bae8e 3723 int val = 0;
2989a365 3724 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3725 struct inferior *inf = current_inferior ();
c5aa993b 3726
dfd4cc63 3727 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3728 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3729
6c95b8df 3730 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3731 inferior_ptid = ptid;
35df4500 3732 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3733 {
35df4500 3734 if (bl->pspace != inf->pspace)
6c95b8df
PA
3735 continue;
3736
bd9673a4
PW
3737 /* This function must physically remove breakpoints locations
3738 from the specified ptid, without modifying the breakpoint
3739 package's state. Locations of type bp_loc_other are only
3740 maintained at GDB side. So, there is no need to remove
3741 these bp_loc_other locations. Moreover, removing these
3742 would modify the breakpoint package's state. */
3743 if (bl->loc_type == bp_loc_other)
3744 continue;
3745
35df4500 3746 if (bl->inserted)
b2b6a7da 3747 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3748 }
d03285ec 3749
3a1bae8e 3750 return val;
c906108c
SS
3751}
3752
35df4500 3753/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3754 Note that this is used to detach breakpoints from a child fork.
3755 When we get here, the child isn't in the inferior list, and neither
3756 do we have objects to represent its address space --- we should
35df4500 3757 *not* look at bl->pspace->aspace here. */
6c95b8df 3758
c906108c 3759static int
b2b6a7da 3760remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3761{
3762 int val;
c5aa993b 3763
35df4500
TJB
3764 /* BL is never in moribund_locations by our callers. */
3765 gdb_assert (bl->owner != NULL);
2bdf28a0 3766
74960c60
VP
3767 /* The type of none suggests that owner is actually deleted.
3768 This should not ever happen. */
35df4500 3769 gdb_assert (bl->owner->type != bp_none);
0bde7532 3770
35df4500
TJB
3771 if (bl->loc_type == bp_loc_software_breakpoint
3772 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3773 {
c02f5703
MS
3774 /* "Normal" instruction breakpoint: either the standard
3775 trap-instruction bp (bp_breakpoint), or a
3776 bp_hardware_breakpoint. */
3777
3778 /* First check to see if we have to handle an overlay. */
3779 if (overlay_debugging == ovly_off
35df4500
TJB
3780 || bl->section == NULL
3781 || !(section_is_overlay (bl->section)))
c02f5703
MS
3782 {
3783 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3784
3785 /* If we're trying to uninsert a memory breakpoint that we
3786 know is set in a dynamic object that is marked
3787 shlib_disabled, then either the dynamic object was
3788 removed with "remove-symbol-file" or with
3789 "nosharedlibrary". In the former case, we don't know
3790 whether another dynamic object might have loaded over the
3791 breakpoint's address -- the user might well let us know
3792 about it next with add-symbol-file (the whole point of
d03de421 3793 add-symbol-file is letting the user manually maintain a
08351840
PA
3794 list of dynamically loaded objects). If we have the
3795 breakpoint's shadow memory, that is, this is a software
3796 breakpoint managed by GDB, check whether the breakpoint
3797 is still inserted in memory, to avoid overwriting wrong
3798 code with stale saved shadow contents. Note that HW
3799 breakpoints don't have shadow memory, as they're
3800 implemented using a mechanism that is not dependent on
3801 being able to modify the target's memory, and as such
3802 they should always be removed. */
3803 if (bl->shlib_disabled
3804 && bl->target_info.shadow_len != 0
3805 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3806 val = 0;
3807 else
73971819 3808 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3809 }
c906108c
SS
3810 else
3811 {
4a64f543 3812 /* This breakpoint is in an overlay section.
c02f5703
MS
3813 Did we set a breakpoint at the LMA? */
3814 if (!overlay_events_enabled)
3815 {
3816 /* Yes -- overlay event support is not active, so we
3817 should have set a breakpoint at the LMA. Remove it.
3818 */
c02f5703
MS
3819 /* Ignore any failures: if the LMA is in ROM, we will
3820 have already warned when we failed to insert it. */
35df4500
TJB
3821 if (bl->loc_type == bp_loc_hardware_breakpoint)
3822 target_remove_hw_breakpoint (bl->gdbarch,
3823 &bl->overlay_target_info);
c02f5703 3824 else
35df4500 3825 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3826 &bl->overlay_target_info,
3827 reason);
c02f5703
MS
3828 }
3829 /* Did we set a breakpoint at the VMA?
3830 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3831 if (bl->inserted)
c906108c 3832 {
c02f5703
MS
3833 /* Yes -- remove it. Previously we did not bother to
3834 remove the breakpoint if the section had been
3835 unmapped, but let's not rely on that being safe. We
3836 don't know what the overlay manager might do. */
aa67235e
UW
3837
3838 /* However, we should remove *software* breakpoints only
3839 if the section is still mapped, or else we overwrite
3840 wrong code with the saved shadow contents. */
348d480f
PA
3841 if (bl->loc_type == bp_loc_hardware_breakpoint
3842 || section_is_mapped (bl->section))
73971819 3843 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3844 else
3845 val = 0;
c906108c 3846 }
c02f5703
MS
3847 else
3848 {
3849 /* No -- not inserted, so no need to remove. No error. */
3850 val = 0;
3851 }
c906108c 3852 }
879d1e6b 3853
08351840
PA
3854 /* In some cases, we might not be able to remove a breakpoint in
3855 a shared library that has already been removed, but we have
3856 not yet processed the shlib unload event. Similarly for an
3857 unloaded add-symbol-file object - the user might not yet have
3858 had the chance to remove-symbol-file it. shlib_disabled will
3859 be set if the library/object has already been removed, but
3860 the breakpoint hasn't been uninserted yet, e.g., after
3861 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3862 always-inserted mode. */
076855f9 3863 if (val
08351840
PA
3864 && (bl->loc_type == bp_loc_software_breakpoint
3865 && (bl->shlib_disabled
3866 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3867 || shared_objfile_contains_address_p (bl->pspace,
3868 bl->address))))
879d1e6b
UW
3869 val = 0;
3870
c906108c
SS
3871 if (val)
3872 return val;
b2b6a7da 3873 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3874 }
35df4500 3875 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3876 {
77b06cd7
TJB
3877 gdb_assert (bl->owner->ops != NULL
3878 && bl->owner->ops->remove_location != NULL);
3879
b2b6a7da 3880 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3881 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3882
c906108c 3883 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3884 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3885 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3886 bl->owner->number);
c906108c 3887 }
35df4500
TJB
3888 else if (bl->owner->type == bp_catchpoint
3889 && breakpoint_enabled (bl->owner)
3890 && !bl->duplicate)
ce78b96d 3891 {
77b06cd7
TJB
3892 gdb_assert (bl->owner->ops != NULL
3893 && bl->owner->ops->remove_location != NULL);
ce78b96d 3894
73971819 3895 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3896 if (val)
3897 return val;
77b06cd7 3898
b2b6a7da 3899 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3900 }
c906108c
SS
3901
3902 return 0;
3903}
3904
6c95b8df 3905static int
834c0d03 3906remove_breakpoint (struct bp_location *bl)
6c95b8df 3907{
35df4500
TJB
3908 /* BL is never in moribund_locations by our callers. */
3909 gdb_assert (bl->owner != NULL);
2bdf28a0 3910
6c95b8df
PA
3911 /* The type of none suggests that owner is actually deleted.
3912 This should not ever happen. */
35df4500 3913 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3914
5ed8105e 3915 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3916
35df4500 3917 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3918
5ed8105e 3919 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3920}
3921
c906108c
SS
3922/* Clear the "inserted" flag in all breakpoints. */
3923
25b22b0a 3924void
fba45db2 3925mark_breakpoints_out (void)
c906108c 3926{
35df4500 3927 struct bp_location *bl, **blp_tmp;
c906108c 3928
35df4500 3929 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3930 if (bl->pspace == current_program_space)
35df4500 3931 bl->inserted = 0;
c906108c
SS
3932}
3933
53a5351d
JM
3934/* Clear the "inserted" flag in all breakpoints and delete any
3935 breakpoints which should go away between runs of the program.
c906108c
SS
3936
3937 Plus other such housekeeping that has to be done for breakpoints
3938 between runs.
3939
53a5351d
JM
3940 Note: this function gets called at the end of a run (by
3941 generic_mourn_inferior) and when a run begins (by
4a64f543 3942 init_wait_for_inferior). */
c906108c
SS
3943
3944
3945
3946void
fba45db2 3947breakpoint_init_inferior (enum inf_context context)
c906108c 3948{
35df4500 3949 struct breakpoint *b, *b_tmp;
870f88f7 3950 struct bp_location *bl;
1c5cfe86 3951 int ix;
6c95b8df 3952 struct program_space *pspace = current_program_space;
c906108c 3953
50c71eaf
PA
3954 /* If breakpoint locations are shared across processes, then there's
3955 nothing to do. */
f5656ead 3956 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3957 return;
3958
1a853c52 3959 mark_breakpoints_out ();
075f6582 3960
35df4500 3961 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3962 {
6c95b8df
PA
3963 if (b->loc && b->loc->pspace != pspace)
3964 continue;
3965
c5aa993b
JM
3966 switch (b->type)
3967 {
3968 case bp_call_dummy:
e2e4d78b 3969 case bp_longjmp_call_dummy:
c906108c 3970
c5aa993b 3971 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3972 cause problems when the inferior is rerun, so we better get
3973 rid of it. */
3974
3975 case bp_watchpoint_scope:
3976
3977 /* Also get rid of scope breakpoints. */
3978
3979 case bp_shlib_event:
3980
3981 /* Also remove solib event breakpoints. Their addresses may
3982 have changed since the last time we ran the program.
3983 Actually we may now be debugging against different target;
3984 and so the solib backend that installed this breakpoint may
3985 not be used in by the target. E.g.,
3986
3987 (gdb) file prog-linux
3988 (gdb) run # native linux target
3989 ...
3990 (gdb) kill
3991 (gdb) file prog-win.exe
3992 (gdb) tar rem :9999 # remote Windows gdbserver.
3993 */
c906108c 3994
f59f708a
PA
3995 case bp_step_resume:
3996
3997 /* Also remove step-resume breakpoints. */
3998
7c16b83e
PA
3999 case bp_single_step:
4000
4001 /* Also remove single-step breakpoints. */
4002
c5aa993b
JM
4003 delete_breakpoint (b);
4004 break;
c906108c 4005
c5aa993b
JM
4006 case bp_watchpoint:
4007 case bp_hardware_watchpoint:
4008 case bp_read_watchpoint:
4009 case bp_access_watchpoint:
3a5c3e22
PA
4010 {
4011 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4012
3a5c3e22
PA
4013 /* Likewise for watchpoints on local expressions. */
4014 if (w->exp_valid_block != NULL)
4015 delete_breakpoint (b);
63000888 4016 else
3a5c3e22 4017 {
63000888
PA
4018 /* Get rid of existing locations, which are no longer
4019 valid. New ones will be created in
4020 update_watchpoint, when the inferior is restarted.
4021 The next update_global_location_list call will
4022 garbage collect them. */
4023 b->loc = NULL;
4024
4025 if (context == inf_starting)
4026 {
4027 /* Reset val field to force reread of starting value in
4028 insert_breakpoints. */
4029 if (w->val)
4030 value_free (w->val);
4031 w->val = NULL;
4032 w->val_valid = 0;
4033 }
4034 }
3a5c3e22 4035 }
c5aa993b
JM
4036 break;
4037 default:
c5aa993b
JM
4038 break;
4039 }
4040 }
1c5cfe86
PA
4041
4042 /* Get rid of the moribund locations. */
35df4500
TJB
4043 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4044 decref_bp_location (&bl);
1c5cfe86 4045 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4046}
4047
6c95b8df
PA
4048/* These functions concern about actual breakpoints inserted in the
4049 target --- to e.g. check if we need to do decr_pc adjustment or if
4050 we need to hop over the bkpt --- so we check for address space
4051 match, not program space. */
4052
c2c6d25f
JM
4053/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4054 exists at PC. It returns ordinary_breakpoint_here if it's an
4055 ordinary breakpoint, or permanent_breakpoint_here if it's a
4056 permanent breakpoint.
4057 - When continuing from a location with an ordinary breakpoint, we
4058 actually single step once before calling insert_breakpoints.
e5dd4106 4059 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4060 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4061 the target, to advance the PC past the breakpoint. */
c906108c 4062
c2c6d25f 4063enum breakpoint_here
6c95b8df 4064breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4065{
35df4500 4066 struct bp_location *bl, **blp_tmp;
c2c6d25f 4067 int any_breakpoint_here = 0;
c906108c 4068
35df4500 4069 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4070 {
35df4500
TJB
4071 if (bl->loc_type != bp_loc_software_breakpoint
4072 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4073 continue;
4074
f1310107 4075 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4076 if ((breakpoint_enabled (bl->owner)
1a853c52 4077 || bl->permanent)
f1310107 4078 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4079 {
4080 if (overlay_debugging
35df4500
TJB
4081 && section_is_overlay (bl->section)
4082 && !section_is_mapped (bl->section))
075f6582 4083 continue; /* unmapped overlay -- can't be a match */
1a853c52 4084 else if (bl->permanent)
075f6582
DJ
4085 return permanent_breakpoint_here;
4086 else
4087 any_breakpoint_here = 1;
4088 }
4089 }
c906108c 4090
f486487f 4091 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4092}
4093
d35ae833
PA
4094/* See breakpoint.h. */
4095
4096int
4097breakpoint_in_range_p (struct address_space *aspace,
4098 CORE_ADDR addr, ULONGEST len)
4099{
4100 struct bp_location *bl, **blp_tmp;
4101
4102 ALL_BP_LOCATIONS (bl, blp_tmp)
4103 {
4104 if (bl->loc_type != bp_loc_software_breakpoint
4105 && bl->loc_type != bp_loc_hardware_breakpoint)
4106 continue;
4107
4108 if ((breakpoint_enabled (bl->owner)
4109 || bl->permanent)
4110 && breakpoint_location_address_range_overlap (bl, aspace,
4111 addr, len))
4112 {
4113 if (overlay_debugging
4114 && section_is_overlay (bl->section)
4115 && !section_is_mapped (bl->section))
4116 {
4117 /* Unmapped overlay -- can't be a match. */
4118 continue;
4119 }
4120
4121 return 1;
4122 }
4123 }
4124
4125 return 0;
4126}
4127
1c5cfe86
PA
4128/* Return true if there's a moribund breakpoint at PC. */
4129
4130int
6c95b8df 4131moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4132{
4133 struct bp_location *loc;
4134 int ix;
4135
4136 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4137 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4138 return 1;
4139
4140 return 0;
4141}
c2c6d25f 4142
f7ce857f
PA
4143/* Returns non-zero iff BL is inserted at PC, in address space
4144 ASPACE. */
4145
4146static int
4147bp_location_inserted_here_p (struct bp_location *bl,
4148 struct address_space *aspace, CORE_ADDR pc)
4149{
4150 if (bl->inserted
4151 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4152 aspace, pc))
4153 {
4154 if (overlay_debugging
4155 && section_is_overlay (bl->section)
4156 && !section_is_mapped (bl->section))
4157 return 0; /* unmapped overlay -- can't be a match */
4158 else
4159 return 1;
4160 }
4161 return 0;
4162}
4163
a1fd2fa5 4164/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4165
4166int
a1fd2fa5 4167breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4168{
f7ce857f 4169 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4170
f7ce857f 4171 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4172 {
f7ce857f
PA
4173 struct bp_location *bl = *blp;
4174
35df4500
TJB
4175 if (bl->loc_type != bp_loc_software_breakpoint
4176 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4177 continue;
4178
f7ce857f
PA
4179 if (bp_location_inserted_here_p (bl, aspace, pc))
4180 return 1;
c5aa993b 4181 }
c36b740a
VP
4182 return 0;
4183}
4184
a1fd2fa5
PA
4185/* This function returns non-zero iff there is a software breakpoint
4186 inserted at PC. */
c36b740a
VP
4187
4188int
a1fd2fa5
PA
4189software_breakpoint_inserted_here_p (struct address_space *aspace,
4190 CORE_ADDR pc)
4fa8626c 4191{
f7ce857f 4192 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4193
f7ce857f 4194 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4195 {
f7ce857f
PA
4196 struct bp_location *bl = *blp;
4197
35df4500 4198 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4199 continue;
4200
f7ce857f
PA
4201 if (bp_location_inserted_here_p (bl, aspace, pc))
4202 return 1;
4fa8626c
DJ
4203 }
4204
4205 return 0;
9c02b525
PA
4206}
4207
4208/* See breakpoint.h. */
4209
4210int
4211hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4212 CORE_ADDR pc)
4213{
4214 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4215
4216 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4217 {
4218 struct bp_location *bl = *blp;
4219
4220 if (bl->loc_type != bp_loc_hardware_breakpoint)
4221 continue;
4222
4223 if (bp_location_inserted_here_p (bl, aspace, pc))
4224 return 1;
4225 }
4226
4227 return 0;
4fa8626c
DJ
4228}
4229
9093389c
PA
4230int
4231hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4232 CORE_ADDR addr, ULONGEST len)
4233{
4234 struct breakpoint *bpt;
4235
4236 ALL_BREAKPOINTS (bpt)
4237 {
4238 struct bp_location *loc;
4239
4240 if (bpt->type != bp_hardware_watchpoint
4241 && bpt->type != bp_access_watchpoint)
4242 continue;
4243
4244 if (!breakpoint_enabled (bpt))
4245 continue;
4246
4247 for (loc = bpt->loc; loc; loc = loc->next)
4248 if (loc->pspace->aspace == aspace && loc->inserted)
4249 {
4250 CORE_ADDR l, h;
4251
4252 /* Check for intersection. */
768adc05
PA
4253 l = std::max<CORE_ADDR> (loc->address, addr);
4254 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4255 if (l < h)
4256 return 1;
4257 }
4258 }
4259 return 0;
4260}
c906108c 4261\f
c5aa993b 4262
c906108c
SS
4263/* bpstat stuff. External routines' interfaces are documented
4264 in breakpoint.h. */
4265
4266int
c326b90e 4267is_catchpoint (struct breakpoint *ep)
c906108c 4268{
533be4dd 4269 return (ep->type == bp_catchpoint);
c906108c
SS
4270}
4271
f431efe5
PA
4272/* Frees any storage that is part of a bpstat. Does not walk the
4273 'next' chain. */
4274
04afa70c 4275bpstats::~bpstats ()
198757a8 4276{
04afa70c
TT
4277 if (old_val != NULL)
4278 value_free (old_val);
04afa70c
TT
4279 if (bp_location_at != NULL)
4280 decref_bp_location (&bp_location_at);
198757a8
VP
4281}
4282
c906108c
SS
4283/* Clear a bpstat so that it says we are not at any breakpoint.
4284 Also free any storage that is part of a bpstat. */
4285
4286void
fba45db2 4287bpstat_clear (bpstat *bsp)
c906108c
SS
4288{
4289 bpstat p;
4290 bpstat q;
4291
4292 if (bsp == 0)
4293 return;
4294 p = *bsp;
4295 while (p != NULL)
4296 {
4297 q = p->next;
04afa70c 4298 delete p;
c906108c
SS
4299 p = q;
4300 }
4301 *bsp = NULL;
4302}
4303
04afa70c
TT
4304bpstats::bpstats (const bpstats &other)
4305 : next (NULL),
4306 bp_location_at (other.bp_location_at),
4307 breakpoint_at (other.breakpoint_at),
4308 commands (other.commands),
4309 old_val (other.old_val),
4310 print (other.print),
4311 stop (other.stop),
4312 print_it (other.print_it)
4313{
4314 if (old_val != NULL)
4315 {
4316 old_val = value_copy (old_val);
4317 release_value (old_val);
4318 }
4319 incref_bp_location (bp_location_at);
04afa70c
TT
4320}
4321
c906108c
SS
4322/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4323 is part of the bpstat is copied as well. */
4324
4325bpstat
fba45db2 4326bpstat_copy (bpstat bs)
c906108c
SS
4327{
4328 bpstat p = NULL;
4329 bpstat tmp;
4330 bpstat retval = NULL;
4331
4332 if (bs == NULL)
4333 return bs;
4334
4335 for (; bs != NULL; bs = bs->next)
4336 {
04afa70c 4337 tmp = new bpstats (*bs);
31cc81e9 4338
c906108c
SS
4339 if (p == NULL)
4340 /* This is the first thing in the chain. */
4341 retval = tmp;
4342 else
4343 p->next = tmp;
4344 p = tmp;
4345 }
4346 p->next = NULL;
4347 return retval;
4348}
4349
4a64f543 4350/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4351
4352bpstat
fba45db2 4353bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4354{
c5aa993b
JM
4355 if (bsp == NULL)
4356 return NULL;
c906108c 4357
c5aa993b
JM
4358 for (; bsp != NULL; bsp = bsp->next)
4359 {
f431efe5 4360 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4361 return bsp;
4362 }
c906108c
SS
4363 return NULL;
4364}
4365
ab04a2af
TT
4366/* See breakpoint.h. */
4367
47591c29 4368int
427cd150 4369bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4370{
ab04a2af
TT
4371 for (; bsp != NULL; bsp = bsp->next)
4372 {
427cd150
TT
4373 if (bsp->breakpoint_at == NULL)
4374 {
4375 /* A moribund location can never explain a signal other than
4376 GDB_SIGNAL_TRAP. */
4377 if (sig == GDB_SIGNAL_TRAP)
47591c29 4378 return 1;
427cd150
TT
4379 }
4380 else
47591c29
PA
4381 {
4382 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4383 sig))
4384 return 1;
4385 }
ab04a2af
TT
4386 }
4387
47591c29 4388 return 0;
ab04a2af
TT
4389}
4390
4a64f543
MS
4391/* Put in *NUM the breakpoint number of the first breakpoint we are
4392 stopped at. *BSP upon return is a bpstat which points to the
4393 remaining breakpoints stopped at (but which is not guaranteed to be
4394 good for anything but further calls to bpstat_num).
4395
8671a17b
PA
4396 Return 0 if passed a bpstat which does not indicate any breakpoints.
4397 Return -1 if stopped at a breakpoint that has been deleted since
4398 we set it.
4399 Return 1 otherwise. */
c906108c
SS
4400
4401int
8671a17b 4402bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4403{
4404 struct breakpoint *b;
4405
4406 if ((*bsp) == NULL)
4407 return 0; /* No more breakpoint values */
8671a17b 4408
4a64f543
MS
4409 /* We assume we'll never have several bpstats that correspond to a
4410 single breakpoint -- otherwise, this function might return the
4411 same number more than once and this will look ugly. */
f431efe5 4412 b = (*bsp)->breakpoint_at;
8671a17b
PA
4413 *bsp = (*bsp)->next;
4414 if (b == NULL)
4415 return -1; /* breakpoint that's been deleted since */
4416
4417 *num = b->number; /* We have its number */
4418 return 1;
c906108c
SS
4419}
4420
e93ca019 4421/* See breakpoint.h. */
c906108c
SS
4422
4423void
e93ca019 4424bpstat_clear_actions (void)
c906108c 4425{
e93ca019
JK
4426 struct thread_info *tp;
4427 bpstat bs;
4428
4429 if (ptid_equal (inferior_ptid, null_ptid))
4430 return;
4431
4432 tp = find_thread_ptid (inferior_ptid);
4433 if (tp == NULL)
4434 return;
4435
4436 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4437 {
d1b0a7bf 4438 bs->commands = NULL;
abf85f46 4439
c906108c
SS
4440 if (bs->old_val != NULL)
4441 {
4442 value_free (bs->old_val);
4443 bs->old_val = NULL;
4444 }
4445 }
4446}
4447
f3b1572e
PA
4448/* Called when a command is about to proceed the inferior. */
4449
4450static void
4451breakpoint_about_to_proceed (void)
4452{
4453 if (!ptid_equal (inferior_ptid, null_ptid))
4454 {
4455 struct thread_info *tp = inferior_thread ();
4456
4457 /* Allow inferior function calls in breakpoint commands to not
4458 interrupt the command list. When the call finishes
4459 successfully, the inferior will be standing at the same
4460 breakpoint as if nothing happened. */
16c381f0 4461 if (tp->control.in_infcall)
f3b1572e
PA
4462 return;
4463 }
4464
4465 breakpoint_proceeded = 1;
4466}
4467
4a64f543
MS
4468/* Stub for cleaning up our state if we error-out of a breakpoint
4469 command. */
c906108c 4470static void
4efb68b1 4471cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4472{
4473 executing_breakpoint_commands = 0;
4474}
4475
abf85f46
JK
4476/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4477 or its equivalent. */
4478
4479static int
4480command_line_is_silent (struct command_line *cmd)
4481{
4f45d445 4482 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4483}
4484
4a64f543
MS
4485/* Execute all the commands associated with all the breakpoints at
4486 this location. Any of these commands could cause the process to
4487 proceed beyond this point, etc. We look out for such changes by
4488 checking the global "breakpoint_proceeded" after each command.
c906108c 4489
347bddb7
PA
4490 Returns true if a breakpoint command resumed the inferior. In that
4491 case, it is the caller's responsibility to recall it again with the
4492 bpstat of the current thread. */
4493
4494static int
4495bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4496{
4497 bpstat bs;
4498 struct cleanup *old_chain;
347bddb7 4499 int again = 0;
c906108c
SS
4500
4501 /* Avoid endless recursion if a `source' command is contained
4502 in bs->commands. */
4503 if (executing_breakpoint_commands)
347bddb7 4504 return 0;
c906108c
SS
4505
4506 executing_breakpoint_commands = 1;
4507 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4508
1ac32117 4509 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4510
4a64f543 4511 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4512 bs = *bsp;
4513
4514 breakpoint_proceeded = 0;
4515 for (; bs != NULL; bs = bs->next)
4516 {
d1b0a7bf 4517 struct command_line *cmd = NULL;
6c50ab1c
JB
4518
4519 /* Take ownership of the BSP's command tree, if it has one.
4520
4521 The command tree could legitimately contain commands like
4522 'step' and 'next', which call clear_proceed_status, which
4523 frees stop_bpstat's command tree. To make sure this doesn't
4524 free the tree we're executing out from under us, we need to
4525 take ownership of the tree ourselves. Since a given bpstat's
4526 commands are only executed once, we don't need to copy it; we
4527 can clear the pointer in the bpstat, and make sure we free
4528 the tree when we're done. */
d1b0a7bf 4529 counted_command_line ccmd = bs->commands;
9add0f1b 4530 bs->commands = NULL;
d1b0a7bf
TT
4531 if (ccmd != NULL)
4532 cmd = ccmd.get ();
abf85f46
JK
4533 if (command_line_is_silent (cmd))
4534 {
4535 /* The action has been already done by bpstat_stop_status. */
4536 cmd = cmd->next;
4537 }
6c50ab1c 4538
c906108c
SS
4539 while (cmd != NULL)
4540 {
4541 execute_control_command (cmd);
4542
4543 if (breakpoint_proceeded)
4544 break;
4545 else
4546 cmd = cmd->next;
4547 }
6c50ab1c 4548
c906108c 4549 if (breakpoint_proceeded)
32c1e744 4550 {
cb814510 4551 if (current_ui->async)
347bddb7
PA
4552 /* If we are in async mode, then the target might be still
4553 running, not stopped at any breakpoint, so nothing for
4554 us to do here -- just return to the event loop. */
4555 ;
32c1e744
VP
4556 else
4557 /* In sync mode, when execute_control_command returns
4558 we're already standing on the next breakpoint.
347bddb7
PA
4559 Breakpoint commands for that stop were not run, since
4560 execute_command does not run breakpoint commands --
4561 only command_line_handler does, but that one is not
4562 involved in execution of breakpoint commands. So, we
4563 can now execute breakpoint commands. It should be
4564 noted that making execute_command do bpstat actions is
4565 not an option -- in this case we'll have recursive
4566 invocation of bpstat for each breakpoint with a
4567 command, and can easily blow up GDB stack. Instead, we
4568 return true, which will trigger the caller to recall us
4569 with the new stop_bpstat. */
4570 again = 1;
4571 break;
32c1e744 4572 }
c906108c 4573 }
c2b8ed2c 4574 do_cleanups (old_chain);
347bddb7
PA
4575 return again;
4576}
4577
4578void
4579bpstat_do_actions (void)
4580{
353d1d73
JK
4581 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4582
347bddb7
PA
4583 /* Do any commands attached to breakpoint we are stopped at. */
4584 while (!ptid_equal (inferior_ptid, null_ptid)
4585 && target_has_execution
4586 && !is_exited (inferior_ptid)
4587 && !is_executing (inferior_ptid))
4588 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4589 and only return when it is stopped at the next breakpoint, we
4590 keep doing breakpoint actions until it returns false to
4591 indicate the inferior was not resumed. */
16c381f0 4592 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4593 break;
353d1d73
JK
4594
4595 discard_cleanups (cleanup_if_error);
c906108c
SS
4596}
4597
fa4727a6
DJ
4598/* Print out the (old or new) value associated with a watchpoint. */
4599
4600static void
4601watchpoint_value_print (struct value *val, struct ui_file *stream)
4602{
4603 if (val == NULL)
4604 fprintf_unfiltered (stream, _("<unreadable>"));
4605 else
79a45b7d
TT
4606 {
4607 struct value_print_options opts;
4608 get_user_print_options (&opts);
4609 value_print (val, stream, &opts);
4610 }
fa4727a6
DJ
4611}
4612
f303dbd6
PA
4613/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4614 debugging multiple threads. */
4615
4616void
4617maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4618{
112e8700 4619 if (uiout->is_mi_like_p ())
f303dbd6
PA
4620 return;
4621
112e8700 4622 uiout->text ("\n");
f303dbd6
PA
4623
4624 if (show_thread_that_caused_stop ())
4625 {
4626 const char *name;
4627 struct thread_info *thr = inferior_thread ();
4628
112e8700
SM
4629 uiout->text ("Thread ");
4630 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4631
4632 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4633 if (name != NULL)
4634 {
112e8700
SM
4635 uiout->text (" \"");
4636 uiout->field_fmt ("name", "%s", name);
4637 uiout->text ("\"");
f303dbd6
PA
4638 }
4639
112e8700 4640 uiout->text (" hit ");
f303dbd6
PA
4641 }
4642}
4643
e514a9d6 4644/* Generic routine for printing messages indicating why we
4a64f543 4645 stopped. The behavior of this function depends on the value
e514a9d6
JM
4646 'print_it' in the bpstat structure. Under some circumstances we
4647 may decide not to print anything here and delegate the task to
4a64f543 4648 normal_stop(). */
e514a9d6
JM
4649
4650static enum print_stop_action
4651print_bp_stop_message (bpstat bs)
4652{
4653 switch (bs->print_it)
4654 {
4655 case print_it_noop:
4a64f543 4656 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4657 return PRINT_UNKNOWN;
4658 break;
4659
4660 case print_it_done:
4661 /* We still want to print the frame, but we already printed the
4a64f543 4662 relevant messages. */
e514a9d6
JM
4663 return PRINT_SRC_AND_LOC;
4664 break;
4665
4666 case print_it_normal:
4f8d1dc6 4667 {
f431efe5
PA
4668 struct breakpoint *b = bs->breakpoint_at;
4669
1a6a67de
TJB
4670 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4671 which has since been deleted. */
4672 if (b == NULL)
4673 return PRINT_UNKNOWN;
4674
348d480f
PA
4675 /* Normal case. Call the breakpoint's print_it method. */
4676 return b->ops->print_it (bs);
4f8d1dc6 4677 }
348d480f 4678 break;
3086aeae 4679
e514a9d6 4680 default:
8e65ff28 4681 internal_error (__FILE__, __LINE__,
e2e0b3e5 4682 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4683 break;
c906108c 4684 }
c906108c
SS
4685}
4686
edcc5120
TT
4687/* A helper function that prints a shared library stopped event. */
4688
4689static void
4690print_solib_event (int is_catchpoint)
4691{
4692 int any_deleted
4693 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4694 int any_added
4695 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4696
4697 if (!is_catchpoint)
4698 {
4699 if (any_added || any_deleted)
112e8700 4700 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4701 else
112e8700
SM
4702 current_uiout->text (_("Stopped due to shared library event (no "
4703 "libraries added or removed)\n"));
edcc5120
TT
4704 }
4705
112e8700
SM
4706 if (current_uiout->is_mi_like_p ())
4707 current_uiout->field_string ("reason",
4708 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4709
4710 if (any_deleted)
4711 {
edcc5120
TT
4712 char *name;
4713 int ix;
4714
112e8700 4715 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4716 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4717 for (ix = 0;
4718 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4719 ix, name);
4720 ++ix)
4721 {
4722 if (ix > 0)
112e8700
SM
4723 current_uiout->text (" ");
4724 current_uiout->field_string ("library", name);
4725 current_uiout->text ("\n");
edcc5120 4726 }
edcc5120
TT
4727 }
4728
4729 if (any_added)
4730 {
4731 struct so_list *iter;
4732 int ix;
edcc5120 4733
112e8700 4734 current_uiout->text (_(" Inferior loaded "));
10f489e5 4735 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4736 for (ix = 0;
4737 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4738 ix, iter);
4739 ++ix)
4740 {
4741 if (ix > 0)
112e8700
SM
4742 current_uiout->text (" ");
4743 current_uiout->field_string ("library", iter->so_name);
4744 current_uiout->text ("\n");
edcc5120 4745 }
edcc5120
TT
4746 }
4747}
4748
e514a9d6
JM
4749/* Print a message indicating what happened. This is called from
4750 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4751 list - a list of the eventpoints that caused this stop. KIND is
4752 the target_waitkind for the stopping event. This
e514a9d6
JM
4753 routine calls the generic print routine for printing a message
4754 about reasons for stopping. This will print (for example) the
4755 "Breakpoint n," part of the output. The return value of this
4756 routine is one of:
c906108c 4757
4a64f543 4758 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4759 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4760 code to print the location. An example is
c5aa993b
JM
4761 "Breakpoint 1, " which should be followed by
4762 the location.
917317f4 4763 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4764 to also print the location part of the message.
4765 An example is the catch/throw messages, which
4a64f543 4766 don't require a location appended to the end.
917317f4 4767 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4768 further info to be printed. */
c906108c 4769
917317f4 4770enum print_stop_action
36dfb11c 4771bpstat_print (bpstat bs, int kind)
c906108c 4772{
f486487f 4773 enum print_stop_action val;
c5aa993b 4774
c906108c 4775 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4776 (Currently all watchpoints go on the bpstat whether hit or not.
4777 That probably could (should) be changed, provided care is taken
c906108c 4778 with respect to bpstat_explains_signal). */
e514a9d6
JM
4779 for (; bs; bs = bs->next)
4780 {
4781 val = print_bp_stop_message (bs);
4782 if (val == PRINT_SRC_ONLY
4783 || val == PRINT_SRC_AND_LOC
4784 || val == PRINT_NOTHING)
4785 return val;
4786 }
c906108c 4787
36dfb11c
TT
4788 /* If we had hit a shared library event breakpoint,
4789 print_bp_stop_message would print out this message. If we hit an
4790 OS-level shared library event, do the same thing. */
4791 if (kind == TARGET_WAITKIND_LOADED)
4792 {
edcc5120 4793 print_solib_event (0);
36dfb11c
TT
4794 return PRINT_NOTHING;
4795 }
4796
e514a9d6 4797 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4798 with and nothing was printed. */
917317f4 4799 return PRINT_UNKNOWN;
c906108c
SS
4800}
4801
bf469271 4802/* Evaluate the boolean expression EXP and return the result. */
c906108c 4803
bf469271
PA
4804static bool
4805breakpoint_cond_eval (expression *exp)
c906108c 4806{
278cd55f 4807 struct value *mark = value_mark ();
bf469271 4808 bool res = value_true (evaluate_expression (exp));
cc59ec59 4809
c906108c 4810 value_free_to_mark (mark);
bf469271 4811 return res;
c906108c
SS
4812}
4813
5760d0ab 4814/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4815
04afa70c
TT
4816bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4817 : next (NULL),
4818 bp_location_at (bl),
4819 breakpoint_at (bl->owner),
4820 commands (NULL),
4821 old_val (NULL),
4822 print (0),
4823 stop (0),
4824 print_it (print_it_normal)
c906108c 4825{
f431efe5 4826 incref_bp_location (bl);
04afa70c
TT
4827 **bs_link_pointer = this;
4828 *bs_link_pointer = &next;
4829}
4830
4831bpstats::bpstats ()
4832 : next (NULL),
4833 bp_location_at (NULL),
4834 breakpoint_at (NULL),
4835 commands (NULL),
4836 old_val (NULL),
4837 print (0),
4838 stop (0),
4839 print_it (print_it_normal)
4840{
c906108c
SS
4841}
4842\f
d983da9c
DJ
4843/* The target has stopped with waitstatus WS. Check if any hardware
4844 watchpoints have triggered, according to the target. */
4845
4846int
4847watchpoints_triggered (struct target_waitstatus *ws)
4848{
d92524f1 4849 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4850 CORE_ADDR addr;
4851 struct breakpoint *b;
4852
4853 if (!stopped_by_watchpoint)
4854 {
4855 /* We were not stopped by a watchpoint. Mark all watchpoints
4856 as not triggered. */
4857 ALL_BREAKPOINTS (b)
cc60f2e3 4858 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4859 {
4860 struct watchpoint *w = (struct watchpoint *) b;
4861
4862 w->watchpoint_triggered = watch_triggered_no;
4863 }
d983da9c
DJ
4864
4865 return 0;
4866 }
4867
4868 if (!target_stopped_data_address (&current_target, &addr))
4869 {
4870 /* We were stopped by a watchpoint, but we don't know where.
4871 Mark all watchpoints as unknown. */
4872 ALL_BREAKPOINTS (b)
cc60f2e3 4873 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4874 {
4875 struct watchpoint *w = (struct watchpoint *) b;
4876
4877 w->watchpoint_triggered = watch_triggered_unknown;
4878 }
d983da9c 4879
3c4797ba 4880 return 1;
d983da9c
DJ
4881 }
4882
4883 /* The target could report the data address. Mark watchpoints
4884 affected by this data address as triggered, and all others as not
4885 triggered. */
4886
4887 ALL_BREAKPOINTS (b)
cc60f2e3 4888 if (is_hardware_watchpoint (b))
d983da9c 4889 {
3a5c3e22 4890 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4891 struct bp_location *loc;
d983da9c 4892
3a5c3e22 4893 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4894 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4895 {
3a5c3e22 4896 if (is_masked_watchpoint (b))
9c06b0b4 4897 {
3a5c3e22
PA
4898 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4899 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4900
4901 if (newaddr == start)
4902 {
3a5c3e22 4903 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4904 break;
4905 }
4906 }
4907 /* Exact match not required. Within range is sufficient. */
4908 else if (target_watchpoint_addr_within_range (&current_target,
4909 addr, loc->address,
4910 loc->length))
4911 {
3a5c3e22 4912 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4913 break;
4914 }
4915 }
d983da9c
DJ
4916 }
4917
4918 return 1;
4919}
4920
bf469271
PA
4921/* Possible return values for watchpoint_check. */
4922enum wp_check_result
4923 {
4924 /* The watchpoint has been deleted. */
4925 WP_DELETED = 1,
4926
4927 /* The value has changed. */
4928 WP_VALUE_CHANGED = 2,
4929
4930 /* The value has not changed. */
4931 WP_VALUE_NOT_CHANGED = 3,
4932
4933 /* Ignore this watchpoint, no matter if the value changed or not. */
4934 WP_IGNORE = 4,
4935 };
c906108c
SS
4936
4937#define BP_TEMPFLAG 1
4938#define BP_HARDWAREFLAG 2
4939
4a64f543 4940/* Evaluate watchpoint condition expression and check if its value
bf469271 4941 changed. */
553e4c11 4942
bf469271
PA
4943static wp_check_result
4944watchpoint_check (bpstat bs)
c906108c 4945{
3a5c3e22 4946 struct watchpoint *b;
c906108c
SS
4947 struct frame_info *fr;
4948 int within_current_scope;
4949
f431efe5 4950 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4951 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4952 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4953
f6bc2008
PA
4954 /* If this is a local watchpoint, we only want to check if the
4955 watchpoint frame is in scope if the current thread is the thread
4956 that was used to create the watchpoint. */
4957 if (!watchpoint_in_thread_scope (b))
60e1c644 4958 return WP_IGNORE;
f6bc2008 4959
c906108c
SS
4960 if (b->exp_valid_block == NULL)
4961 within_current_scope = 1;
4962 else
4963 {
edb3359d
DJ
4964 struct frame_info *frame = get_current_frame ();
4965 struct gdbarch *frame_arch = get_frame_arch (frame);
4966 CORE_ADDR frame_pc = get_frame_pc (frame);
4967
c9cf6e20 4968 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4969 still in the function but the stack frame has already been
4970 invalidated. Since we can't rely on the values of local
4971 variables after the stack has been destroyed, we are treating
4972 the watchpoint in that state as `not changed' without further
4973 checking. Don't mark watchpoints as changed if the current
4974 frame is in an epilogue - even if they are in some other
4975 frame, our view of the stack is likely to be wrong and
4976 frame_find_by_id could error out. */
c9cf6e20 4977 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4978 return WP_IGNORE;
a0f49112 4979
101dcfbe 4980 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4981 within_current_scope = (fr != NULL);
69fbadd5
DJ
4982
4983 /* If we've gotten confused in the unwinder, we might have
4984 returned a frame that can't describe this variable. */
edb3359d
DJ
4985 if (within_current_scope)
4986 {
4987 struct symbol *function;
4988
4989 function = get_frame_function (fr);
4990 if (function == NULL
4991 || !contained_in (b->exp_valid_block,
4992 SYMBOL_BLOCK_VALUE (function)))
4993 within_current_scope = 0;
4994 }
69fbadd5 4995
edb3359d 4996 if (within_current_scope)
c906108c
SS
4997 /* If we end up stopping, the current frame will get selected
4998 in normal_stop. So this call to select_frame won't affect
4999 the user. */
0f7d239c 5000 select_frame (fr);
c906108c 5001 }
c5aa993b 5002
c906108c
SS
5003 if (within_current_scope)
5004 {
4a64f543
MS
5005 /* We use value_{,free_to_}mark because it could be a *long*
5006 time before we return to the command level and call
5007 free_all_values. We can't call free_all_values because we
5008 might be in the middle of evaluating a function call. */
c906108c 5009
0cf6dd15 5010 int pc = 0;
9c06b0b4 5011 struct value *mark;
fa4727a6
DJ
5012 struct value *new_val;
5013
c1fc2657 5014 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5015 /* Since we don't know the exact trigger address (from
5016 stopped_data_address), just tell the user we've triggered
5017 a mask watchpoint. */
5018 return WP_VALUE_CHANGED;
5019
5020 mark = value_mark ();
4d01a485 5021 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5022
bb9d5f81
PP
5023 if (b->val_bitsize != 0)
5024 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5025
4a64f543
MS
5026 /* We use value_equal_contents instead of value_equal because
5027 the latter coerces an array to a pointer, thus comparing just
5028 the address of the array instead of its contents. This is
5029 not what we want. */
fa4727a6 5030 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5031 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5032 {
fa4727a6
DJ
5033 if (new_val != NULL)
5034 {
5035 release_value (new_val);
5036 value_free_to_mark (mark);
5037 }
c906108c
SS
5038 bs->old_val = b->val;
5039 b->val = new_val;
fa4727a6 5040 b->val_valid = 1;
c906108c
SS
5041 return WP_VALUE_CHANGED;
5042 }
5043 else
5044 {
60e1c644 5045 /* Nothing changed. */
c906108c 5046 value_free_to_mark (mark);
c906108c
SS
5047 return WP_VALUE_NOT_CHANGED;
5048 }
5049 }
5050 else
5051 {
5052 /* This seems like the only logical thing to do because
c5aa993b
JM
5053 if we temporarily ignored the watchpoint, then when
5054 we reenter the block in which it is valid it contains
5055 garbage (in the case of a function, it may have two
5056 garbage values, one before and one after the prologue).
5057 So we can't even detect the first assignment to it and
5058 watch after that (since the garbage may or may not equal
5059 the first value assigned). */
348d480f
PA
5060 /* We print all the stop information in
5061 breakpoint_ops->print_it, but in this case, by the time we
5062 call breakpoint_ops->print_it this bp will be deleted
5063 already. So we have no choice but print the information
5064 here. */
468afe6c 5065
0e454242 5066 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5067 {
5068 struct ui_out *uiout = current_uiout;
5069
112e8700
SM
5070 if (uiout->is_mi_like_p ())
5071 uiout->field_string
5072 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5073 uiout->text ("\nWatchpoint ");
c1fc2657 5074 uiout->field_int ("wpnum", b->number);
112e8700 5075 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5076 "which its expression is valid.\n");
5077 }
4ce44c66 5078
cdac0397 5079 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5080 b->commands = NULL;
d0fb5eae 5081 watchpoint_del_at_next_stop (b);
c906108c
SS
5082
5083 return WP_DELETED;
5084 }
5085}
5086
18a18393 5087/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5088 breakpoint location BL. This function does not check if we should
5089 stop, only if BL explains the stop. */
5090
18a18393 5091static int
6c95b8df 5092bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5093 struct address_space *aspace, CORE_ADDR bp_addr,
5094 const struct target_waitstatus *ws)
18a18393
VP
5095{
5096 struct breakpoint *b = bl->owner;
5097
348d480f 5098 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5099 gdb_assert (b != NULL);
5100
09ac7c10 5101 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5102}
5103
3a5c3e22
PA
5104/* Determine if the watched values have actually changed, and we
5105 should stop. If not, set BS->stop to 0. */
5106
18a18393
VP
5107static void
5108bpstat_check_watchpoint (bpstat bs)
5109{
2bdf28a0 5110 const struct bp_location *bl;
3a5c3e22 5111 struct watchpoint *b;
2bdf28a0
JK
5112
5113 /* BS is built for existing struct breakpoint. */
f431efe5 5114 bl = bs->bp_location_at;
2bdf28a0 5115 gdb_assert (bl != NULL);
3a5c3e22 5116 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5117 gdb_assert (b != NULL);
18a18393 5118
18a18393 5119 {
18a18393
VP
5120 int must_check_value = 0;
5121
c1fc2657 5122 if (b->type == bp_watchpoint)
18a18393
VP
5123 /* For a software watchpoint, we must always check the
5124 watched value. */
5125 must_check_value = 1;
5126 else if (b->watchpoint_triggered == watch_triggered_yes)
5127 /* We have a hardware watchpoint (read, write, or access)
5128 and the target earlier reported an address watched by
5129 this watchpoint. */
5130 must_check_value = 1;
5131 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5132 && b->type == bp_hardware_watchpoint)
18a18393
VP
5133 /* We were stopped by a hardware watchpoint, but the target could
5134 not report the data address. We must check the watchpoint's
5135 value. Access and read watchpoints are out of luck; without
5136 a data address, we can't figure it out. */
5137 must_check_value = 1;
3a5c3e22 5138
18a18393
VP
5139 if (must_check_value)
5140 {
bf469271
PA
5141 wp_check_result e;
5142
5143 TRY
5144 {
5145 e = watchpoint_check (bs);
5146 }
5147 CATCH (ex, RETURN_MASK_ALL)
5148 {
5149 exception_fprintf (gdb_stderr, ex,
5150 "Error evaluating expression "
5151 "for watchpoint %d\n",
5152 b->number);
5153
5154 SWITCH_THRU_ALL_UIS ()
5155 {
5156 printf_filtered (_("Watchpoint %d deleted.\n"),
5157 b->number);
5158 }
5159 watchpoint_del_at_next_stop (b);
5160 e = WP_DELETED;
5161 }
5162 END_CATCH
5163
18a18393
VP
5164 switch (e)
5165 {
5166 case WP_DELETED:
5167 /* We've already printed what needs to be printed. */
5168 bs->print_it = print_it_done;
5169 /* Stop. */
5170 break;
60e1c644
PA
5171 case WP_IGNORE:
5172 bs->print_it = print_it_noop;
5173 bs->stop = 0;
5174 break;
18a18393 5175 case WP_VALUE_CHANGED:
c1fc2657 5176 if (b->type == bp_read_watchpoint)
18a18393 5177 {
85d721b8
PA
5178 /* There are two cases to consider here:
5179
4a64f543 5180 1. We're watching the triggered memory for reads.
85d721b8
PA
5181 In that case, trust the target, and always report
5182 the watchpoint hit to the user. Even though
5183 reads don't cause value changes, the value may
5184 have changed since the last time it was read, and
5185 since we're not trapping writes, we will not see
5186 those, and as such we should ignore our notion of
5187 old value.
5188
4a64f543 5189 2. We're watching the triggered memory for both
85d721b8
PA
5190 reads and writes. There are two ways this may
5191 happen:
5192
4a64f543 5193 2.1. This is a target that can't break on data
85d721b8
PA
5194 reads only, but can break on accesses (reads or
5195 writes), such as e.g., x86. We detect this case
5196 at the time we try to insert read watchpoints.
5197
4a64f543 5198 2.2. Otherwise, the target supports read
85d721b8
PA
5199 watchpoints, but, the user set an access or write
5200 watchpoint watching the same memory as this read
5201 watchpoint.
5202
5203 If we're watching memory writes as well as reads,
5204 ignore watchpoint hits when we find that the
5205 value hasn't changed, as reads don't cause
5206 changes. This still gives false positives when
5207 the program writes the same value to memory as
5208 what there was already in memory (we will confuse
5209 it for a read), but it's much better than
5210 nothing. */
5211
5212 int other_write_watchpoint = 0;
5213
5214 if (bl->watchpoint_type == hw_read)
5215 {
5216 struct breakpoint *other_b;
5217
5218 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5219 if (other_b->type == bp_hardware_watchpoint
5220 || other_b->type == bp_access_watchpoint)
85d721b8 5221 {
3a5c3e22
PA
5222 struct watchpoint *other_w =
5223 (struct watchpoint *) other_b;
5224
5225 if (other_w->watchpoint_triggered
5226 == watch_triggered_yes)
5227 {
5228 other_write_watchpoint = 1;
5229 break;
5230 }
85d721b8
PA
5231 }
5232 }
5233
5234 if (other_write_watchpoint
5235 || bl->watchpoint_type == hw_access)
5236 {
5237 /* We're watching the same memory for writes,
5238 and the value changed since the last time we
5239 updated it, so this trap must be for a write.
5240 Ignore it. */
5241 bs->print_it = print_it_noop;
5242 bs->stop = 0;
5243 }
18a18393
VP
5244 }
5245 break;
5246 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5247 if (b->type == bp_hardware_watchpoint
5248 || b->type == bp_watchpoint)
18a18393
VP
5249 {
5250 /* Don't stop: write watchpoints shouldn't fire if
5251 the value hasn't changed. */
5252 bs->print_it = print_it_noop;
5253 bs->stop = 0;
5254 }
5255 /* Stop. */
5256 break;
5257 default:
5258 /* Can't happen. */
18a18393
VP
5259 break;
5260 }
5261 }
5262 else /* must_check_value == 0 */
5263 {
5264 /* This is a case where some watchpoint(s) triggered, but
5265 not at the address of this watchpoint, or else no
5266 watchpoint triggered after all. So don't print
5267 anything for this watchpoint. */
5268 bs->print_it = print_it_noop;
5269 bs->stop = 0;
5270 }
5271 }
5272}
5273
7d4df6a4
DE
5274/* For breakpoints that are currently marked as telling gdb to stop,
5275 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5276 of breakpoint referred to by BS. If we should not stop for this
5277 breakpoint, set BS->stop to 0. */
f431efe5 5278
18a18393
VP
5279static void
5280bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5281{
2bdf28a0
JK
5282 const struct bp_location *bl;
5283 struct breakpoint *b;
bf469271
PA
5284 /* Assume stop. */
5285 bool condition_result = true;
7d4df6a4
DE
5286 struct expression *cond;
5287
5288 gdb_assert (bs->stop);
2bdf28a0
JK
5289
5290 /* BS is built for existing struct breakpoint. */
f431efe5 5291 bl = bs->bp_location_at;
2bdf28a0 5292 gdb_assert (bl != NULL);
f431efe5 5293 b = bs->breakpoint_at;
2bdf28a0 5294 gdb_assert (b != NULL);
18a18393 5295
b775012e
LM
5296 /* Even if the target evaluated the condition on its end and notified GDB, we
5297 need to do so again since GDB does not know if we stopped due to a
5298 breakpoint or a single step breakpoint. */
5299
18a18393 5300 if (frame_id_p (b->frame_id)
edb3359d 5301 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5302 {
7d4df6a4
DE
5303 bs->stop = 0;
5304 return;
5305 }
60e1c644 5306
12ab52e9
PA
5307 /* If this is a thread/task-specific breakpoint, don't waste cpu
5308 evaluating the condition if this isn't the specified
5309 thread/task. */
5d5658a1 5310 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5311 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5312
6c1b0f7b
DE
5313 {
5314 bs->stop = 0;
5315 return;
5316 }
5317
6dddc817
DE
5318 /* Evaluate extension language breakpoints that have a "stop" method
5319 implemented. */
5320 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5321
7d4df6a4
DE
5322 if (is_watchpoint (b))
5323 {
5324 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5325
4d01a485 5326 cond = w->cond_exp.get ();
7d4df6a4
DE
5327 }
5328 else
4d01a485 5329 cond = bl->cond.get ();
60e1c644 5330
7d4df6a4
DE
5331 if (cond && b->disposition != disp_del_at_next_stop)
5332 {
5333 int within_current_scope = 1;
5334 struct watchpoint * w;
60e1c644 5335
7d4df6a4
DE
5336 /* We use value_mark and value_free_to_mark because it could
5337 be a long time before we return to the command level and
5338 call free_all_values. We can't call free_all_values
5339 because we might be in the middle of evaluating a
5340 function call. */
5341 struct value *mark = value_mark ();
5342
5343 if (is_watchpoint (b))
5344 w = (struct watchpoint *) b;
5345 else
5346 w = NULL;
5347
5348 /* Need to select the frame, with all that implies so that
5349 the conditions will have the right context. Because we
5350 use the frame, we will not see an inlined function's
5351 variables when we arrive at a breakpoint at the start
5352 of the inlined function; the current frame will be the
5353 call site. */
5354 if (w == NULL || w->cond_exp_valid_block == NULL)
5355 select_frame (get_current_frame ());
5356 else
18a18393 5357 {
7d4df6a4
DE
5358 struct frame_info *frame;
5359
5360 /* For local watchpoint expressions, which particular
5361 instance of a local is being watched matters, so we
5362 keep track of the frame to evaluate the expression
5363 in. To evaluate the condition however, it doesn't
5364 really matter which instantiation of the function
5365 where the condition makes sense triggers the
5366 watchpoint. This allows an expression like "watch
5367 global if q > 10" set in `func', catch writes to
5368 global on all threads that call `func', or catch
5369 writes on all recursive calls of `func' by a single
5370 thread. We simply always evaluate the condition in
5371 the innermost frame that's executing where it makes
5372 sense to evaluate the condition. It seems
5373 intuitive. */
5374 frame = block_innermost_frame (w->cond_exp_valid_block);
5375 if (frame != NULL)
5376 select_frame (frame);
5377 else
5378 within_current_scope = 0;
18a18393 5379 }
7d4df6a4 5380 if (within_current_scope)
bf469271
PA
5381 {
5382 TRY
5383 {
5384 condition_result = breakpoint_cond_eval (cond);
5385 }
5386 CATCH (ex, RETURN_MASK_ALL)
5387 {
5388 exception_fprintf (gdb_stderr, ex,
5389 "Error in testing breakpoint condition:\n");
5390 }
5391 END_CATCH
5392 }
7d4df6a4 5393 else
18a18393 5394 {
7d4df6a4
DE
5395 warning (_("Watchpoint condition cannot be tested "
5396 "in the current scope"));
5397 /* If we failed to set the right context for this
5398 watchpoint, unconditionally report it. */
18a18393 5399 }
7d4df6a4
DE
5400 /* FIXME-someday, should give breakpoint #. */
5401 value_free_to_mark (mark);
18a18393 5402 }
7d4df6a4 5403
bf469271 5404 if (cond && !condition_result)
7d4df6a4
DE
5405 {
5406 bs->stop = 0;
5407 }
7d4df6a4
DE
5408 else if (b->ignore_count > 0)
5409 {
5410 b->ignore_count--;
5411 bs->stop = 0;
5412 /* Increase the hit count even though we don't stop. */
5413 ++(b->hit_count);
5414 observer_notify_breakpoint_modified (b);
5415 }
18a18393
VP
5416}
5417
1cf4d951
PA
5418/* Returns true if we need to track moribund locations of LOC's type
5419 on the current target. */
5420
5421static int
5422need_moribund_for_location_type (struct bp_location *loc)
5423{
5424 return ((loc->loc_type == bp_loc_software_breakpoint
5425 && !target_supports_stopped_by_sw_breakpoint ())
5426 || (loc->loc_type == bp_loc_hardware_breakpoint
5427 && !target_supports_stopped_by_hw_breakpoint ()));
5428}
5429
18a18393 5430
9709f61c 5431/* Get a bpstat associated with having just stopped at address
d983da9c 5432 BP_ADDR in thread PTID.
c906108c 5433
d983da9c 5434 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5435 don't understand this stop. Result is a chain of bpstat's such
5436 that:
c906108c 5437
c5aa993b 5438 if we don't understand the stop, the result is a null pointer.
c906108c 5439
c5aa993b 5440 if we understand why we stopped, the result is not null.
c906108c 5441
c5aa993b
JM
5442 Each element of the chain refers to a particular breakpoint or
5443 watchpoint at which we have stopped. (We may have stopped for
5444 several reasons concurrently.)
c906108c 5445
c5aa993b
JM
5446 Each element of the chain has valid next, breakpoint_at,
5447 commands, FIXME??? fields. */
c906108c
SS
5448
5449bpstat
6c95b8df 5450bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5451 CORE_ADDR bp_addr, ptid_t ptid,
5452 const struct target_waitstatus *ws)
c906108c 5453{
0d381245 5454 struct breakpoint *b = NULL;
afe38095 5455 struct bp_location *bl;
20874c92 5456 struct bp_location *loc;
5760d0ab
JK
5457 /* First item of allocated bpstat's. */
5458 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5459 /* Pointer to the last thing in the chain currently. */
5760d0ab 5460 bpstat bs;
20874c92 5461 int ix;
429374b8 5462 int need_remove_insert;
f431efe5 5463 int removed_any;
c906108c 5464
f431efe5
PA
5465 /* First, build the bpstat chain with locations that explain a
5466 target stop, while being careful to not set the target running,
5467 as that may invalidate locations (in particular watchpoint
5468 locations are recreated). Resuming will happen here with
5469 breakpoint conditions or watchpoint expressions that include
5470 inferior function calls. */
c5aa993b 5471
429374b8
JK
5472 ALL_BREAKPOINTS (b)
5473 {
1a853c52 5474 if (!breakpoint_enabled (b))
429374b8 5475 continue;
a5606eee 5476
429374b8
JK
5477 for (bl = b->loc; bl != NULL; bl = bl->next)
5478 {
4a64f543
MS
5479 /* For hardware watchpoints, we look only at the first
5480 location. The watchpoint_check function will work on the
5481 entire expression, not the individual locations. For
5482 read watchpoints, the watchpoints_triggered function has
5483 checked all locations already. */
429374b8
JK
5484 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5485 break;
18a18393 5486
f6592439 5487 if (!bl->enabled || bl->shlib_disabled)
429374b8 5488 continue;
c5aa993b 5489
09ac7c10 5490 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5491 continue;
c5aa993b 5492
4a64f543
MS
5493 /* Come here if it's a watchpoint, or if the break address
5494 matches. */
c5aa993b 5495
04afa70c 5496 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5497 explain stop. */
c5aa993b 5498
f431efe5
PA
5499 /* Assume we stop. Should we find a watchpoint that is not
5500 actually triggered, or if the condition of the breakpoint
5501 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5502 bs->stop = 1;
5503 bs->print = 1;
d983da9c 5504
f431efe5
PA
5505 /* If this is a scope breakpoint, mark the associated
5506 watchpoint as triggered so that we will handle the
5507 out-of-scope event. We'll get to the watchpoint next
5508 iteration. */
d0fb5eae 5509 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5510 {
5511 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5512
5513 w->watchpoint_triggered = watch_triggered_yes;
5514 }
f431efe5
PA
5515 }
5516 }
5517
7c16b83e 5518 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5519 if (!target_supports_stopped_by_sw_breakpoint ()
5520 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5521 {
1cf4d951 5522 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5523 {
1cf4d951
PA
5524 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5525 && need_moribund_for_location_type (loc))
5526 {
04afa70c 5527 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5528 /* For hits of moribund locations, we should just proceed. */
5529 bs->stop = 0;
5530 bs->print = 0;
5531 bs->print_it = print_it_noop;
5532 }
f431efe5
PA
5533 }
5534 }
5535
edcc5120
TT
5536 /* A bit of special processing for shlib breakpoints. We need to
5537 process solib loading here, so that the lists of loaded and
5538 unloaded libraries are correct before we handle "catch load" and
5539 "catch unload". */
5540 for (bs = bs_head; bs != NULL; bs = bs->next)
5541 {
5d268276 5542 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5543 {
5544 handle_solib_event ();
5545 break;
5546 }
5547 }
5548
f431efe5
PA
5549 /* Now go through the locations that caused the target to stop, and
5550 check whether we're interested in reporting this stop to higher
5551 layers, or whether we should resume the target transparently. */
5552
5553 removed_any = 0;
5554
5760d0ab 5555 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5556 {
5557 if (!bs->stop)
5558 continue;
5559
f431efe5 5560 b = bs->breakpoint_at;
348d480f
PA
5561 b->ops->check_status (bs);
5562 if (bs->stop)
28010a5d 5563 {
348d480f 5564 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5565
429374b8
JK
5566 if (bs->stop)
5567 {
5568 ++(b->hit_count);
8d3788bd 5569 observer_notify_breakpoint_modified (b);
c906108c 5570
4a64f543 5571 /* We will stop here. */
429374b8
JK
5572 if (b->disposition == disp_disable)
5573 {
816338b5 5574 --(b->enable_count);
1a853c52 5575 if (b->enable_count <= 0)
429374b8 5576 b->enable_state = bp_disabled;
f431efe5 5577 removed_any = 1;
429374b8
JK
5578 }
5579 if (b->silent)
5580 bs->print = 0;
5581 bs->commands = b->commands;
abf85f46 5582 if (command_line_is_silent (bs->commands
d1b0a7bf 5583 ? bs->commands.get () : NULL))
abf85f46 5584 bs->print = 0;
9d6e6e84
HZ
5585
5586 b->ops->after_condition_true (bs);
429374b8
JK
5587 }
5588
348d480f 5589 }
a9b3a50f
PA
5590
5591 /* Print nothing for this entry if we don't stop or don't
5592 print. */
5593 if (!bs->stop || !bs->print)
5594 bs->print_it = print_it_noop;
429374b8 5595 }
876fa593 5596
d983da9c
DJ
5597 /* If we aren't stopping, the value of some hardware watchpoint may
5598 not have changed, but the intermediate memory locations we are
5599 watching may have. Don't bother if we're stopping; this will get
5600 done later. */
d832cb68 5601 need_remove_insert = 0;
5760d0ab
JK
5602 if (! bpstat_causes_stop (bs_head))
5603 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5604 if (!bs->stop
f431efe5
PA
5605 && bs->breakpoint_at
5606 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5607 {
3a5c3e22
PA
5608 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5609
5610 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5611 need_remove_insert = 1;
d983da9c
DJ
5612 }
5613
d832cb68 5614 if (need_remove_insert)
44702360 5615 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5616 else if (removed_any)
44702360 5617 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5618
5760d0ab 5619 return bs_head;
c906108c 5620}
628fe4e4
JK
5621
5622static void
5623handle_jit_event (void)
5624{
5625 struct frame_info *frame;
5626 struct gdbarch *gdbarch;
5627
243a9253
PA
5628 if (debug_infrun)
5629 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5630
628fe4e4
JK
5631 /* Switch terminal for any messages produced by
5632 breakpoint_re_set. */
223ffa71 5633 target_terminal::ours_for_output ();
628fe4e4
JK
5634
5635 frame = get_current_frame ();
5636 gdbarch = get_frame_arch (frame);
5637
5638 jit_event_handler (gdbarch);
5639
223ffa71 5640 target_terminal::inferior ();
628fe4e4
JK
5641}
5642
5643/* Prepare WHAT final decision for infrun. */
5644
5645/* Decide what infrun needs to do with this bpstat. */
5646
c906108c 5647struct bpstat_what
0e30163f 5648bpstat_what (bpstat bs_head)
c906108c 5649{
c906108c 5650 struct bpstat_what retval;
0e30163f 5651 bpstat bs;
c906108c 5652
628fe4e4 5653 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5654 retval.call_dummy = STOP_NONE;
186c406b 5655 retval.is_longjmp = 0;
628fe4e4 5656
0e30163f 5657 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5658 {
628fe4e4
JK
5659 /* Extract this BS's action. After processing each BS, we check
5660 if its action overrides all we've seem so far. */
5661 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5662 enum bptype bptype;
5663
c906108c 5664 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5665 {
5666 /* I suspect this can happen if it was a momentary
5667 breakpoint which has since been deleted. */
5668 bptype = bp_none;
5669 }
20874c92 5670 else
f431efe5 5671 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5672
5673 switch (bptype)
c906108c
SS
5674 {
5675 case bp_none:
628fe4e4 5676 break;
c906108c
SS
5677 case bp_breakpoint:
5678 case bp_hardware_breakpoint:
7c16b83e 5679 case bp_single_step:
c906108c
SS
5680 case bp_until:
5681 case bp_finish:
a9b3a50f 5682 case bp_shlib_event:
c906108c
SS
5683 if (bs->stop)
5684 {
5685 if (bs->print)
628fe4e4 5686 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5687 else
628fe4e4 5688 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5689 }
5690 else
628fe4e4 5691 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5692 break;
5693 case bp_watchpoint:
5694 case bp_hardware_watchpoint:
5695 case bp_read_watchpoint:
5696 case bp_access_watchpoint:
5697 if (bs->stop)
5698 {
5699 if (bs->print)
628fe4e4 5700 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5701 else
628fe4e4 5702 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5703 }
5704 else
628fe4e4
JK
5705 {
5706 /* There was a watchpoint, but we're not stopping.
5707 This requires no further action. */
5708 }
c906108c
SS
5709 break;
5710 case bp_longjmp:
e2e4d78b 5711 case bp_longjmp_call_dummy:
186c406b 5712 case bp_exception:
0a39bb32
PA
5713 if (bs->stop)
5714 {
5715 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5716 retval.is_longjmp = bptype != bp_exception;
5717 }
5718 else
5719 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5720 break;
5721 case bp_longjmp_resume:
186c406b 5722 case bp_exception_resume:
0a39bb32
PA
5723 if (bs->stop)
5724 {
5725 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5726 retval.is_longjmp = bptype == bp_longjmp_resume;
5727 }
5728 else
5729 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5730 break;
5731 case bp_step_resume:
5732 if (bs->stop)
628fe4e4
JK
5733 this_action = BPSTAT_WHAT_STEP_RESUME;
5734 else
c906108c 5735 {
628fe4e4
JK
5736 /* It is for the wrong frame. */
5737 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5738 }
c906108c 5739 break;
2c03e5be
PA
5740 case bp_hp_step_resume:
5741 if (bs->stop)
5742 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5743 else
5744 {
5745 /* It is for the wrong frame. */
5746 this_action = BPSTAT_WHAT_SINGLE;
5747 }
5748 break;
c906108c 5749 case bp_watchpoint_scope:
c4093a6a 5750 case bp_thread_event:
1900040c 5751 case bp_overlay_event:
0fd8e87f 5752 case bp_longjmp_master:
aa7d318d 5753 case bp_std_terminate_master:
186c406b 5754 case bp_exception_master:
628fe4e4 5755 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5756 break;
ce78b96d 5757 case bp_catchpoint:
c5aa993b
JM
5758 if (bs->stop)
5759 {
5760 if (bs->print)
628fe4e4 5761 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5762 else
628fe4e4 5763 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5764 }
5765 else
628fe4e4
JK
5766 {
5767 /* There was a catchpoint, but we're not stopping.
5768 This requires no further action. */
5769 }
5770 break;
628fe4e4 5771 case bp_jit_event:
628fe4e4 5772 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5773 break;
c906108c 5774 case bp_call_dummy:
53a5351d
JM
5775 /* Make sure the action is stop (silent or noisy),
5776 so infrun.c pops the dummy frame. */
aa7d318d 5777 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5778 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5779 break;
5780 case bp_std_terminate:
5781 /* Make sure the action is stop (silent or noisy),
5782 so infrun.c pops the dummy frame. */
aa7d318d 5783 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5784 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5785 break;
1042e4c0 5786 case bp_tracepoint:
7a697b8d 5787 case bp_fast_tracepoint:
0fb4aa4b 5788 case bp_static_tracepoint:
1042e4c0
SS
5789 /* Tracepoint hits should not be reported back to GDB, and
5790 if one got through somehow, it should have been filtered
5791 out already. */
5792 internal_error (__FILE__, __LINE__,
7a697b8d 5793 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5794 break;
5795 case bp_gnu_ifunc_resolver:
5796 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5797 this_action = BPSTAT_WHAT_SINGLE;
5798 break;
5799 case bp_gnu_ifunc_resolver_return:
5800 /* The breakpoint will be removed, execution will restart from the
5801 PC of the former breakpoint. */
5802 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5803 break;
e7e0cddf
SS
5804
5805 case bp_dprintf:
a11cfd87
HZ
5806 if (bs->stop)
5807 this_action = BPSTAT_WHAT_STOP_SILENT;
5808 else
5809 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5810 break;
5811
628fe4e4
JK
5812 default:
5813 internal_error (__FILE__, __LINE__,
5814 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5815 }
628fe4e4 5816
325fac50 5817 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5818 }
628fe4e4 5819
243a9253
PA
5820 return retval;
5821}
628fe4e4 5822
243a9253
PA
5823void
5824bpstat_run_callbacks (bpstat bs_head)
5825{
5826 bpstat bs;
628fe4e4 5827
0e30163f
JK
5828 for (bs = bs_head; bs != NULL; bs = bs->next)
5829 {
5830 struct breakpoint *b = bs->breakpoint_at;
5831
5832 if (b == NULL)
5833 continue;
5834 switch (b->type)
5835 {
243a9253
PA
5836 case bp_jit_event:
5837 handle_jit_event ();
5838 break;
0e30163f
JK
5839 case bp_gnu_ifunc_resolver:
5840 gnu_ifunc_resolver_stop (b);
5841 break;
5842 case bp_gnu_ifunc_resolver_return:
5843 gnu_ifunc_resolver_return_stop (b);
5844 break;
5845 }
5846 }
c906108c
SS
5847}
5848
5849/* Nonzero if we should step constantly (e.g. watchpoints on machines
5850 without hardware support). This isn't related to a specific bpstat,
5851 just to things like whether watchpoints are set. */
5852
c5aa993b 5853int
fba45db2 5854bpstat_should_step (void)
c906108c
SS
5855{
5856 struct breakpoint *b;
cc59ec59 5857
c906108c 5858 ALL_BREAKPOINTS (b)
717a8278 5859 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5860 return 1;
c906108c
SS
5861 return 0;
5862}
5863
67822962
PA
5864int
5865bpstat_causes_stop (bpstat bs)
5866{
5867 for (; bs != NULL; bs = bs->next)
5868 if (bs->stop)
5869 return 1;
5870
5871 return 0;
5872}
5873
c906108c 5874\f
c5aa993b 5875
170b53b2
UW
5876/* Compute a string of spaces suitable to indent the next line
5877 so it starts at the position corresponding to the table column
5878 named COL_NAME in the currently active table of UIOUT. */
5879
5880static char *
5881wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5882{
5883 static char wrap_indent[80];
5884 int i, total_width, width, align;
c5209615 5885 const char *text;
170b53b2
UW
5886
5887 total_width = 0;
112e8700 5888 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5889 {
5890 if (strcmp (text, col_name) == 0)
5891 {
5892 gdb_assert (total_width < sizeof wrap_indent);
5893 memset (wrap_indent, ' ', total_width);
5894 wrap_indent[total_width] = 0;
5895
5896 return wrap_indent;
5897 }
5898
5899 total_width += width + 1;
5900 }
5901
5902 return NULL;
5903}
5904
b775012e
LM
5905/* Determine if the locations of this breakpoint will have their conditions
5906 evaluated by the target, host or a mix of both. Returns the following:
5907
5908 "host": Host evals condition.
5909 "host or target": Host or Target evals condition.
5910 "target": Target evals condition.
5911*/
5912
5913static const char *
5914bp_condition_evaluator (struct breakpoint *b)
5915{
5916 struct bp_location *bl;
5917 char host_evals = 0;
5918 char target_evals = 0;
5919
5920 if (!b)
5921 return NULL;
5922
5923 if (!is_breakpoint (b))
5924 return NULL;
5925
5926 if (gdb_evaluates_breakpoint_condition_p ()
5927 || !target_supports_evaluation_of_breakpoint_conditions ())
5928 return condition_evaluation_host;
5929
5930 for (bl = b->loc; bl; bl = bl->next)
5931 {
5932 if (bl->cond_bytecode)
5933 target_evals++;
5934 else
5935 host_evals++;
5936 }
5937
5938 if (host_evals && target_evals)
5939 return condition_evaluation_both;
5940 else if (target_evals)
5941 return condition_evaluation_target;
5942 else
5943 return condition_evaluation_host;
5944}
5945
5946/* Determine the breakpoint location's condition evaluator. This is
5947 similar to bp_condition_evaluator, but for locations. */
5948
5949static const char *
5950bp_location_condition_evaluator (struct bp_location *bl)
5951{
5952 if (bl && !is_breakpoint (bl->owner))
5953 return NULL;
5954
5955 if (gdb_evaluates_breakpoint_condition_p ()
5956 || !target_supports_evaluation_of_breakpoint_conditions ())
5957 return condition_evaluation_host;
5958
5959 if (bl && bl->cond_bytecode)
5960 return condition_evaluation_target;
5961 else
5962 return condition_evaluation_host;
5963}
5964
859825b8
JK
5965/* Print the LOC location out of the list of B->LOC locations. */
5966
170b53b2
UW
5967static void
5968print_breakpoint_location (struct breakpoint *b,
5969 struct bp_location *loc)
0d381245 5970{
79a45e25 5971 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5972
5973 scoped_restore_current_program_space restore_pspace;
6c95b8df 5974
859825b8
JK
5975 if (loc != NULL && loc->shlib_disabled)
5976 loc = NULL;
5977
6c95b8df
PA
5978 if (loc != NULL)
5979 set_current_program_space (loc->pspace);
5980
56435ebe 5981 if (b->display_canonical)
d28cd78a 5982 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5983 else if (loc && loc->symtab)
0d381245
VP
5984 {
5985 struct symbol *sym
5986 = find_pc_sect_function (loc->address, loc->section);
5987 if (sym)
5988 {
112e8700
SM
5989 uiout->text ("in ");
5990 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5991 uiout->text (" ");
5992 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5993 uiout->text ("at ");
0d381245 5994 }
112e8700 5995 uiout->field_string ("file",
05cba821 5996 symtab_to_filename_for_display (loc->symtab));
112e8700 5997 uiout->text (":");
05cba821 5998
112e8700
SM
5999 if (uiout->is_mi_like_p ())
6000 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6001
112e8700 6002 uiout->field_int ("line", loc->line_number);
0d381245 6003 }
859825b8 6004 else if (loc)
0d381245 6005 {
d7e74731 6006 string_file stb;
170b53b2 6007
d7e74731 6008 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6009 demangle, "");
112e8700 6010 uiout->field_stream ("at", stb);
0d381245 6011 }
859825b8 6012 else
f00aae0f 6013 {
d28cd78a
TT
6014 uiout->field_string ("pending",
6015 event_location_to_string (b->location.get ()));
f00aae0f
KS
6016 /* If extra_string is available, it could be holding a condition
6017 or dprintf arguments. In either case, make sure it is printed,
6018 too, but only for non-MI streams. */
112e8700 6019 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6020 {
6021 if (b->type == bp_dprintf)
112e8700 6022 uiout->text (",");
f00aae0f 6023 else
112e8700
SM
6024 uiout->text (" ");
6025 uiout->text (b->extra_string);
f00aae0f
KS
6026 }
6027 }
6c95b8df 6028
b775012e
LM
6029 if (loc && is_breakpoint (b)
6030 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6031 && bp_condition_evaluator (b) == condition_evaluation_both)
6032 {
112e8700
SM
6033 uiout->text (" (");
6034 uiout->field_string ("evaluated-by",
b775012e 6035 bp_location_condition_evaluator (loc));
112e8700 6036 uiout->text (")");
b775012e 6037 }
0d381245
VP
6038}
6039
269b11a2
PA
6040static const char *
6041bptype_string (enum bptype type)
c906108c 6042{
c4093a6a
JM
6043 struct ep_type_description
6044 {
6045 enum bptype type;
a121b7c1 6046 const char *description;
c4093a6a
JM
6047 };
6048 static struct ep_type_description bptypes[] =
c906108c 6049 {
c5aa993b
JM
6050 {bp_none, "?deleted?"},
6051 {bp_breakpoint, "breakpoint"},
c906108c 6052 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6053 {bp_single_step, "sw single-step"},
c5aa993b
JM
6054 {bp_until, "until"},
6055 {bp_finish, "finish"},
6056 {bp_watchpoint, "watchpoint"},
c906108c 6057 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6058 {bp_read_watchpoint, "read watchpoint"},
6059 {bp_access_watchpoint, "acc watchpoint"},
6060 {bp_longjmp, "longjmp"},
6061 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6062 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6063 {bp_exception, "exception"},
6064 {bp_exception_resume, "exception resume"},
c5aa993b 6065 {bp_step_resume, "step resume"},
2c03e5be 6066 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6067 {bp_watchpoint_scope, "watchpoint scope"},
6068 {bp_call_dummy, "call dummy"},
aa7d318d 6069 {bp_std_terminate, "std::terminate"},
c5aa993b 6070 {bp_shlib_event, "shlib events"},
c4093a6a 6071 {bp_thread_event, "thread events"},
1900040c 6072 {bp_overlay_event, "overlay events"},
0fd8e87f 6073 {bp_longjmp_master, "longjmp master"},
aa7d318d 6074 {bp_std_terminate_master, "std::terminate master"},
186c406b 6075 {bp_exception_master, "exception master"},
ce78b96d 6076 {bp_catchpoint, "catchpoint"},
1042e4c0 6077 {bp_tracepoint, "tracepoint"},
7a697b8d 6078 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6079 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6080 {bp_dprintf, "dprintf"},
4efc6507 6081 {bp_jit_event, "jit events"},
0e30163f
JK
6082 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6083 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6084 };
269b11a2
PA
6085
6086 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6087 || ((int) type != bptypes[(int) type].type))
6088 internal_error (__FILE__, __LINE__,
6089 _("bptypes table does not describe type #%d."),
6090 (int) type);
6091
6092 return bptypes[(int) type].description;
6093}
6094
998580f1
MK
6095/* For MI, output a field named 'thread-groups' with a list as the value.
6096 For CLI, prefix the list with the string 'inf'. */
6097
6098static void
6099output_thread_groups (struct ui_out *uiout,
6100 const char *field_name,
6101 VEC(int) *inf_num,
6102 int mi_only)
6103{
112e8700 6104 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6105 int inf;
6106 int i;
6107
6108 /* For backward compatibility, don't display inferiors in CLI unless
6109 there are several. Always display them for MI. */
6110 if (!is_mi && mi_only)
6111 return;
6112
10f489e5 6113 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6114
998580f1
MK
6115 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6116 {
6117 if (is_mi)
6118 {
6119 char mi_group[10];
6120
6121 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6122 uiout->field_string (NULL, mi_group);
998580f1
MK
6123 }
6124 else
6125 {
6126 if (i == 0)
112e8700 6127 uiout->text (" inf ");
998580f1 6128 else
112e8700 6129 uiout->text (", ");
998580f1 6130
112e8700 6131 uiout->text (plongest (inf));
998580f1
MK
6132 }
6133 }
998580f1
MK
6134}
6135
269b11a2
PA
6136/* Print B to gdb_stdout. */
6137
6138static void
6139print_one_breakpoint_location (struct breakpoint *b,
6140 struct bp_location *loc,
6141 int loc_number,
6142 struct bp_location **last_loc,
269b11a2
PA
6143 int allflag)
6144{
6145 struct command_line *l;
c2c6d25f 6146 static char bpenables[] = "nynny";
c906108c 6147
79a45e25 6148 struct ui_out *uiout = current_uiout;
0d381245
VP
6149 int header_of_multiple = 0;
6150 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6151 struct value_print_options opts;
6152
6153 get_user_print_options (&opts);
0d381245
VP
6154
6155 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6156 /* See comment in print_one_breakpoint concerning treatment of
6157 breakpoints with single disabled location. */
0d381245
VP
6158 if (loc == NULL
6159 && (b->loc != NULL
6160 && (b->loc->next != NULL || !b->loc->enabled)))
6161 header_of_multiple = 1;
6162 if (loc == NULL)
6163 loc = b->loc;
6164
c4093a6a
JM
6165 annotate_record ();
6166
6167 /* 1 */
6168 annotate_field (0);
0d381245
VP
6169 if (part_of_multiple)
6170 {
6171 char *formatted;
0c6773c1 6172 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6173 uiout->field_string ("number", formatted);
0d381245
VP
6174 xfree (formatted);
6175 }
6176 else
6177 {
112e8700 6178 uiout->field_int ("number", b->number);
0d381245 6179 }
c4093a6a
JM
6180
6181 /* 2 */
6182 annotate_field (1);
0d381245 6183 if (part_of_multiple)
112e8700 6184 uiout->field_skip ("type");
269b11a2 6185 else
112e8700 6186 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6187
6188 /* 3 */
6189 annotate_field (2);
0d381245 6190 if (part_of_multiple)
112e8700 6191 uiout->field_skip ("disp");
0d381245 6192 else
112e8700 6193 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6194
c4093a6a
JM
6195
6196 /* 4 */
6197 annotate_field (3);
0d381245 6198 if (part_of_multiple)
112e8700 6199 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6200 else
112e8700
SM
6201 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6202 uiout->spaces (2);
0d381245 6203
c4093a6a
JM
6204
6205 /* 5 and 6 */
3086aeae 6206 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6207 {
4a64f543
MS
6208 /* Although the print_one can possibly print all locations,
6209 calling it here is not likely to get any nice result. So,
6210 make sure there's just one location. */
0d381245 6211 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6212 b->ops->print_one (b, last_loc);
0d381245 6213 }
3086aeae
DJ
6214 else
6215 switch (b->type)
6216 {
6217 case bp_none:
6218 internal_error (__FILE__, __LINE__,
e2e0b3e5 6219 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6220 break;
c906108c 6221
3086aeae
DJ
6222 case bp_watchpoint:
6223 case bp_hardware_watchpoint:
6224 case bp_read_watchpoint:
6225 case bp_access_watchpoint:
3a5c3e22
PA
6226 {
6227 struct watchpoint *w = (struct watchpoint *) b;
6228
6229 /* Field 4, the address, is omitted (which makes the columns
6230 not line up too nicely with the headers, but the effect
6231 is relatively readable). */
6232 if (opts.addressprint)
112e8700 6233 uiout->field_skip ("addr");
3a5c3e22 6234 annotate_field (5);
112e8700 6235 uiout->field_string ("what", w->exp_string);
3a5c3e22 6236 }
3086aeae
DJ
6237 break;
6238
3086aeae
DJ
6239 case bp_breakpoint:
6240 case bp_hardware_breakpoint:
7c16b83e 6241 case bp_single_step:
3086aeae
DJ
6242 case bp_until:
6243 case bp_finish:
6244 case bp_longjmp:
6245 case bp_longjmp_resume:
e2e4d78b 6246 case bp_longjmp_call_dummy:
186c406b
TT
6247 case bp_exception:
6248 case bp_exception_resume:
3086aeae 6249 case bp_step_resume:
2c03e5be 6250 case bp_hp_step_resume:
3086aeae
DJ
6251 case bp_watchpoint_scope:
6252 case bp_call_dummy:
aa7d318d 6253 case bp_std_terminate:
3086aeae
DJ
6254 case bp_shlib_event:
6255 case bp_thread_event:
6256 case bp_overlay_event:
0fd8e87f 6257 case bp_longjmp_master:
aa7d318d 6258 case bp_std_terminate_master:
186c406b 6259 case bp_exception_master:
1042e4c0 6260 case bp_tracepoint:
7a697b8d 6261 case bp_fast_tracepoint:
0fb4aa4b 6262 case bp_static_tracepoint:
e7e0cddf 6263 case bp_dprintf:
4efc6507 6264 case bp_jit_event:
0e30163f
JK
6265 case bp_gnu_ifunc_resolver:
6266 case bp_gnu_ifunc_resolver_return:
79a45b7d 6267 if (opts.addressprint)
3086aeae
DJ
6268 {
6269 annotate_field (4);
54e52265 6270 if (header_of_multiple)
112e8700 6271 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6272 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6273 uiout->field_string ("addr", "<PENDING>");
0101ce28 6274 else
112e8700 6275 uiout->field_core_addr ("addr",
5af949e3 6276 loc->gdbarch, loc->address);
3086aeae
DJ
6277 }
6278 annotate_field (5);
0d381245 6279 if (!header_of_multiple)
170b53b2 6280 print_breakpoint_location (b, loc);
0d381245 6281 if (b->loc)
a6d9a66e 6282 *last_loc = b->loc;
3086aeae
DJ
6283 break;
6284 }
c906108c 6285
6c95b8df 6286
998580f1 6287 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6288 {
6289 struct inferior *inf;
998580f1
MK
6290 VEC(int) *inf_num = NULL;
6291 int mi_only = 1;
6c95b8df 6292
998580f1 6293 ALL_INFERIORS (inf)
6c95b8df
PA
6294 {
6295 if (inf->pspace == loc->pspace)
998580f1 6296 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6297 }
998580f1
MK
6298
6299 /* For backward compatibility, don't display inferiors in CLI unless
6300 there are several. Always display for MI. */
6301 if (allflag
6302 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6303 && (number_of_program_spaces () > 1
6304 || number_of_inferiors () > 1)
6305 /* LOC is for existing B, it cannot be in
6306 moribund_locations and thus having NULL OWNER. */
6307 && loc->owner->type != bp_catchpoint))
6308 mi_only = 0;
6309 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6310 VEC_free (int, inf_num);
6c95b8df
PA
6311 }
6312
4a306c9a 6313 if (!part_of_multiple)
c4093a6a 6314 {
4a306c9a
JB
6315 if (b->thread != -1)
6316 {
6317 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6318 "stop only in" line a little further down. */
112e8700
SM
6319 uiout->text (" thread ");
6320 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6321 }
6322 else if (b->task != 0)
6323 {
112e8700
SM
6324 uiout->text (" task ");
6325 uiout->field_int ("task", b->task);
4a306c9a 6326 }
c4093a6a 6327 }
f1310107 6328
112e8700 6329 uiout->text ("\n");
f1310107 6330
348d480f 6331 if (!part_of_multiple)
f1310107
TJB
6332 b->ops->print_one_detail (b, uiout);
6333
0d381245 6334 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6335 {
6336 annotate_field (6);
112e8700 6337 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6338 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6339 the frame ID. */
112e8700 6340 uiout->field_core_addr ("frame",
5af949e3 6341 b->gdbarch, b->frame_id.stack_addr);
112e8700 6342 uiout->text ("\n");
c4093a6a
JM
6343 }
6344
28010a5d 6345 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6346 {
6347 annotate_field (7);
d77f58be 6348 if (is_tracepoint (b))
112e8700 6349 uiout->text ("\ttrace only if ");
1042e4c0 6350 else
112e8700
SM
6351 uiout->text ("\tstop only if ");
6352 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6353
6354 /* Print whether the target is doing the breakpoint's condition
6355 evaluation. If GDB is doing the evaluation, don't print anything. */
6356 if (is_breakpoint (b)
6357 && breakpoint_condition_evaluation_mode ()
6358 == condition_evaluation_target)
6359 {
112e8700
SM
6360 uiout->text (" (");
6361 uiout->field_string ("evaluated-by",
b775012e 6362 bp_condition_evaluator (b));
112e8700 6363 uiout->text (" evals)");
b775012e 6364 }
112e8700 6365 uiout->text ("\n");
0101ce28
JJ
6366 }
6367
0d381245 6368 if (!part_of_multiple && b->thread != -1)
c4093a6a 6369 {
4a64f543 6370 /* FIXME should make an annotation for this. */
112e8700
SM
6371 uiout->text ("\tstop only in thread ");
6372 if (uiout->is_mi_like_p ())
6373 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6374 else
6375 {
6376 struct thread_info *thr = find_thread_global_id (b->thread);
6377
112e8700 6378 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6379 }
112e8700 6380 uiout->text ("\n");
c4093a6a
JM
6381 }
6382
556ec64d
YQ
6383 if (!part_of_multiple)
6384 {
6385 if (b->hit_count)
31f56a27
YQ
6386 {
6387 /* FIXME should make an annotation for this. */
6388 if (is_catchpoint (b))
112e8700 6389 uiout->text ("\tcatchpoint");
31f56a27 6390 else if (is_tracepoint (b))
112e8700 6391 uiout->text ("\ttracepoint");
31f56a27 6392 else
112e8700
SM
6393 uiout->text ("\tbreakpoint");
6394 uiout->text (" already hit ");
6395 uiout->field_int ("times", b->hit_count);
31f56a27 6396 if (b->hit_count == 1)
112e8700 6397 uiout->text (" time\n");
31f56a27 6398 else
112e8700 6399 uiout->text (" times\n");
31f56a27 6400 }
556ec64d
YQ
6401 else
6402 {
31f56a27 6403 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6404 if (uiout->is_mi_like_p ())
6405 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6406 }
6407 }
8b93c638 6408
0d381245 6409 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6410 {
6411 annotate_field (8);
112e8700
SM
6412 uiout->text ("\tignore next ");
6413 uiout->field_int ("ignore", b->ignore_count);
6414 uiout->text (" hits\n");
c4093a6a 6415 }
059fb39f 6416
816338b5
SS
6417 /* Note that an enable count of 1 corresponds to "enable once"
6418 behavior, which is reported by the combination of enablement and
6419 disposition, so we don't need to mention it here. */
6420 if (!part_of_multiple && b->enable_count > 1)
6421 {
6422 annotate_field (8);
112e8700 6423 uiout->text ("\tdisable after ");
816338b5
SS
6424 /* Tweak the wording to clarify that ignore and enable counts
6425 are distinct, and have additive effect. */
6426 if (b->ignore_count)
112e8700 6427 uiout->text ("additional ");
816338b5 6428 else
112e8700
SM
6429 uiout->text ("next ");
6430 uiout->field_int ("enable", b->enable_count);
6431 uiout->text (" hits\n");
816338b5
SS
6432 }
6433
f196051f
SS
6434 if (!part_of_multiple && is_tracepoint (b))
6435 {
6436 struct tracepoint *tp = (struct tracepoint *) b;
6437
6438 if (tp->traceframe_usage)
6439 {
112e8700
SM
6440 uiout->text ("\ttrace buffer usage ");
6441 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6442 uiout->text (" bytes\n");
f196051f
SS
6443 }
6444 }
d3ce09f5 6445
d1b0a7bf 6446 l = b->commands ? b->commands.get () : NULL;
059fb39f 6447 if (!part_of_multiple && l)
c4093a6a
JM
6448 {
6449 annotate_field (9);
2e783024 6450 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6451 print_command_lines (uiout, l, 4);
c4093a6a 6452 }
d24317b4 6453
d9b3f62e 6454 if (is_tracepoint (b))
1042e4c0 6455 {
d9b3f62e
PA
6456 struct tracepoint *t = (struct tracepoint *) b;
6457
6458 if (!part_of_multiple && t->pass_count)
6459 {
6460 annotate_field (10);
112e8700
SM
6461 uiout->text ("\tpass count ");
6462 uiout->field_int ("pass", t->pass_count);
6463 uiout->text (" \n");
d9b3f62e 6464 }
f2a8bc8a
YQ
6465
6466 /* Don't display it when tracepoint or tracepoint location is
6467 pending. */
6468 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6469 {
6470 annotate_field (11);
6471
112e8700
SM
6472 if (uiout->is_mi_like_p ())
6473 uiout->field_string ("installed",
f2a8bc8a
YQ
6474 loc->inserted ? "y" : "n");
6475 else
6476 {
6477 if (loc->inserted)
112e8700 6478 uiout->text ("\t");
f2a8bc8a 6479 else
112e8700
SM
6480 uiout->text ("\tnot ");
6481 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6482 }
6483 }
1042e4c0
SS
6484 }
6485
112e8700 6486 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6487 {
3a5c3e22
PA
6488 if (is_watchpoint (b))
6489 {
6490 struct watchpoint *w = (struct watchpoint *) b;
6491
112e8700 6492 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6493 }
f00aae0f 6494 else if (b->location != NULL
d28cd78a 6495 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6496 uiout->field_string ("original-location",
d28cd78a 6497 event_location_to_string (b->location.get ()));
d24317b4 6498 }
c4093a6a 6499}
c5aa993b 6500
0d381245
VP
6501static void
6502print_one_breakpoint (struct breakpoint *b,
4a64f543 6503 struct bp_location **last_loc,
6c95b8df 6504 int allflag)
0d381245 6505{
79a45e25 6506 struct ui_out *uiout = current_uiout;
8d3788bd 6507
2e783024
TT
6508 {
6509 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6510
2e783024
TT
6511 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6512 }
0d381245
VP
6513
6514 /* If this breakpoint has custom print function,
6515 it's already printed. Otherwise, print individual
6516 locations, if any. */
6517 if (b->ops == NULL || b->ops->print_one == NULL)
6518 {
4a64f543
MS
6519 /* If breakpoint has a single location that is disabled, we
6520 print it as if it had several locations, since otherwise it's
6521 hard to represent "breakpoint enabled, location disabled"
6522 situation.
6523
6524 Note that while hardware watchpoints have several locations
a3be7890 6525 internally, that's not a property exposed to user. */
0d381245 6526 if (b->loc
a5606eee 6527 && !is_hardware_watchpoint (b)
8d3788bd 6528 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6529 {
6530 struct bp_location *loc;
6531 int n = 1;
8d3788bd 6532
0d381245 6533 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6534 {
2e783024 6535 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6536 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6537 }
0d381245
VP
6538 }
6539 }
6540}
6541
a6d9a66e
UW
6542static int
6543breakpoint_address_bits (struct breakpoint *b)
6544{
6545 int print_address_bits = 0;
6546 struct bp_location *loc;
6547
c6d81124
PA
6548 /* Software watchpoints that aren't watching memory don't have an
6549 address to print. */
6550 if (is_no_memory_software_watchpoint (b))
6551 return 0;
6552
a6d9a66e
UW
6553 for (loc = b->loc; loc; loc = loc->next)
6554 {
c7437ca6
PA
6555 int addr_bit;
6556
c7437ca6 6557 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6558 if (addr_bit > print_address_bits)
6559 print_address_bits = addr_bit;
6560 }
6561
6562 return print_address_bits;
6563}
0d381245 6564
65630365 6565/* See breakpoint.h. */
c5aa993b 6566
65630365
PA
6567void
6568print_breakpoint (breakpoint *b)
c4093a6a 6569{
a6d9a66e 6570 struct bp_location *dummy_loc = NULL;
65630365 6571 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6572}
c5aa993b 6573
09d682a4
TT
6574/* Return true if this breakpoint was set by the user, false if it is
6575 internal or momentary. */
6576
6577int
6578user_breakpoint_p (struct breakpoint *b)
6579{
46c6471b 6580 return b->number > 0;
09d682a4
TT
6581}
6582
93daf339
TT
6583/* See breakpoint.h. */
6584
6585int
6586pending_breakpoint_p (struct breakpoint *b)
6587{
6588 return b->loc == NULL;
6589}
6590
7f3b0473 6591/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6592 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6593 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6594 FILTER is non-NULL, call it on each breakpoint and only include the
6595 ones for which it returns non-zero. Return the total number of
6596 breakpoints listed. */
c906108c 6597
d77f58be 6598static int
4495129a 6599breakpoint_1 (const char *args, int allflag,
4a64f543 6600 int (*filter) (const struct breakpoint *))
c4093a6a 6601{
52f0bd74 6602 struct breakpoint *b;
a6d9a66e 6603 struct bp_location *last_loc = NULL;
7f3b0473 6604 int nr_printable_breakpoints;
79a45b7d 6605 struct value_print_options opts;
a6d9a66e 6606 int print_address_bits = 0;
269b11a2 6607 int print_type_col_width = 14;
79a45e25 6608 struct ui_out *uiout = current_uiout;
269b11a2 6609
79a45b7d
TT
6610 get_user_print_options (&opts);
6611
4a64f543
MS
6612 /* Compute the number of rows in the table, as well as the size
6613 required for address fields. */
7f3b0473
AC
6614 nr_printable_breakpoints = 0;
6615 ALL_BREAKPOINTS (b)
e5a67952
MS
6616 {
6617 /* If we have a filter, only list the breakpoints it accepts. */
6618 if (filter && !filter (b))
6619 continue;
6620
6621 /* If we have an "args" string, it is a list of breakpoints to
6622 accept. Skip the others. */
6623 if (args != NULL && *args != '\0')
6624 {
6625 if (allflag && parse_and_eval_long (args) != b->number)
6626 continue;
6627 if (!allflag && !number_is_in_list (args, b->number))
6628 continue;
6629 }
269b11a2 6630
e5a67952
MS
6631 if (allflag || user_breakpoint_p (b))
6632 {
6633 int addr_bit, type_len;
a6d9a66e 6634
e5a67952
MS
6635 addr_bit = breakpoint_address_bits (b);
6636 if (addr_bit > print_address_bits)
6637 print_address_bits = addr_bit;
269b11a2 6638
e5a67952
MS
6639 type_len = strlen (bptype_string (b->type));
6640 if (type_len > print_type_col_width)
6641 print_type_col_width = type_len;
6642
6643 nr_printable_breakpoints++;
6644 }
6645 }
7f3b0473 6646
4a2b031d
TT
6647 {
6648 ui_out_emit_table table_emitter (uiout,
6649 opts.addressprint ? 6 : 5,
6650 nr_printable_breakpoints,
6651 "BreakpointTable");
6652
6653 if (nr_printable_breakpoints > 0)
6654 annotate_breakpoints_headers ();
6655 if (nr_printable_breakpoints > 0)
6656 annotate_field (0);
6657 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6658 if (nr_printable_breakpoints > 0)
6659 annotate_field (1);
6660 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6661 if (nr_printable_breakpoints > 0)
6662 annotate_field (2);
6663 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6664 if (nr_printable_breakpoints > 0)
6665 annotate_field (3);
6666 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6667 if (opts.addressprint)
6668 {
6669 if (nr_printable_breakpoints > 0)
6670 annotate_field (4);
6671 if (print_address_bits <= 32)
6672 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6673 else
6674 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6675 }
6676 if (nr_printable_breakpoints > 0)
6677 annotate_field (5);
6678 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6679 uiout->table_body ();
6680 if (nr_printable_breakpoints > 0)
6681 annotate_breakpoints_table ();
6682
6683 ALL_BREAKPOINTS (b)
6684 {
6685 QUIT;
6686 /* If we have a filter, only list the breakpoints it accepts. */
6687 if (filter && !filter (b))
6688 continue;
e5a67952 6689
4a2b031d
TT
6690 /* If we have an "args" string, it is a list of breakpoints to
6691 accept. Skip the others. */
e5a67952 6692
4a2b031d
TT
6693 if (args != NULL && *args != '\0')
6694 {
6695 if (allflag) /* maintenance info breakpoint */
6696 {
6697 if (parse_and_eval_long (args) != b->number)
6698 continue;
6699 }
6700 else /* all others */
6701 {
6702 if (!number_is_in_list (args, b->number))
6703 continue;
6704 }
6705 }
6706 /* We only print out user settable breakpoints unless the
6707 allflag is set. */
6708 if (allflag || user_breakpoint_p (b))
6709 print_one_breakpoint (b, &last_loc, allflag);
6710 }
6711 }
698384cd 6712
7f3b0473 6713 if (nr_printable_breakpoints == 0)
c906108c 6714 {
4a64f543
MS
6715 /* If there's a filter, let the caller decide how to report
6716 empty list. */
d77f58be
SS
6717 if (!filter)
6718 {
e5a67952 6719 if (args == NULL || *args == '\0')
112e8700 6720 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6721 else
112e8700 6722 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6723 args);
d77f58be 6724 }
c906108c
SS
6725 }
6726 else
c4093a6a 6727 {
a6d9a66e
UW
6728 if (last_loc && !server_command)
6729 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6730 }
c906108c 6731
4a64f543 6732 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6733 there have been breakpoints? */
c906108c 6734 annotate_breakpoints_table_end ();
d77f58be
SS
6735
6736 return nr_printable_breakpoints;
c906108c
SS
6737}
6738
ad443146
SS
6739/* Display the value of default-collect in a way that is generally
6740 compatible with the breakpoint list. */
6741
6742static void
6743default_collect_info (void)
6744{
79a45e25
PA
6745 struct ui_out *uiout = current_uiout;
6746
ad443146
SS
6747 /* If it has no value (which is frequently the case), say nothing; a
6748 message like "No default-collect." gets in user's face when it's
6749 not wanted. */
6750 if (!*default_collect)
6751 return;
6752
6753 /* The following phrase lines up nicely with per-tracepoint collect
6754 actions. */
112e8700
SM
6755 uiout->text ("default collect ");
6756 uiout->field_string ("default-collect", default_collect);
6757 uiout->text (" \n");
ad443146
SS
6758}
6759
c906108c 6760static void
11db9430 6761info_breakpoints_command (char *args, int from_tty)
c906108c 6762{
e5a67952 6763 breakpoint_1 (args, 0, NULL);
ad443146
SS
6764
6765 default_collect_info ();
d77f58be
SS
6766}
6767
6768static void
11db9430 6769info_watchpoints_command (char *args, int from_tty)
d77f58be 6770{
e5a67952 6771 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6772 struct ui_out *uiout = current_uiout;
d77f58be
SS
6773
6774 if (num_printed == 0)
6775 {
e5a67952 6776 if (args == NULL || *args == '\0')
112e8700 6777 uiout->message ("No watchpoints.\n");
d77f58be 6778 else
112e8700 6779 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6780 }
c906108c
SS
6781}
6782
7a292a7a 6783static void
4495129a 6784maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6785{
e5a67952 6786 breakpoint_1 (args, 1, NULL);
ad443146
SS
6787
6788 default_collect_info ();
c906108c
SS
6789}
6790
0d381245 6791static int
714835d5 6792breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6793 struct program_space *pspace,
714835d5 6794 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6795{
6796 struct bp_location *bl = b->loc;
cc59ec59 6797
0d381245
VP
6798 for (; bl; bl = bl->next)
6799 {
6c95b8df
PA
6800 if (bl->pspace == pspace
6801 && bl->address == pc
0d381245
VP
6802 && (!overlay_debugging || bl->section == section))
6803 return 1;
6804 }
6805 return 0;
6806}
6807
672f9b60 6808/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6809 concerns with logical breakpoints, so we match program spaces, not
6810 address spaces. */
c906108c
SS
6811
6812static void
6c95b8df
PA
6813describe_other_breakpoints (struct gdbarch *gdbarch,
6814 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6815 struct obj_section *section, int thread)
c906108c 6816{
52f0bd74
AC
6817 int others = 0;
6818 struct breakpoint *b;
c906108c
SS
6819
6820 ALL_BREAKPOINTS (b)
672f9b60
KP
6821 others += (user_breakpoint_p (b)
6822 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6823 if (others > 0)
6824 {
a3f17187
AC
6825 if (others == 1)
6826 printf_filtered (_("Note: breakpoint "));
6827 else /* if (others == ???) */
6828 printf_filtered (_("Note: breakpoints "));
c906108c 6829 ALL_BREAKPOINTS (b)
672f9b60 6830 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6831 {
6832 others--;
6833 printf_filtered ("%d", b->number);
6834 if (b->thread == -1 && thread != -1)
6835 printf_filtered (" (all threads)");
6836 else if (b->thread != -1)
6837 printf_filtered (" (thread %d)", b->thread);
6838 printf_filtered ("%s%s ",
059fb39f 6839 ((b->enable_state == bp_disabled
f8eba3c6 6840 || b->enable_state == bp_call_disabled)
0d381245 6841 ? " (disabled)"
0d381245
VP
6842 : ""),
6843 (others > 1) ? ","
6844 : ((others == 1) ? " and" : ""));
6845 }
a3f17187 6846 printf_filtered (_("also set at pc "));
5af949e3 6847 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6848 printf_filtered (".\n");
6849 }
6850}
6851\f
c906108c 6852
e4f237da 6853/* Return true iff it is meaningful to use the address member of
244558af
LM
6854 BPT locations. For some breakpoint types, the locations' address members
6855 are irrelevant and it makes no sense to attempt to compare them to other
6856 addresses (or use them for any other purpose either).
e4f237da 6857
4a64f543 6858 More specifically, each of the following breakpoint types will
244558af 6859 always have a zero valued location address and we don't want to mark
4a64f543 6860 breakpoints of any of these types to be a duplicate of an actual
244558af 6861 breakpoint location at address zero:
e4f237da
KB
6862
6863 bp_watchpoint
2d134ed3
PA
6864 bp_catchpoint
6865
6866*/
e4f237da
KB
6867
6868static int
6869breakpoint_address_is_meaningful (struct breakpoint *bpt)
6870{
6871 enum bptype type = bpt->type;
6872
2d134ed3
PA
6873 return (type != bp_watchpoint && type != bp_catchpoint);
6874}
6875
6876/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6877 true if LOC1 and LOC2 represent the same watchpoint location. */
6878
6879static int
4a64f543
MS
6880watchpoint_locations_match (struct bp_location *loc1,
6881 struct bp_location *loc2)
2d134ed3 6882{
3a5c3e22
PA
6883 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6884 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6885
6886 /* Both of them must exist. */
6887 gdb_assert (w1 != NULL);
6888 gdb_assert (w2 != NULL);
2bdf28a0 6889
4a64f543
MS
6890 /* If the target can evaluate the condition expression in hardware,
6891 then we we need to insert both watchpoints even if they are at
6892 the same place. Otherwise the watchpoint will only trigger when
6893 the condition of whichever watchpoint was inserted evaluates to
6894 true, not giving a chance for GDB to check the condition of the
6895 other watchpoint. */
3a5c3e22 6896 if ((w1->cond_exp
4a64f543
MS
6897 && target_can_accel_watchpoint_condition (loc1->address,
6898 loc1->length,
0cf6dd15 6899 loc1->watchpoint_type,
4d01a485 6900 w1->cond_exp.get ()))
3a5c3e22 6901 || (w2->cond_exp
4a64f543
MS
6902 && target_can_accel_watchpoint_condition (loc2->address,
6903 loc2->length,
0cf6dd15 6904 loc2->watchpoint_type,
4d01a485 6905 w2->cond_exp.get ())))
0cf6dd15
TJB
6906 return 0;
6907
85d721b8
PA
6908 /* Note that this checks the owner's type, not the location's. In
6909 case the target does not support read watchpoints, but does
6910 support access watchpoints, we'll have bp_read_watchpoint
6911 watchpoints with hw_access locations. Those should be considered
6912 duplicates of hw_read locations. The hw_read locations will
6913 become hw_access locations later. */
2d134ed3
PA
6914 return (loc1->owner->type == loc2->owner->type
6915 && loc1->pspace->aspace == loc2->pspace->aspace
6916 && loc1->address == loc2->address
6917 && loc1->length == loc2->length);
e4f237da
KB
6918}
6919
31e77af2 6920/* See breakpoint.h. */
6c95b8df 6921
31e77af2 6922int
6c95b8df
PA
6923breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6924 struct address_space *aspace2, CORE_ADDR addr2)
6925{
f5656ead 6926 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6927 || aspace1 == aspace2)
6928 && addr1 == addr2);
6929}
6930
f1310107
TJB
6931/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6932 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6933 matches ASPACE2. On targets that have global breakpoints, the address
6934 space doesn't really matter. */
6935
6936static int
6937breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6938 int len1, struct address_space *aspace2,
6939 CORE_ADDR addr2)
6940{
f5656ead 6941 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6942 || aspace1 == aspace2)
6943 && addr2 >= addr1 && addr2 < addr1 + len1);
6944}
6945
6946/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6947 a ranged breakpoint. In most targets, a match happens only if ASPACE
6948 matches the breakpoint's address space. On targets that have global
6949 breakpoints, the address space doesn't really matter. */
6950
6951static int
6952breakpoint_location_address_match (struct bp_location *bl,
6953 struct address_space *aspace,
6954 CORE_ADDR addr)
6955{
6956 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6957 aspace, addr)
6958 || (bl->length
6959 && breakpoint_address_match_range (bl->pspace->aspace,
6960 bl->address, bl->length,
6961 aspace, addr)));
6962}
6963
d35ae833
PA
6964/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6965 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6966 match happens only if ASPACE matches the breakpoint's address
6967 space. On targets that have global breakpoints, the address space
6968 doesn't really matter. */
6969
6970static int
6971breakpoint_location_address_range_overlap (struct bp_location *bl,
6972 struct address_space *aspace,
6973 CORE_ADDR addr, int len)
6974{
6975 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6976 || bl->pspace->aspace == aspace)
6977 {
6978 int bl_len = bl->length != 0 ? bl->length : 1;
6979
6980 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6981 return 1;
6982 }
6983 return 0;
6984}
6985
1e4d1764
YQ
6986/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6987 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6988 true, otherwise returns false. */
6989
6990static int
6991tracepoint_locations_match (struct bp_location *loc1,
6992 struct bp_location *loc2)
6993{
6994 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6995 /* Since tracepoint locations are never duplicated with others', tracepoint
6996 locations at the same address of different tracepoints are regarded as
6997 different locations. */
6998 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6999 else
7000 return 0;
7001}
7002
2d134ed3
PA
7003/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7004 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7005 represent the same location. */
7006
7007static int
4a64f543
MS
7008breakpoint_locations_match (struct bp_location *loc1,
7009 struct bp_location *loc2)
2d134ed3 7010{
2bdf28a0
JK
7011 int hw_point1, hw_point2;
7012
7013 /* Both of them must not be in moribund_locations. */
7014 gdb_assert (loc1->owner != NULL);
7015 gdb_assert (loc2->owner != NULL);
7016
7017 hw_point1 = is_hardware_watchpoint (loc1->owner);
7018 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7019
7020 if (hw_point1 != hw_point2)
7021 return 0;
7022 else if (hw_point1)
7023 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7024 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7025 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7026 else
f1310107
TJB
7027 /* We compare bp_location.length in order to cover ranged breakpoints. */
7028 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7029 loc2->pspace->aspace, loc2->address)
7030 && loc1->length == loc2->length);
2d134ed3
PA
7031}
7032
76897487
KB
7033static void
7034breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7035 int bnum, int have_bnum)
7036{
f63fbe86
MS
7037 /* The longest string possibly returned by hex_string_custom
7038 is 50 chars. These must be at least that big for safety. */
7039 char astr1[64];
7040 char astr2[64];
76897487 7041
bb599908
PH
7042 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7043 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7044 if (have_bnum)
8a3fe4f8 7045 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7046 bnum, astr1, astr2);
7047 else
8a3fe4f8 7048 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7049}
7050
4a64f543
MS
7051/* Adjust a breakpoint's address to account for architectural
7052 constraints on breakpoint placement. Return the adjusted address.
7053 Note: Very few targets require this kind of adjustment. For most
7054 targets, this function is simply the identity function. */
76897487
KB
7055
7056static CORE_ADDR
a6d9a66e
UW
7057adjust_breakpoint_address (struct gdbarch *gdbarch,
7058 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7059{
a6d9a66e 7060 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7061 {
7062 /* Very few targets need any kind of breakpoint adjustment. */
7063 return bpaddr;
7064 }
88f7da05
KB
7065 else if (bptype == bp_watchpoint
7066 || bptype == bp_hardware_watchpoint
7067 || bptype == bp_read_watchpoint
7068 || bptype == bp_access_watchpoint
fe798b75 7069 || bptype == bp_catchpoint)
88f7da05
KB
7070 {
7071 /* Watchpoints and the various bp_catch_* eventpoints should not
7072 have their addresses modified. */
7073 return bpaddr;
7074 }
7c16b83e
PA
7075 else if (bptype == bp_single_step)
7076 {
7077 /* Single-step breakpoints should not have their addresses
7078 modified. If there's any architectural constrain that
7079 applies to this address, then it should have already been
7080 taken into account when the breakpoint was created in the
7081 first place. If we didn't do this, stepping through e.g.,
7082 Thumb-2 IT blocks would break. */
7083 return bpaddr;
7084 }
76897487
KB
7085 else
7086 {
7087 CORE_ADDR adjusted_bpaddr;
7088
7089 /* Some targets have architectural constraints on the placement
7090 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7091 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7092
7093 /* An adjusted breakpoint address can significantly alter
7094 a user's expectations. Print a warning if an adjustment
7095 is required. */
7096 if (adjusted_bpaddr != bpaddr)
7097 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7098
7099 return adjusted_bpaddr;
7100 }
7101}
7102
5625a286 7103bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7104{
5625a286 7105 bp_location *loc = this;
7cc221ef 7106
348d480f
PA
7107 gdb_assert (ops != NULL);
7108
28010a5d
PA
7109 loc->ops = ops;
7110 loc->owner = owner;
b775012e 7111 loc->cond_bytecode = NULL;
0d381245
VP
7112 loc->shlib_disabled = 0;
7113 loc->enabled = 1;
e049a4b5 7114
28010a5d 7115 switch (owner->type)
e049a4b5
DJ
7116 {
7117 case bp_breakpoint:
7c16b83e 7118 case bp_single_step:
e049a4b5
DJ
7119 case bp_until:
7120 case bp_finish:
7121 case bp_longjmp:
7122 case bp_longjmp_resume:
e2e4d78b 7123 case bp_longjmp_call_dummy:
186c406b
TT
7124 case bp_exception:
7125 case bp_exception_resume:
e049a4b5 7126 case bp_step_resume:
2c03e5be 7127 case bp_hp_step_resume:
e049a4b5
DJ
7128 case bp_watchpoint_scope:
7129 case bp_call_dummy:
aa7d318d 7130 case bp_std_terminate:
e049a4b5
DJ
7131 case bp_shlib_event:
7132 case bp_thread_event:
7133 case bp_overlay_event:
4efc6507 7134 case bp_jit_event:
0fd8e87f 7135 case bp_longjmp_master:
aa7d318d 7136 case bp_std_terminate_master:
186c406b 7137 case bp_exception_master:
0e30163f
JK
7138 case bp_gnu_ifunc_resolver:
7139 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7140 case bp_dprintf:
e049a4b5 7141 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7142 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7143 break;
7144 case bp_hardware_breakpoint:
7145 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7146 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7147 break;
7148 case bp_hardware_watchpoint:
7149 case bp_read_watchpoint:
7150 case bp_access_watchpoint:
7151 loc->loc_type = bp_loc_hardware_watchpoint;
7152 break;
7153 case bp_watchpoint:
ce78b96d 7154 case bp_catchpoint:
15c3d785
PA
7155 case bp_tracepoint:
7156 case bp_fast_tracepoint:
0fb4aa4b 7157 case bp_static_tracepoint:
e049a4b5
DJ
7158 loc->loc_type = bp_loc_other;
7159 break;
7160 default:
e2e0b3e5 7161 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7162 }
7163
f431efe5 7164 loc->refc = 1;
28010a5d
PA
7165}
7166
7167/* Allocate a struct bp_location. */
7168
7169static struct bp_location *
7170allocate_bp_location (struct breakpoint *bpt)
7171{
348d480f
PA
7172 return bpt->ops->allocate_location (bpt);
7173}
7cc221ef 7174
f431efe5
PA
7175static void
7176free_bp_location (struct bp_location *loc)
fe3f5fa8 7177{
348d480f 7178 loc->ops->dtor (loc);
4d01a485 7179 delete loc;
fe3f5fa8
VP
7180}
7181
f431efe5
PA
7182/* Increment reference count. */
7183
7184static void
7185incref_bp_location (struct bp_location *bl)
7186{
7187 ++bl->refc;
7188}
7189
7190/* Decrement reference count. If the reference count reaches 0,
7191 destroy the bp_location. Sets *BLP to NULL. */
7192
7193static void
7194decref_bp_location (struct bp_location **blp)
7195{
0807b50c
PA
7196 gdb_assert ((*blp)->refc > 0);
7197
f431efe5
PA
7198 if (--(*blp)->refc == 0)
7199 free_bp_location (*blp);
7200 *blp = NULL;
7201}
7202
346774a9 7203/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7204
b270e6f9
TT
7205static breakpoint *
7206add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7207{
346774a9 7208 struct breakpoint *b1;
b270e6f9 7209 struct breakpoint *result = b.get ();
c906108c 7210
346774a9
PA
7211 /* Add this breakpoint to the end of the chain so that a list of
7212 breakpoints will come out in order of increasing numbers. */
7213
7214 b1 = breakpoint_chain;
7215 if (b1 == 0)
b270e6f9 7216 breakpoint_chain = b.release ();
346774a9
PA
7217 else
7218 {
7219 while (b1->next)
7220 b1 = b1->next;
b270e6f9 7221 b1->next = b.release ();
346774a9 7222 }
b270e6f9
TT
7223
7224 return result;
346774a9
PA
7225}
7226
7227/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7228
7229static void
7230init_raw_breakpoint_without_location (struct breakpoint *b,
7231 struct gdbarch *gdbarch,
28010a5d 7232 enum bptype bptype,
c0a91b2b 7233 const struct breakpoint_ops *ops)
346774a9 7234{
348d480f
PA
7235 gdb_assert (ops != NULL);
7236
28010a5d 7237 b->ops = ops;
4d28f7a8 7238 b->type = bptype;
a6d9a66e 7239 b->gdbarch = gdbarch;
c906108c
SS
7240 b->language = current_language->la_language;
7241 b->input_radix = input_radix;
d0fb5eae 7242 b->related_breakpoint = b;
346774a9
PA
7243}
7244
7245/* Helper to set_raw_breakpoint below. Creates a breakpoint
7246 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7247
7248static struct breakpoint *
7249set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7250 enum bptype bptype,
c0a91b2b 7251 const struct breakpoint_ops *ops)
346774a9 7252{
3b0871f4 7253 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7254
3b0871f4 7255 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7256 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7257}
7258
0e30163f
JK
7259/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7260 resolutions should be made as the user specified the location explicitly
7261 enough. */
7262
0d381245 7263static void
0e30163f 7264set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7265{
2bdf28a0
JK
7266 gdb_assert (loc->owner != NULL);
7267
0d381245 7268 if (loc->owner->type == bp_breakpoint
1042e4c0 7269 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7270 || is_tracepoint (loc->owner))
0d381245 7271 {
0e30163f 7272 int is_gnu_ifunc;
2c02bd72 7273 const char *function_name;
6a3a010b 7274 CORE_ADDR func_addr;
0e30163f 7275
2c02bd72 7276 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7277 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7278
7279 if (is_gnu_ifunc && !explicit_loc)
7280 {
7281 struct breakpoint *b = loc->owner;
7282
7283 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7284 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7285 &loc->requested_address))
7286 {
7287 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7288 loc->address = adjust_breakpoint_address (loc->gdbarch,
7289 loc->requested_address,
7290 b->type);
7291 }
7292 else if (b->type == bp_breakpoint && b->loc == loc
7293 && loc->next == NULL && b->related_breakpoint == b)
7294 {
7295 /* Create only the whole new breakpoint of this type but do not
7296 mess more complicated breakpoints with multiple locations. */
7297 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7298 /* Remember the resolver's address for use by the return
7299 breakpoint. */
7300 loc->related_address = func_addr;
0e30163f
JK
7301 }
7302 }
7303
2c02bd72
DE
7304 if (function_name)
7305 loc->function_name = xstrdup (function_name);
0d381245
VP
7306 }
7307}
7308
a6d9a66e 7309/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7310struct gdbarch *
a6d9a66e
UW
7311get_sal_arch (struct symtab_and_line sal)
7312{
7313 if (sal.section)
7314 return get_objfile_arch (sal.section->objfile);
7315 if (sal.symtab)
eb822aa6 7316 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7317
7318 return NULL;
7319}
7320
346774a9
PA
7321/* Low level routine for partially initializing a breakpoint of type
7322 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7323 file name, and line number are provided by SAL.
0d381245
VP
7324
7325 It is expected that the caller will complete the initialization of
7326 the newly created breakpoint struct as well as output any status
c56053d2 7327 information regarding the creation of a new breakpoint. */
0d381245 7328
346774a9
PA
7329static void
7330init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7331 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7332 const struct breakpoint_ops *ops)
0d381245 7333{
28010a5d 7334 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7335
3742cc8b 7336 add_location_to_breakpoint (b, &sal);
0d381245 7337
6c95b8df
PA
7338 if (bptype != bp_catchpoint)
7339 gdb_assert (sal.pspace != NULL);
7340
f8eba3c6
TT
7341 /* Store the program space that was used to set the breakpoint,
7342 except for ordinary breakpoints, which are independent of the
7343 program space. */
7344 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7345 b->pspace = sal.pspace;
346774a9 7346}
c906108c 7347
346774a9
PA
7348/* set_raw_breakpoint is a low level routine for allocating and
7349 partially initializing a breakpoint of type BPTYPE. The newly
7350 created breakpoint's address, section, source file name, and line
7351 number are provided by SAL. The newly created and partially
7352 initialized breakpoint is added to the breakpoint chain and
7353 is also returned as the value of this function.
7354
7355 It is expected that the caller will complete the initialization of
7356 the newly created breakpoint struct as well as output any status
7357 information regarding the creation of a new breakpoint. In
7358 particular, set_raw_breakpoint does NOT set the breakpoint
7359 number! Care should be taken to not allow an error to occur
7360 prior to completing the initialization of the breakpoint. If this
7361 should happen, a bogus breakpoint will be left on the chain. */
7362
7363struct breakpoint *
7364set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7365 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7366 const struct breakpoint_ops *ops)
346774a9 7367{
3b0871f4 7368 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7369
3b0871f4 7370 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7371 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7372}
7373
53a5351d 7374/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7375 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7376 initiated the operation. */
c906108c
SS
7377
7378void
186c406b 7379set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7380{
35df4500 7381 struct breakpoint *b, *b_tmp;
5d5658a1 7382 int thread = tp->global_num;
0fd8e87f
UW
7383
7384 /* To avoid having to rescan all objfile symbols at every step,
7385 we maintain a list of continually-inserted but always disabled
7386 longjmp "master" breakpoints. Here, we simply create momentary
7387 clones of those and enable them for the requested thread. */
35df4500 7388 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7389 if (b->pspace == current_program_space
186c406b
TT
7390 && (b->type == bp_longjmp_master
7391 || b->type == bp_exception_master))
0fd8e87f 7392 {
06edf0c0
PA
7393 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7394 struct breakpoint *clone;
cc59ec59 7395
e2e4d78b
JK
7396 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7397 after their removal. */
06edf0c0 7398 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7399 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7400 clone->thread = thread;
7401 }
186c406b
TT
7402
7403 tp->initiating_frame = frame;
c906108c
SS
7404}
7405
611c83ae 7406/* Delete all longjmp breakpoints from THREAD. */
c906108c 7407void
611c83ae 7408delete_longjmp_breakpoint (int thread)
c906108c 7409{
35df4500 7410 struct breakpoint *b, *b_tmp;
c906108c 7411
35df4500 7412 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7413 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7414 {
7415 if (b->thread == thread)
7416 delete_breakpoint (b);
7417 }
c906108c
SS
7418}
7419
f59f708a
PA
7420void
7421delete_longjmp_breakpoint_at_next_stop (int thread)
7422{
7423 struct breakpoint *b, *b_tmp;
7424
7425 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7426 if (b->type == bp_longjmp || b->type == bp_exception)
7427 {
7428 if (b->thread == thread)
7429 b->disposition = disp_del_at_next_stop;
7430 }
7431}
7432
e2e4d78b
JK
7433/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7434 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7435 pointer to any of them. Return NULL if this system cannot place longjmp
7436 breakpoints. */
7437
7438struct breakpoint *
7439set_longjmp_breakpoint_for_call_dummy (void)
7440{
7441 struct breakpoint *b, *retval = NULL;
7442
7443 ALL_BREAKPOINTS (b)
7444 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7445 {
7446 struct breakpoint *new_b;
7447
7448 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7449 &momentary_breakpoint_ops,
7450 1);
5d5658a1 7451 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7452
7453 /* Link NEW_B into the chain of RETVAL breakpoints. */
7454
7455 gdb_assert (new_b->related_breakpoint == new_b);
7456 if (retval == NULL)
7457 retval = new_b;
7458 new_b->related_breakpoint = retval;
7459 while (retval->related_breakpoint != new_b->related_breakpoint)
7460 retval = retval->related_breakpoint;
7461 retval->related_breakpoint = new_b;
7462 }
7463
7464 return retval;
7465}
7466
7467/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7468 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7469 stack.
7470
7471 You should call this function only at places where it is safe to currently
7472 unwind the whole stack. Failed stack unwind would discard live dummy
7473 frames. */
7474
7475void
b67a2c6f 7476check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7477{
7478 struct breakpoint *b, *b_tmp;
7479
7480 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7481 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7482 {
7483 struct breakpoint *dummy_b = b->related_breakpoint;
7484
7485 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7486 dummy_b = dummy_b->related_breakpoint;
7487 if (dummy_b->type != bp_call_dummy
7488 || frame_find_by_id (dummy_b->frame_id) != NULL)
7489 continue;
7490
b67a2c6f 7491 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7492
7493 while (b->related_breakpoint != b)
7494 {
7495 if (b_tmp == b->related_breakpoint)
7496 b_tmp = b->related_breakpoint->next;
7497 delete_breakpoint (b->related_breakpoint);
7498 }
7499 delete_breakpoint (b);
7500 }
7501}
7502
1900040c
MS
7503void
7504enable_overlay_breakpoints (void)
7505{
52f0bd74 7506 struct breakpoint *b;
1900040c
MS
7507
7508 ALL_BREAKPOINTS (b)
7509 if (b->type == bp_overlay_event)
7510 {
7511 b->enable_state = bp_enabled;
44702360 7512 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7513 overlay_events_enabled = 1;
1900040c
MS
7514 }
7515}
7516
7517void
7518disable_overlay_breakpoints (void)
7519{
52f0bd74 7520 struct breakpoint *b;
1900040c
MS
7521
7522 ALL_BREAKPOINTS (b)
7523 if (b->type == bp_overlay_event)
7524 {
7525 b->enable_state = bp_disabled;
44702360 7526 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7527 overlay_events_enabled = 0;
1900040c
MS
7528 }
7529}
7530
aa7d318d
TT
7531/* Set an active std::terminate breakpoint for each std::terminate
7532 master breakpoint. */
7533void
7534set_std_terminate_breakpoint (void)
7535{
35df4500 7536 struct breakpoint *b, *b_tmp;
aa7d318d 7537
35df4500 7538 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7539 if (b->pspace == current_program_space
7540 && b->type == bp_std_terminate_master)
7541 {
06edf0c0 7542 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7543 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7544 }
7545}
7546
7547/* Delete all the std::terminate breakpoints. */
7548void
7549delete_std_terminate_breakpoint (void)
7550{
35df4500 7551 struct breakpoint *b, *b_tmp;
aa7d318d 7552
35df4500 7553 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7554 if (b->type == bp_std_terminate)
7555 delete_breakpoint (b);
7556}
7557
c4093a6a 7558struct breakpoint *
a6d9a66e 7559create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7560{
7561 struct breakpoint *b;
c4093a6a 7562
06edf0c0
PA
7563 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7564 &internal_breakpoint_ops);
7565
b5de0fa7 7566 b->enable_state = bp_enabled;
f00aae0f 7567 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7568 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7569
44702360 7570 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7571
c4093a6a
JM
7572 return b;
7573}
7574
0101ce28
JJ
7575struct lang_and_radix
7576 {
7577 enum language lang;
7578 int radix;
7579 };
7580
4efc6507
DE
7581/* Create a breakpoint for JIT code registration and unregistration. */
7582
7583struct breakpoint *
7584create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7585{
2a7f3dff
PA
7586 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7587 &internal_breakpoint_ops);
4efc6507 7588}
0101ce28 7589
03673fc7
PP
7590/* Remove JIT code registration and unregistration breakpoint(s). */
7591
7592void
7593remove_jit_event_breakpoints (void)
7594{
7595 struct breakpoint *b, *b_tmp;
7596
7597 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7598 if (b->type == bp_jit_event
7599 && b->loc->pspace == current_program_space)
7600 delete_breakpoint (b);
7601}
7602
cae688ec
JJ
7603void
7604remove_solib_event_breakpoints (void)
7605{
35df4500 7606 struct breakpoint *b, *b_tmp;
cae688ec 7607
35df4500 7608 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7609 if (b->type == bp_shlib_event
7610 && b->loc->pspace == current_program_space)
cae688ec
JJ
7611 delete_breakpoint (b);
7612}
7613
f37f681c
PA
7614/* See breakpoint.h. */
7615
7616void
7617remove_solib_event_breakpoints_at_next_stop (void)
7618{
7619 struct breakpoint *b, *b_tmp;
7620
7621 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7622 if (b->type == bp_shlib_event
7623 && b->loc->pspace == current_program_space)
7624 b->disposition = disp_del_at_next_stop;
7625}
7626
04086b45
PA
7627/* Helper for create_solib_event_breakpoint /
7628 create_and_insert_solib_event_breakpoint. Allows specifying which
7629 INSERT_MODE to pass through to update_global_location_list. */
7630
7631static struct breakpoint *
7632create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7633 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7634{
7635 struct breakpoint *b;
7636
06edf0c0
PA
7637 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7638 &internal_breakpoint_ops);
04086b45 7639 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7640 return b;
7641}
7642
04086b45
PA
7643struct breakpoint *
7644create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7645{
7646 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7647}
7648
f37f681c
PA
7649/* See breakpoint.h. */
7650
7651struct breakpoint *
7652create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7653{
7654 struct breakpoint *b;
7655
04086b45
PA
7656 /* Explicitly tell update_global_location_list to insert
7657 locations. */
7658 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7659 if (!b->loc->inserted)
7660 {
7661 delete_breakpoint (b);
7662 return NULL;
7663 }
7664 return b;
7665}
7666
cae688ec
JJ
7667/* Disable any breakpoints that are on code in shared libraries. Only
7668 apply to enabled breakpoints, disabled ones can just stay disabled. */
7669
7670void
cb851954 7671disable_breakpoints_in_shlibs (void)
cae688ec 7672{
876fa593 7673 struct bp_location *loc, **locp_tmp;
cae688ec 7674
876fa593 7675 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7676 {
2bdf28a0 7677 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7678 struct breakpoint *b = loc->owner;
2bdf28a0 7679
4a64f543
MS
7680 /* We apply the check to all breakpoints, including disabled for
7681 those with loc->duplicate set. This is so that when breakpoint
7682 becomes enabled, or the duplicate is removed, gdb will try to
7683 insert all breakpoints. If we don't set shlib_disabled here,
7684 we'll try to insert those breakpoints and fail. */
1042e4c0 7685 if (((b->type == bp_breakpoint)
508ccb1f 7686 || (b->type == bp_jit_event)
1042e4c0 7687 || (b->type == bp_hardware_breakpoint)
d77f58be 7688 || (is_tracepoint (b)))
6c95b8df 7689 && loc->pspace == current_program_space
0d381245 7690 && !loc->shlib_disabled
6c95b8df 7691 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7692 )
0d381245
VP
7693 {
7694 loc->shlib_disabled = 1;
7695 }
cae688ec
JJ
7696 }
7697}
7698
63644780
NB
7699/* Disable any breakpoints and tracepoints that are in SOLIB upon
7700 notification of unloaded_shlib. Only apply to enabled breakpoints,
7701 disabled ones can just stay disabled. */
84acb35a 7702
75149521 7703static void
84acb35a
JJ
7704disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7705{
876fa593 7706 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7707 int disabled_shlib_breaks = 0;
7708
876fa593 7709 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7710 {
2bdf28a0 7711 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7712 struct breakpoint *b = loc->owner;
cc59ec59 7713
1e4d1764 7714 if (solib->pspace == loc->pspace
e2dd7057 7715 && !loc->shlib_disabled
1e4d1764
YQ
7716 && (((b->type == bp_breakpoint
7717 || b->type == bp_jit_event
7718 || b->type == bp_hardware_breakpoint)
7719 && (loc->loc_type == bp_loc_hardware_breakpoint
7720 || loc->loc_type == bp_loc_software_breakpoint))
7721 || is_tracepoint (b))
e2dd7057 7722 && solib_contains_address_p (solib, loc->address))
84acb35a 7723 {
e2dd7057
PP
7724 loc->shlib_disabled = 1;
7725 /* At this point, we cannot rely on remove_breakpoint
7726 succeeding so we must mark the breakpoint as not inserted
7727 to prevent future errors occurring in remove_breakpoints. */
7728 loc->inserted = 0;
8d3788bd
VP
7729
7730 /* This may cause duplicate notifications for the same breakpoint. */
7731 observer_notify_breakpoint_modified (b);
7732
e2dd7057
PP
7733 if (!disabled_shlib_breaks)
7734 {
223ffa71 7735 target_terminal::ours_for_output ();
3e43a32a
MS
7736 warning (_("Temporarily disabling breakpoints "
7737 "for unloaded shared library \"%s\""),
e2dd7057 7738 solib->so_name);
84acb35a 7739 }
e2dd7057 7740 disabled_shlib_breaks = 1;
84acb35a
JJ
7741 }
7742 }
84acb35a
JJ
7743}
7744
63644780
NB
7745/* Disable any breakpoints and tracepoints in OBJFILE upon
7746 notification of free_objfile. Only apply to enabled breakpoints,
7747 disabled ones can just stay disabled. */
7748
7749static void
7750disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7751{
7752 struct breakpoint *b;
7753
7754 if (objfile == NULL)
7755 return;
7756
d03de421
PA
7757 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7758 managed by the user with add-symbol-file/remove-symbol-file.
7759 Similarly to how breakpoints in shared libraries are handled in
7760 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7761 shlib_disabled so they end up uninserted on the next global
7762 location list update. Shared libraries not loaded by the user
7763 aren't handled here -- they're already handled in
7764 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7765 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7766 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7767 main objfile). */
7768 if ((objfile->flags & OBJF_SHARED) == 0
7769 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7770 return;
7771
7772 ALL_BREAKPOINTS (b)
7773 {
7774 struct bp_location *loc;
7775 int bp_modified = 0;
7776
7777 if (!is_breakpoint (b) && !is_tracepoint (b))
7778 continue;
7779
7780 for (loc = b->loc; loc != NULL; loc = loc->next)
7781 {
7782 CORE_ADDR loc_addr = loc->address;
7783
7784 if (loc->loc_type != bp_loc_hardware_breakpoint
7785 && loc->loc_type != bp_loc_software_breakpoint)
7786 continue;
7787
7788 if (loc->shlib_disabled != 0)
7789 continue;
7790
7791 if (objfile->pspace != loc->pspace)
7792 continue;
7793
7794 if (loc->loc_type != bp_loc_hardware_breakpoint
7795 && loc->loc_type != bp_loc_software_breakpoint)
7796 continue;
7797
7798 if (is_addr_in_objfile (loc_addr, objfile))
7799 {
7800 loc->shlib_disabled = 1;
08351840
PA
7801 /* At this point, we don't know whether the object was
7802 unmapped from the inferior or not, so leave the
7803 inserted flag alone. We'll handle failure to
7804 uninsert quietly, in case the object was indeed
7805 unmapped. */
63644780
NB
7806
7807 mark_breakpoint_location_modified (loc);
7808
7809 bp_modified = 1;
7810 }
7811 }
7812
7813 if (bp_modified)
7814 observer_notify_breakpoint_modified (b);
7815 }
7816}
7817
ce78b96d
JB
7818/* FORK & VFORK catchpoints. */
7819
e29a4733 7820/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7821 catchpoint. A breakpoint is really of this type iff its ops pointer points
7822 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7823
c1fc2657 7824struct fork_catchpoint : public breakpoint
e29a4733 7825{
e29a4733
PA
7826 /* Process id of a child process whose forking triggered this
7827 catchpoint. This field is only valid immediately after this
7828 catchpoint has triggered. */
7829 ptid_t forked_inferior_pid;
7830};
7831
4a64f543
MS
7832/* Implement the "insert" breakpoint_ops method for fork
7833 catchpoints. */
ce78b96d 7834
77b06cd7
TJB
7835static int
7836insert_catch_fork (struct bp_location *bl)
ce78b96d 7837{
dfd4cc63 7838 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7839}
7840
4a64f543
MS
7841/* Implement the "remove" breakpoint_ops method for fork
7842 catchpoints. */
ce78b96d
JB
7843
7844static int
73971819 7845remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7846{
dfd4cc63 7847 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7848}
7849
7850/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7851 catchpoints. */
7852
7853static int
f1310107 7854breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7855 struct address_space *aspace, CORE_ADDR bp_addr,
7856 const struct target_waitstatus *ws)
ce78b96d 7857{
e29a4733
PA
7858 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7859
f90263c1
TT
7860 if (ws->kind != TARGET_WAITKIND_FORKED)
7861 return 0;
7862
7863 c->forked_inferior_pid = ws->value.related_pid;
7864 return 1;
ce78b96d
JB
7865}
7866
4a64f543
MS
7867/* Implement the "print_it" breakpoint_ops method for fork
7868 catchpoints. */
ce78b96d
JB
7869
7870static enum print_stop_action
348d480f 7871print_it_catch_fork (bpstat bs)
ce78b96d 7872{
36dfb11c 7873 struct ui_out *uiout = current_uiout;
348d480f
PA
7874 struct breakpoint *b = bs->breakpoint_at;
7875 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7876
ce78b96d 7877 annotate_catchpoint (b->number);
f303dbd6 7878 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7879 if (b->disposition == disp_del)
112e8700 7880 uiout->text ("Temporary catchpoint ");
36dfb11c 7881 else
112e8700
SM
7882 uiout->text ("Catchpoint ");
7883 if (uiout->is_mi_like_p ())
36dfb11c 7884 {
112e8700
SM
7885 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7886 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7887 }
112e8700
SM
7888 uiout->field_int ("bkptno", b->number);
7889 uiout->text (" (forked process ");
7890 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7891 uiout->text ("), ");
ce78b96d
JB
7892 return PRINT_SRC_AND_LOC;
7893}
7894
4a64f543
MS
7895/* Implement the "print_one" breakpoint_ops method for fork
7896 catchpoints. */
ce78b96d
JB
7897
7898static void
a6d9a66e 7899print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7900{
e29a4733 7901 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7902 struct value_print_options opts;
79a45e25 7903 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7904
7905 get_user_print_options (&opts);
7906
4a64f543
MS
7907 /* Field 4, the address, is omitted (which makes the columns not
7908 line up too nicely with the headers, but the effect is relatively
7909 readable). */
79a45b7d 7910 if (opts.addressprint)
112e8700 7911 uiout->field_skip ("addr");
ce78b96d 7912 annotate_field (5);
112e8700 7913 uiout->text ("fork");
e29a4733 7914 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7915 {
112e8700
SM
7916 uiout->text (", process ");
7917 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7918 uiout->spaces (1);
ce78b96d 7919 }
8ac3646f 7920
112e8700
SM
7921 if (uiout->is_mi_like_p ())
7922 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7923}
7924
7925/* Implement the "print_mention" breakpoint_ops method for fork
7926 catchpoints. */
7927
7928static void
7929print_mention_catch_fork (struct breakpoint *b)
7930{
7931 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7932}
7933
6149aea9
PA
7934/* Implement the "print_recreate" breakpoint_ops method for fork
7935 catchpoints. */
7936
7937static void
7938print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7939{
7940 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7941 print_recreate_thread (b, fp);
6149aea9
PA
7942}
7943
ce78b96d
JB
7944/* The breakpoint_ops structure to be used in fork catchpoints. */
7945
2060206e 7946static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7947
4a64f543
MS
7948/* Implement the "insert" breakpoint_ops method for vfork
7949 catchpoints. */
ce78b96d 7950
77b06cd7
TJB
7951static int
7952insert_catch_vfork (struct bp_location *bl)
ce78b96d 7953{
dfd4cc63 7954 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7955}
7956
4a64f543
MS
7957/* Implement the "remove" breakpoint_ops method for vfork
7958 catchpoints. */
ce78b96d
JB
7959
7960static int
73971819 7961remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7962{
dfd4cc63 7963 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7964}
7965
7966/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7967 catchpoints. */
7968
7969static int
f1310107 7970breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7971 struct address_space *aspace, CORE_ADDR bp_addr,
7972 const struct target_waitstatus *ws)
ce78b96d 7973{
e29a4733
PA
7974 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7975
f90263c1
TT
7976 if (ws->kind != TARGET_WAITKIND_VFORKED)
7977 return 0;
7978
7979 c->forked_inferior_pid = ws->value.related_pid;
7980 return 1;
ce78b96d
JB
7981}
7982
4a64f543
MS
7983/* Implement the "print_it" breakpoint_ops method for vfork
7984 catchpoints. */
ce78b96d
JB
7985
7986static enum print_stop_action
348d480f 7987print_it_catch_vfork (bpstat bs)
ce78b96d 7988{
36dfb11c 7989 struct ui_out *uiout = current_uiout;
348d480f 7990 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7991 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7992
ce78b96d 7993 annotate_catchpoint (b->number);
f303dbd6 7994 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7995 if (b->disposition == disp_del)
112e8700 7996 uiout->text ("Temporary catchpoint ");
36dfb11c 7997 else
112e8700
SM
7998 uiout->text ("Catchpoint ");
7999 if (uiout->is_mi_like_p ())
36dfb11c 8000 {
112e8700
SM
8001 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8002 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8003 }
112e8700
SM
8004 uiout->field_int ("bkptno", b->number);
8005 uiout->text (" (vforked process ");
8006 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8007 uiout->text ("), ");
ce78b96d
JB
8008 return PRINT_SRC_AND_LOC;
8009}
8010
4a64f543
MS
8011/* Implement the "print_one" breakpoint_ops method for vfork
8012 catchpoints. */
ce78b96d
JB
8013
8014static void
a6d9a66e 8015print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8016{
e29a4733 8017 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8018 struct value_print_options opts;
79a45e25 8019 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8020
8021 get_user_print_options (&opts);
4a64f543
MS
8022 /* Field 4, the address, is omitted (which makes the columns not
8023 line up too nicely with the headers, but the effect is relatively
8024 readable). */
79a45b7d 8025 if (opts.addressprint)
112e8700 8026 uiout->field_skip ("addr");
ce78b96d 8027 annotate_field (5);
112e8700 8028 uiout->text ("vfork");
e29a4733 8029 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8030 {
112e8700
SM
8031 uiout->text (", process ");
8032 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8033 uiout->spaces (1);
ce78b96d 8034 }
8ac3646f 8035
112e8700
SM
8036 if (uiout->is_mi_like_p ())
8037 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8038}
8039
8040/* Implement the "print_mention" breakpoint_ops method for vfork
8041 catchpoints. */
8042
8043static void
8044print_mention_catch_vfork (struct breakpoint *b)
8045{
8046 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8047}
8048
6149aea9
PA
8049/* Implement the "print_recreate" breakpoint_ops method for vfork
8050 catchpoints. */
8051
8052static void
8053print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8054{
8055 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8056 print_recreate_thread (b, fp);
6149aea9
PA
8057}
8058
ce78b96d
JB
8059/* The breakpoint_ops structure to be used in vfork catchpoints. */
8060
2060206e 8061static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8062
edcc5120 8063/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8064 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8065 CATCH_SOLIB_BREAKPOINT_OPS. */
8066
c1fc2657 8067struct solib_catchpoint : public breakpoint
edcc5120 8068{
c1fc2657 8069 ~solib_catchpoint () override;
edcc5120
TT
8070
8071 /* True for "catch load", false for "catch unload". */
8072 unsigned char is_load;
8073
8074 /* Regular expression to match, if any. COMPILED is only valid when
8075 REGEX is non-NULL. */
8076 char *regex;
2d7cc5c7 8077 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8078};
8079
c1fc2657 8080solib_catchpoint::~solib_catchpoint ()
edcc5120 8081{
c1fc2657 8082 xfree (this->regex);
edcc5120
TT
8083}
8084
8085static int
8086insert_catch_solib (struct bp_location *ignore)
8087{
8088 return 0;
8089}
8090
8091static int
73971819 8092remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8093{
8094 return 0;
8095}
8096
8097static int
8098breakpoint_hit_catch_solib (const struct bp_location *bl,
8099 struct address_space *aspace,
8100 CORE_ADDR bp_addr,
8101 const struct target_waitstatus *ws)
8102{
8103 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8104 struct breakpoint *other;
8105
8106 if (ws->kind == TARGET_WAITKIND_LOADED)
8107 return 1;
8108
8109 ALL_BREAKPOINTS (other)
8110 {
8111 struct bp_location *other_bl;
8112
8113 if (other == bl->owner)
8114 continue;
8115
8116 if (other->type != bp_shlib_event)
8117 continue;
8118
c1fc2657 8119 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8120 continue;
8121
8122 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8123 {
8124 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8125 return 1;
8126 }
8127 }
8128
8129 return 0;
8130}
8131
8132static void
8133check_status_catch_solib (struct bpstats *bs)
8134{
8135 struct solib_catchpoint *self
8136 = (struct solib_catchpoint *) bs->breakpoint_at;
8137 int ix;
8138
8139 if (self->is_load)
8140 {
8141 struct so_list *iter;
8142
8143 for (ix = 0;
8144 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8145 ix, iter);
8146 ++ix)
8147 {
8148 if (!self->regex
2d7cc5c7 8149 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8150 return;
8151 }
8152 }
8153 else
8154 {
8155 char *iter;
8156
8157 for (ix = 0;
8158 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8159 ix, iter);
8160 ++ix)
8161 {
8162 if (!self->regex
2d7cc5c7 8163 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8164 return;
8165 }
8166 }
8167
8168 bs->stop = 0;
8169 bs->print_it = print_it_noop;
8170}
8171
8172static enum print_stop_action
8173print_it_catch_solib (bpstat bs)
8174{
8175 struct breakpoint *b = bs->breakpoint_at;
8176 struct ui_out *uiout = current_uiout;
8177
8178 annotate_catchpoint (b->number);
f303dbd6 8179 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8180 if (b->disposition == disp_del)
112e8700 8181 uiout->text ("Temporary catchpoint ");
edcc5120 8182 else
112e8700
SM
8183 uiout->text ("Catchpoint ");
8184 uiout->field_int ("bkptno", b->number);
8185 uiout->text ("\n");
8186 if (uiout->is_mi_like_p ())
8187 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8188 print_solib_event (1);
8189 return PRINT_SRC_AND_LOC;
8190}
8191
8192static void
8193print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8194{
8195 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8196 struct value_print_options opts;
8197 struct ui_out *uiout = current_uiout;
8198 char *msg;
8199
8200 get_user_print_options (&opts);
8201 /* Field 4, the address, is omitted (which makes the columns not
8202 line up too nicely with the headers, but the effect is relatively
8203 readable). */
8204 if (opts.addressprint)
8205 {
8206 annotate_field (4);
112e8700 8207 uiout->field_skip ("addr");
edcc5120
TT
8208 }
8209
8210 annotate_field (5);
8211 if (self->is_load)
8212 {
8213 if (self->regex)
8214 msg = xstrprintf (_("load of library matching %s"), self->regex);
8215 else
8216 msg = xstrdup (_("load of library"));
8217 }
8218 else
8219 {
8220 if (self->regex)
8221 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8222 else
8223 msg = xstrdup (_("unload of library"));
8224 }
112e8700 8225 uiout->field_string ("what", msg);
edcc5120 8226 xfree (msg);
8ac3646f 8227
112e8700
SM
8228 if (uiout->is_mi_like_p ())
8229 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8230}
8231
8232static void
8233print_mention_catch_solib (struct breakpoint *b)
8234{
8235 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8236
8237 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8238 self->is_load ? "load" : "unload");
8239}
8240
8241static void
8242print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8243{
8244 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8245
8246 fprintf_unfiltered (fp, "%s %s",
8247 b->disposition == disp_del ? "tcatch" : "catch",
8248 self->is_load ? "load" : "unload");
8249 if (self->regex)
8250 fprintf_unfiltered (fp, " %s", self->regex);
8251 fprintf_unfiltered (fp, "\n");
8252}
8253
8254static struct breakpoint_ops catch_solib_breakpoint_ops;
8255
91985142
MG
8256/* Shared helper function (MI and CLI) for creating and installing
8257 a shared object event catchpoint. If IS_LOAD is non-zero then
8258 the events to be caught are load events, otherwise they are
8259 unload events. If IS_TEMP is non-zero the catchpoint is a
8260 temporary one. If ENABLED is non-zero the catchpoint is
8261 created in an enabled state. */
edcc5120 8262
91985142 8263void
a121b7c1 8264add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8265{
edcc5120 8266 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8267
edcc5120
TT
8268 if (!arg)
8269 arg = "";
f1735a53 8270 arg = skip_spaces (arg);
edcc5120 8271
36bd8eaa 8272 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8273
8274 if (*arg != '\0')
8275 {
2d7cc5c7
PA
8276 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8277 _("Invalid regexp")));
edcc5120
TT
8278 c->regex = xstrdup (arg);
8279 }
8280
8281 c->is_load = is_load;
36bd8eaa 8282 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8283 &catch_solib_breakpoint_ops);
8284
c1fc2657 8285 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8286
b270e6f9 8287 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8288}
8289
91985142
MG
8290/* A helper function that does all the work for "catch load" and
8291 "catch unload". */
8292
8293static void
8294catch_load_or_unload (char *arg, int from_tty, int is_load,
8295 struct cmd_list_element *command)
8296{
8297 int tempflag;
8298 const int enabled = 1;
8299
8300 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8301
8302 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8303}
8304
edcc5120
TT
8305static void
8306catch_load_command_1 (char *arg, int from_tty,
8307 struct cmd_list_element *command)
8308{
8309 catch_load_or_unload (arg, from_tty, 1, command);
8310}
8311
8312static void
8313catch_unload_command_1 (char *arg, int from_tty,
8314 struct cmd_list_element *command)
8315{
8316 catch_load_or_unload (arg, from_tty, 0, command);
8317}
8318
346774a9
PA
8319/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8320 is non-zero, then make the breakpoint temporary. If COND_STRING is
8321 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8322 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8323
ab04a2af 8324void
346774a9
PA
8325init_catchpoint (struct breakpoint *b,
8326 struct gdbarch *gdbarch, int tempflag,
63160a43 8327 const char *cond_string,
c0a91b2b 8328 const struct breakpoint_ops *ops)
c906108c 8329{
51abb421 8330 symtab_and_line sal;
6c95b8df 8331 sal.pspace = current_program_space;
c5aa993b 8332
28010a5d 8333 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8334
1b36a34b 8335 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8336 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8337}
8338
28010a5d 8339void
b270e6f9 8340install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8341{
b270e6f9 8342 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8343 set_breakpoint_number (internal, b);
558a9d82
YQ
8344 if (is_tracepoint (b))
8345 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8346 if (!internal)
8347 mention (b);
c56053d2 8348 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8349
8350 if (update_gll)
44702360 8351 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8352}
8353
9b70b993 8354static void
a6d9a66e 8355create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8356 int tempflag, const char *cond_string,
c0a91b2b 8357 const struct breakpoint_ops *ops)
c906108c 8358{
b270e6f9 8359 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8360
b270e6f9 8361 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8362
8363 c->forked_inferior_pid = null_ptid;
8364
b270e6f9 8365 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8366}
8367
fe798b75
JB
8368/* Exec catchpoints. */
8369
b4d90040 8370/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8371 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8372 CATCH_EXEC_BREAKPOINT_OPS. */
8373
c1fc2657 8374struct exec_catchpoint : public breakpoint
b4d90040 8375{
c1fc2657 8376 ~exec_catchpoint () override;
b4d90040
PA
8377
8378 /* Filename of a program whose exec triggered this catchpoint.
8379 This field is only valid immediately after this catchpoint has
8380 triggered. */
8381 char *exec_pathname;
8382};
8383
c1fc2657 8384/* Exec catchpoint destructor. */
b4d90040 8385
c1fc2657 8386exec_catchpoint::~exec_catchpoint ()
b4d90040 8387{
c1fc2657 8388 xfree (this->exec_pathname);
b4d90040
PA
8389}
8390
77b06cd7
TJB
8391static int
8392insert_catch_exec (struct bp_location *bl)
c906108c 8393{
dfd4cc63 8394 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8395}
c906108c 8396
fe798b75 8397static int
73971819 8398remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8399{
dfd4cc63 8400 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8401}
c906108c 8402
fe798b75 8403static int
f1310107 8404breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8405 struct address_space *aspace, CORE_ADDR bp_addr,
8406 const struct target_waitstatus *ws)
fe798b75 8407{
b4d90040
PA
8408 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8409
f90263c1
TT
8410 if (ws->kind != TARGET_WAITKIND_EXECD)
8411 return 0;
8412
8413 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8414 return 1;
fe798b75 8415}
c906108c 8416
fe798b75 8417static enum print_stop_action
348d480f 8418print_it_catch_exec (bpstat bs)
fe798b75 8419{
36dfb11c 8420 struct ui_out *uiout = current_uiout;
348d480f 8421 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8422 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8423
fe798b75 8424 annotate_catchpoint (b->number);
f303dbd6 8425 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8426 if (b->disposition == disp_del)
112e8700 8427 uiout->text ("Temporary catchpoint ");
36dfb11c 8428 else
112e8700
SM
8429 uiout->text ("Catchpoint ");
8430 if (uiout->is_mi_like_p ())
36dfb11c 8431 {
112e8700
SM
8432 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8433 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8434 }
112e8700
SM
8435 uiout->field_int ("bkptno", b->number);
8436 uiout->text (" (exec'd ");
8437 uiout->field_string ("new-exec", c->exec_pathname);
8438 uiout->text ("), ");
36dfb11c 8439
fe798b75 8440 return PRINT_SRC_AND_LOC;
c906108c
SS
8441}
8442
fe798b75 8443static void
a6d9a66e 8444print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8445{
b4d90040 8446 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8447 struct value_print_options opts;
79a45e25 8448 struct ui_out *uiout = current_uiout;
fe798b75
JB
8449
8450 get_user_print_options (&opts);
8451
8452 /* Field 4, the address, is omitted (which makes the columns
8453 not line up too nicely with the headers, but the effect
8454 is relatively readable). */
8455 if (opts.addressprint)
112e8700 8456 uiout->field_skip ("addr");
fe798b75 8457 annotate_field (5);
112e8700 8458 uiout->text ("exec");
b4d90040 8459 if (c->exec_pathname != NULL)
fe798b75 8460 {
112e8700
SM
8461 uiout->text (", program \"");
8462 uiout->field_string ("what", c->exec_pathname);
8463 uiout->text ("\" ");
fe798b75 8464 }
8ac3646f 8465
112e8700
SM
8466 if (uiout->is_mi_like_p ())
8467 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8468}
8469
8470static void
8471print_mention_catch_exec (struct breakpoint *b)
8472{
8473 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8474}
8475
6149aea9
PA
8476/* Implement the "print_recreate" breakpoint_ops method for exec
8477 catchpoints. */
8478
8479static void
8480print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8481{
8482 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8483 print_recreate_thread (b, fp);
6149aea9
PA
8484}
8485
2060206e 8486static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8487
c906108c 8488static int
fba45db2 8489hw_breakpoint_used_count (void)
c906108c 8490{
c906108c 8491 int i = 0;
f1310107
TJB
8492 struct breakpoint *b;
8493 struct bp_location *bl;
c906108c
SS
8494
8495 ALL_BREAKPOINTS (b)
c5aa993b 8496 {
d6b74ac4 8497 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8498 for (bl = b->loc; bl; bl = bl->next)
8499 {
8500 /* Special types of hardware breakpoints may use more than
8501 one register. */
348d480f 8502 i += b->ops->resources_needed (bl);
f1310107 8503 }
c5aa993b 8504 }
c906108c
SS
8505
8506 return i;
8507}
8508
a1398e0c
PA
8509/* Returns the resources B would use if it were a hardware
8510 watchpoint. */
8511
c906108c 8512static int
a1398e0c 8513hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8514{
c906108c 8515 int i = 0;
e09342b5 8516 struct bp_location *bl;
c906108c 8517
a1398e0c
PA
8518 if (!breakpoint_enabled (b))
8519 return 0;
8520
8521 for (bl = b->loc; bl; bl = bl->next)
8522 {
8523 /* Special types of hardware watchpoints may use more than
8524 one register. */
8525 i += b->ops->resources_needed (bl);
8526 }
8527
8528 return i;
8529}
8530
8531/* Returns the sum the used resources of all hardware watchpoints of
8532 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8533 the sum of the used resources of all hardware watchpoints of other
8534 types _not_ TYPE. */
8535
8536static int
8537hw_watchpoint_used_count_others (struct breakpoint *except,
8538 enum bptype type, int *other_type_used)
8539{
8540 int i = 0;
8541 struct breakpoint *b;
8542
c906108c
SS
8543 *other_type_used = 0;
8544 ALL_BREAKPOINTS (b)
e09342b5 8545 {
a1398e0c
PA
8546 if (b == except)
8547 continue;
e09342b5
TJB
8548 if (!breakpoint_enabled (b))
8549 continue;
8550
a1398e0c
PA
8551 if (b->type == type)
8552 i += hw_watchpoint_use_count (b);
8553 else if (is_hardware_watchpoint (b))
8554 *other_type_used = 1;
e09342b5
TJB
8555 }
8556
c906108c
SS
8557 return i;
8558}
8559
c906108c 8560void
fba45db2 8561disable_watchpoints_before_interactive_call_start (void)
c906108c 8562{
c5aa993b 8563 struct breakpoint *b;
c906108c
SS
8564
8565 ALL_BREAKPOINTS (b)
c5aa993b 8566 {
cc60f2e3 8567 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8568 {
b5de0fa7 8569 b->enable_state = bp_call_disabled;
44702360 8570 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8571 }
8572 }
c906108c
SS
8573}
8574
8575void
fba45db2 8576enable_watchpoints_after_interactive_call_stop (void)
c906108c 8577{
c5aa993b 8578 struct breakpoint *b;
c906108c
SS
8579
8580 ALL_BREAKPOINTS (b)
c5aa993b 8581 {
cc60f2e3 8582 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8583 {
b5de0fa7 8584 b->enable_state = bp_enabled;
44702360 8585 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8586 }
8587 }
c906108c
SS
8588}
8589
8bea4e01
UW
8590void
8591disable_breakpoints_before_startup (void)
8592{
6c95b8df 8593 current_program_space->executing_startup = 1;
44702360 8594 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8595}
8596
8597void
8598enable_breakpoints_after_startup (void)
8599{
6c95b8df 8600 current_program_space->executing_startup = 0;
f8eba3c6 8601 breakpoint_re_set ();
8bea4e01
UW
8602}
8603
7c16b83e
PA
8604/* Create a new single-step breakpoint for thread THREAD, with no
8605 locations. */
c906108c 8606
7c16b83e
PA
8607static struct breakpoint *
8608new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8609{
b270e6f9 8610 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8611
b270e6f9 8612 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8613 &momentary_breakpoint_ops);
8614
8615 b->disposition = disp_donttouch;
8616 b->frame_id = null_frame_id;
8617
8618 b->thread = thread;
8619 gdb_assert (b->thread != 0);
8620
b270e6f9 8621 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8622}
8623
8624/* Set a momentary breakpoint of type TYPE at address specified by
8625 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8626 frame. */
c906108c
SS
8627
8628struct breakpoint *
a6d9a66e
UW
8629set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8630 struct frame_id frame_id, enum bptype type)
c906108c 8631{
52f0bd74 8632 struct breakpoint *b;
edb3359d 8633
193facb3
JK
8634 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8635 tail-called one. */
8636 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8637
06edf0c0 8638 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8639 b->enable_state = bp_enabled;
8640 b->disposition = disp_donttouch;
818dd999 8641 b->frame_id = frame_id;
c906108c 8642
4a64f543
MS
8643 /* If we're debugging a multi-threaded program, then we want
8644 momentary breakpoints to be active in only a single thread of
8645 control. */
39f77062 8646 if (in_thread_list (inferior_ptid))
5d5658a1 8647 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8648
44702360 8649 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8650
c906108c
SS
8651 return b;
8652}
611c83ae 8653
06edf0c0 8654/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8655 The new breakpoint will have type TYPE, use OPS as its
8656 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8657
06edf0c0
PA
8658static struct breakpoint *
8659momentary_breakpoint_from_master (struct breakpoint *orig,
8660 enum bptype type,
a1aa2221
LM
8661 const struct breakpoint_ops *ops,
8662 int loc_enabled)
e58b0e63
PA
8663{
8664 struct breakpoint *copy;
8665
06edf0c0 8666 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8667 copy->loc = allocate_bp_location (copy);
0e30163f 8668 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8669
a6d9a66e 8670 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8671 copy->loc->requested_address = orig->loc->requested_address;
8672 copy->loc->address = orig->loc->address;
8673 copy->loc->section = orig->loc->section;
6c95b8df 8674 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8675 copy->loc->probe = orig->loc->probe;
f8eba3c6 8676 copy->loc->line_number = orig->loc->line_number;
2f202fde 8677 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8678 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8679 copy->frame_id = orig->frame_id;
8680 copy->thread = orig->thread;
6c95b8df 8681 copy->pspace = orig->pspace;
e58b0e63
PA
8682
8683 copy->enable_state = bp_enabled;
8684 copy->disposition = disp_donttouch;
8685 copy->number = internal_breakpoint_number--;
8686
44702360 8687 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8688 return copy;
8689}
8690
06edf0c0
PA
8691/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8692 ORIG is NULL. */
8693
8694struct breakpoint *
8695clone_momentary_breakpoint (struct breakpoint *orig)
8696{
8697 /* If there's nothing to clone, then return nothing. */
8698 if (orig == NULL)
8699 return NULL;
8700
a1aa2221 8701 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8702}
8703
611c83ae 8704struct breakpoint *
a6d9a66e
UW
8705set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8706 enum bptype type)
611c83ae
PA
8707{
8708 struct symtab_and_line sal;
8709
8710 sal = find_pc_line (pc, 0);
8711 sal.pc = pc;
8712 sal.section = find_pc_overlay (pc);
8713 sal.explicit_pc = 1;
8714
a6d9a66e 8715 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8716}
c906108c 8717\f
c5aa993b 8718
c906108c
SS
8719/* Tell the user we have just set a breakpoint B. */
8720
8721static void
fba45db2 8722mention (struct breakpoint *b)
c906108c 8723{
348d480f 8724 b->ops->print_mention (b);
112e8700 8725 if (current_uiout->is_mi_like_p ())
fb40c209 8726 return;
c906108c
SS
8727 printf_filtered ("\n");
8728}
c906108c 8729\f
c5aa993b 8730
1a853c52
PA
8731static int bp_loc_is_permanent (struct bp_location *loc);
8732
0d381245 8733static struct bp_location *
39d61571 8734add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8735 const struct symtab_and_line *sal)
8736{
8737 struct bp_location *loc, **tmp;
3742cc8b
YQ
8738 CORE_ADDR adjusted_address;
8739 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8740
8741 if (loc_gdbarch == NULL)
8742 loc_gdbarch = b->gdbarch;
8743
8744 /* Adjust the breakpoint's address prior to allocating a location.
8745 Once we call allocate_bp_location(), that mostly uninitialized
8746 location will be placed on the location chain. Adjustment of the
8747 breakpoint may cause target_read_memory() to be called and we do
8748 not want its scan of the location chain to find a breakpoint and
8749 location that's only been partially initialized. */
8750 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8751 sal->pc, b->type);
0d381245 8752
d30113d4 8753 /* Sort the locations by their ADDRESS. */
39d61571 8754 loc = allocate_bp_location (b);
d30113d4
JK
8755 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8756 tmp = &((*tmp)->next))
0d381245 8757 ;
d30113d4 8758 loc->next = *tmp;
0d381245 8759 *tmp = loc;
3742cc8b 8760
0d381245 8761 loc->requested_address = sal->pc;
3742cc8b 8762 loc->address = adjusted_address;
6c95b8df 8763 loc->pspace = sal->pspace;
729662a5
TT
8764 loc->probe.probe = sal->probe;
8765 loc->probe.objfile = sal->objfile;
6c95b8df 8766 gdb_assert (loc->pspace != NULL);
0d381245 8767 loc->section = sal->section;
3742cc8b 8768 loc->gdbarch = loc_gdbarch;
f8eba3c6 8769 loc->line_number = sal->line;
2f202fde 8770 loc->symtab = sal->symtab;
f8eba3c6 8771
0e30163f
JK
8772 set_breakpoint_location_function (loc,
8773 sal->explicit_pc || sal->explicit_line);
1a853c52 8774
6ae88661
LM
8775 /* While by definition, permanent breakpoints are already present in the
8776 code, we don't mark the location as inserted. Normally one would expect
8777 that GDB could rely on that breakpoint instruction to stop the program,
8778 thus removing the need to insert its own breakpoint, except that executing
8779 the breakpoint instruction can kill the target instead of reporting a
8780 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8781 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8782 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8783 breakpoint be inserted normally results in QEMU knowing about the GDB
8784 breakpoint, and thus trap before the breakpoint instruction is executed.
8785 (If GDB later needs to continue execution past the permanent breakpoint,
8786 it manually increments the PC, thus avoiding executing the breakpoint
8787 instruction.) */
1a853c52 8788 if (bp_loc_is_permanent (loc))
6ae88661 8789 loc->permanent = 1;
1a853c52 8790
0d381245
VP
8791 return loc;
8792}
514f746b
AR
8793\f
8794
1cf4d951 8795/* See breakpoint.h. */
514f746b 8796
1cf4d951
PA
8797int
8798program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8799{
8800 int len;
8801 CORE_ADDR addr;
1afeeb75 8802 const gdb_byte *bpoint;
514f746b
AR
8803 gdb_byte *target_mem;
8804
1cf4d951
PA
8805 addr = address;
8806 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8807
8808 /* Software breakpoints unsupported? */
8809 if (bpoint == NULL)
8810 return 0;
8811
224c3ddb 8812 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8813
8814 /* Enable the automatic memory restoration from breakpoints while
8815 we read the memory. Otherwise we could say about our temporary
8816 breakpoints they are permanent. */
cb85b21b
TT
8817 scoped_restore restore_memory
8818 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8819
8820 if (target_read_memory (address, target_mem, len) == 0
8821 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8822 return 1;
1cf4d951 8823
cb85b21b 8824 return 0;
1cf4d951
PA
8825}
8826
8827/* Return 1 if LOC is pointing to a permanent breakpoint,
8828 return 0 otherwise. */
8829
8830static int
8831bp_loc_is_permanent (struct bp_location *loc)
8832{
514f746b
AR
8833 gdb_assert (loc != NULL);
8834
244558af
LM
8835 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8836 attempt to read from the addresses the locations of these breakpoint types
8837 point to. program_breakpoint_here_p, below, will attempt to read
8838 memory. */
8839 if (!breakpoint_address_is_meaningful (loc->owner))
8840 return 0;
8841
5ed8105e 8842 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8843 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8844 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8845}
8846
e7e0cddf
SS
8847/* Build a command list for the dprintf corresponding to the current
8848 settings of the dprintf style options. */
8849
8850static void
8851update_dprintf_command_list (struct breakpoint *b)
8852{
8853 char *dprintf_args = b->extra_string;
8854 char *printf_line = NULL;
8855
8856 if (!dprintf_args)
8857 return;
8858
8859 dprintf_args = skip_spaces (dprintf_args);
8860
8861 /* Allow a comma, as it may have terminated a location, but don't
8862 insist on it. */
8863 if (*dprintf_args == ',')
8864 ++dprintf_args;
8865 dprintf_args = skip_spaces (dprintf_args);
8866
8867 if (*dprintf_args != '"')
8868 error (_("Bad format string, missing '\"'."));
8869
d3ce09f5 8870 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8871 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8872 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8873 {
8874 if (!dprintf_function)
8875 error (_("No function supplied for dprintf call"));
8876
8877 if (dprintf_channel && strlen (dprintf_channel) > 0)
8878 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8879 dprintf_function,
8880 dprintf_channel,
8881 dprintf_args);
8882 else
8883 printf_line = xstrprintf ("call (void) %s (%s)",
8884 dprintf_function,
8885 dprintf_args);
8886 }
d3ce09f5
SS
8887 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8888 {
8889 if (target_can_run_breakpoint_commands ())
8890 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8891 else
8892 {
8893 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8894 printf_line = xstrprintf ("printf %s", dprintf_args);
8895 }
8896 }
e7e0cddf
SS
8897 else
8898 internal_error (__FILE__, __LINE__,
8899 _("Invalid dprintf style."));
8900
f28045c2 8901 gdb_assert (printf_line != NULL);
9d6e6e84 8902 /* Manufacture a printf sequence. */
f28045c2 8903 {
8d749320 8904 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8905
f28045c2
YQ
8906 printf_cmd_line->control_type = simple_control;
8907 printf_cmd_line->body_count = 0;
8908 printf_cmd_line->body_list = NULL;
9d6e6e84 8909 printf_cmd_line->next = NULL;
f28045c2 8910 printf_cmd_line->line = printf_line;
e7e0cddf 8911
93921405 8912 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8913 }
e7e0cddf
SS
8914}
8915
8916/* Update all dprintf commands, making their command lists reflect
8917 current style settings. */
8918
8919static void
8920update_dprintf_commands (char *args, int from_tty,
8921 struct cmd_list_element *c)
8922{
8923 struct breakpoint *b;
8924
8925 ALL_BREAKPOINTS (b)
8926 {
8927 if (b->type == bp_dprintf)
8928 update_dprintf_command_list (b);
8929 }
8930}
c3f6f71d 8931
f00aae0f
KS
8932/* Create a breakpoint with SAL as location. Use LOCATION
8933 as a description of the location, and COND_STRING
b35a8b2f
DE
8934 as condition expression. If LOCATION is NULL then create an
8935 "address location" from the address in the SAL. */
018d34a4
VP
8936
8937static void
d9b3f62e 8938init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8939 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8940 event_location_up &&location,
e1e01040
PA
8941 gdb::unique_xmalloc_ptr<char> filter,
8942 gdb::unique_xmalloc_ptr<char> cond_string,
8943 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8944 enum bptype type, enum bpdisp disposition,
8945 int thread, int task, int ignore_count,
c0a91b2b 8946 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8947 int enabled, int internal, unsigned flags,
8948 int display_canonical)
018d34a4 8949{
0d381245 8950 int i;
018d34a4
VP
8951
8952 if (type == bp_hardware_breakpoint)
8953 {
fbbd034e
AS
8954 int target_resources_ok;
8955
8956 i = hw_breakpoint_used_count ();
8957 target_resources_ok =
8958 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8959 i + 1, 0);
8960 if (target_resources_ok == 0)
8961 error (_("No hardware breakpoint support in the target."));
8962 else if (target_resources_ok < 0)
8963 error (_("Hardware breakpoints used exceeds limit."));
8964 }
8965
6c5b2ebe 8966 gdb_assert (!sals.empty ());
6c95b8df 8967
6c5b2ebe 8968 for (const auto &sal : sals)
0d381245 8969 {
0d381245
VP
8970 struct bp_location *loc;
8971
8972 if (from_tty)
5af949e3
UW
8973 {
8974 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8975 if (!loc_gdbarch)
8976 loc_gdbarch = gdbarch;
8977
8978 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8979 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8980 }
0d381245 8981
6c5b2ebe 8982 if (&sal == &sals[0])
0d381245 8983 {
d9b3f62e 8984 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8985 b->thread = thread;
4a306c9a 8986 b->task = task;
855a6e68 8987
e1e01040
PA
8988 b->cond_string = cond_string.release ();
8989 b->extra_string = extra_string.release ();
0d381245 8990 b->ignore_count = ignore_count;
41447f92 8991 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8992 b->disposition = disposition;
6c95b8df 8993
44f238bb
PA
8994 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8995 b->loc->inserted = 1;
8996
0fb4aa4b
PA
8997 if (type == bp_static_tracepoint)
8998 {
d9b3f62e 8999 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9000 struct static_tracepoint_marker marker;
9001
983af33b 9002 if (strace_marker_p (b))
0fb4aa4b
PA
9003 {
9004 /* We already know the marker exists, otherwise, we
9005 wouldn't see a sal for it. */
d28cd78a
TT
9006 const char *p
9007 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9008 const char *endp;
0fb4aa4b 9009 char *marker_str;
0fb4aa4b 9010
f1735a53 9011 p = skip_spaces (p);
0fb4aa4b 9012
f1735a53 9013 endp = skip_to_space (p);
0fb4aa4b
PA
9014
9015 marker_str = savestring (p, endp - p);
d9b3f62e 9016 t->static_trace_marker_id = marker_str;
0fb4aa4b 9017
3e43a32a
MS
9018 printf_filtered (_("Probed static tracepoint "
9019 "marker \"%s\"\n"),
d9b3f62e 9020 t->static_trace_marker_id);
0fb4aa4b
PA
9021 }
9022 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9023 {
d9b3f62e 9024 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9025 release_static_tracepoint_marker (&marker);
9026
3e43a32a
MS
9027 printf_filtered (_("Probed static tracepoint "
9028 "marker \"%s\"\n"),
d9b3f62e 9029 t->static_trace_marker_id);
0fb4aa4b
PA
9030 }
9031 else
3e43a32a
MS
9032 warning (_("Couldn't determine the static "
9033 "tracepoint marker to probe"));
0fb4aa4b
PA
9034 }
9035
0d381245
VP
9036 loc = b->loc;
9037 }
9038 else
018d34a4 9039 {
39d61571 9040 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9041 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9042 loc->inserted = 1;
0d381245
VP
9043 }
9044
9045 if (b->cond_string)
9046 {
bbc13ae3
KS
9047 const char *arg = b->cond_string;
9048
1bb9788d
TT
9049 loc->cond = parse_exp_1 (&arg, loc->address,
9050 block_for_pc (loc->address), 0);
0d381245 9051 if (*arg)
588ae58c 9052 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9053 }
e7e0cddf
SS
9054
9055 /* Dynamic printf requires and uses additional arguments on the
9056 command line, otherwise it's an error. */
9057 if (type == bp_dprintf)
9058 {
9059 if (b->extra_string)
9060 update_dprintf_command_list (b);
9061 else
9062 error (_("Format string required"));
9063 }
9064 else if (b->extra_string)
588ae58c 9065 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9066 }
018d34a4 9067
56435ebe 9068 b->display_canonical = display_canonical;
f00aae0f 9069 if (location != NULL)
d28cd78a 9070 b->location = std::move (location);
018d34a4 9071 else
d28cd78a 9072 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9073 b->filter = filter.release ();
d9b3f62e 9074}
018d34a4 9075
d9b3f62e
PA
9076static void
9077create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9078 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9079 event_location_up &&location,
e1e01040
PA
9080 gdb::unique_xmalloc_ptr<char> filter,
9081 gdb::unique_xmalloc_ptr<char> cond_string,
9082 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9083 enum bptype type, enum bpdisp disposition,
9084 int thread, int task, int ignore_count,
c0a91b2b 9085 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9086 int enabled, int internal, unsigned flags,
9087 int display_canonical)
d9b3f62e 9088{
a5e364af 9089 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9090
a5e364af 9091 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9092 sals, std::move (location),
e1e01040
PA
9093 std::move (filter),
9094 std::move (cond_string),
9095 std::move (extra_string),
d9b3f62e
PA
9096 type, disposition,
9097 thread, task, ignore_count,
9098 ops, from_tty,
44f238bb
PA
9099 enabled, internal, flags,
9100 display_canonical);
d9b3f62e 9101
b270e6f9 9102 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9103}
9104
9105/* Add SALS.nelts breakpoints to the breakpoint table. For each
9106 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9107 value. COND_STRING, if not NULL, specified the condition to be
9108 used for all breakpoints. Essentially the only case where
9109 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9110 function. In that case, it's still not possible to specify
9111 separate conditions for different overloaded functions, so
9112 we take just a single condition string.
9113
c3f6f71d 9114 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9115 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9116 array contents). If the function fails (error() is called), the
9117 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9118 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9119
9120static void
8cdf0e15 9121create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9122 struct linespec_result *canonical,
e1e01040
PA
9123 gdb::unique_xmalloc_ptr<char> cond_string,
9124 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9125 enum bptype type, enum bpdisp disposition,
9126 int thread, int task, int ignore_count,
c0a91b2b 9127 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9128 int enabled, int internal, unsigned flags)
c906108c 9129{
f8eba3c6 9130 if (canonical->pre_expanded)
6c5b2ebe 9131 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9132
6c5b2ebe 9133 for (const auto &lsal : canonical->lsals)
c3f6f71d 9134 {
f00aae0f 9135 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9136 'break', without arguments. */
ffc2605c 9137 event_location_up location
f00aae0f 9138 = (canonical->location != NULL
8e9e35b1 9139 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9140 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9141 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9142
6c5b2ebe 9143 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9144 std::move (location),
e1e01040
PA
9145 std::move (filter_string),
9146 std::move (cond_string),
9147 std::move (extra_string),
e7e0cddf 9148 type, disposition,
84f4c1fe 9149 thread, task, ignore_count, ops,
44f238bb 9150 from_tty, enabled, internal, flags,
56435ebe 9151 canonical->special_display);
c3f6f71d 9152 }
c3f6f71d 9153}
c906108c 9154
f00aae0f 9155/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9156 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9157 addresses found. LOCATION points to the end of the SAL (for
9158 linespec locations).
9998af43
TJB
9159
9160 The array and the line spec strings are allocated on the heap, it is
9161 the caller's responsibility to free them. */
c906108c 9162
b9362cc7 9163static void
f00aae0f 9164parse_breakpoint_sals (const struct event_location *location,
58438ac1 9165 struct linespec_result *canonical)
c3f6f71d 9166{
f00aae0f
KS
9167 struct symtab_and_line cursal;
9168
9169 if (event_location_type (location) == LINESPEC_LOCATION)
9170 {
9171 const char *address = get_linespec_location (location);
9172
9173 if (address == NULL)
9174 {
9175 /* The last displayed codepoint, if it's valid, is our default
9176 breakpoint address. */
9177 if (last_displayed_sal_is_valid ())
9178 {
f00aae0f
KS
9179 /* Set sal's pspace, pc, symtab, and line to the values
9180 corresponding to the last call to print_frame_info.
9181 Be sure to reinitialize LINE with NOTCURRENT == 0
9182 as the breakpoint line number is inappropriate otherwise.
9183 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9184 symtab_and_line sal = get_last_displayed_sal ();
9185 CORE_ADDR pc = sal.pc;
9186
f00aae0f
KS
9187 sal = find_pc_line (pc, 0);
9188
9189 /* "break" without arguments is equivalent to "break *PC"
9190 where PC is the last displayed codepoint's address. So
9191 make sure to set sal.explicit_pc to prevent GDB from
9192 trying to expand the list of sals to include all other
9193 instances with the same symtab and line. */
9194 sal.pc = pc;
9195 sal.explicit_pc = 1;
9196
6c5b2ebe
PA
9197 struct linespec_sals lsal;
9198 lsal.sals = {sal};
f00aae0f
KS
9199 lsal.canonical = NULL;
9200
6c5b2ebe 9201 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9202 return;
9203 }
9204 else
9205 error (_("No default breakpoint address now."));
c906108c 9206 }
c906108c 9207 }
f00aae0f
KS
9208
9209 /* Force almost all breakpoints to be in terms of the
9210 current_source_symtab (which is decode_line_1's default).
9211 This should produce the results we want almost all of the
9212 time while leaving default_breakpoint_* alone.
9213
9214 ObjC: However, don't match an Objective-C method name which
9215 may have a '+' or '-' succeeded by a '['. */
9216 cursal = get_current_source_symtab_and_line ();
9217 if (last_displayed_sal_is_valid ())
c906108c 9218 {
f00aae0f 9219 const char *address = NULL;
cc80f267 9220
f00aae0f
KS
9221 if (event_location_type (location) == LINESPEC_LOCATION)
9222 address = get_linespec_location (location);
cc80f267 9223
f00aae0f
KS
9224 if (!cursal.symtab
9225 || (address != NULL
9226 && strchr ("+-", address[0]) != NULL
9227 && address[1] != '['))
9228 {
c2f4122d 9229 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9230 get_last_displayed_symtab (),
9231 get_last_displayed_line (),
9232 canonical, NULL, NULL);
9233 return;
9234 }
c906108c 9235 }
f00aae0f 9236
c2f4122d 9237 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9238 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9239}
c906108c 9240
c906108c 9241
c3f6f71d 9242/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9243 inserted as a breakpoint. If it can't throw an error. */
c906108c 9244
b9362cc7 9245static void
6c5b2ebe 9246breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9247{
6c5b2ebe
PA
9248 for (auto &sal : sals)
9249 resolve_sal_pc (&sal);
c3f6f71d
JM
9250}
9251
7a697b8d
SS
9252/* Fast tracepoints may have restrictions on valid locations. For
9253 instance, a fast tracepoint using a jump instead of a trap will
9254 likely have to overwrite more bytes than a trap would, and so can
9255 only be placed where the instruction is longer than the jump, or a
9256 multi-instruction sequence does not have a jump into the middle of
9257 it, etc. */
9258
9259static void
9260check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9261 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9262{
6c5b2ebe 9263 int rslt;
7a697b8d
SS
9264 char *msg;
9265 struct cleanup *old_chain;
9266
6c5b2ebe 9267 for (const auto &sal : sals)
7a697b8d 9268 {
f8eba3c6
TT
9269 struct gdbarch *sarch;
9270
6c5b2ebe 9271 sarch = get_sal_arch (sal);
f8eba3c6
TT
9272 /* We fall back to GDBARCH if there is no architecture
9273 associated with SAL. */
9274 if (sarch == NULL)
9275 sarch = gdbarch;
6c5b2ebe 9276 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9277 old_chain = make_cleanup (xfree, msg);
9278
9279 if (!rslt)
53c3572a 9280 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9281 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9282
9283 do_cleanups (old_chain);
9284 }
9285}
9286
018d34a4
VP
9287/* Given TOK, a string specification of condition and thread, as
9288 accepted by the 'break' command, extract the condition
9289 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9290 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9291 If no condition is found, *COND_STRING is set to NULL.
9292 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9293
9294static void
bbc13ae3 9295find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9296 char **cond_string, int *thread, int *task,
9297 char **rest)
018d34a4
VP
9298{
9299 *cond_string = NULL;
9300 *thread = -1;
ed1d1739
KS
9301 *task = 0;
9302 *rest = NULL;
9303
018d34a4
VP
9304 while (tok && *tok)
9305 {
bbc13ae3 9306 const char *end_tok;
018d34a4 9307 int toklen;
bbc13ae3
KS
9308 const char *cond_start = NULL;
9309 const char *cond_end = NULL;
cc59ec59 9310
f1735a53 9311 tok = skip_spaces (tok);
e7e0cddf
SS
9312
9313 if ((*tok == '"' || *tok == ',') && rest)
9314 {
9315 *rest = savestring (tok, strlen (tok));
9316 return;
9317 }
9318
f1735a53 9319 end_tok = skip_to_space (tok);
d634f2de 9320
018d34a4 9321 toklen = end_tok - tok;
d634f2de 9322
018d34a4
VP
9323 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9324 {
9325 tok = cond_start = end_tok + 1;
4d01a485 9326 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9327 cond_end = tok;
d634f2de 9328 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9329 }
9330 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9331 {
5d5658a1
PA
9332 const char *tmptok;
9333 struct thread_info *thr;
d634f2de 9334
018d34a4 9335 tok = end_tok + 1;
5d5658a1 9336 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9337 if (tok == tmptok)
9338 error (_("Junk after thread keyword."));
5d5658a1 9339 *thread = thr->global_num;
bbc13ae3 9340 tok = tmptok;
018d34a4 9341 }
4a306c9a
JB
9342 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9343 {
9344 char *tmptok;
9345
9346 tok = end_tok + 1;
bbc13ae3 9347 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9348 if (tok == tmptok)
9349 error (_("Junk after task keyword."));
9350 if (!valid_task_id (*task))
b6199126 9351 error (_("Unknown task %d."), *task);
bbc13ae3 9352 tok = tmptok;
4a306c9a 9353 }
e7e0cddf
SS
9354 else if (rest)
9355 {
9356 *rest = savestring (tok, strlen (tok));
ccab2054 9357 return;
e7e0cddf 9358 }
018d34a4
VP
9359 else
9360 error (_("Junk at end of arguments."));
9361 }
9362}
9363
0fb4aa4b
PA
9364/* Decode a static tracepoint marker spec. */
9365
6c5b2ebe 9366static std::vector<symtab_and_line>
f00aae0f 9367decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9368{
9369 VEC(static_tracepoint_marker_p) *markers = NULL;
0fb4aa4b 9370 struct cleanup *old_chain;
f00aae0f
KS
9371 const char *p = &(*arg_p)[3];
9372 const char *endp;
0fb4aa4b
PA
9373 char *marker_str;
9374 int i;
9375
f1735a53 9376 p = skip_spaces (p);
0fb4aa4b 9377
f1735a53 9378 endp = skip_to_space (p);
0fb4aa4b
PA
9379
9380 marker_str = savestring (p, endp - p);
9381 old_chain = make_cleanup (xfree, marker_str);
9382
9383 markers = target_static_tracepoint_markers_by_strid (marker_str);
9384 if (VEC_empty(static_tracepoint_marker_p, markers))
9385 error (_("No known static tracepoint marker named %s"), marker_str);
9386
6c5b2ebe
PA
9387 std::vector<symtab_and_line> sals;
9388 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9389
6c5b2ebe 9390 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9391 {
9392 struct static_tracepoint_marker *marker;
9393
9394 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9395
51abb421 9396 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9397 sal.pc = marker->address;
9398 sals.push_back (sal);
0fb4aa4b
PA
9399
9400 release_static_tracepoint_marker (marker);
9401 }
9402
9403 do_cleanups (old_chain);
9404
9405 *arg_p = endp;
9406 return sals;
9407}
9408
f00aae0f 9409/* See breakpoint.h. */
0101ce28 9410
8cdf0e15
VP
9411int
9412create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9413 const struct event_location *location,
9414 const char *cond_string,
9415 int thread, const char *extra_string,
f00aae0f 9416 int parse_extra,
0fb4aa4b 9417 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9418 int ignore_count,
9419 enum auto_boolean pending_break_support,
c0a91b2b 9420 const struct breakpoint_ops *ops,
44f238bb
PA
9421 int from_tty, int enabled, int internal,
9422 unsigned flags)
c3f6f71d 9423{
7efd8fc2 9424 struct linespec_result canonical;
80c99de1 9425 struct cleanup *bkpt_chain = NULL;
0101ce28 9426 int pending = 0;
4a306c9a 9427 int task = 0;
86b17b60 9428 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9429
348d480f
PA
9430 gdb_assert (ops != NULL);
9431
f00aae0f
KS
9432 /* If extra_string isn't useful, set it to NULL. */
9433 if (extra_string != NULL && *extra_string == '\0')
9434 extra_string = NULL;
9435
492d29ea 9436 TRY
b78a6381 9437 {
f00aae0f 9438 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9439 }
492d29ea 9440 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9441 {
492d29ea
PA
9442 /* If caller is interested in rc value from parse, set
9443 value. */
9444 if (e.error == NOT_FOUND_ERROR)
0101ce28 9445 {
05ff989b
AC
9446 /* If pending breakpoint support is turned off, throw
9447 error. */
fa8d40ab
JJ
9448
9449 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9450 throw_exception (e);
9451
9452 exception_print (gdb_stderr, e);
fa8d40ab 9453
05ff989b
AC
9454 /* If pending breakpoint support is auto query and the user
9455 selects no, then simply return the error code. */
059fb39f 9456 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9457 && !nquery (_("Make %s pending on future shared library load? "),
9458 bptype_string (type_wanted)))
fd9b8c24 9459 return 0;
fa8d40ab 9460
05ff989b
AC
9461 /* At this point, either the user was queried about setting
9462 a pending breakpoint and selected yes, or pending
9463 breakpoint behavior is on and thus a pending breakpoint
9464 is defaulted on behalf of the user. */
f00aae0f 9465 pending = 1;
0101ce28 9466 }
492d29ea
PA
9467 else
9468 throw_exception (e);
0101ce28 9469 }
492d29ea
PA
9470 END_CATCH
9471
6c5b2ebe 9472 if (!pending && canonical.lsals.empty ())
492d29ea 9473 return 0;
c3f6f71d 9474
c3f6f71d
JM
9475 /* ----------------------------- SNIP -----------------------------
9476 Anything added to the cleanup chain beyond this point is assumed
9477 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9478 then the memory is not reclaimed. */
9479 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9480
c3f6f71d
JM
9481 /* Resolve all line numbers to PC's and verify that the addresses
9482 are ok for the target. */
0101ce28 9483 if (!pending)
f8eba3c6 9484 {
6c5b2ebe
PA
9485 for (auto &lsal : canonical.lsals)
9486 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9487 }
c3f6f71d 9488
7a697b8d 9489 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9490 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9491 {
6c5b2ebe
PA
9492 for (const auto &lsal : canonical.lsals)
9493 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9494 }
7a697b8d 9495
c3f6f71d
JM
9496 /* Verify that condition can be parsed, before setting any
9497 breakpoints. Allocate a separate condition expression for each
4a64f543 9498 breakpoint. */
0101ce28 9499 if (!pending)
c3f6f71d 9500 {
e1e01040
PA
9501 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9502 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9503
f00aae0f 9504 if (parse_extra)
72b2ff0e 9505 {
0878d0fa 9506 char *rest;
e1e01040 9507 char *cond;
52d361e1 9508
6c5b2ebe 9509 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9510
0878d0fa
YQ
9511 /* Here we only parse 'arg' to separate condition
9512 from thread number, so parsing in context of first
9513 sal is OK. When setting the breakpoint we'll
9514 re-parse it in context of each sal. */
9515
6c5b2ebe 9516 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9517 &cond, &thread, &task, &rest);
9518 cond_string_copy.reset (cond);
9519 extra_string_copy.reset (rest);
72b2ff0e 9520 }
2f069f6f 9521 else
72b2ff0e 9522 {
f00aae0f
KS
9523 if (type_wanted != bp_dprintf
9524 && extra_string != NULL && *extra_string != '\0')
9525 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9526
9527 /* Create a private copy of condition string. */
9528 if (cond_string)
e1e01040 9529 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9530 /* Create a private copy of any extra string. */
9531 if (extra_string)
e1e01040 9532 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9533 }
0fb4aa4b 9534
52d361e1 9535 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9536 std::move (cond_string_copy),
9537 std::move (extra_string_copy),
9538 type_wanted,
d9b3f62e
PA
9539 tempflag ? disp_del : disp_donttouch,
9540 thread, task, ignore_count, ops,
44f238bb 9541 from_tty, enabled, internal, flags);
c906108c 9542 }
0101ce28
JJ
9543 else
9544 {
a5e364af 9545 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9546
a5e364af 9547 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9548 b->location = copy_event_location (location);
bfccc43c 9549
f00aae0f
KS
9550 if (parse_extra)
9551 b->cond_string = NULL;
e12c7713
MK
9552 else
9553 {
9554 /* Create a private copy of condition string. */
e1e01040 9555 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9556 b->thread = thread;
e12c7713 9557 }
f00aae0f
KS
9558
9559 /* Create a private copy of any extra string. */
e1e01040 9560 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9561 b->ignore_count = ignore_count;
0101ce28 9562 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9563 b->condition_not_parsed = 1;
41447f92 9564 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9565 if ((type_wanted != bp_breakpoint
9566 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9567 b->pspace = current_program_space;
8bea4e01 9568
b270e6f9 9569 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9570 }
9571
6c5b2ebe 9572 if (canonical.lsals.size () > 1)
95a42b64 9573 {
3e43a32a
MS
9574 warning (_("Multiple breakpoints were set.\nUse the "
9575 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9576 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9577 }
9578
80c99de1
PA
9579 /* That's it. Discard the cleanups for data inserted into the
9580 breakpoint. */
9581 discard_cleanups (bkpt_chain);
217dc9e2 9582
80c99de1 9583 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9584 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9585
9586 return 1;
c3f6f71d 9587}
c906108c 9588
348d480f 9589/* Set a breakpoint.
72b2ff0e
VP
9590 ARG is a string describing breakpoint address,
9591 condition, and thread.
9592 FLAG specifies if a breakpoint is hardware on,
9593 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9594 and BP_TEMPFLAG. */
348d480f 9595
98deb0da 9596static void
f2fc3015 9597break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9598{
72b2ff0e 9599 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9600 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9601 ? bp_hardware_breakpoint
9602 : bp_breakpoint);
55aa24fb 9603 struct breakpoint_ops *ops;
f00aae0f 9604
ffc2605c 9605 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9606
9607 /* Matching breakpoints on probes. */
5b56227b 9608 if (location != NULL
ffc2605c 9609 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9610 ops = &bkpt_probe_breakpoint_ops;
9611 else
9612 ops = &bkpt_breakpoint_ops;
c3f6f71d 9613
8cdf0e15 9614 create_breakpoint (get_current_arch (),
ffc2605c 9615 location.get (),
f00aae0f 9616 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9617 tempflag, type_wanted,
8cdf0e15
VP
9618 0 /* Ignore count */,
9619 pending_break_support,
55aa24fb 9620 ops,
8cdf0e15 9621 from_tty,
84f4c1fe 9622 1 /* enabled */,
44f238bb
PA
9623 0 /* internal */,
9624 0);
c906108c
SS
9625}
9626
c906108c
SS
9627/* Helper function for break_command_1 and disassemble_command. */
9628
9629void
fba45db2 9630resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9631{
9632 CORE_ADDR pc;
9633
9634 if (sal->pc == 0 && sal->symtab != NULL)
9635 {
9636 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9637 error (_("No line %d in file \"%s\"."),
05cba821 9638 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9639 sal->pc = pc;
6a048695 9640
4a64f543
MS
9641 /* If this SAL corresponds to a breakpoint inserted using a line
9642 number, then skip the function prologue if necessary. */
6a048695 9643 if (sal->explicit_line)
059acae7 9644 skip_prologue_sal (sal);
c906108c
SS
9645 }
9646
9647 if (sal->section == 0 && sal->symtab != NULL)
9648 {
346d1dfe 9649 const struct blockvector *bv;
3977b71f 9650 const struct block *b;
c5aa993b 9651 struct symbol *sym;
c906108c 9652
43f3e411
DE
9653 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9654 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9655 if (bv != NULL)
9656 {
7f0df278 9657 sym = block_linkage_function (b);
c906108c
SS
9658 if (sym != NULL)
9659 {
eb822aa6
DE
9660 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9661 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9662 sym);
c906108c
SS
9663 }
9664 else
9665 {
4a64f543
MS
9666 /* It really is worthwhile to have the section, so we'll
9667 just have to look harder. This case can be executed
9668 if we have line numbers but no functions (as can
9669 happen in assembly source). */
c906108c 9670
5ed8105e 9671 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9672 switch_to_program_space_and_thread (sal->pspace);
c906108c 9673
5ed8105e 9674 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9675 if (msym.minsym)
efd66ac6 9676 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9677 }
9678 }
9679 }
9680}
9681
9682void
fba45db2 9683break_command (char *arg, int from_tty)
c906108c 9684{
db107f19 9685 break_command_1 (arg, 0, from_tty);
c906108c
SS
9686}
9687
c906108c 9688void
fba45db2 9689tbreak_command (char *arg, int from_tty)
c906108c 9690{
db107f19 9691 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9692}
9693
c906108c 9694static void
fba45db2 9695hbreak_command (char *arg, int from_tty)
c906108c 9696{
db107f19 9697 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9698}
9699
9700static void
fba45db2 9701thbreak_command (char *arg, int from_tty)
c906108c 9702{
db107f19 9703 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9704}
9705
9706static void
fba45db2 9707stop_command (char *arg, int from_tty)
c906108c 9708{
a3f17187 9709 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9710Usage: stop in <function | address>\n\
a3f17187 9711 stop at <line>\n"));
c906108c
SS
9712}
9713
9714static void
4495129a 9715stopin_command (const char *arg, int from_tty)
c906108c
SS
9716{
9717 int badInput = 0;
9718
c5aa993b 9719 if (arg == (char *) NULL)
c906108c
SS
9720 badInput = 1;
9721 else if (*arg != '*')
9722 {
4495129a 9723 const char *argptr = arg;
c906108c
SS
9724 int hasColon = 0;
9725
4a64f543 9726 /* Look for a ':'. If this is a line number specification, then
53a5351d 9727 say it is bad, otherwise, it should be an address or
4a64f543 9728 function/method name. */
c906108c 9729 while (*argptr && !hasColon)
c5aa993b
JM
9730 {
9731 hasColon = (*argptr == ':');
9732 argptr++;
9733 }
c906108c
SS
9734
9735 if (hasColon)
c5aa993b 9736 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9737 else
c5aa993b 9738 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9739 }
9740
9741 if (badInput)
a3f17187 9742 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9743 else
db107f19 9744 break_command_1 (arg, 0, from_tty);
c906108c
SS
9745}
9746
9747static void
4495129a 9748stopat_command (const char *arg, int from_tty)
c906108c
SS
9749{
9750 int badInput = 0;
9751
c5aa993b 9752 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9753 badInput = 1;
9754 else
9755 {
4495129a 9756 const char *argptr = arg;
c906108c
SS
9757 int hasColon = 0;
9758
4a64f543
MS
9759 /* Look for a ':'. If there is a '::' then get out, otherwise
9760 it is probably a line number. */
c906108c 9761 while (*argptr && !hasColon)
c5aa993b
JM
9762 {
9763 hasColon = (*argptr == ':');
9764 argptr++;
9765 }
c906108c
SS
9766
9767 if (hasColon)
c5aa993b 9768 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9769 else
c5aa993b 9770 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9771 }
9772
9773 if (badInput)
a3f17187 9774 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9775 else
db107f19 9776 break_command_1 (arg, 0, from_tty);
c906108c
SS
9777}
9778
e7e0cddf
SS
9779/* The dynamic printf command is mostly like a regular breakpoint, but
9780 with a prewired command list consisting of a single output command,
9781 built from extra arguments supplied on the dprintf command
9782 line. */
9783
da821c7b 9784static void
f2fc3015 9785dprintf_command (char *arg_in, int from_tty)
e7e0cddf 9786{
f2fc3015 9787 const char *arg = arg_in;
ffc2605c 9788 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9789
9790 /* If non-NULL, ARG should have been advanced past the location;
9791 the next character must be ','. */
9792 if (arg != NULL)
9793 {
9794 if (arg[0] != ',' || arg[1] == '\0')
9795 error (_("Format string required"));
9796 else
9797 {
9798 /* Skip the comma. */
9799 ++arg;
9800 }
9801 }
9802
e7e0cddf 9803 create_breakpoint (get_current_arch (),
ffc2605c 9804 location.get (),
f00aae0f 9805 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9806 0, bp_dprintf,
9807 0 /* Ignore count */,
9808 pending_break_support,
9809 &dprintf_breakpoint_ops,
9810 from_tty,
9811 1 /* enabled */,
9812 0 /* internal */,
9813 0);
9814}
9815
d3ce09f5
SS
9816static void
9817agent_printf_command (char *arg, int from_tty)
9818{
9819 error (_("May only run agent-printf on the target"));
9820}
9821
f1310107
TJB
9822/* Implement the "breakpoint_hit" breakpoint_ops method for
9823 ranged breakpoints. */
9824
9825static int
9826breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9827 struct address_space *aspace,
09ac7c10
TT
9828 CORE_ADDR bp_addr,
9829 const struct target_waitstatus *ws)
f1310107 9830{
09ac7c10 9831 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9832 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9833 return 0;
9834
f1310107
TJB
9835 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9836 bl->length, aspace, bp_addr);
9837}
9838
9839/* Implement the "resources_needed" breakpoint_ops method for
9840 ranged breakpoints. */
9841
9842static int
9843resources_needed_ranged_breakpoint (const struct bp_location *bl)
9844{
9845 return target_ranged_break_num_registers ();
9846}
9847
9848/* Implement the "print_it" breakpoint_ops method for
9849 ranged breakpoints. */
9850
9851static enum print_stop_action
348d480f 9852print_it_ranged_breakpoint (bpstat bs)
f1310107 9853{
348d480f 9854 struct breakpoint *b = bs->breakpoint_at;
f1310107 9855 struct bp_location *bl = b->loc;
79a45e25 9856 struct ui_out *uiout = current_uiout;
f1310107
TJB
9857
9858 gdb_assert (b->type == bp_hardware_breakpoint);
9859
9860 /* Ranged breakpoints have only one location. */
9861 gdb_assert (bl && bl->next == NULL);
9862
9863 annotate_breakpoint (b->number);
f303dbd6
PA
9864
9865 maybe_print_thread_hit_breakpoint (uiout);
9866
f1310107 9867 if (b->disposition == disp_del)
112e8700 9868 uiout->text ("Temporary ranged breakpoint ");
f1310107 9869 else
112e8700
SM
9870 uiout->text ("Ranged breakpoint ");
9871 if (uiout->is_mi_like_p ())
f1310107 9872 {
112e8700 9873 uiout->field_string ("reason",
f1310107 9874 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9875 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9876 }
112e8700
SM
9877 uiout->field_int ("bkptno", b->number);
9878 uiout->text (", ");
f1310107
TJB
9879
9880 return PRINT_SRC_AND_LOC;
9881}
9882
9883/* Implement the "print_one" breakpoint_ops method for
9884 ranged breakpoints. */
9885
9886static void
9887print_one_ranged_breakpoint (struct breakpoint *b,
9888 struct bp_location **last_loc)
9889{
9890 struct bp_location *bl = b->loc;
9891 struct value_print_options opts;
79a45e25 9892 struct ui_out *uiout = current_uiout;
f1310107
TJB
9893
9894 /* Ranged breakpoints have only one location. */
9895 gdb_assert (bl && bl->next == NULL);
9896
9897 get_user_print_options (&opts);
9898
9899 if (opts.addressprint)
9900 /* We don't print the address range here, it will be printed later
9901 by print_one_detail_ranged_breakpoint. */
112e8700 9902 uiout->field_skip ("addr");
f1310107
TJB
9903 annotate_field (5);
9904 print_breakpoint_location (b, bl);
9905 *last_loc = bl;
9906}
9907
9908/* Implement the "print_one_detail" breakpoint_ops method for
9909 ranged breakpoints. */
9910
9911static void
9912print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9913 struct ui_out *uiout)
9914{
9915 CORE_ADDR address_start, address_end;
9916 struct bp_location *bl = b->loc;
d7e74731 9917 string_file stb;
f1310107
TJB
9918
9919 gdb_assert (bl);
9920
9921 address_start = bl->address;
9922 address_end = address_start + bl->length - 1;
9923
112e8700 9924 uiout->text ("\taddress range: ");
d7e74731
PA
9925 stb.printf ("[%s, %s]",
9926 print_core_address (bl->gdbarch, address_start),
9927 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9928 uiout->field_stream ("addr", stb);
9929 uiout->text ("\n");
f1310107
TJB
9930}
9931
9932/* Implement the "print_mention" breakpoint_ops method for
9933 ranged breakpoints. */
9934
9935static void
9936print_mention_ranged_breakpoint (struct breakpoint *b)
9937{
9938 struct bp_location *bl = b->loc;
79a45e25 9939 struct ui_out *uiout = current_uiout;
f1310107
TJB
9940
9941 gdb_assert (bl);
9942 gdb_assert (b->type == bp_hardware_breakpoint);
9943
112e8700 9944 if (uiout->is_mi_like_p ())
f1310107
TJB
9945 return;
9946
9947 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9948 b->number, paddress (bl->gdbarch, bl->address),
9949 paddress (bl->gdbarch, bl->address + bl->length - 1));
9950}
9951
9952/* Implement the "print_recreate" breakpoint_ops method for
9953 ranged breakpoints. */
9954
9955static void
9956print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9957{
f00aae0f 9958 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9959 event_location_to_string (b->location.get ()),
9960 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9961 print_recreate_thread (b, fp);
f1310107
TJB
9962}
9963
9964/* The breakpoint_ops structure to be used in ranged breakpoints. */
9965
2060206e 9966static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9967
9968/* Find the address where the end of the breakpoint range should be
9969 placed, given the SAL of the end of the range. This is so that if
9970 the user provides a line number, the end of the range is set to the
9971 last instruction of the given line. */
9972
9973static CORE_ADDR
9974find_breakpoint_range_end (struct symtab_and_line sal)
9975{
9976 CORE_ADDR end;
9977
9978 /* If the user provided a PC value, use it. Otherwise,
9979 find the address of the end of the given location. */
9980 if (sal.explicit_pc)
9981 end = sal.pc;
9982 else
9983 {
9984 int ret;
9985 CORE_ADDR start;
9986
9987 ret = find_line_pc_range (sal, &start, &end);
9988 if (!ret)
9989 error (_("Could not find location of the end of the range."));
9990
9991 /* find_line_pc_range returns the start of the next line. */
9992 end--;
9993 }
9994
9995 return end;
9996}
9997
9998/* Implement the "break-range" CLI command. */
9999
10000static void
f2fc3015 10001break_range_command (char *arg_in, int from_tty)
f1310107 10002{
f2fc3015
TT
10003 const char *arg = arg_in;
10004 const char *arg_start;
10005 char *addr_string_start;
f1310107
TJB
10006 struct linespec_result canonical_start, canonical_end;
10007 int bp_count, can_use_bp, length;
10008 CORE_ADDR end;
10009 struct breakpoint *b;
f1310107
TJB
10010 struct cleanup *cleanup_bkpt;
10011
10012 /* We don't support software ranged breakpoints. */
10013 if (target_ranged_break_num_registers () < 0)
10014 error (_("This target does not support hardware ranged breakpoints."));
10015
10016 bp_count = hw_breakpoint_used_count ();
10017 bp_count += target_ranged_break_num_registers ();
10018 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10019 bp_count, 0);
10020 if (can_use_bp < 0)
10021 error (_("Hardware breakpoints used exceeds limit."));
10022
f8eba3c6 10023 arg = skip_spaces (arg);
f1310107
TJB
10024 if (arg == NULL || arg[0] == '\0')
10025 error(_("No address range specified."));
10026
f8eba3c6 10027 arg_start = arg;
ffc2605c
TT
10028 event_location_up start_location = string_to_event_location (&arg,
10029 current_language);
10030 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10031
10032 if (arg[0] != ',')
10033 error (_("Too few arguments."));
6c5b2ebe 10034 else if (canonical_start.lsals.empty ())
f1310107 10035 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10036
6c5b2ebe 10037 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10038
6c5b2ebe
PA
10039 if (canonical_start.lsals.size () > 1
10040 || lsal_start.sals.size () != 1)
f1310107
TJB
10041 error (_("Cannot create a ranged breakpoint with multiple locations."));
10042
6c5b2ebe 10043 const symtab_and_line &sal_start = lsal_start.sals[0];
f8eba3c6 10044 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10045 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10046
10047 arg++; /* Skip the comma. */
f8eba3c6 10048 arg = skip_spaces (arg);
f1310107
TJB
10049
10050 /* Parse the end location. */
10051
f1310107
TJB
10052 arg_start = arg;
10053
f8eba3c6 10054 /* We call decode_line_full directly here instead of using
f1310107
TJB
10055 parse_breakpoint_sals because we need to specify the start location's
10056 symtab and line as the default symtab and line for the end of the
10057 range. This makes it possible to have ranges like "foo.c:27, +14",
10058 where +14 means 14 lines from the start location. */
ffc2605c
TT
10059 event_location_up end_location = string_to_event_location (&arg,
10060 current_language);
10061 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10062 sal_start.symtab, sal_start.line,
10063 &canonical_end, NULL, NULL);
10064
6c5b2ebe 10065 if (canonical_end.lsals.empty ())
f1310107 10066 error (_("Could not find location of the end of the range."));
f8eba3c6 10067
6c5b2ebe
PA
10068 const linespec_sals &lsal_end = canonical_end.lsals[0];
10069 if (canonical_end.lsals.size () > 1
10070 || lsal_end.sals.size () != 1)
f1310107
TJB
10071 error (_("Cannot create a ranged breakpoint with multiple locations."));
10072
6c5b2ebe 10073 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10074
10075 end = find_breakpoint_range_end (sal_end);
10076 if (sal_start.pc > end)
177b42fe 10077 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10078
10079 length = end - sal_start.pc + 1;
10080 if (length < 0)
10081 /* Length overflowed. */
10082 error (_("Address range too large."));
10083 else if (length == 1)
10084 {
10085 /* This range is simple enough to be handled by
10086 the `hbreak' command. */
10087 hbreak_command (addr_string_start, 1);
10088
10089 do_cleanups (cleanup_bkpt);
10090
10091 return;
10092 }
10093
10094 /* Now set up the breakpoint. */
10095 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10096 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10097 set_breakpoint_count (breakpoint_count + 1);
10098 b->number = breakpoint_count;
10099 b->disposition = disp_donttouch;
d28cd78a
TT
10100 b->location = std::move (start_location);
10101 b->location_range_end = std::move (end_location);
f1310107
TJB
10102 b->loc->length = length;
10103
f8eba3c6 10104 do_cleanups (cleanup_bkpt);
f1310107
TJB
10105
10106 mention (b);
8d3788bd 10107 observer_notify_breakpoint_created (b);
44702360 10108 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10109}
10110
4a64f543
MS
10111/* Return non-zero if EXP is verified as constant. Returned zero
10112 means EXP is variable. Also the constant detection may fail for
10113 some constant expressions and in such case still falsely return
10114 zero. */
2e6e3d9c 10115
65d79d4b
SDJ
10116static int
10117watchpoint_exp_is_const (const struct expression *exp)
10118{
10119 int i = exp->nelts;
10120
10121 while (i > 0)
10122 {
10123 int oplenp, argsp;
10124
10125 /* We are only interested in the descriptor of each element. */
10126 operator_length (exp, i, &oplenp, &argsp);
10127 i -= oplenp;
10128
10129 switch (exp->elts[i].opcode)
10130 {
10131 case BINOP_ADD:
10132 case BINOP_SUB:
10133 case BINOP_MUL:
10134 case BINOP_DIV:
10135 case BINOP_REM:
10136 case BINOP_MOD:
10137 case BINOP_LSH:
10138 case BINOP_RSH:
10139 case BINOP_LOGICAL_AND:
10140 case BINOP_LOGICAL_OR:
10141 case BINOP_BITWISE_AND:
10142 case BINOP_BITWISE_IOR:
10143 case BINOP_BITWISE_XOR:
10144 case BINOP_EQUAL:
10145 case BINOP_NOTEQUAL:
10146 case BINOP_LESS:
10147 case BINOP_GTR:
10148 case BINOP_LEQ:
10149 case BINOP_GEQ:
10150 case BINOP_REPEAT:
10151 case BINOP_COMMA:
10152 case BINOP_EXP:
10153 case BINOP_MIN:
10154 case BINOP_MAX:
10155 case BINOP_INTDIV:
10156 case BINOP_CONCAT:
65d79d4b
SDJ
10157 case TERNOP_COND:
10158 case TERNOP_SLICE:
65d79d4b
SDJ
10159
10160 case OP_LONG:
10161 case OP_DOUBLE:
10162 case OP_DECFLOAT:
10163 case OP_LAST:
10164 case OP_COMPLEX:
10165 case OP_STRING:
65d79d4b
SDJ
10166 case OP_ARRAY:
10167 case OP_TYPE:
608b4967
TT
10168 case OP_TYPEOF:
10169 case OP_DECLTYPE:
6e72ca20 10170 case OP_TYPEID:
65d79d4b
SDJ
10171 case OP_NAME:
10172 case OP_OBJC_NSSTRING:
10173
10174 case UNOP_NEG:
10175 case UNOP_LOGICAL_NOT:
10176 case UNOP_COMPLEMENT:
10177 case UNOP_ADDR:
10178 case UNOP_HIGH:
aeaa2474 10179 case UNOP_CAST:
9eaf6705
TT
10180
10181 case UNOP_CAST_TYPE:
10182 case UNOP_REINTERPRET_CAST:
10183 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10184 /* Unary, binary and ternary operators: We have to check
10185 their operands. If they are constant, then so is the
10186 result of that operation. For instance, if A and B are
10187 determined to be constants, then so is "A + B".
10188
10189 UNOP_IND is one exception to the rule above, because the
10190 value of *ADDR is not necessarily a constant, even when
10191 ADDR is. */
65d79d4b
SDJ
10192 break;
10193
10194 case OP_VAR_VALUE:
10195 /* Check whether the associated symbol is a constant.
4a64f543 10196
65d79d4b 10197 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10198 possible that a buggy compiler could mark a variable as
10199 constant even when it is not, and TYPE_CONST would return
10200 true in this case, while SYMBOL_CLASS wouldn't.
10201
10202 We also have to check for function symbols because they
10203 are always constant. */
65d79d4b
SDJ
10204 {
10205 struct symbol *s = exp->elts[i + 2].symbol;
10206
10207 if (SYMBOL_CLASS (s) != LOC_BLOCK
10208 && SYMBOL_CLASS (s) != LOC_CONST
10209 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10210 return 0;
10211 break;
10212 }
10213
10214 /* The default action is to return 0 because we are using
10215 the optimistic approach here: If we don't know something,
10216 then it is not a constant. */
10217 default:
10218 return 0;
10219 }
10220 }
10221
10222 return 1;
10223}
10224
c1fc2657 10225/* Watchpoint destructor. */
3a5c3e22 10226
c1fc2657 10227watchpoint::~watchpoint ()
3a5c3e22 10228{
c1fc2657
SM
10229 xfree (this->exp_string);
10230 xfree (this->exp_string_reparse);
10231 value_free (this->val);
3a5c3e22
PA
10232}
10233
348d480f
PA
10234/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10235
10236static void
10237re_set_watchpoint (struct breakpoint *b)
10238{
3a5c3e22
PA
10239 struct watchpoint *w = (struct watchpoint *) b;
10240
348d480f
PA
10241 /* Watchpoint can be either on expression using entirely global
10242 variables, or it can be on local variables.
10243
10244 Watchpoints of the first kind are never auto-deleted, and even
10245 persist across program restarts. Since they can use variables
10246 from shared libraries, we need to reparse expression as libraries
10247 are loaded and unloaded.
10248
10249 Watchpoints on local variables can also change meaning as result
10250 of solib event. For example, if a watchpoint uses both a local
10251 and a global variables in expression, it's a local watchpoint,
10252 but unloading of a shared library will make the expression
10253 invalid. This is not a very common use case, but we still
10254 re-evaluate expression, to avoid surprises to the user.
10255
10256 Note that for local watchpoints, we re-evaluate it only if
10257 watchpoints frame id is still valid. If it's not, it means the
10258 watchpoint is out of scope and will be deleted soon. In fact,
10259 I'm not sure we'll ever be called in this case.
10260
10261 If a local watchpoint's frame id is still valid, then
3a5c3e22 10262 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10263
3a5c3e22
PA
10264 Don't do anything about disabled watchpoints, since they will be
10265 reevaluated again when enabled. */
10266 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10267}
10268
77b06cd7
TJB
10269/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10270
10271static int
10272insert_watchpoint (struct bp_location *bl)
10273{
3a5c3e22
PA
10274 struct watchpoint *w = (struct watchpoint *) bl->owner;
10275 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10276
10277 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10278 w->cond_exp.get ());
77b06cd7
TJB
10279}
10280
10281/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10282
10283static int
73971819 10284remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10285{
3a5c3e22
PA
10286 struct watchpoint *w = (struct watchpoint *) bl->owner;
10287 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10288
10289 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10290 w->cond_exp.get ());
e09342b5
TJB
10291}
10292
e09342b5 10293static int
348d480f 10294breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10295 struct address_space *aspace, CORE_ADDR bp_addr,
10296 const struct target_waitstatus *ws)
e09342b5 10297{
348d480f 10298 struct breakpoint *b = bl->owner;
3a5c3e22 10299 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10300
348d480f
PA
10301 /* Continuable hardware watchpoints are treated as non-existent if the
10302 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10303 some data address). Otherwise gdb won't stop on a break instruction
10304 in the code (not from a breakpoint) when a hardware watchpoint has
10305 been defined. Also skip watchpoints which we know did not trigger
10306 (did not match the data address). */
10307 if (is_hardware_watchpoint (b)
3a5c3e22 10308 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10309 return 0;
9c06b0b4 10310
348d480f 10311 return 1;
9c06b0b4
TJB
10312}
10313
348d480f
PA
10314static void
10315check_status_watchpoint (bpstat bs)
9c06b0b4 10316{
348d480f 10317 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10318
348d480f 10319 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10320}
10321
10322/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10323 hardware watchpoints. */
9c06b0b4
TJB
10324
10325static int
348d480f 10326resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10327{
3a5c3e22
PA
10328 struct watchpoint *w = (struct watchpoint *) bl->owner;
10329 int length = w->exact? 1 : bl->length;
348d480f
PA
10330
10331 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10332}
10333
10334/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10335 hardware watchpoints. */
9c06b0b4
TJB
10336
10337static int
348d480f 10338works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10339{
efa80663
PA
10340 /* Read and access watchpoints only work with hardware support. */
10341 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10342}
10343
9c06b0b4 10344static enum print_stop_action
348d480f 10345print_it_watchpoint (bpstat bs)
9c06b0b4 10346{
348d480f 10347 struct breakpoint *b;
348d480f 10348 enum print_stop_action result;
3a5c3e22 10349 struct watchpoint *w;
79a45e25 10350 struct ui_out *uiout = current_uiout;
348d480f
PA
10351
10352 gdb_assert (bs->bp_location_at != NULL);
10353
348d480f 10354 b = bs->breakpoint_at;
3a5c3e22 10355 w = (struct watchpoint *) b;
348d480f 10356
f303dbd6
PA
10357 annotate_watchpoint (b->number);
10358 maybe_print_thread_hit_breakpoint (uiout);
10359
d7e74731
PA
10360 string_file stb;
10361
76f9c9cf 10362 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10363 switch (b->type)
10364 {
348d480f 10365 case bp_watchpoint:
9c06b0b4 10366 case bp_hardware_watchpoint:
112e8700
SM
10367 if (uiout->is_mi_like_p ())
10368 uiout->field_string
10369 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10370 mention (b);
76f9c9cf 10371 tuple_emitter.emplace (uiout, "value");
112e8700 10372 uiout->text ("\nOld value = ");
d7e74731 10373 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10374 uiout->field_stream ("old", stb);
10375 uiout->text ("\nNew value = ");
d7e74731 10376 watchpoint_value_print (w->val, &stb);
112e8700
SM
10377 uiout->field_stream ("new", stb);
10378 uiout->text ("\n");
348d480f
PA
10379 /* More than one watchpoint may have been triggered. */
10380 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10381 break;
10382
10383 case bp_read_watchpoint:
112e8700
SM
10384 if (uiout->is_mi_like_p ())
10385 uiout->field_string
10386 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10387 mention (b);
76f9c9cf 10388 tuple_emitter.emplace (uiout, "value");
112e8700 10389 uiout->text ("\nValue = ");
d7e74731 10390 watchpoint_value_print (w->val, &stb);
112e8700
SM
10391 uiout->field_stream ("value", stb);
10392 uiout->text ("\n");
348d480f 10393 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10394 break;
10395
10396 case bp_access_watchpoint:
348d480f
PA
10397 if (bs->old_val != NULL)
10398 {
112e8700
SM
10399 if (uiout->is_mi_like_p ())
10400 uiout->field_string
10401 ("reason",
348d480f
PA
10402 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10403 mention (b);
76f9c9cf 10404 tuple_emitter.emplace (uiout, "value");
112e8700 10405 uiout->text ("\nOld value = ");
d7e74731 10406 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10407 uiout->field_stream ("old", stb);
10408 uiout->text ("\nNew value = ");
348d480f
PA
10409 }
10410 else
10411 {
10412 mention (b);
112e8700
SM
10413 if (uiout->is_mi_like_p ())
10414 uiout->field_string
10415 ("reason",
348d480f 10416 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10417 tuple_emitter.emplace (uiout, "value");
112e8700 10418 uiout->text ("\nValue = ");
348d480f 10419 }
d7e74731 10420 watchpoint_value_print (w->val, &stb);
112e8700
SM
10421 uiout->field_stream ("new", stb);
10422 uiout->text ("\n");
348d480f 10423 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10424 break;
10425 default:
348d480f 10426 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10427 }
10428
348d480f
PA
10429 return result;
10430}
10431
10432/* Implement the "print_mention" breakpoint_ops method for hardware
10433 watchpoints. */
10434
10435static void
10436print_mention_watchpoint (struct breakpoint *b)
10437{
3a5c3e22 10438 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10439 struct ui_out *uiout = current_uiout;
46b9c129 10440 const char *tuple_name;
348d480f
PA
10441
10442 switch (b->type)
10443 {
10444 case bp_watchpoint:
112e8700 10445 uiout->text ("Watchpoint ");
46b9c129 10446 tuple_name = "wpt";
348d480f
PA
10447 break;
10448 case bp_hardware_watchpoint:
112e8700 10449 uiout->text ("Hardware watchpoint ");
46b9c129 10450 tuple_name = "wpt";
348d480f
PA
10451 break;
10452 case bp_read_watchpoint:
112e8700 10453 uiout->text ("Hardware read watchpoint ");
46b9c129 10454 tuple_name = "hw-rwpt";
348d480f
PA
10455 break;
10456 case bp_access_watchpoint:
112e8700 10457 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10458 tuple_name = "hw-awpt";
348d480f
PA
10459 break;
10460 default:
10461 internal_error (__FILE__, __LINE__,
10462 _("Invalid hardware watchpoint type."));
10463 }
10464
46b9c129 10465 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10466 uiout->field_int ("number", b->number);
10467 uiout->text (": ");
10468 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10469}
10470
10471/* Implement the "print_recreate" breakpoint_ops method for
10472 watchpoints. */
10473
10474static void
10475print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10476{
3a5c3e22
PA
10477 struct watchpoint *w = (struct watchpoint *) b;
10478
348d480f
PA
10479 switch (b->type)
10480 {
10481 case bp_watchpoint:
10482 case bp_hardware_watchpoint:
10483 fprintf_unfiltered (fp, "watch");
10484 break;
10485 case bp_read_watchpoint:
10486 fprintf_unfiltered (fp, "rwatch");
10487 break;
10488 case bp_access_watchpoint:
10489 fprintf_unfiltered (fp, "awatch");
10490 break;
10491 default:
10492 internal_error (__FILE__, __LINE__,
10493 _("Invalid watchpoint type."));
10494 }
10495
3a5c3e22 10496 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10497 print_recreate_thread (b, fp);
348d480f
PA
10498}
10499
427cd150
TT
10500/* Implement the "explains_signal" breakpoint_ops method for
10501 watchpoints. */
10502
47591c29 10503static int
427cd150
TT
10504explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10505{
10506 /* A software watchpoint cannot cause a signal other than
10507 GDB_SIGNAL_TRAP. */
10508 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10509 return 0;
427cd150 10510
47591c29 10511 return 1;
427cd150
TT
10512}
10513
348d480f
PA
10514/* The breakpoint_ops structure to be used in hardware watchpoints. */
10515
2060206e 10516static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10517
10518/* Implement the "insert" breakpoint_ops method for
10519 masked hardware watchpoints. */
10520
10521static int
10522insert_masked_watchpoint (struct bp_location *bl)
10523{
3a5c3e22
PA
10524 struct watchpoint *w = (struct watchpoint *) bl->owner;
10525
10526 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10527 bl->watchpoint_type);
10528}
10529
10530/* Implement the "remove" breakpoint_ops method for
10531 masked hardware watchpoints. */
10532
10533static int
73971819 10534remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10535{
3a5c3e22
PA
10536 struct watchpoint *w = (struct watchpoint *) bl->owner;
10537
10538 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10539 bl->watchpoint_type);
10540}
10541
10542/* Implement the "resources_needed" breakpoint_ops method for
10543 masked hardware watchpoints. */
10544
10545static int
10546resources_needed_masked_watchpoint (const struct bp_location *bl)
10547{
3a5c3e22
PA
10548 struct watchpoint *w = (struct watchpoint *) bl->owner;
10549
10550 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10551}
10552
10553/* Implement the "works_in_software_mode" breakpoint_ops method for
10554 masked hardware watchpoints. */
10555
10556static int
10557works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10558{
10559 return 0;
10560}
10561
10562/* Implement the "print_it" breakpoint_ops method for
10563 masked hardware watchpoints. */
10564
10565static enum print_stop_action
10566print_it_masked_watchpoint (bpstat bs)
10567{
10568 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10569 struct ui_out *uiout = current_uiout;
348d480f
PA
10570
10571 /* Masked watchpoints have only one location. */
10572 gdb_assert (b->loc && b->loc->next == NULL);
10573
f303dbd6
PA
10574 annotate_watchpoint (b->number);
10575 maybe_print_thread_hit_breakpoint (uiout);
10576
348d480f
PA
10577 switch (b->type)
10578 {
10579 case bp_hardware_watchpoint:
112e8700
SM
10580 if (uiout->is_mi_like_p ())
10581 uiout->field_string
10582 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10583 break;
10584
10585 case bp_read_watchpoint:
112e8700
SM
10586 if (uiout->is_mi_like_p ())
10587 uiout->field_string
10588 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10589 break;
10590
10591 case bp_access_watchpoint:
112e8700
SM
10592 if (uiout->is_mi_like_p ())
10593 uiout->field_string
10594 ("reason",
348d480f
PA
10595 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10596 break;
10597 default:
10598 internal_error (__FILE__, __LINE__,
10599 _("Invalid hardware watchpoint type."));
10600 }
10601
10602 mention (b);
112e8700 10603 uiout->text (_("\n\
9c06b0b4
TJB
10604Check the underlying instruction at PC for the memory\n\
10605address and value which triggered this watchpoint.\n"));
112e8700 10606 uiout->text ("\n");
9c06b0b4
TJB
10607
10608 /* More than one watchpoint may have been triggered. */
10609 return PRINT_UNKNOWN;
10610}
10611
10612/* Implement the "print_one_detail" breakpoint_ops method for
10613 masked hardware watchpoints. */
10614
10615static void
10616print_one_detail_masked_watchpoint (const struct breakpoint *b,
10617 struct ui_out *uiout)
10618{
3a5c3e22
PA
10619 struct watchpoint *w = (struct watchpoint *) b;
10620
9c06b0b4
TJB
10621 /* Masked watchpoints have only one location. */
10622 gdb_assert (b->loc && b->loc->next == NULL);
10623
112e8700
SM
10624 uiout->text ("\tmask ");
10625 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10626 uiout->text ("\n");
9c06b0b4
TJB
10627}
10628
10629/* Implement the "print_mention" breakpoint_ops method for
10630 masked hardware watchpoints. */
10631
10632static void
10633print_mention_masked_watchpoint (struct breakpoint *b)
10634{
3a5c3e22 10635 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10636 struct ui_out *uiout = current_uiout;
46b9c129 10637 const char *tuple_name;
9c06b0b4
TJB
10638
10639 switch (b->type)
10640 {
10641 case bp_hardware_watchpoint:
112e8700 10642 uiout->text ("Masked hardware watchpoint ");
46b9c129 10643 tuple_name = "wpt";
9c06b0b4
TJB
10644 break;
10645 case bp_read_watchpoint:
112e8700 10646 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10647 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10648 break;
10649 case bp_access_watchpoint:
112e8700 10650 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10651 tuple_name = "hw-awpt";
9c06b0b4
TJB
10652 break;
10653 default:
10654 internal_error (__FILE__, __LINE__,
10655 _("Invalid hardware watchpoint type."));
10656 }
10657
46b9c129 10658 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10659 uiout->field_int ("number", b->number);
10660 uiout->text (": ");
10661 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10662}
10663
10664/* Implement the "print_recreate" breakpoint_ops method for
10665 masked hardware watchpoints. */
10666
10667static void
10668print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10669{
3a5c3e22 10670 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10671 char tmp[40];
10672
10673 switch (b->type)
10674 {
10675 case bp_hardware_watchpoint:
10676 fprintf_unfiltered (fp, "watch");
10677 break;
10678 case bp_read_watchpoint:
10679 fprintf_unfiltered (fp, "rwatch");
10680 break;
10681 case bp_access_watchpoint:
10682 fprintf_unfiltered (fp, "awatch");
10683 break;
10684 default:
10685 internal_error (__FILE__, __LINE__,
10686 _("Invalid hardware watchpoint type."));
10687 }
10688
3a5c3e22
PA
10689 sprintf_vma (tmp, w->hw_wp_mask);
10690 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10691 print_recreate_thread (b, fp);
9c06b0b4
TJB
10692}
10693
10694/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10695
2060206e 10696static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10697
10698/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10699
10700static int
10701is_masked_watchpoint (const struct breakpoint *b)
10702{
10703 return b->ops == &masked_watchpoint_breakpoint_ops;
10704}
10705
53a5351d
JM
10706/* accessflag: hw_write: watch write,
10707 hw_read: watch read,
10708 hw_access: watch access (read or write) */
c906108c 10709static void
bbc13ae3 10710watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10711 int just_location, int internal)
c906108c 10712{
c1fc2657 10713 struct breakpoint *scope_breakpoint = NULL;
270140bd 10714 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10715 struct value *val, *mark, *result;
bb9d5f81 10716 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10717 const char *exp_start = NULL;
10718 const char *exp_end = NULL;
10719 const char *tok, *end_tok;
9c06b0b4 10720 int toklen = -1;
bbc13ae3
KS
10721 const char *cond_start = NULL;
10722 const char *cond_end = NULL;
c906108c 10723 enum bptype bp_type;
37e4754d 10724 int thread = -1;
0cf6dd15 10725 int pc = 0;
9c06b0b4
TJB
10726 /* Flag to indicate whether we are going to use masks for
10727 the hardware watchpoint. */
10728 int use_mask = 0;
10729 CORE_ADDR mask = 0;
bbc13ae3
KS
10730 char *expression;
10731 struct cleanup *back_to;
c906108c 10732
37e4754d
LM
10733 /* Make sure that we actually have parameters to parse. */
10734 if (arg != NULL && arg[0] != '\0')
10735 {
bbc13ae3
KS
10736 const char *value_start;
10737
10738 exp_end = arg + strlen (arg);
37e4754d 10739
9c06b0b4
TJB
10740 /* Look for "parameter value" pairs at the end
10741 of the arguments string. */
bbc13ae3 10742 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10743 {
10744 /* Skip whitespace at the end of the argument list. */
10745 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10746 tok--;
10747
10748 /* Find the beginning of the last token.
10749 This is the value of the parameter. */
10750 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10751 tok--;
10752 value_start = tok + 1;
10753
10754 /* Skip whitespace. */
10755 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10756 tok--;
10757
10758 end_tok = tok;
10759
10760 /* Find the beginning of the second to last token.
10761 This is the parameter itself. */
10762 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10763 tok--;
10764 tok++;
10765 toklen = end_tok - tok + 1;
10766
61012eef 10767 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10768 {
5d5658a1 10769 struct thread_info *thr;
9c06b0b4
TJB
10770 /* At this point we've found a "thread" token, which means
10771 the user is trying to set a watchpoint that triggers
10772 only in a specific thread. */
5d5658a1 10773 const char *endp;
37e4754d 10774
9c06b0b4
TJB
10775 if (thread != -1)
10776 error(_("You can specify only one thread."));
37e4754d 10777
9c06b0b4 10778 /* Extract the thread ID from the next token. */
5d5658a1 10779 thr = parse_thread_id (value_start, &endp);
37e4754d 10780
5d5658a1 10781 /* Check if the user provided a valid thread ID. */
9c06b0b4 10782 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10783 invalid_thread_id_error (value_start);
9c06b0b4 10784
5d5658a1 10785 thread = thr->global_num;
9c06b0b4 10786 }
61012eef 10787 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10788 {
10789 /* We've found a "mask" token, which means the user wants to
10790 create a hardware watchpoint that is going to have the mask
10791 facility. */
10792 struct value *mask_value, *mark;
37e4754d 10793
9c06b0b4
TJB
10794 if (use_mask)
10795 error(_("You can specify only one mask."));
37e4754d 10796
9c06b0b4 10797 use_mask = just_location = 1;
37e4754d 10798
9c06b0b4
TJB
10799 mark = value_mark ();
10800 mask_value = parse_to_comma_and_eval (&value_start);
10801 mask = value_as_address (mask_value);
10802 value_free_to_mark (mark);
10803 }
10804 else
10805 /* We didn't recognize what we found. We should stop here. */
10806 break;
37e4754d 10807
9c06b0b4
TJB
10808 /* Truncate the string and get rid of the "parameter value" pair before
10809 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10810 exp_end = tok;
9c06b0b4 10811 }
37e4754d 10812 }
bbc13ae3
KS
10813 else
10814 exp_end = arg;
37e4754d 10815
bbc13ae3
KS
10816 /* Parse the rest of the arguments. From here on out, everything
10817 is in terms of a newly allocated string instead of the original
10818 ARG. */
c906108c 10819 innermost_block = NULL;
bbc13ae3
KS
10820 expression = savestring (arg, exp_end - arg);
10821 back_to = make_cleanup (xfree, expression);
10822 exp_start = arg = expression;
4d01a485 10823 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10824 exp_end = arg;
fa8a61dc
TT
10825 /* Remove trailing whitespace from the expression before saving it.
10826 This makes the eventual display of the expression string a bit
10827 prettier. */
10828 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10829 --exp_end;
10830
65d79d4b 10831 /* Checking if the expression is not constant. */
4d01a485 10832 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10833 {
10834 int len;
10835
10836 len = exp_end - exp_start;
10837 while (len > 0 && isspace (exp_start[len - 1]))
10838 len--;
10839 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10840 }
10841
c906108c
SS
10842 exp_valid_block = innermost_block;
10843 mark = value_mark ();
4d01a485 10844 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10845
bb9d5f81
PP
10846 if (val != NULL && just_location)
10847 {
10848 saved_bitpos = value_bitpos (val);
10849 saved_bitsize = value_bitsize (val);
10850 }
10851
06a64a0b
TT
10852 if (just_location)
10853 {
9c06b0b4
TJB
10854 int ret;
10855
06a64a0b 10856 exp_valid_block = NULL;
a1442452 10857 val = value_addr (result);
06a64a0b
TT
10858 release_value (val);
10859 value_free_to_mark (mark);
9c06b0b4
TJB
10860
10861 if (use_mask)
10862 {
10863 ret = target_masked_watch_num_registers (value_as_address (val),
10864 mask);
10865 if (ret == -1)
10866 error (_("This target does not support masked watchpoints."));
10867 else if (ret == -2)
10868 error (_("Invalid mask or memory region."));
10869 }
06a64a0b
TT
10870 }
10871 else if (val != NULL)
fa4727a6 10872 release_value (val);
c906108c 10873
f1735a53
TT
10874 tok = skip_spaces (arg);
10875 end_tok = skip_to_space (tok);
c906108c
SS
10876
10877 toklen = end_tok - tok;
10878 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10879 {
60e1c644 10880 innermost_block = NULL;
c906108c 10881 tok = cond_start = end_tok + 1;
4d01a485 10882 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10883
10884 /* The watchpoint expression may not be local, but the condition
10885 may still be. E.g.: `watch global if local > 0'. */
10886 cond_exp_valid_block = innermost_block;
10887
c906108c
SS
10888 cond_end = tok;
10889 }
10890 if (*tok)
8a3fe4f8 10891 error (_("Junk at end of command."));
c906108c 10892
441d7c93
PA
10893 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10894
10895 /* Save this because create_internal_breakpoint below invalidates
10896 'wp_frame'. */
10897 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10898
10899 /* If the expression is "local", then set up a "watchpoint scope"
10900 breakpoint at the point where we've left the scope of the watchpoint
10901 expression. Create the scope breakpoint before the watchpoint, so
10902 that we will encounter it first in bpstat_stop_status. */
441d7c93 10903 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10904 {
441d7c93
PA
10905 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10906
10907 if (frame_id_p (caller_frame_id))
edb3359d 10908 {
441d7c93
PA
10909 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10910 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10911
edb3359d 10912 scope_breakpoint
441d7c93 10913 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10914 bp_watchpoint_scope,
10915 &momentary_breakpoint_ops);
d983da9c 10916
441d7c93
PA
10917 /* create_internal_breakpoint could invalidate WP_FRAME. */
10918 wp_frame = NULL;
10919
edb3359d 10920 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10921
edb3359d
DJ
10922 /* Automatically delete the breakpoint when it hits. */
10923 scope_breakpoint->disposition = disp_del;
d983da9c 10924
edb3359d 10925 /* Only break in the proper frame (help with recursion). */
441d7c93 10926 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10927
edb3359d 10928 /* Set the address at which we will stop. */
441d7c93
PA
10929 scope_breakpoint->loc->gdbarch = caller_arch;
10930 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10931 scope_breakpoint->loc->address
a6d9a66e
UW
10932 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10933 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10934 scope_breakpoint->type);
10935 }
d983da9c
DJ
10936 }
10937
e8369a73
AB
10938 /* Now set up the breakpoint. We create all watchpoints as hardware
10939 watchpoints here even if hardware watchpoints are turned off, a call
10940 to update_watchpoint later in this function will cause the type to
10941 drop back to bp_watchpoint (software watchpoint) if required. */
10942
10943 if (accessflag == hw_read)
10944 bp_type = bp_read_watchpoint;
10945 else if (accessflag == hw_access)
10946 bp_type = bp_access_watchpoint;
10947 else
10948 bp_type = bp_hardware_watchpoint;
3a5c3e22 10949
b270e6f9 10950 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10951
348d480f 10952 if (use_mask)
b270e6f9 10953 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10954 &masked_watchpoint_breakpoint_ops);
348d480f 10955 else
b270e6f9 10956 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10957 &watchpoint_breakpoint_ops);
c1fc2657
SM
10958 w->thread = thread;
10959 w->disposition = disp_donttouch;
10960 w->pspace = current_program_space;
b22e99fd 10961 w->exp = std::move (exp);
3a5c3e22
PA
10962 w->exp_valid_block = exp_valid_block;
10963 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10964 if (just_location)
10965 {
10966 struct type *t = value_type (val);
10967 CORE_ADDR addr = value_as_address (val);
06a64a0b 10968
43cc5389
TT
10969 w->exp_string_reparse
10970 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10971
3a5c3e22 10972 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10973 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10974 }
10975 else
3a5c3e22 10976 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10977
10978 if (use_mask)
10979 {
3a5c3e22 10980 w->hw_wp_mask = mask;
9c06b0b4
TJB
10981 }
10982 else
10983 {
3a5c3e22 10984 w->val = val;
bb9d5f81
PP
10985 w->val_bitpos = saved_bitpos;
10986 w->val_bitsize = saved_bitsize;
3a5c3e22 10987 w->val_valid = 1;
9c06b0b4 10988 }
77b06cd7 10989
c906108c 10990 if (cond_start)
c1fc2657 10991 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10992 else
c1fc2657 10993 w->cond_string = 0;
c5aa993b 10994
441d7c93 10995 if (frame_id_p (watchpoint_frame))
f6bc2008 10996 {
441d7c93 10997 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10998 w->watchpoint_thread = inferior_ptid;
f6bc2008 10999 }
c906108c 11000 else
f6bc2008 11001 {
3a5c3e22
PA
11002 w->watchpoint_frame = null_frame_id;
11003 w->watchpoint_thread = null_ptid;
f6bc2008 11004 }
c906108c 11005
d983da9c 11006 if (scope_breakpoint != NULL)
c906108c 11007 {
d983da9c
DJ
11008 /* The scope breakpoint is related to the watchpoint. We will
11009 need to act on them together. */
c1fc2657 11010 w->related_breakpoint = scope_breakpoint;
b270e6f9 11011 scope_breakpoint->related_breakpoint = w.get ();
c906108c 11012 }
d983da9c 11013
06a64a0b
TT
11014 if (!just_location)
11015 value_free_to_mark (mark);
2d134ed3 11016
b270e6f9
TT
11017 /* Finally update the new watchpoint. This creates the locations
11018 that should be inserted. */
11019 update_watchpoint (w.get (), 1);
a9634178 11020
b270e6f9 11021 install_breakpoint (internal, std::move (w), 1);
bbc13ae3 11022 do_cleanups (back_to);
c906108c
SS
11023}
11024
e09342b5 11025/* Return count of debug registers needed to watch the given expression.
e09342b5 11026 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11027
c906108c 11028static int
a9634178 11029can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11030{
11031 int found_memory_cnt = 0;
2e70b7b9 11032 struct value *head = v;
c906108c
SS
11033
11034 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11035 if (!can_use_hw_watchpoints)
c906108c 11036 return 0;
c5aa993b 11037
5c44784c
JM
11038 /* Make sure that the value of the expression depends only upon
11039 memory contents, and values computed from them within GDB. If we
11040 find any register references or function calls, we can't use a
11041 hardware watchpoint.
11042
11043 The idea here is that evaluating an expression generates a series
11044 of values, one holding the value of every subexpression. (The
11045 expression a*b+c has five subexpressions: a, b, a*b, c, and
11046 a*b+c.) GDB's values hold almost enough information to establish
11047 the criteria given above --- they identify memory lvalues,
11048 register lvalues, computed values, etcetera. So we can evaluate
11049 the expression, and then scan the chain of values that leaves
11050 behind to decide whether we can detect any possible change to the
11051 expression's final value using only hardware watchpoints.
11052
11053 However, I don't think that the values returned by inferior
11054 function calls are special in any way. So this function may not
11055 notice that an expression involving an inferior function call
11056 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11057 for (; v; v = value_next (v))
c906108c 11058 {
5c44784c 11059 if (VALUE_LVAL (v) == lval_memory)
c906108c 11060 {
8464be76
DJ
11061 if (v != head && value_lazy (v))
11062 /* A lazy memory lvalue in the chain is one that GDB never
11063 needed to fetch; we either just used its address (e.g.,
11064 `a' in `a.b') or we never needed it at all (e.g., `a'
11065 in `a,b'). This doesn't apply to HEAD; if that is
11066 lazy then it was not readable, but watch it anyway. */
5c44784c 11067 ;
53a5351d 11068 else
5c44784c
JM
11069 {
11070 /* Ahh, memory we actually used! Check if we can cover
11071 it with hardware watchpoints. */
df407dfe 11072 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11073
11074 /* We only watch structs and arrays if user asked for it
11075 explicitly, never if they just happen to appear in a
11076 middle of some value chain. */
11077 if (v == head
11078 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11079 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11080 {
42ae5230 11081 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11082 int len;
11083 int num_regs;
11084
a9634178 11085 len = (target_exact_watchpoints
e09342b5
TJB
11086 && is_scalar_type_recursive (vtype))?
11087 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11088
e09342b5
TJB
11089 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11090 if (!num_regs)
2e70b7b9
MS
11091 return 0;
11092 else
e09342b5 11093 found_memory_cnt += num_regs;
2e70b7b9 11094 }
5c44784c 11095 }
c5aa993b 11096 }
5086187c
AC
11097 else if (VALUE_LVAL (v) != not_lval
11098 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11099 return 0; /* These are values from the history (e.g., $1). */
5086187c 11100 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11101 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11102 }
11103
11104 /* The expression itself looks suitable for using a hardware
11105 watchpoint, but give the target machine a chance to reject it. */
11106 return found_memory_cnt;
11107}
11108
8b93c638 11109void
f2fc3015 11110watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11111{
84f4c1fe 11112 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11113}
11114
06a64a0b
TT
11115/* A helper function that looks for the "-location" argument and then
11116 calls watch_command_1. */
11117
11118static void
11119watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11120{
11121 int just_location = 0;
11122
11123 if (arg
11124 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11125 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11126 {
e9cafbcc 11127 arg = skip_spaces (arg);
06a64a0b
TT
11128 just_location = 1;
11129 }
11130
84f4c1fe 11131 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11132}
8926118c 11133
c5aa993b 11134static void
fba45db2 11135watch_command (char *arg, int from_tty)
c906108c 11136{
06a64a0b 11137 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11138}
11139
8b93c638 11140void
f2fc3015 11141rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11142{
84f4c1fe 11143 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11144}
8926118c 11145
c5aa993b 11146static void
fba45db2 11147rwatch_command (char *arg, int from_tty)
c906108c 11148{
06a64a0b 11149 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11150}
11151
8b93c638 11152void
f2fc3015 11153awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11154{
84f4c1fe 11155 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11156}
8926118c 11157
c5aa993b 11158static void
fba45db2 11159awatch_command (char *arg, int from_tty)
c906108c 11160{
06a64a0b 11161 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11162}
c906108c 11163\f
c5aa993b 11164
cfc31633
PA
11165/* Data for the FSM that manages the until(location)/advance commands
11166 in infcmd.c. Here because it uses the mechanisms of
11167 breakpoints. */
c906108c 11168
cfc31633 11169struct until_break_fsm
bfec99b2 11170{
cfc31633
PA
11171 /* The base class. */
11172 struct thread_fsm thread_fsm;
11173
11174 /* The thread that as current when the command was executed. */
11175 int thread;
11176
11177 /* The breakpoint set at the destination location. */
11178 struct breakpoint *location_breakpoint;
11179
11180 /* Breakpoint set at the return address in the caller frame. May be
11181 NULL. */
11182 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11183};
11184
8980e177
PA
11185static void until_break_fsm_clean_up (struct thread_fsm *self,
11186 struct thread_info *thread);
11187static int until_break_fsm_should_stop (struct thread_fsm *self,
11188 struct thread_info *thread);
cfc31633
PA
11189static enum async_reply_reason
11190 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11191
11192/* until_break_fsm's vtable. */
11193
11194static struct thread_fsm_ops until_break_fsm_ops =
11195{
11196 NULL, /* dtor */
11197 until_break_fsm_clean_up,
11198 until_break_fsm_should_stop,
11199 NULL, /* return_value */
11200 until_break_fsm_async_reply_reason,
11201};
11202
11203/* Allocate a new until_break_command_fsm. */
11204
11205static struct until_break_fsm *
8980e177 11206new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11207 struct breakpoint *location_breakpoint,
11208 struct breakpoint *caller_breakpoint)
11209{
11210 struct until_break_fsm *sm;
11211
11212 sm = XCNEW (struct until_break_fsm);
8980e177 11213 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11214
11215 sm->thread = thread;
11216 sm->location_breakpoint = location_breakpoint;
11217 sm->caller_breakpoint = caller_breakpoint;
11218
11219 return sm;
11220}
11221
11222/* Implementation of the 'should_stop' FSM method for the
11223 until(location)/advance commands. */
11224
11225static int
8980e177
PA
11226until_break_fsm_should_stop (struct thread_fsm *self,
11227 struct thread_info *tp)
cfc31633
PA
11228{
11229 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11230
11231 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11232 sm->location_breakpoint) != NULL
11233 || (sm->caller_breakpoint != NULL
11234 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11235 sm->caller_breakpoint) != NULL))
11236 thread_fsm_set_finished (self);
11237
11238 return 1;
11239}
11240
11241/* Implementation of the 'clean_up' FSM method for the
11242 until(location)/advance commands. */
11243
c2c6d25f 11244static void
8980e177
PA
11245until_break_fsm_clean_up (struct thread_fsm *self,
11246 struct thread_info *thread)
43ff13b4 11247{
cfc31633 11248 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11249
cfc31633
PA
11250 /* Clean up our temporary breakpoints. */
11251 if (sm->location_breakpoint != NULL)
11252 {
11253 delete_breakpoint (sm->location_breakpoint);
11254 sm->location_breakpoint = NULL;
11255 }
11256 if (sm->caller_breakpoint != NULL)
11257 {
11258 delete_breakpoint (sm->caller_breakpoint);
11259 sm->caller_breakpoint = NULL;
11260 }
11261 delete_longjmp_breakpoint (sm->thread);
11262}
11263
11264/* Implementation of the 'async_reply_reason' FSM method for the
11265 until(location)/advance commands. */
11266
11267static enum async_reply_reason
11268until_break_fsm_async_reply_reason (struct thread_fsm *self)
11269{
11270 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11271}
11272
c906108c 11273void
f2fc3015 11274until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11275{
8556afb4
PA
11276 struct frame_info *frame;
11277 struct gdbarch *frame_gdbarch;
11278 struct frame_id stack_frame_id;
11279 struct frame_id caller_frame_id;
cfc31633
PA
11280 struct breakpoint *location_breakpoint;
11281 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11282 struct cleanup *old_chain;
186c406b
TT
11283 int thread;
11284 struct thread_info *tp;
cfc31633 11285 struct until_break_fsm *sm;
c906108c 11286
70509625 11287 clear_proceed_status (0);
c906108c
SS
11288
11289 /* Set a breakpoint where the user wants it and at return from
4a64f543 11290 this function. */
c5aa993b 11291
ffc2605c 11292 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11293
6c5b2ebe
PA
11294 std::vector<symtab_and_line> sals
11295 = (last_displayed_sal_is_valid ()
11296 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11297 get_last_displayed_symtab (),
11298 get_last_displayed_line ())
11299 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11300 NULL, (struct symtab *) NULL, 0));
c5aa993b 11301
6c5b2ebe 11302 if (sals.size () != 1)
8a3fe4f8 11303 error (_("Couldn't get information on specified line."));
c5aa993b 11304
6c5b2ebe 11305 symtab_and_line &sal = sals[0];
c5aa993b 11306
c906108c 11307 if (*arg)
8a3fe4f8 11308 error (_("Junk at end of arguments."));
c5aa993b 11309
c906108c 11310 resolve_sal_pc (&sal);
c5aa993b 11311
186c406b 11312 tp = inferior_thread ();
5d5658a1 11313 thread = tp->global_num;
186c406b 11314
883bc8d1
PA
11315 old_chain = make_cleanup (null_cleanup, NULL);
11316
8556afb4
PA
11317 /* Note linespec handling above invalidates the frame chain.
11318 Installing a breakpoint also invalidates the frame chain (as it
11319 may need to switch threads), so do any frame handling before
11320 that. */
11321
11322 frame = get_selected_frame (NULL);
11323 frame_gdbarch = get_frame_arch (frame);
11324 stack_frame_id = get_stack_frame_id (frame);
11325 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11326
ae66c1fc
EZ
11327 /* Keep within the current frame, or in frames called by the current
11328 one. */
edb3359d 11329
883bc8d1 11330 if (frame_id_p (caller_frame_id))
c906108c 11331 {
883bc8d1 11332 struct symtab_and_line sal2;
cfc31633 11333 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11334
11335 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11336 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11337 caller_gdbarch = frame_unwind_caller_arch (frame);
11338 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11339 sal2,
11340 caller_frame_id,
11341 bp_until);
11342 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11343
883bc8d1 11344 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11345 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11346 }
c5aa993b 11347
c70a6932
JK
11348 /* set_momentary_breakpoint could invalidate FRAME. */
11349 frame = NULL;
11350
883bc8d1
PA
11351 if (anywhere)
11352 /* If the user told us to continue until a specified location,
11353 we don't specify a frame at which we need to stop. */
cfc31633
PA
11354 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11355 null_frame_id, bp_until);
883bc8d1
PA
11356 else
11357 /* Otherwise, specify the selected frame, because we want to stop
11358 only at the very same frame. */
cfc31633
PA
11359 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11360 stack_frame_id, bp_until);
11361 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11362
8980e177 11363 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11364 location_breakpoint, caller_breakpoint);
cfc31633 11365 tp->thread_fsm = &sm->thread_fsm;
f107f563 11366
cfc31633 11367 discard_cleanups (old_chain);
f107f563 11368
cfc31633 11369 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11370}
ae66c1fc 11371
c906108c
SS
11372/* This function attempts to parse an optional "if <cond>" clause
11373 from the arg string. If one is not found, it returns NULL.
c5aa993b 11374
c906108c
SS
11375 Else, it returns a pointer to the condition string. (It does not
11376 attempt to evaluate the string against a particular block.) And,
11377 it updates arg to point to the first character following the parsed
4a64f543 11378 if clause in the arg string. */
53a5351d 11379
63160a43
PA
11380const char *
11381ep_parse_optional_if_clause (const char **arg)
c906108c 11382{
63160a43 11383 const char *cond_string;
c5aa993b
JM
11384
11385 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11386 return NULL;
c5aa993b 11387
4a64f543 11388 /* Skip the "if" keyword. */
c906108c 11389 (*arg) += 2;
c5aa993b 11390
c906108c 11391 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11392 condition string. */
f1735a53 11393 *arg = skip_spaces (*arg);
c906108c 11394 cond_string = *arg;
c5aa993b 11395
4a64f543
MS
11396 /* Assume that the condition occupies the remainder of the arg
11397 string. */
c906108c 11398 (*arg) += strlen (cond_string);
c5aa993b 11399
c906108c
SS
11400 return cond_string;
11401}
c5aa993b 11402
c906108c
SS
11403/* Commands to deal with catching events, such as signals, exceptions,
11404 process start/exit, etc. */
c5aa993b
JM
11405
11406typedef enum
11407{
44feb3ce
TT
11408 catch_fork_temporary, catch_vfork_temporary,
11409 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11410}
11411catch_fork_kind;
11412
c906108c 11413static void
63160a43 11414catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11415 struct cmd_list_element *command)
c906108c 11416{
63160a43 11417 const char *arg = arg_entry;
a6d9a66e 11418 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11419 const char *cond_string = NULL;
44feb3ce
TT
11420 catch_fork_kind fork_kind;
11421 int tempflag;
11422
11423 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11424 tempflag = (fork_kind == catch_fork_temporary
11425 || fork_kind == catch_vfork_temporary);
c5aa993b 11426
44feb3ce
TT
11427 if (!arg)
11428 arg = "";
f1735a53 11429 arg = skip_spaces (arg);
c5aa993b 11430
c906108c 11431 /* The allowed syntax is:
c5aa993b
JM
11432 catch [v]fork
11433 catch [v]fork if <cond>
11434
4a64f543 11435 First, check if there's an if clause. */
c906108c 11436 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11437
c906108c 11438 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11439 error (_("Junk at end of arguments."));
c5aa993b 11440
c906108c 11441 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11442 and enable reporting of such events. */
c5aa993b
JM
11443 switch (fork_kind)
11444 {
44feb3ce
TT
11445 case catch_fork_temporary:
11446 case catch_fork_permanent:
a6d9a66e 11447 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11448 &catch_fork_breakpoint_ops);
c906108c 11449 break;
44feb3ce
TT
11450 case catch_vfork_temporary:
11451 case catch_vfork_permanent:
a6d9a66e 11452 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11453 &catch_vfork_breakpoint_ops);
c906108c 11454 break;
c5aa993b 11455 default:
8a3fe4f8 11456 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11457 break;
c5aa993b 11458 }
c906108c
SS
11459}
11460
11461static void
63160a43 11462catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11463 struct cmd_list_element *command)
c906108c 11464{
63160a43 11465 const char *arg = arg_entry;
a6d9a66e 11466 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11467 int tempflag;
63160a43 11468 const char *cond_string = NULL;
c906108c 11469
44feb3ce
TT
11470 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11471
11472 if (!arg)
11473 arg = "";
f1735a53 11474 arg = skip_spaces (arg);
c906108c
SS
11475
11476 /* The allowed syntax is:
c5aa993b
JM
11477 catch exec
11478 catch exec if <cond>
c906108c 11479
4a64f543 11480 First, check if there's an if clause. */
c906108c
SS
11481 cond_string = ep_parse_optional_if_clause (&arg);
11482
11483 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11484 error (_("Junk at end of arguments."));
c906108c 11485
b270e6f9
TT
11486 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11487 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11488 &catch_exec_breakpoint_ops);
11489 c->exec_pathname = NULL;
11490
b270e6f9 11491 install_breakpoint (0, std::move (c), 1);
c906108c 11492}
c5aa993b 11493
9ac4176b 11494void
28010a5d
PA
11495init_ada_exception_breakpoint (struct breakpoint *b,
11496 struct gdbarch *gdbarch,
11497 struct symtab_and_line sal,
f2fc3015 11498 const char *addr_string,
c0a91b2b 11499 const struct breakpoint_ops *ops,
28010a5d 11500 int tempflag,
349774ef 11501 int enabled,
28010a5d 11502 int from_tty)
f7f9143b 11503{
f7f9143b
JB
11504 if (from_tty)
11505 {
5af949e3
UW
11506 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11507 if (!loc_gdbarch)
11508 loc_gdbarch = gdbarch;
11509
6c95b8df
PA
11510 describe_other_breakpoints (loc_gdbarch,
11511 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11512 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11513 version for exception catchpoints, because two catchpoints
11514 used for different exception names will use the same address.
11515 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11516 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11517 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11518 the user what type of catchpoint it is. The above is good
11519 enough for now, though. */
11520 }
11521
28010a5d 11522 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11523
349774ef 11524 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11525 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11526 b->location = string_to_event_location (&addr_string,
11527 language_def (language_ada));
f7f9143b 11528 b->language = language_ada;
f7f9143b
JB
11529}
11530
c906108c 11531static void
fba45db2 11532catch_command (char *arg, int from_tty)
c906108c 11533{
44feb3ce 11534 error (_("Catch requires an event name."));
c906108c
SS
11535}
11536\f
11537
11538static void
fba45db2 11539tcatch_command (char *arg, int from_tty)
c906108c 11540{
44feb3ce 11541 error (_("Catch requires an event name."));
c906108c
SS
11542}
11543
8a2c437b
TT
11544/* A qsort comparison function that sorts breakpoints in order. */
11545
11546static int
11547compare_breakpoints (const void *a, const void *b)
11548{
9a3c8263 11549 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11550 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11551 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11552 uintptr_t ub = (uintptr_t) *bb;
11553
11554 if ((*ba)->number < (*bb)->number)
11555 return -1;
11556 else if ((*ba)->number > (*bb)->number)
11557 return 1;
11558
11559 /* Now sort by address, in case we see, e..g, two breakpoints with
11560 the number 0. */
11561 if (ua < ub)
11562 return -1;
94b0e70d 11563 return ua > ub ? 1 : 0;
8a2c437b
TT
11564}
11565
80f8a6eb 11566/* Delete breakpoints by address or line. */
c906108c
SS
11567
11568static void
fba45db2 11569clear_command (char *arg, int from_tty)
c906108c 11570{
8a2c437b 11571 struct breakpoint *b, *prev;
d6e956e5
VP
11572 VEC(breakpoint_p) *found = 0;
11573 int ix;
c906108c 11574 int default_match;
c906108c 11575 int i;
8a2c437b 11576 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c 11577
6c5b2ebe
PA
11578 std::vector<symtab_and_line> decoded_sals;
11579 symtab_and_line last_sal;
11580 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11581 if (arg)
11582 {
6c5b2ebe
PA
11583 decoded_sals
11584 = decode_line_with_current_source (arg,
11585 (DECODE_LINE_FUNFIRSTLINE
11586 | DECODE_LINE_LIST_MODE));
c906108c 11587 default_match = 0;
6c5b2ebe 11588 sals = decoded_sals;
c906108c
SS
11589 }
11590 else
11591 {
1bfeeb0f
JL
11592 /* Set sal's line, symtab, pc, and pspace to the values
11593 corresponding to the last call to print_frame_info. If the
11594 codepoint is not valid, this will set all the fields to 0. */
51abb421 11595 last_sal = get_last_displayed_sal ();
6c5b2ebe 11596 if (last_sal.symtab == 0)
8a3fe4f8 11597 error (_("No source file specified."));
c906108c 11598
c906108c 11599 default_match = 1;
6c5b2ebe 11600 sals = last_sal;
c906108c
SS
11601 }
11602
4a64f543
MS
11603 /* We don't call resolve_sal_pc here. That's not as bad as it
11604 seems, because all existing breakpoints typically have both
11605 file/line and pc set. So, if clear is given file/line, we can
11606 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11607
11608 We only support clearing given the address explicitly
11609 present in breakpoint table. Say, we've set breakpoint
4a64f543 11610 at file:line. There were several PC values for that file:line,
ed0616c6 11611 due to optimization, all in one block.
4a64f543
MS
11612
11613 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11614 PC corresponding to the same file:line, the breakpoint won't
11615 be cleared. We probably can still clear the breakpoint, but
11616 since the other PC value is never presented to user, user
11617 can only find it by guessing, and it does not seem important
11618 to support that. */
11619
4a64f543
MS
11620 /* For each line spec given, delete bps which correspond to it. Do
11621 it in two passes, solely to preserve the current behavior that
11622 from_tty is forced true if we delete more than one
11623 breakpoint. */
c906108c 11624
80f8a6eb 11625 found = NULL;
8a2c437b 11626 make_cleanup (VEC_cleanup (breakpoint_p), &found);
6c5b2ebe 11627 for (const auto &sal : sals)
c906108c 11628 {
05cba821
JK
11629 const char *sal_fullname;
11630
c906108c 11631 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11632 If line given (pc == 0), clear all bpts on specified line.
11633 If defaulting, clear all bpts on default line
c906108c 11634 or at default pc.
c5aa993b
JM
11635
11636 defaulting sal.pc != 0 tests to do
11637
11638 0 1 pc
11639 1 1 pc _and_ line
11640 0 0 line
11641 1 0 <can't happen> */
c906108c 11642
05cba821
JK
11643 sal_fullname = (sal.symtab == NULL
11644 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11645
4a64f543 11646 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11647 ALL_BREAKPOINTS (b)
c5aa993b 11648 {
0d381245 11649 int match = 0;
4a64f543 11650 /* Are we going to delete b? */
cc60f2e3 11651 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11652 {
11653 struct bp_location *loc = b->loc;
11654 for (; loc; loc = loc->next)
11655 {
f8eba3c6
TT
11656 /* If the user specified file:line, don't allow a PC
11657 match. This matches historical gdb behavior. */
11658 int pc_match = (!sal.explicit_line
11659 && sal.pc
11660 && (loc->pspace == sal.pspace)
11661 && (loc->address == sal.pc)
11662 && (!section_is_overlay (loc->section)
11663 || loc->section == sal.section));
4aac40c8
TT
11664 int line_match = 0;
11665
11666 if ((default_match || sal.explicit_line)
2f202fde 11667 && loc->symtab != NULL
05cba821 11668 && sal_fullname != NULL
4aac40c8 11669 && sal.pspace == loc->pspace
05cba821
JK
11670 && loc->line_number == sal.line
11671 && filename_cmp (symtab_to_fullname (loc->symtab),
11672 sal_fullname) == 0)
11673 line_match = 1;
4aac40c8 11674
0d381245
VP
11675 if (pc_match || line_match)
11676 {
11677 match = 1;
11678 break;
11679 }
11680 }
11681 }
11682
11683 if (match)
d6e956e5 11684 VEC_safe_push(breakpoint_p, found, b);
c906108c 11685 }
80f8a6eb 11686 }
8a2c437b 11687
80f8a6eb 11688 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11689 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11690 {
11691 if (arg)
8a3fe4f8 11692 error (_("No breakpoint at %s."), arg);
80f8a6eb 11693 else
8a3fe4f8 11694 error (_("No breakpoint at this line."));
80f8a6eb 11695 }
c906108c 11696
8a2c437b
TT
11697 /* Remove duplicates from the vec. */
11698 qsort (VEC_address (breakpoint_p, found),
11699 VEC_length (breakpoint_p, found),
11700 sizeof (breakpoint_p),
11701 compare_breakpoints);
11702 prev = VEC_index (breakpoint_p, found, 0);
11703 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11704 {
11705 if (b == prev)
11706 {
11707 VEC_ordered_remove (breakpoint_p, found, ix);
11708 --ix;
11709 }
11710 }
11711
d6e956e5 11712 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11713 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11714 if (from_tty)
a3f17187 11715 {
d6e956e5 11716 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11717 printf_unfiltered (_("Deleted breakpoint "));
11718 else
11719 printf_unfiltered (_("Deleted breakpoints "));
11720 }
d6e956e5
VP
11721
11722 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11723 {
c5aa993b 11724 if (from_tty)
d6e956e5
VP
11725 printf_unfiltered ("%d ", b->number);
11726 delete_breakpoint (b);
c906108c 11727 }
80f8a6eb
MS
11728 if (from_tty)
11729 putchar_unfiltered ('\n');
8a2c437b
TT
11730
11731 do_cleanups (cleanups);
c906108c
SS
11732}
11733\f
11734/* Delete breakpoint in BS if they are `delete' breakpoints and
11735 all breakpoints that are marked for deletion, whether hit or not.
11736 This is called after any breakpoint is hit, or after errors. */
11737
11738void
fba45db2 11739breakpoint_auto_delete (bpstat bs)
c906108c 11740{
35df4500 11741 struct breakpoint *b, *b_tmp;
c906108c
SS
11742
11743 for (; bs; bs = bs->next)
f431efe5
PA
11744 if (bs->breakpoint_at
11745 && bs->breakpoint_at->disposition == disp_del
c906108c 11746 && bs->stop)
f431efe5 11747 delete_breakpoint (bs->breakpoint_at);
c906108c 11748
35df4500 11749 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11750 {
b5de0fa7 11751 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11752 delete_breakpoint (b);
11753 }
c906108c
SS
11754}
11755
4a64f543
MS
11756/* A comparison function for bp_location AP and BP being interfaced to
11757 qsort. Sort elements primarily by their ADDRESS (no matter what
11758 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11759 secondarily by ordering first permanent elements and
4a64f543 11760 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11761 qsort being an unstable algorithm. */
876fa593
JK
11762
11763static int
f5336ca5 11764bp_locations_compare (const void *ap, const void *bp)
876fa593 11765{
9a3c8263
SM
11766 const struct bp_location *a = *(const struct bp_location **) ap;
11767 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11768
11769 if (a->address != b->address)
11770 return (a->address > b->address) - (a->address < b->address);
11771
dea2aa5f
LM
11772 /* Sort locations at the same address by their pspace number, keeping
11773 locations of the same inferior (in a multi-inferior environment)
11774 grouped. */
11775
11776 if (a->pspace->num != b->pspace->num)
11777 return ((a->pspace->num > b->pspace->num)
11778 - (a->pspace->num < b->pspace->num));
11779
876fa593 11780 /* Sort permanent breakpoints first. */
1a853c52
PA
11781 if (a->permanent != b->permanent)
11782 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11783
c56a97f9
JK
11784 /* Make the internal GDB representation stable across GDB runs
11785 where A and B memory inside GDB can differ. Breakpoint locations of
11786 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11787
11788 if (a->owner->number != b->owner->number)
c56a97f9
JK
11789 return ((a->owner->number > b->owner->number)
11790 - (a->owner->number < b->owner->number));
876fa593
JK
11791
11792 return (a > b) - (a < b);
11793}
11794
f5336ca5
PA
11795/* Set bp_locations_placed_address_before_address_max and
11796 bp_locations_shadow_len_after_address_max according to the current
11797 content of the bp_locations array. */
f7545552
TT
11798
11799static void
f5336ca5 11800bp_locations_target_extensions_update (void)
f7545552 11801{
876fa593
JK
11802 struct bp_location *bl, **blp_tmp;
11803
f5336ca5
PA
11804 bp_locations_placed_address_before_address_max = 0;
11805 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11806
11807 ALL_BP_LOCATIONS (bl, blp_tmp)
11808 {
11809 CORE_ADDR start, end, addr;
11810
11811 if (!bp_location_has_shadow (bl))
11812 continue;
11813
11814 start = bl->target_info.placed_address;
11815 end = start + bl->target_info.shadow_len;
11816
11817 gdb_assert (bl->address >= start);
11818 addr = bl->address - start;
f5336ca5
PA
11819 if (addr > bp_locations_placed_address_before_address_max)
11820 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11821
11822 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11823
11824 gdb_assert (bl->address < end);
11825 addr = end - bl->address;
f5336ca5
PA
11826 if (addr > bp_locations_shadow_len_after_address_max)
11827 bp_locations_shadow_len_after_address_max = addr;
876fa593 11828 }
f7545552
TT
11829}
11830
1e4d1764
YQ
11831/* Download tracepoint locations if they haven't been. */
11832
11833static void
11834download_tracepoint_locations (void)
11835{
7ed2c994 11836 struct breakpoint *b;
dd2e65cc 11837 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11838
5ed8105e 11839 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11840
7ed2c994 11841 ALL_TRACEPOINTS (b)
1e4d1764 11842 {
7ed2c994 11843 struct bp_location *bl;
1e4d1764 11844 struct tracepoint *t;
f2a8bc8a 11845 int bp_location_downloaded = 0;
1e4d1764 11846
7ed2c994 11847 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11848 ? !may_insert_fast_tracepoints
11849 : !may_insert_tracepoints))
11850 continue;
11851
dd2e65cc
YQ
11852 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11853 {
11854 if (target_can_download_tracepoint ())
11855 can_download_tracepoint = TRIBOOL_TRUE;
11856 else
11857 can_download_tracepoint = TRIBOOL_FALSE;
11858 }
11859
11860 if (can_download_tracepoint == TRIBOOL_FALSE)
11861 break;
11862
7ed2c994
YQ
11863 for (bl = b->loc; bl; bl = bl->next)
11864 {
11865 /* In tracepoint, locations are _never_ duplicated, so
11866 should_be_inserted is equivalent to
11867 unduplicated_should_be_inserted. */
11868 if (!should_be_inserted (bl) || bl->inserted)
11869 continue;
1e4d1764 11870
7ed2c994 11871 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11872
7ed2c994 11873 target_download_tracepoint (bl);
1e4d1764 11874
7ed2c994 11875 bl->inserted = 1;
f2a8bc8a 11876 bp_location_downloaded = 1;
7ed2c994
YQ
11877 }
11878 t = (struct tracepoint *) b;
11879 t->number_on_target = b->number;
f2a8bc8a
YQ
11880 if (bp_location_downloaded)
11881 observer_notify_breakpoint_modified (b);
1e4d1764 11882 }
1e4d1764
YQ
11883}
11884
934709f0
PW
11885/* Swap the insertion/duplication state between two locations. */
11886
11887static void
11888swap_insertion (struct bp_location *left, struct bp_location *right)
11889{
11890 const int left_inserted = left->inserted;
11891 const int left_duplicate = left->duplicate;
b775012e 11892 const int left_needs_update = left->needs_update;
934709f0
PW
11893 const struct bp_target_info left_target_info = left->target_info;
11894
1e4d1764
YQ
11895 /* Locations of tracepoints can never be duplicated. */
11896 if (is_tracepoint (left->owner))
11897 gdb_assert (!left->duplicate);
11898 if (is_tracepoint (right->owner))
11899 gdb_assert (!right->duplicate);
11900
934709f0
PW
11901 left->inserted = right->inserted;
11902 left->duplicate = right->duplicate;
b775012e 11903 left->needs_update = right->needs_update;
934709f0
PW
11904 left->target_info = right->target_info;
11905 right->inserted = left_inserted;
11906 right->duplicate = left_duplicate;
b775012e 11907 right->needs_update = left_needs_update;
934709f0
PW
11908 right->target_info = left_target_info;
11909}
11910
b775012e
LM
11911/* Force the re-insertion of the locations at ADDRESS. This is called
11912 once a new/deleted/modified duplicate location is found and we are evaluating
11913 conditions on the target's side. Such conditions need to be updated on
11914 the target. */
11915
11916static void
11917force_breakpoint_reinsertion (struct bp_location *bl)
11918{
11919 struct bp_location **locp = NULL, **loc2p;
11920 struct bp_location *loc;
11921 CORE_ADDR address = 0;
11922 int pspace_num;
11923
11924 address = bl->address;
11925 pspace_num = bl->pspace->num;
11926
11927 /* This is only meaningful if the target is
11928 evaluating conditions and if the user has
11929 opted for condition evaluation on the target's
11930 side. */
11931 if (gdb_evaluates_breakpoint_condition_p ()
11932 || !target_supports_evaluation_of_breakpoint_conditions ())
11933 return;
11934
11935 /* Flag all breakpoint locations with this address and
11936 the same program space as the location
11937 as "its condition has changed". We need to
11938 update the conditions on the target's side. */
11939 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11940 {
11941 loc = *loc2p;
11942
11943 if (!is_breakpoint (loc->owner)
11944 || pspace_num != loc->pspace->num)
11945 continue;
11946
11947 /* Flag the location appropriately. We use a different state to
11948 let everyone know that we already updated the set of locations
11949 with addr bl->address and program space bl->pspace. This is so
11950 we don't have to keep calling these functions just to mark locations
11951 that have already been marked. */
11952 loc->condition_changed = condition_updated;
11953
11954 /* Free the agent expression bytecode as well. We will compute
11955 it later on. */
833177a4 11956 loc->cond_bytecode.reset ();
b775012e
LM
11957 }
11958}
44702360
PA
11959/* Called whether new breakpoints are created, or existing breakpoints
11960 deleted, to update the global location list and recompute which
11961 locations are duplicate of which.
b775012e 11962
04086b45
PA
11963 The INSERT_MODE flag determines whether locations may not, may, or
11964 shall be inserted now. See 'enum ugll_insert_mode' for more
11965 info. */
b60e7edf 11966
0d381245 11967static void
44702360 11968update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11969{
74960c60 11970 struct breakpoint *b;
876fa593 11971 struct bp_location **locp, *loc;
f7545552 11972 struct cleanup *cleanups;
b775012e
LM
11973 /* Last breakpoint location address that was marked for update. */
11974 CORE_ADDR last_addr = 0;
11975 /* Last breakpoint location program space that was marked for update. */
11976 int last_pspace_num = -1;
f7545552 11977
2d134ed3
PA
11978 /* Used in the duplicates detection below. When iterating over all
11979 bp_locations, points to the first bp_location of a given address.
11980 Breakpoints and watchpoints of different types are never
11981 duplicates of each other. Keep one pointer for each type of
11982 breakpoint/watchpoint, so we only need to loop over all locations
11983 once. */
11984 struct bp_location *bp_loc_first; /* breakpoint */
11985 struct bp_location *wp_loc_first; /* hardware watchpoint */
11986 struct bp_location *awp_loc_first; /* access watchpoint */
11987 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11988
f5336ca5
PA
11989 /* Saved former bp_locations array which we compare against the newly
11990 built bp_locations from the current state of ALL_BREAKPOINTS. */
11991 struct bp_location **old_locations, **old_locp;
11992 unsigned old_locations_count;
876fa593 11993
f5336ca5
PA
11994 old_locations = bp_locations;
11995 old_locations_count = bp_locations_count;
11996 bp_locations = NULL;
11997 bp_locations_count = 0;
11998 cleanups = make_cleanup (xfree, old_locations);
0d381245 11999
74960c60 12000 ALL_BREAKPOINTS (b)
876fa593 12001 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12002 bp_locations_count++;
876fa593 12003
f5336ca5
PA
12004 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12005 locp = bp_locations;
876fa593
JK
12006 ALL_BREAKPOINTS (b)
12007 for (loc = b->loc; loc; loc = loc->next)
12008 *locp++ = loc;
f5336ca5
PA
12009 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12010 bp_locations_compare);
876fa593 12011
f5336ca5 12012 bp_locations_target_extensions_update ();
74960c60 12013
4a64f543
MS
12014 /* Identify bp_location instances that are no longer present in the
12015 new list, and therefore should be freed. Note that it's not
12016 necessary that those locations should be removed from inferior --
12017 if there's another location at the same address (previously
12018 marked as duplicate), we don't need to remove/insert the
12019 location.
876fa593 12020
4a64f543
MS
12021 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12022 and former bp_location array state respectively. */
876fa593 12023
f5336ca5
PA
12024 locp = bp_locations;
12025 for (old_locp = old_locations;
12026 old_locp < old_locations + old_locations_count;
876fa593 12027 old_locp++)
74960c60 12028 {
876fa593 12029 struct bp_location *old_loc = *old_locp;
c7d46a38 12030 struct bp_location **loc2p;
876fa593 12031
e5dd4106 12032 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12033 not, we have to free it. */
c7d46a38 12034 int found_object = 0;
20874c92
VP
12035 /* Tells if the location should remain inserted in the target. */
12036 int keep_in_target = 0;
12037 int removed = 0;
876fa593 12038
4a64f543
MS
12039 /* Skip LOCP entries which will definitely never be needed.
12040 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12041 while (locp < bp_locations + bp_locations_count
c7d46a38 12042 && (*locp)->address < old_loc->address)
876fa593 12043 locp++;
c7d46a38
PA
12044
12045 for (loc2p = locp;
f5336ca5 12046 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12047 && (*loc2p)->address == old_loc->address);
12048 loc2p++)
12049 {
b775012e
LM
12050 /* Check if this is a new/duplicated location or a duplicated
12051 location that had its condition modified. If so, we want to send
12052 its condition to the target if evaluation of conditions is taking
12053 place there. */
12054 if ((*loc2p)->condition_changed == condition_modified
12055 && (last_addr != old_loc->address
12056 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12057 {
b775012e
LM
12058 force_breakpoint_reinsertion (*loc2p);
12059 last_pspace_num = old_loc->pspace->num;
c7d46a38 12060 }
b775012e
LM
12061
12062 if (*loc2p == old_loc)
12063 found_object = 1;
c7d46a38 12064 }
74960c60 12065
b775012e
LM
12066 /* We have already handled this address, update it so that we don't
12067 have to go through updates again. */
12068 last_addr = old_loc->address;
12069
12070 /* Target-side condition evaluation: Handle deleted locations. */
12071 if (!found_object)
12072 force_breakpoint_reinsertion (old_loc);
12073
4a64f543
MS
12074 /* If this location is no longer present, and inserted, look if
12075 there's maybe a new location at the same address. If so,
12076 mark that one inserted, and don't remove this one. This is
12077 needed so that we don't have a time window where a breakpoint
12078 at certain location is not inserted. */
74960c60 12079
876fa593 12080 if (old_loc->inserted)
0d381245 12081 {
4a64f543
MS
12082 /* If the location is inserted now, we might have to remove
12083 it. */
74960c60 12084
876fa593 12085 if (found_object && should_be_inserted (old_loc))
74960c60 12086 {
4a64f543
MS
12087 /* The location is still present in the location list,
12088 and still should be inserted. Don't do anything. */
20874c92 12089 keep_in_target = 1;
74960c60
VP
12090 }
12091 else
12092 {
b775012e
LM
12093 /* This location still exists, but it won't be kept in the
12094 target since it may have been disabled. We proceed to
12095 remove its target-side condition. */
12096
4a64f543
MS
12097 /* The location is either no longer present, or got
12098 disabled. See if there's another location at the
12099 same address, in which case we don't need to remove
12100 this one from the target. */
876fa593 12101
2bdf28a0 12102 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12103 if (breakpoint_address_is_meaningful (old_loc->owner))
12104 {
876fa593 12105 for (loc2p = locp;
f5336ca5 12106 (loc2p < bp_locations + bp_locations_count
c7d46a38 12107 && (*loc2p)->address == old_loc->address);
876fa593
JK
12108 loc2p++)
12109 {
12110 struct bp_location *loc2 = *loc2p;
12111
2d134ed3 12112 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12113 {
85d721b8
PA
12114 /* Read watchpoint locations are switched to
12115 access watchpoints, if the former are not
12116 supported, but the latter are. */
12117 if (is_hardware_watchpoint (old_loc->owner))
12118 {
12119 gdb_assert (is_hardware_watchpoint (loc2->owner));
12120 loc2->watchpoint_type = old_loc->watchpoint_type;
12121 }
12122
934709f0
PW
12123 /* loc2 is a duplicated location. We need to check
12124 if it should be inserted in case it will be
12125 unduplicated. */
12126 if (loc2 != old_loc
12127 && unduplicated_should_be_inserted (loc2))
c7d46a38 12128 {
934709f0 12129 swap_insertion (old_loc, loc2);
c7d46a38
PA
12130 keep_in_target = 1;
12131 break;
12132 }
876fa593
JK
12133 }
12134 }
12135 }
74960c60
VP
12136 }
12137
20874c92
VP
12138 if (!keep_in_target)
12139 {
834c0d03 12140 if (remove_breakpoint (old_loc))
20874c92 12141 {
4a64f543
MS
12142 /* This is just about all we can do. We could keep
12143 this location on the global list, and try to
12144 remove it next time, but there's no particular
12145 reason why we will succeed next time.
20874c92 12146
4a64f543
MS
12147 Note that at this point, old_loc->owner is still
12148 valid, as delete_breakpoint frees the breakpoint
12149 only after calling us. */
3e43a32a
MS
12150 printf_filtered (_("warning: Error removing "
12151 "breakpoint %d\n"),
876fa593 12152 old_loc->owner->number);
20874c92
VP
12153 }
12154 removed = 1;
12155 }
0d381245 12156 }
74960c60
VP
12157
12158 if (!found_object)
1c5cfe86 12159 {
fbea99ea 12160 if (removed && target_is_non_stop_p ()
1cf4d951 12161 && need_moribund_for_location_type (old_loc))
20874c92 12162 {
db82e815
PA
12163 /* This location was removed from the target. In
12164 non-stop mode, a race condition is possible where
12165 we've removed a breakpoint, but stop events for that
12166 breakpoint are already queued and will arrive later.
12167 We apply an heuristic to be able to distinguish such
12168 SIGTRAPs from other random SIGTRAPs: we keep this
12169 breakpoint location for a bit, and will retire it
12170 after we see some number of events. The theory here
12171 is that reporting of events should, "on the average",
12172 be fair, so after a while we'll see events from all
12173 threads that have anything of interest, and no longer
12174 need to keep this breakpoint location around. We
12175 don't hold locations forever so to reduce chances of
12176 mistaking a non-breakpoint SIGTRAP for a breakpoint
12177 SIGTRAP.
12178
12179 The heuristic failing can be disastrous on
12180 decr_pc_after_break targets.
12181
12182 On decr_pc_after_break targets, like e.g., x86-linux,
12183 if we fail to recognize a late breakpoint SIGTRAP,
12184 because events_till_retirement has reached 0 too
12185 soon, we'll fail to do the PC adjustment, and report
12186 a random SIGTRAP to the user. When the user resumes
12187 the inferior, it will most likely immediately crash
2dec564e 12188 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12189 corrupted, because of being resumed e.g., in the
12190 middle of a multi-byte instruction, or skipped a
12191 one-byte instruction. This was actually seen happen
12192 on native x86-linux, and should be less rare on
12193 targets that do not support new thread events, like
12194 remote, due to the heuristic depending on
12195 thread_count.
12196
12197 Mistaking a random SIGTRAP for a breakpoint trap
12198 causes similar symptoms (PC adjustment applied when
12199 it shouldn't), but then again, playing with SIGTRAPs
12200 behind the debugger's back is asking for trouble.
12201
12202 Since hardware watchpoint traps are always
12203 distinguishable from other traps, so we don't need to
12204 apply keep hardware watchpoint moribund locations
12205 around. We simply always ignore hardware watchpoint
12206 traps we can no longer explain. */
12207
876fa593
JK
12208 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12209 old_loc->owner = NULL;
20874c92 12210
876fa593 12211 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12212 }
12213 else
f431efe5
PA
12214 {
12215 old_loc->owner = NULL;
12216 decref_bp_location (&old_loc);
12217 }
20874c92 12218 }
74960c60 12219 }
1c5cfe86 12220
348d480f
PA
12221 /* Rescan breakpoints at the same address and section, marking the
12222 first one as "first" and any others as "duplicates". This is so
12223 that the bpt instruction is only inserted once. If we have a
12224 permanent breakpoint at the same place as BPT, make that one the
12225 official one, and the rest as duplicates. Permanent breakpoints
12226 are sorted first for the same address.
12227
12228 Do the same for hardware watchpoints, but also considering the
12229 watchpoint's type (regular/access/read) and length. */
12230
12231 bp_loc_first = NULL;
12232 wp_loc_first = NULL;
12233 awp_loc_first = NULL;
12234 rwp_loc_first = NULL;
12235 ALL_BP_LOCATIONS (loc, locp)
12236 {
12237 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12238 non-NULL. */
348d480f 12239 struct bp_location **loc_first_p;
d3fbdd86 12240 b = loc->owner;
348d480f 12241
6f380991 12242 if (!unduplicated_should_be_inserted (loc)
348d480f 12243 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12244 /* Don't detect duplicate for tracepoint locations because they are
12245 never duplicated. See the comments in field `duplicate' of
12246 `struct bp_location'. */
348d480f 12247 || is_tracepoint (b))
b775012e
LM
12248 {
12249 /* Clear the condition modification flag. */
12250 loc->condition_changed = condition_unchanged;
12251 continue;
12252 }
348d480f 12253
348d480f
PA
12254 if (b->type == bp_hardware_watchpoint)
12255 loc_first_p = &wp_loc_first;
12256 else if (b->type == bp_read_watchpoint)
12257 loc_first_p = &rwp_loc_first;
12258 else if (b->type == bp_access_watchpoint)
12259 loc_first_p = &awp_loc_first;
12260 else
12261 loc_first_p = &bp_loc_first;
12262
12263 if (*loc_first_p == NULL
12264 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12265 || !breakpoint_locations_match (loc, *loc_first_p))
12266 {
12267 *loc_first_p = loc;
12268 loc->duplicate = 0;
b775012e
LM
12269
12270 if (is_breakpoint (loc->owner) && loc->condition_changed)
12271 {
12272 loc->needs_update = 1;
12273 /* Clear the condition modification flag. */
12274 loc->condition_changed = condition_unchanged;
12275 }
348d480f
PA
12276 continue;
12277 }
12278
934709f0
PW
12279
12280 /* This and the above ensure the invariant that the first location
12281 is not duplicated, and is the inserted one.
12282 All following are marked as duplicated, and are not inserted. */
12283 if (loc->inserted)
12284 swap_insertion (loc, *loc_first_p);
348d480f
PA
12285 loc->duplicate = 1;
12286
b775012e
LM
12287 /* Clear the condition modification flag. */
12288 loc->condition_changed = condition_unchanged;
348d480f
PA
12289 }
12290
a25a5a45 12291 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12292 {
04086b45 12293 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12294 insert_breakpoint_locations ();
12295 else
12296 {
44702360
PA
12297 /* Even though the caller told us to not insert new
12298 locations, we may still need to update conditions on the
12299 target's side of breakpoints that were already inserted
12300 if the target is evaluating breakpoint conditions. We
b775012e
LM
12301 only update conditions for locations that are marked
12302 "needs_update". */
12303 update_inserted_breakpoint_locations ();
12304 }
12305 }
348d480f 12306
04086b45 12307 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12308 download_tracepoint_locations ();
12309
348d480f
PA
12310 do_cleanups (cleanups);
12311}
12312
12313void
12314breakpoint_retire_moribund (void)
12315{
12316 struct bp_location *loc;
12317 int ix;
12318
12319 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12320 if (--(loc->events_till_retirement) == 0)
12321 {
12322 decref_bp_location (&loc);
12323 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12324 --ix;
12325 }
12326}
12327
12328static void
44702360 12329update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12330{
348d480f 12331
492d29ea
PA
12332 TRY
12333 {
12334 update_global_location_list (insert_mode);
12335 }
12336 CATCH (e, RETURN_MASK_ERROR)
12337 {
12338 }
12339 END_CATCH
348d480f
PA
12340}
12341
12342/* Clear BKP from a BPS. */
12343
12344static void
12345bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12346{
12347 bpstat bs;
12348
12349 for (bs = bps; bs; bs = bs->next)
12350 if (bs->breakpoint_at == bpt)
12351 {
12352 bs->breakpoint_at = NULL;
12353 bs->old_val = NULL;
12354 /* bs->commands will be freed later. */
12355 }
12356}
12357
12358/* Callback for iterate_over_threads. */
12359static int
12360bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12361{
9a3c8263 12362 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12363
12364 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12365 return 0;
12366}
12367
12368/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12369 callbacks. */
12370
12371static void
12372say_where (struct breakpoint *b)
12373{
12374 struct value_print_options opts;
12375
12376 get_user_print_options (&opts);
12377
12378 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12379 single string. */
12380 if (b->loc == NULL)
12381 {
f00aae0f
KS
12382 /* For pending locations, the output differs slightly based
12383 on b->extra_string. If this is non-NULL, it contains either
12384 a condition or dprintf arguments. */
12385 if (b->extra_string == NULL)
12386 {
12387 printf_filtered (_(" (%s) pending."),
d28cd78a 12388 event_location_to_string (b->location.get ()));
f00aae0f
KS
12389 }
12390 else if (b->type == bp_dprintf)
12391 {
12392 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12393 event_location_to_string (b->location.get ()),
f00aae0f
KS
12394 b->extra_string);
12395 }
12396 else
12397 {
12398 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12399 event_location_to_string (b->location.get ()),
f00aae0f
KS
12400 b->extra_string);
12401 }
348d480f
PA
12402 }
12403 else
12404 {
2f202fde 12405 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12406 {
12407 printf_filtered (" at ");
12408 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12409 gdb_stdout);
12410 }
2f202fde 12411 if (b->loc->symtab != NULL)
f8eba3c6
TT
12412 {
12413 /* If there is a single location, we can print the location
12414 more nicely. */
12415 if (b->loc->next == NULL)
12416 printf_filtered (": file %s, line %d.",
05cba821
JK
12417 symtab_to_filename_for_display (b->loc->symtab),
12418 b->loc->line_number);
f8eba3c6
TT
12419 else
12420 /* This is not ideal, but each location may have a
12421 different file name, and this at least reflects the
12422 real situation somewhat. */
f00aae0f 12423 printf_filtered (": %s.",
d28cd78a 12424 event_location_to_string (b->location.get ()));
f8eba3c6 12425 }
348d480f
PA
12426
12427 if (b->loc->next)
12428 {
12429 struct bp_location *loc = b->loc;
12430 int n = 0;
12431 for (; loc; loc = loc->next)
12432 ++n;
12433 printf_filtered (" (%d locations)", n);
12434 }
12435 }
12436}
12437
348d480f
PA
12438/* Default bp_location_ops methods. */
12439
12440static void
12441bp_location_dtor (struct bp_location *self)
12442{
348d480f
PA
12443 xfree (self->function_name);
12444}
12445
12446static const struct bp_location_ops bp_location_ops =
12447{
12448 bp_location_dtor
12449};
12450
c1fc2657 12451/* Destructor for the breakpoint base class. */
348d480f 12452
c1fc2657 12453breakpoint::~breakpoint ()
348d480f 12454{
c1fc2657
SM
12455 xfree (this->cond_string);
12456 xfree (this->extra_string);
12457 xfree (this->filter);
348d480f
PA
12458}
12459
2060206e
PA
12460static struct bp_location *
12461base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12462{
5625a286 12463 return new bp_location (&bp_location_ops, self);
348d480f
PA
12464}
12465
2060206e
PA
12466static void
12467base_breakpoint_re_set (struct breakpoint *b)
12468{
12469 /* Nothing to re-set. */
12470}
12471
12472#define internal_error_pure_virtual_called() \
12473 gdb_assert_not_reached ("pure virtual function called")
12474
12475static int
12476base_breakpoint_insert_location (struct bp_location *bl)
12477{
12478 internal_error_pure_virtual_called ();
12479}
12480
12481static int
73971819
PA
12482base_breakpoint_remove_location (struct bp_location *bl,
12483 enum remove_bp_reason reason)
2060206e
PA
12484{
12485 internal_error_pure_virtual_called ();
12486}
12487
12488static int
12489base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12490 struct address_space *aspace,
09ac7c10
TT
12491 CORE_ADDR bp_addr,
12492 const struct target_waitstatus *ws)
2060206e
PA
12493{
12494 internal_error_pure_virtual_called ();
12495}
12496
12497static void
12498base_breakpoint_check_status (bpstat bs)
12499{
12500 /* Always stop. */
12501}
12502
12503/* A "works_in_software_mode" breakpoint_ops method that just internal
12504 errors. */
12505
12506static int
12507base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12508{
12509 internal_error_pure_virtual_called ();
12510}
12511
12512/* A "resources_needed" breakpoint_ops method that just internal
12513 errors. */
12514
12515static int
12516base_breakpoint_resources_needed (const struct bp_location *bl)
12517{
12518 internal_error_pure_virtual_called ();
12519}
12520
12521static enum print_stop_action
12522base_breakpoint_print_it (bpstat bs)
12523{
12524 internal_error_pure_virtual_called ();
12525}
12526
12527static void
12528base_breakpoint_print_one_detail (const struct breakpoint *self,
12529 struct ui_out *uiout)
12530{
12531 /* nothing */
12532}
12533
12534static void
12535base_breakpoint_print_mention (struct breakpoint *b)
12536{
12537 internal_error_pure_virtual_called ();
12538}
12539
12540static void
12541base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12542{
12543 internal_error_pure_virtual_called ();
12544}
12545
983af33b 12546static void
f00aae0f
KS
12547base_breakpoint_create_sals_from_location
12548 (const struct event_location *location,
12549 struct linespec_result *canonical,
12550 enum bptype type_wanted)
983af33b
SDJ
12551{
12552 internal_error_pure_virtual_called ();
12553}
12554
12555static void
12556base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12557 struct linespec_result *c,
e1e01040
PA
12558 gdb::unique_xmalloc_ptr<char> cond_string,
12559 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12560 enum bptype type_wanted,
12561 enum bpdisp disposition,
12562 int thread,
12563 int task, int ignore_count,
12564 const struct breakpoint_ops *o,
12565 int from_tty, int enabled,
44f238bb 12566 int internal, unsigned flags)
983af33b
SDJ
12567{
12568 internal_error_pure_virtual_called ();
12569}
12570
6c5b2ebe 12571static std::vector<symtab_and_line>
f00aae0f
KS
12572base_breakpoint_decode_location (struct breakpoint *b,
12573 const struct event_location *location,
6c5b2ebe 12574 struct program_space *search_pspace)
983af33b
SDJ
12575{
12576 internal_error_pure_virtual_called ();
12577}
12578
ab04a2af
TT
12579/* The default 'explains_signal' method. */
12580
47591c29 12581static int
427cd150 12582base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12583{
47591c29 12584 return 1;
ab04a2af
TT
12585}
12586
9d6e6e84
HZ
12587/* The default "after_condition_true" method. */
12588
12589static void
12590base_breakpoint_after_condition_true (struct bpstats *bs)
12591{
12592 /* Nothing to do. */
12593}
12594
ab04a2af 12595struct breakpoint_ops base_breakpoint_ops =
2060206e 12596{
2060206e
PA
12597 base_breakpoint_allocate_location,
12598 base_breakpoint_re_set,
12599 base_breakpoint_insert_location,
12600 base_breakpoint_remove_location,
12601 base_breakpoint_breakpoint_hit,
12602 base_breakpoint_check_status,
12603 base_breakpoint_resources_needed,
12604 base_breakpoint_works_in_software_mode,
12605 base_breakpoint_print_it,
12606 NULL,
12607 base_breakpoint_print_one_detail,
12608 base_breakpoint_print_mention,
983af33b 12609 base_breakpoint_print_recreate,
5f700d83 12610 base_breakpoint_create_sals_from_location,
983af33b 12611 base_breakpoint_create_breakpoints_sal,
5f700d83 12612 base_breakpoint_decode_location,
9d6e6e84
HZ
12613 base_breakpoint_explains_signal,
12614 base_breakpoint_after_condition_true,
2060206e
PA
12615};
12616
12617/* Default breakpoint_ops methods. */
12618
12619static void
348d480f
PA
12620bkpt_re_set (struct breakpoint *b)
12621{
06edf0c0 12622 /* FIXME: is this still reachable? */
9ef9e6a6 12623 if (breakpoint_event_location_empty_p (b))
06edf0c0 12624 {
f00aae0f 12625 /* Anything without a location can't be re-set. */
348d480f 12626 delete_breakpoint (b);
06edf0c0 12627 return;
348d480f 12628 }
06edf0c0
PA
12629
12630 breakpoint_re_set_default (b);
348d480f
PA
12631}
12632
2060206e 12633static int
348d480f
PA
12634bkpt_insert_location (struct bp_location *bl)
12635{
cd6c3b4f
YQ
12636 CORE_ADDR addr = bl->target_info.reqstd_address;
12637
579c6ad9 12638 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12639 bl->target_info.placed_address = addr;
12640
348d480f 12641 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12642 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12643 else
7c16b83e 12644 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12645}
12646
2060206e 12647static int
73971819 12648bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12649{
12650 if (bl->loc_type == bp_loc_hardware_breakpoint)
12651 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12652 else
73971819 12653 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12654}
12655
2060206e 12656static int
348d480f 12657bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12658 struct address_space *aspace, CORE_ADDR bp_addr,
12659 const struct target_waitstatus *ws)
348d480f 12660{
09ac7c10 12661 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12662 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12663 return 0;
12664
348d480f
PA
12665 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12666 aspace, bp_addr))
12667 return 0;
12668
12669 if (overlay_debugging /* unmapped overlay section */
12670 && section_is_overlay (bl->section)
12671 && !section_is_mapped (bl->section))
12672 return 0;
12673
12674 return 1;
12675}
12676
cd1608cc
PA
12677static int
12678dprintf_breakpoint_hit (const struct bp_location *bl,
12679 struct address_space *aspace, CORE_ADDR bp_addr,
12680 const struct target_waitstatus *ws)
12681{
12682 if (dprintf_style == dprintf_style_agent
12683 && target_can_run_breakpoint_commands ())
12684 {
12685 /* An agent-style dprintf never causes a stop. If we see a trap
12686 for this address it must be for a breakpoint that happens to
12687 be set at the same address. */
12688 return 0;
12689 }
12690
12691 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12692}
12693
2060206e 12694static int
348d480f
PA
12695bkpt_resources_needed (const struct bp_location *bl)
12696{
12697 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12698
12699 return 1;
12700}
12701
2060206e 12702static enum print_stop_action
348d480f
PA
12703bkpt_print_it (bpstat bs)
12704{
348d480f
PA
12705 struct breakpoint *b;
12706 const struct bp_location *bl;
001c8c33 12707 int bp_temp;
79a45e25 12708 struct ui_out *uiout = current_uiout;
348d480f
PA
12709
12710 gdb_assert (bs->bp_location_at != NULL);
12711
12712 bl = bs->bp_location_at;
12713 b = bs->breakpoint_at;
12714
001c8c33
PA
12715 bp_temp = b->disposition == disp_del;
12716 if (bl->address != bl->requested_address)
12717 breakpoint_adjustment_warning (bl->requested_address,
12718 bl->address,
12719 b->number, 1);
12720 annotate_breakpoint (b->number);
f303dbd6
PA
12721 maybe_print_thread_hit_breakpoint (uiout);
12722
001c8c33 12723 if (bp_temp)
112e8700 12724 uiout->text ("Temporary breakpoint ");
001c8c33 12725 else
112e8700
SM
12726 uiout->text ("Breakpoint ");
12727 if (uiout->is_mi_like_p ())
348d480f 12728 {
112e8700 12729 uiout->field_string ("reason",
001c8c33 12730 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12731 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12732 }
112e8700
SM
12733 uiout->field_int ("bkptno", b->number);
12734 uiout->text (", ");
06edf0c0 12735
001c8c33 12736 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12737}
12738
2060206e 12739static void
06edf0c0
PA
12740bkpt_print_mention (struct breakpoint *b)
12741{
112e8700 12742 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12743 return;
12744
12745 switch (b->type)
12746 {
12747 case bp_breakpoint:
12748 case bp_gnu_ifunc_resolver:
12749 if (b->disposition == disp_del)
12750 printf_filtered (_("Temporary breakpoint"));
12751 else
12752 printf_filtered (_("Breakpoint"));
12753 printf_filtered (_(" %d"), b->number);
12754 if (b->type == bp_gnu_ifunc_resolver)
12755 printf_filtered (_(" at gnu-indirect-function resolver"));
12756 break;
12757 case bp_hardware_breakpoint:
12758 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12759 break;
e7e0cddf
SS
12760 case bp_dprintf:
12761 printf_filtered (_("Dprintf %d"), b->number);
12762 break;
06edf0c0
PA
12763 }
12764
12765 say_where (b);
12766}
12767
2060206e 12768static void
06edf0c0
PA
12769bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12770{
12771 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12772 fprintf_unfiltered (fp, "tbreak");
12773 else if (tp->type == bp_breakpoint)
12774 fprintf_unfiltered (fp, "break");
12775 else if (tp->type == bp_hardware_breakpoint
12776 && tp->disposition == disp_del)
12777 fprintf_unfiltered (fp, "thbreak");
12778 else if (tp->type == bp_hardware_breakpoint)
12779 fprintf_unfiltered (fp, "hbreak");
12780 else
12781 internal_error (__FILE__, __LINE__,
12782 _("unhandled breakpoint type %d"), (int) tp->type);
12783
f00aae0f 12784 fprintf_unfiltered (fp, " %s",
d28cd78a 12785 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12786
12787 /* Print out extra_string if this breakpoint is pending. It might
12788 contain, for example, conditions that were set by the user. */
12789 if (tp->loc == NULL && tp->extra_string != NULL)
12790 fprintf_unfiltered (fp, " %s", tp->extra_string);
12791
dd11a36c 12792 print_recreate_thread (tp, fp);
06edf0c0
PA
12793}
12794
983af33b 12795static void
f00aae0f
KS
12796bkpt_create_sals_from_location (const struct event_location *location,
12797 struct linespec_result *canonical,
12798 enum bptype type_wanted)
983af33b 12799{
f00aae0f 12800 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12801}
12802
12803static void
12804bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12805 struct linespec_result *canonical,
e1e01040
PA
12806 gdb::unique_xmalloc_ptr<char> cond_string,
12807 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12808 enum bptype type_wanted,
12809 enum bpdisp disposition,
12810 int thread,
12811 int task, int ignore_count,
12812 const struct breakpoint_ops *ops,
12813 int from_tty, int enabled,
44f238bb 12814 int internal, unsigned flags)
983af33b 12815{
023fa29b 12816 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12817 std::move (cond_string),
12818 std::move (extra_string),
e7e0cddf 12819 type_wanted,
983af33b
SDJ
12820 disposition, thread, task,
12821 ignore_count, ops, from_tty,
44f238bb 12822 enabled, internal, flags);
983af33b
SDJ
12823}
12824
6c5b2ebe 12825static std::vector<symtab_and_line>
f00aae0f
KS
12826bkpt_decode_location (struct breakpoint *b,
12827 const struct event_location *location,
6c5b2ebe 12828 struct program_space *search_pspace)
983af33b 12829{
6c5b2ebe 12830 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12831}
12832
06edf0c0
PA
12833/* Virtual table for internal breakpoints. */
12834
12835static void
12836internal_bkpt_re_set (struct breakpoint *b)
12837{
12838 switch (b->type)
12839 {
12840 /* Delete overlay event and longjmp master breakpoints; they
12841 will be reset later by breakpoint_re_set. */
12842 case bp_overlay_event:
12843 case bp_longjmp_master:
12844 case bp_std_terminate_master:
12845 case bp_exception_master:
12846 delete_breakpoint (b);
12847 break;
12848
12849 /* This breakpoint is special, it's set up when the inferior
12850 starts and we really don't want to touch it. */
12851 case bp_shlib_event:
12852
12853 /* Like bp_shlib_event, this breakpoint type is special. Once
12854 it is set up, we do not want to touch it. */
12855 case bp_thread_event:
12856 break;
12857 }
12858}
12859
12860static void
12861internal_bkpt_check_status (bpstat bs)
12862{
a9b3a50f
PA
12863 if (bs->breakpoint_at->type == bp_shlib_event)
12864 {
12865 /* If requested, stop when the dynamic linker notifies GDB of
12866 events. This allows the user to get control and place
12867 breakpoints in initializer routines for dynamically loaded
12868 objects (among other things). */
12869 bs->stop = stop_on_solib_events;
12870 bs->print = stop_on_solib_events;
12871 }
12872 else
12873 bs->stop = 0;
06edf0c0
PA
12874}
12875
12876static enum print_stop_action
12877internal_bkpt_print_it (bpstat bs)
12878{
06edf0c0 12879 struct breakpoint *b;
06edf0c0 12880
06edf0c0
PA
12881 b = bs->breakpoint_at;
12882
06edf0c0
PA
12883 switch (b->type)
12884 {
348d480f
PA
12885 case bp_shlib_event:
12886 /* Did we stop because the user set the stop_on_solib_events
12887 variable? (If so, we report this as a generic, "Stopped due
12888 to shlib event" message.) */
edcc5120 12889 print_solib_event (0);
348d480f
PA
12890 break;
12891
12892 case bp_thread_event:
12893 /* Not sure how we will get here.
12894 GDB should not stop for these breakpoints. */
12895 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12896 break;
12897
12898 case bp_overlay_event:
12899 /* By analogy with the thread event, GDB should not stop for these. */
12900 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12901 break;
12902
12903 case bp_longjmp_master:
12904 /* These should never be enabled. */
12905 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12906 break;
12907
12908 case bp_std_terminate_master:
12909 /* These should never be enabled. */
12910 printf_filtered (_("std::terminate Master Breakpoint: "
12911 "gdb should not stop!\n"));
348d480f
PA
12912 break;
12913
12914 case bp_exception_master:
12915 /* These should never be enabled. */
12916 printf_filtered (_("Exception Master Breakpoint: "
12917 "gdb should not stop!\n"));
06edf0c0
PA
12918 break;
12919 }
12920
001c8c33 12921 return PRINT_NOTHING;
06edf0c0
PA
12922}
12923
12924static void
12925internal_bkpt_print_mention (struct breakpoint *b)
12926{
12927 /* Nothing to mention. These breakpoints are internal. */
12928}
12929
06edf0c0
PA
12930/* Virtual table for momentary breakpoints */
12931
12932static void
12933momentary_bkpt_re_set (struct breakpoint *b)
12934{
12935 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12936 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12937 Otherwise these should have been blown away via the cleanup chain
12938 or by breakpoint_init_inferior when we rerun the executable. */
12939}
12940
12941static void
12942momentary_bkpt_check_status (bpstat bs)
12943{
12944 /* Nothing. The point of these breakpoints is causing a stop. */
12945}
12946
12947static enum print_stop_action
12948momentary_bkpt_print_it (bpstat bs)
12949{
001c8c33 12950 return PRINT_UNKNOWN;
348d480f
PA
12951}
12952
06edf0c0
PA
12953static void
12954momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12955{
06edf0c0 12956 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12957}
12958
e2e4d78b
JK
12959/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12960
12961 It gets cleared already on the removal of the first one of such placed
12962 breakpoints. This is OK as they get all removed altogether. */
12963
c1fc2657 12964longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12965{
c1fc2657 12966 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12967
c1fc2657 12968 if (tp != NULL)
e2e4d78b 12969 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12970}
12971
55aa24fb
SDJ
12972/* Specific methods for probe breakpoints. */
12973
12974static int
12975bkpt_probe_insert_location (struct bp_location *bl)
12976{
12977 int v = bkpt_insert_location (bl);
12978
12979 if (v == 0)
12980 {
12981 /* The insertion was successful, now let's set the probe's semaphore
12982 if needed. */
0ea5cda8
SDJ
12983 if (bl->probe.probe->pops->set_semaphore != NULL)
12984 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12985 bl->probe.objfile,
12986 bl->gdbarch);
55aa24fb
SDJ
12987 }
12988
12989 return v;
12990}
12991
12992static int
73971819
PA
12993bkpt_probe_remove_location (struct bp_location *bl,
12994 enum remove_bp_reason reason)
55aa24fb
SDJ
12995{
12996 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12997 if (bl->probe.probe->pops->clear_semaphore != NULL)
12998 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12999 bl->probe.objfile,
13000 bl->gdbarch);
55aa24fb 13001
73971819 13002 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13003}
13004
13005static void
f00aae0f 13006bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13007 struct linespec_result *canonical,
f00aae0f 13008 enum bptype type_wanted)
55aa24fb
SDJ
13009{
13010 struct linespec_sals lsal;
13011
c2f4122d 13012 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13013 lsal.canonical
13014 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13015 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
13016}
13017
6c5b2ebe 13018static std::vector<symtab_and_line>
f00aae0f
KS
13019bkpt_probe_decode_location (struct breakpoint *b,
13020 const struct event_location *location,
6c5b2ebe 13021 struct program_space *search_pspace)
55aa24fb 13022{
6c5b2ebe
PA
13023 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13024 if (sals.empty ())
55aa24fb 13025 error (_("probe not found"));
6c5b2ebe 13026 return sals;
55aa24fb
SDJ
13027}
13028
348d480f 13029/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13030
348d480f
PA
13031static void
13032tracepoint_re_set (struct breakpoint *b)
13033{
13034 breakpoint_re_set_default (b);
13035}
876fa593 13036
348d480f
PA
13037static int
13038tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13039 struct address_space *aspace, CORE_ADDR bp_addr,
13040 const struct target_waitstatus *ws)
348d480f
PA
13041{
13042 /* By definition, the inferior does not report stops at
13043 tracepoints. */
13044 return 0;
74960c60
VP
13045}
13046
13047static void
348d480f
PA
13048tracepoint_print_one_detail (const struct breakpoint *self,
13049 struct ui_out *uiout)
74960c60 13050{
d9b3f62e
PA
13051 struct tracepoint *tp = (struct tracepoint *) self;
13052 if (tp->static_trace_marker_id)
348d480f
PA
13053 {
13054 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13055
112e8700
SM
13056 uiout->text ("\tmarker id is ");
13057 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13058 tp->static_trace_marker_id);
112e8700 13059 uiout->text ("\n");
348d480f 13060 }
0d381245
VP
13061}
13062
a474d7c2 13063static void
348d480f 13064tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13065{
112e8700 13066 if (current_uiout->is_mi_like_p ())
348d480f 13067 return;
cc59ec59 13068
348d480f
PA
13069 switch (b->type)
13070 {
13071 case bp_tracepoint:
13072 printf_filtered (_("Tracepoint"));
13073 printf_filtered (_(" %d"), b->number);
13074 break;
13075 case bp_fast_tracepoint:
13076 printf_filtered (_("Fast tracepoint"));
13077 printf_filtered (_(" %d"), b->number);
13078 break;
13079 case bp_static_tracepoint:
13080 printf_filtered (_("Static tracepoint"));
13081 printf_filtered (_(" %d"), b->number);
13082 break;
13083 default:
13084 internal_error (__FILE__, __LINE__,
13085 _("unhandled tracepoint type %d"), (int) b->type);
13086 }
13087
13088 say_where (b);
a474d7c2
PA
13089}
13090
348d480f 13091static void
d9b3f62e 13092tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13093{
d9b3f62e
PA
13094 struct tracepoint *tp = (struct tracepoint *) self;
13095
13096 if (self->type == bp_fast_tracepoint)
348d480f 13097 fprintf_unfiltered (fp, "ftrace");
c93e8391 13098 else if (self->type == bp_static_tracepoint)
348d480f 13099 fprintf_unfiltered (fp, "strace");
d9b3f62e 13100 else if (self->type == bp_tracepoint)
348d480f
PA
13101 fprintf_unfiltered (fp, "trace");
13102 else
13103 internal_error (__FILE__, __LINE__,
d9b3f62e 13104 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13105
f00aae0f 13106 fprintf_unfiltered (fp, " %s",
d28cd78a 13107 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13108 print_recreate_thread (self, fp);
13109
13110 if (tp->pass_count)
13111 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13112}
13113
983af33b 13114static void
f00aae0f
KS
13115tracepoint_create_sals_from_location (const struct event_location *location,
13116 struct linespec_result *canonical,
13117 enum bptype type_wanted)
983af33b 13118{
f00aae0f 13119 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13120}
13121
13122static void
13123tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13124 struct linespec_result *canonical,
e1e01040
PA
13125 gdb::unique_xmalloc_ptr<char> cond_string,
13126 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13127 enum bptype type_wanted,
13128 enum bpdisp disposition,
13129 int thread,
13130 int task, int ignore_count,
13131 const struct breakpoint_ops *ops,
13132 int from_tty, int enabled,
44f238bb 13133 int internal, unsigned flags)
983af33b 13134{
023fa29b 13135 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13136 std::move (cond_string),
13137 std::move (extra_string),
e7e0cddf 13138 type_wanted,
983af33b
SDJ
13139 disposition, thread, task,
13140 ignore_count, ops, from_tty,
44f238bb 13141 enabled, internal, flags);
983af33b
SDJ
13142}
13143
6c5b2ebe 13144static std::vector<symtab_and_line>
f00aae0f
KS
13145tracepoint_decode_location (struct breakpoint *b,
13146 const struct event_location *location,
6c5b2ebe 13147 struct program_space *search_pspace)
983af33b 13148{
6c5b2ebe 13149 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13150}
13151
2060206e 13152struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13153
55aa24fb
SDJ
13154/* The breakpoint_ops structure to be use on tracepoints placed in a
13155 static probe. */
13156
13157static void
f00aae0f
KS
13158tracepoint_probe_create_sals_from_location
13159 (const struct event_location *location,
13160 struct linespec_result *canonical,
13161 enum bptype type_wanted)
55aa24fb
SDJ
13162{
13163 /* We use the same method for breakpoint on probes. */
f00aae0f 13164 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13165}
13166
6c5b2ebe 13167static std::vector<symtab_and_line>
f00aae0f
KS
13168tracepoint_probe_decode_location (struct breakpoint *b,
13169 const struct event_location *location,
6c5b2ebe 13170 struct program_space *search_pspace)
55aa24fb
SDJ
13171{
13172 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13173 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13174}
13175
13176static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13177
5c2b4418
HZ
13178/* Dprintf breakpoint_ops methods. */
13179
13180static void
13181dprintf_re_set (struct breakpoint *b)
13182{
13183 breakpoint_re_set_default (b);
13184
f00aae0f
KS
13185 /* extra_string should never be non-NULL for dprintf. */
13186 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13187
13188 /* 1 - connect to target 1, that can run breakpoint commands.
13189 2 - create a dprintf, which resolves fine.
13190 3 - disconnect from target 1
13191 4 - connect to target 2, that can NOT run breakpoint commands.
13192
13193 After steps #3/#4, you'll want the dprintf command list to
13194 be updated, because target 1 and 2 may well return different
13195 answers for target_can_run_breakpoint_commands().
13196 Given absence of finer grained resetting, we get to do
13197 it all the time. */
13198 if (b->extra_string != NULL)
13199 update_dprintf_command_list (b);
13200}
13201
2d9442cc
HZ
13202/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13203
13204static void
13205dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13206{
f00aae0f 13207 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13208 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13209 tp->extra_string);
13210 print_recreate_thread (tp, fp);
13211}
13212
9d6e6e84
HZ
13213/* Implement the "after_condition_true" breakpoint_ops method for
13214 dprintf.
13215
13216 dprintf's are implemented with regular commands in their command
13217 list, but we run the commands here instead of before presenting the
13218 stop to the user, as dprintf's don't actually cause a stop. This
13219 also makes it so that the commands of multiple dprintfs at the same
13220 address are all handled. */
13221
13222static void
13223dprintf_after_condition_true (struct bpstats *bs)
13224{
04afa70c 13225 struct bpstats tmp_bs;
9d6e6e84
HZ
13226 struct bpstats *tmp_bs_p = &tmp_bs;
13227
13228 /* dprintf's never cause a stop. This wasn't set in the
13229 check_status hook instead because that would make the dprintf's
13230 condition not be evaluated. */
13231 bs->stop = 0;
13232
13233 /* Run the command list here. Take ownership of it instead of
13234 copying. We never want these commands to run later in
13235 bpstat_do_actions, if a breakpoint that causes a stop happens to
13236 be set at same address as this dprintf, or even if running the
13237 commands here throws. */
13238 tmp_bs.commands = bs->commands;
13239 bs->commands = NULL;
9d6e6e84
HZ
13240
13241 bpstat_do_actions_1 (&tmp_bs_p);
13242
13243 /* 'tmp_bs.commands' will usually be NULL by now, but
13244 bpstat_do_actions_1 may return early without processing the whole
13245 list. */
9d6e6e84
HZ
13246}
13247
983af33b
SDJ
13248/* The breakpoint_ops structure to be used on static tracepoints with
13249 markers (`-m'). */
13250
13251static void
f00aae0f 13252strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13253 struct linespec_result *canonical,
f00aae0f 13254 enum bptype type_wanted)
983af33b
SDJ
13255{
13256 struct linespec_sals lsal;
f00aae0f 13257 const char *arg_start, *arg;
983af33b 13258
f00aae0f
KS
13259 arg = arg_start = get_linespec_location (location);
13260 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13261
f2fc3015
TT
13262 std::string str (arg_start, arg - arg_start);
13263 const char *ptr = str.c_str ();
13264 canonical->location = new_linespec_location (&ptr);
983af33b 13265
8e9e35b1
TT
13266 lsal.canonical
13267 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13268 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13269}
13270
13271static void
13272strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13273 struct linespec_result *canonical,
e1e01040
PA
13274 gdb::unique_xmalloc_ptr<char> cond_string,
13275 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13276 enum bptype type_wanted,
13277 enum bpdisp disposition,
13278 int thread,
13279 int task, int ignore_count,
13280 const struct breakpoint_ops *ops,
13281 int from_tty, int enabled,
44f238bb 13282 int internal, unsigned flags)
983af33b 13283{
6c5b2ebe 13284 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13285
13286 /* If the user is creating a static tracepoint by marker id
13287 (strace -m MARKER_ID), then store the sals index, so that
13288 breakpoint_re_set can try to match up which of the newly
13289 found markers corresponds to this one, and, don't try to
13290 expand multiple locations for each sal, given than SALS
13291 already should contain all sals for MARKER_ID. */
13292
6c5b2ebe 13293 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13294 {
6c5b2ebe
PA
13295 event_location_up location
13296 = copy_event_location (canonical->location.get ());
983af33b 13297
b270e6f9 13298 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13299 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13300 std::move (location), NULL,
e1e01040
PA
13301 std::move (cond_string),
13302 std::move (extra_string),
e7e0cddf 13303 type_wanted, disposition,
983af33b 13304 thread, task, ignore_count, ops,
44f238bb 13305 from_tty, enabled, internal, flags,
983af33b
SDJ
13306 canonical->special_display);
13307 /* Given that its possible to have multiple markers with
13308 the same string id, if the user is creating a static
13309 tracepoint by marker id ("strace -m MARKER_ID"), then
13310 store the sals index, so that breakpoint_re_set can
13311 try to match up which of the newly found markers
13312 corresponds to this one */
13313 tp->static_trace_marker_id_idx = i;
13314
b270e6f9 13315 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13316 }
13317}
13318
6c5b2ebe 13319static std::vector<symtab_and_line>
f00aae0f
KS
13320strace_marker_decode_location (struct breakpoint *b,
13321 const struct event_location *location,
6c5b2ebe 13322 struct program_space *search_pspace)
983af33b
SDJ
13323{
13324 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13325 const char *s = get_linespec_location (location);
983af33b 13326
6c5b2ebe
PA
13327 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13328 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13329 {
6c5b2ebe
PA
13330 sals[0] = sals[tp->static_trace_marker_id_idx];
13331 sals.resize (1);
13332 return sals;
983af33b
SDJ
13333 }
13334 else
13335 error (_("marker %s not found"), tp->static_trace_marker_id);
13336}
13337
13338static struct breakpoint_ops strace_marker_breakpoint_ops;
13339
13340static int
13341strace_marker_p (struct breakpoint *b)
13342{
13343 return b->ops == &strace_marker_breakpoint_ops;
13344}
13345
53a5351d 13346/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13347 structures. */
c906108c
SS
13348
13349void
fba45db2 13350delete_breakpoint (struct breakpoint *bpt)
c906108c 13351{
52f0bd74 13352 struct breakpoint *b;
c906108c 13353
8a3fe4f8 13354 gdb_assert (bpt != NULL);
c906108c 13355
4a64f543
MS
13356 /* Has this bp already been deleted? This can happen because
13357 multiple lists can hold pointers to bp's. bpstat lists are
13358 especial culprits.
13359
13360 One example of this happening is a watchpoint's scope bp. When
13361 the scope bp triggers, we notice that the watchpoint is out of
13362 scope, and delete it. We also delete its scope bp. But the
13363 scope bp is marked "auto-deleting", and is already on a bpstat.
13364 That bpstat is then checked for auto-deleting bp's, which are
13365 deleted.
13366
13367 A real solution to this problem might involve reference counts in
13368 bp's, and/or giving them pointers back to their referencing
13369 bpstat's, and teaching delete_breakpoint to only free a bp's
13370 storage when no more references were extent. A cheaper bandaid
13371 was chosen. */
c906108c
SS
13372 if (bpt->type == bp_none)
13373 return;
13374
4a64f543
MS
13375 /* At least avoid this stale reference until the reference counting
13376 of breakpoints gets resolved. */
d0fb5eae 13377 if (bpt->related_breakpoint != bpt)
e5a0a904 13378 {
d0fb5eae 13379 struct breakpoint *related;
3a5c3e22 13380 struct watchpoint *w;
d0fb5eae
JK
13381
13382 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13383 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13384 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13385 w = (struct watchpoint *) bpt;
13386 else
13387 w = NULL;
13388 if (w != NULL)
13389 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13390
13391 /* Unlink bpt from the bpt->related_breakpoint ring. */
13392 for (related = bpt; related->related_breakpoint != bpt;
13393 related = related->related_breakpoint);
13394 related->related_breakpoint = bpt->related_breakpoint;
13395 bpt->related_breakpoint = bpt;
e5a0a904
JK
13396 }
13397
a9634178
TJB
13398 /* watch_command_1 creates a watchpoint but only sets its number if
13399 update_watchpoint succeeds in creating its bp_locations. If there's
13400 a problem in that process, we'll be asked to delete the half-created
13401 watchpoint. In that case, don't announce the deletion. */
13402 if (bpt->number)
13403 observer_notify_breakpoint_deleted (bpt);
c906108c 13404
c906108c
SS
13405 if (breakpoint_chain == bpt)
13406 breakpoint_chain = bpt->next;
13407
c906108c
SS
13408 ALL_BREAKPOINTS (b)
13409 if (b->next == bpt)
c5aa993b
JM
13410 {
13411 b->next = bpt->next;
13412 break;
13413 }
c906108c 13414
f431efe5
PA
13415 /* Be sure no bpstat's are pointing at the breakpoint after it's
13416 been freed. */
13417 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13418 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13419 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13420 commands are associated with the bpstat; if we remove it here,
13421 then the later call to bpstat_do_actions (&stop_bpstat); in
13422 event-top.c won't do anything, and temporary breakpoints with
13423 commands won't work. */
13424
13425 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13426
4a64f543
MS
13427 /* Now that breakpoint is removed from breakpoint list, update the
13428 global location list. This will remove locations that used to
13429 belong to this breakpoint. Do this before freeing the breakpoint
13430 itself, since remove_breakpoint looks at location's owner. It
13431 might be better design to have location completely
13432 self-contained, but it's not the case now. */
44702360 13433 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13434
4a64f543
MS
13435 /* On the chance that someone will soon try again to delete this
13436 same bp, we mark it as deleted before freeing its storage. */
c906108c 13437 bpt->type = bp_none;
4d01a485 13438 delete bpt;
c906108c
SS
13439}
13440
4d6140d9
AC
13441static void
13442do_delete_breakpoint_cleanup (void *b)
13443{
9a3c8263 13444 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13445}
13446
13447struct cleanup *
13448make_cleanup_delete_breakpoint (struct breakpoint *b)
13449{
13450 return make_cleanup (do_delete_breakpoint_cleanup, b);
13451}
13452
51be5b68
PA
13453/* Iterator function to call a user-provided callback function once
13454 for each of B and its related breakpoints. */
13455
13456static void
13457iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13458 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13459{
13460 struct breakpoint *related;
13461
13462 related = b;
13463 do
13464 {
13465 struct breakpoint *next;
13466
13467 /* FUNCTION may delete RELATED. */
13468 next = related->related_breakpoint;
13469
13470 if (next == related)
13471 {
13472 /* RELATED is the last ring entry. */
48649e1b 13473 function (related);
51be5b68
PA
13474
13475 /* FUNCTION may have deleted it, so we'd never reach back to
13476 B. There's nothing left to do anyway, so just break
13477 out. */
13478 break;
13479 }
13480 else
48649e1b 13481 function (related);
51be5b68
PA
13482
13483 related = next;
13484 }
13485 while (related != b);
13486}
95a42b64 13487
4495129a 13488static void
fba45db2 13489delete_command (char *arg, int from_tty)
c906108c 13490{
35df4500 13491 struct breakpoint *b, *b_tmp;
c906108c 13492
ea9365bb
TT
13493 dont_repeat ();
13494
c906108c
SS
13495 if (arg == 0)
13496 {
13497 int breaks_to_delete = 0;
13498
46c6471b
PA
13499 /* Delete all breakpoints if no argument. Do not delete
13500 internal breakpoints, these have to be deleted with an
13501 explicit breakpoint number argument. */
c5aa993b 13502 ALL_BREAKPOINTS (b)
46c6471b 13503 if (user_breakpoint_p (b))
973d738b
DJ
13504 {
13505 breaks_to_delete = 1;
13506 break;
13507 }
c906108c
SS
13508
13509 /* Ask user only if there are some breakpoints to delete. */
13510 if (!from_tty
e2e0b3e5 13511 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13512 {
35df4500 13513 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13514 if (user_breakpoint_p (b))
c5aa993b 13515 delete_breakpoint (b);
c906108c
SS
13516 }
13517 }
13518 else
48649e1b
TT
13519 map_breakpoint_numbers
13520 (arg, [&] (breakpoint *b)
13521 {
13522 iterate_over_related_breakpoints (b, delete_breakpoint);
13523 });
c906108c
SS
13524}
13525
c2f4122d
PA
13526/* Return true if all locations of B bound to PSPACE are pending. If
13527 PSPACE is NULL, all locations of all program spaces are
13528 considered. */
13529
0d381245 13530static int
c2f4122d 13531all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13532{
c2f4122d
PA
13533 struct bp_location *loc;
13534
13535 for (loc = b->loc; loc != NULL; loc = loc->next)
13536 if ((pspace == NULL
13537 || loc->pspace == pspace)
13538 && !loc->shlib_disabled
8645ff69 13539 && !loc->pspace->executing_startup)
0d381245
VP
13540 return 0;
13541 return 1;
fe3f5fa8
VP
13542}
13543
776592bf
DE
13544/* Subroutine of update_breakpoint_locations to simplify it.
13545 Return non-zero if multiple fns in list LOC have the same name.
13546 Null names are ignored. */
13547
13548static int
13549ambiguous_names_p (struct bp_location *loc)
13550{
13551 struct bp_location *l;
13552 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13553 (int (*) (const void *,
13554 const void *)) streq,
776592bf
DE
13555 NULL, xcalloc, xfree);
13556
13557 for (l = loc; l != NULL; l = l->next)
13558 {
13559 const char **slot;
13560 const char *name = l->function_name;
13561
13562 /* Allow for some names to be NULL, ignore them. */
13563 if (name == NULL)
13564 continue;
13565
13566 slot = (const char **) htab_find_slot (htab, (const void *) name,
13567 INSERT);
4a64f543
MS
13568 /* NOTE: We can assume slot != NULL here because xcalloc never
13569 returns NULL. */
776592bf
DE
13570 if (*slot != NULL)
13571 {
13572 htab_delete (htab);
13573 return 1;
13574 }
13575 *slot = name;
13576 }
13577
13578 htab_delete (htab);
13579 return 0;
13580}
13581
0fb4aa4b
PA
13582/* When symbols change, it probably means the sources changed as well,
13583 and it might mean the static tracepoint markers are no longer at
13584 the same address or line numbers they used to be at last we
13585 checked. Losing your static tracepoints whenever you rebuild is
13586 undesirable. This function tries to resync/rematch gdb static
13587 tracepoints with the markers on the target, for static tracepoints
13588 that have not been set by marker id. Static tracepoint that have
13589 been set by marker id are reset by marker id in breakpoint_re_set.
13590 The heuristic is:
13591
13592 1) For a tracepoint set at a specific address, look for a marker at
13593 the old PC. If one is found there, assume to be the same marker.
13594 If the name / string id of the marker found is different from the
13595 previous known name, assume that means the user renamed the marker
13596 in the sources, and output a warning.
13597
13598 2) For a tracepoint set at a given line number, look for a marker
13599 at the new address of the old line number. If one is found there,
13600 assume to be the same marker. If the name / string id of the
13601 marker found is different from the previous known name, assume that
13602 means the user renamed the marker in the sources, and output a
13603 warning.
13604
13605 3) If a marker is no longer found at the same address or line, it
13606 may mean the marker no longer exists. But it may also just mean
13607 the code changed a bit. Maybe the user added a few lines of code
13608 that made the marker move up or down (in line number terms). Ask
13609 the target for info about the marker with the string id as we knew
13610 it. If found, update line number and address in the matching
13611 static tracepoint. This will get confused if there's more than one
13612 marker with the same ID (possible in UST, although unadvised
13613 precisely because it confuses tools). */
13614
13615static struct symtab_and_line
13616update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13617{
d9b3f62e 13618 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13619 struct static_tracepoint_marker marker;
13620 CORE_ADDR pc;
0fb4aa4b
PA
13621
13622 pc = sal.pc;
13623 if (sal.line)
13624 find_line_pc (sal.symtab, sal.line, &pc);
13625
13626 if (target_static_tracepoint_marker_at (pc, &marker))
13627 {
d9b3f62e 13628 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13629 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13630 b->number,
d9b3f62e 13631 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13632
d9b3f62e
PA
13633 xfree (tp->static_trace_marker_id);
13634 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13635 release_static_tracepoint_marker (&marker);
13636
13637 return sal;
13638 }
13639
13640 /* Old marker wasn't found on target at lineno. Try looking it up
13641 by string ID. */
13642 if (!sal.explicit_pc
13643 && sal.line != 0
13644 && sal.symtab != NULL
d9b3f62e 13645 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13646 {
13647 VEC(static_tracepoint_marker_p) *markers;
13648
13649 markers
d9b3f62e 13650 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13651
13652 if (!VEC_empty(static_tracepoint_marker_p, markers))
13653 {
0fb4aa4b 13654 struct symbol *sym;
80e1d417 13655 struct static_tracepoint_marker *tpmarker;
79a45e25 13656 struct ui_out *uiout = current_uiout;
67994074 13657 struct explicit_location explicit_loc;
0fb4aa4b 13658
80e1d417 13659 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13660
d9b3f62e 13661 xfree (tp->static_trace_marker_id);
80e1d417 13662 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13663
13664 warning (_("marker for static tracepoint %d (%s) not "
13665 "found at previous line number"),
d9b3f62e 13666 b->number, tp->static_trace_marker_id);
0fb4aa4b 13667
51abb421 13668 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13669 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13670 uiout->text ("Now in ");
0fb4aa4b
PA
13671 if (sym)
13672 {
112e8700
SM
13673 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13674 uiout->text (" at ");
0fb4aa4b 13675 }
112e8700 13676 uiout->field_string ("file",
05cba821 13677 symtab_to_filename_for_display (sal2.symtab));
112e8700 13678 uiout->text (":");
0fb4aa4b 13679
112e8700 13680 if (uiout->is_mi_like_p ())
0fb4aa4b 13681 {
0b0865da 13682 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13683
112e8700 13684 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13685 }
13686
112e8700
SM
13687 uiout->field_int ("line", sal2.line);
13688 uiout->text ("\n");
0fb4aa4b 13689
80e1d417 13690 b->loc->line_number = sal2.line;
2f202fde 13691 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13692
d28cd78a 13693 b->location.reset (NULL);
67994074
KS
13694 initialize_explicit_location (&explicit_loc);
13695 explicit_loc.source_filename
00e52e53 13696 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13697 explicit_loc.line_offset.offset = b->loc->line_number;
13698 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13699 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13700
13701 /* Might be nice to check if function changed, and warn if
13702 so. */
13703
80e1d417 13704 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13705 }
13706 }
13707 return sal;
13708}
13709
8d3788bd
VP
13710/* Returns 1 iff locations A and B are sufficiently same that
13711 we don't need to report breakpoint as changed. */
13712
13713static int
13714locations_are_equal (struct bp_location *a, struct bp_location *b)
13715{
13716 while (a && b)
13717 {
13718 if (a->address != b->address)
13719 return 0;
13720
13721 if (a->shlib_disabled != b->shlib_disabled)
13722 return 0;
13723
13724 if (a->enabled != b->enabled)
13725 return 0;
13726
13727 a = a->next;
13728 b = b->next;
13729 }
13730
13731 if ((a == NULL) != (b == NULL))
13732 return 0;
13733
13734 return 1;
13735}
13736
c2f4122d
PA
13737/* Split all locations of B that are bound to PSPACE out of B's
13738 location list to a separate list and return that list's head. If
13739 PSPACE is NULL, hoist out all locations of B. */
13740
13741static struct bp_location *
13742hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13743{
13744 struct bp_location head;
13745 struct bp_location *i = b->loc;
13746 struct bp_location **i_link = &b->loc;
13747 struct bp_location *hoisted = &head;
13748
13749 if (pspace == NULL)
13750 {
13751 i = b->loc;
13752 b->loc = NULL;
13753 return i;
13754 }
13755
13756 head.next = NULL;
13757
13758 while (i != NULL)
13759 {
13760 if (i->pspace == pspace)
13761 {
13762 *i_link = i->next;
13763 i->next = NULL;
13764 hoisted->next = i;
13765 hoisted = i;
13766 }
13767 else
13768 i_link = &i->next;
13769 i = *i_link;
13770 }
13771
13772 return head.next;
13773}
13774
13775/* Create new breakpoint locations for B (a hardware or software
13776 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13777 zero, then B is a ranged breakpoint. Only recreates locations for
13778 FILTER_PSPACE. Locations of other program spaces are left
13779 untouched. */
f1310107 13780
0e30163f 13781void
0d381245 13782update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13783 struct program_space *filter_pspace,
6c5b2ebe
PA
13784 gdb::array_view<const symtab_and_line> sals,
13785 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13786{
13787 int i;
c2f4122d 13788 struct bp_location *existing_locations;
0d381245 13789
6c5b2ebe 13790 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13791 {
13792 /* Ranged breakpoints have only one start location and one end
13793 location. */
13794 b->enable_state = bp_disabled;
f8eba3c6
TT
13795 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13796 "multiple locations found\n"),
13797 b->number);
13798 return;
13799 }
f1310107 13800
4a64f543
MS
13801 /* If there's no new locations, and all existing locations are
13802 pending, don't do anything. This optimizes the common case where
13803 all locations are in the same shared library, that was unloaded.
13804 We'd like to retain the location, so that when the library is
13805 loaded again, we don't loose the enabled/disabled status of the
13806 individual locations. */
6c5b2ebe 13807 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13808 return;
13809
c2f4122d 13810 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13811
6c5b2ebe 13812 for (const auto &sal : sals)
fe3f5fa8 13813 {
f8eba3c6
TT
13814 struct bp_location *new_loc;
13815
6c5b2ebe 13816 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13817
6c5b2ebe 13818 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13819
0d381245
VP
13820 /* Reparse conditions, they might contain references to the
13821 old symtab. */
13822 if (b->cond_string != NULL)
13823 {
bbc13ae3 13824 const char *s;
fe3f5fa8 13825
0d381245 13826 s = b->cond_string;
492d29ea 13827 TRY
0d381245 13828 {
6c5b2ebe
PA
13829 new_loc->cond = parse_exp_1 (&s, sal.pc,
13830 block_for_pc (sal.pc),
0d381245
VP
13831 0);
13832 }
492d29ea 13833 CATCH (e, RETURN_MASK_ERROR)
0d381245 13834 {
3e43a32a
MS
13835 warning (_("failed to reevaluate condition "
13836 "for breakpoint %d: %s"),
0d381245
VP
13837 b->number, e.message);
13838 new_loc->enabled = 0;
13839 }
492d29ea 13840 END_CATCH
0d381245 13841 }
fe3f5fa8 13842
6c5b2ebe 13843 if (!sals_end.empty ())
f1310107 13844 {
6c5b2ebe 13845 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13846
6c5b2ebe 13847 new_loc->length = end - sals[0].pc + 1;
f1310107 13848 }
0d381245 13849 }
fe3f5fa8 13850
4a64f543
MS
13851 /* If possible, carry over 'disable' status from existing
13852 breakpoints. */
0d381245
VP
13853 {
13854 struct bp_location *e = existing_locations;
776592bf
DE
13855 /* If there are multiple breakpoints with the same function name,
13856 e.g. for inline functions, comparing function names won't work.
13857 Instead compare pc addresses; this is just a heuristic as things
13858 may have moved, but in practice it gives the correct answer
13859 often enough until a better solution is found. */
13860 int have_ambiguous_names = ambiguous_names_p (b->loc);
13861
0d381245
VP
13862 for (; e; e = e->next)
13863 {
13864 if (!e->enabled && e->function_name)
13865 {
13866 struct bp_location *l = b->loc;
776592bf
DE
13867 if (have_ambiguous_names)
13868 {
13869 for (; l; l = l->next)
f1310107 13870 if (breakpoint_locations_match (e, l))
776592bf
DE
13871 {
13872 l->enabled = 0;
13873 break;
13874 }
13875 }
13876 else
13877 {
13878 for (; l; l = l->next)
13879 if (l->function_name
13880 && strcmp (e->function_name, l->function_name) == 0)
13881 {
13882 l->enabled = 0;
13883 break;
13884 }
13885 }
0d381245
VP
13886 }
13887 }
13888 }
fe3f5fa8 13889
8d3788bd
VP
13890 if (!locations_are_equal (existing_locations, b->loc))
13891 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13892}
13893
f00aae0f 13894/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13895 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13896
6c5b2ebe 13897static std::vector<symtab_and_line>
f00aae0f 13898location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13899 struct program_space *search_pspace, int *found)
ef23e705 13900{
492d29ea 13901 struct gdb_exception exception = exception_none;
ef23e705 13902
983af33b 13903 gdb_assert (b->ops != NULL);
ef23e705 13904
6c5b2ebe
PA
13905 std::vector<symtab_and_line> sals;
13906
492d29ea 13907 TRY
ef23e705 13908 {
6c5b2ebe 13909 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13910 }
492d29ea 13911 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13912 {
13913 int not_found_and_ok = 0;
492d29ea
PA
13914
13915 exception = e;
13916
ef23e705
TJB
13917 /* For pending breakpoints, it's expected that parsing will
13918 fail until the right shared library is loaded. User has
13919 already told to create pending breakpoints and don't need
13920 extra messages. If breakpoint is in bp_shlib_disabled
13921 state, then user already saw the message about that
13922 breakpoint being disabled, and don't want to see more
13923 errors. */
58438ac1 13924 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13925 && (b->condition_not_parsed
13926 || (b->loc != NULL
13927 && search_pspace != NULL
13928 && b->loc->pspace != search_pspace)
ef23e705 13929 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13930 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13931 || b->enable_state == bp_disabled))
13932 not_found_and_ok = 1;
13933
13934 if (!not_found_and_ok)
13935 {
13936 /* We surely don't want to warn about the same breakpoint
13937 10 times. One solution, implemented here, is disable
13938 the breakpoint on error. Another solution would be to
13939 have separate 'warning emitted' flag. Since this
13940 happens only when a binary has changed, I don't know
13941 which approach is better. */
13942 b->enable_state = bp_disabled;
13943 throw_exception (e);
13944 }
13945 }
492d29ea 13946 END_CATCH
ef23e705 13947
492d29ea 13948 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13949 {
6c5b2ebe
PA
13950 for (auto &sal : sals)
13951 resolve_sal_pc (&sal);
f00aae0f 13952 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13953 {
ed1d1739
KS
13954 char *cond_string, *extra_string;
13955 int thread, task;
ef23e705 13956
6c5b2ebe 13957 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13958 &cond_string, &thread, &task,
13959 &extra_string);
f00aae0f 13960 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13961 if (cond_string)
13962 b->cond_string = cond_string;
13963 b->thread = thread;
13964 b->task = task;
e7e0cddf 13965 if (extra_string)
f00aae0f
KS
13966 {
13967 xfree (b->extra_string);
13968 b->extra_string = extra_string;
13969 }
ef23e705
TJB
13970 b->condition_not_parsed = 0;
13971 }
13972
983af33b 13973 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13974 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13975
58438ac1
TT
13976 *found = 1;
13977 }
13978 else
13979 *found = 0;
ef23e705
TJB
13980
13981 return sals;
13982}
13983
348d480f
PA
13984/* The default re_set method, for typical hardware or software
13985 breakpoints. Reevaluate the breakpoint and recreate its
13986 locations. */
13987
13988static void
28010a5d 13989breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13990{
c2f4122d 13991 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13992 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13993
6c5b2ebe
PA
13994 int found;
13995 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13996 filter_pspace, &found);
ef23e705 13997 if (found)
6c5b2ebe 13998 expanded = std::move (sals);
ef23e705 13999
f00aae0f 14000 if (b->location_range_end != NULL)
f1310107 14001 {
6c5b2ebe
PA
14002 std::vector<symtab_and_line> sals_end
14003 = location_to_sals (b, b->location_range_end.get (),
14004 filter_pspace, &found);
f1310107 14005 if (found)
6c5b2ebe 14006 expanded_end = std::move (sals_end);
f1310107
TJB
14007 }
14008
c2f4122d 14009 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14010}
14011
983af33b
SDJ
14012/* Default method for creating SALs from an address string. It basically
14013 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14014
14015static void
f00aae0f
KS
14016create_sals_from_location_default (const struct event_location *location,
14017 struct linespec_result *canonical,
14018 enum bptype type_wanted)
983af33b 14019{
f00aae0f 14020 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14021}
14022
14023/* Call create_breakpoints_sal for the given arguments. This is the default
14024 function for the `create_breakpoints_sal' method of
14025 breakpoint_ops. */
14026
14027static void
14028create_breakpoints_sal_default (struct gdbarch *gdbarch,
14029 struct linespec_result *canonical,
e1e01040
PA
14030 gdb::unique_xmalloc_ptr<char> cond_string,
14031 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14032 enum bptype type_wanted,
14033 enum bpdisp disposition,
14034 int thread,
14035 int task, int ignore_count,
14036 const struct breakpoint_ops *ops,
14037 int from_tty, int enabled,
44f238bb 14038 int internal, unsigned flags)
983af33b 14039{
e1e01040
PA
14040 create_breakpoints_sal (gdbarch, canonical,
14041 std::move (cond_string),
14042 std::move (extra_string),
983af33b
SDJ
14043 type_wanted, disposition,
14044 thread, task, ignore_count, ops, from_tty,
44f238bb 14045 enabled, internal, flags);
983af33b
SDJ
14046}
14047
14048/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14049 default function for the `decode_location' method of breakpoint_ops. */
983af33b 14050
6c5b2ebe 14051static std::vector<symtab_and_line>
f00aae0f
KS
14052decode_location_default (struct breakpoint *b,
14053 const struct event_location *location,
6c5b2ebe 14054 struct program_space *search_pspace)
983af33b
SDJ
14055{
14056 struct linespec_result canonical;
14057
c2f4122d 14058 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14059 (struct symtab *) NULL, 0,
14060 &canonical, multiple_symbols_all,
14061 b->filter);
14062
14063 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 14064 gdb_assert (canonical.lsals.size () < 2);
983af33b 14065
6c5b2ebe 14066 if (!canonical.lsals.empty ())
983af33b 14067 {
6c5b2ebe
PA
14068 const linespec_sals &lsal = canonical.lsals[0];
14069 return std::move (lsal.sals);
983af33b 14070 }
6c5b2ebe 14071 return {};
983af33b
SDJ
14072}
14073
28010a5d
PA
14074/* Prepare the global context for a re-set of breakpoint B. */
14075
14076static struct cleanup *
14077prepare_re_set_context (struct breakpoint *b)
14078{
28010a5d 14079 input_radix = b->input_radix;
28010a5d
PA
14080 set_language (b->language);
14081
c2f4122d 14082 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14083}
14084
bf469271 14085/* Reset a breakpoint. */
c906108c 14086
bf469271
PA
14087static void
14088breakpoint_re_set_one (breakpoint *b)
c906108c 14089{
348d480f 14090 struct cleanup *cleanups;
c906108c 14091
348d480f
PA
14092 cleanups = prepare_re_set_context (b);
14093 b->ops->re_set (b);
14094 do_cleanups (cleanups);
c906108c
SS
14095}
14096
c2f4122d
PA
14097/* Re-set breakpoint locations for the current program space.
14098 Locations bound to other program spaces are left untouched. */
14099
c906108c 14100void
69de3c6a 14101breakpoint_re_set (void)
c906108c 14102{
35df4500 14103 struct breakpoint *b, *b_tmp;
c906108c
SS
14104 enum language save_language;
14105 int save_input_radix;
c5aa993b 14106
c906108c
SS
14107 save_language = current_language->la_language;
14108 save_input_radix = input_radix;
2a7f3dff 14109
c5aa993b 14110 {
5ed8105e 14111 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14112
5ed8105e
PA
14113 /* Note: we must not try to insert locations until after all
14114 breakpoints have been re-set. Otherwise, e.g., when re-setting
14115 breakpoint 1, we'd insert the locations of breakpoint 2, which
14116 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14117
5ed8105e
PA
14118 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14119 {
bf469271
PA
14120 TRY
14121 {
14122 breakpoint_re_set_one (b);
14123 }
14124 CATCH (ex, RETURN_MASK_ALL)
14125 {
14126 exception_fprintf (gdb_stderr, ex,
14127 "Error in re-setting breakpoint %d: ",
14128 b->number);
14129 }
14130 END_CATCH
5ed8105e
PA
14131 }
14132 set_language (save_language);
14133 input_radix = save_input_radix;
14134
14135 jit_breakpoint_re_set ();
14136 }
6c95b8df 14137
af02033e
PP
14138 create_overlay_event_breakpoint ();
14139 create_longjmp_master_breakpoint ();
14140 create_std_terminate_master_breakpoint ();
186c406b 14141 create_exception_master_breakpoint ();
2a7f3dff
PA
14142
14143 /* Now we can insert. */
14144 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14145}
14146\f
c906108c
SS
14147/* Reset the thread number of this breakpoint:
14148
14149 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14150 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14151void
fba45db2 14152breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14153{
14154 if (b->thread != -1)
14155 {
39f77062 14156 if (in_thread_list (inferior_ptid))
5d5658a1 14157 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14158
14159 /* We're being called after following a fork. The new fork is
14160 selected as current, and unless this was a vfork will have a
14161 different program space from the original thread. Reset that
14162 as well. */
14163 b->loc->pspace = current_program_space;
c906108c
SS
14164 }
14165}
14166
03ac34d5
MS
14167/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14168 If from_tty is nonzero, it prints a message to that effect,
14169 which ends with a period (no newline). */
14170
c906108c 14171void
fba45db2 14172set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14173{
52f0bd74 14174 struct breakpoint *b;
c906108c
SS
14175
14176 if (count < 0)
14177 count = 0;
14178
14179 ALL_BREAKPOINTS (b)
14180 if (b->number == bptnum)
c5aa993b 14181 {
d77f58be
SS
14182 if (is_tracepoint (b))
14183 {
14184 if (from_tty && count != 0)
14185 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14186 bptnum);
14187 return;
14188 }
14189
c5aa993b 14190 b->ignore_count = count;
221ea385
KS
14191 if (from_tty)
14192 {
14193 if (count == 0)
3e43a32a
MS
14194 printf_filtered (_("Will stop next time "
14195 "breakpoint %d is reached."),
221ea385
KS
14196 bptnum);
14197 else if (count == 1)
a3f17187 14198 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14199 bptnum);
14200 else
3e43a32a
MS
14201 printf_filtered (_("Will ignore next %d "
14202 "crossings of breakpoint %d."),
221ea385
KS
14203 count, bptnum);
14204 }
8d3788bd 14205 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14206 return;
14207 }
c906108c 14208
8a3fe4f8 14209 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14210}
14211
c906108c
SS
14212/* Command to set ignore-count of breakpoint N to COUNT. */
14213
14214static void
fba45db2 14215ignore_command (char *args, int from_tty)
c906108c
SS
14216{
14217 char *p = args;
52f0bd74 14218 int num;
c906108c
SS
14219
14220 if (p == 0)
e2e0b3e5 14221 error_no_arg (_("a breakpoint number"));
c5aa993b 14222
c906108c 14223 num = get_number (&p);
5c44784c 14224 if (num == 0)
8a3fe4f8 14225 error (_("bad breakpoint number: '%s'"), args);
c906108c 14226 if (*p == 0)
8a3fe4f8 14227 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14228
14229 set_ignore_count (num,
14230 longest_to_int (value_as_long (parse_and_eval (p))),
14231 from_tty);
221ea385
KS
14232 if (from_tty)
14233 printf_filtered ("\n");
c906108c
SS
14234}
14235\f
14236/* Call FUNCTION on each of the breakpoints
14237 whose numbers are given in ARGS. */
14238
14239static void
896b6bda 14240map_breakpoint_numbers (const char *args,
48649e1b 14241 gdb::function_view<void (breakpoint *)> function)
c906108c 14242{
52f0bd74
AC
14243 int num;
14244 struct breakpoint *b, *tmp;
c906108c 14245
b9d61307 14246 if (args == 0 || *args == '\0')
e2e0b3e5 14247 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14248
bfd28288 14249 number_or_range_parser parser (args);
197f0a60 14250
bfd28288 14251 while (!parser.finished ())
c906108c 14252 {
bfd28288
PA
14253 const char *p = parser.cur_tok ();
14254 bool match = false;
197f0a60 14255
bfd28288 14256 num = parser.get_number ();
5c44784c 14257 if (num == 0)
c5aa993b 14258 {
8a3fe4f8 14259 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14260 }
14261 else
14262 {
14263 ALL_BREAKPOINTS_SAFE (b, tmp)
14264 if (b->number == num)
14265 {
bfd28288 14266 match = true;
48649e1b 14267 function (b);
11cf8741 14268 break;
5c44784c 14269 }
bfd28288 14270 if (!match)
a3f17187 14271 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14272 }
c906108c
SS
14273 }
14274}
14275
0d381245 14276static struct bp_location *
cb791d59 14277find_location_by_number (const char *number)
0d381245 14278{
cb791d59 14279 const char *p1;
0d381245
VP
14280 int bp_num;
14281 int loc_num;
14282 struct breakpoint *b;
14283 struct bp_location *loc;
14284
0d381245 14285 p1 = number;
cb791d59
TT
14286 bp_num = get_number_trailer (&p1, '.');
14287 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14288 error (_("Bad breakpoint number '%s'"), number);
14289
14290 ALL_BREAKPOINTS (b)
14291 if (b->number == bp_num)
14292 {
14293 break;
14294 }
14295
14296 if (!b || b->number != bp_num)
14297 error (_("Bad breakpoint number '%s'"), number);
14298
cb791d59
TT
14299 /* Skip the dot. */
14300 ++p1;
14301 const char *save = p1;
197f0a60 14302 loc_num = get_number (&p1);
0d381245
VP
14303 if (loc_num == 0)
14304 error (_("Bad breakpoint location number '%s'"), number);
14305
14306 --loc_num;
14307 loc = b->loc;
14308 for (;loc_num && loc; --loc_num, loc = loc->next)
14309 ;
14310 if (!loc)
cb791d59 14311 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14312
14313 return loc;
14314}
14315
14316
1900040c
MS
14317/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14318 If from_tty is nonzero, it prints a message to that effect,
14319 which ends with a period (no newline). */
14320
c906108c 14321void
fba45db2 14322disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14323{
14324 /* Never disable a watchpoint scope breakpoint; we want to
14325 hit them when we leave scope so we can delete both the
14326 watchpoint and its scope breakpoint at that time. */
14327 if (bpt->type == bp_watchpoint_scope)
14328 return;
14329
b5de0fa7 14330 bpt->enable_state = bp_disabled;
c906108c 14331
b775012e
LM
14332 /* Mark breakpoint locations modified. */
14333 mark_breakpoint_modified (bpt);
14334
d248b706
KY
14335 if (target_supports_enable_disable_tracepoint ()
14336 && current_trace_status ()->running && is_tracepoint (bpt))
14337 {
14338 struct bp_location *location;
14339
14340 for (location = bpt->loc; location; location = location->next)
14341 target_disable_tracepoint (location);
14342 }
14343
44702360 14344 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14345
8d3788bd 14346 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14347}
14348
c906108c 14349static void
fba45db2 14350disable_command (char *args, int from_tty)
c906108c 14351{
c906108c 14352 if (args == 0)
46c6471b
PA
14353 {
14354 struct breakpoint *bpt;
14355
14356 ALL_BREAKPOINTS (bpt)
14357 if (user_breakpoint_p (bpt))
14358 disable_breakpoint (bpt);
14359 }
9eaabc75 14360 else
0d381245 14361 {
cb791d59 14362 std::string num = extract_arg (&args);
9eaabc75 14363
cb791d59 14364 while (!num.empty ())
d248b706 14365 {
cb791d59 14366 if (num.find ('.') != std::string::npos)
b775012e 14367 {
cb791d59 14368 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14369
14370 if (loc)
14371 {
14372 if (loc->enabled)
14373 {
14374 loc->enabled = 0;
14375 mark_breakpoint_location_modified (loc);
14376 }
14377 if (target_supports_enable_disable_tracepoint ()
14378 && current_trace_status ()->running && loc->owner
14379 && is_tracepoint (loc->owner))
14380 target_disable_tracepoint (loc);
14381 }
44702360 14382 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14383 }
9eaabc75 14384 else
48649e1b
TT
14385 map_breakpoint_numbers
14386 (num.c_str (), [&] (breakpoint *b)
14387 {
14388 iterate_over_related_breakpoints (b, disable_breakpoint);
14389 });
9eaabc75 14390 num = extract_arg (&args);
d248b706 14391 }
0d381245 14392 }
c906108c
SS
14393}
14394
14395static void
816338b5
SS
14396enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14397 int count)
c906108c 14398{
afe38095 14399 int target_resources_ok;
c906108c
SS
14400
14401 if (bpt->type == bp_hardware_breakpoint)
14402 {
14403 int i;
c5aa993b 14404 i = hw_breakpoint_used_count ();
53a5351d 14405 target_resources_ok =
d92524f1 14406 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14407 i + 1, 0);
c906108c 14408 if (target_resources_ok == 0)
8a3fe4f8 14409 error (_("No hardware breakpoint support in the target."));
c906108c 14410 else if (target_resources_ok < 0)
8a3fe4f8 14411 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14412 }
14413
cc60f2e3 14414 if (is_watchpoint (bpt))
c906108c 14415 {
d07205c2 14416 /* Initialize it just to avoid a GCC false warning. */
f486487f 14417 enum enable_state orig_enable_state = bp_disabled;
dde02812 14418
492d29ea 14419 TRY
c906108c 14420 {
3a5c3e22
PA
14421 struct watchpoint *w = (struct watchpoint *) bpt;
14422
1e718ff1
TJB
14423 orig_enable_state = bpt->enable_state;
14424 bpt->enable_state = bp_enabled;
3a5c3e22 14425 update_watchpoint (w, 1 /* reparse */);
c906108c 14426 }
492d29ea 14427 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14428 {
1e718ff1 14429 bpt->enable_state = orig_enable_state;
dde02812
ES
14430 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14431 bpt->number);
14432 return;
c5aa993b 14433 }
492d29ea 14434 END_CATCH
c906108c 14435 }
0101ce28 14436
b775012e
LM
14437 bpt->enable_state = bp_enabled;
14438
14439 /* Mark breakpoint locations modified. */
14440 mark_breakpoint_modified (bpt);
14441
d248b706
KY
14442 if (target_supports_enable_disable_tracepoint ()
14443 && current_trace_status ()->running && is_tracepoint (bpt))
14444 {
14445 struct bp_location *location;
14446
14447 for (location = bpt->loc; location; location = location->next)
14448 target_enable_tracepoint (location);
14449 }
14450
b4c291bb 14451 bpt->disposition = disposition;
816338b5 14452 bpt->enable_count = count;
44702360 14453 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14454
8d3788bd 14455 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14456}
14457
fe3f5fa8 14458
c906108c 14459void
fba45db2 14460enable_breakpoint (struct breakpoint *bpt)
c906108c 14461{
816338b5 14462 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14463}
14464
c906108c
SS
14465/* The enable command enables the specified breakpoints (or all defined
14466 breakpoints) so they once again become (or continue to be) effective
1272ad14 14467 in stopping the inferior. */
c906108c 14468
c906108c 14469static void
fba45db2 14470enable_command (char *args, int from_tty)
c906108c 14471{
c906108c 14472 if (args == 0)
46c6471b
PA
14473 {
14474 struct breakpoint *bpt;
14475
14476 ALL_BREAKPOINTS (bpt)
14477 if (user_breakpoint_p (bpt))
14478 enable_breakpoint (bpt);
14479 }
9eaabc75 14480 else
0d381245 14481 {
cb791d59 14482 std::string num = extract_arg (&args);
9eaabc75 14483
cb791d59 14484 while (!num.empty ())
d248b706 14485 {
cb791d59 14486 if (num.find ('.') != std::string::npos)
b775012e 14487 {
cb791d59 14488 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14489
14490 if (loc)
14491 {
14492 if (!loc->enabled)
14493 {
14494 loc->enabled = 1;
14495 mark_breakpoint_location_modified (loc);
14496 }
14497 if (target_supports_enable_disable_tracepoint ()
14498 && current_trace_status ()->running && loc->owner
14499 && is_tracepoint (loc->owner))
14500 target_enable_tracepoint (loc);
14501 }
44702360 14502 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14503 }
9eaabc75 14504 else
48649e1b
TT
14505 map_breakpoint_numbers
14506 (num.c_str (), [&] (breakpoint *b)
14507 {
14508 iterate_over_related_breakpoints (b, enable_breakpoint);
14509 });
9eaabc75 14510 num = extract_arg (&args);
d248b706 14511 }
0d381245 14512 }
c906108c
SS
14513}
14514
c906108c 14515static void
4495129a 14516enable_once_command (const char *args, int from_tty)
c906108c 14517{
48649e1b
TT
14518 map_breakpoint_numbers
14519 (args, [&] (breakpoint *b)
14520 {
14521 iterate_over_related_breakpoints
14522 (b, [&] (breakpoint *bpt)
14523 {
14524 enable_breakpoint_disp (bpt, disp_disable, 1);
14525 });
14526 });
816338b5
SS
14527}
14528
14529static void
4495129a 14530enable_count_command (const char *args, int from_tty)
816338b5 14531{
b9d61307
SM
14532 int count;
14533
14534 if (args == NULL)
14535 error_no_arg (_("hit count"));
14536
14537 count = get_number (&args);
816338b5 14538
48649e1b
TT
14539 map_breakpoint_numbers
14540 (args, [&] (breakpoint *b)
14541 {
14542 iterate_over_related_breakpoints
14543 (b, [&] (breakpoint *bpt)
14544 {
14545 enable_breakpoint_disp (bpt, disp_disable, count);
14546 });
14547 });
c906108c
SS
14548}
14549
c906108c 14550static void
4495129a 14551enable_delete_command (const char *args, int from_tty)
c906108c 14552{
48649e1b
TT
14553 map_breakpoint_numbers
14554 (args, [&] (breakpoint *b)
14555 {
14556 iterate_over_related_breakpoints
14557 (b, [&] (breakpoint *bpt)
14558 {
14559 enable_breakpoint_disp (bpt, disp_del, 1);
14560 });
14561 });
c906108c
SS
14562}
14563\f
fa8d40ab
JJ
14564static void
14565set_breakpoint_cmd (char *args, int from_tty)
14566{
14567}
14568
14569static void
14570show_breakpoint_cmd (char *args, int from_tty)
14571{
14572}
14573
1f3b5d1b
PP
14574/* Invalidate last known value of any hardware watchpoint if
14575 the memory which that value represents has been written to by
14576 GDB itself. */
14577
14578static void
8de0566d
YQ
14579invalidate_bp_value_on_memory_change (struct inferior *inferior,
14580 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14581 const bfd_byte *data)
14582{
14583 struct breakpoint *bp;
14584
14585 ALL_BREAKPOINTS (bp)
14586 if (bp->enable_state == bp_enabled
3a5c3e22 14587 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14588 {
3a5c3e22 14589 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14590
3a5c3e22
PA
14591 if (wp->val_valid && wp->val)
14592 {
14593 struct bp_location *loc;
14594
14595 for (loc = bp->loc; loc != NULL; loc = loc->next)
14596 if (loc->loc_type == bp_loc_hardware_watchpoint
14597 && loc->address + loc->length > addr
14598 && addr + len > loc->address)
14599 {
14600 value_free (wp->val);
14601 wp->val = NULL;
14602 wp->val_valid = 0;
14603 }
14604 }
1f3b5d1b
PP
14605 }
14606}
14607
8181d85f
DJ
14608/* Create and insert a breakpoint for software single step. */
14609
14610void
6c95b8df 14611insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14612 struct address_space *aspace,
14613 CORE_ADDR next_pc)
8181d85f 14614{
7c16b83e
PA
14615 struct thread_info *tp = inferior_thread ();
14616 struct symtab_and_line sal;
14617 CORE_ADDR pc = next_pc;
8181d85f 14618
34b7e8a6
PA
14619 if (tp->control.single_step_breakpoints == NULL)
14620 {
14621 tp->control.single_step_breakpoints
5d5658a1 14622 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14623 }
8181d85f 14624
7c16b83e
PA
14625 sal = find_pc_line (pc, 0);
14626 sal.pc = pc;
14627 sal.section = find_pc_overlay (pc);
14628 sal.explicit_pc = 1;
34b7e8a6 14629 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14630
7c16b83e 14631 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14632}
14633
93f9a11f
YQ
14634/* Insert single step breakpoints according to the current state. */
14635
14636int
14637insert_single_step_breakpoints (struct gdbarch *gdbarch)
14638{
f5ea389a 14639 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14640 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14641
f5ea389a 14642 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14643
a0ff9e1a 14644 if (!next_pcs.empty ())
93f9a11f 14645 {
f5ea389a 14646 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14647 struct address_space *aspace = get_frame_address_space (frame);
14648
a0ff9e1a 14649 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14650 insert_single_step_breakpoint (gdbarch, aspace, pc);
14651
93f9a11f
YQ
14652 return 1;
14653 }
14654 else
14655 return 0;
14656}
14657
34b7e8a6 14658/* See breakpoint.h. */
f02253f1
HZ
14659
14660int
7c16b83e
PA
14661breakpoint_has_location_inserted_here (struct breakpoint *bp,
14662 struct address_space *aspace,
14663 CORE_ADDR pc)
1aafd4da 14664{
7c16b83e 14665 struct bp_location *loc;
1aafd4da 14666
7c16b83e
PA
14667 for (loc = bp->loc; loc != NULL; loc = loc->next)
14668 if (loc->inserted
14669 && breakpoint_location_address_match (loc, aspace, pc))
14670 return 1;
1aafd4da 14671
7c16b83e 14672 return 0;
ef370185
JB
14673}
14674
14675/* Check whether a software single-step breakpoint is inserted at
14676 PC. */
14677
14678int
14679single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14680 CORE_ADDR pc)
14681{
34b7e8a6
PA
14682 struct breakpoint *bpt;
14683
14684 ALL_BREAKPOINTS (bpt)
14685 {
14686 if (bpt->type == bp_single_step
14687 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14688 return 1;
14689 }
14690 return 0;
1aafd4da
UW
14691}
14692
1042e4c0
SS
14693/* Tracepoint-specific operations. */
14694
14695/* Set tracepoint count to NUM. */
14696static void
14697set_tracepoint_count (int num)
14698{
14699 tracepoint_count = num;
4fa62494 14700 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14701}
14702
70221824 14703static void
f2fc3015 14704trace_command (char *arg_in, int from_tty)
1042e4c0 14705{
f2fc3015 14706 const char *arg = arg_in;
55aa24fb 14707 struct breakpoint_ops *ops;
55aa24fb 14708
ffc2605c
TT
14709 event_location_up location = string_to_event_location (&arg,
14710 current_language);
5b56227b 14711 if (location != NULL
ffc2605c 14712 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14713 ops = &tracepoint_probe_breakpoint_ops;
14714 else
14715 ops = &tracepoint_breakpoint_ops;
14716
558a9d82 14717 create_breakpoint (get_current_arch (),
ffc2605c 14718 location.get (),
f00aae0f 14719 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14720 0 /* tempflag */,
14721 bp_tracepoint /* type_wanted */,
14722 0 /* Ignore count */,
14723 pending_break_support,
14724 ops,
14725 from_tty,
14726 1 /* enabled */,
14727 0 /* internal */, 0);
1042e4c0
SS
14728}
14729
70221824 14730static void
f2fc3015 14731ftrace_command (char *arg_in, int from_tty)
7a697b8d 14732{
f2fc3015 14733 const char *arg = arg_in;
ffc2605c
TT
14734 event_location_up location = string_to_event_location (&arg,
14735 current_language);
558a9d82 14736 create_breakpoint (get_current_arch (),
ffc2605c 14737 location.get (),
f00aae0f 14738 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14739 0 /* tempflag */,
14740 bp_fast_tracepoint /* type_wanted */,
14741 0 /* Ignore count */,
14742 pending_break_support,
14743 &tracepoint_breakpoint_ops,
14744 from_tty,
14745 1 /* enabled */,
14746 0 /* internal */, 0);
0fb4aa4b
PA
14747}
14748
14749/* strace command implementation. Creates a static tracepoint. */
14750
70221824 14751static void
f2fc3015 14752strace_command (char *arg_in, int from_tty)
0fb4aa4b 14753{
f2fc3015 14754 const char *arg = arg_in;
983af33b 14755 struct breakpoint_ops *ops;
ffc2605c 14756 event_location_up location;
f00aae0f 14757 struct cleanup *back_to;
983af33b
SDJ
14758
14759 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14760 or with a normal static tracepoint. */
61012eef 14761 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14762 {
14763 ops = &strace_marker_breakpoint_ops;
14764 location = new_linespec_location (&arg);
14765 }
983af33b 14766 else
f00aae0f
KS
14767 {
14768 ops = &tracepoint_breakpoint_ops;
14769 location = string_to_event_location (&arg, current_language);
14770 }
983af33b 14771
558a9d82 14772 create_breakpoint (get_current_arch (),
ffc2605c 14773 location.get (),
f00aae0f 14774 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14775 0 /* tempflag */,
14776 bp_static_tracepoint /* type_wanted */,
14777 0 /* Ignore count */,
14778 pending_break_support,
14779 ops,
14780 from_tty,
14781 1 /* enabled */,
14782 0 /* internal */, 0);
7a697b8d
SS
14783}
14784
409873ef
SS
14785/* Set up a fake reader function that gets command lines from a linked
14786 list that was acquired during tracepoint uploading. */
14787
14788static struct uploaded_tp *this_utp;
3149d8c1 14789static int next_cmd;
409873ef
SS
14790
14791static char *
14792read_uploaded_action (void)
14793{
14794 char *rslt;
14795
3149d8c1 14796 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14797
3149d8c1 14798 next_cmd++;
409873ef
SS
14799
14800 return rslt;
14801}
14802
00bf0b85
SS
14803/* Given information about a tracepoint as recorded on a target (which
14804 can be either a live system or a trace file), attempt to create an
14805 equivalent GDB tracepoint. This is not a reliable process, since
14806 the target does not necessarily have all the information used when
14807 the tracepoint was originally defined. */
14808
d9b3f62e 14809struct tracepoint *
00bf0b85 14810create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14811{
f2fc3015
TT
14812 const char *addr_str;
14813 char small_buf[100];
d9b3f62e 14814 struct tracepoint *tp;
fd9b8c24 14815
409873ef
SS
14816 if (utp->at_string)
14817 addr_str = utp->at_string;
14818 else
14819 {
14820 /* In the absence of a source location, fall back to raw
14821 address. Since there is no way to confirm that the address
14822 means the same thing as when the trace was started, warn the
14823 user. */
3e43a32a
MS
14824 warning (_("Uploaded tracepoint %d has no "
14825 "source location, using raw address"),
409873ef 14826 utp->number);
8c042590 14827 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14828 addr_str = small_buf;
14829 }
14830
14831 /* There's not much we can do with a sequence of bytecodes. */
14832 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14833 warning (_("Uploaded tracepoint %d condition "
14834 "has no source form, ignoring it"),
409873ef 14835 utp->number);
d5551862 14836
ffc2605c
TT
14837 event_location_up location = string_to_event_location (&addr_str,
14838 current_language);
8cdf0e15 14839 if (!create_breakpoint (get_current_arch (),
ffc2605c 14840 location.get (),
f00aae0f 14841 utp->cond_string, -1, addr_str,
e7e0cddf 14842 0 /* parse cond/thread */,
8cdf0e15 14843 0 /* tempflag */,
0fb4aa4b 14844 utp->type /* type_wanted */,
8cdf0e15
VP
14845 0 /* Ignore count */,
14846 pending_break_support,
348d480f 14847 &tracepoint_breakpoint_ops,
8cdf0e15 14848 0 /* from_tty */,
84f4c1fe 14849 utp->enabled /* enabled */,
44f238bb
PA
14850 0 /* internal */,
14851 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14852 return NULL;
fd9b8c24 14853
409873ef 14854 /* Get the tracepoint we just created. */
fd9b8c24
PA
14855 tp = get_tracepoint (tracepoint_count);
14856 gdb_assert (tp != NULL);
d5551862 14857
00bf0b85
SS
14858 if (utp->pass > 0)
14859 {
8c042590 14860 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14861 tp->number);
00bf0b85 14862
409873ef 14863 trace_pass_command (small_buf, 0);
00bf0b85
SS
14864 }
14865
409873ef
SS
14866 /* If we have uploaded versions of the original commands, set up a
14867 special-purpose "reader" function and call the usual command line
14868 reader, then pass the result to the breakpoint command-setting
14869 function. */
3149d8c1 14870 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14871 {
93921405 14872 command_line_up cmd_list;
00bf0b85 14873
409873ef 14874 this_utp = utp;
3149d8c1 14875 next_cmd = 0;
d5551862 14876
409873ef
SS
14877 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14878
c1fc2657 14879 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14880 }
3149d8c1
SS
14881 else if (!VEC_empty (char_ptr, utp->actions)
14882 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14883 warning (_("Uploaded tracepoint %d actions "
14884 "have no source form, ignoring them"),
409873ef 14885 utp->number);
00bf0b85 14886
f196051f 14887 /* Copy any status information that might be available. */
c1fc2657 14888 tp->hit_count = utp->hit_count;
f196051f
SS
14889 tp->traceframe_usage = utp->traceframe_usage;
14890
00bf0b85 14891 return tp;
d9b3f62e 14892}
00bf0b85 14893
1042e4c0
SS
14894/* Print information on tracepoint number TPNUM_EXP, or all if
14895 omitted. */
14896
14897static void
11db9430 14898info_tracepoints_command (char *args, int from_tty)
1042e4c0 14899{
79a45e25 14900 struct ui_out *uiout = current_uiout;
e5a67952 14901 int num_printed;
1042e4c0 14902
e5a67952 14903 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14904
14905 if (num_printed == 0)
1042e4c0 14906 {
e5a67952 14907 if (args == NULL || *args == '\0')
112e8700 14908 uiout->message ("No tracepoints.\n");
d77f58be 14909 else
112e8700 14910 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14911 }
ad443146
SS
14912
14913 default_collect_info ();
1042e4c0
SS
14914}
14915
4a64f543 14916/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14917 Not supported by all targets. */
14918static void
14919enable_trace_command (char *args, int from_tty)
14920{
14921 enable_command (args, from_tty);
14922}
14923
4a64f543 14924/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14925 Not supported by all targets. */
14926static void
14927disable_trace_command (char *args, int from_tty)
14928{
14929 disable_command (args, from_tty);
14930}
14931
4a64f543 14932/* Remove a tracepoint (or all if no argument). */
1042e4c0 14933static void
4495129a 14934delete_trace_command (const char *arg, int from_tty)
1042e4c0 14935{
35df4500 14936 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14937
14938 dont_repeat ();
14939
14940 if (arg == 0)
14941 {
14942 int breaks_to_delete = 0;
14943
14944 /* Delete all breakpoints if no argument.
14945 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14946 have to be deleted with an explicit breakpoint number
14947 argument. */
1042e4c0 14948 ALL_TRACEPOINTS (b)
46c6471b 14949 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14950 {
14951 breaks_to_delete = 1;
14952 break;
14953 }
1042e4c0
SS
14954
14955 /* Ask user only if there are some breakpoints to delete. */
14956 if (!from_tty
14957 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14958 {
35df4500 14959 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14960 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14961 delete_breakpoint (b);
1042e4c0
SS
14962 }
14963 }
14964 else
48649e1b
TT
14965 map_breakpoint_numbers
14966 (arg, [&] (breakpoint *b)
14967 {
14968 iterate_over_related_breakpoints (b, delete_breakpoint);
14969 });
1042e4c0
SS
14970}
14971
197f0a60
TT
14972/* Helper function for trace_pass_command. */
14973
14974static void
d9b3f62e 14975trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14976{
d9b3f62e 14977 tp->pass_count = count;
c1fc2657 14978 observer_notify_breakpoint_modified (tp);
197f0a60
TT
14979 if (from_tty)
14980 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14981 tp->number, count);
197f0a60
TT
14982}
14983
1042e4c0
SS
14984/* Set passcount for tracepoint.
14985
14986 First command argument is passcount, second is tracepoint number.
14987 If tracepoint number omitted, apply to most recently defined.
14988 Also accepts special argument "all". */
14989
14990static void
14991trace_pass_command (char *args, int from_tty)
14992{
d9b3f62e 14993 struct tracepoint *t1;
1042e4c0 14994 unsigned int count;
1042e4c0
SS
14995
14996 if (args == 0 || *args == 0)
3e43a32a
MS
14997 error (_("passcount command requires an "
14998 "argument (count + optional TP num)"));
1042e4c0 14999
4a64f543 15000 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15001
529480d0 15002 args = skip_spaces (args);
1042e4c0
SS
15003 if (*args && strncasecmp (args, "all", 3) == 0)
15004 {
d9b3f62e
PA
15005 struct breakpoint *b;
15006
1042e4c0 15007 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15008 if (*args)
15009 error (_("Junk at end of arguments."));
1042e4c0 15010
d9b3f62e 15011 ALL_TRACEPOINTS (b)
197f0a60 15012 {
d9b3f62e 15013 t1 = (struct tracepoint *) b;
197f0a60
TT
15014 trace_pass_set_count (t1, count, from_tty);
15015 }
15016 }
15017 else if (*args == '\0')
1042e4c0 15018 {
5fa1d40e 15019 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15020 if (t1)
197f0a60
TT
15021 trace_pass_set_count (t1, count, from_tty);
15022 }
15023 else
15024 {
bfd28288
PA
15025 number_or_range_parser parser (args);
15026 while (!parser.finished ())
1042e4c0 15027 {
bfd28288 15028 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15029 if (t1)
15030 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15031 }
15032 }
1042e4c0
SS
15033}
15034
d9b3f62e 15035struct tracepoint *
1042e4c0
SS
15036get_tracepoint (int num)
15037{
15038 struct breakpoint *t;
15039
15040 ALL_TRACEPOINTS (t)
15041 if (t->number == num)
d9b3f62e 15042 return (struct tracepoint *) t;
1042e4c0
SS
15043
15044 return NULL;
15045}
15046
d5551862
SS
15047/* Find the tracepoint with the given target-side number (which may be
15048 different from the tracepoint number after disconnecting and
15049 reconnecting). */
15050
d9b3f62e 15051struct tracepoint *
d5551862
SS
15052get_tracepoint_by_number_on_target (int num)
15053{
d9b3f62e 15054 struct breakpoint *b;
d5551862 15055
d9b3f62e
PA
15056 ALL_TRACEPOINTS (b)
15057 {
15058 struct tracepoint *t = (struct tracepoint *) b;
15059
15060 if (t->number_on_target == num)
15061 return t;
15062 }
d5551862
SS
15063
15064 return NULL;
15065}
15066
1042e4c0 15067/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15068 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15069 If the argument is missing, the most recent tracepoint
15070 (tracepoint_count) is returned. */
15071
d9b3f62e 15072struct tracepoint *
197f0a60 15073get_tracepoint_by_number (char **arg,
bfd28288 15074 number_or_range_parser *parser)
1042e4c0 15075{
1042e4c0
SS
15076 struct breakpoint *t;
15077 int tpnum;
15078 char *instring = arg == NULL ? NULL : *arg;
15079
bfd28288 15080 if (parser != NULL)
197f0a60 15081 {
bfd28288
PA
15082 gdb_assert (!parser->finished ());
15083 tpnum = parser->get_number ();
197f0a60
TT
15084 }
15085 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15086 tpnum = tracepoint_count;
1042e4c0 15087 else
197f0a60 15088 tpnum = get_number (arg);
1042e4c0
SS
15089
15090 if (tpnum <= 0)
15091 {
15092 if (instring && *instring)
15093 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15094 instring);
15095 else
5fa1d40e 15096 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15097 return NULL;
15098 }
15099
15100 ALL_TRACEPOINTS (t)
15101 if (t->number == tpnum)
15102 {
d9b3f62e 15103 return (struct tracepoint *) t;
1042e4c0
SS
15104 }
15105
1042e4c0
SS
15106 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15107 return NULL;
15108}
15109
d9b3f62e
PA
15110void
15111print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15112{
15113 if (b->thread != -1)
15114 fprintf_unfiltered (fp, " thread %d", b->thread);
15115
15116 if (b->task != 0)
15117 fprintf_unfiltered (fp, " task %d", b->task);
15118
15119 fprintf_unfiltered (fp, "\n");
15120}
15121
6149aea9
PA
15122/* Save information on user settable breakpoints (watchpoints, etc) to
15123 a new script file named FILENAME. If FILTER is non-NULL, call it
15124 on each breakpoint and only include the ones for which it returns
15125 non-zero. */
15126
1042e4c0 15127static void
4495129a 15128save_breakpoints (const char *filename, int from_tty,
6149aea9 15129 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15130{
15131 struct breakpoint *tp;
6149aea9 15132 int any = 0;
6149aea9 15133 int extra_trace_bits = 0;
1042e4c0 15134
6149aea9
PA
15135 if (filename == 0 || *filename == 0)
15136 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15137
15138 /* See if we have anything to save. */
6149aea9 15139 ALL_BREAKPOINTS (tp)
1042e4c0 15140 {
6149aea9 15141 /* Skip internal and momentary breakpoints. */
09d682a4 15142 if (!user_breakpoint_p (tp))
6149aea9
PA
15143 continue;
15144
15145 /* If we have a filter, only save the breakpoints it accepts. */
15146 if (filter && !filter (tp))
15147 continue;
15148
15149 any = 1;
15150
15151 if (is_tracepoint (tp))
15152 {
15153 extra_trace_bits = 1;
15154
15155 /* We can stop searching. */
15156 break;
15157 }
1042e4c0 15158 }
6149aea9
PA
15159
15160 if (!any)
1042e4c0 15161 {
6149aea9 15162 warning (_("Nothing to save."));
1042e4c0
SS
15163 return;
15164 }
15165
ee0c3293 15166 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15167
15168 stdio_file fp;
15169
ee0c3293 15170 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15171 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15172 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15173
6149aea9 15174 if (extra_trace_bits)
d7e74731 15175 save_trace_state_variables (&fp);
8bf6485c 15176
6149aea9 15177 ALL_BREAKPOINTS (tp)
1042e4c0 15178 {
6149aea9 15179 /* Skip internal and momentary breakpoints. */
09d682a4 15180 if (!user_breakpoint_p (tp))
6149aea9 15181 continue;
8bf6485c 15182
6149aea9
PA
15183 /* If we have a filter, only save the breakpoints it accepts. */
15184 if (filter && !filter (tp))
15185 continue;
15186
d7e74731 15187 tp->ops->print_recreate (tp, &fp);
1042e4c0 15188
6149aea9
PA
15189 /* Note, we can't rely on tp->number for anything, as we can't
15190 assume the recreated breakpoint numbers will match. Use $bpnum
15191 instead. */
15192
15193 if (tp->cond_string)
d7e74731 15194 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15195
15196 if (tp->ignore_count)
d7e74731 15197 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15198
2d9442cc 15199 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15200 {
d7e74731 15201 fp.puts (" commands\n");
a7bdde9e 15202
d7e74731 15203 current_uiout->redirect (&fp);
492d29ea 15204 TRY
1042e4c0 15205 {
d1b0a7bf 15206 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15207 }
492d29ea
PA
15208 CATCH (ex, RETURN_MASK_ALL)
15209 {
112e8700 15210 current_uiout->redirect (NULL);
492d29ea
PA
15211 throw_exception (ex);
15212 }
15213 END_CATCH
1042e4c0 15214
112e8700 15215 current_uiout->redirect (NULL);
d7e74731 15216 fp.puts (" end\n");
1042e4c0 15217 }
6149aea9
PA
15218
15219 if (tp->enable_state == bp_disabled)
d7e74731 15220 fp.puts ("disable $bpnum\n");
6149aea9
PA
15221
15222 /* If this is a multi-location breakpoint, check if the locations
15223 should be individually disabled. Watchpoint locations are
15224 special, and not user visible. */
15225 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15226 {
15227 struct bp_location *loc;
15228 int n = 1;
15229
15230 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15231 if (!loc->enabled)
d7e74731 15232 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15233 }
1042e4c0 15234 }
8bf6485c 15235
6149aea9 15236 if (extra_trace_bits && *default_collect)
d7e74731 15237 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15238
1042e4c0 15239 if (from_tty)
ee0c3293 15240 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15241}
15242
15243/* The `save breakpoints' command. */
15244
15245static void
4495129a 15246save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15247{
15248 save_breakpoints (args, from_tty, NULL);
15249}
15250
15251/* The `save tracepoints' command. */
15252
15253static void
4495129a 15254save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15255{
15256 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15257}
15258
15259/* Create a vector of all tracepoints. */
15260
15261VEC(breakpoint_p) *
eeae04df 15262all_tracepoints (void)
1042e4c0
SS
15263{
15264 VEC(breakpoint_p) *tp_vec = 0;
15265 struct breakpoint *tp;
15266
15267 ALL_TRACEPOINTS (tp)
15268 {
15269 VEC_safe_push (breakpoint_p, tp_vec, tp);
15270 }
15271
15272 return tp_vec;
15273}
15274
c906108c 15275\f
629500fa
KS
15276/* This help string is used to consolidate all the help string for specifying
15277 locations used by several commands. */
15278
15279#define LOCATION_HELP_STRING \
15280"Linespecs are colon-separated lists of location parameters, such as\n\
15281source filename, function name, label name, and line number.\n\
15282Example: To specify the start of a label named \"the_top\" in the\n\
15283function \"fact\" in the file \"factorial.c\", use\n\
15284\"factorial.c:fact:the_top\".\n\
15285\n\
15286Address locations begin with \"*\" and specify an exact address in the\n\
15287program. Example: To specify the fourth byte past the start function\n\
15288\"main\", use \"*main + 4\".\n\
15289\n\
15290Explicit locations are similar to linespecs but use an option/argument\n\
15291syntax to specify location parameters.\n\
15292Example: To specify the start of the label named \"the_top\" in the\n\
15293function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15294-function fact -label the_top\".\n"
15295
4a64f543
MS
15296/* This help string is used for the break, hbreak, tbreak and thbreak
15297 commands. It is defined as a macro to prevent duplication.
15298 COMMAND should be a string constant containing the name of the
15299 command. */
629500fa 15300
31e2b00f 15301#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15302command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15303PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15304probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15305guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15306`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15307LOCATION may be a linespec, address, or explicit location as described\n\
15308below.\n\
15309\n\
dc10affe
PA
15310With no LOCATION, uses current execution address of the selected\n\
15311stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15312\n\
15313THREADNUM is the number from \"info threads\".\n\
15314CONDITION is a boolean expression.\n\
629500fa 15315\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15316Multiple breakpoints at one place are permitted, and useful if their\n\
15317conditions are different.\n\
31e2b00f
AS
15318\n\
15319Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15320
44feb3ce
TT
15321/* List of subcommands for "catch". */
15322static struct cmd_list_element *catch_cmdlist;
15323
15324/* List of subcommands for "tcatch". */
15325static struct cmd_list_element *tcatch_cmdlist;
15326
9ac4176b 15327void
a121b7c1 15328add_catch_command (const char *name, const char *docstring,
82ae6c8d 15329 cmd_sfunc_ftype *sfunc,
625e8578 15330 completer_ftype *completer,
44feb3ce
TT
15331 void *user_data_catch,
15332 void *user_data_tcatch)
15333{
15334 struct cmd_list_element *command;
15335
0450cc4c 15336 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15337 &catch_cmdlist);
15338 set_cmd_sfunc (command, sfunc);
15339 set_cmd_context (command, user_data_catch);
a96d9b2e 15340 set_cmd_completer (command, completer);
44feb3ce 15341
0450cc4c 15342 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15343 &tcatch_cmdlist);
15344 set_cmd_sfunc (command, sfunc);
15345 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15346 set_cmd_completer (command, completer);
44feb3ce
TT
15347}
15348
6149aea9
PA
15349static void
15350save_command (char *arg, int from_tty)
15351{
3e43a32a
MS
15352 printf_unfiltered (_("\"save\" must be followed by "
15353 "the name of a save subcommand.\n"));
635c7e8a 15354 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15355}
15356
84f4c1fe
PM
15357struct breakpoint *
15358iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15359 void *data)
15360{
35df4500 15361 struct breakpoint *b, *b_tmp;
84f4c1fe 15362
35df4500 15363 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15364 {
15365 if ((*callback) (b, data))
15366 return b;
15367 }
15368
15369 return NULL;
15370}
15371
0574c78f
GB
15372/* Zero if any of the breakpoint's locations could be a location where
15373 functions have been inlined, nonzero otherwise. */
15374
15375static int
15376is_non_inline_function (struct breakpoint *b)
15377{
15378 /* The shared library event breakpoint is set on the address of a
15379 non-inline function. */
15380 if (b->type == bp_shlib_event)
15381 return 1;
15382
15383 return 0;
15384}
15385
15386/* Nonzero if the specified PC cannot be a location where functions
15387 have been inlined. */
15388
15389int
09ac7c10
TT
15390pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15391 const struct target_waitstatus *ws)
0574c78f
GB
15392{
15393 struct breakpoint *b;
15394 struct bp_location *bl;
15395
15396 ALL_BREAKPOINTS (b)
15397 {
15398 if (!is_non_inline_function (b))
15399 continue;
15400
15401 for (bl = b->loc; bl != NULL; bl = bl->next)
15402 {
15403 if (!bl->shlib_disabled
09ac7c10 15404 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15405 return 1;
15406 }
15407 }
15408
15409 return 0;
15410}
15411
2f202fde
JK
15412/* Remove any references to OBJFILE which is going to be freed. */
15413
15414void
15415breakpoint_free_objfile (struct objfile *objfile)
15416{
15417 struct bp_location **locp, *loc;
15418
15419 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15420 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15421 loc->symtab = NULL;
15422}
15423
2060206e
PA
15424void
15425initialize_breakpoint_ops (void)
15426{
15427 static int initialized = 0;
15428
15429 struct breakpoint_ops *ops;
15430
15431 if (initialized)
15432 return;
15433 initialized = 1;
15434
15435 /* The breakpoint_ops structure to be inherit by all kinds of
15436 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15437 internal and momentary breakpoints, etc.). */
15438 ops = &bkpt_base_breakpoint_ops;
15439 *ops = base_breakpoint_ops;
15440 ops->re_set = bkpt_re_set;
15441 ops->insert_location = bkpt_insert_location;
15442 ops->remove_location = bkpt_remove_location;
15443 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15444 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15445 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15446 ops->decode_location = bkpt_decode_location;
2060206e
PA
15447
15448 /* The breakpoint_ops structure to be used in regular breakpoints. */
15449 ops = &bkpt_breakpoint_ops;
15450 *ops = bkpt_base_breakpoint_ops;
15451 ops->re_set = bkpt_re_set;
15452 ops->resources_needed = bkpt_resources_needed;
15453 ops->print_it = bkpt_print_it;
15454 ops->print_mention = bkpt_print_mention;
15455 ops->print_recreate = bkpt_print_recreate;
15456
15457 /* Ranged breakpoints. */
15458 ops = &ranged_breakpoint_ops;
15459 *ops = bkpt_breakpoint_ops;
15460 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15461 ops->resources_needed = resources_needed_ranged_breakpoint;
15462 ops->print_it = print_it_ranged_breakpoint;
15463 ops->print_one = print_one_ranged_breakpoint;
15464 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15465 ops->print_mention = print_mention_ranged_breakpoint;
15466 ops->print_recreate = print_recreate_ranged_breakpoint;
15467
15468 /* Internal breakpoints. */
15469 ops = &internal_breakpoint_ops;
15470 *ops = bkpt_base_breakpoint_ops;
15471 ops->re_set = internal_bkpt_re_set;
15472 ops->check_status = internal_bkpt_check_status;
15473 ops->print_it = internal_bkpt_print_it;
15474 ops->print_mention = internal_bkpt_print_mention;
15475
15476 /* Momentary breakpoints. */
15477 ops = &momentary_breakpoint_ops;
15478 *ops = bkpt_base_breakpoint_ops;
15479 ops->re_set = momentary_bkpt_re_set;
15480 ops->check_status = momentary_bkpt_check_status;
15481 ops->print_it = momentary_bkpt_print_it;
15482 ops->print_mention = momentary_bkpt_print_mention;
15483
55aa24fb
SDJ
15484 /* Probe breakpoints. */
15485 ops = &bkpt_probe_breakpoint_ops;
15486 *ops = bkpt_breakpoint_ops;
15487 ops->insert_location = bkpt_probe_insert_location;
15488 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15489 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15490 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15491
2060206e
PA
15492 /* Watchpoints. */
15493 ops = &watchpoint_breakpoint_ops;
15494 *ops = base_breakpoint_ops;
15495 ops->re_set = re_set_watchpoint;
15496 ops->insert_location = insert_watchpoint;
15497 ops->remove_location = remove_watchpoint;
15498 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15499 ops->check_status = check_status_watchpoint;
15500 ops->resources_needed = resources_needed_watchpoint;
15501 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15502 ops->print_it = print_it_watchpoint;
15503 ops->print_mention = print_mention_watchpoint;
15504 ops->print_recreate = print_recreate_watchpoint;
427cd150 15505 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15506
15507 /* Masked watchpoints. */
15508 ops = &masked_watchpoint_breakpoint_ops;
15509 *ops = watchpoint_breakpoint_ops;
15510 ops->insert_location = insert_masked_watchpoint;
15511 ops->remove_location = remove_masked_watchpoint;
15512 ops->resources_needed = resources_needed_masked_watchpoint;
15513 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15514 ops->print_it = print_it_masked_watchpoint;
15515 ops->print_one_detail = print_one_detail_masked_watchpoint;
15516 ops->print_mention = print_mention_masked_watchpoint;
15517 ops->print_recreate = print_recreate_masked_watchpoint;
15518
15519 /* Tracepoints. */
15520 ops = &tracepoint_breakpoint_ops;
15521 *ops = base_breakpoint_ops;
15522 ops->re_set = tracepoint_re_set;
15523 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15524 ops->print_one_detail = tracepoint_print_one_detail;
15525 ops->print_mention = tracepoint_print_mention;
15526 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15527 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15528 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15529 ops->decode_location = tracepoint_decode_location;
983af33b 15530
55aa24fb
SDJ
15531 /* Probe tracepoints. */
15532 ops = &tracepoint_probe_breakpoint_ops;
15533 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15534 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15535 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15536
983af33b
SDJ
15537 /* Static tracepoints with marker (`-m'). */
15538 ops = &strace_marker_breakpoint_ops;
15539 *ops = tracepoint_breakpoint_ops;
5f700d83 15540 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15541 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15542 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15543
15544 /* Fork catchpoints. */
15545 ops = &catch_fork_breakpoint_ops;
15546 *ops = base_breakpoint_ops;
15547 ops->insert_location = insert_catch_fork;
15548 ops->remove_location = remove_catch_fork;
15549 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15550 ops->print_it = print_it_catch_fork;
15551 ops->print_one = print_one_catch_fork;
15552 ops->print_mention = print_mention_catch_fork;
15553 ops->print_recreate = print_recreate_catch_fork;
15554
15555 /* Vfork catchpoints. */
15556 ops = &catch_vfork_breakpoint_ops;
15557 *ops = base_breakpoint_ops;
15558 ops->insert_location = insert_catch_vfork;
15559 ops->remove_location = remove_catch_vfork;
15560 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15561 ops->print_it = print_it_catch_vfork;
15562 ops->print_one = print_one_catch_vfork;
15563 ops->print_mention = print_mention_catch_vfork;
15564 ops->print_recreate = print_recreate_catch_vfork;
15565
15566 /* Exec catchpoints. */
15567 ops = &catch_exec_breakpoint_ops;
15568 *ops = base_breakpoint_ops;
2060206e
PA
15569 ops->insert_location = insert_catch_exec;
15570 ops->remove_location = remove_catch_exec;
15571 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15572 ops->print_it = print_it_catch_exec;
15573 ops->print_one = print_one_catch_exec;
15574 ops->print_mention = print_mention_catch_exec;
15575 ops->print_recreate = print_recreate_catch_exec;
15576
edcc5120
TT
15577 /* Solib-related catchpoints. */
15578 ops = &catch_solib_breakpoint_ops;
15579 *ops = base_breakpoint_ops;
edcc5120
TT
15580 ops->insert_location = insert_catch_solib;
15581 ops->remove_location = remove_catch_solib;
15582 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15583 ops->check_status = check_status_catch_solib;
15584 ops->print_it = print_it_catch_solib;
15585 ops->print_one = print_one_catch_solib;
15586 ops->print_mention = print_mention_catch_solib;
15587 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15588
15589 ops = &dprintf_breakpoint_ops;
15590 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15591 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15592 ops->resources_needed = bkpt_resources_needed;
15593 ops->print_it = bkpt_print_it;
15594 ops->print_mention = bkpt_print_mention;
2d9442cc 15595 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15596 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15597 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15598}
15599
8bfd80db
YQ
15600/* Chain containing all defined "enable breakpoint" subcommands. */
15601
15602static struct cmd_list_element *enablebreaklist = NULL;
15603
c906108c 15604void
fba45db2 15605_initialize_breakpoint (void)
c906108c
SS
15606{
15607 struct cmd_list_element *c;
15608
2060206e
PA
15609 initialize_breakpoint_ops ();
15610
84acb35a 15611 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15612 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15613 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15614
55aa24fb
SDJ
15615 breakpoint_objfile_key
15616 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15617
c906108c
SS
15618 breakpoint_chain = 0;
15619 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15620 before a breakpoint is set. */
15621 breakpoint_count = 0;
15622
1042e4c0
SS
15623 tracepoint_count = 0;
15624
1bedd215
AC
15625 add_com ("ignore", class_breakpoint, ignore_command, _("\
15626Set ignore-count of breakpoint number N to COUNT.\n\
15627Usage is `ignore N COUNT'."));
c906108c 15628
1bedd215 15629 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15630Set commands to be executed when the given breakpoints are hit.\n\
15631Give a space-separated breakpoint list as argument after \"commands\".\n\
15632A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15633(e.g. `5-7').\n\
c906108c
SS
15634With no argument, the targeted breakpoint is the last one set.\n\
15635The commands themselves follow starting on the next line.\n\
15636Type a line containing \"end\" to indicate the end of them.\n\
15637Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15638then no output is printed when it is hit, except what the commands print."));
c906108c 15639
d55637df 15640 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15641Specify breakpoint number N to break only if COND is true.\n\
c906108c 15642Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15643expression to be evaluated whenever breakpoint N is reached."));
d55637df 15644 set_cmd_completer (c, condition_completer);
c906108c 15645
1bedd215 15646 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15647Set a temporary breakpoint.\n\
c906108c
SS
15648Like \"break\" except the breakpoint is only temporary,\n\
15649so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15650by using \"enable delete\" on the breakpoint number.\n\
15651\n"
15652BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15653 set_cmd_completer (c, location_completer);
c94fdfd0 15654
1bedd215 15655 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15656Set a hardware assisted breakpoint.\n\
c906108c 15657Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15658some target hardware may not have this support.\n\
15659\n"
15660BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15661 set_cmd_completer (c, location_completer);
c906108c 15662
1bedd215 15663 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15664Set a temporary hardware assisted breakpoint.\n\
c906108c 15665Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15666so it will be deleted when hit.\n\
15667\n"
15668BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15669 set_cmd_completer (c, location_completer);
c906108c 15670
1bedd215
AC
15671 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15672Enable some breakpoints.\n\
c906108c
SS
15673Give breakpoint numbers (separated by spaces) as arguments.\n\
15674With no subcommand, breakpoints are enabled until you command otherwise.\n\
15675This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15676With a subcommand you can enable temporarily."),
c906108c 15677 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15678
15679 add_com_alias ("en", "enable", class_breakpoint, 1);
15680
84951ab5 15681 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15682Enable some breakpoints.\n\
c906108c
SS
15683Give breakpoint numbers (separated by spaces) as arguments.\n\
15684This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15685May be abbreviated to simply \"enable\".\n"),
c5aa993b 15686 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15687
1a966eab
AC
15688 add_cmd ("once", no_class, enable_once_command, _("\
15689Enable breakpoints for one hit. Give breakpoint numbers.\n\
15690If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15691 &enablebreaklist);
15692
1a966eab
AC
15693 add_cmd ("delete", no_class, enable_delete_command, _("\
15694Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15695If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15696 &enablebreaklist);
15697
816338b5
SS
15698 add_cmd ("count", no_class, enable_count_command, _("\
15699Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15700If a breakpoint is hit while enabled in this fashion,\n\
15701the count is decremented; when it reaches zero, the breakpoint is disabled."),
15702 &enablebreaklist);
15703
1a966eab
AC
15704 add_cmd ("delete", no_class, enable_delete_command, _("\
15705Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15706If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15707 &enablelist);
15708
1a966eab
AC
15709 add_cmd ("once", no_class, enable_once_command, _("\
15710Enable breakpoints for one hit. Give breakpoint numbers.\n\
15711If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15712 &enablelist);
15713
15714 add_cmd ("count", no_class, enable_count_command, _("\
15715Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15716If a breakpoint is hit while enabled in this fashion,\n\
15717the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15718 &enablelist);
15719
1bedd215
AC
15720 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15721Disable some breakpoints.\n\
c906108c
SS
15722Arguments are breakpoint numbers with spaces in between.\n\
15723To disable all breakpoints, give no argument.\n\
64b9b334 15724A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15725 &disablelist, "disable ", 1, &cmdlist);
15726 add_com_alias ("dis", "disable", class_breakpoint, 1);
15727 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15728
1a966eab
AC
15729 add_cmd ("breakpoints", class_alias, disable_command, _("\
15730Disable some breakpoints.\n\
c906108c
SS
15731Arguments are breakpoint numbers with spaces in between.\n\
15732To disable all breakpoints, give no argument.\n\
64b9b334 15733A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15734This command may be abbreviated \"disable\"."),
c906108c
SS
15735 &disablelist);
15736
1bedd215
AC
15737 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15738Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15739Arguments are breakpoint numbers with spaces in between.\n\
15740To delete all breakpoints, give no argument.\n\
15741\n\
15742Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15743The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15744 &deletelist, "delete ", 1, &cmdlist);
15745 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15746 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15747
1a966eab
AC
15748 add_cmd ("breakpoints", class_alias, delete_command, _("\
15749Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15750Arguments are breakpoint numbers with spaces in between.\n\
15751To delete all breakpoints, give no argument.\n\
1a966eab 15752This command may be abbreviated \"delete\"."),
c906108c
SS
15753 &deletelist);
15754
1bedd215 15755 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15756Clear breakpoint at specified location.\n\
15757Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15758\n\
15759With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15760is executing in.\n"
15761"\n" LOCATION_HELP_STRING "\n\
1bedd215 15762See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15763 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15764
1bedd215 15765 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15766Set breakpoint at specified location.\n"
31e2b00f 15767BREAK_ARGS_HELP ("break")));
5ba2abeb 15768 set_cmd_completer (c, location_completer);
c94fdfd0 15769
c906108c
SS
15770 add_com_alias ("b", "break", class_run, 1);
15771 add_com_alias ("br", "break", class_run, 1);
15772 add_com_alias ("bre", "break", class_run, 1);
15773 add_com_alias ("brea", "break", class_run, 1);
15774
c906108c
SS
15775 if (dbx_commands)
15776 {
1bedd215
AC
15777 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15778Break in function/address or break at a line in the current file."),
c5aa993b
JM
15779 &stoplist, "stop ", 1, &cmdlist);
15780 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15781 _("Break in function or address."), &stoplist);
c5aa993b 15782 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15783 _("Break at a line in the current file."), &stoplist);
11db9430 15784 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15785Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15786The \"Type\" column indicates one of:\n\
15787\tbreakpoint - normal breakpoint\n\
15788\twatchpoint - watchpoint\n\
15789The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15790the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15791breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15792address and file/line number respectively.\n\
15793\n\
15794Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15795are set to the address of the last breakpoint listed unless the command\n\
15796is prefixed with \"server \".\n\n\
c906108c 15797Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15798breakpoint set."));
c906108c
SS
15799 }
15800
11db9430 15801 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15802Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15803The \"Type\" column indicates one of:\n\
15804\tbreakpoint - normal breakpoint\n\
15805\twatchpoint - watchpoint\n\
15806The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15807the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15808breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15809address and file/line number respectively.\n\
15810\n\
15811Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15812are set to the address of the last breakpoint listed unless the command\n\
15813is prefixed with \"server \".\n\n\
c906108c 15814Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15815breakpoint set."));
c906108c 15816
6b04bdb7
MS
15817 add_info_alias ("b", "breakpoints", 1);
15818
1a966eab
AC
15819 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15820Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15821The \"Type\" column indicates one of:\n\
15822\tbreakpoint - normal breakpoint\n\
15823\twatchpoint - watchpoint\n\
15824\tlongjmp - internal breakpoint used to step through longjmp()\n\
15825\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15826\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15827\tfinish - internal breakpoint used by the \"finish\" command\n\
15828The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15829the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15830breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15831address and file/line number respectively.\n\
15832\n\
15833Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15834are set to the address of the last breakpoint listed unless the command\n\
15835is prefixed with \"server \".\n\n\
c906108c 15836Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15837breakpoint set."),
c906108c
SS
15838 &maintenanceinfolist);
15839
44feb3ce
TT
15840 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15841Set catchpoints to catch events."),
15842 &catch_cmdlist, "catch ",
15843 0/*allow-unknown*/, &cmdlist);
15844
15845 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15846Set temporary catchpoints to catch events."),
15847 &tcatch_cmdlist, "tcatch ",
15848 0/*allow-unknown*/, &cmdlist);
15849
44feb3ce
TT
15850 add_catch_command ("fork", _("Catch calls to fork."),
15851 catch_fork_command_1,
a96d9b2e 15852 NULL,
44feb3ce
TT
15853 (void *) (uintptr_t) catch_fork_permanent,
15854 (void *) (uintptr_t) catch_fork_temporary);
15855 add_catch_command ("vfork", _("Catch calls to vfork."),
15856 catch_fork_command_1,
a96d9b2e 15857 NULL,
44feb3ce
TT
15858 (void *) (uintptr_t) catch_vfork_permanent,
15859 (void *) (uintptr_t) catch_vfork_temporary);
15860 add_catch_command ("exec", _("Catch calls to exec."),
15861 catch_exec_command_1,
a96d9b2e
SDJ
15862 NULL,
15863 CATCH_PERMANENT,
15864 CATCH_TEMPORARY);
edcc5120
TT
15865 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15866Usage: catch load [REGEX]\n\
15867If REGEX is given, only stop for libraries matching the regular expression."),
15868 catch_load_command_1,
15869 NULL,
15870 CATCH_PERMANENT,
15871 CATCH_TEMPORARY);
15872 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15873Usage: catch unload [REGEX]\n\
15874If REGEX is given, only stop for libraries matching the regular expression."),
15875 catch_unload_command_1,
15876 NULL,
15877 CATCH_PERMANENT,
15878 CATCH_TEMPORARY);
c5aa993b 15879
1bedd215
AC
15880 c = add_com ("watch", class_breakpoint, watch_command, _("\
15881Set a watchpoint for an expression.\n\
06a64a0b 15882Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15883A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15884an expression changes.\n\
15885If -l or -location is given, this evaluates EXPRESSION and watches\n\
15886the memory to which it refers."));
65d12d83 15887 set_cmd_completer (c, expression_completer);
c906108c 15888
1bedd215
AC
15889 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15890Set a read watchpoint for an expression.\n\
06a64a0b 15891Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15892A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15893an expression is read.\n\
15894If -l or -location is given, this evaluates EXPRESSION and watches\n\
15895the memory to which it refers."));
65d12d83 15896 set_cmd_completer (c, expression_completer);
c906108c 15897
1bedd215
AC
15898 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15899Set a watchpoint for an expression.\n\
06a64a0b 15900Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15901A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15902an expression is either read or written.\n\
15903If -l or -location is given, this evaluates EXPRESSION and watches\n\
15904the memory to which it refers."));
65d12d83 15905 set_cmd_completer (c, expression_completer);
c906108c 15906
11db9430 15907 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15908Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15909
920d2a44
AC
15910 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15911 respond to changes - contrary to the description. */
85c07804
AC
15912 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15913 &can_use_hw_watchpoints, _("\
15914Set debugger's willingness to use watchpoint hardware."), _("\
15915Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15916If zero, gdb will not use hardware for new watchpoints, even if\n\
15917such is available. (However, any hardware watchpoints that were\n\
15918created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15919hardware.)"),
15920 NULL,
920d2a44 15921 show_can_use_hw_watchpoints,
85c07804 15922 &setlist, &showlist);
c906108c
SS
15923
15924 can_use_hw_watchpoints = 1;
fa8d40ab 15925
1042e4c0
SS
15926 /* Tracepoint manipulation commands. */
15927
15928 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15929Set a tracepoint at specified location.\n\
1042e4c0
SS
15930\n"
15931BREAK_ARGS_HELP ("trace") "\n\
15932Do \"help tracepoints\" for info on other tracepoint commands."));
15933 set_cmd_completer (c, location_completer);
15934
15935 add_com_alias ("tp", "trace", class_alias, 0);
15936 add_com_alias ("tr", "trace", class_alias, 1);
15937 add_com_alias ("tra", "trace", class_alias, 1);
15938 add_com_alias ("trac", "trace", class_alias, 1);
15939
7a697b8d 15940 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15941Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15942\n"
15943BREAK_ARGS_HELP ("ftrace") "\n\
15944Do \"help tracepoints\" for info on other tracepoint commands."));
15945 set_cmd_completer (c, location_completer);
15946
0fb4aa4b 15947 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15948Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15949\n\
15950strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15951LOCATION may be a linespec, explicit, or address location (described below) \n\
15952or -m MARKER_ID.\n\n\
15953If a marker id is specified, probe the marker with that name. With\n\
15954no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15955Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15956This collects arbitrary user data passed in the probe point call to the\n\
15957tracing library. You can inspect it when analyzing the trace buffer,\n\
15958by printing the $_sdata variable like any other convenience variable.\n\
15959\n\
15960CONDITION is a boolean expression.\n\
629500fa 15961\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15962Multiple tracepoints at one place are permitted, and useful if their\n\
15963conditions are different.\n\
0fb4aa4b
PA
15964\n\
15965Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15966Do \"help tracepoints\" for info on other tracepoint commands."));
15967 set_cmd_completer (c, location_completer);
15968
11db9430 15969 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15970Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15971Convenience variable \"$tpnum\" contains the number of the\n\
15972last tracepoint set."));
15973
15974 add_info_alias ("tp", "tracepoints", 1);
15975
15976 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15977Delete specified tracepoints.\n\
15978Arguments are tracepoint numbers, separated by spaces.\n\
15979No argument means delete all tracepoints."),
15980 &deletelist);
7e20dfcd 15981 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15982
15983 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15984Disable specified tracepoints.\n\
15985Arguments are tracepoint numbers, separated by spaces.\n\
15986No argument means disable all tracepoints."),
15987 &disablelist);
15988 deprecate_cmd (c, "disable");
15989
15990 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15991Enable specified tracepoints.\n\
15992Arguments are tracepoint numbers, separated by spaces.\n\
15993No argument means enable all tracepoints."),
15994 &enablelist);
15995 deprecate_cmd (c, "enable");
15996
15997 add_com ("passcount", class_trace, trace_pass_command, _("\
15998Set the passcount for a tracepoint.\n\
15999The trace will end when the tracepoint has been passed 'count' times.\n\
16000Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16001if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16002
6149aea9
PA
16003 add_prefix_cmd ("save", class_breakpoint, save_command,
16004 _("Save breakpoint definitions as a script."),
16005 &save_cmdlist, "save ",
16006 0/*allow-unknown*/, &cmdlist);
16007
16008 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16009Save current breakpoint definitions as a script.\n\
cce7e648 16010This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16011catchpoints, tracepoints). Use the 'source' command in another debug\n\
16012session to restore them."),
16013 &save_cmdlist);
16014 set_cmd_completer (c, filename_completer);
16015
16016 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16017Save current tracepoint definitions as a script.\n\
6149aea9
PA
16018Use the 'source' command in another debug session to restore them."),
16019 &save_cmdlist);
1042e4c0
SS
16020 set_cmd_completer (c, filename_completer);
16021
6149aea9
PA
16022 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16023 deprecate_cmd (c, "save tracepoints");
16024
1bedd215 16025 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16026Breakpoint specific settings\n\
16027Configure various breakpoint-specific variables such as\n\
1bedd215 16028pending breakpoint behavior"),
fa8d40ab
JJ
16029 &breakpoint_set_cmdlist, "set breakpoint ",
16030 0/*allow-unknown*/, &setlist);
1bedd215 16031 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16032Breakpoint specific settings\n\
16033Configure various breakpoint-specific variables such as\n\
1bedd215 16034pending breakpoint behavior"),
fa8d40ab
JJ
16035 &breakpoint_show_cmdlist, "show breakpoint ",
16036 0/*allow-unknown*/, &showlist);
16037
7915a72c
AC
16038 add_setshow_auto_boolean_cmd ("pending", no_class,
16039 &pending_break_support, _("\
16040Set debugger's behavior regarding pending breakpoints."), _("\
16041Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16042If on, an unrecognized breakpoint location will cause gdb to create a\n\
16043pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16044an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16045user-query to see if a pending breakpoint should be created."),
2c5b56ce 16046 NULL,
920d2a44 16047 show_pending_break_support,
6e1d7d6c
AC
16048 &breakpoint_set_cmdlist,
16049 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16050
16051 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16052
16053 add_setshow_boolean_cmd ("auto-hw", no_class,
16054 &automatic_hardware_breakpoints, _("\
16055Set automatic usage of hardware breakpoints."), _("\
16056Show automatic usage of hardware breakpoints."), _("\
16057If set, the debugger will automatically use hardware breakpoints for\n\
16058breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16059a warning will be emitted for such breakpoints."),
16060 NULL,
16061 show_automatic_hardware_breakpoints,
16062 &breakpoint_set_cmdlist,
16063 &breakpoint_show_cmdlist);
74960c60 16064
a25a5a45
PA
16065 add_setshow_boolean_cmd ("always-inserted", class_support,
16066 &always_inserted_mode, _("\
74960c60
VP
16067Set mode for inserting breakpoints."), _("\
16068Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16069When this mode is on, breakpoints are inserted immediately as soon as\n\
16070they're created, kept inserted even when execution stops, and removed\n\
16071only when the user deletes them. When this mode is off (the default),\n\
16072breakpoints are inserted only when execution continues, and removed\n\
16073when execution stops."),
72d0e2c5
YQ
16074 NULL,
16075 &show_always_inserted_mode,
16076 &breakpoint_set_cmdlist,
16077 &breakpoint_show_cmdlist);
f1310107 16078
b775012e
LM
16079 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16080 condition_evaluation_enums,
16081 &condition_evaluation_mode_1, _("\
16082Set mode of breakpoint condition evaluation."), _("\
16083Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16084When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16085evaluated on the host's side by GDB. When it is set to \"target\",\n\
16086breakpoint conditions will be downloaded to the target (if the target\n\
16087supports such feature) and conditions will be evaluated on the target's side.\n\
16088If this is set to \"auto\" (default), this will be automatically set to\n\
16089\"target\" if it supports condition evaluation, otherwise it will\n\
16090be set to \"gdb\""),
16091 &set_condition_evaluation_mode,
16092 &show_condition_evaluation_mode,
16093 &breakpoint_set_cmdlist,
16094 &breakpoint_show_cmdlist);
16095
f1310107
TJB
16096 add_com ("break-range", class_breakpoint, break_range_command, _("\
16097Set a breakpoint for an address range.\n\
16098break-range START-LOCATION, END-LOCATION\n\
16099where START-LOCATION and END-LOCATION can be one of the following:\n\
16100 LINENUM, for that line in the current file,\n\
16101 FILE:LINENUM, for that line in that file,\n\
16102 +OFFSET, for that number of lines after the current line\n\
16103 or the start of the range\n\
16104 FUNCTION, for the first line in that function,\n\
16105 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16106 *ADDRESS, for the instruction at that address.\n\
16107\n\
16108The breakpoint will stop execution of the inferior whenever it executes\n\
16109an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16110range (including START-LOCATION and END-LOCATION)."));
16111
e7e0cddf 16112 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16113Set a dynamic printf at specified location.\n\
e7e0cddf 16114dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16115location may be a linespec, explicit, or address location.\n"
16116"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16117 set_cmd_completer (c, location_completer);
16118
16119 add_setshow_enum_cmd ("dprintf-style", class_support,
16120 dprintf_style_enums, &dprintf_style, _("\
16121Set the style of usage for dynamic printf."), _("\
16122Show the style of usage for dynamic printf."), _("\
16123This setting chooses how GDB will do a dynamic printf.\n\
16124If the value is \"gdb\", then the printing is done by GDB to its own\n\
16125console, as with the \"printf\" command.\n\
16126If the value is \"call\", the print is done by calling a function in your\n\
16127program; by default printf(), but you can choose a different function or\n\
16128output stream by setting dprintf-function and dprintf-channel."),
16129 update_dprintf_commands, NULL,
16130 &setlist, &showlist);
16131
16132 dprintf_function = xstrdup ("printf");
16133 add_setshow_string_cmd ("dprintf-function", class_support,
16134 &dprintf_function, _("\
16135Set the function to use for dynamic printf"), _("\
16136Show the function to use for dynamic printf"), NULL,
16137 update_dprintf_commands, NULL,
16138 &setlist, &showlist);
16139
16140 dprintf_channel = xstrdup ("");
16141 add_setshow_string_cmd ("dprintf-channel", class_support,
16142 &dprintf_channel, _("\
16143Set the channel to use for dynamic printf"), _("\
16144Show the channel to use for dynamic printf"), NULL,
16145 update_dprintf_commands, NULL,
16146 &setlist, &showlist);
16147
d3ce09f5
SS
16148 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16149 &disconnected_dprintf, _("\
16150Set whether dprintf continues after GDB disconnects."), _("\
16151Show whether dprintf continues after GDB disconnects."), _("\
16152Use this to let dprintf commands continue to hit and produce output\n\
16153even if GDB disconnects or detaches from the target."),
16154 NULL,
16155 NULL,
16156 &setlist, &showlist);
16157
16158 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16159agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16160(target agent only) This is useful for formatted output in user-defined commands."));
16161
765dc015 16162 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16163
16164 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16165 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16166}
This page took 3.139299 seconds and 4 git commands to generate.