[AArch64] Mark LR clobbered by BL in inline asm
[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
896b6bda 96static void map_breakpoint_numbers (const char *,
48649e1b 97 gdb::function_view<void (breakpoint *)>);
c906108c 98
a14ed312 99static void ignore_command (char *, int);
c906108c 100
348d480f
PA
101static void breakpoint_re_set_default (struct breakpoint *);
102
f00aae0f
KS
103static void
104 create_sals_from_location_default (const struct event_location *location,
105 struct linespec_result *canonical,
106 enum bptype type_wanted);
983af33b
SDJ
107
108static void create_breakpoints_sal_default (struct gdbarch *,
109 struct linespec_result *,
e1e01040
PA
110 gdb::unique_xmalloc_ptr<char>,
111 gdb::unique_xmalloc_ptr<char>,
112 enum bptype,
983af33b
SDJ
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
44f238bb 116 int, int, int, unsigned);
983af33b 117
6c5b2ebe
PA
118static std::vector<symtab_and_line> decode_location_default
119 (struct breakpoint *b, const struct event_location *location,
120 struct program_space *search_pspace);
983af33b 121
a14ed312 122static void clear_command (char *, int);
c906108c 123
a9634178 124static int can_use_hardware_watchpoint (struct value *);
c906108c 125
a14ed312 126static void mention (struct breakpoint *);
c906108c 127
348d480f
PA
128static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
129 enum bptype,
c0a91b2b 130 const struct breakpoint_ops *);
3742cc8b
YQ
131static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
132 const struct symtab_and_line *);
133
4a64f543
MS
134/* This function is used in gdbtk sources and thus can not be made
135 static. */
63c252f8 136struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 137 struct symtab_and_line,
c0a91b2b
TT
138 enum bptype,
139 const struct breakpoint_ops *);
c906108c 140
06edf0c0
PA
141static struct breakpoint *
142 momentary_breakpoint_from_master (struct breakpoint *orig,
143 enum bptype type,
a1aa2221
LM
144 const struct breakpoint_ops *ops,
145 int loc_enabled);
06edf0c0 146
76897487
KB
147static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
148
a6d9a66e
UW
149static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
150 CORE_ADDR bpaddr,
88f7da05 151 enum bptype bptype);
76897487 152
6c95b8df
PA
153static void describe_other_breakpoints (struct gdbarch *,
154 struct program_space *, CORE_ADDR,
5af949e3 155 struct obj_section *, int);
c906108c 156
85d721b8
PA
157static int watchpoint_locations_match (struct bp_location *loc1,
158 struct bp_location *loc2);
159
f1310107 160static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 161 const struct address_space *aspace,
f1310107
TJB
162 CORE_ADDR addr);
163
d35ae833 164static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 165 const address_space *,
d35ae833
PA
166 CORE_ADDR, int);
167
11db9430 168static void info_breakpoints_command (char *, int);
c906108c 169
11db9430 170static void info_watchpoints_command (char *, int);
d77f58be 171
a14ed312 172static void commands_command (char *, int);
c906108c 173
a14ed312 174static void condition_command (char *, int);
c906108c 175
834c0d03 176static int remove_breakpoint (struct bp_location *);
b2b6a7da 177static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 178
e514a9d6 179static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 180
a14ed312 181static int hw_breakpoint_used_count (void);
c906108c 182
a1398e0c
PA
183static int hw_watchpoint_use_count (struct breakpoint *);
184
185static int hw_watchpoint_used_count_others (struct breakpoint *except,
186 enum bptype type,
187 int *other_type_used);
c906108c 188
a14ed312 189static void hbreak_command (char *, int);
c906108c 190
a14ed312 191static void thbreak_command (char *, int);
c906108c 192
816338b5
SS
193static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
194 int count);
c906108c 195
a14ed312 196static void stop_command (char *arg, int from_tty);
7a292a7a 197
fe3f5fa8 198static void free_bp_location (struct bp_location *loc);
f431efe5
PA
199static void incref_bp_location (struct bp_location *loc);
200static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 201
39d61571 202static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 203
44702360
PA
204/* update_global_location_list's modes of operation wrt to whether to
205 insert locations now. */
206enum ugll_insert_mode
207{
208 /* Don't insert any breakpoint locations into the inferior, only
209 remove already-inserted locations that no longer should be
210 inserted. Functions that delete a breakpoint or breakpoints
211 should specify this mode, so that deleting a breakpoint doesn't
212 have the side effect of inserting the locations of other
213 breakpoints that are marked not-inserted, but should_be_inserted
214 returns true on them.
215
216 This behavior is useful is situations close to tear-down -- e.g.,
217 after an exec, while the target still has execution, but
218 breakpoint shadows of the previous executable image should *NOT*
219 be restored to the new image; or before detaching, where the
220 target still has execution and wants to delete breakpoints from
221 GDB's lists, and all breakpoints had already been removed from
222 the inferior. */
223 UGLL_DONT_INSERT,
224
a25a5a45
PA
225 /* May insert breakpoints iff breakpoints_should_be_inserted_now
226 claims breakpoints should be inserted now. */
04086b45
PA
227 UGLL_MAY_INSERT,
228
a25a5a45
PA
229 /* Insert locations now, irrespective of
230 breakpoints_should_be_inserted_now. E.g., say all threads are
231 stopped right now, and the user did "continue". We need to
232 insert breakpoints _before_ resuming the target, but
233 UGLL_MAY_INSERT wouldn't insert them, because
234 breakpoints_should_be_inserted_now returns false at that point,
235 as no thread is running yet. */
04086b45 236 UGLL_INSERT
44702360
PA
237};
238
239static void update_global_location_list (enum ugll_insert_mode);
a5606eee 240
44702360 241static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 242
d77f58be 243static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
244
245static void insert_breakpoint_locations (void);
a5606eee 246
11db9430 247static void info_tracepoints_command (char *, int);
1042e4c0 248
1042e4c0
SS
249static void enable_trace_command (char *, int);
250
251static void disable_trace_command (char *, int);
252
253static void trace_pass_command (char *, int);
254
558a9d82
YQ
255static void set_tracepoint_count (int num);
256
9c06b0b4
TJB
257static int is_masked_watchpoint (const struct breakpoint *b);
258
b775012e
LM
259static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
260
983af33b
SDJ
261/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
262 otherwise. */
263
264static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 265
2060206e
PA
266/* The breakpoint_ops structure to be inherited by all breakpoint_ops
267 that are implemented on top of software or hardware breakpoints
268 (user breakpoints, internal and momentary breakpoints, etc.). */
269static struct breakpoint_ops bkpt_base_breakpoint_ops;
270
271/* Internal breakpoints class type. */
06edf0c0 272static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
273
274/* Momentary breakpoints class type. */
06edf0c0
PA
275static struct breakpoint_ops momentary_breakpoint_ops;
276
2060206e
PA
277/* The breakpoint_ops structure to be used in regular user created
278 breakpoints. */
279struct breakpoint_ops bkpt_breakpoint_ops;
280
55aa24fb
SDJ
281/* Breakpoints set on probes. */
282static struct breakpoint_ops bkpt_probe_breakpoint_ops;
283
e7e0cddf 284/* Dynamic printf class type. */
c5867ab6 285struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 286
d3ce09f5
SS
287/* The style in which to perform a dynamic printf. This is a user
288 option because different output options have different tradeoffs;
289 if GDB does the printing, there is better error handling if there
290 is a problem with any of the arguments, but using an inferior
291 function lets you have special-purpose printers and sending of
292 output to the same place as compiled-in print functions. */
293
294static const char dprintf_style_gdb[] = "gdb";
295static const char dprintf_style_call[] = "call";
296static const char dprintf_style_agent[] = "agent";
297static const char *const dprintf_style_enums[] = {
298 dprintf_style_gdb,
299 dprintf_style_call,
300 dprintf_style_agent,
301 NULL
302};
303static const char *dprintf_style = dprintf_style_gdb;
304
305/* The function to use for dynamic printf if the preferred style is to
306 call into the inferior. The value is simply a string that is
307 copied into the command, so it can be anything that GDB can
308 evaluate to a callable address, not necessarily a function name. */
309
bde6261a 310static char *dprintf_function;
d3ce09f5
SS
311
312/* The channel to use for dynamic printf if the preferred style is to
313 call into the inferior; if a nonempty string, it will be passed to
314 the call as the first argument, with the format string as the
315 second. As with the dprintf function, this can be anything that
316 GDB knows how to evaluate, so in addition to common choices like
317 "stderr", this could be an app-specific expression like
318 "mystreams[curlogger]". */
319
bde6261a 320static char *dprintf_channel;
d3ce09f5
SS
321
322/* True if dprintf commands should continue to operate even if GDB
323 has disconnected. */
324static int disconnected_dprintf = 1;
325
5cea2a26
PA
326struct command_line *
327breakpoint_commands (struct breakpoint *b)
328{
d1b0a7bf 329 return b->commands ? b->commands.get () : NULL;
5cea2a26 330}
3daf8fe5 331
f3b1572e
PA
332/* Flag indicating that a command has proceeded the inferior past the
333 current breakpoint. */
334
335static int breakpoint_proceeded;
336
956a9fb9 337const char *
2cec12e5
AR
338bpdisp_text (enum bpdisp disp)
339{
4a64f543
MS
340 /* NOTE: the following values are a part of MI protocol and
341 represent values of 'disp' field returned when inferior stops at
342 a breakpoint. */
bc043ef3 343 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 344
2cec12e5
AR
345 return bpdisps[(int) disp];
346}
c906108c 347
4a64f543 348/* Prototypes for exported functions. */
c906108c 349/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 350 if such is available. */
c906108c
SS
351static int can_use_hw_watchpoints;
352
920d2a44
AC
353static void
354show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357{
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Debugger's willingness to use "
360 "watchpoint hardware is %s.\n"),
920d2a44
AC
361 value);
362}
363
fa8d40ab
JJ
364/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
365 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 366 for unrecognized breakpoint locations.
fa8d40ab
JJ
367 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
368static enum auto_boolean pending_break_support;
920d2a44
AC
369static void
370show_pending_break_support (struct ui_file *file, int from_tty,
371 struct cmd_list_element *c,
372 const char *value)
373{
3e43a32a
MS
374 fprintf_filtered (file,
375 _("Debugger's behavior regarding "
376 "pending breakpoints is %s.\n"),
920d2a44
AC
377 value);
378}
fa8d40ab 379
765dc015 380/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 381 set with "break" but falling in read-only memory.
765dc015
VP
382 If 0, gdb will warn about such breakpoints, but won't automatically
383 use hardware breakpoints. */
384static int automatic_hardware_breakpoints;
385static void
386show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
387 struct cmd_list_element *c,
388 const char *value)
389{
3e43a32a
MS
390 fprintf_filtered (file,
391 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
392 value);
393}
394
a25a5a45
PA
395/* If on, GDB keeps breakpoints inserted even if the inferior is
396 stopped, and immediately inserts any new breakpoints as soon as
397 they're created. If off (default), GDB keeps breakpoints off of
398 the target as long as possible. That is, it delays inserting
399 breakpoints until the next resume, and removes them again when the
400 target fully stops. This is a bit safer in case GDB crashes while
401 processing user input. */
402static int always_inserted_mode = 0;
72d0e2c5 403
33e5cbd6 404static void
74960c60 405show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 406 struct cmd_list_element *c, const char *value)
74960c60 407{
a25a5a45
PA
408 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
409 value);
74960c60
VP
410}
411
b57bacec
PA
412/* See breakpoint.h. */
413
33e5cbd6 414int
a25a5a45 415breakpoints_should_be_inserted_now (void)
33e5cbd6 416{
a25a5a45
PA
417 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
418 {
419 /* If breakpoints are global, they should be inserted even if no
420 thread under gdb's control is running, or even if there are
421 no threads under GDB's control yet. */
422 return 1;
423 }
424 else if (target_has_execution)
425 {
372316f1
PA
426 struct thread_info *tp;
427
a25a5a45
PA
428 if (always_inserted_mode)
429 {
430 /* The user wants breakpoints inserted even if all threads
431 are stopped. */
432 return 1;
433 }
434
b57bacec
PA
435 if (threads_are_executing ())
436 return 1;
372316f1
PA
437
438 /* Don't remove breakpoints yet if, even though all threads are
439 stopped, we still have events to process. */
440 ALL_NON_EXITED_THREADS (tp)
441 if (tp->resumed
442 && tp->suspend.waitstatus_pending_p)
443 return 1;
a25a5a45
PA
444 }
445 return 0;
33e5cbd6 446}
765dc015 447
b775012e
LM
448static const char condition_evaluation_both[] = "host or target";
449
450/* Modes for breakpoint condition evaluation. */
451static const char condition_evaluation_auto[] = "auto";
452static const char condition_evaluation_host[] = "host";
453static const char condition_evaluation_target[] = "target";
454static const char *const condition_evaluation_enums[] = {
455 condition_evaluation_auto,
456 condition_evaluation_host,
457 condition_evaluation_target,
458 NULL
459};
460
461/* Global that holds the current mode for breakpoint condition evaluation. */
462static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
463
464/* Global that we use to display information to the user (gets its value from
465 condition_evaluation_mode_1. */
466static const char *condition_evaluation_mode = condition_evaluation_auto;
467
468/* Translate a condition evaluation mode MODE into either "host"
469 or "target". This is used mostly to translate from "auto" to the
470 real setting that is being used. It returns the translated
471 evaluation mode. */
472
473static const char *
474translate_condition_evaluation_mode (const char *mode)
475{
476 if (mode == condition_evaluation_auto)
477 {
478 if (target_supports_evaluation_of_breakpoint_conditions ())
479 return condition_evaluation_target;
480 else
481 return condition_evaluation_host;
482 }
483 else
484 return mode;
485}
486
487/* Discovers what condition_evaluation_auto translates to. */
488
489static const char *
490breakpoint_condition_evaluation_mode (void)
491{
492 return translate_condition_evaluation_mode (condition_evaluation_mode);
493}
494
495/* Return true if GDB should evaluate breakpoint conditions or false
496 otherwise. */
497
498static int
499gdb_evaluates_breakpoint_condition_p (void)
500{
501 const char *mode = breakpoint_condition_evaluation_mode ();
502
503 return (mode == condition_evaluation_host);
504}
505
c906108c
SS
506/* Are we executing breakpoint commands? */
507static int executing_breakpoint_commands;
508
c02f5703
MS
509/* Are overlay event breakpoints enabled? */
510static int overlay_events_enabled;
511
e09342b5
TJB
512/* See description in breakpoint.h. */
513int target_exact_watchpoints = 0;
514
c906108c 515/* Walk the following statement or block through all breakpoints.
e5dd4106 516 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 517 current breakpoint. */
c906108c 518
5c44784c 519#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 520
5c44784c
JM
521#define ALL_BREAKPOINTS_SAFE(B,TMP) \
522 for (B = breakpoint_chain; \
523 B ? (TMP=B->next, 1): 0; \
524 B = TMP)
c906108c 525
4a64f543
MS
526/* Similar iterator for the low-level breakpoints. SAFE variant is
527 not provided so update_global_location_list must not be called
528 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 529
876fa593 530#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
531 for (BP_TMP = bp_locations; \
532 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 533 BP_TMP++)
7cc221ef 534
b775012e
LM
535/* Iterates through locations with address ADDRESS for the currently selected
536 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
537 to where the loop should start from.
538 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
539 appropriate location to start with. */
540
541#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
542 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
543 BP_LOCP_TMP = BP_LOCP_START; \
544 BP_LOCP_START \
f5336ca5 545 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
546 && (*BP_LOCP_TMP)->address == ADDRESS); \
547 BP_LOCP_TMP++)
548
1042e4c0
SS
549/* Iterator for tracepoints only. */
550
551#define ALL_TRACEPOINTS(B) \
552 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 553 if (is_tracepoint (B))
1042e4c0 554
7cc221ef 555/* Chains of all breakpoints defined. */
c906108c
SS
556
557struct breakpoint *breakpoint_chain;
558
f5336ca5 559/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 560
f5336ca5 561static struct bp_location **bp_locations;
876fa593 562
f5336ca5 563/* Number of elements of BP_LOCATIONS. */
876fa593 564
f5336ca5 565static unsigned bp_locations_count;
876fa593 566
4a64f543 567/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 568 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 569 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 570 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 571 an address you need to read. */
876fa593 572
f5336ca5 573static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 574
4a64f543
MS
575/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
576 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
577 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
578 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 579 scan for shadow bytes for an address you need to read. */
876fa593 580
f5336ca5 581static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 582
4a64f543 583/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
584 from the bp_locations array, but for which a hit may still be
585 reported by a target. */
20874c92
VP
586VEC(bp_location_p) *moribund_locations = NULL;
587
c906108c
SS
588/* Number of last breakpoint made. */
589
95a42b64
TT
590static int breakpoint_count;
591
86b17b60
PA
592/* The value of `breakpoint_count' before the last command that
593 created breakpoints. If the last (break-like) command created more
594 than one breakpoint, then the difference between BREAKPOINT_COUNT
595 and PREV_BREAKPOINT_COUNT is more than one. */
596static int prev_breakpoint_count;
c906108c 597
1042e4c0
SS
598/* Number of last tracepoint made. */
599
95a42b64 600static int tracepoint_count;
1042e4c0 601
6149aea9
PA
602static struct cmd_list_element *breakpoint_set_cmdlist;
603static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 604struct cmd_list_element *save_cmdlist;
6149aea9 605
badd37ce
SDJ
606/* See declaration at breakpoint.h. */
607
608struct breakpoint *
609breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
610 void *user_data)
611{
612 struct breakpoint *b = NULL;
613
614 ALL_BREAKPOINTS (b)
615 {
616 if (func (b, user_data) != 0)
617 break;
618 }
619
620 return b;
621}
622
468d015d
JJ
623/* Return whether a breakpoint is an active enabled breakpoint. */
624static int
625breakpoint_enabled (struct breakpoint *b)
626{
0d381245 627 return (b->enable_state == bp_enabled);
468d015d
JJ
628}
629
c906108c
SS
630/* Set breakpoint count to NUM. */
631
95a42b64 632static void
fba45db2 633set_breakpoint_count (int num)
c906108c 634{
86b17b60 635 prev_breakpoint_count = breakpoint_count;
c906108c 636 breakpoint_count = num;
4fa62494 637 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
638}
639
86b17b60
PA
640/* Used by `start_rbreak_breakpoints' below, to record the current
641 breakpoint count before "rbreak" creates any breakpoint. */
642static int rbreak_start_breakpoint_count;
643
95a42b64
TT
644/* Called at the start an "rbreak" command to record the first
645 breakpoint made. */
86b17b60 646
95a42b64
TT
647void
648start_rbreak_breakpoints (void)
649{
86b17b60 650 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
651}
652
653/* Called at the end of an "rbreak" command to record the last
654 breakpoint made. */
86b17b60 655
95a42b64
TT
656void
657end_rbreak_breakpoints (void)
658{
86b17b60 659 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
660}
661
4a64f543 662/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
663
664void
fba45db2 665clear_breakpoint_hit_counts (void)
c906108c
SS
666{
667 struct breakpoint *b;
668
669 ALL_BREAKPOINTS (b)
670 b->hit_count = 0;
671}
672
c906108c 673\f
48cb2d85
VP
674/* Return the breakpoint with the specified number, or NULL
675 if the number does not refer to an existing breakpoint. */
676
677struct breakpoint *
678get_breakpoint (int num)
679{
680 struct breakpoint *b;
681
682 ALL_BREAKPOINTS (b)
683 if (b->number == num)
684 return b;
685
686 return NULL;
687}
5c44784c 688
c906108c 689\f
adc36818 690
b775012e
LM
691/* Mark locations as "conditions have changed" in case the target supports
692 evaluating conditions on its side. */
693
694static void
695mark_breakpoint_modified (struct breakpoint *b)
696{
697 struct bp_location *loc;
698
699 /* This is only meaningful if the target is
700 evaluating conditions and if the user has
701 opted for condition evaluation on the target's
702 side. */
703 if (gdb_evaluates_breakpoint_condition_p ()
704 || !target_supports_evaluation_of_breakpoint_conditions ())
705 return;
706
707 if (!is_breakpoint (b))
708 return;
709
710 for (loc = b->loc; loc; loc = loc->next)
711 loc->condition_changed = condition_modified;
712}
713
714/* Mark location as "conditions have changed" in case the target supports
715 evaluating conditions on its side. */
716
717static void
718mark_breakpoint_location_modified (struct bp_location *loc)
719{
720 /* This is only meaningful if the target is
721 evaluating conditions and if the user has
722 opted for condition evaluation on the target's
723 side. */
724 if (gdb_evaluates_breakpoint_condition_p ()
725 || !target_supports_evaluation_of_breakpoint_conditions ())
726
727 return;
728
729 if (!is_breakpoint (loc->owner))
730 return;
731
732 loc->condition_changed = condition_modified;
733}
734
735/* Sets the condition-evaluation mode using the static global
736 condition_evaluation_mode. */
737
738static void
739set_condition_evaluation_mode (char *args, int from_tty,
740 struct cmd_list_element *c)
741{
b775012e
LM
742 const char *old_mode, *new_mode;
743
744 if ((condition_evaluation_mode_1 == condition_evaluation_target)
745 && !target_supports_evaluation_of_breakpoint_conditions ())
746 {
747 condition_evaluation_mode_1 = condition_evaluation_mode;
748 warning (_("Target does not support breakpoint condition evaluation.\n"
749 "Using host evaluation mode instead."));
750 return;
751 }
752
753 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
754 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
755
abf1152a
JK
756 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
757 settings was "auto". */
758 condition_evaluation_mode = condition_evaluation_mode_1;
759
b775012e
LM
760 /* Only update the mode if the user picked a different one. */
761 if (new_mode != old_mode)
762 {
763 struct bp_location *loc, **loc_tmp;
764 /* If the user switched to a different evaluation mode, we
765 need to synch the changes with the target as follows:
766
767 "host" -> "target": Send all (valid) conditions to the target.
768 "target" -> "host": Remove all the conditions from the target.
769 */
770
b775012e
LM
771 if (new_mode == condition_evaluation_target)
772 {
773 /* Mark everything modified and synch conditions with the
774 target. */
775 ALL_BP_LOCATIONS (loc, loc_tmp)
776 mark_breakpoint_location_modified (loc);
777 }
778 else
779 {
780 /* Manually mark non-duplicate locations to synch conditions
781 with the target. We do this to remove all the conditions the
782 target knows about. */
783 ALL_BP_LOCATIONS (loc, loc_tmp)
784 if (is_breakpoint (loc->owner) && loc->inserted)
785 loc->needs_update = 1;
786 }
787
788 /* Do the update. */
44702360 789 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
790 }
791
792 return;
793}
794
795/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
796 what "auto" is translating to. */
797
798static void
799show_condition_evaluation_mode (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801{
802 if (condition_evaluation_mode == condition_evaluation_auto)
803 fprintf_filtered (file,
804 _("Breakpoint condition evaluation "
805 "mode is %s (currently %s).\n"),
806 value,
807 breakpoint_condition_evaluation_mode ());
808 else
809 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
810 value);
811}
812
813/* A comparison function for bp_location AP and BP that is used by
814 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 815 the more general bp_locations_compare function. */
b775012e
LM
816
817static int
f5336ca5 818bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 819{
9a3c8263
SM
820 const struct bp_location *a = *(const struct bp_location **) ap;
821 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
822
823 if (a->address == b->address)
824 return 0;
825 else
826 return ((a->address > b->address) - (a->address < b->address));
827}
828
829/* Helper function to skip all bp_locations with addresses
830 less than ADDRESS. It returns the first bp_location that
831 is greater than or equal to ADDRESS. If none is found, just
832 return NULL. */
833
834static struct bp_location **
835get_first_locp_gte_addr (CORE_ADDR address)
836{
837 struct bp_location dummy_loc;
838 struct bp_location *dummy_locp = &dummy_loc;
839 struct bp_location **locp_found = NULL;
840
841 /* Initialize the dummy location's address field. */
b775012e
LM
842 dummy_loc.address = address;
843
844 /* Find a close match to the first location at ADDRESS. */
9a3c8263 845 locp_found = ((struct bp_location **)
f5336ca5 846 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 847 sizeof (struct bp_location **),
f5336ca5 848 bp_locations_compare_addrs));
b775012e
LM
849
850 /* Nothing was found, nothing left to do. */
851 if (locp_found == NULL)
852 return NULL;
853
854 /* We may have found a location that is at ADDRESS but is not the first in the
855 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 856 while ((locp_found - 1) >= bp_locations
b775012e
LM
857 && (*(locp_found - 1))->address == address)
858 locp_found--;
859
860 return locp_found;
861}
862
adc36818 863void
7a26bd4d 864set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
865 int from_tty)
866{
3a5c3e22
PA
867 xfree (b->cond_string);
868 b->cond_string = NULL;
adc36818 869
3a5c3e22 870 if (is_watchpoint (b))
adc36818 871 {
3a5c3e22
PA
872 struct watchpoint *w = (struct watchpoint *) b;
873
4d01a485 874 w->cond_exp.reset ();
3a5c3e22
PA
875 }
876 else
877 {
878 struct bp_location *loc;
879
880 for (loc = b->loc; loc; loc = loc->next)
881 {
4d01a485 882 loc->cond.reset ();
b775012e
LM
883
884 /* No need to free the condition agent expression
885 bytecode (if we have one). We will handle this
886 when we go through update_global_location_list. */
3a5c3e22 887 }
adc36818 888 }
adc36818
PM
889
890 if (*exp == 0)
891 {
892 if (from_tty)
893 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
894 }
895 else
896 {
bbc13ae3 897 const char *arg = exp;
cc59ec59 898
adc36818
PM
899 /* I don't know if it matters whether this is the string the user
900 typed in or the decompiled expression. */
901 b->cond_string = xstrdup (arg);
902 b->condition_not_parsed = 0;
903
904 if (is_watchpoint (b))
905 {
3a5c3e22
PA
906 struct watchpoint *w = (struct watchpoint *) b;
907
adc36818
PM
908 innermost_block = NULL;
909 arg = exp;
1bb9788d 910 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
911 if (*arg)
912 error (_("Junk at end of expression"));
3a5c3e22 913 w->cond_exp_valid_block = innermost_block;
adc36818
PM
914 }
915 else
916 {
3a5c3e22
PA
917 struct bp_location *loc;
918
adc36818
PM
919 for (loc = b->loc; loc; loc = loc->next)
920 {
921 arg = exp;
922 loc->cond =
1bb9788d
TT
923 parse_exp_1 (&arg, loc->address,
924 block_for_pc (loc->address), 0);
adc36818
PM
925 if (*arg)
926 error (_("Junk at end of expression"));
927 }
928 }
929 }
b775012e
LM
930 mark_breakpoint_modified (b);
931
8d3788bd 932 observer_notify_breakpoint_modified (b);
adc36818
PM
933}
934
d55637df
TT
935/* Completion for the "condition" command. */
936
eb3ff9a5 937static void
6f937416 938condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 939 completion_tracker &tracker,
6f937416 940 const char *text, const char *word)
d55637df 941{
6f937416 942 const char *space;
d55637df 943
f1735a53
TT
944 text = skip_spaces (text);
945 space = skip_to_space (text);
d55637df
TT
946 if (*space == '\0')
947 {
948 int len;
949 struct breakpoint *b;
950 VEC (char_ptr) *result = NULL;
951
952 if (text[0] == '$')
953 {
954 /* We don't support completion of history indices. */
eb3ff9a5
PA
955 if (!isdigit (text[1]))
956 complete_internalvar (tracker, &text[1]);
957 return;
d55637df
TT
958 }
959
960 /* We're completing the breakpoint number. */
961 len = strlen (text);
962
963 ALL_BREAKPOINTS (b)
58ce7251
SDJ
964 {
965 char number[50];
966
967 xsnprintf (number, sizeof (number), "%d", b->number);
968
969 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
970 {
971 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
972 tracker.add_completion (std::move (copy));
973 }
58ce7251 974 }
d55637df 975
eb3ff9a5 976 return;
d55637df
TT
977 }
978
979 /* We're completing the expression part. */
f1735a53 980 text = skip_spaces (space);
eb3ff9a5 981 expression_completer (cmd, tracker, text, word);
d55637df
TT
982}
983
c906108c
SS
984/* condition N EXP -- set break condition of breakpoint N to EXP. */
985
986static void
fba45db2 987condition_command (char *arg, int from_tty)
c906108c 988{
52f0bd74 989 struct breakpoint *b;
c906108c 990 char *p;
52f0bd74 991 int bnum;
c906108c
SS
992
993 if (arg == 0)
e2e0b3e5 994 error_no_arg (_("breakpoint number"));
c906108c
SS
995
996 p = arg;
997 bnum = get_number (&p);
5c44784c 998 if (bnum == 0)
8a3fe4f8 999 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1000
1001 ALL_BREAKPOINTS (b)
1002 if (b->number == bnum)
2f069f6f 1003 {
6dddc817
DE
1004 /* Check if this breakpoint has a "stop" method implemented in an
1005 extension language. This method and conditions entered into GDB
1006 from the CLI are mutually exclusive. */
1007 const struct extension_language_defn *extlang
1008 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1009
1010 if (extlang != NULL)
1011 {
1012 error (_("Only one stop condition allowed. There is currently"
1013 " a %s stop condition defined for this breakpoint."),
1014 ext_lang_capitalized_name (extlang));
1015 }
2566ad2d 1016 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1017
1018 if (is_breakpoint (b))
44702360 1019 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1020
2f069f6f
JB
1021 return;
1022 }
c906108c 1023
8a3fe4f8 1024 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1025}
1026
a7bdde9e
VP
1027/* Check that COMMAND do not contain commands that are suitable
1028 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1029 Throw if any such commands is found. */
1030
a7bdde9e
VP
1031static void
1032check_no_tracepoint_commands (struct command_line *commands)
1033{
1034 struct command_line *c;
cc59ec59 1035
a7bdde9e
VP
1036 for (c = commands; c; c = c->next)
1037 {
1038 int i;
1039
1040 if (c->control_type == while_stepping_control)
3e43a32a
MS
1041 error (_("The 'while-stepping' command can "
1042 "only be used for tracepoints"));
a7bdde9e
VP
1043
1044 for (i = 0; i < c->body_count; ++i)
1045 check_no_tracepoint_commands ((c->body_list)[i]);
1046
1047 /* Not that command parsing removes leading whitespace and comment
4a64f543 1048 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1049 command directly. */
1050 if (strstr (c->line, "collect ") == c->line)
1051 error (_("The 'collect' command can only be used for tracepoints"));
1052
51661e93
VP
1053 if (strstr (c->line, "teval ") == c->line)
1054 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1055 }
1056}
1057
c1fc2657 1058struct longjmp_breakpoint : public breakpoint
3b0871f4 1059{
c1fc2657 1060 ~longjmp_breakpoint () override;
3b0871f4
SM
1061};
1062
d77f58be
SS
1063/* Encapsulate tests for different types of tracepoints. */
1064
3b0871f4
SM
1065static bool
1066is_tracepoint_type (bptype type)
d9b3f62e
PA
1067{
1068 return (type == bp_tracepoint
1069 || type == bp_fast_tracepoint
1070 || type == bp_static_tracepoint);
1071}
1072
3b0871f4
SM
1073static bool
1074is_longjmp_type (bptype type)
1075{
1076 return type == bp_longjmp || type == bp_exception;
1077}
1078
a7bdde9e 1079int
d77f58be 1080is_tracepoint (const struct breakpoint *b)
a7bdde9e 1081{
d9b3f62e 1082 return is_tracepoint_type (b->type);
a7bdde9e 1083}
d9b3f62e 1084
a5e364af
SM
1085/* Factory function to create an appropriate instance of breakpoint given
1086 TYPE. */
1087
1088static std::unique_ptr<breakpoint>
1089new_breakpoint_from_type (bptype type)
1090{
1091 breakpoint *b;
1092
1093 if (is_tracepoint_type (type))
c1fc2657 1094 b = new tracepoint ();
3b0871f4 1095 else if (is_longjmp_type (type))
c1fc2657 1096 b = new longjmp_breakpoint ();
a5e364af
SM
1097 else
1098 b = new breakpoint ();
1099
1100 return std::unique_ptr<breakpoint> (b);
1101}
1102
e5dd4106 1103/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1104 breakpoint. This function will throw an exception if a problem is
1105 found. */
48cb2d85 1106
95a42b64
TT
1107static void
1108validate_commands_for_breakpoint (struct breakpoint *b,
1109 struct command_line *commands)
48cb2d85 1110{
d77f58be 1111 if (is_tracepoint (b))
a7bdde9e 1112 {
c9a6ce02 1113 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1114 struct command_line *c;
1115 struct command_line *while_stepping = 0;
c9a6ce02
PA
1116
1117 /* Reset the while-stepping step count. The previous commands
1118 might have included a while-stepping action, while the new
1119 ones might not. */
1120 t->step_count = 0;
1121
1122 /* We need to verify that each top-level element of commands is
1123 valid for tracepoints, that there's at most one
1124 while-stepping element, and that the while-stepping's body
1125 has valid tracing commands excluding nested while-stepping.
1126 We also need to validate the tracepoint action line in the
1127 context of the tracepoint --- validate_actionline actually
1128 has side effects, like setting the tracepoint's
1129 while-stepping STEP_COUNT, in addition to checking if the
1130 collect/teval actions parse and make sense in the
1131 tracepoint's context. */
a7bdde9e
VP
1132 for (c = commands; c; c = c->next)
1133 {
a7bdde9e
VP
1134 if (c->control_type == while_stepping_control)
1135 {
1136 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1137 error (_("The 'while-stepping' command "
1138 "cannot be used for fast tracepoint"));
0fb4aa4b 1139 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1140 error (_("The 'while-stepping' command "
1141 "cannot be used for static tracepoint"));
a7bdde9e
VP
1142
1143 if (while_stepping)
3e43a32a
MS
1144 error (_("The 'while-stepping' command "
1145 "can be used only once"));
a7bdde9e
VP
1146 else
1147 while_stepping = c;
1148 }
c9a6ce02
PA
1149
1150 validate_actionline (c->line, b);
a7bdde9e
VP
1151 }
1152 if (while_stepping)
1153 {
1154 struct command_line *c2;
1155
1156 gdb_assert (while_stepping->body_count == 1);
1157 c2 = while_stepping->body_list[0];
1158 for (; c2; c2 = c2->next)
1159 {
a7bdde9e
VP
1160 if (c2->control_type == while_stepping_control)
1161 error (_("The 'while-stepping' command cannot be nested"));
1162 }
1163 }
1164 }
1165 else
1166 {
1167 check_no_tracepoint_commands (commands);
1168 }
95a42b64
TT
1169}
1170
0fb4aa4b
PA
1171/* Return a vector of all the static tracepoints set at ADDR. The
1172 caller is responsible for releasing the vector. */
1173
1174VEC(breakpoint_p) *
1175static_tracepoints_here (CORE_ADDR addr)
1176{
1177 struct breakpoint *b;
1178 VEC(breakpoint_p) *found = 0;
1179 struct bp_location *loc;
1180
1181 ALL_BREAKPOINTS (b)
1182 if (b->type == bp_static_tracepoint)
1183 {
1184 for (loc = b->loc; loc; loc = loc->next)
1185 if (loc->address == addr)
1186 VEC_safe_push(breakpoint_p, found, b);
1187 }
1188
1189 return found;
1190}
1191
95a42b64 1192/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1193 validate that only allowed commands are included. */
95a42b64
TT
1194
1195void
4a64f543 1196breakpoint_set_commands (struct breakpoint *b,
93921405 1197 command_line_up &&commands)
95a42b64 1198{
93921405 1199 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1200
d1b0a7bf 1201 b->commands = std::move (commands);
8d3788bd 1202 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1203}
1204
45a43567
TT
1205/* Set the internal `silent' flag on the breakpoint. Note that this
1206 is not the same as the "silent" that may appear in the breakpoint's
1207 commands. */
1208
1209void
1210breakpoint_set_silent (struct breakpoint *b, int silent)
1211{
1212 int old_silent = b->silent;
1213
1214 b->silent = silent;
1215 if (old_silent != silent)
8d3788bd 1216 observer_notify_breakpoint_modified (b);
45a43567
TT
1217}
1218
1219/* Set the thread for this breakpoint. If THREAD is -1, make the
1220 breakpoint work for any thread. */
1221
1222void
1223breakpoint_set_thread (struct breakpoint *b, int thread)
1224{
1225 int old_thread = b->thread;
1226
1227 b->thread = thread;
1228 if (old_thread != thread)
8d3788bd 1229 observer_notify_breakpoint_modified (b);
45a43567
TT
1230}
1231
1232/* Set the task for this breakpoint. If TASK is 0, make the
1233 breakpoint work for any task. */
1234
1235void
1236breakpoint_set_task (struct breakpoint *b, int task)
1237{
1238 int old_task = b->task;
1239
1240 b->task = task;
1241 if (old_task != task)
8d3788bd 1242 observer_notify_breakpoint_modified (b);
45a43567
TT
1243}
1244
95a42b64
TT
1245void
1246check_tracepoint_command (char *line, void *closure)
a7bdde9e 1247{
9a3c8263 1248 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1249
6f937416 1250 validate_actionline (line, b);
a7bdde9e
VP
1251}
1252
95a42b64 1253static void
896b6bda 1254commands_command_1 (const char *arg, int from_tty,
4a64f543 1255 struct command_line *control)
95a42b64 1256{
d1b0a7bf 1257 counted_command_line cmd;
95a42b64 1258
896b6bda
PA
1259 std::string new_arg;
1260
95a42b64
TT
1261 if (arg == NULL || !*arg)
1262 {
86b17b60 1263 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1264 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1265 breakpoint_count);
95a42b64 1266 else if (breakpoint_count > 0)
896b6bda 1267 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1268 arg = new_arg.c_str ();
1269 }
1270
1271 map_breakpoint_numbers
1272 (arg, [&] (breakpoint *b)
1273 {
1274 if (cmd == NULL)
1275 {
48649e1b 1276 if (control != NULL)
d1b0a7bf 1277 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1278 else
1279 {
81b1e71c
TT
1280 std::string str
1281 = string_printf (_("Type commands for breakpoint(s) "
1282 "%s, one per line."),
1283 arg);
48649e1b 1284
81b1e71c 1285 cmd = read_command_lines (&str[0],
d1b0a7bf
TT
1286 from_tty, 1,
1287 (is_tracepoint (b)
1288 ? check_tracepoint_command : 0),
1289 b);
48649e1b 1290 }
48649e1b
TT
1291 }
1292
1293 /* If a breakpoint was on the list more than once, we don't need to
1294 do anything. */
1295 if (b->commands != cmd)
1296 {
d1b0a7bf 1297 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1298 b->commands = cmd;
1299 observer_notify_breakpoint_modified (b);
1300 }
1301 });
95a42b64 1302
48649e1b 1303 if (cmd == NULL)
95a42b64 1304 error (_("No breakpoints specified."));
95a42b64
TT
1305}
1306
1307static void
1308commands_command (char *arg, int from_tty)
1309{
1310 commands_command_1 (arg, from_tty, NULL);
c906108c 1311}
40c03ae8
EZ
1312
1313/* Like commands_command, but instead of reading the commands from
1314 input stream, takes them from an already parsed command structure.
1315
1316 This is used by cli-script.c to DTRT with breakpoint commands
1317 that are part of if and while bodies. */
1318enum command_control_type
896b6bda 1319commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1320{
95a42b64
TT
1321 commands_command_1 (arg, 0, cmd);
1322 return simple_control;
40c03ae8 1323}
876fa593
JK
1324
1325/* Return non-zero if BL->TARGET_INFO contains valid information. */
1326
1327static int
1328bp_location_has_shadow (struct bp_location *bl)
1329{
1330 if (bl->loc_type != bp_loc_software_breakpoint)
1331 return 0;
1332 if (!bl->inserted)
1333 return 0;
1334 if (bl->target_info.shadow_len == 0)
e5dd4106 1335 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1336 return 0;
1337 return 1;
1338}
1339
9d497a19
PA
1340/* Update BUF, which is LEN bytes read from the target address
1341 MEMADDR, by replacing a memory breakpoint with its shadowed
1342 contents.
1343
1344 If READBUF is not NULL, this buffer must not overlap with the of
1345 the breakpoint location's shadow_contents buffer. Otherwise, a
1346 failed assertion internal error will be raised. */
1347
1348static void
1349one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1350 const gdb_byte *writebuf_org,
1351 ULONGEST memaddr, LONGEST len,
1352 struct bp_target_info *target_info,
1353 struct gdbarch *gdbarch)
1354{
1355 /* Now do full processing of the found relevant range of elements. */
1356 CORE_ADDR bp_addr = 0;
1357 int bp_size = 0;
1358 int bptoffset = 0;
1359
1360 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1361 current_program_space->aspace, 0))
1362 {
1363 /* The breakpoint is inserted in a different address space. */
1364 return;
1365 }
1366
1367 /* Addresses and length of the part of the breakpoint that
1368 we need to copy. */
1369 bp_addr = target_info->placed_address;
1370 bp_size = target_info->shadow_len;
1371
1372 if (bp_addr + bp_size <= memaddr)
1373 {
1374 /* The breakpoint is entirely before the chunk of memory we are
1375 reading. */
1376 return;
1377 }
1378
1379 if (bp_addr >= memaddr + len)
1380 {
1381 /* The breakpoint is entirely after the chunk of memory we are
1382 reading. */
1383 return;
1384 }
1385
1386 /* Offset within shadow_contents. */
1387 if (bp_addr < memaddr)
1388 {
1389 /* Only copy the second part of the breakpoint. */
1390 bp_size -= memaddr - bp_addr;
1391 bptoffset = memaddr - bp_addr;
1392 bp_addr = memaddr;
1393 }
1394
1395 if (bp_addr + bp_size > memaddr + len)
1396 {
1397 /* Only copy the first part of the breakpoint. */
1398 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1399 }
1400
1401 if (readbuf != NULL)
1402 {
1403 /* Verify that the readbuf buffer does not overlap with the
1404 shadow_contents buffer. */
1405 gdb_assert (target_info->shadow_contents >= readbuf + len
1406 || readbuf >= (target_info->shadow_contents
1407 + target_info->shadow_len));
1408
1409 /* Update the read buffer with this inserted breakpoint's
1410 shadow. */
1411 memcpy (readbuf + bp_addr - memaddr,
1412 target_info->shadow_contents + bptoffset, bp_size);
1413 }
1414 else
1415 {
1416 const unsigned char *bp;
0d5ed153
MR
1417 CORE_ADDR addr = target_info->reqstd_address;
1418 int placed_size;
9d497a19
PA
1419
1420 /* Update the shadow with what we want to write to memory. */
1421 memcpy (target_info->shadow_contents + bptoffset,
1422 writebuf_org + bp_addr - memaddr, bp_size);
1423
1424 /* Determine appropriate breakpoint contents and size for this
1425 address. */
0d5ed153 1426 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1427
1428 /* Update the final write buffer with this inserted
1429 breakpoint's INSN. */
1430 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1431 }
1432}
1433
8defab1a 1434/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1435 by replacing any memory breakpoints with their shadowed contents.
1436
35c63cd8
JB
1437 If READBUF is not NULL, this buffer must not overlap with any of
1438 the breakpoint location's shadow_contents buffers. Otherwise,
1439 a failed assertion internal error will be raised.
1440
876fa593 1441 The range of shadowed area by each bp_location is:
f5336ca5
PA
1442 bl->address - bp_locations_placed_address_before_address_max
1443 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1444 The range we were requested to resolve shadows for is:
1445 memaddr ... memaddr + len
1446 Thus the safe cutoff boundaries for performance optimization are
35df4500 1447 memaddr + len <= (bl->address
f5336ca5 1448 - bp_locations_placed_address_before_address_max)
876fa593 1449 and:
f5336ca5 1450 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1451
8defab1a 1452void
f0ba3972
PA
1453breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1454 const gdb_byte *writebuf_org,
1455 ULONGEST memaddr, LONGEST len)
c906108c 1456{
4a64f543
MS
1457 /* Left boundary, right boundary and median element of our binary
1458 search. */
876fa593
JK
1459 unsigned bc_l, bc_r, bc;
1460
4a64f543
MS
1461 /* Find BC_L which is a leftmost element which may affect BUF
1462 content. It is safe to report lower value but a failure to
1463 report higher one. */
876fa593
JK
1464
1465 bc_l = 0;
f5336ca5 1466 bc_r = bp_locations_count;
876fa593
JK
1467 while (bc_l + 1 < bc_r)
1468 {
35df4500 1469 struct bp_location *bl;
876fa593
JK
1470
1471 bc = (bc_l + bc_r) / 2;
f5336ca5 1472 bl = bp_locations[bc];
876fa593 1473
4a64f543
MS
1474 /* Check first BL->ADDRESS will not overflow due to the added
1475 constant. Then advance the left boundary only if we are sure
1476 the BC element can in no way affect the BUF content (MEMADDR
1477 to MEMADDR + LEN range).
876fa593 1478
f5336ca5 1479 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1480 offset so that we cannot miss a breakpoint with its shadow
1481 range tail still reaching MEMADDR. */
c5aa993b 1482
f5336ca5 1483 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1484 >= bl->address)
f5336ca5 1485 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1486 <= memaddr))
876fa593
JK
1487 bc_l = bc;
1488 else
1489 bc_r = bc;
1490 }
1491
128070bb
PA
1492 /* Due to the binary search above, we need to make sure we pick the
1493 first location that's at BC_L's address. E.g., if there are
1494 multiple locations at the same address, BC_L may end up pointing
1495 at a duplicate location, and miss the "master"/"inserted"
1496 location. Say, given locations L1, L2 and L3 at addresses A and
1497 B:
1498
1499 L1@A, L2@A, L3@B, ...
1500
1501 BC_L could end up pointing at location L2, while the "master"
1502 location could be L1. Since the `loc->inserted' flag is only set
1503 on "master" locations, we'd forget to restore the shadow of L1
1504 and L2. */
1505 while (bc_l > 0
f5336ca5 1506 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1507 bc_l--;
1508
876fa593
JK
1509 /* Now do full processing of the found relevant range of elements. */
1510
f5336ca5 1511 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1512 {
f5336ca5 1513 struct bp_location *bl = bp_locations[bc];
876fa593 1514
35df4500
TJB
1515 /* bp_location array has BL->OWNER always non-NULL. */
1516 if (bl->owner->type == bp_none)
8a3fe4f8 1517 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1518 bl->owner->number);
ffce0d52 1519
e5dd4106 1520 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1521 content. */
1522
f5336ca5
PA
1523 if (bl->address >= bp_locations_placed_address_before_address_max
1524 && memaddr + len <= (bl->address
1525 - bp_locations_placed_address_before_address_max))
876fa593
JK
1526 break;
1527
35df4500 1528 if (!bp_location_has_shadow (bl))
c5aa993b 1529 continue;
6c95b8df 1530
9d497a19
PA
1531 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1532 memaddr, len, &bl->target_info, bl->gdbarch);
1533 }
c906108c 1534}
9d497a19 1535
c906108c 1536\f
c5aa993b 1537
b775012e
LM
1538/* Return true if BPT is either a software breakpoint or a hardware
1539 breakpoint. */
1540
1541int
1542is_breakpoint (const struct breakpoint *bpt)
1543{
1544 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1545 || bpt->type == bp_hardware_breakpoint
1546 || bpt->type == bp_dprintf);
b775012e
LM
1547}
1548
60e1c644
PA
1549/* Return true if BPT is of any hardware watchpoint kind. */
1550
a5606eee 1551static int
d77f58be 1552is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1553{
1554 return (bpt->type == bp_hardware_watchpoint
1555 || bpt->type == bp_read_watchpoint
1556 || bpt->type == bp_access_watchpoint);
1557}
7270d8f2 1558
60e1c644
PA
1559/* Return true if BPT is of any watchpoint kind, hardware or
1560 software. */
1561
3a5c3e22 1562int
d77f58be 1563is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1564{
1565 return (is_hardware_watchpoint (bpt)
1566 || bpt->type == bp_watchpoint);
1567}
1568
3a5c3e22
PA
1569/* Returns true if the current thread and its running state are safe
1570 to evaluate or update watchpoint B. Watchpoints on local
1571 expressions need to be evaluated in the context of the thread that
1572 was current when the watchpoint was created, and, that thread needs
1573 to be stopped to be able to select the correct frame context.
1574 Watchpoints on global expressions can be evaluated on any thread,
1575 and in any state. It is presently left to the target allowing
1576 memory accesses when threads are running. */
f6bc2008
PA
1577
1578static int
3a5c3e22 1579watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1580{
c1fc2657 1581 return (b->pspace == current_program_space
d0d8b0c6
JK
1582 && (ptid_equal (b->watchpoint_thread, null_ptid)
1583 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1584 && !is_executing (inferior_ptid))));
f6bc2008
PA
1585}
1586
d0fb5eae
JK
1587/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1588 associated bp_watchpoint_scope breakpoint. */
1589
1590static void
3a5c3e22 1591watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1592{
c1fc2657 1593 if (w->related_breakpoint != w)
d0fb5eae 1594 {
c1fc2657
SM
1595 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1596 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1597 w->related_breakpoint->disposition = disp_del_at_next_stop;
1598 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1599 w->related_breakpoint = w;
d0fb5eae 1600 }
c1fc2657 1601 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1602}
1603
bb9d5f81
PP
1604/* Extract a bitfield value from value VAL using the bit parameters contained in
1605 watchpoint W. */
1606
1607static struct value *
1608extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1609{
1610 struct value *bit_val;
1611
1612 if (val == NULL)
1613 return NULL;
1614
1615 bit_val = allocate_value (value_type (val));
1616
1617 unpack_value_bitfield (bit_val,
1618 w->val_bitpos,
1619 w->val_bitsize,
1620 value_contents_for_printing (val),
1621 value_offset (val),
1622 val);
1623
1624 return bit_val;
1625}
1626
c6d81124
PA
1627/* Allocate a dummy location and add it to B, which must be a software
1628 watchpoint. This is required because even if a software watchpoint
1629 is not watching any memory, bpstat_stop_status requires a location
1630 to be able to report stops. */
1631
1632static void
1633software_watchpoint_add_no_memory_location (struct breakpoint *b,
1634 struct program_space *pspace)
1635{
1636 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1637
1638 b->loc = allocate_bp_location (b);
1639 b->loc->pspace = pspace;
1640 b->loc->address = -1;
1641 b->loc->length = -1;
1642}
1643
1644/* Returns true if B is a software watchpoint that is not watching any
1645 memory (e.g., "watch $pc"). */
1646
1647static int
1648is_no_memory_software_watchpoint (struct breakpoint *b)
1649{
1650 return (b->type == bp_watchpoint
1651 && b->loc != NULL
1652 && b->loc->next == NULL
1653 && b->loc->address == -1
1654 && b->loc->length == -1);
1655}
1656
567e1b4e
JB
1657/* Assuming that B is a watchpoint:
1658 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1659 - Evaluate expression and store the result in B->val
567e1b4e
JB
1660 - Evaluate the condition if there is one, and store the result
1661 in b->loc->cond.
a5606eee
VP
1662 - Update the list of values that must be watched in B->loc.
1663
4a64f543
MS
1664 If the watchpoint disposition is disp_del_at_next_stop, then do
1665 nothing. If this is local watchpoint that is out of scope, delete
1666 it.
1667
1668 Even with `set breakpoint always-inserted on' the watchpoints are
1669 removed + inserted on each stop here. Normal breakpoints must
1670 never be removed because they might be missed by a running thread
1671 when debugging in non-stop mode. On the other hand, hardware
1672 watchpoints (is_hardware_watchpoint; processed here) are specific
1673 to each LWP since they are stored in each LWP's hardware debug
1674 registers. Therefore, such LWP must be stopped first in order to
1675 be able to modify its hardware watchpoints.
1676
1677 Hardware watchpoints must be reset exactly once after being
1678 presented to the user. It cannot be done sooner, because it would
1679 reset the data used to present the watchpoint hit to the user. And
1680 it must not be done later because it could display the same single
1681 watchpoint hit during multiple GDB stops. Note that the latter is
1682 relevant only to the hardware watchpoint types bp_read_watchpoint
1683 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1684 not user-visible - its hit is suppressed if the memory content has
1685 not changed.
1686
1687 The following constraints influence the location where we can reset
1688 hardware watchpoints:
1689
1690 * target_stopped_by_watchpoint and target_stopped_data_address are
1691 called several times when GDB stops.
1692
1693 [linux]
1694 * Multiple hardware watchpoints can be hit at the same time,
1695 causing GDB to stop. GDB only presents one hardware watchpoint
1696 hit at a time as the reason for stopping, and all the other hits
1697 are presented later, one after the other, each time the user
1698 requests the execution to be resumed. Execution is not resumed
1699 for the threads still having pending hit event stored in
1700 LWP_INFO->STATUS. While the watchpoint is already removed from
1701 the inferior on the first stop the thread hit event is kept being
1702 reported from its cached value by linux_nat_stopped_data_address
1703 until the real thread resume happens after the watchpoint gets
1704 presented and thus its LWP_INFO->STATUS gets reset.
1705
1706 Therefore the hardware watchpoint hit can get safely reset on the
1707 watchpoint removal from inferior. */
a79d3c27 1708
b40ce68a 1709static void
3a5c3e22 1710update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1711{
a5606eee 1712 int within_current_scope;
a5606eee 1713 struct frame_id saved_frame_id;
66076460 1714 int frame_saved;
a5606eee 1715
f6bc2008
PA
1716 /* If this is a local watchpoint, we only want to check if the
1717 watchpoint frame is in scope if the current thread is the thread
1718 that was used to create the watchpoint. */
1719 if (!watchpoint_in_thread_scope (b))
1720 return;
1721
c1fc2657 1722 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1723 return;
1724
66076460 1725 frame_saved = 0;
a5606eee
VP
1726
1727 /* Determine if the watchpoint is within scope. */
1728 if (b->exp_valid_block == NULL)
1729 within_current_scope = 1;
1730 else
1731 {
b5db5dfc
UW
1732 struct frame_info *fi = get_current_frame ();
1733 struct gdbarch *frame_arch = get_frame_arch (fi);
1734 CORE_ADDR frame_pc = get_frame_pc (fi);
1735
c9cf6e20
MG
1736 /* If we're at a point where the stack has been destroyed
1737 (e.g. in a function epilogue), unwinding may not work
1738 properly. Do not attempt to recreate locations at this
b5db5dfc 1739 point. See similar comments in watchpoint_check. */
c9cf6e20 1740 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1741 return;
66076460
DJ
1742
1743 /* Save the current frame's ID so we can restore it after
1744 evaluating the watchpoint expression on its own frame. */
1745 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1746 took a frame parameter, so that we didn't have to change the
1747 selected frame. */
1748 frame_saved = 1;
1749 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1750
a5606eee
VP
1751 fi = frame_find_by_id (b->watchpoint_frame);
1752 within_current_scope = (fi != NULL);
1753 if (within_current_scope)
1754 select_frame (fi);
1755 }
1756
b5db5dfc
UW
1757 /* We don't free locations. They are stored in the bp_location array
1758 and update_global_location_list will eventually delete them and
1759 remove breakpoints if needed. */
c1fc2657 1760 b->loc = NULL;
b5db5dfc 1761
a5606eee
VP
1762 if (within_current_scope && reparse)
1763 {
bbc13ae3 1764 const char *s;
d63d0675 1765
4d01a485 1766 b->exp.reset ();
d63d0675 1767 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1768 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1769 /* If the meaning of expression itself changed, the old value is
1770 no longer relevant. We don't want to report a watchpoint hit
1771 to the user when the old value and the new value may actually
1772 be completely different objects. */
1773 value_free (b->val);
fa4727a6
DJ
1774 b->val = NULL;
1775 b->val_valid = 0;
60e1c644
PA
1776
1777 /* Note that unlike with breakpoints, the watchpoint's condition
1778 expression is stored in the breakpoint object, not in the
1779 locations (re)created below. */
c1fc2657 1780 if (b->cond_string != NULL)
60e1c644 1781 {
4d01a485 1782 b->cond_exp.reset ();
60e1c644 1783
c1fc2657 1784 s = b->cond_string;
1bb9788d 1785 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1786 }
a5606eee 1787 }
a5606eee
VP
1788
1789 /* If we failed to parse the expression, for example because
1790 it refers to a global variable in a not-yet-loaded shared library,
1791 don't try to insert watchpoint. We don't automatically delete
1792 such watchpoint, though, since failure to parse expression
1793 is different from out-of-scope watchpoint. */
e8369a73 1794 if (!target_has_execution)
2d134ed3
PA
1795 {
1796 /* Without execution, memory can't change. No use to try and
1797 set watchpoint locations. The watchpoint will be reset when
1798 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1799 if (!can_use_hw_watchpoints)
1800 {
c1fc2657
SM
1801 if (b->ops->works_in_software_mode (b))
1802 b->type = bp_watchpoint;
e8369a73 1803 else
638aa5a1
AB
1804 error (_("Can't set read/access watchpoint when "
1805 "hardware watchpoints are disabled."));
e8369a73 1806 }
2d134ed3
PA
1807 }
1808 else if (within_current_scope && b->exp)
a5606eee 1809 {
0cf6dd15 1810 int pc = 0;
fa4727a6 1811 struct value *val_chain, *v, *result, *next;
2d134ed3 1812 struct program_space *frame_pspace;
a5606eee 1813
4d01a485 1814 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1815
a5606eee
VP
1816 /* Avoid setting b->val if it's already set. The meaning of
1817 b->val is 'the last value' user saw, and we should update
1818 it only if we reported that last value to user. As it
9c06b0b4
TJB
1819 happens, the code that reports it updates b->val directly.
1820 We don't keep track of the memory value for masked
1821 watchpoints. */
c1fc2657 1822 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1823 {
bb9d5f81
PP
1824 if (b->val_bitsize != 0)
1825 {
1826 v = extract_bitfield_from_watchpoint_value (b, v);
1827 if (v != NULL)
1828 release_value (v);
1829 }
fa4727a6
DJ
1830 b->val = v;
1831 b->val_valid = 1;
1832 }
a5606eee 1833
2d134ed3
PA
1834 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1835
a5606eee 1836 /* Look at each value on the value chain. */
9fa40276 1837 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1838 {
1839 /* If it's a memory location, and GDB actually needed
1840 its contents to evaluate the expression, then we
fa4727a6
DJ
1841 must watch it. If the first value returned is
1842 still lazy, that means an error occurred reading it;
1843 watch it anyway in case it becomes readable. */
a5606eee 1844 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1845 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1846 {
1847 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1848
a5606eee
VP
1849 /* We only watch structs and arrays if user asked
1850 for it explicitly, never if they just happen to
1851 appear in the middle of some value chain. */
fa4727a6 1852 if (v == result
a5606eee
VP
1853 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1854 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1855 {
1856 CORE_ADDR addr;
f486487f 1857 enum target_hw_bp_type type;
a5606eee 1858 struct bp_location *loc, **tmp;
bb9d5f81
PP
1859 int bitpos = 0, bitsize = 0;
1860
1861 if (value_bitsize (v) != 0)
1862 {
1863 /* Extract the bit parameters out from the bitfield
1864 sub-expression. */
1865 bitpos = value_bitpos (v);
1866 bitsize = value_bitsize (v);
1867 }
1868 else if (v == result && b->val_bitsize != 0)
1869 {
1870 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1871 lvalue whose bit parameters are saved in the fields
1872 VAL_BITPOS and VAL_BITSIZE. */
1873 bitpos = b->val_bitpos;
1874 bitsize = b->val_bitsize;
1875 }
a5606eee 1876
42ae5230 1877 addr = value_address (v);
bb9d5f81
PP
1878 if (bitsize != 0)
1879 {
1880 /* Skip the bytes that don't contain the bitfield. */
1881 addr += bitpos / 8;
1882 }
1883
a5606eee 1884 type = hw_write;
c1fc2657 1885 if (b->type == bp_read_watchpoint)
a5606eee 1886 type = hw_read;
c1fc2657 1887 else if (b->type == bp_access_watchpoint)
a5606eee 1888 type = hw_access;
3a5c3e22 1889
c1fc2657
SM
1890 loc = allocate_bp_location (b);
1891 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1892 ;
1893 *tmp = loc;
a6d9a66e 1894 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1895
1896 loc->pspace = frame_pspace;
a5606eee 1897 loc->address = addr;
bb9d5f81
PP
1898
1899 if (bitsize != 0)
1900 {
1901 /* Just cover the bytes that make up the bitfield. */
1902 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1903 }
1904 else
1905 loc->length = TYPE_LENGTH (value_type (v));
1906
a5606eee
VP
1907 loc->watchpoint_type = type;
1908 }
1909 }
9fa40276
TJB
1910 }
1911
1912 /* Change the type of breakpoint between hardware assisted or
1913 an ordinary watchpoint depending on the hardware support
1914 and free hardware slots. REPARSE is set when the inferior
1915 is started. */
a9634178 1916 if (reparse)
9fa40276 1917 {
e09342b5 1918 int reg_cnt;
9fa40276
TJB
1919 enum bp_loc_type loc_type;
1920 struct bp_location *bl;
a5606eee 1921
a9634178 1922 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1923
1924 if (reg_cnt)
9fa40276
TJB
1925 {
1926 int i, target_resources_ok, other_type_used;
a1398e0c 1927 enum bptype type;
9fa40276 1928
a9634178
TJB
1929 /* Use an exact watchpoint when there's only one memory region to be
1930 watched, and only one debug register is needed to watch it. */
1931 b->exact = target_exact_watchpoints && reg_cnt == 1;
1932
9fa40276 1933 /* We need to determine how many resources are already
e09342b5
TJB
1934 used for all other hardware watchpoints plus this one
1935 to see if we still have enough resources to also fit
a1398e0c
PA
1936 this watchpoint in as well. */
1937
1938 /* If this is a software watchpoint, we try to turn it
1939 to a hardware one -- count resources as if B was of
1940 hardware watchpoint type. */
c1fc2657 1941 type = b->type;
a1398e0c
PA
1942 if (type == bp_watchpoint)
1943 type = bp_hardware_watchpoint;
1944
1945 /* This watchpoint may or may not have been placed on
1946 the list yet at this point (it won't be in the list
1947 if we're trying to create it for the first time,
1948 through watch_command), so always account for it
1949 manually. */
1950
1951 /* Count resources used by all watchpoints except B. */
c1fc2657 1952 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1953
1954 /* Add in the resources needed for B. */
c1fc2657 1955 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1956
1957 target_resources_ok
1958 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1959 if (target_resources_ok <= 0)
a9634178 1960 {
c1fc2657 1961 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1962
1963 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1964 error (_("Target does not support this type of "
1965 "hardware watchpoint."));
9c06b0b4
TJB
1966 else if (target_resources_ok < 0 && !sw_mode)
1967 error (_("There are not enough available hardware "
1968 "resources for this watchpoint."));
a1398e0c
PA
1969
1970 /* Downgrade to software watchpoint. */
c1fc2657 1971 b->type = bp_watchpoint;
a1398e0c
PA
1972 }
1973 else
1974 {
1975 /* If this was a software watchpoint, we've just
1976 found we have enough resources to turn it to a
1977 hardware watchpoint. Otherwise, this is a
1978 nop. */
c1fc2657 1979 b->type = type;
a9634178 1980 }
9fa40276 1981 }
c1fc2657 1982 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1983 {
1984 if (!can_use_hw_watchpoints)
1985 error (_("Can't set read/access watchpoint when "
1986 "hardware watchpoints are disabled."));
1987 else
1988 error (_("Expression cannot be implemented with "
1989 "read/access watchpoint."));
1990 }
9fa40276 1991 else
c1fc2657 1992 b->type = bp_watchpoint;
9fa40276 1993
c1fc2657 1994 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1995 : bp_loc_hardware_watchpoint);
c1fc2657 1996 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1997 bl->loc_type = loc_type;
1998 }
1999
2000 for (v = val_chain; v; v = next)
2001 {
a5606eee
VP
2002 next = value_next (v);
2003 if (v != b->val)
2004 value_free (v);
2005 }
2006
c7437ca6
PA
2007 /* If a software watchpoint is not watching any memory, then the
2008 above left it without any location set up. But,
2009 bpstat_stop_status requires a location to be able to report
2010 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2011 if (b->type == bp_watchpoint && b->loc == NULL)
2012 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2013 }
2014 else if (!within_current_scope)
7270d8f2 2015 {
ac74f770
MS
2016 printf_filtered (_("\
2017Watchpoint %d deleted because the program has left the block\n\
2018in which its expression is valid.\n"),
c1fc2657 2019 b->number);
d0fb5eae 2020 watchpoint_del_at_next_stop (b);
7270d8f2 2021 }
a5606eee
VP
2022
2023 /* Restore the selected frame. */
66076460
DJ
2024 if (frame_saved)
2025 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2026}
2027
a5606eee 2028
74960c60 2029/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2030 inserted in the inferior. We don't differentiate the type of BL's owner
2031 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2032 breakpoint_ops is not defined, because in insert_bp_location,
2033 tracepoint's insert_location will not be called. */
74960c60 2034static int
35df4500 2035should_be_inserted (struct bp_location *bl)
74960c60 2036{
35df4500 2037 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2038 return 0;
2039
35df4500 2040 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2041 return 0;
2042
35df4500 2043 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2044 return 0;
2045
f8eba3c6
TT
2046 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2047 return 0;
2048
56710373
PA
2049 /* This is set for example, when we're attached to the parent of a
2050 vfork, and have detached from the child. The child is running
2051 free, and we expect it to do an exec or exit, at which point the
2052 OS makes the parent schedulable again (and the target reports
2053 that the vfork is done). Until the child is done with the shared
2054 memory region, do not insert breakpoints in the parent, otherwise
2055 the child could still trip on the parent's breakpoints. Since
2056 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2057 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2058 return 0;
2059
31e77af2 2060 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2061 location, except if the breakpoint is a single-step breakpoint,
2062 and the breakpoint's thread is the thread which is stepping past
2063 a breakpoint. */
31e77af2
PA
2064 if ((bl->loc_type == bp_loc_software_breakpoint
2065 || bl->loc_type == bp_loc_hardware_breakpoint)
2066 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2067 bl->address)
2068 /* The single-step breakpoint may be inserted at the location
2069 we're trying to step if the instruction branches to itself.
2070 However, the instruction won't be executed at all and it may
2071 break the semantics of the instruction, for example, the
2072 instruction is a conditional branch or updates some flags.
2073 We can't fix it unless GDB is able to emulate the instruction
2074 or switch to displaced stepping. */
2075 && !(bl->owner->type == bp_single_step
2076 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2077 {
2078 if (debug_infrun)
2079 {
2080 fprintf_unfiltered (gdb_stdlog,
2081 "infrun: skipping breakpoint: "
2082 "stepping past insn at: %s\n",
2083 paddress (bl->gdbarch, bl->address));
2084 }
2085 return 0;
2086 }
31e77af2 2087
963f9c80
PA
2088 /* Don't insert watchpoints if we're trying to step past the
2089 instruction that triggered one. */
2090 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2091 && stepping_past_nonsteppable_watchpoint ())
2092 {
2093 if (debug_infrun)
2094 {
2095 fprintf_unfiltered (gdb_stdlog,
2096 "infrun: stepping past non-steppable watchpoint. "
2097 "skipping watchpoint at %s:%d\n",
2098 paddress (bl->gdbarch, bl->address),
2099 bl->length);
2100 }
2101 return 0;
2102 }
2103
74960c60
VP
2104 return 1;
2105}
2106
934709f0
PW
2107/* Same as should_be_inserted but does the check assuming
2108 that the location is not duplicated. */
2109
2110static int
2111unduplicated_should_be_inserted (struct bp_location *bl)
2112{
2113 int result;
2114 const int save_duplicate = bl->duplicate;
2115
2116 bl->duplicate = 0;
2117 result = should_be_inserted (bl);
2118 bl->duplicate = save_duplicate;
2119 return result;
2120}
2121
b775012e
LM
2122/* Parses a conditional described by an expression COND into an
2123 agent expression bytecode suitable for evaluation
2124 by the bytecode interpreter. Return NULL if there was
2125 any error during parsing. */
2126
833177a4 2127static agent_expr_up
b775012e
LM
2128parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2129{
833177a4 2130 if (cond == NULL)
b775012e
LM
2131 return NULL;
2132
833177a4
PA
2133 agent_expr_up aexpr;
2134
b775012e
LM
2135 /* We don't want to stop processing, so catch any errors
2136 that may show up. */
492d29ea 2137 TRY
b775012e 2138 {
036e657b 2139 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2140 }
2141
492d29ea 2142 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2143 {
2144 /* If we got here, it means the condition could not be parsed to a valid
2145 bytecode expression and thus can't be evaluated on the target's side.
2146 It's no use iterating through the conditions. */
b775012e 2147 }
492d29ea 2148 END_CATCH
b775012e
LM
2149
2150 /* We have a valid agent expression. */
2151 return aexpr;
2152}
2153
2154/* Based on location BL, create a list of breakpoint conditions to be
2155 passed on to the target. If we have duplicated locations with different
2156 conditions, we will add such conditions to the list. The idea is that the
2157 target will evaluate the list of conditions and will only notify GDB when
2158 one of them is true. */
2159
2160static void
2161build_target_condition_list (struct bp_location *bl)
2162{
2163 struct bp_location **locp = NULL, **loc2p;
2164 int null_condition_or_parse_error = 0;
2165 int modified = bl->needs_update;
2166 struct bp_location *loc;
2167
8b4f3082 2168 /* Release conditions left over from a previous insert. */
3cde5c42 2169 bl->target_info.conditions.clear ();
8b4f3082 2170
b775012e
LM
2171 /* This is only meaningful if the target is
2172 evaluating conditions and if the user has
2173 opted for condition evaluation on the target's
2174 side. */
2175 if (gdb_evaluates_breakpoint_condition_p ()
2176 || !target_supports_evaluation_of_breakpoint_conditions ())
2177 return;
2178
2179 /* Do a first pass to check for locations with no assigned
2180 conditions or conditions that fail to parse to a valid agent expression
2181 bytecode. If any of these happen, then it's no use to send conditions
2182 to the target since this location will always trigger and generate a
2183 response back to GDB. */
2184 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2185 {
2186 loc = (*loc2p);
2187 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2188 {
2189 if (modified)
2190 {
b775012e
LM
2191 /* Re-parse the conditions since something changed. In that
2192 case we already freed the condition bytecodes (see
2193 force_breakpoint_reinsertion). We just
2194 need to parse the condition to bytecodes again. */
833177a4
PA
2195 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2196 loc->cond.get ());
b775012e
LM
2197 }
2198
2199 /* If we have a NULL bytecode expression, it means something
2200 went wrong or we have a null condition expression. */
2201 if (!loc->cond_bytecode)
2202 {
2203 null_condition_or_parse_error = 1;
2204 break;
2205 }
2206 }
2207 }
2208
2209 /* If any of these happened, it means we will have to evaluate the conditions
2210 for the location's address on gdb's side. It is no use keeping bytecodes
2211 for all the other duplicate locations, thus we free all of them here.
2212
2213 This is so we have a finer control over which locations' conditions are
2214 being evaluated by GDB or the remote stub. */
2215 if (null_condition_or_parse_error)
2216 {
2217 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2218 {
2219 loc = (*loc2p);
2220 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2221 {
2222 /* Only go as far as the first NULL bytecode is
2223 located. */
2224 if (!loc->cond_bytecode)
2225 return;
2226
833177a4 2227 loc->cond_bytecode.reset ();
b775012e
LM
2228 }
2229 }
2230 }
2231
2232 /* No NULL conditions or failed bytecode generation. Build a condition list
2233 for this location's address. */
2234 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2235 {
2236 loc = (*loc2p);
2237 if (loc->cond
2238 && is_breakpoint (loc->owner)
2239 && loc->pspace->num == bl->pspace->num
2240 && loc->owner->enable_state == bp_enabled
2241 && loc->enabled)
3cde5c42
PA
2242 {
2243 /* Add the condition to the vector. This will be used later
2244 to send the conditions to the target. */
2245 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2246 }
b775012e
LM
2247 }
2248
2249 return;
2250}
2251
d3ce09f5
SS
2252/* Parses a command described by string CMD into an agent expression
2253 bytecode suitable for evaluation by the bytecode interpreter.
2254 Return NULL if there was any error during parsing. */
2255
833177a4 2256static agent_expr_up
d3ce09f5
SS
2257parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2258{
2259 struct cleanup *old_cleanups = 0;
4d01a485 2260 struct expression **argvec;
bbc13ae3
KS
2261 const char *cmdrest;
2262 const char *format_start, *format_end;
d3ce09f5
SS
2263 struct format_piece *fpieces;
2264 int nargs;
2265 struct gdbarch *gdbarch = get_current_arch ();
2266
833177a4 2267 if (cmd == NULL)
d3ce09f5
SS
2268 return NULL;
2269
2270 cmdrest = cmd;
2271
2272 if (*cmdrest == ',')
2273 ++cmdrest;
f1735a53 2274 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2275
2276 if (*cmdrest++ != '"')
2277 error (_("No format string following the location"));
2278
2279 format_start = cmdrest;
2280
2281 fpieces = parse_format_string (&cmdrest);
2282
2283 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2284
2285 format_end = cmdrest;
2286
2287 if (*cmdrest++ != '"')
2288 error (_("Bad format string, non-terminated '\"'."));
2289
f1735a53 2290 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2291
2292 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2293 error (_("Invalid argument syntax"));
2294
2295 if (*cmdrest == ',')
2296 cmdrest++;
f1735a53 2297 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2298
2299 /* For each argument, make an expression. */
2300
2301 argvec = (struct expression **) alloca (strlen (cmd)
2302 * sizeof (struct expression *));
2303
2304 nargs = 0;
2305 while (*cmdrest != '\0')
2306 {
bbc13ae3 2307 const char *cmd1;
d3ce09f5
SS
2308
2309 cmd1 = cmdrest;
4d01a485
PA
2310 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2311 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2312 cmdrest = cmd1;
2313 if (*cmdrest == ',')
2314 ++cmdrest;
2315 }
2316
833177a4
PA
2317 agent_expr_up aexpr;
2318
d3ce09f5
SS
2319 /* We don't want to stop processing, so catch any errors
2320 that may show up. */
492d29ea 2321 TRY
d3ce09f5 2322 {
036e657b
JB
2323 aexpr = gen_printf (scope, gdbarch, 0, 0,
2324 format_start, format_end - format_start,
2325 fpieces, nargs, argvec);
d3ce09f5 2326 }
492d29ea 2327 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2328 {
2329 /* If we got here, it means the command could not be parsed to a valid
2330 bytecode expression and thus can't be evaluated on the target's side.
2331 It's no use iterating through the other commands. */
d3ce09f5 2332 }
492d29ea
PA
2333 END_CATCH
2334
2335 do_cleanups (old_cleanups);
d3ce09f5 2336
d3ce09f5
SS
2337 /* We have a valid agent expression, return it. */
2338 return aexpr;
2339}
2340
2341/* Based on location BL, create a list of breakpoint commands to be
2342 passed on to the target. If we have duplicated locations with
2343 different commands, we will add any such to the list. */
2344
2345static void
2346build_target_command_list (struct bp_location *bl)
2347{
2348 struct bp_location **locp = NULL, **loc2p;
2349 int null_command_or_parse_error = 0;
2350 int modified = bl->needs_update;
2351 struct bp_location *loc;
2352
3cde5c42
PA
2353 /* Clear commands left over from a previous insert. */
2354 bl->target_info.tcommands.clear ();
8b4f3082 2355
41fac0cf 2356 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2357 return;
2358
41fac0cf
PA
2359 /* For now, limit to agent-style dprintf breakpoints. */
2360 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2361 return;
2362
41fac0cf
PA
2363 /* For now, if we have any duplicate location that isn't a dprintf,
2364 don't install the target-side commands, as that would make the
2365 breakpoint not be reported to the core, and we'd lose
2366 control. */
2367 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2368 {
2369 loc = (*loc2p);
2370 if (is_breakpoint (loc->owner)
2371 && loc->pspace->num == bl->pspace->num
2372 && loc->owner->type != bp_dprintf)
2373 return;
2374 }
2375
d3ce09f5
SS
2376 /* Do a first pass to check for locations with no assigned
2377 conditions or conditions that fail to parse to a valid agent expression
2378 bytecode. If any of these happen, then it's no use to send conditions
2379 to the target since this location will always trigger and generate a
2380 response back to GDB. */
2381 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2382 {
2383 loc = (*loc2p);
2384 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2385 {
2386 if (modified)
2387 {
d3ce09f5
SS
2388 /* Re-parse the commands since something changed. In that
2389 case we already freed the command bytecodes (see
2390 force_breakpoint_reinsertion). We just
2391 need to parse the command to bytecodes again. */
833177a4
PA
2392 loc->cmd_bytecode
2393 = parse_cmd_to_aexpr (bl->address,
2394 loc->owner->extra_string);
d3ce09f5
SS
2395 }
2396
2397 /* If we have a NULL bytecode expression, it means something
2398 went wrong or we have a null command expression. */
2399 if (!loc->cmd_bytecode)
2400 {
2401 null_command_or_parse_error = 1;
2402 break;
2403 }
2404 }
2405 }
2406
2407 /* If anything failed, then we're not doing target-side commands,
2408 and so clean up. */
2409 if (null_command_or_parse_error)
2410 {
2411 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2412 {
2413 loc = (*loc2p);
2414 if (is_breakpoint (loc->owner)
2415 && loc->pspace->num == bl->pspace->num)
2416 {
2417 /* Only go as far as the first NULL bytecode is
2418 located. */
40fb6c5e 2419 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2420 return;
2421
833177a4 2422 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2423 }
2424 }
2425 }
2426
2427 /* No NULL commands or failed bytecode generation. Build a command list
2428 for this location's address. */
2429 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2430 {
2431 loc = (*loc2p);
2432 if (loc->owner->extra_string
2433 && is_breakpoint (loc->owner)
2434 && loc->pspace->num == bl->pspace->num
2435 && loc->owner->enable_state == bp_enabled
2436 && loc->enabled)
3cde5c42
PA
2437 {
2438 /* Add the command to the vector. This will be used later
2439 to send the commands to the target. */
2440 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2441 }
d3ce09f5
SS
2442 }
2443
2444 bl->target_info.persist = 0;
2445 /* Maybe flag this location as persistent. */
2446 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2447 bl->target_info.persist = 1;
2448}
2449
833b7ab5
YQ
2450/* Return the kind of breakpoint on address *ADDR. Get the kind
2451 of breakpoint according to ADDR except single-step breakpoint.
2452 Get the kind of single-step breakpoint according to the current
2453 registers state. */
cd6c3b4f
YQ
2454
2455static int
2456breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2457{
833b7ab5
YQ
2458 if (bl->owner->type == bp_single_step)
2459 {
2460 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2461 struct regcache *regcache;
2462
2463 regcache = get_thread_regcache (thr->ptid);
2464
2465 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2466 regcache, addr);
2467 }
2468 else
2469 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2470}
2471
35df4500
TJB
2472/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2473 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2474 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2475 Returns 0 for success, 1 if the bp_location type is not supported or
2476 -1 for failure.
879bfdc2 2477
4a64f543
MS
2478 NOTE drow/2003-09-09: This routine could be broken down to an
2479 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2480static int
35df4500 2481insert_bp_location (struct bp_location *bl,
26bb91f3 2482 struct ui_file *tmp_error_stream,
3fbb6ffa 2483 int *disabled_breaks,
dd61ec5c
MW
2484 int *hw_breakpoint_error,
2485 int *hw_bp_error_explained_already)
879bfdc2 2486{
0000e5cc
PA
2487 enum errors bp_err = GDB_NO_ERROR;
2488 const char *bp_err_message = NULL;
879bfdc2 2489
b775012e 2490 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2491 return 0;
2492
35c63cd8
JB
2493 /* Note we don't initialize bl->target_info, as that wipes out
2494 the breakpoint location's shadow_contents if the breakpoint
2495 is still inserted at that location. This in turn breaks
2496 target_read_memory which depends on these buffers when
2497 a memory read is requested at the breakpoint location:
2498 Once the target_info has been wiped, we fail to see that
2499 we have a breakpoint inserted at that address and thus
2500 read the breakpoint instead of returning the data saved in
2501 the breakpoint location's shadow contents. */
0d5ed153 2502 bl->target_info.reqstd_address = bl->address;
35df4500 2503 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2504 bl->target_info.length = bl->length;
8181d85f 2505
b775012e
LM
2506 /* When working with target-side conditions, we must pass all the conditions
2507 for the same breakpoint address down to the target since GDB will not
2508 insert those locations. With a list of breakpoint conditions, the target
2509 can decide when to stop and notify GDB. */
2510
2511 if (is_breakpoint (bl->owner))
2512 {
2513 build_target_condition_list (bl);
d3ce09f5
SS
2514 build_target_command_list (bl);
2515 /* Reset the modification marker. */
b775012e
LM
2516 bl->needs_update = 0;
2517 }
2518
35df4500
TJB
2519 if (bl->loc_type == bp_loc_software_breakpoint
2520 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2521 {
35df4500 2522 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2523 {
2524 /* If the explicitly specified breakpoint type
2525 is not hardware breakpoint, check the memory map to see
2526 if the breakpoint address is in read only memory or not.
4a64f543 2527
765dc015
VP
2528 Two important cases are:
2529 - location type is not hardware breakpoint, memory
2530 is readonly. We change the type of the location to
2531 hardware breakpoint.
4a64f543
MS
2532 - location type is hardware breakpoint, memory is
2533 read-write. This means we've previously made the
2534 location hardware one, but then the memory map changed,
2535 so we undo.
765dc015 2536
4a64f543
MS
2537 When breakpoints are removed, remove_breakpoints will use
2538 location types we've just set here, the only possible
2539 problem is that memory map has changed during running
2540 program, but it's not going to work anyway with current
2541 gdb. */
765dc015 2542 struct mem_region *mr
0d5ed153 2543 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2544
2545 if (mr)
2546 {
2547 if (automatic_hardware_breakpoints)
2548 {
765dc015
VP
2549 enum bp_loc_type new_type;
2550
2551 if (mr->attrib.mode != MEM_RW)
2552 new_type = bp_loc_hardware_breakpoint;
2553 else
2554 new_type = bp_loc_software_breakpoint;
2555
35df4500 2556 if (new_type != bl->loc_type)
765dc015
VP
2557 {
2558 static int said = 0;
cc59ec59 2559
35df4500 2560 bl->loc_type = new_type;
765dc015
VP
2561 if (!said)
2562 {
3e43a32a
MS
2563 fprintf_filtered (gdb_stdout,
2564 _("Note: automatically using "
2565 "hardware breakpoints for "
2566 "read-only addresses.\n"));
765dc015
VP
2567 said = 1;
2568 }
2569 }
2570 }
35df4500 2571 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2572 && mr->attrib.mode != MEM_RW)
2573 {
2574 fprintf_unfiltered (tmp_error_stream,
2575 _("Cannot insert breakpoint %d.\n"
2576 "Cannot set software breakpoint "
2577 "at read-only address %s\n"),
2578 bl->owner->number,
2579 paddress (bl->gdbarch, bl->address));
2580 return 1;
2581 }
765dc015
VP
2582 }
2583 }
2584
879bfdc2
DJ
2585 /* First check to see if we have to handle an overlay. */
2586 if (overlay_debugging == ovly_off
35df4500
TJB
2587 || bl->section == NULL
2588 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2589 {
2590 /* No overlay handling: just set the breakpoint. */
492d29ea 2591 TRY
dd61ec5c 2592 {
0000e5cc
PA
2593 int val;
2594
dd61ec5c 2595 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2596 if (val)
2597 bp_err = GENERIC_ERROR;
dd61ec5c 2598 }
492d29ea 2599 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2600 {
0000e5cc
PA
2601 bp_err = e.error;
2602 bp_err_message = e.message;
dd61ec5c 2603 }
492d29ea 2604 END_CATCH
879bfdc2
DJ
2605 }
2606 else
2607 {
4a64f543 2608 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2609 Shall we set a breakpoint at the LMA? */
2610 if (!overlay_events_enabled)
2611 {
2612 /* Yes -- overlay event support is not active,
2613 so we must try to set a breakpoint at the LMA.
2614 This will not work for a hardware breakpoint. */
35df4500 2615 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2616 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2617 bl->owner->number);
879bfdc2
DJ
2618 else
2619 {
35df4500
TJB
2620 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2621 bl->section);
879bfdc2 2622 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2623 bl->overlay_target_info = bl->target_info;
0d5ed153 2624 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2625
2626 /* No overlay handling: just set the breakpoint. */
492d29ea 2627 TRY
0000e5cc
PA
2628 {
2629 int val;
2630
579c6ad9 2631 bl->overlay_target_info.kind
cd6c3b4f
YQ
2632 = breakpoint_kind (bl, &addr);
2633 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2634 val = target_insert_breakpoint (bl->gdbarch,
2635 &bl->overlay_target_info);
2636 if (val)
2637 bp_err = GENERIC_ERROR;
2638 }
492d29ea 2639 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2640 {
2641 bp_err = e.error;
2642 bp_err_message = e.message;
2643 }
492d29ea 2644 END_CATCH
0000e5cc
PA
2645
2646 if (bp_err != GDB_NO_ERROR)
99361f52 2647 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2648 "Overlay breakpoint %d "
2649 "failed: in ROM?\n",
35df4500 2650 bl->owner->number);
879bfdc2
DJ
2651 }
2652 }
2653 /* Shall we set a breakpoint at the VMA? */
35df4500 2654 if (section_is_mapped (bl->section))
879bfdc2
DJ
2655 {
2656 /* Yes. This overlay section is mapped into memory. */
492d29ea 2657 TRY
dd61ec5c 2658 {
0000e5cc
PA
2659 int val;
2660
dd61ec5c 2661 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2662 if (val)
2663 bp_err = GENERIC_ERROR;
dd61ec5c 2664 }
492d29ea 2665 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2666 {
0000e5cc
PA
2667 bp_err = e.error;
2668 bp_err_message = e.message;
dd61ec5c 2669 }
492d29ea 2670 END_CATCH
879bfdc2
DJ
2671 }
2672 else
2673 {
2674 /* No. This breakpoint will not be inserted.
2675 No error, but do not mark the bp as 'inserted'. */
2676 return 0;
2677 }
2678 }
2679
0000e5cc 2680 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2681 {
2682 /* Can't set the breakpoint. */
0000e5cc
PA
2683
2684 /* In some cases, we might not be able to insert a
2685 breakpoint in a shared library that has already been
2686 removed, but we have not yet processed the shlib unload
2687 event. Unfortunately, some targets that implement
076855f9
PA
2688 breakpoint insertion themselves can't tell why the
2689 breakpoint insertion failed (e.g., the remote target
2690 doesn't define error codes), so we must treat generic
2691 errors as memory errors. */
0000e5cc 2692 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2693 && bl->loc_type == bp_loc_software_breakpoint
08351840 2694 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2695 || shared_objfile_contains_address_p (bl->pspace,
2696 bl->address)))
879bfdc2 2697 {
4a64f543 2698 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2699 bl->shlib_disabled = 1;
8d3788bd 2700 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2701 if (!*disabled_breaks)
2702 {
2703 fprintf_unfiltered (tmp_error_stream,
2704 "Cannot insert breakpoint %d.\n",
2705 bl->owner->number);
2706 fprintf_unfiltered (tmp_error_stream,
2707 "Temporarily disabling shared "
2708 "library breakpoints:\n");
2709 }
2710 *disabled_breaks = 1;
879bfdc2 2711 fprintf_unfiltered (tmp_error_stream,
35df4500 2712 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2713 return 0;
879bfdc2
DJ
2714 }
2715 else
879bfdc2 2716 {
35df4500 2717 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2718 {
0000e5cc
PA
2719 *hw_breakpoint_error = 1;
2720 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2721 fprintf_unfiltered (tmp_error_stream,
2722 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2723 bl->owner->number, bp_err_message ? ":" : ".\n");
2724 if (bp_err_message != NULL)
2725 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2726 }
2727 else
2728 {
0000e5cc
PA
2729 if (bp_err_message == NULL)
2730 {
1ccbe998 2731 std::string message
0000e5cc
PA
2732 = memory_error_message (TARGET_XFER_E_IO,
2733 bl->gdbarch, bl->address);
0000e5cc
PA
2734
2735 fprintf_unfiltered (tmp_error_stream,
2736 "Cannot insert breakpoint %d.\n"
2737 "%s\n",
1ccbe998 2738 bl->owner->number, message.c_str ());
0000e5cc
PA
2739 }
2740 else
2741 {
2742 fprintf_unfiltered (tmp_error_stream,
2743 "Cannot insert breakpoint %d: %s\n",
2744 bl->owner->number,
2745 bp_err_message);
2746 }
879bfdc2 2747 }
0000e5cc 2748 return 1;
879bfdc2
DJ
2749
2750 }
2751 }
2752 else
35df4500 2753 bl->inserted = 1;
879bfdc2 2754
0000e5cc 2755 return 0;
879bfdc2
DJ
2756 }
2757
35df4500 2758 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2759 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2760 watchpoints. It's not clear that it's necessary... */
35df4500 2761 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2762 {
0000e5cc
PA
2763 int val;
2764
77b06cd7
TJB
2765 gdb_assert (bl->owner->ops != NULL
2766 && bl->owner->ops->insert_location != NULL);
2767
2768 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2769
2770 /* If trying to set a read-watchpoint, and it turns out it's not
2771 supported, try emulating one with an access watchpoint. */
35df4500 2772 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2773 {
2774 struct bp_location *loc, **loc_temp;
2775
2776 /* But don't try to insert it, if there's already another
2777 hw_access location that would be considered a duplicate
2778 of this one. */
2779 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2780 if (loc != bl
85d721b8 2781 && loc->watchpoint_type == hw_access
35df4500 2782 && watchpoint_locations_match (bl, loc))
85d721b8 2783 {
35df4500
TJB
2784 bl->duplicate = 1;
2785 bl->inserted = 1;
2786 bl->target_info = loc->target_info;
2787 bl->watchpoint_type = hw_access;
85d721b8
PA
2788 val = 0;
2789 break;
2790 }
2791
2792 if (val == 1)
2793 {
77b06cd7
TJB
2794 bl->watchpoint_type = hw_access;
2795 val = bl->owner->ops->insert_location (bl);
2796
2797 if (val)
2798 /* Back to the original value. */
2799 bl->watchpoint_type = hw_read;
85d721b8
PA
2800 }
2801 }
2802
35df4500 2803 bl->inserted = (val == 0);
879bfdc2
DJ
2804 }
2805
35df4500 2806 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2807 {
0000e5cc
PA
2808 int val;
2809
77b06cd7
TJB
2810 gdb_assert (bl->owner->ops != NULL
2811 && bl->owner->ops->insert_location != NULL);
2812
2813 val = bl->owner->ops->insert_location (bl);
2814 if (val)
2815 {
2816 bl->owner->enable_state = bp_disabled;
2817
2818 if (val == 1)
2819 warning (_("\
2820Error inserting catchpoint %d: Your system does not support this type\n\
2821of catchpoint."), bl->owner->number);
2822 else
2823 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2824 }
2825
2826 bl->inserted = (val == 0);
1640b821
DJ
2827
2828 /* We've already printed an error message if there was a problem
2829 inserting this catchpoint, and we've disabled the catchpoint,
2830 so just return success. */
2831 return 0;
879bfdc2
DJ
2832 }
2833
2834 return 0;
2835}
2836
6c95b8df
PA
2837/* This function is called when program space PSPACE is about to be
2838 deleted. It takes care of updating breakpoints to not reference
2839 PSPACE anymore. */
2840
2841void
2842breakpoint_program_space_exit (struct program_space *pspace)
2843{
2844 struct breakpoint *b, *b_temp;
876fa593 2845 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2846
2847 /* Remove any breakpoint that was set through this program space. */
2848 ALL_BREAKPOINTS_SAFE (b, b_temp)
2849 {
2850 if (b->pspace == pspace)
2851 delete_breakpoint (b);
2852 }
2853
2854 /* Breakpoints set through other program spaces could have locations
2855 bound to PSPACE as well. Remove those. */
876fa593 2856 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2857 {
2858 struct bp_location *tmp;
2859
2860 if (loc->pspace == pspace)
2861 {
2bdf28a0 2862 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2863 if (loc->owner->loc == loc)
2864 loc->owner->loc = loc->next;
2865 else
2866 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2867 if (tmp->next == loc)
2868 {
2869 tmp->next = loc->next;
2870 break;
2871 }
2872 }
2873 }
2874
2875 /* Now update the global location list to permanently delete the
2876 removed locations above. */
44702360 2877 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2878}
2879
74960c60
VP
2880/* Make sure all breakpoints are inserted in inferior.
2881 Throws exception on any error.
2882 A breakpoint that is already inserted won't be inserted
2883 again, so calling this function twice is safe. */
2884void
2885insert_breakpoints (void)
2886{
2887 struct breakpoint *bpt;
2888
2889 ALL_BREAKPOINTS (bpt)
2890 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2891 {
2892 struct watchpoint *w = (struct watchpoint *) bpt;
2893
2894 update_watchpoint (w, 0 /* don't reparse. */);
2895 }
74960c60 2896
04086b45
PA
2897 /* Updating watchpoints creates new locations, so update the global
2898 location list. Explicitly tell ugll to insert locations and
2899 ignore breakpoints_always_inserted_mode. */
2900 update_global_location_list (UGLL_INSERT);
74960c60
VP
2901}
2902
20388dd6
YQ
2903/* Invoke CALLBACK for each of bp_location. */
2904
2905void
2906iterate_over_bp_locations (walk_bp_location_callback callback)
2907{
2908 struct bp_location *loc, **loc_tmp;
2909
2910 ALL_BP_LOCATIONS (loc, loc_tmp)
2911 {
2912 callback (loc, NULL);
2913 }
2914}
2915
b775012e
LM
2916/* This is used when we need to synch breakpoint conditions between GDB and the
2917 target. It is the case with deleting and disabling of breakpoints when using
2918 always-inserted mode. */
2919
2920static void
2921update_inserted_breakpoint_locations (void)
2922{
2923 struct bp_location *bl, **blp_tmp;
2924 int error_flag = 0;
2925 int val = 0;
2926 int disabled_breaks = 0;
2927 int hw_breakpoint_error = 0;
dd61ec5c 2928 int hw_bp_details_reported = 0;
b775012e 2929
d7e74731 2930 string_file tmp_error_stream;
b775012e
LM
2931
2932 /* Explicitly mark the warning -- this will only be printed if
2933 there was an error. */
d7e74731 2934 tmp_error_stream.puts ("Warning:\n");
b775012e 2935
5ed8105e 2936 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2937
2938 ALL_BP_LOCATIONS (bl, blp_tmp)
2939 {
2940 /* We only want to update software breakpoints and hardware
2941 breakpoints. */
2942 if (!is_breakpoint (bl->owner))
2943 continue;
2944
2945 /* We only want to update locations that are already inserted
2946 and need updating. This is to avoid unwanted insertion during
2947 deletion of breakpoints. */
2948 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2949 continue;
2950
2951 switch_to_program_space_and_thread (bl->pspace);
2952
2953 /* For targets that support global breakpoints, there's no need
2954 to select an inferior to insert breakpoint to. In fact, even
2955 if we aren't attached to any process yet, we should still
2956 insert breakpoints. */
f5656ead 2957 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2958 && ptid_equal (inferior_ptid, null_ptid))
2959 continue;
2960
d7e74731 2961 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2962 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2963 if (val)
2964 error_flag = val;
2965 }
2966
2967 if (error_flag)
2968 {
223ffa71 2969 target_terminal::ours_for_output ();
b775012e
LM
2970 error_stream (tmp_error_stream);
2971 }
b775012e
LM
2972}
2973
c30eee59 2974/* Used when starting or continuing the program. */
c906108c 2975
74960c60
VP
2976static void
2977insert_breakpoint_locations (void)
c906108c 2978{
a5606eee 2979 struct breakpoint *bpt;
35df4500 2980 struct bp_location *bl, **blp_tmp;
eacd795a 2981 int error_flag = 0;
c906108c 2982 int val = 0;
3fbb6ffa 2983 int disabled_breaks = 0;
81d0cc19 2984 int hw_breakpoint_error = 0;
dd61ec5c 2985 int hw_bp_error_explained_already = 0;
c906108c 2986
d7e74731
PA
2987 string_file tmp_error_stream;
2988
81d0cc19
GS
2989 /* Explicitly mark the warning -- this will only be printed if
2990 there was an error. */
d7e74731 2991 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2992
5ed8105e 2993 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2994
35df4500 2995 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2996 {
b775012e 2997 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2998 continue;
2999
4a64f543
MS
3000 /* There is no point inserting thread-specific breakpoints if
3001 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3002 has BL->OWNER always non-NULL. */
35df4500 3003 if (bl->owner->thread != -1
5d5658a1 3004 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3005 continue;
3006
35df4500 3007 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3008
3009 /* For targets that support global breakpoints, there's no need
3010 to select an inferior to insert breakpoint to. In fact, even
3011 if we aren't attached to any process yet, we should still
3012 insert breakpoints. */
f5656ead 3013 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3014 && ptid_equal (inferior_ptid, null_ptid))
3015 continue;
3016
d7e74731 3017 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3018 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3019 if (val)
eacd795a 3020 error_flag = val;
879bfdc2 3021 }
c906108c 3022
4a64f543
MS
3023 /* If we failed to insert all locations of a watchpoint, remove
3024 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3025 ALL_BREAKPOINTS (bpt)
3026 {
3027 int some_failed = 0;
3028 struct bp_location *loc;
3029
3030 if (!is_hardware_watchpoint (bpt))
3031 continue;
3032
d6b74ac4 3033 if (!breakpoint_enabled (bpt))
a5606eee 3034 continue;
74960c60
VP
3035
3036 if (bpt->disposition == disp_del_at_next_stop)
3037 continue;
a5606eee
VP
3038
3039 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3040 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3041 {
3042 some_failed = 1;
3043 break;
3044 }
3045 if (some_failed)
3046 {
3047 for (loc = bpt->loc; loc; loc = loc->next)
3048 if (loc->inserted)
834c0d03 3049 remove_breakpoint (loc);
a5606eee
VP
3050
3051 hw_breakpoint_error = 1;
d7e74731
PA
3052 tmp_error_stream.printf ("Could not insert "
3053 "hardware watchpoint %d.\n",
3054 bpt->number);
eacd795a 3055 error_flag = -1;
a5606eee
VP
3056 }
3057 }
3058
eacd795a 3059 if (error_flag)
81d0cc19
GS
3060 {
3061 /* If a hardware breakpoint or watchpoint was inserted, add a
3062 message about possibly exhausted resources. */
dd61ec5c 3063 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3064 {
d7e74731 3065 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3066You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3067 }
223ffa71 3068 target_terminal::ours_for_output ();
81d0cc19
GS
3069 error_stream (tmp_error_stream);
3070 }
c906108c
SS
3071}
3072
c30eee59
TJB
3073/* Used when the program stops.
3074 Returns zero if successful, or non-zero if there was a problem
3075 removing a breakpoint location. */
3076
c906108c 3077int
fba45db2 3078remove_breakpoints (void)
c906108c 3079{
35df4500 3080 struct bp_location *bl, **blp_tmp;
3a1bae8e 3081 int val = 0;
c906108c 3082
35df4500 3083 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3084 {
1e4d1764 3085 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3086 val |= remove_breakpoint (bl);
c5aa993b 3087 }
3a1bae8e 3088 return val;
c906108c
SS
3089}
3090
49fa26b0
PA
3091/* When a thread exits, remove breakpoints that are related to
3092 that thread. */
3093
3094static void
3095remove_threaded_breakpoints (struct thread_info *tp, int silent)
3096{
3097 struct breakpoint *b, *b_tmp;
3098
3099 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3100 {
5d5658a1 3101 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3102 {
3103 b->disposition = disp_del_at_next_stop;
3104
3105 printf_filtered (_("\
43792cf0
PA
3106Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3107 b->number, print_thread_id (tp));
49fa26b0
PA
3108
3109 /* Hide it from the user. */
3110 b->number = 0;
3111 }
3112 }
3113}
3114
6c95b8df
PA
3115/* Remove breakpoints of process PID. */
3116
3117int
3118remove_breakpoints_pid (int pid)
3119{
35df4500 3120 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3121 int val;
3122 struct inferior *inf = find_inferior_pid (pid);
3123
35df4500 3124 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3125 {
35df4500 3126 if (bl->pspace != inf->pspace)
6c95b8df
PA
3127 continue;
3128
fc126975 3129 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3130 {
834c0d03 3131 val = remove_breakpoint (bl);
6c95b8df
PA
3132 if (val != 0)
3133 return val;
3134 }
3135 }
3136 return 0;
3137}
3138
e58b0e63
PA
3139static int internal_breakpoint_number = -1;
3140
84f4c1fe
PM
3141/* Set the breakpoint number of B, depending on the value of INTERNAL.
3142 If INTERNAL is non-zero, the breakpoint number will be populated
3143 from internal_breakpoint_number and that variable decremented.
e5dd4106 3144 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3145 breakpoint_count and that value incremented. Internal breakpoints
3146 do not set the internal var bpnum. */
3147static void
3148set_breakpoint_number (int internal, struct breakpoint *b)
3149{
3150 if (internal)
3151 b->number = internal_breakpoint_number--;
3152 else
3153 {
3154 set_breakpoint_count (breakpoint_count + 1);
3155 b->number = breakpoint_count;
3156 }
3157}
3158
e62c965a 3159static struct breakpoint *
a6d9a66e 3160create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3161 CORE_ADDR address, enum bptype type,
c0a91b2b 3162 const struct breakpoint_ops *ops)
e62c965a 3163{
51abb421 3164 symtab_and_line sal;
e62c965a
PP
3165 sal.pc = address;
3166 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3167 sal.pspace = current_program_space;
e62c965a 3168
51abb421 3169 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3170 b->number = internal_breakpoint_number--;
3171 b->disposition = disp_donttouch;
3172
3173 return b;
3174}
3175
17450429
PP
3176static const char *const longjmp_names[] =
3177 {
3178 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3179 };
3180#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3181
3182/* Per-objfile data private to breakpoint.c. */
3183struct breakpoint_objfile_data
3184{
3185 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3186 struct bound_minimal_symbol overlay_msym;
17450429
PP
3187
3188 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3189 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3190
28106bc2
SDJ
3191 /* True if we have looked for longjmp probes. */
3192 int longjmp_searched;
3193
3194 /* SystemTap probe points for longjmp (if any). */
3195 VEC (probe_p) *longjmp_probes;
3196
17450429 3197 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3198 struct bound_minimal_symbol terminate_msym;
17450429
PP
3199
3200 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3201 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3202
3203 /* True if we have looked for exception probes. */
3204 int exception_searched;
3205
3206 /* SystemTap probe points for unwinding (if any). */
3207 VEC (probe_p) *exception_probes;
17450429
PP
3208};
3209
3210static const struct objfile_data *breakpoint_objfile_key;
3211
3212/* Minimal symbol not found sentinel. */
3213static struct minimal_symbol msym_not_found;
3214
3215/* Returns TRUE if MSYM point to the "not found" sentinel. */
3216
3217static int
3218msym_not_found_p (const struct minimal_symbol *msym)
3219{
3220 return msym == &msym_not_found;
3221}
3222
3223/* Return per-objfile data needed by breakpoint.c.
3224 Allocate the data if necessary. */
3225
3226static struct breakpoint_objfile_data *
3227get_breakpoint_objfile_data (struct objfile *objfile)
3228{
3229 struct breakpoint_objfile_data *bp_objfile_data;
3230
9a3c8263
SM
3231 bp_objfile_data = ((struct breakpoint_objfile_data *)
3232 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3233 if (bp_objfile_data == NULL)
3234 {
8d749320
SM
3235 bp_objfile_data =
3236 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3237
3238 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3239 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3240 }
3241 return bp_objfile_data;
3242}
3243
28106bc2
SDJ
3244static void
3245free_breakpoint_probes (struct objfile *obj, void *data)
3246{
9a3c8263
SM
3247 struct breakpoint_objfile_data *bp_objfile_data
3248 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3249
3250 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3251 VEC_free (probe_p, bp_objfile_data->exception_probes);
3252}
3253
e62c965a 3254static void
af02033e 3255create_overlay_event_breakpoint (void)
e62c965a 3256{
69de3c6a 3257 struct objfile *objfile;
af02033e 3258 const char *const func_name = "_ovly_debug_event";
e62c965a 3259
69de3c6a
PP
3260 ALL_OBJFILES (objfile)
3261 {
3262 struct breakpoint *b;
17450429
PP
3263 struct breakpoint_objfile_data *bp_objfile_data;
3264 CORE_ADDR addr;
67994074 3265 struct explicit_location explicit_loc;
69de3c6a 3266
17450429
PP
3267 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3268
3b7344d5 3269 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3270 continue;
3271
3b7344d5 3272 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3273 {
3b7344d5 3274 struct bound_minimal_symbol m;
17450429
PP
3275
3276 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3277 if (m.minsym == NULL)
17450429
PP
3278 {
3279 /* Avoid future lookups in this objfile. */
3b7344d5 3280 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3281 continue;
3282 }
3283 bp_objfile_data->overlay_msym = m;
3284 }
e62c965a 3285
77e371c0 3286 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3287 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3288 bp_overlay_event,
3289 &internal_breakpoint_ops);
67994074
KS
3290 initialize_explicit_location (&explicit_loc);
3291 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3292 b->location = new_explicit_location (&explicit_loc);
e62c965a 3293
69de3c6a
PP
3294 if (overlay_debugging == ovly_auto)
3295 {
3296 b->enable_state = bp_enabled;
3297 overlay_events_enabled = 1;
3298 }
3299 else
3300 {
3301 b->enable_state = bp_disabled;
3302 overlay_events_enabled = 0;
3303 }
e62c965a 3304 }
e62c965a
PP
3305}
3306
0fd8e87f 3307static void
af02033e 3308create_longjmp_master_breakpoint (void)
0fd8e87f 3309{
6c95b8df 3310 struct program_space *pspace;
6c95b8df 3311
5ed8105e 3312 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3313
6c95b8df 3314 ALL_PSPACES (pspace)
af02033e
PP
3315 {
3316 struct objfile *objfile;
3317
3318 set_current_program_space (pspace);
3319
3320 ALL_OBJFILES (objfile)
0fd8e87f 3321 {
af02033e
PP
3322 int i;
3323 struct gdbarch *gdbarch;
17450429 3324 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3325
af02033e 3326 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3327
17450429
PP
3328 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3329
28106bc2
SDJ
3330 if (!bp_objfile_data->longjmp_searched)
3331 {
25f9533e
SDJ
3332 VEC (probe_p) *ret;
3333
3334 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3335 if (ret != NULL)
3336 {
3337 /* We are only interested in checking one element. */
3338 struct probe *p = VEC_index (probe_p, ret, 0);
3339
3340 if (!can_evaluate_probe_arguments (p))
3341 {
3342 /* We cannot use the probe interface here, because it does
3343 not know how to evaluate arguments. */
3344 VEC_free (probe_p, ret);
3345 ret = NULL;
3346 }
3347 }
3348 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3349 bp_objfile_data->longjmp_searched = 1;
3350 }
3351
3352 if (bp_objfile_data->longjmp_probes != NULL)
3353 {
3354 int i;
3355 struct probe *probe;
3356 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3357
3358 for (i = 0;
3359 VEC_iterate (probe_p,
3360 bp_objfile_data->longjmp_probes,
3361 i, probe);
3362 ++i)
3363 {
3364 struct breakpoint *b;
3365
729662a5
TT
3366 b = create_internal_breakpoint (gdbarch,
3367 get_probe_address (probe,
3368 objfile),
28106bc2
SDJ
3369 bp_longjmp_master,
3370 &internal_breakpoint_ops);
d28cd78a 3371 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3372 b->enable_state = bp_disabled;
3373 }
3374
3375 continue;
3376 }
3377
0569175e
TSD
3378 if (!gdbarch_get_longjmp_target_p (gdbarch))
3379 continue;
3380
17450429 3381 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3382 {
3383 struct breakpoint *b;
af02033e 3384 const char *func_name;
17450429 3385 CORE_ADDR addr;
67994074 3386 struct explicit_location explicit_loc;
6c95b8df 3387
3b7344d5 3388 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3389 continue;
0fd8e87f 3390
17450429 3391 func_name = longjmp_names[i];
3b7344d5 3392 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3393 {
3b7344d5 3394 struct bound_minimal_symbol m;
17450429
PP
3395
3396 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3397 if (m.minsym == NULL)
17450429
PP
3398 {
3399 /* Prevent future lookups in this objfile. */
3b7344d5 3400 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3401 continue;
3402 }
3403 bp_objfile_data->longjmp_msym[i] = m;
3404 }
3405
77e371c0 3406 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3407 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3408 &internal_breakpoint_ops);
67994074
KS
3409 initialize_explicit_location (&explicit_loc);
3410 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3411 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3412 b->enable_state = bp_disabled;
3413 }
0fd8e87f 3414 }
af02033e 3415 }
0fd8e87f
UW
3416}
3417
af02033e 3418/* Create a master std::terminate breakpoint. */
aa7d318d 3419static void
af02033e 3420create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3421{
3422 struct program_space *pspace;
af02033e 3423 const char *const func_name = "std::terminate()";
aa7d318d 3424
5ed8105e 3425 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3426
3427 ALL_PSPACES (pspace)
17450429
PP
3428 {
3429 struct objfile *objfile;
3430 CORE_ADDR addr;
3431
3432 set_current_program_space (pspace);
3433
aa7d318d
TT
3434 ALL_OBJFILES (objfile)
3435 {
3436 struct breakpoint *b;
17450429 3437 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3438 struct explicit_location explicit_loc;
aa7d318d 3439
17450429 3440 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3441
3b7344d5 3442 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3443 continue;
3444
3b7344d5 3445 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3446 {
3b7344d5 3447 struct bound_minimal_symbol m;
17450429
PP
3448
3449 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3450 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3451 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3452 {
3453 /* Prevent future lookups in this objfile. */
3b7344d5 3454 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3455 continue;
3456 }
3457 bp_objfile_data->terminate_msym = m;
3458 }
aa7d318d 3459
77e371c0 3460 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3461 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3462 bp_std_terminate_master,
3463 &internal_breakpoint_ops);
67994074
KS
3464 initialize_explicit_location (&explicit_loc);
3465 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3466 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3467 b->enable_state = bp_disabled;
3468 }
17450429 3469 }
aa7d318d
TT
3470}
3471
186c406b
TT
3472/* Install a master breakpoint on the unwinder's debug hook. */
3473
70221824 3474static void
186c406b
TT
3475create_exception_master_breakpoint (void)
3476{
3477 struct objfile *objfile;
17450429 3478 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3479
3480 ALL_OBJFILES (objfile)
3481 {
17450429
PP
3482 struct breakpoint *b;
3483 struct gdbarch *gdbarch;
3484 struct breakpoint_objfile_data *bp_objfile_data;
3485 CORE_ADDR addr;
67994074 3486 struct explicit_location explicit_loc;
17450429
PP
3487
3488 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3489
28106bc2
SDJ
3490 /* We prefer the SystemTap probe point if it exists. */
3491 if (!bp_objfile_data->exception_searched)
3492 {
25f9533e
SDJ
3493 VEC (probe_p) *ret;
3494
3495 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3496
3497 if (ret != NULL)
3498 {
3499 /* We are only interested in checking one element. */
3500 struct probe *p = VEC_index (probe_p, ret, 0);
3501
3502 if (!can_evaluate_probe_arguments (p))
3503 {
3504 /* We cannot use the probe interface here, because it does
3505 not know how to evaluate arguments. */
3506 VEC_free (probe_p, ret);
3507 ret = NULL;
3508 }
3509 }
3510 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3511 bp_objfile_data->exception_searched = 1;
3512 }
3513
3514 if (bp_objfile_data->exception_probes != NULL)
3515 {
3516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3517 int i;
3518 struct probe *probe;
3519
3520 for (i = 0;
3521 VEC_iterate (probe_p,
3522 bp_objfile_data->exception_probes,
3523 i, probe);
3524 ++i)
3525 {
3526 struct breakpoint *b;
3527
729662a5
TT
3528 b = create_internal_breakpoint (gdbarch,
3529 get_probe_address (probe,
3530 objfile),
28106bc2
SDJ
3531 bp_exception_master,
3532 &internal_breakpoint_ops);
d28cd78a 3533 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3534 b->enable_state = bp_disabled;
3535 }
3536
3537 continue;
3538 }
3539
3540 /* Otherwise, try the hook function. */
3541
3b7344d5 3542 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3543 continue;
3544
3545 gdbarch = get_objfile_arch (objfile);
186c406b 3546
3b7344d5 3547 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3548 {
3b7344d5 3549 struct bound_minimal_symbol debug_hook;
186c406b 3550
17450429 3551 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3552 if (debug_hook.minsym == NULL)
17450429 3553 {
3b7344d5 3554 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3555 continue;
3556 }
3557
3558 bp_objfile_data->exception_msym = debug_hook;
186c406b 3559 }
17450429 3560
77e371c0 3561 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3562 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3563 &current_target);
06edf0c0
PA
3564 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3565 &internal_breakpoint_ops);
67994074
KS
3566 initialize_explicit_location (&explicit_loc);
3567 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3568 b->location = new_explicit_location (&explicit_loc);
17450429 3569 b->enable_state = bp_disabled;
186c406b 3570 }
186c406b
TT
3571}
3572
9ef9e6a6
KS
3573/* Does B have a location spec? */
3574
3575static int
3576breakpoint_event_location_empty_p (const struct breakpoint *b)
3577{
d28cd78a 3578 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3579}
3580
c906108c 3581void
fba45db2 3582update_breakpoints_after_exec (void)
c906108c 3583{
35df4500 3584 struct breakpoint *b, *b_tmp;
876fa593 3585 struct bp_location *bploc, **bplocp_tmp;
c906108c 3586
25b22b0a
PA
3587 /* We're about to delete breakpoints from GDB's lists. If the
3588 INSERTED flag is true, GDB will try to lift the breakpoints by
3589 writing the breakpoints' "shadow contents" back into memory. The
3590 "shadow contents" are NOT valid after an exec, so GDB should not
3591 do that. Instead, the target is responsible from marking
3592 breakpoints out as soon as it detects an exec. We don't do that
3593 here instead, because there may be other attempts to delete
3594 breakpoints after detecting an exec and before reaching here. */
876fa593 3595 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3596 if (bploc->pspace == current_program_space)
3597 gdb_assert (!bploc->inserted);
c906108c 3598
35df4500 3599 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3600 {
6c95b8df
PA
3601 if (b->pspace != current_program_space)
3602 continue;
3603
4a64f543 3604 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3605 if (b->type == bp_shlib_event)
3606 {
3607 delete_breakpoint (b);
3608 continue;
3609 }
c906108c 3610
4a64f543 3611 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3612 if (b->type == bp_jit_event)
3613 {
3614 delete_breakpoint (b);
3615 continue;
3616 }
3617
1900040c 3618 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3619 as must overlay event and longjmp master breakpoints. */
3620 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3621 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3622 || b->type == bp_exception_master)
c4093a6a
JM
3623 {
3624 delete_breakpoint (b);
3625 continue;
3626 }
3627
4a64f543 3628 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3629 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3630 {
3631 delete_breakpoint (b);
3632 continue;
3633 }
3634
7c16b83e
PA
3635 /* Just like single-step breakpoints. */
3636 if (b->type == bp_single_step)
3637 {
3638 delete_breakpoint (b);
3639 continue;
3640 }
3641
611c83ae
PA
3642 /* Longjmp and longjmp-resume breakpoints are also meaningless
3643 after an exec. */
186c406b 3644 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3645 || b->type == bp_longjmp_call_dummy
186c406b 3646 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3647 {
3648 delete_breakpoint (b);
3649 continue;
3650 }
3651
ce78b96d
JB
3652 if (b->type == bp_catchpoint)
3653 {
3654 /* For now, none of the bp_catchpoint breakpoints need to
3655 do anything at this point. In the future, if some of
3656 the catchpoints need to something, we will need to add
3657 a new method, and call this method from here. */
3658 continue;
3659 }
3660
c5aa993b
JM
3661 /* bp_finish is a special case. The only way we ought to be able
3662 to see one of these when an exec() has happened, is if the user
3663 caught a vfork, and then said "finish". Ordinarily a finish just
3664 carries them to the call-site of the current callee, by setting
3665 a temporary bp there and resuming. But in this case, the finish
3666 will carry them entirely through the vfork & exec.
3667
3668 We don't want to allow a bp_finish to remain inserted now. But
3669 we can't safely delete it, 'cause finish_command has a handle to
3670 the bp on a bpstat, and will later want to delete it. There's a
3671 chance (and I've seen it happen) that if we delete the bp_finish
3672 here, that its storage will get reused by the time finish_command
3673 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3674 We really must allow finish_command to delete a bp_finish.
3675
e5dd4106 3676 In the absence of a general solution for the "how do we know
53a5351d
JM
3677 it's safe to delete something others may have handles to?"
3678 problem, what we'll do here is just uninsert the bp_finish, and
3679 let finish_command delete it.
3680
3681 (We know the bp_finish is "doomed" in the sense that it's
3682 momentary, and will be deleted as soon as finish_command sees
3683 the inferior stopped. So it doesn't matter that the bp's
3684 address is probably bogus in the new a.out, unlike e.g., the
3685 solib breakpoints.) */
c5aa993b 3686
c5aa993b
JM
3687 if (b->type == bp_finish)
3688 {
3689 continue;
3690 }
3691
3692 /* Without a symbolic address, we have little hope of the
3693 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3694 a.out. */
9ef9e6a6 3695 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3696 {
3697 delete_breakpoint (b);
3698 continue;
3699 }
c5aa993b 3700 }
c906108c
SS
3701}
3702
3703int
d80ee84f 3704detach_breakpoints (ptid_t ptid)
c906108c 3705{
35df4500 3706 struct bp_location *bl, **blp_tmp;
3a1bae8e 3707 int val = 0;
2989a365 3708 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3709 struct inferior *inf = current_inferior ();
c5aa993b 3710
dfd4cc63 3711 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3712 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3713
6c95b8df 3714 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3715 inferior_ptid = ptid;
35df4500 3716 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3717 {
35df4500 3718 if (bl->pspace != inf->pspace)
6c95b8df
PA
3719 continue;
3720
bd9673a4
PW
3721 /* This function must physically remove breakpoints locations
3722 from the specified ptid, without modifying the breakpoint
3723 package's state. Locations of type bp_loc_other are only
3724 maintained at GDB side. So, there is no need to remove
3725 these bp_loc_other locations. Moreover, removing these
3726 would modify the breakpoint package's state. */
3727 if (bl->loc_type == bp_loc_other)
3728 continue;
3729
35df4500 3730 if (bl->inserted)
b2b6a7da 3731 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3732 }
d03285ec 3733
3a1bae8e 3734 return val;
c906108c
SS
3735}
3736
35df4500 3737/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3738 Note that this is used to detach breakpoints from a child fork.
3739 When we get here, the child isn't in the inferior list, and neither
3740 do we have objects to represent its address space --- we should
35df4500 3741 *not* look at bl->pspace->aspace here. */
6c95b8df 3742
c906108c 3743static int
b2b6a7da 3744remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3745{
3746 int val;
c5aa993b 3747
35df4500
TJB
3748 /* BL is never in moribund_locations by our callers. */
3749 gdb_assert (bl->owner != NULL);
2bdf28a0 3750
74960c60
VP
3751 /* The type of none suggests that owner is actually deleted.
3752 This should not ever happen. */
35df4500 3753 gdb_assert (bl->owner->type != bp_none);
0bde7532 3754
35df4500
TJB
3755 if (bl->loc_type == bp_loc_software_breakpoint
3756 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3757 {
c02f5703
MS
3758 /* "Normal" instruction breakpoint: either the standard
3759 trap-instruction bp (bp_breakpoint), or a
3760 bp_hardware_breakpoint. */
3761
3762 /* First check to see if we have to handle an overlay. */
3763 if (overlay_debugging == ovly_off
35df4500
TJB
3764 || bl->section == NULL
3765 || !(section_is_overlay (bl->section)))
c02f5703
MS
3766 {
3767 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3768
3769 /* If we're trying to uninsert a memory breakpoint that we
3770 know is set in a dynamic object that is marked
3771 shlib_disabled, then either the dynamic object was
3772 removed with "remove-symbol-file" or with
3773 "nosharedlibrary". In the former case, we don't know
3774 whether another dynamic object might have loaded over the
3775 breakpoint's address -- the user might well let us know
3776 about it next with add-symbol-file (the whole point of
d03de421 3777 add-symbol-file is letting the user manually maintain a
08351840
PA
3778 list of dynamically loaded objects). If we have the
3779 breakpoint's shadow memory, that is, this is a software
3780 breakpoint managed by GDB, check whether the breakpoint
3781 is still inserted in memory, to avoid overwriting wrong
3782 code with stale saved shadow contents. Note that HW
3783 breakpoints don't have shadow memory, as they're
3784 implemented using a mechanism that is not dependent on
3785 being able to modify the target's memory, and as such
3786 they should always be removed. */
3787 if (bl->shlib_disabled
3788 && bl->target_info.shadow_len != 0
3789 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3790 val = 0;
3791 else
73971819 3792 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3793 }
c906108c
SS
3794 else
3795 {
4a64f543 3796 /* This breakpoint is in an overlay section.
c02f5703
MS
3797 Did we set a breakpoint at the LMA? */
3798 if (!overlay_events_enabled)
3799 {
3800 /* Yes -- overlay event support is not active, so we
3801 should have set a breakpoint at the LMA. Remove it.
3802 */
c02f5703
MS
3803 /* Ignore any failures: if the LMA is in ROM, we will
3804 have already warned when we failed to insert it. */
35df4500
TJB
3805 if (bl->loc_type == bp_loc_hardware_breakpoint)
3806 target_remove_hw_breakpoint (bl->gdbarch,
3807 &bl->overlay_target_info);
c02f5703 3808 else
35df4500 3809 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3810 &bl->overlay_target_info,
3811 reason);
c02f5703
MS
3812 }
3813 /* Did we set a breakpoint at the VMA?
3814 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3815 if (bl->inserted)
c906108c 3816 {
c02f5703
MS
3817 /* Yes -- remove it. Previously we did not bother to
3818 remove the breakpoint if the section had been
3819 unmapped, but let's not rely on that being safe. We
3820 don't know what the overlay manager might do. */
aa67235e
UW
3821
3822 /* However, we should remove *software* breakpoints only
3823 if the section is still mapped, or else we overwrite
3824 wrong code with the saved shadow contents. */
348d480f
PA
3825 if (bl->loc_type == bp_loc_hardware_breakpoint
3826 || section_is_mapped (bl->section))
73971819 3827 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3828 else
3829 val = 0;
c906108c 3830 }
c02f5703
MS
3831 else
3832 {
3833 /* No -- not inserted, so no need to remove. No error. */
3834 val = 0;
3835 }
c906108c 3836 }
879d1e6b 3837
08351840
PA
3838 /* In some cases, we might not be able to remove a breakpoint in
3839 a shared library that has already been removed, but we have
3840 not yet processed the shlib unload event. Similarly for an
3841 unloaded add-symbol-file object - the user might not yet have
3842 had the chance to remove-symbol-file it. shlib_disabled will
3843 be set if the library/object has already been removed, but
3844 the breakpoint hasn't been uninserted yet, e.g., after
3845 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3846 always-inserted mode. */
076855f9 3847 if (val
08351840
PA
3848 && (bl->loc_type == bp_loc_software_breakpoint
3849 && (bl->shlib_disabled
3850 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3851 || shared_objfile_contains_address_p (bl->pspace,
3852 bl->address))))
879d1e6b
UW
3853 val = 0;
3854
c906108c
SS
3855 if (val)
3856 return val;
b2b6a7da 3857 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3858 }
35df4500 3859 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3860 {
77b06cd7
TJB
3861 gdb_assert (bl->owner->ops != NULL
3862 && bl->owner->ops->remove_location != NULL);
3863
b2b6a7da 3864 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3865 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3866
c906108c 3867 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3868 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3869 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3870 bl->owner->number);
c906108c 3871 }
35df4500
TJB
3872 else if (bl->owner->type == bp_catchpoint
3873 && breakpoint_enabled (bl->owner)
3874 && !bl->duplicate)
ce78b96d 3875 {
77b06cd7
TJB
3876 gdb_assert (bl->owner->ops != NULL
3877 && bl->owner->ops->remove_location != NULL);
ce78b96d 3878
73971819 3879 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3880 if (val)
3881 return val;
77b06cd7 3882
b2b6a7da 3883 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3884 }
c906108c
SS
3885
3886 return 0;
3887}
3888
6c95b8df 3889static int
834c0d03 3890remove_breakpoint (struct bp_location *bl)
6c95b8df 3891{
35df4500
TJB
3892 /* BL is never in moribund_locations by our callers. */
3893 gdb_assert (bl->owner != NULL);
2bdf28a0 3894
6c95b8df
PA
3895 /* The type of none suggests that owner is actually deleted.
3896 This should not ever happen. */
35df4500 3897 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3898
5ed8105e 3899 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3900
35df4500 3901 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3902
5ed8105e 3903 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3904}
3905
c906108c
SS
3906/* Clear the "inserted" flag in all breakpoints. */
3907
25b22b0a 3908void
fba45db2 3909mark_breakpoints_out (void)
c906108c 3910{
35df4500 3911 struct bp_location *bl, **blp_tmp;
c906108c 3912
35df4500 3913 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3914 if (bl->pspace == current_program_space)
35df4500 3915 bl->inserted = 0;
c906108c
SS
3916}
3917
53a5351d
JM
3918/* Clear the "inserted" flag in all breakpoints and delete any
3919 breakpoints which should go away between runs of the program.
c906108c
SS
3920
3921 Plus other such housekeeping that has to be done for breakpoints
3922 between runs.
3923
53a5351d
JM
3924 Note: this function gets called at the end of a run (by
3925 generic_mourn_inferior) and when a run begins (by
4a64f543 3926 init_wait_for_inferior). */
c906108c
SS
3927
3928
3929
3930void
fba45db2 3931breakpoint_init_inferior (enum inf_context context)
c906108c 3932{
35df4500 3933 struct breakpoint *b, *b_tmp;
870f88f7 3934 struct bp_location *bl;
1c5cfe86 3935 int ix;
6c95b8df 3936 struct program_space *pspace = current_program_space;
c906108c 3937
50c71eaf
PA
3938 /* If breakpoint locations are shared across processes, then there's
3939 nothing to do. */
f5656ead 3940 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3941 return;
3942
1a853c52 3943 mark_breakpoints_out ();
075f6582 3944
35df4500 3945 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3946 {
6c95b8df
PA
3947 if (b->loc && b->loc->pspace != pspace)
3948 continue;
3949
c5aa993b
JM
3950 switch (b->type)
3951 {
3952 case bp_call_dummy:
e2e4d78b 3953 case bp_longjmp_call_dummy:
c906108c 3954
c5aa993b 3955 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3956 cause problems when the inferior is rerun, so we better get
3957 rid of it. */
3958
3959 case bp_watchpoint_scope:
3960
3961 /* Also get rid of scope breakpoints. */
3962
3963 case bp_shlib_event:
3964
3965 /* Also remove solib event breakpoints. Their addresses may
3966 have changed since the last time we ran the program.
3967 Actually we may now be debugging against different target;
3968 and so the solib backend that installed this breakpoint may
3969 not be used in by the target. E.g.,
3970
3971 (gdb) file prog-linux
3972 (gdb) run # native linux target
3973 ...
3974 (gdb) kill
3975 (gdb) file prog-win.exe
3976 (gdb) tar rem :9999 # remote Windows gdbserver.
3977 */
c906108c 3978
f59f708a
PA
3979 case bp_step_resume:
3980
3981 /* Also remove step-resume breakpoints. */
3982
7c16b83e
PA
3983 case bp_single_step:
3984
3985 /* Also remove single-step breakpoints. */
3986
c5aa993b
JM
3987 delete_breakpoint (b);
3988 break;
c906108c 3989
c5aa993b
JM
3990 case bp_watchpoint:
3991 case bp_hardware_watchpoint:
3992 case bp_read_watchpoint:
3993 case bp_access_watchpoint:
3a5c3e22
PA
3994 {
3995 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3996
3a5c3e22
PA
3997 /* Likewise for watchpoints on local expressions. */
3998 if (w->exp_valid_block != NULL)
3999 delete_breakpoint (b);
63000888 4000 else
3a5c3e22 4001 {
63000888
PA
4002 /* Get rid of existing locations, which are no longer
4003 valid. New ones will be created in
4004 update_watchpoint, when the inferior is restarted.
4005 The next update_global_location_list call will
4006 garbage collect them. */
4007 b->loc = NULL;
4008
4009 if (context == inf_starting)
4010 {
4011 /* Reset val field to force reread of starting value in
4012 insert_breakpoints. */
4013 if (w->val)
4014 value_free (w->val);
4015 w->val = NULL;
4016 w->val_valid = 0;
4017 }
4018 }
3a5c3e22 4019 }
c5aa993b
JM
4020 break;
4021 default:
c5aa993b
JM
4022 break;
4023 }
4024 }
1c5cfe86
PA
4025
4026 /* Get rid of the moribund locations. */
35df4500
TJB
4027 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4028 decref_bp_location (&bl);
1c5cfe86 4029 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4030}
4031
6c95b8df
PA
4032/* These functions concern about actual breakpoints inserted in the
4033 target --- to e.g. check if we need to do decr_pc adjustment or if
4034 we need to hop over the bkpt --- so we check for address space
4035 match, not program space. */
4036
c2c6d25f
JM
4037/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4038 exists at PC. It returns ordinary_breakpoint_here if it's an
4039 ordinary breakpoint, or permanent_breakpoint_here if it's a
4040 permanent breakpoint.
4041 - When continuing from a location with an ordinary breakpoint, we
4042 actually single step once before calling insert_breakpoints.
e5dd4106 4043 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4044 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4045 the target, to advance the PC past the breakpoint. */
c906108c 4046
c2c6d25f 4047enum breakpoint_here
accd0bcd 4048breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4049{
35df4500 4050 struct bp_location *bl, **blp_tmp;
c2c6d25f 4051 int any_breakpoint_here = 0;
c906108c 4052
35df4500 4053 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4054 {
35df4500
TJB
4055 if (bl->loc_type != bp_loc_software_breakpoint
4056 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4057 continue;
4058
f1310107 4059 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4060 if ((breakpoint_enabled (bl->owner)
1a853c52 4061 || bl->permanent)
f1310107 4062 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4063 {
4064 if (overlay_debugging
35df4500
TJB
4065 && section_is_overlay (bl->section)
4066 && !section_is_mapped (bl->section))
075f6582 4067 continue; /* unmapped overlay -- can't be a match */
1a853c52 4068 else if (bl->permanent)
075f6582
DJ
4069 return permanent_breakpoint_here;
4070 else
4071 any_breakpoint_here = 1;
4072 }
4073 }
c906108c 4074
f486487f 4075 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4076}
4077
d35ae833
PA
4078/* See breakpoint.h. */
4079
4080int
accd0bcd 4081breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4082 CORE_ADDR addr, ULONGEST len)
4083{
4084 struct bp_location *bl, **blp_tmp;
4085
4086 ALL_BP_LOCATIONS (bl, blp_tmp)
4087 {
4088 if (bl->loc_type != bp_loc_software_breakpoint
4089 && bl->loc_type != bp_loc_hardware_breakpoint)
4090 continue;
4091
4092 if ((breakpoint_enabled (bl->owner)
4093 || bl->permanent)
4094 && breakpoint_location_address_range_overlap (bl, aspace,
4095 addr, len))
4096 {
4097 if (overlay_debugging
4098 && section_is_overlay (bl->section)
4099 && !section_is_mapped (bl->section))
4100 {
4101 /* Unmapped overlay -- can't be a match. */
4102 continue;
4103 }
4104
4105 return 1;
4106 }
4107 }
4108
4109 return 0;
4110}
4111
1c5cfe86
PA
4112/* Return true if there's a moribund breakpoint at PC. */
4113
4114int
accd0bcd 4115moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4116{
4117 struct bp_location *loc;
4118 int ix;
4119
4120 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4121 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4122 return 1;
4123
4124 return 0;
4125}
c2c6d25f 4126
f7ce857f
PA
4127/* Returns non-zero iff BL is inserted at PC, in address space
4128 ASPACE. */
4129
4130static int
4131bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4132 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4133{
4134 if (bl->inserted
4135 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4136 aspace, pc))
4137 {
4138 if (overlay_debugging
4139 && section_is_overlay (bl->section)
4140 && !section_is_mapped (bl->section))
4141 return 0; /* unmapped overlay -- can't be a match */
4142 else
4143 return 1;
4144 }
4145 return 0;
4146}
4147
a1fd2fa5 4148/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4149
4150int
accd0bcd 4151breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4152{
f7ce857f 4153 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4154
f7ce857f 4155 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4156 {
f7ce857f
PA
4157 struct bp_location *bl = *blp;
4158
35df4500
TJB
4159 if (bl->loc_type != bp_loc_software_breakpoint
4160 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4161 continue;
4162
f7ce857f
PA
4163 if (bp_location_inserted_here_p (bl, aspace, pc))
4164 return 1;
c5aa993b 4165 }
c36b740a
VP
4166 return 0;
4167}
4168
a1fd2fa5
PA
4169/* This function returns non-zero iff there is a software breakpoint
4170 inserted at PC. */
c36b740a
VP
4171
4172int
accd0bcd 4173software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4174 CORE_ADDR pc)
4fa8626c 4175{
f7ce857f 4176 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4177
f7ce857f 4178 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4179 {
f7ce857f
PA
4180 struct bp_location *bl = *blp;
4181
35df4500 4182 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4183 continue;
4184
f7ce857f
PA
4185 if (bp_location_inserted_here_p (bl, aspace, pc))
4186 return 1;
4fa8626c
DJ
4187 }
4188
4189 return 0;
9c02b525
PA
4190}
4191
4192/* See breakpoint.h. */
4193
4194int
accd0bcd 4195hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4196 CORE_ADDR pc)
4197{
4198 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4199
4200 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4201 {
4202 struct bp_location *bl = *blp;
4203
4204 if (bl->loc_type != bp_loc_hardware_breakpoint)
4205 continue;
4206
4207 if (bp_location_inserted_here_p (bl, aspace, pc))
4208 return 1;
4209 }
4210
4211 return 0;
4fa8626c
DJ
4212}
4213
9093389c 4214int
accd0bcd 4215hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4216 CORE_ADDR addr, ULONGEST len)
4217{
4218 struct breakpoint *bpt;
4219
4220 ALL_BREAKPOINTS (bpt)
4221 {
4222 struct bp_location *loc;
4223
4224 if (bpt->type != bp_hardware_watchpoint
4225 && bpt->type != bp_access_watchpoint)
4226 continue;
4227
4228 if (!breakpoint_enabled (bpt))
4229 continue;
4230
4231 for (loc = bpt->loc; loc; loc = loc->next)
4232 if (loc->pspace->aspace == aspace && loc->inserted)
4233 {
4234 CORE_ADDR l, h;
4235
4236 /* Check for intersection. */
768adc05
PA
4237 l = std::max<CORE_ADDR> (loc->address, addr);
4238 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4239 if (l < h)
4240 return 1;
4241 }
4242 }
4243 return 0;
4244}
c906108c 4245\f
c5aa993b 4246
c906108c
SS
4247/* bpstat stuff. External routines' interfaces are documented
4248 in breakpoint.h. */
4249
4250int
c326b90e 4251is_catchpoint (struct breakpoint *ep)
c906108c 4252{
533be4dd 4253 return (ep->type == bp_catchpoint);
c906108c
SS
4254}
4255
f431efe5
PA
4256/* Frees any storage that is part of a bpstat. Does not walk the
4257 'next' chain. */
4258
04afa70c 4259bpstats::~bpstats ()
198757a8 4260{
04afa70c
TT
4261 if (old_val != NULL)
4262 value_free (old_val);
04afa70c
TT
4263 if (bp_location_at != NULL)
4264 decref_bp_location (&bp_location_at);
198757a8
VP
4265}
4266
c906108c
SS
4267/* Clear a bpstat so that it says we are not at any breakpoint.
4268 Also free any storage that is part of a bpstat. */
4269
4270void
fba45db2 4271bpstat_clear (bpstat *bsp)
c906108c
SS
4272{
4273 bpstat p;
4274 bpstat q;
4275
4276 if (bsp == 0)
4277 return;
4278 p = *bsp;
4279 while (p != NULL)
4280 {
4281 q = p->next;
04afa70c 4282 delete p;
c906108c
SS
4283 p = q;
4284 }
4285 *bsp = NULL;
4286}
4287
04afa70c
TT
4288bpstats::bpstats (const bpstats &other)
4289 : next (NULL),
4290 bp_location_at (other.bp_location_at),
4291 breakpoint_at (other.breakpoint_at),
4292 commands (other.commands),
4293 old_val (other.old_val),
4294 print (other.print),
4295 stop (other.stop),
4296 print_it (other.print_it)
4297{
4298 if (old_val != NULL)
4299 {
4300 old_val = value_copy (old_val);
4301 release_value (old_val);
4302 }
4303 incref_bp_location (bp_location_at);
04afa70c
TT
4304}
4305
c906108c
SS
4306/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4307 is part of the bpstat is copied as well. */
4308
4309bpstat
fba45db2 4310bpstat_copy (bpstat bs)
c906108c
SS
4311{
4312 bpstat p = NULL;
4313 bpstat tmp;
4314 bpstat retval = NULL;
4315
4316 if (bs == NULL)
4317 return bs;
4318
4319 for (; bs != NULL; bs = bs->next)
4320 {
04afa70c 4321 tmp = new bpstats (*bs);
31cc81e9 4322
c906108c
SS
4323 if (p == NULL)
4324 /* This is the first thing in the chain. */
4325 retval = tmp;
4326 else
4327 p->next = tmp;
4328 p = tmp;
4329 }
4330 p->next = NULL;
4331 return retval;
4332}
4333
4a64f543 4334/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4335
4336bpstat
fba45db2 4337bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4338{
c5aa993b
JM
4339 if (bsp == NULL)
4340 return NULL;
c906108c 4341
c5aa993b
JM
4342 for (; bsp != NULL; bsp = bsp->next)
4343 {
f431efe5 4344 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4345 return bsp;
4346 }
c906108c
SS
4347 return NULL;
4348}
4349
ab04a2af
TT
4350/* See breakpoint.h. */
4351
47591c29 4352int
427cd150 4353bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4354{
ab04a2af
TT
4355 for (; bsp != NULL; bsp = bsp->next)
4356 {
427cd150
TT
4357 if (bsp->breakpoint_at == NULL)
4358 {
4359 /* A moribund location can never explain a signal other than
4360 GDB_SIGNAL_TRAP. */
4361 if (sig == GDB_SIGNAL_TRAP)
47591c29 4362 return 1;
427cd150
TT
4363 }
4364 else
47591c29
PA
4365 {
4366 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4367 sig))
4368 return 1;
4369 }
ab04a2af
TT
4370 }
4371
47591c29 4372 return 0;
ab04a2af
TT
4373}
4374
4a64f543
MS
4375/* Put in *NUM the breakpoint number of the first breakpoint we are
4376 stopped at. *BSP upon return is a bpstat which points to the
4377 remaining breakpoints stopped at (but which is not guaranteed to be
4378 good for anything but further calls to bpstat_num).
4379
8671a17b
PA
4380 Return 0 if passed a bpstat which does not indicate any breakpoints.
4381 Return -1 if stopped at a breakpoint that has been deleted since
4382 we set it.
4383 Return 1 otherwise. */
c906108c
SS
4384
4385int
8671a17b 4386bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4387{
4388 struct breakpoint *b;
4389
4390 if ((*bsp) == NULL)
4391 return 0; /* No more breakpoint values */
8671a17b 4392
4a64f543
MS
4393 /* We assume we'll never have several bpstats that correspond to a
4394 single breakpoint -- otherwise, this function might return the
4395 same number more than once and this will look ugly. */
f431efe5 4396 b = (*bsp)->breakpoint_at;
8671a17b
PA
4397 *bsp = (*bsp)->next;
4398 if (b == NULL)
4399 return -1; /* breakpoint that's been deleted since */
4400
4401 *num = b->number; /* We have its number */
4402 return 1;
c906108c
SS
4403}
4404
e93ca019 4405/* See breakpoint.h. */
c906108c
SS
4406
4407void
e93ca019 4408bpstat_clear_actions (void)
c906108c 4409{
e93ca019
JK
4410 struct thread_info *tp;
4411 bpstat bs;
4412
4413 if (ptid_equal (inferior_ptid, null_ptid))
4414 return;
4415
4416 tp = find_thread_ptid (inferior_ptid);
4417 if (tp == NULL)
4418 return;
4419
4420 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4421 {
d1b0a7bf 4422 bs->commands = NULL;
abf85f46 4423
c906108c
SS
4424 if (bs->old_val != NULL)
4425 {
4426 value_free (bs->old_val);
4427 bs->old_val = NULL;
4428 }
4429 }
4430}
4431
f3b1572e
PA
4432/* Called when a command is about to proceed the inferior. */
4433
4434static void
4435breakpoint_about_to_proceed (void)
4436{
4437 if (!ptid_equal (inferior_ptid, null_ptid))
4438 {
4439 struct thread_info *tp = inferior_thread ();
4440
4441 /* Allow inferior function calls in breakpoint commands to not
4442 interrupt the command list. When the call finishes
4443 successfully, the inferior will be standing at the same
4444 breakpoint as if nothing happened. */
16c381f0 4445 if (tp->control.in_infcall)
f3b1572e
PA
4446 return;
4447 }
4448
4449 breakpoint_proceeded = 1;
4450}
4451
abf85f46
JK
4452/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4453 or its equivalent. */
4454
4455static int
4456command_line_is_silent (struct command_line *cmd)
4457{
4f45d445 4458 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4459}
4460
4a64f543
MS
4461/* Execute all the commands associated with all the breakpoints at
4462 this location. Any of these commands could cause the process to
4463 proceed beyond this point, etc. We look out for such changes by
4464 checking the global "breakpoint_proceeded" after each command.
c906108c 4465
347bddb7
PA
4466 Returns true if a breakpoint command resumed the inferior. In that
4467 case, it is the caller's responsibility to recall it again with the
4468 bpstat of the current thread. */
4469
4470static int
4471bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4472{
4473 bpstat bs;
347bddb7 4474 int again = 0;
c906108c
SS
4475
4476 /* Avoid endless recursion if a `source' command is contained
4477 in bs->commands. */
4478 if (executing_breakpoint_commands)
347bddb7 4479 return 0;
c906108c 4480
81b1e71c
TT
4481 scoped_restore save_executing
4482 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4483
1ac32117 4484 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4485
4a64f543 4486 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4487 bs = *bsp;
4488
4489 breakpoint_proceeded = 0;
4490 for (; bs != NULL; bs = bs->next)
4491 {
d1b0a7bf 4492 struct command_line *cmd = NULL;
6c50ab1c
JB
4493
4494 /* Take ownership of the BSP's command tree, if it has one.
4495
4496 The command tree could legitimately contain commands like
4497 'step' and 'next', which call clear_proceed_status, which
4498 frees stop_bpstat's command tree. To make sure this doesn't
4499 free the tree we're executing out from under us, we need to
4500 take ownership of the tree ourselves. Since a given bpstat's
4501 commands are only executed once, we don't need to copy it; we
4502 can clear the pointer in the bpstat, and make sure we free
4503 the tree when we're done. */
d1b0a7bf 4504 counted_command_line ccmd = bs->commands;
9add0f1b 4505 bs->commands = NULL;
d1b0a7bf
TT
4506 if (ccmd != NULL)
4507 cmd = ccmd.get ();
abf85f46
JK
4508 if (command_line_is_silent (cmd))
4509 {
4510 /* The action has been already done by bpstat_stop_status. */
4511 cmd = cmd->next;
4512 }
6c50ab1c 4513
c906108c
SS
4514 while (cmd != NULL)
4515 {
4516 execute_control_command (cmd);
4517
4518 if (breakpoint_proceeded)
4519 break;
4520 else
4521 cmd = cmd->next;
4522 }
6c50ab1c 4523
c906108c 4524 if (breakpoint_proceeded)
32c1e744 4525 {
cb814510 4526 if (current_ui->async)
347bddb7
PA
4527 /* If we are in async mode, then the target might be still
4528 running, not stopped at any breakpoint, so nothing for
4529 us to do here -- just return to the event loop. */
4530 ;
32c1e744
VP
4531 else
4532 /* In sync mode, when execute_control_command returns
4533 we're already standing on the next breakpoint.
347bddb7
PA
4534 Breakpoint commands for that stop were not run, since
4535 execute_command does not run breakpoint commands --
4536 only command_line_handler does, but that one is not
4537 involved in execution of breakpoint commands. So, we
4538 can now execute breakpoint commands. It should be
4539 noted that making execute_command do bpstat actions is
4540 not an option -- in this case we'll have recursive
4541 invocation of bpstat for each breakpoint with a
4542 command, and can easily blow up GDB stack. Instead, we
4543 return true, which will trigger the caller to recall us
4544 with the new stop_bpstat. */
4545 again = 1;
4546 break;
32c1e744 4547 }
c906108c 4548 }
347bddb7
PA
4549 return again;
4550}
4551
4552void
4553bpstat_do_actions (void)
4554{
353d1d73
JK
4555 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4556
347bddb7
PA
4557 /* Do any commands attached to breakpoint we are stopped at. */
4558 while (!ptid_equal (inferior_ptid, null_ptid)
4559 && target_has_execution
4560 && !is_exited (inferior_ptid)
4561 && !is_executing (inferior_ptid))
4562 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4563 and only return when it is stopped at the next breakpoint, we
4564 keep doing breakpoint actions until it returns false to
4565 indicate the inferior was not resumed. */
16c381f0 4566 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4567 break;
353d1d73
JK
4568
4569 discard_cleanups (cleanup_if_error);
c906108c
SS
4570}
4571
fa4727a6
DJ
4572/* Print out the (old or new) value associated with a watchpoint. */
4573
4574static void
4575watchpoint_value_print (struct value *val, struct ui_file *stream)
4576{
4577 if (val == NULL)
4578 fprintf_unfiltered (stream, _("<unreadable>"));
4579 else
79a45b7d
TT
4580 {
4581 struct value_print_options opts;
4582 get_user_print_options (&opts);
4583 value_print (val, stream, &opts);
4584 }
fa4727a6
DJ
4585}
4586
f303dbd6
PA
4587/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4588 debugging multiple threads. */
4589
4590void
4591maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4592{
112e8700 4593 if (uiout->is_mi_like_p ())
f303dbd6
PA
4594 return;
4595
112e8700 4596 uiout->text ("\n");
f303dbd6
PA
4597
4598 if (show_thread_that_caused_stop ())
4599 {
4600 const char *name;
4601 struct thread_info *thr = inferior_thread ();
4602
112e8700
SM
4603 uiout->text ("Thread ");
4604 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4605
4606 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4607 if (name != NULL)
4608 {
112e8700
SM
4609 uiout->text (" \"");
4610 uiout->field_fmt ("name", "%s", name);
4611 uiout->text ("\"");
f303dbd6
PA
4612 }
4613
112e8700 4614 uiout->text (" hit ");
f303dbd6
PA
4615 }
4616}
4617
e514a9d6 4618/* Generic routine for printing messages indicating why we
4a64f543 4619 stopped. The behavior of this function depends on the value
e514a9d6
JM
4620 'print_it' in the bpstat structure. Under some circumstances we
4621 may decide not to print anything here and delegate the task to
4a64f543 4622 normal_stop(). */
e514a9d6
JM
4623
4624static enum print_stop_action
4625print_bp_stop_message (bpstat bs)
4626{
4627 switch (bs->print_it)
4628 {
4629 case print_it_noop:
4a64f543 4630 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4631 return PRINT_UNKNOWN;
4632 break;
4633
4634 case print_it_done:
4635 /* We still want to print the frame, but we already printed the
4a64f543 4636 relevant messages. */
e514a9d6
JM
4637 return PRINT_SRC_AND_LOC;
4638 break;
4639
4640 case print_it_normal:
4f8d1dc6 4641 {
f431efe5
PA
4642 struct breakpoint *b = bs->breakpoint_at;
4643
1a6a67de
TJB
4644 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4645 which has since been deleted. */
4646 if (b == NULL)
4647 return PRINT_UNKNOWN;
4648
348d480f
PA
4649 /* Normal case. Call the breakpoint's print_it method. */
4650 return b->ops->print_it (bs);
4f8d1dc6 4651 }
348d480f 4652 break;
3086aeae 4653
e514a9d6 4654 default:
8e65ff28 4655 internal_error (__FILE__, __LINE__,
e2e0b3e5 4656 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4657 break;
c906108c 4658 }
c906108c
SS
4659}
4660
edcc5120
TT
4661/* A helper function that prints a shared library stopped event. */
4662
4663static void
4664print_solib_event (int is_catchpoint)
4665{
4666 int any_deleted
4667 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4668 int any_added
4669 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4670
4671 if (!is_catchpoint)
4672 {
4673 if (any_added || any_deleted)
112e8700 4674 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4675 else
112e8700
SM
4676 current_uiout->text (_("Stopped due to shared library event (no "
4677 "libraries added or removed)\n"));
edcc5120
TT
4678 }
4679
112e8700
SM
4680 if (current_uiout->is_mi_like_p ())
4681 current_uiout->field_string ("reason",
4682 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4683
4684 if (any_deleted)
4685 {
edcc5120
TT
4686 char *name;
4687 int ix;
4688
112e8700 4689 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4690 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4691 for (ix = 0;
4692 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4693 ix, name);
4694 ++ix)
4695 {
4696 if (ix > 0)
112e8700
SM
4697 current_uiout->text (" ");
4698 current_uiout->field_string ("library", name);
4699 current_uiout->text ("\n");
edcc5120 4700 }
edcc5120
TT
4701 }
4702
4703 if (any_added)
4704 {
4705 struct so_list *iter;
4706 int ix;
edcc5120 4707
112e8700 4708 current_uiout->text (_(" Inferior loaded "));
10f489e5 4709 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4710 for (ix = 0;
4711 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4712 ix, iter);
4713 ++ix)
4714 {
4715 if (ix > 0)
112e8700
SM
4716 current_uiout->text (" ");
4717 current_uiout->field_string ("library", iter->so_name);
4718 current_uiout->text ("\n");
edcc5120 4719 }
edcc5120
TT
4720 }
4721}
4722
e514a9d6
JM
4723/* Print a message indicating what happened. This is called from
4724 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4725 list - a list of the eventpoints that caused this stop. KIND is
4726 the target_waitkind for the stopping event. This
e514a9d6
JM
4727 routine calls the generic print routine for printing a message
4728 about reasons for stopping. This will print (for example) the
4729 "Breakpoint n," part of the output. The return value of this
4730 routine is one of:
c906108c 4731
4a64f543 4732 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4733 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4734 code to print the location. An example is
c5aa993b
JM
4735 "Breakpoint 1, " which should be followed by
4736 the location.
917317f4 4737 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4738 to also print the location part of the message.
4739 An example is the catch/throw messages, which
4a64f543 4740 don't require a location appended to the end.
917317f4 4741 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4742 further info to be printed. */
c906108c 4743
917317f4 4744enum print_stop_action
36dfb11c 4745bpstat_print (bpstat bs, int kind)
c906108c 4746{
f486487f 4747 enum print_stop_action val;
c5aa993b 4748
c906108c 4749 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4750 (Currently all watchpoints go on the bpstat whether hit or not.
4751 That probably could (should) be changed, provided care is taken
c906108c 4752 with respect to bpstat_explains_signal). */
e514a9d6
JM
4753 for (; bs; bs = bs->next)
4754 {
4755 val = print_bp_stop_message (bs);
4756 if (val == PRINT_SRC_ONLY
4757 || val == PRINT_SRC_AND_LOC
4758 || val == PRINT_NOTHING)
4759 return val;
4760 }
c906108c 4761
36dfb11c
TT
4762 /* If we had hit a shared library event breakpoint,
4763 print_bp_stop_message would print out this message. If we hit an
4764 OS-level shared library event, do the same thing. */
4765 if (kind == TARGET_WAITKIND_LOADED)
4766 {
edcc5120 4767 print_solib_event (0);
36dfb11c
TT
4768 return PRINT_NOTHING;
4769 }
4770
e514a9d6 4771 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4772 with and nothing was printed. */
917317f4 4773 return PRINT_UNKNOWN;
c906108c
SS
4774}
4775
bf469271 4776/* Evaluate the boolean expression EXP and return the result. */
c906108c 4777
bf469271
PA
4778static bool
4779breakpoint_cond_eval (expression *exp)
c906108c 4780{
278cd55f 4781 struct value *mark = value_mark ();
bf469271 4782 bool res = value_true (evaluate_expression (exp));
cc59ec59 4783
c906108c 4784 value_free_to_mark (mark);
bf469271 4785 return res;
c906108c
SS
4786}
4787
5760d0ab 4788/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4789
04afa70c
TT
4790bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4791 : next (NULL),
4792 bp_location_at (bl),
4793 breakpoint_at (bl->owner),
4794 commands (NULL),
4795 old_val (NULL),
4796 print (0),
4797 stop (0),
4798 print_it (print_it_normal)
c906108c 4799{
f431efe5 4800 incref_bp_location (bl);
04afa70c
TT
4801 **bs_link_pointer = this;
4802 *bs_link_pointer = &next;
4803}
4804
4805bpstats::bpstats ()
4806 : next (NULL),
4807 bp_location_at (NULL),
4808 breakpoint_at (NULL),
4809 commands (NULL),
4810 old_val (NULL),
4811 print (0),
4812 stop (0),
4813 print_it (print_it_normal)
4814{
c906108c
SS
4815}
4816\f
d983da9c
DJ
4817/* The target has stopped with waitstatus WS. Check if any hardware
4818 watchpoints have triggered, according to the target. */
4819
4820int
4821watchpoints_triggered (struct target_waitstatus *ws)
4822{
d92524f1 4823 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4824 CORE_ADDR addr;
4825 struct breakpoint *b;
4826
4827 if (!stopped_by_watchpoint)
4828 {
4829 /* We were not stopped by a watchpoint. Mark all watchpoints
4830 as not triggered. */
4831 ALL_BREAKPOINTS (b)
cc60f2e3 4832 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4833 {
4834 struct watchpoint *w = (struct watchpoint *) b;
4835
4836 w->watchpoint_triggered = watch_triggered_no;
4837 }
d983da9c
DJ
4838
4839 return 0;
4840 }
4841
4842 if (!target_stopped_data_address (&current_target, &addr))
4843 {
4844 /* We were stopped by a watchpoint, but we don't know where.
4845 Mark all watchpoints as unknown. */
4846 ALL_BREAKPOINTS (b)
cc60f2e3 4847 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4848 {
4849 struct watchpoint *w = (struct watchpoint *) b;
4850
4851 w->watchpoint_triggered = watch_triggered_unknown;
4852 }
d983da9c 4853
3c4797ba 4854 return 1;
d983da9c
DJ
4855 }
4856
4857 /* The target could report the data address. Mark watchpoints
4858 affected by this data address as triggered, and all others as not
4859 triggered. */
4860
4861 ALL_BREAKPOINTS (b)
cc60f2e3 4862 if (is_hardware_watchpoint (b))
d983da9c 4863 {
3a5c3e22 4864 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4865 struct bp_location *loc;
d983da9c 4866
3a5c3e22 4867 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4868 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4869 {
3a5c3e22 4870 if (is_masked_watchpoint (b))
9c06b0b4 4871 {
3a5c3e22
PA
4872 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4873 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4874
4875 if (newaddr == start)
4876 {
3a5c3e22 4877 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4878 break;
4879 }
4880 }
4881 /* Exact match not required. Within range is sufficient. */
4882 else if (target_watchpoint_addr_within_range (&current_target,
4883 addr, loc->address,
4884 loc->length))
4885 {
3a5c3e22 4886 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4887 break;
4888 }
4889 }
d983da9c
DJ
4890 }
4891
4892 return 1;
4893}
4894
bf469271
PA
4895/* Possible return values for watchpoint_check. */
4896enum wp_check_result
4897 {
4898 /* The watchpoint has been deleted. */
4899 WP_DELETED = 1,
4900
4901 /* The value has changed. */
4902 WP_VALUE_CHANGED = 2,
4903
4904 /* The value has not changed. */
4905 WP_VALUE_NOT_CHANGED = 3,
4906
4907 /* Ignore this watchpoint, no matter if the value changed or not. */
4908 WP_IGNORE = 4,
4909 };
c906108c
SS
4910
4911#define BP_TEMPFLAG 1
4912#define BP_HARDWAREFLAG 2
4913
4a64f543 4914/* Evaluate watchpoint condition expression and check if its value
bf469271 4915 changed. */
553e4c11 4916
bf469271
PA
4917static wp_check_result
4918watchpoint_check (bpstat bs)
c906108c 4919{
3a5c3e22 4920 struct watchpoint *b;
c906108c
SS
4921 struct frame_info *fr;
4922 int within_current_scope;
4923
f431efe5 4924 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4925 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4926 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4927
f6bc2008
PA
4928 /* If this is a local watchpoint, we only want to check if the
4929 watchpoint frame is in scope if the current thread is the thread
4930 that was used to create the watchpoint. */
4931 if (!watchpoint_in_thread_scope (b))
60e1c644 4932 return WP_IGNORE;
f6bc2008 4933
c906108c
SS
4934 if (b->exp_valid_block == NULL)
4935 within_current_scope = 1;
4936 else
4937 {
edb3359d
DJ
4938 struct frame_info *frame = get_current_frame ();
4939 struct gdbarch *frame_arch = get_frame_arch (frame);
4940 CORE_ADDR frame_pc = get_frame_pc (frame);
4941
c9cf6e20 4942 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4943 still in the function but the stack frame has already been
4944 invalidated. Since we can't rely on the values of local
4945 variables after the stack has been destroyed, we are treating
4946 the watchpoint in that state as `not changed' without further
4947 checking. Don't mark watchpoints as changed if the current
4948 frame is in an epilogue - even if they are in some other
4949 frame, our view of the stack is likely to be wrong and
4950 frame_find_by_id could error out. */
c9cf6e20 4951 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4952 return WP_IGNORE;
a0f49112 4953
101dcfbe 4954 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4955 within_current_scope = (fr != NULL);
69fbadd5
DJ
4956
4957 /* If we've gotten confused in the unwinder, we might have
4958 returned a frame that can't describe this variable. */
edb3359d
DJ
4959 if (within_current_scope)
4960 {
4961 struct symbol *function;
4962
4963 function = get_frame_function (fr);
4964 if (function == NULL
4965 || !contained_in (b->exp_valid_block,
4966 SYMBOL_BLOCK_VALUE (function)))
4967 within_current_scope = 0;
4968 }
69fbadd5 4969
edb3359d 4970 if (within_current_scope)
c906108c
SS
4971 /* If we end up stopping, the current frame will get selected
4972 in normal_stop. So this call to select_frame won't affect
4973 the user. */
0f7d239c 4974 select_frame (fr);
c906108c 4975 }
c5aa993b 4976
c906108c
SS
4977 if (within_current_scope)
4978 {
4a64f543
MS
4979 /* We use value_{,free_to_}mark because it could be a *long*
4980 time before we return to the command level and call
4981 free_all_values. We can't call free_all_values because we
4982 might be in the middle of evaluating a function call. */
c906108c 4983
0cf6dd15 4984 int pc = 0;
9c06b0b4 4985 struct value *mark;
fa4727a6
DJ
4986 struct value *new_val;
4987
c1fc2657 4988 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4989 /* Since we don't know the exact trigger address (from
4990 stopped_data_address), just tell the user we've triggered
4991 a mask watchpoint. */
4992 return WP_VALUE_CHANGED;
4993
4994 mark = value_mark ();
4d01a485 4995 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4996
bb9d5f81
PP
4997 if (b->val_bitsize != 0)
4998 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4999
4a64f543
MS
5000 /* We use value_equal_contents instead of value_equal because
5001 the latter coerces an array to a pointer, thus comparing just
5002 the address of the array instead of its contents. This is
5003 not what we want. */
fa4727a6 5004 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5005 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5006 {
fa4727a6
DJ
5007 if (new_val != NULL)
5008 {
5009 release_value (new_val);
5010 value_free_to_mark (mark);
5011 }
c906108c
SS
5012 bs->old_val = b->val;
5013 b->val = new_val;
fa4727a6 5014 b->val_valid = 1;
c906108c
SS
5015 return WP_VALUE_CHANGED;
5016 }
5017 else
5018 {
60e1c644 5019 /* Nothing changed. */
c906108c 5020 value_free_to_mark (mark);
c906108c
SS
5021 return WP_VALUE_NOT_CHANGED;
5022 }
5023 }
5024 else
5025 {
5026 /* This seems like the only logical thing to do because
c5aa993b
JM
5027 if we temporarily ignored the watchpoint, then when
5028 we reenter the block in which it is valid it contains
5029 garbage (in the case of a function, it may have two
5030 garbage values, one before and one after the prologue).
5031 So we can't even detect the first assignment to it and
5032 watch after that (since the garbage may or may not equal
5033 the first value assigned). */
348d480f
PA
5034 /* We print all the stop information in
5035 breakpoint_ops->print_it, but in this case, by the time we
5036 call breakpoint_ops->print_it this bp will be deleted
5037 already. So we have no choice but print the information
5038 here. */
468afe6c 5039
0e454242 5040 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5041 {
5042 struct ui_out *uiout = current_uiout;
5043
112e8700
SM
5044 if (uiout->is_mi_like_p ())
5045 uiout->field_string
5046 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5047 uiout->text ("\nWatchpoint ");
c1fc2657 5048 uiout->field_int ("wpnum", b->number);
112e8700 5049 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5050 "which its expression is valid.\n");
5051 }
4ce44c66 5052
cdac0397 5053 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5054 b->commands = NULL;
d0fb5eae 5055 watchpoint_del_at_next_stop (b);
c906108c
SS
5056
5057 return WP_DELETED;
5058 }
5059}
5060
18a18393 5061/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5062 breakpoint location BL. This function does not check if we should
5063 stop, only if BL explains the stop. */
5064
18a18393 5065static int
6c95b8df 5066bpstat_check_location (const struct bp_location *bl,
accd0bcd 5067 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5068 const struct target_waitstatus *ws)
18a18393
VP
5069{
5070 struct breakpoint *b = bl->owner;
5071
348d480f 5072 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5073 gdb_assert (b != NULL);
5074
bd522513 5075 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5076}
5077
3a5c3e22
PA
5078/* Determine if the watched values have actually changed, and we
5079 should stop. If not, set BS->stop to 0. */
5080
18a18393
VP
5081static void
5082bpstat_check_watchpoint (bpstat bs)
5083{
2bdf28a0 5084 const struct bp_location *bl;
3a5c3e22 5085 struct watchpoint *b;
2bdf28a0
JK
5086
5087 /* BS is built for existing struct breakpoint. */
f431efe5 5088 bl = bs->bp_location_at;
2bdf28a0 5089 gdb_assert (bl != NULL);
3a5c3e22 5090 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5091 gdb_assert (b != NULL);
18a18393 5092
18a18393 5093 {
18a18393
VP
5094 int must_check_value = 0;
5095
c1fc2657 5096 if (b->type == bp_watchpoint)
18a18393
VP
5097 /* For a software watchpoint, we must always check the
5098 watched value. */
5099 must_check_value = 1;
5100 else if (b->watchpoint_triggered == watch_triggered_yes)
5101 /* We have a hardware watchpoint (read, write, or access)
5102 and the target earlier reported an address watched by
5103 this watchpoint. */
5104 must_check_value = 1;
5105 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5106 && b->type == bp_hardware_watchpoint)
18a18393
VP
5107 /* We were stopped by a hardware watchpoint, but the target could
5108 not report the data address. We must check the watchpoint's
5109 value. Access and read watchpoints are out of luck; without
5110 a data address, we can't figure it out. */
5111 must_check_value = 1;
3a5c3e22 5112
18a18393
VP
5113 if (must_check_value)
5114 {
bf469271
PA
5115 wp_check_result e;
5116
5117 TRY
5118 {
5119 e = watchpoint_check (bs);
5120 }
5121 CATCH (ex, RETURN_MASK_ALL)
5122 {
5123 exception_fprintf (gdb_stderr, ex,
5124 "Error evaluating expression "
5125 "for watchpoint %d\n",
5126 b->number);
5127
5128 SWITCH_THRU_ALL_UIS ()
5129 {
5130 printf_filtered (_("Watchpoint %d deleted.\n"),
5131 b->number);
5132 }
5133 watchpoint_del_at_next_stop (b);
5134 e = WP_DELETED;
5135 }
5136 END_CATCH
5137
18a18393
VP
5138 switch (e)
5139 {
5140 case WP_DELETED:
5141 /* We've already printed what needs to be printed. */
5142 bs->print_it = print_it_done;
5143 /* Stop. */
5144 break;
60e1c644
PA
5145 case WP_IGNORE:
5146 bs->print_it = print_it_noop;
5147 bs->stop = 0;
5148 break;
18a18393 5149 case WP_VALUE_CHANGED:
c1fc2657 5150 if (b->type == bp_read_watchpoint)
18a18393 5151 {
85d721b8
PA
5152 /* There are two cases to consider here:
5153
4a64f543 5154 1. We're watching the triggered memory for reads.
85d721b8
PA
5155 In that case, trust the target, and always report
5156 the watchpoint hit to the user. Even though
5157 reads don't cause value changes, the value may
5158 have changed since the last time it was read, and
5159 since we're not trapping writes, we will not see
5160 those, and as such we should ignore our notion of
5161 old value.
5162
4a64f543 5163 2. We're watching the triggered memory for both
85d721b8
PA
5164 reads and writes. There are two ways this may
5165 happen:
5166
4a64f543 5167 2.1. This is a target that can't break on data
85d721b8
PA
5168 reads only, but can break on accesses (reads or
5169 writes), such as e.g., x86. We detect this case
5170 at the time we try to insert read watchpoints.
5171
4a64f543 5172 2.2. Otherwise, the target supports read
85d721b8
PA
5173 watchpoints, but, the user set an access or write
5174 watchpoint watching the same memory as this read
5175 watchpoint.
5176
5177 If we're watching memory writes as well as reads,
5178 ignore watchpoint hits when we find that the
5179 value hasn't changed, as reads don't cause
5180 changes. This still gives false positives when
5181 the program writes the same value to memory as
5182 what there was already in memory (we will confuse
5183 it for a read), but it's much better than
5184 nothing. */
5185
5186 int other_write_watchpoint = 0;
5187
5188 if (bl->watchpoint_type == hw_read)
5189 {
5190 struct breakpoint *other_b;
5191
5192 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5193 if (other_b->type == bp_hardware_watchpoint
5194 || other_b->type == bp_access_watchpoint)
85d721b8 5195 {
3a5c3e22
PA
5196 struct watchpoint *other_w =
5197 (struct watchpoint *) other_b;
5198
5199 if (other_w->watchpoint_triggered
5200 == watch_triggered_yes)
5201 {
5202 other_write_watchpoint = 1;
5203 break;
5204 }
85d721b8
PA
5205 }
5206 }
5207
5208 if (other_write_watchpoint
5209 || bl->watchpoint_type == hw_access)
5210 {
5211 /* We're watching the same memory for writes,
5212 and the value changed since the last time we
5213 updated it, so this trap must be for a write.
5214 Ignore it. */
5215 bs->print_it = print_it_noop;
5216 bs->stop = 0;
5217 }
18a18393
VP
5218 }
5219 break;
5220 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5221 if (b->type == bp_hardware_watchpoint
5222 || b->type == bp_watchpoint)
18a18393
VP
5223 {
5224 /* Don't stop: write watchpoints shouldn't fire if
5225 the value hasn't changed. */
5226 bs->print_it = print_it_noop;
5227 bs->stop = 0;
5228 }
5229 /* Stop. */
5230 break;
5231 default:
5232 /* Can't happen. */
18a18393
VP
5233 break;
5234 }
5235 }
5236 else /* must_check_value == 0 */
5237 {
5238 /* This is a case where some watchpoint(s) triggered, but
5239 not at the address of this watchpoint, or else no
5240 watchpoint triggered after all. So don't print
5241 anything for this watchpoint. */
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 }
5245 }
5246}
5247
7d4df6a4
DE
5248/* For breakpoints that are currently marked as telling gdb to stop,
5249 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5250 of breakpoint referred to by BS. If we should not stop for this
5251 breakpoint, set BS->stop to 0. */
f431efe5 5252
18a18393
VP
5253static void
5254bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5255{
2bdf28a0
JK
5256 const struct bp_location *bl;
5257 struct breakpoint *b;
bf469271
PA
5258 /* Assume stop. */
5259 bool condition_result = true;
7d4df6a4
DE
5260 struct expression *cond;
5261
5262 gdb_assert (bs->stop);
2bdf28a0
JK
5263
5264 /* BS is built for existing struct breakpoint. */
f431efe5 5265 bl = bs->bp_location_at;
2bdf28a0 5266 gdb_assert (bl != NULL);
f431efe5 5267 b = bs->breakpoint_at;
2bdf28a0 5268 gdb_assert (b != NULL);
18a18393 5269
b775012e
LM
5270 /* Even if the target evaluated the condition on its end and notified GDB, we
5271 need to do so again since GDB does not know if we stopped due to a
5272 breakpoint or a single step breakpoint. */
5273
18a18393 5274 if (frame_id_p (b->frame_id)
edb3359d 5275 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5276 {
7d4df6a4
DE
5277 bs->stop = 0;
5278 return;
5279 }
60e1c644 5280
12ab52e9
PA
5281 /* If this is a thread/task-specific breakpoint, don't waste cpu
5282 evaluating the condition if this isn't the specified
5283 thread/task. */
5d5658a1 5284 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5285 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5286
6c1b0f7b
DE
5287 {
5288 bs->stop = 0;
5289 return;
5290 }
5291
6dddc817
DE
5292 /* Evaluate extension language breakpoints that have a "stop" method
5293 implemented. */
5294 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5295
7d4df6a4
DE
5296 if (is_watchpoint (b))
5297 {
5298 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5299
4d01a485 5300 cond = w->cond_exp.get ();
7d4df6a4
DE
5301 }
5302 else
4d01a485 5303 cond = bl->cond.get ();
60e1c644 5304
7d4df6a4
DE
5305 if (cond && b->disposition != disp_del_at_next_stop)
5306 {
5307 int within_current_scope = 1;
5308 struct watchpoint * w;
60e1c644 5309
7d4df6a4
DE
5310 /* We use value_mark and value_free_to_mark because it could
5311 be a long time before we return to the command level and
5312 call free_all_values. We can't call free_all_values
5313 because we might be in the middle of evaluating a
5314 function call. */
5315 struct value *mark = value_mark ();
5316
5317 if (is_watchpoint (b))
5318 w = (struct watchpoint *) b;
5319 else
5320 w = NULL;
5321
5322 /* Need to select the frame, with all that implies so that
5323 the conditions will have the right context. Because we
5324 use the frame, we will not see an inlined function's
5325 variables when we arrive at a breakpoint at the start
5326 of the inlined function; the current frame will be the
5327 call site. */
5328 if (w == NULL || w->cond_exp_valid_block == NULL)
5329 select_frame (get_current_frame ());
5330 else
18a18393 5331 {
7d4df6a4
DE
5332 struct frame_info *frame;
5333
5334 /* For local watchpoint expressions, which particular
5335 instance of a local is being watched matters, so we
5336 keep track of the frame to evaluate the expression
5337 in. To evaluate the condition however, it doesn't
5338 really matter which instantiation of the function
5339 where the condition makes sense triggers the
5340 watchpoint. This allows an expression like "watch
5341 global if q > 10" set in `func', catch writes to
5342 global on all threads that call `func', or catch
5343 writes on all recursive calls of `func' by a single
5344 thread. We simply always evaluate the condition in
5345 the innermost frame that's executing where it makes
5346 sense to evaluate the condition. It seems
5347 intuitive. */
5348 frame = block_innermost_frame (w->cond_exp_valid_block);
5349 if (frame != NULL)
5350 select_frame (frame);
5351 else
5352 within_current_scope = 0;
18a18393 5353 }
7d4df6a4 5354 if (within_current_scope)
bf469271
PA
5355 {
5356 TRY
5357 {
5358 condition_result = breakpoint_cond_eval (cond);
5359 }
5360 CATCH (ex, RETURN_MASK_ALL)
5361 {
5362 exception_fprintf (gdb_stderr, ex,
5363 "Error in testing breakpoint condition:\n");
5364 }
5365 END_CATCH
5366 }
7d4df6a4 5367 else
18a18393 5368 {
7d4df6a4
DE
5369 warning (_("Watchpoint condition cannot be tested "
5370 "in the current scope"));
5371 /* If we failed to set the right context for this
5372 watchpoint, unconditionally report it. */
18a18393 5373 }
7d4df6a4
DE
5374 /* FIXME-someday, should give breakpoint #. */
5375 value_free_to_mark (mark);
18a18393 5376 }
7d4df6a4 5377
bf469271 5378 if (cond && !condition_result)
7d4df6a4
DE
5379 {
5380 bs->stop = 0;
5381 }
7d4df6a4
DE
5382 else if (b->ignore_count > 0)
5383 {
5384 b->ignore_count--;
5385 bs->stop = 0;
5386 /* Increase the hit count even though we don't stop. */
5387 ++(b->hit_count);
5388 observer_notify_breakpoint_modified (b);
5389 }
18a18393
VP
5390}
5391
1cf4d951
PA
5392/* Returns true if we need to track moribund locations of LOC's type
5393 on the current target. */
5394
5395static int
5396need_moribund_for_location_type (struct bp_location *loc)
5397{
5398 return ((loc->loc_type == bp_loc_software_breakpoint
5399 && !target_supports_stopped_by_sw_breakpoint ())
5400 || (loc->loc_type == bp_loc_hardware_breakpoint
5401 && !target_supports_stopped_by_hw_breakpoint ()));
5402}
5403
18a18393 5404
9709f61c 5405/* Get a bpstat associated with having just stopped at address
d983da9c 5406 BP_ADDR in thread PTID.
c906108c 5407
d983da9c 5408 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5409 don't understand this stop. Result is a chain of bpstat's such
5410 that:
c906108c 5411
c5aa993b 5412 if we don't understand the stop, the result is a null pointer.
c906108c 5413
c5aa993b 5414 if we understand why we stopped, the result is not null.
c906108c 5415
c5aa993b
JM
5416 Each element of the chain refers to a particular breakpoint or
5417 watchpoint at which we have stopped. (We may have stopped for
5418 several reasons concurrently.)
c906108c 5419
c5aa993b
JM
5420 Each element of the chain has valid next, breakpoint_at,
5421 commands, FIXME??? fields. */
c906108c
SS
5422
5423bpstat
accd0bcd 5424bpstat_stop_status (const address_space *aspace,
09ac7c10
TT
5425 CORE_ADDR bp_addr, ptid_t ptid,
5426 const struct target_waitstatus *ws)
c906108c 5427{
0d381245 5428 struct breakpoint *b = NULL;
afe38095 5429 struct bp_location *bl;
20874c92 5430 struct bp_location *loc;
5760d0ab
JK
5431 /* First item of allocated bpstat's. */
5432 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5433 /* Pointer to the last thing in the chain currently. */
5760d0ab 5434 bpstat bs;
20874c92 5435 int ix;
429374b8 5436 int need_remove_insert;
f431efe5 5437 int removed_any;
c906108c 5438
f431efe5
PA
5439 /* First, build the bpstat chain with locations that explain a
5440 target stop, while being careful to not set the target running,
5441 as that may invalidate locations (in particular watchpoint
5442 locations are recreated). Resuming will happen here with
5443 breakpoint conditions or watchpoint expressions that include
5444 inferior function calls. */
c5aa993b 5445
429374b8
JK
5446 ALL_BREAKPOINTS (b)
5447 {
1a853c52 5448 if (!breakpoint_enabled (b))
429374b8 5449 continue;
a5606eee 5450
429374b8
JK
5451 for (bl = b->loc; bl != NULL; bl = bl->next)
5452 {
4a64f543
MS
5453 /* For hardware watchpoints, we look only at the first
5454 location. The watchpoint_check function will work on the
5455 entire expression, not the individual locations. For
5456 read watchpoints, the watchpoints_triggered function has
5457 checked all locations already. */
429374b8
JK
5458 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5459 break;
18a18393 5460
f6592439 5461 if (!bl->enabled || bl->shlib_disabled)
429374b8 5462 continue;
c5aa993b 5463
09ac7c10 5464 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5465 continue;
c5aa993b 5466
4a64f543
MS
5467 /* Come here if it's a watchpoint, or if the break address
5468 matches. */
c5aa993b 5469
04afa70c 5470 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5471 explain stop. */
c5aa993b 5472
f431efe5
PA
5473 /* Assume we stop. Should we find a watchpoint that is not
5474 actually triggered, or if the condition of the breakpoint
5475 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5476 bs->stop = 1;
5477 bs->print = 1;
d983da9c 5478
f431efe5
PA
5479 /* If this is a scope breakpoint, mark the associated
5480 watchpoint as triggered so that we will handle the
5481 out-of-scope event. We'll get to the watchpoint next
5482 iteration. */
d0fb5eae 5483 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5484 {
5485 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5486
5487 w->watchpoint_triggered = watch_triggered_yes;
5488 }
f431efe5
PA
5489 }
5490 }
5491
7c16b83e 5492 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5493 if (!target_supports_stopped_by_sw_breakpoint ()
5494 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5495 {
1cf4d951 5496 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5497 {
1cf4d951
PA
5498 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5499 && need_moribund_for_location_type (loc))
5500 {
04afa70c 5501 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5502 /* For hits of moribund locations, we should just proceed. */
5503 bs->stop = 0;
5504 bs->print = 0;
5505 bs->print_it = print_it_noop;
5506 }
f431efe5
PA
5507 }
5508 }
5509
edcc5120
TT
5510 /* A bit of special processing for shlib breakpoints. We need to
5511 process solib loading here, so that the lists of loaded and
5512 unloaded libraries are correct before we handle "catch load" and
5513 "catch unload". */
5514 for (bs = bs_head; bs != NULL; bs = bs->next)
5515 {
5d268276 5516 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5517 {
5518 handle_solib_event ();
5519 break;
5520 }
5521 }
5522
f431efe5
PA
5523 /* Now go through the locations that caused the target to stop, and
5524 check whether we're interested in reporting this stop to higher
5525 layers, or whether we should resume the target transparently. */
5526
5527 removed_any = 0;
5528
5760d0ab 5529 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5530 {
5531 if (!bs->stop)
5532 continue;
5533
f431efe5 5534 b = bs->breakpoint_at;
348d480f
PA
5535 b->ops->check_status (bs);
5536 if (bs->stop)
28010a5d 5537 {
348d480f 5538 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5539
429374b8
JK
5540 if (bs->stop)
5541 {
5542 ++(b->hit_count);
8d3788bd 5543 observer_notify_breakpoint_modified (b);
c906108c 5544
4a64f543 5545 /* We will stop here. */
429374b8
JK
5546 if (b->disposition == disp_disable)
5547 {
816338b5 5548 --(b->enable_count);
1a853c52 5549 if (b->enable_count <= 0)
429374b8 5550 b->enable_state = bp_disabled;
f431efe5 5551 removed_any = 1;
429374b8
JK
5552 }
5553 if (b->silent)
5554 bs->print = 0;
5555 bs->commands = b->commands;
abf85f46 5556 if (command_line_is_silent (bs->commands
d1b0a7bf 5557 ? bs->commands.get () : NULL))
abf85f46 5558 bs->print = 0;
9d6e6e84
HZ
5559
5560 b->ops->after_condition_true (bs);
429374b8
JK
5561 }
5562
348d480f 5563 }
a9b3a50f
PA
5564
5565 /* Print nothing for this entry if we don't stop or don't
5566 print. */
5567 if (!bs->stop || !bs->print)
5568 bs->print_it = print_it_noop;
429374b8 5569 }
876fa593 5570
d983da9c
DJ
5571 /* If we aren't stopping, the value of some hardware watchpoint may
5572 not have changed, but the intermediate memory locations we are
5573 watching may have. Don't bother if we're stopping; this will get
5574 done later. */
d832cb68 5575 need_remove_insert = 0;
5760d0ab
JK
5576 if (! bpstat_causes_stop (bs_head))
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5578 if (!bs->stop
f431efe5
PA
5579 && bs->breakpoint_at
5580 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5581 {
3a5c3e22
PA
5582 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5583
5584 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5585 need_remove_insert = 1;
d983da9c
DJ
5586 }
5587
d832cb68 5588 if (need_remove_insert)
44702360 5589 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5590 else if (removed_any)
44702360 5591 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5592
5760d0ab 5593 return bs_head;
c906108c 5594}
628fe4e4
JK
5595
5596static void
5597handle_jit_event (void)
5598{
5599 struct frame_info *frame;
5600 struct gdbarch *gdbarch;
5601
243a9253
PA
5602 if (debug_infrun)
5603 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5604
628fe4e4
JK
5605 /* Switch terminal for any messages produced by
5606 breakpoint_re_set. */
223ffa71 5607 target_terminal::ours_for_output ();
628fe4e4
JK
5608
5609 frame = get_current_frame ();
5610 gdbarch = get_frame_arch (frame);
5611
5612 jit_event_handler (gdbarch);
5613
223ffa71 5614 target_terminal::inferior ();
628fe4e4
JK
5615}
5616
5617/* Prepare WHAT final decision for infrun. */
5618
5619/* Decide what infrun needs to do with this bpstat. */
5620
c906108c 5621struct bpstat_what
0e30163f 5622bpstat_what (bpstat bs_head)
c906108c 5623{
c906108c 5624 struct bpstat_what retval;
0e30163f 5625 bpstat bs;
c906108c 5626
628fe4e4 5627 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5628 retval.call_dummy = STOP_NONE;
186c406b 5629 retval.is_longjmp = 0;
628fe4e4 5630
0e30163f 5631 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5632 {
628fe4e4
JK
5633 /* Extract this BS's action. After processing each BS, we check
5634 if its action overrides all we've seem so far. */
5635 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5636 enum bptype bptype;
5637
c906108c 5638 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5639 {
5640 /* I suspect this can happen if it was a momentary
5641 breakpoint which has since been deleted. */
5642 bptype = bp_none;
5643 }
20874c92 5644 else
f431efe5 5645 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5646
5647 switch (bptype)
c906108c
SS
5648 {
5649 case bp_none:
628fe4e4 5650 break;
c906108c
SS
5651 case bp_breakpoint:
5652 case bp_hardware_breakpoint:
7c16b83e 5653 case bp_single_step:
c906108c
SS
5654 case bp_until:
5655 case bp_finish:
a9b3a50f 5656 case bp_shlib_event:
c906108c
SS
5657 if (bs->stop)
5658 {
5659 if (bs->print)
628fe4e4 5660 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5661 else
628fe4e4 5662 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5663 }
5664 else
628fe4e4 5665 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5666 break;
5667 case bp_watchpoint:
5668 case bp_hardware_watchpoint:
5669 case bp_read_watchpoint:
5670 case bp_access_watchpoint:
5671 if (bs->stop)
5672 {
5673 if (bs->print)
628fe4e4 5674 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5675 else
628fe4e4 5676 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5677 }
5678 else
628fe4e4
JK
5679 {
5680 /* There was a watchpoint, but we're not stopping.
5681 This requires no further action. */
5682 }
c906108c
SS
5683 break;
5684 case bp_longjmp:
e2e4d78b 5685 case bp_longjmp_call_dummy:
186c406b 5686 case bp_exception:
0a39bb32
PA
5687 if (bs->stop)
5688 {
5689 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5690 retval.is_longjmp = bptype != bp_exception;
5691 }
5692 else
5693 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5694 break;
5695 case bp_longjmp_resume:
186c406b 5696 case bp_exception_resume:
0a39bb32
PA
5697 if (bs->stop)
5698 {
5699 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5700 retval.is_longjmp = bptype == bp_longjmp_resume;
5701 }
5702 else
5703 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5704 break;
5705 case bp_step_resume:
5706 if (bs->stop)
628fe4e4
JK
5707 this_action = BPSTAT_WHAT_STEP_RESUME;
5708 else
c906108c 5709 {
628fe4e4
JK
5710 /* It is for the wrong frame. */
5711 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5712 }
c906108c 5713 break;
2c03e5be
PA
5714 case bp_hp_step_resume:
5715 if (bs->stop)
5716 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5717 else
5718 {
5719 /* It is for the wrong frame. */
5720 this_action = BPSTAT_WHAT_SINGLE;
5721 }
5722 break;
c906108c 5723 case bp_watchpoint_scope:
c4093a6a 5724 case bp_thread_event:
1900040c 5725 case bp_overlay_event:
0fd8e87f 5726 case bp_longjmp_master:
aa7d318d 5727 case bp_std_terminate_master:
186c406b 5728 case bp_exception_master:
628fe4e4 5729 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5730 break;
ce78b96d 5731 case bp_catchpoint:
c5aa993b
JM
5732 if (bs->stop)
5733 {
5734 if (bs->print)
628fe4e4 5735 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5736 else
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5738 }
5739 else
628fe4e4
JK
5740 {
5741 /* There was a catchpoint, but we're not stopping.
5742 This requires no further action. */
5743 }
5744 break;
628fe4e4 5745 case bp_jit_event:
628fe4e4 5746 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5747 break;
c906108c 5748 case bp_call_dummy:
53a5351d
JM
5749 /* Make sure the action is stop (silent or noisy),
5750 so infrun.c pops the dummy frame. */
aa7d318d 5751 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5752 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5753 break;
5754 case bp_std_terminate:
5755 /* Make sure the action is stop (silent or noisy),
5756 so infrun.c pops the dummy frame. */
aa7d318d 5757 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5758 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5759 break;
1042e4c0 5760 case bp_tracepoint:
7a697b8d 5761 case bp_fast_tracepoint:
0fb4aa4b 5762 case bp_static_tracepoint:
1042e4c0
SS
5763 /* Tracepoint hits should not be reported back to GDB, and
5764 if one got through somehow, it should have been filtered
5765 out already. */
5766 internal_error (__FILE__, __LINE__,
7a697b8d 5767 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5768 break;
5769 case bp_gnu_ifunc_resolver:
5770 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5771 this_action = BPSTAT_WHAT_SINGLE;
5772 break;
5773 case bp_gnu_ifunc_resolver_return:
5774 /* The breakpoint will be removed, execution will restart from the
5775 PC of the former breakpoint. */
5776 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5777 break;
e7e0cddf
SS
5778
5779 case bp_dprintf:
a11cfd87
HZ
5780 if (bs->stop)
5781 this_action = BPSTAT_WHAT_STOP_SILENT;
5782 else
5783 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5784 break;
5785
628fe4e4
JK
5786 default:
5787 internal_error (__FILE__, __LINE__,
5788 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5789 }
628fe4e4 5790
325fac50 5791 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5792 }
628fe4e4 5793
243a9253
PA
5794 return retval;
5795}
628fe4e4 5796
243a9253
PA
5797void
5798bpstat_run_callbacks (bpstat bs_head)
5799{
5800 bpstat bs;
628fe4e4 5801
0e30163f
JK
5802 for (bs = bs_head; bs != NULL; bs = bs->next)
5803 {
5804 struct breakpoint *b = bs->breakpoint_at;
5805
5806 if (b == NULL)
5807 continue;
5808 switch (b->type)
5809 {
243a9253
PA
5810 case bp_jit_event:
5811 handle_jit_event ();
5812 break;
0e30163f
JK
5813 case bp_gnu_ifunc_resolver:
5814 gnu_ifunc_resolver_stop (b);
5815 break;
5816 case bp_gnu_ifunc_resolver_return:
5817 gnu_ifunc_resolver_return_stop (b);
5818 break;
5819 }
5820 }
c906108c
SS
5821}
5822
5823/* Nonzero if we should step constantly (e.g. watchpoints on machines
5824 without hardware support). This isn't related to a specific bpstat,
5825 just to things like whether watchpoints are set. */
5826
c5aa993b 5827int
fba45db2 5828bpstat_should_step (void)
c906108c
SS
5829{
5830 struct breakpoint *b;
cc59ec59 5831
c906108c 5832 ALL_BREAKPOINTS (b)
717a8278 5833 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5834 return 1;
c906108c
SS
5835 return 0;
5836}
5837
67822962
PA
5838int
5839bpstat_causes_stop (bpstat bs)
5840{
5841 for (; bs != NULL; bs = bs->next)
5842 if (bs->stop)
5843 return 1;
5844
5845 return 0;
5846}
5847
c906108c 5848\f
c5aa993b 5849
170b53b2
UW
5850/* Compute a string of spaces suitable to indent the next line
5851 so it starts at the position corresponding to the table column
5852 named COL_NAME in the currently active table of UIOUT. */
5853
5854static char *
5855wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5856{
5857 static char wrap_indent[80];
5858 int i, total_width, width, align;
c5209615 5859 const char *text;
170b53b2
UW
5860
5861 total_width = 0;
112e8700 5862 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5863 {
5864 if (strcmp (text, col_name) == 0)
5865 {
5866 gdb_assert (total_width < sizeof wrap_indent);
5867 memset (wrap_indent, ' ', total_width);
5868 wrap_indent[total_width] = 0;
5869
5870 return wrap_indent;
5871 }
5872
5873 total_width += width + 1;
5874 }
5875
5876 return NULL;
5877}
5878
b775012e
LM
5879/* Determine if the locations of this breakpoint will have their conditions
5880 evaluated by the target, host or a mix of both. Returns the following:
5881
5882 "host": Host evals condition.
5883 "host or target": Host or Target evals condition.
5884 "target": Target evals condition.
5885*/
5886
5887static const char *
5888bp_condition_evaluator (struct breakpoint *b)
5889{
5890 struct bp_location *bl;
5891 char host_evals = 0;
5892 char target_evals = 0;
5893
5894 if (!b)
5895 return NULL;
5896
5897 if (!is_breakpoint (b))
5898 return NULL;
5899
5900 if (gdb_evaluates_breakpoint_condition_p ()
5901 || !target_supports_evaluation_of_breakpoint_conditions ())
5902 return condition_evaluation_host;
5903
5904 for (bl = b->loc; bl; bl = bl->next)
5905 {
5906 if (bl->cond_bytecode)
5907 target_evals++;
5908 else
5909 host_evals++;
5910 }
5911
5912 if (host_evals && target_evals)
5913 return condition_evaluation_both;
5914 else if (target_evals)
5915 return condition_evaluation_target;
5916 else
5917 return condition_evaluation_host;
5918}
5919
5920/* Determine the breakpoint location's condition evaluator. This is
5921 similar to bp_condition_evaluator, but for locations. */
5922
5923static const char *
5924bp_location_condition_evaluator (struct bp_location *bl)
5925{
5926 if (bl && !is_breakpoint (bl->owner))
5927 return NULL;
5928
5929 if (gdb_evaluates_breakpoint_condition_p ()
5930 || !target_supports_evaluation_of_breakpoint_conditions ())
5931 return condition_evaluation_host;
5932
5933 if (bl && bl->cond_bytecode)
5934 return condition_evaluation_target;
5935 else
5936 return condition_evaluation_host;
5937}
5938
859825b8
JK
5939/* Print the LOC location out of the list of B->LOC locations. */
5940
170b53b2
UW
5941static void
5942print_breakpoint_location (struct breakpoint *b,
5943 struct bp_location *loc)
0d381245 5944{
79a45e25 5945 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5946
5947 scoped_restore_current_program_space restore_pspace;
6c95b8df 5948
859825b8
JK
5949 if (loc != NULL && loc->shlib_disabled)
5950 loc = NULL;
5951
6c95b8df
PA
5952 if (loc != NULL)
5953 set_current_program_space (loc->pspace);
5954
56435ebe 5955 if (b->display_canonical)
d28cd78a 5956 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5957 else if (loc && loc->symtab)
0d381245
VP
5958 {
5959 struct symbol *sym
5960 = find_pc_sect_function (loc->address, loc->section);
5961 if (sym)
5962 {
112e8700
SM
5963 uiout->text ("in ");
5964 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5965 uiout->text (" ");
5966 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5967 uiout->text ("at ");
0d381245 5968 }
112e8700 5969 uiout->field_string ("file",
05cba821 5970 symtab_to_filename_for_display (loc->symtab));
112e8700 5971 uiout->text (":");
05cba821 5972
112e8700
SM
5973 if (uiout->is_mi_like_p ())
5974 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5975
112e8700 5976 uiout->field_int ("line", loc->line_number);
0d381245 5977 }
859825b8 5978 else if (loc)
0d381245 5979 {
d7e74731 5980 string_file stb;
170b53b2 5981
d7e74731 5982 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5983 demangle, "");
112e8700 5984 uiout->field_stream ("at", stb);
0d381245 5985 }
859825b8 5986 else
f00aae0f 5987 {
d28cd78a
TT
5988 uiout->field_string ("pending",
5989 event_location_to_string (b->location.get ()));
f00aae0f
KS
5990 /* If extra_string is available, it could be holding a condition
5991 or dprintf arguments. In either case, make sure it is printed,
5992 too, but only for non-MI streams. */
112e8700 5993 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5994 {
5995 if (b->type == bp_dprintf)
112e8700 5996 uiout->text (",");
f00aae0f 5997 else
112e8700
SM
5998 uiout->text (" ");
5999 uiout->text (b->extra_string);
f00aae0f
KS
6000 }
6001 }
6c95b8df 6002
b775012e
LM
6003 if (loc && is_breakpoint (b)
6004 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6005 && bp_condition_evaluator (b) == condition_evaluation_both)
6006 {
112e8700
SM
6007 uiout->text (" (");
6008 uiout->field_string ("evaluated-by",
b775012e 6009 bp_location_condition_evaluator (loc));
112e8700 6010 uiout->text (")");
b775012e 6011 }
0d381245
VP
6012}
6013
269b11a2
PA
6014static const char *
6015bptype_string (enum bptype type)
c906108c 6016{
c4093a6a
JM
6017 struct ep_type_description
6018 {
6019 enum bptype type;
a121b7c1 6020 const char *description;
c4093a6a
JM
6021 };
6022 static struct ep_type_description bptypes[] =
c906108c 6023 {
c5aa993b
JM
6024 {bp_none, "?deleted?"},
6025 {bp_breakpoint, "breakpoint"},
c906108c 6026 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6027 {bp_single_step, "sw single-step"},
c5aa993b
JM
6028 {bp_until, "until"},
6029 {bp_finish, "finish"},
6030 {bp_watchpoint, "watchpoint"},
c906108c 6031 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6032 {bp_read_watchpoint, "read watchpoint"},
6033 {bp_access_watchpoint, "acc watchpoint"},
6034 {bp_longjmp, "longjmp"},
6035 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6036 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6037 {bp_exception, "exception"},
6038 {bp_exception_resume, "exception resume"},
c5aa993b 6039 {bp_step_resume, "step resume"},
2c03e5be 6040 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6041 {bp_watchpoint_scope, "watchpoint scope"},
6042 {bp_call_dummy, "call dummy"},
aa7d318d 6043 {bp_std_terminate, "std::terminate"},
c5aa993b 6044 {bp_shlib_event, "shlib events"},
c4093a6a 6045 {bp_thread_event, "thread events"},
1900040c 6046 {bp_overlay_event, "overlay events"},
0fd8e87f 6047 {bp_longjmp_master, "longjmp master"},
aa7d318d 6048 {bp_std_terminate_master, "std::terminate master"},
186c406b 6049 {bp_exception_master, "exception master"},
ce78b96d 6050 {bp_catchpoint, "catchpoint"},
1042e4c0 6051 {bp_tracepoint, "tracepoint"},
7a697b8d 6052 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6053 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6054 {bp_dprintf, "dprintf"},
4efc6507 6055 {bp_jit_event, "jit events"},
0e30163f
JK
6056 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6057 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6058 };
269b11a2
PA
6059
6060 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6061 || ((int) type != bptypes[(int) type].type))
6062 internal_error (__FILE__, __LINE__,
6063 _("bptypes table does not describe type #%d."),
6064 (int) type);
6065
6066 return bptypes[(int) type].description;
6067}
6068
998580f1
MK
6069/* For MI, output a field named 'thread-groups' with a list as the value.
6070 For CLI, prefix the list with the string 'inf'. */
6071
6072static void
6073output_thread_groups (struct ui_out *uiout,
6074 const char *field_name,
6075 VEC(int) *inf_num,
6076 int mi_only)
6077{
112e8700 6078 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6079 int inf;
6080 int i;
6081
6082 /* For backward compatibility, don't display inferiors in CLI unless
6083 there are several. Always display them for MI. */
6084 if (!is_mi && mi_only)
6085 return;
6086
10f489e5 6087 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6088
998580f1
MK
6089 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6090 {
6091 if (is_mi)
6092 {
6093 char mi_group[10];
6094
6095 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6096 uiout->field_string (NULL, mi_group);
998580f1
MK
6097 }
6098 else
6099 {
6100 if (i == 0)
112e8700 6101 uiout->text (" inf ");
998580f1 6102 else
112e8700 6103 uiout->text (", ");
998580f1 6104
112e8700 6105 uiout->text (plongest (inf));
998580f1
MK
6106 }
6107 }
998580f1
MK
6108}
6109
269b11a2
PA
6110/* Print B to gdb_stdout. */
6111
6112static void
6113print_one_breakpoint_location (struct breakpoint *b,
6114 struct bp_location *loc,
6115 int loc_number,
6116 struct bp_location **last_loc,
269b11a2
PA
6117 int allflag)
6118{
6119 struct command_line *l;
c2c6d25f 6120 static char bpenables[] = "nynny";
c906108c 6121
79a45e25 6122 struct ui_out *uiout = current_uiout;
0d381245
VP
6123 int header_of_multiple = 0;
6124 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6125 struct value_print_options opts;
6126
6127 get_user_print_options (&opts);
0d381245
VP
6128
6129 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6130 /* See comment in print_one_breakpoint concerning treatment of
6131 breakpoints with single disabled location. */
0d381245
VP
6132 if (loc == NULL
6133 && (b->loc != NULL
6134 && (b->loc->next != NULL || !b->loc->enabled)))
6135 header_of_multiple = 1;
6136 if (loc == NULL)
6137 loc = b->loc;
6138
c4093a6a
JM
6139 annotate_record ();
6140
6141 /* 1 */
6142 annotate_field (0);
0d381245
VP
6143 if (part_of_multiple)
6144 {
6145 char *formatted;
0c6773c1 6146 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6147 uiout->field_string ("number", formatted);
0d381245
VP
6148 xfree (formatted);
6149 }
6150 else
6151 {
112e8700 6152 uiout->field_int ("number", b->number);
0d381245 6153 }
c4093a6a
JM
6154
6155 /* 2 */
6156 annotate_field (1);
0d381245 6157 if (part_of_multiple)
112e8700 6158 uiout->field_skip ("type");
269b11a2 6159 else
112e8700 6160 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6161
6162 /* 3 */
6163 annotate_field (2);
0d381245 6164 if (part_of_multiple)
112e8700 6165 uiout->field_skip ("disp");
0d381245 6166 else
112e8700 6167 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6168
c4093a6a
JM
6169
6170 /* 4 */
6171 annotate_field (3);
0d381245 6172 if (part_of_multiple)
112e8700 6173 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6174 else
112e8700
SM
6175 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6176 uiout->spaces (2);
0d381245 6177
c4093a6a
JM
6178
6179 /* 5 and 6 */
3086aeae 6180 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6181 {
4a64f543
MS
6182 /* Although the print_one can possibly print all locations,
6183 calling it here is not likely to get any nice result. So,
6184 make sure there's just one location. */
0d381245 6185 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6186 b->ops->print_one (b, last_loc);
0d381245 6187 }
3086aeae
DJ
6188 else
6189 switch (b->type)
6190 {
6191 case bp_none:
6192 internal_error (__FILE__, __LINE__,
e2e0b3e5 6193 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6194 break;
c906108c 6195
3086aeae
DJ
6196 case bp_watchpoint:
6197 case bp_hardware_watchpoint:
6198 case bp_read_watchpoint:
6199 case bp_access_watchpoint:
3a5c3e22
PA
6200 {
6201 struct watchpoint *w = (struct watchpoint *) b;
6202
6203 /* Field 4, the address, is omitted (which makes the columns
6204 not line up too nicely with the headers, but the effect
6205 is relatively readable). */
6206 if (opts.addressprint)
112e8700 6207 uiout->field_skip ("addr");
3a5c3e22 6208 annotate_field (5);
112e8700 6209 uiout->field_string ("what", w->exp_string);
3a5c3e22 6210 }
3086aeae
DJ
6211 break;
6212
3086aeae
DJ
6213 case bp_breakpoint:
6214 case bp_hardware_breakpoint:
7c16b83e 6215 case bp_single_step:
3086aeae
DJ
6216 case bp_until:
6217 case bp_finish:
6218 case bp_longjmp:
6219 case bp_longjmp_resume:
e2e4d78b 6220 case bp_longjmp_call_dummy:
186c406b
TT
6221 case bp_exception:
6222 case bp_exception_resume:
3086aeae 6223 case bp_step_resume:
2c03e5be 6224 case bp_hp_step_resume:
3086aeae
DJ
6225 case bp_watchpoint_scope:
6226 case bp_call_dummy:
aa7d318d 6227 case bp_std_terminate:
3086aeae
DJ
6228 case bp_shlib_event:
6229 case bp_thread_event:
6230 case bp_overlay_event:
0fd8e87f 6231 case bp_longjmp_master:
aa7d318d 6232 case bp_std_terminate_master:
186c406b 6233 case bp_exception_master:
1042e4c0 6234 case bp_tracepoint:
7a697b8d 6235 case bp_fast_tracepoint:
0fb4aa4b 6236 case bp_static_tracepoint:
e7e0cddf 6237 case bp_dprintf:
4efc6507 6238 case bp_jit_event:
0e30163f
JK
6239 case bp_gnu_ifunc_resolver:
6240 case bp_gnu_ifunc_resolver_return:
79a45b7d 6241 if (opts.addressprint)
3086aeae
DJ
6242 {
6243 annotate_field (4);
54e52265 6244 if (header_of_multiple)
112e8700 6245 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6246 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6247 uiout->field_string ("addr", "<PENDING>");
0101ce28 6248 else
112e8700 6249 uiout->field_core_addr ("addr",
5af949e3 6250 loc->gdbarch, loc->address);
3086aeae
DJ
6251 }
6252 annotate_field (5);
0d381245 6253 if (!header_of_multiple)
170b53b2 6254 print_breakpoint_location (b, loc);
0d381245 6255 if (b->loc)
a6d9a66e 6256 *last_loc = b->loc;
3086aeae
DJ
6257 break;
6258 }
c906108c 6259
6c95b8df 6260
998580f1 6261 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6262 {
6263 struct inferior *inf;
998580f1
MK
6264 VEC(int) *inf_num = NULL;
6265 int mi_only = 1;
6c95b8df 6266
998580f1 6267 ALL_INFERIORS (inf)
6c95b8df
PA
6268 {
6269 if (inf->pspace == loc->pspace)
998580f1 6270 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6271 }
998580f1
MK
6272
6273 /* For backward compatibility, don't display inferiors in CLI unless
6274 there are several. Always display for MI. */
6275 if (allflag
6276 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6277 && (number_of_program_spaces () > 1
6278 || number_of_inferiors () > 1)
6279 /* LOC is for existing B, it cannot be in
6280 moribund_locations and thus having NULL OWNER. */
6281 && loc->owner->type != bp_catchpoint))
6282 mi_only = 0;
6283 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6284 VEC_free (int, inf_num);
6c95b8df
PA
6285 }
6286
4a306c9a 6287 if (!part_of_multiple)
c4093a6a 6288 {
4a306c9a
JB
6289 if (b->thread != -1)
6290 {
6291 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6292 "stop only in" line a little further down. */
112e8700
SM
6293 uiout->text (" thread ");
6294 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6295 }
6296 else if (b->task != 0)
6297 {
112e8700
SM
6298 uiout->text (" task ");
6299 uiout->field_int ("task", b->task);
4a306c9a 6300 }
c4093a6a 6301 }
f1310107 6302
112e8700 6303 uiout->text ("\n");
f1310107 6304
348d480f 6305 if (!part_of_multiple)
f1310107
TJB
6306 b->ops->print_one_detail (b, uiout);
6307
0d381245 6308 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6309 {
6310 annotate_field (6);
112e8700 6311 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6312 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6313 the frame ID. */
112e8700 6314 uiout->field_core_addr ("frame",
5af949e3 6315 b->gdbarch, b->frame_id.stack_addr);
112e8700 6316 uiout->text ("\n");
c4093a6a
JM
6317 }
6318
28010a5d 6319 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6320 {
6321 annotate_field (7);
d77f58be 6322 if (is_tracepoint (b))
112e8700 6323 uiout->text ("\ttrace only if ");
1042e4c0 6324 else
112e8700
SM
6325 uiout->text ("\tstop only if ");
6326 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6327
6328 /* Print whether the target is doing the breakpoint's condition
6329 evaluation. If GDB is doing the evaluation, don't print anything. */
6330 if (is_breakpoint (b)
6331 && breakpoint_condition_evaluation_mode ()
6332 == condition_evaluation_target)
6333 {
112e8700
SM
6334 uiout->text (" (");
6335 uiout->field_string ("evaluated-by",
b775012e 6336 bp_condition_evaluator (b));
112e8700 6337 uiout->text (" evals)");
b775012e 6338 }
112e8700 6339 uiout->text ("\n");
0101ce28
JJ
6340 }
6341
0d381245 6342 if (!part_of_multiple && b->thread != -1)
c4093a6a 6343 {
4a64f543 6344 /* FIXME should make an annotation for this. */
112e8700
SM
6345 uiout->text ("\tstop only in thread ");
6346 if (uiout->is_mi_like_p ())
6347 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6348 else
6349 {
6350 struct thread_info *thr = find_thread_global_id (b->thread);
6351
112e8700 6352 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6353 }
112e8700 6354 uiout->text ("\n");
c4093a6a
JM
6355 }
6356
556ec64d
YQ
6357 if (!part_of_multiple)
6358 {
6359 if (b->hit_count)
31f56a27
YQ
6360 {
6361 /* FIXME should make an annotation for this. */
6362 if (is_catchpoint (b))
112e8700 6363 uiout->text ("\tcatchpoint");
31f56a27 6364 else if (is_tracepoint (b))
112e8700 6365 uiout->text ("\ttracepoint");
31f56a27 6366 else
112e8700
SM
6367 uiout->text ("\tbreakpoint");
6368 uiout->text (" already hit ");
6369 uiout->field_int ("times", b->hit_count);
31f56a27 6370 if (b->hit_count == 1)
112e8700 6371 uiout->text (" time\n");
31f56a27 6372 else
112e8700 6373 uiout->text (" times\n");
31f56a27 6374 }
556ec64d
YQ
6375 else
6376 {
31f56a27 6377 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6378 if (uiout->is_mi_like_p ())
6379 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6380 }
6381 }
8b93c638 6382
0d381245 6383 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6384 {
6385 annotate_field (8);
112e8700
SM
6386 uiout->text ("\tignore next ");
6387 uiout->field_int ("ignore", b->ignore_count);
6388 uiout->text (" hits\n");
c4093a6a 6389 }
059fb39f 6390
816338b5
SS
6391 /* Note that an enable count of 1 corresponds to "enable once"
6392 behavior, which is reported by the combination of enablement and
6393 disposition, so we don't need to mention it here. */
6394 if (!part_of_multiple && b->enable_count > 1)
6395 {
6396 annotate_field (8);
112e8700 6397 uiout->text ("\tdisable after ");
816338b5
SS
6398 /* Tweak the wording to clarify that ignore and enable counts
6399 are distinct, and have additive effect. */
6400 if (b->ignore_count)
112e8700 6401 uiout->text ("additional ");
816338b5 6402 else
112e8700
SM
6403 uiout->text ("next ");
6404 uiout->field_int ("enable", b->enable_count);
6405 uiout->text (" hits\n");
816338b5
SS
6406 }
6407
f196051f
SS
6408 if (!part_of_multiple && is_tracepoint (b))
6409 {
6410 struct tracepoint *tp = (struct tracepoint *) b;
6411
6412 if (tp->traceframe_usage)
6413 {
112e8700
SM
6414 uiout->text ("\ttrace buffer usage ");
6415 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6416 uiout->text (" bytes\n");
f196051f
SS
6417 }
6418 }
d3ce09f5 6419
d1b0a7bf 6420 l = b->commands ? b->commands.get () : NULL;
059fb39f 6421 if (!part_of_multiple && l)
c4093a6a
JM
6422 {
6423 annotate_field (9);
2e783024 6424 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6425 print_command_lines (uiout, l, 4);
c4093a6a 6426 }
d24317b4 6427
d9b3f62e 6428 if (is_tracepoint (b))
1042e4c0 6429 {
d9b3f62e
PA
6430 struct tracepoint *t = (struct tracepoint *) b;
6431
6432 if (!part_of_multiple && t->pass_count)
6433 {
6434 annotate_field (10);
112e8700
SM
6435 uiout->text ("\tpass count ");
6436 uiout->field_int ("pass", t->pass_count);
6437 uiout->text (" \n");
d9b3f62e 6438 }
f2a8bc8a
YQ
6439
6440 /* Don't display it when tracepoint or tracepoint location is
6441 pending. */
6442 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6443 {
6444 annotate_field (11);
6445
112e8700
SM
6446 if (uiout->is_mi_like_p ())
6447 uiout->field_string ("installed",
f2a8bc8a
YQ
6448 loc->inserted ? "y" : "n");
6449 else
6450 {
6451 if (loc->inserted)
112e8700 6452 uiout->text ("\t");
f2a8bc8a 6453 else
112e8700
SM
6454 uiout->text ("\tnot ");
6455 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6456 }
6457 }
1042e4c0
SS
6458 }
6459
112e8700 6460 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6461 {
3a5c3e22
PA
6462 if (is_watchpoint (b))
6463 {
6464 struct watchpoint *w = (struct watchpoint *) b;
6465
112e8700 6466 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6467 }
f00aae0f 6468 else if (b->location != NULL
d28cd78a 6469 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6470 uiout->field_string ("original-location",
d28cd78a 6471 event_location_to_string (b->location.get ()));
d24317b4 6472 }
c4093a6a 6473}
c5aa993b 6474
0d381245
VP
6475static void
6476print_one_breakpoint (struct breakpoint *b,
4a64f543 6477 struct bp_location **last_loc,
6c95b8df 6478 int allflag)
0d381245 6479{
79a45e25 6480 struct ui_out *uiout = current_uiout;
8d3788bd 6481
2e783024
TT
6482 {
6483 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6484
2e783024
TT
6485 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6486 }
0d381245
VP
6487
6488 /* If this breakpoint has custom print function,
6489 it's already printed. Otherwise, print individual
6490 locations, if any. */
6491 if (b->ops == NULL || b->ops->print_one == NULL)
6492 {
4a64f543
MS
6493 /* If breakpoint has a single location that is disabled, we
6494 print it as if it had several locations, since otherwise it's
6495 hard to represent "breakpoint enabled, location disabled"
6496 situation.
6497
6498 Note that while hardware watchpoints have several locations
a3be7890 6499 internally, that's not a property exposed to user. */
0d381245 6500 if (b->loc
a5606eee 6501 && !is_hardware_watchpoint (b)
8d3788bd 6502 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6503 {
6504 struct bp_location *loc;
6505 int n = 1;
8d3788bd 6506
0d381245 6507 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6508 {
2e783024 6509 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6510 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6511 }
0d381245
VP
6512 }
6513 }
6514}
6515
a6d9a66e
UW
6516static int
6517breakpoint_address_bits (struct breakpoint *b)
6518{
6519 int print_address_bits = 0;
6520 struct bp_location *loc;
6521
c6d81124
PA
6522 /* Software watchpoints that aren't watching memory don't have an
6523 address to print. */
6524 if (is_no_memory_software_watchpoint (b))
6525 return 0;
6526
a6d9a66e
UW
6527 for (loc = b->loc; loc; loc = loc->next)
6528 {
c7437ca6
PA
6529 int addr_bit;
6530
c7437ca6 6531 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6532 if (addr_bit > print_address_bits)
6533 print_address_bits = addr_bit;
6534 }
6535
6536 return print_address_bits;
6537}
0d381245 6538
65630365 6539/* See breakpoint.h. */
c5aa993b 6540
65630365
PA
6541void
6542print_breakpoint (breakpoint *b)
c4093a6a 6543{
a6d9a66e 6544 struct bp_location *dummy_loc = NULL;
65630365 6545 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6546}
c5aa993b 6547
09d682a4
TT
6548/* Return true if this breakpoint was set by the user, false if it is
6549 internal or momentary. */
6550
6551int
6552user_breakpoint_p (struct breakpoint *b)
6553{
46c6471b 6554 return b->number > 0;
09d682a4
TT
6555}
6556
93daf339
TT
6557/* See breakpoint.h. */
6558
6559int
6560pending_breakpoint_p (struct breakpoint *b)
6561{
6562 return b->loc == NULL;
6563}
6564
7f3b0473 6565/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6566 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6567 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6568 FILTER is non-NULL, call it on each breakpoint and only include the
6569 ones for which it returns non-zero. Return the total number of
6570 breakpoints listed. */
c906108c 6571
d77f58be 6572static int
4495129a 6573breakpoint_1 (const char *args, int allflag,
4a64f543 6574 int (*filter) (const struct breakpoint *))
c4093a6a 6575{
52f0bd74 6576 struct breakpoint *b;
a6d9a66e 6577 struct bp_location *last_loc = NULL;
7f3b0473 6578 int nr_printable_breakpoints;
79a45b7d 6579 struct value_print_options opts;
a6d9a66e 6580 int print_address_bits = 0;
269b11a2 6581 int print_type_col_width = 14;
79a45e25 6582 struct ui_out *uiout = current_uiout;
269b11a2 6583
79a45b7d
TT
6584 get_user_print_options (&opts);
6585
4a64f543
MS
6586 /* Compute the number of rows in the table, as well as the size
6587 required for address fields. */
7f3b0473
AC
6588 nr_printable_breakpoints = 0;
6589 ALL_BREAKPOINTS (b)
e5a67952
MS
6590 {
6591 /* If we have a filter, only list the breakpoints it accepts. */
6592 if (filter && !filter (b))
6593 continue;
6594
6595 /* If we have an "args" string, it is a list of breakpoints to
6596 accept. Skip the others. */
6597 if (args != NULL && *args != '\0')
6598 {
6599 if (allflag && parse_and_eval_long (args) != b->number)
6600 continue;
6601 if (!allflag && !number_is_in_list (args, b->number))
6602 continue;
6603 }
269b11a2 6604
e5a67952
MS
6605 if (allflag || user_breakpoint_p (b))
6606 {
6607 int addr_bit, type_len;
a6d9a66e 6608
e5a67952
MS
6609 addr_bit = breakpoint_address_bits (b);
6610 if (addr_bit > print_address_bits)
6611 print_address_bits = addr_bit;
269b11a2 6612
e5a67952
MS
6613 type_len = strlen (bptype_string (b->type));
6614 if (type_len > print_type_col_width)
6615 print_type_col_width = type_len;
6616
6617 nr_printable_breakpoints++;
6618 }
6619 }
7f3b0473 6620
4a2b031d
TT
6621 {
6622 ui_out_emit_table table_emitter (uiout,
6623 opts.addressprint ? 6 : 5,
6624 nr_printable_breakpoints,
6625 "BreakpointTable");
6626
6627 if (nr_printable_breakpoints > 0)
6628 annotate_breakpoints_headers ();
6629 if (nr_printable_breakpoints > 0)
6630 annotate_field (0);
6631 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6632 if (nr_printable_breakpoints > 0)
6633 annotate_field (1);
6634 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6635 if (nr_printable_breakpoints > 0)
6636 annotate_field (2);
6637 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (3);
6640 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6641 if (opts.addressprint)
6642 {
6643 if (nr_printable_breakpoints > 0)
6644 annotate_field (4);
6645 if (print_address_bits <= 32)
6646 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6647 else
6648 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6649 }
6650 if (nr_printable_breakpoints > 0)
6651 annotate_field (5);
6652 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6653 uiout->table_body ();
6654 if (nr_printable_breakpoints > 0)
6655 annotate_breakpoints_table ();
6656
6657 ALL_BREAKPOINTS (b)
6658 {
6659 QUIT;
6660 /* If we have a filter, only list the breakpoints it accepts. */
6661 if (filter && !filter (b))
6662 continue;
e5a67952 6663
4a2b031d
TT
6664 /* If we have an "args" string, it is a list of breakpoints to
6665 accept. Skip the others. */
e5a67952 6666
4a2b031d
TT
6667 if (args != NULL && *args != '\0')
6668 {
6669 if (allflag) /* maintenance info breakpoint */
6670 {
6671 if (parse_and_eval_long (args) != b->number)
6672 continue;
6673 }
6674 else /* all others */
6675 {
6676 if (!number_is_in_list (args, b->number))
6677 continue;
6678 }
6679 }
6680 /* We only print out user settable breakpoints unless the
6681 allflag is set. */
6682 if (allflag || user_breakpoint_p (b))
6683 print_one_breakpoint (b, &last_loc, allflag);
6684 }
6685 }
698384cd 6686
7f3b0473 6687 if (nr_printable_breakpoints == 0)
c906108c 6688 {
4a64f543
MS
6689 /* If there's a filter, let the caller decide how to report
6690 empty list. */
d77f58be
SS
6691 if (!filter)
6692 {
e5a67952 6693 if (args == NULL || *args == '\0')
112e8700 6694 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6695 else
112e8700 6696 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6697 args);
d77f58be 6698 }
c906108c
SS
6699 }
6700 else
c4093a6a 6701 {
a6d9a66e
UW
6702 if (last_loc && !server_command)
6703 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6704 }
c906108c 6705
4a64f543 6706 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6707 there have been breakpoints? */
c906108c 6708 annotate_breakpoints_table_end ();
d77f58be
SS
6709
6710 return nr_printable_breakpoints;
c906108c
SS
6711}
6712
ad443146
SS
6713/* Display the value of default-collect in a way that is generally
6714 compatible with the breakpoint list. */
6715
6716static void
6717default_collect_info (void)
6718{
79a45e25
PA
6719 struct ui_out *uiout = current_uiout;
6720
ad443146
SS
6721 /* If it has no value (which is frequently the case), say nothing; a
6722 message like "No default-collect." gets in user's face when it's
6723 not wanted. */
6724 if (!*default_collect)
6725 return;
6726
6727 /* The following phrase lines up nicely with per-tracepoint collect
6728 actions. */
112e8700
SM
6729 uiout->text ("default collect ");
6730 uiout->field_string ("default-collect", default_collect);
6731 uiout->text (" \n");
ad443146
SS
6732}
6733
c906108c 6734static void
11db9430 6735info_breakpoints_command (char *args, int from_tty)
c906108c 6736{
e5a67952 6737 breakpoint_1 (args, 0, NULL);
ad443146
SS
6738
6739 default_collect_info ();
d77f58be
SS
6740}
6741
6742static void
11db9430 6743info_watchpoints_command (char *args, int from_tty)
d77f58be 6744{
e5a67952 6745 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6746 struct ui_out *uiout = current_uiout;
d77f58be
SS
6747
6748 if (num_printed == 0)
6749 {
e5a67952 6750 if (args == NULL || *args == '\0')
112e8700 6751 uiout->message ("No watchpoints.\n");
d77f58be 6752 else
112e8700 6753 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6754 }
c906108c
SS
6755}
6756
7a292a7a 6757static void
4495129a 6758maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6759{
e5a67952 6760 breakpoint_1 (args, 1, NULL);
ad443146
SS
6761
6762 default_collect_info ();
c906108c
SS
6763}
6764
0d381245 6765static int
714835d5 6766breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6767 struct program_space *pspace,
714835d5 6768 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6769{
6770 struct bp_location *bl = b->loc;
cc59ec59 6771
0d381245
VP
6772 for (; bl; bl = bl->next)
6773 {
6c95b8df
PA
6774 if (bl->pspace == pspace
6775 && bl->address == pc
0d381245
VP
6776 && (!overlay_debugging || bl->section == section))
6777 return 1;
6778 }
6779 return 0;
6780}
6781
672f9b60 6782/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6783 concerns with logical breakpoints, so we match program spaces, not
6784 address spaces. */
c906108c
SS
6785
6786static void
6c95b8df
PA
6787describe_other_breakpoints (struct gdbarch *gdbarch,
6788 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6789 struct obj_section *section, int thread)
c906108c 6790{
52f0bd74
AC
6791 int others = 0;
6792 struct breakpoint *b;
c906108c
SS
6793
6794 ALL_BREAKPOINTS (b)
672f9b60
KP
6795 others += (user_breakpoint_p (b)
6796 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6797 if (others > 0)
6798 {
a3f17187
AC
6799 if (others == 1)
6800 printf_filtered (_("Note: breakpoint "));
6801 else /* if (others == ???) */
6802 printf_filtered (_("Note: breakpoints "));
c906108c 6803 ALL_BREAKPOINTS (b)
672f9b60 6804 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6805 {
6806 others--;
6807 printf_filtered ("%d", b->number);
6808 if (b->thread == -1 && thread != -1)
6809 printf_filtered (" (all threads)");
6810 else if (b->thread != -1)
6811 printf_filtered (" (thread %d)", b->thread);
6812 printf_filtered ("%s%s ",
059fb39f 6813 ((b->enable_state == bp_disabled
f8eba3c6 6814 || b->enable_state == bp_call_disabled)
0d381245 6815 ? " (disabled)"
0d381245
VP
6816 : ""),
6817 (others > 1) ? ","
6818 : ((others == 1) ? " and" : ""));
6819 }
a3f17187 6820 printf_filtered (_("also set at pc "));
5af949e3 6821 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6822 printf_filtered (".\n");
6823 }
6824}
6825\f
c906108c 6826
e4f237da 6827/* Return true iff it is meaningful to use the address member of
244558af
LM
6828 BPT locations. For some breakpoint types, the locations' address members
6829 are irrelevant and it makes no sense to attempt to compare them to other
6830 addresses (or use them for any other purpose either).
e4f237da 6831
4a64f543 6832 More specifically, each of the following breakpoint types will
244558af 6833 always have a zero valued location address and we don't want to mark
4a64f543 6834 breakpoints of any of these types to be a duplicate of an actual
244558af 6835 breakpoint location at address zero:
e4f237da
KB
6836
6837 bp_watchpoint
2d134ed3
PA
6838 bp_catchpoint
6839
6840*/
e4f237da
KB
6841
6842static int
6843breakpoint_address_is_meaningful (struct breakpoint *bpt)
6844{
6845 enum bptype type = bpt->type;
6846
2d134ed3
PA
6847 return (type != bp_watchpoint && type != bp_catchpoint);
6848}
6849
6850/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6851 true if LOC1 and LOC2 represent the same watchpoint location. */
6852
6853static int
4a64f543
MS
6854watchpoint_locations_match (struct bp_location *loc1,
6855 struct bp_location *loc2)
2d134ed3 6856{
3a5c3e22
PA
6857 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6858 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6859
6860 /* Both of them must exist. */
6861 gdb_assert (w1 != NULL);
6862 gdb_assert (w2 != NULL);
2bdf28a0 6863
4a64f543
MS
6864 /* If the target can evaluate the condition expression in hardware,
6865 then we we need to insert both watchpoints even if they are at
6866 the same place. Otherwise the watchpoint will only trigger when
6867 the condition of whichever watchpoint was inserted evaluates to
6868 true, not giving a chance for GDB to check the condition of the
6869 other watchpoint. */
3a5c3e22 6870 if ((w1->cond_exp
4a64f543
MS
6871 && target_can_accel_watchpoint_condition (loc1->address,
6872 loc1->length,
0cf6dd15 6873 loc1->watchpoint_type,
4d01a485 6874 w1->cond_exp.get ()))
3a5c3e22 6875 || (w2->cond_exp
4a64f543
MS
6876 && target_can_accel_watchpoint_condition (loc2->address,
6877 loc2->length,
0cf6dd15 6878 loc2->watchpoint_type,
4d01a485 6879 w2->cond_exp.get ())))
0cf6dd15
TJB
6880 return 0;
6881
85d721b8
PA
6882 /* Note that this checks the owner's type, not the location's. In
6883 case the target does not support read watchpoints, but does
6884 support access watchpoints, we'll have bp_read_watchpoint
6885 watchpoints with hw_access locations. Those should be considered
6886 duplicates of hw_read locations. The hw_read locations will
6887 become hw_access locations later. */
2d134ed3
PA
6888 return (loc1->owner->type == loc2->owner->type
6889 && loc1->pspace->aspace == loc2->pspace->aspace
6890 && loc1->address == loc2->address
6891 && loc1->length == loc2->length);
e4f237da
KB
6892}
6893
31e77af2 6894/* See breakpoint.h. */
6c95b8df 6895
31e77af2 6896int
accd0bcd
YQ
6897breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6898 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6899{
f5656ead 6900 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6901 || aspace1 == aspace2)
6902 && addr1 == addr2);
6903}
6904
f1310107
TJB
6905/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6906 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6907 matches ASPACE2. On targets that have global breakpoints, the address
6908 space doesn't really matter. */
6909
6910static int
accd0bcd
YQ
6911breakpoint_address_match_range (const address_space *aspace1,
6912 CORE_ADDR addr1,
6913 int len1, const address_space *aspace2,
f1310107
TJB
6914 CORE_ADDR addr2)
6915{
f5656ead 6916 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6917 || aspace1 == aspace2)
6918 && addr2 >= addr1 && addr2 < addr1 + len1);
6919}
6920
6921/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6922 a ranged breakpoint. In most targets, a match happens only if ASPACE
6923 matches the breakpoint's address space. On targets that have global
6924 breakpoints, the address space doesn't really matter. */
6925
6926static int
6927breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6928 const address_space *aspace,
f1310107
TJB
6929 CORE_ADDR addr)
6930{
6931 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6932 aspace, addr)
6933 || (bl->length
6934 && breakpoint_address_match_range (bl->pspace->aspace,
6935 bl->address, bl->length,
6936 aspace, addr)));
6937}
6938
d35ae833
PA
6939/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6940 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6941 match happens only if ASPACE matches the breakpoint's address
6942 space. On targets that have global breakpoints, the address space
6943 doesn't really matter. */
6944
6945static int
6946breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6947 const address_space *aspace,
d35ae833
PA
6948 CORE_ADDR addr, int len)
6949{
6950 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6951 || bl->pspace->aspace == aspace)
6952 {
6953 int bl_len = bl->length != 0 ? bl->length : 1;
6954
6955 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6956 return 1;
6957 }
6958 return 0;
6959}
6960
1e4d1764
YQ
6961/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6962 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6963 true, otherwise returns false. */
6964
6965static int
6966tracepoint_locations_match (struct bp_location *loc1,
6967 struct bp_location *loc2)
6968{
6969 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6970 /* Since tracepoint locations are never duplicated with others', tracepoint
6971 locations at the same address of different tracepoints are regarded as
6972 different locations. */
6973 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6974 else
6975 return 0;
6976}
6977
2d134ed3
PA
6978/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6979 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6980 represent the same location. */
6981
6982static int
4a64f543
MS
6983breakpoint_locations_match (struct bp_location *loc1,
6984 struct bp_location *loc2)
2d134ed3 6985{
2bdf28a0
JK
6986 int hw_point1, hw_point2;
6987
6988 /* Both of them must not be in moribund_locations. */
6989 gdb_assert (loc1->owner != NULL);
6990 gdb_assert (loc2->owner != NULL);
6991
6992 hw_point1 = is_hardware_watchpoint (loc1->owner);
6993 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6994
6995 if (hw_point1 != hw_point2)
6996 return 0;
6997 else if (hw_point1)
6998 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6999 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7000 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7001 else
f1310107
TJB
7002 /* We compare bp_location.length in order to cover ranged breakpoints. */
7003 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7004 loc2->pspace->aspace, loc2->address)
7005 && loc1->length == loc2->length);
2d134ed3
PA
7006}
7007
76897487
KB
7008static void
7009breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7010 int bnum, int have_bnum)
7011{
f63fbe86
MS
7012 /* The longest string possibly returned by hex_string_custom
7013 is 50 chars. These must be at least that big for safety. */
7014 char astr1[64];
7015 char astr2[64];
76897487 7016
bb599908
PH
7017 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7018 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7019 if (have_bnum)
8a3fe4f8 7020 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7021 bnum, astr1, astr2);
7022 else
8a3fe4f8 7023 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7024}
7025
4a64f543
MS
7026/* Adjust a breakpoint's address to account for architectural
7027 constraints on breakpoint placement. Return the adjusted address.
7028 Note: Very few targets require this kind of adjustment. For most
7029 targets, this function is simply the identity function. */
76897487
KB
7030
7031static CORE_ADDR
a6d9a66e
UW
7032adjust_breakpoint_address (struct gdbarch *gdbarch,
7033 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7034{
a6d9a66e 7035 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7036 {
7037 /* Very few targets need any kind of breakpoint adjustment. */
7038 return bpaddr;
7039 }
88f7da05
KB
7040 else if (bptype == bp_watchpoint
7041 || bptype == bp_hardware_watchpoint
7042 || bptype == bp_read_watchpoint
7043 || bptype == bp_access_watchpoint
fe798b75 7044 || bptype == bp_catchpoint)
88f7da05
KB
7045 {
7046 /* Watchpoints and the various bp_catch_* eventpoints should not
7047 have their addresses modified. */
7048 return bpaddr;
7049 }
7c16b83e
PA
7050 else if (bptype == bp_single_step)
7051 {
7052 /* Single-step breakpoints should not have their addresses
7053 modified. If there's any architectural constrain that
7054 applies to this address, then it should have already been
7055 taken into account when the breakpoint was created in the
7056 first place. If we didn't do this, stepping through e.g.,
7057 Thumb-2 IT blocks would break. */
7058 return bpaddr;
7059 }
76897487
KB
7060 else
7061 {
7062 CORE_ADDR adjusted_bpaddr;
7063
7064 /* Some targets have architectural constraints on the placement
7065 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7066 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7067
7068 /* An adjusted breakpoint address can significantly alter
7069 a user's expectations. Print a warning if an adjustment
7070 is required. */
7071 if (adjusted_bpaddr != bpaddr)
7072 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7073
7074 return adjusted_bpaddr;
7075 }
7076}
7077
5625a286 7078bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7079{
5625a286 7080 bp_location *loc = this;
7cc221ef 7081
348d480f
PA
7082 gdb_assert (ops != NULL);
7083
28010a5d
PA
7084 loc->ops = ops;
7085 loc->owner = owner;
b775012e 7086 loc->cond_bytecode = NULL;
0d381245
VP
7087 loc->shlib_disabled = 0;
7088 loc->enabled = 1;
e049a4b5 7089
28010a5d 7090 switch (owner->type)
e049a4b5
DJ
7091 {
7092 case bp_breakpoint:
7c16b83e 7093 case bp_single_step:
e049a4b5
DJ
7094 case bp_until:
7095 case bp_finish:
7096 case bp_longjmp:
7097 case bp_longjmp_resume:
e2e4d78b 7098 case bp_longjmp_call_dummy:
186c406b
TT
7099 case bp_exception:
7100 case bp_exception_resume:
e049a4b5 7101 case bp_step_resume:
2c03e5be 7102 case bp_hp_step_resume:
e049a4b5
DJ
7103 case bp_watchpoint_scope:
7104 case bp_call_dummy:
aa7d318d 7105 case bp_std_terminate:
e049a4b5
DJ
7106 case bp_shlib_event:
7107 case bp_thread_event:
7108 case bp_overlay_event:
4efc6507 7109 case bp_jit_event:
0fd8e87f 7110 case bp_longjmp_master:
aa7d318d 7111 case bp_std_terminate_master:
186c406b 7112 case bp_exception_master:
0e30163f
JK
7113 case bp_gnu_ifunc_resolver:
7114 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7115 case bp_dprintf:
e049a4b5 7116 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7117 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7118 break;
7119 case bp_hardware_breakpoint:
7120 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7121 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7122 break;
7123 case bp_hardware_watchpoint:
7124 case bp_read_watchpoint:
7125 case bp_access_watchpoint:
7126 loc->loc_type = bp_loc_hardware_watchpoint;
7127 break;
7128 case bp_watchpoint:
ce78b96d 7129 case bp_catchpoint:
15c3d785
PA
7130 case bp_tracepoint:
7131 case bp_fast_tracepoint:
0fb4aa4b 7132 case bp_static_tracepoint:
e049a4b5
DJ
7133 loc->loc_type = bp_loc_other;
7134 break;
7135 default:
e2e0b3e5 7136 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7137 }
7138
f431efe5 7139 loc->refc = 1;
28010a5d
PA
7140}
7141
7142/* Allocate a struct bp_location. */
7143
7144static struct bp_location *
7145allocate_bp_location (struct breakpoint *bpt)
7146{
348d480f
PA
7147 return bpt->ops->allocate_location (bpt);
7148}
7cc221ef 7149
f431efe5
PA
7150static void
7151free_bp_location (struct bp_location *loc)
fe3f5fa8 7152{
348d480f 7153 loc->ops->dtor (loc);
4d01a485 7154 delete loc;
fe3f5fa8
VP
7155}
7156
f431efe5
PA
7157/* Increment reference count. */
7158
7159static void
7160incref_bp_location (struct bp_location *bl)
7161{
7162 ++bl->refc;
7163}
7164
7165/* Decrement reference count. If the reference count reaches 0,
7166 destroy the bp_location. Sets *BLP to NULL. */
7167
7168static void
7169decref_bp_location (struct bp_location **blp)
7170{
0807b50c
PA
7171 gdb_assert ((*blp)->refc > 0);
7172
f431efe5
PA
7173 if (--(*blp)->refc == 0)
7174 free_bp_location (*blp);
7175 *blp = NULL;
7176}
7177
346774a9 7178/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7179
b270e6f9
TT
7180static breakpoint *
7181add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7182{
346774a9 7183 struct breakpoint *b1;
b270e6f9 7184 struct breakpoint *result = b.get ();
c906108c 7185
346774a9
PA
7186 /* Add this breakpoint to the end of the chain so that a list of
7187 breakpoints will come out in order of increasing numbers. */
7188
7189 b1 = breakpoint_chain;
7190 if (b1 == 0)
b270e6f9 7191 breakpoint_chain = b.release ();
346774a9
PA
7192 else
7193 {
7194 while (b1->next)
7195 b1 = b1->next;
b270e6f9 7196 b1->next = b.release ();
346774a9 7197 }
b270e6f9
TT
7198
7199 return result;
346774a9
PA
7200}
7201
7202/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7203
7204static void
7205init_raw_breakpoint_without_location (struct breakpoint *b,
7206 struct gdbarch *gdbarch,
28010a5d 7207 enum bptype bptype,
c0a91b2b 7208 const struct breakpoint_ops *ops)
346774a9 7209{
348d480f
PA
7210 gdb_assert (ops != NULL);
7211
28010a5d 7212 b->ops = ops;
4d28f7a8 7213 b->type = bptype;
a6d9a66e 7214 b->gdbarch = gdbarch;
c906108c
SS
7215 b->language = current_language->la_language;
7216 b->input_radix = input_radix;
d0fb5eae 7217 b->related_breakpoint = b;
346774a9
PA
7218}
7219
7220/* Helper to set_raw_breakpoint below. Creates a breakpoint
7221 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7222
7223static struct breakpoint *
7224set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7225 enum bptype bptype,
c0a91b2b 7226 const struct breakpoint_ops *ops)
346774a9 7227{
3b0871f4 7228 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7229
3b0871f4 7230 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7231 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7232}
7233
0e30163f
JK
7234/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7235 resolutions should be made as the user specified the location explicitly
7236 enough. */
7237
0d381245 7238static void
0e30163f 7239set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7240{
2bdf28a0
JK
7241 gdb_assert (loc->owner != NULL);
7242
0d381245 7243 if (loc->owner->type == bp_breakpoint
1042e4c0 7244 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7245 || is_tracepoint (loc->owner))
0d381245 7246 {
0e30163f 7247 int is_gnu_ifunc;
2c02bd72 7248 const char *function_name;
6a3a010b 7249 CORE_ADDR func_addr;
0e30163f 7250
2c02bd72 7251 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7252 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7253
7254 if (is_gnu_ifunc && !explicit_loc)
7255 {
7256 struct breakpoint *b = loc->owner;
7257
7258 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7259 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7260 &loc->requested_address))
7261 {
7262 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7263 loc->address = adjust_breakpoint_address (loc->gdbarch,
7264 loc->requested_address,
7265 b->type);
7266 }
7267 else if (b->type == bp_breakpoint && b->loc == loc
7268 && loc->next == NULL && b->related_breakpoint == b)
7269 {
7270 /* Create only the whole new breakpoint of this type but do not
7271 mess more complicated breakpoints with multiple locations. */
7272 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7273 /* Remember the resolver's address for use by the return
7274 breakpoint. */
7275 loc->related_address = func_addr;
0e30163f
JK
7276 }
7277 }
7278
2c02bd72
DE
7279 if (function_name)
7280 loc->function_name = xstrdup (function_name);
0d381245
VP
7281 }
7282}
7283
a6d9a66e 7284/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7285struct gdbarch *
a6d9a66e
UW
7286get_sal_arch (struct symtab_and_line sal)
7287{
7288 if (sal.section)
7289 return get_objfile_arch (sal.section->objfile);
7290 if (sal.symtab)
eb822aa6 7291 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7292
7293 return NULL;
7294}
7295
346774a9
PA
7296/* Low level routine for partially initializing a breakpoint of type
7297 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7298 file name, and line number are provided by SAL.
0d381245
VP
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
c56053d2 7302 information regarding the creation of a new breakpoint. */
0d381245 7303
346774a9
PA
7304static void
7305init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7306 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7307 const struct breakpoint_ops *ops)
0d381245 7308{
28010a5d 7309 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7310
3742cc8b 7311 add_location_to_breakpoint (b, &sal);
0d381245 7312
6c95b8df
PA
7313 if (bptype != bp_catchpoint)
7314 gdb_assert (sal.pspace != NULL);
7315
f8eba3c6
TT
7316 /* Store the program space that was used to set the breakpoint,
7317 except for ordinary breakpoints, which are independent of the
7318 program space. */
7319 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7320 b->pspace = sal.pspace;
346774a9 7321}
c906108c 7322
346774a9
PA
7323/* set_raw_breakpoint is a low level routine for allocating and
7324 partially initializing a breakpoint of type BPTYPE. The newly
7325 created breakpoint's address, section, source file name, and line
7326 number are provided by SAL. The newly created and partially
7327 initialized breakpoint is added to the breakpoint chain and
7328 is also returned as the value of this function.
7329
7330 It is expected that the caller will complete the initialization of
7331 the newly created breakpoint struct as well as output any status
7332 information regarding the creation of a new breakpoint. In
7333 particular, set_raw_breakpoint does NOT set the breakpoint
7334 number! Care should be taken to not allow an error to occur
7335 prior to completing the initialization of the breakpoint. If this
7336 should happen, a bogus breakpoint will be left on the chain. */
7337
7338struct breakpoint *
7339set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7340 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7341 const struct breakpoint_ops *ops)
346774a9 7342{
3b0871f4 7343 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7344
3b0871f4 7345 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7346 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7347}
7348
53a5351d 7349/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7350 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7351 initiated the operation. */
c906108c
SS
7352
7353void
186c406b 7354set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7355{
35df4500 7356 struct breakpoint *b, *b_tmp;
5d5658a1 7357 int thread = tp->global_num;
0fd8e87f
UW
7358
7359 /* To avoid having to rescan all objfile symbols at every step,
7360 we maintain a list of continually-inserted but always disabled
7361 longjmp "master" breakpoints. Here, we simply create momentary
7362 clones of those and enable them for the requested thread. */
35df4500 7363 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7364 if (b->pspace == current_program_space
186c406b
TT
7365 && (b->type == bp_longjmp_master
7366 || b->type == bp_exception_master))
0fd8e87f 7367 {
06edf0c0
PA
7368 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7369 struct breakpoint *clone;
cc59ec59 7370
e2e4d78b
JK
7371 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7372 after their removal. */
06edf0c0 7373 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7374 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7375 clone->thread = thread;
7376 }
186c406b
TT
7377
7378 tp->initiating_frame = frame;
c906108c
SS
7379}
7380
611c83ae 7381/* Delete all longjmp breakpoints from THREAD. */
c906108c 7382void
611c83ae 7383delete_longjmp_breakpoint (int thread)
c906108c 7384{
35df4500 7385 struct breakpoint *b, *b_tmp;
c906108c 7386
35df4500 7387 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7388 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7389 {
7390 if (b->thread == thread)
7391 delete_breakpoint (b);
7392 }
c906108c
SS
7393}
7394
f59f708a
PA
7395void
7396delete_longjmp_breakpoint_at_next_stop (int thread)
7397{
7398 struct breakpoint *b, *b_tmp;
7399
7400 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7401 if (b->type == bp_longjmp || b->type == bp_exception)
7402 {
7403 if (b->thread == thread)
7404 b->disposition = disp_del_at_next_stop;
7405 }
7406}
7407
e2e4d78b
JK
7408/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7409 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7410 pointer to any of them. Return NULL if this system cannot place longjmp
7411 breakpoints. */
7412
7413struct breakpoint *
7414set_longjmp_breakpoint_for_call_dummy (void)
7415{
7416 struct breakpoint *b, *retval = NULL;
7417
7418 ALL_BREAKPOINTS (b)
7419 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7420 {
7421 struct breakpoint *new_b;
7422
7423 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7424 &momentary_breakpoint_ops,
7425 1);
5d5658a1 7426 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7427
7428 /* Link NEW_B into the chain of RETVAL breakpoints. */
7429
7430 gdb_assert (new_b->related_breakpoint == new_b);
7431 if (retval == NULL)
7432 retval = new_b;
7433 new_b->related_breakpoint = retval;
7434 while (retval->related_breakpoint != new_b->related_breakpoint)
7435 retval = retval->related_breakpoint;
7436 retval->related_breakpoint = new_b;
7437 }
7438
7439 return retval;
7440}
7441
7442/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7443 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7444 stack.
7445
7446 You should call this function only at places where it is safe to currently
7447 unwind the whole stack. Failed stack unwind would discard live dummy
7448 frames. */
7449
7450void
b67a2c6f 7451check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7452{
7453 struct breakpoint *b, *b_tmp;
7454
7455 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7456 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7457 {
7458 struct breakpoint *dummy_b = b->related_breakpoint;
7459
7460 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7461 dummy_b = dummy_b->related_breakpoint;
7462 if (dummy_b->type != bp_call_dummy
7463 || frame_find_by_id (dummy_b->frame_id) != NULL)
7464 continue;
7465
b67a2c6f 7466 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7467
7468 while (b->related_breakpoint != b)
7469 {
7470 if (b_tmp == b->related_breakpoint)
7471 b_tmp = b->related_breakpoint->next;
7472 delete_breakpoint (b->related_breakpoint);
7473 }
7474 delete_breakpoint (b);
7475 }
7476}
7477
1900040c
MS
7478void
7479enable_overlay_breakpoints (void)
7480{
52f0bd74 7481 struct breakpoint *b;
1900040c
MS
7482
7483 ALL_BREAKPOINTS (b)
7484 if (b->type == bp_overlay_event)
7485 {
7486 b->enable_state = bp_enabled;
44702360 7487 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7488 overlay_events_enabled = 1;
1900040c
MS
7489 }
7490}
7491
7492void
7493disable_overlay_breakpoints (void)
7494{
52f0bd74 7495 struct breakpoint *b;
1900040c
MS
7496
7497 ALL_BREAKPOINTS (b)
7498 if (b->type == bp_overlay_event)
7499 {
7500 b->enable_state = bp_disabled;
44702360 7501 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7502 overlay_events_enabled = 0;
1900040c
MS
7503 }
7504}
7505
aa7d318d
TT
7506/* Set an active std::terminate breakpoint for each std::terminate
7507 master breakpoint. */
7508void
7509set_std_terminate_breakpoint (void)
7510{
35df4500 7511 struct breakpoint *b, *b_tmp;
aa7d318d 7512
35df4500 7513 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7514 if (b->pspace == current_program_space
7515 && b->type == bp_std_terminate_master)
7516 {
06edf0c0 7517 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7518 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7519 }
7520}
7521
7522/* Delete all the std::terminate breakpoints. */
7523void
7524delete_std_terminate_breakpoint (void)
7525{
35df4500 7526 struct breakpoint *b, *b_tmp;
aa7d318d 7527
35df4500 7528 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7529 if (b->type == bp_std_terminate)
7530 delete_breakpoint (b);
7531}
7532
c4093a6a 7533struct breakpoint *
a6d9a66e 7534create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7535{
7536 struct breakpoint *b;
c4093a6a 7537
06edf0c0
PA
7538 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7539 &internal_breakpoint_ops);
7540
b5de0fa7 7541 b->enable_state = bp_enabled;
f00aae0f 7542 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7543 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7544
44702360 7545 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7546
c4093a6a
JM
7547 return b;
7548}
7549
0101ce28
JJ
7550struct lang_and_radix
7551 {
7552 enum language lang;
7553 int radix;
7554 };
7555
4efc6507
DE
7556/* Create a breakpoint for JIT code registration and unregistration. */
7557
7558struct breakpoint *
7559create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7560{
2a7f3dff
PA
7561 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7562 &internal_breakpoint_ops);
4efc6507 7563}
0101ce28 7564
03673fc7
PP
7565/* Remove JIT code registration and unregistration breakpoint(s). */
7566
7567void
7568remove_jit_event_breakpoints (void)
7569{
7570 struct breakpoint *b, *b_tmp;
7571
7572 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7573 if (b->type == bp_jit_event
7574 && b->loc->pspace == current_program_space)
7575 delete_breakpoint (b);
7576}
7577
cae688ec
JJ
7578void
7579remove_solib_event_breakpoints (void)
7580{
35df4500 7581 struct breakpoint *b, *b_tmp;
cae688ec 7582
35df4500 7583 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7584 if (b->type == bp_shlib_event
7585 && b->loc->pspace == current_program_space)
cae688ec
JJ
7586 delete_breakpoint (b);
7587}
7588
f37f681c
PA
7589/* See breakpoint.h. */
7590
7591void
7592remove_solib_event_breakpoints_at_next_stop (void)
7593{
7594 struct breakpoint *b, *b_tmp;
7595
7596 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7597 if (b->type == bp_shlib_event
7598 && b->loc->pspace == current_program_space)
7599 b->disposition = disp_del_at_next_stop;
7600}
7601
04086b45
PA
7602/* Helper for create_solib_event_breakpoint /
7603 create_and_insert_solib_event_breakpoint. Allows specifying which
7604 INSERT_MODE to pass through to update_global_location_list. */
7605
7606static struct breakpoint *
7607create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7608 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7609{
7610 struct breakpoint *b;
7611
06edf0c0
PA
7612 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7613 &internal_breakpoint_ops);
04086b45 7614 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7615 return b;
7616}
7617
04086b45
PA
7618struct breakpoint *
7619create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7620{
7621 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7622}
7623
f37f681c
PA
7624/* See breakpoint.h. */
7625
7626struct breakpoint *
7627create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7628{
7629 struct breakpoint *b;
7630
04086b45
PA
7631 /* Explicitly tell update_global_location_list to insert
7632 locations. */
7633 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7634 if (!b->loc->inserted)
7635 {
7636 delete_breakpoint (b);
7637 return NULL;
7638 }
7639 return b;
7640}
7641
cae688ec
JJ
7642/* Disable any breakpoints that are on code in shared libraries. Only
7643 apply to enabled breakpoints, disabled ones can just stay disabled. */
7644
7645void
cb851954 7646disable_breakpoints_in_shlibs (void)
cae688ec 7647{
876fa593 7648 struct bp_location *loc, **locp_tmp;
cae688ec 7649
876fa593 7650 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7651 {
2bdf28a0 7652 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7653 struct breakpoint *b = loc->owner;
2bdf28a0 7654
4a64f543
MS
7655 /* We apply the check to all breakpoints, including disabled for
7656 those with loc->duplicate set. This is so that when breakpoint
7657 becomes enabled, or the duplicate is removed, gdb will try to
7658 insert all breakpoints. If we don't set shlib_disabled here,
7659 we'll try to insert those breakpoints and fail. */
1042e4c0 7660 if (((b->type == bp_breakpoint)
508ccb1f 7661 || (b->type == bp_jit_event)
1042e4c0 7662 || (b->type == bp_hardware_breakpoint)
d77f58be 7663 || (is_tracepoint (b)))
6c95b8df 7664 && loc->pspace == current_program_space
0d381245 7665 && !loc->shlib_disabled
6c95b8df 7666 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7667 )
0d381245
VP
7668 {
7669 loc->shlib_disabled = 1;
7670 }
cae688ec
JJ
7671 }
7672}
7673
63644780
NB
7674/* Disable any breakpoints and tracepoints that are in SOLIB upon
7675 notification of unloaded_shlib. Only apply to enabled breakpoints,
7676 disabled ones can just stay disabled. */
84acb35a 7677
75149521 7678static void
84acb35a
JJ
7679disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7680{
876fa593 7681 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7682 int disabled_shlib_breaks = 0;
7683
876fa593 7684 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7685 {
2bdf28a0 7686 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7687 struct breakpoint *b = loc->owner;
cc59ec59 7688
1e4d1764 7689 if (solib->pspace == loc->pspace
e2dd7057 7690 && !loc->shlib_disabled
1e4d1764
YQ
7691 && (((b->type == bp_breakpoint
7692 || b->type == bp_jit_event
7693 || b->type == bp_hardware_breakpoint)
7694 && (loc->loc_type == bp_loc_hardware_breakpoint
7695 || loc->loc_type == bp_loc_software_breakpoint))
7696 || is_tracepoint (b))
e2dd7057 7697 && solib_contains_address_p (solib, loc->address))
84acb35a 7698 {
e2dd7057
PP
7699 loc->shlib_disabled = 1;
7700 /* At this point, we cannot rely on remove_breakpoint
7701 succeeding so we must mark the breakpoint as not inserted
7702 to prevent future errors occurring in remove_breakpoints. */
7703 loc->inserted = 0;
8d3788bd
VP
7704
7705 /* This may cause duplicate notifications for the same breakpoint. */
7706 observer_notify_breakpoint_modified (b);
7707
e2dd7057
PP
7708 if (!disabled_shlib_breaks)
7709 {
223ffa71 7710 target_terminal::ours_for_output ();
3e43a32a
MS
7711 warning (_("Temporarily disabling breakpoints "
7712 "for unloaded shared library \"%s\""),
e2dd7057 7713 solib->so_name);
84acb35a 7714 }
e2dd7057 7715 disabled_shlib_breaks = 1;
84acb35a
JJ
7716 }
7717 }
84acb35a
JJ
7718}
7719
63644780
NB
7720/* Disable any breakpoints and tracepoints in OBJFILE upon
7721 notification of free_objfile. Only apply to enabled breakpoints,
7722 disabled ones can just stay disabled. */
7723
7724static void
7725disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7726{
7727 struct breakpoint *b;
7728
7729 if (objfile == NULL)
7730 return;
7731
d03de421
PA
7732 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7733 managed by the user with add-symbol-file/remove-symbol-file.
7734 Similarly to how breakpoints in shared libraries are handled in
7735 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7736 shlib_disabled so they end up uninserted on the next global
7737 location list update. Shared libraries not loaded by the user
7738 aren't handled here -- they're already handled in
7739 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7740 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7741 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7742 main objfile). */
7743 if ((objfile->flags & OBJF_SHARED) == 0
7744 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7745 return;
7746
7747 ALL_BREAKPOINTS (b)
7748 {
7749 struct bp_location *loc;
7750 int bp_modified = 0;
7751
7752 if (!is_breakpoint (b) && !is_tracepoint (b))
7753 continue;
7754
7755 for (loc = b->loc; loc != NULL; loc = loc->next)
7756 {
7757 CORE_ADDR loc_addr = loc->address;
7758
7759 if (loc->loc_type != bp_loc_hardware_breakpoint
7760 && loc->loc_type != bp_loc_software_breakpoint)
7761 continue;
7762
7763 if (loc->shlib_disabled != 0)
7764 continue;
7765
7766 if (objfile->pspace != loc->pspace)
7767 continue;
7768
7769 if (loc->loc_type != bp_loc_hardware_breakpoint
7770 && loc->loc_type != bp_loc_software_breakpoint)
7771 continue;
7772
7773 if (is_addr_in_objfile (loc_addr, objfile))
7774 {
7775 loc->shlib_disabled = 1;
08351840
PA
7776 /* At this point, we don't know whether the object was
7777 unmapped from the inferior or not, so leave the
7778 inserted flag alone. We'll handle failure to
7779 uninsert quietly, in case the object was indeed
7780 unmapped. */
63644780
NB
7781
7782 mark_breakpoint_location_modified (loc);
7783
7784 bp_modified = 1;
7785 }
7786 }
7787
7788 if (bp_modified)
7789 observer_notify_breakpoint_modified (b);
7790 }
7791}
7792
ce78b96d
JB
7793/* FORK & VFORK catchpoints. */
7794
e29a4733 7795/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7796 catchpoint. A breakpoint is really of this type iff its ops pointer points
7797 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7798
c1fc2657 7799struct fork_catchpoint : public breakpoint
e29a4733 7800{
e29a4733
PA
7801 /* Process id of a child process whose forking triggered this
7802 catchpoint. This field is only valid immediately after this
7803 catchpoint has triggered. */
7804 ptid_t forked_inferior_pid;
7805};
7806
4a64f543
MS
7807/* Implement the "insert" breakpoint_ops method for fork
7808 catchpoints. */
ce78b96d 7809
77b06cd7
TJB
7810static int
7811insert_catch_fork (struct bp_location *bl)
ce78b96d 7812{
dfd4cc63 7813 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7814}
7815
4a64f543
MS
7816/* Implement the "remove" breakpoint_ops method for fork
7817 catchpoints. */
ce78b96d
JB
7818
7819static int
73971819 7820remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7821{
dfd4cc63 7822 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7823}
7824
7825/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7826 catchpoints. */
7827
7828static int
f1310107 7829breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7830 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7831 const struct target_waitstatus *ws)
ce78b96d 7832{
e29a4733
PA
7833 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7834
f90263c1
TT
7835 if (ws->kind != TARGET_WAITKIND_FORKED)
7836 return 0;
7837
7838 c->forked_inferior_pid = ws->value.related_pid;
7839 return 1;
ce78b96d
JB
7840}
7841
4a64f543
MS
7842/* Implement the "print_it" breakpoint_ops method for fork
7843 catchpoints. */
ce78b96d
JB
7844
7845static enum print_stop_action
348d480f 7846print_it_catch_fork (bpstat bs)
ce78b96d 7847{
36dfb11c 7848 struct ui_out *uiout = current_uiout;
348d480f
PA
7849 struct breakpoint *b = bs->breakpoint_at;
7850 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7851
ce78b96d 7852 annotate_catchpoint (b->number);
f303dbd6 7853 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7854 if (b->disposition == disp_del)
112e8700 7855 uiout->text ("Temporary catchpoint ");
36dfb11c 7856 else
112e8700
SM
7857 uiout->text ("Catchpoint ");
7858 if (uiout->is_mi_like_p ())
36dfb11c 7859 {
112e8700
SM
7860 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7861 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7862 }
112e8700
SM
7863 uiout->field_int ("bkptno", b->number);
7864 uiout->text (" (forked process ");
7865 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7866 uiout->text ("), ");
ce78b96d
JB
7867 return PRINT_SRC_AND_LOC;
7868}
7869
4a64f543
MS
7870/* Implement the "print_one" breakpoint_ops method for fork
7871 catchpoints. */
ce78b96d
JB
7872
7873static void
a6d9a66e 7874print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7875{
e29a4733 7876 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7877 struct value_print_options opts;
79a45e25 7878 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7879
7880 get_user_print_options (&opts);
7881
4a64f543
MS
7882 /* Field 4, the address, is omitted (which makes the columns not
7883 line up too nicely with the headers, but the effect is relatively
7884 readable). */
79a45b7d 7885 if (opts.addressprint)
112e8700 7886 uiout->field_skip ("addr");
ce78b96d 7887 annotate_field (5);
112e8700 7888 uiout->text ("fork");
e29a4733 7889 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7890 {
112e8700
SM
7891 uiout->text (", process ");
7892 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7893 uiout->spaces (1);
ce78b96d 7894 }
8ac3646f 7895
112e8700
SM
7896 if (uiout->is_mi_like_p ())
7897 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7898}
7899
7900/* Implement the "print_mention" breakpoint_ops method for fork
7901 catchpoints. */
7902
7903static void
7904print_mention_catch_fork (struct breakpoint *b)
7905{
7906 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7907}
7908
6149aea9
PA
7909/* Implement the "print_recreate" breakpoint_ops method for fork
7910 catchpoints. */
7911
7912static void
7913print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7914{
7915 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7916 print_recreate_thread (b, fp);
6149aea9
PA
7917}
7918
ce78b96d
JB
7919/* The breakpoint_ops structure to be used in fork catchpoints. */
7920
2060206e 7921static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7922
4a64f543
MS
7923/* Implement the "insert" breakpoint_ops method for vfork
7924 catchpoints. */
ce78b96d 7925
77b06cd7
TJB
7926static int
7927insert_catch_vfork (struct bp_location *bl)
ce78b96d 7928{
dfd4cc63 7929 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7930}
7931
4a64f543
MS
7932/* Implement the "remove" breakpoint_ops method for vfork
7933 catchpoints. */
ce78b96d
JB
7934
7935static int
73971819 7936remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7937{
dfd4cc63 7938 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7939}
7940
7941/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7942 catchpoints. */
7943
7944static int
f1310107 7945breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7946 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7947 const struct target_waitstatus *ws)
ce78b96d 7948{
e29a4733
PA
7949 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7950
f90263c1
TT
7951 if (ws->kind != TARGET_WAITKIND_VFORKED)
7952 return 0;
7953
7954 c->forked_inferior_pid = ws->value.related_pid;
7955 return 1;
ce78b96d
JB
7956}
7957
4a64f543
MS
7958/* Implement the "print_it" breakpoint_ops method for vfork
7959 catchpoints. */
ce78b96d
JB
7960
7961static enum print_stop_action
348d480f 7962print_it_catch_vfork (bpstat bs)
ce78b96d 7963{
36dfb11c 7964 struct ui_out *uiout = current_uiout;
348d480f 7965 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7966 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7967
ce78b96d 7968 annotate_catchpoint (b->number);
f303dbd6 7969 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7970 if (b->disposition == disp_del)
112e8700 7971 uiout->text ("Temporary catchpoint ");
36dfb11c 7972 else
112e8700
SM
7973 uiout->text ("Catchpoint ");
7974 if (uiout->is_mi_like_p ())
36dfb11c 7975 {
112e8700
SM
7976 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7977 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7978 }
112e8700
SM
7979 uiout->field_int ("bkptno", b->number);
7980 uiout->text (" (vforked process ");
7981 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7982 uiout->text ("), ");
ce78b96d
JB
7983 return PRINT_SRC_AND_LOC;
7984}
7985
4a64f543
MS
7986/* Implement the "print_one" breakpoint_ops method for vfork
7987 catchpoints. */
ce78b96d
JB
7988
7989static void
a6d9a66e 7990print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7991{
e29a4733 7992 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7993 struct value_print_options opts;
79a45e25 7994 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7995
7996 get_user_print_options (&opts);
4a64f543
MS
7997 /* Field 4, the address, is omitted (which makes the columns not
7998 line up too nicely with the headers, but the effect is relatively
7999 readable). */
79a45b7d 8000 if (opts.addressprint)
112e8700 8001 uiout->field_skip ("addr");
ce78b96d 8002 annotate_field (5);
112e8700 8003 uiout->text ("vfork");
e29a4733 8004 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8005 {
112e8700
SM
8006 uiout->text (", process ");
8007 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8008 uiout->spaces (1);
ce78b96d 8009 }
8ac3646f 8010
112e8700
SM
8011 if (uiout->is_mi_like_p ())
8012 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8013}
8014
8015/* Implement the "print_mention" breakpoint_ops method for vfork
8016 catchpoints. */
8017
8018static void
8019print_mention_catch_vfork (struct breakpoint *b)
8020{
8021 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8022}
8023
6149aea9
PA
8024/* Implement the "print_recreate" breakpoint_ops method for vfork
8025 catchpoints. */
8026
8027static void
8028print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8029{
8030 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8031 print_recreate_thread (b, fp);
6149aea9
PA
8032}
8033
ce78b96d
JB
8034/* The breakpoint_ops structure to be used in vfork catchpoints. */
8035
2060206e 8036static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8037
edcc5120 8038/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8039 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8040 CATCH_SOLIB_BREAKPOINT_OPS. */
8041
c1fc2657 8042struct solib_catchpoint : public breakpoint
edcc5120 8043{
c1fc2657 8044 ~solib_catchpoint () override;
edcc5120
TT
8045
8046 /* True for "catch load", false for "catch unload". */
8047 unsigned char is_load;
8048
8049 /* Regular expression to match, if any. COMPILED is only valid when
8050 REGEX is non-NULL. */
8051 char *regex;
2d7cc5c7 8052 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8053};
8054
c1fc2657 8055solib_catchpoint::~solib_catchpoint ()
edcc5120 8056{
c1fc2657 8057 xfree (this->regex);
edcc5120
TT
8058}
8059
8060static int
8061insert_catch_solib (struct bp_location *ignore)
8062{
8063 return 0;
8064}
8065
8066static int
73971819 8067remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8068{
8069 return 0;
8070}
8071
8072static int
8073breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8074 const address_space *aspace,
edcc5120
TT
8075 CORE_ADDR bp_addr,
8076 const struct target_waitstatus *ws)
8077{
8078 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8079 struct breakpoint *other;
8080
8081 if (ws->kind == TARGET_WAITKIND_LOADED)
8082 return 1;
8083
8084 ALL_BREAKPOINTS (other)
8085 {
8086 struct bp_location *other_bl;
8087
8088 if (other == bl->owner)
8089 continue;
8090
8091 if (other->type != bp_shlib_event)
8092 continue;
8093
c1fc2657 8094 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8095 continue;
8096
8097 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8098 {
8099 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8100 return 1;
8101 }
8102 }
8103
8104 return 0;
8105}
8106
8107static void
8108check_status_catch_solib (struct bpstats *bs)
8109{
8110 struct solib_catchpoint *self
8111 = (struct solib_catchpoint *) bs->breakpoint_at;
8112 int ix;
8113
8114 if (self->is_load)
8115 {
8116 struct so_list *iter;
8117
8118 for (ix = 0;
8119 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8120 ix, iter);
8121 ++ix)
8122 {
8123 if (!self->regex
2d7cc5c7 8124 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8125 return;
8126 }
8127 }
8128 else
8129 {
8130 char *iter;
8131
8132 for (ix = 0;
8133 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8134 ix, iter);
8135 ++ix)
8136 {
8137 if (!self->regex
2d7cc5c7 8138 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8139 return;
8140 }
8141 }
8142
8143 bs->stop = 0;
8144 bs->print_it = print_it_noop;
8145}
8146
8147static enum print_stop_action
8148print_it_catch_solib (bpstat bs)
8149{
8150 struct breakpoint *b = bs->breakpoint_at;
8151 struct ui_out *uiout = current_uiout;
8152
8153 annotate_catchpoint (b->number);
f303dbd6 8154 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8155 if (b->disposition == disp_del)
112e8700 8156 uiout->text ("Temporary catchpoint ");
edcc5120 8157 else
112e8700
SM
8158 uiout->text ("Catchpoint ");
8159 uiout->field_int ("bkptno", b->number);
8160 uiout->text ("\n");
8161 if (uiout->is_mi_like_p ())
8162 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8163 print_solib_event (1);
8164 return PRINT_SRC_AND_LOC;
8165}
8166
8167static void
8168print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8169{
8170 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8171 struct value_print_options opts;
8172 struct ui_out *uiout = current_uiout;
8173 char *msg;
8174
8175 get_user_print_options (&opts);
8176 /* Field 4, the address, is omitted (which makes the columns not
8177 line up too nicely with the headers, but the effect is relatively
8178 readable). */
8179 if (opts.addressprint)
8180 {
8181 annotate_field (4);
112e8700 8182 uiout->field_skip ("addr");
edcc5120
TT
8183 }
8184
8185 annotate_field (5);
8186 if (self->is_load)
8187 {
8188 if (self->regex)
8189 msg = xstrprintf (_("load of library matching %s"), self->regex);
8190 else
8191 msg = xstrdup (_("load of library"));
8192 }
8193 else
8194 {
8195 if (self->regex)
8196 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8197 else
8198 msg = xstrdup (_("unload of library"));
8199 }
112e8700 8200 uiout->field_string ("what", msg);
edcc5120 8201 xfree (msg);
8ac3646f 8202
112e8700
SM
8203 if (uiout->is_mi_like_p ())
8204 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8205}
8206
8207static void
8208print_mention_catch_solib (struct breakpoint *b)
8209{
8210 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8211
8212 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8213 self->is_load ? "load" : "unload");
8214}
8215
8216static void
8217print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8218{
8219 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8220
8221 fprintf_unfiltered (fp, "%s %s",
8222 b->disposition == disp_del ? "tcatch" : "catch",
8223 self->is_load ? "load" : "unload");
8224 if (self->regex)
8225 fprintf_unfiltered (fp, " %s", self->regex);
8226 fprintf_unfiltered (fp, "\n");
8227}
8228
8229static struct breakpoint_ops catch_solib_breakpoint_ops;
8230
91985142
MG
8231/* Shared helper function (MI and CLI) for creating and installing
8232 a shared object event catchpoint. If IS_LOAD is non-zero then
8233 the events to be caught are load events, otherwise they are
8234 unload events. If IS_TEMP is non-zero the catchpoint is a
8235 temporary one. If ENABLED is non-zero the catchpoint is
8236 created in an enabled state. */
edcc5120 8237
91985142 8238void
a121b7c1 8239add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8240{
edcc5120 8241 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8242
edcc5120
TT
8243 if (!arg)
8244 arg = "";
f1735a53 8245 arg = skip_spaces (arg);
edcc5120 8246
36bd8eaa 8247 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8248
8249 if (*arg != '\0')
8250 {
2d7cc5c7
PA
8251 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8252 _("Invalid regexp")));
edcc5120
TT
8253 c->regex = xstrdup (arg);
8254 }
8255
8256 c->is_load = is_load;
36bd8eaa 8257 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8258 &catch_solib_breakpoint_ops);
8259
c1fc2657 8260 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8261
b270e6f9 8262 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8263}
8264
91985142
MG
8265/* A helper function that does all the work for "catch load" and
8266 "catch unload". */
8267
8268static void
8269catch_load_or_unload (char *arg, int from_tty, int is_load,
8270 struct cmd_list_element *command)
8271{
8272 int tempflag;
8273 const int enabled = 1;
8274
8275 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8276
8277 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8278}
8279
edcc5120
TT
8280static void
8281catch_load_command_1 (char *arg, int from_tty,
8282 struct cmd_list_element *command)
8283{
8284 catch_load_or_unload (arg, from_tty, 1, command);
8285}
8286
8287static void
8288catch_unload_command_1 (char *arg, int from_tty,
8289 struct cmd_list_element *command)
8290{
8291 catch_load_or_unload (arg, from_tty, 0, command);
8292}
8293
346774a9
PA
8294/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8295 is non-zero, then make the breakpoint temporary. If COND_STRING is
8296 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8297 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8298
ab04a2af 8299void
346774a9
PA
8300init_catchpoint (struct breakpoint *b,
8301 struct gdbarch *gdbarch, int tempflag,
63160a43 8302 const char *cond_string,
c0a91b2b 8303 const struct breakpoint_ops *ops)
c906108c 8304{
51abb421 8305 symtab_and_line sal;
6c95b8df 8306 sal.pspace = current_program_space;
c5aa993b 8307
28010a5d 8308 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8309
1b36a34b 8310 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8311 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8312}
8313
28010a5d 8314void
b270e6f9 8315install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8316{
b270e6f9 8317 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8318 set_breakpoint_number (internal, b);
558a9d82
YQ
8319 if (is_tracepoint (b))
8320 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8321 if (!internal)
8322 mention (b);
c56053d2 8323 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8324
8325 if (update_gll)
44702360 8326 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8327}
8328
9b70b993 8329static void
a6d9a66e 8330create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8331 int tempflag, const char *cond_string,
c0a91b2b 8332 const struct breakpoint_ops *ops)
c906108c 8333{
b270e6f9 8334 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8335
b270e6f9 8336 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8337
8338 c->forked_inferior_pid = null_ptid;
8339
b270e6f9 8340 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8341}
8342
fe798b75
JB
8343/* Exec catchpoints. */
8344
b4d90040 8345/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8346 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8347 CATCH_EXEC_BREAKPOINT_OPS. */
8348
c1fc2657 8349struct exec_catchpoint : public breakpoint
b4d90040 8350{
c1fc2657 8351 ~exec_catchpoint () override;
b4d90040
PA
8352
8353 /* Filename of a program whose exec triggered this catchpoint.
8354 This field is only valid immediately after this catchpoint has
8355 triggered. */
8356 char *exec_pathname;
8357};
8358
c1fc2657 8359/* Exec catchpoint destructor. */
b4d90040 8360
c1fc2657 8361exec_catchpoint::~exec_catchpoint ()
b4d90040 8362{
c1fc2657 8363 xfree (this->exec_pathname);
b4d90040
PA
8364}
8365
77b06cd7
TJB
8366static int
8367insert_catch_exec (struct bp_location *bl)
c906108c 8368{
dfd4cc63 8369 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8370}
c906108c 8371
fe798b75 8372static int
73971819 8373remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8374{
dfd4cc63 8375 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8376}
c906108c 8377
fe798b75 8378static int
f1310107 8379breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8380 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8381 const struct target_waitstatus *ws)
fe798b75 8382{
b4d90040
PA
8383 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8384
f90263c1
TT
8385 if (ws->kind != TARGET_WAITKIND_EXECD)
8386 return 0;
8387
8388 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8389 return 1;
fe798b75 8390}
c906108c 8391
fe798b75 8392static enum print_stop_action
348d480f 8393print_it_catch_exec (bpstat bs)
fe798b75 8394{
36dfb11c 8395 struct ui_out *uiout = current_uiout;
348d480f 8396 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8397 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8398
fe798b75 8399 annotate_catchpoint (b->number);
f303dbd6 8400 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8401 if (b->disposition == disp_del)
112e8700 8402 uiout->text ("Temporary catchpoint ");
36dfb11c 8403 else
112e8700
SM
8404 uiout->text ("Catchpoint ");
8405 if (uiout->is_mi_like_p ())
36dfb11c 8406 {
112e8700
SM
8407 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8408 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8409 }
112e8700
SM
8410 uiout->field_int ("bkptno", b->number);
8411 uiout->text (" (exec'd ");
8412 uiout->field_string ("new-exec", c->exec_pathname);
8413 uiout->text ("), ");
36dfb11c 8414
fe798b75 8415 return PRINT_SRC_AND_LOC;
c906108c
SS
8416}
8417
fe798b75 8418static void
a6d9a66e 8419print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8420{
b4d90040 8421 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8422 struct value_print_options opts;
79a45e25 8423 struct ui_out *uiout = current_uiout;
fe798b75
JB
8424
8425 get_user_print_options (&opts);
8426
8427 /* Field 4, the address, is omitted (which makes the columns
8428 not line up too nicely with the headers, but the effect
8429 is relatively readable). */
8430 if (opts.addressprint)
112e8700 8431 uiout->field_skip ("addr");
fe798b75 8432 annotate_field (5);
112e8700 8433 uiout->text ("exec");
b4d90040 8434 if (c->exec_pathname != NULL)
fe798b75 8435 {
112e8700
SM
8436 uiout->text (", program \"");
8437 uiout->field_string ("what", c->exec_pathname);
8438 uiout->text ("\" ");
fe798b75 8439 }
8ac3646f 8440
112e8700
SM
8441 if (uiout->is_mi_like_p ())
8442 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8443}
8444
8445static void
8446print_mention_catch_exec (struct breakpoint *b)
8447{
8448 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8449}
8450
6149aea9
PA
8451/* Implement the "print_recreate" breakpoint_ops method for exec
8452 catchpoints. */
8453
8454static void
8455print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8456{
8457 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8458 print_recreate_thread (b, fp);
6149aea9
PA
8459}
8460
2060206e 8461static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8462
c906108c 8463static int
fba45db2 8464hw_breakpoint_used_count (void)
c906108c 8465{
c906108c 8466 int i = 0;
f1310107
TJB
8467 struct breakpoint *b;
8468 struct bp_location *bl;
c906108c
SS
8469
8470 ALL_BREAKPOINTS (b)
c5aa993b 8471 {
d6b74ac4 8472 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8473 for (bl = b->loc; bl; bl = bl->next)
8474 {
8475 /* Special types of hardware breakpoints may use more than
8476 one register. */
348d480f 8477 i += b->ops->resources_needed (bl);
f1310107 8478 }
c5aa993b 8479 }
c906108c
SS
8480
8481 return i;
8482}
8483
a1398e0c
PA
8484/* Returns the resources B would use if it were a hardware
8485 watchpoint. */
8486
c906108c 8487static int
a1398e0c 8488hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8489{
c906108c 8490 int i = 0;
e09342b5 8491 struct bp_location *bl;
c906108c 8492
a1398e0c
PA
8493 if (!breakpoint_enabled (b))
8494 return 0;
8495
8496 for (bl = b->loc; bl; bl = bl->next)
8497 {
8498 /* Special types of hardware watchpoints may use more than
8499 one register. */
8500 i += b->ops->resources_needed (bl);
8501 }
8502
8503 return i;
8504}
8505
8506/* Returns the sum the used resources of all hardware watchpoints of
8507 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8508 the sum of the used resources of all hardware watchpoints of other
8509 types _not_ TYPE. */
8510
8511static int
8512hw_watchpoint_used_count_others (struct breakpoint *except,
8513 enum bptype type, int *other_type_used)
8514{
8515 int i = 0;
8516 struct breakpoint *b;
8517
c906108c
SS
8518 *other_type_used = 0;
8519 ALL_BREAKPOINTS (b)
e09342b5 8520 {
a1398e0c
PA
8521 if (b == except)
8522 continue;
e09342b5
TJB
8523 if (!breakpoint_enabled (b))
8524 continue;
8525
a1398e0c
PA
8526 if (b->type == type)
8527 i += hw_watchpoint_use_count (b);
8528 else if (is_hardware_watchpoint (b))
8529 *other_type_used = 1;
e09342b5
TJB
8530 }
8531
c906108c
SS
8532 return i;
8533}
8534
c906108c 8535void
fba45db2 8536disable_watchpoints_before_interactive_call_start (void)
c906108c 8537{
c5aa993b 8538 struct breakpoint *b;
c906108c
SS
8539
8540 ALL_BREAKPOINTS (b)
c5aa993b 8541 {
cc60f2e3 8542 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8543 {
b5de0fa7 8544 b->enable_state = bp_call_disabled;
44702360 8545 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8546 }
8547 }
c906108c
SS
8548}
8549
8550void
fba45db2 8551enable_watchpoints_after_interactive_call_stop (void)
c906108c 8552{
c5aa993b 8553 struct breakpoint *b;
c906108c
SS
8554
8555 ALL_BREAKPOINTS (b)
c5aa993b 8556 {
cc60f2e3 8557 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8558 {
b5de0fa7 8559 b->enable_state = bp_enabled;
44702360 8560 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8561 }
8562 }
c906108c
SS
8563}
8564
8bea4e01
UW
8565void
8566disable_breakpoints_before_startup (void)
8567{
6c95b8df 8568 current_program_space->executing_startup = 1;
44702360 8569 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8570}
8571
8572void
8573enable_breakpoints_after_startup (void)
8574{
6c95b8df 8575 current_program_space->executing_startup = 0;
f8eba3c6 8576 breakpoint_re_set ();
8bea4e01
UW
8577}
8578
7c16b83e
PA
8579/* Create a new single-step breakpoint for thread THREAD, with no
8580 locations. */
c906108c 8581
7c16b83e
PA
8582static struct breakpoint *
8583new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8584{
b270e6f9 8585 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8586
b270e6f9 8587 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8588 &momentary_breakpoint_ops);
8589
8590 b->disposition = disp_donttouch;
8591 b->frame_id = null_frame_id;
8592
8593 b->thread = thread;
8594 gdb_assert (b->thread != 0);
8595
b270e6f9 8596 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8597}
8598
8599/* Set a momentary breakpoint of type TYPE at address specified by
8600 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8601 frame. */
c906108c
SS
8602
8603struct breakpoint *
a6d9a66e
UW
8604set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8605 struct frame_id frame_id, enum bptype type)
c906108c 8606{
52f0bd74 8607 struct breakpoint *b;
edb3359d 8608
193facb3
JK
8609 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8610 tail-called one. */
8611 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8612
06edf0c0 8613 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8614 b->enable_state = bp_enabled;
8615 b->disposition = disp_donttouch;
818dd999 8616 b->frame_id = frame_id;
c906108c 8617
4a64f543
MS
8618 /* If we're debugging a multi-threaded program, then we want
8619 momentary breakpoints to be active in only a single thread of
8620 control. */
39f77062 8621 if (in_thread_list (inferior_ptid))
5d5658a1 8622 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8623
44702360 8624 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8625
c906108c
SS
8626 return b;
8627}
611c83ae 8628
06edf0c0 8629/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8630 The new breakpoint will have type TYPE, use OPS as its
8631 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8632
06edf0c0
PA
8633static struct breakpoint *
8634momentary_breakpoint_from_master (struct breakpoint *orig,
8635 enum bptype type,
a1aa2221
LM
8636 const struct breakpoint_ops *ops,
8637 int loc_enabled)
e58b0e63
PA
8638{
8639 struct breakpoint *copy;
8640
06edf0c0 8641 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8642 copy->loc = allocate_bp_location (copy);
0e30163f 8643 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8644
a6d9a66e 8645 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8646 copy->loc->requested_address = orig->loc->requested_address;
8647 copy->loc->address = orig->loc->address;
8648 copy->loc->section = orig->loc->section;
6c95b8df 8649 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8650 copy->loc->probe = orig->loc->probe;
f8eba3c6 8651 copy->loc->line_number = orig->loc->line_number;
2f202fde 8652 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8653 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8654 copy->frame_id = orig->frame_id;
8655 copy->thread = orig->thread;
6c95b8df 8656 copy->pspace = orig->pspace;
e58b0e63
PA
8657
8658 copy->enable_state = bp_enabled;
8659 copy->disposition = disp_donttouch;
8660 copy->number = internal_breakpoint_number--;
8661
44702360 8662 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8663 return copy;
8664}
8665
06edf0c0
PA
8666/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8667 ORIG is NULL. */
8668
8669struct breakpoint *
8670clone_momentary_breakpoint (struct breakpoint *orig)
8671{
8672 /* If there's nothing to clone, then return nothing. */
8673 if (orig == NULL)
8674 return NULL;
8675
a1aa2221 8676 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8677}
8678
611c83ae 8679struct breakpoint *
a6d9a66e
UW
8680set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8681 enum bptype type)
611c83ae
PA
8682{
8683 struct symtab_and_line sal;
8684
8685 sal = find_pc_line (pc, 0);
8686 sal.pc = pc;
8687 sal.section = find_pc_overlay (pc);
8688 sal.explicit_pc = 1;
8689
a6d9a66e 8690 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8691}
c906108c 8692\f
c5aa993b 8693
c906108c
SS
8694/* Tell the user we have just set a breakpoint B. */
8695
8696static void
fba45db2 8697mention (struct breakpoint *b)
c906108c 8698{
348d480f 8699 b->ops->print_mention (b);
112e8700 8700 if (current_uiout->is_mi_like_p ())
fb40c209 8701 return;
c906108c
SS
8702 printf_filtered ("\n");
8703}
c906108c 8704\f
c5aa993b 8705
1a853c52
PA
8706static int bp_loc_is_permanent (struct bp_location *loc);
8707
0d381245 8708static struct bp_location *
39d61571 8709add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8710 const struct symtab_and_line *sal)
8711{
8712 struct bp_location *loc, **tmp;
3742cc8b
YQ
8713 CORE_ADDR adjusted_address;
8714 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8715
8716 if (loc_gdbarch == NULL)
8717 loc_gdbarch = b->gdbarch;
8718
8719 /* Adjust the breakpoint's address prior to allocating a location.
8720 Once we call allocate_bp_location(), that mostly uninitialized
8721 location will be placed on the location chain. Adjustment of the
8722 breakpoint may cause target_read_memory() to be called and we do
8723 not want its scan of the location chain to find a breakpoint and
8724 location that's only been partially initialized. */
8725 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8726 sal->pc, b->type);
0d381245 8727
d30113d4 8728 /* Sort the locations by their ADDRESS. */
39d61571 8729 loc = allocate_bp_location (b);
d30113d4
JK
8730 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8731 tmp = &((*tmp)->next))
0d381245 8732 ;
d30113d4 8733 loc->next = *tmp;
0d381245 8734 *tmp = loc;
3742cc8b 8735
0d381245 8736 loc->requested_address = sal->pc;
3742cc8b 8737 loc->address = adjusted_address;
6c95b8df 8738 loc->pspace = sal->pspace;
729662a5
TT
8739 loc->probe.probe = sal->probe;
8740 loc->probe.objfile = sal->objfile;
6c95b8df 8741 gdb_assert (loc->pspace != NULL);
0d381245 8742 loc->section = sal->section;
3742cc8b 8743 loc->gdbarch = loc_gdbarch;
f8eba3c6 8744 loc->line_number = sal->line;
2f202fde 8745 loc->symtab = sal->symtab;
f8eba3c6 8746
0e30163f
JK
8747 set_breakpoint_location_function (loc,
8748 sal->explicit_pc || sal->explicit_line);
1a853c52 8749
6ae88661
LM
8750 /* While by definition, permanent breakpoints are already present in the
8751 code, we don't mark the location as inserted. Normally one would expect
8752 that GDB could rely on that breakpoint instruction to stop the program,
8753 thus removing the need to insert its own breakpoint, except that executing
8754 the breakpoint instruction can kill the target instead of reporting a
8755 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8756 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8757 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8758 breakpoint be inserted normally results in QEMU knowing about the GDB
8759 breakpoint, and thus trap before the breakpoint instruction is executed.
8760 (If GDB later needs to continue execution past the permanent breakpoint,
8761 it manually increments the PC, thus avoiding executing the breakpoint
8762 instruction.) */
1a853c52 8763 if (bp_loc_is_permanent (loc))
6ae88661 8764 loc->permanent = 1;
1a853c52 8765
0d381245
VP
8766 return loc;
8767}
514f746b
AR
8768\f
8769
1cf4d951 8770/* See breakpoint.h. */
514f746b 8771
1cf4d951
PA
8772int
8773program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8774{
8775 int len;
8776 CORE_ADDR addr;
1afeeb75 8777 const gdb_byte *bpoint;
514f746b
AR
8778 gdb_byte *target_mem;
8779
1cf4d951
PA
8780 addr = address;
8781 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8782
8783 /* Software breakpoints unsupported? */
8784 if (bpoint == NULL)
8785 return 0;
8786
224c3ddb 8787 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8788
8789 /* Enable the automatic memory restoration from breakpoints while
8790 we read the memory. Otherwise we could say about our temporary
8791 breakpoints they are permanent. */
cb85b21b
TT
8792 scoped_restore restore_memory
8793 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8794
8795 if (target_read_memory (address, target_mem, len) == 0
8796 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8797 return 1;
1cf4d951 8798
cb85b21b 8799 return 0;
1cf4d951
PA
8800}
8801
8802/* Return 1 if LOC is pointing to a permanent breakpoint,
8803 return 0 otherwise. */
8804
8805static int
8806bp_loc_is_permanent (struct bp_location *loc)
8807{
514f746b
AR
8808 gdb_assert (loc != NULL);
8809
244558af
LM
8810 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8811 attempt to read from the addresses the locations of these breakpoint types
8812 point to. program_breakpoint_here_p, below, will attempt to read
8813 memory. */
8814 if (!breakpoint_address_is_meaningful (loc->owner))
8815 return 0;
8816
5ed8105e 8817 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8818 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8819 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8820}
8821
e7e0cddf
SS
8822/* Build a command list for the dprintf corresponding to the current
8823 settings of the dprintf style options. */
8824
8825static void
8826update_dprintf_command_list (struct breakpoint *b)
8827{
8828 char *dprintf_args = b->extra_string;
8829 char *printf_line = NULL;
8830
8831 if (!dprintf_args)
8832 return;
8833
8834 dprintf_args = skip_spaces (dprintf_args);
8835
8836 /* Allow a comma, as it may have terminated a location, but don't
8837 insist on it. */
8838 if (*dprintf_args == ',')
8839 ++dprintf_args;
8840 dprintf_args = skip_spaces (dprintf_args);
8841
8842 if (*dprintf_args != '"')
8843 error (_("Bad format string, missing '\"'."));
8844
d3ce09f5 8845 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8846 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8847 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8848 {
8849 if (!dprintf_function)
8850 error (_("No function supplied for dprintf call"));
8851
8852 if (dprintf_channel && strlen (dprintf_channel) > 0)
8853 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8854 dprintf_function,
8855 dprintf_channel,
8856 dprintf_args);
8857 else
8858 printf_line = xstrprintf ("call (void) %s (%s)",
8859 dprintf_function,
8860 dprintf_args);
8861 }
d3ce09f5
SS
8862 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8863 {
8864 if (target_can_run_breakpoint_commands ())
8865 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8866 else
8867 {
8868 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8869 printf_line = xstrprintf ("printf %s", dprintf_args);
8870 }
8871 }
e7e0cddf
SS
8872 else
8873 internal_error (__FILE__, __LINE__,
8874 _("Invalid dprintf style."));
8875
f28045c2 8876 gdb_assert (printf_line != NULL);
9d6e6e84 8877 /* Manufacture a printf sequence. */
f28045c2 8878 {
8d749320 8879 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8880
f28045c2
YQ
8881 printf_cmd_line->control_type = simple_control;
8882 printf_cmd_line->body_count = 0;
8883 printf_cmd_line->body_list = NULL;
9d6e6e84 8884 printf_cmd_line->next = NULL;
f28045c2 8885 printf_cmd_line->line = printf_line;
e7e0cddf 8886
93921405 8887 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8888 }
e7e0cddf
SS
8889}
8890
8891/* Update all dprintf commands, making their command lists reflect
8892 current style settings. */
8893
8894static void
8895update_dprintf_commands (char *args, int from_tty,
8896 struct cmd_list_element *c)
8897{
8898 struct breakpoint *b;
8899
8900 ALL_BREAKPOINTS (b)
8901 {
8902 if (b->type == bp_dprintf)
8903 update_dprintf_command_list (b);
8904 }
8905}
c3f6f71d 8906
f00aae0f
KS
8907/* Create a breakpoint with SAL as location. Use LOCATION
8908 as a description of the location, and COND_STRING
b35a8b2f
DE
8909 as condition expression. If LOCATION is NULL then create an
8910 "address location" from the address in the SAL. */
018d34a4
VP
8911
8912static void
d9b3f62e 8913init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8914 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8915 event_location_up &&location,
e1e01040
PA
8916 gdb::unique_xmalloc_ptr<char> filter,
8917 gdb::unique_xmalloc_ptr<char> cond_string,
8918 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8919 enum bptype type, enum bpdisp disposition,
8920 int thread, int task, int ignore_count,
c0a91b2b 8921 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8922 int enabled, int internal, unsigned flags,
8923 int display_canonical)
018d34a4 8924{
0d381245 8925 int i;
018d34a4
VP
8926
8927 if (type == bp_hardware_breakpoint)
8928 {
fbbd034e
AS
8929 int target_resources_ok;
8930
8931 i = hw_breakpoint_used_count ();
8932 target_resources_ok =
8933 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8934 i + 1, 0);
8935 if (target_resources_ok == 0)
8936 error (_("No hardware breakpoint support in the target."));
8937 else if (target_resources_ok < 0)
8938 error (_("Hardware breakpoints used exceeds limit."));
8939 }
8940
6c5b2ebe 8941 gdb_assert (!sals.empty ());
6c95b8df 8942
6c5b2ebe 8943 for (const auto &sal : sals)
0d381245 8944 {
0d381245
VP
8945 struct bp_location *loc;
8946
8947 if (from_tty)
5af949e3
UW
8948 {
8949 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8950 if (!loc_gdbarch)
8951 loc_gdbarch = gdbarch;
8952
8953 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8954 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8955 }
0d381245 8956
6c5b2ebe 8957 if (&sal == &sals[0])
0d381245 8958 {
d9b3f62e 8959 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8960 b->thread = thread;
4a306c9a 8961 b->task = task;
855a6e68 8962
e1e01040
PA
8963 b->cond_string = cond_string.release ();
8964 b->extra_string = extra_string.release ();
0d381245 8965 b->ignore_count = ignore_count;
41447f92 8966 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8967 b->disposition = disposition;
6c95b8df 8968
44f238bb
PA
8969 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8970 b->loc->inserted = 1;
8971
0fb4aa4b
PA
8972 if (type == bp_static_tracepoint)
8973 {
d9b3f62e 8974 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8975 struct static_tracepoint_marker marker;
8976
983af33b 8977 if (strace_marker_p (b))
0fb4aa4b
PA
8978 {
8979 /* We already know the marker exists, otherwise, we
8980 wouldn't see a sal for it. */
d28cd78a
TT
8981 const char *p
8982 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8983 const char *endp;
0fb4aa4b 8984 char *marker_str;
0fb4aa4b 8985
f1735a53 8986 p = skip_spaces (p);
0fb4aa4b 8987
f1735a53 8988 endp = skip_to_space (p);
0fb4aa4b
PA
8989
8990 marker_str = savestring (p, endp - p);
d9b3f62e 8991 t->static_trace_marker_id = marker_str;
0fb4aa4b 8992
3e43a32a
MS
8993 printf_filtered (_("Probed static tracepoint "
8994 "marker \"%s\"\n"),
d9b3f62e 8995 t->static_trace_marker_id);
0fb4aa4b
PA
8996 }
8997 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8998 {
d9b3f62e 8999 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9000 release_static_tracepoint_marker (&marker);
9001
3e43a32a
MS
9002 printf_filtered (_("Probed static tracepoint "
9003 "marker \"%s\"\n"),
d9b3f62e 9004 t->static_trace_marker_id);
0fb4aa4b
PA
9005 }
9006 else
3e43a32a
MS
9007 warning (_("Couldn't determine the static "
9008 "tracepoint marker to probe"));
0fb4aa4b
PA
9009 }
9010
0d381245
VP
9011 loc = b->loc;
9012 }
9013 else
018d34a4 9014 {
39d61571 9015 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9016 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9017 loc->inserted = 1;
0d381245
VP
9018 }
9019
9020 if (b->cond_string)
9021 {
bbc13ae3
KS
9022 const char *arg = b->cond_string;
9023
1bb9788d
TT
9024 loc->cond = parse_exp_1 (&arg, loc->address,
9025 block_for_pc (loc->address), 0);
0d381245 9026 if (*arg)
588ae58c 9027 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9028 }
e7e0cddf
SS
9029
9030 /* Dynamic printf requires and uses additional arguments on the
9031 command line, otherwise it's an error. */
9032 if (type == bp_dprintf)
9033 {
9034 if (b->extra_string)
9035 update_dprintf_command_list (b);
9036 else
9037 error (_("Format string required"));
9038 }
9039 else if (b->extra_string)
588ae58c 9040 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9041 }
018d34a4 9042
56435ebe 9043 b->display_canonical = display_canonical;
f00aae0f 9044 if (location != NULL)
d28cd78a 9045 b->location = std::move (location);
018d34a4 9046 else
d28cd78a 9047 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9048 b->filter = filter.release ();
d9b3f62e 9049}
018d34a4 9050
d9b3f62e
PA
9051static void
9052create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9053 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9054 event_location_up &&location,
e1e01040
PA
9055 gdb::unique_xmalloc_ptr<char> filter,
9056 gdb::unique_xmalloc_ptr<char> cond_string,
9057 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9058 enum bptype type, enum bpdisp disposition,
9059 int thread, int task, int ignore_count,
c0a91b2b 9060 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9061 int enabled, int internal, unsigned flags,
9062 int display_canonical)
d9b3f62e 9063{
a5e364af 9064 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9065
a5e364af 9066 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9067 sals, std::move (location),
e1e01040
PA
9068 std::move (filter),
9069 std::move (cond_string),
9070 std::move (extra_string),
d9b3f62e
PA
9071 type, disposition,
9072 thread, task, ignore_count,
9073 ops, from_tty,
44f238bb
PA
9074 enabled, internal, flags,
9075 display_canonical);
d9b3f62e 9076
b270e6f9 9077 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9078}
9079
9080/* Add SALS.nelts breakpoints to the breakpoint table. For each
9081 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9082 value. COND_STRING, if not NULL, specified the condition to be
9083 used for all breakpoints. Essentially the only case where
9084 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9085 function. In that case, it's still not possible to specify
9086 separate conditions for different overloaded functions, so
9087 we take just a single condition string.
9088
c3f6f71d 9089 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9090 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9091 array contents). If the function fails (error() is called), the
9092 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9093 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9094
9095static void
8cdf0e15 9096create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9097 struct linespec_result *canonical,
e1e01040
PA
9098 gdb::unique_xmalloc_ptr<char> cond_string,
9099 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9100 enum bptype type, enum bpdisp disposition,
9101 int thread, int task, int ignore_count,
c0a91b2b 9102 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9103 int enabled, int internal, unsigned flags)
c906108c 9104{
f8eba3c6 9105 if (canonical->pre_expanded)
6c5b2ebe 9106 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9107
6c5b2ebe 9108 for (const auto &lsal : canonical->lsals)
c3f6f71d 9109 {
f00aae0f 9110 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9111 'break', without arguments. */
ffc2605c 9112 event_location_up location
f00aae0f 9113 = (canonical->location != NULL
8e9e35b1 9114 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9115 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9116 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9117
6c5b2ebe 9118 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9119 std::move (location),
e1e01040
PA
9120 std::move (filter_string),
9121 std::move (cond_string),
9122 std::move (extra_string),
e7e0cddf 9123 type, disposition,
84f4c1fe 9124 thread, task, ignore_count, ops,
44f238bb 9125 from_tty, enabled, internal, flags,
56435ebe 9126 canonical->special_display);
c3f6f71d 9127 }
c3f6f71d 9128}
c906108c 9129
f00aae0f 9130/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9131 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9132 addresses found. LOCATION points to the end of the SAL (for
9133 linespec locations).
9998af43
TJB
9134
9135 The array and the line spec strings are allocated on the heap, it is
9136 the caller's responsibility to free them. */
c906108c 9137
b9362cc7 9138static void
f00aae0f 9139parse_breakpoint_sals (const struct event_location *location,
58438ac1 9140 struct linespec_result *canonical)
c3f6f71d 9141{
f00aae0f
KS
9142 struct symtab_and_line cursal;
9143
9144 if (event_location_type (location) == LINESPEC_LOCATION)
9145 {
9146 const char *address = get_linespec_location (location);
9147
9148 if (address == NULL)
9149 {
9150 /* The last displayed codepoint, if it's valid, is our default
9151 breakpoint address. */
9152 if (last_displayed_sal_is_valid ())
9153 {
f00aae0f
KS
9154 /* Set sal's pspace, pc, symtab, and line to the values
9155 corresponding to the last call to print_frame_info.
9156 Be sure to reinitialize LINE with NOTCURRENT == 0
9157 as the breakpoint line number is inappropriate otherwise.
9158 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9159 symtab_and_line sal = get_last_displayed_sal ();
9160 CORE_ADDR pc = sal.pc;
9161
f00aae0f
KS
9162 sal = find_pc_line (pc, 0);
9163
9164 /* "break" without arguments is equivalent to "break *PC"
9165 where PC is the last displayed codepoint's address. So
9166 make sure to set sal.explicit_pc to prevent GDB from
9167 trying to expand the list of sals to include all other
9168 instances with the same symtab and line. */
9169 sal.pc = pc;
9170 sal.explicit_pc = 1;
9171
6c5b2ebe
PA
9172 struct linespec_sals lsal;
9173 lsal.sals = {sal};
f00aae0f
KS
9174 lsal.canonical = NULL;
9175
6c5b2ebe 9176 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9177 return;
9178 }
9179 else
9180 error (_("No default breakpoint address now."));
c906108c 9181 }
c906108c 9182 }
f00aae0f
KS
9183
9184 /* Force almost all breakpoints to be in terms of the
9185 current_source_symtab (which is decode_line_1's default).
9186 This should produce the results we want almost all of the
9187 time while leaving default_breakpoint_* alone.
9188
9189 ObjC: However, don't match an Objective-C method name which
9190 may have a '+' or '-' succeeded by a '['. */
9191 cursal = get_current_source_symtab_and_line ();
9192 if (last_displayed_sal_is_valid ())
c906108c 9193 {
f00aae0f 9194 const char *address = NULL;
cc80f267 9195
f00aae0f
KS
9196 if (event_location_type (location) == LINESPEC_LOCATION)
9197 address = get_linespec_location (location);
cc80f267 9198
f00aae0f
KS
9199 if (!cursal.symtab
9200 || (address != NULL
9201 && strchr ("+-", address[0]) != NULL
9202 && address[1] != '['))
9203 {
c2f4122d 9204 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9205 get_last_displayed_symtab (),
9206 get_last_displayed_line (),
9207 canonical, NULL, NULL);
9208 return;
9209 }
c906108c 9210 }
f00aae0f 9211
c2f4122d 9212 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9213 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9214}
c906108c 9215
c906108c 9216
c3f6f71d 9217/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9218 inserted as a breakpoint. If it can't throw an error. */
c906108c 9219
b9362cc7 9220static void
6c5b2ebe 9221breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9222{
6c5b2ebe
PA
9223 for (auto &sal : sals)
9224 resolve_sal_pc (&sal);
c3f6f71d
JM
9225}
9226
7a697b8d
SS
9227/* Fast tracepoints may have restrictions on valid locations. For
9228 instance, a fast tracepoint using a jump instead of a trap will
9229 likely have to overwrite more bytes than a trap would, and so can
9230 only be placed where the instruction is longer than the jump, or a
9231 multi-instruction sequence does not have a jump into the middle of
9232 it, etc. */
9233
9234static void
9235check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9236 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9237{
6c5b2ebe 9238 int rslt;
7a697b8d
SS
9239 char *msg;
9240 struct cleanup *old_chain;
9241
6c5b2ebe 9242 for (const auto &sal : sals)
7a697b8d 9243 {
f8eba3c6
TT
9244 struct gdbarch *sarch;
9245
6c5b2ebe 9246 sarch = get_sal_arch (sal);
f8eba3c6
TT
9247 /* We fall back to GDBARCH if there is no architecture
9248 associated with SAL. */
9249 if (sarch == NULL)
9250 sarch = gdbarch;
6c5b2ebe 9251 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9252 old_chain = make_cleanup (xfree, msg);
9253
9254 if (!rslt)
53c3572a 9255 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9256 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9257
9258 do_cleanups (old_chain);
9259 }
9260}
9261
018d34a4
VP
9262/* Given TOK, a string specification of condition and thread, as
9263 accepted by the 'break' command, extract the condition
9264 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9265 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9266 If no condition is found, *COND_STRING is set to NULL.
9267 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9268
9269static void
bbc13ae3 9270find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9271 char **cond_string, int *thread, int *task,
9272 char **rest)
018d34a4
VP
9273{
9274 *cond_string = NULL;
9275 *thread = -1;
ed1d1739
KS
9276 *task = 0;
9277 *rest = NULL;
9278
018d34a4
VP
9279 while (tok && *tok)
9280 {
bbc13ae3 9281 const char *end_tok;
018d34a4 9282 int toklen;
bbc13ae3
KS
9283 const char *cond_start = NULL;
9284 const char *cond_end = NULL;
cc59ec59 9285
f1735a53 9286 tok = skip_spaces (tok);
e7e0cddf
SS
9287
9288 if ((*tok == '"' || *tok == ',') && rest)
9289 {
9290 *rest = savestring (tok, strlen (tok));
9291 return;
9292 }
9293
f1735a53 9294 end_tok = skip_to_space (tok);
d634f2de 9295
018d34a4 9296 toklen = end_tok - tok;
d634f2de 9297
018d34a4
VP
9298 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9299 {
9300 tok = cond_start = end_tok + 1;
4d01a485 9301 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9302 cond_end = tok;
d634f2de 9303 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9304 }
9305 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9306 {
5d5658a1
PA
9307 const char *tmptok;
9308 struct thread_info *thr;
d634f2de 9309
018d34a4 9310 tok = end_tok + 1;
5d5658a1 9311 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9312 if (tok == tmptok)
9313 error (_("Junk after thread keyword."));
5d5658a1 9314 *thread = thr->global_num;
bbc13ae3 9315 tok = tmptok;
018d34a4 9316 }
4a306c9a
JB
9317 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9318 {
9319 char *tmptok;
9320
9321 tok = end_tok + 1;
bbc13ae3 9322 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9323 if (tok == tmptok)
9324 error (_("Junk after task keyword."));
9325 if (!valid_task_id (*task))
b6199126 9326 error (_("Unknown task %d."), *task);
bbc13ae3 9327 tok = tmptok;
4a306c9a 9328 }
e7e0cddf
SS
9329 else if (rest)
9330 {
9331 *rest = savestring (tok, strlen (tok));
ccab2054 9332 return;
e7e0cddf 9333 }
018d34a4
VP
9334 else
9335 error (_("Junk at end of arguments."));
9336 }
9337}
9338
0fb4aa4b
PA
9339/* Decode a static tracepoint marker spec. */
9340
6c5b2ebe 9341static std::vector<symtab_and_line>
f00aae0f 9342decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9343{
9344 VEC(static_tracepoint_marker_p) *markers = NULL;
f00aae0f
KS
9345 const char *p = &(*arg_p)[3];
9346 const char *endp;
0fb4aa4b
PA
9347 int i;
9348
f1735a53 9349 p = skip_spaces (p);
0fb4aa4b 9350
f1735a53 9351 endp = skip_to_space (p);
0fb4aa4b 9352
81b1e71c 9353 std::string marker_str (p, endp - p);
0fb4aa4b 9354
81b1e71c 9355 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
0fb4aa4b 9356 if (VEC_empty(static_tracepoint_marker_p, markers))
81b1e71c
TT
9357 error (_("No known static tracepoint marker named %s"),
9358 marker_str.c_str ());
0fb4aa4b 9359
6c5b2ebe
PA
9360 std::vector<symtab_and_line> sals;
9361 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9362
6c5b2ebe 9363 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9364 {
9365 struct static_tracepoint_marker *marker;
9366
9367 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9368
51abb421 9369 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9370 sal.pc = marker->address;
9371 sals.push_back (sal);
0fb4aa4b
PA
9372
9373 release_static_tracepoint_marker (marker);
9374 }
9375
0fb4aa4b
PA
9376 *arg_p = endp;
9377 return sals;
9378}
9379
f00aae0f 9380/* See breakpoint.h. */
0101ce28 9381
8cdf0e15
VP
9382int
9383create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9384 const struct event_location *location,
9385 const char *cond_string,
9386 int thread, const char *extra_string,
f00aae0f 9387 int parse_extra,
0fb4aa4b 9388 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9389 int ignore_count,
9390 enum auto_boolean pending_break_support,
c0a91b2b 9391 const struct breakpoint_ops *ops,
44f238bb
PA
9392 int from_tty, int enabled, int internal,
9393 unsigned flags)
c3f6f71d 9394{
7efd8fc2 9395 struct linespec_result canonical;
80c99de1 9396 struct cleanup *bkpt_chain = NULL;
0101ce28 9397 int pending = 0;
4a306c9a 9398 int task = 0;
86b17b60 9399 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9400
348d480f
PA
9401 gdb_assert (ops != NULL);
9402
f00aae0f
KS
9403 /* If extra_string isn't useful, set it to NULL. */
9404 if (extra_string != NULL && *extra_string == '\0')
9405 extra_string = NULL;
9406
492d29ea 9407 TRY
b78a6381 9408 {
f00aae0f 9409 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9410 }
492d29ea 9411 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9412 {
492d29ea
PA
9413 /* If caller is interested in rc value from parse, set
9414 value. */
9415 if (e.error == NOT_FOUND_ERROR)
0101ce28 9416 {
05ff989b
AC
9417 /* If pending breakpoint support is turned off, throw
9418 error. */
fa8d40ab
JJ
9419
9420 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9421 throw_exception (e);
9422
9423 exception_print (gdb_stderr, e);
fa8d40ab 9424
05ff989b
AC
9425 /* If pending breakpoint support is auto query and the user
9426 selects no, then simply return the error code. */
059fb39f 9427 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9428 && !nquery (_("Make %s pending on future shared library load? "),
9429 bptype_string (type_wanted)))
fd9b8c24 9430 return 0;
fa8d40ab 9431
05ff989b
AC
9432 /* At this point, either the user was queried about setting
9433 a pending breakpoint and selected yes, or pending
9434 breakpoint behavior is on and thus a pending breakpoint
9435 is defaulted on behalf of the user. */
f00aae0f 9436 pending = 1;
0101ce28 9437 }
492d29ea
PA
9438 else
9439 throw_exception (e);
0101ce28 9440 }
492d29ea
PA
9441 END_CATCH
9442
6c5b2ebe 9443 if (!pending && canonical.lsals.empty ())
492d29ea 9444 return 0;
c3f6f71d 9445
c3f6f71d
JM
9446 /* ----------------------------- SNIP -----------------------------
9447 Anything added to the cleanup chain beyond this point is assumed
9448 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9449 then the memory is not reclaimed. */
9450 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9451
c3f6f71d
JM
9452 /* Resolve all line numbers to PC's and verify that the addresses
9453 are ok for the target. */
0101ce28 9454 if (!pending)
f8eba3c6 9455 {
6c5b2ebe
PA
9456 for (auto &lsal : canonical.lsals)
9457 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9458 }
c3f6f71d 9459
7a697b8d 9460 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9461 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9462 {
6c5b2ebe
PA
9463 for (const auto &lsal : canonical.lsals)
9464 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9465 }
7a697b8d 9466
c3f6f71d
JM
9467 /* Verify that condition can be parsed, before setting any
9468 breakpoints. Allocate a separate condition expression for each
4a64f543 9469 breakpoint. */
0101ce28 9470 if (!pending)
c3f6f71d 9471 {
e1e01040
PA
9472 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9473 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9474
f00aae0f 9475 if (parse_extra)
72b2ff0e 9476 {
0878d0fa 9477 char *rest;
e1e01040 9478 char *cond;
52d361e1 9479
6c5b2ebe 9480 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9481
0878d0fa
YQ
9482 /* Here we only parse 'arg' to separate condition
9483 from thread number, so parsing in context of first
9484 sal is OK. When setting the breakpoint we'll
9485 re-parse it in context of each sal. */
9486
6c5b2ebe 9487 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9488 &cond, &thread, &task, &rest);
9489 cond_string_copy.reset (cond);
9490 extra_string_copy.reset (rest);
72b2ff0e 9491 }
2f069f6f 9492 else
72b2ff0e 9493 {
f00aae0f
KS
9494 if (type_wanted != bp_dprintf
9495 && extra_string != NULL && *extra_string != '\0')
9496 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9497
9498 /* Create a private copy of condition string. */
9499 if (cond_string)
e1e01040 9500 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9501 /* Create a private copy of any extra string. */
9502 if (extra_string)
e1e01040 9503 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9504 }
0fb4aa4b 9505
52d361e1 9506 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9507 std::move (cond_string_copy),
9508 std::move (extra_string_copy),
9509 type_wanted,
d9b3f62e
PA
9510 tempflag ? disp_del : disp_donttouch,
9511 thread, task, ignore_count, ops,
44f238bb 9512 from_tty, enabled, internal, flags);
c906108c 9513 }
0101ce28
JJ
9514 else
9515 {
a5e364af 9516 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9517
a5e364af 9518 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9519 b->location = copy_event_location (location);
bfccc43c 9520
f00aae0f
KS
9521 if (parse_extra)
9522 b->cond_string = NULL;
e12c7713
MK
9523 else
9524 {
9525 /* Create a private copy of condition string. */
e1e01040 9526 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9527 b->thread = thread;
e12c7713 9528 }
f00aae0f
KS
9529
9530 /* Create a private copy of any extra string. */
e1e01040 9531 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9532 b->ignore_count = ignore_count;
0101ce28 9533 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9534 b->condition_not_parsed = 1;
41447f92 9535 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9536 if ((type_wanted != bp_breakpoint
9537 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9538 b->pspace = current_program_space;
8bea4e01 9539
b270e6f9 9540 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9541 }
9542
6c5b2ebe 9543 if (canonical.lsals.size () > 1)
95a42b64 9544 {
3e43a32a
MS
9545 warning (_("Multiple breakpoints were set.\nUse the "
9546 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9547 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9548 }
9549
80c99de1
PA
9550 /* That's it. Discard the cleanups for data inserted into the
9551 breakpoint. */
9552 discard_cleanups (bkpt_chain);
217dc9e2 9553
80c99de1 9554 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9555 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9556
9557 return 1;
c3f6f71d 9558}
c906108c 9559
348d480f 9560/* Set a breakpoint.
72b2ff0e
VP
9561 ARG is a string describing breakpoint address,
9562 condition, and thread.
9563 FLAG specifies if a breakpoint is hardware on,
9564 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9565 and BP_TEMPFLAG. */
348d480f 9566
98deb0da 9567static void
f2fc3015 9568break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9569{
72b2ff0e 9570 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9571 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9572 ? bp_hardware_breakpoint
9573 : bp_breakpoint);
55aa24fb 9574 struct breakpoint_ops *ops;
f00aae0f 9575
ffc2605c 9576 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9577
9578 /* Matching breakpoints on probes. */
5b56227b 9579 if (location != NULL
ffc2605c 9580 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9581 ops = &bkpt_probe_breakpoint_ops;
9582 else
9583 ops = &bkpt_breakpoint_ops;
c3f6f71d 9584
8cdf0e15 9585 create_breakpoint (get_current_arch (),
ffc2605c 9586 location.get (),
f00aae0f 9587 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9588 tempflag, type_wanted,
8cdf0e15
VP
9589 0 /* Ignore count */,
9590 pending_break_support,
55aa24fb 9591 ops,
8cdf0e15 9592 from_tty,
84f4c1fe 9593 1 /* enabled */,
44f238bb
PA
9594 0 /* internal */,
9595 0);
c906108c
SS
9596}
9597
c906108c
SS
9598/* Helper function for break_command_1 and disassemble_command. */
9599
9600void
fba45db2 9601resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9602{
9603 CORE_ADDR pc;
9604
9605 if (sal->pc == 0 && sal->symtab != NULL)
9606 {
9607 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9608 error (_("No line %d in file \"%s\"."),
05cba821 9609 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9610 sal->pc = pc;
6a048695 9611
4a64f543
MS
9612 /* If this SAL corresponds to a breakpoint inserted using a line
9613 number, then skip the function prologue if necessary. */
6a048695 9614 if (sal->explicit_line)
059acae7 9615 skip_prologue_sal (sal);
c906108c
SS
9616 }
9617
9618 if (sal->section == 0 && sal->symtab != NULL)
9619 {
346d1dfe 9620 const struct blockvector *bv;
3977b71f 9621 const struct block *b;
c5aa993b 9622 struct symbol *sym;
c906108c 9623
43f3e411
DE
9624 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9625 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9626 if (bv != NULL)
9627 {
7f0df278 9628 sym = block_linkage_function (b);
c906108c
SS
9629 if (sym != NULL)
9630 {
eb822aa6
DE
9631 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9632 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9633 sym);
c906108c
SS
9634 }
9635 else
9636 {
4a64f543
MS
9637 /* It really is worthwhile to have the section, so we'll
9638 just have to look harder. This case can be executed
9639 if we have line numbers but no functions (as can
9640 happen in assembly source). */
c906108c 9641
5ed8105e 9642 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9643 switch_to_program_space_and_thread (sal->pspace);
c906108c 9644
5ed8105e 9645 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9646 if (msym.minsym)
efd66ac6 9647 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9648 }
9649 }
9650 }
9651}
9652
9653void
fba45db2 9654break_command (char *arg, int from_tty)
c906108c 9655{
db107f19 9656 break_command_1 (arg, 0, from_tty);
c906108c
SS
9657}
9658
c906108c 9659void
fba45db2 9660tbreak_command (char *arg, int from_tty)
c906108c 9661{
db107f19 9662 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9663}
9664
c906108c 9665static void
fba45db2 9666hbreak_command (char *arg, int from_tty)
c906108c 9667{
db107f19 9668 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9669}
9670
9671static void
fba45db2 9672thbreak_command (char *arg, int from_tty)
c906108c 9673{
db107f19 9674 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9675}
9676
9677static void
fba45db2 9678stop_command (char *arg, int from_tty)
c906108c 9679{
a3f17187 9680 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9681Usage: stop in <function | address>\n\
a3f17187 9682 stop at <line>\n"));
c906108c
SS
9683}
9684
9685static void
4495129a 9686stopin_command (const char *arg, int from_tty)
c906108c
SS
9687{
9688 int badInput = 0;
9689
c5aa993b 9690 if (arg == (char *) NULL)
c906108c
SS
9691 badInput = 1;
9692 else if (*arg != '*')
9693 {
4495129a 9694 const char *argptr = arg;
c906108c
SS
9695 int hasColon = 0;
9696
4a64f543 9697 /* Look for a ':'. If this is a line number specification, then
53a5351d 9698 say it is bad, otherwise, it should be an address or
4a64f543 9699 function/method name. */
c906108c 9700 while (*argptr && !hasColon)
c5aa993b
JM
9701 {
9702 hasColon = (*argptr == ':');
9703 argptr++;
9704 }
c906108c
SS
9705
9706 if (hasColon)
c5aa993b 9707 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9708 else
c5aa993b 9709 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9710 }
9711
9712 if (badInput)
a3f17187 9713 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9714 else
db107f19 9715 break_command_1 (arg, 0, from_tty);
c906108c
SS
9716}
9717
9718static void
4495129a 9719stopat_command (const char *arg, int from_tty)
c906108c
SS
9720{
9721 int badInput = 0;
9722
c5aa993b 9723 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9724 badInput = 1;
9725 else
9726 {
4495129a 9727 const char *argptr = arg;
c906108c
SS
9728 int hasColon = 0;
9729
4a64f543
MS
9730 /* Look for a ':'. If there is a '::' then get out, otherwise
9731 it is probably a line number. */
c906108c 9732 while (*argptr && !hasColon)
c5aa993b
JM
9733 {
9734 hasColon = (*argptr == ':');
9735 argptr++;
9736 }
c906108c
SS
9737
9738 if (hasColon)
c5aa993b 9739 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9740 else
c5aa993b 9741 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9742 }
9743
9744 if (badInput)
a3f17187 9745 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9746 else
db107f19 9747 break_command_1 (arg, 0, from_tty);
c906108c
SS
9748}
9749
e7e0cddf
SS
9750/* The dynamic printf command is mostly like a regular breakpoint, but
9751 with a prewired command list consisting of a single output command,
9752 built from extra arguments supplied on the dprintf command
9753 line. */
9754
da821c7b 9755static void
f2fc3015 9756dprintf_command (char *arg_in, int from_tty)
e7e0cddf 9757{
f2fc3015 9758 const char *arg = arg_in;
ffc2605c 9759 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9760
9761 /* If non-NULL, ARG should have been advanced past the location;
9762 the next character must be ','. */
9763 if (arg != NULL)
9764 {
9765 if (arg[0] != ',' || arg[1] == '\0')
9766 error (_("Format string required"));
9767 else
9768 {
9769 /* Skip the comma. */
9770 ++arg;
9771 }
9772 }
9773
e7e0cddf 9774 create_breakpoint (get_current_arch (),
ffc2605c 9775 location.get (),
f00aae0f 9776 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9777 0, bp_dprintf,
9778 0 /* Ignore count */,
9779 pending_break_support,
9780 &dprintf_breakpoint_ops,
9781 from_tty,
9782 1 /* enabled */,
9783 0 /* internal */,
9784 0);
9785}
9786
d3ce09f5
SS
9787static void
9788agent_printf_command (char *arg, int from_tty)
9789{
9790 error (_("May only run agent-printf on the target"));
9791}
9792
f1310107
TJB
9793/* Implement the "breakpoint_hit" breakpoint_ops method for
9794 ranged breakpoints. */
9795
9796static int
9797breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9798 const address_space *aspace,
09ac7c10
TT
9799 CORE_ADDR bp_addr,
9800 const struct target_waitstatus *ws)
f1310107 9801{
09ac7c10 9802 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9803 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9804 return 0;
9805
f1310107
TJB
9806 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9807 bl->length, aspace, bp_addr);
9808}
9809
9810/* Implement the "resources_needed" breakpoint_ops method for
9811 ranged breakpoints. */
9812
9813static int
9814resources_needed_ranged_breakpoint (const struct bp_location *bl)
9815{
9816 return target_ranged_break_num_registers ();
9817}
9818
9819/* Implement the "print_it" breakpoint_ops method for
9820 ranged breakpoints. */
9821
9822static enum print_stop_action
348d480f 9823print_it_ranged_breakpoint (bpstat bs)
f1310107 9824{
348d480f 9825 struct breakpoint *b = bs->breakpoint_at;
f1310107 9826 struct bp_location *bl = b->loc;
79a45e25 9827 struct ui_out *uiout = current_uiout;
f1310107
TJB
9828
9829 gdb_assert (b->type == bp_hardware_breakpoint);
9830
9831 /* Ranged breakpoints have only one location. */
9832 gdb_assert (bl && bl->next == NULL);
9833
9834 annotate_breakpoint (b->number);
f303dbd6
PA
9835
9836 maybe_print_thread_hit_breakpoint (uiout);
9837
f1310107 9838 if (b->disposition == disp_del)
112e8700 9839 uiout->text ("Temporary ranged breakpoint ");
f1310107 9840 else
112e8700
SM
9841 uiout->text ("Ranged breakpoint ");
9842 if (uiout->is_mi_like_p ())
f1310107 9843 {
112e8700 9844 uiout->field_string ("reason",
f1310107 9845 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9846 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9847 }
112e8700
SM
9848 uiout->field_int ("bkptno", b->number);
9849 uiout->text (", ");
f1310107
TJB
9850
9851 return PRINT_SRC_AND_LOC;
9852}
9853
9854/* Implement the "print_one" breakpoint_ops method for
9855 ranged breakpoints. */
9856
9857static void
9858print_one_ranged_breakpoint (struct breakpoint *b,
9859 struct bp_location **last_loc)
9860{
9861 struct bp_location *bl = b->loc;
9862 struct value_print_options opts;
79a45e25 9863 struct ui_out *uiout = current_uiout;
f1310107
TJB
9864
9865 /* Ranged breakpoints have only one location. */
9866 gdb_assert (bl && bl->next == NULL);
9867
9868 get_user_print_options (&opts);
9869
9870 if (opts.addressprint)
9871 /* We don't print the address range here, it will be printed later
9872 by print_one_detail_ranged_breakpoint. */
112e8700 9873 uiout->field_skip ("addr");
f1310107
TJB
9874 annotate_field (5);
9875 print_breakpoint_location (b, bl);
9876 *last_loc = bl;
9877}
9878
9879/* Implement the "print_one_detail" breakpoint_ops method for
9880 ranged breakpoints. */
9881
9882static void
9883print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9884 struct ui_out *uiout)
9885{
9886 CORE_ADDR address_start, address_end;
9887 struct bp_location *bl = b->loc;
d7e74731 9888 string_file stb;
f1310107
TJB
9889
9890 gdb_assert (bl);
9891
9892 address_start = bl->address;
9893 address_end = address_start + bl->length - 1;
9894
112e8700 9895 uiout->text ("\taddress range: ");
d7e74731
PA
9896 stb.printf ("[%s, %s]",
9897 print_core_address (bl->gdbarch, address_start),
9898 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9899 uiout->field_stream ("addr", stb);
9900 uiout->text ("\n");
f1310107
TJB
9901}
9902
9903/* Implement the "print_mention" breakpoint_ops method for
9904 ranged breakpoints. */
9905
9906static void
9907print_mention_ranged_breakpoint (struct breakpoint *b)
9908{
9909 struct bp_location *bl = b->loc;
79a45e25 9910 struct ui_out *uiout = current_uiout;
f1310107
TJB
9911
9912 gdb_assert (bl);
9913 gdb_assert (b->type == bp_hardware_breakpoint);
9914
112e8700 9915 if (uiout->is_mi_like_p ())
f1310107
TJB
9916 return;
9917
9918 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9919 b->number, paddress (bl->gdbarch, bl->address),
9920 paddress (bl->gdbarch, bl->address + bl->length - 1));
9921}
9922
9923/* Implement the "print_recreate" breakpoint_ops method for
9924 ranged breakpoints. */
9925
9926static void
9927print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9928{
f00aae0f 9929 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9930 event_location_to_string (b->location.get ()),
9931 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9932 print_recreate_thread (b, fp);
f1310107
TJB
9933}
9934
9935/* The breakpoint_ops structure to be used in ranged breakpoints. */
9936
2060206e 9937static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9938
9939/* Find the address where the end of the breakpoint range should be
9940 placed, given the SAL of the end of the range. This is so that if
9941 the user provides a line number, the end of the range is set to the
9942 last instruction of the given line. */
9943
9944static CORE_ADDR
9945find_breakpoint_range_end (struct symtab_and_line sal)
9946{
9947 CORE_ADDR end;
9948
9949 /* If the user provided a PC value, use it. Otherwise,
9950 find the address of the end of the given location. */
9951 if (sal.explicit_pc)
9952 end = sal.pc;
9953 else
9954 {
9955 int ret;
9956 CORE_ADDR start;
9957
9958 ret = find_line_pc_range (sal, &start, &end);
9959 if (!ret)
9960 error (_("Could not find location of the end of the range."));
9961
9962 /* find_line_pc_range returns the start of the next line. */
9963 end--;
9964 }
9965
9966 return end;
9967}
9968
9969/* Implement the "break-range" CLI command. */
9970
9971static void
f2fc3015 9972break_range_command (char *arg_in, int from_tty)
f1310107 9973{
f2fc3015
TT
9974 const char *arg = arg_in;
9975 const char *arg_start;
f1310107
TJB
9976 struct linespec_result canonical_start, canonical_end;
9977 int bp_count, can_use_bp, length;
9978 CORE_ADDR end;
9979 struct breakpoint *b;
f1310107
TJB
9980
9981 /* We don't support software ranged breakpoints. */
9982 if (target_ranged_break_num_registers () < 0)
9983 error (_("This target does not support hardware ranged breakpoints."));
9984
9985 bp_count = hw_breakpoint_used_count ();
9986 bp_count += target_ranged_break_num_registers ();
9987 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9988 bp_count, 0);
9989 if (can_use_bp < 0)
9990 error (_("Hardware breakpoints used exceeds limit."));
9991
f8eba3c6 9992 arg = skip_spaces (arg);
f1310107
TJB
9993 if (arg == NULL || arg[0] == '\0')
9994 error(_("No address range specified."));
9995
f8eba3c6 9996 arg_start = arg;
ffc2605c
TT
9997 event_location_up start_location = string_to_event_location (&arg,
9998 current_language);
9999 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10000
10001 if (arg[0] != ',')
10002 error (_("Too few arguments."));
6c5b2ebe 10003 else if (canonical_start.lsals.empty ())
f1310107 10004 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10005
6c5b2ebe 10006 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10007
6c5b2ebe
PA
10008 if (canonical_start.lsals.size () > 1
10009 || lsal_start.sals.size () != 1)
f1310107
TJB
10010 error (_("Cannot create a ranged breakpoint with multiple locations."));
10011
6c5b2ebe 10012 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 10013 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
10014
10015 arg++; /* Skip the comma. */
f8eba3c6 10016 arg = skip_spaces (arg);
f1310107
TJB
10017
10018 /* Parse the end location. */
10019
f1310107
TJB
10020 arg_start = arg;
10021
f8eba3c6 10022 /* We call decode_line_full directly here instead of using
f1310107
TJB
10023 parse_breakpoint_sals because we need to specify the start location's
10024 symtab and line as the default symtab and line for the end of the
10025 range. This makes it possible to have ranges like "foo.c:27, +14",
10026 where +14 means 14 lines from the start location. */
ffc2605c
TT
10027 event_location_up end_location = string_to_event_location (&arg,
10028 current_language);
10029 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10030 sal_start.symtab, sal_start.line,
10031 &canonical_end, NULL, NULL);
10032
6c5b2ebe 10033 if (canonical_end.lsals.empty ())
f1310107 10034 error (_("Could not find location of the end of the range."));
f8eba3c6 10035
6c5b2ebe
PA
10036 const linespec_sals &lsal_end = canonical_end.lsals[0];
10037 if (canonical_end.lsals.size () > 1
10038 || lsal_end.sals.size () != 1)
f1310107
TJB
10039 error (_("Cannot create a ranged breakpoint with multiple locations."));
10040
6c5b2ebe 10041 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10042
10043 end = find_breakpoint_range_end (sal_end);
10044 if (sal_start.pc > end)
177b42fe 10045 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10046
10047 length = end - sal_start.pc + 1;
10048 if (length < 0)
10049 /* Length overflowed. */
10050 error (_("Address range too large."));
10051 else if (length == 1)
10052 {
10053 /* This range is simple enough to be handled by
10054 the `hbreak' command. */
81b1e71c 10055 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10056
10057 return;
10058 }
10059
10060 /* Now set up the breakpoint. */
10061 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10062 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10063 set_breakpoint_count (breakpoint_count + 1);
10064 b->number = breakpoint_count;
10065 b->disposition = disp_donttouch;
d28cd78a
TT
10066 b->location = std::move (start_location);
10067 b->location_range_end = std::move (end_location);
f1310107
TJB
10068 b->loc->length = length;
10069
f1310107 10070 mention (b);
8d3788bd 10071 observer_notify_breakpoint_created (b);
44702360 10072 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10073}
10074
4a64f543
MS
10075/* Return non-zero if EXP is verified as constant. Returned zero
10076 means EXP is variable. Also the constant detection may fail for
10077 some constant expressions and in such case still falsely return
10078 zero. */
2e6e3d9c 10079
65d79d4b
SDJ
10080static int
10081watchpoint_exp_is_const (const struct expression *exp)
10082{
10083 int i = exp->nelts;
10084
10085 while (i > 0)
10086 {
10087 int oplenp, argsp;
10088
10089 /* We are only interested in the descriptor of each element. */
10090 operator_length (exp, i, &oplenp, &argsp);
10091 i -= oplenp;
10092
10093 switch (exp->elts[i].opcode)
10094 {
10095 case BINOP_ADD:
10096 case BINOP_SUB:
10097 case BINOP_MUL:
10098 case BINOP_DIV:
10099 case BINOP_REM:
10100 case BINOP_MOD:
10101 case BINOP_LSH:
10102 case BINOP_RSH:
10103 case BINOP_LOGICAL_AND:
10104 case BINOP_LOGICAL_OR:
10105 case BINOP_BITWISE_AND:
10106 case BINOP_BITWISE_IOR:
10107 case BINOP_BITWISE_XOR:
10108 case BINOP_EQUAL:
10109 case BINOP_NOTEQUAL:
10110 case BINOP_LESS:
10111 case BINOP_GTR:
10112 case BINOP_LEQ:
10113 case BINOP_GEQ:
10114 case BINOP_REPEAT:
10115 case BINOP_COMMA:
10116 case BINOP_EXP:
10117 case BINOP_MIN:
10118 case BINOP_MAX:
10119 case BINOP_INTDIV:
10120 case BINOP_CONCAT:
65d79d4b
SDJ
10121 case TERNOP_COND:
10122 case TERNOP_SLICE:
65d79d4b
SDJ
10123
10124 case OP_LONG:
edd079d9 10125 case OP_FLOAT:
65d79d4b
SDJ
10126 case OP_LAST:
10127 case OP_COMPLEX:
10128 case OP_STRING:
65d79d4b
SDJ
10129 case OP_ARRAY:
10130 case OP_TYPE:
608b4967
TT
10131 case OP_TYPEOF:
10132 case OP_DECLTYPE:
6e72ca20 10133 case OP_TYPEID:
65d79d4b
SDJ
10134 case OP_NAME:
10135 case OP_OBJC_NSSTRING:
10136
10137 case UNOP_NEG:
10138 case UNOP_LOGICAL_NOT:
10139 case UNOP_COMPLEMENT:
10140 case UNOP_ADDR:
10141 case UNOP_HIGH:
aeaa2474 10142 case UNOP_CAST:
9eaf6705
TT
10143
10144 case UNOP_CAST_TYPE:
10145 case UNOP_REINTERPRET_CAST:
10146 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10147 /* Unary, binary and ternary operators: We have to check
10148 their operands. If they are constant, then so is the
10149 result of that operation. For instance, if A and B are
10150 determined to be constants, then so is "A + B".
10151
10152 UNOP_IND is one exception to the rule above, because the
10153 value of *ADDR is not necessarily a constant, even when
10154 ADDR is. */
65d79d4b
SDJ
10155 break;
10156
10157 case OP_VAR_VALUE:
10158 /* Check whether the associated symbol is a constant.
4a64f543 10159
65d79d4b 10160 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10161 possible that a buggy compiler could mark a variable as
10162 constant even when it is not, and TYPE_CONST would return
10163 true in this case, while SYMBOL_CLASS wouldn't.
10164
10165 We also have to check for function symbols because they
10166 are always constant. */
65d79d4b
SDJ
10167 {
10168 struct symbol *s = exp->elts[i + 2].symbol;
10169
10170 if (SYMBOL_CLASS (s) != LOC_BLOCK
10171 && SYMBOL_CLASS (s) != LOC_CONST
10172 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10173 return 0;
10174 break;
10175 }
10176
10177 /* The default action is to return 0 because we are using
10178 the optimistic approach here: If we don't know something,
10179 then it is not a constant. */
10180 default:
10181 return 0;
10182 }
10183 }
10184
10185 return 1;
10186}
10187
c1fc2657 10188/* Watchpoint destructor. */
3a5c3e22 10189
c1fc2657 10190watchpoint::~watchpoint ()
3a5c3e22 10191{
c1fc2657
SM
10192 xfree (this->exp_string);
10193 xfree (this->exp_string_reparse);
10194 value_free (this->val);
3a5c3e22
PA
10195}
10196
348d480f
PA
10197/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10198
10199static void
10200re_set_watchpoint (struct breakpoint *b)
10201{
3a5c3e22
PA
10202 struct watchpoint *w = (struct watchpoint *) b;
10203
348d480f
PA
10204 /* Watchpoint can be either on expression using entirely global
10205 variables, or it can be on local variables.
10206
10207 Watchpoints of the first kind are never auto-deleted, and even
10208 persist across program restarts. Since they can use variables
10209 from shared libraries, we need to reparse expression as libraries
10210 are loaded and unloaded.
10211
10212 Watchpoints on local variables can also change meaning as result
10213 of solib event. For example, if a watchpoint uses both a local
10214 and a global variables in expression, it's a local watchpoint,
10215 but unloading of a shared library will make the expression
10216 invalid. This is not a very common use case, but we still
10217 re-evaluate expression, to avoid surprises to the user.
10218
10219 Note that for local watchpoints, we re-evaluate it only if
10220 watchpoints frame id is still valid. If it's not, it means the
10221 watchpoint is out of scope and will be deleted soon. In fact,
10222 I'm not sure we'll ever be called in this case.
10223
10224 If a local watchpoint's frame id is still valid, then
3a5c3e22 10225 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10226
3a5c3e22
PA
10227 Don't do anything about disabled watchpoints, since they will be
10228 reevaluated again when enabled. */
10229 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10230}
10231
77b06cd7
TJB
10232/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10233
10234static int
10235insert_watchpoint (struct bp_location *bl)
10236{
3a5c3e22
PA
10237 struct watchpoint *w = (struct watchpoint *) bl->owner;
10238 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10239
10240 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10241 w->cond_exp.get ());
77b06cd7
TJB
10242}
10243
10244/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10245
10246static int
73971819 10247remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10248{
3a5c3e22
PA
10249 struct watchpoint *w = (struct watchpoint *) bl->owner;
10250 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10251
10252 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10253 w->cond_exp.get ());
e09342b5
TJB
10254}
10255
e09342b5 10256static int
348d480f 10257breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10258 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10259 const struct target_waitstatus *ws)
e09342b5 10260{
348d480f 10261 struct breakpoint *b = bl->owner;
3a5c3e22 10262 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10263
348d480f
PA
10264 /* Continuable hardware watchpoints are treated as non-existent if the
10265 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10266 some data address). Otherwise gdb won't stop on a break instruction
10267 in the code (not from a breakpoint) when a hardware watchpoint has
10268 been defined. Also skip watchpoints which we know did not trigger
10269 (did not match the data address). */
10270 if (is_hardware_watchpoint (b)
3a5c3e22 10271 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10272 return 0;
9c06b0b4 10273
348d480f 10274 return 1;
9c06b0b4
TJB
10275}
10276
348d480f
PA
10277static void
10278check_status_watchpoint (bpstat bs)
9c06b0b4 10279{
348d480f 10280 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10281
348d480f 10282 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10283}
10284
10285/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10286 hardware watchpoints. */
9c06b0b4
TJB
10287
10288static int
348d480f 10289resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10290{
3a5c3e22
PA
10291 struct watchpoint *w = (struct watchpoint *) bl->owner;
10292 int length = w->exact? 1 : bl->length;
348d480f
PA
10293
10294 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10295}
10296
10297/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10298 hardware watchpoints. */
9c06b0b4
TJB
10299
10300static int
348d480f 10301works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10302{
efa80663
PA
10303 /* Read and access watchpoints only work with hardware support. */
10304 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10305}
10306
9c06b0b4 10307static enum print_stop_action
348d480f 10308print_it_watchpoint (bpstat bs)
9c06b0b4 10309{
348d480f 10310 struct breakpoint *b;
348d480f 10311 enum print_stop_action result;
3a5c3e22 10312 struct watchpoint *w;
79a45e25 10313 struct ui_out *uiout = current_uiout;
348d480f
PA
10314
10315 gdb_assert (bs->bp_location_at != NULL);
10316
348d480f 10317 b = bs->breakpoint_at;
3a5c3e22 10318 w = (struct watchpoint *) b;
348d480f 10319
f303dbd6
PA
10320 annotate_watchpoint (b->number);
10321 maybe_print_thread_hit_breakpoint (uiout);
10322
d7e74731
PA
10323 string_file stb;
10324
76f9c9cf 10325 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10326 switch (b->type)
10327 {
348d480f 10328 case bp_watchpoint:
9c06b0b4 10329 case bp_hardware_watchpoint:
112e8700
SM
10330 if (uiout->is_mi_like_p ())
10331 uiout->field_string
10332 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10333 mention (b);
76f9c9cf 10334 tuple_emitter.emplace (uiout, "value");
112e8700 10335 uiout->text ("\nOld value = ");
d7e74731 10336 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10337 uiout->field_stream ("old", stb);
10338 uiout->text ("\nNew value = ");
d7e74731 10339 watchpoint_value_print (w->val, &stb);
112e8700
SM
10340 uiout->field_stream ("new", stb);
10341 uiout->text ("\n");
348d480f
PA
10342 /* More than one watchpoint may have been triggered. */
10343 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10344 break;
10345
10346 case bp_read_watchpoint:
112e8700
SM
10347 if (uiout->is_mi_like_p ())
10348 uiout->field_string
10349 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10350 mention (b);
76f9c9cf 10351 tuple_emitter.emplace (uiout, "value");
112e8700 10352 uiout->text ("\nValue = ");
d7e74731 10353 watchpoint_value_print (w->val, &stb);
112e8700
SM
10354 uiout->field_stream ("value", stb);
10355 uiout->text ("\n");
348d480f 10356 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10357 break;
10358
10359 case bp_access_watchpoint:
348d480f
PA
10360 if (bs->old_val != NULL)
10361 {
112e8700
SM
10362 if (uiout->is_mi_like_p ())
10363 uiout->field_string
10364 ("reason",
348d480f
PA
10365 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10366 mention (b);
76f9c9cf 10367 tuple_emitter.emplace (uiout, "value");
112e8700 10368 uiout->text ("\nOld value = ");
d7e74731 10369 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10370 uiout->field_stream ("old", stb);
10371 uiout->text ("\nNew value = ");
348d480f
PA
10372 }
10373 else
10374 {
10375 mention (b);
112e8700
SM
10376 if (uiout->is_mi_like_p ())
10377 uiout->field_string
10378 ("reason",
348d480f 10379 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10380 tuple_emitter.emplace (uiout, "value");
112e8700 10381 uiout->text ("\nValue = ");
348d480f 10382 }
d7e74731 10383 watchpoint_value_print (w->val, &stb);
112e8700
SM
10384 uiout->field_stream ("new", stb);
10385 uiout->text ("\n");
348d480f 10386 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10387 break;
10388 default:
348d480f 10389 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10390 }
10391
348d480f
PA
10392 return result;
10393}
10394
10395/* Implement the "print_mention" breakpoint_ops method for hardware
10396 watchpoints. */
10397
10398static void
10399print_mention_watchpoint (struct breakpoint *b)
10400{
3a5c3e22 10401 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10402 struct ui_out *uiout = current_uiout;
46b9c129 10403 const char *tuple_name;
348d480f
PA
10404
10405 switch (b->type)
10406 {
10407 case bp_watchpoint:
112e8700 10408 uiout->text ("Watchpoint ");
46b9c129 10409 tuple_name = "wpt";
348d480f
PA
10410 break;
10411 case bp_hardware_watchpoint:
112e8700 10412 uiout->text ("Hardware watchpoint ");
46b9c129 10413 tuple_name = "wpt";
348d480f
PA
10414 break;
10415 case bp_read_watchpoint:
112e8700 10416 uiout->text ("Hardware read watchpoint ");
46b9c129 10417 tuple_name = "hw-rwpt";
348d480f
PA
10418 break;
10419 case bp_access_watchpoint:
112e8700 10420 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10421 tuple_name = "hw-awpt";
348d480f
PA
10422 break;
10423 default:
10424 internal_error (__FILE__, __LINE__,
10425 _("Invalid hardware watchpoint type."));
10426 }
10427
46b9c129 10428 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10429 uiout->field_int ("number", b->number);
10430 uiout->text (": ");
10431 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10432}
10433
10434/* Implement the "print_recreate" breakpoint_ops method for
10435 watchpoints. */
10436
10437static void
10438print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10439{
3a5c3e22
PA
10440 struct watchpoint *w = (struct watchpoint *) b;
10441
348d480f
PA
10442 switch (b->type)
10443 {
10444 case bp_watchpoint:
10445 case bp_hardware_watchpoint:
10446 fprintf_unfiltered (fp, "watch");
10447 break;
10448 case bp_read_watchpoint:
10449 fprintf_unfiltered (fp, "rwatch");
10450 break;
10451 case bp_access_watchpoint:
10452 fprintf_unfiltered (fp, "awatch");
10453 break;
10454 default:
10455 internal_error (__FILE__, __LINE__,
10456 _("Invalid watchpoint type."));
10457 }
10458
3a5c3e22 10459 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10460 print_recreate_thread (b, fp);
348d480f
PA
10461}
10462
427cd150
TT
10463/* Implement the "explains_signal" breakpoint_ops method for
10464 watchpoints. */
10465
47591c29 10466static int
427cd150
TT
10467explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10468{
10469 /* A software watchpoint cannot cause a signal other than
10470 GDB_SIGNAL_TRAP. */
10471 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10472 return 0;
427cd150 10473
47591c29 10474 return 1;
427cd150
TT
10475}
10476
348d480f
PA
10477/* The breakpoint_ops structure to be used in hardware watchpoints. */
10478
2060206e 10479static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10480
10481/* Implement the "insert" breakpoint_ops method for
10482 masked hardware watchpoints. */
10483
10484static int
10485insert_masked_watchpoint (struct bp_location *bl)
10486{
3a5c3e22
PA
10487 struct watchpoint *w = (struct watchpoint *) bl->owner;
10488
10489 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10490 bl->watchpoint_type);
10491}
10492
10493/* Implement the "remove" breakpoint_ops method for
10494 masked hardware watchpoints. */
10495
10496static int
73971819 10497remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10498{
3a5c3e22
PA
10499 struct watchpoint *w = (struct watchpoint *) bl->owner;
10500
10501 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10502 bl->watchpoint_type);
10503}
10504
10505/* Implement the "resources_needed" breakpoint_ops method for
10506 masked hardware watchpoints. */
10507
10508static int
10509resources_needed_masked_watchpoint (const struct bp_location *bl)
10510{
3a5c3e22
PA
10511 struct watchpoint *w = (struct watchpoint *) bl->owner;
10512
10513 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10514}
10515
10516/* Implement the "works_in_software_mode" breakpoint_ops method for
10517 masked hardware watchpoints. */
10518
10519static int
10520works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10521{
10522 return 0;
10523}
10524
10525/* Implement the "print_it" breakpoint_ops method for
10526 masked hardware watchpoints. */
10527
10528static enum print_stop_action
10529print_it_masked_watchpoint (bpstat bs)
10530{
10531 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10532 struct ui_out *uiout = current_uiout;
348d480f
PA
10533
10534 /* Masked watchpoints have only one location. */
10535 gdb_assert (b->loc && b->loc->next == NULL);
10536
f303dbd6
PA
10537 annotate_watchpoint (b->number);
10538 maybe_print_thread_hit_breakpoint (uiout);
10539
348d480f
PA
10540 switch (b->type)
10541 {
10542 case bp_hardware_watchpoint:
112e8700
SM
10543 if (uiout->is_mi_like_p ())
10544 uiout->field_string
10545 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10546 break;
10547
10548 case bp_read_watchpoint:
112e8700
SM
10549 if (uiout->is_mi_like_p ())
10550 uiout->field_string
10551 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10552 break;
10553
10554 case bp_access_watchpoint:
112e8700
SM
10555 if (uiout->is_mi_like_p ())
10556 uiout->field_string
10557 ("reason",
348d480f
PA
10558 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10559 break;
10560 default:
10561 internal_error (__FILE__, __LINE__,
10562 _("Invalid hardware watchpoint type."));
10563 }
10564
10565 mention (b);
112e8700 10566 uiout->text (_("\n\
9c06b0b4
TJB
10567Check the underlying instruction at PC for the memory\n\
10568address and value which triggered this watchpoint.\n"));
112e8700 10569 uiout->text ("\n");
9c06b0b4
TJB
10570
10571 /* More than one watchpoint may have been triggered. */
10572 return PRINT_UNKNOWN;
10573}
10574
10575/* Implement the "print_one_detail" breakpoint_ops method for
10576 masked hardware watchpoints. */
10577
10578static void
10579print_one_detail_masked_watchpoint (const struct breakpoint *b,
10580 struct ui_out *uiout)
10581{
3a5c3e22
PA
10582 struct watchpoint *w = (struct watchpoint *) b;
10583
9c06b0b4
TJB
10584 /* Masked watchpoints have only one location. */
10585 gdb_assert (b->loc && b->loc->next == NULL);
10586
112e8700
SM
10587 uiout->text ("\tmask ");
10588 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10589 uiout->text ("\n");
9c06b0b4
TJB
10590}
10591
10592/* Implement the "print_mention" breakpoint_ops method for
10593 masked hardware watchpoints. */
10594
10595static void
10596print_mention_masked_watchpoint (struct breakpoint *b)
10597{
3a5c3e22 10598 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10599 struct ui_out *uiout = current_uiout;
46b9c129 10600 const char *tuple_name;
9c06b0b4
TJB
10601
10602 switch (b->type)
10603 {
10604 case bp_hardware_watchpoint:
112e8700 10605 uiout->text ("Masked hardware watchpoint ");
46b9c129 10606 tuple_name = "wpt";
9c06b0b4
TJB
10607 break;
10608 case bp_read_watchpoint:
112e8700 10609 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10610 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10611 break;
10612 case bp_access_watchpoint:
112e8700 10613 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10614 tuple_name = "hw-awpt";
9c06b0b4
TJB
10615 break;
10616 default:
10617 internal_error (__FILE__, __LINE__,
10618 _("Invalid hardware watchpoint type."));
10619 }
10620
46b9c129 10621 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10622 uiout->field_int ("number", b->number);
10623 uiout->text (": ");
10624 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10625}
10626
10627/* Implement the "print_recreate" breakpoint_ops method for
10628 masked hardware watchpoints. */
10629
10630static void
10631print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10632{
3a5c3e22 10633 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10634 char tmp[40];
10635
10636 switch (b->type)
10637 {
10638 case bp_hardware_watchpoint:
10639 fprintf_unfiltered (fp, "watch");
10640 break;
10641 case bp_read_watchpoint:
10642 fprintf_unfiltered (fp, "rwatch");
10643 break;
10644 case bp_access_watchpoint:
10645 fprintf_unfiltered (fp, "awatch");
10646 break;
10647 default:
10648 internal_error (__FILE__, __LINE__,
10649 _("Invalid hardware watchpoint type."));
10650 }
10651
3a5c3e22
PA
10652 sprintf_vma (tmp, w->hw_wp_mask);
10653 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10654 print_recreate_thread (b, fp);
9c06b0b4
TJB
10655}
10656
10657/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10658
2060206e 10659static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10660
10661/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10662
10663static int
10664is_masked_watchpoint (const struct breakpoint *b)
10665{
10666 return b->ops == &masked_watchpoint_breakpoint_ops;
10667}
10668
53a5351d
JM
10669/* accessflag: hw_write: watch write,
10670 hw_read: watch read,
10671 hw_access: watch access (read or write) */
c906108c 10672static void
bbc13ae3 10673watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10674 int just_location, int internal)
c906108c 10675{
c1fc2657 10676 struct breakpoint *scope_breakpoint = NULL;
270140bd 10677 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10678 struct value *val, *mark, *result;
bb9d5f81 10679 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10680 const char *exp_start = NULL;
10681 const char *exp_end = NULL;
10682 const char *tok, *end_tok;
9c06b0b4 10683 int toklen = -1;
bbc13ae3
KS
10684 const char *cond_start = NULL;
10685 const char *cond_end = NULL;
c906108c 10686 enum bptype bp_type;
37e4754d 10687 int thread = -1;
0cf6dd15 10688 int pc = 0;
9c06b0b4
TJB
10689 /* Flag to indicate whether we are going to use masks for
10690 the hardware watchpoint. */
10691 int use_mask = 0;
10692 CORE_ADDR mask = 0;
c906108c 10693
37e4754d
LM
10694 /* Make sure that we actually have parameters to parse. */
10695 if (arg != NULL && arg[0] != '\0')
10696 {
bbc13ae3
KS
10697 const char *value_start;
10698
10699 exp_end = arg + strlen (arg);
37e4754d 10700
9c06b0b4
TJB
10701 /* Look for "parameter value" pairs at the end
10702 of the arguments string. */
bbc13ae3 10703 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10704 {
10705 /* Skip whitespace at the end of the argument list. */
10706 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10707 tok--;
10708
10709 /* Find the beginning of the last token.
10710 This is the value of the parameter. */
10711 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10712 tok--;
10713 value_start = tok + 1;
10714
10715 /* Skip whitespace. */
10716 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10717 tok--;
10718
10719 end_tok = tok;
10720
10721 /* Find the beginning of the second to last token.
10722 This is the parameter itself. */
10723 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10724 tok--;
10725 tok++;
10726 toklen = end_tok - tok + 1;
10727
61012eef 10728 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10729 {
5d5658a1 10730 struct thread_info *thr;
9c06b0b4
TJB
10731 /* At this point we've found a "thread" token, which means
10732 the user is trying to set a watchpoint that triggers
10733 only in a specific thread. */
5d5658a1 10734 const char *endp;
37e4754d 10735
9c06b0b4
TJB
10736 if (thread != -1)
10737 error(_("You can specify only one thread."));
37e4754d 10738
9c06b0b4 10739 /* Extract the thread ID from the next token. */
5d5658a1 10740 thr = parse_thread_id (value_start, &endp);
37e4754d 10741
5d5658a1 10742 /* Check if the user provided a valid thread ID. */
9c06b0b4 10743 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10744 invalid_thread_id_error (value_start);
9c06b0b4 10745
5d5658a1 10746 thread = thr->global_num;
9c06b0b4 10747 }
61012eef 10748 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10749 {
10750 /* We've found a "mask" token, which means the user wants to
10751 create a hardware watchpoint that is going to have the mask
10752 facility. */
10753 struct value *mask_value, *mark;
37e4754d 10754
9c06b0b4
TJB
10755 if (use_mask)
10756 error(_("You can specify only one mask."));
37e4754d 10757
9c06b0b4 10758 use_mask = just_location = 1;
37e4754d 10759
9c06b0b4
TJB
10760 mark = value_mark ();
10761 mask_value = parse_to_comma_and_eval (&value_start);
10762 mask = value_as_address (mask_value);
10763 value_free_to_mark (mark);
10764 }
10765 else
10766 /* We didn't recognize what we found. We should stop here. */
10767 break;
37e4754d 10768
9c06b0b4
TJB
10769 /* Truncate the string and get rid of the "parameter value" pair before
10770 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10771 exp_end = tok;
9c06b0b4 10772 }
37e4754d 10773 }
bbc13ae3
KS
10774 else
10775 exp_end = arg;
37e4754d 10776
bbc13ae3
KS
10777 /* Parse the rest of the arguments. From here on out, everything
10778 is in terms of a newly allocated string instead of the original
10779 ARG. */
c906108c 10780 innermost_block = NULL;
81b1e71c
TT
10781 std::string expression (arg, exp_end - arg);
10782 exp_start = arg = expression.c_str ();
4d01a485 10783 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10784 exp_end = arg;
fa8a61dc
TT
10785 /* Remove trailing whitespace from the expression before saving it.
10786 This makes the eventual display of the expression string a bit
10787 prettier. */
10788 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10789 --exp_end;
10790
65d79d4b 10791 /* Checking if the expression is not constant. */
4d01a485 10792 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10793 {
10794 int len;
10795
10796 len = exp_end - exp_start;
10797 while (len > 0 && isspace (exp_start[len - 1]))
10798 len--;
10799 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10800 }
10801
c906108c
SS
10802 exp_valid_block = innermost_block;
10803 mark = value_mark ();
4d01a485 10804 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10805
bb9d5f81
PP
10806 if (val != NULL && just_location)
10807 {
10808 saved_bitpos = value_bitpos (val);
10809 saved_bitsize = value_bitsize (val);
10810 }
10811
06a64a0b
TT
10812 if (just_location)
10813 {
9c06b0b4
TJB
10814 int ret;
10815
06a64a0b 10816 exp_valid_block = NULL;
a1442452 10817 val = value_addr (result);
06a64a0b
TT
10818 release_value (val);
10819 value_free_to_mark (mark);
9c06b0b4
TJB
10820
10821 if (use_mask)
10822 {
10823 ret = target_masked_watch_num_registers (value_as_address (val),
10824 mask);
10825 if (ret == -1)
10826 error (_("This target does not support masked watchpoints."));
10827 else if (ret == -2)
10828 error (_("Invalid mask or memory region."));
10829 }
06a64a0b
TT
10830 }
10831 else if (val != NULL)
fa4727a6 10832 release_value (val);
c906108c 10833
f1735a53
TT
10834 tok = skip_spaces (arg);
10835 end_tok = skip_to_space (tok);
c906108c
SS
10836
10837 toklen = end_tok - tok;
10838 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10839 {
60e1c644 10840 innermost_block = NULL;
c906108c 10841 tok = cond_start = end_tok + 1;
4d01a485 10842 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10843
10844 /* The watchpoint expression may not be local, but the condition
10845 may still be. E.g.: `watch global if local > 0'. */
10846 cond_exp_valid_block = innermost_block;
10847
c906108c
SS
10848 cond_end = tok;
10849 }
10850 if (*tok)
8a3fe4f8 10851 error (_("Junk at end of command."));
c906108c 10852
441d7c93
PA
10853 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10854
10855 /* Save this because create_internal_breakpoint below invalidates
10856 'wp_frame'. */
10857 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10858
10859 /* If the expression is "local", then set up a "watchpoint scope"
10860 breakpoint at the point where we've left the scope of the watchpoint
10861 expression. Create the scope breakpoint before the watchpoint, so
10862 that we will encounter it first in bpstat_stop_status. */
441d7c93 10863 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10864 {
441d7c93
PA
10865 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10866
10867 if (frame_id_p (caller_frame_id))
edb3359d 10868 {
441d7c93
PA
10869 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10870 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10871
edb3359d 10872 scope_breakpoint
441d7c93 10873 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10874 bp_watchpoint_scope,
10875 &momentary_breakpoint_ops);
d983da9c 10876
441d7c93
PA
10877 /* create_internal_breakpoint could invalidate WP_FRAME. */
10878 wp_frame = NULL;
10879
edb3359d 10880 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10881
edb3359d
DJ
10882 /* Automatically delete the breakpoint when it hits. */
10883 scope_breakpoint->disposition = disp_del;
d983da9c 10884
edb3359d 10885 /* Only break in the proper frame (help with recursion). */
441d7c93 10886 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10887
edb3359d 10888 /* Set the address at which we will stop. */
441d7c93
PA
10889 scope_breakpoint->loc->gdbarch = caller_arch;
10890 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10891 scope_breakpoint->loc->address
a6d9a66e
UW
10892 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10893 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10894 scope_breakpoint->type);
10895 }
d983da9c
DJ
10896 }
10897
e8369a73
AB
10898 /* Now set up the breakpoint. We create all watchpoints as hardware
10899 watchpoints here even if hardware watchpoints are turned off, a call
10900 to update_watchpoint later in this function will cause the type to
10901 drop back to bp_watchpoint (software watchpoint) if required. */
10902
10903 if (accessflag == hw_read)
10904 bp_type = bp_read_watchpoint;
10905 else if (accessflag == hw_access)
10906 bp_type = bp_access_watchpoint;
10907 else
10908 bp_type = bp_hardware_watchpoint;
3a5c3e22 10909
b270e6f9 10910 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10911
348d480f 10912 if (use_mask)
b270e6f9 10913 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10914 &masked_watchpoint_breakpoint_ops);
348d480f 10915 else
b270e6f9 10916 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10917 &watchpoint_breakpoint_ops);
c1fc2657
SM
10918 w->thread = thread;
10919 w->disposition = disp_donttouch;
10920 w->pspace = current_program_space;
b22e99fd 10921 w->exp = std::move (exp);
3a5c3e22
PA
10922 w->exp_valid_block = exp_valid_block;
10923 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10924 if (just_location)
10925 {
10926 struct type *t = value_type (val);
10927 CORE_ADDR addr = value_as_address (val);
06a64a0b 10928
43cc5389
TT
10929 w->exp_string_reparse
10930 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10931
3a5c3e22 10932 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10933 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10934 }
10935 else
3a5c3e22 10936 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10937
10938 if (use_mask)
10939 {
3a5c3e22 10940 w->hw_wp_mask = mask;
9c06b0b4
TJB
10941 }
10942 else
10943 {
3a5c3e22 10944 w->val = val;
bb9d5f81
PP
10945 w->val_bitpos = saved_bitpos;
10946 w->val_bitsize = saved_bitsize;
3a5c3e22 10947 w->val_valid = 1;
9c06b0b4 10948 }
77b06cd7 10949
c906108c 10950 if (cond_start)
c1fc2657 10951 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10952 else
c1fc2657 10953 w->cond_string = 0;
c5aa993b 10954
441d7c93 10955 if (frame_id_p (watchpoint_frame))
f6bc2008 10956 {
441d7c93 10957 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10958 w->watchpoint_thread = inferior_ptid;
f6bc2008 10959 }
c906108c 10960 else
f6bc2008 10961 {
3a5c3e22
PA
10962 w->watchpoint_frame = null_frame_id;
10963 w->watchpoint_thread = null_ptid;
f6bc2008 10964 }
c906108c 10965
d983da9c 10966 if (scope_breakpoint != NULL)
c906108c 10967 {
d983da9c
DJ
10968 /* The scope breakpoint is related to the watchpoint. We will
10969 need to act on them together. */
c1fc2657 10970 w->related_breakpoint = scope_breakpoint;
b270e6f9 10971 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10972 }
d983da9c 10973
06a64a0b
TT
10974 if (!just_location)
10975 value_free_to_mark (mark);
2d134ed3 10976
b270e6f9
TT
10977 /* Finally update the new watchpoint. This creates the locations
10978 that should be inserted. */
10979 update_watchpoint (w.get (), 1);
a9634178 10980
b270e6f9 10981 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10982}
10983
e09342b5 10984/* Return count of debug registers needed to watch the given expression.
e09342b5 10985 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10986
c906108c 10987static int
a9634178 10988can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10989{
10990 int found_memory_cnt = 0;
2e70b7b9 10991 struct value *head = v;
c906108c
SS
10992
10993 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10994 if (!can_use_hw_watchpoints)
c906108c 10995 return 0;
c5aa993b 10996
5c44784c
JM
10997 /* Make sure that the value of the expression depends only upon
10998 memory contents, and values computed from them within GDB. If we
10999 find any register references or function calls, we can't use a
11000 hardware watchpoint.
11001
11002 The idea here is that evaluating an expression generates a series
11003 of values, one holding the value of every subexpression. (The
11004 expression a*b+c has five subexpressions: a, b, a*b, c, and
11005 a*b+c.) GDB's values hold almost enough information to establish
11006 the criteria given above --- they identify memory lvalues,
11007 register lvalues, computed values, etcetera. So we can evaluate
11008 the expression, and then scan the chain of values that leaves
11009 behind to decide whether we can detect any possible change to the
11010 expression's final value using only hardware watchpoints.
11011
11012 However, I don't think that the values returned by inferior
11013 function calls are special in any way. So this function may not
11014 notice that an expression involving an inferior function call
11015 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11016 for (; v; v = value_next (v))
c906108c 11017 {
5c44784c 11018 if (VALUE_LVAL (v) == lval_memory)
c906108c 11019 {
8464be76
DJ
11020 if (v != head && value_lazy (v))
11021 /* A lazy memory lvalue in the chain is one that GDB never
11022 needed to fetch; we either just used its address (e.g.,
11023 `a' in `a.b') or we never needed it at all (e.g., `a'
11024 in `a,b'). This doesn't apply to HEAD; if that is
11025 lazy then it was not readable, but watch it anyway. */
5c44784c 11026 ;
53a5351d 11027 else
5c44784c
JM
11028 {
11029 /* Ahh, memory we actually used! Check if we can cover
11030 it with hardware watchpoints. */
df407dfe 11031 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11032
11033 /* We only watch structs and arrays if user asked for it
11034 explicitly, never if they just happen to appear in a
11035 middle of some value chain. */
11036 if (v == head
11037 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11038 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11039 {
42ae5230 11040 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11041 int len;
11042 int num_regs;
11043
a9634178 11044 len = (target_exact_watchpoints
e09342b5
TJB
11045 && is_scalar_type_recursive (vtype))?
11046 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11047
e09342b5
TJB
11048 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11049 if (!num_regs)
2e70b7b9
MS
11050 return 0;
11051 else
e09342b5 11052 found_memory_cnt += num_regs;
2e70b7b9 11053 }
5c44784c 11054 }
c5aa993b 11055 }
5086187c
AC
11056 else if (VALUE_LVAL (v) != not_lval
11057 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11058 return 0; /* These are values from the history (e.g., $1). */
5086187c 11059 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11060 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11061 }
11062
11063 /* The expression itself looks suitable for using a hardware
11064 watchpoint, but give the target machine a chance to reject it. */
11065 return found_memory_cnt;
11066}
11067
8b93c638 11068void
f2fc3015 11069watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11070{
84f4c1fe 11071 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11072}
11073
06a64a0b
TT
11074/* A helper function that looks for the "-location" argument and then
11075 calls watch_command_1. */
11076
11077static void
11078watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11079{
11080 int just_location = 0;
11081
11082 if (arg
11083 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11084 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11085 {
e9cafbcc 11086 arg = skip_spaces (arg);
06a64a0b
TT
11087 just_location = 1;
11088 }
11089
84f4c1fe 11090 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11091}
8926118c 11092
c5aa993b 11093static void
fba45db2 11094watch_command (char *arg, int from_tty)
c906108c 11095{
06a64a0b 11096 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11097}
11098
8b93c638 11099void
f2fc3015 11100rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11101{
84f4c1fe 11102 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11103}
8926118c 11104
c5aa993b 11105static void
fba45db2 11106rwatch_command (char *arg, int from_tty)
c906108c 11107{
06a64a0b 11108 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11109}
11110
8b93c638 11111void
f2fc3015 11112awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11113{
84f4c1fe 11114 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11115}
8926118c 11116
c5aa993b 11117static void
fba45db2 11118awatch_command (char *arg, int from_tty)
c906108c 11119{
06a64a0b 11120 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11121}
c906108c 11122\f
c5aa993b 11123
cfc31633
PA
11124/* Data for the FSM that manages the until(location)/advance commands
11125 in infcmd.c. Here because it uses the mechanisms of
11126 breakpoints. */
c906108c 11127
cfc31633 11128struct until_break_fsm
bfec99b2 11129{
cfc31633
PA
11130 /* The base class. */
11131 struct thread_fsm thread_fsm;
11132
11133 /* The thread that as current when the command was executed. */
11134 int thread;
11135
11136 /* The breakpoint set at the destination location. */
11137 struct breakpoint *location_breakpoint;
11138
11139 /* Breakpoint set at the return address in the caller frame. May be
11140 NULL. */
11141 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11142};
11143
8980e177
PA
11144static void until_break_fsm_clean_up (struct thread_fsm *self,
11145 struct thread_info *thread);
11146static int until_break_fsm_should_stop (struct thread_fsm *self,
11147 struct thread_info *thread);
cfc31633
PA
11148static enum async_reply_reason
11149 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11150
11151/* until_break_fsm's vtable. */
11152
11153static struct thread_fsm_ops until_break_fsm_ops =
11154{
11155 NULL, /* dtor */
11156 until_break_fsm_clean_up,
11157 until_break_fsm_should_stop,
11158 NULL, /* return_value */
11159 until_break_fsm_async_reply_reason,
11160};
11161
11162/* Allocate a new until_break_command_fsm. */
11163
11164static struct until_break_fsm *
8980e177 11165new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11166 struct breakpoint *location_breakpoint,
11167 struct breakpoint *caller_breakpoint)
11168{
11169 struct until_break_fsm *sm;
11170
11171 sm = XCNEW (struct until_break_fsm);
8980e177 11172 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11173
11174 sm->thread = thread;
11175 sm->location_breakpoint = location_breakpoint;
11176 sm->caller_breakpoint = caller_breakpoint;
11177
11178 return sm;
11179}
11180
11181/* Implementation of the 'should_stop' FSM method for the
11182 until(location)/advance commands. */
11183
11184static int
8980e177
PA
11185until_break_fsm_should_stop (struct thread_fsm *self,
11186 struct thread_info *tp)
cfc31633
PA
11187{
11188 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11189
11190 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11191 sm->location_breakpoint) != NULL
11192 || (sm->caller_breakpoint != NULL
11193 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11194 sm->caller_breakpoint) != NULL))
11195 thread_fsm_set_finished (self);
11196
11197 return 1;
11198}
11199
11200/* Implementation of the 'clean_up' FSM method for the
11201 until(location)/advance commands. */
11202
c2c6d25f 11203static void
8980e177
PA
11204until_break_fsm_clean_up (struct thread_fsm *self,
11205 struct thread_info *thread)
43ff13b4 11206{
cfc31633 11207 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11208
cfc31633
PA
11209 /* Clean up our temporary breakpoints. */
11210 if (sm->location_breakpoint != NULL)
11211 {
11212 delete_breakpoint (sm->location_breakpoint);
11213 sm->location_breakpoint = NULL;
11214 }
11215 if (sm->caller_breakpoint != NULL)
11216 {
11217 delete_breakpoint (sm->caller_breakpoint);
11218 sm->caller_breakpoint = NULL;
11219 }
11220 delete_longjmp_breakpoint (sm->thread);
11221}
11222
11223/* Implementation of the 'async_reply_reason' FSM method for the
11224 until(location)/advance commands. */
11225
11226static enum async_reply_reason
11227until_break_fsm_async_reply_reason (struct thread_fsm *self)
11228{
11229 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11230}
11231
c906108c 11232void
f2fc3015 11233until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11234{
8556afb4
PA
11235 struct frame_info *frame;
11236 struct gdbarch *frame_gdbarch;
11237 struct frame_id stack_frame_id;
11238 struct frame_id caller_frame_id;
cfc31633
PA
11239 struct breakpoint *location_breakpoint;
11240 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11241 struct cleanup *old_chain;
186c406b
TT
11242 int thread;
11243 struct thread_info *tp;
cfc31633 11244 struct until_break_fsm *sm;
c906108c 11245
70509625 11246 clear_proceed_status (0);
c906108c
SS
11247
11248 /* Set a breakpoint where the user wants it and at return from
4a64f543 11249 this function. */
c5aa993b 11250
ffc2605c 11251 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11252
6c5b2ebe
PA
11253 std::vector<symtab_and_line> sals
11254 = (last_displayed_sal_is_valid ()
11255 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11256 get_last_displayed_symtab (),
11257 get_last_displayed_line ())
11258 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11259 NULL, (struct symtab *) NULL, 0));
c5aa993b 11260
6c5b2ebe 11261 if (sals.size () != 1)
8a3fe4f8 11262 error (_("Couldn't get information on specified line."));
c5aa993b 11263
6c5b2ebe 11264 symtab_and_line &sal = sals[0];
c5aa993b 11265
c906108c 11266 if (*arg)
8a3fe4f8 11267 error (_("Junk at end of arguments."));
c5aa993b 11268
c906108c 11269 resolve_sal_pc (&sal);
c5aa993b 11270
186c406b 11271 tp = inferior_thread ();
5d5658a1 11272 thread = tp->global_num;
186c406b 11273
883bc8d1
PA
11274 old_chain = make_cleanup (null_cleanup, NULL);
11275
8556afb4
PA
11276 /* Note linespec handling above invalidates the frame chain.
11277 Installing a breakpoint also invalidates the frame chain (as it
11278 may need to switch threads), so do any frame handling before
11279 that. */
11280
11281 frame = get_selected_frame (NULL);
11282 frame_gdbarch = get_frame_arch (frame);
11283 stack_frame_id = get_stack_frame_id (frame);
11284 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11285
ae66c1fc
EZ
11286 /* Keep within the current frame, or in frames called by the current
11287 one. */
edb3359d 11288
883bc8d1 11289 if (frame_id_p (caller_frame_id))
c906108c 11290 {
883bc8d1 11291 struct symtab_and_line sal2;
cfc31633 11292 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11293
11294 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11295 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11296 caller_gdbarch = frame_unwind_caller_arch (frame);
11297 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11298 sal2,
11299 caller_frame_id,
11300 bp_until);
11301 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11302
883bc8d1 11303 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11304 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11305 }
c5aa993b 11306
c70a6932
JK
11307 /* set_momentary_breakpoint could invalidate FRAME. */
11308 frame = NULL;
11309
883bc8d1
PA
11310 if (anywhere)
11311 /* If the user told us to continue until a specified location,
11312 we don't specify a frame at which we need to stop. */
cfc31633
PA
11313 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11314 null_frame_id, bp_until);
883bc8d1
PA
11315 else
11316 /* Otherwise, specify the selected frame, because we want to stop
11317 only at the very same frame. */
cfc31633
PA
11318 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11319 stack_frame_id, bp_until);
11320 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11321
8980e177 11322 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11323 location_breakpoint, caller_breakpoint);
cfc31633 11324 tp->thread_fsm = &sm->thread_fsm;
f107f563 11325
cfc31633 11326 discard_cleanups (old_chain);
f107f563 11327
cfc31633 11328 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11329}
ae66c1fc 11330
c906108c
SS
11331/* This function attempts to parse an optional "if <cond>" clause
11332 from the arg string. If one is not found, it returns NULL.
c5aa993b 11333
c906108c
SS
11334 Else, it returns a pointer to the condition string. (It does not
11335 attempt to evaluate the string against a particular block.) And,
11336 it updates arg to point to the first character following the parsed
4a64f543 11337 if clause in the arg string. */
53a5351d 11338
63160a43
PA
11339const char *
11340ep_parse_optional_if_clause (const char **arg)
c906108c 11341{
63160a43 11342 const char *cond_string;
c5aa993b
JM
11343
11344 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11345 return NULL;
c5aa993b 11346
4a64f543 11347 /* Skip the "if" keyword. */
c906108c 11348 (*arg) += 2;
c5aa993b 11349
c906108c 11350 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11351 condition string. */
f1735a53 11352 *arg = skip_spaces (*arg);
c906108c 11353 cond_string = *arg;
c5aa993b 11354
4a64f543
MS
11355 /* Assume that the condition occupies the remainder of the arg
11356 string. */
c906108c 11357 (*arg) += strlen (cond_string);
c5aa993b 11358
c906108c
SS
11359 return cond_string;
11360}
c5aa993b 11361
c906108c
SS
11362/* Commands to deal with catching events, such as signals, exceptions,
11363 process start/exit, etc. */
c5aa993b
JM
11364
11365typedef enum
11366{
44feb3ce
TT
11367 catch_fork_temporary, catch_vfork_temporary,
11368 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11369}
11370catch_fork_kind;
11371
c906108c 11372static void
63160a43 11373catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11374 struct cmd_list_element *command)
c906108c 11375{
63160a43 11376 const char *arg = arg_entry;
a6d9a66e 11377 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11378 const char *cond_string = NULL;
44feb3ce
TT
11379 catch_fork_kind fork_kind;
11380 int tempflag;
11381
11382 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11383 tempflag = (fork_kind == catch_fork_temporary
11384 || fork_kind == catch_vfork_temporary);
c5aa993b 11385
44feb3ce
TT
11386 if (!arg)
11387 arg = "";
f1735a53 11388 arg = skip_spaces (arg);
c5aa993b 11389
c906108c 11390 /* The allowed syntax is:
c5aa993b
JM
11391 catch [v]fork
11392 catch [v]fork if <cond>
11393
4a64f543 11394 First, check if there's an if clause. */
c906108c 11395 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11396
c906108c 11397 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11398 error (_("Junk at end of arguments."));
c5aa993b 11399
c906108c 11400 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11401 and enable reporting of such events. */
c5aa993b
JM
11402 switch (fork_kind)
11403 {
44feb3ce
TT
11404 case catch_fork_temporary:
11405 case catch_fork_permanent:
a6d9a66e 11406 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11407 &catch_fork_breakpoint_ops);
c906108c 11408 break;
44feb3ce
TT
11409 case catch_vfork_temporary:
11410 case catch_vfork_permanent:
a6d9a66e 11411 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11412 &catch_vfork_breakpoint_ops);
c906108c 11413 break;
c5aa993b 11414 default:
8a3fe4f8 11415 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11416 break;
c5aa993b 11417 }
c906108c
SS
11418}
11419
11420static void
63160a43 11421catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11422 struct cmd_list_element *command)
c906108c 11423{
63160a43 11424 const char *arg = arg_entry;
a6d9a66e 11425 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11426 int tempflag;
63160a43 11427 const char *cond_string = NULL;
c906108c 11428
44feb3ce
TT
11429 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11430
11431 if (!arg)
11432 arg = "";
f1735a53 11433 arg = skip_spaces (arg);
c906108c
SS
11434
11435 /* The allowed syntax is:
c5aa993b
JM
11436 catch exec
11437 catch exec if <cond>
c906108c 11438
4a64f543 11439 First, check if there's an if clause. */
c906108c
SS
11440 cond_string = ep_parse_optional_if_clause (&arg);
11441
11442 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11443 error (_("Junk at end of arguments."));
c906108c 11444
b270e6f9
TT
11445 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11446 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11447 &catch_exec_breakpoint_ops);
11448 c->exec_pathname = NULL;
11449
b270e6f9 11450 install_breakpoint (0, std::move (c), 1);
c906108c 11451}
c5aa993b 11452
9ac4176b 11453void
28010a5d
PA
11454init_ada_exception_breakpoint (struct breakpoint *b,
11455 struct gdbarch *gdbarch,
11456 struct symtab_and_line sal,
f2fc3015 11457 const char *addr_string,
c0a91b2b 11458 const struct breakpoint_ops *ops,
28010a5d 11459 int tempflag,
349774ef 11460 int enabled,
28010a5d 11461 int from_tty)
f7f9143b 11462{
f7f9143b
JB
11463 if (from_tty)
11464 {
5af949e3
UW
11465 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11466 if (!loc_gdbarch)
11467 loc_gdbarch = gdbarch;
11468
6c95b8df
PA
11469 describe_other_breakpoints (loc_gdbarch,
11470 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11471 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11472 version for exception catchpoints, because two catchpoints
11473 used for different exception names will use the same address.
11474 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11475 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11476 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11477 the user what type of catchpoint it is. The above is good
11478 enough for now, though. */
11479 }
11480
28010a5d 11481 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11482
349774ef 11483 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11484 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11485 b->location = string_to_event_location (&addr_string,
11486 language_def (language_ada));
f7f9143b 11487 b->language = language_ada;
f7f9143b
JB
11488}
11489
c906108c 11490static void
981a3fb3 11491catch_command (const char *arg, int from_tty)
c906108c 11492{
44feb3ce 11493 error (_("Catch requires an event name."));
c906108c
SS
11494}
11495\f
11496
11497static void
981a3fb3 11498tcatch_command (const char *arg, int from_tty)
c906108c 11499{
44feb3ce 11500 error (_("Catch requires an event name."));
c906108c
SS
11501}
11502
81b1e71c 11503/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11504
11505static int
81b1e71c 11506compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11507{
81b1e71c
TT
11508 uintptr_t ua = (uintptr_t) a;
11509 uintptr_t ub = (uintptr_t) b;
8a2c437b 11510
81b1e71c 11511 if (a->number < b->number)
8a2c437b 11512 return -1;
81b1e71c 11513 else if (a->number > b->number)
8a2c437b
TT
11514 return 1;
11515
11516 /* Now sort by address, in case we see, e..g, two breakpoints with
11517 the number 0. */
11518 if (ua < ub)
11519 return -1;
94b0e70d 11520 return ua > ub ? 1 : 0;
8a2c437b
TT
11521}
11522
80f8a6eb 11523/* Delete breakpoints by address or line. */
c906108c
SS
11524
11525static void
fba45db2 11526clear_command (char *arg, int from_tty)
c906108c 11527{
81b1e71c 11528 struct breakpoint *b;
c906108c 11529 int default_match;
c906108c
SS
11530 int i;
11531
6c5b2ebe
PA
11532 std::vector<symtab_and_line> decoded_sals;
11533 symtab_and_line last_sal;
11534 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11535 if (arg)
11536 {
6c5b2ebe
PA
11537 decoded_sals
11538 = decode_line_with_current_source (arg,
11539 (DECODE_LINE_FUNFIRSTLINE
11540 | DECODE_LINE_LIST_MODE));
c906108c 11541 default_match = 0;
6c5b2ebe 11542 sals = decoded_sals;
c906108c
SS
11543 }
11544 else
11545 {
1bfeeb0f
JL
11546 /* Set sal's line, symtab, pc, and pspace to the values
11547 corresponding to the last call to print_frame_info. If the
11548 codepoint is not valid, this will set all the fields to 0. */
51abb421 11549 last_sal = get_last_displayed_sal ();
6c5b2ebe 11550 if (last_sal.symtab == 0)
8a3fe4f8 11551 error (_("No source file specified."));
c906108c 11552
c906108c 11553 default_match = 1;
6c5b2ebe 11554 sals = last_sal;
c906108c
SS
11555 }
11556
4a64f543
MS
11557 /* We don't call resolve_sal_pc here. That's not as bad as it
11558 seems, because all existing breakpoints typically have both
11559 file/line and pc set. So, if clear is given file/line, we can
11560 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11561
11562 We only support clearing given the address explicitly
11563 present in breakpoint table. Say, we've set breakpoint
4a64f543 11564 at file:line. There were several PC values for that file:line,
ed0616c6 11565 due to optimization, all in one block.
4a64f543
MS
11566
11567 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11568 PC corresponding to the same file:line, the breakpoint won't
11569 be cleared. We probably can still clear the breakpoint, but
11570 since the other PC value is never presented to user, user
11571 can only find it by guessing, and it does not seem important
11572 to support that. */
11573
4a64f543
MS
11574 /* For each line spec given, delete bps which correspond to it. Do
11575 it in two passes, solely to preserve the current behavior that
11576 from_tty is forced true if we delete more than one
11577 breakpoint. */
c906108c 11578
81b1e71c 11579 std::vector<struct breakpoint *> found;
6c5b2ebe 11580 for (const auto &sal : sals)
c906108c 11581 {
05cba821
JK
11582 const char *sal_fullname;
11583
c906108c 11584 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11585 If line given (pc == 0), clear all bpts on specified line.
11586 If defaulting, clear all bpts on default line
c906108c 11587 or at default pc.
c5aa993b
JM
11588
11589 defaulting sal.pc != 0 tests to do
11590
11591 0 1 pc
11592 1 1 pc _and_ line
11593 0 0 line
11594 1 0 <can't happen> */
c906108c 11595
05cba821
JK
11596 sal_fullname = (sal.symtab == NULL
11597 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11598
4a64f543 11599 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11600 ALL_BREAKPOINTS (b)
c5aa993b 11601 {
0d381245 11602 int match = 0;
4a64f543 11603 /* Are we going to delete b? */
cc60f2e3 11604 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11605 {
11606 struct bp_location *loc = b->loc;
11607 for (; loc; loc = loc->next)
11608 {
f8eba3c6
TT
11609 /* If the user specified file:line, don't allow a PC
11610 match. This matches historical gdb behavior. */
11611 int pc_match = (!sal.explicit_line
11612 && sal.pc
11613 && (loc->pspace == sal.pspace)
11614 && (loc->address == sal.pc)
11615 && (!section_is_overlay (loc->section)
11616 || loc->section == sal.section));
4aac40c8
TT
11617 int line_match = 0;
11618
11619 if ((default_match || sal.explicit_line)
2f202fde 11620 && loc->symtab != NULL
05cba821 11621 && sal_fullname != NULL
4aac40c8 11622 && sal.pspace == loc->pspace
05cba821
JK
11623 && loc->line_number == sal.line
11624 && filename_cmp (symtab_to_fullname (loc->symtab),
11625 sal_fullname) == 0)
11626 line_match = 1;
4aac40c8 11627
0d381245
VP
11628 if (pc_match || line_match)
11629 {
11630 match = 1;
11631 break;
11632 }
11633 }
11634 }
11635
11636 if (match)
81b1e71c 11637 found.push_back (b);
c906108c 11638 }
80f8a6eb 11639 }
8a2c437b 11640
80f8a6eb 11641 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11642 if (found.empty ())
80f8a6eb
MS
11643 {
11644 if (arg)
8a3fe4f8 11645 error (_("No breakpoint at %s."), arg);
80f8a6eb 11646 else
8a3fe4f8 11647 error (_("No breakpoint at this line."));
80f8a6eb 11648 }
c906108c 11649
8a2c437b 11650 /* Remove duplicates from the vec. */
81b1e71c
TT
11651 std::sort (found.begin (), found.end (),
11652 [] (const breakpoint *a, const breakpoint *b)
11653 {
11654 return compare_breakpoints (a, b) < 0;
11655 });
11656 found.erase (std::unique (found.begin (), found.end (),
11657 [] (const breakpoint *a, const breakpoint *b)
11658 {
11659 return compare_breakpoints (a, b) == 0;
11660 }),
11661 found.end ());
8a2c437b 11662
81b1e71c 11663 if (found.size () > 1)
4a64f543 11664 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11665 if (from_tty)
a3f17187 11666 {
81b1e71c 11667 if (found.size () == 1)
a3f17187
AC
11668 printf_unfiltered (_("Deleted breakpoint "));
11669 else
11670 printf_unfiltered (_("Deleted breakpoints "));
11671 }
d6e956e5 11672
81b1e71c 11673 for (breakpoint *iter : found)
80f8a6eb 11674 {
c5aa993b 11675 if (from_tty)
81b1e71c
TT
11676 printf_unfiltered ("%d ", iter->number);
11677 delete_breakpoint (iter);
c906108c 11678 }
80f8a6eb
MS
11679 if (from_tty)
11680 putchar_unfiltered ('\n');
c906108c
SS
11681}
11682\f
11683/* Delete breakpoint in BS if they are `delete' breakpoints and
11684 all breakpoints that are marked for deletion, whether hit or not.
11685 This is called after any breakpoint is hit, or after errors. */
11686
11687void
fba45db2 11688breakpoint_auto_delete (bpstat bs)
c906108c 11689{
35df4500 11690 struct breakpoint *b, *b_tmp;
c906108c
SS
11691
11692 for (; bs; bs = bs->next)
f431efe5
PA
11693 if (bs->breakpoint_at
11694 && bs->breakpoint_at->disposition == disp_del
c906108c 11695 && bs->stop)
f431efe5 11696 delete_breakpoint (bs->breakpoint_at);
c906108c 11697
35df4500 11698 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11699 {
b5de0fa7 11700 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11701 delete_breakpoint (b);
11702 }
c906108c
SS
11703}
11704
4a64f543
MS
11705/* A comparison function for bp_location AP and BP being interfaced to
11706 qsort. Sort elements primarily by their ADDRESS (no matter what
11707 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11708 secondarily by ordering first permanent elements and
4a64f543 11709 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11710 qsort being an unstable algorithm. */
876fa593
JK
11711
11712static int
f5336ca5 11713bp_locations_compare (const void *ap, const void *bp)
876fa593 11714{
9a3c8263
SM
11715 const struct bp_location *a = *(const struct bp_location **) ap;
11716 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11717
11718 if (a->address != b->address)
11719 return (a->address > b->address) - (a->address < b->address);
11720
dea2aa5f
LM
11721 /* Sort locations at the same address by their pspace number, keeping
11722 locations of the same inferior (in a multi-inferior environment)
11723 grouped. */
11724
11725 if (a->pspace->num != b->pspace->num)
11726 return ((a->pspace->num > b->pspace->num)
11727 - (a->pspace->num < b->pspace->num));
11728
876fa593 11729 /* Sort permanent breakpoints first. */
1a853c52
PA
11730 if (a->permanent != b->permanent)
11731 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11732
c56a97f9
JK
11733 /* Make the internal GDB representation stable across GDB runs
11734 where A and B memory inside GDB can differ. Breakpoint locations of
11735 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11736
11737 if (a->owner->number != b->owner->number)
c56a97f9
JK
11738 return ((a->owner->number > b->owner->number)
11739 - (a->owner->number < b->owner->number));
876fa593
JK
11740
11741 return (a > b) - (a < b);
11742}
11743
f5336ca5
PA
11744/* Set bp_locations_placed_address_before_address_max and
11745 bp_locations_shadow_len_after_address_max according to the current
11746 content of the bp_locations array. */
f7545552
TT
11747
11748static void
f5336ca5 11749bp_locations_target_extensions_update (void)
f7545552 11750{
876fa593
JK
11751 struct bp_location *bl, **blp_tmp;
11752
f5336ca5
PA
11753 bp_locations_placed_address_before_address_max = 0;
11754 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11755
11756 ALL_BP_LOCATIONS (bl, blp_tmp)
11757 {
11758 CORE_ADDR start, end, addr;
11759
11760 if (!bp_location_has_shadow (bl))
11761 continue;
11762
11763 start = bl->target_info.placed_address;
11764 end = start + bl->target_info.shadow_len;
11765
11766 gdb_assert (bl->address >= start);
11767 addr = bl->address - start;
f5336ca5
PA
11768 if (addr > bp_locations_placed_address_before_address_max)
11769 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11770
11771 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11772
11773 gdb_assert (bl->address < end);
11774 addr = end - bl->address;
f5336ca5
PA
11775 if (addr > bp_locations_shadow_len_after_address_max)
11776 bp_locations_shadow_len_after_address_max = addr;
876fa593 11777 }
f7545552
TT
11778}
11779
1e4d1764
YQ
11780/* Download tracepoint locations if they haven't been. */
11781
11782static void
11783download_tracepoint_locations (void)
11784{
7ed2c994 11785 struct breakpoint *b;
dd2e65cc 11786 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11787
5ed8105e 11788 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11789
7ed2c994 11790 ALL_TRACEPOINTS (b)
1e4d1764 11791 {
7ed2c994 11792 struct bp_location *bl;
1e4d1764 11793 struct tracepoint *t;
f2a8bc8a 11794 int bp_location_downloaded = 0;
1e4d1764 11795
7ed2c994 11796 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11797 ? !may_insert_fast_tracepoints
11798 : !may_insert_tracepoints))
11799 continue;
11800
dd2e65cc
YQ
11801 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11802 {
11803 if (target_can_download_tracepoint ())
11804 can_download_tracepoint = TRIBOOL_TRUE;
11805 else
11806 can_download_tracepoint = TRIBOOL_FALSE;
11807 }
11808
11809 if (can_download_tracepoint == TRIBOOL_FALSE)
11810 break;
11811
7ed2c994
YQ
11812 for (bl = b->loc; bl; bl = bl->next)
11813 {
11814 /* In tracepoint, locations are _never_ duplicated, so
11815 should_be_inserted is equivalent to
11816 unduplicated_should_be_inserted. */
11817 if (!should_be_inserted (bl) || bl->inserted)
11818 continue;
1e4d1764 11819
7ed2c994 11820 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11821
7ed2c994 11822 target_download_tracepoint (bl);
1e4d1764 11823
7ed2c994 11824 bl->inserted = 1;
f2a8bc8a 11825 bp_location_downloaded = 1;
7ed2c994
YQ
11826 }
11827 t = (struct tracepoint *) b;
11828 t->number_on_target = b->number;
f2a8bc8a
YQ
11829 if (bp_location_downloaded)
11830 observer_notify_breakpoint_modified (b);
1e4d1764 11831 }
1e4d1764
YQ
11832}
11833
934709f0
PW
11834/* Swap the insertion/duplication state between two locations. */
11835
11836static void
11837swap_insertion (struct bp_location *left, struct bp_location *right)
11838{
11839 const int left_inserted = left->inserted;
11840 const int left_duplicate = left->duplicate;
b775012e 11841 const int left_needs_update = left->needs_update;
934709f0
PW
11842 const struct bp_target_info left_target_info = left->target_info;
11843
1e4d1764
YQ
11844 /* Locations of tracepoints can never be duplicated. */
11845 if (is_tracepoint (left->owner))
11846 gdb_assert (!left->duplicate);
11847 if (is_tracepoint (right->owner))
11848 gdb_assert (!right->duplicate);
11849
934709f0
PW
11850 left->inserted = right->inserted;
11851 left->duplicate = right->duplicate;
b775012e 11852 left->needs_update = right->needs_update;
934709f0
PW
11853 left->target_info = right->target_info;
11854 right->inserted = left_inserted;
11855 right->duplicate = left_duplicate;
b775012e 11856 right->needs_update = left_needs_update;
934709f0
PW
11857 right->target_info = left_target_info;
11858}
11859
b775012e
LM
11860/* Force the re-insertion of the locations at ADDRESS. This is called
11861 once a new/deleted/modified duplicate location is found and we are evaluating
11862 conditions on the target's side. Such conditions need to be updated on
11863 the target. */
11864
11865static void
11866force_breakpoint_reinsertion (struct bp_location *bl)
11867{
11868 struct bp_location **locp = NULL, **loc2p;
11869 struct bp_location *loc;
11870 CORE_ADDR address = 0;
11871 int pspace_num;
11872
11873 address = bl->address;
11874 pspace_num = bl->pspace->num;
11875
11876 /* This is only meaningful if the target is
11877 evaluating conditions and if the user has
11878 opted for condition evaluation on the target's
11879 side. */
11880 if (gdb_evaluates_breakpoint_condition_p ()
11881 || !target_supports_evaluation_of_breakpoint_conditions ())
11882 return;
11883
11884 /* Flag all breakpoint locations with this address and
11885 the same program space as the location
11886 as "its condition has changed". We need to
11887 update the conditions on the target's side. */
11888 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11889 {
11890 loc = *loc2p;
11891
11892 if (!is_breakpoint (loc->owner)
11893 || pspace_num != loc->pspace->num)
11894 continue;
11895
11896 /* Flag the location appropriately. We use a different state to
11897 let everyone know that we already updated the set of locations
11898 with addr bl->address and program space bl->pspace. This is so
11899 we don't have to keep calling these functions just to mark locations
11900 that have already been marked. */
11901 loc->condition_changed = condition_updated;
11902
11903 /* Free the agent expression bytecode as well. We will compute
11904 it later on. */
833177a4 11905 loc->cond_bytecode.reset ();
b775012e
LM
11906 }
11907}
44702360
PA
11908/* Called whether new breakpoints are created, or existing breakpoints
11909 deleted, to update the global location list and recompute which
11910 locations are duplicate of which.
b775012e 11911
04086b45
PA
11912 The INSERT_MODE flag determines whether locations may not, may, or
11913 shall be inserted now. See 'enum ugll_insert_mode' for more
11914 info. */
b60e7edf 11915
0d381245 11916static void
44702360 11917update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11918{
74960c60 11919 struct breakpoint *b;
876fa593 11920 struct bp_location **locp, *loc;
b775012e
LM
11921 /* Last breakpoint location address that was marked for update. */
11922 CORE_ADDR last_addr = 0;
11923 /* Last breakpoint location program space that was marked for update. */
11924 int last_pspace_num = -1;
f7545552 11925
2d134ed3
PA
11926 /* Used in the duplicates detection below. When iterating over all
11927 bp_locations, points to the first bp_location of a given address.
11928 Breakpoints and watchpoints of different types are never
11929 duplicates of each other. Keep one pointer for each type of
11930 breakpoint/watchpoint, so we only need to loop over all locations
11931 once. */
11932 struct bp_location *bp_loc_first; /* breakpoint */
11933 struct bp_location *wp_loc_first; /* hardware watchpoint */
11934 struct bp_location *awp_loc_first; /* access watchpoint */
11935 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11936
f5336ca5
PA
11937 /* Saved former bp_locations array which we compare against the newly
11938 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11939 struct bp_location **old_locp;
f5336ca5 11940 unsigned old_locations_count;
81b1e71c 11941 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11942
f5336ca5
PA
11943 old_locations_count = bp_locations_count;
11944 bp_locations = NULL;
11945 bp_locations_count = 0;
0d381245 11946
74960c60 11947 ALL_BREAKPOINTS (b)
876fa593 11948 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11949 bp_locations_count++;
876fa593 11950
f5336ca5
PA
11951 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11952 locp = bp_locations;
876fa593
JK
11953 ALL_BREAKPOINTS (b)
11954 for (loc = b->loc; loc; loc = loc->next)
11955 *locp++ = loc;
f5336ca5
PA
11956 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11957 bp_locations_compare);
876fa593 11958
f5336ca5 11959 bp_locations_target_extensions_update ();
74960c60 11960
4a64f543
MS
11961 /* Identify bp_location instances that are no longer present in the
11962 new list, and therefore should be freed. Note that it's not
11963 necessary that those locations should be removed from inferior --
11964 if there's another location at the same address (previously
11965 marked as duplicate), we don't need to remove/insert the
11966 location.
876fa593 11967
4a64f543
MS
11968 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11969 and former bp_location array state respectively. */
876fa593 11970
f5336ca5 11971 locp = bp_locations;
81b1e71c
TT
11972 for (old_locp = old_locations.get ();
11973 old_locp < old_locations.get () + old_locations_count;
876fa593 11974 old_locp++)
74960c60 11975 {
876fa593 11976 struct bp_location *old_loc = *old_locp;
c7d46a38 11977 struct bp_location **loc2p;
876fa593 11978
e5dd4106 11979 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11980 not, we have to free it. */
c7d46a38 11981 int found_object = 0;
20874c92
VP
11982 /* Tells if the location should remain inserted in the target. */
11983 int keep_in_target = 0;
11984 int removed = 0;
876fa593 11985
4a64f543
MS
11986 /* Skip LOCP entries which will definitely never be needed.
11987 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11988 while (locp < bp_locations + bp_locations_count
c7d46a38 11989 && (*locp)->address < old_loc->address)
876fa593 11990 locp++;
c7d46a38
PA
11991
11992 for (loc2p = locp;
f5336ca5 11993 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11994 && (*loc2p)->address == old_loc->address);
11995 loc2p++)
11996 {
b775012e
LM
11997 /* Check if this is a new/duplicated location or a duplicated
11998 location that had its condition modified. If so, we want to send
11999 its condition to the target if evaluation of conditions is taking
12000 place there. */
12001 if ((*loc2p)->condition_changed == condition_modified
12002 && (last_addr != old_loc->address
12003 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12004 {
b775012e
LM
12005 force_breakpoint_reinsertion (*loc2p);
12006 last_pspace_num = old_loc->pspace->num;
c7d46a38 12007 }
b775012e
LM
12008
12009 if (*loc2p == old_loc)
12010 found_object = 1;
c7d46a38 12011 }
74960c60 12012
b775012e
LM
12013 /* We have already handled this address, update it so that we don't
12014 have to go through updates again. */
12015 last_addr = old_loc->address;
12016
12017 /* Target-side condition evaluation: Handle deleted locations. */
12018 if (!found_object)
12019 force_breakpoint_reinsertion (old_loc);
12020
4a64f543
MS
12021 /* If this location is no longer present, and inserted, look if
12022 there's maybe a new location at the same address. If so,
12023 mark that one inserted, and don't remove this one. This is
12024 needed so that we don't have a time window where a breakpoint
12025 at certain location is not inserted. */
74960c60 12026
876fa593 12027 if (old_loc->inserted)
0d381245 12028 {
4a64f543
MS
12029 /* If the location is inserted now, we might have to remove
12030 it. */
74960c60 12031
876fa593 12032 if (found_object && should_be_inserted (old_loc))
74960c60 12033 {
4a64f543
MS
12034 /* The location is still present in the location list,
12035 and still should be inserted. Don't do anything. */
20874c92 12036 keep_in_target = 1;
74960c60
VP
12037 }
12038 else
12039 {
b775012e
LM
12040 /* This location still exists, but it won't be kept in the
12041 target since it may have been disabled. We proceed to
12042 remove its target-side condition. */
12043
4a64f543
MS
12044 /* The location is either no longer present, or got
12045 disabled. See if there's another location at the
12046 same address, in which case we don't need to remove
12047 this one from the target. */
876fa593 12048
2bdf28a0 12049 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12050 if (breakpoint_address_is_meaningful (old_loc->owner))
12051 {
876fa593 12052 for (loc2p = locp;
f5336ca5 12053 (loc2p < bp_locations + bp_locations_count
c7d46a38 12054 && (*loc2p)->address == old_loc->address);
876fa593
JK
12055 loc2p++)
12056 {
12057 struct bp_location *loc2 = *loc2p;
12058
2d134ed3 12059 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12060 {
85d721b8
PA
12061 /* Read watchpoint locations are switched to
12062 access watchpoints, if the former are not
12063 supported, but the latter are. */
12064 if (is_hardware_watchpoint (old_loc->owner))
12065 {
12066 gdb_assert (is_hardware_watchpoint (loc2->owner));
12067 loc2->watchpoint_type = old_loc->watchpoint_type;
12068 }
12069
934709f0
PW
12070 /* loc2 is a duplicated location. We need to check
12071 if it should be inserted in case it will be
12072 unduplicated. */
12073 if (loc2 != old_loc
12074 && unduplicated_should_be_inserted (loc2))
c7d46a38 12075 {
934709f0 12076 swap_insertion (old_loc, loc2);
c7d46a38
PA
12077 keep_in_target = 1;
12078 break;
12079 }
876fa593
JK
12080 }
12081 }
12082 }
74960c60
VP
12083 }
12084
20874c92
VP
12085 if (!keep_in_target)
12086 {
834c0d03 12087 if (remove_breakpoint (old_loc))
20874c92 12088 {
4a64f543
MS
12089 /* This is just about all we can do. We could keep
12090 this location on the global list, and try to
12091 remove it next time, but there's no particular
12092 reason why we will succeed next time.
20874c92 12093
4a64f543
MS
12094 Note that at this point, old_loc->owner is still
12095 valid, as delete_breakpoint frees the breakpoint
12096 only after calling us. */
3e43a32a
MS
12097 printf_filtered (_("warning: Error removing "
12098 "breakpoint %d\n"),
876fa593 12099 old_loc->owner->number);
20874c92
VP
12100 }
12101 removed = 1;
12102 }
0d381245 12103 }
74960c60
VP
12104
12105 if (!found_object)
1c5cfe86 12106 {
fbea99ea 12107 if (removed && target_is_non_stop_p ()
1cf4d951 12108 && need_moribund_for_location_type (old_loc))
20874c92 12109 {
db82e815
PA
12110 /* This location was removed from the target. In
12111 non-stop mode, a race condition is possible where
12112 we've removed a breakpoint, but stop events for that
12113 breakpoint are already queued and will arrive later.
12114 We apply an heuristic to be able to distinguish such
12115 SIGTRAPs from other random SIGTRAPs: we keep this
12116 breakpoint location for a bit, and will retire it
12117 after we see some number of events. The theory here
12118 is that reporting of events should, "on the average",
12119 be fair, so after a while we'll see events from all
12120 threads that have anything of interest, and no longer
12121 need to keep this breakpoint location around. We
12122 don't hold locations forever so to reduce chances of
12123 mistaking a non-breakpoint SIGTRAP for a breakpoint
12124 SIGTRAP.
12125
12126 The heuristic failing can be disastrous on
12127 decr_pc_after_break targets.
12128
12129 On decr_pc_after_break targets, like e.g., x86-linux,
12130 if we fail to recognize a late breakpoint SIGTRAP,
12131 because events_till_retirement has reached 0 too
12132 soon, we'll fail to do the PC adjustment, and report
12133 a random SIGTRAP to the user. When the user resumes
12134 the inferior, it will most likely immediately crash
2dec564e 12135 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12136 corrupted, because of being resumed e.g., in the
12137 middle of a multi-byte instruction, or skipped a
12138 one-byte instruction. This was actually seen happen
12139 on native x86-linux, and should be less rare on
12140 targets that do not support new thread events, like
12141 remote, due to the heuristic depending on
12142 thread_count.
12143
12144 Mistaking a random SIGTRAP for a breakpoint trap
12145 causes similar symptoms (PC adjustment applied when
12146 it shouldn't), but then again, playing with SIGTRAPs
12147 behind the debugger's back is asking for trouble.
12148
12149 Since hardware watchpoint traps are always
12150 distinguishable from other traps, so we don't need to
12151 apply keep hardware watchpoint moribund locations
12152 around. We simply always ignore hardware watchpoint
12153 traps we can no longer explain. */
12154
876fa593
JK
12155 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12156 old_loc->owner = NULL;
20874c92 12157
876fa593 12158 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12159 }
12160 else
f431efe5
PA
12161 {
12162 old_loc->owner = NULL;
12163 decref_bp_location (&old_loc);
12164 }
20874c92 12165 }
74960c60 12166 }
1c5cfe86 12167
348d480f
PA
12168 /* Rescan breakpoints at the same address and section, marking the
12169 first one as "first" and any others as "duplicates". This is so
12170 that the bpt instruction is only inserted once. If we have a
12171 permanent breakpoint at the same place as BPT, make that one the
12172 official one, and the rest as duplicates. Permanent breakpoints
12173 are sorted first for the same address.
12174
12175 Do the same for hardware watchpoints, but also considering the
12176 watchpoint's type (regular/access/read) and length. */
12177
12178 bp_loc_first = NULL;
12179 wp_loc_first = NULL;
12180 awp_loc_first = NULL;
12181 rwp_loc_first = NULL;
12182 ALL_BP_LOCATIONS (loc, locp)
12183 {
12184 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12185 non-NULL. */
348d480f 12186 struct bp_location **loc_first_p;
d3fbdd86 12187 b = loc->owner;
348d480f 12188
6f380991 12189 if (!unduplicated_should_be_inserted (loc)
348d480f 12190 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12191 /* Don't detect duplicate for tracepoint locations because they are
12192 never duplicated. See the comments in field `duplicate' of
12193 `struct bp_location'. */
348d480f 12194 || is_tracepoint (b))
b775012e
LM
12195 {
12196 /* Clear the condition modification flag. */
12197 loc->condition_changed = condition_unchanged;
12198 continue;
12199 }
348d480f 12200
348d480f
PA
12201 if (b->type == bp_hardware_watchpoint)
12202 loc_first_p = &wp_loc_first;
12203 else if (b->type == bp_read_watchpoint)
12204 loc_first_p = &rwp_loc_first;
12205 else if (b->type == bp_access_watchpoint)
12206 loc_first_p = &awp_loc_first;
12207 else
12208 loc_first_p = &bp_loc_first;
12209
12210 if (*loc_first_p == NULL
12211 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12212 || !breakpoint_locations_match (loc, *loc_first_p))
12213 {
12214 *loc_first_p = loc;
12215 loc->duplicate = 0;
b775012e
LM
12216
12217 if (is_breakpoint (loc->owner) && loc->condition_changed)
12218 {
12219 loc->needs_update = 1;
12220 /* Clear the condition modification flag. */
12221 loc->condition_changed = condition_unchanged;
12222 }
348d480f
PA
12223 continue;
12224 }
12225
934709f0
PW
12226
12227 /* This and the above ensure the invariant that the first location
12228 is not duplicated, and is the inserted one.
12229 All following are marked as duplicated, and are not inserted. */
12230 if (loc->inserted)
12231 swap_insertion (loc, *loc_first_p);
348d480f
PA
12232 loc->duplicate = 1;
12233
b775012e
LM
12234 /* Clear the condition modification flag. */
12235 loc->condition_changed = condition_unchanged;
348d480f
PA
12236 }
12237
a25a5a45 12238 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12239 {
04086b45 12240 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12241 insert_breakpoint_locations ();
12242 else
12243 {
44702360
PA
12244 /* Even though the caller told us to not insert new
12245 locations, we may still need to update conditions on the
12246 target's side of breakpoints that were already inserted
12247 if the target is evaluating breakpoint conditions. We
b775012e
LM
12248 only update conditions for locations that are marked
12249 "needs_update". */
12250 update_inserted_breakpoint_locations ();
12251 }
12252 }
348d480f 12253
04086b45 12254 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12255 download_tracepoint_locations ();
348d480f
PA
12256}
12257
12258void
12259breakpoint_retire_moribund (void)
12260{
12261 struct bp_location *loc;
12262 int ix;
12263
12264 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12265 if (--(loc->events_till_retirement) == 0)
12266 {
12267 decref_bp_location (&loc);
12268 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12269 --ix;
12270 }
12271}
12272
12273static void
44702360 12274update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12275{
348d480f 12276
492d29ea
PA
12277 TRY
12278 {
12279 update_global_location_list (insert_mode);
12280 }
12281 CATCH (e, RETURN_MASK_ERROR)
12282 {
12283 }
12284 END_CATCH
348d480f
PA
12285}
12286
12287/* Clear BKP from a BPS. */
12288
12289static void
12290bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12291{
12292 bpstat bs;
12293
12294 for (bs = bps; bs; bs = bs->next)
12295 if (bs->breakpoint_at == bpt)
12296 {
12297 bs->breakpoint_at = NULL;
12298 bs->old_val = NULL;
12299 /* bs->commands will be freed later. */
12300 }
12301}
12302
12303/* Callback for iterate_over_threads. */
12304static int
12305bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12306{
9a3c8263 12307 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12308
12309 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12310 return 0;
12311}
12312
12313/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12314 callbacks. */
12315
12316static void
12317say_where (struct breakpoint *b)
12318{
12319 struct value_print_options opts;
12320
12321 get_user_print_options (&opts);
12322
12323 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12324 single string. */
12325 if (b->loc == NULL)
12326 {
f00aae0f
KS
12327 /* For pending locations, the output differs slightly based
12328 on b->extra_string. If this is non-NULL, it contains either
12329 a condition or dprintf arguments. */
12330 if (b->extra_string == NULL)
12331 {
12332 printf_filtered (_(" (%s) pending."),
d28cd78a 12333 event_location_to_string (b->location.get ()));
f00aae0f
KS
12334 }
12335 else if (b->type == bp_dprintf)
12336 {
12337 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12338 event_location_to_string (b->location.get ()),
f00aae0f
KS
12339 b->extra_string);
12340 }
12341 else
12342 {
12343 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12344 event_location_to_string (b->location.get ()),
f00aae0f
KS
12345 b->extra_string);
12346 }
348d480f
PA
12347 }
12348 else
12349 {
2f202fde 12350 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12351 {
12352 printf_filtered (" at ");
12353 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12354 gdb_stdout);
12355 }
2f202fde 12356 if (b->loc->symtab != NULL)
f8eba3c6
TT
12357 {
12358 /* If there is a single location, we can print the location
12359 more nicely. */
12360 if (b->loc->next == NULL)
12361 printf_filtered (": file %s, line %d.",
05cba821
JK
12362 symtab_to_filename_for_display (b->loc->symtab),
12363 b->loc->line_number);
f8eba3c6
TT
12364 else
12365 /* This is not ideal, but each location may have a
12366 different file name, and this at least reflects the
12367 real situation somewhat. */
f00aae0f 12368 printf_filtered (": %s.",
d28cd78a 12369 event_location_to_string (b->location.get ()));
f8eba3c6 12370 }
348d480f
PA
12371
12372 if (b->loc->next)
12373 {
12374 struct bp_location *loc = b->loc;
12375 int n = 0;
12376 for (; loc; loc = loc->next)
12377 ++n;
12378 printf_filtered (" (%d locations)", n);
12379 }
12380 }
12381}
12382
348d480f
PA
12383/* Default bp_location_ops methods. */
12384
12385static void
12386bp_location_dtor (struct bp_location *self)
12387{
348d480f
PA
12388 xfree (self->function_name);
12389}
12390
12391static const struct bp_location_ops bp_location_ops =
12392{
12393 bp_location_dtor
12394};
12395
c1fc2657 12396/* Destructor for the breakpoint base class. */
348d480f 12397
c1fc2657 12398breakpoint::~breakpoint ()
348d480f 12399{
c1fc2657
SM
12400 xfree (this->cond_string);
12401 xfree (this->extra_string);
12402 xfree (this->filter);
348d480f
PA
12403}
12404
2060206e
PA
12405static struct bp_location *
12406base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12407{
5625a286 12408 return new bp_location (&bp_location_ops, self);
348d480f
PA
12409}
12410
2060206e
PA
12411static void
12412base_breakpoint_re_set (struct breakpoint *b)
12413{
12414 /* Nothing to re-set. */
12415}
12416
12417#define internal_error_pure_virtual_called() \
12418 gdb_assert_not_reached ("pure virtual function called")
12419
12420static int
12421base_breakpoint_insert_location (struct bp_location *bl)
12422{
12423 internal_error_pure_virtual_called ();
12424}
12425
12426static int
73971819
PA
12427base_breakpoint_remove_location (struct bp_location *bl,
12428 enum remove_bp_reason reason)
2060206e
PA
12429{
12430 internal_error_pure_virtual_called ();
12431}
12432
12433static int
12434base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12435 const address_space *aspace,
09ac7c10
TT
12436 CORE_ADDR bp_addr,
12437 const struct target_waitstatus *ws)
2060206e
PA
12438{
12439 internal_error_pure_virtual_called ();
12440}
12441
12442static void
12443base_breakpoint_check_status (bpstat bs)
12444{
12445 /* Always stop. */
12446}
12447
12448/* A "works_in_software_mode" breakpoint_ops method that just internal
12449 errors. */
12450
12451static int
12452base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12453{
12454 internal_error_pure_virtual_called ();
12455}
12456
12457/* A "resources_needed" breakpoint_ops method that just internal
12458 errors. */
12459
12460static int
12461base_breakpoint_resources_needed (const struct bp_location *bl)
12462{
12463 internal_error_pure_virtual_called ();
12464}
12465
12466static enum print_stop_action
12467base_breakpoint_print_it (bpstat bs)
12468{
12469 internal_error_pure_virtual_called ();
12470}
12471
12472static void
12473base_breakpoint_print_one_detail (const struct breakpoint *self,
12474 struct ui_out *uiout)
12475{
12476 /* nothing */
12477}
12478
12479static void
12480base_breakpoint_print_mention (struct breakpoint *b)
12481{
12482 internal_error_pure_virtual_called ();
12483}
12484
12485static void
12486base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12487{
12488 internal_error_pure_virtual_called ();
12489}
12490
983af33b 12491static void
f00aae0f
KS
12492base_breakpoint_create_sals_from_location
12493 (const struct event_location *location,
12494 struct linespec_result *canonical,
12495 enum bptype type_wanted)
983af33b
SDJ
12496{
12497 internal_error_pure_virtual_called ();
12498}
12499
12500static void
12501base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12502 struct linespec_result *c,
e1e01040
PA
12503 gdb::unique_xmalloc_ptr<char> cond_string,
12504 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12505 enum bptype type_wanted,
12506 enum bpdisp disposition,
12507 int thread,
12508 int task, int ignore_count,
12509 const struct breakpoint_ops *o,
12510 int from_tty, int enabled,
44f238bb 12511 int internal, unsigned flags)
983af33b
SDJ
12512{
12513 internal_error_pure_virtual_called ();
12514}
12515
6c5b2ebe 12516static std::vector<symtab_and_line>
f00aae0f
KS
12517base_breakpoint_decode_location (struct breakpoint *b,
12518 const struct event_location *location,
6c5b2ebe 12519 struct program_space *search_pspace)
983af33b
SDJ
12520{
12521 internal_error_pure_virtual_called ();
12522}
12523
ab04a2af
TT
12524/* The default 'explains_signal' method. */
12525
47591c29 12526static int
427cd150 12527base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12528{
47591c29 12529 return 1;
ab04a2af
TT
12530}
12531
9d6e6e84
HZ
12532/* The default "after_condition_true" method. */
12533
12534static void
12535base_breakpoint_after_condition_true (struct bpstats *bs)
12536{
12537 /* Nothing to do. */
12538}
12539
ab04a2af 12540struct breakpoint_ops base_breakpoint_ops =
2060206e 12541{
2060206e
PA
12542 base_breakpoint_allocate_location,
12543 base_breakpoint_re_set,
12544 base_breakpoint_insert_location,
12545 base_breakpoint_remove_location,
12546 base_breakpoint_breakpoint_hit,
12547 base_breakpoint_check_status,
12548 base_breakpoint_resources_needed,
12549 base_breakpoint_works_in_software_mode,
12550 base_breakpoint_print_it,
12551 NULL,
12552 base_breakpoint_print_one_detail,
12553 base_breakpoint_print_mention,
983af33b 12554 base_breakpoint_print_recreate,
5f700d83 12555 base_breakpoint_create_sals_from_location,
983af33b 12556 base_breakpoint_create_breakpoints_sal,
5f700d83 12557 base_breakpoint_decode_location,
9d6e6e84
HZ
12558 base_breakpoint_explains_signal,
12559 base_breakpoint_after_condition_true,
2060206e
PA
12560};
12561
12562/* Default breakpoint_ops methods. */
12563
12564static void
348d480f
PA
12565bkpt_re_set (struct breakpoint *b)
12566{
06edf0c0 12567 /* FIXME: is this still reachable? */
9ef9e6a6 12568 if (breakpoint_event_location_empty_p (b))
06edf0c0 12569 {
f00aae0f 12570 /* Anything without a location can't be re-set. */
348d480f 12571 delete_breakpoint (b);
06edf0c0 12572 return;
348d480f 12573 }
06edf0c0
PA
12574
12575 breakpoint_re_set_default (b);
348d480f
PA
12576}
12577
2060206e 12578static int
348d480f
PA
12579bkpt_insert_location (struct bp_location *bl)
12580{
cd6c3b4f
YQ
12581 CORE_ADDR addr = bl->target_info.reqstd_address;
12582
579c6ad9 12583 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12584 bl->target_info.placed_address = addr;
12585
348d480f 12586 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12587 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12588 else
7c16b83e 12589 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12590}
12591
2060206e 12592static int
73971819 12593bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12594{
12595 if (bl->loc_type == bp_loc_hardware_breakpoint)
12596 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12597 else
73971819 12598 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12599}
12600
2060206e 12601static int
348d480f 12602bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12603 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12604 const struct target_waitstatus *ws)
348d480f 12605{
09ac7c10 12606 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12607 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12608 return 0;
12609
348d480f
PA
12610 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12611 aspace, bp_addr))
12612 return 0;
12613
12614 if (overlay_debugging /* unmapped overlay section */
12615 && section_is_overlay (bl->section)
12616 && !section_is_mapped (bl->section))
12617 return 0;
12618
12619 return 1;
12620}
12621
cd1608cc
PA
12622static int
12623dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12624 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12625 const struct target_waitstatus *ws)
12626{
12627 if (dprintf_style == dprintf_style_agent
12628 && target_can_run_breakpoint_commands ())
12629 {
12630 /* An agent-style dprintf never causes a stop. If we see a trap
12631 for this address it must be for a breakpoint that happens to
12632 be set at the same address. */
12633 return 0;
12634 }
12635
12636 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12637}
12638
2060206e 12639static int
348d480f
PA
12640bkpt_resources_needed (const struct bp_location *bl)
12641{
12642 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12643
12644 return 1;
12645}
12646
2060206e 12647static enum print_stop_action
348d480f
PA
12648bkpt_print_it (bpstat bs)
12649{
348d480f
PA
12650 struct breakpoint *b;
12651 const struct bp_location *bl;
001c8c33 12652 int bp_temp;
79a45e25 12653 struct ui_out *uiout = current_uiout;
348d480f
PA
12654
12655 gdb_assert (bs->bp_location_at != NULL);
12656
12657 bl = bs->bp_location_at;
12658 b = bs->breakpoint_at;
12659
001c8c33
PA
12660 bp_temp = b->disposition == disp_del;
12661 if (bl->address != bl->requested_address)
12662 breakpoint_adjustment_warning (bl->requested_address,
12663 bl->address,
12664 b->number, 1);
12665 annotate_breakpoint (b->number);
f303dbd6
PA
12666 maybe_print_thread_hit_breakpoint (uiout);
12667
001c8c33 12668 if (bp_temp)
112e8700 12669 uiout->text ("Temporary breakpoint ");
001c8c33 12670 else
112e8700
SM
12671 uiout->text ("Breakpoint ");
12672 if (uiout->is_mi_like_p ())
348d480f 12673 {
112e8700 12674 uiout->field_string ("reason",
001c8c33 12675 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12676 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12677 }
112e8700
SM
12678 uiout->field_int ("bkptno", b->number);
12679 uiout->text (", ");
06edf0c0 12680
001c8c33 12681 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12682}
12683
2060206e 12684static void
06edf0c0
PA
12685bkpt_print_mention (struct breakpoint *b)
12686{
112e8700 12687 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12688 return;
12689
12690 switch (b->type)
12691 {
12692 case bp_breakpoint:
12693 case bp_gnu_ifunc_resolver:
12694 if (b->disposition == disp_del)
12695 printf_filtered (_("Temporary breakpoint"));
12696 else
12697 printf_filtered (_("Breakpoint"));
12698 printf_filtered (_(" %d"), b->number);
12699 if (b->type == bp_gnu_ifunc_resolver)
12700 printf_filtered (_(" at gnu-indirect-function resolver"));
12701 break;
12702 case bp_hardware_breakpoint:
12703 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12704 break;
e7e0cddf
SS
12705 case bp_dprintf:
12706 printf_filtered (_("Dprintf %d"), b->number);
12707 break;
06edf0c0
PA
12708 }
12709
12710 say_where (b);
12711}
12712
2060206e 12713static void
06edf0c0
PA
12714bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12715{
12716 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12717 fprintf_unfiltered (fp, "tbreak");
12718 else if (tp->type == bp_breakpoint)
12719 fprintf_unfiltered (fp, "break");
12720 else if (tp->type == bp_hardware_breakpoint
12721 && tp->disposition == disp_del)
12722 fprintf_unfiltered (fp, "thbreak");
12723 else if (tp->type == bp_hardware_breakpoint)
12724 fprintf_unfiltered (fp, "hbreak");
12725 else
12726 internal_error (__FILE__, __LINE__,
12727 _("unhandled breakpoint type %d"), (int) tp->type);
12728
f00aae0f 12729 fprintf_unfiltered (fp, " %s",
d28cd78a 12730 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12731
12732 /* Print out extra_string if this breakpoint is pending. It might
12733 contain, for example, conditions that were set by the user. */
12734 if (tp->loc == NULL && tp->extra_string != NULL)
12735 fprintf_unfiltered (fp, " %s", tp->extra_string);
12736
dd11a36c 12737 print_recreate_thread (tp, fp);
06edf0c0
PA
12738}
12739
983af33b 12740static void
f00aae0f
KS
12741bkpt_create_sals_from_location (const struct event_location *location,
12742 struct linespec_result *canonical,
12743 enum bptype type_wanted)
983af33b 12744{
f00aae0f 12745 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12746}
12747
12748static void
12749bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12750 struct linespec_result *canonical,
e1e01040
PA
12751 gdb::unique_xmalloc_ptr<char> cond_string,
12752 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12753 enum bptype type_wanted,
12754 enum bpdisp disposition,
12755 int thread,
12756 int task, int ignore_count,
12757 const struct breakpoint_ops *ops,
12758 int from_tty, int enabled,
44f238bb 12759 int internal, unsigned flags)
983af33b 12760{
023fa29b 12761 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12762 std::move (cond_string),
12763 std::move (extra_string),
e7e0cddf 12764 type_wanted,
983af33b
SDJ
12765 disposition, thread, task,
12766 ignore_count, ops, from_tty,
44f238bb 12767 enabled, internal, flags);
983af33b
SDJ
12768}
12769
6c5b2ebe 12770static std::vector<symtab_and_line>
f00aae0f
KS
12771bkpt_decode_location (struct breakpoint *b,
12772 const struct event_location *location,
6c5b2ebe 12773 struct program_space *search_pspace)
983af33b 12774{
6c5b2ebe 12775 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12776}
12777
06edf0c0
PA
12778/* Virtual table for internal breakpoints. */
12779
12780static void
12781internal_bkpt_re_set (struct breakpoint *b)
12782{
12783 switch (b->type)
12784 {
12785 /* Delete overlay event and longjmp master breakpoints; they
12786 will be reset later by breakpoint_re_set. */
12787 case bp_overlay_event:
12788 case bp_longjmp_master:
12789 case bp_std_terminate_master:
12790 case bp_exception_master:
12791 delete_breakpoint (b);
12792 break;
12793
12794 /* This breakpoint is special, it's set up when the inferior
12795 starts and we really don't want to touch it. */
12796 case bp_shlib_event:
12797
12798 /* Like bp_shlib_event, this breakpoint type is special. Once
12799 it is set up, we do not want to touch it. */
12800 case bp_thread_event:
12801 break;
12802 }
12803}
12804
12805static void
12806internal_bkpt_check_status (bpstat bs)
12807{
a9b3a50f
PA
12808 if (bs->breakpoint_at->type == bp_shlib_event)
12809 {
12810 /* If requested, stop when the dynamic linker notifies GDB of
12811 events. This allows the user to get control and place
12812 breakpoints in initializer routines for dynamically loaded
12813 objects (among other things). */
12814 bs->stop = stop_on_solib_events;
12815 bs->print = stop_on_solib_events;
12816 }
12817 else
12818 bs->stop = 0;
06edf0c0
PA
12819}
12820
12821static enum print_stop_action
12822internal_bkpt_print_it (bpstat bs)
12823{
06edf0c0 12824 struct breakpoint *b;
06edf0c0 12825
06edf0c0
PA
12826 b = bs->breakpoint_at;
12827
06edf0c0
PA
12828 switch (b->type)
12829 {
348d480f
PA
12830 case bp_shlib_event:
12831 /* Did we stop because the user set the stop_on_solib_events
12832 variable? (If so, we report this as a generic, "Stopped due
12833 to shlib event" message.) */
edcc5120 12834 print_solib_event (0);
348d480f
PA
12835 break;
12836
12837 case bp_thread_event:
12838 /* Not sure how we will get here.
12839 GDB should not stop for these breakpoints. */
12840 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12841 break;
12842
12843 case bp_overlay_event:
12844 /* By analogy with the thread event, GDB should not stop for these. */
12845 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12846 break;
12847
12848 case bp_longjmp_master:
12849 /* These should never be enabled. */
12850 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12851 break;
12852
12853 case bp_std_terminate_master:
12854 /* These should never be enabled. */
12855 printf_filtered (_("std::terminate Master Breakpoint: "
12856 "gdb should not stop!\n"));
348d480f
PA
12857 break;
12858
12859 case bp_exception_master:
12860 /* These should never be enabled. */
12861 printf_filtered (_("Exception Master Breakpoint: "
12862 "gdb should not stop!\n"));
06edf0c0
PA
12863 break;
12864 }
12865
001c8c33 12866 return PRINT_NOTHING;
06edf0c0
PA
12867}
12868
12869static void
12870internal_bkpt_print_mention (struct breakpoint *b)
12871{
12872 /* Nothing to mention. These breakpoints are internal. */
12873}
12874
06edf0c0
PA
12875/* Virtual table for momentary breakpoints */
12876
12877static void
12878momentary_bkpt_re_set (struct breakpoint *b)
12879{
12880 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12881 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12882 Otherwise these should have been blown away via the cleanup chain
12883 or by breakpoint_init_inferior when we rerun the executable. */
12884}
12885
12886static void
12887momentary_bkpt_check_status (bpstat bs)
12888{
12889 /* Nothing. The point of these breakpoints is causing a stop. */
12890}
12891
12892static enum print_stop_action
12893momentary_bkpt_print_it (bpstat bs)
12894{
001c8c33 12895 return PRINT_UNKNOWN;
348d480f
PA
12896}
12897
06edf0c0
PA
12898static void
12899momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12900{
06edf0c0 12901 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12902}
12903
e2e4d78b
JK
12904/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12905
12906 It gets cleared already on the removal of the first one of such placed
12907 breakpoints. This is OK as they get all removed altogether. */
12908
c1fc2657 12909longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12910{
c1fc2657 12911 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12912
c1fc2657 12913 if (tp != NULL)
e2e4d78b 12914 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12915}
12916
55aa24fb
SDJ
12917/* Specific methods for probe breakpoints. */
12918
12919static int
12920bkpt_probe_insert_location (struct bp_location *bl)
12921{
12922 int v = bkpt_insert_location (bl);
12923
12924 if (v == 0)
12925 {
12926 /* The insertion was successful, now let's set the probe's semaphore
12927 if needed. */
0ea5cda8
SDJ
12928 if (bl->probe.probe->pops->set_semaphore != NULL)
12929 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12930 bl->probe.objfile,
12931 bl->gdbarch);
55aa24fb
SDJ
12932 }
12933
12934 return v;
12935}
12936
12937static int
73971819
PA
12938bkpt_probe_remove_location (struct bp_location *bl,
12939 enum remove_bp_reason reason)
55aa24fb
SDJ
12940{
12941 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12942 if (bl->probe.probe->pops->clear_semaphore != NULL)
12943 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12944 bl->probe.objfile,
12945 bl->gdbarch);
55aa24fb 12946
73971819 12947 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12948}
12949
12950static void
f00aae0f 12951bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12952 struct linespec_result *canonical,
f00aae0f 12953 enum bptype type_wanted)
55aa24fb
SDJ
12954{
12955 struct linespec_sals lsal;
12956
c2f4122d 12957 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12958 lsal.canonical
12959 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12960 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12961}
12962
6c5b2ebe 12963static std::vector<symtab_and_line>
f00aae0f
KS
12964bkpt_probe_decode_location (struct breakpoint *b,
12965 const struct event_location *location,
6c5b2ebe 12966 struct program_space *search_pspace)
55aa24fb 12967{
6c5b2ebe
PA
12968 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12969 if (sals.empty ())
55aa24fb 12970 error (_("probe not found"));
6c5b2ebe 12971 return sals;
55aa24fb
SDJ
12972}
12973
348d480f 12974/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12975
348d480f
PA
12976static void
12977tracepoint_re_set (struct breakpoint *b)
12978{
12979 breakpoint_re_set_default (b);
12980}
876fa593 12981
348d480f
PA
12982static int
12983tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12984 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12985 const struct target_waitstatus *ws)
348d480f
PA
12986{
12987 /* By definition, the inferior does not report stops at
12988 tracepoints. */
12989 return 0;
74960c60
VP
12990}
12991
12992static void
348d480f
PA
12993tracepoint_print_one_detail (const struct breakpoint *self,
12994 struct ui_out *uiout)
74960c60 12995{
d9b3f62e
PA
12996 struct tracepoint *tp = (struct tracepoint *) self;
12997 if (tp->static_trace_marker_id)
348d480f
PA
12998 {
12999 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13000
112e8700
SM
13001 uiout->text ("\tmarker id is ");
13002 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13003 tp->static_trace_marker_id);
112e8700 13004 uiout->text ("\n");
348d480f 13005 }
0d381245
VP
13006}
13007
a474d7c2 13008static void
348d480f 13009tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13010{
112e8700 13011 if (current_uiout->is_mi_like_p ())
348d480f 13012 return;
cc59ec59 13013
348d480f
PA
13014 switch (b->type)
13015 {
13016 case bp_tracepoint:
13017 printf_filtered (_("Tracepoint"));
13018 printf_filtered (_(" %d"), b->number);
13019 break;
13020 case bp_fast_tracepoint:
13021 printf_filtered (_("Fast tracepoint"));
13022 printf_filtered (_(" %d"), b->number);
13023 break;
13024 case bp_static_tracepoint:
13025 printf_filtered (_("Static tracepoint"));
13026 printf_filtered (_(" %d"), b->number);
13027 break;
13028 default:
13029 internal_error (__FILE__, __LINE__,
13030 _("unhandled tracepoint type %d"), (int) b->type);
13031 }
13032
13033 say_where (b);
a474d7c2
PA
13034}
13035
348d480f 13036static void
d9b3f62e 13037tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13038{
d9b3f62e
PA
13039 struct tracepoint *tp = (struct tracepoint *) self;
13040
13041 if (self->type == bp_fast_tracepoint)
348d480f 13042 fprintf_unfiltered (fp, "ftrace");
c93e8391 13043 else if (self->type == bp_static_tracepoint)
348d480f 13044 fprintf_unfiltered (fp, "strace");
d9b3f62e 13045 else if (self->type == bp_tracepoint)
348d480f
PA
13046 fprintf_unfiltered (fp, "trace");
13047 else
13048 internal_error (__FILE__, __LINE__,
d9b3f62e 13049 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13050
f00aae0f 13051 fprintf_unfiltered (fp, " %s",
d28cd78a 13052 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13053 print_recreate_thread (self, fp);
13054
13055 if (tp->pass_count)
13056 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13057}
13058
983af33b 13059static void
f00aae0f
KS
13060tracepoint_create_sals_from_location (const struct event_location *location,
13061 struct linespec_result *canonical,
13062 enum bptype type_wanted)
983af33b 13063{
f00aae0f 13064 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13065}
13066
13067static void
13068tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13069 struct linespec_result *canonical,
e1e01040
PA
13070 gdb::unique_xmalloc_ptr<char> cond_string,
13071 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13072 enum bptype type_wanted,
13073 enum bpdisp disposition,
13074 int thread,
13075 int task, int ignore_count,
13076 const struct breakpoint_ops *ops,
13077 int from_tty, int enabled,
44f238bb 13078 int internal, unsigned flags)
983af33b 13079{
023fa29b 13080 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13081 std::move (cond_string),
13082 std::move (extra_string),
e7e0cddf 13083 type_wanted,
983af33b
SDJ
13084 disposition, thread, task,
13085 ignore_count, ops, from_tty,
44f238bb 13086 enabled, internal, flags);
983af33b
SDJ
13087}
13088
6c5b2ebe 13089static std::vector<symtab_and_line>
f00aae0f
KS
13090tracepoint_decode_location (struct breakpoint *b,
13091 const struct event_location *location,
6c5b2ebe 13092 struct program_space *search_pspace)
983af33b 13093{
6c5b2ebe 13094 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13095}
13096
2060206e 13097struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13098
55aa24fb
SDJ
13099/* The breakpoint_ops structure to be use on tracepoints placed in a
13100 static probe. */
13101
13102static void
f00aae0f
KS
13103tracepoint_probe_create_sals_from_location
13104 (const struct event_location *location,
13105 struct linespec_result *canonical,
13106 enum bptype type_wanted)
55aa24fb
SDJ
13107{
13108 /* We use the same method for breakpoint on probes. */
f00aae0f 13109 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13110}
13111
6c5b2ebe 13112static std::vector<symtab_and_line>
f00aae0f
KS
13113tracepoint_probe_decode_location (struct breakpoint *b,
13114 const struct event_location *location,
6c5b2ebe 13115 struct program_space *search_pspace)
55aa24fb
SDJ
13116{
13117 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13118 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13119}
13120
13121static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13122
5c2b4418
HZ
13123/* Dprintf breakpoint_ops methods. */
13124
13125static void
13126dprintf_re_set (struct breakpoint *b)
13127{
13128 breakpoint_re_set_default (b);
13129
f00aae0f
KS
13130 /* extra_string should never be non-NULL for dprintf. */
13131 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13132
13133 /* 1 - connect to target 1, that can run breakpoint commands.
13134 2 - create a dprintf, which resolves fine.
13135 3 - disconnect from target 1
13136 4 - connect to target 2, that can NOT run breakpoint commands.
13137
13138 After steps #3/#4, you'll want the dprintf command list to
13139 be updated, because target 1 and 2 may well return different
13140 answers for target_can_run_breakpoint_commands().
13141 Given absence of finer grained resetting, we get to do
13142 it all the time. */
13143 if (b->extra_string != NULL)
13144 update_dprintf_command_list (b);
13145}
13146
2d9442cc
HZ
13147/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13148
13149static void
13150dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13151{
f00aae0f 13152 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13153 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13154 tp->extra_string);
13155 print_recreate_thread (tp, fp);
13156}
13157
9d6e6e84
HZ
13158/* Implement the "after_condition_true" breakpoint_ops method for
13159 dprintf.
13160
13161 dprintf's are implemented with regular commands in their command
13162 list, but we run the commands here instead of before presenting the
13163 stop to the user, as dprintf's don't actually cause a stop. This
13164 also makes it so that the commands of multiple dprintfs at the same
13165 address are all handled. */
13166
13167static void
13168dprintf_after_condition_true (struct bpstats *bs)
13169{
04afa70c 13170 struct bpstats tmp_bs;
9d6e6e84
HZ
13171 struct bpstats *tmp_bs_p = &tmp_bs;
13172
13173 /* dprintf's never cause a stop. This wasn't set in the
13174 check_status hook instead because that would make the dprintf's
13175 condition not be evaluated. */
13176 bs->stop = 0;
13177
13178 /* Run the command list here. Take ownership of it instead of
13179 copying. We never want these commands to run later in
13180 bpstat_do_actions, if a breakpoint that causes a stop happens to
13181 be set at same address as this dprintf, or even if running the
13182 commands here throws. */
13183 tmp_bs.commands = bs->commands;
13184 bs->commands = NULL;
9d6e6e84
HZ
13185
13186 bpstat_do_actions_1 (&tmp_bs_p);
13187
13188 /* 'tmp_bs.commands' will usually be NULL by now, but
13189 bpstat_do_actions_1 may return early without processing the whole
13190 list. */
9d6e6e84
HZ
13191}
13192
983af33b
SDJ
13193/* The breakpoint_ops structure to be used on static tracepoints with
13194 markers (`-m'). */
13195
13196static void
f00aae0f 13197strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13198 struct linespec_result *canonical,
f00aae0f 13199 enum bptype type_wanted)
983af33b
SDJ
13200{
13201 struct linespec_sals lsal;
f00aae0f 13202 const char *arg_start, *arg;
983af33b 13203
f00aae0f
KS
13204 arg = arg_start = get_linespec_location (location);
13205 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13206
f2fc3015
TT
13207 std::string str (arg_start, arg - arg_start);
13208 const char *ptr = str.c_str ();
13209 canonical->location = new_linespec_location (&ptr);
983af33b 13210
8e9e35b1
TT
13211 lsal.canonical
13212 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13213 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13214}
13215
13216static void
13217strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13218 struct linespec_result *canonical,
e1e01040
PA
13219 gdb::unique_xmalloc_ptr<char> cond_string,
13220 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13221 enum bptype type_wanted,
13222 enum bpdisp disposition,
13223 int thread,
13224 int task, int ignore_count,
13225 const struct breakpoint_ops *ops,
13226 int from_tty, int enabled,
44f238bb 13227 int internal, unsigned flags)
983af33b 13228{
6c5b2ebe 13229 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13230
13231 /* If the user is creating a static tracepoint by marker id
13232 (strace -m MARKER_ID), then store the sals index, so that
13233 breakpoint_re_set can try to match up which of the newly
13234 found markers corresponds to this one, and, don't try to
13235 expand multiple locations for each sal, given than SALS
13236 already should contain all sals for MARKER_ID. */
13237
6c5b2ebe 13238 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13239 {
6c5b2ebe
PA
13240 event_location_up location
13241 = copy_event_location (canonical->location.get ());
983af33b 13242
b270e6f9 13243 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13244 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13245 std::move (location), NULL,
e1e01040
PA
13246 std::move (cond_string),
13247 std::move (extra_string),
e7e0cddf 13248 type_wanted, disposition,
983af33b 13249 thread, task, ignore_count, ops,
44f238bb 13250 from_tty, enabled, internal, flags,
983af33b
SDJ
13251 canonical->special_display);
13252 /* Given that its possible to have multiple markers with
13253 the same string id, if the user is creating a static
13254 tracepoint by marker id ("strace -m MARKER_ID"), then
13255 store the sals index, so that breakpoint_re_set can
13256 try to match up which of the newly found markers
13257 corresponds to this one */
13258 tp->static_trace_marker_id_idx = i;
13259
b270e6f9 13260 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13261 }
13262}
13263
6c5b2ebe 13264static std::vector<symtab_and_line>
f00aae0f
KS
13265strace_marker_decode_location (struct breakpoint *b,
13266 const struct event_location *location,
6c5b2ebe 13267 struct program_space *search_pspace)
983af33b
SDJ
13268{
13269 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13270 const char *s = get_linespec_location (location);
983af33b 13271
6c5b2ebe
PA
13272 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13273 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13274 {
6c5b2ebe
PA
13275 sals[0] = sals[tp->static_trace_marker_id_idx];
13276 sals.resize (1);
13277 return sals;
983af33b
SDJ
13278 }
13279 else
13280 error (_("marker %s not found"), tp->static_trace_marker_id);
13281}
13282
13283static struct breakpoint_ops strace_marker_breakpoint_ops;
13284
13285static int
13286strace_marker_p (struct breakpoint *b)
13287{
13288 return b->ops == &strace_marker_breakpoint_ops;
13289}
13290
53a5351d 13291/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13292 structures. */
c906108c
SS
13293
13294void
fba45db2 13295delete_breakpoint (struct breakpoint *bpt)
c906108c 13296{
52f0bd74 13297 struct breakpoint *b;
c906108c 13298
8a3fe4f8 13299 gdb_assert (bpt != NULL);
c906108c 13300
4a64f543
MS
13301 /* Has this bp already been deleted? This can happen because
13302 multiple lists can hold pointers to bp's. bpstat lists are
13303 especial culprits.
13304
13305 One example of this happening is a watchpoint's scope bp. When
13306 the scope bp triggers, we notice that the watchpoint is out of
13307 scope, and delete it. We also delete its scope bp. But the
13308 scope bp is marked "auto-deleting", and is already on a bpstat.
13309 That bpstat is then checked for auto-deleting bp's, which are
13310 deleted.
13311
13312 A real solution to this problem might involve reference counts in
13313 bp's, and/or giving them pointers back to their referencing
13314 bpstat's, and teaching delete_breakpoint to only free a bp's
13315 storage when no more references were extent. A cheaper bandaid
13316 was chosen. */
c906108c
SS
13317 if (bpt->type == bp_none)
13318 return;
13319
4a64f543
MS
13320 /* At least avoid this stale reference until the reference counting
13321 of breakpoints gets resolved. */
d0fb5eae 13322 if (bpt->related_breakpoint != bpt)
e5a0a904 13323 {
d0fb5eae 13324 struct breakpoint *related;
3a5c3e22 13325 struct watchpoint *w;
d0fb5eae
JK
13326
13327 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13328 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13329 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13330 w = (struct watchpoint *) bpt;
13331 else
13332 w = NULL;
13333 if (w != NULL)
13334 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13335
13336 /* Unlink bpt from the bpt->related_breakpoint ring. */
13337 for (related = bpt; related->related_breakpoint != bpt;
13338 related = related->related_breakpoint);
13339 related->related_breakpoint = bpt->related_breakpoint;
13340 bpt->related_breakpoint = bpt;
e5a0a904
JK
13341 }
13342
a9634178
TJB
13343 /* watch_command_1 creates a watchpoint but only sets its number if
13344 update_watchpoint succeeds in creating its bp_locations. If there's
13345 a problem in that process, we'll be asked to delete the half-created
13346 watchpoint. In that case, don't announce the deletion. */
13347 if (bpt->number)
13348 observer_notify_breakpoint_deleted (bpt);
c906108c 13349
c906108c
SS
13350 if (breakpoint_chain == bpt)
13351 breakpoint_chain = bpt->next;
13352
c906108c
SS
13353 ALL_BREAKPOINTS (b)
13354 if (b->next == bpt)
c5aa993b
JM
13355 {
13356 b->next = bpt->next;
13357 break;
13358 }
c906108c 13359
f431efe5
PA
13360 /* Be sure no bpstat's are pointing at the breakpoint after it's
13361 been freed. */
13362 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13363 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13364 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13365 commands are associated with the bpstat; if we remove it here,
13366 then the later call to bpstat_do_actions (&stop_bpstat); in
13367 event-top.c won't do anything, and temporary breakpoints with
13368 commands won't work. */
13369
13370 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13371
4a64f543
MS
13372 /* Now that breakpoint is removed from breakpoint list, update the
13373 global location list. This will remove locations that used to
13374 belong to this breakpoint. Do this before freeing the breakpoint
13375 itself, since remove_breakpoint looks at location's owner. It
13376 might be better design to have location completely
13377 self-contained, but it's not the case now. */
44702360 13378 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13379
4a64f543
MS
13380 /* On the chance that someone will soon try again to delete this
13381 same bp, we mark it as deleted before freeing its storage. */
c906108c 13382 bpt->type = bp_none;
4d01a485 13383 delete bpt;
c906108c
SS
13384}
13385
4d6140d9
AC
13386static void
13387do_delete_breakpoint_cleanup (void *b)
13388{
9a3c8263 13389 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13390}
13391
13392struct cleanup *
13393make_cleanup_delete_breakpoint (struct breakpoint *b)
13394{
13395 return make_cleanup (do_delete_breakpoint_cleanup, b);
13396}
13397
51be5b68
PA
13398/* Iterator function to call a user-provided callback function once
13399 for each of B and its related breakpoints. */
13400
13401static void
13402iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13403 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13404{
13405 struct breakpoint *related;
13406
13407 related = b;
13408 do
13409 {
13410 struct breakpoint *next;
13411
13412 /* FUNCTION may delete RELATED. */
13413 next = related->related_breakpoint;
13414
13415 if (next == related)
13416 {
13417 /* RELATED is the last ring entry. */
48649e1b 13418 function (related);
51be5b68
PA
13419
13420 /* FUNCTION may have deleted it, so we'd never reach back to
13421 B. There's nothing left to do anyway, so just break
13422 out. */
13423 break;
13424 }
13425 else
48649e1b 13426 function (related);
51be5b68
PA
13427
13428 related = next;
13429 }
13430 while (related != b);
13431}
95a42b64 13432
4495129a 13433static void
981a3fb3 13434delete_command (const char *arg, int from_tty)
c906108c 13435{
35df4500 13436 struct breakpoint *b, *b_tmp;
c906108c 13437
ea9365bb
TT
13438 dont_repeat ();
13439
c906108c
SS
13440 if (arg == 0)
13441 {
13442 int breaks_to_delete = 0;
13443
46c6471b
PA
13444 /* Delete all breakpoints if no argument. Do not delete
13445 internal breakpoints, these have to be deleted with an
13446 explicit breakpoint number argument. */
c5aa993b 13447 ALL_BREAKPOINTS (b)
46c6471b 13448 if (user_breakpoint_p (b))
973d738b
DJ
13449 {
13450 breaks_to_delete = 1;
13451 break;
13452 }
c906108c
SS
13453
13454 /* Ask user only if there are some breakpoints to delete. */
13455 if (!from_tty
e2e0b3e5 13456 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13457 {
35df4500 13458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13459 if (user_breakpoint_p (b))
c5aa993b 13460 delete_breakpoint (b);
c906108c
SS
13461 }
13462 }
13463 else
48649e1b
TT
13464 map_breakpoint_numbers
13465 (arg, [&] (breakpoint *b)
13466 {
13467 iterate_over_related_breakpoints (b, delete_breakpoint);
13468 });
c906108c
SS
13469}
13470
c2f4122d
PA
13471/* Return true if all locations of B bound to PSPACE are pending. If
13472 PSPACE is NULL, all locations of all program spaces are
13473 considered. */
13474
0d381245 13475static int
c2f4122d 13476all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13477{
c2f4122d
PA
13478 struct bp_location *loc;
13479
13480 for (loc = b->loc; loc != NULL; loc = loc->next)
13481 if ((pspace == NULL
13482 || loc->pspace == pspace)
13483 && !loc->shlib_disabled
8645ff69 13484 && !loc->pspace->executing_startup)
0d381245
VP
13485 return 0;
13486 return 1;
fe3f5fa8
VP
13487}
13488
776592bf
DE
13489/* Subroutine of update_breakpoint_locations to simplify it.
13490 Return non-zero if multiple fns in list LOC have the same name.
13491 Null names are ignored. */
13492
13493static int
13494ambiguous_names_p (struct bp_location *loc)
13495{
13496 struct bp_location *l;
13497 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13498 (int (*) (const void *,
13499 const void *)) streq,
776592bf
DE
13500 NULL, xcalloc, xfree);
13501
13502 for (l = loc; l != NULL; l = l->next)
13503 {
13504 const char **slot;
13505 const char *name = l->function_name;
13506
13507 /* Allow for some names to be NULL, ignore them. */
13508 if (name == NULL)
13509 continue;
13510
13511 slot = (const char **) htab_find_slot (htab, (const void *) name,
13512 INSERT);
4a64f543
MS
13513 /* NOTE: We can assume slot != NULL here because xcalloc never
13514 returns NULL. */
776592bf
DE
13515 if (*slot != NULL)
13516 {
13517 htab_delete (htab);
13518 return 1;
13519 }
13520 *slot = name;
13521 }
13522
13523 htab_delete (htab);
13524 return 0;
13525}
13526
0fb4aa4b
PA
13527/* When symbols change, it probably means the sources changed as well,
13528 and it might mean the static tracepoint markers are no longer at
13529 the same address or line numbers they used to be at last we
13530 checked. Losing your static tracepoints whenever you rebuild is
13531 undesirable. This function tries to resync/rematch gdb static
13532 tracepoints with the markers on the target, for static tracepoints
13533 that have not been set by marker id. Static tracepoint that have
13534 been set by marker id are reset by marker id in breakpoint_re_set.
13535 The heuristic is:
13536
13537 1) For a tracepoint set at a specific address, look for a marker at
13538 the old PC. If one is found there, assume to be the same marker.
13539 If the name / string id of the marker found is different from the
13540 previous known name, assume that means the user renamed the marker
13541 in the sources, and output a warning.
13542
13543 2) For a tracepoint set at a given line number, look for a marker
13544 at the new address of the old line number. If one is found there,
13545 assume to be the same marker. If the name / string id of the
13546 marker found is different from the previous known name, assume that
13547 means the user renamed the marker in the sources, and output a
13548 warning.
13549
13550 3) If a marker is no longer found at the same address or line, it
13551 may mean the marker no longer exists. But it may also just mean
13552 the code changed a bit. Maybe the user added a few lines of code
13553 that made the marker move up or down (in line number terms). Ask
13554 the target for info about the marker with the string id as we knew
13555 it. If found, update line number and address in the matching
13556 static tracepoint. This will get confused if there's more than one
13557 marker with the same ID (possible in UST, although unadvised
13558 precisely because it confuses tools). */
13559
13560static struct symtab_and_line
13561update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13562{
d9b3f62e 13563 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13564 struct static_tracepoint_marker marker;
13565 CORE_ADDR pc;
0fb4aa4b
PA
13566
13567 pc = sal.pc;
13568 if (sal.line)
13569 find_line_pc (sal.symtab, sal.line, &pc);
13570
13571 if (target_static_tracepoint_marker_at (pc, &marker))
13572 {
d9b3f62e 13573 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13574 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13575 b->number,
d9b3f62e 13576 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13577
d9b3f62e
PA
13578 xfree (tp->static_trace_marker_id);
13579 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13580 release_static_tracepoint_marker (&marker);
13581
13582 return sal;
13583 }
13584
13585 /* Old marker wasn't found on target at lineno. Try looking it up
13586 by string ID. */
13587 if (!sal.explicit_pc
13588 && sal.line != 0
13589 && sal.symtab != NULL
d9b3f62e 13590 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13591 {
13592 VEC(static_tracepoint_marker_p) *markers;
13593
13594 markers
d9b3f62e 13595 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13596
13597 if (!VEC_empty(static_tracepoint_marker_p, markers))
13598 {
0fb4aa4b 13599 struct symbol *sym;
80e1d417 13600 struct static_tracepoint_marker *tpmarker;
79a45e25 13601 struct ui_out *uiout = current_uiout;
67994074 13602 struct explicit_location explicit_loc;
0fb4aa4b 13603
80e1d417 13604 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13605
d9b3f62e 13606 xfree (tp->static_trace_marker_id);
80e1d417 13607 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13608
13609 warning (_("marker for static tracepoint %d (%s) not "
13610 "found at previous line number"),
d9b3f62e 13611 b->number, tp->static_trace_marker_id);
0fb4aa4b 13612
51abb421 13613 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13614 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13615 uiout->text ("Now in ");
0fb4aa4b
PA
13616 if (sym)
13617 {
112e8700
SM
13618 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13619 uiout->text (" at ");
0fb4aa4b 13620 }
112e8700 13621 uiout->field_string ("file",
05cba821 13622 symtab_to_filename_for_display (sal2.symtab));
112e8700 13623 uiout->text (":");
0fb4aa4b 13624
112e8700 13625 if (uiout->is_mi_like_p ())
0fb4aa4b 13626 {
0b0865da 13627 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13628
112e8700 13629 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13630 }
13631
112e8700
SM
13632 uiout->field_int ("line", sal2.line);
13633 uiout->text ("\n");
0fb4aa4b 13634
80e1d417 13635 b->loc->line_number = sal2.line;
2f202fde 13636 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13637
d28cd78a 13638 b->location.reset (NULL);
67994074
KS
13639 initialize_explicit_location (&explicit_loc);
13640 explicit_loc.source_filename
00e52e53 13641 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13642 explicit_loc.line_offset.offset = b->loc->line_number;
13643 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13644 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13645
13646 /* Might be nice to check if function changed, and warn if
13647 so. */
13648
80e1d417 13649 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13650 }
13651 }
13652 return sal;
13653}
13654
8d3788bd
VP
13655/* Returns 1 iff locations A and B are sufficiently same that
13656 we don't need to report breakpoint as changed. */
13657
13658static int
13659locations_are_equal (struct bp_location *a, struct bp_location *b)
13660{
13661 while (a && b)
13662 {
13663 if (a->address != b->address)
13664 return 0;
13665
13666 if (a->shlib_disabled != b->shlib_disabled)
13667 return 0;
13668
13669 if (a->enabled != b->enabled)
13670 return 0;
13671
13672 a = a->next;
13673 b = b->next;
13674 }
13675
13676 if ((a == NULL) != (b == NULL))
13677 return 0;
13678
13679 return 1;
13680}
13681
c2f4122d
PA
13682/* Split all locations of B that are bound to PSPACE out of B's
13683 location list to a separate list and return that list's head. If
13684 PSPACE is NULL, hoist out all locations of B. */
13685
13686static struct bp_location *
13687hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13688{
13689 struct bp_location head;
13690 struct bp_location *i = b->loc;
13691 struct bp_location **i_link = &b->loc;
13692 struct bp_location *hoisted = &head;
13693
13694 if (pspace == NULL)
13695 {
13696 i = b->loc;
13697 b->loc = NULL;
13698 return i;
13699 }
13700
13701 head.next = NULL;
13702
13703 while (i != NULL)
13704 {
13705 if (i->pspace == pspace)
13706 {
13707 *i_link = i->next;
13708 i->next = NULL;
13709 hoisted->next = i;
13710 hoisted = i;
13711 }
13712 else
13713 i_link = &i->next;
13714 i = *i_link;
13715 }
13716
13717 return head.next;
13718}
13719
13720/* Create new breakpoint locations for B (a hardware or software
13721 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13722 zero, then B is a ranged breakpoint. Only recreates locations for
13723 FILTER_PSPACE. Locations of other program spaces are left
13724 untouched. */
f1310107 13725
0e30163f 13726void
0d381245 13727update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13728 struct program_space *filter_pspace,
6c5b2ebe
PA
13729 gdb::array_view<const symtab_and_line> sals,
13730 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13731{
13732 int i;
c2f4122d 13733 struct bp_location *existing_locations;
0d381245 13734
6c5b2ebe 13735 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13736 {
13737 /* Ranged breakpoints have only one start location and one end
13738 location. */
13739 b->enable_state = bp_disabled;
f8eba3c6
TT
13740 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13741 "multiple locations found\n"),
13742 b->number);
13743 return;
13744 }
f1310107 13745
4a64f543
MS
13746 /* If there's no new locations, and all existing locations are
13747 pending, don't do anything. This optimizes the common case where
13748 all locations are in the same shared library, that was unloaded.
13749 We'd like to retain the location, so that when the library is
13750 loaded again, we don't loose the enabled/disabled status of the
13751 individual locations. */
6c5b2ebe 13752 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13753 return;
13754
c2f4122d 13755 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13756
6c5b2ebe 13757 for (const auto &sal : sals)
fe3f5fa8 13758 {
f8eba3c6
TT
13759 struct bp_location *new_loc;
13760
6c5b2ebe 13761 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13762
6c5b2ebe 13763 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13764
0d381245
VP
13765 /* Reparse conditions, they might contain references to the
13766 old symtab. */
13767 if (b->cond_string != NULL)
13768 {
bbc13ae3 13769 const char *s;
fe3f5fa8 13770
0d381245 13771 s = b->cond_string;
492d29ea 13772 TRY
0d381245 13773 {
6c5b2ebe
PA
13774 new_loc->cond = parse_exp_1 (&s, sal.pc,
13775 block_for_pc (sal.pc),
0d381245
VP
13776 0);
13777 }
492d29ea 13778 CATCH (e, RETURN_MASK_ERROR)
0d381245 13779 {
3e43a32a
MS
13780 warning (_("failed to reevaluate condition "
13781 "for breakpoint %d: %s"),
0d381245
VP
13782 b->number, e.message);
13783 new_loc->enabled = 0;
13784 }
492d29ea 13785 END_CATCH
0d381245 13786 }
fe3f5fa8 13787
6c5b2ebe 13788 if (!sals_end.empty ())
f1310107 13789 {
6c5b2ebe 13790 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13791
6c5b2ebe 13792 new_loc->length = end - sals[0].pc + 1;
f1310107 13793 }
0d381245 13794 }
fe3f5fa8 13795
4a64f543
MS
13796 /* If possible, carry over 'disable' status from existing
13797 breakpoints. */
0d381245
VP
13798 {
13799 struct bp_location *e = existing_locations;
776592bf
DE
13800 /* If there are multiple breakpoints with the same function name,
13801 e.g. for inline functions, comparing function names won't work.
13802 Instead compare pc addresses; this is just a heuristic as things
13803 may have moved, but in practice it gives the correct answer
13804 often enough until a better solution is found. */
13805 int have_ambiguous_names = ambiguous_names_p (b->loc);
13806
0d381245
VP
13807 for (; e; e = e->next)
13808 {
13809 if (!e->enabled && e->function_name)
13810 {
13811 struct bp_location *l = b->loc;
776592bf
DE
13812 if (have_ambiguous_names)
13813 {
13814 for (; l; l = l->next)
f1310107 13815 if (breakpoint_locations_match (e, l))
776592bf
DE
13816 {
13817 l->enabled = 0;
13818 break;
13819 }
13820 }
13821 else
13822 {
13823 for (; l; l = l->next)
13824 if (l->function_name
13825 && strcmp (e->function_name, l->function_name) == 0)
13826 {
13827 l->enabled = 0;
13828 break;
13829 }
13830 }
0d381245
VP
13831 }
13832 }
13833 }
fe3f5fa8 13834
8d3788bd
VP
13835 if (!locations_are_equal (existing_locations, b->loc))
13836 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13837}
13838
f00aae0f 13839/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13840 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13841
6c5b2ebe 13842static std::vector<symtab_and_line>
f00aae0f 13843location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13844 struct program_space *search_pspace, int *found)
ef23e705 13845{
492d29ea 13846 struct gdb_exception exception = exception_none;
ef23e705 13847
983af33b 13848 gdb_assert (b->ops != NULL);
ef23e705 13849
6c5b2ebe
PA
13850 std::vector<symtab_and_line> sals;
13851
492d29ea 13852 TRY
ef23e705 13853 {
6c5b2ebe 13854 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13855 }
492d29ea 13856 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13857 {
13858 int not_found_and_ok = 0;
492d29ea
PA
13859
13860 exception = e;
13861
ef23e705
TJB
13862 /* For pending breakpoints, it's expected that parsing will
13863 fail until the right shared library is loaded. User has
13864 already told to create pending breakpoints and don't need
13865 extra messages. If breakpoint is in bp_shlib_disabled
13866 state, then user already saw the message about that
13867 breakpoint being disabled, and don't want to see more
13868 errors. */
58438ac1 13869 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13870 && (b->condition_not_parsed
13871 || (b->loc != NULL
13872 && search_pspace != NULL
13873 && b->loc->pspace != search_pspace)
ef23e705 13874 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13875 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13876 || b->enable_state == bp_disabled))
13877 not_found_and_ok = 1;
13878
13879 if (!not_found_and_ok)
13880 {
13881 /* We surely don't want to warn about the same breakpoint
13882 10 times. One solution, implemented here, is disable
13883 the breakpoint on error. Another solution would be to
13884 have separate 'warning emitted' flag. Since this
13885 happens only when a binary has changed, I don't know
13886 which approach is better. */
13887 b->enable_state = bp_disabled;
13888 throw_exception (e);
13889 }
13890 }
492d29ea 13891 END_CATCH
ef23e705 13892
492d29ea 13893 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13894 {
6c5b2ebe
PA
13895 for (auto &sal : sals)
13896 resolve_sal_pc (&sal);
f00aae0f 13897 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13898 {
ed1d1739
KS
13899 char *cond_string, *extra_string;
13900 int thread, task;
ef23e705 13901
6c5b2ebe 13902 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13903 &cond_string, &thread, &task,
13904 &extra_string);
f00aae0f 13905 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13906 if (cond_string)
13907 b->cond_string = cond_string;
13908 b->thread = thread;
13909 b->task = task;
e7e0cddf 13910 if (extra_string)
f00aae0f
KS
13911 {
13912 xfree (b->extra_string);
13913 b->extra_string = extra_string;
13914 }
ef23e705
TJB
13915 b->condition_not_parsed = 0;
13916 }
13917
983af33b 13918 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13919 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13920
58438ac1
TT
13921 *found = 1;
13922 }
13923 else
13924 *found = 0;
ef23e705
TJB
13925
13926 return sals;
13927}
13928
348d480f
PA
13929/* The default re_set method, for typical hardware or software
13930 breakpoints. Reevaluate the breakpoint and recreate its
13931 locations. */
13932
13933static void
28010a5d 13934breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13935{
c2f4122d 13936 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13937 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13938
6c5b2ebe
PA
13939 int found;
13940 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13941 filter_pspace, &found);
ef23e705 13942 if (found)
6c5b2ebe 13943 expanded = std::move (sals);
ef23e705 13944
f00aae0f 13945 if (b->location_range_end != NULL)
f1310107 13946 {
6c5b2ebe
PA
13947 std::vector<symtab_and_line> sals_end
13948 = location_to_sals (b, b->location_range_end.get (),
13949 filter_pspace, &found);
f1310107 13950 if (found)
6c5b2ebe 13951 expanded_end = std::move (sals_end);
f1310107
TJB
13952 }
13953
c2f4122d 13954 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13955}
13956
983af33b
SDJ
13957/* Default method for creating SALs from an address string. It basically
13958 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13959
13960static void
f00aae0f
KS
13961create_sals_from_location_default (const struct event_location *location,
13962 struct linespec_result *canonical,
13963 enum bptype type_wanted)
983af33b 13964{
f00aae0f 13965 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13966}
13967
13968/* Call create_breakpoints_sal for the given arguments. This is the default
13969 function for the `create_breakpoints_sal' method of
13970 breakpoint_ops. */
13971
13972static void
13973create_breakpoints_sal_default (struct gdbarch *gdbarch,
13974 struct linespec_result *canonical,
e1e01040
PA
13975 gdb::unique_xmalloc_ptr<char> cond_string,
13976 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13977 enum bptype type_wanted,
13978 enum bpdisp disposition,
13979 int thread,
13980 int task, int ignore_count,
13981 const struct breakpoint_ops *ops,
13982 int from_tty, int enabled,
44f238bb 13983 int internal, unsigned flags)
983af33b 13984{
e1e01040
PA
13985 create_breakpoints_sal (gdbarch, canonical,
13986 std::move (cond_string),
13987 std::move (extra_string),
983af33b
SDJ
13988 type_wanted, disposition,
13989 thread, task, ignore_count, ops, from_tty,
44f238bb 13990 enabled, internal, flags);
983af33b
SDJ
13991}
13992
13993/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13994 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13995
6c5b2ebe 13996static std::vector<symtab_and_line>
f00aae0f
KS
13997decode_location_default (struct breakpoint *b,
13998 const struct event_location *location,
6c5b2ebe 13999 struct program_space *search_pspace)
983af33b
SDJ
14000{
14001 struct linespec_result canonical;
14002
c2f4122d 14003 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14004 (struct symtab *) NULL, 0,
14005 &canonical, multiple_symbols_all,
14006 b->filter);
14007
14008 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 14009 gdb_assert (canonical.lsals.size () < 2);
983af33b 14010
6c5b2ebe 14011 if (!canonical.lsals.empty ())
983af33b 14012 {
6c5b2ebe
PA
14013 const linespec_sals &lsal = canonical.lsals[0];
14014 return std::move (lsal.sals);
983af33b 14015 }
6c5b2ebe 14016 return {};
983af33b
SDJ
14017}
14018
bf469271 14019/* Reset a breakpoint. */
c906108c 14020
bf469271
PA
14021static void
14022breakpoint_re_set_one (breakpoint *b)
c906108c 14023{
fdf44873
TT
14024 input_radix = b->input_radix;
14025 set_language (b->language);
c906108c 14026
348d480f 14027 b->ops->re_set (b);
c906108c
SS
14028}
14029
c2f4122d
PA
14030/* Re-set breakpoint locations for the current program space.
14031 Locations bound to other program spaces are left untouched. */
14032
c906108c 14033void
69de3c6a 14034breakpoint_re_set (void)
c906108c 14035{
35df4500 14036 struct breakpoint *b, *b_tmp;
2a7f3dff 14037
c5aa993b 14038 {
fdf44873
TT
14039 scoped_restore_current_language save_language;
14040 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14041 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14042
5ed8105e
PA
14043 /* Note: we must not try to insert locations until after all
14044 breakpoints have been re-set. Otherwise, e.g., when re-setting
14045 breakpoint 1, we'd insert the locations of breakpoint 2, which
14046 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14047
5ed8105e
PA
14048 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14049 {
bf469271
PA
14050 TRY
14051 {
14052 breakpoint_re_set_one (b);
14053 }
14054 CATCH (ex, RETURN_MASK_ALL)
14055 {
14056 exception_fprintf (gdb_stderr, ex,
14057 "Error in re-setting breakpoint %d: ",
14058 b->number);
14059 }
14060 END_CATCH
5ed8105e 14061 }
5ed8105e
PA
14062
14063 jit_breakpoint_re_set ();
14064 }
6c95b8df 14065
af02033e
PP
14066 create_overlay_event_breakpoint ();
14067 create_longjmp_master_breakpoint ();
14068 create_std_terminate_master_breakpoint ();
186c406b 14069 create_exception_master_breakpoint ();
2a7f3dff
PA
14070
14071 /* Now we can insert. */
14072 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14073}
14074\f
c906108c
SS
14075/* Reset the thread number of this breakpoint:
14076
14077 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14078 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14079void
fba45db2 14080breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14081{
14082 if (b->thread != -1)
14083 {
39f77062 14084 if (in_thread_list (inferior_ptid))
5d5658a1 14085 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14086
14087 /* We're being called after following a fork. The new fork is
14088 selected as current, and unless this was a vfork will have a
14089 different program space from the original thread. Reset that
14090 as well. */
14091 b->loc->pspace = current_program_space;
c906108c
SS
14092 }
14093}
14094
03ac34d5
MS
14095/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14096 If from_tty is nonzero, it prints a message to that effect,
14097 which ends with a period (no newline). */
14098
c906108c 14099void
fba45db2 14100set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14101{
52f0bd74 14102 struct breakpoint *b;
c906108c
SS
14103
14104 if (count < 0)
14105 count = 0;
14106
14107 ALL_BREAKPOINTS (b)
14108 if (b->number == bptnum)
c5aa993b 14109 {
d77f58be
SS
14110 if (is_tracepoint (b))
14111 {
14112 if (from_tty && count != 0)
14113 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14114 bptnum);
14115 return;
14116 }
14117
c5aa993b 14118 b->ignore_count = count;
221ea385
KS
14119 if (from_tty)
14120 {
14121 if (count == 0)
3e43a32a
MS
14122 printf_filtered (_("Will stop next time "
14123 "breakpoint %d is reached."),
221ea385
KS
14124 bptnum);
14125 else if (count == 1)
a3f17187 14126 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14127 bptnum);
14128 else
3e43a32a
MS
14129 printf_filtered (_("Will ignore next %d "
14130 "crossings of breakpoint %d."),
221ea385
KS
14131 count, bptnum);
14132 }
8d3788bd 14133 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14134 return;
14135 }
c906108c 14136
8a3fe4f8 14137 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14138}
14139
c906108c
SS
14140/* Command to set ignore-count of breakpoint N to COUNT. */
14141
14142static void
fba45db2 14143ignore_command (char *args, int from_tty)
c906108c
SS
14144{
14145 char *p = args;
52f0bd74 14146 int num;
c906108c
SS
14147
14148 if (p == 0)
e2e0b3e5 14149 error_no_arg (_("a breakpoint number"));
c5aa993b 14150
c906108c 14151 num = get_number (&p);
5c44784c 14152 if (num == 0)
8a3fe4f8 14153 error (_("bad breakpoint number: '%s'"), args);
c906108c 14154 if (*p == 0)
8a3fe4f8 14155 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14156
14157 set_ignore_count (num,
14158 longest_to_int (value_as_long (parse_and_eval (p))),
14159 from_tty);
221ea385
KS
14160 if (from_tty)
14161 printf_filtered ("\n");
c906108c
SS
14162}
14163\f
14164/* Call FUNCTION on each of the breakpoints
14165 whose numbers are given in ARGS. */
14166
14167static void
896b6bda 14168map_breakpoint_numbers (const char *args,
48649e1b 14169 gdb::function_view<void (breakpoint *)> function)
c906108c 14170{
52f0bd74
AC
14171 int num;
14172 struct breakpoint *b, *tmp;
c906108c 14173
b9d61307 14174 if (args == 0 || *args == '\0')
e2e0b3e5 14175 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14176
bfd28288 14177 number_or_range_parser parser (args);
197f0a60 14178
bfd28288 14179 while (!parser.finished ())
c906108c 14180 {
bfd28288
PA
14181 const char *p = parser.cur_tok ();
14182 bool match = false;
197f0a60 14183
bfd28288 14184 num = parser.get_number ();
5c44784c 14185 if (num == 0)
c5aa993b 14186 {
8a3fe4f8 14187 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14188 }
14189 else
14190 {
14191 ALL_BREAKPOINTS_SAFE (b, tmp)
14192 if (b->number == num)
14193 {
bfd28288 14194 match = true;
48649e1b 14195 function (b);
11cf8741 14196 break;
5c44784c 14197 }
bfd28288 14198 if (!match)
a3f17187 14199 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14200 }
c906108c
SS
14201 }
14202}
14203
0d381245 14204static struct bp_location *
cb791d59 14205find_location_by_number (const char *number)
0d381245 14206{
cb791d59 14207 const char *p1;
0d381245
VP
14208 int bp_num;
14209 int loc_num;
14210 struct breakpoint *b;
14211 struct bp_location *loc;
14212
0d381245 14213 p1 = number;
cb791d59
TT
14214 bp_num = get_number_trailer (&p1, '.');
14215 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14216 error (_("Bad breakpoint number '%s'"), number);
14217
14218 ALL_BREAKPOINTS (b)
14219 if (b->number == bp_num)
14220 {
14221 break;
14222 }
14223
14224 if (!b || b->number != bp_num)
14225 error (_("Bad breakpoint number '%s'"), number);
14226
cb791d59
TT
14227 /* Skip the dot. */
14228 ++p1;
14229 const char *save = p1;
197f0a60 14230 loc_num = get_number (&p1);
0d381245
VP
14231 if (loc_num == 0)
14232 error (_("Bad breakpoint location number '%s'"), number);
14233
14234 --loc_num;
14235 loc = b->loc;
14236 for (;loc_num && loc; --loc_num, loc = loc->next)
14237 ;
14238 if (!loc)
cb791d59 14239 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14240
14241 return loc;
14242}
14243
14244
1900040c
MS
14245/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14246 If from_tty is nonzero, it prints a message to that effect,
14247 which ends with a period (no newline). */
14248
c906108c 14249void
fba45db2 14250disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14251{
14252 /* Never disable a watchpoint scope breakpoint; we want to
14253 hit them when we leave scope so we can delete both the
14254 watchpoint and its scope breakpoint at that time. */
14255 if (bpt->type == bp_watchpoint_scope)
14256 return;
14257
b5de0fa7 14258 bpt->enable_state = bp_disabled;
c906108c 14259
b775012e
LM
14260 /* Mark breakpoint locations modified. */
14261 mark_breakpoint_modified (bpt);
14262
d248b706
KY
14263 if (target_supports_enable_disable_tracepoint ()
14264 && current_trace_status ()->running && is_tracepoint (bpt))
14265 {
14266 struct bp_location *location;
14267
14268 for (location = bpt->loc; location; location = location->next)
14269 target_disable_tracepoint (location);
14270 }
14271
44702360 14272 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14273
8d3788bd 14274 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14275}
14276
c906108c 14277static void
981a3fb3 14278disable_command (const char *args, int from_tty)
c906108c 14279{
c906108c 14280 if (args == 0)
46c6471b
PA
14281 {
14282 struct breakpoint *bpt;
14283
14284 ALL_BREAKPOINTS (bpt)
14285 if (user_breakpoint_p (bpt))
14286 disable_breakpoint (bpt);
14287 }
9eaabc75 14288 else
0d381245 14289 {
cb791d59 14290 std::string num = extract_arg (&args);
9eaabc75 14291
cb791d59 14292 while (!num.empty ())
d248b706 14293 {
cb791d59 14294 if (num.find ('.') != std::string::npos)
b775012e 14295 {
cb791d59 14296 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14297
14298 if (loc)
14299 {
14300 if (loc->enabled)
14301 {
14302 loc->enabled = 0;
14303 mark_breakpoint_location_modified (loc);
14304 }
14305 if (target_supports_enable_disable_tracepoint ()
14306 && current_trace_status ()->running && loc->owner
14307 && is_tracepoint (loc->owner))
14308 target_disable_tracepoint (loc);
14309 }
44702360 14310 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14311 }
9eaabc75 14312 else
48649e1b
TT
14313 map_breakpoint_numbers
14314 (num.c_str (), [&] (breakpoint *b)
14315 {
14316 iterate_over_related_breakpoints (b, disable_breakpoint);
14317 });
9eaabc75 14318 num = extract_arg (&args);
d248b706 14319 }
0d381245 14320 }
c906108c
SS
14321}
14322
14323static void
816338b5
SS
14324enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14325 int count)
c906108c 14326{
afe38095 14327 int target_resources_ok;
c906108c
SS
14328
14329 if (bpt->type == bp_hardware_breakpoint)
14330 {
14331 int i;
c5aa993b 14332 i = hw_breakpoint_used_count ();
53a5351d 14333 target_resources_ok =
d92524f1 14334 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14335 i + 1, 0);
c906108c 14336 if (target_resources_ok == 0)
8a3fe4f8 14337 error (_("No hardware breakpoint support in the target."));
c906108c 14338 else if (target_resources_ok < 0)
8a3fe4f8 14339 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14340 }
14341
cc60f2e3 14342 if (is_watchpoint (bpt))
c906108c 14343 {
d07205c2 14344 /* Initialize it just to avoid a GCC false warning. */
f486487f 14345 enum enable_state orig_enable_state = bp_disabled;
dde02812 14346
492d29ea 14347 TRY
c906108c 14348 {
3a5c3e22
PA
14349 struct watchpoint *w = (struct watchpoint *) bpt;
14350
1e718ff1
TJB
14351 orig_enable_state = bpt->enable_state;
14352 bpt->enable_state = bp_enabled;
3a5c3e22 14353 update_watchpoint (w, 1 /* reparse */);
c906108c 14354 }
492d29ea 14355 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14356 {
1e718ff1 14357 bpt->enable_state = orig_enable_state;
dde02812
ES
14358 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14359 bpt->number);
14360 return;
c5aa993b 14361 }
492d29ea 14362 END_CATCH
c906108c 14363 }
0101ce28 14364
b775012e
LM
14365 bpt->enable_state = bp_enabled;
14366
14367 /* Mark breakpoint locations modified. */
14368 mark_breakpoint_modified (bpt);
14369
d248b706
KY
14370 if (target_supports_enable_disable_tracepoint ()
14371 && current_trace_status ()->running && is_tracepoint (bpt))
14372 {
14373 struct bp_location *location;
14374
14375 for (location = bpt->loc; location; location = location->next)
14376 target_enable_tracepoint (location);
14377 }
14378
b4c291bb 14379 bpt->disposition = disposition;
816338b5 14380 bpt->enable_count = count;
44702360 14381 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14382
8d3788bd 14383 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14384}
14385
fe3f5fa8 14386
c906108c 14387void
fba45db2 14388enable_breakpoint (struct breakpoint *bpt)
c906108c 14389{
816338b5 14390 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14391}
14392
c906108c
SS
14393/* The enable command enables the specified breakpoints (or all defined
14394 breakpoints) so they once again become (or continue to be) effective
1272ad14 14395 in stopping the inferior. */
c906108c 14396
c906108c 14397static void
981a3fb3 14398enable_command (const char *args, int from_tty)
c906108c 14399{
c906108c 14400 if (args == 0)
46c6471b
PA
14401 {
14402 struct breakpoint *bpt;
14403
14404 ALL_BREAKPOINTS (bpt)
14405 if (user_breakpoint_p (bpt))
14406 enable_breakpoint (bpt);
14407 }
9eaabc75 14408 else
0d381245 14409 {
cb791d59 14410 std::string num = extract_arg (&args);
9eaabc75 14411
cb791d59 14412 while (!num.empty ())
d248b706 14413 {
cb791d59 14414 if (num.find ('.') != std::string::npos)
b775012e 14415 {
cb791d59 14416 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14417
14418 if (loc)
14419 {
14420 if (!loc->enabled)
14421 {
14422 loc->enabled = 1;
14423 mark_breakpoint_location_modified (loc);
14424 }
14425 if (target_supports_enable_disable_tracepoint ()
14426 && current_trace_status ()->running && loc->owner
14427 && is_tracepoint (loc->owner))
14428 target_enable_tracepoint (loc);
14429 }
44702360 14430 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14431 }
9eaabc75 14432 else
48649e1b
TT
14433 map_breakpoint_numbers
14434 (num.c_str (), [&] (breakpoint *b)
14435 {
14436 iterate_over_related_breakpoints (b, enable_breakpoint);
14437 });
9eaabc75 14438 num = extract_arg (&args);
d248b706 14439 }
0d381245 14440 }
c906108c
SS
14441}
14442
c906108c 14443static void
4495129a 14444enable_once_command (const char *args, int from_tty)
c906108c 14445{
48649e1b
TT
14446 map_breakpoint_numbers
14447 (args, [&] (breakpoint *b)
14448 {
14449 iterate_over_related_breakpoints
14450 (b, [&] (breakpoint *bpt)
14451 {
14452 enable_breakpoint_disp (bpt, disp_disable, 1);
14453 });
14454 });
816338b5
SS
14455}
14456
14457static void
4495129a 14458enable_count_command (const char *args, int from_tty)
816338b5 14459{
b9d61307
SM
14460 int count;
14461
14462 if (args == NULL)
14463 error_no_arg (_("hit count"));
14464
14465 count = get_number (&args);
816338b5 14466
48649e1b
TT
14467 map_breakpoint_numbers
14468 (args, [&] (breakpoint *b)
14469 {
14470 iterate_over_related_breakpoints
14471 (b, [&] (breakpoint *bpt)
14472 {
14473 enable_breakpoint_disp (bpt, disp_disable, count);
14474 });
14475 });
c906108c
SS
14476}
14477
c906108c 14478static void
4495129a 14479enable_delete_command (const char *args, int from_tty)
c906108c 14480{
48649e1b
TT
14481 map_breakpoint_numbers
14482 (args, [&] (breakpoint *b)
14483 {
14484 iterate_over_related_breakpoints
14485 (b, [&] (breakpoint *bpt)
14486 {
14487 enable_breakpoint_disp (bpt, disp_del, 1);
14488 });
14489 });
c906108c
SS
14490}
14491\f
fa8d40ab 14492static void
981a3fb3 14493set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14494{
14495}
14496
14497static void
981a3fb3 14498show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14499{
14500}
14501
1f3b5d1b
PP
14502/* Invalidate last known value of any hardware watchpoint if
14503 the memory which that value represents has been written to by
14504 GDB itself. */
14505
14506static void
8de0566d
YQ
14507invalidate_bp_value_on_memory_change (struct inferior *inferior,
14508 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14509 const bfd_byte *data)
14510{
14511 struct breakpoint *bp;
14512
14513 ALL_BREAKPOINTS (bp)
14514 if (bp->enable_state == bp_enabled
3a5c3e22 14515 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14516 {
3a5c3e22 14517 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14518
3a5c3e22
PA
14519 if (wp->val_valid && wp->val)
14520 {
14521 struct bp_location *loc;
14522
14523 for (loc = bp->loc; loc != NULL; loc = loc->next)
14524 if (loc->loc_type == bp_loc_hardware_watchpoint
14525 && loc->address + loc->length > addr
14526 && addr + len > loc->address)
14527 {
14528 value_free (wp->val);
14529 wp->val = NULL;
14530 wp->val_valid = 0;
14531 }
14532 }
1f3b5d1b
PP
14533 }
14534}
14535
8181d85f
DJ
14536/* Create and insert a breakpoint for software single step. */
14537
14538void
6c95b8df 14539insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14540 const address_space *aspace,
4a64f543 14541 CORE_ADDR next_pc)
8181d85f 14542{
7c16b83e
PA
14543 struct thread_info *tp = inferior_thread ();
14544 struct symtab_and_line sal;
14545 CORE_ADDR pc = next_pc;
8181d85f 14546
34b7e8a6
PA
14547 if (tp->control.single_step_breakpoints == NULL)
14548 {
14549 tp->control.single_step_breakpoints
5d5658a1 14550 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14551 }
8181d85f 14552
7c16b83e
PA
14553 sal = find_pc_line (pc, 0);
14554 sal.pc = pc;
14555 sal.section = find_pc_overlay (pc);
14556 sal.explicit_pc = 1;
34b7e8a6 14557 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14558
7c16b83e 14559 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14560}
14561
93f9a11f
YQ
14562/* Insert single step breakpoints according to the current state. */
14563
14564int
14565insert_single_step_breakpoints (struct gdbarch *gdbarch)
14566{
f5ea389a 14567 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14568 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14569
f5ea389a 14570 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14571
a0ff9e1a 14572 if (!next_pcs.empty ())
93f9a11f 14573 {
f5ea389a 14574 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14575 struct address_space *aspace = get_frame_address_space (frame);
14576
a0ff9e1a 14577 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14578 insert_single_step_breakpoint (gdbarch, aspace, pc);
14579
93f9a11f
YQ
14580 return 1;
14581 }
14582 else
14583 return 0;
14584}
14585
34b7e8a6 14586/* See breakpoint.h. */
f02253f1
HZ
14587
14588int
7c16b83e 14589breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14590 const address_space *aspace,
7c16b83e 14591 CORE_ADDR pc)
1aafd4da 14592{
7c16b83e 14593 struct bp_location *loc;
1aafd4da 14594
7c16b83e
PA
14595 for (loc = bp->loc; loc != NULL; loc = loc->next)
14596 if (loc->inserted
14597 && breakpoint_location_address_match (loc, aspace, pc))
14598 return 1;
1aafd4da 14599
7c16b83e 14600 return 0;
ef370185
JB
14601}
14602
14603/* Check whether a software single-step breakpoint is inserted at
14604 PC. */
14605
14606int
accd0bcd 14607single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14608 CORE_ADDR pc)
14609{
34b7e8a6
PA
14610 struct breakpoint *bpt;
14611
14612 ALL_BREAKPOINTS (bpt)
14613 {
14614 if (bpt->type == bp_single_step
14615 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14616 return 1;
14617 }
14618 return 0;
1aafd4da
UW
14619}
14620
1042e4c0
SS
14621/* Tracepoint-specific operations. */
14622
14623/* Set tracepoint count to NUM. */
14624static void
14625set_tracepoint_count (int num)
14626{
14627 tracepoint_count = num;
4fa62494 14628 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14629}
14630
70221824 14631static void
f2fc3015 14632trace_command (char *arg_in, int from_tty)
1042e4c0 14633{
f2fc3015 14634 const char *arg = arg_in;
55aa24fb 14635 struct breakpoint_ops *ops;
55aa24fb 14636
ffc2605c
TT
14637 event_location_up location = string_to_event_location (&arg,
14638 current_language);
5b56227b 14639 if (location != NULL
ffc2605c 14640 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14641 ops = &tracepoint_probe_breakpoint_ops;
14642 else
14643 ops = &tracepoint_breakpoint_ops;
14644
558a9d82 14645 create_breakpoint (get_current_arch (),
ffc2605c 14646 location.get (),
f00aae0f 14647 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14648 0 /* tempflag */,
14649 bp_tracepoint /* type_wanted */,
14650 0 /* Ignore count */,
14651 pending_break_support,
14652 ops,
14653 from_tty,
14654 1 /* enabled */,
14655 0 /* internal */, 0);
1042e4c0
SS
14656}
14657
70221824 14658static void
f2fc3015 14659ftrace_command (char *arg_in, int from_tty)
7a697b8d 14660{
f2fc3015 14661 const char *arg = arg_in;
ffc2605c
TT
14662 event_location_up location = string_to_event_location (&arg,
14663 current_language);
558a9d82 14664 create_breakpoint (get_current_arch (),
ffc2605c 14665 location.get (),
f00aae0f 14666 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14667 0 /* tempflag */,
14668 bp_fast_tracepoint /* type_wanted */,
14669 0 /* Ignore count */,
14670 pending_break_support,
14671 &tracepoint_breakpoint_ops,
14672 from_tty,
14673 1 /* enabled */,
14674 0 /* internal */, 0);
0fb4aa4b
PA
14675}
14676
14677/* strace command implementation. Creates a static tracepoint. */
14678
70221824 14679static void
f2fc3015 14680strace_command (char *arg_in, int from_tty)
0fb4aa4b 14681{
f2fc3015 14682 const char *arg = arg_in;
983af33b 14683 struct breakpoint_ops *ops;
ffc2605c 14684 event_location_up location;
983af33b
SDJ
14685
14686 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14687 or with a normal static tracepoint. */
61012eef 14688 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14689 {
14690 ops = &strace_marker_breakpoint_ops;
14691 location = new_linespec_location (&arg);
14692 }
983af33b 14693 else
f00aae0f
KS
14694 {
14695 ops = &tracepoint_breakpoint_ops;
14696 location = string_to_event_location (&arg, current_language);
14697 }
983af33b 14698
558a9d82 14699 create_breakpoint (get_current_arch (),
ffc2605c 14700 location.get (),
f00aae0f 14701 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14702 0 /* tempflag */,
14703 bp_static_tracepoint /* type_wanted */,
14704 0 /* Ignore count */,
14705 pending_break_support,
14706 ops,
14707 from_tty,
14708 1 /* enabled */,
14709 0 /* internal */, 0);
7a697b8d
SS
14710}
14711
409873ef
SS
14712/* Set up a fake reader function that gets command lines from a linked
14713 list that was acquired during tracepoint uploading. */
14714
14715static struct uploaded_tp *this_utp;
3149d8c1 14716static int next_cmd;
409873ef
SS
14717
14718static char *
14719read_uploaded_action (void)
14720{
14721 char *rslt;
14722
3149d8c1 14723 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14724
3149d8c1 14725 next_cmd++;
409873ef
SS
14726
14727 return rslt;
14728}
14729
00bf0b85
SS
14730/* Given information about a tracepoint as recorded on a target (which
14731 can be either a live system or a trace file), attempt to create an
14732 equivalent GDB tracepoint. This is not a reliable process, since
14733 the target does not necessarily have all the information used when
14734 the tracepoint was originally defined. */
14735
d9b3f62e 14736struct tracepoint *
00bf0b85 14737create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14738{
f2fc3015
TT
14739 const char *addr_str;
14740 char small_buf[100];
d9b3f62e 14741 struct tracepoint *tp;
fd9b8c24 14742
409873ef
SS
14743 if (utp->at_string)
14744 addr_str = utp->at_string;
14745 else
14746 {
14747 /* In the absence of a source location, fall back to raw
14748 address. Since there is no way to confirm that the address
14749 means the same thing as when the trace was started, warn the
14750 user. */
3e43a32a
MS
14751 warning (_("Uploaded tracepoint %d has no "
14752 "source location, using raw address"),
409873ef 14753 utp->number);
8c042590 14754 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14755 addr_str = small_buf;
14756 }
14757
14758 /* There's not much we can do with a sequence of bytecodes. */
14759 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14760 warning (_("Uploaded tracepoint %d condition "
14761 "has no source form, ignoring it"),
409873ef 14762 utp->number);
d5551862 14763
ffc2605c
TT
14764 event_location_up location = string_to_event_location (&addr_str,
14765 current_language);
8cdf0e15 14766 if (!create_breakpoint (get_current_arch (),
ffc2605c 14767 location.get (),
f00aae0f 14768 utp->cond_string, -1, addr_str,
e7e0cddf 14769 0 /* parse cond/thread */,
8cdf0e15 14770 0 /* tempflag */,
0fb4aa4b 14771 utp->type /* type_wanted */,
8cdf0e15
VP
14772 0 /* Ignore count */,
14773 pending_break_support,
348d480f 14774 &tracepoint_breakpoint_ops,
8cdf0e15 14775 0 /* from_tty */,
84f4c1fe 14776 utp->enabled /* enabled */,
44f238bb
PA
14777 0 /* internal */,
14778 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14779 return NULL;
fd9b8c24 14780
409873ef 14781 /* Get the tracepoint we just created. */
fd9b8c24
PA
14782 tp = get_tracepoint (tracepoint_count);
14783 gdb_assert (tp != NULL);
d5551862 14784
00bf0b85
SS
14785 if (utp->pass > 0)
14786 {
8c042590 14787 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14788 tp->number);
00bf0b85 14789
409873ef 14790 trace_pass_command (small_buf, 0);
00bf0b85
SS
14791 }
14792
409873ef
SS
14793 /* If we have uploaded versions of the original commands, set up a
14794 special-purpose "reader" function and call the usual command line
14795 reader, then pass the result to the breakpoint command-setting
14796 function. */
3149d8c1 14797 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14798 {
93921405 14799 command_line_up cmd_list;
00bf0b85 14800
409873ef 14801 this_utp = utp;
3149d8c1 14802 next_cmd = 0;
d5551862 14803
409873ef
SS
14804 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14805
c1fc2657 14806 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14807 }
3149d8c1
SS
14808 else if (!VEC_empty (char_ptr, utp->actions)
14809 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14810 warning (_("Uploaded tracepoint %d actions "
14811 "have no source form, ignoring them"),
409873ef 14812 utp->number);
00bf0b85 14813
f196051f 14814 /* Copy any status information that might be available. */
c1fc2657 14815 tp->hit_count = utp->hit_count;
f196051f
SS
14816 tp->traceframe_usage = utp->traceframe_usage;
14817
00bf0b85 14818 return tp;
d9b3f62e 14819}
00bf0b85 14820
1042e4c0
SS
14821/* Print information on tracepoint number TPNUM_EXP, or all if
14822 omitted. */
14823
14824static void
11db9430 14825info_tracepoints_command (char *args, int from_tty)
1042e4c0 14826{
79a45e25 14827 struct ui_out *uiout = current_uiout;
e5a67952 14828 int num_printed;
1042e4c0 14829
e5a67952 14830 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14831
14832 if (num_printed == 0)
1042e4c0 14833 {
e5a67952 14834 if (args == NULL || *args == '\0')
112e8700 14835 uiout->message ("No tracepoints.\n");
d77f58be 14836 else
112e8700 14837 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14838 }
ad443146
SS
14839
14840 default_collect_info ();
1042e4c0
SS
14841}
14842
4a64f543 14843/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14844 Not supported by all targets. */
14845static void
14846enable_trace_command (char *args, int from_tty)
14847{
14848 enable_command (args, from_tty);
14849}
14850
4a64f543 14851/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14852 Not supported by all targets. */
14853static void
14854disable_trace_command (char *args, int from_tty)
14855{
14856 disable_command (args, from_tty);
14857}
14858
4a64f543 14859/* Remove a tracepoint (or all if no argument). */
1042e4c0 14860static void
4495129a 14861delete_trace_command (const char *arg, int from_tty)
1042e4c0 14862{
35df4500 14863 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14864
14865 dont_repeat ();
14866
14867 if (arg == 0)
14868 {
14869 int breaks_to_delete = 0;
14870
14871 /* Delete all breakpoints if no argument.
14872 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14873 have to be deleted with an explicit breakpoint number
14874 argument. */
1042e4c0 14875 ALL_TRACEPOINTS (b)
46c6471b 14876 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14877 {
14878 breaks_to_delete = 1;
14879 break;
14880 }
1042e4c0
SS
14881
14882 /* Ask user only if there are some breakpoints to delete. */
14883 if (!from_tty
14884 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14885 {
35df4500 14886 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14887 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14888 delete_breakpoint (b);
1042e4c0
SS
14889 }
14890 }
14891 else
48649e1b
TT
14892 map_breakpoint_numbers
14893 (arg, [&] (breakpoint *b)
14894 {
14895 iterate_over_related_breakpoints (b, delete_breakpoint);
14896 });
1042e4c0
SS
14897}
14898
197f0a60
TT
14899/* Helper function for trace_pass_command. */
14900
14901static void
d9b3f62e 14902trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14903{
d9b3f62e 14904 tp->pass_count = count;
c1fc2657 14905 observer_notify_breakpoint_modified (tp);
197f0a60
TT
14906 if (from_tty)
14907 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14908 tp->number, count);
197f0a60
TT
14909}
14910
1042e4c0
SS
14911/* Set passcount for tracepoint.
14912
14913 First command argument is passcount, second is tracepoint number.
14914 If tracepoint number omitted, apply to most recently defined.
14915 Also accepts special argument "all". */
14916
14917static void
14918trace_pass_command (char *args, int from_tty)
14919{
d9b3f62e 14920 struct tracepoint *t1;
1042e4c0 14921 unsigned int count;
1042e4c0
SS
14922
14923 if (args == 0 || *args == 0)
3e43a32a
MS
14924 error (_("passcount command requires an "
14925 "argument (count + optional TP num)"));
1042e4c0 14926
4a64f543 14927 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14928
529480d0 14929 args = skip_spaces (args);
1042e4c0
SS
14930 if (*args && strncasecmp (args, "all", 3) == 0)
14931 {
d9b3f62e
PA
14932 struct breakpoint *b;
14933
1042e4c0 14934 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14935 if (*args)
14936 error (_("Junk at end of arguments."));
1042e4c0 14937
d9b3f62e 14938 ALL_TRACEPOINTS (b)
197f0a60 14939 {
d9b3f62e 14940 t1 = (struct tracepoint *) b;
197f0a60
TT
14941 trace_pass_set_count (t1, count, from_tty);
14942 }
14943 }
14944 else if (*args == '\0')
1042e4c0 14945 {
5fa1d40e 14946 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14947 if (t1)
197f0a60
TT
14948 trace_pass_set_count (t1, count, from_tty);
14949 }
14950 else
14951 {
bfd28288
PA
14952 number_or_range_parser parser (args);
14953 while (!parser.finished ())
1042e4c0 14954 {
bfd28288 14955 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14956 if (t1)
14957 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14958 }
14959 }
1042e4c0
SS
14960}
14961
d9b3f62e 14962struct tracepoint *
1042e4c0
SS
14963get_tracepoint (int num)
14964{
14965 struct breakpoint *t;
14966
14967 ALL_TRACEPOINTS (t)
14968 if (t->number == num)
d9b3f62e 14969 return (struct tracepoint *) t;
1042e4c0
SS
14970
14971 return NULL;
14972}
14973
d5551862
SS
14974/* Find the tracepoint with the given target-side number (which may be
14975 different from the tracepoint number after disconnecting and
14976 reconnecting). */
14977
d9b3f62e 14978struct tracepoint *
d5551862
SS
14979get_tracepoint_by_number_on_target (int num)
14980{
d9b3f62e 14981 struct breakpoint *b;
d5551862 14982
d9b3f62e
PA
14983 ALL_TRACEPOINTS (b)
14984 {
14985 struct tracepoint *t = (struct tracepoint *) b;
14986
14987 if (t->number_on_target == num)
14988 return t;
14989 }
d5551862
SS
14990
14991 return NULL;
14992}
14993
1042e4c0 14994/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14995 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14996 If the argument is missing, the most recent tracepoint
14997 (tracepoint_count) is returned. */
14998
d9b3f62e 14999struct tracepoint *
197f0a60 15000get_tracepoint_by_number (char **arg,
bfd28288 15001 number_or_range_parser *parser)
1042e4c0 15002{
1042e4c0
SS
15003 struct breakpoint *t;
15004 int tpnum;
15005 char *instring = arg == NULL ? NULL : *arg;
15006
bfd28288 15007 if (parser != NULL)
197f0a60 15008 {
bfd28288
PA
15009 gdb_assert (!parser->finished ());
15010 tpnum = parser->get_number ();
197f0a60
TT
15011 }
15012 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15013 tpnum = tracepoint_count;
1042e4c0 15014 else
197f0a60 15015 tpnum = get_number (arg);
1042e4c0
SS
15016
15017 if (tpnum <= 0)
15018 {
15019 if (instring && *instring)
15020 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15021 instring);
15022 else
5fa1d40e 15023 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15024 return NULL;
15025 }
15026
15027 ALL_TRACEPOINTS (t)
15028 if (t->number == tpnum)
15029 {
d9b3f62e 15030 return (struct tracepoint *) t;
1042e4c0
SS
15031 }
15032
1042e4c0
SS
15033 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15034 return NULL;
15035}
15036
d9b3f62e
PA
15037void
15038print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15039{
15040 if (b->thread != -1)
15041 fprintf_unfiltered (fp, " thread %d", b->thread);
15042
15043 if (b->task != 0)
15044 fprintf_unfiltered (fp, " task %d", b->task);
15045
15046 fprintf_unfiltered (fp, "\n");
15047}
15048
6149aea9
PA
15049/* Save information on user settable breakpoints (watchpoints, etc) to
15050 a new script file named FILENAME. If FILTER is non-NULL, call it
15051 on each breakpoint and only include the ones for which it returns
15052 non-zero. */
15053
1042e4c0 15054static void
4495129a 15055save_breakpoints (const char *filename, int from_tty,
6149aea9 15056 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15057{
15058 struct breakpoint *tp;
6149aea9 15059 int any = 0;
6149aea9 15060 int extra_trace_bits = 0;
1042e4c0 15061
6149aea9
PA
15062 if (filename == 0 || *filename == 0)
15063 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15064
15065 /* See if we have anything to save. */
6149aea9 15066 ALL_BREAKPOINTS (tp)
1042e4c0 15067 {
6149aea9 15068 /* Skip internal and momentary breakpoints. */
09d682a4 15069 if (!user_breakpoint_p (tp))
6149aea9
PA
15070 continue;
15071
15072 /* If we have a filter, only save the breakpoints it accepts. */
15073 if (filter && !filter (tp))
15074 continue;
15075
15076 any = 1;
15077
15078 if (is_tracepoint (tp))
15079 {
15080 extra_trace_bits = 1;
15081
15082 /* We can stop searching. */
15083 break;
15084 }
1042e4c0 15085 }
6149aea9
PA
15086
15087 if (!any)
1042e4c0 15088 {
6149aea9 15089 warning (_("Nothing to save."));
1042e4c0
SS
15090 return;
15091 }
15092
ee0c3293 15093 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15094
15095 stdio_file fp;
15096
ee0c3293 15097 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15098 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15099 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15100
6149aea9 15101 if (extra_trace_bits)
d7e74731 15102 save_trace_state_variables (&fp);
8bf6485c 15103
6149aea9 15104 ALL_BREAKPOINTS (tp)
1042e4c0 15105 {
6149aea9 15106 /* Skip internal and momentary breakpoints. */
09d682a4 15107 if (!user_breakpoint_p (tp))
6149aea9 15108 continue;
8bf6485c 15109
6149aea9
PA
15110 /* If we have a filter, only save the breakpoints it accepts. */
15111 if (filter && !filter (tp))
15112 continue;
15113
d7e74731 15114 tp->ops->print_recreate (tp, &fp);
1042e4c0 15115
6149aea9
PA
15116 /* Note, we can't rely on tp->number for anything, as we can't
15117 assume the recreated breakpoint numbers will match. Use $bpnum
15118 instead. */
15119
15120 if (tp->cond_string)
d7e74731 15121 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15122
15123 if (tp->ignore_count)
d7e74731 15124 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15125
2d9442cc 15126 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15127 {
d7e74731 15128 fp.puts (" commands\n");
a7bdde9e 15129
d7e74731 15130 current_uiout->redirect (&fp);
492d29ea 15131 TRY
1042e4c0 15132 {
d1b0a7bf 15133 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15134 }
492d29ea
PA
15135 CATCH (ex, RETURN_MASK_ALL)
15136 {
112e8700 15137 current_uiout->redirect (NULL);
492d29ea
PA
15138 throw_exception (ex);
15139 }
15140 END_CATCH
1042e4c0 15141
112e8700 15142 current_uiout->redirect (NULL);
d7e74731 15143 fp.puts (" end\n");
1042e4c0 15144 }
6149aea9
PA
15145
15146 if (tp->enable_state == bp_disabled)
d7e74731 15147 fp.puts ("disable $bpnum\n");
6149aea9
PA
15148
15149 /* If this is a multi-location breakpoint, check if the locations
15150 should be individually disabled. Watchpoint locations are
15151 special, and not user visible. */
15152 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15153 {
15154 struct bp_location *loc;
15155 int n = 1;
15156
15157 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15158 if (!loc->enabled)
d7e74731 15159 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15160 }
1042e4c0 15161 }
8bf6485c 15162
6149aea9 15163 if (extra_trace_bits && *default_collect)
d7e74731 15164 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15165
1042e4c0 15166 if (from_tty)
ee0c3293 15167 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15168}
15169
15170/* The `save breakpoints' command. */
15171
15172static void
4495129a 15173save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15174{
15175 save_breakpoints (args, from_tty, NULL);
15176}
15177
15178/* The `save tracepoints' command. */
15179
15180static void
4495129a 15181save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15182{
15183 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15184}
15185
15186/* Create a vector of all tracepoints. */
15187
15188VEC(breakpoint_p) *
eeae04df 15189all_tracepoints (void)
1042e4c0
SS
15190{
15191 VEC(breakpoint_p) *tp_vec = 0;
15192 struct breakpoint *tp;
15193
15194 ALL_TRACEPOINTS (tp)
15195 {
15196 VEC_safe_push (breakpoint_p, tp_vec, tp);
15197 }
15198
15199 return tp_vec;
15200}
15201
c906108c 15202\f
629500fa
KS
15203/* This help string is used to consolidate all the help string for specifying
15204 locations used by several commands. */
15205
15206#define LOCATION_HELP_STRING \
15207"Linespecs are colon-separated lists of location parameters, such as\n\
15208source filename, function name, label name, and line number.\n\
15209Example: To specify the start of a label named \"the_top\" in the\n\
15210function \"fact\" in the file \"factorial.c\", use\n\
15211\"factorial.c:fact:the_top\".\n\
15212\n\
15213Address locations begin with \"*\" and specify an exact address in the\n\
15214program. Example: To specify the fourth byte past the start function\n\
15215\"main\", use \"*main + 4\".\n\
15216\n\
15217Explicit locations are similar to linespecs but use an option/argument\n\
15218syntax to specify location parameters.\n\
15219Example: To specify the start of the label named \"the_top\" in the\n\
15220function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15221-function fact -label the_top\".\n"
15222
4a64f543
MS
15223/* This help string is used for the break, hbreak, tbreak and thbreak
15224 commands. It is defined as a macro to prevent duplication.
15225 COMMAND should be a string constant containing the name of the
15226 command. */
629500fa 15227
31e2b00f 15228#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15229command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15230PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15231probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15232guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15233`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15234LOCATION may be a linespec, address, or explicit location as described\n\
15235below.\n\
15236\n\
dc10affe
PA
15237With no LOCATION, uses current execution address of the selected\n\
15238stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15239\n\
15240THREADNUM is the number from \"info threads\".\n\
15241CONDITION is a boolean expression.\n\
629500fa 15242\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15243Multiple breakpoints at one place are permitted, and useful if their\n\
15244conditions are different.\n\
31e2b00f
AS
15245\n\
15246Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15247
44feb3ce
TT
15248/* List of subcommands for "catch". */
15249static struct cmd_list_element *catch_cmdlist;
15250
15251/* List of subcommands for "tcatch". */
15252static struct cmd_list_element *tcatch_cmdlist;
15253
9ac4176b 15254void
a121b7c1 15255add_catch_command (const char *name, const char *docstring,
82ae6c8d 15256 cmd_sfunc_ftype *sfunc,
625e8578 15257 completer_ftype *completer,
44feb3ce
TT
15258 void *user_data_catch,
15259 void *user_data_tcatch)
15260{
15261 struct cmd_list_element *command;
15262
0450cc4c 15263 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15264 &catch_cmdlist);
15265 set_cmd_sfunc (command, sfunc);
15266 set_cmd_context (command, user_data_catch);
a96d9b2e 15267 set_cmd_completer (command, completer);
44feb3ce 15268
0450cc4c 15269 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15270 &tcatch_cmdlist);
15271 set_cmd_sfunc (command, sfunc);
15272 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15273 set_cmd_completer (command, completer);
44feb3ce
TT
15274}
15275
6149aea9 15276static void
981a3fb3 15277save_command (const char *arg, int from_tty)
6149aea9 15278{
3e43a32a
MS
15279 printf_unfiltered (_("\"save\" must be followed by "
15280 "the name of a save subcommand.\n"));
635c7e8a 15281 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15282}
15283
84f4c1fe
PM
15284struct breakpoint *
15285iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15286 void *data)
15287{
35df4500 15288 struct breakpoint *b, *b_tmp;
84f4c1fe 15289
35df4500 15290 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15291 {
15292 if ((*callback) (b, data))
15293 return b;
15294 }
15295
15296 return NULL;
15297}
15298
0574c78f
GB
15299/* Zero if any of the breakpoint's locations could be a location where
15300 functions have been inlined, nonzero otherwise. */
15301
15302static int
15303is_non_inline_function (struct breakpoint *b)
15304{
15305 /* The shared library event breakpoint is set on the address of a
15306 non-inline function. */
15307 if (b->type == bp_shlib_event)
15308 return 1;
15309
15310 return 0;
15311}
15312
15313/* Nonzero if the specified PC cannot be a location where functions
15314 have been inlined. */
15315
15316int
accd0bcd 15317pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15318 const struct target_waitstatus *ws)
0574c78f
GB
15319{
15320 struct breakpoint *b;
15321 struct bp_location *bl;
15322
15323 ALL_BREAKPOINTS (b)
15324 {
15325 if (!is_non_inline_function (b))
15326 continue;
15327
15328 for (bl = b->loc; bl != NULL; bl = bl->next)
15329 {
15330 if (!bl->shlib_disabled
09ac7c10 15331 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15332 return 1;
15333 }
15334 }
15335
15336 return 0;
15337}
15338
2f202fde
JK
15339/* Remove any references to OBJFILE which is going to be freed. */
15340
15341void
15342breakpoint_free_objfile (struct objfile *objfile)
15343{
15344 struct bp_location **locp, *loc;
15345
15346 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15347 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15348 loc->symtab = NULL;
15349}
15350
2060206e
PA
15351void
15352initialize_breakpoint_ops (void)
15353{
15354 static int initialized = 0;
15355
15356 struct breakpoint_ops *ops;
15357
15358 if (initialized)
15359 return;
15360 initialized = 1;
15361
15362 /* The breakpoint_ops structure to be inherit by all kinds of
15363 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15364 internal and momentary breakpoints, etc.). */
15365 ops = &bkpt_base_breakpoint_ops;
15366 *ops = base_breakpoint_ops;
15367 ops->re_set = bkpt_re_set;
15368 ops->insert_location = bkpt_insert_location;
15369 ops->remove_location = bkpt_remove_location;
15370 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15371 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15372 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15373 ops->decode_location = bkpt_decode_location;
2060206e
PA
15374
15375 /* The breakpoint_ops structure to be used in regular breakpoints. */
15376 ops = &bkpt_breakpoint_ops;
15377 *ops = bkpt_base_breakpoint_ops;
15378 ops->re_set = bkpt_re_set;
15379 ops->resources_needed = bkpt_resources_needed;
15380 ops->print_it = bkpt_print_it;
15381 ops->print_mention = bkpt_print_mention;
15382 ops->print_recreate = bkpt_print_recreate;
15383
15384 /* Ranged breakpoints. */
15385 ops = &ranged_breakpoint_ops;
15386 *ops = bkpt_breakpoint_ops;
15387 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15388 ops->resources_needed = resources_needed_ranged_breakpoint;
15389 ops->print_it = print_it_ranged_breakpoint;
15390 ops->print_one = print_one_ranged_breakpoint;
15391 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15392 ops->print_mention = print_mention_ranged_breakpoint;
15393 ops->print_recreate = print_recreate_ranged_breakpoint;
15394
15395 /* Internal breakpoints. */
15396 ops = &internal_breakpoint_ops;
15397 *ops = bkpt_base_breakpoint_ops;
15398 ops->re_set = internal_bkpt_re_set;
15399 ops->check_status = internal_bkpt_check_status;
15400 ops->print_it = internal_bkpt_print_it;
15401 ops->print_mention = internal_bkpt_print_mention;
15402
15403 /* Momentary breakpoints. */
15404 ops = &momentary_breakpoint_ops;
15405 *ops = bkpt_base_breakpoint_ops;
15406 ops->re_set = momentary_bkpt_re_set;
15407 ops->check_status = momentary_bkpt_check_status;
15408 ops->print_it = momentary_bkpt_print_it;
15409 ops->print_mention = momentary_bkpt_print_mention;
15410
55aa24fb
SDJ
15411 /* Probe breakpoints. */
15412 ops = &bkpt_probe_breakpoint_ops;
15413 *ops = bkpt_breakpoint_ops;
15414 ops->insert_location = bkpt_probe_insert_location;
15415 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15416 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15417 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15418
2060206e
PA
15419 /* Watchpoints. */
15420 ops = &watchpoint_breakpoint_ops;
15421 *ops = base_breakpoint_ops;
15422 ops->re_set = re_set_watchpoint;
15423 ops->insert_location = insert_watchpoint;
15424 ops->remove_location = remove_watchpoint;
15425 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15426 ops->check_status = check_status_watchpoint;
15427 ops->resources_needed = resources_needed_watchpoint;
15428 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15429 ops->print_it = print_it_watchpoint;
15430 ops->print_mention = print_mention_watchpoint;
15431 ops->print_recreate = print_recreate_watchpoint;
427cd150 15432 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15433
15434 /* Masked watchpoints. */
15435 ops = &masked_watchpoint_breakpoint_ops;
15436 *ops = watchpoint_breakpoint_ops;
15437 ops->insert_location = insert_masked_watchpoint;
15438 ops->remove_location = remove_masked_watchpoint;
15439 ops->resources_needed = resources_needed_masked_watchpoint;
15440 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15441 ops->print_it = print_it_masked_watchpoint;
15442 ops->print_one_detail = print_one_detail_masked_watchpoint;
15443 ops->print_mention = print_mention_masked_watchpoint;
15444 ops->print_recreate = print_recreate_masked_watchpoint;
15445
15446 /* Tracepoints. */
15447 ops = &tracepoint_breakpoint_ops;
15448 *ops = base_breakpoint_ops;
15449 ops->re_set = tracepoint_re_set;
15450 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15451 ops->print_one_detail = tracepoint_print_one_detail;
15452 ops->print_mention = tracepoint_print_mention;
15453 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15454 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15455 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15456 ops->decode_location = tracepoint_decode_location;
983af33b 15457
55aa24fb
SDJ
15458 /* Probe tracepoints. */
15459 ops = &tracepoint_probe_breakpoint_ops;
15460 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15461 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15462 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15463
983af33b
SDJ
15464 /* Static tracepoints with marker (`-m'). */
15465 ops = &strace_marker_breakpoint_ops;
15466 *ops = tracepoint_breakpoint_ops;
5f700d83 15467 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15468 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15469 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15470
15471 /* Fork catchpoints. */
15472 ops = &catch_fork_breakpoint_ops;
15473 *ops = base_breakpoint_ops;
15474 ops->insert_location = insert_catch_fork;
15475 ops->remove_location = remove_catch_fork;
15476 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15477 ops->print_it = print_it_catch_fork;
15478 ops->print_one = print_one_catch_fork;
15479 ops->print_mention = print_mention_catch_fork;
15480 ops->print_recreate = print_recreate_catch_fork;
15481
15482 /* Vfork catchpoints. */
15483 ops = &catch_vfork_breakpoint_ops;
15484 *ops = base_breakpoint_ops;
15485 ops->insert_location = insert_catch_vfork;
15486 ops->remove_location = remove_catch_vfork;
15487 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15488 ops->print_it = print_it_catch_vfork;
15489 ops->print_one = print_one_catch_vfork;
15490 ops->print_mention = print_mention_catch_vfork;
15491 ops->print_recreate = print_recreate_catch_vfork;
15492
15493 /* Exec catchpoints. */
15494 ops = &catch_exec_breakpoint_ops;
15495 *ops = base_breakpoint_ops;
2060206e
PA
15496 ops->insert_location = insert_catch_exec;
15497 ops->remove_location = remove_catch_exec;
15498 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15499 ops->print_it = print_it_catch_exec;
15500 ops->print_one = print_one_catch_exec;
15501 ops->print_mention = print_mention_catch_exec;
15502 ops->print_recreate = print_recreate_catch_exec;
15503
edcc5120
TT
15504 /* Solib-related catchpoints. */
15505 ops = &catch_solib_breakpoint_ops;
15506 *ops = base_breakpoint_ops;
edcc5120
TT
15507 ops->insert_location = insert_catch_solib;
15508 ops->remove_location = remove_catch_solib;
15509 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15510 ops->check_status = check_status_catch_solib;
15511 ops->print_it = print_it_catch_solib;
15512 ops->print_one = print_one_catch_solib;
15513 ops->print_mention = print_mention_catch_solib;
15514 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15515
15516 ops = &dprintf_breakpoint_ops;
15517 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15518 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15519 ops->resources_needed = bkpt_resources_needed;
15520 ops->print_it = bkpt_print_it;
15521 ops->print_mention = bkpt_print_mention;
2d9442cc 15522 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15523 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15524 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15525}
15526
8bfd80db
YQ
15527/* Chain containing all defined "enable breakpoint" subcommands. */
15528
15529static struct cmd_list_element *enablebreaklist = NULL;
15530
c906108c 15531void
fba45db2 15532_initialize_breakpoint (void)
c906108c
SS
15533{
15534 struct cmd_list_element *c;
15535
2060206e
PA
15536 initialize_breakpoint_ops ();
15537
84acb35a 15538 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15539 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15540 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15541
55aa24fb
SDJ
15542 breakpoint_objfile_key
15543 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15544
c906108c
SS
15545 breakpoint_chain = 0;
15546 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15547 before a breakpoint is set. */
15548 breakpoint_count = 0;
15549
1042e4c0
SS
15550 tracepoint_count = 0;
15551
1bedd215
AC
15552 add_com ("ignore", class_breakpoint, ignore_command, _("\
15553Set ignore-count of breakpoint number N to COUNT.\n\
15554Usage is `ignore N COUNT'."));
c906108c 15555
1bedd215 15556 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15557Set commands to be executed when the given breakpoints are hit.\n\
15558Give a space-separated breakpoint list as argument after \"commands\".\n\
15559A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15560(e.g. `5-7').\n\
c906108c
SS
15561With no argument, the targeted breakpoint is the last one set.\n\
15562The commands themselves follow starting on the next line.\n\
15563Type a line containing \"end\" to indicate the end of them.\n\
15564Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15565then no output is printed when it is hit, except what the commands print."));
c906108c 15566
d55637df 15567 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15568Specify breakpoint number N to break only if COND is true.\n\
c906108c 15569Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15570expression to be evaluated whenever breakpoint N is reached."));
d55637df 15571 set_cmd_completer (c, condition_completer);
c906108c 15572
1bedd215 15573 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15574Set a temporary breakpoint.\n\
c906108c
SS
15575Like \"break\" except the breakpoint is only temporary,\n\
15576so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15577by using \"enable delete\" on the breakpoint number.\n\
15578\n"
15579BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15580 set_cmd_completer (c, location_completer);
c94fdfd0 15581
1bedd215 15582 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15583Set a hardware assisted breakpoint.\n\
c906108c 15584Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15585some target hardware may not have this support.\n\
15586\n"
15587BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15588 set_cmd_completer (c, location_completer);
c906108c 15589
1bedd215 15590 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15591Set a temporary hardware assisted breakpoint.\n\
c906108c 15592Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15593so it will be deleted when hit.\n\
15594\n"
15595BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15596 set_cmd_completer (c, location_completer);
c906108c 15597
1bedd215
AC
15598 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15599Enable some breakpoints.\n\
c906108c
SS
15600Give breakpoint numbers (separated by spaces) as arguments.\n\
15601With no subcommand, breakpoints are enabled until you command otherwise.\n\
15602This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15603With a subcommand you can enable temporarily."),
c906108c 15604 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15605
15606 add_com_alias ("en", "enable", class_breakpoint, 1);
15607
84951ab5 15608 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15609Enable some breakpoints.\n\
c906108c
SS
15610Give breakpoint numbers (separated by spaces) as arguments.\n\
15611This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15612May be abbreviated to simply \"enable\".\n"),
c5aa993b 15613 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15614
1a966eab
AC
15615 add_cmd ("once", no_class, enable_once_command, _("\
15616Enable breakpoints for one hit. Give breakpoint numbers.\n\
15617If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15618 &enablebreaklist);
15619
1a966eab
AC
15620 add_cmd ("delete", no_class, enable_delete_command, _("\
15621Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15622If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15623 &enablebreaklist);
15624
816338b5
SS
15625 add_cmd ("count", no_class, enable_count_command, _("\
15626Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15627If a breakpoint is hit while enabled in this fashion,\n\
15628the count is decremented; when it reaches zero, the breakpoint is disabled."),
15629 &enablebreaklist);
15630
1a966eab
AC
15631 add_cmd ("delete", no_class, enable_delete_command, _("\
15632Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15633If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15634 &enablelist);
15635
1a966eab
AC
15636 add_cmd ("once", no_class, enable_once_command, _("\
15637Enable breakpoints for one hit. Give breakpoint numbers.\n\
15638If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15639 &enablelist);
15640
15641 add_cmd ("count", no_class, enable_count_command, _("\
15642Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15643If a breakpoint is hit while enabled in this fashion,\n\
15644the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15645 &enablelist);
15646
1bedd215
AC
15647 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15648Disable some breakpoints.\n\
c906108c
SS
15649Arguments are breakpoint numbers with spaces in between.\n\
15650To disable all breakpoints, give no argument.\n\
64b9b334 15651A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15652 &disablelist, "disable ", 1, &cmdlist);
15653 add_com_alias ("dis", "disable", class_breakpoint, 1);
15654 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15655
1a966eab
AC
15656 add_cmd ("breakpoints", class_alias, disable_command, _("\
15657Disable some breakpoints.\n\
c906108c
SS
15658Arguments are breakpoint numbers with spaces in between.\n\
15659To disable all breakpoints, give no argument.\n\
64b9b334 15660A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15661This command may be abbreviated \"disable\"."),
c906108c
SS
15662 &disablelist);
15663
1bedd215
AC
15664 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15665Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15666Arguments are breakpoint numbers with spaces in between.\n\
15667To delete all breakpoints, give no argument.\n\
15668\n\
15669Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15670The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15671 &deletelist, "delete ", 1, &cmdlist);
15672 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15673 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15674
1a966eab
AC
15675 add_cmd ("breakpoints", class_alias, delete_command, _("\
15676Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15677Arguments are breakpoint numbers with spaces in between.\n\
15678To delete all breakpoints, give no argument.\n\
1a966eab 15679This command may be abbreviated \"delete\"."),
c906108c
SS
15680 &deletelist);
15681
1bedd215 15682 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15683Clear breakpoint at specified location.\n\
15684Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15685\n\
15686With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15687is executing in.\n"
15688"\n" LOCATION_HELP_STRING "\n\
1bedd215 15689See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15690 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15691
1bedd215 15692 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15693Set breakpoint at specified location.\n"
31e2b00f 15694BREAK_ARGS_HELP ("break")));
5ba2abeb 15695 set_cmd_completer (c, location_completer);
c94fdfd0 15696
c906108c
SS
15697 add_com_alias ("b", "break", class_run, 1);
15698 add_com_alias ("br", "break", class_run, 1);
15699 add_com_alias ("bre", "break", class_run, 1);
15700 add_com_alias ("brea", "break", class_run, 1);
15701
c906108c
SS
15702 if (dbx_commands)
15703 {
1bedd215
AC
15704 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15705Break in function/address or break at a line in the current file."),
c5aa993b
JM
15706 &stoplist, "stop ", 1, &cmdlist);
15707 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15708 _("Break in function or address."), &stoplist);
c5aa993b 15709 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15710 _("Break at a line in the current file."), &stoplist);
11db9430 15711 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15712Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15713The \"Type\" column indicates one of:\n\
15714\tbreakpoint - normal breakpoint\n\
15715\twatchpoint - watchpoint\n\
15716The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15717the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15718breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15719address and file/line number respectively.\n\
15720\n\
15721Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15722are set to the address of the last breakpoint listed unless the command\n\
15723is prefixed with \"server \".\n\n\
c906108c 15724Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15725breakpoint set."));
c906108c
SS
15726 }
15727
11db9430 15728 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15729Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15730The \"Type\" column indicates one of:\n\
15731\tbreakpoint - normal breakpoint\n\
15732\twatchpoint - watchpoint\n\
15733The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15734the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15735breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15736address and file/line number respectively.\n\
15737\n\
15738Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15739are set to the address of the last breakpoint listed unless the command\n\
15740is prefixed with \"server \".\n\n\
c906108c 15741Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15742breakpoint set."));
c906108c 15743
6b04bdb7
MS
15744 add_info_alias ("b", "breakpoints", 1);
15745
1a966eab
AC
15746 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15747Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15748The \"Type\" column indicates one of:\n\
15749\tbreakpoint - normal breakpoint\n\
15750\twatchpoint - watchpoint\n\
15751\tlongjmp - internal breakpoint used to step through longjmp()\n\
15752\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15753\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15754\tfinish - internal breakpoint used by the \"finish\" command\n\
15755The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15756the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15757breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15758address and file/line number respectively.\n\
15759\n\
15760Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15761are set to the address of the last breakpoint listed unless the command\n\
15762is prefixed with \"server \".\n\n\
c906108c 15763Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15764breakpoint set."),
c906108c
SS
15765 &maintenanceinfolist);
15766
44feb3ce
TT
15767 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15768Set catchpoints to catch events."),
15769 &catch_cmdlist, "catch ",
15770 0/*allow-unknown*/, &cmdlist);
15771
15772 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15773Set temporary catchpoints to catch events."),
15774 &tcatch_cmdlist, "tcatch ",
15775 0/*allow-unknown*/, &cmdlist);
15776
44feb3ce
TT
15777 add_catch_command ("fork", _("Catch calls to fork."),
15778 catch_fork_command_1,
a96d9b2e 15779 NULL,
44feb3ce
TT
15780 (void *) (uintptr_t) catch_fork_permanent,
15781 (void *) (uintptr_t) catch_fork_temporary);
15782 add_catch_command ("vfork", _("Catch calls to vfork."),
15783 catch_fork_command_1,
a96d9b2e 15784 NULL,
44feb3ce
TT
15785 (void *) (uintptr_t) catch_vfork_permanent,
15786 (void *) (uintptr_t) catch_vfork_temporary);
15787 add_catch_command ("exec", _("Catch calls to exec."),
15788 catch_exec_command_1,
a96d9b2e
SDJ
15789 NULL,
15790 CATCH_PERMANENT,
15791 CATCH_TEMPORARY);
edcc5120
TT
15792 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15793Usage: catch load [REGEX]\n\
15794If REGEX is given, only stop for libraries matching the regular expression."),
15795 catch_load_command_1,
15796 NULL,
15797 CATCH_PERMANENT,
15798 CATCH_TEMPORARY);
15799 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15800Usage: catch unload [REGEX]\n\
15801If REGEX is given, only stop for libraries matching the regular expression."),
15802 catch_unload_command_1,
15803 NULL,
15804 CATCH_PERMANENT,
15805 CATCH_TEMPORARY);
c5aa993b 15806
1bedd215
AC
15807 c = add_com ("watch", class_breakpoint, watch_command, _("\
15808Set a watchpoint for an expression.\n\
06a64a0b 15809Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15810A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15811an expression changes.\n\
15812If -l or -location is given, this evaluates EXPRESSION and watches\n\
15813the memory to which it refers."));
65d12d83 15814 set_cmd_completer (c, expression_completer);
c906108c 15815
1bedd215
AC
15816 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15817Set a read watchpoint for an expression.\n\
06a64a0b 15818Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15819A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15820an expression is read.\n\
15821If -l or -location is given, this evaluates EXPRESSION and watches\n\
15822the memory to which it refers."));
65d12d83 15823 set_cmd_completer (c, expression_completer);
c906108c 15824
1bedd215
AC
15825 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15826Set a watchpoint for an expression.\n\
06a64a0b 15827Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15828A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15829an expression is either read or written.\n\
15830If -l or -location is given, this evaluates EXPRESSION and watches\n\
15831the memory to which it refers."));
65d12d83 15832 set_cmd_completer (c, expression_completer);
c906108c 15833
11db9430 15834 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15835Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15836
920d2a44
AC
15837 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15838 respond to changes - contrary to the description. */
85c07804
AC
15839 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15840 &can_use_hw_watchpoints, _("\
15841Set debugger's willingness to use watchpoint hardware."), _("\
15842Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15843If zero, gdb will not use hardware for new watchpoints, even if\n\
15844such is available. (However, any hardware watchpoints that were\n\
15845created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15846hardware.)"),
15847 NULL,
920d2a44 15848 show_can_use_hw_watchpoints,
85c07804 15849 &setlist, &showlist);
c906108c
SS
15850
15851 can_use_hw_watchpoints = 1;
fa8d40ab 15852
1042e4c0
SS
15853 /* Tracepoint manipulation commands. */
15854
15855 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15856Set a tracepoint at specified location.\n\
1042e4c0
SS
15857\n"
15858BREAK_ARGS_HELP ("trace") "\n\
15859Do \"help tracepoints\" for info on other tracepoint commands."));
15860 set_cmd_completer (c, location_completer);
15861
15862 add_com_alias ("tp", "trace", class_alias, 0);
15863 add_com_alias ("tr", "trace", class_alias, 1);
15864 add_com_alias ("tra", "trace", class_alias, 1);
15865 add_com_alias ("trac", "trace", class_alias, 1);
15866
7a697b8d 15867 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15868Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15869\n"
15870BREAK_ARGS_HELP ("ftrace") "\n\
15871Do \"help tracepoints\" for info on other tracepoint commands."));
15872 set_cmd_completer (c, location_completer);
15873
0fb4aa4b 15874 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15875Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15876\n\
15877strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15878LOCATION may be a linespec, explicit, or address location (described below) \n\
15879or -m MARKER_ID.\n\n\
15880If a marker id is specified, probe the marker with that name. With\n\
15881no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15882Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15883This collects arbitrary user data passed in the probe point call to the\n\
15884tracing library. You can inspect it when analyzing the trace buffer,\n\
15885by printing the $_sdata variable like any other convenience variable.\n\
15886\n\
15887CONDITION is a boolean expression.\n\
629500fa 15888\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15889Multiple tracepoints at one place are permitted, and useful if their\n\
15890conditions are different.\n\
0fb4aa4b
PA
15891\n\
15892Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15893Do \"help tracepoints\" for info on other tracepoint commands."));
15894 set_cmd_completer (c, location_completer);
15895
11db9430 15896 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15897Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15898Convenience variable \"$tpnum\" contains the number of the\n\
15899last tracepoint set."));
15900
15901 add_info_alias ("tp", "tracepoints", 1);
15902
15903 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15904Delete specified tracepoints.\n\
15905Arguments are tracepoint numbers, separated by spaces.\n\
15906No argument means delete all tracepoints."),
15907 &deletelist);
7e20dfcd 15908 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15909
15910 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15911Disable specified tracepoints.\n\
15912Arguments are tracepoint numbers, separated by spaces.\n\
15913No argument means disable all tracepoints."),
15914 &disablelist);
15915 deprecate_cmd (c, "disable");
15916
15917 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15918Enable specified tracepoints.\n\
15919Arguments are tracepoint numbers, separated by spaces.\n\
15920No argument means enable all tracepoints."),
15921 &enablelist);
15922 deprecate_cmd (c, "enable");
15923
15924 add_com ("passcount", class_trace, trace_pass_command, _("\
15925Set the passcount for a tracepoint.\n\
15926The trace will end when the tracepoint has been passed 'count' times.\n\
15927Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15928if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15929
6149aea9
PA
15930 add_prefix_cmd ("save", class_breakpoint, save_command,
15931 _("Save breakpoint definitions as a script."),
15932 &save_cmdlist, "save ",
15933 0/*allow-unknown*/, &cmdlist);
15934
15935 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15936Save current breakpoint definitions as a script.\n\
cce7e648 15937This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15938catchpoints, tracepoints). Use the 'source' command in another debug\n\
15939session to restore them."),
15940 &save_cmdlist);
15941 set_cmd_completer (c, filename_completer);
15942
15943 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15944Save current tracepoint definitions as a script.\n\
6149aea9
PA
15945Use the 'source' command in another debug session to restore them."),
15946 &save_cmdlist);
1042e4c0
SS
15947 set_cmd_completer (c, filename_completer);
15948
6149aea9
PA
15949 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15950 deprecate_cmd (c, "save tracepoints");
15951
1bedd215 15952 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15953Breakpoint specific settings\n\
15954Configure various breakpoint-specific variables such as\n\
1bedd215 15955pending breakpoint behavior"),
fa8d40ab
JJ
15956 &breakpoint_set_cmdlist, "set breakpoint ",
15957 0/*allow-unknown*/, &setlist);
1bedd215 15958 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15959Breakpoint specific settings\n\
15960Configure various breakpoint-specific variables such as\n\
1bedd215 15961pending breakpoint behavior"),
fa8d40ab
JJ
15962 &breakpoint_show_cmdlist, "show breakpoint ",
15963 0/*allow-unknown*/, &showlist);
15964
7915a72c
AC
15965 add_setshow_auto_boolean_cmd ("pending", no_class,
15966 &pending_break_support, _("\
15967Set debugger's behavior regarding pending breakpoints."), _("\
15968Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15969If on, an unrecognized breakpoint location will cause gdb to create a\n\
15970pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15971an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15972user-query to see if a pending breakpoint should be created."),
2c5b56ce 15973 NULL,
920d2a44 15974 show_pending_break_support,
6e1d7d6c
AC
15975 &breakpoint_set_cmdlist,
15976 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15977
15978 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15979
15980 add_setshow_boolean_cmd ("auto-hw", no_class,
15981 &automatic_hardware_breakpoints, _("\
15982Set automatic usage of hardware breakpoints."), _("\
15983Show automatic usage of hardware breakpoints."), _("\
15984If set, the debugger will automatically use hardware breakpoints for\n\
15985breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15986a warning will be emitted for such breakpoints."),
15987 NULL,
15988 show_automatic_hardware_breakpoints,
15989 &breakpoint_set_cmdlist,
15990 &breakpoint_show_cmdlist);
74960c60 15991
a25a5a45
PA
15992 add_setshow_boolean_cmd ("always-inserted", class_support,
15993 &always_inserted_mode, _("\
74960c60
VP
15994Set mode for inserting breakpoints."), _("\
15995Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15996When this mode is on, breakpoints are inserted immediately as soon as\n\
15997they're created, kept inserted even when execution stops, and removed\n\
15998only when the user deletes them. When this mode is off (the default),\n\
15999breakpoints are inserted only when execution continues, and removed\n\
16000when execution stops."),
72d0e2c5
YQ
16001 NULL,
16002 &show_always_inserted_mode,
16003 &breakpoint_set_cmdlist,
16004 &breakpoint_show_cmdlist);
f1310107 16005
b775012e
LM
16006 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16007 condition_evaluation_enums,
16008 &condition_evaluation_mode_1, _("\
16009Set mode of breakpoint condition evaluation."), _("\
16010Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16011When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16012evaluated on the host's side by GDB. When it is set to \"target\",\n\
16013breakpoint conditions will be downloaded to the target (if the target\n\
16014supports such feature) and conditions will be evaluated on the target's side.\n\
16015If this is set to \"auto\" (default), this will be automatically set to\n\
16016\"target\" if it supports condition evaluation, otherwise it will\n\
16017be set to \"gdb\""),
16018 &set_condition_evaluation_mode,
16019 &show_condition_evaluation_mode,
16020 &breakpoint_set_cmdlist,
16021 &breakpoint_show_cmdlist);
16022
f1310107
TJB
16023 add_com ("break-range", class_breakpoint, break_range_command, _("\
16024Set a breakpoint for an address range.\n\
16025break-range START-LOCATION, END-LOCATION\n\
16026where START-LOCATION and END-LOCATION can be one of the following:\n\
16027 LINENUM, for that line in the current file,\n\
16028 FILE:LINENUM, for that line in that file,\n\
16029 +OFFSET, for that number of lines after the current line\n\
16030 or the start of the range\n\
16031 FUNCTION, for the first line in that function,\n\
16032 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16033 *ADDRESS, for the instruction at that address.\n\
16034\n\
16035The breakpoint will stop execution of the inferior whenever it executes\n\
16036an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16037range (including START-LOCATION and END-LOCATION)."));
16038
e7e0cddf 16039 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16040Set a dynamic printf at specified location.\n\
e7e0cddf 16041dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16042location may be a linespec, explicit, or address location.\n"
16043"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16044 set_cmd_completer (c, location_completer);
16045
16046 add_setshow_enum_cmd ("dprintf-style", class_support,
16047 dprintf_style_enums, &dprintf_style, _("\
16048Set the style of usage for dynamic printf."), _("\
16049Show the style of usage for dynamic printf."), _("\
16050This setting chooses how GDB will do a dynamic printf.\n\
16051If the value is \"gdb\", then the printing is done by GDB to its own\n\
16052console, as with the \"printf\" command.\n\
16053If the value is \"call\", the print is done by calling a function in your\n\
16054program; by default printf(), but you can choose a different function or\n\
16055output stream by setting dprintf-function and dprintf-channel."),
16056 update_dprintf_commands, NULL,
16057 &setlist, &showlist);
16058
16059 dprintf_function = xstrdup ("printf");
16060 add_setshow_string_cmd ("dprintf-function", class_support,
16061 &dprintf_function, _("\
16062Set the function to use for dynamic printf"), _("\
16063Show the function to use for dynamic printf"), NULL,
16064 update_dprintf_commands, NULL,
16065 &setlist, &showlist);
16066
16067 dprintf_channel = xstrdup ("");
16068 add_setshow_string_cmd ("dprintf-channel", class_support,
16069 &dprintf_channel, _("\
16070Set the channel to use for dynamic printf"), _("\
16071Show the channel to use for dynamic printf"), NULL,
16072 update_dprintf_commands, NULL,
16073 &setlist, &showlist);
16074
d3ce09f5
SS
16075 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16076 &disconnected_dprintf, _("\
16077Set whether dprintf continues after GDB disconnects."), _("\
16078Show whether dprintf continues after GDB disconnects."), _("\
16079Use this to let dprintf commands continue to hit and produce output\n\
16080even if GDB disconnects or detaches from the target."),
16081 NULL,
16082 NULL,
16083 &setlist, &showlist);
16084
16085 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16086agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16087(target agent only) This is useful for formatted output in user-defined commands."));
16088
765dc015 16089 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16090
16091 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16092 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16093}
This page took 3.285738 seconds and 4 git commands to generate.