]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Use std::vector in h8300-tdep.c
[thirdparty/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
c80049d3 647scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 648{
86b17b60 649 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
650}
651
652/* Called at the end of an "rbreak" command to record the last
653 breakpoint made. */
86b17b60 654
c80049d3 655scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 656{
86b17b60 657 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
658}
659
4a64f543 660/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
661
662void
fba45db2 663clear_breakpoint_hit_counts (void)
c906108c
SS
664{
665 struct breakpoint *b;
666
667 ALL_BREAKPOINTS (b)
668 b->hit_count = 0;
669}
670
c906108c 671\f
48cb2d85
VP
672/* Return the breakpoint with the specified number, or NULL
673 if the number does not refer to an existing breakpoint. */
674
675struct breakpoint *
676get_breakpoint (int num)
677{
678 struct breakpoint *b;
679
680 ALL_BREAKPOINTS (b)
681 if (b->number == num)
682 return b;
683
684 return NULL;
685}
5c44784c 686
c906108c 687\f
adc36818 688
b775012e
LM
689/* Mark locations as "conditions have changed" in case the target supports
690 evaluating conditions on its side. */
691
692static void
693mark_breakpoint_modified (struct breakpoint *b)
694{
695 struct bp_location *loc;
696
697 /* This is only meaningful if the target is
698 evaluating conditions and if the user has
699 opted for condition evaluation on the target's
700 side. */
701 if (gdb_evaluates_breakpoint_condition_p ()
702 || !target_supports_evaluation_of_breakpoint_conditions ())
703 return;
704
705 if (!is_breakpoint (b))
706 return;
707
708 for (loc = b->loc; loc; loc = loc->next)
709 loc->condition_changed = condition_modified;
710}
711
712/* Mark location as "conditions have changed" in case the target supports
713 evaluating conditions on its side. */
714
715static void
716mark_breakpoint_location_modified (struct bp_location *loc)
717{
718 /* This is only meaningful if the target is
719 evaluating conditions and if the user has
720 opted for condition evaluation on the target's
721 side. */
722 if (gdb_evaluates_breakpoint_condition_p ()
723 || !target_supports_evaluation_of_breakpoint_conditions ())
724
725 return;
726
727 if (!is_breakpoint (loc->owner))
728 return;
729
730 loc->condition_changed = condition_modified;
731}
732
733/* Sets the condition-evaluation mode using the static global
734 condition_evaluation_mode. */
735
736static void
737set_condition_evaluation_mode (char *args, int from_tty,
738 struct cmd_list_element *c)
739{
b775012e
LM
740 const char *old_mode, *new_mode;
741
742 if ((condition_evaluation_mode_1 == condition_evaluation_target)
743 && !target_supports_evaluation_of_breakpoint_conditions ())
744 {
745 condition_evaluation_mode_1 = condition_evaluation_mode;
746 warning (_("Target does not support breakpoint condition evaluation.\n"
747 "Using host evaluation mode instead."));
748 return;
749 }
750
751 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
752 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
753
abf1152a
JK
754 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
755 settings was "auto". */
756 condition_evaluation_mode = condition_evaluation_mode_1;
757
b775012e
LM
758 /* Only update the mode if the user picked a different one. */
759 if (new_mode != old_mode)
760 {
761 struct bp_location *loc, **loc_tmp;
762 /* If the user switched to a different evaluation mode, we
763 need to synch the changes with the target as follows:
764
765 "host" -> "target": Send all (valid) conditions to the target.
766 "target" -> "host": Remove all the conditions from the target.
767 */
768
b775012e
LM
769 if (new_mode == condition_evaluation_target)
770 {
771 /* Mark everything modified and synch conditions with the
772 target. */
773 ALL_BP_LOCATIONS (loc, loc_tmp)
774 mark_breakpoint_location_modified (loc);
775 }
776 else
777 {
778 /* Manually mark non-duplicate locations to synch conditions
779 with the target. We do this to remove all the conditions the
780 target knows about. */
781 ALL_BP_LOCATIONS (loc, loc_tmp)
782 if (is_breakpoint (loc->owner) && loc->inserted)
783 loc->needs_update = 1;
784 }
785
786 /* Do the update. */
44702360 787 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
788 }
789
790 return;
791}
792
793/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
794 what "auto" is translating to. */
795
796static void
797show_condition_evaluation_mode (struct ui_file *file, int from_tty,
798 struct cmd_list_element *c, const char *value)
799{
800 if (condition_evaluation_mode == condition_evaluation_auto)
801 fprintf_filtered (file,
802 _("Breakpoint condition evaluation "
803 "mode is %s (currently %s).\n"),
804 value,
805 breakpoint_condition_evaluation_mode ());
806 else
807 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
808 value);
809}
810
811/* A comparison function for bp_location AP and BP that is used by
812 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 813 the more general bp_locations_compare function. */
b775012e
LM
814
815static int
f5336ca5 816bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 817{
9a3c8263
SM
818 const struct bp_location *a = *(const struct bp_location **) ap;
819 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
820
821 if (a->address == b->address)
822 return 0;
823 else
824 return ((a->address > b->address) - (a->address < b->address));
825}
826
827/* Helper function to skip all bp_locations with addresses
828 less than ADDRESS. It returns the first bp_location that
829 is greater than or equal to ADDRESS. If none is found, just
830 return NULL. */
831
832static struct bp_location **
833get_first_locp_gte_addr (CORE_ADDR address)
834{
835 struct bp_location dummy_loc;
836 struct bp_location *dummy_locp = &dummy_loc;
837 struct bp_location **locp_found = NULL;
838
839 /* Initialize the dummy location's address field. */
b775012e
LM
840 dummy_loc.address = address;
841
842 /* Find a close match to the first location at ADDRESS. */
9a3c8263 843 locp_found = ((struct bp_location **)
f5336ca5 844 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 845 sizeof (struct bp_location **),
f5336ca5 846 bp_locations_compare_addrs));
b775012e
LM
847
848 /* Nothing was found, nothing left to do. */
849 if (locp_found == NULL)
850 return NULL;
851
852 /* We may have found a location that is at ADDRESS but is not the first in the
853 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 854 while ((locp_found - 1) >= bp_locations
b775012e
LM
855 && (*(locp_found - 1))->address == address)
856 locp_found--;
857
858 return locp_found;
859}
860
adc36818 861void
7a26bd4d 862set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
863 int from_tty)
864{
3a5c3e22
PA
865 xfree (b->cond_string);
866 b->cond_string = NULL;
adc36818 867
3a5c3e22 868 if (is_watchpoint (b))
adc36818 869 {
3a5c3e22
PA
870 struct watchpoint *w = (struct watchpoint *) b;
871
4d01a485 872 w->cond_exp.reset ();
3a5c3e22
PA
873 }
874 else
875 {
876 struct bp_location *loc;
877
878 for (loc = b->loc; loc; loc = loc->next)
879 {
4d01a485 880 loc->cond.reset ();
b775012e
LM
881
882 /* No need to free the condition agent expression
883 bytecode (if we have one). We will handle this
884 when we go through update_global_location_list. */
3a5c3e22 885 }
adc36818 886 }
adc36818
PM
887
888 if (*exp == 0)
889 {
890 if (from_tty)
891 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
892 }
893 else
894 {
bbc13ae3 895 const char *arg = exp;
cc59ec59 896
adc36818
PM
897 /* I don't know if it matters whether this is the string the user
898 typed in or the decompiled expression. */
899 b->cond_string = xstrdup (arg);
900 b->condition_not_parsed = 0;
901
902 if (is_watchpoint (b))
903 {
3a5c3e22
PA
904 struct watchpoint *w = (struct watchpoint *) b;
905
adc36818
PM
906 innermost_block = NULL;
907 arg = exp;
1bb9788d 908 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
909 if (*arg)
910 error (_("Junk at end of expression"));
3a5c3e22 911 w->cond_exp_valid_block = innermost_block;
adc36818
PM
912 }
913 else
914 {
3a5c3e22
PA
915 struct bp_location *loc;
916
adc36818
PM
917 for (loc = b->loc; loc; loc = loc->next)
918 {
919 arg = exp;
920 loc->cond =
1bb9788d
TT
921 parse_exp_1 (&arg, loc->address,
922 block_for_pc (loc->address), 0);
adc36818
PM
923 if (*arg)
924 error (_("Junk at end of expression"));
925 }
926 }
927 }
b775012e
LM
928 mark_breakpoint_modified (b);
929
8d3788bd 930 observer_notify_breakpoint_modified (b);
adc36818
PM
931}
932
d55637df
TT
933/* Completion for the "condition" command. */
934
eb3ff9a5 935static void
6f937416 936condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 937 completion_tracker &tracker,
6f937416 938 const char *text, const char *word)
d55637df 939{
6f937416 940 const char *space;
d55637df 941
f1735a53
TT
942 text = skip_spaces (text);
943 space = skip_to_space (text);
d55637df
TT
944 if (*space == '\0')
945 {
946 int len;
947 struct breakpoint *b;
948 VEC (char_ptr) *result = NULL;
949
950 if (text[0] == '$')
951 {
952 /* We don't support completion of history indices. */
eb3ff9a5
PA
953 if (!isdigit (text[1]))
954 complete_internalvar (tracker, &text[1]);
955 return;
d55637df
TT
956 }
957
958 /* We're completing the breakpoint number. */
959 len = strlen (text);
960
961 ALL_BREAKPOINTS (b)
58ce7251
SDJ
962 {
963 char number[50];
964
965 xsnprintf (number, sizeof (number), "%d", b->number);
966
967 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
968 {
969 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
970 tracker.add_completion (std::move (copy));
971 }
58ce7251 972 }
d55637df 973
eb3ff9a5 974 return;
d55637df
TT
975 }
976
977 /* We're completing the expression part. */
f1735a53 978 text = skip_spaces (space);
eb3ff9a5 979 expression_completer (cmd, tracker, text, word);
d55637df
TT
980}
981
c906108c
SS
982/* condition N EXP -- set break condition of breakpoint N to EXP. */
983
984static void
fba45db2 985condition_command (char *arg, int from_tty)
c906108c 986{
52f0bd74 987 struct breakpoint *b;
c906108c 988 char *p;
52f0bd74 989 int bnum;
c906108c
SS
990
991 if (arg == 0)
e2e0b3e5 992 error_no_arg (_("breakpoint number"));
c906108c
SS
993
994 p = arg;
995 bnum = get_number (&p);
5c44784c 996 if (bnum == 0)
8a3fe4f8 997 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
998
999 ALL_BREAKPOINTS (b)
1000 if (b->number == bnum)
2f069f6f 1001 {
6dddc817
DE
1002 /* Check if this breakpoint has a "stop" method implemented in an
1003 extension language. This method and conditions entered into GDB
1004 from the CLI are mutually exclusive. */
1005 const struct extension_language_defn *extlang
1006 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1007
1008 if (extlang != NULL)
1009 {
1010 error (_("Only one stop condition allowed. There is currently"
1011 " a %s stop condition defined for this breakpoint."),
1012 ext_lang_capitalized_name (extlang));
1013 }
2566ad2d 1014 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1015
1016 if (is_breakpoint (b))
44702360 1017 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1018
2f069f6f
JB
1019 return;
1020 }
c906108c 1021
8a3fe4f8 1022 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1023}
1024
a7bdde9e
VP
1025/* Check that COMMAND do not contain commands that are suitable
1026 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1027 Throw if any such commands is found. */
1028
a7bdde9e
VP
1029static void
1030check_no_tracepoint_commands (struct command_line *commands)
1031{
1032 struct command_line *c;
cc59ec59 1033
a7bdde9e
VP
1034 for (c = commands; c; c = c->next)
1035 {
1036 int i;
1037
1038 if (c->control_type == while_stepping_control)
3e43a32a
MS
1039 error (_("The 'while-stepping' command can "
1040 "only be used for tracepoints"));
a7bdde9e
VP
1041
1042 for (i = 0; i < c->body_count; ++i)
1043 check_no_tracepoint_commands ((c->body_list)[i]);
1044
1045 /* Not that command parsing removes leading whitespace and comment
4a64f543 1046 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1047 command directly. */
1048 if (strstr (c->line, "collect ") == c->line)
1049 error (_("The 'collect' command can only be used for tracepoints"));
1050
51661e93
VP
1051 if (strstr (c->line, "teval ") == c->line)
1052 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1053 }
1054}
1055
c1fc2657 1056struct longjmp_breakpoint : public breakpoint
3b0871f4 1057{
c1fc2657 1058 ~longjmp_breakpoint () override;
3b0871f4
SM
1059};
1060
d77f58be
SS
1061/* Encapsulate tests for different types of tracepoints. */
1062
3b0871f4
SM
1063static bool
1064is_tracepoint_type (bptype type)
d9b3f62e
PA
1065{
1066 return (type == bp_tracepoint
1067 || type == bp_fast_tracepoint
1068 || type == bp_static_tracepoint);
1069}
1070
3b0871f4
SM
1071static bool
1072is_longjmp_type (bptype type)
1073{
1074 return type == bp_longjmp || type == bp_exception;
1075}
1076
a7bdde9e 1077int
d77f58be 1078is_tracepoint (const struct breakpoint *b)
a7bdde9e 1079{
d9b3f62e 1080 return is_tracepoint_type (b->type);
a7bdde9e 1081}
d9b3f62e 1082
a5e364af
SM
1083/* Factory function to create an appropriate instance of breakpoint given
1084 TYPE. */
1085
1086static std::unique_ptr<breakpoint>
1087new_breakpoint_from_type (bptype type)
1088{
1089 breakpoint *b;
1090
1091 if (is_tracepoint_type (type))
c1fc2657 1092 b = new tracepoint ();
3b0871f4 1093 else if (is_longjmp_type (type))
c1fc2657 1094 b = new longjmp_breakpoint ();
a5e364af
SM
1095 else
1096 b = new breakpoint ();
1097
1098 return std::unique_ptr<breakpoint> (b);
1099}
1100
e5dd4106 1101/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1102 breakpoint. This function will throw an exception if a problem is
1103 found. */
48cb2d85 1104
95a42b64
TT
1105static void
1106validate_commands_for_breakpoint (struct breakpoint *b,
1107 struct command_line *commands)
48cb2d85 1108{
d77f58be 1109 if (is_tracepoint (b))
a7bdde9e 1110 {
c9a6ce02 1111 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1112 struct command_line *c;
1113 struct command_line *while_stepping = 0;
c9a6ce02
PA
1114
1115 /* Reset the while-stepping step count. The previous commands
1116 might have included a while-stepping action, while the new
1117 ones might not. */
1118 t->step_count = 0;
1119
1120 /* We need to verify that each top-level element of commands is
1121 valid for tracepoints, that there's at most one
1122 while-stepping element, and that the while-stepping's body
1123 has valid tracing commands excluding nested while-stepping.
1124 We also need to validate the tracepoint action line in the
1125 context of the tracepoint --- validate_actionline actually
1126 has side effects, like setting the tracepoint's
1127 while-stepping STEP_COUNT, in addition to checking if the
1128 collect/teval actions parse and make sense in the
1129 tracepoint's context. */
a7bdde9e
VP
1130 for (c = commands; c; c = c->next)
1131 {
a7bdde9e
VP
1132 if (c->control_type == while_stepping_control)
1133 {
1134 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1135 error (_("The 'while-stepping' command "
1136 "cannot be used for fast tracepoint"));
0fb4aa4b 1137 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1138 error (_("The 'while-stepping' command "
1139 "cannot be used for static tracepoint"));
a7bdde9e
VP
1140
1141 if (while_stepping)
3e43a32a
MS
1142 error (_("The 'while-stepping' command "
1143 "can be used only once"));
a7bdde9e
VP
1144 else
1145 while_stepping = c;
1146 }
c9a6ce02
PA
1147
1148 validate_actionline (c->line, b);
a7bdde9e
VP
1149 }
1150 if (while_stepping)
1151 {
1152 struct command_line *c2;
1153
1154 gdb_assert (while_stepping->body_count == 1);
1155 c2 = while_stepping->body_list[0];
1156 for (; c2; c2 = c2->next)
1157 {
a7bdde9e
VP
1158 if (c2->control_type == while_stepping_control)
1159 error (_("The 'while-stepping' command cannot be nested"));
1160 }
1161 }
1162 }
1163 else
1164 {
1165 check_no_tracepoint_commands (commands);
1166 }
95a42b64
TT
1167}
1168
0fb4aa4b
PA
1169/* Return a vector of all the static tracepoints set at ADDR. The
1170 caller is responsible for releasing the vector. */
1171
1172VEC(breakpoint_p) *
1173static_tracepoints_here (CORE_ADDR addr)
1174{
1175 struct breakpoint *b;
1176 VEC(breakpoint_p) *found = 0;
1177 struct bp_location *loc;
1178
1179 ALL_BREAKPOINTS (b)
1180 if (b->type == bp_static_tracepoint)
1181 {
1182 for (loc = b->loc; loc; loc = loc->next)
1183 if (loc->address == addr)
1184 VEC_safe_push(breakpoint_p, found, b);
1185 }
1186
1187 return found;
1188}
1189
95a42b64 1190/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1191 validate that only allowed commands are included. */
95a42b64
TT
1192
1193void
4a64f543 1194breakpoint_set_commands (struct breakpoint *b,
93921405 1195 command_line_up &&commands)
95a42b64 1196{
93921405 1197 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1198
d1b0a7bf 1199 b->commands = std::move (commands);
8d3788bd 1200 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1201}
1202
45a43567
TT
1203/* Set the internal `silent' flag on the breakpoint. Note that this
1204 is not the same as the "silent" that may appear in the breakpoint's
1205 commands. */
1206
1207void
1208breakpoint_set_silent (struct breakpoint *b, int silent)
1209{
1210 int old_silent = b->silent;
1211
1212 b->silent = silent;
1213 if (old_silent != silent)
8d3788bd 1214 observer_notify_breakpoint_modified (b);
45a43567
TT
1215}
1216
1217/* Set the thread for this breakpoint. If THREAD is -1, make the
1218 breakpoint work for any thread. */
1219
1220void
1221breakpoint_set_thread (struct breakpoint *b, int thread)
1222{
1223 int old_thread = b->thread;
1224
1225 b->thread = thread;
1226 if (old_thread != thread)
8d3788bd 1227 observer_notify_breakpoint_modified (b);
45a43567
TT
1228}
1229
1230/* Set the task for this breakpoint. If TASK is 0, make the
1231 breakpoint work for any task. */
1232
1233void
1234breakpoint_set_task (struct breakpoint *b, int task)
1235{
1236 int old_task = b->task;
1237
1238 b->task = task;
1239 if (old_task != task)
8d3788bd 1240 observer_notify_breakpoint_modified (b);
45a43567
TT
1241}
1242
95a42b64
TT
1243void
1244check_tracepoint_command (char *line, void *closure)
a7bdde9e 1245{
9a3c8263 1246 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1247
6f937416 1248 validate_actionline (line, b);
a7bdde9e
VP
1249}
1250
95a42b64 1251static void
896b6bda 1252commands_command_1 (const char *arg, int from_tty,
4a64f543 1253 struct command_line *control)
95a42b64 1254{
d1b0a7bf 1255 counted_command_line cmd;
95a42b64 1256
896b6bda
PA
1257 std::string new_arg;
1258
95a42b64
TT
1259 if (arg == NULL || !*arg)
1260 {
86b17b60 1261 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1262 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1263 breakpoint_count);
95a42b64 1264 else if (breakpoint_count > 0)
896b6bda 1265 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1266 arg = new_arg.c_str ();
1267 }
1268
1269 map_breakpoint_numbers
1270 (arg, [&] (breakpoint *b)
1271 {
1272 if (cmd == NULL)
1273 {
48649e1b 1274 if (control != NULL)
d1b0a7bf 1275 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1276 else
1277 {
81b1e71c
TT
1278 std::string str
1279 = string_printf (_("Type commands for breakpoint(s) "
1280 "%s, one per line."),
1281 arg);
48649e1b 1282
81b1e71c 1283 cmd = read_command_lines (&str[0],
d1b0a7bf
TT
1284 from_tty, 1,
1285 (is_tracepoint (b)
1286 ? check_tracepoint_command : 0),
1287 b);
48649e1b 1288 }
48649e1b
TT
1289 }
1290
1291 /* If a breakpoint was on the list more than once, we don't need to
1292 do anything. */
1293 if (b->commands != cmd)
1294 {
d1b0a7bf 1295 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1296 b->commands = cmd;
1297 observer_notify_breakpoint_modified (b);
1298 }
1299 });
95a42b64 1300
48649e1b 1301 if (cmd == NULL)
95a42b64 1302 error (_("No breakpoints specified."));
95a42b64
TT
1303}
1304
1305static void
1306commands_command (char *arg, int from_tty)
1307{
1308 commands_command_1 (arg, from_tty, NULL);
c906108c 1309}
40c03ae8
EZ
1310
1311/* Like commands_command, but instead of reading the commands from
1312 input stream, takes them from an already parsed command structure.
1313
1314 This is used by cli-script.c to DTRT with breakpoint commands
1315 that are part of if and while bodies. */
1316enum command_control_type
896b6bda 1317commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1318{
95a42b64
TT
1319 commands_command_1 (arg, 0, cmd);
1320 return simple_control;
40c03ae8 1321}
876fa593
JK
1322
1323/* Return non-zero if BL->TARGET_INFO contains valid information. */
1324
1325static int
1326bp_location_has_shadow (struct bp_location *bl)
1327{
1328 if (bl->loc_type != bp_loc_software_breakpoint)
1329 return 0;
1330 if (!bl->inserted)
1331 return 0;
1332 if (bl->target_info.shadow_len == 0)
e5dd4106 1333 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1334 return 0;
1335 return 1;
1336}
1337
9d497a19
PA
1338/* Update BUF, which is LEN bytes read from the target address
1339 MEMADDR, by replacing a memory breakpoint with its shadowed
1340 contents.
1341
1342 If READBUF is not NULL, this buffer must not overlap with the of
1343 the breakpoint location's shadow_contents buffer. Otherwise, a
1344 failed assertion internal error will be raised. */
1345
1346static void
1347one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1348 const gdb_byte *writebuf_org,
1349 ULONGEST memaddr, LONGEST len,
1350 struct bp_target_info *target_info,
1351 struct gdbarch *gdbarch)
1352{
1353 /* Now do full processing of the found relevant range of elements. */
1354 CORE_ADDR bp_addr = 0;
1355 int bp_size = 0;
1356 int bptoffset = 0;
1357
1358 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1359 current_program_space->aspace, 0))
1360 {
1361 /* The breakpoint is inserted in a different address space. */
1362 return;
1363 }
1364
1365 /* Addresses and length of the part of the breakpoint that
1366 we need to copy. */
1367 bp_addr = target_info->placed_address;
1368 bp_size = target_info->shadow_len;
1369
1370 if (bp_addr + bp_size <= memaddr)
1371 {
1372 /* The breakpoint is entirely before the chunk of memory we are
1373 reading. */
1374 return;
1375 }
1376
1377 if (bp_addr >= memaddr + len)
1378 {
1379 /* The breakpoint is entirely after the chunk of memory we are
1380 reading. */
1381 return;
1382 }
1383
1384 /* Offset within shadow_contents. */
1385 if (bp_addr < memaddr)
1386 {
1387 /* Only copy the second part of the breakpoint. */
1388 bp_size -= memaddr - bp_addr;
1389 bptoffset = memaddr - bp_addr;
1390 bp_addr = memaddr;
1391 }
1392
1393 if (bp_addr + bp_size > memaddr + len)
1394 {
1395 /* Only copy the first part of the breakpoint. */
1396 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1397 }
1398
1399 if (readbuf != NULL)
1400 {
1401 /* Verify that the readbuf buffer does not overlap with the
1402 shadow_contents buffer. */
1403 gdb_assert (target_info->shadow_contents >= readbuf + len
1404 || readbuf >= (target_info->shadow_contents
1405 + target_info->shadow_len));
1406
1407 /* Update the read buffer with this inserted breakpoint's
1408 shadow. */
1409 memcpy (readbuf + bp_addr - memaddr,
1410 target_info->shadow_contents + bptoffset, bp_size);
1411 }
1412 else
1413 {
1414 const unsigned char *bp;
0d5ed153
MR
1415 CORE_ADDR addr = target_info->reqstd_address;
1416 int placed_size;
9d497a19
PA
1417
1418 /* Update the shadow with what we want to write to memory. */
1419 memcpy (target_info->shadow_contents + bptoffset,
1420 writebuf_org + bp_addr - memaddr, bp_size);
1421
1422 /* Determine appropriate breakpoint contents and size for this
1423 address. */
0d5ed153 1424 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1425
1426 /* Update the final write buffer with this inserted
1427 breakpoint's INSN. */
1428 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1429 }
1430}
1431
8defab1a 1432/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1433 by replacing any memory breakpoints with their shadowed contents.
1434
35c63cd8
JB
1435 If READBUF is not NULL, this buffer must not overlap with any of
1436 the breakpoint location's shadow_contents buffers. Otherwise,
1437 a failed assertion internal error will be raised.
1438
876fa593 1439 The range of shadowed area by each bp_location is:
f5336ca5
PA
1440 bl->address - bp_locations_placed_address_before_address_max
1441 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1442 The range we were requested to resolve shadows for is:
1443 memaddr ... memaddr + len
1444 Thus the safe cutoff boundaries for performance optimization are
35df4500 1445 memaddr + len <= (bl->address
f5336ca5 1446 - bp_locations_placed_address_before_address_max)
876fa593 1447 and:
f5336ca5 1448 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1449
8defab1a 1450void
f0ba3972
PA
1451breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1452 const gdb_byte *writebuf_org,
1453 ULONGEST memaddr, LONGEST len)
c906108c 1454{
4a64f543
MS
1455 /* Left boundary, right boundary and median element of our binary
1456 search. */
876fa593
JK
1457 unsigned bc_l, bc_r, bc;
1458
4a64f543
MS
1459 /* Find BC_L which is a leftmost element which may affect BUF
1460 content. It is safe to report lower value but a failure to
1461 report higher one. */
876fa593
JK
1462
1463 bc_l = 0;
f5336ca5 1464 bc_r = bp_locations_count;
876fa593
JK
1465 while (bc_l + 1 < bc_r)
1466 {
35df4500 1467 struct bp_location *bl;
876fa593
JK
1468
1469 bc = (bc_l + bc_r) / 2;
f5336ca5 1470 bl = bp_locations[bc];
876fa593 1471
4a64f543
MS
1472 /* Check first BL->ADDRESS will not overflow due to the added
1473 constant. Then advance the left boundary only if we are sure
1474 the BC element can in no way affect the BUF content (MEMADDR
1475 to MEMADDR + LEN range).
876fa593 1476
f5336ca5 1477 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1478 offset so that we cannot miss a breakpoint with its shadow
1479 range tail still reaching MEMADDR. */
c5aa993b 1480
f5336ca5 1481 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1482 >= bl->address)
f5336ca5 1483 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1484 <= memaddr))
876fa593
JK
1485 bc_l = bc;
1486 else
1487 bc_r = bc;
1488 }
1489
128070bb
PA
1490 /* Due to the binary search above, we need to make sure we pick the
1491 first location that's at BC_L's address. E.g., if there are
1492 multiple locations at the same address, BC_L may end up pointing
1493 at a duplicate location, and miss the "master"/"inserted"
1494 location. Say, given locations L1, L2 and L3 at addresses A and
1495 B:
1496
1497 L1@A, L2@A, L3@B, ...
1498
1499 BC_L could end up pointing at location L2, while the "master"
1500 location could be L1. Since the `loc->inserted' flag is only set
1501 on "master" locations, we'd forget to restore the shadow of L1
1502 and L2. */
1503 while (bc_l > 0
f5336ca5 1504 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1505 bc_l--;
1506
876fa593
JK
1507 /* Now do full processing of the found relevant range of elements. */
1508
f5336ca5 1509 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1510 {
f5336ca5 1511 struct bp_location *bl = bp_locations[bc];
876fa593 1512
35df4500
TJB
1513 /* bp_location array has BL->OWNER always non-NULL. */
1514 if (bl->owner->type == bp_none)
8a3fe4f8 1515 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1516 bl->owner->number);
ffce0d52 1517
e5dd4106 1518 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1519 content. */
1520
f5336ca5
PA
1521 if (bl->address >= bp_locations_placed_address_before_address_max
1522 && memaddr + len <= (bl->address
1523 - bp_locations_placed_address_before_address_max))
876fa593
JK
1524 break;
1525
35df4500 1526 if (!bp_location_has_shadow (bl))
c5aa993b 1527 continue;
6c95b8df 1528
9d497a19
PA
1529 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1530 memaddr, len, &bl->target_info, bl->gdbarch);
1531 }
c906108c 1532}
9d497a19 1533
c906108c 1534\f
c5aa993b 1535
b775012e
LM
1536/* Return true if BPT is either a software breakpoint or a hardware
1537 breakpoint. */
1538
1539int
1540is_breakpoint (const struct breakpoint *bpt)
1541{
1542 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1543 || bpt->type == bp_hardware_breakpoint
1544 || bpt->type == bp_dprintf);
b775012e
LM
1545}
1546
60e1c644
PA
1547/* Return true if BPT is of any hardware watchpoint kind. */
1548
a5606eee 1549static int
d77f58be 1550is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1551{
1552 return (bpt->type == bp_hardware_watchpoint
1553 || bpt->type == bp_read_watchpoint
1554 || bpt->type == bp_access_watchpoint);
1555}
7270d8f2 1556
60e1c644
PA
1557/* Return true if BPT is of any watchpoint kind, hardware or
1558 software. */
1559
3a5c3e22 1560int
d77f58be 1561is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1562{
1563 return (is_hardware_watchpoint (bpt)
1564 || bpt->type == bp_watchpoint);
1565}
1566
3a5c3e22
PA
1567/* Returns true if the current thread and its running state are safe
1568 to evaluate or update watchpoint B. Watchpoints on local
1569 expressions need to be evaluated in the context of the thread that
1570 was current when the watchpoint was created, and, that thread needs
1571 to be stopped to be able to select the correct frame context.
1572 Watchpoints on global expressions can be evaluated on any thread,
1573 and in any state. It is presently left to the target allowing
1574 memory accesses when threads are running. */
f6bc2008
PA
1575
1576static int
3a5c3e22 1577watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1578{
c1fc2657 1579 return (b->pspace == current_program_space
d0d8b0c6
JK
1580 && (ptid_equal (b->watchpoint_thread, null_ptid)
1581 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1582 && !is_executing (inferior_ptid))));
f6bc2008
PA
1583}
1584
d0fb5eae
JK
1585/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1586 associated bp_watchpoint_scope breakpoint. */
1587
1588static void
3a5c3e22 1589watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1590{
c1fc2657 1591 if (w->related_breakpoint != w)
d0fb5eae 1592 {
c1fc2657
SM
1593 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1594 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1595 w->related_breakpoint->disposition = disp_del_at_next_stop;
1596 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1597 w->related_breakpoint = w;
d0fb5eae 1598 }
c1fc2657 1599 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1600}
1601
bb9d5f81
PP
1602/* Extract a bitfield value from value VAL using the bit parameters contained in
1603 watchpoint W. */
1604
1605static struct value *
1606extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1607{
1608 struct value *bit_val;
1609
1610 if (val == NULL)
1611 return NULL;
1612
1613 bit_val = allocate_value (value_type (val));
1614
1615 unpack_value_bitfield (bit_val,
1616 w->val_bitpos,
1617 w->val_bitsize,
1618 value_contents_for_printing (val),
1619 value_offset (val),
1620 val);
1621
1622 return bit_val;
1623}
1624
c6d81124
PA
1625/* Allocate a dummy location and add it to B, which must be a software
1626 watchpoint. This is required because even if a software watchpoint
1627 is not watching any memory, bpstat_stop_status requires a location
1628 to be able to report stops. */
1629
1630static void
1631software_watchpoint_add_no_memory_location (struct breakpoint *b,
1632 struct program_space *pspace)
1633{
1634 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1635
1636 b->loc = allocate_bp_location (b);
1637 b->loc->pspace = pspace;
1638 b->loc->address = -1;
1639 b->loc->length = -1;
1640}
1641
1642/* Returns true if B is a software watchpoint that is not watching any
1643 memory (e.g., "watch $pc"). */
1644
1645static int
1646is_no_memory_software_watchpoint (struct breakpoint *b)
1647{
1648 return (b->type == bp_watchpoint
1649 && b->loc != NULL
1650 && b->loc->next == NULL
1651 && b->loc->address == -1
1652 && b->loc->length == -1);
1653}
1654
567e1b4e
JB
1655/* Assuming that B is a watchpoint:
1656 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1657 - Evaluate expression and store the result in B->val
567e1b4e
JB
1658 - Evaluate the condition if there is one, and store the result
1659 in b->loc->cond.
a5606eee
VP
1660 - Update the list of values that must be watched in B->loc.
1661
4a64f543
MS
1662 If the watchpoint disposition is disp_del_at_next_stop, then do
1663 nothing. If this is local watchpoint that is out of scope, delete
1664 it.
1665
1666 Even with `set breakpoint always-inserted on' the watchpoints are
1667 removed + inserted on each stop here. Normal breakpoints must
1668 never be removed because they might be missed by a running thread
1669 when debugging in non-stop mode. On the other hand, hardware
1670 watchpoints (is_hardware_watchpoint; processed here) are specific
1671 to each LWP since they are stored in each LWP's hardware debug
1672 registers. Therefore, such LWP must be stopped first in order to
1673 be able to modify its hardware watchpoints.
1674
1675 Hardware watchpoints must be reset exactly once after being
1676 presented to the user. It cannot be done sooner, because it would
1677 reset the data used to present the watchpoint hit to the user. And
1678 it must not be done later because it could display the same single
1679 watchpoint hit during multiple GDB stops. Note that the latter is
1680 relevant only to the hardware watchpoint types bp_read_watchpoint
1681 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1682 not user-visible - its hit is suppressed if the memory content has
1683 not changed.
1684
1685 The following constraints influence the location where we can reset
1686 hardware watchpoints:
1687
1688 * target_stopped_by_watchpoint and target_stopped_data_address are
1689 called several times when GDB stops.
1690
1691 [linux]
1692 * Multiple hardware watchpoints can be hit at the same time,
1693 causing GDB to stop. GDB only presents one hardware watchpoint
1694 hit at a time as the reason for stopping, and all the other hits
1695 are presented later, one after the other, each time the user
1696 requests the execution to be resumed. Execution is not resumed
1697 for the threads still having pending hit event stored in
1698 LWP_INFO->STATUS. While the watchpoint is already removed from
1699 the inferior on the first stop the thread hit event is kept being
1700 reported from its cached value by linux_nat_stopped_data_address
1701 until the real thread resume happens after the watchpoint gets
1702 presented and thus its LWP_INFO->STATUS gets reset.
1703
1704 Therefore the hardware watchpoint hit can get safely reset on the
1705 watchpoint removal from inferior. */
a79d3c27 1706
b40ce68a 1707static void
3a5c3e22 1708update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1709{
a5606eee 1710 int within_current_scope;
a5606eee 1711 struct frame_id saved_frame_id;
66076460 1712 int frame_saved;
a5606eee 1713
f6bc2008
PA
1714 /* If this is a local watchpoint, we only want to check if the
1715 watchpoint frame is in scope if the current thread is the thread
1716 that was used to create the watchpoint. */
1717 if (!watchpoint_in_thread_scope (b))
1718 return;
1719
c1fc2657 1720 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1721 return;
1722
66076460 1723 frame_saved = 0;
a5606eee
VP
1724
1725 /* Determine if the watchpoint is within scope. */
1726 if (b->exp_valid_block == NULL)
1727 within_current_scope = 1;
1728 else
1729 {
b5db5dfc
UW
1730 struct frame_info *fi = get_current_frame ();
1731 struct gdbarch *frame_arch = get_frame_arch (fi);
1732 CORE_ADDR frame_pc = get_frame_pc (fi);
1733
c9cf6e20
MG
1734 /* If we're at a point where the stack has been destroyed
1735 (e.g. in a function epilogue), unwinding may not work
1736 properly. Do not attempt to recreate locations at this
b5db5dfc 1737 point. See similar comments in watchpoint_check. */
c9cf6e20 1738 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1739 return;
66076460
DJ
1740
1741 /* Save the current frame's ID so we can restore it after
1742 evaluating the watchpoint expression on its own frame. */
1743 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1744 took a frame parameter, so that we didn't have to change the
1745 selected frame. */
1746 frame_saved = 1;
1747 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1748
a5606eee
VP
1749 fi = frame_find_by_id (b->watchpoint_frame);
1750 within_current_scope = (fi != NULL);
1751 if (within_current_scope)
1752 select_frame (fi);
1753 }
1754
b5db5dfc
UW
1755 /* We don't free locations. They are stored in the bp_location array
1756 and update_global_location_list will eventually delete them and
1757 remove breakpoints if needed. */
c1fc2657 1758 b->loc = NULL;
b5db5dfc 1759
a5606eee
VP
1760 if (within_current_scope && reparse)
1761 {
bbc13ae3 1762 const char *s;
d63d0675 1763
4d01a485 1764 b->exp.reset ();
d63d0675 1765 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1766 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1767 /* If the meaning of expression itself changed, the old value is
1768 no longer relevant. We don't want to report a watchpoint hit
1769 to the user when the old value and the new value may actually
1770 be completely different objects. */
1771 value_free (b->val);
fa4727a6
DJ
1772 b->val = NULL;
1773 b->val_valid = 0;
60e1c644
PA
1774
1775 /* Note that unlike with breakpoints, the watchpoint's condition
1776 expression is stored in the breakpoint object, not in the
1777 locations (re)created below. */
c1fc2657 1778 if (b->cond_string != NULL)
60e1c644 1779 {
4d01a485 1780 b->cond_exp.reset ();
60e1c644 1781
c1fc2657 1782 s = b->cond_string;
1bb9788d 1783 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1784 }
a5606eee 1785 }
a5606eee
VP
1786
1787 /* If we failed to parse the expression, for example because
1788 it refers to a global variable in a not-yet-loaded shared library,
1789 don't try to insert watchpoint. We don't automatically delete
1790 such watchpoint, though, since failure to parse expression
1791 is different from out-of-scope watchpoint. */
e8369a73 1792 if (!target_has_execution)
2d134ed3
PA
1793 {
1794 /* Without execution, memory can't change. No use to try and
1795 set watchpoint locations. The watchpoint will be reset when
1796 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1797 if (!can_use_hw_watchpoints)
1798 {
c1fc2657
SM
1799 if (b->ops->works_in_software_mode (b))
1800 b->type = bp_watchpoint;
e8369a73 1801 else
638aa5a1
AB
1802 error (_("Can't set read/access watchpoint when "
1803 "hardware watchpoints are disabled."));
e8369a73 1804 }
2d134ed3
PA
1805 }
1806 else if (within_current_scope && b->exp)
a5606eee 1807 {
0cf6dd15 1808 int pc = 0;
fa4727a6 1809 struct value *val_chain, *v, *result, *next;
2d134ed3 1810 struct program_space *frame_pspace;
a5606eee 1811
4d01a485 1812 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1813
a5606eee
VP
1814 /* Avoid setting b->val if it's already set. The meaning of
1815 b->val is 'the last value' user saw, and we should update
1816 it only if we reported that last value to user. As it
9c06b0b4
TJB
1817 happens, the code that reports it updates b->val directly.
1818 We don't keep track of the memory value for masked
1819 watchpoints. */
c1fc2657 1820 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1821 {
bb9d5f81
PP
1822 if (b->val_bitsize != 0)
1823 {
1824 v = extract_bitfield_from_watchpoint_value (b, v);
1825 if (v != NULL)
1826 release_value (v);
1827 }
fa4727a6
DJ
1828 b->val = v;
1829 b->val_valid = 1;
1830 }
a5606eee 1831
2d134ed3
PA
1832 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1833
a5606eee 1834 /* Look at each value on the value chain. */
9fa40276 1835 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1836 {
1837 /* If it's a memory location, and GDB actually needed
1838 its contents to evaluate the expression, then we
fa4727a6
DJ
1839 must watch it. If the first value returned is
1840 still lazy, that means an error occurred reading it;
1841 watch it anyway in case it becomes readable. */
a5606eee 1842 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1843 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1844 {
1845 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1846
a5606eee
VP
1847 /* We only watch structs and arrays if user asked
1848 for it explicitly, never if they just happen to
1849 appear in the middle of some value chain. */
fa4727a6 1850 if (v == result
a5606eee
VP
1851 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1852 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1853 {
1854 CORE_ADDR addr;
f486487f 1855 enum target_hw_bp_type type;
a5606eee 1856 struct bp_location *loc, **tmp;
bb9d5f81
PP
1857 int bitpos = 0, bitsize = 0;
1858
1859 if (value_bitsize (v) != 0)
1860 {
1861 /* Extract the bit parameters out from the bitfield
1862 sub-expression. */
1863 bitpos = value_bitpos (v);
1864 bitsize = value_bitsize (v);
1865 }
1866 else if (v == result && b->val_bitsize != 0)
1867 {
1868 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1869 lvalue whose bit parameters are saved in the fields
1870 VAL_BITPOS and VAL_BITSIZE. */
1871 bitpos = b->val_bitpos;
1872 bitsize = b->val_bitsize;
1873 }
a5606eee 1874
42ae5230 1875 addr = value_address (v);
bb9d5f81
PP
1876 if (bitsize != 0)
1877 {
1878 /* Skip the bytes that don't contain the bitfield. */
1879 addr += bitpos / 8;
1880 }
1881
a5606eee 1882 type = hw_write;
c1fc2657 1883 if (b->type == bp_read_watchpoint)
a5606eee 1884 type = hw_read;
c1fc2657 1885 else if (b->type == bp_access_watchpoint)
a5606eee 1886 type = hw_access;
3a5c3e22 1887
c1fc2657
SM
1888 loc = allocate_bp_location (b);
1889 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1890 ;
1891 *tmp = loc;
a6d9a66e 1892 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1893
1894 loc->pspace = frame_pspace;
a5606eee 1895 loc->address = addr;
bb9d5f81
PP
1896
1897 if (bitsize != 0)
1898 {
1899 /* Just cover the bytes that make up the bitfield. */
1900 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1901 }
1902 else
1903 loc->length = TYPE_LENGTH (value_type (v));
1904
a5606eee
VP
1905 loc->watchpoint_type = type;
1906 }
1907 }
9fa40276
TJB
1908 }
1909
1910 /* Change the type of breakpoint between hardware assisted or
1911 an ordinary watchpoint depending on the hardware support
1912 and free hardware slots. REPARSE is set when the inferior
1913 is started. */
a9634178 1914 if (reparse)
9fa40276 1915 {
e09342b5 1916 int reg_cnt;
9fa40276
TJB
1917 enum bp_loc_type loc_type;
1918 struct bp_location *bl;
a5606eee 1919
a9634178 1920 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1921
1922 if (reg_cnt)
9fa40276
TJB
1923 {
1924 int i, target_resources_ok, other_type_used;
a1398e0c 1925 enum bptype type;
9fa40276 1926
a9634178
TJB
1927 /* Use an exact watchpoint when there's only one memory region to be
1928 watched, and only one debug register is needed to watch it. */
1929 b->exact = target_exact_watchpoints && reg_cnt == 1;
1930
9fa40276 1931 /* We need to determine how many resources are already
e09342b5
TJB
1932 used for all other hardware watchpoints plus this one
1933 to see if we still have enough resources to also fit
a1398e0c
PA
1934 this watchpoint in as well. */
1935
1936 /* If this is a software watchpoint, we try to turn it
1937 to a hardware one -- count resources as if B was of
1938 hardware watchpoint type. */
c1fc2657 1939 type = b->type;
a1398e0c
PA
1940 if (type == bp_watchpoint)
1941 type = bp_hardware_watchpoint;
1942
1943 /* This watchpoint may or may not have been placed on
1944 the list yet at this point (it won't be in the list
1945 if we're trying to create it for the first time,
1946 through watch_command), so always account for it
1947 manually. */
1948
1949 /* Count resources used by all watchpoints except B. */
c1fc2657 1950 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1951
1952 /* Add in the resources needed for B. */
c1fc2657 1953 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1954
1955 target_resources_ok
1956 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1957 if (target_resources_ok <= 0)
a9634178 1958 {
c1fc2657 1959 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1960
1961 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1962 error (_("Target does not support this type of "
1963 "hardware watchpoint."));
9c06b0b4
TJB
1964 else if (target_resources_ok < 0 && !sw_mode)
1965 error (_("There are not enough available hardware "
1966 "resources for this watchpoint."));
a1398e0c
PA
1967
1968 /* Downgrade to software watchpoint. */
c1fc2657 1969 b->type = bp_watchpoint;
a1398e0c
PA
1970 }
1971 else
1972 {
1973 /* If this was a software watchpoint, we've just
1974 found we have enough resources to turn it to a
1975 hardware watchpoint. Otherwise, this is a
1976 nop. */
c1fc2657 1977 b->type = type;
a9634178 1978 }
9fa40276 1979 }
c1fc2657 1980 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1981 {
1982 if (!can_use_hw_watchpoints)
1983 error (_("Can't set read/access watchpoint when "
1984 "hardware watchpoints are disabled."));
1985 else
1986 error (_("Expression cannot be implemented with "
1987 "read/access watchpoint."));
1988 }
9fa40276 1989 else
c1fc2657 1990 b->type = bp_watchpoint;
9fa40276 1991
c1fc2657 1992 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1993 : bp_loc_hardware_watchpoint);
c1fc2657 1994 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1995 bl->loc_type = loc_type;
1996 }
1997
1998 for (v = val_chain; v; v = next)
1999 {
a5606eee
VP
2000 next = value_next (v);
2001 if (v != b->val)
2002 value_free (v);
2003 }
2004
c7437ca6
PA
2005 /* If a software watchpoint is not watching any memory, then the
2006 above left it without any location set up. But,
2007 bpstat_stop_status requires a location to be able to report
2008 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2009 if (b->type == bp_watchpoint && b->loc == NULL)
2010 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2011 }
2012 else if (!within_current_scope)
7270d8f2 2013 {
ac74f770
MS
2014 printf_filtered (_("\
2015Watchpoint %d deleted because the program has left the block\n\
2016in which its expression is valid.\n"),
c1fc2657 2017 b->number);
d0fb5eae 2018 watchpoint_del_at_next_stop (b);
7270d8f2 2019 }
a5606eee
VP
2020
2021 /* Restore the selected frame. */
66076460
DJ
2022 if (frame_saved)
2023 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2024}
2025
a5606eee 2026
74960c60 2027/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2028 inserted in the inferior. We don't differentiate the type of BL's owner
2029 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2030 breakpoint_ops is not defined, because in insert_bp_location,
2031 tracepoint's insert_location will not be called. */
74960c60 2032static int
35df4500 2033should_be_inserted (struct bp_location *bl)
74960c60 2034{
35df4500 2035 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2036 return 0;
2037
35df4500 2038 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2039 return 0;
2040
35df4500 2041 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2042 return 0;
2043
f8eba3c6
TT
2044 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2045 return 0;
2046
56710373
PA
2047 /* This is set for example, when we're attached to the parent of a
2048 vfork, and have detached from the child. The child is running
2049 free, and we expect it to do an exec or exit, at which point the
2050 OS makes the parent schedulable again (and the target reports
2051 that the vfork is done). Until the child is done with the shared
2052 memory region, do not insert breakpoints in the parent, otherwise
2053 the child could still trip on the parent's breakpoints. Since
2054 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2055 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2056 return 0;
2057
31e77af2 2058 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2059 location, except if the breakpoint is a single-step breakpoint,
2060 and the breakpoint's thread is the thread which is stepping past
2061 a breakpoint. */
31e77af2
PA
2062 if ((bl->loc_type == bp_loc_software_breakpoint
2063 || bl->loc_type == bp_loc_hardware_breakpoint)
2064 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2065 bl->address)
2066 /* The single-step breakpoint may be inserted at the location
2067 we're trying to step if the instruction branches to itself.
2068 However, the instruction won't be executed at all and it may
2069 break the semantics of the instruction, for example, the
2070 instruction is a conditional branch or updates some flags.
2071 We can't fix it unless GDB is able to emulate the instruction
2072 or switch to displaced stepping. */
2073 && !(bl->owner->type == bp_single_step
2074 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2075 {
2076 if (debug_infrun)
2077 {
2078 fprintf_unfiltered (gdb_stdlog,
2079 "infrun: skipping breakpoint: "
2080 "stepping past insn at: %s\n",
2081 paddress (bl->gdbarch, bl->address));
2082 }
2083 return 0;
2084 }
31e77af2 2085
963f9c80
PA
2086 /* Don't insert watchpoints if we're trying to step past the
2087 instruction that triggered one. */
2088 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2089 && stepping_past_nonsteppable_watchpoint ())
2090 {
2091 if (debug_infrun)
2092 {
2093 fprintf_unfiltered (gdb_stdlog,
2094 "infrun: stepping past non-steppable watchpoint. "
2095 "skipping watchpoint at %s:%d\n",
2096 paddress (bl->gdbarch, bl->address),
2097 bl->length);
2098 }
2099 return 0;
2100 }
2101
74960c60
VP
2102 return 1;
2103}
2104
934709f0
PW
2105/* Same as should_be_inserted but does the check assuming
2106 that the location is not duplicated. */
2107
2108static int
2109unduplicated_should_be_inserted (struct bp_location *bl)
2110{
2111 int result;
2112 const int save_duplicate = bl->duplicate;
2113
2114 bl->duplicate = 0;
2115 result = should_be_inserted (bl);
2116 bl->duplicate = save_duplicate;
2117 return result;
2118}
2119
b775012e
LM
2120/* Parses a conditional described by an expression COND into an
2121 agent expression bytecode suitable for evaluation
2122 by the bytecode interpreter. Return NULL if there was
2123 any error during parsing. */
2124
833177a4 2125static agent_expr_up
b775012e
LM
2126parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2127{
833177a4 2128 if (cond == NULL)
b775012e
LM
2129 return NULL;
2130
833177a4
PA
2131 agent_expr_up aexpr;
2132
b775012e
LM
2133 /* We don't want to stop processing, so catch any errors
2134 that may show up. */
492d29ea 2135 TRY
b775012e 2136 {
036e657b 2137 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2138 }
2139
492d29ea 2140 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2141 {
2142 /* If we got here, it means the condition could not be parsed to a valid
2143 bytecode expression and thus can't be evaluated on the target's side.
2144 It's no use iterating through the conditions. */
b775012e 2145 }
492d29ea 2146 END_CATCH
b775012e
LM
2147
2148 /* We have a valid agent expression. */
2149 return aexpr;
2150}
2151
2152/* Based on location BL, create a list of breakpoint conditions to be
2153 passed on to the target. If we have duplicated locations with different
2154 conditions, we will add such conditions to the list. The idea is that the
2155 target will evaluate the list of conditions and will only notify GDB when
2156 one of them is true. */
2157
2158static void
2159build_target_condition_list (struct bp_location *bl)
2160{
2161 struct bp_location **locp = NULL, **loc2p;
2162 int null_condition_or_parse_error = 0;
2163 int modified = bl->needs_update;
2164 struct bp_location *loc;
2165
8b4f3082 2166 /* Release conditions left over from a previous insert. */
3cde5c42 2167 bl->target_info.conditions.clear ();
8b4f3082 2168
b775012e
LM
2169 /* This is only meaningful if the target is
2170 evaluating conditions and if the user has
2171 opted for condition evaluation on the target's
2172 side. */
2173 if (gdb_evaluates_breakpoint_condition_p ()
2174 || !target_supports_evaluation_of_breakpoint_conditions ())
2175 return;
2176
2177 /* Do a first pass to check for locations with no assigned
2178 conditions or conditions that fail to parse to a valid agent expression
2179 bytecode. If any of these happen, then it's no use to send conditions
2180 to the target since this location will always trigger and generate a
2181 response back to GDB. */
2182 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2183 {
2184 loc = (*loc2p);
2185 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2186 {
2187 if (modified)
2188 {
b775012e
LM
2189 /* Re-parse the conditions since something changed. In that
2190 case we already freed the condition bytecodes (see
2191 force_breakpoint_reinsertion). We just
2192 need to parse the condition to bytecodes again. */
833177a4
PA
2193 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2194 loc->cond.get ());
b775012e
LM
2195 }
2196
2197 /* If we have a NULL bytecode expression, it means something
2198 went wrong or we have a null condition expression. */
2199 if (!loc->cond_bytecode)
2200 {
2201 null_condition_or_parse_error = 1;
2202 break;
2203 }
2204 }
2205 }
2206
2207 /* If any of these happened, it means we will have to evaluate the conditions
2208 for the location's address on gdb's side. It is no use keeping bytecodes
2209 for all the other duplicate locations, thus we free all of them here.
2210
2211 This is so we have a finer control over which locations' conditions are
2212 being evaluated by GDB or the remote stub. */
2213 if (null_condition_or_parse_error)
2214 {
2215 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2216 {
2217 loc = (*loc2p);
2218 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2219 {
2220 /* Only go as far as the first NULL bytecode is
2221 located. */
2222 if (!loc->cond_bytecode)
2223 return;
2224
833177a4 2225 loc->cond_bytecode.reset ();
b775012e
LM
2226 }
2227 }
2228 }
2229
2230 /* No NULL conditions or failed bytecode generation. Build a condition list
2231 for this location's address. */
2232 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2233 {
2234 loc = (*loc2p);
2235 if (loc->cond
2236 && is_breakpoint (loc->owner)
2237 && loc->pspace->num == bl->pspace->num
2238 && loc->owner->enable_state == bp_enabled
2239 && loc->enabled)
3cde5c42
PA
2240 {
2241 /* Add the condition to the vector. This will be used later
2242 to send the conditions to the target. */
2243 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2244 }
b775012e
LM
2245 }
2246
2247 return;
2248}
2249
d3ce09f5
SS
2250/* Parses a command described by string CMD into an agent expression
2251 bytecode suitable for evaluation by the bytecode interpreter.
2252 Return NULL if there was any error during parsing. */
2253
833177a4 2254static agent_expr_up
d3ce09f5
SS
2255parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2256{
2257 struct cleanup *old_cleanups = 0;
4d01a485 2258 struct expression **argvec;
bbc13ae3
KS
2259 const char *cmdrest;
2260 const char *format_start, *format_end;
d3ce09f5
SS
2261 struct format_piece *fpieces;
2262 int nargs;
2263 struct gdbarch *gdbarch = get_current_arch ();
2264
833177a4 2265 if (cmd == NULL)
d3ce09f5
SS
2266 return NULL;
2267
2268 cmdrest = cmd;
2269
2270 if (*cmdrest == ',')
2271 ++cmdrest;
f1735a53 2272 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2273
2274 if (*cmdrest++ != '"')
2275 error (_("No format string following the location"));
2276
2277 format_start = cmdrest;
2278
2279 fpieces = parse_format_string (&cmdrest);
2280
2281 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2282
2283 format_end = cmdrest;
2284
2285 if (*cmdrest++ != '"')
2286 error (_("Bad format string, non-terminated '\"'."));
2287
f1735a53 2288 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2289
2290 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2291 error (_("Invalid argument syntax"));
2292
2293 if (*cmdrest == ',')
2294 cmdrest++;
f1735a53 2295 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2296
2297 /* For each argument, make an expression. */
2298
2299 argvec = (struct expression **) alloca (strlen (cmd)
2300 * sizeof (struct expression *));
2301
2302 nargs = 0;
2303 while (*cmdrest != '\0')
2304 {
bbc13ae3 2305 const char *cmd1;
d3ce09f5
SS
2306
2307 cmd1 = cmdrest;
4d01a485
PA
2308 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2309 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2310 cmdrest = cmd1;
2311 if (*cmdrest == ',')
2312 ++cmdrest;
2313 }
2314
833177a4
PA
2315 agent_expr_up aexpr;
2316
d3ce09f5
SS
2317 /* We don't want to stop processing, so catch any errors
2318 that may show up. */
492d29ea 2319 TRY
d3ce09f5 2320 {
036e657b
JB
2321 aexpr = gen_printf (scope, gdbarch, 0, 0,
2322 format_start, format_end - format_start,
2323 fpieces, nargs, argvec);
d3ce09f5 2324 }
492d29ea 2325 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2326 {
2327 /* If we got here, it means the command could not be parsed to a valid
2328 bytecode expression and thus can't be evaluated on the target's side.
2329 It's no use iterating through the other commands. */
d3ce09f5 2330 }
492d29ea
PA
2331 END_CATCH
2332
2333 do_cleanups (old_cleanups);
d3ce09f5 2334
d3ce09f5
SS
2335 /* We have a valid agent expression, return it. */
2336 return aexpr;
2337}
2338
2339/* Based on location BL, create a list of breakpoint commands to be
2340 passed on to the target. If we have duplicated locations with
2341 different commands, we will add any such to the list. */
2342
2343static void
2344build_target_command_list (struct bp_location *bl)
2345{
2346 struct bp_location **locp = NULL, **loc2p;
2347 int null_command_or_parse_error = 0;
2348 int modified = bl->needs_update;
2349 struct bp_location *loc;
2350
3cde5c42
PA
2351 /* Clear commands left over from a previous insert. */
2352 bl->target_info.tcommands.clear ();
8b4f3082 2353
41fac0cf 2354 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2355 return;
2356
41fac0cf
PA
2357 /* For now, limit to agent-style dprintf breakpoints. */
2358 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2359 return;
2360
41fac0cf
PA
2361 /* For now, if we have any duplicate location that isn't a dprintf,
2362 don't install the target-side commands, as that would make the
2363 breakpoint not be reported to the core, and we'd lose
2364 control. */
2365 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2366 {
2367 loc = (*loc2p);
2368 if (is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->type != bp_dprintf)
2371 return;
2372 }
2373
d3ce09f5
SS
2374 /* Do a first pass to check for locations with no assigned
2375 conditions or conditions that fail to parse to a valid agent expression
2376 bytecode. If any of these happen, then it's no use to send conditions
2377 to the target since this location will always trigger and generate a
2378 response back to GDB. */
2379 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2380 {
2381 loc = (*loc2p);
2382 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2383 {
2384 if (modified)
2385 {
d3ce09f5
SS
2386 /* Re-parse the commands since something changed. In that
2387 case we already freed the command bytecodes (see
2388 force_breakpoint_reinsertion). We just
2389 need to parse the command to bytecodes again. */
833177a4
PA
2390 loc->cmd_bytecode
2391 = parse_cmd_to_aexpr (bl->address,
2392 loc->owner->extra_string);
d3ce09f5
SS
2393 }
2394
2395 /* If we have a NULL bytecode expression, it means something
2396 went wrong or we have a null command expression. */
2397 if (!loc->cmd_bytecode)
2398 {
2399 null_command_or_parse_error = 1;
2400 break;
2401 }
2402 }
2403 }
2404
2405 /* If anything failed, then we're not doing target-side commands,
2406 and so clean up. */
2407 if (null_command_or_parse_error)
2408 {
2409 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2410 {
2411 loc = (*loc2p);
2412 if (is_breakpoint (loc->owner)
2413 && loc->pspace->num == bl->pspace->num)
2414 {
2415 /* Only go as far as the first NULL bytecode is
2416 located. */
40fb6c5e 2417 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2418 return;
2419
833177a4 2420 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2421 }
2422 }
2423 }
2424
2425 /* No NULL commands or failed bytecode generation. Build a command list
2426 for this location's address. */
2427 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2428 {
2429 loc = (*loc2p);
2430 if (loc->owner->extra_string
2431 && is_breakpoint (loc->owner)
2432 && loc->pspace->num == bl->pspace->num
2433 && loc->owner->enable_state == bp_enabled
2434 && loc->enabled)
3cde5c42
PA
2435 {
2436 /* Add the command to the vector. This will be used later
2437 to send the commands to the target. */
2438 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2439 }
d3ce09f5
SS
2440 }
2441
2442 bl->target_info.persist = 0;
2443 /* Maybe flag this location as persistent. */
2444 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2445 bl->target_info.persist = 1;
2446}
2447
833b7ab5
YQ
2448/* Return the kind of breakpoint on address *ADDR. Get the kind
2449 of breakpoint according to ADDR except single-step breakpoint.
2450 Get the kind of single-step breakpoint according to the current
2451 registers state. */
cd6c3b4f
YQ
2452
2453static int
2454breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2455{
833b7ab5
YQ
2456 if (bl->owner->type == bp_single_step)
2457 {
2458 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2459 struct regcache *regcache;
2460
2461 regcache = get_thread_regcache (thr->ptid);
2462
2463 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2464 regcache, addr);
2465 }
2466 else
2467 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2468}
2469
35df4500
TJB
2470/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2471 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2472 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2473 Returns 0 for success, 1 if the bp_location type is not supported or
2474 -1 for failure.
879bfdc2 2475
4a64f543
MS
2476 NOTE drow/2003-09-09: This routine could be broken down to an
2477 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2478static int
35df4500 2479insert_bp_location (struct bp_location *bl,
26bb91f3 2480 struct ui_file *tmp_error_stream,
3fbb6ffa 2481 int *disabled_breaks,
dd61ec5c
MW
2482 int *hw_breakpoint_error,
2483 int *hw_bp_error_explained_already)
879bfdc2 2484{
0000e5cc
PA
2485 enum errors bp_err = GDB_NO_ERROR;
2486 const char *bp_err_message = NULL;
879bfdc2 2487
b775012e 2488 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2489 return 0;
2490
35c63cd8
JB
2491 /* Note we don't initialize bl->target_info, as that wipes out
2492 the breakpoint location's shadow_contents if the breakpoint
2493 is still inserted at that location. This in turn breaks
2494 target_read_memory which depends on these buffers when
2495 a memory read is requested at the breakpoint location:
2496 Once the target_info has been wiped, we fail to see that
2497 we have a breakpoint inserted at that address and thus
2498 read the breakpoint instead of returning the data saved in
2499 the breakpoint location's shadow contents. */
0d5ed153 2500 bl->target_info.reqstd_address = bl->address;
35df4500 2501 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2502 bl->target_info.length = bl->length;
8181d85f 2503
b775012e
LM
2504 /* When working with target-side conditions, we must pass all the conditions
2505 for the same breakpoint address down to the target since GDB will not
2506 insert those locations. With a list of breakpoint conditions, the target
2507 can decide when to stop and notify GDB. */
2508
2509 if (is_breakpoint (bl->owner))
2510 {
2511 build_target_condition_list (bl);
d3ce09f5
SS
2512 build_target_command_list (bl);
2513 /* Reset the modification marker. */
b775012e
LM
2514 bl->needs_update = 0;
2515 }
2516
35df4500
TJB
2517 if (bl->loc_type == bp_loc_software_breakpoint
2518 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2519 {
35df4500 2520 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2521 {
2522 /* If the explicitly specified breakpoint type
2523 is not hardware breakpoint, check the memory map to see
2524 if the breakpoint address is in read only memory or not.
4a64f543 2525
765dc015
VP
2526 Two important cases are:
2527 - location type is not hardware breakpoint, memory
2528 is readonly. We change the type of the location to
2529 hardware breakpoint.
4a64f543
MS
2530 - location type is hardware breakpoint, memory is
2531 read-write. This means we've previously made the
2532 location hardware one, but then the memory map changed,
2533 so we undo.
765dc015 2534
4a64f543
MS
2535 When breakpoints are removed, remove_breakpoints will use
2536 location types we've just set here, the only possible
2537 problem is that memory map has changed during running
2538 program, but it's not going to work anyway with current
2539 gdb. */
765dc015 2540 struct mem_region *mr
0d5ed153 2541 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2542
2543 if (mr)
2544 {
2545 if (automatic_hardware_breakpoints)
2546 {
765dc015
VP
2547 enum bp_loc_type new_type;
2548
2549 if (mr->attrib.mode != MEM_RW)
2550 new_type = bp_loc_hardware_breakpoint;
2551 else
2552 new_type = bp_loc_software_breakpoint;
2553
35df4500 2554 if (new_type != bl->loc_type)
765dc015
VP
2555 {
2556 static int said = 0;
cc59ec59 2557
35df4500 2558 bl->loc_type = new_type;
765dc015
VP
2559 if (!said)
2560 {
3e43a32a
MS
2561 fprintf_filtered (gdb_stdout,
2562 _("Note: automatically using "
2563 "hardware breakpoints for "
2564 "read-only addresses.\n"));
765dc015
VP
2565 said = 1;
2566 }
2567 }
2568 }
35df4500 2569 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2570 && mr->attrib.mode != MEM_RW)
2571 {
2572 fprintf_unfiltered (tmp_error_stream,
2573 _("Cannot insert breakpoint %d.\n"
2574 "Cannot set software breakpoint "
2575 "at read-only address %s\n"),
2576 bl->owner->number,
2577 paddress (bl->gdbarch, bl->address));
2578 return 1;
2579 }
765dc015
VP
2580 }
2581 }
2582
879bfdc2
DJ
2583 /* First check to see if we have to handle an overlay. */
2584 if (overlay_debugging == ovly_off
35df4500
TJB
2585 || bl->section == NULL
2586 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2587 {
2588 /* No overlay handling: just set the breakpoint. */
492d29ea 2589 TRY
dd61ec5c 2590 {
0000e5cc
PA
2591 int val;
2592
dd61ec5c 2593 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2594 if (val)
2595 bp_err = GENERIC_ERROR;
dd61ec5c 2596 }
492d29ea 2597 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2598 {
0000e5cc
PA
2599 bp_err = e.error;
2600 bp_err_message = e.message;
dd61ec5c 2601 }
492d29ea 2602 END_CATCH
879bfdc2
DJ
2603 }
2604 else
2605 {
4a64f543 2606 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2607 Shall we set a breakpoint at the LMA? */
2608 if (!overlay_events_enabled)
2609 {
2610 /* Yes -- overlay event support is not active,
2611 so we must try to set a breakpoint at the LMA.
2612 This will not work for a hardware breakpoint. */
35df4500 2613 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2614 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2615 bl->owner->number);
879bfdc2
DJ
2616 else
2617 {
35df4500
TJB
2618 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2619 bl->section);
879bfdc2 2620 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2621 bl->overlay_target_info = bl->target_info;
0d5ed153 2622 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2623
2624 /* No overlay handling: just set the breakpoint. */
492d29ea 2625 TRY
0000e5cc
PA
2626 {
2627 int val;
2628
579c6ad9 2629 bl->overlay_target_info.kind
cd6c3b4f
YQ
2630 = breakpoint_kind (bl, &addr);
2631 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2632 val = target_insert_breakpoint (bl->gdbarch,
2633 &bl->overlay_target_info);
2634 if (val)
2635 bp_err = GENERIC_ERROR;
2636 }
492d29ea 2637 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2638 {
2639 bp_err = e.error;
2640 bp_err_message = e.message;
2641 }
492d29ea 2642 END_CATCH
0000e5cc
PA
2643
2644 if (bp_err != GDB_NO_ERROR)
99361f52 2645 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2646 "Overlay breakpoint %d "
2647 "failed: in ROM?\n",
35df4500 2648 bl->owner->number);
879bfdc2
DJ
2649 }
2650 }
2651 /* Shall we set a breakpoint at the VMA? */
35df4500 2652 if (section_is_mapped (bl->section))
879bfdc2
DJ
2653 {
2654 /* Yes. This overlay section is mapped into memory. */
492d29ea 2655 TRY
dd61ec5c 2656 {
0000e5cc
PA
2657 int val;
2658
dd61ec5c 2659 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2660 if (val)
2661 bp_err = GENERIC_ERROR;
dd61ec5c 2662 }
492d29ea 2663 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2664 {
0000e5cc
PA
2665 bp_err = e.error;
2666 bp_err_message = e.message;
dd61ec5c 2667 }
492d29ea 2668 END_CATCH
879bfdc2
DJ
2669 }
2670 else
2671 {
2672 /* No. This breakpoint will not be inserted.
2673 No error, but do not mark the bp as 'inserted'. */
2674 return 0;
2675 }
2676 }
2677
0000e5cc 2678 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2679 {
2680 /* Can't set the breakpoint. */
0000e5cc
PA
2681
2682 /* In some cases, we might not be able to insert a
2683 breakpoint in a shared library that has already been
2684 removed, but we have not yet processed the shlib unload
2685 event. Unfortunately, some targets that implement
076855f9
PA
2686 breakpoint insertion themselves can't tell why the
2687 breakpoint insertion failed (e.g., the remote target
2688 doesn't define error codes), so we must treat generic
2689 errors as memory errors. */
0000e5cc 2690 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2691 && bl->loc_type == bp_loc_software_breakpoint
08351840 2692 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2693 || shared_objfile_contains_address_p (bl->pspace,
2694 bl->address)))
879bfdc2 2695 {
4a64f543 2696 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2697 bl->shlib_disabled = 1;
8d3788bd 2698 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2699 if (!*disabled_breaks)
2700 {
2701 fprintf_unfiltered (tmp_error_stream,
2702 "Cannot insert breakpoint %d.\n",
2703 bl->owner->number);
2704 fprintf_unfiltered (tmp_error_stream,
2705 "Temporarily disabling shared "
2706 "library breakpoints:\n");
2707 }
2708 *disabled_breaks = 1;
879bfdc2 2709 fprintf_unfiltered (tmp_error_stream,
35df4500 2710 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2711 return 0;
879bfdc2
DJ
2712 }
2713 else
879bfdc2 2714 {
35df4500 2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2716 {
0000e5cc
PA
2717 *hw_breakpoint_error = 1;
2718 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2719 fprintf_unfiltered (tmp_error_stream,
2720 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2721 bl->owner->number, bp_err_message ? ":" : ".\n");
2722 if (bp_err_message != NULL)
2723 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2724 }
2725 else
2726 {
0000e5cc
PA
2727 if (bp_err_message == NULL)
2728 {
1ccbe998 2729 std::string message
0000e5cc
PA
2730 = memory_error_message (TARGET_XFER_E_IO,
2731 bl->gdbarch, bl->address);
0000e5cc
PA
2732
2733 fprintf_unfiltered (tmp_error_stream,
2734 "Cannot insert breakpoint %d.\n"
2735 "%s\n",
1ccbe998 2736 bl->owner->number, message.c_str ());
0000e5cc
PA
2737 }
2738 else
2739 {
2740 fprintf_unfiltered (tmp_error_stream,
2741 "Cannot insert breakpoint %d: %s\n",
2742 bl->owner->number,
2743 bp_err_message);
2744 }
879bfdc2 2745 }
0000e5cc 2746 return 1;
879bfdc2
DJ
2747
2748 }
2749 }
2750 else
35df4500 2751 bl->inserted = 1;
879bfdc2 2752
0000e5cc 2753 return 0;
879bfdc2
DJ
2754 }
2755
35df4500 2756 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2757 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2758 watchpoints. It's not clear that it's necessary... */
35df4500 2759 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2760 {
0000e5cc
PA
2761 int val;
2762
77b06cd7
TJB
2763 gdb_assert (bl->owner->ops != NULL
2764 && bl->owner->ops->insert_location != NULL);
2765
2766 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2767
2768 /* If trying to set a read-watchpoint, and it turns out it's not
2769 supported, try emulating one with an access watchpoint. */
35df4500 2770 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2771 {
2772 struct bp_location *loc, **loc_temp;
2773
2774 /* But don't try to insert it, if there's already another
2775 hw_access location that would be considered a duplicate
2776 of this one. */
2777 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2778 if (loc != bl
85d721b8 2779 && loc->watchpoint_type == hw_access
35df4500 2780 && watchpoint_locations_match (bl, loc))
85d721b8 2781 {
35df4500
TJB
2782 bl->duplicate = 1;
2783 bl->inserted = 1;
2784 bl->target_info = loc->target_info;
2785 bl->watchpoint_type = hw_access;
85d721b8
PA
2786 val = 0;
2787 break;
2788 }
2789
2790 if (val == 1)
2791 {
77b06cd7
TJB
2792 bl->watchpoint_type = hw_access;
2793 val = bl->owner->ops->insert_location (bl);
2794
2795 if (val)
2796 /* Back to the original value. */
2797 bl->watchpoint_type = hw_read;
85d721b8
PA
2798 }
2799 }
2800
35df4500 2801 bl->inserted = (val == 0);
879bfdc2
DJ
2802 }
2803
35df4500 2804 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2805 {
0000e5cc
PA
2806 int val;
2807
77b06cd7
TJB
2808 gdb_assert (bl->owner->ops != NULL
2809 && bl->owner->ops->insert_location != NULL);
2810
2811 val = bl->owner->ops->insert_location (bl);
2812 if (val)
2813 {
2814 bl->owner->enable_state = bp_disabled;
2815
2816 if (val == 1)
2817 warning (_("\
2818Error inserting catchpoint %d: Your system does not support this type\n\
2819of catchpoint."), bl->owner->number);
2820 else
2821 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2822 }
2823
2824 bl->inserted = (val == 0);
1640b821
DJ
2825
2826 /* We've already printed an error message if there was a problem
2827 inserting this catchpoint, and we've disabled the catchpoint,
2828 so just return success. */
2829 return 0;
879bfdc2
DJ
2830 }
2831
2832 return 0;
2833}
2834
6c95b8df
PA
2835/* This function is called when program space PSPACE is about to be
2836 deleted. It takes care of updating breakpoints to not reference
2837 PSPACE anymore. */
2838
2839void
2840breakpoint_program_space_exit (struct program_space *pspace)
2841{
2842 struct breakpoint *b, *b_temp;
876fa593 2843 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2844
2845 /* Remove any breakpoint that was set through this program space. */
2846 ALL_BREAKPOINTS_SAFE (b, b_temp)
2847 {
2848 if (b->pspace == pspace)
2849 delete_breakpoint (b);
2850 }
2851
2852 /* Breakpoints set through other program spaces could have locations
2853 bound to PSPACE as well. Remove those. */
876fa593 2854 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2855 {
2856 struct bp_location *tmp;
2857
2858 if (loc->pspace == pspace)
2859 {
2bdf28a0 2860 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2861 if (loc->owner->loc == loc)
2862 loc->owner->loc = loc->next;
2863 else
2864 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2865 if (tmp->next == loc)
2866 {
2867 tmp->next = loc->next;
2868 break;
2869 }
2870 }
2871 }
2872
2873 /* Now update the global location list to permanently delete the
2874 removed locations above. */
44702360 2875 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2876}
2877
74960c60
VP
2878/* Make sure all breakpoints are inserted in inferior.
2879 Throws exception on any error.
2880 A breakpoint that is already inserted won't be inserted
2881 again, so calling this function twice is safe. */
2882void
2883insert_breakpoints (void)
2884{
2885 struct breakpoint *bpt;
2886
2887 ALL_BREAKPOINTS (bpt)
2888 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2889 {
2890 struct watchpoint *w = (struct watchpoint *) bpt;
2891
2892 update_watchpoint (w, 0 /* don't reparse. */);
2893 }
74960c60 2894
04086b45
PA
2895 /* Updating watchpoints creates new locations, so update the global
2896 location list. Explicitly tell ugll to insert locations and
2897 ignore breakpoints_always_inserted_mode. */
2898 update_global_location_list (UGLL_INSERT);
74960c60
VP
2899}
2900
20388dd6
YQ
2901/* Invoke CALLBACK for each of bp_location. */
2902
2903void
2904iterate_over_bp_locations (walk_bp_location_callback callback)
2905{
2906 struct bp_location *loc, **loc_tmp;
2907
2908 ALL_BP_LOCATIONS (loc, loc_tmp)
2909 {
2910 callback (loc, NULL);
2911 }
2912}
2913
b775012e
LM
2914/* This is used when we need to synch breakpoint conditions between GDB and the
2915 target. It is the case with deleting and disabling of breakpoints when using
2916 always-inserted mode. */
2917
2918static void
2919update_inserted_breakpoint_locations (void)
2920{
2921 struct bp_location *bl, **blp_tmp;
2922 int error_flag = 0;
2923 int val = 0;
2924 int disabled_breaks = 0;
2925 int hw_breakpoint_error = 0;
dd61ec5c 2926 int hw_bp_details_reported = 0;
b775012e 2927
d7e74731 2928 string_file tmp_error_stream;
b775012e
LM
2929
2930 /* Explicitly mark the warning -- this will only be printed if
2931 there was an error. */
d7e74731 2932 tmp_error_stream.puts ("Warning:\n");
b775012e 2933
5ed8105e 2934 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2935
2936 ALL_BP_LOCATIONS (bl, blp_tmp)
2937 {
2938 /* We only want to update software breakpoints and hardware
2939 breakpoints. */
2940 if (!is_breakpoint (bl->owner))
2941 continue;
2942
2943 /* We only want to update locations that are already inserted
2944 and need updating. This is to avoid unwanted insertion during
2945 deletion of breakpoints. */
2946 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2947 continue;
2948
2949 switch_to_program_space_and_thread (bl->pspace);
2950
2951 /* For targets that support global breakpoints, there's no need
2952 to select an inferior to insert breakpoint to. In fact, even
2953 if we aren't attached to any process yet, we should still
2954 insert breakpoints. */
f5656ead 2955 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2956 && ptid_equal (inferior_ptid, null_ptid))
2957 continue;
2958
d7e74731 2959 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2960 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2961 if (val)
2962 error_flag = val;
2963 }
2964
2965 if (error_flag)
2966 {
223ffa71 2967 target_terminal::ours_for_output ();
b775012e
LM
2968 error_stream (tmp_error_stream);
2969 }
b775012e
LM
2970}
2971
c30eee59 2972/* Used when starting or continuing the program. */
c906108c 2973
74960c60
VP
2974static void
2975insert_breakpoint_locations (void)
c906108c 2976{
a5606eee 2977 struct breakpoint *bpt;
35df4500 2978 struct bp_location *bl, **blp_tmp;
eacd795a 2979 int error_flag = 0;
c906108c 2980 int val = 0;
3fbb6ffa 2981 int disabled_breaks = 0;
81d0cc19 2982 int hw_breakpoint_error = 0;
dd61ec5c 2983 int hw_bp_error_explained_already = 0;
c906108c 2984
d7e74731
PA
2985 string_file tmp_error_stream;
2986
81d0cc19
GS
2987 /* Explicitly mark the warning -- this will only be printed if
2988 there was an error. */
d7e74731 2989 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2990
5ed8105e 2991 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2992
35df4500 2993 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2994 {
b775012e 2995 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2996 continue;
2997
4a64f543
MS
2998 /* There is no point inserting thread-specific breakpoints if
2999 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3000 has BL->OWNER always non-NULL. */
35df4500 3001 if (bl->owner->thread != -1
5d5658a1 3002 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3003 continue;
3004
35df4500 3005 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3006
3007 /* For targets that support global breakpoints, there's no need
3008 to select an inferior to insert breakpoint to. In fact, even
3009 if we aren't attached to any process yet, we should still
3010 insert breakpoints. */
f5656ead 3011 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3012 && ptid_equal (inferior_ptid, null_ptid))
3013 continue;
3014
d7e74731 3015 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3016 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3017 if (val)
eacd795a 3018 error_flag = val;
879bfdc2 3019 }
c906108c 3020
4a64f543
MS
3021 /* If we failed to insert all locations of a watchpoint, remove
3022 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3023 ALL_BREAKPOINTS (bpt)
3024 {
3025 int some_failed = 0;
3026 struct bp_location *loc;
3027
3028 if (!is_hardware_watchpoint (bpt))
3029 continue;
3030
d6b74ac4 3031 if (!breakpoint_enabled (bpt))
a5606eee 3032 continue;
74960c60
VP
3033
3034 if (bpt->disposition == disp_del_at_next_stop)
3035 continue;
a5606eee
VP
3036
3037 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3038 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3039 {
3040 some_failed = 1;
3041 break;
3042 }
3043 if (some_failed)
3044 {
3045 for (loc = bpt->loc; loc; loc = loc->next)
3046 if (loc->inserted)
834c0d03 3047 remove_breakpoint (loc);
a5606eee
VP
3048
3049 hw_breakpoint_error = 1;
d7e74731
PA
3050 tmp_error_stream.printf ("Could not insert "
3051 "hardware watchpoint %d.\n",
3052 bpt->number);
eacd795a 3053 error_flag = -1;
a5606eee
VP
3054 }
3055 }
3056
eacd795a 3057 if (error_flag)
81d0cc19
GS
3058 {
3059 /* If a hardware breakpoint or watchpoint was inserted, add a
3060 message about possibly exhausted resources. */
dd61ec5c 3061 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3062 {
d7e74731 3063 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3064You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3065 }
223ffa71 3066 target_terminal::ours_for_output ();
81d0cc19
GS
3067 error_stream (tmp_error_stream);
3068 }
c906108c
SS
3069}
3070
c30eee59
TJB
3071/* Used when the program stops.
3072 Returns zero if successful, or non-zero if there was a problem
3073 removing a breakpoint location. */
3074
c906108c 3075int
fba45db2 3076remove_breakpoints (void)
c906108c 3077{
35df4500 3078 struct bp_location *bl, **blp_tmp;
3a1bae8e 3079 int val = 0;
c906108c 3080
35df4500 3081 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3082 {
1e4d1764 3083 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3084 val |= remove_breakpoint (bl);
c5aa993b 3085 }
3a1bae8e 3086 return val;
c906108c
SS
3087}
3088
49fa26b0
PA
3089/* When a thread exits, remove breakpoints that are related to
3090 that thread. */
3091
3092static void
3093remove_threaded_breakpoints (struct thread_info *tp, int silent)
3094{
3095 struct breakpoint *b, *b_tmp;
3096
3097 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3098 {
5d5658a1 3099 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3100 {
3101 b->disposition = disp_del_at_next_stop;
3102
3103 printf_filtered (_("\
43792cf0
PA
3104Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3105 b->number, print_thread_id (tp));
49fa26b0
PA
3106
3107 /* Hide it from the user. */
3108 b->number = 0;
3109 }
3110 }
3111}
3112
6c95b8df
PA
3113/* Remove breakpoints of process PID. */
3114
3115int
3116remove_breakpoints_pid (int pid)
3117{
35df4500 3118 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3119 int val;
3120 struct inferior *inf = find_inferior_pid (pid);
3121
35df4500 3122 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3123 {
35df4500 3124 if (bl->pspace != inf->pspace)
6c95b8df
PA
3125 continue;
3126
fc126975 3127 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3128 {
834c0d03 3129 val = remove_breakpoint (bl);
6c95b8df
PA
3130 if (val != 0)
3131 return val;
3132 }
3133 }
3134 return 0;
3135}
3136
e58b0e63
PA
3137static int internal_breakpoint_number = -1;
3138
84f4c1fe
PM
3139/* Set the breakpoint number of B, depending on the value of INTERNAL.
3140 If INTERNAL is non-zero, the breakpoint number will be populated
3141 from internal_breakpoint_number and that variable decremented.
e5dd4106 3142 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3143 breakpoint_count and that value incremented. Internal breakpoints
3144 do not set the internal var bpnum. */
3145static void
3146set_breakpoint_number (int internal, struct breakpoint *b)
3147{
3148 if (internal)
3149 b->number = internal_breakpoint_number--;
3150 else
3151 {
3152 set_breakpoint_count (breakpoint_count + 1);
3153 b->number = breakpoint_count;
3154 }
3155}
3156
e62c965a 3157static struct breakpoint *
a6d9a66e 3158create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3159 CORE_ADDR address, enum bptype type,
c0a91b2b 3160 const struct breakpoint_ops *ops)
e62c965a 3161{
51abb421 3162 symtab_and_line sal;
e62c965a
PP
3163 sal.pc = address;
3164 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3165 sal.pspace = current_program_space;
e62c965a 3166
51abb421 3167 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3168 b->number = internal_breakpoint_number--;
3169 b->disposition = disp_donttouch;
3170
3171 return b;
3172}
3173
17450429
PP
3174static const char *const longjmp_names[] =
3175 {
3176 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3177 };
3178#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3179
3180/* Per-objfile data private to breakpoint.c. */
3181struct breakpoint_objfile_data
3182{
3183 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3184 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3185
3186 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3187 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3188
28106bc2 3189 /* True if we have looked for longjmp probes. */
43dce439 3190 int longjmp_searched = 0;
28106bc2 3191
45461e0d
SM
3192 /* SystemTap probe points for longjmp (if any). These are non-owning
3193 references. */
3194 std::vector<probe *> longjmp_probes;
28106bc2 3195
17450429 3196 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3197 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3198
3199 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3200 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3201
3202 /* True if we have looked for exception probes. */
43dce439 3203 int exception_searched = 0;
28106bc2 3204
45461e0d
SM
3205 /* SystemTap probe points for unwinding (if any). These are non-owning
3206 references. */
3207 std::vector<probe *> 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 {
43dce439 3235 bp_objfile_data = new breakpoint_objfile_data ();
17450429
PP
3236 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3237 }
3238 return bp_objfile_data;
3239}
3240
28106bc2 3241static void
43dce439 3242free_breakpoint_objfile_data (struct objfile *obj, void *data)
28106bc2 3243{
9a3c8263
SM
3244 struct breakpoint_objfile_data *bp_objfile_data
3245 = (struct breakpoint_objfile_data *) data;
28106bc2 3246
43dce439 3247 delete bp_objfile_data;
28106bc2
SDJ
3248}
3249
e62c965a 3250static void
af02033e 3251create_overlay_event_breakpoint (void)
e62c965a 3252{
69de3c6a 3253 struct objfile *objfile;
af02033e 3254 const char *const func_name = "_ovly_debug_event";
e62c965a 3255
69de3c6a
PP
3256 ALL_OBJFILES (objfile)
3257 {
3258 struct breakpoint *b;
17450429
PP
3259 struct breakpoint_objfile_data *bp_objfile_data;
3260 CORE_ADDR addr;
67994074 3261 struct explicit_location explicit_loc;
69de3c6a 3262
17450429
PP
3263 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3264
3b7344d5 3265 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3266 continue;
3267
3b7344d5 3268 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3269 {
3b7344d5 3270 struct bound_minimal_symbol m;
17450429
PP
3271
3272 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3273 if (m.minsym == NULL)
17450429
PP
3274 {
3275 /* Avoid future lookups in this objfile. */
3b7344d5 3276 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3277 continue;
3278 }
3279 bp_objfile_data->overlay_msym = m;
3280 }
e62c965a 3281
77e371c0 3282 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3283 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3284 bp_overlay_event,
3285 &internal_breakpoint_ops);
67994074
KS
3286 initialize_explicit_location (&explicit_loc);
3287 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3288 b->location = new_explicit_location (&explicit_loc);
e62c965a 3289
69de3c6a
PP
3290 if (overlay_debugging == ovly_auto)
3291 {
3292 b->enable_state = bp_enabled;
3293 overlay_events_enabled = 1;
3294 }
3295 else
3296 {
3297 b->enable_state = bp_disabled;
3298 overlay_events_enabled = 0;
3299 }
e62c965a 3300 }
e62c965a
PP
3301}
3302
0fd8e87f 3303static void
af02033e 3304create_longjmp_master_breakpoint (void)
0fd8e87f 3305{
6c95b8df 3306 struct program_space *pspace;
6c95b8df 3307
5ed8105e 3308 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3309
6c95b8df 3310 ALL_PSPACES (pspace)
af02033e
PP
3311 {
3312 struct objfile *objfile;
3313
3314 set_current_program_space (pspace);
3315
3316 ALL_OBJFILES (objfile)
0fd8e87f 3317 {
af02033e
PP
3318 int i;
3319 struct gdbarch *gdbarch;
17450429 3320 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3321
af02033e 3322 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3323
17450429
PP
3324 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3325
28106bc2
SDJ
3326 if (!bp_objfile_data->longjmp_searched)
3327 {
45461e0d
SM
3328 std::vector<probe *> ret
3329 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3330
45461e0d 3331 if (!ret.empty ())
25f9533e
SDJ
3332 {
3333 /* We are only interested in checking one element. */
45461e0d 3334 probe *p = ret[0];
25f9533e
SDJ
3335
3336 if (!can_evaluate_probe_arguments (p))
3337 {
3338 /* We cannot use the probe interface here, because it does
3339 not know how to evaluate arguments. */
45461e0d 3340 ret.clear ();
25f9533e
SDJ
3341 }
3342 }
3343 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3344 bp_objfile_data->longjmp_searched = 1;
3345 }
3346
45461e0d 3347 if (!bp_objfile_data->longjmp_probes.empty ())
28106bc2 3348 {
28106bc2
SDJ
3349 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3350
45461e0d 3351 for (probe *p : bp_objfile_data->longjmp_probes)
28106bc2
SDJ
3352 {
3353 struct breakpoint *b;
3354
729662a5 3355 b = create_internal_breakpoint (gdbarch,
45461e0d 3356 get_probe_address (p, objfile),
28106bc2
SDJ
3357 bp_longjmp_master,
3358 &internal_breakpoint_ops);
d28cd78a 3359 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3360 b->enable_state = bp_disabled;
3361 }
3362
3363 continue;
3364 }
3365
0569175e
TSD
3366 if (!gdbarch_get_longjmp_target_p (gdbarch))
3367 continue;
3368
17450429 3369 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3370 {
3371 struct breakpoint *b;
af02033e 3372 const char *func_name;
17450429 3373 CORE_ADDR addr;
67994074 3374 struct explicit_location explicit_loc;
6c95b8df 3375
3b7344d5 3376 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3377 continue;
0fd8e87f 3378
17450429 3379 func_name = longjmp_names[i];
3b7344d5 3380 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3381 {
3b7344d5 3382 struct bound_minimal_symbol m;
17450429
PP
3383
3384 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3385 if (m.minsym == NULL)
17450429
PP
3386 {
3387 /* Prevent future lookups in this objfile. */
3b7344d5 3388 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3389 continue;
3390 }
3391 bp_objfile_data->longjmp_msym[i] = m;
3392 }
3393
77e371c0 3394 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3395 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3396 &internal_breakpoint_ops);
67994074
KS
3397 initialize_explicit_location (&explicit_loc);
3398 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3399 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3400 b->enable_state = bp_disabled;
3401 }
0fd8e87f 3402 }
af02033e 3403 }
0fd8e87f
UW
3404}
3405
af02033e 3406/* Create a master std::terminate breakpoint. */
aa7d318d 3407static void
af02033e 3408create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3409{
3410 struct program_space *pspace;
af02033e 3411 const char *const func_name = "std::terminate()";
aa7d318d 3412
5ed8105e 3413 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3414
3415 ALL_PSPACES (pspace)
17450429
PP
3416 {
3417 struct objfile *objfile;
3418 CORE_ADDR addr;
3419
3420 set_current_program_space (pspace);
3421
aa7d318d
TT
3422 ALL_OBJFILES (objfile)
3423 {
3424 struct breakpoint *b;
17450429 3425 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3426 struct explicit_location explicit_loc;
aa7d318d 3427
17450429 3428 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3429
3b7344d5 3430 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3431 continue;
3432
3b7344d5 3433 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3434 {
3b7344d5 3435 struct bound_minimal_symbol m;
17450429
PP
3436
3437 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3438 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3439 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3440 {
3441 /* Prevent future lookups in this objfile. */
3b7344d5 3442 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3443 continue;
3444 }
3445 bp_objfile_data->terminate_msym = m;
3446 }
aa7d318d 3447
77e371c0 3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3449 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3450 bp_std_terminate_master,
3451 &internal_breakpoint_ops);
67994074
KS
3452 initialize_explicit_location (&explicit_loc);
3453 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3454 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3455 b->enable_state = bp_disabled;
3456 }
17450429 3457 }
aa7d318d
TT
3458}
3459
186c406b
TT
3460/* Install a master breakpoint on the unwinder's debug hook. */
3461
70221824 3462static void
186c406b
TT
3463create_exception_master_breakpoint (void)
3464{
3465 struct objfile *objfile;
17450429 3466 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3467
3468 ALL_OBJFILES (objfile)
3469 {
17450429
PP
3470 struct breakpoint *b;
3471 struct gdbarch *gdbarch;
3472 struct breakpoint_objfile_data *bp_objfile_data;
3473 CORE_ADDR addr;
67994074 3474 struct explicit_location explicit_loc;
17450429
PP
3475
3476 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3477
28106bc2
SDJ
3478 /* We prefer the SystemTap probe point if it exists. */
3479 if (!bp_objfile_data->exception_searched)
3480 {
45461e0d
SM
3481 std::vector<probe *> ret
3482 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3483
45461e0d 3484 if (!ret.empty ())
25f9533e
SDJ
3485 {
3486 /* We are only interested in checking one element. */
45461e0d 3487 probe *p = ret[0];
25f9533e
SDJ
3488
3489 if (!can_evaluate_probe_arguments (p))
3490 {
3491 /* We cannot use the probe interface here, because it does
3492 not know how to evaluate arguments. */
45461e0d 3493 ret.clear ();
25f9533e
SDJ
3494 }
3495 }
3496 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3497 bp_objfile_data->exception_searched = 1;
3498 }
3499
45461e0d 3500 if (!bp_objfile_data->exception_probes.empty ())
28106bc2
SDJ
3501 {
3502 struct gdbarch *gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3503
3504 for (probe *p : bp_objfile_data->exception_probes)
28106bc2
SDJ
3505 {
3506 struct breakpoint *b;
3507
729662a5 3508 b = create_internal_breakpoint (gdbarch,
45461e0d 3509 get_probe_address (p, objfile),
28106bc2
SDJ
3510 bp_exception_master,
3511 &internal_breakpoint_ops);
d28cd78a 3512 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3513 b->enable_state = bp_disabled;
3514 }
3515
3516 continue;
3517 }
3518
3519 /* Otherwise, try the hook function. */
3520
3b7344d5 3521 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3522 continue;
3523
3524 gdbarch = get_objfile_arch (objfile);
186c406b 3525
3b7344d5 3526 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3527 {
3b7344d5 3528 struct bound_minimal_symbol debug_hook;
186c406b 3529
17450429 3530 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3531 if (debug_hook.minsym == NULL)
17450429 3532 {
3b7344d5 3533 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3534 continue;
3535 }
3536
3537 bp_objfile_data->exception_msym = debug_hook;
186c406b 3538 }
17450429 3539
77e371c0 3540 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3541 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3542 &current_target);
06edf0c0
PA
3543 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3544 &internal_breakpoint_ops);
67994074
KS
3545 initialize_explicit_location (&explicit_loc);
3546 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3547 b->location = new_explicit_location (&explicit_loc);
17450429 3548 b->enable_state = bp_disabled;
186c406b 3549 }
186c406b
TT
3550}
3551
9ef9e6a6
KS
3552/* Does B have a location spec? */
3553
3554static int
3555breakpoint_event_location_empty_p (const struct breakpoint *b)
3556{
d28cd78a 3557 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3558}
3559
c906108c 3560void
fba45db2 3561update_breakpoints_after_exec (void)
c906108c 3562{
35df4500 3563 struct breakpoint *b, *b_tmp;
876fa593 3564 struct bp_location *bploc, **bplocp_tmp;
c906108c 3565
25b22b0a
PA
3566 /* We're about to delete breakpoints from GDB's lists. If the
3567 INSERTED flag is true, GDB will try to lift the breakpoints by
3568 writing the breakpoints' "shadow contents" back into memory. The
3569 "shadow contents" are NOT valid after an exec, so GDB should not
3570 do that. Instead, the target is responsible from marking
3571 breakpoints out as soon as it detects an exec. We don't do that
3572 here instead, because there may be other attempts to delete
3573 breakpoints after detecting an exec and before reaching here. */
876fa593 3574 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3575 if (bploc->pspace == current_program_space)
3576 gdb_assert (!bploc->inserted);
c906108c 3577
35df4500 3578 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3579 {
6c95b8df
PA
3580 if (b->pspace != current_program_space)
3581 continue;
3582
4a64f543 3583 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3584 if (b->type == bp_shlib_event)
3585 {
3586 delete_breakpoint (b);
3587 continue;
3588 }
c906108c 3589
4a64f543 3590 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3591 if (b->type == bp_jit_event)
3592 {
3593 delete_breakpoint (b);
3594 continue;
3595 }
3596
1900040c 3597 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3598 as must overlay event and longjmp master breakpoints. */
3599 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3600 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3601 || b->type == bp_exception_master)
c4093a6a
JM
3602 {
3603 delete_breakpoint (b);
3604 continue;
3605 }
3606
4a64f543 3607 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3608 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3609 {
3610 delete_breakpoint (b);
3611 continue;
3612 }
3613
7c16b83e
PA
3614 /* Just like single-step breakpoints. */
3615 if (b->type == bp_single_step)
3616 {
3617 delete_breakpoint (b);
3618 continue;
3619 }
3620
611c83ae
PA
3621 /* Longjmp and longjmp-resume breakpoints are also meaningless
3622 after an exec. */
186c406b 3623 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3624 || b->type == bp_longjmp_call_dummy
186c406b 3625 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3626 {
3627 delete_breakpoint (b);
3628 continue;
3629 }
3630
ce78b96d
JB
3631 if (b->type == bp_catchpoint)
3632 {
3633 /* For now, none of the bp_catchpoint breakpoints need to
3634 do anything at this point. In the future, if some of
3635 the catchpoints need to something, we will need to add
3636 a new method, and call this method from here. */
3637 continue;
3638 }
3639
c5aa993b
JM
3640 /* bp_finish is a special case. The only way we ought to be able
3641 to see one of these when an exec() has happened, is if the user
3642 caught a vfork, and then said "finish". Ordinarily a finish just
3643 carries them to the call-site of the current callee, by setting
3644 a temporary bp there and resuming. But in this case, the finish
3645 will carry them entirely through the vfork & exec.
3646
3647 We don't want to allow a bp_finish to remain inserted now. But
3648 we can't safely delete it, 'cause finish_command has a handle to
3649 the bp on a bpstat, and will later want to delete it. There's a
3650 chance (and I've seen it happen) that if we delete the bp_finish
3651 here, that its storage will get reused by the time finish_command
3652 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3653 We really must allow finish_command to delete a bp_finish.
3654
e5dd4106 3655 In the absence of a general solution for the "how do we know
53a5351d
JM
3656 it's safe to delete something others may have handles to?"
3657 problem, what we'll do here is just uninsert the bp_finish, and
3658 let finish_command delete it.
3659
3660 (We know the bp_finish is "doomed" in the sense that it's
3661 momentary, and will be deleted as soon as finish_command sees
3662 the inferior stopped. So it doesn't matter that the bp's
3663 address is probably bogus in the new a.out, unlike e.g., the
3664 solib breakpoints.) */
c5aa993b 3665
c5aa993b
JM
3666 if (b->type == bp_finish)
3667 {
3668 continue;
3669 }
3670
3671 /* Without a symbolic address, we have little hope of the
3672 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3673 a.out. */
9ef9e6a6 3674 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3675 {
3676 delete_breakpoint (b);
3677 continue;
3678 }
c5aa993b 3679 }
c906108c
SS
3680}
3681
3682int
d80ee84f 3683detach_breakpoints (ptid_t ptid)
c906108c 3684{
35df4500 3685 struct bp_location *bl, **blp_tmp;
3a1bae8e 3686 int val = 0;
2989a365 3687 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3688 struct inferior *inf = current_inferior ();
c5aa993b 3689
dfd4cc63 3690 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3691 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3692
6c95b8df 3693 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3694 inferior_ptid = ptid;
35df4500 3695 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3696 {
35df4500 3697 if (bl->pspace != inf->pspace)
6c95b8df
PA
3698 continue;
3699
bd9673a4
PW
3700 /* This function must physically remove breakpoints locations
3701 from the specified ptid, without modifying the breakpoint
3702 package's state. Locations of type bp_loc_other are only
3703 maintained at GDB side. So, there is no need to remove
3704 these bp_loc_other locations. Moreover, removing these
3705 would modify the breakpoint package's state. */
3706 if (bl->loc_type == bp_loc_other)
3707 continue;
3708
35df4500 3709 if (bl->inserted)
b2b6a7da 3710 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3711 }
d03285ec 3712
3a1bae8e 3713 return val;
c906108c
SS
3714}
3715
35df4500 3716/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3717 Note that this is used to detach breakpoints from a child fork.
3718 When we get here, the child isn't in the inferior list, and neither
3719 do we have objects to represent its address space --- we should
35df4500 3720 *not* look at bl->pspace->aspace here. */
6c95b8df 3721
c906108c 3722static int
b2b6a7da 3723remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3724{
3725 int val;
c5aa993b 3726
35df4500
TJB
3727 /* BL is never in moribund_locations by our callers. */
3728 gdb_assert (bl->owner != NULL);
2bdf28a0 3729
74960c60
VP
3730 /* The type of none suggests that owner is actually deleted.
3731 This should not ever happen. */
35df4500 3732 gdb_assert (bl->owner->type != bp_none);
0bde7532 3733
35df4500
TJB
3734 if (bl->loc_type == bp_loc_software_breakpoint
3735 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3736 {
c02f5703
MS
3737 /* "Normal" instruction breakpoint: either the standard
3738 trap-instruction bp (bp_breakpoint), or a
3739 bp_hardware_breakpoint. */
3740
3741 /* First check to see if we have to handle an overlay. */
3742 if (overlay_debugging == ovly_off
35df4500
TJB
3743 || bl->section == NULL
3744 || !(section_is_overlay (bl->section)))
c02f5703
MS
3745 {
3746 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3747
3748 /* If we're trying to uninsert a memory breakpoint that we
3749 know is set in a dynamic object that is marked
3750 shlib_disabled, then either the dynamic object was
3751 removed with "remove-symbol-file" or with
3752 "nosharedlibrary". In the former case, we don't know
3753 whether another dynamic object might have loaded over the
3754 breakpoint's address -- the user might well let us know
3755 about it next with add-symbol-file (the whole point of
d03de421 3756 add-symbol-file is letting the user manually maintain a
08351840
PA
3757 list of dynamically loaded objects). If we have the
3758 breakpoint's shadow memory, that is, this is a software
3759 breakpoint managed by GDB, check whether the breakpoint
3760 is still inserted in memory, to avoid overwriting wrong
3761 code with stale saved shadow contents. Note that HW
3762 breakpoints don't have shadow memory, as they're
3763 implemented using a mechanism that is not dependent on
3764 being able to modify the target's memory, and as such
3765 they should always be removed. */
3766 if (bl->shlib_disabled
3767 && bl->target_info.shadow_len != 0
3768 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3769 val = 0;
3770 else
73971819 3771 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3772 }
c906108c
SS
3773 else
3774 {
4a64f543 3775 /* This breakpoint is in an overlay section.
c02f5703
MS
3776 Did we set a breakpoint at the LMA? */
3777 if (!overlay_events_enabled)
3778 {
3779 /* Yes -- overlay event support is not active, so we
3780 should have set a breakpoint at the LMA. Remove it.
3781 */
c02f5703
MS
3782 /* Ignore any failures: if the LMA is in ROM, we will
3783 have already warned when we failed to insert it. */
35df4500
TJB
3784 if (bl->loc_type == bp_loc_hardware_breakpoint)
3785 target_remove_hw_breakpoint (bl->gdbarch,
3786 &bl->overlay_target_info);
c02f5703 3787 else
35df4500 3788 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3789 &bl->overlay_target_info,
3790 reason);
c02f5703
MS
3791 }
3792 /* Did we set a breakpoint at the VMA?
3793 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3794 if (bl->inserted)
c906108c 3795 {
c02f5703
MS
3796 /* Yes -- remove it. Previously we did not bother to
3797 remove the breakpoint if the section had been
3798 unmapped, but let's not rely on that being safe. We
3799 don't know what the overlay manager might do. */
aa67235e
UW
3800
3801 /* However, we should remove *software* breakpoints only
3802 if the section is still mapped, or else we overwrite
3803 wrong code with the saved shadow contents. */
348d480f
PA
3804 if (bl->loc_type == bp_loc_hardware_breakpoint
3805 || section_is_mapped (bl->section))
73971819 3806 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3807 else
3808 val = 0;
c906108c 3809 }
c02f5703
MS
3810 else
3811 {
3812 /* No -- not inserted, so no need to remove. No error. */
3813 val = 0;
3814 }
c906108c 3815 }
879d1e6b 3816
08351840
PA
3817 /* In some cases, we might not be able to remove a breakpoint in
3818 a shared library that has already been removed, but we have
3819 not yet processed the shlib unload event. Similarly for an
3820 unloaded add-symbol-file object - the user might not yet have
3821 had the chance to remove-symbol-file it. shlib_disabled will
3822 be set if the library/object has already been removed, but
3823 the breakpoint hasn't been uninserted yet, e.g., after
3824 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3825 always-inserted mode. */
076855f9 3826 if (val
08351840
PA
3827 && (bl->loc_type == bp_loc_software_breakpoint
3828 && (bl->shlib_disabled
3829 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3830 || shared_objfile_contains_address_p (bl->pspace,
3831 bl->address))))
879d1e6b
UW
3832 val = 0;
3833
c906108c
SS
3834 if (val)
3835 return val;
b2b6a7da 3836 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3837 }
35df4500 3838 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3839 {
77b06cd7
TJB
3840 gdb_assert (bl->owner->ops != NULL
3841 && bl->owner->ops->remove_location != NULL);
3842
b2b6a7da 3843 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3844 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3845
c906108c 3846 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3847 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3848 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3849 bl->owner->number);
c906108c 3850 }
35df4500
TJB
3851 else if (bl->owner->type == bp_catchpoint
3852 && breakpoint_enabled (bl->owner)
3853 && !bl->duplicate)
ce78b96d 3854 {
77b06cd7
TJB
3855 gdb_assert (bl->owner->ops != NULL
3856 && bl->owner->ops->remove_location != NULL);
ce78b96d 3857
73971819 3858 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3859 if (val)
3860 return val;
77b06cd7 3861
b2b6a7da 3862 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3863 }
c906108c
SS
3864
3865 return 0;
3866}
3867
6c95b8df 3868static int
834c0d03 3869remove_breakpoint (struct bp_location *bl)
6c95b8df 3870{
35df4500
TJB
3871 /* BL is never in moribund_locations by our callers. */
3872 gdb_assert (bl->owner != NULL);
2bdf28a0 3873
6c95b8df
PA
3874 /* The type of none suggests that owner is actually deleted.
3875 This should not ever happen. */
35df4500 3876 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3877
5ed8105e 3878 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3879
35df4500 3880 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3881
5ed8105e 3882 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3883}
3884
c906108c
SS
3885/* Clear the "inserted" flag in all breakpoints. */
3886
25b22b0a 3887void
fba45db2 3888mark_breakpoints_out (void)
c906108c 3889{
35df4500 3890 struct bp_location *bl, **blp_tmp;
c906108c 3891
35df4500 3892 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3893 if (bl->pspace == current_program_space)
35df4500 3894 bl->inserted = 0;
c906108c
SS
3895}
3896
53a5351d
JM
3897/* Clear the "inserted" flag in all breakpoints and delete any
3898 breakpoints which should go away between runs of the program.
c906108c
SS
3899
3900 Plus other such housekeeping that has to be done for breakpoints
3901 between runs.
3902
53a5351d
JM
3903 Note: this function gets called at the end of a run (by
3904 generic_mourn_inferior) and when a run begins (by
4a64f543 3905 init_wait_for_inferior). */
c906108c
SS
3906
3907
3908
3909void
fba45db2 3910breakpoint_init_inferior (enum inf_context context)
c906108c 3911{
35df4500 3912 struct breakpoint *b, *b_tmp;
870f88f7 3913 struct bp_location *bl;
1c5cfe86 3914 int ix;
6c95b8df 3915 struct program_space *pspace = current_program_space;
c906108c 3916
50c71eaf
PA
3917 /* If breakpoint locations are shared across processes, then there's
3918 nothing to do. */
f5656ead 3919 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3920 return;
3921
1a853c52 3922 mark_breakpoints_out ();
075f6582 3923
35df4500 3924 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3925 {
6c95b8df
PA
3926 if (b->loc && b->loc->pspace != pspace)
3927 continue;
3928
c5aa993b
JM
3929 switch (b->type)
3930 {
3931 case bp_call_dummy:
e2e4d78b 3932 case bp_longjmp_call_dummy:
c906108c 3933
c5aa993b 3934 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3935 cause problems when the inferior is rerun, so we better get
3936 rid of it. */
3937
3938 case bp_watchpoint_scope:
3939
3940 /* Also get rid of scope breakpoints. */
3941
3942 case bp_shlib_event:
3943
3944 /* Also remove solib event breakpoints. Their addresses may
3945 have changed since the last time we ran the program.
3946 Actually we may now be debugging against different target;
3947 and so the solib backend that installed this breakpoint may
3948 not be used in by the target. E.g.,
3949
3950 (gdb) file prog-linux
3951 (gdb) run # native linux target
3952 ...
3953 (gdb) kill
3954 (gdb) file prog-win.exe
3955 (gdb) tar rem :9999 # remote Windows gdbserver.
3956 */
c906108c 3957
f59f708a
PA
3958 case bp_step_resume:
3959
3960 /* Also remove step-resume breakpoints. */
3961
7c16b83e
PA
3962 case bp_single_step:
3963
3964 /* Also remove single-step breakpoints. */
3965
c5aa993b
JM
3966 delete_breakpoint (b);
3967 break;
c906108c 3968
c5aa993b
JM
3969 case bp_watchpoint:
3970 case bp_hardware_watchpoint:
3971 case bp_read_watchpoint:
3972 case bp_access_watchpoint:
3a5c3e22
PA
3973 {
3974 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3975
3a5c3e22
PA
3976 /* Likewise for watchpoints on local expressions. */
3977 if (w->exp_valid_block != NULL)
3978 delete_breakpoint (b);
63000888 3979 else
3a5c3e22 3980 {
63000888
PA
3981 /* Get rid of existing locations, which are no longer
3982 valid. New ones will be created in
3983 update_watchpoint, when the inferior is restarted.
3984 The next update_global_location_list call will
3985 garbage collect them. */
3986 b->loc = NULL;
3987
3988 if (context == inf_starting)
3989 {
3990 /* Reset val field to force reread of starting value in
3991 insert_breakpoints. */
3992 if (w->val)
3993 value_free (w->val);
3994 w->val = NULL;
3995 w->val_valid = 0;
3996 }
3997 }
3a5c3e22 3998 }
c5aa993b
JM
3999 break;
4000 default:
c5aa993b
JM
4001 break;
4002 }
4003 }
1c5cfe86
PA
4004
4005 /* Get rid of the moribund locations. */
35df4500
TJB
4006 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4007 decref_bp_location (&bl);
1c5cfe86 4008 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4009}
4010
6c95b8df
PA
4011/* These functions concern about actual breakpoints inserted in the
4012 target --- to e.g. check if we need to do decr_pc adjustment or if
4013 we need to hop over the bkpt --- so we check for address space
4014 match, not program space. */
4015
c2c6d25f
JM
4016/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4017 exists at PC. It returns ordinary_breakpoint_here if it's an
4018 ordinary breakpoint, or permanent_breakpoint_here if it's a
4019 permanent breakpoint.
4020 - When continuing from a location with an ordinary breakpoint, we
4021 actually single step once before calling insert_breakpoints.
e5dd4106 4022 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4023 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4024 the target, to advance the PC past the breakpoint. */
c906108c 4025
c2c6d25f 4026enum breakpoint_here
accd0bcd 4027breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4028{
35df4500 4029 struct bp_location *bl, **blp_tmp;
c2c6d25f 4030 int any_breakpoint_here = 0;
c906108c 4031
35df4500 4032 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4033 {
35df4500
TJB
4034 if (bl->loc_type != bp_loc_software_breakpoint
4035 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4036 continue;
4037
f1310107 4038 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4039 if ((breakpoint_enabled (bl->owner)
1a853c52 4040 || bl->permanent)
f1310107 4041 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4042 {
4043 if (overlay_debugging
35df4500
TJB
4044 && section_is_overlay (bl->section)
4045 && !section_is_mapped (bl->section))
075f6582 4046 continue; /* unmapped overlay -- can't be a match */
1a853c52 4047 else if (bl->permanent)
075f6582
DJ
4048 return permanent_breakpoint_here;
4049 else
4050 any_breakpoint_here = 1;
4051 }
4052 }
c906108c 4053
f486487f 4054 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4055}
4056
d35ae833
PA
4057/* See breakpoint.h. */
4058
4059int
accd0bcd 4060breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4061 CORE_ADDR addr, ULONGEST len)
4062{
4063 struct bp_location *bl, **blp_tmp;
4064
4065 ALL_BP_LOCATIONS (bl, blp_tmp)
4066 {
4067 if (bl->loc_type != bp_loc_software_breakpoint
4068 && bl->loc_type != bp_loc_hardware_breakpoint)
4069 continue;
4070
4071 if ((breakpoint_enabled (bl->owner)
4072 || bl->permanent)
4073 && breakpoint_location_address_range_overlap (bl, aspace,
4074 addr, len))
4075 {
4076 if (overlay_debugging
4077 && section_is_overlay (bl->section)
4078 && !section_is_mapped (bl->section))
4079 {
4080 /* Unmapped overlay -- can't be a match. */
4081 continue;
4082 }
4083
4084 return 1;
4085 }
4086 }
4087
4088 return 0;
4089}
4090
1c5cfe86
PA
4091/* Return true if there's a moribund breakpoint at PC. */
4092
4093int
accd0bcd 4094moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4095{
4096 struct bp_location *loc;
4097 int ix;
4098
4099 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4100 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4101 return 1;
4102
4103 return 0;
4104}
c2c6d25f 4105
f7ce857f
PA
4106/* Returns non-zero iff BL is inserted at PC, in address space
4107 ASPACE. */
4108
4109static int
4110bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4111 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4112{
4113 if (bl->inserted
4114 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4115 aspace, pc))
4116 {
4117 if (overlay_debugging
4118 && section_is_overlay (bl->section)
4119 && !section_is_mapped (bl->section))
4120 return 0; /* unmapped overlay -- can't be a match */
4121 else
4122 return 1;
4123 }
4124 return 0;
4125}
4126
a1fd2fa5 4127/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4128
4129int
accd0bcd 4130breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4131{
f7ce857f 4132 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4133
f7ce857f 4134 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4135 {
f7ce857f
PA
4136 struct bp_location *bl = *blp;
4137
35df4500
TJB
4138 if (bl->loc_type != bp_loc_software_breakpoint
4139 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4140 continue;
4141
f7ce857f
PA
4142 if (bp_location_inserted_here_p (bl, aspace, pc))
4143 return 1;
c5aa993b 4144 }
c36b740a
VP
4145 return 0;
4146}
4147
a1fd2fa5
PA
4148/* This function returns non-zero iff there is a software breakpoint
4149 inserted at PC. */
c36b740a
VP
4150
4151int
accd0bcd 4152software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4153 CORE_ADDR pc)
4fa8626c 4154{
f7ce857f 4155 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4156
f7ce857f 4157 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4158 {
f7ce857f
PA
4159 struct bp_location *bl = *blp;
4160
35df4500 4161 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4162 continue;
4163
f7ce857f
PA
4164 if (bp_location_inserted_here_p (bl, aspace, pc))
4165 return 1;
4fa8626c
DJ
4166 }
4167
4168 return 0;
9c02b525
PA
4169}
4170
4171/* See breakpoint.h. */
4172
4173int
accd0bcd 4174hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4175 CORE_ADDR pc)
4176{
4177 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4178
4179 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4180 {
4181 struct bp_location *bl = *blp;
4182
4183 if (bl->loc_type != bp_loc_hardware_breakpoint)
4184 continue;
4185
4186 if (bp_location_inserted_here_p (bl, aspace, pc))
4187 return 1;
4188 }
4189
4190 return 0;
4fa8626c
DJ
4191}
4192
9093389c 4193int
accd0bcd 4194hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4195 CORE_ADDR addr, ULONGEST len)
4196{
4197 struct breakpoint *bpt;
4198
4199 ALL_BREAKPOINTS (bpt)
4200 {
4201 struct bp_location *loc;
4202
4203 if (bpt->type != bp_hardware_watchpoint
4204 && bpt->type != bp_access_watchpoint)
4205 continue;
4206
4207 if (!breakpoint_enabled (bpt))
4208 continue;
4209
4210 for (loc = bpt->loc; loc; loc = loc->next)
4211 if (loc->pspace->aspace == aspace && loc->inserted)
4212 {
4213 CORE_ADDR l, h;
4214
4215 /* Check for intersection. */
768adc05
PA
4216 l = std::max<CORE_ADDR> (loc->address, addr);
4217 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4218 if (l < h)
4219 return 1;
4220 }
4221 }
4222 return 0;
4223}
c906108c 4224\f
c5aa993b 4225
c906108c
SS
4226/* bpstat stuff. External routines' interfaces are documented
4227 in breakpoint.h. */
4228
4229int
c326b90e 4230is_catchpoint (struct breakpoint *ep)
c906108c 4231{
533be4dd 4232 return (ep->type == bp_catchpoint);
c906108c
SS
4233}
4234
f431efe5
PA
4235/* Frees any storage that is part of a bpstat. Does not walk the
4236 'next' chain. */
4237
04afa70c 4238bpstats::~bpstats ()
198757a8 4239{
04afa70c
TT
4240 if (old_val != NULL)
4241 value_free (old_val);
04afa70c
TT
4242 if (bp_location_at != NULL)
4243 decref_bp_location (&bp_location_at);
198757a8
VP
4244}
4245
c906108c
SS
4246/* Clear a bpstat so that it says we are not at any breakpoint.
4247 Also free any storage that is part of a bpstat. */
4248
4249void
fba45db2 4250bpstat_clear (bpstat *bsp)
c906108c
SS
4251{
4252 bpstat p;
4253 bpstat q;
4254
4255 if (bsp == 0)
4256 return;
4257 p = *bsp;
4258 while (p != NULL)
4259 {
4260 q = p->next;
04afa70c 4261 delete p;
c906108c
SS
4262 p = q;
4263 }
4264 *bsp = NULL;
4265}
4266
04afa70c
TT
4267bpstats::bpstats (const bpstats &other)
4268 : next (NULL),
4269 bp_location_at (other.bp_location_at),
4270 breakpoint_at (other.breakpoint_at),
4271 commands (other.commands),
4272 old_val (other.old_val),
4273 print (other.print),
4274 stop (other.stop),
4275 print_it (other.print_it)
4276{
4277 if (old_val != NULL)
4278 {
4279 old_val = value_copy (old_val);
4280 release_value (old_val);
4281 }
4282 incref_bp_location (bp_location_at);
04afa70c
TT
4283}
4284
c906108c
SS
4285/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4286 is part of the bpstat is copied as well. */
4287
4288bpstat
fba45db2 4289bpstat_copy (bpstat bs)
c906108c
SS
4290{
4291 bpstat p = NULL;
4292 bpstat tmp;
4293 bpstat retval = NULL;
4294
4295 if (bs == NULL)
4296 return bs;
4297
4298 for (; bs != NULL; bs = bs->next)
4299 {
04afa70c 4300 tmp = new bpstats (*bs);
31cc81e9 4301
c906108c
SS
4302 if (p == NULL)
4303 /* This is the first thing in the chain. */
4304 retval = tmp;
4305 else
4306 p->next = tmp;
4307 p = tmp;
4308 }
4309 p->next = NULL;
4310 return retval;
4311}
4312
4a64f543 4313/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4314
4315bpstat
fba45db2 4316bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4317{
c5aa993b
JM
4318 if (bsp == NULL)
4319 return NULL;
c906108c 4320
c5aa993b
JM
4321 for (; bsp != NULL; bsp = bsp->next)
4322 {
f431efe5 4323 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4324 return bsp;
4325 }
c906108c
SS
4326 return NULL;
4327}
4328
ab04a2af
TT
4329/* See breakpoint.h. */
4330
47591c29 4331int
427cd150 4332bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4333{
ab04a2af
TT
4334 for (; bsp != NULL; bsp = bsp->next)
4335 {
427cd150
TT
4336 if (bsp->breakpoint_at == NULL)
4337 {
4338 /* A moribund location can never explain a signal other than
4339 GDB_SIGNAL_TRAP. */
4340 if (sig == GDB_SIGNAL_TRAP)
47591c29 4341 return 1;
427cd150
TT
4342 }
4343 else
47591c29
PA
4344 {
4345 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4346 sig))
4347 return 1;
4348 }
ab04a2af
TT
4349 }
4350
47591c29 4351 return 0;
ab04a2af
TT
4352}
4353
4a64f543
MS
4354/* Put in *NUM the breakpoint number of the first breakpoint we are
4355 stopped at. *BSP upon return is a bpstat which points to the
4356 remaining breakpoints stopped at (but which is not guaranteed to be
4357 good for anything but further calls to bpstat_num).
4358
8671a17b
PA
4359 Return 0 if passed a bpstat which does not indicate any breakpoints.
4360 Return -1 if stopped at a breakpoint that has been deleted since
4361 we set it.
4362 Return 1 otherwise. */
c906108c
SS
4363
4364int
8671a17b 4365bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4366{
4367 struct breakpoint *b;
4368
4369 if ((*bsp) == NULL)
4370 return 0; /* No more breakpoint values */
8671a17b 4371
4a64f543
MS
4372 /* We assume we'll never have several bpstats that correspond to a
4373 single breakpoint -- otherwise, this function might return the
4374 same number more than once and this will look ugly. */
f431efe5 4375 b = (*bsp)->breakpoint_at;
8671a17b
PA
4376 *bsp = (*bsp)->next;
4377 if (b == NULL)
4378 return -1; /* breakpoint that's been deleted since */
4379
4380 *num = b->number; /* We have its number */
4381 return 1;
c906108c
SS
4382}
4383
e93ca019 4384/* See breakpoint.h. */
c906108c
SS
4385
4386void
e93ca019 4387bpstat_clear_actions (void)
c906108c 4388{
e93ca019
JK
4389 struct thread_info *tp;
4390 bpstat bs;
4391
4392 if (ptid_equal (inferior_ptid, null_ptid))
4393 return;
4394
4395 tp = find_thread_ptid (inferior_ptid);
4396 if (tp == NULL)
4397 return;
4398
4399 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4400 {
d1b0a7bf 4401 bs->commands = NULL;
abf85f46 4402
c906108c
SS
4403 if (bs->old_val != NULL)
4404 {
4405 value_free (bs->old_val);
4406 bs->old_val = NULL;
4407 }
4408 }
4409}
4410
f3b1572e
PA
4411/* Called when a command is about to proceed the inferior. */
4412
4413static void
4414breakpoint_about_to_proceed (void)
4415{
4416 if (!ptid_equal (inferior_ptid, null_ptid))
4417 {
4418 struct thread_info *tp = inferior_thread ();
4419
4420 /* Allow inferior function calls in breakpoint commands to not
4421 interrupt the command list. When the call finishes
4422 successfully, the inferior will be standing at the same
4423 breakpoint as if nothing happened. */
16c381f0 4424 if (tp->control.in_infcall)
f3b1572e
PA
4425 return;
4426 }
4427
4428 breakpoint_proceeded = 1;
4429}
4430
abf85f46
JK
4431/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4432 or its equivalent. */
4433
4434static int
4435command_line_is_silent (struct command_line *cmd)
4436{
4f45d445 4437 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4438}
4439
4a64f543
MS
4440/* Execute all the commands associated with all the breakpoints at
4441 this location. Any of these commands could cause the process to
4442 proceed beyond this point, etc. We look out for such changes by
4443 checking the global "breakpoint_proceeded" after each command.
c906108c 4444
347bddb7
PA
4445 Returns true if a breakpoint command resumed the inferior. In that
4446 case, it is the caller's responsibility to recall it again with the
4447 bpstat of the current thread. */
4448
4449static int
4450bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4451{
4452 bpstat bs;
347bddb7 4453 int again = 0;
c906108c
SS
4454
4455 /* Avoid endless recursion if a `source' command is contained
4456 in bs->commands. */
4457 if (executing_breakpoint_commands)
347bddb7 4458 return 0;
c906108c 4459
81b1e71c
TT
4460 scoped_restore save_executing
4461 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4462
1ac32117 4463 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4464
4a64f543 4465 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4466 bs = *bsp;
4467
4468 breakpoint_proceeded = 0;
4469 for (; bs != NULL; bs = bs->next)
4470 {
d1b0a7bf 4471 struct command_line *cmd = NULL;
6c50ab1c
JB
4472
4473 /* Take ownership of the BSP's command tree, if it has one.
4474
4475 The command tree could legitimately contain commands like
4476 'step' and 'next', which call clear_proceed_status, which
4477 frees stop_bpstat's command tree. To make sure this doesn't
4478 free the tree we're executing out from under us, we need to
4479 take ownership of the tree ourselves. Since a given bpstat's
4480 commands are only executed once, we don't need to copy it; we
4481 can clear the pointer in the bpstat, and make sure we free
4482 the tree when we're done. */
d1b0a7bf 4483 counted_command_line ccmd = bs->commands;
9add0f1b 4484 bs->commands = NULL;
d1b0a7bf
TT
4485 if (ccmd != NULL)
4486 cmd = ccmd.get ();
abf85f46
JK
4487 if (command_line_is_silent (cmd))
4488 {
4489 /* The action has been already done by bpstat_stop_status. */
4490 cmd = cmd->next;
4491 }
6c50ab1c 4492
c906108c
SS
4493 while (cmd != NULL)
4494 {
4495 execute_control_command (cmd);
4496
4497 if (breakpoint_proceeded)
4498 break;
4499 else
4500 cmd = cmd->next;
4501 }
6c50ab1c 4502
c906108c 4503 if (breakpoint_proceeded)
32c1e744 4504 {
cb814510 4505 if (current_ui->async)
347bddb7
PA
4506 /* If we are in async mode, then the target might be still
4507 running, not stopped at any breakpoint, so nothing for
4508 us to do here -- just return to the event loop. */
4509 ;
32c1e744
VP
4510 else
4511 /* In sync mode, when execute_control_command returns
4512 we're already standing on the next breakpoint.
347bddb7
PA
4513 Breakpoint commands for that stop were not run, since
4514 execute_command does not run breakpoint commands --
4515 only command_line_handler does, but that one is not
4516 involved in execution of breakpoint commands. So, we
4517 can now execute breakpoint commands. It should be
4518 noted that making execute_command do bpstat actions is
4519 not an option -- in this case we'll have recursive
4520 invocation of bpstat for each breakpoint with a
4521 command, and can easily blow up GDB stack. Instead, we
4522 return true, which will trigger the caller to recall us
4523 with the new stop_bpstat. */
4524 again = 1;
4525 break;
32c1e744 4526 }
c906108c 4527 }
347bddb7
PA
4528 return again;
4529}
4530
4531void
4532bpstat_do_actions (void)
4533{
353d1d73
JK
4534 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4535
347bddb7
PA
4536 /* Do any commands attached to breakpoint we are stopped at. */
4537 while (!ptid_equal (inferior_ptid, null_ptid)
4538 && target_has_execution
4539 && !is_exited (inferior_ptid)
4540 && !is_executing (inferior_ptid))
4541 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4542 and only return when it is stopped at the next breakpoint, we
4543 keep doing breakpoint actions until it returns false to
4544 indicate the inferior was not resumed. */
16c381f0 4545 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4546 break;
353d1d73
JK
4547
4548 discard_cleanups (cleanup_if_error);
c906108c
SS
4549}
4550
fa4727a6
DJ
4551/* Print out the (old or new) value associated with a watchpoint. */
4552
4553static void
4554watchpoint_value_print (struct value *val, struct ui_file *stream)
4555{
4556 if (val == NULL)
4557 fprintf_unfiltered (stream, _("<unreadable>"));
4558 else
79a45b7d
TT
4559 {
4560 struct value_print_options opts;
4561 get_user_print_options (&opts);
4562 value_print (val, stream, &opts);
4563 }
fa4727a6
DJ
4564}
4565
f303dbd6
PA
4566/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4567 debugging multiple threads. */
4568
4569void
4570maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4571{
112e8700 4572 if (uiout->is_mi_like_p ())
f303dbd6
PA
4573 return;
4574
112e8700 4575 uiout->text ("\n");
f303dbd6
PA
4576
4577 if (show_thread_that_caused_stop ())
4578 {
4579 const char *name;
4580 struct thread_info *thr = inferior_thread ();
4581
112e8700
SM
4582 uiout->text ("Thread ");
4583 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4584
4585 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4586 if (name != NULL)
4587 {
112e8700
SM
4588 uiout->text (" \"");
4589 uiout->field_fmt ("name", "%s", name);
4590 uiout->text ("\"");
f303dbd6
PA
4591 }
4592
112e8700 4593 uiout->text (" hit ");
f303dbd6
PA
4594 }
4595}
4596
e514a9d6 4597/* Generic routine for printing messages indicating why we
4a64f543 4598 stopped. The behavior of this function depends on the value
e514a9d6
JM
4599 'print_it' in the bpstat structure. Under some circumstances we
4600 may decide not to print anything here and delegate the task to
4a64f543 4601 normal_stop(). */
e514a9d6
JM
4602
4603static enum print_stop_action
4604print_bp_stop_message (bpstat bs)
4605{
4606 switch (bs->print_it)
4607 {
4608 case print_it_noop:
4a64f543 4609 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4610 return PRINT_UNKNOWN;
4611 break;
4612
4613 case print_it_done:
4614 /* We still want to print the frame, but we already printed the
4a64f543 4615 relevant messages. */
e514a9d6
JM
4616 return PRINT_SRC_AND_LOC;
4617 break;
4618
4619 case print_it_normal:
4f8d1dc6 4620 {
f431efe5
PA
4621 struct breakpoint *b = bs->breakpoint_at;
4622
1a6a67de
TJB
4623 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4624 which has since been deleted. */
4625 if (b == NULL)
4626 return PRINT_UNKNOWN;
4627
348d480f
PA
4628 /* Normal case. Call the breakpoint's print_it method. */
4629 return b->ops->print_it (bs);
4f8d1dc6 4630 }
348d480f 4631 break;
3086aeae 4632
e514a9d6 4633 default:
8e65ff28 4634 internal_error (__FILE__, __LINE__,
e2e0b3e5 4635 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4636 break;
c906108c 4637 }
c906108c
SS
4638}
4639
edcc5120
TT
4640/* A helper function that prints a shared library stopped event. */
4641
4642static void
4643print_solib_event (int is_catchpoint)
4644{
4645 int any_deleted
4646 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4647 int any_added
4648 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4649
4650 if (!is_catchpoint)
4651 {
4652 if (any_added || any_deleted)
112e8700 4653 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4654 else
112e8700
SM
4655 current_uiout->text (_("Stopped due to shared library event (no "
4656 "libraries added or removed)\n"));
edcc5120
TT
4657 }
4658
112e8700
SM
4659 if (current_uiout->is_mi_like_p ())
4660 current_uiout->field_string ("reason",
4661 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4662
4663 if (any_deleted)
4664 {
edcc5120
TT
4665 char *name;
4666 int ix;
4667
112e8700 4668 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4669 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4670 for (ix = 0;
4671 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4672 ix, name);
4673 ++ix)
4674 {
4675 if (ix > 0)
112e8700
SM
4676 current_uiout->text (" ");
4677 current_uiout->field_string ("library", name);
4678 current_uiout->text ("\n");
edcc5120 4679 }
edcc5120
TT
4680 }
4681
4682 if (any_added)
4683 {
4684 struct so_list *iter;
4685 int ix;
edcc5120 4686
112e8700 4687 current_uiout->text (_(" Inferior loaded "));
10f489e5 4688 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4689 for (ix = 0;
4690 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4691 ix, iter);
4692 ++ix)
4693 {
4694 if (ix > 0)
112e8700
SM
4695 current_uiout->text (" ");
4696 current_uiout->field_string ("library", iter->so_name);
4697 current_uiout->text ("\n");
edcc5120 4698 }
edcc5120
TT
4699 }
4700}
4701
e514a9d6
JM
4702/* Print a message indicating what happened. This is called from
4703 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4704 list - a list of the eventpoints that caused this stop. KIND is
4705 the target_waitkind for the stopping event. This
e514a9d6
JM
4706 routine calls the generic print routine for printing a message
4707 about reasons for stopping. This will print (for example) the
4708 "Breakpoint n," part of the output. The return value of this
4709 routine is one of:
c906108c 4710
4a64f543 4711 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4712 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4713 code to print the location. An example is
c5aa993b
JM
4714 "Breakpoint 1, " which should be followed by
4715 the location.
917317f4 4716 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4717 to also print the location part of the message.
4718 An example is the catch/throw messages, which
4a64f543 4719 don't require a location appended to the end.
917317f4 4720 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4721 further info to be printed. */
c906108c 4722
917317f4 4723enum print_stop_action
36dfb11c 4724bpstat_print (bpstat bs, int kind)
c906108c 4725{
f486487f 4726 enum print_stop_action val;
c5aa993b 4727
c906108c 4728 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4729 (Currently all watchpoints go on the bpstat whether hit or not.
4730 That probably could (should) be changed, provided care is taken
c906108c 4731 with respect to bpstat_explains_signal). */
e514a9d6
JM
4732 for (; bs; bs = bs->next)
4733 {
4734 val = print_bp_stop_message (bs);
4735 if (val == PRINT_SRC_ONLY
4736 || val == PRINT_SRC_AND_LOC
4737 || val == PRINT_NOTHING)
4738 return val;
4739 }
c906108c 4740
36dfb11c
TT
4741 /* If we had hit a shared library event breakpoint,
4742 print_bp_stop_message would print out this message. If we hit an
4743 OS-level shared library event, do the same thing. */
4744 if (kind == TARGET_WAITKIND_LOADED)
4745 {
edcc5120 4746 print_solib_event (0);
36dfb11c
TT
4747 return PRINT_NOTHING;
4748 }
4749
e514a9d6 4750 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4751 with and nothing was printed. */
917317f4 4752 return PRINT_UNKNOWN;
c906108c
SS
4753}
4754
bf469271 4755/* Evaluate the boolean expression EXP and return the result. */
c906108c 4756
bf469271
PA
4757static bool
4758breakpoint_cond_eval (expression *exp)
c906108c 4759{
278cd55f 4760 struct value *mark = value_mark ();
bf469271 4761 bool res = value_true (evaluate_expression (exp));
cc59ec59 4762
c906108c 4763 value_free_to_mark (mark);
bf469271 4764 return res;
c906108c
SS
4765}
4766
5760d0ab 4767/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4768
04afa70c
TT
4769bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4770 : next (NULL),
4771 bp_location_at (bl),
4772 breakpoint_at (bl->owner),
4773 commands (NULL),
4774 old_val (NULL),
4775 print (0),
4776 stop (0),
4777 print_it (print_it_normal)
c906108c 4778{
f431efe5 4779 incref_bp_location (bl);
04afa70c
TT
4780 **bs_link_pointer = this;
4781 *bs_link_pointer = &next;
4782}
4783
4784bpstats::bpstats ()
4785 : next (NULL),
4786 bp_location_at (NULL),
4787 breakpoint_at (NULL),
4788 commands (NULL),
4789 old_val (NULL),
4790 print (0),
4791 stop (0),
4792 print_it (print_it_normal)
4793{
c906108c
SS
4794}
4795\f
d983da9c
DJ
4796/* The target has stopped with waitstatus WS. Check if any hardware
4797 watchpoints have triggered, according to the target. */
4798
4799int
4800watchpoints_triggered (struct target_waitstatus *ws)
4801{
d92524f1 4802 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4803 CORE_ADDR addr;
4804 struct breakpoint *b;
4805
4806 if (!stopped_by_watchpoint)
4807 {
4808 /* We were not stopped by a watchpoint. Mark all watchpoints
4809 as not triggered. */
4810 ALL_BREAKPOINTS (b)
cc60f2e3 4811 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4812 {
4813 struct watchpoint *w = (struct watchpoint *) b;
4814
4815 w->watchpoint_triggered = watch_triggered_no;
4816 }
d983da9c
DJ
4817
4818 return 0;
4819 }
4820
4821 if (!target_stopped_data_address (&current_target, &addr))
4822 {
4823 /* We were stopped by a watchpoint, but we don't know where.
4824 Mark all watchpoints as unknown. */
4825 ALL_BREAKPOINTS (b)
cc60f2e3 4826 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4827 {
4828 struct watchpoint *w = (struct watchpoint *) b;
4829
4830 w->watchpoint_triggered = watch_triggered_unknown;
4831 }
d983da9c 4832
3c4797ba 4833 return 1;
d983da9c
DJ
4834 }
4835
4836 /* The target could report the data address. Mark watchpoints
4837 affected by this data address as triggered, and all others as not
4838 triggered. */
4839
4840 ALL_BREAKPOINTS (b)
cc60f2e3 4841 if (is_hardware_watchpoint (b))
d983da9c 4842 {
3a5c3e22 4843 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4844 struct bp_location *loc;
d983da9c 4845
3a5c3e22 4846 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4847 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4848 {
3a5c3e22 4849 if (is_masked_watchpoint (b))
9c06b0b4 4850 {
3a5c3e22
PA
4851 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4852 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4853
4854 if (newaddr == start)
4855 {
3a5c3e22 4856 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4857 break;
4858 }
4859 }
4860 /* Exact match not required. Within range is sufficient. */
4861 else if (target_watchpoint_addr_within_range (&current_target,
4862 addr, loc->address,
4863 loc->length))
4864 {
3a5c3e22 4865 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4866 break;
4867 }
4868 }
d983da9c
DJ
4869 }
4870
4871 return 1;
4872}
4873
bf469271
PA
4874/* Possible return values for watchpoint_check. */
4875enum wp_check_result
4876 {
4877 /* The watchpoint has been deleted. */
4878 WP_DELETED = 1,
4879
4880 /* The value has changed. */
4881 WP_VALUE_CHANGED = 2,
4882
4883 /* The value has not changed. */
4884 WP_VALUE_NOT_CHANGED = 3,
4885
4886 /* Ignore this watchpoint, no matter if the value changed or not. */
4887 WP_IGNORE = 4,
4888 };
c906108c
SS
4889
4890#define BP_TEMPFLAG 1
4891#define BP_HARDWAREFLAG 2
4892
4a64f543 4893/* Evaluate watchpoint condition expression and check if its value
bf469271 4894 changed. */
553e4c11 4895
bf469271
PA
4896static wp_check_result
4897watchpoint_check (bpstat bs)
c906108c 4898{
3a5c3e22 4899 struct watchpoint *b;
c906108c
SS
4900 struct frame_info *fr;
4901 int within_current_scope;
4902
f431efe5 4903 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4904 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4905 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4906
f6bc2008
PA
4907 /* If this is a local watchpoint, we only want to check if the
4908 watchpoint frame is in scope if the current thread is the thread
4909 that was used to create the watchpoint. */
4910 if (!watchpoint_in_thread_scope (b))
60e1c644 4911 return WP_IGNORE;
f6bc2008 4912
c906108c
SS
4913 if (b->exp_valid_block == NULL)
4914 within_current_scope = 1;
4915 else
4916 {
edb3359d
DJ
4917 struct frame_info *frame = get_current_frame ();
4918 struct gdbarch *frame_arch = get_frame_arch (frame);
4919 CORE_ADDR frame_pc = get_frame_pc (frame);
4920
c9cf6e20 4921 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4922 still in the function but the stack frame has already been
4923 invalidated. Since we can't rely on the values of local
4924 variables after the stack has been destroyed, we are treating
4925 the watchpoint in that state as `not changed' without further
4926 checking. Don't mark watchpoints as changed if the current
4927 frame is in an epilogue - even if they are in some other
4928 frame, our view of the stack is likely to be wrong and
4929 frame_find_by_id could error out. */
c9cf6e20 4930 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4931 return WP_IGNORE;
a0f49112 4932
101dcfbe 4933 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4934 within_current_scope = (fr != NULL);
69fbadd5
DJ
4935
4936 /* If we've gotten confused in the unwinder, we might have
4937 returned a frame that can't describe this variable. */
edb3359d
DJ
4938 if (within_current_scope)
4939 {
4940 struct symbol *function;
4941
4942 function = get_frame_function (fr);
4943 if (function == NULL
4944 || !contained_in (b->exp_valid_block,
4945 SYMBOL_BLOCK_VALUE (function)))
4946 within_current_scope = 0;
4947 }
69fbadd5 4948
edb3359d 4949 if (within_current_scope)
c906108c
SS
4950 /* If we end up stopping, the current frame will get selected
4951 in normal_stop. So this call to select_frame won't affect
4952 the user. */
0f7d239c 4953 select_frame (fr);
c906108c 4954 }
c5aa993b 4955
c906108c
SS
4956 if (within_current_scope)
4957 {
4a64f543
MS
4958 /* We use value_{,free_to_}mark because it could be a *long*
4959 time before we return to the command level and call
4960 free_all_values. We can't call free_all_values because we
4961 might be in the middle of evaluating a function call. */
c906108c 4962
0cf6dd15 4963 int pc = 0;
9c06b0b4 4964 struct value *mark;
fa4727a6
DJ
4965 struct value *new_val;
4966
c1fc2657 4967 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4968 /* Since we don't know the exact trigger address (from
4969 stopped_data_address), just tell the user we've triggered
4970 a mask watchpoint. */
4971 return WP_VALUE_CHANGED;
4972
4973 mark = value_mark ();
4d01a485 4974 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4975
bb9d5f81
PP
4976 if (b->val_bitsize != 0)
4977 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4978
4a64f543
MS
4979 /* We use value_equal_contents instead of value_equal because
4980 the latter coerces an array to a pointer, thus comparing just
4981 the address of the array instead of its contents. This is
4982 not what we want. */
fa4727a6 4983 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4984 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4985 {
fa4727a6
DJ
4986 if (new_val != NULL)
4987 {
4988 release_value (new_val);
4989 value_free_to_mark (mark);
4990 }
c906108c
SS
4991 bs->old_val = b->val;
4992 b->val = new_val;
fa4727a6 4993 b->val_valid = 1;
c906108c
SS
4994 return WP_VALUE_CHANGED;
4995 }
4996 else
4997 {
60e1c644 4998 /* Nothing changed. */
c906108c 4999 value_free_to_mark (mark);
c906108c
SS
5000 return WP_VALUE_NOT_CHANGED;
5001 }
5002 }
5003 else
5004 {
5005 /* This seems like the only logical thing to do because
c5aa993b
JM
5006 if we temporarily ignored the watchpoint, then when
5007 we reenter the block in which it is valid it contains
5008 garbage (in the case of a function, it may have two
5009 garbage values, one before and one after the prologue).
5010 So we can't even detect the first assignment to it and
5011 watch after that (since the garbage may or may not equal
5012 the first value assigned). */
348d480f
PA
5013 /* We print all the stop information in
5014 breakpoint_ops->print_it, but in this case, by the time we
5015 call breakpoint_ops->print_it this bp will be deleted
5016 already. So we have no choice but print the information
5017 here. */
468afe6c 5018
0e454242 5019 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5020 {
5021 struct ui_out *uiout = current_uiout;
5022
112e8700
SM
5023 if (uiout->is_mi_like_p ())
5024 uiout->field_string
5025 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5026 uiout->text ("\nWatchpoint ");
c1fc2657 5027 uiout->field_int ("wpnum", b->number);
112e8700 5028 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5029 "which its expression is valid.\n");
5030 }
4ce44c66 5031
cdac0397 5032 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5033 b->commands = NULL;
d0fb5eae 5034 watchpoint_del_at_next_stop (b);
c906108c
SS
5035
5036 return WP_DELETED;
5037 }
5038}
5039
18a18393 5040/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5041 breakpoint location BL. This function does not check if we should
5042 stop, only if BL explains the stop. */
5043
18a18393 5044static int
6c95b8df 5045bpstat_check_location (const struct bp_location *bl,
accd0bcd 5046 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5047 const struct target_waitstatus *ws)
18a18393
VP
5048{
5049 struct breakpoint *b = bl->owner;
5050
348d480f 5051 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5052 gdb_assert (b != NULL);
5053
bd522513 5054 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5055}
5056
3a5c3e22
PA
5057/* Determine if the watched values have actually changed, and we
5058 should stop. If not, set BS->stop to 0. */
5059
18a18393
VP
5060static void
5061bpstat_check_watchpoint (bpstat bs)
5062{
2bdf28a0 5063 const struct bp_location *bl;
3a5c3e22 5064 struct watchpoint *b;
2bdf28a0
JK
5065
5066 /* BS is built for existing struct breakpoint. */
f431efe5 5067 bl = bs->bp_location_at;
2bdf28a0 5068 gdb_assert (bl != NULL);
3a5c3e22 5069 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5070 gdb_assert (b != NULL);
18a18393 5071
18a18393 5072 {
18a18393
VP
5073 int must_check_value = 0;
5074
c1fc2657 5075 if (b->type == bp_watchpoint)
18a18393
VP
5076 /* For a software watchpoint, we must always check the
5077 watched value. */
5078 must_check_value = 1;
5079 else if (b->watchpoint_triggered == watch_triggered_yes)
5080 /* We have a hardware watchpoint (read, write, or access)
5081 and the target earlier reported an address watched by
5082 this watchpoint. */
5083 must_check_value = 1;
5084 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5085 && b->type == bp_hardware_watchpoint)
18a18393
VP
5086 /* We were stopped by a hardware watchpoint, but the target could
5087 not report the data address. We must check the watchpoint's
5088 value. Access and read watchpoints are out of luck; without
5089 a data address, we can't figure it out. */
5090 must_check_value = 1;
3a5c3e22 5091
18a18393
VP
5092 if (must_check_value)
5093 {
bf469271
PA
5094 wp_check_result e;
5095
5096 TRY
5097 {
5098 e = watchpoint_check (bs);
5099 }
5100 CATCH (ex, RETURN_MASK_ALL)
5101 {
5102 exception_fprintf (gdb_stderr, ex,
5103 "Error evaluating expression "
5104 "for watchpoint %d\n",
5105 b->number);
5106
5107 SWITCH_THRU_ALL_UIS ()
5108 {
5109 printf_filtered (_("Watchpoint %d deleted.\n"),
5110 b->number);
5111 }
5112 watchpoint_del_at_next_stop (b);
5113 e = WP_DELETED;
5114 }
5115 END_CATCH
5116
18a18393
VP
5117 switch (e)
5118 {
5119 case WP_DELETED:
5120 /* We've already printed what needs to be printed. */
5121 bs->print_it = print_it_done;
5122 /* Stop. */
5123 break;
60e1c644
PA
5124 case WP_IGNORE:
5125 bs->print_it = print_it_noop;
5126 bs->stop = 0;
5127 break;
18a18393 5128 case WP_VALUE_CHANGED:
c1fc2657 5129 if (b->type == bp_read_watchpoint)
18a18393 5130 {
85d721b8
PA
5131 /* There are two cases to consider here:
5132
4a64f543 5133 1. We're watching the triggered memory for reads.
85d721b8
PA
5134 In that case, trust the target, and always report
5135 the watchpoint hit to the user. Even though
5136 reads don't cause value changes, the value may
5137 have changed since the last time it was read, and
5138 since we're not trapping writes, we will not see
5139 those, and as such we should ignore our notion of
5140 old value.
5141
4a64f543 5142 2. We're watching the triggered memory for both
85d721b8
PA
5143 reads and writes. There are two ways this may
5144 happen:
5145
4a64f543 5146 2.1. This is a target that can't break on data
85d721b8
PA
5147 reads only, but can break on accesses (reads or
5148 writes), such as e.g., x86. We detect this case
5149 at the time we try to insert read watchpoints.
5150
4a64f543 5151 2.2. Otherwise, the target supports read
85d721b8
PA
5152 watchpoints, but, the user set an access or write
5153 watchpoint watching the same memory as this read
5154 watchpoint.
5155
5156 If we're watching memory writes as well as reads,
5157 ignore watchpoint hits when we find that the
5158 value hasn't changed, as reads don't cause
5159 changes. This still gives false positives when
5160 the program writes the same value to memory as
5161 what there was already in memory (we will confuse
5162 it for a read), but it's much better than
5163 nothing. */
5164
5165 int other_write_watchpoint = 0;
5166
5167 if (bl->watchpoint_type == hw_read)
5168 {
5169 struct breakpoint *other_b;
5170
5171 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5172 if (other_b->type == bp_hardware_watchpoint
5173 || other_b->type == bp_access_watchpoint)
85d721b8 5174 {
3a5c3e22
PA
5175 struct watchpoint *other_w =
5176 (struct watchpoint *) other_b;
5177
5178 if (other_w->watchpoint_triggered
5179 == watch_triggered_yes)
5180 {
5181 other_write_watchpoint = 1;
5182 break;
5183 }
85d721b8
PA
5184 }
5185 }
5186
5187 if (other_write_watchpoint
5188 || bl->watchpoint_type == hw_access)
5189 {
5190 /* We're watching the same memory for writes,
5191 and the value changed since the last time we
5192 updated it, so this trap must be for a write.
5193 Ignore it. */
5194 bs->print_it = print_it_noop;
5195 bs->stop = 0;
5196 }
18a18393
VP
5197 }
5198 break;
5199 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5200 if (b->type == bp_hardware_watchpoint
5201 || b->type == bp_watchpoint)
18a18393
VP
5202 {
5203 /* Don't stop: write watchpoints shouldn't fire if
5204 the value hasn't changed. */
5205 bs->print_it = print_it_noop;
5206 bs->stop = 0;
5207 }
5208 /* Stop. */
5209 break;
5210 default:
5211 /* Can't happen. */
18a18393
VP
5212 break;
5213 }
5214 }
5215 else /* must_check_value == 0 */
5216 {
5217 /* This is a case where some watchpoint(s) triggered, but
5218 not at the address of this watchpoint, or else no
5219 watchpoint triggered after all. So don't print
5220 anything for this watchpoint. */
5221 bs->print_it = print_it_noop;
5222 bs->stop = 0;
5223 }
5224 }
5225}
5226
7d4df6a4
DE
5227/* For breakpoints that are currently marked as telling gdb to stop,
5228 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5229 of breakpoint referred to by BS. If we should not stop for this
5230 breakpoint, set BS->stop to 0. */
f431efe5 5231
18a18393
VP
5232static void
5233bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5234{
2bdf28a0
JK
5235 const struct bp_location *bl;
5236 struct breakpoint *b;
bf469271
PA
5237 /* Assume stop. */
5238 bool condition_result = true;
7d4df6a4
DE
5239 struct expression *cond;
5240
5241 gdb_assert (bs->stop);
2bdf28a0
JK
5242
5243 /* BS is built for existing struct breakpoint. */
f431efe5 5244 bl = bs->bp_location_at;
2bdf28a0 5245 gdb_assert (bl != NULL);
f431efe5 5246 b = bs->breakpoint_at;
2bdf28a0 5247 gdb_assert (b != NULL);
18a18393 5248
b775012e
LM
5249 /* Even if the target evaluated the condition on its end and notified GDB, we
5250 need to do so again since GDB does not know if we stopped due to a
5251 breakpoint or a single step breakpoint. */
5252
18a18393 5253 if (frame_id_p (b->frame_id)
edb3359d 5254 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5255 {
7d4df6a4
DE
5256 bs->stop = 0;
5257 return;
5258 }
60e1c644 5259
12ab52e9
PA
5260 /* If this is a thread/task-specific breakpoint, don't waste cpu
5261 evaluating the condition if this isn't the specified
5262 thread/task. */
5d5658a1 5263 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5264 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5265
6c1b0f7b
DE
5266 {
5267 bs->stop = 0;
5268 return;
5269 }
5270
6dddc817
DE
5271 /* Evaluate extension language breakpoints that have a "stop" method
5272 implemented. */
5273 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5274
7d4df6a4
DE
5275 if (is_watchpoint (b))
5276 {
5277 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5278
4d01a485 5279 cond = w->cond_exp.get ();
7d4df6a4
DE
5280 }
5281 else
4d01a485 5282 cond = bl->cond.get ();
60e1c644 5283
7d4df6a4
DE
5284 if (cond && b->disposition != disp_del_at_next_stop)
5285 {
5286 int within_current_scope = 1;
5287 struct watchpoint * w;
60e1c644 5288
7d4df6a4
DE
5289 /* We use value_mark and value_free_to_mark because it could
5290 be a long time before we return to the command level and
5291 call free_all_values. We can't call free_all_values
5292 because we might be in the middle of evaluating a
5293 function call. */
5294 struct value *mark = value_mark ();
5295
5296 if (is_watchpoint (b))
5297 w = (struct watchpoint *) b;
5298 else
5299 w = NULL;
5300
5301 /* Need to select the frame, with all that implies so that
5302 the conditions will have the right context. Because we
5303 use the frame, we will not see an inlined function's
5304 variables when we arrive at a breakpoint at the start
5305 of the inlined function; the current frame will be the
5306 call site. */
5307 if (w == NULL || w->cond_exp_valid_block == NULL)
5308 select_frame (get_current_frame ());
5309 else
18a18393 5310 {
7d4df6a4
DE
5311 struct frame_info *frame;
5312
5313 /* For local watchpoint expressions, which particular
5314 instance of a local is being watched matters, so we
5315 keep track of the frame to evaluate the expression
5316 in. To evaluate the condition however, it doesn't
5317 really matter which instantiation of the function
5318 where the condition makes sense triggers the
5319 watchpoint. This allows an expression like "watch
5320 global if q > 10" set in `func', catch writes to
5321 global on all threads that call `func', or catch
5322 writes on all recursive calls of `func' by a single
5323 thread. We simply always evaluate the condition in
5324 the innermost frame that's executing where it makes
5325 sense to evaluate the condition. It seems
5326 intuitive. */
5327 frame = block_innermost_frame (w->cond_exp_valid_block);
5328 if (frame != NULL)
5329 select_frame (frame);
5330 else
5331 within_current_scope = 0;
18a18393 5332 }
7d4df6a4 5333 if (within_current_scope)
bf469271
PA
5334 {
5335 TRY
5336 {
5337 condition_result = breakpoint_cond_eval (cond);
5338 }
5339 CATCH (ex, RETURN_MASK_ALL)
5340 {
5341 exception_fprintf (gdb_stderr, ex,
5342 "Error in testing breakpoint condition:\n");
5343 }
5344 END_CATCH
5345 }
7d4df6a4 5346 else
18a18393 5347 {
7d4df6a4
DE
5348 warning (_("Watchpoint condition cannot be tested "
5349 "in the current scope"));
5350 /* If we failed to set the right context for this
5351 watchpoint, unconditionally report it. */
18a18393 5352 }
7d4df6a4
DE
5353 /* FIXME-someday, should give breakpoint #. */
5354 value_free_to_mark (mark);
18a18393 5355 }
7d4df6a4 5356
bf469271 5357 if (cond && !condition_result)
7d4df6a4
DE
5358 {
5359 bs->stop = 0;
5360 }
7d4df6a4
DE
5361 else if (b->ignore_count > 0)
5362 {
5363 b->ignore_count--;
5364 bs->stop = 0;
5365 /* Increase the hit count even though we don't stop. */
5366 ++(b->hit_count);
5367 observer_notify_breakpoint_modified (b);
5368 }
18a18393
VP
5369}
5370
1cf4d951
PA
5371/* Returns true if we need to track moribund locations of LOC's type
5372 on the current target. */
5373
5374static int
5375need_moribund_for_location_type (struct bp_location *loc)
5376{
5377 return ((loc->loc_type == bp_loc_software_breakpoint
5378 && !target_supports_stopped_by_sw_breakpoint ())
5379 || (loc->loc_type == bp_loc_hardware_breakpoint
5380 && !target_supports_stopped_by_hw_breakpoint ()));
5381}
5382
18a18393 5383
9709f61c 5384/* Get a bpstat associated with having just stopped at address
d983da9c 5385 BP_ADDR in thread PTID.
c906108c 5386
d983da9c 5387 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5388 don't understand this stop. Result is a chain of bpstat's such
5389 that:
c906108c 5390
c5aa993b 5391 if we don't understand the stop, the result is a null pointer.
c906108c 5392
c5aa993b 5393 if we understand why we stopped, the result is not null.
c906108c 5394
c5aa993b
JM
5395 Each element of the chain refers to a particular breakpoint or
5396 watchpoint at which we have stopped. (We may have stopped for
5397 several reasons concurrently.)
c906108c 5398
c5aa993b
JM
5399 Each element of the chain has valid next, breakpoint_at,
5400 commands, FIXME??? fields. */
c906108c
SS
5401
5402bpstat
accd0bcd 5403bpstat_stop_status (const address_space *aspace,
09ac7c10
TT
5404 CORE_ADDR bp_addr, ptid_t ptid,
5405 const struct target_waitstatus *ws)
c906108c 5406{
0d381245 5407 struct breakpoint *b = NULL;
afe38095 5408 struct bp_location *bl;
20874c92 5409 struct bp_location *loc;
5760d0ab
JK
5410 /* First item of allocated bpstat's. */
5411 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5412 /* Pointer to the last thing in the chain currently. */
5760d0ab 5413 bpstat bs;
20874c92 5414 int ix;
429374b8 5415 int need_remove_insert;
f431efe5 5416 int removed_any;
c906108c 5417
f431efe5
PA
5418 /* First, build the bpstat chain with locations that explain a
5419 target stop, while being careful to not set the target running,
5420 as that may invalidate locations (in particular watchpoint
5421 locations are recreated). Resuming will happen here with
5422 breakpoint conditions or watchpoint expressions that include
5423 inferior function calls. */
c5aa993b 5424
429374b8
JK
5425 ALL_BREAKPOINTS (b)
5426 {
1a853c52 5427 if (!breakpoint_enabled (b))
429374b8 5428 continue;
a5606eee 5429
429374b8
JK
5430 for (bl = b->loc; bl != NULL; bl = bl->next)
5431 {
4a64f543
MS
5432 /* For hardware watchpoints, we look only at the first
5433 location. The watchpoint_check function will work on the
5434 entire expression, not the individual locations. For
5435 read watchpoints, the watchpoints_triggered function has
5436 checked all locations already. */
429374b8
JK
5437 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5438 break;
18a18393 5439
f6592439 5440 if (!bl->enabled || bl->shlib_disabled)
429374b8 5441 continue;
c5aa993b 5442
09ac7c10 5443 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5444 continue;
c5aa993b 5445
4a64f543
MS
5446 /* Come here if it's a watchpoint, or if the break address
5447 matches. */
c5aa993b 5448
04afa70c 5449 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5450 explain stop. */
c5aa993b 5451
f431efe5
PA
5452 /* Assume we stop. Should we find a watchpoint that is not
5453 actually triggered, or if the condition of the breakpoint
5454 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5455 bs->stop = 1;
5456 bs->print = 1;
d983da9c 5457
f431efe5
PA
5458 /* If this is a scope breakpoint, mark the associated
5459 watchpoint as triggered so that we will handle the
5460 out-of-scope event. We'll get to the watchpoint next
5461 iteration. */
d0fb5eae 5462 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5463 {
5464 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5465
5466 w->watchpoint_triggered = watch_triggered_yes;
5467 }
f431efe5
PA
5468 }
5469 }
5470
7c16b83e 5471 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5472 if (!target_supports_stopped_by_sw_breakpoint ()
5473 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5474 {
1cf4d951 5475 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5476 {
1cf4d951
PA
5477 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5478 && need_moribund_for_location_type (loc))
5479 {
04afa70c 5480 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5481 /* For hits of moribund locations, we should just proceed. */
5482 bs->stop = 0;
5483 bs->print = 0;
5484 bs->print_it = print_it_noop;
5485 }
f431efe5
PA
5486 }
5487 }
5488
edcc5120
TT
5489 /* A bit of special processing for shlib breakpoints. We need to
5490 process solib loading here, so that the lists of loaded and
5491 unloaded libraries are correct before we handle "catch load" and
5492 "catch unload". */
5493 for (bs = bs_head; bs != NULL; bs = bs->next)
5494 {
5d268276 5495 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5496 {
5497 handle_solib_event ();
5498 break;
5499 }
5500 }
5501
f431efe5
PA
5502 /* Now go through the locations that caused the target to stop, and
5503 check whether we're interested in reporting this stop to higher
5504 layers, or whether we should resume the target transparently. */
5505
5506 removed_any = 0;
5507
5760d0ab 5508 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5509 {
5510 if (!bs->stop)
5511 continue;
5512
f431efe5 5513 b = bs->breakpoint_at;
348d480f
PA
5514 b->ops->check_status (bs);
5515 if (bs->stop)
28010a5d 5516 {
348d480f 5517 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5518
429374b8
JK
5519 if (bs->stop)
5520 {
5521 ++(b->hit_count);
8d3788bd 5522 observer_notify_breakpoint_modified (b);
c906108c 5523
4a64f543 5524 /* We will stop here. */
429374b8
JK
5525 if (b->disposition == disp_disable)
5526 {
816338b5 5527 --(b->enable_count);
1a853c52 5528 if (b->enable_count <= 0)
429374b8 5529 b->enable_state = bp_disabled;
f431efe5 5530 removed_any = 1;
429374b8
JK
5531 }
5532 if (b->silent)
5533 bs->print = 0;
5534 bs->commands = b->commands;
abf85f46 5535 if (command_line_is_silent (bs->commands
d1b0a7bf 5536 ? bs->commands.get () : NULL))
abf85f46 5537 bs->print = 0;
9d6e6e84
HZ
5538
5539 b->ops->after_condition_true (bs);
429374b8
JK
5540 }
5541
348d480f 5542 }
a9b3a50f
PA
5543
5544 /* Print nothing for this entry if we don't stop or don't
5545 print. */
5546 if (!bs->stop || !bs->print)
5547 bs->print_it = print_it_noop;
429374b8 5548 }
876fa593 5549
d983da9c
DJ
5550 /* If we aren't stopping, the value of some hardware watchpoint may
5551 not have changed, but the intermediate memory locations we are
5552 watching may have. Don't bother if we're stopping; this will get
5553 done later. */
d832cb68 5554 need_remove_insert = 0;
5760d0ab
JK
5555 if (! bpstat_causes_stop (bs_head))
5556 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5557 if (!bs->stop
f431efe5
PA
5558 && bs->breakpoint_at
5559 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5560 {
3a5c3e22
PA
5561 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5562
5563 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5564 need_remove_insert = 1;
d983da9c
DJ
5565 }
5566
d832cb68 5567 if (need_remove_insert)
44702360 5568 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5569 else if (removed_any)
44702360 5570 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5571
5760d0ab 5572 return bs_head;
c906108c 5573}
628fe4e4
JK
5574
5575static void
5576handle_jit_event (void)
5577{
5578 struct frame_info *frame;
5579 struct gdbarch *gdbarch;
5580
243a9253
PA
5581 if (debug_infrun)
5582 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5583
628fe4e4
JK
5584 /* Switch terminal for any messages produced by
5585 breakpoint_re_set. */
223ffa71 5586 target_terminal::ours_for_output ();
628fe4e4
JK
5587
5588 frame = get_current_frame ();
5589 gdbarch = get_frame_arch (frame);
5590
5591 jit_event_handler (gdbarch);
5592
223ffa71 5593 target_terminal::inferior ();
628fe4e4
JK
5594}
5595
5596/* Prepare WHAT final decision for infrun. */
5597
5598/* Decide what infrun needs to do with this bpstat. */
5599
c906108c 5600struct bpstat_what
0e30163f 5601bpstat_what (bpstat bs_head)
c906108c 5602{
c906108c 5603 struct bpstat_what retval;
0e30163f 5604 bpstat bs;
c906108c 5605
628fe4e4 5606 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5607 retval.call_dummy = STOP_NONE;
186c406b 5608 retval.is_longjmp = 0;
628fe4e4 5609
0e30163f 5610 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5611 {
628fe4e4
JK
5612 /* Extract this BS's action. After processing each BS, we check
5613 if its action overrides all we've seem so far. */
5614 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5615 enum bptype bptype;
5616
c906108c 5617 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5618 {
5619 /* I suspect this can happen if it was a momentary
5620 breakpoint which has since been deleted. */
5621 bptype = bp_none;
5622 }
20874c92 5623 else
f431efe5 5624 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5625
5626 switch (bptype)
c906108c
SS
5627 {
5628 case bp_none:
628fe4e4 5629 break;
c906108c
SS
5630 case bp_breakpoint:
5631 case bp_hardware_breakpoint:
7c16b83e 5632 case bp_single_step:
c906108c
SS
5633 case bp_until:
5634 case bp_finish:
a9b3a50f 5635 case bp_shlib_event:
c906108c
SS
5636 if (bs->stop)
5637 {
5638 if (bs->print)
628fe4e4 5639 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5640 else
628fe4e4 5641 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5642 }
5643 else
628fe4e4 5644 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5645 break;
5646 case bp_watchpoint:
5647 case bp_hardware_watchpoint:
5648 case bp_read_watchpoint:
5649 case bp_access_watchpoint:
5650 if (bs->stop)
5651 {
5652 if (bs->print)
628fe4e4 5653 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5654 else
628fe4e4 5655 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5656 }
5657 else
628fe4e4
JK
5658 {
5659 /* There was a watchpoint, but we're not stopping.
5660 This requires no further action. */
5661 }
c906108c
SS
5662 break;
5663 case bp_longjmp:
e2e4d78b 5664 case bp_longjmp_call_dummy:
186c406b 5665 case bp_exception:
0a39bb32
PA
5666 if (bs->stop)
5667 {
5668 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5669 retval.is_longjmp = bptype != bp_exception;
5670 }
5671 else
5672 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5673 break;
5674 case bp_longjmp_resume:
186c406b 5675 case bp_exception_resume:
0a39bb32
PA
5676 if (bs->stop)
5677 {
5678 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5679 retval.is_longjmp = bptype == bp_longjmp_resume;
5680 }
5681 else
5682 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5683 break;
5684 case bp_step_resume:
5685 if (bs->stop)
628fe4e4
JK
5686 this_action = BPSTAT_WHAT_STEP_RESUME;
5687 else
c906108c 5688 {
628fe4e4
JK
5689 /* It is for the wrong frame. */
5690 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5691 }
c906108c 5692 break;
2c03e5be
PA
5693 case bp_hp_step_resume:
5694 if (bs->stop)
5695 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5696 else
5697 {
5698 /* It is for the wrong frame. */
5699 this_action = BPSTAT_WHAT_SINGLE;
5700 }
5701 break;
c906108c 5702 case bp_watchpoint_scope:
c4093a6a 5703 case bp_thread_event:
1900040c 5704 case bp_overlay_event:
0fd8e87f 5705 case bp_longjmp_master:
aa7d318d 5706 case bp_std_terminate_master:
186c406b 5707 case bp_exception_master:
628fe4e4 5708 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5709 break;
ce78b96d 5710 case bp_catchpoint:
c5aa993b
JM
5711 if (bs->stop)
5712 {
5713 if (bs->print)
628fe4e4 5714 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5715 else
628fe4e4 5716 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5717 }
5718 else
628fe4e4
JK
5719 {
5720 /* There was a catchpoint, but we're not stopping.
5721 This requires no further action. */
5722 }
5723 break;
628fe4e4 5724 case bp_jit_event:
628fe4e4 5725 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5726 break;
c906108c 5727 case bp_call_dummy:
53a5351d
JM
5728 /* Make sure the action is stop (silent or noisy),
5729 so infrun.c pops the dummy frame. */
aa7d318d 5730 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5731 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5732 break;
5733 case bp_std_terminate:
5734 /* Make sure the action is stop (silent or noisy),
5735 so infrun.c pops the dummy frame. */
aa7d318d 5736 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5738 break;
1042e4c0 5739 case bp_tracepoint:
7a697b8d 5740 case bp_fast_tracepoint:
0fb4aa4b 5741 case bp_static_tracepoint:
1042e4c0
SS
5742 /* Tracepoint hits should not be reported back to GDB, and
5743 if one got through somehow, it should have been filtered
5744 out already. */
5745 internal_error (__FILE__, __LINE__,
7a697b8d 5746 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5747 break;
5748 case bp_gnu_ifunc_resolver:
5749 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5750 this_action = BPSTAT_WHAT_SINGLE;
5751 break;
5752 case bp_gnu_ifunc_resolver_return:
5753 /* The breakpoint will be removed, execution will restart from the
5754 PC of the former breakpoint. */
5755 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5756 break;
e7e0cddf
SS
5757
5758 case bp_dprintf:
a11cfd87
HZ
5759 if (bs->stop)
5760 this_action = BPSTAT_WHAT_STOP_SILENT;
5761 else
5762 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5763 break;
5764
628fe4e4
JK
5765 default:
5766 internal_error (__FILE__, __LINE__,
5767 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5768 }
628fe4e4 5769
325fac50 5770 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5771 }
628fe4e4 5772
243a9253
PA
5773 return retval;
5774}
628fe4e4 5775
243a9253
PA
5776void
5777bpstat_run_callbacks (bpstat bs_head)
5778{
5779 bpstat bs;
628fe4e4 5780
0e30163f
JK
5781 for (bs = bs_head; bs != NULL; bs = bs->next)
5782 {
5783 struct breakpoint *b = bs->breakpoint_at;
5784
5785 if (b == NULL)
5786 continue;
5787 switch (b->type)
5788 {
243a9253
PA
5789 case bp_jit_event:
5790 handle_jit_event ();
5791 break;
0e30163f
JK
5792 case bp_gnu_ifunc_resolver:
5793 gnu_ifunc_resolver_stop (b);
5794 break;
5795 case bp_gnu_ifunc_resolver_return:
5796 gnu_ifunc_resolver_return_stop (b);
5797 break;
5798 }
5799 }
c906108c
SS
5800}
5801
5802/* Nonzero if we should step constantly (e.g. watchpoints on machines
5803 without hardware support). This isn't related to a specific bpstat,
5804 just to things like whether watchpoints are set. */
5805
c5aa993b 5806int
fba45db2 5807bpstat_should_step (void)
c906108c
SS
5808{
5809 struct breakpoint *b;
cc59ec59 5810
c906108c 5811 ALL_BREAKPOINTS (b)
717a8278 5812 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5813 return 1;
c906108c
SS
5814 return 0;
5815}
5816
67822962
PA
5817int
5818bpstat_causes_stop (bpstat bs)
5819{
5820 for (; bs != NULL; bs = bs->next)
5821 if (bs->stop)
5822 return 1;
5823
5824 return 0;
5825}
5826
c906108c 5827\f
c5aa993b 5828
170b53b2
UW
5829/* Compute a string of spaces suitable to indent the next line
5830 so it starts at the position corresponding to the table column
5831 named COL_NAME in the currently active table of UIOUT. */
5832
5833static char *
5834wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5835{
5836 static char wrap_indent[80];
5837 int i, total_width, width, align;
c5209615 5838 const char *text;
170b53b2
UW
5839
5840 total_width = 0;
112e8700 5841 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5842 {
5843 if (strcmp (text, col_name) == 0)
5844 {
5845 gdb_assert (total_width < sizeof wrap_indent);
5846 memset (wrap_indent, ' ', total_width);
5847 wrap_indent[total_width] = 0;
5848
5849 return wrap_indent;
5850 }
5851
5852 total_width += width + 1;
5853 }
5854
5855 return NULL;
5856}
5857
b775012e
LM
5858/* Determine if the locations of this breakpoint will have their conditions
5859 evaluated by the target, host or a mix of both. Returns the following:
5860
5861 "host": Host evals condition.
5862 "host or target": Host or Target evals condition.
5863 "target": Target evals condition.
5864*/
5865
5866static const char *
5867bp_condition_evaluator (struct breakpoint *b)
5868{
5869 struct bp_location *bl;
5870 char host_evals = 0;
5871 char target_evals = 0;
5872
5873 if (!b)
5874 return NULL;
5875
5876 if (!is_breakpoint (b))
5877 return NULL;
5878
5879 if (gdb_evaluates_breakpoint_condition_p ()
5880 || !target_supports_evaluation_of_breakpoint_conditions ())
5881 return condition_evaluation_host;
5882
5883 for (bl = b->loc; bl; bl = bl->next)
5884 {
5885 if (bl->cond_bytecode)
5886 target_evals++;
5887 else
5888 host_evals++;
5889 }
5890
5891 if (host_evals && target_evals)
5892 return condition_evaluation_both;
5893 else if (target_evals)
5894 return condition_evaluation_target;
5895 else
5896 return condition_evaluation_host;
5897}
5898
5899/* Determine the breakpoint location's condition evaluator. This is
5900 similar to bp_condition_evaluator, but for locations. */
5901
5902static const char *
5903bp_location_condition_evaluator (struct bp_location *bl)
5904{
5905 if (bl && !is_breakpoint (bl->owner))
5906 return NULL;
5907
5908 if (gdb_evaluates_breakpoint_condition_p ()
5909 || !target_supports_evaluation_of_breakpoint_conditions ())
5910 return condition_evaluation_host;
5911
5912 if (bl && bl->cond_bytecode)
5913 return condition_evaluation_target;
5914 else
5915 return condition_evaluation_host;
5916}
5917
859825b8
JK
5918/* Print the LOC location out of the list of B->LOC locations. */
5919
170b53b2
UW
5920static void
5921print_breakpoint_location (struct breakpoint *b,
5922 struct bp_location *loc)
0d381245 5923{
79a45e25 5924 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5925
5926 scoped_restore_current_program_space restore_pspace;
6c95b8df 5927
859825b8
JK
5928 if (loc != NULL && loc->shlib_disabled)
5929 loc = NULL;
5930
6c95b8df
PA
5931 if (loc != NULL)
5932 set_current_program_space (loc->pspace);
5933
56435ebe 5934 if (b->display_canonical)
d28cd78a 5935 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5936 else if (loc && loc->symtab)
0d381245 5937 {
4a27f119
KS
5938 const struct symbol *sym = loc->symbol;
5939
5940 if (sym == NULL)
5941 sym = find_pc_sect_function (loc->address, loc->section);
5942
0d381245
VP
5943 if (sym)
5944 {
112e8700
SM
5945 uiout->text ("in ");
5946 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5947 uiout->text (" ");
5948 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5949 uiout->text ("at ");
0d381245 5950 }
112e8700 5951 uiout->field_string ("file",
05cba821 5952 symtab_to_filename_for_display (loc->symtab));
112e8700 5953 uiout->text (":");
05cba821 5954
112e8700
SM
5955 if (uiout->is_mi_like_p ())
5956 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5957
112e8700 5958 uiout->field_int ("line", loc->line_number);
0d381245 5959 }
859825b8 5960 else if (loc)
0d381245 5961 {
d7e74731 5962 string_file stb;
170b53b2 5963
d7e74731 5964 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5965 demangle, "");
112e8700 5966 uiout->field_stream ("at", stb);
0d381245 5967 }
859825b8 5968 else
f00aae0f 5969 {
d28cd78a
TT
5970 uiout->field_string ("pending",
5971 event_location_to_string (b->location.get ()));
f00aae0f
KS
5972 /* If extra_string is available, it could be holding a condition
5973 or dprintf arguments. In either case, make sure it is printed,
5974 too, but only for non-MI streams. */
112e8700 5975 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5976 {
5977 if (b->type == bp_dprintf)
112e8700 5978 uiout->text (",");
f00aae0f 5979 else
112e8700
SM
5980 uiout->text (" ");
5981 uiout->text (b->extra_string);
f00aae0f
KS
5982 }
5983 }
6c95b8df 5984
b775012e
LM
5985 if (loc && is_breakpoint (b)
5986 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5987 && bp_condition_evaluator (b) == condition_evaluation_both)
5988 {
112e8700
SM
5989 uiout->text (" (");
5990 uiout->field_string ("evaluated-by",
b775012e 5991 bp_location_condition_evaluator (loc));
112e8700 5992 uiout->text (")");
b775012e 5993 }
0d381245
VP
5994}
5995
269b11a2
PA
5996static const char *
5997bptype_string (enum bptype type)
c906108c 5998{
c4093a6a
JM
5999 struct ep_type_description
6000 {
6001 enum bptype type;
a121b7c1 6002 const char *description;
c4093a6a
JM
6003 };
6004 static struct ep_type_description bptypes[] =
c906108c 6005 {
c5aa993b
JM
6006 {bp_none, "?deleted?"},
6007 {bp_breakpoint, "breakpoint"},
c906108c 6008 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6009 {bp_single_step, "sw single-step"},
c5aa993b
JM
6010 {bp_until, "until"},
6011 {bp_finish, "finish"},
6012 {bp_watchpoint, "watchpoint"},
c906108c 6013 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6014 {bp_read_watchpoint, "read watchpoint"},
6015 {bp_access_watchpoint, "acc watchpoint"},
6016 {bp_longjmp, "longjmp"},
6017 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6018 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6019 {bp_exception, "exception"},
6020 {bp_exception_resume, "exception resume"},
c5aa993b 6021 {bp_step_resume, "step resume"},
2c03e5be 6022 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6023 {bp_watchpoint_scope, "watchpoint scope"},
6024 {bp_call_dummy, "call dummy"},
aa7d318d 6025 {bp_std_terminate, "std::terminate"},
c5aa993b 6026 {bp_shlib_event, "shlib events"},
c4093a6a 6027 {bp_thread_event, "thread events"},
1900040c 6028 {bp_overlay_event, "overlay events"},
0fd8e87f 6029 {bp_longjmp_master, "longjmp master"},
aa7d318d 6030 {bp_std_terminate_master, "std::terminate master"},
186c406b 6031 {bp_exception_master, "exception master"},
ce78b96d 6032 {bp_catchpoint, "catchpoint"},
1042e4c0 6033 {bp_tracepoint, "tracepoint"},
7a697b8d 6034 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6035 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6036 {bp_dprintf, "dprintf"},
4efc6507 6037 {bp_jit_event, "jit events"},
0e30163f
JK
6038 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6039 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6040 };
269b11a2
PA
6041
6042 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6043 || ((int) type != bptypes[(int) type].type))
6044 internal_error (__FILE__, __LINE__,
6045 _("bptypes table does not describe type #%d."),
6046 (int) type);
6047
6048 return bptypes[(int) type].description;
6049}
6050
998580f1
MK
6051/* For MI, output a field named 'thread-groups' with a list as the value.
6052 For CLI, prefix the list with the string 'inf'. */
6053
6054static void
6055output_thread_groups (struct ui_out *uiout,
6056 const char *field_name,
6057 VEC(int) *inf_num,
6058 int mi_only)
6059{
112e8700 6060 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6061 int inf;
6062 int i;
6063
6064 /* For backward compatibility, don't display inferiors in CLI unless
6065 there are several. Always display them for MI. */
6066 if (!is_mi && mi_only)
6067 return;
6068
10f489e5 6069 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6070
998580f1
MK
6071 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6072 {
6073 if (is_mi)
6074 {
6075 char mi_group[10];
6076
6077 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6078 uiout->field_string (NULL, mi_group);
998580f1
MK
6079 }
6080 else
6081 {
6082 if (i == 0)
112e8700 6083 uiout->text (" inf ");
998580f1 6084 else
112e8700 6085 uiout->text (", ");
998580f1 6086
112e8700 6087 uiout->text (plongest (inf));
998580f1
MK
6088 }
6089 }
998580f1
MK
6090}
6091
269b11a2
PA
6092/* Print B to gdb_stdout. */
6093
6094static void
6095print_one_breakpoint_location (struct breakpoint *b,
6096 struct bp_location *loc,
6097 int loc_number,
6098 struct bp_location **last_loc,
269b11a2
PA
6099 int allflag)
6100{
6101 struct command_line *l;
c2c6d25f 6102 static char bpenables[] = "nynny";
c906108c 6103
79a45e25 6104 struct ui_out *uiout = current_uiout;
0d381245
VP
6105 int header_of_multiple = 0;
6106 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6107 struct value_print_options opts;
6108
6109 get_user_print_options (&opts);
0d381245
VP
6110
6111 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6112 /* See comment in print_one_breakpoint concerning treatment of
6113 breakpoints with single disabled location. */
0d381245
VP
6114 if (loc == NULL
6115 && (b->loc != NULL
6116 && (b->loc->next != NULL || !b->loc->enabled)))
6117 header_of_multiple = 1;
6118 if (loc == NULL)
6119 loc = b->loc;
6120
c4093a6a
JM
6121 annotate_record ();
6122
6123 /* 1 */
6124 annotate_field (0);
0d381245
VP
6125 if (part_of_multiple)
6126 {
6127 char *formatted;
0c6773c1 6128 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6129 uiout->field_string ("number", formatted);
0d381245
VP
6130 xfree (formatted);
6131 }
6132 else
6133 {
112e8700 6134 uiout->field_int ("number", b->number);
0d381245 6135 }
c4093a6a
JM
6136
6137 /* 2 */
6138 annotate_field (1);
0d381245 6139 if (part_of_multiple)
112e8700 6140 uiout->field_skip ("type");
269b11a2 6141 else
112e8700 6142 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6143
6144 /* 3 */
6145 annotate_field (2);
0d381245 6146 if (part_of_multiple)
112e8700 6147 uiout->field_skip ("disp");
0d381245 6148 else
112e8700 6149 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6150
c4093a6a
JM
6151
6152 /* 4 */
6153 annotate_field (3);
0d381245 6154 if (part_of_multiple)
112e8700 6155 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6156 else
112e8700
SM
6157 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6158 uiout->spaces (2);
0d381245 6159
c4093a6a
JM
6160
6161 /* 5 and 6 */
3086aeae 6162 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6163 {
4a64f543
MS
6164 /* Although the print_one can possibly print all locations,
6165 calling it here is not likely to get any nice result. So,
6166 make sure there's just one location. */
0d381245 6167 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6168 b->ops->print_one (b, last_loc);
0d381245 6169 }
3086aeae
DJ
6170 else
6171 switch (b->type)
6172 {
6173 case bp_none:
6174 internal_error (__FILE__, __LINE__,
e2e0b3e5 6175 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6176 break;
c906108c 6177
3086aeae
DJ
6178 case bp_watchpoint:
6179 case bp_hardware_watchpoint:
6180 case bp_read_watchpoint:
6181 case bp_access_watchpoint:
3a5c3e22
PA
6182 {
6183 struct watchpoint *w = (struct watchpoint *) b;
6184
6185 /* Field 4, the address, is omitted (which makes the columns
6186 not line up too nicely with the headers, but the effect
6187 is relatively readable). */
6188 if (opts.addressprint)
112e8700 6189 uiout->field_skip ("addr");
3a5c3e22 6190 annotate_field (5);
112e8700 6191 uiout->field_string ("what", w->exp_string);
3a5c3e22 6192 }
3086aeae
DJ
6193 break;
6194
3086aeae
DJ
6195 case bp_breakpoint:
6196 case bp_hardware_breakpoint:
7c16b83e 6197 case bp_single_step:
3086aeae
DJ
6198 case bp_until:
6199 case bp_finish:
6200 case bp_longjmp:
6201 case bp_longjmp_resume:
e2e4d78b 6202 case bp_longjmp_call_dummy:
186c406b
TT
6203 case bp_exception:
6204 case bp_exception_resume:
3086aeae 6205 case bp_step_resume:
2c03e5be 6206 case bp_hp_step_resume:
3086aeae
DJ
6207 case bp_watchpoint_scope:
6208 case bp_call_dummy:
aa7d318d 6209 case bp_std_terminate:
3086aeae
DJ
6210 case bp_shlib_event:
6211 case bp_thread_event:
6212 case bp_overlay_event:
0fd8e87f 6213 case bp_longjmp_master:
aa7d318d 6214 case bp_std_terminate_master:
186c406b 6215 case bp_exception_master:
1042e4c0 6216 case bp_tracepoint:
7a697b8d 6217 case bp_fast_tracepoint:
0fb4aa4b 6218 case bp_static_tracepoint:
e7e0cddf 6219 case bp_dprintf:
4efc6507 6220 case bp_jit_event:
0e30163f
JK
6221 case bp_gnu_ifunc_resolver:
6222 case bp_gnu_ifunc_resolver_return:
79a45b7d 6223 if (opts.addressprint)
3086aeae
DJ
6224 {
6225 annotate_field (4);
54e52265 6226 if (header_of_multiple)
112e8700 6227 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6228 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6229 uiout->field_string ("addr", "<PENDING>");
0101ce28 6230 else
112e8700 6231 uiout->field_core_addr ("addr",
5af949e3 6232 loc->gdbarch, loc->address);
3086aeae
DJ
6233 }
6234 annotate_field (5);
0d381245 6235 if (!header_of_multiple)
170b53b2 6236 print_breakpoint_location (b, loc);
0d381245 6237 if (b->loc)
a6d9a66e 6238 *last_loc = b->loc;
3086aeae
DJ
6239 break;
6240 }
c906108c 6241
6c95b8df 6242
998580f1 6243 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6244 {
6245 struct inferior *inf;
998580f1
MK
6246 VEC(int) *inf_num = NULL;
6247 int mi_only = 1;
6c95b8df 6248
998580f1 6249 ALL_INFERIORS (inf)
6c95b8df
PA
6250 {
6251 if (inf->pspace == loc->pspace)
998580f1 6252 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6253 }
998580f1
MK
6254
6255 /* For backward compatibility, don't display inferiors in CLI unless
6256 there are several. Always display for MI. */
6257 if (allflag
6258 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6259 && (number_of_program_spaces () > 1
6260 || number_of_inferiors () > 1)
6261 /* LOC is for existing B, it cannot be in
6262 moribund_locations and thus having NULL OWNER. */
6263 && loc->owner->type != bp_catchpoint))
6264 mi_only = 0;
6265 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6266 VEC_free (int, inf_num);
6c95b8df
PA
6267 }
6268
4a306c9a 6269 if (!part_of_multiple)
c4093a6a 6270 {
4a306c9a
JB
6271 if (b->thread != -1)
6272 {
6273 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6274 "stop only in" line a little further down. */
112e8700
SM
6275 uiout->text (" thread ");
6276 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6277 }
6278 else if (b->task != 0)
6279 {
112e8700
SM
6280 uiout->text (" task ");
6281 uiout->field_int ("task", b->task);
4a306c9a 6282 }
c4093a6a 6283 }
f1310107 6284
112e8700 6285 uiout->text ("\n");
f1310107 6286
348d480f 6287 if (!part_of_multiple)
f1310107
TJB
6288 b->ops->print_one_detail (b, uiout);
6289
0d381245 6290 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6291 {
6292 annotate_field (6);
112e8700 6293 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6294 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6295 the frame ID. */
112e8700 6296 uiout->field_core_addr ("frame",
5af949e3 6297 b->gdbarch, b->frame_id.stack_addr);
112e8700 6298 uiout->text ("\n");
c4093a6a
JM
6299 }
6300
28010a5d 6301 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6302 {
6303 annotate_field (7);
d77f58be 6304 if (is_tracepoint (b))
112e8700 6305 uiout->text ("\ttrace only if ");
1042e4c0 6306 else
112e8700
SM
6307 uiout->text ("\tstop only if ");
6308 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6309
6310 /* Print whether the target is doing the breakpoint's condition
6311 evaluation. If GDB is doing the evaluation, don't print anything. */
6312 if (is_breakpoint (b)
6313 && breakpoint_condition_evaluation_mode ()
6314 == condition_evaluation_target)
6315 {
112e8700
SM
6316 uiout->text (" (");
6317 uiout->field_string ("evaluated-by",
b775012e 6318 bp_condition_evaluator (b));
112e8700 6319 uiout->text (" evals)");
b775012e 6320 }
112e8700 6321 uiout->text ("\n");
0101ce28
JJ
6322 }
6323
0d381245 6324 if (!part_of_multiple && b->thread != -1)
c4093a6a 6325 {
4a64f543 6326 /* FIXME should make an annotation for this. */
112e8700
SM
6327 uiout->text ("\tstop only in thread ");
6328 if (uiout->is_mi_like_p ())
6329 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6330 else
6331 {
6332 struct thread_info *thr = find_thread_global_id (b->thread);
6333
112e8700 6334 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6335 }
112e8700 6336 uiout->text ("\n");
c4093a6a
JM
6337 }
6338
556ec64d
YQ
6339 if (!part_of_multiple)
6340 {
6341 if (b->hit_count)
31f56a27
YQ
6342 {
6343 /* FIXME should make an annotation for this. */
6344 if (is_catchpoint (b))
112e8700 6345 uiout->text ("\tcatchpoint");
31f56a27 6346 else if (is_tracepoint (b))
112e8700 6347 uiout->text ("\ttracepoint");
31f56a27 6348 else
112e8700
SM
6349 uiout->text ("\tbreakpoint");
6350 uiout->text (" already hit ");
6351 uiout->field_int ("times", b->hit_count);
31f56a27 6352 if (b->hit_count == 1)
112e8700 6353 uiout->text (" time\n");
31f56a27 6354 else
112e8700 6355 uiout->text (" times\n");
31f56a27 6356 }
556ec64d
YQ
6357 else
6358 {
31f56a27 6359 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6360 if (uiout->is_mi_like_p ())
6361 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6362 }
6363 }
8b93c638 6364
0d381245 6365 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6366 {
6367 annotate_field (8);
112e8700
SM
6368 uiout->text ("\tignore next ");
6369 uiout->field_int ("ignore", b->ignore_count);
6370 uiout->text (" hits\n");
c4093a6a 6371 }
059fb39f 6372
816338b5
SS
6373 /* Note that an enable count of 1 corresponds to "enable once"
6374 behavior, which is reported by the combination of enablement and
6375 disposition, so we don't need to mention it here. */
6376 if (!part_of_multiple && b->enable_count > 1)
6377 {
6378 annotate_field (8);
112e8700 6379 uiout->text ("\tdisable after ");
816338b5
SS
6380 /* Tweak the wording to clarify that ignore and enable counts
6381 are distinct, and have additive effect. */
6382 if (b->ignore_count)
112e8700 6383 uiout->text ("additional ");
816338b5 6384 else
112e8700
SM
6385 uiout->text ("next ");
6386 uiout->field_int ("enable", b->enable_count);
6387 uiout->text (" hits\n");
816338b5
SS
6388 }
6389
f196051f
SS
6390 if (!part_of_multiple && is_tracepoint (b))
6391 {
6392 struct tracepoint *tp = (struct tracepoint *) b;
6393
6394 if (tp->traceframe_usage)
6395 {
112e8700
SM
6396 uiout->text ("\ttrace buffer usage ");
6397 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6398 uiout->text (" bytes\n");
f196051f
SS
6399 }
6400 }
d3ce09f5 6401
d1b0a7bf 6402 l = b->commands ? b->commands.get () : NULL;
059fb39f 6403 if (!part_of_multiple && l)
c4093a6a
JM
6404 {
6405 annotate_field (9);
2e783024 6406 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6407 print_command_lines (uiout, l, 4);
c4093a6a 6408 }
d24317b4 6409
d9b3f62e 6410 if (is_tracepoint (b))
1042e4c0 6411 {
d9b3f62e
PA
6412 struct tracepoint *t = (struct tracepoint *) b;
6413
6414 if (!part_of_multiple && t->pass_count)
6415 {
6416 annotate_field (10);
112e8700
SM
6417 uiout->text ("\tpass count ");
6418 uiout->field_int ("pass", t->pass_count);
6419 uiout->text (" \n");
d9b3f62e 6420 }
f2a8bc8a
YQ
6421
6422 /* Don't display it when tracepoint or tracepoint location is
6423 pending. */
6424 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6425 {
6426 annotate_field (11);
6427
112e8700
SM
6428 if (uiout->is_mi_like_p ())
6429 uiout->field_string ("installed",
f2a8bc8a
YQ
6430 loc->inserted ? "y" : "n");
6431 else
6432 {
6433 if (loc->inserted)
112e8700 6434 uiout->text ("\t");
f2a8bc8a 6435 else
112e8700
SM
6436 uiout->text ("\tnot ");
6437 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6438 }
6439 }
1042e4c0
SS
6440 }
6441
112e8700 6442 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6443 {
3a5c3e22
PA
6444 if (is_watchpoint (b))
6445 {
6446 struct watchpoint *w = (struct watchpoint *) b;
6447
112e8700 6448 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6449 }
f00aae0f 6450 else if (b->location != NULL
d28cd78a 6451 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6452 uiout->field_string ("original-location",
d28cd78a 6453 event_location_to_string (b->location.get ()));
d24317b4 6454 }
c4093a6a 6455}
c5aa993b 6456
0d381245
VP
6457static void
6458print_one_breakpoint (struct breakpoint *b,
4a64f543 6459 struct bp_location **last_loc,
6c95b8df 6460 int allflag)
0d381245 6461{
79a45e25 6462 struct ui_out *uiout = current_uiout;
8d3788bd 6463
2e783024
TT
6464 {
6465 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6466
2e783024
TT
6467 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6468 }
0d381245
VP
6469
6470 /* If this breakpoint has custom print function,
6471 it's already printed. Otherwise, print individual
6472 locations, if any. */
6473 if (b->ops == NULL || b->ops->print_one == NULL)
6474 {
4a64f543
MS
6475 /* If breakpoint has a single location that is disabled, we
6476 print it as if it had several locations, since otherwise it's
6477 hard to represent "breakpoint enabled, location disabled"
6478 situation.
6479
6480 Note that while hardware watchpoints have several locations
a3be7890 6481 internally, that's not a property exposed to user. */
0d381245 6482 if (b->loc
a5606eee 6483 && !is_hardware_watchpoint (b)
8d3788bd 6484 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6485 {
6486 struct bp_location *loc;
6487 int n = 1;
8d3788bd 6488
0d381245 6489 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6490 {
2e783024 6491 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6492 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6493 }
0d381245
VP
6494 }
6495 }
6496}
6497
a6d9a66e
UW
6498static int
6499breakpoint_address_bits (struct breakpoint *b)
6500{
6501 int print_address_bits = 0;
6502 struct bp_location *loc;
6503
c6d81124
PA
6504 /* Software watchpoints that aren't watching memory don't have an
6505 address to print. */
6506 if (is_no_memory_software_watchpoint (b))
6507 return 0;
6508
a6d9a66e
UW
6509 for (loc = b->loc; loc; loc = loc->next)
6510 {
c7437ca6
PA
6511 int addr_bit;
6512
c7437ca6 6513 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6514 if (addr_bit > print_address_bits)
6515 print_address_bits = addr_bit;
6516 }
6517
6518 return print_address_bits;
6519}
0d381245 6520
65630365 6521/* See breakpoint.h. */
c5aa993b 6522
65630365
PA
6523void
6524print_breakpoint (breakpoint *b)
c4093a6a 6525{
a6d9a66e 6526 struct bp_location *dummy_loc = NULL;
65630365 6527 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6528}
c5aa993b 6529
09d682a4
TT
6530/* Return true if this breakpoint was set by the user, false if it is
6531 internal or momentary. */
6532
6533int
6534user_breakpoint_p (struct breakpoint *b)
6535{
46c6471b 6536 return b->number > 0;
09d682a4
TT
6537}
6538
93daf339
TT
6539/* See breakpoint.h. */
6540
6541int
6542pending_breakpoint_p (struct breakpoint *b)
6543{
6544 return b->loc == NULL;
6545}
6546
7f3b0473 6547/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6548 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6549 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6550 FILTER is non-NULL, call it on each breakpoint and only include the
6551 ones for which it returns non-zero. Return the total number of
6552 breakpoints listed. */
c906108c 6553
d77f58be 6554static int
4495129a 6555breakpoint_1 (const char *args, int allflag,
4a64f543 6556 int (*filter) (const struct breakpoint *))
c4093a6a 6557{
52f0bd74 6558 struct breakpoint *b;
a6d9a66e 6559 struct bp_location *last_loc = NULL;
7f3b0473 6560 int nr_printable_breakpoints;
79a45b7d 6561 struct value_print_options opts;
a6d9a66e 6562 int print_address_bits = 0;
269b11a2 6563 int print_type_col_width = 14;
79a45e25 6564 struct ui_out *uiout = current_uiout;
269b11a2 6565
79a45b7d
TT
6566 get_user_print_options (&opts);
6567
4a64f543
MS
6568 /* Compute the number of rows in the table, as well as the size
6569 required for address fields. */
7f3b0473
AC
6570 nr_printable_breakpoints = 0;
6571 ALL_BREAKPOINTS (b)
e5a67952
MS
6572 {
6573 /* If we have a filter, only list the breakpoints it accepts. */
6574 if (filter && !filter (b))
6575 continue;
6576
6577 /* If we have an "args" string, it is a list of breakpoints to
6578 accept. Skip the others. */
6579 if (args != NULL && *args != '\0')
6580 {
6581 if (allflag && parse_and_eval_long (args) != b->number)
6582 continue;
6583 if (!allflag && !number_is_in_list (args, b->number))
6584 continue;
6585 }
269b11a2 6586
e5a67952
MS
6587 if (allflag || user_breakpoint_p (b))
6588 {
6589 int addr_bit, type_len;
a6d9a66e 6590
e5a67952
MS
6591 addr_bit = breakpoint_address_bits (b);
6592 if (addr_bit > print_address_bits)
6593 print_address_bits = addr_bit;
269b11a2 6594
e5a67952
MS
6595 type_len = strlen (bptype_string (b->type));
6596 if (type_len > print_type_col_width)
6597 print_type_col_width = type_len;
6598
6599 nr_printable_breakpoints++;
6600 }
6601 }
7f3b0473 6602
4a2b031d
TT
6603 {
6604 ui_out_emit_table table_emitter (uiout,
6605 opts.addressprint ? 6 : 5,
6606 nr_printable_breakpoints,
6607 "BreakpointTable");
6608
6609 if (nr_printable_breakpoints > 0)
6610 annotate_breakpoints_headers ();
6611 if (nr_printable_breakpoints > 0)
6612 annotate_field (0);
6613 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6614 if (nr_printable_breakpoints > 0)
6615 annotate_field (1);
6616 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6617 if (nr_printable_breakpoints > 0)
6618 annotate_field (2);
6619 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6620 if (nr_printable_breakpoints > 0)
6621 annotate_field (3);
6622 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6623 if (opts.addressprint)
6624 {
6625 if (nr_printable_breakpoints > 0)
6626 annotate_field (4);
6627 if (print_address_bits <= 32)
6628 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6629 else
6630 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6631 }
6632 if (nr_printable_breakpoints > 0)
6633 annotate_field (5);
6634 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6635 uiout->table_body ();
6636 if (nr_printable_breakpoints > 0)
6637 annotate_breakpoints_table ();
6638
6639 ALL_BREAKPOINTS (b)
6640 {
6641 QUIT;
6642 /* If we have a filter, only list the breakpoints it accepts. */
6643 if (filter && !filter (b))
6644 continue;
e5a67952 6645
4a2b031d
TT
6646 /* If we have an "args" string, it is a list of breakpoints to
6647 accept. Skip the others. */
e5a67952 6648
4a2b031d
TT
6649 if (args != NULL && *args != '\0')
6650 {
6651 if (allflag) /* maintenance info breakpoint */
6652 {
6653 if (parse_and_eval_long (args) != b->number)
6654 continue;
6655 }
6656 else /* all others */
6657 {
6658 if (!number_is_in_list (args, b->number))
6659 continue;
6660 }
6661 }
6662 /* We only print out user settable breakpoints unless the
6663 allflag is set. */
6664 if (allflag || user_breakpoint_p (b))
6665 print_one_breakpoint (b, &last_loc, allflag);
6666 }
6667 }
698384cd 6668
7f3b0473 6669 if (nr_printable_breakpoints == 0)
c906108c 6670 {
4a64f543
MS
6671 /* If there's a filter, let the caller decide how to report
6672 empty list. */
d77f58be
SS
6673 if (!filter)
6674 {
e5a67952 6675 if (args == NULL || *args == '\0')
112e8700 6676 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6677 else
112e8700 6678 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6679 args);
d77f58be 6680 }
c906108c
SS
6681 }
6682 else
c4093a6a 6683 {
a6d9a66e
UW
6684 if (last_loc && !server_command)
6685 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6686 }
c906108c 6687
4a64f543 6688 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6689 there have been breakpoints? */
c906108c 6690 annotate_breakpoints_table_end ();
d77f58be
SS
6691
6692 return nr_printable_breakpoints;
c906108c
SS
6693}
6694
ad443146
SS
6695/* Display the value of default-collect in a way that is generally
6696 compatible with the breakpoint list. */
6697
6698static void
6699default_collect_info (void)
6700{
79a45e25
PA
6701 struct ui_out *uiout = current_uiout;
6702
ad443146
SS
6703 /* If it has no value (which is frequently the case), say nothing; a
6704 message like "No default-collect." gets in user's face when it's
6705 not wanted. */
6706 if (!*default_collect)
6707 return;
6708
6709 /* The following phrase lines up nicely with per-tracepoint collect
6710 actions. */
112e8700
SM
6711 uiout->text ("default collect ");
6712 uiout->field_string ("default-collect", default_collect);
6713 uiout->text (" \n");
ad443146
SS
6714}
6715
c906108c 6716static void
11db9430 6717info_breakpoints_command (char *args, int from_tty)
c906108c 6718{
e5a67952 6719 breakpoint_1 (args, 0, NULL);
ad443146
SS
6720
6721 default_collect_info ();
d77f58be
SS
6722}
6723
6724static void
11db9430 6725info_watchpoints_command (char *args, int from_tty)
d77f58be 6726{
e5a67952 6727 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6728 struct ui_out *uiout = current_uiout;
d77f58be
SS
6729
6730 if (num_printed == 0)
6731 {
e5a67952 6732 if (args == NULL || *args == '\0')
112e8700 6733 uiout->message ("No watchpoints.\n");
d77f58be 6734 else
112e8700 6735 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6736 }
c906108c
SS
6737}
6738
7a292a7a 6739static void
4495129a 6740maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6741{
e5a67952 6742 breakpoint_1 (args, 1, NULL);
ad443146
SS
6743
6744 default_collect_info ();
c906108c
SS
6745}
6746
0d381245 6747static int
714835d5 6748breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6749 struct program_space *pspace,
714835d5 6750 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6751{
6752 struct bp_location *bl = b->loc;
cc59ec59 6753
0d381245
VP
6754 for (; bl; bl = bl->next)
6755 {
6c95b8df
PA
6756 if (bl->pspace == pspace
6757 && bl->address == pc
0d381245
VP
6758 && (!overlay_debugging || bl->section == section))
6759 return 1;
6760 }
6761 return 0;
6762}
6763
672f9b60 6764/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6765 concerns with logical breakpoints, so we match program spaces, not
6766 address spaces. */
c906108c
SS
6767
6768static void
6c95b8df
PA
6769describe_other_breakpoints (struct gdbarch *gdbarch,
6770 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6771 struct obj_section *section, int thread)
c906108c 6772{
52f0bd74
AC
6773 int others = 0;
6774 struct breakpoint *b;
c906108c
SS
6775
6776 ALL_BREAKPOINTS (b)
672f9b60
KP
6777 others += (user_breakpoint_p (b)
6778 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6779 if (others > 0)
6780 {
a3f17187
AC
6781 if (others == 1)
6782 printf_filtered (_("Note: breakpoint "));
6783 else /* if (others == ???) */
6784 printf_filtered (_("Note: breakpoints "));
c906108c 6785 ALL_BREAKPOINTS (b)
672f9b60 6786 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6787 {
6788 others--;
6789 printf_filtered ("%d", b->number);
6790 if (b->thread == -1 && thread != -1)
6791 printf_filtered (" (all threads)");
6792 else if (b->thread != -1)
6793 printf_filtered (" (thread %d)", b->thread);
6794 printf_filtered ("%s%s ",
059fb39f 6795 ((b->enable_state == bp_disabled
f8eba3c6 6796 || b->enable_state == bp_call_disabled)
0d381245 6797 ? " (disabled)"
0d381245
VP
6798 : ""),
6799 (others > 1) ? ","
6800 : ((others == 1) ? " and" : ""));
6801 }
a3f17187 6802 printf_filtered (_("also set at pc "));
5af949e3 6803 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6804 printf_filtered (".\n");
6805 }
6806}
6807\f
c906108c 6808
e4f237da 6809/* Return true iff it is meaningful to use the address member of
244558af
LM
6810 BPT locations. For some breakpoint types, the locations' address members
6811 are irrelevant and it makes no sense to attempt to compare them to other
6812 addresses (or use them for any other purpose either).
e4f237da 6813
4a64f543 6814 More specifically, each of the following breakpoint types will
244558af 6815 always have a zero valued location address and we don't want to mark
4a64f543 6816 breakpoints of any of these types to be a duplicate of an actual
244558af 6817 breakpoint location at address zero:
e4f237da
KB
6818
6819 bp_watchpoint
2d134ed3
PA
6820 bp_catchpoint
6821
6822*/
e4f237da
KB
6823
6824static int
6825breakpoint_address_is_meaningful (struct breakpoint *bpt)
6826{
6827 enum bptype type = bpt->type;
6828
2d134ed3
PA
6829 return (type != bp_watchpoint && type != bp_catchpoint);
6830}
6831
6832/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6833 true if LOC1 and LOC2 represent the same watchpoint location. */
6834
6835static int
4a64f543
MS
6836watchpoint_locations_match (struct bp_location *loc1,
6837 struct bp_location *loc2)
2d134ed3 6838{
3a5c3e22
PA
6839 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6840 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6841
6842 /* Both of them must exist. */
6843 gdb_assert (w1 != NULL);
6844 gdb_assert (w2 != NULL);
2bdf28a0 6845
4a64f543
MS
6846 /* If the target can evaluate the condition expression in hardware,
6847 then we we need to insert both watchpoints even if they are at
6848 the same place. Otherwise the watchpoint will only trigger when
6849 the condition of whichever watchpoint was inserted evaluates to
6850 true, not giving a chance for GDB to check the condition of the
6851 other watchpoint. */
3a5c3e22 6852 if ((w1->cond_exp
4a64f543
MS
6853 && target_can_accel_watchpoint_condition (loc1->address,
6854 loc1->length,
0cf6dd15 6855 loc1->watchpoint_type,
4d01a485 6856 w1->cond_exp.get ()))
3a5c3e22 6857 || (w2->cond_exp
4a64f543
MS
6858 && target_can_accel_watchpoint_condition (loc2->address,
6859 loc2->length,
0cf6dd15 6860 loc2->watchpoint_type,
4d01a485 6861 w2->cond_exp.get ())))
0cf6dd15
TJB
6862 return 0;
6863
85d721b8
PA
6864 /* Note that this checks the owner's type, not the location's. In
6865 case the target does not support read watchpoints, but does
6866 support access watchpoints, we'll have bp_read_watchpoint
6867 watchpoints with hw_access locations. Those should be considered
6868 duplicates of hw_read locations. The hw_read locations will
6869 become hw_access locations later. */
2d134ed3
PA
6870 return (loc1->owner->type == loc2->owner->type
6871 && loc1->pspace->aspace == loc2->pspace->aspace
6872 && loc1->address == loc2->address
6873 && loc1->length == loc2->length);
e4f237da
KB
6874}
6875
31e77af2 6876/* See breakpoint.h. */
6c95b8df 6877
31e77af2 6878int
accd0bcd
YQ
6879breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6880 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6881{
f5656ead 6882 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6883 || aspace1 == aspace2)
6884 && addr1 == addr2);
6885}
6886
f1310107
TJB
6887/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6888 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6889 matches ASPACE2. On targets that have global breakpoints, the address
6890 space doesn't really matter. */
6891
6892static int
accd0bcd
YQ
6893breakpoint_address_match_range (const address_space *aspace1,
6894 CORE_ADDR addr1,
6895 int len1, const address_space *aspace2,
f1310107
TJB
6896 CORE_ADDR addr2)
6897{
f5656ead 6898 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6899 || aspace1 == aspace2)
6900 && addr2 >= addr1 && addr2 < addr1 + len1);
6901}
6902
6903/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6904 a ranged breakpoint. In most targets, a match happens only if ASPACE
6905 matches the breakpoint's address space. On targets that have global
6906 breakpoints, the address space doesn't really matter. */
6907
6908static int
6909breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6910 const address_space *aspace,
f1310107
TJB
6911 CORE_ADDR addr)
6912{
6913 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6914 aspace, addr)
6915 || (bl->length
6916 && breakpoint_address_match_range (bl->pspace->aspace,
6917 bl->address, bl->length,
6918 aspace, addr)));
6919}
6920
d35ae833
PA
6921/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6922 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6923 match happens only if ASPACE matches the breakpoint's address
6924 space. On targets that have global breakpoints, the address space
6925 doesn't really matter. */
6926
6927static int
6928breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6929 const address_space *aspace,
d35ae833
PA
6930 CORE_ADDR addr, int len)
6931{
6932 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6933 || bl->pspace->aspace == aspace)
6934 {
6935 int bl_len = bl->length != 0 ? bl->length : 1;
6936
6937 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6938 return 1;
6939 }
6940 return 0;
6941}
6942
1e4d1764
YQ
6943/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6944 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6945 true, otherwise returns false. */
6946
6947static int
6948tracepoint_locations_match (struct bp_location *loc1,
6949 struct bp_location *loc2)
6950{
6951 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6952 /* Since tracepoint locations are never duplicated with others', tracepoint
6953 locations at the same address of different tracepoints are regarded as
6954 different locations. */
6955 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6956 else
6957 return 0;
6958}
6959
2d134ed3
PA
6960/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6961 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6962 represent the same location. */
6963
6964static int
4a64f543
MS
6965breakpoint_locations_match (struct bp_location *loc1,
6966 struct bp_location *loc2)
2d134ed3 6967{
2bdf28a0
JK
6968 int hw_point1, hw_point2;
6969
6970 /* Both of them must not be in moribund_locations. */
6971 gdb_assert (loc1->owner != NULL);
6972 gdb_assert (loc2->owner != NULL);
6973
6974 hw_point1 = is_hardware_watchpoint (loc1->owner);
6975 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6976
6977 if (hw_point1 != hw_point2)
6978 return 0;
6979 else if (hw_point1)
6980 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6981 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6982 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6983 else
f1310107
TJB
6984 /* We compare bp_location.length in order to cover ranged breakpoints. */
6985 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6986 loc2->pspace->aspace, loc2->address)
6987 && loc1->length == loc2->length);
2d134ed3
PA
6988}
6989
76897487
KB
6990static void
6991breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6992 int bnum, int have_bnum)
6993{
f63fbe86
MS
6994 /* The longest string possibly returned by hex_string_custom
6995 is 50 chars. These must be at least that big for safety. */
6996 char astr1[64];
6997 char astr2[64];
76897487 6998
bb599908
PH
6999 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7000 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7001 if (have_bnum)
8a3fe4f8 7002 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7003 bnum, astr1, astr2);
7004 else
8a3fe4f8 7005 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7006}
7007
4a64f543
MS
7008/* Adjust a breakpoint's address to account for architectural
7009 constraints on breakpoint placement. Return the adjusted address.
7010 Note: Very few targets require this kind of adjustment. For most
7011 targets, this function is simply the identity function. */
76897487
KB
7012
7013static CORE_ADDR
a6d9a66e
UW
7014adjust_breakpoint_address (struct gdbarch *gdbarch,
7015 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7016{
a6d9a66e 7017 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7018 {
7019 /* Very few targets need any kind of breakpoint adjustment. */
7020 return bpaddr;
7021 }
88f7da05
KB
7022 else if (bptype == bp_watchpoint
7023 || bptype == bp_hardware_watchpoint
7024 || bptype == bp_read_watchpoint
7025 || bptype == bp_access_watchpoint
fe798b75 7026 || bptype == bp_catchpoint)
88f7da05
KB
7027 {
7028 /* Watchpoints and the various bp_catch_* eventpoints should not
7029 have their addresses modified. */
7030 return bpaddr;
7031 }
7c16b83e
PA
7032 else if (bptype == bp_single_step)
7033 {
7034 /* Single-step breakpoints should not have their addresses
7035 modified. If there's any architectural constrain that
7036 applies to this address, then it should have already been
7037 taken into account when the breakpoint was created in the
7038 first place. If we didn't do this, stepping through e.g.,
7039 Thumb-2 IT blocks would break. */
7040 return bpaddr;
7041 }
76897487
KB
7042 else
7043 {
7044 CORE_ADDR adjusted_bpaddr;
7045
7046 /* Some targets have architectural constraints on the placement
7047 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7048 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7049
7050 /* An adjusted breakpoint address can significantly alter
7051 a user's expectations. Print a warning if an adjustment
7052 is required. */
7053 if (adjusted_bpaddr != bpaddr)
7054 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7055
7056 return adjusted_bpaddr;
7057 }
7058}
7059
5625a286 7060bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7061{
5625a286 7062 bp_location *loc = this;
7cc221ef 7063
348d480f
PA
7064 gdb_assert (ops != NULL);
7065
28010a5d
PA
7066 loc->ops = ops;
7067 loc->owner = owner;
b775012e 7068 loc->cond_bytecode = NULL;
0d381245
VP
7069 loc->shlib_disabled = 0;
7070 loc->enabled = 1;
e049a4b5 7071
28010a5d 7072 switch (owner->type)
e049a4b5
DJ
7073 {
7074 case bp_breakpoint:
7c16b83e 7075 case bp_single_step:
e049a4b5
DJ
7076 case bp_until:
7077 case bp_finish:
7078 case bp_longjmp:
7079 case bp_longjmp_resume:
e2e4d78b 7080 case bp_longjmp_call_dummy:
186c406b
TT
7081 case bp_exception:
7082 case bp_exception_resume:
e049a4b5 7083 case bp_step_resume:
2c03e5be 7084 case bp_hp_step_resume:
e049a4b5
DJ
7085 case bp_watchpoint_scope:
7086 case bp_call_dummy:
aa7d318d 7087 case bp_std_terminate:
e049a4b5
DJ
7088 case bp_shlib_event:
7089 case bp_thread_event:
7090 case bp_overlay_event:
4efc6507 7091 case bp_jit_event:
0fd8e87f 7092 case bp_longjmp_master:
aa7d318d 7093 case bp_std_terminate_master:
186c406b 7094 case bp_exception_master:
0e30163f
JK
7095 case bp_gnu_ifunc_resolver:
7096 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7097 case bp_dprintf:
e049a4b5 7098 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7099 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7100 break;
7101 case bp_hardware_breakpoint:
7102 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7103 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7104 break;
7105 case bp_hardware_watchpoint:
7106 case bp_read_watchpoint:
7107 case bp_access_watchpoint:
7108 loc->loc_type = bp_loc_hardware_watchpoint;
7109 break;
7110 case bp_watchpoint:
ce78b96d 7111 case bp_catchpoint:
15c3d785
PA
7112 case bp_tracepoint:
7113 case bp_fast_tracepoint:
0fb4aa4b 7114 case bp_static_tracepoint:
e049a4b5
DJ
7115 loc->loc_type = bp_loc_other;
7116 break;
7117 default:
e2e0b3e5 7118 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7119 }
7120
f431efe5 7121 loc->refc = 1;
28010a5d
PA
7122}
7123
7124/* Allocate a struct bp_location. */
7125
7126static struct bp_location *
7127allocate_bp_location (struct breakpoint *bpt)
7128{
348d480f
PA
7129 return bpt->ops->allocate_location (bpt);
7130}
7cc221ef 7131
f431efe5
PA
7132static void
7133free_bp_location (struct bp_location *loc)
fe3f5fa8 7134{
348d480f 7135 loc->ops->dtor (loc);
4d01a485 7136 delete loc;
fe3f5fa8
VP
7137}
7138
f431efe5
PA
7139/* Increment reference count. */
7140
7141static void
7142incref_bp_location (struct bp_location *bl)
7143{
7144 ++bl->refc;
7145}
7146
7147/* Decrement reference count. If the reference count reaches 0,
7148 destroy the bp_location. Sets *BLP to NULL. */
7149
7150static void
7151decref_bp_location (struct bp_location **blp)
7152{
0807b50c
PA
7153 gdb_assert ((*blp)->refc > 0);
7154
f431efe5
PA
7155 if (--(*blp)->refc == 0)
7156 free_bp_location (*blp);
7157 *blp = NULL;
7158}
7159
346774a9 7160/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7161
b270e6f9
TT
7162static breakpoint *
7163add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7164{
346774a9 7165 struct breakpoint *b1;
b270e6f9 7166 struct breakpoint *result = b.get ();
c906108c 7167
346774a9
PA
7168 /* Add this breakpoint to the end of the chain so that a list of
7169 breakpoints will come out in order of increasing numbers. */
7170
7171 b1 = breakpoint_chain;
7172 if (b1 == 0)
b270e6f9 7173 breakpoint_chain = b.release ();
346774a9
PA
7174 else
7175 {
7176 while (b1->next)
7177 b1 = b1->next;
b270e6f9 7178 b1->next = b.release ();
346774a9 7179 }
b270e6f9
TT
7180
7181 return result;
346774a9
PA
7182}
7183
7184/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7185
7186static void
7187init_raw_breakpoint_without_location (struct breakpoint *b,
7188 struct gdbarch *gdbarch,
28010a5d 7189 enum bptype bptype,
c0a91b2b 7190 const struct breakpoint_ops *ops)
346774a9 7191{
348d480f
PA
7192 gdb_assert (ops != NULL);
7193
28010a5d 7194 b->ops = ops;
4d28f7a8 7195 b->type = bptype;
a6d9a66e 7196 b->gdbarch = gdbarch;
c906108c
SS
7197 b->language = current_language->la_language;
7198 b->input_radix = input_radix;
d0fb5eae 7199 b->related_breakpoint = b;
346774a9
PA
7200}
7201
7202/* Helper to set_raw_breakpoint below. Creates a breakpoint
7203 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7204
7205static struct breakpoint *
7206set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7207 enum bptype bptype,
c0a91b2b 7208 const struct breakpoint_ops *ops)
346774a9 7209{
3b0871f4 7210 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7211
3b0871f4 7212 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7213 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7214}
7215
0e30163f
JK
7216/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7217 resolutions should be made as the user specified the location explicitly
7218 enough. */
7219
0d381245 7220static void
0e30163f 7221set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7222{
2bdf28a0
JK
7223 gdb_assert (loc->owner != NULL);
7224
0d381245 7225 if (loc->owner->type == bp_breakpoint
1042e4c0 7226 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7227 || is_tracepoint (loc->owner))
0d381245 7228 {
0e30163f 7229 int is_gnu_ifunc;
2c02bd72 7230 const char *function_name;
6a3a010b 7231 CORE_ADDR func_addr;
0e30163f 7232
2c02bd72 7233 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7234 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7235
7236 if (is_gnu_ifunc && !explicit_loc)
7237 {
7238 struct breakpoint *b = loc->owner;
7239
7240 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7241 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7242 &loc->requested_address))
7243 {
7244 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7245 loc->address = adjust_breakpoint_address (loc->gdbarch,
7246 loc->requested_address,
7247 b->type);
7248 }
7249 else if (b->type == bp_breakpoint && b->loc == loc
7250 && loc->next == NULL && b->related_breakpoint == b)
7251 {
7252 /* Create only the whole new breakpoint of this type but do not
7253 mess more complicated breakpoints with multiple locations. */
7254 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7255 /* Remember the resolver's address for use by the return
7256 breakpoint. */
7257 loc->related_address = func_addr;
0e30163f
JK
7258 }
7259 }
7260
2c02bd72
DE
7261 if (function_name)
7262 loc->function_name = xstrdup (function_name);
0d381245
VP
7263 }
7264}
7265
a6d9a66e 7266/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7267struct gdbarch *
a6d9a66e
UW
7268get_sal_arch (struct symtab_and_line sal)
7269{
7270 if (sal.section)
7271 return get_objfile_arch (sal.section->objfile);
7272 if (sal.symtab)
eb822aa6 7273 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7274
7275 return NULL;
7276}
7277
346774a9
PA
7278/* Low level routine for partially initializing a breakpoint of type
7279 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7280 file name, and line number are provided by SAL.
0d381245
VP
7281
7282 It is expected that the caller will complete the initialization of
7283 the newly created breakpoint struct as well as output any status
c56053d2 7284 information regarding the creation of a new breakpoint. */
0d381245 7285
346774a9
PA
7286static void
7287init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7288 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7289 const struct breakpoint_ops *ops)
0d381245 7290{
28010a5d 7291 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7292
3742cc8b 7293 add_location_to_breakpoint (b, &sal);
0d381245 7294
6c95b8df
PA
7295 if (bptype != bp_catchpoint)
7296 gdb_assert (sal.pspace != NULL);
7297
f8eba3c6
TT
7298 /* Store the program space that was used to set the breakpoint,
7299 except for ordinary breakpoints, which are independent of the
7300 program space. */
7301 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7302 b->pspace = sal.pspace;
346774a9 7303}
c906108c 7304
346774a9
PA
7305/* set_raw_breakpoint is a low level routine for allocating and
7306 partially initializing a breakpoint of type BPTYPE. The newly
7307 created breakpoint's address, section, source file name, and line
7308 number are provided by SAL. The newly created and partially
7309 initialized breakpoint is added to the breakpoint chain and
7310 is also returned as the value of this function.
7311
7312 It is expected that the caller will complete the initialization of
7313 the newly created breakpoint struct as well as output any status
7314 information regarding the creation of a new breakpoint. In
7315 particular, set_raw_breakpoint does NOT set the breakpoint
7316 number! Care should be taken to not allow an error to occur
7317 prior to completing the initialization of the breakpoint. If this
7318 should happen, a bogus breakpoint will be left on the chain. */
7319
7320struct breakpoint *
7321set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7322 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7323 const struct breakpoint_ops *ops)
346774a9 7324{
3b0871f4 7325 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7326
3b0871f4 7327 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7328 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7329}
7330
53a5351d 7331/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7332 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7333 initiated the operation. */
c906108c
SS
7334
7335void
186c406b 7336set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7337{
35df4500 7338 struct breakpoint *b, *b_tmp;
5d5658a1 7339 int thread = tp->global_num;
0fd8e87f
UW
7340
7341 /* To avoid having to rescan all objfile symbols at every step,
7342 we maintain a list of continually-inserted but always disabled
7343 longjmp "master" breakpoints. Here, we simply create momentary
7344 clones of those and enable them for the requested thread. */
35df4500 7345 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7346 if (b->pspace == current_program_space
186c406b
TT
7347 && (b->type == bp_longjmp_master
7348 || b->type == bp_exception_master))
0fd8e87f 7349 {
06edf0c0
PA
7350 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7351 struct breakpoint *clone;
cc59ec59 7352
e2e4d78b
JK
7353 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7354 after their removal. */
06edf0c0 7355 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7356 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7357 clone->thread = thread;
7358 }
186c406b
TT
7359
7360 tp->initiating_frame = frame;
c906108c
SS
7361}
7362
611c83ae 7363/* Delete all longjmp breakpoints from THREAD. */
c906108c 7364void
611c83ae 7365delete_longjmp_breakpoint (int thread)
c906108c 7366{
35df4500 7367 struct breakpoint *b, *b_tmp;
c906108c 7368
35df4500 7369 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7370 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7371 {
7372 if (b->thread == thread)
7373 delete_breakpoint (b);
7374 }
c906108c
SS
7375}
7376
f59f708a
PA
7377void
7378delete_longjmp_breakpoint_at_next_stop (int thread)
7379{
7380 struct breakpoint *b, *b_tmp;
7381
7382 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7383 if (b->type == bp_longjmp || b->type == bp_exception)
7384 {
7385 if (b->thread == thread)
7386 b->disposition = disp_del_at_next_stop;
7387 }
7388}
7389
e2e4d78b
JK
7390/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7391 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7392 pointer to any of them. Return NULL if this system cannot place longjmp
7393 breakpoints. */
7394
7395struct breakpoint *
7396set_longjmp_breakpoint_for_call_dummy (void)
7397{
7398 struct breakpoint *b, *retval = NULL;
7399
7400 ALL_BREAKPOINTS (b)
7401 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7402 {
7403 struct breakpoint *new_b;
7404
7405 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7406 &momentary_breakpoint_ops,
7407 1);
5d5658a1 7408 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7409
7410 /* Link NEW_B into the chain of RETVAL breakpoints. */
7411
7412 gdb_assert (new_b->related_breakpoint == new_b);
7413 if (retval == NULL)
7414 retval = new_b;
7415 new_b->related_breakpoint = retval;
7416 while (retval->related_breakpoint != new_b->related_breakpoint)
7417 retval = retval->related_breakpoint;
7418 retval->related_breakpoint = new_b;
7419 }
7420
7421 return retval;
7422}
7423
7424/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7425 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7426 stack.
7427
7428 You should call this function only at places where it is safe to currently
7429 unwind the whole stack. Failed stack unwind would discard live dummy
7430 frames. */
7431
7432void
b67a2c6f 7433check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7434{
7435 struct breakpoint *b, *b_tmp;
7436
7437 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7438 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7439 {
7440 struct breakpoint *dummy_b = b->related_breakpoint;
7441
7442 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7443 dummy_b = dummy_b->related_breakpoint;
7444 if (dummy_b->type != bp_call_dummy
7445 || frame_find_by_id (dummy_b->frame_id) != NULL)
7446 continue;
7447
b67a2c6f 7448 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7449
7450 while (b->related_breakpoint != b)
7451 {
7452 if (b_tmp == b->related_breakpoint)
7453 b_tmp = b->related_breakpoint->next;
7454 delete_breakpoint (b->related_breakpoint);
7455 }
7456 delete_breakpoint (b);
7457 }
7458}
7459
1900040c
MS
7460void
7461enable_overlay_breakpoints (void)
7462{
52f0bd74 7463 struct breakpoint *b;
1900040c
MS
7464
7465 ALL_BREAKPOINTS (b)
7466 if (b->type == bp_overlay_event)
7467 {
7468 b->enable_state = bp_enabled;
44702360 7469 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7470 overlay_events_enabled = 1;
1900040c
MS
7471 }
7472}
7473
7474void
7475disable_overlay_breakpoints (void)
7476{
52f0bd74 7477 struct breakpoint *b;
1900040c
MS
7478
7479 ALL_BREAKPOINTS (b)
7480 if (b->type == bp_overlay_event)
7481 {
7482 b->enable_state = bp_disabled;
44702360 7483 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7484 overlay_events_enabled = 0;
1900040c
MS
7485 }
7486}
7487
aa7d318d
TT
7488/* Set an active std::terminate breakpoint for each std::terminate
7489 master breakpoint. */
7490void
7491set_std_terminate_breakpoint (void)
7492{
35df4500 7493 struct breakpoint *b, *b_tmp;
aa7d318d 7494
35df4500 7495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7496 if (b->pspace == current_program_space
7497 && b->type == bp_std_terminate_master)
7498 {
06edf0c0 7499 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7500 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7501 }
7502}
7503
7504/* Delete all the std::terminate breakpoints. */
7505void
7506delete_std_terminate_breakpoint (void)
7507{
35df4500 7508 struct breakpoint *b, *b_tmp;
aa7d318d 7509
35df4500 7510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7511 if (b->type == bp_std_terminate)
7512 delete_breakpoint (b);
7513}
7514
c4093a6a 7515struct breakpoint *
a6d9a66e 7516create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7517{
7518 struct breakpoint *b;
c4093a6a 7519
06edf0c0
PA
7520 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7521 &internal_breakpoint_ops);
7522
b5de0fa7 7523 b->enable_state = bp_enabled;
f00aae0f 7524 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7525 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7526
44702360 7527 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7528
c4093a6a
JM
7529 return b;
7530}
7531
0101ce28
JJ
7532struct lang_and_radix
7533 {
7534 enum language lang;
7535 int radix;
7536 };
7537
4efc6507
DE
7538/* Create a breakpoint for JIT code registration and unregistration. */
7539
7540struct breakpoint *
7541create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7542{
2a7f3dff
PA
7543 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7544 &internal_breakpoint_ops);
4efc6507 7545}
0101ce28 7546
03673fc7
PP
7547/* Remove JIT code registration and unregistration breakpoint(s). */
7548
7549void
7550remove_jit_event_breakpoints (void)
7551{
7552 struct breakpoint *b, *b_tmp;
7553
7554 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7555 if (b->type == bp_jit_event
7556 && b->loc->pspace == current_program_space)
7557 delete_breakpoint (b);
7558}
7559
cae688ec
JJ
7560void
7561remove_solib_event_breakpoints (void)
7562{
35df4500 7563 struct breakpoint *b, *b_tmp;
cae688ec 7564
35df4500 7565 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7566 if (b->type == bp_shlib_event
7567 && b->loc->pspace == current_program_space)
cae688ec
JJ
7568 delete_breakpoint (b);
7569}
7570
f37f681c
PA
7571/* See breakpoint.h. */
7572
7573void
7574remove_solib_event_breakpoints_at_next_stop (void)
7575{
7576 struct breakpoint *b, *b_tmp;
7577
7578 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7579 if (b->type == bp_shlib_event
7580 && b->loc->pspace == current_program_space)
7581 b->disposition = disp_del_at_next_stop;
7582}
7583
04086b45
PA
7584/* Helper for create_solib_event_breakpoint /
7585 create_and_insert_solib_event_breakpoint. Allows specifying which
7586 INSERT_MODE to pass through to update_global_location_list. */
7587
7588static struct breakpoint *
7589create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7590 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7591{
7592 struct breakpoint *b;
7593
06edf0c0
PA
7594 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7595 &internal_breakpoint_ops);
04086b45 7596 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7597 return b;
7598}
7599
04086b45
PA
7600struct breakpoint *
7601create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7602{
7603 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7604}
7605
f37f681c
PA
7606/* See breakpoint.h. */
7607
7608struct breakpoint *
7609create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7610{
7611 struct breakpoint *b;
7612
04086b45
PA
7613 /* Explicitly tell update_global_location_list to insert
7614 locations. */
7615 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7616 if (!b->loc->inserted)
7617 {
7618 delete_breakpoint (b);
7619 return NULL;
7620 }
7621 return b;
7622}
7623
cae688ec
JJ
7624/* Disable any breakpoints that are on code in shared libraries. Only
7625 apply to enabled breakpoints, disabled ones can just stay disabled. */
7626
7627void
cb851954 7628disable_breakpoints_in_shlibs (void)
cae688ec 7629{
876fa593 7630 struct bp_location *loc, **locp_tmp;
cae688ec 7631
876fa593 7632 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7633 {
2bdf28a0 7634 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7635 struct breakpoint *b = loc->owner;
2bdf28a0 7636
4a64f543
MS
7637 /* We apply the check to all breakpoints, including disabled for
7638 those with loc->duplicate set. This is so that when breakpoint
7639 becomes enabled, or the duplicate is removed, gdb will try to
7640 insert all breakpoints. If we don't set shlib_disabled here,
7641 we'll try to insert those breakpoints and fail. */
1042e4c0 7642 if (((b->type == bp_breakpoint)
508ccb1f 7643 || (b->type == bp_jit_event)
1042e4c0 7644 || (b->type == bp_hardware_breakpoint)
d77f58be 7645 || (is_tracepoint (b)))
6c95b8df 7646 && loc->pspace == current_program_space
0d381245 7647 && !loc->shlib_disabled
6c95b8df 7648 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7649 )
0d381245
VP
7650 {
7651 loc->shlib_disabled = 1;
7652 }
cae688ec
JJ
7653 }
7654}
7655
63644780
NB
7656/* Disable any breakpoints and tracepoints that are in SOLIB upon
7657 notification of unloaded_shlib. Only apply to enabled breakpoints,
7658 disabled ones can just stay disabled. */
84acb35a 7659
75149521 7660static void
84acb35a
JJ
7661disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7662{
876fa593 7663 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7664 int disabled_shlib_breaks = 0;
7665
876fa593 7666 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7667 {
2bdf28a0 7668 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7669 struct breakpoint *b = loc->owner;
cc59ec59 7670
1e4d1764 7671 if (solib->pspace == loc->pspace
e2dd7057 7672 && !loc->shlib_disabled
1e4d1764
YQ
7673 && (((b->type == bp_breakpoint
7674 || b->type == bp_jit_event
7675 || b->type == bp_hardware_breakpoint)
7676 && (loc->loc_type == bp_loc_hardware_breakpoint
7677 || loc->loc_type == bp_loc_software_breakpoint))
7678 || is_tracepoint (b))
e2dd7057 7679 && solib_contains_address_p (solib, loc->address))
84acb35a 7680 {
e2dd7057
PP
7681 loc->shlib_disabled = 1;
7682 /* At this point, we cannot rely on remove_breakpoint
7683 succeeding so we must mark the breakpoint as not inserted
7684 to prevent future errors occurring in remove_breakpoints. */
7685 loc->inserted = 0;
8d3788bd
VP
7686
7687 /* This may cause duplicate notifications for the same breakpoint. */
7688 observer_notify_breakpoint_modified (b);
7689
e2dd7057
PP
7690 if (!disabled_shlib_breaks)
7691 {
223ffa71 7692 target_terminal::ours_for_output ();
3e43a32a
MS
7693 warning (_("Temporarily disabling breakpoints "
7694 "for unloaded shared library \"%s\""),
e2dd7057 7695 solib->so_name);
84acb35a 7696 }
e2dd7057 7697 disabled_shlib_breaks = 1;
84acb35a
JJ
7698 }
7699 }
84acb35a
JJ
7700}
7701
63644780
NB
7702/* Disable any breakpoints and tracepoints in OBJFILE upon
7703 notification of free_objfile. Only apply to enabled breakpoints,
7704 disabled ones can just stay disabled. */
7705
7706static void
7707disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7708{
7709 struct breakpoint *b;
7710
7711 if (objfile == NULL)
7712 return;
7713
d03de421
PA
7714 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7715 managed by the user with add-symbol-file/remove-symbol-file.
7716 Similarly to how breakpoints in shared libraries are handled in
7717 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7718 shlib_disabled so they end up uninserted on the next global
7719 location list update. Shared libraries not loaded by the user
7720 aren't handled here -- they're already handled in
7721 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7722 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7723 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7724 main objfile). */
7725 if ((objfile->flags & OBJF_SHARED) == 0
7726 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7727 return;
7728
7729 ALL_BREAKPOINTS (b)
7730 {
7731 struct bp_location *loc;
7732 int bp_modified = 0;
7733
7734 if (!is_breakpoint (b) && !is_tracepoint (b))
7735 continue;
7736
7737 for (loc = b->loc; loc != NULL; loc = loc->next)
7738 {
7739 CORE_ADDR loc_addr = loc->address;
7740
7741 if (loc->loc_type != bp_loc_hardware_breakpoint
7742 && loc->loc_type != bp_loc_software_breakpoint)
7743 continue;
7744
7745 if (loc->shlib_disabled != 0)
7746 continue;
7747
7748 if (objfile->pspace != loc->pspace)
7749 continue;
7750
7751 if (loc->loc_type != bp_loc_hardware_breakpoint
7752 && loc->loc_type != bp_loc_software_breakpoint)
7753 continue;
7754
7755 if (is_addr_in_objfile (loc_addr, objfile))
7756 {
7757 loc->shlib_disabled = 1;
08351840
PA
7758 /* At this point, we don't know whether the object was
7759 unmapped from the inferior or not, so leave the
7760 inserted flag alone. We'll handle failure to
7761 uninsert quietly, in case the object was indeed
7762 unmapped. */
63644780
NB
7763
7764 mark_breakpoint_location_modified (loc);
7765
7766 bp_modified = 1;
7767 }
7768 }
7769
7770 if (bp_modified)
7771 observer_notify_breakpoint_modified (b);
7772 }
7773}
7774
ce78b96d
JB
7775/* FORK & VFORK catchpoints. */
7776
e29a4733 7777/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7778 catchpoint. A breakpoint is really of this type iff its ops pointer points
7779 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7780
c1fc2657 7781struct fork_catchpoint : public breakpoint
e29a4733 7782{
e29a4733
PA
7783 /* Process id of a child process whose forking triggered this
7784 catchpoint. This field is only valid immediately after this
7785 catchpoint has triggered. */
7786 ptid_t forked_inferior_pid;
7787};
7788
4a64f543
MS
7789/* Implement the "insert" breakpoint_ops method for fork
7790 catchpoints. */
ce78b96d 7791
77b06cd7
TJB
7792static int
7793insert_catch_fork (struct bp_location *bl)
ce78b96d 7794{
dfd4cc63 7795 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7796}
7797
4a64f543
MS
7798/* Implement the "remove" breakpoint_ops method for fork
7799 catchpoints. */
ce78b96d
JB
7800
7801static int
73971819 7802remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7803{
dfd4cc63 7804 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7805}
7806
7807/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7808 catchpoints. */
7809
7810static int
f1310107 7811breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7812 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7813 const struct target_waitstatus *ws)
ce78b96d 7814{
e29a4733
PA
7815 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7816
f90263c1
TT
7817 if (ws->kind != TARGET_WAITKIND_FORKED)
7818 return 0;
7819
7820 c->forked_inferior_pid = ws->value.related_pid;
7821 return 1;
ce78b96d
JB
7822}
7823
4a64f543
MS
7824/* Implement the "print_it" breakpoint_ops method for fork
7825 catchpoints. */
ce78b96d
JB
7826
7827static enum print_stop_action
348d480f 7828print_it_catch_fork (bpstat bs)
ce78b96d 7829{
36dfb11c 7830 struct ui_out *uiout = current_uiout;
348d480f
PA
7831 struct breakpoint *b = bs->breakpoint_at;
7832 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7833
ce78b96d 7834 annotate_catchpoint (b->number);
f303dbd6 7835 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7836 if (b->disposition == disp_del)
112e8700 7837 uiout->text ("Temporary catchpoint ");
36dfb11c 7838 else
112e8700
SM
7839 uiout->text ("Catchpoint ");
7840 if (uiout->is_mi_like_p ())
36dfb11c 7841 {
112e8700
SM
7842 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7843 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7844 }
112e8700
SM
7845 uiout->field_int ("bkptno", b->number);
7846 uiout->text (" (forked process ");
7847 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7848 uiout->text ("), ");
ce78b96d
JB
7849 return PRINT_SRC_AND_LOC;
7850}
7851
4a64f543
MS
7852/* Implement the "print_one" breakpoint_ops method for fork
7853 catchpoints. */
ce78b96d
JB
7854
7855static void
a6d9a66e 7856print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7857{
e29a4733 7858 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7859 struct value_print_options opts;
79a45e25 7860 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7861
7862 get_user_print_options (&opts);
7863
4a64f543
MS
7864 /* Field 4, the address, is omitted (which makes the columns not
7865 line up too nicely with the headers, but the effect is relatively
7866 readable). */
79a45b7d 7867 if (opts.addressprint)
112e8700 7868 uiout->field_skip ("addr");
ce78b96d 7869 annotate_field (5);
112e8700 7870 uiout->text ("fork");
e29a4733 7871 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7872 {
112e8700
SM
7873 uiout->text (", process ");
7874 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7875 uiout->spaces (1);
ce78b96d 7876 }
8ac3646f 7877
112e8700
SM
7878 if (uiout->is_mi_like_p ())
7879 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7880}
7881
7882/* Implement the "print_mention" breakpoint_ops method for fork
7883 catchpoints. */
7884
7885static void
7886print_mention_catch_fork (struct breakpoint *b)
7887{
7888 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7889}
7890
6149aea9
PA
7891/* Implement the "print_recreate" breakpoint_ops method for fork
7892 catchpoints. */
7893
7894static void
7895print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7896{
7897 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7898 print_recreate_thread (b, fp);
6149aea9
PA
7899}
7900
ce78b96d
JB
7901/* The breakpoint_ops structure to be used in fork catchpoints. */
7902
2060206e 7903static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7904
4a64f543
MS
7905/* Implement the "insert" breakpoint_ops method for vfork
7906 catchpoints. */
ce78b96d 7907
77b06cd7
TJB
7908static int
7909insert_catch_vfork (struct bp_location *bl)
ce78b96d 7910{
dfd4cc63 7911 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7912}
7913
4a64f543
MS
7914/* Implement the "remove" breakpoint_ops method for vfork
7915 catchpoints. */
ce78b96d
JB
7916
7917static int
73971819 7918remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7919{
dfd4cc63 7920 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7921}
7922
7923/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7924 catchpoints. */
7925
7926static int
f1310107 7927breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7928 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7929 const struct target_waitstatus *ws)
ce78b96d 7930{
e29a4733
PA
7931 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7932
f90263c1
TT
7933 if (ws->kind != TARGET_WAITKIND_VFORKED)
7934 return 0;
7935
7936 c->forked_inferior_pid = ws->value.related_pid;
7937 return 1;
ce78b96d
JB
7938}
7939
4a64f543
MS
7940/* Implement the "print_it" breakpoint_ops method for vfork
7941 catchpoints. */
ce78b96d
JB
7942
7943static enum print_stop_action
348d480f 7944print_it_catch_vfork (bpstat bs)
ce78b96d 7945{
36dfb11c 7946 struct ui_out *uiout = current_uiout;
348d480f 7947 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7948 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7949
ce78b96d 7950 annotate_catchpoint (b->number);
f303dbd6 7951 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7952 if (b->disposition == disp_del)
112e8700 7953 uiout->text ("Temporary catchpoint ");
36dfb11c 7954 else
112e8700
SM
7955 uiout->text ("Catchpoint ");
7956 if (uiout->is_mi_like_p ())
36dfb11c 7957 {
112e8700
SM
7958 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7959 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7960 }
112e8700
SM
7961 uiout->field_int ("bkptno", b->number);
7962 uiout->text (" (vforked process ");
7963 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7964 uiout->text ("), ");
ce78b96d
JB
7965 return PRINT_SRC_AND_LOC;
7966}
7967
4a64f543
MS
7968/* Implement the "print_one" breakpoint_ops method for vfork
7969 catchpoints. */
ce78b96d
JB
7970
7971static void
a6d9a66e 7972print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7973{
e29a4733 7974 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7975 struct value_print_options opts;
79a45e25 7976 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7977
7978 get_user_print_options (&opts);
4a64f543
MS
7979 /* Field 4, the address, is omitted (which makes the columns not
7980 line up too nicely with the headers, but the effect is relatively
7981 readable). */
79a45b7d 7982 if (opts.addressprint)
112e8700 7983 uiout->field_skip ("addr");
ce78b96d 7984 annotate_field (5);
112e8700 7985 uiout->text ("vfork");
e29a4733 7986 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7987 {
112e8700
SM
7988 uiout->text (", process ");
7989 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7990 uiout->spaces (1);
ce78b96d 7991 }
8ac3646f 7992
112e8700
SM
7993 if (uiout->is_mi_like_p ())
7994 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7995}
7996
7997/* Implement the "print_mention" breakpoint_ops method for vfork
7998 catchpoints. */
7999
8000static void
8001print_mention_catch_vfork (struct breakpoint *b)
8002{
8003 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8004}
8005
6149aea9
PA
8006/* Implement the "print_recreate" breakpoint_ops method for vfork
8007 catchpoints. */
8008
8009static void
8010print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8011{
8012 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8013 print_recreate_thread (b, fp);
6149aea9
PA
8014}
8015
ce78b96d
JB
8016/* The breakpoint_ops structure to be used in vfork catchpoints. */
8017
2060206e 8018static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8019
edcc5120 8020/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8021 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8022 CATCH_SOLIB_BREAKPOINT_OPS. */
8023
c1fc2657 8024struct solib_catchpoint : public breakpoint
edcc5120 8025{
c1fc2657 8026 ~solib_catchpoint () override;
edcc5120
TT
8027
8028 /* True for "catch load", false for "catch unload". */
8029 unsigned char is_load;
8030
8031 /* Regular expression to match, if any. COMPILED is only valid when
8032 REGEX is non-NULL. */
8033 char *regex;
2d7cc5c7 8034 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8035};
8036
c1fc2657 8037solib_catchpoint::~solib_catchpoint ()
edcc5120 8038{
c1fc2657 8039 xfree (this->regex);
edcc5120
TT
8040}
8041
8042static int
8043insert_catch_solib (struct bp_location *ignore)
8044{
8045 return 0;
8046}
8047
8048static int
73971819 8049remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8050{
8051 return 0;
8052}
8053
8054static int
8055breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8056 const address_space *aspace,
edcc5120
TT
8057 CORE_ADDR bp_addr,
8058 const struct target_waitstatus *ws)
8059{
8060 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8061 struct breakpoint *other;
8062
8063 if (ws->kind == TARGET_WAITKIND_LOADED)
8064 return 1;
8065
8066 ALL_BREAKPOINTS (other)
8067 {
8068 struct bp_location *other_bl;
8069
8070 if (other == bl->owner)
8071 continue;
8072
8073 if (other->type != bp_shlib_event)
8074 continue;
8075
c1fc2657 8076 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8077 continue;
8078
8079 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8080 {
8081 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8082 return 1;
8083 }
8084 }
8085
8086 return 0;
8087}
8088
8089static void
8090check_status_catch_solib (struct bpstats *bs)
8091{
8092 struct solib_catchpoint *self
8093 = (struct solib_catchpoint *) bs->breakpoint_at;
8094 int ix;
8095
8096 if (self->is_load)
8097 {
8098 struct so_list *iter;
8099
8100 for (ix = 0;
8101 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8102 ix, iter);
8103 ++ix)
8104 {
8105 if (!self->regex
2d7cc5c7 8106 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8107 return;
8108 }
8109 }
8110 else
8111 {
8112 char *iter;
8113
8114 for (ix = 0;
8115 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8116 ix, iter);
8117 ++ix)
8118 {
8119 if (!self->regex
2d7cc5c7 8120 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8121 return;
8122 }
8123 }
8124
8125 bs->stop = 0;
8126 bs->print_it = print_it_noop;
8127}
8128
8129static enum print_stop_action
8130print_it_catch_solib (bpstat bs)
8131{
8132 struct breakpoint *b = bs->breakpoint_at;
8133 struct ui_out *uiout = current_uiout;
8134
8135 annotate_catchpoint (b->number);
f303dbd6 8136 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8137 if (b->disposition == disp_del)
112e8700 8138 uiout->text ("Temporary catchpoint ");
edcc5120 8139 else
112e8700
SM
8140 uiout->text ("Catchpoint ");
8141 uiout->field_int ("bkptno", b->number);
8142 uiout->text ("\n");
8143 if (uiout->is_mi_like_p ())
8144 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8145 print_solib_event (1);
8146 return PRINT_SRC_AND_LOC;
8147}
8148
8149static void
8150print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8151{
8152 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8153 struct value_print_options opts;
8154 struct ui_out *uiout = current_uiout;
8155 char *msg;
8156
8157 get_user_print_options (&opts);
8158 /* Field 4, the address, is omitted (which makes the columns not
8159 line up too nicely with the headers, but the effect is relatively
8160 readable). */
8161 if (opts.addressprint)
8162 {
8163 annotate_field (4);
112e8700 8164 uiout->field_skip ("addr");
edcc5120
TT
8165 }
8166
8167 annotate_field (5);
8168 if (self->is_load)
8169 {
8170 if (self->regex)
8171 msg = xstrprintf (_("load of library matching %s"), self->regex);
8172 else
8173 msg = xstrdup (_("load of library"));
8174 }
8175 else
8176 {
8177 if (self->regex)
8178 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8179 else
8180 msg = xstrdup (_("unload of library"));
8181 }
112e8700 8182 uiout->field_string ("what", msg);
edcc5120 8183 xfree (msg);
8ac3646f 8184
112e8700
SM
8185 if (uiout->is_mi_like_p ())
8186 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8187}
8188
8189static void
8190print_mention_catch_solib (struct breakpoint *b)
8191{
8192 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8193
8194 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8195 self->is_load ? "load" : "unload");
8196}
8197
8198static void
8199print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8200{
8201 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8202
8203 fprintf_unfiltered (fp, "%s %s",
8204 b->disposition == disp_del ? "tcatch" : "catch",
8205 self->is_load ? "load" : "unload");
8206 if (self->regex)
8207 fprintf_unfiltered (fp, " %s", self->regex);
8208 fprintf_unfiltered (fp, "\n");
8209}
8210
8211static struct breakpoint_ops catch_solib_breakpoint_ops;
8212
91985142
MG
8213/* Shared helper function (MI and CLI) for creating and installing
8214 a shared object event catchpoint. If IS_LOAD is non-zero then
8215 the events to be caught are load events, otherwise they are
8216 unload events. If IS_TEMP is non-zero the catchpoint is a
8217 temporary one. If ENABLED is non-zero the catchpoint is
8218 created in an enabled state. */
edcc5120 8219
91985142 8220void
a121b7c1 8221add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8222{
edcc5120 8223 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8224
edcc5120
TT
8225 if (!arg)
8226 arg = "";
f1735a53 8227 arg = skip_spaces (arg);
edcc5120 8228
36bd8eaa 8229 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8230
8231 if (*arg != '\0')
8232 {
2d7cc5c7
PA
8233 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8234 _("Invalid regexp")));
edcc5120
TT
8235 c->regex = xstrdup (arg);
8236 }
8237
8238 c->is_load = is_load;
36bd8eaa 8239 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8240 &catch_solib_breakpoint_ops);
8241
c1fc2657 8242 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8243
b270e6f9 8244 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8245}
8246
91985142
MG
8247/* A helper function that does all the work for "catch load" and
8248 "catch unload". */
8249
8250static void
8251catch_load_or_unload (char *arg, int from_tty, int is_load,
8252 struct cmd_list_element *command)
8253{
8254 int tempflag;
8255 const int enabled = 1;
8256
8257 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8258
8259 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8260}
8261
edcc5120
TT
8262static void
8263catch_load_command_1 (char *arg, int from_tty,
8264 struct cmd_list_element *command)
8265{
8266 catch_load_or_unload (arg, from_tty, 1, command);
8267}
8268
8269static void
8270catch_unload_command_1 (char *arg, int from_tty,
8271 struct cmd_list_element *command)
8272{
8273 catch_load_or_unload (arg, from_tty, 0, command);
8274}
8275
346774a9
PA
8276/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8277 is non-zero, then make the breakpoint temporary. If COND_STRING is
8278 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8279 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8280
ab04a2af 8281void
346774a9
PA
8282init_catchpoint (struct breakpoint *b,
8283 struct gdbarch *gdbarch, int tempflag,
63160a43 8284 const char *cond_string,
c0a91b2b 8285 const struct breakpoint_ops *ops)
c906108c 8286{
51abb421 8287 symtab_and_line sal;
6c95b8df 8288 sal.pspace = current_program_space;
c5aa993b 8289
28010a5d 8290 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8291
1b36a34b 8292 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8293 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8294}
8295
28010a5d 8296void
b270e6f9 8297install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8298{
b270e6f9 8299 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8300 set_breakpoint_number (internal, b);
558a9d82
YQ
8301 if (is_tracepoint (b))
8302 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8303 if (!internal)
8304 mention (b);
c56053d2 8305 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8306
8307 if (update_gll)
44702360 8308 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8309}
8310
9b70b993 8311static void
a6d9a66e 8312create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8313 int tempflag, const char *cond_string,
c0a91b2b 8314 const struct breakpoint_ops *ops)
c906108c 8315{
b270e6f9 8316 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8317
b270e6f9 8318 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8319
8320 c->forked_inferior_pid = null_ptid;
8321
b270e6f9 8322 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8323}
8324
fe798b75
JB
8325/* Exec catchpoints. */
8326
b4d90040 8327/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8328 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8329 CATCH_EXEC_BREAKPOINT_OPS. */
8330
c1fc2657 8331struct exec_catchpoint : public breakpoint
b4d90040 8332{
c1fc2657 8333 ~exec_catchpoint () override;
b4d90040
PA
8334
8335 /* Filename of a program whose exec triggered this catchpoint.
8336 This field is only valid immediately after this catchpoint has
8337 triggered. */
8338 char *exec_pathname;
8339};
8340
c1fc2657 8341/* Exec catchpoint destructor. */
b4d90040 8342
c1fc2657 8343exec_catchpoint::~exec_catchpoint ()
b4d90040 8344{
c1fc2657 8345 xfree (this->exec_pathname);
b4d90040
PA
8346}
8347
77b06cd7
TJB
8348static int
8349insert_catch_exec (struct bp_location *bl)
c906108c 8350{
dfd4cc63 8351 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8352}
c906108c 8353
fe798b75 8354static int
73971819 8355remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8356{
dfd4cc63 8357 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8358}
c906108c 8359
fe798b75 8360static int
f1310107 8361breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8362 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8363 const struct target_waitstatus *ws)
fe798b75 8364{
b4d90040
PA
8365 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8366
f90263c1
TT
8367 if (ws->kind != TARGET_WAITKIND_EXECD)
8368 return 0;
8369
8370 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8371 return 1;
fe798b75 8372}
c906108c 8373
fe798b75 8374static enum print_stop_action
348d480f 8375print_it_catch_exec (bpstat bs)
fe798b75 8376{
36dfb11c 8377 struct ui_out *uiout = current_uiout;
348d480f 8378 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8379 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8380
fe798b75 8381 annotate_catchpoint (b->number);
f303dbd6 8382 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8383 if (b->disposition == disp_del)
112e8700 8384 uiout->text ("Temporary catchpoint ");
36dfb11c 8385 else
112e8700
SM
8386 uiout->text ("Catchpoint ");
8387 if (uiout->is_mi_like_p ())
36dfb11c 8388 {
112e8700
SM
8389 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8390 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8391 }
112e8700
SM
8392 uiout->field_int ("bkptno", b->number);
8393 uiout->text (" (exec'd ");
8394 uiout->field_string ("new-exec", c->exec_pathname);
8395 uiout->text ("), ");
36dfb11c 8396
fe798b75 8397 return PRINT_SRC_AND_LOC;
c906108c
SS
8398}
8399
fe798b75 8400static void
a6d9a66e 8401print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8402{
b4d90040 8403 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8404 struct value_print_options opts;
79a45e25 8405 struct ui_out *uiout = current_uiout;
fe798b75
JB
8406
8407 get_user_print_options (&opts);
8408
8409 /* Field 4, the address, is omitted (which makes the columns
8410 not line up too nicely with the headers, but the effect
8411 is relatively readable). */
8412 if (opts.addressprint)
112e8700 8413 uiout->field_skip ("addr");
fe798b75 8414 annotate_field (5);
112e8700 8415 uiout->text ("exec");
b4d90040 8416 if (c->exec_pathname != NULL)
fe798b75 8417 {
112e8700
SM
8418 uiout->text (", program \"");
8419 uiout->field_string ("what", c->exec_pathname);
8420 uiout->text ("\" ");
fe798b75 8421 }
8ac3646f 8422
112e8700
SM
8423 if (uiout->is_mi_like_p ())
8424 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8425}
8426
8427static void
8428print_mention_catch_exec (struct breakpoint *b)
8429{
8430 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8431}
8432
6149aea9
PA
8433/* Implement the "print_recreate" breakpoint_ops method for exec
8434 catchpoints. */
8435
8436static void
8437print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8438{
8439 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8440 print_recreate_thread (b, fp);
6149aea9
PA
8441}
8442
2060206e 8443static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8444
c906108c 8445static int
fba45db2 8446hw_breakpoint_used_count (void)
c906108c 8447{
c906108c 8448 int i = 0;
f1310107
TJB
8449 struct breakpoint *b;
8450 struct bp_location *bl;
c906108c
SS
8451
8452 ALL_BREAKPOINTS (b)
c5aa993b 8453 {
d6b74ac4 8454 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8455 for (bl = b->loc; bl; bl = bl->next)
8456 {
8457 /* Special types of hardware breakpoints may use more than
8458 one register. */
348d480f 8459 i += b->ops->resources_needed (bl);
f1310107 8460 }
c5aa993b 8461 }
c906108c
SS
8462
8463 return i;
8464}
8465
a1398e0c
PA
8466/* Returns the resources B would use if it were a hardware
8467 watchpoint. */
8468
c906108c 8469static int
a1398e0c 8470hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8471{
c906108c 8472 int i = 0;
e09342b5 8473 struct bp_location *bl;
c906108c 8474
a1398e0c
PA
8475 if (!breakpoint_enabled (b))
8476 return 0;
8477
8478 for (bl = b->loc; bl; bl = bl->next)
8479 {
8480 /* Special types of hardware watchpoints may use more than
8481 one register. */
8482 i += b->ops->resources_needed (bl);
8483 }
8484
8485 return i;
8486}
8487
8488/* Returns the sum the used resources of all hardware watchpoints of
8489 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8490 the sum of the used resources of all hardware watchpoints of other
8491 types _not_ TYPE. */
8492
8493static int
8494hw_watchpoint_used_count_others (struct breakpoint *except,
8495 enum bptype type, int *other_type_used)
8496{
8497 int i = 0;
8498 struct breakpoint *b;
8499
c906108c
SS
8500 *other_type_used = 0;
8501 ALL_BREAKPOINTS (b)
e09342b5 8502 {
a1398e0c
PA
8503 if (b == except)
8504 continue;
e09342b5
TJB
8505 if (!breakpoint_enabled (b))
8506 continue;
8507
a1398e0c
PA
8508 if (b->type == type)
8509 i += hw_watchpoint_use_count (b);
8510 else if (is_hardware_watchpoint (b))
8511 *other_type_used = 1;
e09342b5
TJB
8512 }
8513
c906108c
SS
8514 return i;
8515}
8516
c906108c 8517void
fba45db2 8518disable_watchpoints_before_interactive_call_start (void)
c906108c 8519{
c5aa993b 8520 struct breakpoint *b;
c906108c
SS
8521
8522 ALL_BREAKPOINTS (b)
c5aa993b 8523 {
cc60f2e3 8524 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8525 {
b5de0fa7 8526 b->enable_state = bp_call_disabled;
44702360 8527 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8528 }
8529 }
c906108c
SS
8530}
8531
8532void
fba45db2 8533enable_watchpoints_after_interactive_call_stop (void)
c906108c 8534{
c5aa993b 8535 struct breakpoint *b;
c906108c
SS
8536
8537 ALL_BREAKPOINTS (b)
c5aa993b 8538 {
cc60f2e3 8539 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8540 {
b5de0fa7 8541 b->enable_state = bp_enabled;
44702360 8542 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8543 }
8544 }
c906108c
SS
8545}
8546
8bea4e01
UW
8547void
8548disable_breakpoints_before_startup (void)
8549{
6c95b8df 8550 current_program_space->executing_startup = 1;
44702360 8551 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8552}
8553
8554void
8555enable_breakpoints_after_startup (void)
8556{
6c95b8df 8557 current_program_space->executing_startup = 0;
f8eba3c6 8558 breakpoint_re_set ();
8bea4e01
UW
8559}
8560
7c16b83e
PA
8561/* Create a new single-step breakpoint for thread THREAD, with no
8562 locations. */
c906108c 8563
7c16b83e
PA
8564static struct breakpoint *
8565new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8566{
b270e6f9 8567 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8568
b270e6f9 8569 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8570 &momentary_breakpoint_ops);
8571
8572 b->disposition = disp_donttouch;
8573 b->frame_id = null_frame_id;
8574
8575 b->thread = thread;
8576 gdb_assert (b->thread != 0);
8577
b270e6f9 8578 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8579}
8580
8581/* Set a momentary breakpoint of type TYPE at address specified by
8582 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8583 frame. */
c906108c 8584
454dafbd 8585breakpoint_up
a6d9a66e
UW
8586set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8587 struct frame_id frame_id, enum bptype type)
c906108c 8588{
52f0bd74 8589 struct breakpoint *b;
edb3359d 8590
193facb3
JK
8591 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8592 tail-called one. */
8593 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8594
06edf0c0 8595 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8596 b->enable_state = bp_enabled;
8597 b->disposition = disp_donttouch;
818dd999 8598 b->frame_id = frame_id;
c906108c 8599
4a64f543
MS
8600 /* If we're debugging a multi-threaded program, then we want
8601 momentary breakpoints to be active in only a single thread of
8602 control. */
39f77062 8603 if (in_thread_list (inferior_ptid))
5d5658a1 8604 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8605
44702360 8606 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8607
454dafbd 8608 return breakpoint_up (b);
c906108c 8609}
611c83ae 8610
06edf0c0 8611/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8612 The new breakpoint will have type TYPE, use OPS as its
8613 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8614
06edf0c0
PA
8615static struct breakpoint *
8616momentary_breakpoint_from_master (struct breakpoint *orig,
8617 enum bptype type,
a1aa2221
LM
8618 const struct breakpoint_ops *ops,
8619 int loc_enabled)
e58b0e63
PA
8620{
8621 struct breakpoint *copy;
8622
06edf0c0 8623 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8624 copy->loc = allocate_bp_location (copy);
0e30163f 8625 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8626
a6d9a66e 8627 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8628 copy->loc->requested_address = orig->loc->requested_address;
8629 copy->loc->address = orig->loc->address;
8630 copy->loc->section = orig->loc->section;
6c95b8df 8631 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8632 copy->loc->probe = orig->loc->probe;
f8eba3c6 8633 copy->loc->line_number = orig->loc->line_number;
2f202fde 8634 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8635 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8636 copy->frame_id = orig->frame_id;
8637 copy->thread = orig->thread;
6c95b8df 8638 copy->pspace = orig->pspace;
e58b0e63
PA
8639
8640 copy->enable_state = bp_enabled;
8641 copy->disposition = disp_donttouch;
8642 copy->number = internal_breakpoint_number--;
8643
44702360 8644 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8645 return copy;
8646}
8647
06edf0c0
PA
8648/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8649 ORIG is NULL. */
8650
8651struct breakpoint *
8652clone_momentary_breakpoint (struct breakpoint *orig)
8653{
8654 /* If there's nothing to clone, then return nothing. */
8655 if (orig == NULL)
8656 return NULL;
8657
a1aa2221 8658 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8659}
8660
454dafbd 8661breakpoint_up
a6d9a66e
UW
8662set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8663 enum bptype type)
611c83ae
PA
8664{
8665 struct symtab_and_line sal;
8666
8667 sal = find_pc_line (pc, 0);
8668 sal.pc = pc;
8669 sal.section = find_pc_overlay (pc);
8670 sal.explicit_pc = 1;
8671
a6d9a66e 8672 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8673}
c906108c 8674\f
c5aa993b 8675
c906108c
SS
8676/* Tell the user we have just set a breakpoint B. */
8677
8678static void
fba45db2 8679mention (struct breakpoint *b)
c906108c 8680{
348d480f 8681 b->ops->print_mention (b);
112e8700 8682 if (current_uiout->is_mi_like_p ())
fb40c209 8683 return;
c906108c
SS
8684 printf_filtered ("\n");
8685}
c906108c 8686\f
c5aa993b 8687
1a853c52
PA
8688static int bp_loc_is_permanent (struct bp_location *loc);
8689
0d381245 8690static struct bp_location *
39d61571 8691add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8692 const struct symtab_and_line *sal)
8693{
8694 struct bp_location *loc, **tmp;
3742cc8b
YQ
8695 CORE_ADDR adjusted_address;
8696 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8697
8698 if (loc_gdbarch == NULL)
8699 loc_gdbarch = b->gdbarch;
8700
8701 /* Adjust the breakpoint's address prior to allocating a location.
8702 Once we call allocate_bp_location(), that mostly uninitialized
8703 location will be placed on the location chain. Adjustment of the
8704 breakpoint may cause target_read_memory() to be called and we do
8705 not want its scan of the location chain to find a breakpoint and
8706 location that's only been partially initialized. */
8707 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8708 sal->pc, b->type);
0d381245 8709
d30113d4 8710 /* Sort the locations by their ADDRESS. */
39d61571 8711 loc = allocate_bp_location (b);
d30113d4
JK
8712 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8713 tmp = &((*tmp)->next))
0d381245 8714 ;
d30113d4 8715 loc->next = *tmp;
0d381245 8716 *tmp = loc;
3742cc8b 8717
0d381245 8718 loc->requested_address = sal->pc;
3742cc8b 8719 loc->address = adjusted_address;
6c95b8df 8720 loc->pspace = sal->pspace;
729662a5
TT
8721 loc->probe.probe = sal->probe;
8722 loc->probe.objfile = sal->objfile;
6c95b8df 8723 gdb_assert (loc->pspace != NULL);
0d381245 8724 loc->section = sal->section;
3742cc8b 8725 loc->gdbarch = loc_gdbarch;
f8eba3c6 8726 loc->line_number = sal->line;
2f202fde 8727 loc->symtab = sal->symtab;
4a27f119 8728 loc->symbol = sal->symbol;
f8eba3c6 8729
0e30163f
JK
8730 set_breakpoint_location_function (loc,
8731 sal->explicit_pc || sal->explicit_line);
1a853c52 8732
6ae88661
LM
8733 /* While by definition, permanent breakpoints are already present in the
8734 code, we don't mark the location as inserted. Normally one would expect
8735 that GDB could rely on that breakpoint instruction to stop the program,
8736 thus removing the need to insert its own breakpoint, except that executing
8737 the breakpoint instruction can kill the target instead of reporting a
8738 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8739 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8740 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8741 breakpoint be inserted normally results in QEMU knowing about the GDB
8742 breakpoint, and thus trap before the breakpoint instruction is executed.
8743 (If GDB later needs to continue execution past the permanent breakpoint,
8744 it manually increments the PC, thus avoiding executing the breakpoint
8745 instruction.) */
1a853c52 8746 if (bp_loc_is_permanent (loc))
6ae88661 8747 loc->permanent = 1;
1a853c52 8748
0d381245
VP
8749 return loc;
8750}
514f746b
AR
8751\f
8752
1cf4d951 8753/* See breakpoint.h. */
514f746b 8754
1cf4d951
PA
8755int
8756program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8757{
8758 int len;
8759 CORE_ADDR addr;
1afeeb75 8760 const gdb_byte *bpoint;
514f746b
AR
8761 gdb_byte *target_mem;
8762
1cf4d951
PA
8763 addr = address;
8764 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8765
8766 /* Software breakpoints unsupported? */
8767 if (bpoint == NULL)
8768 return 0;
8769
224c3ddb 8770 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8771
8772 /* Enable the automatic memory restoration from breakpoints while
8773 we read the memory. Otherwise we could say about our temporary
8774 breakpoints they are permanent. */
cb85b21b
TT
8775 scoped_restore restore_memory
8776 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8777
8778 if (target_read_memory (address, target_mem, len) == 0
8779 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8780 return 1;
1cf4d951 8781
cb85b21b 8782 return 0;
1cf4d951
PA
8783}
8784
8785/* Return 1 if LOC is pointing to a permanent breakpoint,
8786 return 0 otherwise. */
8787
8788static int
8789bp_loc_is_permanent (struct bp_location *loc)
8790{
514f746b
AR
8791 gdb_assert (loc != NULL);
8792
244558af
LM
8793 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8794 attempt to read from the addresses the locations of these breakpoint types
8795 point to. program_breakpoint_here_p, below, will attempt to read
8796 memory. */
8797 if (!breakpoint_address_is_meaningful (loc->owner))
8798 return 0;
8799
5ed8105e 8800 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8801 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8802 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8803}
8804
e7e0cddf
SS
8805/* Build a command list for the dprintf corresponding to the current
8806 settings of the dprintf style options. */
8807
8808static void
8809update_dprintf_command_list (struct breakpoint *b)
8810{
8811 char *dprintf_args = b->extra_string;
8812 char *printf_line = NULL;
8813
8814 if (!dprintf_args)
8815 return;
8816
8817 dprintf_args = skip_spaces (dprintf_args);
8818
8819 /* Allow a comma, as it may have terminated a location, but don't
8820 insist on it. */
8821 if (*dprintf_args == ',')
8822 ++dprintf_args;
8823 dprintf_args = skip_spaces (dprintf_args);
8824
8825 if (*dprintf_args != '"')
8826 error (_("Bad format string, missing '\"'."));
8827
d3ce09f5 8828 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8829 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8830 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8831 {
8832 if (!dprintf_function)
8833 error (_("No function supplied for dprintf call"));
8834
8835 if (dprintf_channel && strlen (dprintf_channel) > 0)
8836 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8837 dprintf_function,
8838 dprintf_channel,
8839 dprintf_args);
8840 else
8841 printf_line = xstrprintf ("call (void) %s (%s)",
8842 dprintf_function,
8843 dprintf_args);
8844 }
d3ce09f5
SS
8845 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8846 {
8847 if (target_can_run_breakpoint_commands ())
8848 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8849 else
8850 {
8851 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8852 printf_line = xstrprintf ("printf %s", dprintf_args);
8853 }
8854 }
e7e0cddf
SS
8855 else
8856 internal_error (__FILE__, __LINE__,
8857 _("Invalid dprintf style."));
8858
f28045c2 8859 gdb_assert (printf_line != NULL);
9d6e6e84 8860 /* Manufacture a printf sequence. */
f28045c2 8861 {
8d749320 8862 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8863
f28045c2
YQ
8864 printf_cmd_line->control_type = simple_control;
8865 printf_cmd_line->body_count = 0;
8866 printf_cmd_line->body_list = NULL;
9d6e6e84 8867 printf_cmd_line->next = NULL;
f28045c2 8868 printf_cmd_line->line = printf_line;
e7e0cddf 8869
93921405 8870 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8871 }
e7e0cddf
SS
8872}
8873
8874/* Update all dprintf commands, making their command lists reflect
8875 current style settings. */
8876
8877static void
8878update_dprintf_commands (char *args, int from_tty,
8879 struct cmd_list_element *c)
8880{
8881 struct breakpoint *b;
8882
8883 ALL_BREAKPOINTS (b)
8884 {
8885 if (b->type == bp_dprintf)
8886 update_dprintf_command_list (b);
8887 }
8888}
c3f6f71d 8889
f00aae0f
KS
8890/* Create a breakpoint with SAL as location. Use LOCATION
8891 as a description of the location, and COND_STRING
b35a8b2f
DE
8892 as condition expression. If LOCATION is NULL then create an
8893 "address location" from the address in the SAL. */
018d34a4
VP
8894
8895static void
d9b3f62e 8896init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8897 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8898 event_location_up &&location,
e1e01040
PA
8899 gdb::unique_xmalloc_ptr<char> filter,
8900 gdb::unique_xmalloc_ptr<char> cond_string,
8901 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8902 enum bptype type, enum bpdisp disposition,
8903 int thread, int task, int ignore_count,
c0a91b2b 8904 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8905 int enabled, int internal, unsigned flags,
8906 int display_canonical)
018d34a4 8907{
0d381245 8908 int i;
018d34a4
VP
8909
8910 if (type == bp_hardware_breakpoint)
8911 {
fbbd034e
AS
8912 int target_resources_ok;
8913
8914 i = hw_breakpoint_used_count ();
8915 target_resources_ok =
8916 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8917 i + 1, 0);
8918 if (target_resources_ok == 0)
8919 error (_("No hardware breakpoint support in the target."));
8920 else if (target_resources_ok < 0)
8921 error (_("Hardware breakpoints used exceeds limit."));
8922 }
8923
6c5b2ebe 8924 gdb_assert (!sals.empty ());
6c95b8df 8925
6c5b2ebe 8926 for (const auto &sal : sals)
0d381245 8927 {
0d381245
VP
8928 struct bp_location *loc;
8929
8930 if (from_tty)
5af949e3
UW
8931 {
8932 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8933 if (!loc_gdbarch)
8934 loc_gdbarch = gdbarch;
8935
8936 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8937 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8938 }
0d381245 8939
6c5b2ebe 8940 if (&sal == &sals[0])
0d381245 8941 {
d9b3f62e 8942 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8943 b->thread = thread;
4a306c9a 8944 b->task = task;
855a6e68 8945
e1e01040
PA
8946 b->cond_string = cond_string.release ();
8947 b->extra_string = extra_string.release ();
0d381245 8948 b->ignore_count = ignore_count;
41447f92 8949 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8950 b->disposition = disposition;
6c95b8df 8951
44f238bb
PA
8952 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8953 b->loc->inserted = 1;
8954
0fb4aa4b
PA
8955 if (type == bp_static_tracepoint)
8956 {
d9b3f62e 8957 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8958 struct static_tracepoint_marker marker;
8959
983af33b 8960 if (strace_marker_p (b))
0fb4aa4b
PA
8961 {
8962 /* We already know the marker exists, otherwise, we
8963 wouldn't see a sal for it. */
d28cd78a
TT
8964 const char *p
8965 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8966 const char *endp;
0fb4aa4b 8967 char *marker_str;
0fb4aa4b 8968
f1735a53 8969 p = skip_spaces (p);
0fb4aa4b 8970
f1735a53 8971 endp = skip_to_space (p);
0fb4aa4b
PA
8972
8973 marker_str = savestring (p, endp - p);
d9b3f62e 8974 t->static_trace_marker_id = marker_str;
0fb4aa4b 8975
3e43a32a
MS
8976 printf_filtered (_("Probed static tracepoint "
8977 "marker \"%s\"\n"),
d9b3f62e 8978 t->static_trace_marker_id);
0fb4aa4b
PA
8979 }
8980 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8981 {
d9b3f62e 8982 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8983 release_static_tracepoint_marker (&marker);
8984
3e43a32a
MS
8985 printf_filtered (_("Probed static tracepoint "
8986 "marker \"%s\"\n"),
d9b3f62e 8987 t->static_trace_marker_id);
0fb4aa4b
PA
8988 }
8989 else
3e43a32a
MS
8990 warning (_("Couldn't determine the static "
8991 "tracepoint marker to probe"));
0fb4aa4b
PA
8992 }
8993
0d381245
VP
8994 loc = b->loc;
8995 }
8996 else
018d34a4 8997 {
39d61571 8998 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8999 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9000 loc->inserted = 1;
0d381245
VP
9001 }
9002
9003 if (b->cond_string)
9004 {
bbc13ae3
KS
9005 const char *arg = b->cond_string;
9006
1bb9788d
TT
9007 loc->cond = parse_exp_1 (&arg, loc->address,
9008 block_for_pc (loc->address), 0);
0d381245 9009 if (*arg)
588ae58c 9010 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9011 }
e7e0cddf
SS
9012
9013 /* Dynamic printf requires and uses additional arguments on the
9014 command line, otherwise it's an error. */
9015 if (type == bp_dprintf)
9016 {
9017 if (b->extra_string)
9018 update_dprintf_command_list (b);
9019 else
9020 error (_("Format string required"));
9021 }
9022 else if (b->extra_string)
588ae58c 9023 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9024 }
018d34a4 9025
56435ebe 9026 b->display_canonical = display_canonical;
f00aae0f 9027 if (location != NULL)
d28cd78a 9028 b->location = std::move (location);
018d34a4 9029 else
d28cd78a 9030 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9031 b->filter = filter.release ();
d9b3f62e 9032}
018d34a4 9033
d9b3f62e
PA
9034static void
9035create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9036 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9037 event_location_up &&location,
e1e01040
PA
9038 gdb::unique_xmalloc_ptr<char> filter,
9039 gdb::unique_xmalloc_ptr<char> cond_string,
9040 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9041 enum bptype type, enum bpdisp disposition,
9042 int thread, int task, int ignore_count,
c0a91b2b 9043 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9044 int enabled, int internal, unsigned flags,
9045 int display_canonical)
d9b3f62e 9046{
a5e364af 9047 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9048
a5e364af 9049 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9050 sals, std::move (location),
e1e01040
PA
9051 std::move (filter),
9052 std::move (cond_string),
9053 std::move (extra_string),
d9b3f62e
PA
9054 type, disposition,
9055 thread, task, ignore_count,
9056 ops, from_tty,
44f238bb
PA
9057 enabled, internal, flags,
9058 display_canonical);
d9b3f62e 9059
b270e6f9 9060 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9061}
9062
9063/* Add SALS.nelts breakpoints to the breakpoint table. For each
9064 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9065 value. COND_STRING, if not NULL, specified the condition to be
9066 used for all breakpoints. Essentially the only case where
9067 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9068 function. In that case, it's still not possible to specify
9069 separate conditions for different overloaded functions, so
9070 we take just a single condition string.
9071
c3f6f71d 9072 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9073 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9074 array contents). If the function fails (error() is called), the
9075 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9076 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9077
9078static void
8cdf0e15 9079create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9080 struct linespec_result *canonical,
e1e01040
PA
9081 gdb::unique_xmalloc_ptr<char> cond_string,
9082 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9083 enum bptype type, enum bpdisp disposition,
9084 int thread, int task, int ignore_count,
c0a91b2b 9085 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9086 int enabled, int internal, unsigned flags)
c906108c 9087{
f8eba3c6 9088 if (canonical->pre_expanded)
6c5b2ebe 9089 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9090
6c5b2ebe 9091 for (const auto &lsal : canonical->lsals)
c3f6f71d 9092 {
f00aae0f 9093 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9094 'break', without arguments. */
ffc2605c 9095 event_location_up location
f00aae0f 9096 = (canonical->location != NULL
8e9e35b1 9097 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9098 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9099 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9100
6c5b2ebe 9101 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9102 std::move (location),
e1e01040
PA
9103 std::move (filter_string),
9104 std::move (cond_string),
9105 std::move (extra_string),
e7e0cddf 9106 type, disposition,
84f4c1fe 9107 thread, task, ignore_count, ops,
44f238bb 9108 from_tty, enabled, internal, flags,
56435ebe 9109 canonical->special_display);
c3f6f71d 9110 }
c3f6f71d 9111}
c906108c 9112
f00aae0f 9113/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9114 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9115 addresses found. LOCATION points to the end of the SAL (for
9116 linespec locations).
9998af43
TJB
9117
9118 The array and the line spec strings are allocated on the heap, it is
9119 the caller's responsibility to free them. */
c906108c 9120
b9362cc7 9121static void
f00aae0f 9122parse_breakpoint_sals (const struct event_location *location,
58438ac1 9123 struct linespec_result *canonical)
c3f6f71d 9124{
f00aae0f
KS
9125 struct symtab_and_line cursal;
9126
9127 if (event_location_type (location) == LINESPEC_LOCATION)
9128 {
9129 const char *address = get_linespec_location (location);
9130
9131 if (address == NULL)
9132 {
9133 /* The last displayed codepoint, if it's valid, is our default
9134 breakpoint address. */
9135 if (last_displayed_sal_is_valid ())
9136 {
f00aae0f
KS
9137 /* Set sal's pspace, pc, symtab, and line to the values
9138 corresponding to the last call to print_frame_info.
9139 Be sure to reinitialize LINE with NOTCURRENT == 0
9140 as the breakpoint line number is inappropriate otherwise.
9141 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9142 symtab_and_line sal = get_last_displayed_sal ();
9143 CORE_ADDR pc = sal.pc;
9144
f00aae0f
KS
9145 sal = find_pc_line (pc, 0);
9146
9147 /* "break" without arguments is equivalent to "break *PC"
9148 where PC is the last displayed codepoint's address. So
9149 make sure to set sal.explicit_pc to prevent GDB from
9150 trying to expand the list of sals to include all other
9151 instances with the same symtab and line. */
9152 sal.pc = pc;
9153 sal.explicit_pc = 1;
9154
6c5b2ebe
PA
9155 struct linespec_sals lsal;
9156 lsal.sals = {sal};
f00aae0f
KS
9157 lsal.canonical = NULL;
9158
6c5b2ebe 9159 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9160 return;
9161 }
9162 else
9163 error (_("No default breakpoint address now."));
c906108c 9164 }
c906108c 9165 }
f00aae0f
KS
9166
9167 /* Force almost all breakpoints to be in terms of the
9168 current_source_symtab (which is decode_line_1's default).
9169 This should produce the results we want almost all of the
9170 time while leaving default_breakpoint_* alone.
9171
9172 ObjC: However, don't match an Objective-C method name which
9173 may have a '+' or '-' succeeded by a '['. */
9174 cursal = get_current_source_symtab_and_line ();
9175 if (last_displayed_sal_is_valid ())
c906108c 9176 {
f00aae0f 9177 const char *address = NULL;
cc80f267 9178
f00aae0f
KS
9179 if (event_location_type (location) == LINESPEC_LOCATION)
9180 address = get_linespec_location (location);
cc80f267 9181
f00aae0f
KS
9182 if (!cursal.symtab
9183 || (address != NULL
9184 && strchr ("+-", address[0]) != NULL
9185 && address[1] != '['))
9186 {
c2f4122d 9187 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9188 get_last_displayed_symtab (),
9189 get_last_displayed_line (),
9190 canonical, NULL, NULL);
9191 return;
9192 }
c906108c 9193 }
f00aae0f 9194
c2f4122d 9195 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9196 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9197}
c906108c 9198
c906108c 9199
c3f6f71d 9200/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9201 inserted as a breakpoint. If it can't throw an error. */
c906108c 9202
b9362cc7 9203static void
6c5b2ebe 9204breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9205{
6c5b2ebe
PA
9206 for (auto &sal : sals)
9207 resolve_sal_pc (&sal);
c3f6f71d
JM
9208}
9209
7a697b8d
SS
9210/* Fast tracepoints may have restrictions on valid locations. For
9211 instance, a fast tracepoint using a jump instead of a trap will
9212 likely have to overwrite more bytes than a trap would, and so can
9213 only be placed where the instruction is longer than the jump, or a
9214 multi-instruction sequence does not have a jump into the middle of
9215 it, etc. */
9216
9217static void
9218check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9219 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9220{
6c5b2ebe 9221 int rslt;
7a697b8d
SS
9222 char *msg;
9223 struct cleanup *old_chain;
9224
6c5b2ebe 9225 for (const auto &sal : sals)
7a697b8d 9226 {
f8eba3c6
TT
9227 struct gdbarch *sarch;
9228
6c5b2ebe 9229 sarch = get_sal_arch (sal);
f8eba3c6
TT
9230 /* We fall back to GDBARCH if there is no architecture
9231 associated with SAL. */
9232 if (sarch == NULL)
9233 sarch = gdbarch;
6c5b2ebe 9234 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9235 old_chain = make_cleanup (xfree, msg);
9236
9237 if (!rslt)
53c3572a 9238 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9239 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9240
9241 do_cleanups (old_chain);
9242 }
9243}
9244
018d34a4
VP
9245/* Given TOK, a string specification of condition and thread, as
9246 accepted by the 'break' command, extract the condition
9247 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9248 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9249 If no condition is found, *COND_STRING is set to NULL.
9250 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9251
9252static void
bbc13ae3 9253find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9254 char **cond_string, int *thread, int *task,
9255 char **rest)
018d34a4
VP
9256{
9257 *cond_string = NULL;
9258 *thread = -1;
ed1d1739
KS
9259 *task = 0;
9260 *rest = NULL;
9261
018d34a4
VP
9262 while (tok && *tok)
9263 {
bbc13ae3 9264 const char *end_tok;
018d34a4 9265 int toklen;
bbc13ae3
KS
9266 const char *cond_start = NULL;
9267 const char *cond_end = NULL;
cc59ec59 9268
f1735a53 9269 tok = skip_spaces (tok);
e7e0cddf
SS
9270
9271 if ((*tok == '"' || *tok == ',') && rest)
9272 {
9273 *rest = savestring (tok, strlen (tok));
9274 return;
9275 }
9276
f1735a53 9277 end_tok = skip_to_space (tok);
d634f2de 9278
018d34a4 9279 toklen = end_tok - tok;
d634f2de 9280
018d34a4
VP
9281 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9282 {
9283 tok = cond_start = end_tok + 1;
4d01a485 9284 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9285 cond_end = tok;
d634f2de 9286 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9287 }
9288 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9289 {
5d5658a1
PA
9290 const char *tmptok;
9291 struct thread_info *thr;
d634f2de 9292
018d34a4 9293 tok = end_tok + 1;
5d5658a1 9294 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9295 if (tok == tmptok)
9296 error (_("Junk after thread keyword."));
5d5658a1 9297 *thread = thr->global_num;
bbc13ae3 9298 tok = tmptok;
018d34a4 9299 }
4a306c9a
JB
9300 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9301 {
9302 char *tmptok;
9303
9304 tok = end_tok + 1;
bbc13ae3 9305 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9306 if (tok == tmptok)
9307 error (_("Junk after task keyword."));
9308 if (!valid_task_id (*task))
b6199126 9309 error (_("Unknown task %d."), *task);
bbc13ae3 9310 tok = tmptok;
4a306c9a 9311 }
e7e0cddf
SS
9312 else if (rest)
9313 {
9314 *rest = savestring (tok, strlen (tok));
ccab2054 9315 return;
e7e0cddf 9316 }
018d34a4
VP
9317 else
9318 error (_("Junk at end of arguments."));
9319 }
9320}
9321
0fb4aa4b
PA
9322/* Decode a static tracepoint marker spec. */
9323
6c5b2ebe 9324static std::vector<symtab_and_line>
f00aae0f 9325decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9326{
9327 VEC(static_tracepoint_marker_p) *markers = NULL;
f00aae0f
KS
9328 const char *p = &(*arg_p)[3];
9329 const char *endp;
0fb4aa4b
PA
9330 int i;
9331
f1735a53 9332 p = skip_spaces (p);
0fb4aa4b 9333
f1735a53 9334 endp = skip_to_space (p);
0fb4aa4b 9335
81b1e71c 9336 std::string marker_str (p, endp - p);
0fb4aa4b 9337
81b1e71c 9338 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
0fb4aa4b 9339 if (VEC_empty(static_tracepoint_marker_p, markers))
81b1e71c
TT
9340 error (_("No known static tracepoint marker named %s"),
9341 marker_str.c_str ());
0fb4aa4b 9342
6c5b2ebe
PA
9343 std::vector<symtab_and_line> sals;
9344 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9345
6c5b2ebe 9346 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9347 {
9348 struct static_tracepoint_marker *marker;
9349
9350 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9351
51abb421 9352 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9353 sal.pc = marker->address;
9354 sals.push_back (sal);
0fb4aa4b
PA
9355
9356 release_static_tracepoint_marker (marker);
9357 }
9358
0fb4aa4b
PA
9359 *arg_p = endp;
9360 return sals;
9361}
9362
f00aae0f 9363/* See breakpoint.h. */
0101ce28 9364
8cdf0e15
VP
9365int
9366create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9367 const struct event_location *location,
9368 const char *cond_string,
9369 int thread, const char *extra_string,
f00aae0f 9370 int parse_extra,
0fb4aa4b 9371 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9372 int ignore_count,
9373 enum auto_boolean pending_break_support,
c0a91b2b 9374 const struct breakpoint_ops *ops,
44f238bb
PA
9375 int from_tty, int enabled, int internal,
9376 unsigned flags)
c3f6f71d 9377{
7efd8fc2 9378 struct linespec_result canonical;
80c99de1 9379 struct cleanup *bkpt_chain = NULL;
0101ce28 9380 int pending = 0;
4a306c9a 9381 int task = 0;
86b17b60 9382 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9383
348d480f
PA
9384 gdb_assert (ops != NULL);
9385
f00aae0f
KS
9386 /* If extra_string isn't useful, set it to NULL. */
9387 if (extra_string != NULL && *extra_string == '\0')
9388 extra_string = NULL;
9389
492d29ea 9390 TRY
b78a6381 9391 {
f00aae0f 9392 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9393 }
492d29ea 9394 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9395 {
492d29ea
PA
9396 /* If caller is interested in rc value from parse, set
9397 value. */
9398 if (e.error == NOT_FOUND_ERROR)
0101ce28 9399 {
05ff989b
AC
9400 /* If pending breakpoint support is turned off, throw
9401 error. */
fa8d40ab
JJ
9402
9403 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9404 throw_exception (e);
9405
9406 exception_print (gdb_stderr, e);
fa8d40ab 9407
05ff989b
AC
9408 /* If pending breakpoint support is auto query and the user
9409 selects no, then simply return the error code. */
059fb39f 9410 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9411 && !nquery (_("Make %s pending on future shared library load? "),
9412 bptype_string (type_wanted)))
fd9b8c24 9413 return 0;
fa8d40ab 9414
05ff989b
AC
9415 /* At this point, either the user was queried about setting
9416 a pending breakpoint and selected yes, or pending
9417 breakpoint behavior is on and thus a pending breakpoint
9418 is defaulted on behalf of the user. */
f00aae0f 9419 pending = 1;
0101ce28 9420 }
492d29ea
PA
9421 else
9422 throw_exception (e);
0101ce28 9423 }
492d29ea
PA
9424 END_CATCH
9425
6c5b2ebe 9426 if (!pending && canonical.lsals.empty ())
492d29ea 9427 return 0;
c3f6f71d 9428
c3f6f71d
JM
9429 /* ----------------------------- SNIP -----------------------------
9430 Anything added to the cleanup chain beyond this point is assumed
9431 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9432 then the memory is not reclaimed. */
9433 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9434
c3f6f71d
JM
9435 /* Resolve all line numbers to PC's and verify that the addresses
9436 are ok for the target. */
0101ce28 9437 if (!pending)
f8eba3c6 9438 {
6c5b2ebe
PA
9439 for (auto &lsal : canonical.lsals)
9440 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9441 }
c3f6f71d 9442
7a697b8d 9443 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9444 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9445 {
6c5b2ebe
PA
9446 for (const auto &lsal : canonical.lsals)
9447 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9448 }
7a697b8d 9449
c3f6f71d
JM
9450 /* Verify that condition can be parsed, before setting any
9451 breakpoints. Allocate a separate condition expression for each
4a64f543 9452 breakpoint. */
0101ce28 9453 if (!pending)
c3f6f71d 9454 {
e1e01040
PA
9455 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9456 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9457
f00aae0f 9458 if (parse_extra)
72b2ff0e 9459 {
0878d0fa 9460 char *rest;
e1e01040 9461 char *cond;
52d361e1 9462
6c5b2ebe 9463 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9464
0878d0fa
YQ
9465 /* Here we only parse 'arg' to separate condition
9466 from thread number, so parsing in context of first
9467 sal is OK. When setting the breakpoint we'll
9468 re-parse it in context of each sal. */
9469
6c5b2ebe 9470 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9471 &cond, &thread, &task, &rest);
9472 cond_string_copy.reset (cond);
9473 extra_string_copy.reset (rest);
72b2ff0e 9474 }
2f069f6f 9475 else
72b2ff0e 9476 {
f00aae0f
KS
9477 if (type_wanted != bp_dprintf
9478 && extra_string != NULL && *extra_string != '\0')
9479 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9480
9481 /* Create a private copy of condition string. */
9482 if (cond_string)
e1e01040 9483 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9484 /* Create a private copy of any extra string. */
9485 if (extra_string)
e1e01040 9486 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9487 }
0fb4aa4b 9488
52d361e1 9489 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9490 std::move (cond_string_copy),
9491 std::move (extra_string_copy),
9492 type_wanted,
d9b3f62e
PA
9493 tempflag ? disp_del : disp_donttouch,
9494 thread, task, ignore_count, ops,
44f238bb 9495 from_tty, enabled, internal, flags);
c906108c 9496 }
0101ce28
JJ
9497 else
9498 {
a5e364af 9499 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9500
a5e364af 9501 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9502 b->location = copy_event_location (location);
bfccc43c 9503
f00aae0f
KS
9504 if (parse_extra)
9505 b->cond_string = NULL;
e12c7713
MK
9506 else
9507 {
9508 /* Create a private copy of condition string. */
e1e01040 9509 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9510 b->thread = thread;
e12c7713 9511 }
f00aae0f
KS
9512
9513 /* Create a private copy of any extra string. */
e1e01040 9514 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9515 b->ignore_count = ignore_count;
0101ce28 9516 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9517 b->condition_not_parsed = 1;
41447f92 9518 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9519 if ((type_wanted != bp_breakpoint
9520 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9521 b->pspace = current_program_space;
8bea4e01 9522
b270e6f9 9523 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9524 }
9525
6c5b2ebe 9526 if (canonical.lsals.size () > 1)
95a42b64 9527 {
3e43a32a
MS
9528 warning (_("Multiple breakpoints were set.\nUse the "
9529 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9530 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9531 }
9532
80c99de1
PA
9533 /* That's it. Discard the cleanups for data inserted into the
9534 breakpoint. */
9535 discard_cleanups (bkpt_chain);
217dc9e2 9536
80c99de1 9537 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9538 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9539
9540 return 1;
c3f6f71d 9541}
c906108c 9542
348d480f 9543/* Set a breakpoint.
72b2ff0e
VP
9544 ARG is a string describing breakpoint address,
9545 condition, and thread.
9546 FLAG specifies if a breakpoint is hardware on,
9547 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9548 and BP_TEMPFLAG. */
348d480f 9549
98deb0da 9550static void
f2fc3015 9551break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9552{
72b2ff0e 9553 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9554 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9555 ? bp_hardware_breakpoint
9556 : bp_breakpoint);
55aa24fb 9557 struct breakpoint_ops *ops;
f00aae0f 9558
ffc2605c 9559 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9560
9561 /* Matching breakpoints on probes. */
5b56227b 9562 if (location != NULL
ffc2605c 9563 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9564 ops = &bkpt_probe_breakpoint_ops;
9565 else
9566 ops = &bkpt_breakpoint_ops;
c3f6f71d 9567
8cdf0e15 9568 create_breakpoint (get_current_arch (),
ffc2605c 9569 location.get (),
f00aae0f 9570 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9571 tempflag, type_wanted,
8cdf0e15
VP
9572 0 /* Ignore count */,
9573 pending_break_support,
55aa24fb 9574 ops,
8cdf0e15 9575 from_tty,
84f4c1fe 9576 1 /* enabled */,
44f238bb
PA
9577 0 /* internal */,
9578 0);
c906108c
SS
9579}
9580
c906108c
SS
9581/* Helper function for break_command_1 and disassemble_command. */
9582
9583void
fba45db2 9584resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9585{
9586 CORE_ADDR pc;
9587
9588 if (sal->pc == 0 && sal->symtab != NULL)
9589 {
9590 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9591 error (_("No line %d in file \"%s\"."),
05cba821 9592 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9593 sal->pc = pc;
6a048695 9594
4a64f543
MS
9595 /* If this SAL corresponds to a breakpoint inserted using a line
9596 number, then skip the function prologue if necessary. */
6a048695 9597 if (sal->explicit_line)
059acae7 9598 skip_prologue_sal (sal);
c906108c
SS
9599 }
9600
9601 if (sal->section == 0 && sal->symtab != NULL)
9602 {
346d1dfe 9603 const struct blockvector *bv;
3977b71f 9604 const struct block *b;
c5aa993b 9605 struct symbol *sym;
c906108c 9606
43f3e411
DE
9607 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9608 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9609 if (bv != NULL)
9610 {
7f0df278 9611 sym = block_linkage_function (b);
c906108c
SS
9612 if (sym != NULL)
9613 {
eb822aa6
DE
9614 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9615 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9616 sym);
c906108c
SS
9617 }
9618 else
9619 {
4a64f543
MS
9620 /* It really is worthwhile to have the section, so we'll
9621 just have to look harder. This case can be executed
9622 if we have line numbers but no functions (as can
9623 happen in assembly source). */
c906108c 9624
5ed8105e 9625 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9626 switch_to_program_space_and_thread (sal->pspace);
c906108c 9627
5ed8105e 9628 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9629 if (msym.minsym)
efd66ac6 9630 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9631 }
9632 }
9633 }
9634}
9635
9636void
fba45db2 9637break_command (char *arg, int from_tty)
c906108c 9638{
db107f19 9639 break_command_1 (arg, 0, from_tty);
c906108c
SS
9640}
9641
c906108c 9642void
fba45db2 9643tbreak_command (char *arg, int from_tty)
c906108c 9644{
db107f19 9645 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9646}
9647
c906108c 9648static void
fba45db2 9649hbreak_command (char *arg, int from_tty)
c906108c 9650{
db107f19 9651 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9652}
9653
9654static void
fba45db2 9655thbreak_command (char *arg, int from_tty)
c906108c 9656{
db107f19 9657 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9658}
9659
9660static void
fba45db2 9661stop_command (char *arg, int from_tty)
c906108c 9662{
a3f17187 9663 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9664Usage: stop in <function | address>\n\
a3f17187 9665 stop at <line>\n"));
c906108c
SS
9666}
9667
9668static void
4495129a 9669stopin_command (const char *arg, int from_tty)
c906108c
SS
9670{
9671 int badInput = 0;
9672
c5aa993b 9673 if (arg == (char *) NULL)
c906108c
SS
9674 badInput = 1;
9675 else if (*arg != '*')
9676 {
4495129a 9677 const char *argptr = arg;
c906108c
SS
9678 int hasColon = 0;
9679
4a64f543 9680 /* Look for a ':'. If this is a line number specification, then
53a5351d 9681 say it is bad, otherwise, it should be an address or
4a64f543 9682 function/method name. */
c906108c 9683 while (*argptr && !hasColon)
c5aa993b
JM
9684 {
9685 hasColon = (*argptr == ':');
9686 argptr++;
9687 }
c906108c
SS
9688
9689 if (hasColon)
c5aa993b 9690 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9691 else
c5aa993b 9692 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9693 }
9694
9695 if (badInput)
a3f17187 9696 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9697 else
db107f19 9698 break_command_1 (arg, 0, from_tty);
c906108c
SS
9699}
9700
9701static void
4495129a 9702stopat_command (const char *arg, int from_tty)
c906108c
SS
9703{
9704 int badInput = 0;
9705
c5aa993b 9706 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9707 badInput = 1;
9708 else
9709 {
4495129a 9710 const char *argptr = arg;
c906108c
SS
9711 int hasColon = 0;
9712
4a64f543
MS
9713 /* Look for a ':'. If there is a '::' then get out, otherwise
9714 it is probably a line number. */
c906108c 9715 while (*argptr && !hasColon)
c5aa993b
JM
9716 {
9717 hasColon = (*argptr == ':');
9718 argptr++;
9719 }
c906108c
SS
9720
9721 if (hasColon)
c5aa993b 9722 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9723 else
c5aa993b 9724 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9725 }
9726
9727 if (badInput)
a3f17187 9728 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9729 else
db107f19 9730 break_command_1 (arg, 0, from_tty);
c906108c
SS
9731}
9732
e7e0cddf
SS
9733/* The dynamic printf command is mostly like a regular breakpoint, but
9734 with a prewired command list consisting of a single output command,
9735 built from extra arguments supplied on the dprintf command
9736 line. */
9737
da821c7b 9738static void
f2fc3015 9739dprintf_command (char *arg_in, int from_tty)
e7e0cddf 9740{
f2fc3015 9741 const char *arg = arg_in;
ffc2605c 9742 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9743
9744 /* If non-NULL, ARG should have been advanced past the location;
9745 the next character must be ','. */
9746 if (arg != NULL)
9747 {
9748 if (arg[0] != ',' || arg[1] == '\0')
9749 error (_("Format string required"));
9750 else
9751 {
9752 /* Skip the comma. */
9753 ++arg;
9754 }
9755 }
9756
e7e0cddf 9757 create_breakpoint (get_current_arch (),
ffc2605c 9758 location.get (),
f00aae0f 9759 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9760 0, bp_dprintf,
9761 0 /* Ignore count */,
9762 pending_break_support,
9763 &dprintf_breakpoint_ops,
9764 from_tty,
9765 1 /* enabled */,
9766 0 /* internal */,
9767 0);
9768}
9769
d3ce09f5
SS
9770static void
9771agent_printf_command (char *arg, int from_tty)
9772{
9773 error (_("May only run agent-printf on the target"));
9774}
9775
f1310107
TJB
9776/* Implement the "breakpoint_hit" breakpoint_ops method for
9777 ranged breakpoints. */
9778
9779static int
9780breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9781 const address_space *aspace,
09ac7c10
TT
9782 CORE_ADDR bp_addr,
9783 const struct target_waitstatus *ws)
f1310107 9784{
09ac7c10 9785 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9786 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9787 return 0;
9788
f1310107
TJB
9789 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9790 bl->length, aspace, bp_addr);
9791}
9792
9793/* Implement the "resources_needed" breakpoint_ops method for
9794 ranged breakpoints. */
9795
9796static int
9797resources_needed_ranged_breakpoint (const struct bp_location *bl)
9798{
9799 return target_ranged_break_num_registers ();
9800}
9801
9802/* Implement the "print_it" breakpoint_ops method for
9803 ranged breakpoints. */
9804
9805static enum print_stop_action
348d480f 9806print_it_ranged_breakpoint (bpstat bs)
f1310107 9807{
348d480f 9808 struct breakpoint *b = bs->breakpoint_at;
f1310107 9809 struct bp_location *bl = b->loc;
79a45e25 9810 struct ui_out *uiout = current_uiout;
f1310107
TJB
9811
9812 gdb_assert (b->type == bp_hardware_breakpoint);
9813
9814 /* Ranged breakpoints have only one location. */
9815 gdb_assert (bl && bl->next == NULL);
9816
9817 annotate_breakpoint (b->number);
f303dbd6
PA
9818
9819 maybe_print_thread_hit_breakpoint (uiout);
9820
f1310107 9821 if (b->disposition == disp_del)
112e8700 9822 uiout->text ("Temporary ranged breakpoint ");
f1310107 9823 else
112e8700
SM
9824 uiout->text ("Ranged breakpoint ");
9825 if (uiout->is_mi_like_p ())
f1310107 9826 {
112e8700 9827 uiout->field_string ("reason",
f1310107 9828 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9829 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9830 }
112e8700
SM
9831 uiout->field_int ("bkptno", b->number);
9832 uiout->text (", ");
f1310107
TJB
9833
9834 return PRINT_SRC_AND_LOC;
9835}
9836
9837/* Implement the "print_one" breakpoint_ops method for
9838 ranged breakpoints. */
9839
9840static void
9841print_one_ranged_breakpoint (struct breakpoint *b,
9842 struct bp_location **last_loc)
9843{
9844 struct bp_location *bl = b->loc;
9845 struct value_print_options opts;
79a45e25 9846 struct ui_out *uiout = current_uiout;
f1310107
TJB
9847
9848 /* Ranged breakpoints have only one location. */
9849 gdb_assert (bl && bl->next == NULL);
9850
9851 get_user_print_options (&opts);
9852
9853 if (opts.addressprint)
9854 /* We don't print the address range here, it will be printed later
9855 by print_one_detail_ranged_breakpoint. */
112e8700 9856 uiout->field_skip ("addr");
f1310107
TJB
9857 annotate_field (5);
9858 print_breakpoint_location (b, bl);
9859 *last_loc = bl;
9860}
9861
9862/* Implement the "print_one_detail" breakpoint_ops method for
9863 ranged breakpoints. */
9864
9865static void
9866print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9867 struct ui_out *uiout)
9868{
9869 CORE_ADDR address_start, address_end;
9870 struct bp_location *bl = b->loc;
d7e74731 9871 string_file stb;
f1310107
TJB
9872
9873 gdb_assert (bl);
9874
9875 address_start = bl->address;
9876 address_end = address_start + bl->length - 1;
9877
112e8700 9878 uiout->text ("\taddress range: ");
d7e74731
PA
9879 stb.printf ("[%s, %s]",
9880 print_core_address (bl->gdbarch, address_start),
9881 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9882 uiout->field_stream ("addr", stb);
9883 uiout->text ("\n");
f1310107
TJB
9884}
9885
9886/* Implement the "print_mention" breakpoint_ops method for
9887 ranged breakpoints. */
9888
9889static void
9890print_mention_ranged_breakpoint (struct breakpoint *b)
9891{
9892 struct bp_location *bl = b->loc;
79a45e25 9893 struct ui_out *uiout = current_uiout;
f1310107
TJB
9894
9895 gdb_assert (bl);
9896 gdb_assert (b->type == bp_hardware_breakpoint);
9897
112e8700 9898 if (uiout->is_mi_like_p ())
f1310107
TJB
9899 return;
9900
9901 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9902 b->number, paddress (bl->gdbarch, bl->address),
9903 paddress (bl->gdbarch, bl->address + bl->length - 1));
9904}
9905
9906/* Implement the "print_recreate" breakpoint_ops method for
9907 ranged breakpoints. */
9908
9909static void
9910print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9911{
f00aae0f 9912 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9913 event_location_to_string (b->location.get ()),
9914 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9915 print_recreate_thread (b, fp);
f1310107
TJB
9916}
9917
9918/* The breakpoint_ops structure to be used in ranged breakpoints. */
9919
2060206e 9920static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9921
9922/* Find the address where the end of the breakpoint range should be
9923 placed, given the SAL of the end of the range. This is so that if
9924 the user provides a line number, the end of the range is set to the
9925 last instruction of the given line. */
9926
9927static CORE_ADDR
9928find_breakpoint_range_end (struct symtab_and_line sal)
9929{
9930 CORE_ADDR end;
9931
9932 /* If the user provided a PC value, use it. Otherwise,
9933 find the address of the end of the given location. */
9934 if (sal.explicit_pc)
9935 end = sal.pc;
9936 else
9937 {
9938 int ret;
9939 CORE_ADDR start;
9940
9941 ret = find_line_pc_range (sal, &start, &end);
9942 if (!ret)
9943 error (_("Could not find location of the end of the range."));
9944
9945 /* find_line_pc_range returns the start of the next line. */
9946 end--;
9947 }
9948
9949 return end;
9950}
9951
9952/* Implement the "break-range" CLI command. */
9953
9954static void
f2fc3015 9955break_range_command (char *arg_in, int from_tty)
f1310107 9956{
f2fc3015
TT
9957 const char *arg = arg_in;
9958 const char *arg_start;
f1310107
TJB
9959 struct linespec_result canonical_start, canonical_end;
9960 int bp_count, can_use_bp, length;
9961 CORE_ADDR end;
9962 struct breakpoint *b;
f1310107
TJB
9963
9964 /* We don't support software ranged breakpoints. */
9965 if (target_ranged_break_num_registers () < 0)
9966 error (_("This target does not support hardware ranged breakpoints."));
9967
9968 bp_count = hw_breakpoint_used_count ();
9969 bp_count += target_ranged_break_num_registers ();
9970 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9971 bp_count, 0);
9972 if (can_use_bp < 0)
9973 error (_("Hardware breakpoints used exceeds limit."));
9974
f8eba3c6 9975 arg = skip_spaces (arg);
f1310107
TJB
9976 if (arg == NULL || arg[0] == '\0')
9977 error(_("No address range specified."));
9978
f8eba3c6 9979 arg_start = arg;
ffc2605c
TT
9980 event_location_up start_location = string_to_event_location (&arg,
9981 current_language);
9982 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9983
9984 if (arg[0] != ',')
9985 error (_("Too few arguments."));
6c5b2ebe 9986 else if (canonical_start.lsals.empty ())
f1310107 9987 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9988
6c5b2ebe 9989 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9990
6c5b2ebe
PA
9991 if (canonical_start.lsals.size () > 1
9992 || lsal_start.sals.size () != 1)
f1310107
TJB
9993 error (_("Cannot create a ranged breakpoint with multiple locations."));
9994
6c5b2ebe 9995 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9996 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9997
9998 arg++; /* Skip the comma. */
f8eba3c6 9999 arg = skip_spaces (arg);
f1310107
TJB
10000
10001 /* Parse the end location. */
10002
f1310107
TJB
10003 arg_start = arg;
10004
f8eba3c6 10005 /* We call decode_line_full directly here instead of using
f1310107
TJB
10006 parse_breakpoint_sals because we need to specify the start location's
10007 symtab and line as the default symtab and line for the end of the
10008 range. This makes it possible to have ranges like "foo.c:27, +14",
10009 where +14 means 14 lines from the start location. */
ffc2605c
TT
10010 event_location_up end_location = string_to_event_location (&arg,
10011 current_language);
10012 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10013 sal_start.symtab, sal_start.line,
10014 &canonical_end, NULL, NULL);
10015
6c5b2ebe 10016 if (canonical_end.lsals.empty ())
f1310107 10017 error (_("Could not find location of the end of the range."));
f8eba3c6 10018
6c5b2ebe
PA
10019 const linespec_sals &lsal_end = canonical_end.lsals[0];
10020 if (canonical_end.lsals.size () > 1
10021 || lsal_end.sals.size () != 1)
f1310107
TJB
10022 error (_("Cannot create a ranged breakpoint with multiple locations."));
10023
6c5b2ebe 10024 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10025
10026 end = find_breakpoint_range_end (sal_end);
10027 if (sal_start.pc > end)
177b42fe 10028 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10029
10030 length = end - sal_start.pc + 1;
10031 if (length < 0)
10032 /* Length overflowed. */
10033 error (_("Address range too large."));
10034 else if (length == 1)
10035 {
10036 /* This range is simple enough to be handled by
10037 the `hbreak' command. */
81b1e71c 10038 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10039
10040 return;
10041 }
10042
10043 /* Now set up the breakpoint. */
10044 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10045 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10046 set_breakpoint_count (breakpoint_count + 1);
10047 b->number = breakpoint_count;
10048 b->disposition = disp_donttouch;
d28cd78a
TT
10049 b->location = std::move (start_location);
10050 b->location_range_end = std::move (end_location);
f1310107
TJB
10051 b->loc->length = length;
10052
f1310107 10053 mention (b);
8d3788bd 10054 observer_notify_breakpoint_created (b);
44702360 10055 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10056}
10057
4a64f543
MS
10058/* Return non-zero if EXP is verified as constant. Returned zero
10059 means EXP is variable. Also the constant detection may fail for
10060 some constant expressions and in such case still falsely return
10061 zero. */
2e6e3d9c 10062
65d79d4b
SDJ
10063static int
10064watchpoint_exp_is_const (const struct expression *exp)
10065{
10066 int i = exp->nelts;
10067
10068 while (i > 0)
10069 {
10070 int oplenp, argsp;
10071
10072 /* We are only interested in the descriptor of each element. */
10073 operator_length (exp, i, &oplenp, &argsp);
10074 i -= oplenp;
10075
10076 switch (exp->elts[i].opcode)
10077 {
10078 case BINOP_ADD:
10079 case BINOP_SUB:
10080 case BINOP_MUL:
10081 case BINOP_DIV:
10082 case BINOP_REM:
10083 case BINOP_MOD:
10084 case BINOP_LSH:
10085 case BINOP_RSH:
10086 case BINOP_LOGICAL_AND:
10087 case BINOP_LOGICAL_OR:
10088 case BINOP_BITWISE_AND:
10089 case BINOP_BITWISE_IOR:
10090 case BINOP_BITWISE_XOR:
10091 case BINOP_EQUAL:
10092 case BINOP_NOTEQUAL:
10093 case BINOP_LESS:
10094 case BINOP_GTR:
10095 case BINOP_LEQ:
10096 case BINOP_GEQ:
10097 case BINOP_REPEAT:
10098 case BINOP_COMMA:
10099 case BINOP_EXP:
10100 case BINOP_MIN:
10101 case BINOP_MAX:
10102 case BINOP_INTDIV:
10103 case BINOP_CONCAT:
65d79d4b
SDJ
10104 case TERNOP_COND:
10105 case TERNOP_SLICE:
65d79d4b
SDJ
10106
10107 case OP_LONG:
edd079d9 10108 case OP_FLOAT:
65d79d4b
SDJ
10109 case OP_LAST:
10110 case OP_COMPLEX:
10111 case OP_STRING:
65d79d4b
SDJ
10112 case OP_ARRAY:
10113 case OP_TYPE:
608b4967
TT
10114 case OP_TYPEOF:
10115 case OP_DECLTYPE:
6e72ca20 10116 case OP_TYPEID:
65d79d4b
SDJ
10117 case OP_NAME:
10118 case OP_OBJC_NSSTRING:
10119
10120 case UNOP_NEG:
10121 case UNOP_LOGICAL_NOT:
10122 case UNOP_COMPLEMENT:
10123 case UNOP_ADDR:
10124 case UNOP_HIGH:
aeaa2474 10125 case UNOP_CAST:
9eaf6705
TT
10126
10127 case UNOP_CAST_TYPE:
10128 case UNOP_REINTERPRET_CAST:
10129 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10130 /* Unary, binary and ternary operators: We have to check
10131 their operands. If they are constant, then so is the
10132 result of that operation. For instance, if A and B are
10133 determined to be constants, then so is "A + B".
10134
10135 UNOP_IND is one exception to the rule above, because the
10136 value of *ADDR is not necessarily a constant, even when
10137 ADDR is. */
65d79d4b
SDJ
10138 break;
10139
10140 case OP_VAR_VALUE:
10141 /* Check whether the associated symbol is a constant.
4a64f543 10142
65d79d4b 10143 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10144 possible that a buggy compiler could mark a variable as
10145 constant even when it is not, and TYPE_CONST would return
10146 true in this case, while SYMBOL_CLASS wouldn't.
10147
10148 We also have to check for function symbols because they
10149 are always constant. */
65d79d4b
SDJ
10150 {
10151 struct symbol *s = exp->elts[i + 2].symbol;
10152
10153 if (SYMBOL_CLASS (s) != LOC_BLOCK
10154 && SYMBOL_CLASS (s) != LOC_CONST
10155 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10156 return 0;
10157 break;
10158 }
10159
10160 /* The default action is to return 0 because we are using
10161 the optimistic approach here: If we don't know something,
10162 then it is not a constant. */
10163 default:
10164 return 0;
10165 }
10166 }
10167
10168 return 1;
10169}
10170
c1fc2657 10171/* Watchpoint destructor. */
3a5c3e22 10172
c1fc2657 10173watchpoint::~watchpoint ()
3a5c3e22 10174{
c1fc2657
SM
10175 xfree (this->exp_string);
10176 xfree (this->exp_string_reparse);
10177 value_free (this->val);
3a5c3e22
PA
10178}
10179
348d480f
PA
10180/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10181
10182static void
10183re_set_watchpoint (struct breakpoint *b)
10184{
3a5c3e22
PA
10185 struct watchpoint *w = (struct watchpoint *) b;
10186
348d480f
PA
10187 /* Watchpoint can be either on expression using entirely global
10188 variables, or it can be on local variables.
10189
10190 Watchpoints of the first kind are never auto-deleted, and even
10191 persist across program restarts. Since they can use variables
10192 from shared libraries, we need to reparse expression as libraries
10193 are loaded and unloaded.
10194
10195 Watchpoints on local variables can also change meaning as result
10196 of solib event. For example, if a watchpoint uses both a local
10197 and a global variables in expression, it's a local watchpoint,
10198 but unloading of a shared library will make the expression
10199 invalid. This is not a very common use case, but we still
10200 re-evaluate expression, to avoid surprises to the user.
10201
10202 Note that for local watchpoints, we re-evaluate it only if
10203 watchpoints frame id is still valid. If it's not, it means the
10204 watchpoint is out of scope and will be deleted soon. In fact,
10205 I'm not sure we'll ever be called in this case.
10206
10207 If a local watchpoint's frame id is still valid, then
3a5c3e22 10208 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10209
3a5c3e22
PA
10210 Don't do anything about disabled watchpoints, since they will be
10211 reevaluated again when enabled. */
10212 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10213}
10214
77b06cd7
TJB
10215/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10216
10217static int
10218insert_watchpoint (struct bp_location *bl)
10219{
3a5c3e22
PA
10220 struct watchpoint *w = (struct watchpoint *) bl->owner;
10221 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10222
10223 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10224 w->cond_exp.get ());
77b06cd7
TJB
10225}
10226
10227/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10228
10229static int
73971819 10230remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10231{
3a5c3e22
PA
10232 struct watchpoint *w = (struct watchpoint *) bl->owner;
10233 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10234
10235 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10236 w->cond_exp.get ());
e09342b5
TJB
10237}
10238
e09342b5 10239static int
348d480f 10240breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10241 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10242 const struct target_waitstatus *ws)
e09342b5 10243{
348d480f 10244 struct breakpoint *b = bl->owner;
3a5c3e22 10245 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10246
348d480f
PA
10247 /* Continuable hardware watchpoints are treated as non-existent if the
10248 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10249 some data address). Otherwise gdb won't stop on a break instruction
10250 in the code (not from a breakpoint) when a hardware watchpoint has
10251 been defined. Also skip watchpoints which we know did not trigger
10252 (did not match the data address). */
10253 if (is_hardware_watchpoint (b)
3a5c3e22 10254 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10255 return 0;
9c06b0b4 10256
348d480f 10257 return 1;
9c06b0b4
TJB
10258}
10259
348d480f
PA
10260static void
10261check_status_watchpoint (bpstat bs)
9c06b0b4 10262{
348d480f 10263 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10264
348d480f 10265 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10266}
10267
10268/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10269 hardware watchpoints. */
9c06b0b4
TJB
10270
10271static int
348d480f 10272resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10273{
3a5c3e22
PA
10274 struct watchpoint *w = (struct watchpoint *) bl->owner;
10275 int length = w->exact? 1 : bl->length;
348d480f
PA
10276
10277 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10278}
10279
10280/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10281 hardware watchpoints. */
9c06b0b4
TJB
10282
10283static int
348d480f 10284works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10285{
efa80663
PA
10286 /* Read and access watchpoints only work with hardware support. */
10287 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10288}
10289
9c06b0b4 10290static enum print_stop_action
348d480f 10291print_it_watchpoint (bpstat bs)
9c06b0b4 10292{
348d480f 10293 struct breakpoint *b;
348d480f 10294 enum print_stop_action result;
3a5c3e22 10295 struct watchpoint *w;
79a45e25 10296 struct ui_out *uiout = current_uiout;
348d480f
PA
10297
10298 gdb_assert (bs->bp_location_at != NULL);
10299
348d480f 10300 b = bs->breakpoint_at;
3a5c3e22 10301 w = (struct watchpoint *) b;
348d480f 10302
f303dbd6
PA
10303 annotate_watchpoint (b->number);
10304 maybe_print_thread_hit_breakpoint (uiout);
10305
d7e74731
PA
10306 string_file stb;
10307
76f9c9cf 10308 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10309 switch (b->type)
10310 {
348d480f 10311 case bp_watchpoint:
9c06b0b4 10312 case bp_hardware_watchpoint:
112e8700
SM
10313 if (uiout->is_mi_like_p ())
10314 uiout->field_string
10315 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10316 mention (b);
76f9c9cf 10317 tuple_emitter.emplace (uiout, "value");
112e8700 10318 uiout->text ("\nOld value = ");
d7e74731 10319 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10320 uiout->field_stream ("old", stb);
10321 uiout->text ("\nNew value = ");
d7e74731 10322 watchpoint_value_print (w->val, &stb);
112e8700
SM
10323 uiout->field_stream ("new", stb);
10324 uiout->text ("\n");
348d480f
PA
10325 /* More than one watchpoint may have been triggered. */
10326 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10327 break;
10328
10329 case bp_read_watchpoint:
112e8700
SM
10330 if (uiout->is_mi_like_p ())
10331 uiout->field_string
10332 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10333 mention (b);
76f9c9cf 10334 tuple_emitter.emplace (uiout, "value");
112e8700 10335 uiout->text ("\nValue = ");
d7e74731 10336 watchpoint_value_print (w->val, &stb);
112e8700
SM
10337 uiout->field_stream ("value", stb);
10338 uiout->text ("\n");
348d480f 10339 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10340 break;
10341
10342 case bp_access_watchpoint:
348d480f
PA
10343 if (bs->old_val != NULL)
10344 {
112e8700
SM
10345 if (uiout->is_mi_like_p ())
10346 uiout->field_string
10347 ("reason",
348d480f
PA
10348 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10349 mention (b);
76f9c9cf 10350 tuple_emitter.emplace (uiout, "value");
112e8700 10351 uiout->text ("\nOld value = ");
d7e74731 10352 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10353 uiout->field_stream ("old", stb);
10354 uiout->text ("\nNew value = ");
348d480f
PA
10355 }
10356 else
10357 {
10358 mention (b);
112e8700
SM
10359 if (uiout->is_mi_like_p ())
10360 uiout->field_string
10361 ("reason",
348d480f 10362 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10363 tuple_emitter.emplace (uiout, "value");
112e8700 10364 uiout->text ("\nValue = ");
348d480f 10365 }
d7e74731 10366 watchpoint_value_print (w->val, &stb);
112e8700
SM
10367 uiout->field_stream ("new", stb);
10368 uiout->text ("\n");
348d480f 10369 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10370 break;
10371 default:
348d480f 10372 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10373 }
10374
348d480f
PA
10375 return result;
10376}
10377
10378/* Implement the "print_mention" breakpoint_ops method for hardware
10379 watchpoints. */
10380
10381static void
10382print_mention_watchpoint (struct breakpoint *b)
10383{
3a5c3e22 10384 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10385 struct ui_out *uiout = current_uiout;
46b9c129 10386 const char *tuple_name;
348d480f
PA
10387
10388 switch (b->type)
10389 {
10390 case bp_watchpoint:
112e8700 10391 uiout->text ("Watchpoint ");
46b9c129 10392 tuple_name = "wpt";
348d480f
PA
10393 break;
10394 case bp_hardware_watchpoint:
112e8700 10395 uiout->text ("Hardware watchpoint ");
46b9c129 10396 tuple_name = "wpt";
348d480f
PA
10397 break;
10398 case bp_read_watchpoint:
112e8700 10399 uiout->text ("Hardware read watchpoint ");
46b9c129 10400 tuple_name = "hw-rwpt";
348d480f
PA
10401 break;
10402 case bp_access_watchpoint:
112e8700 10403 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10404 tuple_name = "hw-awpt";
348d480f
PA
10405 break;
10406 default:
10407 internal_error (__FILE__, __LINE__,
10408 _("Invalid hardware watchpoint type."));
10409 }
10410
46b9c129 10411 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10412 uiout->field_int ("number", b->number);
10413 uiout->text (": ");
10414 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10415}
10416
10417/* Implement the "print_recreate" breakpoint_ops method for
10418 watchpoints. */
10419
10420static void
10421print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10422{
3a5c3e22
PA
10423 struct watchpoint *w = (struct watchpoint *) b;
10424
348d480f
PA
10425 switch (b->type)
10426 {
10427 case bp_watchpoint:
10428 case bp_hardware_watchpoint:
10429 fprintf_unfiltered (fp, "watch");
10430 break;
10431 case bp_read_watchpoint:
10432 fprintf_unfiltered (fp, "rwatch");
10433 break;
10434 case bp_access_watchpoint:
10435 fprintf_unfiltered (fp, "awatch");
10436 break;
10437 default:
10438 internal_error (__FILE__, __LINE__,
10439 _("Invalid watchpoint type."));
10440 }
10441
3a5c3e22 10442 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10443 print_recreate_thread (b, fp);
348d480f
PA
10444}
10445
427cd150
TT
10446/* Implement the "explains_signal" breakpoint_ops method for
10447 watchpoints. */
10448
47591c29 10449static int
427cd150
TT
10450explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10451{
10452 /* A software watchpoint cannot cause a signal other than
10453 GDB_SIGNAL_TRAP. */
10454 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10455 return 0;
427cd150 10456
47591c29 10457 return 1;
427cd150
TT
10458}
10459
348d480f
PA
10460/* The breakpoint_ops structure to be used in hardware watchpoints. */
10461
2060206e 10462static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10463
10464/* Implement the "insert" breakpoint_ops method for
10465 masked hardware watchpoints. */
10466
10467static int
10468insert_masked_watchpoint (struct bp_location *bl)
10469{
3a5c3e22
PA
10470 struct watchpoint *w = (struct watchpoint *) bl->owner;
10471
10472 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10473 bl->watchpoint_type);
10474}
10475
10476/* Implement the "remove" breakpoint_ops method for
10477 masked hardware watchpoints. */
10478
10479static int
73971819 10480remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10481{
3a5c3e22
PA
10482 struct watchpoint *w = (struct watchpoint *) bl->owner;
10483
10484 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10485 bl->watchpoint_type);
10486}
10487
10488/* Implement the "resources_needed" breakpoint_ops method for
10489 masked hardware watchpoints. */
10490
10491static int
10492resources_needed_masked_watchpoint (const struct bp_location *bl)
10493{
3a5c3e22
PA
10494 struct watchpoint *w = (struct watchpoint *) bl->owner;
10495
10496 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10497}
10498
10499/* Implement the "works_in_software_mode" breakpoint_ops method for
10500 masked hardware watchpoints. */
10501
10502static int
10503works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10504{
10505 return 0;
10506}
10507
10508/* Implement the "print_it" breakpoint_ops method for
10509 masked hardware watchpoints. */
10510
10511static enum print_stop_action
10512print_it_masked_watchpoint (bpstat bs)
10513{
10514 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10515 struct ui_out *uiout = current_uiout;
348d480f
PA
10516
10517 /* Masked watchpoints have only one location. */
10518 gdb_assert (b->loc && b->loc->next == NULL);
10519
f303dbd6
PA
10520 annotate_watchpoint (b->number);
10521 maybe_print_thread_hit_breakpoint (uiout);
10522
348d480f
PA
10523 switch (b->type)
10524 {
10525 case bp_hardware_watchpoint:
112e8700
SM
10526 if (uiout->is_mi_like_p ())
10527 uiout->field_string
10528 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10529 break;
10530
10531 case bp_read_watchpoint:
112e8700
SM
10532 if (uiout->is_mi_like_p ())
10533 uiout->field_string
10534 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10535 break;
10536
10537 case bp_access_watchpoint:
112e8700
SM
10538 if (uiout->is_mi_like_p ())
10539 uiout->field_string
10540 ("reason",
348d480f
PA
10541 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10542 break;
10543 default:
10544 internal_error (__FILE__, __LINE__,
10545 _("Invalid hardware watchpoint type."));
10546 }
10547
10548 mention (b);
112e8700 10549 uiout->text (_("\n\
9c06b0b4
TJB
10550Check the underlying instruction at PC for the memory\n\
10551address and value which triggered this watchpoint.\n"));
112e8700 10552 uiout->text ("\n");
9c06b0b4
TJB
10553
10554 /* More than one watchpoint may have been triggered. */
10555 return PRINT_UNKNOWN;
10556}
10557
10558/* Implement the "print_one_detail" breakpoint_ops method for
10559 masked hardware watchpoints. */
10560
10561static void
10562print_one_detail_masked_watchpoint (const struct breakpoint *b,
10563 struct ui_out *uiout)
10564{
3a5c3e22
PA
10565 struct watchpoint *w = (struct watchpoint *) b;
10566
9c06b0b4
TJB
10567 /* Masked watchpoints have only one location. */
10568 gdb_assert (b->loc && b->loc->next == NULL);
10569
112e8700
SM
10570 uiout->text ("\tmask ");
10571 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10572 uiout->text ("\n");
9c06b0b4
TJB
10573}
10574
10575/* Implement the "print_mention" breakpoint_ops method for
10576 masked hardware watchpoints. */
10577
10578static void
10579print_mention_masked_watchpoint (struct breakpoint *b)
10580{
3a5c3e22 10581 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10582 struct ui_out *uiout = current_uiout;
46b9c129 10583 const char *tuple_name;
9c06b0b4
TJB
10584
10585 switch (b->type)
10586 {
10587 case bp_hardware_watchpoint:
112e8700 10588 uiout->text ("Masked hardware watchpoint ");
46b9c129 10589 tuple_name = "wpt";
9c06b0b4
TJB
10590 break;
10591 case bp_read_watchpoint:
112e8700 10592 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10593 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10594 break;
10595 case bp_access_watchpoint:
112e8700 10596 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10597 tuple_name = "hw-awpt";
9c06b0b4
TJB
10598 break;
10599 default:
10600 internal_error (__FILE__, __LINE__,
10601 _("Invalid hardware watchpoint type."));
10602 }
10603
46b9c129 10604 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10605 uiout->field_int ("number", b->number);
10606 uiout->text (": ");
10607 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10608}
10609
10610/* Implement the "print_recreate" breakpoint_ops method for
10611 masked hardware watchpoints. */
10612
10613static void
10614print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10615{
3a5c3e22 10616 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10617 char tmp[40];
10618
10619 switch (b->type)
10620 {
10621 case bp_hardware_watchpoint:
10622 fprintf_unfiltered (fp, "watch");
10623 break;
10624 case bp_read_watchpoint:
10625 fprintf_unfiltered (fp, "rwatch");
10626 break;
10627 case bp_access_watchpoint:
10628 fprintf_unfiltered (fp, "awatch");
10629 break;
10630 default:
10631 internal_error (__FILE__, __LINE__,
10632 _("Invalid hardware watchpoint type."));
10633 }
10634
3a5c3e22
PA
10635 sprintf_vma (tmp, w->hw_wp_mask);
10636 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10637 print_recreate_thread (b, fp);
9c06b0b4
TJB
10638}
10639
10640/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10641
2060206e 10642static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10643
10644/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10645
10646static int
10647is_masked_watchpoint (const struct breakpoint *b)
10648{
10649 return b->ops == &masked_watchpoint_breakpoint_ops;
10650}
10651
53a5351d
JM
10652/* accessflag: hw_write: watch write,
10653 hw_read: watch read,
10654 hw_access: watch access (read or write) */
c906108c 10655static void
bbc13ae3 10656watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10657 int just_location, int internal)
c906108c 10658{
c1fc2657 10659 struct breakpoint *scope_breakpoint = NULL;
270140bd 10660 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10661 struct value *val, *mark, *result;
bb9d5f81 10662 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10663 const char *exp_start = NULL;
10664 const char *exp_end = NULL;
10665 const char *tok, *end_tok;
9c06b0b4 10666 int toklen = -1;
bbc13ae3
KS
10667 const char *cond_start = NULL;
10668 const char *cond_end = NULL;
c906108c 10669 enum bptype bp_type;
37e4754d 10670 int thread = -1;
0cf6dd15 10671 int pc = 0;
9c06b0b4
TJB
10672 /* Flag to indicate whether we are going to use masks for
10673 the hardware watchpoint. */
10674 int use_mask = 0;
10675 CORE_ADDR mask = 0;
c906108c 10676
37e4754d
LM
10677 /* Make sure that we actually have parameters to parse. */
10678 if (arg != NULL && arg[0] != '\0')
10679 {
bbc13ae3
KS
10680 const char *value_start;
10681
10682 exp_end = arg + strlen (arg);
37e4754d 10683
9c06b0b4
TJB
10684 /* Look for "parameter value" pairs at the end
10685 of the arguments string. */
bbc13ae3 10686 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10687 {
10688 /* Skip whitespace at the end of the argument list. */
10689 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10690 tok--;
10691
10692 /* Find the beginning of the last token.
10693 This is the value of the parameter. */
10694 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10695 tok--;
10696 value_start = tok + 1;
10697
10698 /* Skip whitespace. */
10699 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10700 tok--;
10701
10702 end_tok = tok;
10703
10704 /* Find the beginning of the second to last token.
10705 This is the parameter itself. */
10706 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10707 tok--;
10708 tok++;
10709 toklen = end_tok - tok + 1;
10710
61012eef 10711 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10712 {
5d5658a1 10713 struct thread_info *thr;
9c06b0b4
TJB
10714 /* At this point we've found a "thread" token, which means
10715 the user is trying to set a watchpoint that triggers
10716 only in a specific thread. */
5d5658a1 10717 const char *endp;
37e4754d 10718
9c06b0b4
TJB
10719 if (thread != -1)
10720 error(_("You can specify only one thread."));
37e4754d 10721
9c06b0b4 10722 /* Extract the thread ID from the next token. */
5d5658a1 10723 thr = parse_thread_id (value_start, &endp);
37e4754d 10724
5d5658a1 10725 /* Check if the user provided a valid thread ID. */
9c06b0b4 10726 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10727 invalid_thread_id_error (value_start);
9c06b0b4 10728
5d5658a1 10729 thread = thr->global_num;
9c06b0b4 10730 }
61012eef 10731 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10732 {
10733 /* We've found a "mask" token, which means the user wants to
10734 create a hardware watchpoint that is going to have the mask
10735 facility. */
10736 struct value *mask_value, *mark;
37e4754d 10737
9c06b0b4
TJB
10738 if (use_mask)
10739 error(_("You can specify only one mask."));
37e4754d 10740
9c06b0b4 10741 use_mask = just_location = 1;
37e4754d 10742
9c06b0b4
TJB
10743 mark = value_mark ();
10744 mask_value = parse_to_comma_and_eval (&value_start);
10745 mask = value_as_address (mask_value);
10746 value_free_to_mark (mark);
10747 }
10748 else
10749 /* We didn't recognize what we found. We should stop here. */
10750 break;
37e4754d 10751
9c06b0b4
TJB
10752 /* Truncate the string and get rid of the "parameter value" pair before
10753 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10754 exp_end = tok;
9c06b0b4 10755 }
37e4754d 10756 }
bbc13ae3
KS
10757 else
10758 exp_end = arg;
37e4754d 10759
bbc13ae3
KS
10760 /* Parse the rest of the arguments. From here on out, everything
10761 is in terms of a newly allocated string instead of the original
10762 ARG. */
c906108c 10763 innermost_block = NULL;
81b1e71c
TT
10764 std::string expression (arg, exp_end - arg);
10765 exp_start = arg = expression.c_str ();
4d01a485 10766 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10767 exp_end = arg;
fa8a61dc
TT
10768 /* Remove trailing whitespace from the expression before saving it.
10769 This makes the eventual display of the expression string a bit
10770 prettier. */
10771 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10772 --exp_end;
10773
65d79d4b 10774 /* Checking if the expression is not constant. */
4d01a485 10775 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10776 {
10777 int len;
10778
10779 len = exp_end - exp_start;
10780 while (len > 0 && isspace (exp_start[len - 1]))
10781 len--;
10782 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10783 }
10784
c906108c
SS
10785 exp_valid_block = innermost_block;
10786 mark = value_mark ();
4d01a485 10787 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10788
bb9d5f81
PP
10789 if (val != NULL && just_location)
10790 {
10791 saved_bitpos = value_bitpos (val);
10792 saved_bitsize = value_bitsize (val);
10793 }
10794
06a64a0b
TT
10795 if (just_location)
10796 {
9c06b0b4
TJB
10797 int ret;
10798
06a64a0b 10799 exp_valid_block = NULL;
a1442452 10800 val = value_addr (result);
06a64a0b
TT
10801 release_value (val);
10802 value_free_to_mark (mark);
9c06b0b4
TJB
10803
10804 if (use_mask)
10805 {
10806 ret = target_masked_watch_num_registers (value_as_address (val),
10807 mask);
10808 if (ret == -1)
10809 error (_("This target does not support masked watchpoints."));
10810 else if (ret == -2)
10811 error (_("Invalid mask or memory region."));
10812 }
06a64a0b
TT
10813 }
10814 else if (val != NULL)
fa4727a6 10815 release_value (val);
c906108c 10816
f1735a53
TT
10817 tok = skip_spaces (arg);
10818 end_tok = skip_to_space (tok);
c906108c
SS
10819
10820 toklen = end_tok - tok;
10821 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10822 {
60e1c644 10823 innermost_block = NULL;
c906108c 10824 tok = cond_start = end_tok + 1;
4d01a485 10825 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10826
10827 /* The watchpoint expression may not be local, but the condition
10828 may still be. E.g.: `watch global if local > 0'. */
10829 cond_exp_valid_block = innermost_block;
10830
c906108c
SS
10831 cond_end = tok;
10832 }
10833 if (*tok)
8a3fe4f8 10834 error (_("Junk at end of command."));
c906108c 10835
441d7c93
PA
10836 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10837
10838 /* Save this because create_internal_breakpoint below invalidates
10839 'wp_frame'. */
10840 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10841
10842 /* If the expression is "local", then set up a "watchpoint scope"
10843 breakpoint at the point where we've left the scope of the watchpoint
10844 expression. Create the scope breakpoint before the watchpoint, so
10845 that we will encounter it first in bpstat_stop_status. */
441d7c93 10846 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10847 {
441d7c93
PA
10848 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10849
10850 if (frame_id_p (caller_frame_id))
edb3359d 10851 {
441d7c93
PA
10852 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10853 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10854
edb3359d 10855 scope_breakpoint
441d7c93 10856 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10857 bp_watchpoint_scope,
10858 &momentary_breakpoint_ops);
d983da9c 10859
441d7c93
PA
10860 /* create_internal_breakpoint could invalidate WP_FRAME. */
10861 wp_frame = NULL;
10862
edb3359d 10863 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10864
edb3359d
DJ
10865 /* Automatically delete the breakpoint when it hits. */
10866 scope_breakpoint->disposition = disp_del;
d983da9c 10867
edb3359d 10868 /* Only break in the proper frame (help with recursion). */
441d7c93 10869 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10870
edb3359d 10871 /* Set the address at which we will stop. */
441d7c93
PA
10872 scope_breakpoint->loc->gdbarch = caller_arch;
10873 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10874 scope_breakpoint->loc->address
a6d9a66e
UW
10875 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10876 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10877 scope_breakpoint->type);
10878 }
d983da9c
DJ
10879 }
10880
e8369a73
AB
10881 /* Now set up the breakpoint. We create all watchpoints as hardware
10882 watchpoints here even if hardware watchpoints are turned off, a call
10883 to update_watchpoint later in this function will cause the type to
10884 drop back to bp_watchpoint (software watchpoint) if required. */
10885
10886 if (accessflag == hw_read)
10887 bp_type = bp_read_watchpoint;
10888 else if (accessflag == hw_access)
10889 bp_type = bp_access_watchpoint;
10890 else
10891 bp_type = bp_hardware_watchpoint;
3a5c3e22 10892
b270e6f9 10893 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10894
348d480f 10895 if (use_mask)
b270e6f9 10896 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10897 &masked_watchpoint_breakpoint_ops);
348d480f 10898 else
b270e6f9 10899 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10900 &watchpoint_breakpoint_ops);
c1fc2657
SM
10901 w->thread = thread;
10902 w->disposition = disp_donttouch;
10903 w->pspace = current_program_space;
b22e99fd 10904 w->exp = std::move (exp);
3a5c3e22
PA
10905 w->exp_valid_block = exp_valid_block;
10906 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10907 if (just_location)
10908 {
10909 struct type *t = value_type (val);
10910 CORE_ADDR addr = value_as_address (val);
06a64a0b 10911
43cc5389
TT
10912 w->exp_string_reparse
10913 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10914
3a5c3e22 10915 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10916 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10917 }
10918 else
3a5c3e22 10919 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10920
10921 if (use_mask)
10922 {
3a5c3e22 10923 w->hw_wp_mask = mask;
9c06b0b4
TJB
10924 }
10925 else
10926 {
3a5c3e22 10927 w->val = val;
bb9d5f81
PP
10928 w->val_bitpos = saved_bitpos;
10929 w->val_bitsize = saved_bitsize;
3a5c3e22 10930 w->val_valid = 1;
9c06b0b4 10931 }
77b06cd7 10932
c906108c 10933 if (cond_start)
c1fc2657 10934 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10935 else
c1fc2657 10936 w->cond_string = 0;
c5aa993b 10937
441d7c93 10938 if (frame_id_p (watchpoint_frame))
f6bc2008 10939 {
441d7c93 10940 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10941 w->watchpoint_thread = inferior_ptid;
f6bc2008 10942 }
c906108c 10943 else
f6bc2008 10944 {
3a5c3e22
PA
10945 w->watchpoint_frame = null_frame_id;
10946 w->watchpoint_thread = null_ptid;
f6bc2008 10947 }
c906108c 10948
d983da9c 10949 if (scope_breakpoint != NULL)
c906108c 10950 {
d983da9c
DJ
10951 /* The scope breakpoint is related to the watchpoint. We will
10952 need to act on them together. */
c1fc2657 10953 w->related_breakpoint = scope_breakpoint;
b270e6f9 10954 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10955 }
d983da9c 10956
06a64a0b
TT
10957 if (!just_location)
10958 value_free_to_mark (mark);
2d134ed3 10959
b270e6f9
TT
10960 /* Finally update the new watchpoint. This creates the locations
10961 that should be inserted. */
10962 update_watchpoint (w.get (), 1);
a9634178 10963
b270e6f9 10964 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10965}
10966
e09342b5 10967/* Return count of debug registers needed to watch the given expression.
e09342b5 10968 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10969
c906108c 10970static int
a9634178 10971can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10972{
10973 int found_memory_cnt = 0;
2e70b7b9 10974 struct value *head = v;
c906108c
SS
10975
10976 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10977 if (!can_use_hw_watchpoints)
c906108c 10978 return 0;
c5aa993b 10979
5c44784c
JM
10980 /* Make sure that the value of the expression depends only upon
10981 memory contents, and values computed from them within GDB. If we
10982 find any register references or function calls, we can't use a
10983 hardware watchpoint.
10984
10985 The idea here is that evaluating an expression generates a series
10986 of values, one holding the value of every subexpression. (The
10987 expression a*b+c has five subexpressions: a, b, a*b, c, and
10988 a*b+c.) GDB's values hold almost enough information to establish
10989 the criteria given above --- they identify memory lvalues,
10990 register lvalues, computed values, etcetera. So we can evaluate
10991 the expression, and then scan the chain of values that leaves
10992 behind to decide whether we can detect any possible change to the
10993 expression's final value using only hardware watchpoints.
10994
10995 However, I don't think that the values returned by inferior
10996 function calls are special in any way. So this function may not
10997 notice that an expression involving an inferior function call
10998 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10999 for (; v; v = value_next (v))
c906108c 11000 {
5c44784c 11001 if (VALUE_LVAL (v) == lval_memory)
c906108c 11002 {
8464be76
DJ
11003 if (v != head && value_lazy (v))
11004 /* A lazy memory lvalue in the chain is one that GDB never
11005 needed to fetch; we either just used its address (e.g.,
11006 `a' in `a.b') or we never needed it at all (e.g., `a'
11007 in `a,b'). This doesn't apply to HEAD; if that is
11008 lazy then it was not readable, but watch it anyway. */
5c44784c 11009 ;
53a5351d 11010 else
5c44784c
JM
11011 {
11012 /* Ahh, memory we actually used! Check if we can cover
11013 it with hardware watchpoints. */
df407dfe 11014 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11015
11016 /* We only watch structs and arrays if user asked for it
11017 explicitly, never if they just happen to appear in a
11018 middle of some value chain. */
11019 if (v == head
11020 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11021 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11022 {
42ae5230 11023 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11024 int len;
11025 int num_regs;
11026
a9634178 11027 len = (target_exact_watchpoints
e09342b5
TJB
11028 && is_scalar_type_recursive (vtype))?
11029 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11030
e09342b5
TJB
11031 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11032 if (!num_regs)
2e70b7b9
MS
11033 return 0;
11034 else
e09342b5 11035 found_memory_cnt += num_regs;
2e70b7b9 11036 }
5c44784c 11037 }
c5aa993b 11038 }
5086187c
AC
11039 else if (VALUE_LVAL (v) != not_lval
11040 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11041 return 0; /* These are values from the history (e.g., $1). */
5086187c 11042 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11043 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11044 }
11045
11046 /* The expression itself looks suitable for using a hardware
11047 watchpoint, but give the target machine a chance to reject it. */
11048 return found_memory_cnt;
11049}
11050
8b93c638 11051void
f2fc3015 11052watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11053{
84f4c1fe 11054 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11055}
11056
06a64a0b
TT
11057/* A helper function that looks for the "-location" argument and then
11058 calls watch_command_1. */
11059
11060static void
11061watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11062{
11063 int just_location = 0;
11064
11065 if (arg
11066 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11067 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11068 {
e9cafbcc 11069 arg = skip_spaces (arg);
06a64a0b
TT
11070 just_location = 1;
11071 }
11072
84f4c1fe 11073 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11074}
8926118c 11075
c5aa993b 11076static void
fba45db2 11077watch_command (char *arg, int from_tty)
c906108c 11078{
06a64a0b 11079 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11080}
11081
8b93c638 11082void
f2fc3015 11083rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11084{
84f4c1fe 11085 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11086}
8926118c 11087
c5aa993b 11088static void
fba45db2 11089rwatch_command (char *arg, int from_tty)
c906108c 11090{
06a64a0b 11091 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11092}
11093
8b93c638 11094void
f2fc3015 11095awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11096{
84f4c1fe 11097 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11098}
8926118c 11099
c5aa993b 11100static void
fba45db2 11101awatch_command (char *arg, int from_tty)
c906108c 11102{
06a64a0b 11103 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11104}
c906108c 11105\f
c5aa993b 11106
cfc31633
PA
11107/* Data for the FSM that manages the until(location)/advance commands
11108 in infcmd.c. Here because it uses the mechanisms of
11109 breakpoints. */
c906108c 11110
cfc31633 11111struct until_break_fsm
bfec99b2 11112{
cfc31633
PA
11113 /* The base class. */
11114 struct thread_fsm thread_fsm;
11115
11116 /* The thread that as current when the command was executed. */
11117 int thread;
11118
11119 /* The breakpoint set at the destination location. */
11120 struct breakpoint *location_breakpoint;
11121
11122 /* Breakpoint set at the return address in the caller frame. May be
11123 NULL. */
11124 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11125};
11126
8980e177
PA
11127static void until_break_fsm_clean_up (struct thread_fsm *self,
11128 struct thread_info *thread);
11129static int until_break_fsm_should_stop (struct thread_fsm *self,
11130 struct thread_info *thread);
cfc31633
PA
11131static enum async_reply_reason
11132 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11133
11134/* until_break_fsm's vtable. */
11135
11136static struct thread_fsm_ops until_break_fsm_ops =
11137{
11138 NULL, /* dtor */
11139 until_break_fsm_clean_up,
11140 until_break_fsm_should_stop,
11141 NULL, /* return_value */
11142 until_break_fsm_async_reply_reason,
11143};
11144
11145/* Allocate a new until_break_command_fsm. */
11146
11147static struct until_break_fsm *
8980e177 11148new_until_break_fsm (struct interp *cmd_interp, int thread,
454dafbd
TT
11149 breakpoint_up &&location_breakpoint,
11150 breakpoint_up &&caller_breakpoint)
cfc31633
PA
11151{
11152 struct until_break_fsm *sm;
11153
11154 sm = XCNEW (struct until_break_fsm);
8980e177 11155 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11156
11157 sm->thread = thread;
454dafbd
TT
11158 sm->location_breakpoint = location_breakpoint.release ();
11159 sm->caller_breakpoint = caller_breakpoint.release ();
cfc31633
PA
11160
11161 return sm;
11162}
11163
11164/* Implementation of the 'should_stop' FSM method for the
11165 until(location)/advance commands. */
11166
11167static int
8980e177
PA
11168until_break_fsm_should_stop (struct thread_fsm *self,
11169 struct thread_info *tp)
cfc31633
PA
11170{
11171 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11172
11173 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11174 sm->location_breakpoint) != NULL
11175 || (sm->caller_breakpoint != NULL
11176 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11177 sm->caller_breakpoint) != NULL))
11178 thread_fsm_set_finished (self);
11179
11180 return 1;
11181}
11182
11183/* Implementation of the 'clean_up' FSM method for the
11184 until(location)/advance commands. */
11185
c2c6d25f 11186static void
8980e177
PA
11187until_break_fsm_clean_up (struct thread_fsm *self,
11188 struct thread_info *thread)
43ff13b4 11189{
cfc31633 11190 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11191
cfc31633
PA
11192 /* Clean up our temporary breakpoints. */
11193 if (sm->location_breakpoint != NULL)
11194 {
11195 delete_breakpoint (sm->location_breakpoint);
11196 sm->location_breakpoint = NULL;
11197 }
11198 if (sm->caller_breakpoint != NULL)
11199 {
11200 delete_breakpoint (sm->caller_breakpoint);
11201 sm->caller_breakpoint = NULL;
11202 }
11203 delete_longjmp_breakpoint (sm->thread);
11204}
11205
11206/* Implementation of the 'async_reply_reason' FSM method for the
11207 until(location)/advance commands. */
11208
11209static enum async_reply_reason
11210until_break_fsm_async_reply_reason (struct thread_fsm *self)
11211{
11212 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11213}
11214
c906108c 11215void
f2fc3015 11216until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11217{
8556afb4
PA
11218 struct frame_info *frame;
11219 struct gdbarch *frame_gdbarch;
11220 struct frame_id stack_frame_id;
11221 struct frame_id caller_frame_id;
ffc2605c 11222 struct cleanup *old_chain;
186c406b
TT
11223 int thread;
11224 struct thread_info *tp;
cfc31633 11225 struct until_break_fsm *sm;
c906108c 11226
70509625 11227 clear_proceed_status (0);
c906108c
SS
11228
11229 /* Set a breakpoint where the user wants it and at return from
4a64f543 11230 this function. */
c5aa993b 11231
ffc2605c 11232 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11233
6c5b2ebe
PA
11234 std::vector<symtab_and_line> sals
11235 = (last_displayed_sal_is_valid ()
11236 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11237 get_last_displayed_symtab (),
11238 get_last_displayed_line ())
11239 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11240 NULL, (struct symtab *) NULL, 0));
c5aa993b 11241
6c5b2ebe 11242 if (sals.size () != 1)
8a3fe4f8 11243 error (_("Couldn't get information on specified line."));
c5aa993b 11244
6c5b2ebe 11245 symtab_and_line &sal = sals[0];
c5aa993b 11246
c906108c 11247 if (*arg)
8a3fe4f8 11248 error (_("Junk at end of arguments."));
c5aa993b 11249
c906108c 11250 resolve_sal_pc (&sal);
c5aa993b 11251
186c406b 11252 tp = inferior_thread ();
5d5658a1 11253 thread = tp->global_num;
186c406b 11254
883bc8d1
PA
11255 old_chain = make_cleanup (null_cleanup, NULL);
11256
8556afb4
PA
11257 /* Note linespec handling above invalidates the frame chain.
11258 Installing a breakpoint also invalidates the frame chain (as it
11259 may need to switch threads), so do any frame handling before
11260 that. */
11261
11262 frame = get_selected_frame (NULL);
11263 frame_gdbarch = get_frame_arch (frame);
11264 stack_frame_id = get_stack_frame_id (frame);
11265 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11266
ae66c1fc
EZ
11267 /* Keep within the current frame, or in frames called by the current
11268 one. */
edb3359d 11269
454dafbd 11270 breakpoint_up caller_breakpoint;
883bc8d1 11271 if (frame_id_p (caller_frame_id))
c906108c 11272 {
883bc8d1 11273 struct symtab_and_line sal2;
cfc31633 11274 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11275
11276 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11277 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11278 caller_gdbarch = frame_unwind_caller_arch (frame);
11279 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11280 sal2,
11281 caller_frame_id,
11282 bp_until);
186c406b 11283
883bc8d1 11284 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11285 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11286 }
c5aa993b 11287
c70a6932
JK
11288 /* set_momentary_breakpoint could invalidate FRAME. */
11289 frame = NULL;
11290
454dafbd 11291 breakpoint_up location_breakpoint;
883bc8d1
PA
11292 if (anywhere)
11293 /* If the user told us to continue until a specified location,
11294 we don't specify a frame at which we need to stop. */
cfc31633
PA
11295 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11296 null_frame_id, bp_until);
883bc8d1
PA
11297 else
11298 /* Otherwise, specify the selected frame, because we want to stop
11299 only at the very same frame. */
cfc31633
PA
11300 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11301 stack_frame_id, bp_until);
883bc8d1 11302
8980e177 11303 sm = new_until_break_fsm (command_interp (), tp->global_num,
454dafbd
TT
11304 std::move (location_breakpoint),
11305 std::move (caller_breakpoint));
cfc31633 11306 tp->thread_fsm = &sm->thread_fsm;
f107f563 11307
cfc31633 11308 discard_cleanups (old_chain);
f107f563 11309
cfc31633 11310 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11311}
ae66c1fc 11312
c906108c
SS
11313/* This function attempts to parse an optional "if <cond>" clause
11314 from the arg string. If one is not found, it returns NULL.
c5aa993b 11315
c906108c
SS
11316 Else, it returns a pointer to the condition string. (It does not
11317 attempt to evaluate the string against a particular block.) And,
11318 it updates arg to point to the first character following the parsed
4a64f543 11319 if clause in the arg string. */
53a5351d 11320
63160a43
PA
11321const char *
11322ep_parse_optional_if_clause (const char **arg)
c906108c 11323{
63160a43 11324 const char *cond_string;
c5aa993b
JM
11325
11326 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11327 return NULL;
c5aa993b 11328
4a64f543 11329 /* Skip the "if" keyword. */
c906108c 11330 (*arg) += 2;
c5aa993b 11331
c906108c 11332 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11333 condition string. */
f1735a53 11334 *arg = skip_spaces (*arg);
c906108c 11335 cond_string = *arg;
c5aa993b 11336
4a64f543
MS
11337 /* Assume that the condition occupies the remainder of the arg
11338 string. */
c906108c 11339 (*arg) += strlen (cond_string);
c5aa993b 11340
c906108c
SS
11341 return cond_string;
11342}
c5aa993b 11343
c906108c
SS
11344/* Commands to deal with catching events, such as signals, exceptions,
11345 process start/exit, etc. */
c5aa993b
JM
11346
11347typedef enum
11348{
44feb3ce
TT
11349 catch_fork_temporary, catch_vfork_temporary,
11350 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11351}
11352catch_fork_kind;
11353
c906108c 11354static void
63160a43 11355catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11356 struct cmd_list_element *command)
c906108c 11357{
63160a43 11358 const char *arg = arg_entry;
a6d9a66e 11359 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11360 const char *cond_string = NULL;
44feb3ce
TT
11361 catch_fork_kind fork_kind;
11362 int tempflag;
11363
11364 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11365 tempflag = (fork_kind == catch_fork_temporary
11366 || fork_kind == catch_vfork_temporary);
c5aa993b 11367
44feb3ce
TT
11368 if (!arg)
11369 arg = "";
f1735a53 11370 arg = skip_spaces (arg);
c5aa993b 11371
c906108c 11372 /* The allowed syntax is:
c5aa993b
JM
11373 catch [v]fork
11374 catch [v]fork if <cond>
11375
4a64f543 11376 First, check if there's an if clause. */
c906108c 11377 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11378
c906108c 11379 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11380 error (_("Junk at end of arguments."));
c5aa993b 11381
c906108c 11382 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11383 and enable reporting of such events. */
c5aa993b
JM
11384 switch (fork_kind)
11385 {
44feb3ce
TT
11386 case catch_fork_temporary:
11387 case catch_fork_permanent:
a6d9a66e 11388 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11389 &catch_fork_breakpoint_ops);
c906108c 11390 break;
44feb3ce
TT
11391 case catch_vfork_temporary:
11392 case catch_vfork_permanent:
a6d9a66e 11393 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11394 &catch_vfork_breakpoint_ops);
c906108c 11395 break;
c5aa993b 11396 default:
8a3fe4f8 11397 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11398 break;
c5aa993b 11399 }
c906108c
SS
11400}
11401
11402static void
63160a43 11403catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11404 struct cmd_list_element *command)
c906108c 11405{
63160a43 11406 const char *arg = arg_entry;
a6d9a66e 11407 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11408 int tempflag;
63160a43 11409 const char *cond_string = NULL;
c906108c 11410
44feb3ce
TT
11411 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11412
11413 if (!arg)
11414 arg = "";
f1735a53 11415 arg = skip_spaces (arg);
c906108c
SS
11416
11417 /* The allowed syntax is:
c5aa993b
JM
11418 catch exec
11419 catch exec if <cond>
c906108c 11420
4a64f543 11421 First, check if there's an if clause. */
c906108c
SS
11422 cond_string = ep_parse_optional_if_clause (&arg);
11423
11424 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11425 error (_("Junk at end of arguments."));
c906108c 11426
b270e6f9
TT
11427 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11428 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11429 &catch_exec_breakpoint_ops);
11430 c->exec_pathname = NULL;
11431
b270e6f9 11432 install_breakpoint (0, std::move (c), 1);
c906108c 11433}
c5aa993b 11434
9ac4176b 11435void
28010a5d
PA
11436init_ada_exception_breakpoint (struct breakpoint *b,
11437 struct gdbarch *gdbarch,
11438 struct symtab_and_line sal,
f2fc3015 11439 const char *addr_string,
c0a91b2b 11440 const struct breakpoint_ops *ops,
28010a5d 11441 int tempflag,
349774ef 11442 int enabled,
28010a5d 11443 int from_tty)
f7f9143b 11444{
f7f9143b
JB
11445 if (from_tty)
11446 {
5af949e3
UW
11447 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11448 if (!loc_gdbarch)
11449 loc_gdbarch = gdbarch;
11450
6c95b8df
PA
11451 describe_other_breakpoints (loc_gdbarch,
11452 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11453 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11454 version for exception catchpoints, because two catchpoints
11455 used for different exception names will use the same address.
11456 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11457 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11458 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11459 the user what type of catchpoint it is. The above is good
11460 enough for now, though. */
11461 }
11462
28010a5d 11463 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11464
349774ef 11465 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11466 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11467 b->location = string_to_event_location (&addr_string,
11468 language_def (language_ada));
f7f9143b 11469 b->language = language_ada;
f7f9143b
JB
11470}
11471
c906108c 11472static void
981a3fb3 11473catch_command (const char *arg, int from_tty)
c906108c 11474{
44feb3ce 11475 error (_("Catch requires an event name."));
c906108c
SS
11476}
11477\f
11478
11479static void
981a3fb3 11480tcatch_command (const char *arg, int from_tty)
c906108c 11481{
44feb3ce 11482 error (_("Catch requires an event name."));
c906108c
SS
11483}
11484
81b1e71c 11485/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11486
11487static int
81b1e71c 11488compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11489{
81b1e71c
TT
11490 uintptr_t ua = (uintptr_t) a;
11491 uintptr_t ub = (uintptr_t) b;
8a2c437b 11492
81b1e71c 11493 if (a->number < b->number)
8a2c437b 11494 return -1;
81b1e71c 11495 else if (a->number > b->number)
8a2c437b
TT
11496 return 1;
11497
11498 /* Now sort by address, in case we see, e..g, two breakpoints with
11499 the number 0. */
11500 if (ua < ub)
11501 return -1;
94b0e70d 11502 return ua > ub ? 1 : 0;
8a2c437b
TT
11503}
11504
80f8a6eb 11505/* Delete breakpoints by address or line. */
c906108c
SS
11506
11507static void
fba45db2 11508clear_command (char *arg, int from_tty)
c906108c 11509{
81b1e71c 11510 struct breakpoint *b;
c906108c 11511 int default_match;
c906108c
SS
11512 int i;
11513
6c5b2ebe
PA
11514 std::vector<symtab_and_line> decoded_sals;
11515 symtab_and_line last_sal;
11516 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11517 if (arg)
11518 {
6c5b2ebe
PA
11519 decoded_sals
11520 = decode_line_with_current_source (arg,
11521 (DECODE_LINE_FUNFIRSTLINE
11522 | DECODE_LINE_LIST_MODE));
c906108c 11523 default_match = 0;
6c5b2ebe 11524 sals = decoded_sals;
c906108c
SS
11525 }
11526 else
11527 {
1bfeeb0f
JL
11528 /* Set sal's line, symtab, pc, and pspace to the values
11529 corresponding to the last call to print_frame_info. If the
11530 codepoint is not valid, this will set all the fields to 0. */
51abb421 11531 last_sal = get_last_displayed_sal ();
6c5b2ebe 11532 if (last_sal.symtab == 0)
8a3fe4f8 11533 error (_("No source file specified."));
c906108c 11534
c906108c 11535 default_match = 1;
6c5b2ebe 11536 sals = last_sal;
c906108c
SS
11537 }
11538
4a64f543
MS
11539 /* We don't call resolve_sal_pc here. That's not as bad as it
11540 seems, because all existing breakpoints typically have both
11541 file/line and pc set. So, if clear is given file/line, we can
11542 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11543
11544 We only support clearing given the address explicitly
11545 present in breakpoint table. Say, we've set breakpoint
4a64f543 11546 at file:line. There were several PC values for that file:line,
ed0616c6 11547 due to optimization, all in one block.
4a64f543
MS
11548
11549 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11550 PC corresponding to the same file:line, the breakpoint won't
11551 be cleared. We probably can still clear the breakpoint, but
11552 since the other PC value is never presented to user, user
11553 can only find it by guessing, and it does not seem important
11554 to support that. */
11555
4a64f543
MS
11556 /* For each line spec given, delete bps which correspond to it. Do
11557 it in two passes, solely to preserve the current behavior that
11558 from_tty is forced true if we delete more than one
11559 breakpoint. */
c906108c 11560
81b1e71c 11561 std::vector<struct breakpoint *> found;
6c5b2ebe 11562 for (const auto &sal : sals)
c906108c 11563 {
05cba821
JK
11564 const char *sal_fullname;
11565
c906108c 11566 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11567 If line given (pc == 0), clear all bpts on specified line.
11568 If defaulting, clear all bpts on default line
c906108c 11569 or at default pc.
c5aa993b
JM
11570
11571 defaulting sal.pc != 0 tests to do
11572
11573 0 1 pc
11574 1 1 pc _and_ line
11575 0 0 line
11576 1 0 <can't happen> */
c906108c 11577
05cba821
JK
11578 sal_fullname = (sal.symtab == NULL
11579 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11580
4a64f543 11581 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11582 ALL_BREAKPOINTS (b)
c5aa993b 11583 {
0d381245 11584 int match = 0;
4a64f543 11585 /* Are we going to delete b? */
cc60f2e3 11586 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11587 {
11588 struct bp_location *loc = b->loc;
11589 for (; loc; loc = loc->next)
11590 {
f8eba3c6
TT
11591 /* If the user specified file:line, don't allow a PC
11592 match. This matches historical gdb behavior. */
11593 int pc_match = (!sal.explicit_line
11594 && sal.pc
11595 && (loc->pspace == sal.pspace)
11596 && (loc->address == sal.pc)
11597 && (!section_is_overlay (loc->section)
11598 || loc->section == sal.section));
4aac40c8
TT
11599 int line_match = 0;
11600
11601 if ((default_match || sal.explicit_line)
2f202fde 11602 && loc->symtab != NULL
05cba821 11603 && sal_fullname != NULL
4aac40c8 11604 && sal.pspace == loc->pspace
05cba821
JK
11605 && loc->line_number == sal.line
11606 && filename_cmp (symtab_to_fullname (loc->symtab),
11607 sal_fullname) == 0)
11608 line_match = 1;
4aac40c8 11609
0d381245
VP
11610 if (pc_match || line_match)
11611 {
11612 match = 1;
11613 break;
11614 }
11615 }
11616 }
11617
11618 if (match)
81b1e71c 11619 found.push_back (b);
c906108c 11620 }
80f8a6eb 11621 }
8a2c437b 11622
80f8a6eb 11623 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11624 if (found.empty ())
80f8a6eb
MS
11625 {
11626 if (arg)
8a3fe4f8 11627 error (_("No breakpoint at %s."), arg);
80f8a6eb 11628 else
8a3fe4f8 11629 error (_("No breakpoint at this line."));
80f8a6eb 11630 }
c906108c 11631
8a2c437b 11632 /* Remove duplicates from the vec. */
81b1e71c
TT
11633 std::sort (found.begin (), found.end (),
11634 [] (const breakpoint *a, const breakpoint *b)
11635 {
11636 return compare_breakpoints (a, b) < 0;
11637 });
11638 found.erase (std::unique (found.begin (), found.end (),
11639 [] (const breakpoint *a, const breakpoint *b)
11640 {
11641 return compare_breakpoints (a, b) == 0;
11642 }),
11643 found.end ());
8a2c437b 11644
81b1e71c 11645 if (found.size () > 1)
4a64f543 11646 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11647 if (from_tty)
a3f17187 11648 {
81b1e71c 11649 if (found.size () == 1)
a3f17187
AC
11650 printf_unfiltered (_("Deleted breakpoint "));
11651 else
11652 printf_unfiltered (_("Deleted breakpoints "));
11653 }
d6e956e5 11654
81b1e71c 11655 for (breakpoint *iter : found)
80f8a6eb 11656 {
c5aa993b 11657 if (from_tty)
81b1e71c
TT
11658 printf_unfiltered ("%d ", iter->number);
11659 delete_breakpoint (iter);
c906108c 11660 }
80f8a6eb
MS
11661 if (from_tty)
11662 putchar_unfiltered ('\n');
c906108c
SS
11663}
11664\f
11665/* Delete breakpoint in BS if they are `delete' breakpoints and
11666 all breakpoints that are marked for deletion, whether hit or not.
11667 This is called after any breakpoint is hit, or after errors. */
11668
11669void
fba45db2 11670breakpoint_auto_delete (bpstat bs)
c906108c 11671{
35df4500 11672 struct breakpoint *b, *b_tmp;
c906108c
SS
11673
11674 for (; bs; bs = bs->next)
f431efe5
PA
11675 if (bs->breakpoint_at
11676 && bs->breakpoint_at->disposition == disp_del
c906108c 11677 && bs->stop)
f431efe5 11678 delete_breakpoint (bs->breakpoint_at);
c906108c 11679
35df4500 11680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11681 {
b5de0fa7 11682 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11683 delete_breakpoint (b);
11684 }
c906108c
SS
11685}
11686
4a64f543
MS
11687/* A comparison function for bp_location AP and BP being interfaced to
11688 qsort. Sort elements primarily by their ADDRESS (no matter what
11689 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11690 secondarily by ordering first permanent elements and
4a64f543 11691 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11692 qsort being an unstable algorithm. */
876fa593
JK
11693
11694static int
f5336ca5 11695bp_locations_compare (const void *ap, const void *bp)
876fa593 11696{
9a3c8263
SM
11697 const struct bp_location *a = *(const struct bp_location **) ap;
11698 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11699
11700 if (a->address != b->address)
11701 return (a->address > b->address) - (a->address < b->address);
11702
dea2aa5f
LM
11703 /* Sort locations at the same address by their pspace number, keeping
11704 locations of the same inferior (in a multi-inferior environment)
11705 grouped. */
11706
11707 if (a->pspace->num != b->pspace->num)
11708 return ((a->pspace->num > b->pspace->num)
11709 - (a->pspace->num < b->pspace->num));
11710
876fa593 11711 /* Sort permanent breakpoints first. */
1a853c52
PA
11712 if (a->permanent != b->permanent)
11713 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11714
c56a97f9
JK
11715 /* Make the internal GDB representation stable across GDB runs
11716 where A and B memory inside GDB can differ. Breakpoint locations of
11717 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11718
11719 if (a->owner->number != b->owner->number)
c56a97f9
JK
11720 return ((a->owner->number > b->owner->number)
11721 - (a->owner->number < b->owner->number));
876fa593
JK
11722
11723 return (a > b) - (a < b);
11724}
11725
f5336ca5
PA
11726/* Set bp_locations_placed_address_before_address_max and
11727 bp_locations_shadow_len_after_address_max according to the current
11728 content of the bp_locations array. */
f7545552
TT
11729
11730static void
f5336ca5 11731bp_locations_target_extensions_update (void)
f7545552 11732{
876fa593
JK
11733 struct bp_location *bl, **blp_tmp;
11734
f5336ca5
PA
11735 bp_locations_placed_address_before_address_max = 0;
11736 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11737
11738 ALL_BP_LOCATIONS (bl, blp_tmp)
11739 {
11740 CORE_ADDR start, end, addr;
11741
11742 if (!bp_location_has_shadow (bl))
11743 continue;
11744
11745 start = bl->target_info.placed_address;
11746 end = start + bl->target_info.shadow_len;
11747
11748 gdb_assert (bl->address >= start);
11749 addr = bl->address - start;
f5336ca5
PA
11750 if (addr > bp_locations_placed_address_before_address_max)
11751 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11752
11753 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11754
11755 gdb_assert (bl->address < end);
11756 addr = end - bl->address;
f5336ca5
PA
11757 if (addr > bp_locations_shadow_len_after_address_max)
11758 bp_locations_shadow_len_after_address_max = addr;
876fa593 11759 }
f7545552
TT
11760}
11761
1e4d1764
YQ
11762/* Download tracepoint locations if they haven't been. */
11763
11764static void
11765download_tracepoint_locations (void)
11766{
7ed2c994 11767 struct breakpoint *b;
dd2e65cc 11768 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11769
5ed8105e 11770 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11771
7ed2c994 11772 ALL_TRACEPOINTS (b)
1e4d1764 11773 {
7ed2c994 11774 struct bp_location *bl;
1e4d1764 11775 struct tracepoint *t;
f2a8bc8a 11776 int bp_location_downloaded = 0;
1e4d1764 11777
7ed2c994 11778 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11779 ? !may_insert_fast_tracepoints
11780 : !may_insert_tracepoints))
11781 continue;
11782
dd2e65cc
YQ
11783 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11784 {
11785 if (target_can_download_tracepoint ())
11786 can_download_tracepoint = TRIBOOL_TRUE;
11787 else
11788 can_download_tracepoint = TRIBOOL_FALSE;
11789 }
11790
11791 if (can_download_tracepoint == TRIBOOL_FALSE)
11792 break;
11793
7ed2c994
YQ
11794 for (bl = b->loc; bl; bl = bl->next)
11795 {
11796 /* In tracepoint, locations are _never_ duplicated, so
11797 should_be_inserted is equivalent to
11798 unduplicated_should_be_inserted. */
11799 if (!should_be_inserted (bl) || bl->inserted)
11800 continue;
1e4d1764 11801
7ed2c994 11802 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11803
7ed2c994 11804 target_download_tracepoint (bl);
1e4d1764 11805
7ed2c994 11806 bl->inserted = 1;
f2a8bc8a 11807 bp_location_downloaded = 1;
7ed2c994
YQ
11808 }
11809 t = (struct tracepoint *) b;
11810 t->number_on_target = b->number;
f2a8bc8a
YQ
11811 if (bp_location_downloaded)
11812 observer_notify_breakpoint_modified (b);
1e4d1764 11813 }
1e4d1764
YQ
11814}
11815
934709f0
PW
11816/* Swap the insertion/duplication state between two locations. */
11817
11818static void
11819swap_insertion (struct bp_location *left, struct bp_location *right)
11820{
11821 const int left_inserted = left->inserted;
11822 const int left_duplicate = left->duplicate;
b775012e 11823 const int left_needs_update = left->needs_update;
934709f0
PW
11824 const struct bp_target_info left_target_info = left->target_info;
11825
1e4d1764
YQ
11826 /* Locations of tracepoints can never be duplicated. */
11827 if (is_tracepoint (left->owner))
11828 gdb_assert (!left->duplicate);
11829 if (is_tracepoint (right->owner))
11830 gdb_assert (!right->duplicate);
11831
934709f0
PW
11832 left->inserted = right->inserted;
11833 left->duplicate = right->duplicate;
b775012e 11834 left->needs_update = right->needs_update;
934709f0
PW
11835 left->target_info = right->target_info;
11836 right->inserted = left_inserted;
11837 right->duplicate = left_duplicate;
b775012e 11838 right->needs_update = left_needs_update;
934709f0
PW
11839 right->target_info = left_target_info;
11840}
11841
b775012e
LM
11842/* Force the re-insertion of the locations at ADDRESS. This is called
11843 once a new/deleted/modified duplicate location is found and we are evaluating
11844 conditions on the target's side. Such conditions need to be updated on
11845 the target. */
11846
11847static void
11848force_breakpoint_reinsertion (struct bp_location *bl)
11849{
11850 struct bp_location **locp = NULL, **loc2p;
11851 struct bp_location *loc;
11852 CORE_ADDR address = 0;
11853 int pspace_num;
11854
11855 address = bl->address;
11856 pspace_num = bl->pspace->num;
11857
11858 /* This is only meaningful if the target is
11859 evaluating conditions and if the user has
11860 opted for condition evaluation on the target's
11861 side. */
11862 if (gdb_evaluates_breakpoint_condition_p ()
11863 || !target_supports_evaluation_of_breakpoint_conditions ())
11864 return;
11865
11866 /* Flag all breakpoint locations with this address and
11867 the same program space as the location
11868 as "its condition has changed". We need to
11869 update the conditions on the target's side. */
11870 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11871 {
11872 loc = *loc2p;
11873
11874 if (!is_breakpoint (loc->owner)
11875 || pspace_num != loc->pspace->num)
11876 continue;
11877
11878 /* Flag the location appropriately. We use a different state to
11879 let everyone know that we already updated the set of locations
11880 with addr bl->address and program space bl->pspace. This is so
11881 we don't have to keep calling these functions just to mark locations
11882 that have already been marked. */
11883 loc->condition_changed = condition_updated;
11884
11885 /* Free the agent expression bytecode as well. We will compute
11886 it later on. */
833177a4 11887 loc->cond_bytecode.reset ();
b775012e
LM
11888 }
11889}
44702360
PA
11890/* Called whether new breakpoints are created, or existing breakpoints
11891 deleted, to update the global location list and recompute which
11892 locations are duplicate of which.
b775012e 11893
04086b45
PA
11894 The INSERT_MODE flag determines whether locations may not, may, or
11895 shall be inserted now. See 'enum ugll_insert_mode' for more
11896 info. */
b60e7edf 11897
0d381245 11898static void
44702360 11899update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11900{
74960c60 11901 struct breakpoint *b;
876fa593 11902 struct bp_location **locp, *loc;
b775012e
LM
11903 /* Last breakpoint location address that was marked for update. */
11904 CORE_ADDR last_addr = 0;
11905 /* Last breakpoint location program space that was marked for update. */
11906 int last_pspace_num = -1;
f7545552 11907
2d134ed3
PA
11908 /* Used in the duplicates detection below. When iterating over all
11909 bp_locations, points to the first bp_location of a given address.
11910 Breakpoints and watchpoints of different types are never
11911 duplicates of each other. Keep one pointer for each type of
11912 breakpoint/watchpoint, so we only need to loop over all locations
11913 once. */
11914 struct bp_location *bp_loc_first; /* breakpoint */
11915 struct bp_location *wp_loc_first; /* hardware watchpoint */
11916 struct bp_location *awp_loc_first; /* access watchpoint */
11917 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11918
f5336ca5
PA
11919 /* Saved former bp_locations array which we compare against the newly
11920 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11921 struct bp_location **old_locp;
f5336ca5 11922 unsigned old_locations_count;
81b1e71c 11923 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11924
f5336ca5
PA
11925 old_locations_count = bp_locations_count;
11926 bp_locations = NULL;
11927 bp_locations_count = 0;
0d381245 11928
74960c60 11929 ALL_BREAKPOINTS (b)
876fa593 11930 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11931 bp_locations_count++;
876fa593 11932
f5336ca5
PA
11933 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11934 locp = bp_locations;
876fa593
JK
11935 ALL_BREAKPOINTS (b)
11936 for (loc = b->loc; loc; loc = loc->next)
11937 *locp++ = loc;
f5336ca5
PA
11938 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11939 bp_locations_compare);
876fa593 11940
f5336ca5 11941 bp_locations_target_extensions_update ();
74960c60 11942
4a64f543
MS
11943 /* Identify bp_location instances that are no longer present in the
11944 new list, and therefore should be freed. Note that it's not
11945 necessary that those locations should be removed from inferior --
11946 if there's another location at the same address (previously
11947 marked as duplicate), we don't need to remove/insert the
11948 location.
876fa593 11949
4a64f543
MS
11950 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11951 and former bp_location array state respectively. */
876fa593 11952
f5336ca5 11953 locp = bp_locations;
81b1e71c
TT
11954 for (old_locp = old_locations.get ();
11955 old_locp < old_locations.get () + old_locations_count;
876fa593 11956 old_locp++)
74960c60 11957 {
876fa593 11958 struct bp_location *old_loc = *old_locp;
c7d46a38 11959 struct bp_location **loc2p;
876fa593 11960
e5dd4106 11961 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11962 not, we have to free it. */
c7d46a38 11963 int found_object = 0;
20874c92
VP
11964 /* Tells if the location should remain inserted in the target. */
11965 int keep_in_target = 0;
11966 int removed = 0;
876fa593 11967
4a64f543
MS
11968 /* Skip LOCP entries which will definitely never be needed.
11969 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11970 while (locp < bp_locations + bp_locations_count
c7d46a38 11971 && (*locp)->address < old_loc->address)
876fa593 11972 locp++;
c7d46a38
PA
11973
11974 for (loc2p = locp;
f5336ca5 11975 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11976 && (*loc2p)->address == old_loc->address);
11977 loc2p++)
11978 {
b775012e
LM
11979 /* Check if this is a new/duplicated location or a duplicated
11980 location that had its condition modified. If so, we want to send
11981 its condition to the target if evaluation of conditions is taking
11982 place there. */
11983 if ((*loc2p)->condition_changed == condition_modified
11984 && (last_addr != old_loc->address
11985 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11986 {
b775012e
LM
11987 force_breakpoint_reinsertion (*loc2p);
11988 last_pspace_num = old_loc->pspace->num;
c7d46a38 11989 }
b775012e
LM
11990
11991 if (*loc2p == old_loc)
11992 found_object = 1;
c7d46a38 11993 }
74960c60 11994
b775012e
LM
11995 /* We have already handled this address, update it so that we don't
11996 have to go through updates again. */
11997 last_addr = old_loc->address;
11998
11999 /* Target-side condition evaluation: Handle deleted locations. */
12000 if (!found_object)
12001 force_breakpoint_reinsertion (old_loc);
12002
4a64f543
MS
12003 /* If this location is no longer present, and inserted, look if
12004 there's maybe a new location at the same address. If so,
12005 mark that one inserted, and don't remove this one. This is
12006 needed so that we don't have a time window where a breakpoint
12007 at certain location is not inserted. */
74960c60 12008
876fa593 12009 if (old_loc->inserted)
0d381245 12010 {
4a64f543
MS
12011 /* If the location is inserted now, we might have to remove
12012 it. */
74960c60 12013
876fa593 12014 if (found_object && should_be_inserted (old_loc))
74960c60 12015 {
4a64f543
MS
12016 /* The location is still present in the location list,
12017 and still should be inserted. Don't do anything. */
20874c92 12018 keep_in_target = 1;
74960c60
VP
12019 }
12020 else
12021 {
b775012e
LM
12022 /* This location still exists, but it won't be kept in the
12023 target since it may have been disabled. We proceed to
12024 remove its target-side condition. */
12025
4a64f543
MS
12026 /* The location is either no longer present, or got
12027 disabled. See if there's another location at the
12028 same address, in which case we don't need to remove
12029 this one from the target. */
876fa593 12030
2bdf28a0 12031 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12032 if (breakpoint_address_is_meaningful (old_loc->owner))
12033 {
876fa593 12034 for (loc2p = locp;
f5336ca5 12035 (loc2p < bp_locations + bp_locations_count
c7d46a38 12036 && (*loc2p)->address == old_loc->address);
876fa593
JK
12037 loc2p++)
12038 {
12039 struct bp_location *loc2 = *loc2p;
12040
2d134ed3 12041 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12042 {
85d721b8
PA
12043 /* Read watchpoint locations are switched to
12044 access watchpoints, if the former are not
12045 supported, but the latter are. */
12046 if (is_hardware_watchpoint (old_loc->owner))
12047 {
12048 gdb_assert (is_hardware_watchpoint (loc2->owner));
12049 loc2->watchpoint_type = old_loc->watchpoint_type;
12050 }
12051
934709f0
PW
12052 /* loc2 is a duplicated location. We need to check
12053 if it should be inserted in case it will be
12054 unduplicated. */
12055 if (loc2 != old_loc
12056 && unduplicated_should_be_inserted (loc2))
c7d46a38 12057 {
934709f0 12058 swap_insertion (old_loc, loc2);
c7d46a38
PA
12059 keep_in_target = 1;
12060 break;
12061 }
876fa593
JK
12062 }
12063 }
12064 }
74960c60
VP
12065 }
12066
20874c92
VP
12067 if (!keep_in_target)
12068 {
834c0d03 12069 if (remove_breakpoint (old_loc))
20874c92 12070 {
4a64f543
MS
12071 /* This is just about all we can do. We could keep
12072 this location on the global list, and try to
12073 remove it next time, but there's no particular
12074 reason why we will succeed next time.
20874c92 12075
4a64f543
MS
12076 Note that at this point, old_loc->owner is still
12077 valid, as delete_breakpoint frees the breakpoint
12078 only after calling us. */
3e43a32a
MS
12079 printf_filtered (_("warning: Error removing "
12080 "breakpoint %d\n"),
876fa593 12081 old_loc->owner->number);
20874c92
VP
12082 }
12083 removed = 1;
12084 }
0d381245 12085 }
74960c60
VP
12086
12087 if (!found_object)
1c5cfe86 12088 {
fbea99ea 12089 if (removed && target_is_non_stop_p ()
1cf4d951 12090 && need_moribund_for_location_type (old_loc))
20874c92 12091 {
db82e815
PA
12092 /* This location was removed from the target. In
12093 non-stop mode, a race condition is possible where
12094 we've removed a breakpoint, but stop events for that
12095 breakpoint are already queued and will arrive later.
12096 We apply an heuristic to be able to distinguish such
12097 SIGTRAPs from other random SIGTRAPs: we keep this
12098 breakpoint location for a bit, and will retire it
12099 after we see some number of events. The theory here
12100 is that reporting of events should, "on the average",
12101 be fair, so after a while we'll see events from all
12102 threads that have anything of interest, and no longer
12103 need to keep this breakpoint location around. We
12104 don't hold locations forever so to reduce chances of
12105 mistaking a non-breakpoint SIGTRAP for a breakpoint
12106 SIGTRAP.
12107
12108 The heuristic failing can be disastrous on
12109 decr_pc_after_break targets.
12110
12111 On decr_pc_after_break targets, like e.g., x86-linux,
12112 if we fail to recognize a late breakpoint SIGTRAP,
12113 because events_till_retirement has reached 0 too
12114 soon, we'll fail to do the PC adjustment, and report
12115 a random SIGTRAP to the user. When the user resumes
12116 the inferior, it will most likely immediately crash
2dec564e 12117 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12118 corrupted, because of being resumed e.g., in the
12119 middle of a multi-byte instruction, or skipped a
12120 one-byte instruction. This was actually seen happen
12121 on native x86-linux, and should be less rare on
12122 targets that do not support new thread events, like
12123 remote, due to the heuristic depending on
12124 thread_count.
12125
12126 Mistaking a random SIGTRAP for a breakpoint trap
12127 causes similar symptoms (PC adjustment applied when
12128 it shouldn't), but then again, playing with SIGTRAPs
12129 behind the debugger's back is asking for trouble.
12130
12131 Since hardware watchpoint traps are always
12132 distinguishable from other traps, so we don't need to
12133 apply keep hardware watchpoint moribund locations
12134 around. We simply always ignore hardware watchpoint
12135 traps we can no longer explain. */
12136
876fa593
JK
12137 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12138 old_loc->owner = NULL;
20874c92 12139
876fa593 12140 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12141 }
12142 else
f431efe5
PA
12143 {
12144 old_loc->owner = NULL;
12145 decref_bp_location (&old_loc);
12146 }
20874c92 12147 }
74960c60 12148 }
1c5cfe86 12149
348d480f
PA
12150 /* Rescan breakpoints at the same address and section, marking the
12151 first one as "first" and any others as "duplicates". This is so
12152 that the bpt instruction is only inserted once. If we have a
12153 permanent breakpoint at the same place as BPT, make that one the
12154 official one, and the rest as duplicates. Permanent breakpoints
12155 are sorted first for the same address.
12156
12157 Do the same for hardware watchpoints, but also considering the
12158 watchpoint's type (regular/access/read) and length. */
12159
12160 bp_loc_first = NULL;
12161 wp_loc_first = NULL;
12162 awp_loc_first = NULL;
12163 rwp_loc_first = NULL;
12164 ALL_BP_LOCATIONS (loc, locp)
12165 {
12166 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12167 non-NULL. */
348d480f 12168 struct bp_location **loc_first_p;
d3fbdd86 12169 b = loc->owner;
348d480f 12170
6f380991 12171 if (!unduplicated_should_be_inserted (loc)
348d480f 12172 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12173 /* Don't detect duplicate for tracepoint locations because they are
12174 never duplicated. See the comments in field `duplicate' of
12175 `struct bp_location'. */
348d480f 12176 || is_tracepoint (b))
b775012e
LM
12177 {
12178 /* Clear the condition modification flag. */
12179 loc->condition_changed = condition_unchanged;
12180 continue;
12181 }
348d480f 12182
348d480f
PA
12183 if (b->type == bp_hardware_watchpoint)
12184 loc_first_p = &wp_loc_first;
12185 else if (b->type == bp_read_watchpoint)
12186 loc_first_p = &rwp_loc_first;
12187 else if (b->type == bp_access_watchpoint)
12188 loc_first_p = &awp_loc_first;
12189 else
12190 loc_first_p = &bp_loc_first;
12191
12192 if (*loc_first_p == NULL
12193 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12194 || !breakpoint_locations_match (loc, *loc_first_p))
12195 {
12196 *loc_first_p = loc;
12197 loc->duplicate = 0;
b775012e
LM
12198
12199 if (is_breakpoint (loc->owner) && loc->condition_changed)
12200 {
12201 loc->needs_update = 1;
12202 /* Clear the condition modification flag. */
12203 loc->condition_changed = condition_unchanged;
12204 }
348d480f
PA
12205 continue;
12206 }
12207
934709f0
PW
12208
12209 /* This and the above ensure the invariant that the first location
12210 is not duplicated, and is the inserted one.
12211 All following are marked as duplicated, and are not inserted. */
12212 if (loc->inserted)
12213 swap_insertion (loc, *loc_first_p);
348d480f
PA
12214 loc->duplicate = 1;
12215
b775012e
LM
12216 /* Clear the condition modification flag. */
12217 loc->condition_changed = condition_unchanged;
348d480f
PA
12218 }
12219
a25a5a45 12220 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12221 {
04086b45 12222 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12223 insert_breakpoint_locations ();
12224 else
12225 {
44702360
PA
12226 /* Even though the caller told us to not insert new
12227 locations, we may still need to update conditions on the
12228 target's side of breakpoints that were already inserted
12229 if the target is evaluating breakpoint conditions. We
b775012e
LM
12230 only update conditions for locations that are marked
12231 "needs_update". */
12232 update_inserted_breakpoint_locations ();
12233 }
12234 }
348d480f 12235
04086b45 12236 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12237 download_tracepoint_locations ();
348d480f
PA
12238}
12239
12240void
12241breakpoint_retire_moribund (void)
12242{
12243 struct bp_location *loc;
12244 int ix;
12245
12246 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12247 if (--(loc->events_till_retirement) == 0)
12248 {
12249 decref_bp_location (&loc);
12250 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12251 --ix;
12252 }
12253}
12254
12255static void
44702360 12256update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12257{
348d480f 12258
492d29ea
PA
12259 TRY
12260 {
12261 update_global_location_list (insert_mode);
12262 }
12263 CATCH (e, RETURN_MASK_ERROR)
12264 {
12265 }
12266 END_CATCH
348d480f
PA
12267}
12268
12269/* Clear BKP from a BPS. */
12270
12271static void
12272bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12273{
12274 bpstat bs;
12275
12276 for (bs = bps; bs; bs = bs->next)
12277 if (bs->breakpoint_at == bpt)
12278 {
12279 bs->breakpoint_at = NULL;
12280 bs->old_val = NULL;
12281 /* bs->commands will be freed later. */
12282 }
12283}
12284
12285/* Callback for iterate_over_threads. */
12286static int
12287bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12288{
9a3c8263 12289 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12290
12291 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12292 return 0;
12293}
12294
12295/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12296 callbacks. */
12297
12298static void
12299say_where (struct breakpoint *b)
12300{
12301 struct value_print_options opts;
12302
12303 get_user_print_options (&opts);
12304
12305 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12306 single string. */
12307 if (b->loc == NULL)
12308 {
f00aae0f
KS
12309 /* For pending locations, the output differs slightly based
12310 on b->extra_string. If this is non-NULL, it contains either
12311 a condition or dprintf arguments. */
12312 if (b->extra_string == NULL)
12313 {
12314 printf_filtered (_(" (%s) pending."),
d28cd78a 12315 event_location_to_string (b->location.get ()));
f00aae0f
KS
12316 }
12317 else if (b->type == bp_dprintf)
12318 {
12319 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12320 event_location_to_string (b->location.get ()),
f00aae0f
KS
12321 b->extra_string);
12322 }
12323 else
12324 {
12325 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12326 event_location_to_string (b->location.get ()),
f00aae0f
KS
12327 b->extra_string);
12328 }
348d480f
PA
12329 }
12330 else
12331 {
2f202fde 12332 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12333 {
12334 printf_filtered (" at ");
12335 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12336 gdb_stdout);
12337 }
2f202fde 12338 if (b->loc->symtab != NULL)
f8eba3c6
TT
12339 {
12340 /* If there is a single location, we can print the location
12341 more nicely. */
12342 if (b->loc->next == NULL)
12343 printf_filtered (": file %s, line %d.",
05cba821
JK
12344 symtab_to_filename_for_display (b->loc->symtab),
12345 b->loc->line_number);
f8eba3c6
TT
12346 else
12347 /* This is not ideal, but each location may have a
12348 different file name, and this at least reflects the
12349 real situation somewhat. */
f00aae0f 12350 printf_filtered (": %s.",
d28cd78a 12351 event_location_to_string (b->location.get ()));
f8eba3c6 12352 }
348d480f
PA
12353
12354 if (b->loc->next)
12355 {
12356 struct bp_location *loc = b->loc;
12357 int n = 0;
12358 for (; loc; loc = loc->next)
12359 ++n;
12360 printf_filtered (" (%d locations)", n);
12361 }
12362 }
12363}
12364
348d480f
PA
12365/* Default bp_location_ops methods. */
12366
12367static void
12368bp_location_dtor (struct bp_location *self)
12369{
348d480f
PA
12370 xfree (self->function_name);
12371}
12372
12373static const struct bp_location_ops bp_location_ops =
12374{
12375 bp_location_dtor
12376};
12377
c1fc2657 12378/* Destructor for the breakpoint base class. */
348d480f 12379
c1fc2657 12380breakpoint::~breakpoint ()
348d480f 12381{
c1fc2657
SM
12382 xfree (this->cond_string);
12383 xfree (this->extra_string);
12384 xfree (this->filter);
348d480f
PA
12385}
12386
2060206e
PA
12387static struct bp_location *
12388base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12389{
5625a286 12390 return new bp_location (&bp_location_ops, self);
348d480f
PA
12391}
12392
2060206e
PA
12393static void
12394base_breakpoint_re_set (struct breakpoint *b)
12395{
12396 /* Nothing to re-set. */
12397}
12398
12399#define internal_error_pure_virtual_called() \
12400 gdb_assert_not_reached ("pure virtual function called")
12401
12402static int
12403base_breakpoint_insert_location (struct bp_location *bl)
12404{
12405 internal_error_pure_virtual_called ();
12406}
12407
12408static int
73971819
PA
12409base_breakpoint_remove_location (struct bp_location *bl,
12410 enum remove_bp_reason reason)
2060206e
PA
12411{
12412 internal_error_pure_virtual_called ();
12413}
12414
12415static int
12416base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12417 const address_space *aspace,
09ac7c10
TT
12418 CORE_ADDR bp_addr,
12419 const struct target_waitstatus *ws)
2060206e
PA
12420{
12421 internal_error_pure_virtual_called ();
12422}
12423
12424static void
12425base_breakpoint_check_status (bpstat bs)
12426{
12427 /* Always stop. */
12428}
12429
12430/* A "works_in_software_mode" breakpoint_ops method that just internal
12431 errors. */
12432
12433static int
12434base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12435{
12436 internal_error_pure_virtual_called ();
12437}
12438
12439/* A "resources_needed" breakpoint_ops method that just internal
12440 errors. */
12441
12442static int
12443base_breakpoint_resources_needed (const struct bp_location *bl)
12444{
12445 internal_error_pure_virtual_called ();
12446}
12447
12448static enum print_stop_action
12449base_breakpoint_print_it (bpstat bs)
12450{
12451 internal_error_pure_virtual_called ();
12452}
12453
12454static void
12455base_breakpoint_print_one_detail (const struct breakpoint *self,
12456 struct ui_out *uiout)
12457{
12458 /* nothing */
12459}
12460
12461static void
12462base_breakpoint_print_mention (struct breakpoint *b)
12463{
12464 internal_error_pure_virtual_called ();
12465}
12466
12467static void
12468base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12469{
12470 internal_error_pure_virtual_called ();
12471}
12472
983af33b 12473static void
f00aae0f
KS
12474base_breakpoint_create_sals_from_location
12475 (const struct event_location *location,
12476 struct linespec_result *canonical,
12477 enum bptype type_wanted)
983af33b
SDJ
12478{
12479 internal_error_pure_virtual_called ();
12480}
12481
12482static void
12483base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12484 struct linespec_result *c,
e1e01040
PA
12485 gdb::unique_xmalloc_ptr<char> cond_string,
12486 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12487 enum bptype type_wanted,
12488 enum bpdisp disposition,
12489 int thread,
12490 int task, int ignore_count,
12491 const struct breakpoint_ops *o,
12492 int from_tty, int enabled,
44f238bb 12493 int internal, unsigned flags)
983af33b
SDJ
12494{
12495 internal_error_pure_virtual_called ();
12496}
12497
6c5b2ebe 12498static std::vector<symtab_and_line>
f00aae0f
KS
12499base_breakpoint_decode_location (struct breakpoint *b,
12500 const struct event_location *location,
6c5b2ebe 12501 struct program_space *search_pspace)
983af33b
SDJ
12502{
12503 internal_error_pure_virtual_called ();
12504}
12505
ab04a2af
TT
12506/* The default 'explains_signal' method. */
12507
47591c29 12508static int
427cd150 12509base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12510{
47591c29 12511 return 1;
ab04a2af
TT
12512}
12513
9d6e6e84
HZ
12514/* The default "after_condition_true" method. */
12515
12516static void
12517base_breakpoint_after_condition_true (struct bpstats *bs)
12518{
12519 /* Nothing to do. */
12520}
12521
ab04a2af 12522struct breakpoint_ops base_breakpoint_ops =
2060206e 12523{
2060206e
PA
12524 base_breakpoint_allocate_location,
12525 base_breakpoint_re_set,
12526 base_breakpoint_insert_location,
12527 base_breakpoint_remove_location,
12528 base_breakpoint_breakpoint_hit,
12529 base_breakpoint_check_status,
12530 base_breakpoint_resources_needed,
12531 base_breakpoint_works_in_software_mode,
12532 base_breakpoint_print_it,
12533 NULL,
12534 base_breakpoint_print_one_detail,
12535 base_breakpoint_print_mention,
983af33b 12536 base_breakpoint_print_recreate,
5f700d83 12537 base_breakpoint_create_sals_from_location,
983af33b 12538 base_breakpoint_create_breakpoints_sal,
5f700d83 12539 base_breakpoint_decode_location,
9d6e6e84
HZ
12540 base_breakpoint_explains_signal,
12541 base_breakpoint_after_condition_true,
2060206e
PA
12542};
12543
12544/* Default breakpoint_ops methods. */
12545
12546static void
348d480f
PA
12547bkpt_re_set (struct breakpoint *b)
12548{
06edf0c0 12549 /* FIXME: is this still reachable? */
9ef9e6a6 12550 if (breakpoint_event_location_empty_p (b))
06edf0c0 12551 {
f00aae0f 12552 /* Anything without a location can't be re-set. */
348d480f 12553 delete_breakpoint (b);
06edf0c0 12554 return;
348d480f 12555 }
06edf0c0
PA
12556
12557 breakpoint_re_set_default (b);
348d480f
PA
12558}
12559
2060206e 12560static int
348d480f
PA
12561bkpt_insert_location (struct bp_location *bl)
12562{
cd6c3b4f
YQ
12563 CORE_ADDR addr = bl->target_info.reqstd_address;
12564
579c6ad9 12565 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12566 bl->target_info.placed_address = addr;
12567
348d480f 12568 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12569 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12570 else
7c16b83e 12571 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12572}
12573
2060206e 12574static int
73971819 12575bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12576{
12577 if (bl->loc_type == bp_loc_hardware_breakpoint)
12578 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12579 else
73971819 12580 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12581}
12582
2060206e 12583static int
348d480f 12584bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12585 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12586 const struct target_waitstatus *ws)
348d480f 12587{
09ac7c10 12588 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12589 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12590 return 0;
12591
348d480f
PA
12592 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12593 aspace, bp_addr))
12594 return 0;
12595
12596 if (overlay_debugging /* unmapped overlay section */
12597 && section_is_overlay (bl->section)
12598 && !section_is_mapped (bl->section))
12599 return 0;
12600
12601 return 1;
12602}
12603
cd1608cc
PA
12604static int
12605dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12606 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12607 const struct target_waitstatus *ws)
12608{
12609 if (dprintf_style == dprintf_style_agent
12610 && target_can_run_breakpoint_commands ())
12611 {
12612 /* An agent-style dprintf never causes a stop. If we see a trap
12613 for this address it must be for a breakpoint that happens to
12614 be set at the same address. */
12615 return 0;
12616 }
12617
12618 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12619}
12620
2060206e 12621static int
348d480f
PA
12622bkpt_resources_needed (const struct bp_location *bl)
12623{
12624 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12625
12626 return 1;
12627}
12628
2060206e 12629static enum print_stop_action
348d480f
PA
12630bkpt_print_it (bpstat bs)
12631{
348d480f
PA
12632 struct breakpoint *b;
12633 const struct bp_location *bl;
001c8c33 12634 int bp_temp;
79a45e25 12635 struct ui_out *uiout = current_uiout;
348d480f
PA
12636
12637 gdb_assert (bs->bp_location_at != NULL);
12638
12639 bl = bs->bp_location_at;
12640 b = bs->breakpoint_at;
12641
001c8c33
PA
12642 bp_temp = b->disposition == disp_del;
12643 if (bl->address != bl->requested_address)
12644 breakpoint_adjustment_warning (bl->requested_address,
12645 bl->address,
12646 b->number, 1);
12647 annotate_breakpoint (b->number);
f303dbd6
PA
12648 maybe_print_thread_hit_breakpoint (uiout);
12649
001c8c33 12650 if (bp_temp)
112e8700 12651 uiout->text ("Temporary breakpoint ");
001c8c33 12652 else
112e8700
SM
12653 uiout->text ("Breakpoint ");
12654 if (uiout->is_mi_like_p ())
348d480f 12655 {
112e8700 12656 uiout->field_string ("reason",
001c8c33 12657 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12658 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12659 }
112e8700
SM
12660 uiout->field_int ("bkptno", b->number);
12661 uiout->text (", ");
06edf0c0 12662
001c8c33 12663 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12664}
12665
2060206e 12666static void
06edf0c0
PA
12667bkpt_print_mention (struct breakpoint *b)
12668{
112e8700 12669 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12670 return;
12671
12672 switch (b->type)
12673 {
12674 case bp_breakpoint:
12675 case bp_gnu_ifunc_resolver:
12676 if (b->disposition == disp_del)
12677 printf_filtered (_("Temporary breakpoint"));
12678 else
12679 printf_filtered (_("Breakpoint"));
12680 printf_filtered (_(" %d"), b->number);
12681 if (b->type == bp_gnu_ifunc_resolver)
12682 printf_filtered (_(" at gnu-indirect-function resolver"));
12683 break;
12684 case bp_hardware_breakpoint:
12685 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12686 break;
e7e0cddf
SS
12687 case bp_dprintf:
12688 printf_filtered (_("Dprintf %d"), b->number);
12689 break;
06edf0c0
PA
12690 }
12691
12692 say_where (b);
12693}
12694
2060206e 12695static void
06edf0c0
PA
12696bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12697{
12698 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12699 fprintf_unfiltered (fp, "tbreak");
12700 else if (tp->type == bp_breakpoint)
12701 fprintf_unfiltered (fp, "break");
12702 else if (tp->type == bp_hardware_breakpoint
12703 && tp->disposition == disp_del)
12704 fprintf_unfiltered (fp, "thbreak");
12705 else if (tp->type == bp_hardware_breakpoint)
12706 fprintf_unfiltered (fp, "hbreak");
12707 else
12708 internal_error (__FILE__, __LINE__,
12709 _("unhandled breakpoint type %d"), (int) tp->type);
12710
f00aae0f 12711 fprintf_unfiltered (fp, " %s",
d28cd78a 12712 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12713
12714 /* Print out extra_string if this breakpoint is pending. It might
12715 contain, for example, conditions that were set by the user. */
12716 if (tp->loc == NULL && tp->extra_string != NULL)
12717 fprintf_unfiltered (fp, " %s", tp->extra_string);
12718
dd11a36c 12719 print_recreate_thread (tp, fp);
06edf0c0
PA
12720}
12721
983af33b 12722static void
f00aae0f
KS
12723bkpt_create_sals_from_location (const struct event_location *location,
12724 struct linespec_result *canonical,
12725 enum bptype type_wanted)
983af33b 12726{
f00aae0f 12727 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12728}
12729
12730static void
12731bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12732 struct linespec_result *canonical,
e1e01040
PA
12733 gdb::unique_xmalloc_ptr<char> cond_string,
12734 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12735 enum bptype type_wanted,
12736 enum bpdisp disposition,
12737 int thread,
12738 int task, int ignore_count,
12739 const struct breakpoint_ops *ops,
12740 int from_tty, int enabled,
44f238bb 12741 int internal, unsigned flags)
983af33b 12742{
023fa29b 12743 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12744 std::move (cond_string),
12745 std::move (extra_string),
e7e0cddf 12746 type_wanted,
983af33b
SDJ
12747 disposition, thread, task,
12748 ignore_count, ops, from_tty,
44f238bb 12749 enabled, internal, flags);
983af33b
SDJ
12750}
12751
6c5b2ebe 12752static std::vector<symtab_and_line>
f00aae0f
KS
12753bkpt_decode_location (struct breakpoint *b,
12754 const struct event_location *location,
6c5b2ebe 12755 struct program_space *search_pspace)
983af33b 12756{
6c5b2ebe 12757 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12758}
12759
06edf0c0
PA
12760/* Virtual table for internal breakpoints. */
12761
12762static void
12763internal_bkpt_re_set (struct breakpoint *b)
12764{
12765 switch (b->type)
12766 {
12767 /* Delete overlay event and longjmp master breakpoints; they
12768 will be reset later by breakpoint_re_set. */
12769 case bp_overlay_event:
12770 case bp_longjmp_master:
12771 case bp_std_terminate_master:
12772 case bp_exception_master:
12773 delete_breakpoint (b);
12774 break;
12775
12776 /* This breakpoint is special, it's set up when the inferior
12777 starts and we really don't want to touch it. */
12778 case bp_shlib_event:
12779
12780 /* Like bp_shlib_event, this breakpoint type is special. Once
12781 it is set up, we do not want to touch it. */
12782 case bp_thread_event:
12783 break;
12784 }
12785}
12786
12787static void
12788internal_bkpt_check_status (bpstat bs)
12789{
a9b3a50f
PA
12790 if (bs->breakpoint_at->type == bp_shlib_event)
12791 {
12792 /* If requested, stop when the dynamic linker notifies GDB of
12793 events. This allows the user to get control and place
12794 breakpoints in initializer routines for dynamically loaded
12795 objects (among other things). */
12796 bs->stop = stop_on_solib_events;
12797 bs->print = stop_on_solib_events;
12798 }
12799 else
12800 bs->stop = 0;
06edf0c0
PA
12801}
12802
12803static enum print_stop_action
12804internal_bkpt_print_it (bpstat bs)
12805{
06edf0c0 12806 struct breakpoint *b;
06edf0c0 12807
06edf0c0
PA
12808 b = bs->breakpoint_at;
12809
06edf0c0
PA
12810 switch (b->type)
12811 {
348d480f
PA
12812 case bp_shlib_event:
12813 /* Did we stop because the user set the stop_on_solib_events
12814 variable? (If so, we report this as a generic, "Stopped due
12815 to shlib event" message.) */
edcc5120 12816 print_solib_event (0);
348d480f
PA
12817 break;
12818
12819 case bp_thread_event:
12820 /* Not sure how we will get here.
12821 GDB should not stop for these breakpoints. */
12822 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12823 break;
12824
12825 case bp_overlay_event:
12826 /* By analogy with the thread event, GDB should not stop for these. */
12827 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12828 break;
12829
12830 case bp_longjmp_master:
12831 /* These should never be enabled. */
12832 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12833 break;
12834
12835 case bp_std_terminate_master:
12836 /* These should never be enabled. */
12837 printf_filtered (_("std::terminate Master Breakpoint: "
12838 "gdb should not stop!\n"));
348d480f
PA
12839 break;
12840
12841 case bp_exception_master:
12842 /* These should never be enabled. */
12843 printf_filtered (_("Exception Master Breakpoint: "
12844 "gdb should not stop!\n"));
06edf0c0
PA
12845 break;
12846 }
12847
001c8c33 12848 return PRINT_NOTHING;
06edf0c0
PA
12849}
12850
12851static void
12852internal_bkpt_print_mention (struct breakpoint *b)
12853{
12854 /* Nothing to mention. These breakpoints are internal. */
12855}
12856
06edf0c0
PA
12857/* Virtual table for momentary breakpoints */
12858
12859static void
12860momentary_bkpt_re_set (struct breakpoint *b)
12861{
12862 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12863 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12864 Otherwise these should have been blown away via the cleanup chain
12865 or by breakpoint_init_inferior when we rerun the executable. */
12866}
12867
12868static void
12869momentary_bkpt_check_status (bpstat bs)
12870{
12871 /* Nothing. The point of these breakpoints is causing a stop. */
12872}
12873
12874static enum print_stop_action
12875momentary_bkpt_print_it (bpstat bs)
12876{
001c8c33 12877 return PRINT_UNKNOWN;
348d480f
PA
12878}
12879
06edf0c0
PA
12880static void
12881momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12882{
06edf0c0 12883 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12884}
12885
e2e4d78b
JK
12886/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12887
12888 It gets cleared already on the removal of the first one of such placed
12889 breakpoints. This is OK as they get all removed altogether. */
12890
c1fc2657 12891longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12892{
c1fc2657 12893 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12894
c1fc2657 12895 if (tp != NULL)
e2e4d78b 12896 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12897}
12898
55aa24fb
SDJ
12899/* Specific methods for probe breakpoints. */
12900
12901static int
12902bkpt_probe_insert_location (struct bp_location *bl)
12903{
12904 int v = bkpt_insert_location (bl);
12905
12906 if (v == 0)
12907 {
12908 /* The insertion was successful, now let's set the probe's semaphore
12909 if needed. */
0ea5cda8
SDJ
12910 if (bl->probe.probe->pops->set_semaphore != NULL)
12911 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12912 bl->probe.objfile,
12913 bl->gdbarch);
55aa24fb
SDJ
12914 }
12915
12916 return v;
12917}
12918
12919static int
73971819
PA
12920bkpt_probe_remove_location (struct bp_location *bl,
12921 enum remove_bp_reason reason)
55aa24fb
SDJ
12922{
12923 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12924 if (bl->probe.probe->pops->clear_semaphore != NULL)
12925 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12926 bl->probe.objfile,
12927 bl->gdbarch);
55aa24fb 12928
73971819 12929 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12930}
12931
12932static void
f00aae0f 12933bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12934 struct linespec_result *canonical,
f00aae0f 12935 enum bptype type_wanted)
55aa24fb
SDJ
12936{
12937 struct linespec_sals lsal;
12938
c2f4122d 12939 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12940 lsal.canonical
12941 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12942 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12943}
12944
6c5b2ebe 12945static std::vector<symtab_and_line>
f00aae0f
KS
12946bkpt_probe_decode_location (struct breakpoint *b,
12947 const struct event_location *location,
6c5b2ebe 12948 struct program_space *search_pspace)
55aa24fb 12949{
6c5b2ebe
PA
12950 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12951 if (sals.empty ())
55aa24fb 12952 error (_("probe not found"));
6c5b2ebe 12953 return sals;
55aa24fb
SDJ
12954}
12955
348d480f 12956/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12957
348d480f
PA
12958static void
12959tracepoint_re_set (struct breakpoint *b)
12960{
12961 breakpoint_re_set_default (b);
12962}
876fa593 12963
348d480f
PA
12964static int
12965tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12966 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12967 const struct target_waitstatus *ws)
348d480f
PA
12968{
12969 /* By definition, the inferior does not report stops at
12970 tracepoints. */
12971 return 0;
74960c60
VP
12972}
12973
12974static void
348d480f
PA
12975tracepoint_print_one_detail (const struct breakpoint *self,
12976 struct ui_out *uiout)
74960c60 12977{
d9b3f62e
PA
12978 struct tracepoint *tp = (struct tracepoint *) self;
12979 if (tp->static_trace_marker_id)
348d480f
PA
12980 {
12981 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12982
112e8700
SM
12983 uiout->text ("\tmarker id is ");
12984 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12985 tp->static_trace_marker_id);
112e8700 12986 uiout->text ("\n");
348d480f 12987 }
0d381245
VP
12988}
12989
a474d7c2 12990static void
348d480f 12991tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12992{
112e8700 12993 if (current_uiout->is_mi_like_p ())
348d480f 12994 return;
cc59ec59 12995
348d480f
PA
12996 switch (b->type)
12997 {
12998 case bp_tracepoint:
12999 printf_filtered (_("Tracepoint"));
13000 printf_filtered (_(" %d"), b->number);
13001 break;
13002 case bp_fast_tracepoint:
13003 printf_filtered (_("Fast tracepoint"));
13004 printf_filtered (_(" %d"), b->number);
13005 break;
13006 case bp_static_tracepoint:
13007 printf_filtered (_("Static tracepoint"));
13008 printf_filtered (_(" %d"), b->number);
13009 break;
13010 default:
13011 internal_error (__FILE__, __LINE__,
13012 _("unhandled tracepoint type %d"), (int) b->type);
13013 }
13014
13015 say_where (b);
a474d7c2
PA
13016}
13017
348d480f 13018static void
d9b3f62e 13019tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13020{
d9b3f62e
PA
13021 struct tracepoint *tp = (struct tracepoint *) self;
13022
13023 if (self->type == bp_fast_tracepoint)
348d480f 13024 fprintf_unfiltered (fp, "ftrace");
c93e8391 13025 else if (self->type == bp_static_tracepoint)
348d480f 13026 fprintf_unfiltered (fp, "strace");
d9b3f62e 13027 else if (self->type == bp_tracepoint)
348d480f
PA
13028 fprintf_unfiltered (fp, "trace");
13029 else
13030 internal_error (__FILE__, __LINE__,
d9b3f62e 13031 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13032
f00aae0f 13033 fprintf_unfiltered (fp, " %s",
d28cd78a 13034 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13035 print_recreate_thread (self, fp);
13036
13037 if (tp->pass_count)
13038 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13039}
13040
983af33b 13041static void
f00aae0f
KS
13042tracepoint_create_sals_from_location (const struct event_location *location,
13043 struct linespec_result *canonical,
13044 enum bptype type_wanted)
983af33b 13045{
f00aae0f 13046 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13047}
13048
13049static void
13050tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13051 struct linespec_result *canonical,
e1e01040
PA
13052 gdb::unique_xmalloc_ptr<char> cond_string,
13053 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13054 enum bptype type_wanted,
13055 enum bpdisp disposition,
13056 int thread,
13057 int task, int ignore_count,
13058 const struct breakpoint_ops *ops,
13059 int from_tty, int enabled,
44f238bb 13060 int internal, unsigned flags)
983af33b 13061{
023fa29b 13062 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13063 std::move (cond_string),
13064 std::move (extra_string),
e7e0cddf 13065 type_wanted,
983af33b
SDJ
13066 disposition, thread, task,
13067 ignore_count, ops, from_tty,
44f238bb 13068 enabled, internal, flags);
983af33b
SDJ
13069}
13070
6c5b2ebe 13071static std::vector<symtab_and_line>
f00aae0f
KS
13072tracepoint_decode_location (struct breakpoint *b,
13073 const struct event_location *location,
6c5b2ebe 13074 struct program_space *search_pspace)
983af33b 13075{
6c5b2ebe 13076 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13077}
13078
2060206e 13079struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13080
55aa24fb
SDJ
13081/* The breakpoint_ops structure to be use on tracepoints placed in a
13082 static probe. */
13083
13084static void
f00aae0f
KS
13085tracepoint_probe_create_sals_from_location
13086 (const struct event_location *location,
13087 struct linespec_result *canonical,
13088 enum bptype type_wanted)
55aa24fb
SDJ
13089{
13090 /* We use the same method for breakpoint on probes. */
f00aae0f 13091 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13092}
13093
6c5b2ebe 13094static std::vector<symtab_and_line>
f00aae0f
KS
13095tracepoint_probe_decode_location (struct breakpoint *b,
13096 const struct event_location *location,
6c5b2ebe 13097 struct program_space *search_pspace)
55aa24fb
SDJ
13098{
13099 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13100 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13101}
13102
13103static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13104
5c2b4418
HZ
13105/* Dprintf breakpoint_ops methods. */
13106
13107static void
13108dprintf_re_set (struct breakpoint *b)
13109{
13110 breakpoint_re_set_default (b);
13111
f00aae0f
KS
13112 /* extra_string should never be non-NULL for dprintf. */
13113 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13114
13115 /* 1 - connect to target 1, that can run breakpoint commands.
13116 2 - create a dprintf, which resolves fine.
13117 3 - disconnect from target 1
13118 4 - connect to target 2, that can NOT run breakpoint commands.
13119
13120 After steps #3/#4, you'll want the dprintf command list to
13121 be updated, because target 1 and 2 may well return different
13122 answers for target_can_run_breakpoint_commands().
13123 Given absence of finer grained resetting, we get to do
13124 it all the time. */
13125 if (b->extra_string != NULL)
13126 update_dprintf_command_list (b);
13127}
13128
2d9442cc
HZ
13129/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13130
13131static void
13132dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13133{
f00aae0f 13134 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13135 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13136 tp->extra_string);
13137 print_recreate_thread (tp, fp);
13138}
13139
9d6e6e84
HZ
13140/* Implement the "after_condition_true" breakpoint_ops method for
13141 dprintf.
13142
13143 dprintf's are implemented with regular commands in their command
13144 list, but we run the commands here instead of before presenting the
13145 stop to the user, as dprintf's don't actually cause a stop. This
13146 also makes it so that the commands of multiple dprintfs at the same
13147 address are all handled. */
13148
13149static void
13150dprintf_after_condition_true (struct bpstats *bs)
13151{
04afa70c 13152 struct bpstats tmp_bs;
9d6e6e84
HZ
13153 struct bpstats *tmp_bs_p = &tmp_bs;
13154
13155 /* dprintf's never cause a stop. This wasn't set in the
13156 check_status hook instead because that would make the dprintf's
13157 condition not be evaluated. */
13158 bs->stop = 0;
13159
13160 /* Run the command list here. Take ownership of it instead of
13161 copying. We never want these commands to run later in
13162 bpstat_do_actions, if a breakpoint that causes a stop happens to
13163 be set at same address as this dprintf, or even if running the
13164 commands here throws. */
13165 tmp_bs.commands = bs->commands;
13166 bs->commands = NULL;
9d6e6e84
HZ
13167
13168 bpstat_do_actions_1 (&tmp_bs_p);
13169
13170 /* 'tmp_bs.commands' will usually be NULL by now, but
13171 bpstat_do_actions_1 may return early without processing the whole
13172 list. */
9d6e6e84
HZ
13173}
13174
983af33b
SDJ
13175/* The breakpoint_ops structure to be used on static tracepoints with
13176 markers (`-m'). */
13177
13178static void
f00aae0f 13179strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13180 struct linespec_result *canonical,
f00aae0f 13181 enum bptype type_wanted)
983af33b
SDJ
13182{
13183 struct linespec_sals lsal;
f00aae0f 13184 const char *arg_start, *arg;
983af33b 13185
f00aae0f
KS
13186 arg = arg_start = get_linespec_location (location);
13187 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13188
f2fc3015
TT
13189 std::string str (arg_start, arg - arg_start);
13190 const char *ptr = str.c_str ();
13191 canonical->location = new_linespec_location (&ptr);
983af33b 13192
8e9e35b1
TT
13193 lsal.canonical
13194 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13195 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13196}
13197
13198static void
13199strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13200 struct linespec_result *canonical,
e1e01040
PA
13201 gdb::unique_xmalloc_ptr<char> cond_string,
13202 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13203 enum bptype type_wanted,
13204 enum bpdisp disposition,
13205 int thread,
13206 int task, int ignore_count,
13207 const struct breakpoint_ops *ops,
13208 int from_tty, int enabled,
44f238bb 13209 int internal, unsigned flags)
983af33b 13210{
6c5b2ebe 13211 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13212
13213 /* If the user is creating a static tracepoint by marker id
13214 (strace -m MARKER_ID), then store the sals index, so that
13215 breakpoint_re_set can try to match up which of the newly
13216 found markers corresponds to this one, and, don't try to
13217 expand multiple locations for each sal, given than SALS
13218 already should contain all sals for MARKER_ID. */
13219
6c5b2ebe 13220 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13221 {
6c5b2ebe
PA
13222 event_location_up location
13223 = copy_event_location (canonical->location.get ());
983af33b 13224
b270e6f9 13225 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13226 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13227 std::move (location), NULL,
e1e01040
PA
13228 std::move (cond_string),
13229 std::move (extra_string),
e7e0cddf 13230 type_wanted, disposition,
983af33b 13231 thread, task, ignore_count, ops,
44f238bb 13232 from_tty, enabled, internal, flags,
983af33b
SDJ
13233 canonical->special_display);
13234 /* Given that its possible to have multiple markers with
13235 the same string id, if the user is creating a static
13236 tracepoint by marker id ("strace -m MARKER_ID"), then
13237 store the sals index, so that breakpoint_re_set can
13238 try to match up which of the newly found markers
13239 corresponds to this one */
13240 tp->static_trace_marker_id_idx = i;
13241
b270e6f9 13242 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13243 }
13244}
13245
6c5b2ebe 13246static std::vector<symtab_and_line>
f00aae0f
KS
13247strace_marker_decode_location (struct breakpoint *b,
13248 const struct event_location *location,
6c5b2ebe 13249 struct program_space *search_pspace)
983af33b
SDJ
13250{
13251 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13252 const char *s = get_linespec_location (location);
983af33b 13253
6c5b2ebe
PA
13254 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13255 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13256 {
6c5b2ebe
PA
13257 sals[0] = sals[tp->static_trace_marker_id_idx];
13258 sals.resize (1);
13259 return sals;
983af33b
SDJ
13260 }
13261 else
13262 error (_("marker %s not found"), tp->static_trace_marker_id);
13263}
13264
13265static struct breakpoint_ops strace_marker_breakpoint_ops;
13266
13267static int
13268strace_marker_p (struct breakpoint *b)
13269{
13270 return b->ops == &strace_marker_breakpoint_ops;
13271}
13272
53a5351d 13273/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13274 structures. */
c906108c
SS
13275
13276void
fba45db2 13277delete_breakpoint (struct breakpoint *bpt)
c906108c 13278{
52f0bd74 13279 struct breakpoint *b;
c906108c 13280
8a3fe4f8 13281 gdb_assert (bpt != NULL);
c906108c 13282
4a64f543
MS
13283 /* Has this bp already been deleted? This can happen because
13284 multiple lists can hold pointers to bp's. bpstat lists are
13285 especial culprits.
13286
13287 One example of this happening is a watchpoint's scope bp. When
13288 the scope bp triggers, we notice that the watchpoint is out of
13289 scope, and delete it. We also delete its scope bp. But the
13290 scope bp is marked "auto-deleting", and is already on a bpstat.
13291 That bpstat is then checked for auto-deleting bp's, which are
13292 deleted.
13293
13294 A real solution to this problem might involve reference counts in
13295 bp's, and/or giving them pointers back to their referencing
13296 bpstat's, and teaching delete_breakpoint to only free a bp's
13297 storage when no more references were extent. A cheaper bandaid
13298 was chosen. */
c906108c
SS
13299 if (bpt->type == bp_none)
13300 return;
13301
4a64f543
MS
13302 /* At least avoid this stale reference until the reference counting
13303 of breakpoints gets resolved. */
d0fb5eae 13304 if (bpt->related_breakpoint != bpt)
e5a0a904 13305 {
d0fb5eae 13306 struct breakpoint *related;
3a5c3e22 13307 struct watchpoint *w;
d0fb5eae
JK
13308
13309 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13310 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13311 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13312 w = (struct watchpoint *) bpt;
13313 else
13314 w = NULL;
13315 if (w != NULL)
13316 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13317
13318 /* Unlink bpt from the bpt->related_breakpoint ring. */
13319 for (related = bpt; related->related_breakpoint != bpt;
13320 related = related->related_breakpoint);
13321 related->related_breakpoint = bpt->related_breakpoint;
13322 bpt->related_breakpoint = bpt;
e5a0a904
JK
13323 }
13324
a9634178
TJB
13325 /* watch_command_1 creates a watchpoint but only sets its number if
13326 update_watchpoint succeeds in creating its bp_locations. If there's
13327 a problem in that process, we'll be asked to delete the half-created
13328 watchpoint. In that case, don't announce the deletion. */
13329 if (bpt->number)
13330 observer_notify_breakpoint_deleted (bpt);
c906108c 13331
c906108c
SS
13332 if (breakpoint_chain == bpt)
13333 breakpoint_chain = bpt->next;
13334
c906108c
SS
13335 ALL_BREAKPOINTS (b)
13336 if (b->next == bpt)
c5aa993b
JM
13337 {
13338 b->next = bpt->next;
13339 break;
13340 }
c906108c 13341
f431efe5
PA
13342 /* Be sure no bpstat's are pointing at the breakpoint after it's
13343 been freed. */
13344 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13345 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13346 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13347 commands are associated with the bpstat; if we remove it here,
13348 then the later call to bpstat_do_actions (&stop_bpstat); in
13349 event-top.c won't do anything, and temporary breakpoints with
13350 commands won't work. */
13351
13352 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13353
4a64f543
MS
13354 /* Now that breakpoint is removed from breakpoint list, update the
13355 global location list. This will remove locations that used to
13356 belong to this breakpoint. Do this before freeing the breakpoint
13357 itself, since remove_breakpoint looks at location's owner. It
13358 might be better design to have location completely
13359 self-contained, but it's not the case now. */
44702360 13360 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13361
4a64f543
MS
13362 /* On the chance that someone will soon try again to delete this
13363 same bp, we mark it as deleted before freeing its storage. */
c906108c 13364 bpt->type = bp_none;
4d01a485 13365 delete bpt;
c906108c
SS
13366}
13367
51be5b68
PA
13368/* Iterator function to call a user-provided callback function once
13369 for each of B and its related breakpoints. */
13370
13371static void
13372iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13373 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13374{
13375 struct breakpoint *related;
13376
13377 related = b;
13378 do
13379 {
13380 struct breakpoint *next;
13381
13382 /* FUNCTION may delete RELATED. */
13383 next = related->related_breakpoint;
13384
13385 if (next == related)
13386 {
13387 /* RELATED is the last ring entry. */
48649e1b 13388 function (related);
51be5b68
PA
13389
13390 /* FUNCTION may have deleted it, so we'd never reach back to
13391 B. There's nothing left to do anyway, so just break
13392 out. */
13393 break;
13394 }
13395 else
48649e1b 13396 function (related);
51be5b68
PA
13397
13398 related = next;
13399 }
13400 while (related != b);
13401}
95a42b64 13402
4495129a 13403static void
981a3fb3 13404delete_command (const char *arg, int from_tty)
c906108c 13405{
35df4500 13406 struct breakpoint *b, *b_tmp;
c906108c 13407
ea9365bb
TT
13408 dont_repeat ();
13409
c906108c
SS
13410 if (arg == 0)
13411 {
13412 int breaks_to_delete = 0;
13413
46c6471b
PA
13414 /* Delete all breakpoints if no argument. Do not delete
13415 internal breakpoints, these have to be deleted with an
13416 explicit breakpoint number argument. */
c5aa993b 13417 ALL_BREAKPOINTS (b)
46c6471b 13418 if (user_breakpoint_p (b))
973d738b
DJ
13419 {
13420 breaks_to_delete = 1;
13421 break;
13422 }
c906108c
SS
13423
13424 /* Ask user only if there are some breakpoints to delete. */
13425 if (!from_tty
e2e0b3e5 13426 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13427 {
35df4500 13428 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13429 if (user_breakpoint_p (b))
c5aa993b 13430 delete_breakpoint (b);
c906108c
SS
13431 }
13432 }
13433 else
48649e1b
TT
13434 map_breakpoint_numbers
13435 (arg, [&] (breakpoint *b)
13436 {
13437 iterate_over_related_breakpoints (b, delete_breakpoint);
13438 });
c906108c
SS
13439}
13440
c2f4122d
PA
13441/* Return true if all locations of B bound to PSPACE are pending. If
13442 PSPACE is NULL, all locations of all program spaces are
13443 considered. */
13444
0d381245 13445static int
c2f4122d 13446all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13447{
c2f4122d
PA
13448 struct bp_location *loc;
13449
13450 for (loc = b->loc; loc != NULL; loc = loc->next)
13451 if ((pspace == NULL
13452 || loc->pspace == pspace)
13453 && !loc->shlib_disabled
8645ff69 13454 && !loc->pspace->executing_startup)
0d381245
VP
13455 return 0;
13456 return 1;
fe3f5fa8
VP
13457}
13458
776592bf
DE
13459/* Subroutine of update_breakpoint_locations to simplify it.
13460 Return non-zero if multiple fns in list LOC have the same name.
13461 Null names are ignored. */
13462
13463static int
13464ambiguous_names_p (struct bp_location *loc)
13465{
13466 struct bp_location *l;
13467 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13468 (int (*) (const void *,
13469 const void *)) streq,
776592bf
DE
13470 NULL, xcalloc, xfree);
13471
13472 for (l = loc; l != NULL; l = l->next)
13473 {
13474 const char **slot;
13475 const char *name = l->function_name;
13476
13477 /* Allow for some names to be NULL, ignore them. */
13478 if (name == NULL)
13479 continue;
13480
13481 slot = (const char **) htab_find_slot (htab, (const void *) name,
13482 INSERT);
4a64f543
MS
13483 /* NOTE: We can assume slot != NULL here because xcalloc never
13484 returns NULL. */
776592bf
DE
13485 if (*slot != NULL)
13486 {
13487 htab_delete (htab);
13488 return 1;
13489 }
13490 *slot = name;
13491 }
13492
13493 htab_delete (htab);
13494 return 0;
13495}
13496
0fb4aa4b
PA
13497/* When symbols change, it probably means the sources changed as well,
13498 and it might mean the static tracepoint markers are no longer at
13499 the same address or line numbers they used to be at last we
13500 checked. Losing your static tracepoints whenever you rebuild is
13501 undesirable. This function tries to resync/rematch gdb static
13502 tracepoints with the markers on the target, for static tracepoints
13503 that have not been set by marker id. Static tracepoint that have
13504 been set by marker id are reset by marker id in breakpoint_re_set.
13505 The heuristic is:
13506
13507 1) For a tracepoint set at a specific address, look for a marker at
13508 the old PC. If one is found there, assume to be the same marker.
13509 If the name / string id of the marker found is different from the
13510 previous known name, assume that means the user renamed the marker
13511 in the sources, and output a warning.
13512
13513 2) For a tracepoint set at a given line number, look for a marker
13514 at the new address of the old line number. If one is found there,
13515 assume to be the same marker. If the name / string id of the
13516 marker found is different from the previous known name, assume that
13517 means the user renamed the marker in the sources, and output a
13518 warning.
13519
13520 3) If a marker is no longer found at the same address or line, it
13521 may mean the marker no longer exists. But it may also just mean
13522 the code changed a bit. Maybe the user added a few lines of code
13523 that made the marker move up or down (in line number terms). Ask
13524 the target for info about the marker with the string id as we knew
13525 it. If found, update line number and address in the matching
13526 static tracepoint. This will get confused if there's more than one
13527 marker with the same ID (possible in UST, although unadvised
13528 precisely because it confuses tools). */
13529
13530static struct symtab_and_line
13531update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13532{
d9b3f62e 13533 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13534 struct static_tracepoint_marker marker;
13535 CORE_ADDR pc;
0fb4aa4b
PA
13536
13537 pc = sal.pc;
13538 if (sal.line)
13539 find_line_pc (sal.symtab, sal.line, &pc);
13540
13541 if (target_static_tracepoint_marker_at (pc, &marker))
13542 {
d9b3f62e 13543 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13544 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13545 b->number,
d9b3f62e 13546 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13547
d9b3f62e
PA
13548 xfree (tp->static_trace_marker_id);
13549 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13550 release_static_tracepoint_marker (&marker);
13551
13552 return sal;
13553 }
13554
13555 /* Old marker wasn't found on target at lineno. Try looking it up
13556 by string ID. */
13557 if (!sal.explicit_pc
13558 && sal.line != 0
13559 && sal.symtab != NULL
d9b3f62e 13560 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13561 {
13562 VEC(static_tracepoint_marker_p) *markers;
13563
13564 markers
d9b3f62e 13565 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13566
13567 if (!VEC_empty(static_tracepoint_marker_p, markers))
13568 {
0fb4aa4b 13569 struct symbol *sym;
80e1d417 13570 struct static_tracepoint_marker *tpmarker;
79a45e25 13571 struct ui_out *uiout = current_uiout;
67994074 13572 struct explicit_location explicit_loc;
0fb4aa4b 13573
80e1d417 13574 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13575
d9b3f62e 13576 xfree (tp->static_trace_marker_id);
80e1d417 13577 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13578
13579 warning (_("marker for static tracepoint %d (%s) not "
13580 "found at previous line number"),
d9b3f62e 13581 b->number, tp->static_trace_marker_id);
0fb4aa4b 13582
51abb421 13583 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13584 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13585 uiout->text ("Now in ");
0fb4aa4b
PA
13586 if (sym)
13587 {
112e8700
SM
13588 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13589 uiout->text (" at ");
0fb4aa4b 13590 }
112e8700 13591 uiout->field_string ("file",
05cba821 13592 symtab_to_filename_for_display (sal2.symtab));
112e8700 13593 uiout->text (":");
0fb4aa4b 13594
112e8700 13595 if (uiout->is_mi_like_p ())
0fb4aa4b 13596 {
0b0865da 13597 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13598
112e8700 13599 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13600 }
13601
112e8700
SM
13602 uiout->field_int ("line", sal2.line);
13603 uiout->text ("\n");
0fb4aa4b 13604
80e1d417 13605 b->loc->line_number = sal2.line;
2f202fde 13606 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13607
d28cd78a 13608 b->location.reset (NULL);
67994074
KS
13609 initialize_explicit_location (&explicit_loc);
13610 explicit_loc.source_filename
00e52e53 13611 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13612 explicit_loc.line_offset.offset = b->loc->line_number;
13613 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13614 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13615
13616 /* Might be nice to check if function changed, and warn if
13617 so. */
13618
80e1d417 13619 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13620 }
13621 }
13622 return sal;
13623}
13624
8d3788bd
VP
13625/* Returns 1 iff locations A and B are sufficiently same that
13626 we don't need to report breakpoint as changed. */
13627
13628static int
13629locations_are_equal (struct bp_location *a, struct bp_location *b)
13630{
13631 while (a && b)
13632 {
13633 if (a->address != b->address)
13634 return 0;
13635
13636 if (a->shlib_disabled != b->shlib_disabled)
13637 return 0;
13638
13639 if (a->enabled != b->enabled)
13640 return 0;
13641
13642 a = a->next;
13643 b = b->next;
13644 }
13645
13646 if ((a == NULL) != (b == NULL))
13647 return 0;
13648
13649 return 1;
13650}
13651
c2f4122d
PA
13652/* Split all locations of B that are bound to PSPACE out of B's
13653 location list to a separate list and return that list's head. If
13654 PSPACE is NULL, hoist out all locations of B. */
13655
13656static struct bp_location *
13657hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13658{
13659 struct bp_location head;
13660 struct bp_location *i = b->loc;
13661 struct bp_location **i_link = &b->loc;
13662 struct bp_location *hoisted = &head;
13663
13664 if (pspace == NULL)
13665 {
13666 i = b->loc;
13667 b->loc = NULL;
13668 return i;
13669 }
13670
13671 head.next = NULL;
13672
13673 while (i != NULL)
13674 {
13675 if (i->pspace == pspace)
13676 {
13677 *i_link = i->next;
13678 i->next = NULL;
13679 hoisted->next = i;
13680 hoisted = i;
13681 }
13682 else
13683 i_link = &i->next;
13684 i = *i_link;
13685 }
13686
13687 return head.next;
13688}
13689
13690/* Create new breakpoint locations for B (a hardware or software
13691 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13692 zero, then B is a ranged breakpoint. Only recreates locations for
13693 FILTER_PSPACE. Locations of other program spaces are left
13694 untouched. */
f1310107 13695
0e30163f 13696void
0d381245 13697update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13698 struct program_space *filter_pspace,
6c5b2ebe
PA
13699 gdb::array_view<const symtab_and_line> sals,
13700 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13701{
13702 int i;
c2f4122d 13703 struct bp_location *existing_locations;
0d381245 13704
6c5b2ebe 13705 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13706 {
13707 /* Ranged breakpoints have only one start location and one end
13708 location. */
13709 b->enable_state = bp_disabled;
f8eba3c6
TT
13710 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13711 "multiple locations found\n"),
13712 b->number);
13713 return;
13714 }
f1310107 13715
4a64f543
MS
13716 /* If there's no new locations, and all existing locations are
13717 pending, don't do anything. This optimizes the common case where
13718 all locations are in the same shared library, that was unloaded.
13719 We'd like to retain the location, so that when the library is
13720 loaded again, we don't loose the enabled/disabled status of the
13721 individual locations. */
6c5b2ebe 13722 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13723 return;
13724
c2f4122d 13725 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13726
6c5b2ebe 13727 for (const auto &sal : sals)
fe3f5fa8 13728 {
f8eba3c6
TT
13729 struct bp_location *new_loc;
13730
6c5b2ebe 13731 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13732
6c5b2ebe 13733 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13734
0d381245
VP
13735 /* Reparse conditions, they might contain references to the
13736 old symtab. */
13737 if (b->cond_string != NULL)
13738 {
bbc13ae3 13739 const char *s;
fe3f5fa8 13740
0d381245 13741 s = b->cond_string;
492d29ea 13742 TRY
0d381245 13743 {
6c5b2ebe
PA
13744 new_loc->cond = parse_exp_1 (&s, sal.pc,
13745 block_for_pc (sal.pc),
0d381245
VP
13746 0);
13747 }
492d29ea 13748 CATCH (e, RETURN_MASK_ERROR)
0d381245 13749 {
3e43a32a
MS
13750 warning (_("failed to reevaluate condition "
13751 "for breakpoint %d: %s"),
0d381245
VP
13752 b->number, e.message);
13753 new_loc->enabled = 0;
13754 }
492d29ea 13755 END_CATCH
0d381245 13756 }
fe3f5fa8 13757
6c5b2ebe 13758 if (!sals_end.empty ())
f1310107 13759 {
6c5b2ebe 13760 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13761
6c5b2ebe 13762 new_loc->length = end - sals[0].pc + 1;
f1310107 13763 }
0d381245 13764 }
fe3f5fa8 13765
4a64f543
MS
13766 /* If possible, carry over 'disable' status from existing
13767 breakpoints. */
0d381245
VP
13768 {
13769 struct bp_location *e = existing_locations;
776592bf
DE
13770 /* If there are multiple breakpoints with the same function name,
13771 e.g. for inline functions, comparing function names won't work.
13772 Instead compare pc addresses; this is just a heuristic as things
13773 may have moved, but in practice it gives the correct answer
13774 often enough until a better solution is found. */
13775 int have_ambiguous_names = ambiguous_names_p (b->loc);
13776
0d381245
VP
13777 for (; e; e = e->next)
13778 {
13779 if (!e->enabled && e->function_name)
13780 {
13781 struct bp_location *l = b->loc;
776592bf
DE
13782 if (have_ambiguous_names)
13783 {
13784 for (; l; l = l->next)
f1310107 13785 if (breakpoint_locations_match (e, l))
776592bf
DE
13786 {
13787 l->enabled = 0;
13788 break;
13789 }
13790 }
13791 else
13792 {
13793 for (; l; l = l->next)
13794 if (l->function_name
13795 && strcmp (e->function_name, l->function_name) == 0)
13796 {
13797 l->enabled = 0;
13798 break;
13799 }
13800 }
0d381245
VP
13801 }
13802 }
13803 }
fe3f5fa8 13804
8d3788bd
VP
13805 if (!locations_are_equal (existing_locations, b->loc))
13806 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13807}
13808
f00aae0f 13809/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13810 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13811
6c5b2ebe 13812static std::vector<symtab_and_line>
f00aae0f 13813location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13814 struct program_space *search_pspace, int *found)
ef23e705 13815{
492d29ea 13816 struct gdb_exception exception = exception_none;
ef23e705 13817
983af33b 13818 gdb_assert (b->ops != NULL);
ef23e705 13819
6c5b2ebe
PA
13820 std::vector<symtab_and_line> sals;
13821
492d29ea 13822 TRY
ef23e705 13823 {
6c5b2ebe 13824 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13825 }
492d29ea 13826 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13827 {
13828 int not_found_and_ok = 0;
492d29ea
PA
13829
13830 exception = e;
13831
ef23e705
TJB
13832 /* For pending breakpoints, it's expected that parsing will
13833 fail until the right shared library is loaded. User has
13834 already told to create pending breakpoints and don't need
13835 extra messages. If breakpoint is in bp_shlib_disabled
13836 state, then user already saw the message about that
13837 breakpoint being disabled, and don't want to see more
13838 errors. */
58438ac1 13839 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13840 && (b->condition_not_parsed
13841 || (b->loc != NULL
13842 && search_pspace != NULL
13843 && b->loc->pspace != search_pspace)
ef23e705 13844 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13845 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13846 || b->enable_state == bp_disabled))
13847 not_found_and_ok = 1;
13848
13849 if (!not_found_and_ok)
13850 {
13851 /* We surely don't want to warn about the same breakpoint
13852 10 times. One solution, implemented here, is disable
13853 the breakpoint on error. Another solution would be to
13854 have separate 'warning emitted' flag. Since this
13855 happens only when a binary has changed, I don't know
13856 which approach is better. */
13857 b->enable_state = bp_disabled;
13858 throw_exception (e);
13859 }
13860 }
492d29ea 13861 END_CATCH
ef23e705 13862
492d29ea 13863 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13864 {
6c5b2ebe
PA
13865 for (auto &sal : sals)
13866 resolve_sal_pc (&sal);
f00aae0f 13867 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13868 {
ed1d1739
KS
13869 char *cond_string, *extra_string;
13870 int thread, task;
ef23e705 13871
6c5b2ebe 13872 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13873 &cond_string, &thread, &task,
13874 &extra_string);
f00aae0f 13875 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13876 if (cond_string)
13877 b->cond_string = cond_string;
13878 b->thread = thread;
13879 b->task = task;
e7e0cddf 13880 if (extra_string)
f00aae0f
KS
13881 {
13882 xfree (b->extra_string);
13883 b->extra_string = extra_string;
13884 }
ef23e705
TJB
13885 b->condition_not_parsed = 0;
13886 }
13887
983af33b 13888 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13889 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13890
58438ac1
TT
13891 *found = 1;
13892 }
13893 else
13894 *found = 0;
ef23e705
TJB
13895
13896 return sals;
13897}
13898
348d480f
PA
13899/* The default re_set method, for typical hardware or software
13900 breakpoints. Reevaluate the breakpoint and recreate its
13901 locations. */
13902
13903static void
28010a5d 13904breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13905{
c2f4122d 13906 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13907 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13908
6c5b2ebe
PA
13909 int found;
13910 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13911 filter_pspace, &found);
ef23e705 13912 if (found)
6c5b2ebe 13913 expanded = std::move (sals);
ef23e705 13914
f00aae0f 13915 if (b->location_range_end != NULL)
f1310107 13916 {
6c5b2ebe
PA
13917 std::vector<symtab_and_line> sals_end
13918 = location_to_sals (b, b->location_range_end.get (),
13919 filter_pspace, &found);
f1310107 13920 if (found)
6c5b2ebe 13921 expanded_end = std::move (sals_end);
f1310107
TJB
13922 }
13923
c2f4122d 13924 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13925}
13926
983af33b
SDJ
13927/* Default method for creating SALs from an address string. It basically
13928 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13929
13930static void
f00aae0f
KS
13931create_sals_from_location_default (const struct event_location *location,
13932 struct linespec_result *canonical,
13933 enum bptype type_wanted)
983af33b 13934{
f00aae0f 13935 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13936}
13937
13938/* Call create_breakpoints_sal for the given arguments. This is the default
13939 function for the `create_breakpoints_sal' method of
13940 breakpoint_ops. */
13941
13942static void
13943create_breakpoints_sal_default (struct gdbarch *gdbarch,
13944 struct linespec_result *canonical,
e1e01040
PA
13945 gdb::unique_xmalloc_ptr<char> cond_string,
13946 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13947 enum bptype type_wanted,
13948 enum bpdisp disposition,
13949 int thread,
13950 int task, int ignore_count,
13951 const struct breakpoint_ops *ops,
13952 int from_tty, int enabled,
44f238bb 13953 int internal, unsigned flags)
983af33b 13954{
e1e01040
PA
13955 create_breakpoints_sal (gdbarch, canonical,
13956 std::move (cond_string),
13957 std::move (extra_string),
983af33b
SDJ
13958 type_wanted, disposition,
13959 thread, task, ignore_count, ops, from_tty,
44f238bb 13960 enabled, internal, flags);
983af33b
SDJ
13961}
13962
13963/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13964 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13965
6c5b2ebe 13966static std::vector<symtab_and_line>
f00aae0f
KS
13967decode_location_default (struct breakpoint *b,
13968 const struct event_location *location,
6c5b2ebe 13969 struct program_space *search_pspace)
983af33b
SDJ
13970{
13971 struct linespec_result canonical;
13972
c2f4122d 13973 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
13974 (struct symtab *) NULL, 0,
13975 &canonical, multiple_symbols_all,
13976 b->filter);
13977
13978 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13979 gdb_assert (canonical.lsals.size () < 2);
983af33b 13980
6c5b2ebe 13981 if (!canonical.lsals.empty ())
983af33b 13982 {
6c5b2ebe
PA
13983 const linespec_sals &lsal = canonical.lsals[0];
13984 return std::move (lsal.sals);
983af33b 13985 }
6c5b2ebe 13986 return {};
983af33b
SDJ
13987}
13988
bf469271 13989/* Reset a breakpoint. */
c906108c 13990
bf469271
PA
13991static void
13992breakpoint_re_set_one (breakpoint *b)
c906108c 13993{
fdf44873
TT
13994 input_radix = b->input_radix;
13995 set_language (b->language);
c906108c 13996
348d480f 13997 b->ops->re_set (b);
c906108c
SS
13998}
13999
c2f4122d
PA
14000/* Re-set breakpoint locations for the current program space.
14001 Locations bound to other program spaces are left untouched. */
14002
c906108c 14003void
69de3c6a 14004breakpoint_re_set (void)
c906108c 14005{
35df4500 14006 struct breakpoint *b, *b_tmp;
2a7f3dff 14007
c5aa993b 14008 {
fdf44873
TT
14009 scoped_restore_current_language save_language;
14010 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14011 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14012
5ed8105e
PA
14013 /* Note: we must not try to insert locations until after all
14014 breakpoints have been re-set. Otherwise, e.g., when re-setting
14015 breakpoint 1, we'd insert the locations of breakpoint 2, which
14016 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14017
5ed8105e
PA
14018 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14019 {
bf469271
PA
14020 TRY
14021 {
14022 breakpoint_re_set_one (b);
14023 }
14024 CATCH (ex, RETURN_MASK_ALL)
14025 {
14026 exception_fprintf (gdb_stderr, ex,
14027 "Error in re-setting breakpoint %d: ",
14028 b->number);
14029 }
14030 END_CATCH
5ed8105e 14031 }
5ed8105e
PA
14032
14033 jit_breakpoint_re_set ();
14034 }
6c95b8df 14035
af02033e
PP
14036 create_overlay_event_breakpoint ();
14037 create_longjmp_master_breakpoint ();
14038 create_std_terminate_master_breakpoint ();
186c406b 14039 create_exception_master_breakpoint ();
2a7f3dff
PA
14040
14041 /* Now we can insert. */
14042 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14043}
14044\f
c906108c
SS
14045/* Reset the thread number of this breakpoint:
14046
14047 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14048 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14049void
fba45db2 14050breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14051{
14052 if (b->thread != -1)
14053 {
39f77062 14054 if (in_thread_list (inferior_ptid))
5d5658a1 14055 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14056
14057 /* We're being called after following a fork. The new fork is
14058 selected as current, and unless this was a vfork will have a
14059 different program space from the original thread. Reset that
14060 as well. */
14061 b->loc->pspace = current_program_space;
c906108c
SS
14062 }
14063}
14064
03ac34d5
MS
14065/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14066 If from_tty is nonzero, it prints a message to that effect,
14067 which ends with a period (no newline). */
14068
c906108c 14069void
fba45db2 14070set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14071{
52f0bd74 14072 struct breakpoint *b;
c906108c
SS
14073
14074 if (count < 0)
14075 count = 0;
14076
14077 ALL_BREAKPOINTS (b)
14078 if (b->number == bptnum)
c5aa993b 14079 {
d77f58be
SS
14080 if (is_tracepoint (b))
14081 {
14082 if (from_tty && count != 0)
14083 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14084 bptnum);
14085 return;
14086 }
14087
c5aa993b 14088 b->ignore_count = count;
221ea385
KS
14089 if (from_tty)
14090 {
14091 if (count == 0)
3e43a32a
MS
14092 printf_filtered (_("Will stop next time "
14093 "breakpoint %d is reached."),
221ea385
KS
14094 bptnum);
14095 else if (count == 1)
a3f17187 14096 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14097 bptnum);
14098 else
3e43a32a
MS
14099 printf_filtered (_("Will ignore next %d "
14100 "crossings of breakpoint %d."),
221ea385
KS
14101 count, bptnum);
14102 }
8d3788bd 14103 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14104 return;
14105 }
c906108c 14106
8a3fe4f8 14107 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14108}
14109
c906108c
SS
14110/* Command to set ignore-count of breakpoint N to COUNT. */
14111
14112static void
fba45db2 14113ignore_command (char *args, int from_tty)
c906108c
SS
14114{
14115 char *p = args;
52f0bd74 14116 int num;
c906108c
SS
14117
14118 if (p == 0)
e2e0b3e5 14119 error_no_arg (_("a breakpoint number"));
c5aa993b 14120
c906108c 14121 num = get_number (&p);
5c44784c 14122 if (num == 0)
8a3fe4f8 14123 error (_("bad breakpoint number: '%s'"), args);
c906108c 14124 if (*p == 0)
8a3fe4f8 14125 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14126
14127 set_ignore_count (num,
14128 longest_to_int (value_as_long (parse_and_eval (p))),
14129 from_tty);
221ea385
KS
14130 if (from_tty)
14131 printf_filtered ("\n");
c906108c
SS
14132}
14133\f
14134/* Call FUNCTION on each of the breakpoints
14135 whose numbers are given in ARGS. */
14136
14137static void
896b6bda 14138map_breakpoint_numbers (const char *args,
48649e1b 14139 gdb::function_view<void (breakpoint *)> function)
c906108c 14140{
52f0bd74
AC
14141 int num;
14142 struct breakpoint *b, *tmp;
c906108c 14143
b9d61307 14144 if (args == 0 || *args == '\0')
e2e0b3e5 14145 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14146
bfd28288 14147 number_or_range_parser parser (args);
197f0a60 14148
bfd28288 14149 while (!parser.finished ())
c906108c 14150 {
bfd28288
PA
14151 const char *p = parser.cur_tok ();
14152 bool match = false;
197f0a60 14153
bfd28288 14154 num = parser.get_number ();
5c44784c 14155 if (num == 0)
c5aa993b 14156 {
8a3fe4f8 14157 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14158 }
14159 else
14160 {
14161 ALL_BREAKPOINTS_SAFE (b, tmp)
14162 if (b->number == num)
14163 {
bfd28288 14164 match = true;
48649e1b 14165 function (b);
11cf8741 14166 break;
5c44784c 14167 }
bfd28288 14168 if (!match)
a3f17187 14169 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14170 }
c906108c
SS
14171 }
14172}
14173
0d381245 14174static struct bp_location *
cb791d59 14175find_location_by_number (const char *number)
0d381245 14176{
cb791d59 14177 const char *p1;
0d381245
VP
14178 int bp_num;
14179 int loc_num;
14180 struct breakpoint *b;
14181 struct bp_location *loc;
14182
0d381245 14183 p1 = number;
cb791d59
TT
14184 bp_num = get_number_trailer (&p1, '.');
14185 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14186 error (_("Bad breakpoint number '%s'"), number);
14187
14188 ALL_BREAKPOINTS (b)
14189 if (b->number == bp_num)
14190 {
14191 break;
14192 }
14193
14194 if (!b || b->number != bp_num)
14195 error (_("Bad breakpoint number '%s'"), number);
14196
cb791d59
TT
14197 /* Skip the dot. */
14198 ++p1;
14199 const char *save = p1;
197f0a60 14200 loc_num = get_number (&p1);
0d381245
VP
14201 if (loc_num == 0)
14202 error (_("Bad breakpoint location number '%s'"), number);
14203
14204 --loc_num;
14205 loc = b->loc;
14206 for (;loc_num && loc; --loc_num, loc = loc->next)
14207 ;
14208 if (!loc)
cb791d59 14209 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14210
14211 return loc;
14212}
14213
14214
1900040c
MS
14215/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14216 If from_tty is nonzero, it prints a message to that effect,
14217 which ends with a period (no newline). */
14218
c906108c 14219void
fba45db2 14220disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14221{
14222 /* Never disable a watchpoint scope breakpoint; we want to
14223 hit them when we leave scope so we can delete both the
14224 watchpoint and its scope breakpoint at that time. */
14225 if (bpt->type == bp_watchpoint_scope)
14226 return;
14227
b5de0fa7 14228 bpt->enable_state = bp_disabled;
c906108c 14229
b775012e
LM
14230 /* Mark breakpoint locations modified. */
14231 mark_breakpoint_modified (bpt);
14232
d248b706
KY
14233 if (target_supports_enable_disable_tracepoint ()
14234 && current_trace_status ()->running && is_tracepoint (bpt))
14235 {
14236 struct bp_location *location;
14237
14238 for (location = bpt->loc; location; location = location->next)
14239 target_disable_tracepoint (location);
14240 }
14241
44702360 14242 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14243
8d3788bd 14244 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14245}
14246
c906108c 14247static void
981a3fb3 14248disable_command (const char *args, int from_tty)
c906108c 14249{
c906108c 14250 if (args == 0)
46c6471b
PA
14251 {
14252 struct breakpoint *bpt;
14253
14254 ALL_BREAKPOINTS (bpt)
14255 if (user_breakpoint_p (bpt))
14256 disable_breakpoint (bpt);
14257 }
9eaabc75 14258 else
0d381245 14259 {
cb791d59 14260 std::string num = extract_arg (&args);
9eaabc75 14261
cb791d59 14262 while (!num.empty ())
d248b706 14263 {
cb791d59 14264 if (num.find ('.') != std::string::npos)
b775012e 14265 {
cb791d59 14266 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14267
14268 if (loc)
14269 {
14270 if (loc->enabled)
14271 {
14272 loc->enabled = 0;
14273 mark_breakpoint_location_modified (loc);
14274 }
14275 if (target_supports_enable_disable_tracepoint ()
14276 && current_trace_status ()->running && loc->owner
14277 && is_tracepoint (loc->owner))
14278 target_disable_tracepoint (loc);
14279 }
44702360 14280 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14281 }
9eaabc75 14282 else
48649e1b
TT
14283 map_breakpoint_numbers
14284 (num.c_str (), [&] (breakpoint *b)
14285 {
14286 iterate_over_related_breakpoints (b, disable_breakpoint);
14287 });
9eaabc75 14288 num = extract_arg (&args);
d248b706 14289 }
0d381245 14290 }
c906108c
SS
14291}
14292
14293static void
816338b5
SS
14294enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14295 int count)
c906108c 14296{
afe38095 14297 int target_resources_ok;
c906108c
SS
14298
14299 if (bpt->type == bp_hardware_breakpoint)
14300 {
14301 int i;
c5aa993b 14302 i = hw_breakpoint_used_count ();
53a5351d 14303 target_resources_ok =
d92524f1 14304 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14305 i + 1, 0);
c906108c 14306 if (target_resources_ok == 0)
8a3fe4f8 14307 error (_("No hardware breakpoint support in the target."));
c906108c 14308 else if (target_resources_ok < 0)
8a3fe4f8 14309 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14310 }
14311
cc60f2e3 14312 if (is_watchpoint (bpt))
c906108c 14313 {
d07205c2 14314 /* Initialize it just to avoid a GCC false warning. */
f486487f 14315 enum enable_state orig_enable_state = bp_disabled;
dde02812 14316
492d29ea 14317 TRY
c906108c 14318 {
3a5c3e22
PA
14319 struct watchpoint *w = (struct watchpoint *) bpt;
14320
1e718ff1
TJB
14321 orig_enable_state = bpt->enable_state;
14322 bpt->enable_state = bp_enabled;
3a5c3e22 14323 update_watchpoint (w, 1 /* reparse */);
c906108c 14324 }
492d29ea 14325 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14326 {
1e718ff1 14327 bpt->enable_state = orig_enable_state;
dde02812
ES
14328 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14329 bpt->number);
14330 return;
c5aa993b 14331 }
492d29ea 14332 END_CATCH
c906108c 14333 }
0101ce28 14334
b775012e
LM
14335 bpt->enable_state = bp_enabled;
14336
14337 /* Mark breakpoint locations modified. */
14338 mark_breakpoint_modified (bpt);
14339
d248b706
KY
14340 if (target_supports_enable_disable_tracepoint ()
14341 && current_trace_status ()->running && is_tracepoint (bpt))
14342 {
14343 struct bp_location *location;
14344
14345 for (location = bpt->loc; location; location = location->next)
14346 target_enable_tracepoint (location);
14347 }
14348
b4c291bb 14349 bpt->disposition = disposition;
816338b5 14350 bpt->enable_count = count;
44702360 14351 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14352
8d3788bd 14353 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14354}
14355
fe3f5fa8 14356
c906108c 14357void
fba45db2 14358enable_breakpoint (struct breakpoint *bpt)
c906108c 14359{
816338b5 14360 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14361}
14362
c906108c
SS
14363/* The enable command enables the specified breakpoints (or all defined
14364 breakpoints) so they once again become (or continue to be) effective
1272ad14 14365 in stopping the inferior. */
c906108c 14366
c906108c 14367static void
981a3fb3 14368enable_command (const char *args, int from_tty)
c906108c 14369{
c906108c 14370 if (args == 0)
46c6471b
PA
14371 {
14372 struct breakpoint *bpt;
14373
14374 ALL_BREAKPOINTS (bpt)
14375 if (user_breakpoint_p (bpt))
14376 enable_breakpoint (bpt);
14377 }
9eaabc75 14378 else
0d381245 14379 {
cb791d59 14380 std::string num = extract_arg (&args);
9eaabc75 14381
cb791d59 14382 while (!num.empty ())
d248b706 14383 {
cb791d59 14384 if (num.find ('.') != std::string::npos)
b775012e 14385 {
cb791d59 14386 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14387
14388 if (loc)
14389 {
14390 if (!loc->enabled)
14391 {
14392 loc->enabled = 1;
14393 mark_breakpoint_location_modified (loc);
14394 }
14395 if (target_supports_enable_disable_tracepoint ()
14396 && current_trace_status ()->running && loc->owner
14397 && is_tracepoint (loc->owner))
14398 target_enable_tracepoint (loc);
14399 }
44702360 14400 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14401 }
9eaabc75 14402 else
48649e1b
TT
14403 map_breakpoint_numbers
14404 (num.c_str (), [&] (breakpoint *b)
14405 {
14406 iterate_over_related_breakpoints (b, enable_breakpoint);
14407 });
9eaabc75 14408 num = extract_arg (&args);
d248b706 14409 }
0d381245 14410 }
c906108c
SS
14411}
14412
c906108c 14413static void
4495129a 14414enable_once_command (const char *args, int from_tty)
c906108c 14415{
48649e1b
TT
14416 map_breakpoint_numbers
14417 (args, [&] (breakpoint *b)
14418 {
14419 iterate_over_related_breakpoints
14420 (b, [&] (breakpoint *bpt)
14421 {
14422 enable_breakpoint_disp (bpt, disp_disable, 1);
14423 });
14424 });
816338b5
SS
14425}
14426
14427static void
4495129a 14428enable_count_command (const char *args, int from_tty)
816338b5 14429{
b9d61307
SM
14430 int count;
14431
14432 if (args == NULL)
14433 error_no_arg (_("hit count"));
14434
14435 count = get_number (&args);
816338b5 14436
48649e1b
TT
14437 map_breakpoint_numbers
14438 (args, [&] (breakpoint *b)
14439 {
14440 iterate_over_related_breakpoints
14441 (b, [&] (breakpoint *bpt)
14442 {
14443 enable_breakpoint_disp (bpt, disp_disable, count);
14444 });
14445 });
c906108c
SS
14446}
14447
c906108c 14448static void
4495129a 14449enable_delete_command (const char *args, int from_tty)
c906108c 14450{
48649e1b
TT
14451 map_breakpoint_numbers
14452 (args, [&] (breakpoint *b)
14453 {
14454 iterate_over_related_breakpoints
14455 (b, [&] (breakpoint *bpt)
14456 {
14457 enable_breakpoint_disp (bpt, disp_del, 1);
14458 });
14459 });
c906108c
SS
14460}
14461\f
fa8d40ab 14462static void
981a3fb3 14463set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14464{
14465}
14466
14467static void
981a3fb3 14468show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14469{
14470}
14471
1f3b5d1b
PP
14472/* Invalidate last known value of any hardware watchpoint if
14473 the memory which that value represents has been written to by
14474 GDB itself. */
14475
14476static void
8de0566d
YQ
14477invalidate_bp_value_on_memory_change (struct inferior *inferior,
14478 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14479 const bfd_byte *data)
14480{
14481 struct breakpoint *bp;
14482
14483 ALL_BREAKPOINTS (bp)
14484 if (bp->enable_state == bp_enabled
3a5c3e22 14485 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14486 {
3a5c3e22 14487 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14488
3a5c3e22
PA
14489 if (wp->val_valid && wp->val)
14490 {
14491 struct bp_location *loc;
14492
14493 for (loc = bp->loc; loc != NULL; loc = loc->next)
14494 if (loc->loc_type == bp_loc_hardware_watchpoint
14495 && loc->address + loc->length > addr
14496 && addr + len > loc->address)
14497 {
14498 value_free (wp->val);
14499 wp->val = NULL;
14500 wp->val_valid = 0;
14501 }
14502 }
1f3b5d1b
PP
14503 }
14504}
14505
8181d85f
DJ
14506/* Create and insert a breakpoint for software single step. */
14507
14508void
6c95b8df 14509insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14510 const address_space *aspace,
4a64f543 14511 CORE_ADDR next_pc)
8181d85f 14512{
7c16b83e
PA
14513 struct thread_info *tp = inferior_thread ();
14514 struct symtab_and_line sal;
14515 CORE_ADDR pc = next_pc;
8181d85f 14516
34b7e8a6
PA
14517 if (tp->control.single_step_breakpoints == NULL)
14518 {
14519 tp->control.single_step_breakpoints
5d5658a1 14520 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14521 }
8181d85f 14522
7c16b83e
PA
14523 sal = find_pc_line (pc, 0);
14524 sal.pc = pc;
14525 sal.section = find_pc_overlay (pc);
14526 sal.explicit_pc = 1;
34b7e8a6 14527 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14528
7c16b83e 14529 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14530}
14531
93f9a11f
YQ
14532/* Insert single step breakpoints according to the current state. */
14533
14534int
14535insert_single_step_breakpoints (struct gdbarch *gdbarch)
14536{
f5ea389a 14537 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14538 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14539
f5ea389a 14540 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14541
a0ff9e1a 14542 if (!next_pcs.empty ())
93f9a11f 14543 {
f5ea389a 14544 struct frame_info *frame = get_current_frame ();
8b86c959 14545 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14546
a0ff9e1a 14547 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14548 insert_single_step_breakpoint (gdbarch, aspace, pc);
14549
93f9a11f
YQ
14550 return 1;
14551 }
14552 else
14553 return 0;
14554}
14555
34b7e8a6 14556/* See breakpoint.h. */
f02253f1
HZ
14557
14558int
7c16b83e 14559breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14560 const address_space *aspace,
7c16b83e 14561 CORE_ADDR pc)
1aafd4da 14562{
7c16b83e 14563 struct bp_location *loc;
1aafd4da 14564
7c16b83e
PA
14565 for (loc = bp->loc; loc != NULL; loc = loc->next)
14566 if (loc->inserted
14567 && breakpoint_location_address_match (loc, aspace, pc))
14568 return 1;
1aafd4da 14569
7c16b83e 14570 return 0;
ef370185
JB
14571}
14572
14573/* Check whether a software single-step breakpoint is inserted at
14574 PC. */
14575
14576int
accd0bcd 14577single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14578 CORE_ADDR pc)
14579{
34b7e8a6
PA
14580 struct breakpoint *bpt;
14581
14582 ALL_BREAKPOINTS (bpt)
14583 {
14584 if (bpt->type == bp_single_step
14585 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14586 return 1;
14587 }
14588 return 0;
1aafd4da
UW
14589}
14590
1042e4c0
SS
14591/* Tracepoint-specific operations. */
14592
14593/* Set tracepoint count to NUM. */
14594static void
14595set_tracepoint_count (int num)
14596{
14597 tracepoint_count = num;
4fa62494 14598 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14599}
14600
70221824 14601static void
f2fc3015 14602trace_command (char *arg_in, int from_tty)
1042e4c0 14603{
f2fc3015 14604 const char *arg = arg_in;
55aa24fb 14605 struct breakpoint_ops *ops;
55aa24fb 14606
ffc2605c
TT
14607 event_location_up location = string_to_event_location (&arg,
14608 current_language);
5b56227b 14609 if (location != NULL
ffc2605c 14610 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14611 ops = &tracepoint_probe_breakpoint_ops;
14612 else
14613 ops = &tracepoint_breakpoint_ops;
14614
558a9d82 14615 create_breakpoint (get_current_arch (),
ffc2605c 14616 location.get (),
f00aae0f 14617 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14618 0 /* tempflag */,
14619 bp_tracepoint /* type_wanted */,
14620 0 /* Ignore count */,
14621 pending_break_support,
14622 ops,
14623 from_tty,
14624 1 /* enabled */,
14625 0 /* internal */, 0);
1042e4c0
SS
14626}
14627
70221824 14628static void
f2fc3015 14629ftrace_command (char *arg_in, int from_tty)
7a697b8d 14630{
f2fc3015 14631 const char *arg = arg_in;
ffc2605c
TT
14632 event_location_up location = string_to_event_location (&arg,
14633 current_language);
558a9d82 14634 create_breakpoint (get_current_arch (),
ffc2605c 14635 location.get (),
f00aae0f 14636 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14637 0 /* tempflag */,
14638 bp_fast_tracepoint /* type_wanted */,
14639 0 /* Ignore count */,
14640 pending_break_support,
14641 &tracepoint_breakpoint_ops,
14642 from_tty,
14643 1 /* enabled */,
14644 0 /* internal */, 0);
0fb4aa4b
PA
14645}
14646
14647/* strace command implementation. Creates a static tracepoint. */
14648
70221824 14649static void
f2fc3015 14650strace_command (char *arg_in, int from_tty)
0fb4aa4b 14651{
f2fc3015 14652 const char *arg = arg_in;
983af33b 14653 struct breakpoint_ops *ops;
ffc2605c 14654 event_location_up location;
983af33b
SDJ
14655
14656 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14657 or with a normal static tracepoint. */
61012eef 14658 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14659 {
14660 ops = &strace_marker_breakpoint_ops;
14661 location = new_linespec_location (&arg);
14662 }
983af33b 14663 else
f00aae0f
KS
14664 {
14665 ops = &tracepoint_breakpoint_ops;
14666 location = string_to_event_location (&arg, current_language);
14667 }
983af33b 14668
558a9d82 14669 create_breakpoint (get_current_arch (),
ffc2605c 14670 location.get (),
f00aae0f 14671 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14672 0 /* tempflag */,
14673 bp_static_tracepoint /* type_wanted */,
14674 0 /* Ignore count */,
14675 pending_break_support,
14676 ops,
14677 from_tty,
14678 1 /* enabled */,
14679 0 /* internal */, 0);
7a697b8d
SS
14680}
14681
409873ef
SS
14682/* Set up a fake reader function that gets command lines from a linked
14683 list that was acquired during tracepoint uploading. */
14684
14685static struct uploaded_tp *this_utp;
3149d8c1 14686static int next_cmd;
409873ef
SS
14687
14688static char *
14689read_uploaded_action (void)
14690{
14691 char *rslt;
14692
3149d8c1 14693 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14694
3149d8c1 14695 next_cmd++;
409873ef
SS
14696
14697 return rslt;
14698}
14699
00bf0b85
SS
14700/* Given information about a tracepoint as recorded on a target (which
14701 can be either a live system or a trace file), attempt to create an
14702 equivalent GDB tracepoint. This is not a reliable process, since
14703 the target does not necessarily have all the information used when
14704 the tracepoint was originally defined. */
14705
d9b3f62e 14706struct tracepoint *
00bf0b85 14707create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14708{
f2fc3015
TT
14709 const char *addr_str;
14710 char small_buf[100];
d9b3f62e 14711 struct tracepoint *tp;
fd9b8c24 14712
409873ef
SS
14713 if (utp->at_string)
14714 addr_str = utp->at_string;
14715 else
14716 {
14717 /* In the absence of a source location, fall back to raw
14718 address. Since there is no way to confirm that the address
14719 means the same thing as when the trace was started, warn the
14720 user. */
3e43a32a
MS
14721 warning (_("Uploaded tracepoint %d has no "
14722 "source location, using raw address"),
409873ef 14723 utp->number);
8c042590 14724 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14725 addr_str = small_buf;
14726 }
14727
14728 /* There's not much we can do with a sequence of bytecodes. */
14729 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14730 warning (_("Uploaded tracepoint %d condition "
14731 "has no source form, ignoring it"),
409873ef 14732 utp->number);
d5551862 14733
ffc2605c
TT
14734 event_location_up location = string_to_event_location (&addr_str,
14735 current_language);
8cdf0e15 14736 if (!create_breakpoint (get_current_arch (),
ffc2605c 14737 location.get (),
f00aae0f 14738 utp->cond_string, -1, addr_str,
e7e0cddf 14739 0 /* parse cond/thread */,
8cdf0e15 14740 0 /* tempflag */,
0fb4aa4b 14741 utp->type /* type_wanted */,
8cdf0e15
VP
14742 0 /* Ignore count */,
14743 pending_break_support,
348d480f 14744 &tracepoint_breakpoint_ops,
8cdf0e15 14745 0 /* from_tty */,
84f4c1fe 14746 utp->enabled /* enabled */,
44f238bb
PA
14747 0 /* internal */,
14748 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14749 return NULL;
fd9b8c24 14750
409873ef 14751 /* Get the tracepoint we just created. */
fd9b8c24
PA
14752 tp = get_tracepoint (tracepoint_count);
14753 gdb_assert (tp != NULL);
d5551862 14754
00bf0b85
SS
14755 if (utp->pass > 0)
14756 {
8c042590 14757 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14758 tp->number);
00bf0b85 14759
409873ef 14760 trace_pass_command (small_buf, 0);
00bf0b85
SS
14761 }
14762
409873ef
SS
14763 /* If we have uploaded versions of the original commands, set up a
14764 special-purpose "reader" function and call the usual command line
14765 reader, then pass the result to the breakpoint command-setting
14766 function. */
3149d8c1 14767 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14768 {
93921405 14769 command_line_up cmd_list;
00bf0b85 14770
409873ef 14771 this_utp = utp;
3149d8c1 14772 next_cmd = 0;
d5551862 14773
409873ef
SS
14774 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14775
c1fc2657 14776 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14777 }
3149d8c1
SS
14778 else if (!VEC_empty (char_ptr, utp->actions)
14779 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14780 warning (_("Uploaded tracepoint %d actions "
14781 "have no source form, ignoring them"),
409873ef 14782 utp->number);
00bf0b85 14783
f196051f 14784 /* Copy any status information that might be available. */
c1fc2657 14785 tp->hit_count = utp->hit_count;
f196051f
SS
14786 tp->traceframe_usage = utp->traceframe_usage;
14787
00bf0b85 14788 return tp;
d9b3f62e 14789}
00bf0b85 14790
1042e4c0
SS
14791/* Print information on tracepoint number TPNUM_EXP, or all if
14792 omitted. */
14793
14794static void
11db9430 14795info_tracepoints_command (char *args, int from_tty)
1042e4c0 14796{
79a45e25 14797 struct ui_out *uiout = current_uiout;
e5a67952 14798 int num_printed;
1042e4c0 14799
e5a67952 14800 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14801
14802 if (num_printed == 0)
1042e4c0 14803 {
e5a67952 14804 if (args == NULL || *args == '\0')
112e8700 14805 uiout->message ("No tracepoints.\n");
d77f58be 14806 else
112e8700 14807 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14808 }
ad443146
SS
14809
14810 default_collect_info ();
1042e4c0
SS
14811}
14812
4a64f543 14813/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14814 Not supported by all targets. */
14815static void
14816enable_trace_command (char *args, int from_tty)
14817{
14818 enable_command (args, from_tty);
14819}
14820
4a64f543 14821/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14822 Not supported by all targets. */
14823static void
14824disable_trace_command (char *args, int from_tty)
14825{
14826 disable_command (args, from_tty);
14827}
14828
4a64f543 14829/* Remove a tracepoint (or all if no argument). */
1042e4c0 14830static void
4495129a 14831delete_trace_command (const char *arg, int from_tty)
1042e4c0 14832{
35df4500 14833 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14834
14835 dont_repeat ();
14836
14837 if (arg == 0)
14838 {
14839 int breaks_to_delete = 0;
14840
14841 /* Delete all breakpoints if no argument.
14842 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14843 have to be deleted with an explicit breakpoint number
14844 argument. */
1042e4c0 14845 ALL_TRACEPOINTS (b)
46c6471b 14846 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14847 {
14848 breaks_to_delete = 1;
14849 break;
14850 }
1042e4c0
SS
14851
14852 /* Ask user only if there are some breakpoints to delete. */
14853 if (!from_tty
14854 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14855 {
35df4500 14856 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14857 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14858 delete_breakpoint (b);
1042e4c0
SS
14859 }
14860 }
14861 else
48649e1b
TT
14862 map_breakpoint_numbers
14863 (arg, [&] (breakpoint *b)
14864 {
14865 iterate_over_related_breakpoints (b, delete_breakpoint);
14866 });
1042e4c0
SS
14867}
14868
197f0a60
TT
14869/* Helper function for trace_pass_command. */
14870
14871static void
d9b3f62e 14872trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14873{
d9b3f62e 14874 tp->pass_count = count;
c1fc2657 14875 observer_notify_breakpoint_modified (tp);
197f0a60
TT
14876 if (from_tty)
14877 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14878 tp->number, count);
197f0a60
TT
14879}
14880
1042e4c0
SS
14881/* Set passcount for tracepoint.
14882
14883 First command argument is passcount, second is tracepoint number.
14884 If tracepoint number omitted, apply to most recently defined.
14885 Also accepts special argument "all". */
14886
14887static void
14888trace_pass_command (char *args, int from_tty)
14889{
d9b3f62e 14890 struct tracepoint *t1;
1042e4c0 14891 unsigned int count;
1042e4c0
SS
14892
14893 if (args == 0 || *args == 0)
3e43a32a
MS
14894 error (_("passcount command requires an "
14895 "argument (count + optional TP num)"));
1042e4c0 14896
4a64f543 14897 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14898
529480d0 14899 args = skip_spaces (args);
1042e4c0
SS
14900 if (*args && strncasecmp (args, "all", 3) == 0)
14901 {
d9b3f62e
PA
14902 struct breakpoint *b;
14903
1042e4c0 14904 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14905 if (*args)
14906 error (_("Junk at end of arguments."));
1042e4c0 14907
d9b3f62e 14908 ALL_TRACEPOINTS (b)
197f0a60 14909 {
d9b3f62e 14910 t1 = (struct tracepoint *) b;
197f0a60
TT
14911 trace_pass_set_count (t1, count, from_tty);
14912 }
14913 }
14914 else if (*args == '\0')
1042e4c0 14915 {
5fa1d40e 14916 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14917 if (t1)
197f0a60
TT
14918 trace_pass_set_count (t1, count, from_tty);
14919 }
14920 else
14921 {
bfd28288
PA
14922 number_or_range_parser parser (args);
14923 while (!parser.finished ())
1042e4c0 14924 {
bfd28288 14925 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14926 if (t1)
14927 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14928 }
14929 }
1042e4c0
SS
14930}
14931
d9b3f62e 14932struct tracepoint *
1042e4c0
SS
14933get_tracepoint (int num)
14934{
14935 struct breakpoint *t;
14936
14937 ALL_TRACEPOINTS (t)
14938 if (t->number == num)
d9b3f62e 14939 return (struct tracepoint *) t;
1042e4c0
SS
14940
14941 return NULL;
14942}
14943
d5551862
SS
14944/* Find the tracepoint with the given target-side number (which may be
14945 different from the tracepoint number after disconnecting and
14946 reconnecting). */
14947
d9b3f62e 14948struct tracepoint *
d5551862
SS
14949get_tracepoint_by_number_on_target (int num)
14950{
d9b3f62e 14951 struct breakpoint *b;
d5551862 14952
d9b3f62e
PA
14953 ALL_TRACEPOINTS (b)
14954 {
14955 struct tracepoint *t = (struct tracepoint *) b;
14956
14957 if (t->number_on_target == num)
14958 return t;
14959 }
d5551862
SS
14960
14961 return NULL;
14962}
14963
1042e4c0 14964/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14965 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14966 If the argument is missing, the most recent tracepoint
14967 (tracepoint_count) is returned. */
14968
d9b3f62e 14969struct tracepoint *
197f0a60 14970get_tracepoint_by_number (char **arg,
bfd28288 14971 number_or_range_parser *parser)
1042e4c0 14972{
1042e4c0
SS
14973 struct breakpoint *t;
14974 int tpnum;
14975 char *instring = arg == NULL ? NULL : *arg;
14976
bfd28288 14977 if (parser != NULL)
197f0a60 14978 {
bfd28288
PA
14979 gdb_assert (!parser->finished ());
14980 tpnum = parser->get_number ();
197f0a60
TT
14981 }
14982 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14983 tpnum = tracepoint_count;
1042e4c0 14984 else
197f0a60 14985 tpnum = get_number (arg);
1042e4c0
SS
14986
14987 if (tpnum <= 0)
14988 {
14989 if (instring && *instring)
14990 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
14991 instring);
14992 else
5fa1d40e 14993 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
14994 return NULL;
14995 }
14996
14997 ALL_TRACEPOINTS (t)
14998 if (t->number == tpnum)
14999 {
d9b3f62e 15000 return (struct tracepoint *) t;
1042e4c0
SS
15001 }
15002
1042e4c0
SS
15003 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15004 return NULL;
15005}
15006
d9b3f62e
PA
15007void
15008print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15009{
15010 if (b->thread != -1)
15011 fprintf_unfiltered (fp, " thread %d", b->thread);
15012
15013 if (b->task != 0)
15014 fprintf_unfiltered (fp, " task %d", b->task);
15015
15016 fprintf_unfiltered (fp, "\n");
15017}
15018
6149aea9
PA
15019/* Save information on user settable breakpoints (watchpoints, etc) to
15020 a new script file named FILENAME. If FILTER is non-NULL, call it
15021 on each breakpoint and only include the ones for which it returns
15022 non-zero. */
15023
1042e4c0 15024static void
4495129a 15025save_breakpoints (const char *filename, int from_tty,
6149aea9 15026 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15027{
15028 struct breakpoint *tp;
6149aea9 15029 int any = 0;
6149aea9 15030 int extra_trace_bits = 0;
1042e4c0 15031
6149aea9
PA
15032 if (filename == 0 || *filename == 0)
15033 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15034
15035 /* See if we have anything to save. */
6149aea9 15036 ALL_BREAKPOINTS (tp)
1042e4c0 15037 {
6149aea9 15038 /* Skip internal and momentary breakpoints. */
09d682a4 15039 if (!user_breakpoint_p (tp))
6149aea9
PA
15040 continue;
15041
15042 /* If we have a filter, only save the breakpoints it accepts. */
15043 if (filter && !filter (tp))
15044 continue;
15045
15046 any = 1;
15047
15048 if (is_tracepoint (tp))
15049 {
15050 extra_trace_bits = 1;
15051
15052 /* We can stop searching. */
15053 break;
15054 }
1042e4c0 15055 }
6149aea9
PA
15056
15057 if (!any)
1042e4c0 15058 {
6149aea9 15059 warning (_("Nothing to save."));
1042e4c0
SS
15060 return;
15061 }
15062
ee0c3293 15063 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15064
15065 stdio_file fp;
15066
ee0c3293 15067 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15068 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15069 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15070
6149aea9 15071 if (extra_trace_bits)
d7e74731 15072 save_trace_state_variables (&fp);
8bf6485c 15073
6149aea9 15074 ALL_BREAKPOINTS (tp)
1042e4c0 15075 {
6149aea9 15076 /* Skip internal and momentary breakpoints. */
09d682a4 15077 if (!user_breakpoint_p (tp))
6149aea9 15078 continue;
8bf6485c 15079
6149aea9
PA
15080 /* If we have a filter, only save the breakpoints it accepts. */
15081 if (filter && !filter (tp))
15082 continue;
15083
d7e74731 15084 tp->ops->print_recreate (tp, &fp);
1042e4c0 15085
6149aea9
PA
15086 /* Note, we can't rely on tp->number for anything, as we can't
15087 assume the recreated breakpoint numbers will match. Use $bpnum
15088 instead. */
15089
15090 if (tp->cond_string)
d7e74731 15091 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15092
15093 if (tp->ignore_count)
d7e74731 15094 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15095
2d9442cc 15096 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15097 {
d7e74731 15098 fp.puts (" commands\n");
a7bdde9e 15099
d7e74731 15100 current_uiout->redirect (&fp);
492d29ea 15101 TRY
1042e4c0 15102 {
d1b0a7bf 15103 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15104 }
492d29ea
PA
15105 CATCH (ex, RETURN_MASK_ALL)
15106 {
112e8700 15107 current_uiout->redirect (NULL);
492d29ea
PA
15108 throw_exception (ex);
15109 }
15110 END_CATCH
1042e4c0 15111
112e8700 15112 current_uiout->redirect (NULL);
d7e74731 15113 fp.puts (" end\n");
1042e4c0 15114 }
6149aea9
PA
15115
15116 if (tp->enable_state == bp_disabled)
d7e74731 15117 fp.puts ("disable $bpnum\n");
6149aea9
PA
15118
15119 /* If this is a multi-location breakpoint, check if the locations
15120 should be individually disabled. Watchpoint locations are
15121 special, and not user visible. */
15122 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15123 {
15124 struct bp_location *loc;
15125 int n = 1;
15126
15127 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15128 if (!loc->enabled)
d7e74731 15129 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15130 }
1042e4c0 15131 }
8bf6485c 15132
6149aea9 15133 if (extra_trace_bits && *default_collect)
d7e74731 15134 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15135
1042e4c0 15136 if (from_tty)
ee0c3293 15137 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15138}
15139
15140/* The `save breakpoints' command. */
15141
15142static void
4495129a 15143save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15144{
15145 save_breakpoints (args, from_tty, NULL);
15146}
15147
15148/* The `save tracepoints' command. */
15149
15150static void
4495129a 15151save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15152{
15153 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15154}
15155
15156/* Create a vector of all tracepoints. */
15157
15158VEC(breakpoint_p) *
eeae04df 15159all_tracepoints (void)
1042e4c0
SS
15160{
15161 VEC(breakpoint_p) *tp_vec = 0;
15162 struct breakpoint *tp;
15163
15164 ALL_TRACEPOINTS (tp)
15165 {
15166 VEC_safe_push (breakpoint_p, tp_vec, tp);
15167 }
15168
15169 return tp_vec;
15170}
15171
c906108c 15172\f
629500fa
KS
15173/* This help string is used to consolidate all the help string for specifying
15174 locations used by several commands. */
15175
15176#define LOCATION_HELP_STRING \
15177"Linespecs are colon-separated lists of location parameters, such as\n\
15178source filename, function name, label name, and line number.\n\
15179Example: To specify the start of a label named \"the_top\" in the\n\
15180function \"fact\" in the file \"factorial.c\", use\n\
15181\"factorial.c:fact:the_top\".\n\
15182\n\
15183Address locations begin with \"*\" and specify an exact address in the\n\
15184program. Example: To specify the fourth byte past the start function\n\
15185\"main\", use \"*main + 4\".\n\
15186\n\
15187Explicit locations are similar to linespecs but use an option/argument\n\
15188syntax to specify location parameters.\n\
15189Example: To specify the start of the label named \"the_top\" in the\n\
15190function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15191-function fact -label the_top\".\n"
15192
4a64f543
MS
15193/* This help string is used for the break, hbreak, tbreak and thbreak
15194 commands. It is defined as a macro to prevent duplication.
15195 COMMAND should be a string constant containing the name of the
15196 command. */
629500fa 15197
31e2b00f 15198#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15199command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15200PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15201probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15202guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15203`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15204LOCATION may be a linespec, address, or explicit location as described\n\
15205below.\n\
15206\n\
dc10affe
PA
15207With no LOCATION, uses current execution address of the selected\n\
15208stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15209\n\
15210THREADNUM is the number from \"info threads\".\n\
15211CONDITION is a boolean expression.\n\
629500fa 15212\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15213Multiple breakpoints at one place are permitted, and useful if their\n\
15214conditions are different.\n\
31e2b00f
AS
15215\n\
15216Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15217
44feb3ce
TT
15218/* List of subcommands for "catch". */
15219static struct cmd_list_element *catch_cmdlist;
15220
15221/* List of subcommands for "tcatch". */
15222static struct cmd_list_element *tcatch_cmdlist;
15223
9ac4176b 15224void
a121b7c1 15225add_catch_command (const char *name, const char *docstring,
82ae6c8d 15226 cmd_sfunc_ftype *sfunc,
625e8578 15227 completer_ftype *completer,
44feb3ce
TT
15228 void *user_data_catch,
15229 void *user_data_tcatch)
15230{
15231 struct cmd_list_element *command;
15232
0450cc4c 15233 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15234 &catch_cmdlist);
15235 set_cmd_sfunc (command, sfunc);
15236 set_cmd_context (command, user_data_catch);
a96d9b2e 15237 set_cmd_completer (command, completer);
44feb3ce 15238
0450cc4c 15239 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15240 &tcatch_cmdlist);
15241 set_cmd_sfunc (command, sfunc);
15242 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15243 set_cmd_completer (command, completer);
44feb3ce
TT
15244}
15245
6149aea9 15246static void
981a3fb3 15247save_command (const char *arg, int from_tty)
6149aea9 15248{
3e43a32a
MS
15249 printf_unfiltered (_("\"save\" must be followed by "
15250 "the name of a save subcommand.\n"));
635c7e8a 15251 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15252}
15253
84f4c1fe
PM
15254struct breakpoint *
15255iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15256 void *data)
15257{
35df4500 15258 struct breakpoint *b, *b_tmp;
84f4c1fe 15259
35df4500 15260 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15261 {
15262 if ((*callback) (b, data))
15263 return b;
15264 }
15265
15266 return NULL;
15267}
15268
0574c78f
GB
15269/* Zero if any of the breakpoint's locations could be a location where
15270 functions have been inlined, nonzero otherwise. */
15271
15272static int
15273is_non_inline_function (struct breakpoint *b)
15274{
15275 /* The shared library event breakpoint is set on the address of a
15276 non-inline function. */
15277 if (b->type == bp_shlib_event)
15278 return 1;
15279
15280 return 0;
15281}
15282
15283/* Nonzero if the specified PC cannot be a location where functions
15284 have been inlined. */
15285
15286int
accd0bcd 15287pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15288 const struct target_waitstatus *ws)
0574c78f
GB
15289{
15290 struct breakpoint *b;
15291 struct bp_location *bl;
15292
15293 ALL_BREAKPOINTS (b)
15294 {
15295 if (!is_non_inline_function (b))
15296 continue;
15297
15298 for (bl = b->loc; bl != NULL; bl = bl->next)
15299 {
15300 if (!bl->shlib_disabled
09ac7c10 15301 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15302 return 1;
15303 }
15304 }
15305
15306 return 0;
15307}
15308
2f202fde
JK
15309/* Remove any references to OBJFILE which is going to be freed. */
15310
15311void
15312breakpoint_free_objfile (struct objfile *objfile)
15313{
15314 struct bp_location **locp, *loc;
15315
15316 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15317 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15318 loc->symtab = NULL;
15319}
15320
2060206e
PA
15321void
15322initialize_breakpoint_ops (void)
15323{
15324 static int initialized = 0;
15325
15326 struct breakpoint_ops *ops;
15327
15328 if (initialized)
15329 return;
15330 initialized = 1;
15331
15332 /* The breakpoint_ops structure to be inherit by all kinds of
15333 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15334 internal and momentary breakpoints, etc.). */
15335 ops = &bkpt_base_breakpoint_ops;
15336 *ops = base_breakpoint_ops;
15337 ops->re_set = bkpt_re_set;
15338 ops->insert_location = bkpt_insert_location;
15339 ops->remove_location = bkpt_remove_location;
15340 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15341 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15342 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15343 ops->decode_location = bkpt_decode_location;
2060206e
PA
15344
15345 /* The breakpoint_ops structure to be used in regular breakpoints. */
15346 ops = &bkpt_breakpoint_ops;
15347 *ops = bkpt_base_breakpoint_ops;
15348 ops->re_set = bkpt_re_set;
15349 ops->resources_needed = bkpt_resources_needed;
15350 ops->print_it = bkpt_print_it;
15351 ops->print_mention = bkpt_print_mention;
15352 ops->print_recreate = bkpt_print_recreate;
15353
15354 /* Ranged breakpoints. */
15355 ops = &ranged_breakpoint_ops;
15356 *ops = bkpt_breakpoint_ops;
15357 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15358 ops->resources_needed = resources_needed_ranged_breakpoint;
15359 ops->print_it = print_it_ranged_breakpoint;
15360 ops->print_one = print_one_ranged_breakpoint;
15361 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15362 ops->print_mention = print_mention_ranged_breakpoint;
15363 ops->print_recreate = print_recreate_ranged_breakpoint;
15364
15365 /* Internal breakpoints. */
15366 ops = &internal_breakpoint_ops;
15367 *ops = bkpt_base_breakpoint_ops;
15368 ops->re_set = internal_bkpt_re_set;
15369 ops->check_status = internal_bkpt_check_status;
15370 ops->print_it = internal_bkpt_print_it;
15371 ops->print_mention = internal_bkpt_print_mention;
15372
15373 /* Momentary breakpoints. */
15374 ops = &momentary_breakpoint_ops;
15375 *ops = bkpt_base_breakpoint_ops;
15376 ops->re_set = momentary_bkpt_re_set;
15377 ops->check_status = momentary_bkpt_check_status;
15378 ops->print_it = momentary_bkpt_print_it;
15379 ops->print_mention = momentary_bkpt_print_mention;
15380
55aa24fb
SDJ
15381 /* Probe breakpoints. */
15382 ops = &bkpt_probe_breakpoint_ops;
15383 *ops = bkpt_breakpoint_ops;
15384 ops->insert_location = bkpt_probe_insert_location;
15385 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15386 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15387 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15388
2060206e
PA
15389 /* Watchpoints. */
15390 ops = &watchpoint_breakpoint_ops;
15391 *ops = base_breakpoint_ops;
15392 ops->re_set = re_set_watchpoint;
15393 ops->insert_location = insert_watchpoint;
15394 ops->remove_location = remove_watchpoint;
15395 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15396 ops->check_status = check_status_watchpoint;
15397 ops->resources_needed = resources_needed_watchpoint;
15398 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15399 ops->print_it = print_it_watchpoint;
15400 ops->print_mention = print_mention_watchpoint;
15401 ops->print_recreate = print_recreate_watchpoint;
427cd150 15402 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15403
15404 /* Masked watchpoints. */
15405 ops = &masked_watchpoint_breakpoint_ops;
15406 *ops = watchpoint_breakpoint_ops;
15407 ops->insert_location = insert_masked_watchpoint;
15408 ops->remove_location = remove_masked_watchpoint;
15409 ops->resources_needed = resources_needed_masked_watchpoint;
15410 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15411 ops->print_it = print_it_masked_watchpoint;
15412 ops->print_one_detail = print_one_detail_masked_watchpoint;
15413 ops->print_mention = print_mention_masked_watchpoint;
15414 ops->print_recreate = print_recreate_masked_watchpoint;
15415
15416 /* Tracepoints. */
15417 ops = &tracepoint_breakpoint_ops;
15418 *ops = base_breakpoint_ops;
15419 ops->re_set = tracepoint_re_set;
15420 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15421 ops->print_one_detail = tracepoint_print_one_detail;
15422 ops->print_mention = tracepoint_print_mention;
15423 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15424 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15425 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15426 ops->decode_location = tracepoint_decode_location;
983af33b 15427
55aa24fb
SDJ
15428 /* Probe tracepoints. */
15429 ops = &tracepoint_probe_breakpoint_ops;
15430 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15431 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15432 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15433
983af33b
SDJ
15434 /* Static tracepoints with marker (`-m'). */
15435 ops = &strace_marker_breakpoint_ops;
15436 *ops = tracepoint_breakpoint_ops;
5f700d83 15437 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15438 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15439 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15440
15441 /* Fork catchpoints. */
15442 ops = &catch_fork_breakpoint_ops;
15443 *ops = base_breakpoint_ops;
15444 ops->insert_location = insert_catch_fork;
15445 ops->remove_location = remove_catch_fork;
15446 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15447 ops->print_it = print_it_catch_fork;
15448 ops->print_one = print_one_catch_fork;
15449 ops->print_mention = print_mention_catch_fork;
15450 ops->print_recreate = print_recreate_catch_fork;
15451
15452 /* Vfork catchpoints. */
15453 ops = &catch_vfork_breakpoint_ops;
15454 *ops = base_breakpoint_ops;
15455 ops->insert_location = insert_catch_vfork;
15456 ops->remove_location = remove_catch_vfork;
15457 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15458 ops->print_it = print_it_catch_vfork;
15459 ops->print_one = print_one_catch_vfork;
15460 ops->print_mention = print_mention_catch_vfork;
15461 ops->print_recreate = print_recreate_catch_vfork;
15462
15463 /* Exec catchpoints. */
15464 ops = &catch_exec_breakpoint_ops;
15465 *ops = base_breakpoint_ops;
2060206e
PA
15466 ops->insert_location = insert_catch_exec;
15467 ops->remove_location = remove_catch_exec;
15468 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15469 ops->print_it = print_it_catch_exec;
15470 ops->print_one = print_one_catch_exec;
15471 ops->print_mention = print_mention_catch_exec;
15472 ops->print_recreate = print_recreate_catch_exec;
15473
edcc5120
TT
15474 /* Solib-related catchpoints. */
15475 ops = &catch_solib_breakpoint_ops;
15476 *ops = base_breakpoint_ops;
edcc5120
TT
15477 ops->insert_location = insert_catch_solib;
15478 ops->remove_location = remove_catch_solib;
15479 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15480 ops->check_status = check_status_catch_solib;
15481 ops->print_it = print_it_catch_solib;
15482 ops->print_one = print_one_catch_solib;
15483 ops->print_mention = print_mention_catch_solib;
15484 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15485
15486 ops = &dprintf_breakpoint_ops;
15487 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15488 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15489 ops->resources_needed = bkpt_resources_needed;
15490 ops->print_it = bkpt_print_it;
15491 ops->print_mention = bkpt_print_mention;
2d9442cc 15492 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15493 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15494 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15495}
15496
8bfd80db
YQ
15497/* Chain containing all defined "enable breakpoint" subcommands. */
15498
15499static struct cmd_list_element *enablebreaklist = NULL;
15500
c906108c 15501void
fba45db2 15502_initialize_breakpoint (void)
c906108c
SS
15503{
15504 struct cmd_list_element *c;
15505
2060206e
PA
15506 initialize_breakpoint_ops ();
15507
84acb35a 15508 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15509 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15510 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15511
55aa24fb 15512 breakpoint_objfile_key
43dce439 15513 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
17450429 15514
c906108c
SS
15515 breakpoint_chain = 0;
15516 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15517 before a breakpoint is set. */
15518 breakpoint_count = 0;
15519
1042e4c0
SS
15520 tracepoint_count = 0;
15521
1bedd215
AC
15522 add_com ("ignore", class_breakpoint, ignore_command, _("\
15523Set ignore-count of breakpoint number N to COUNT.\n\
15524Usage is `ignore N COUNT'."));
c906108c 15525
1bedd215 15526 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15527Set commands to be executed when the given breakpoints are hit.\n\
15528Give a space-separated breakpoint list as argument after \"commands\".\n\
15529A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15530(e.g. `5-7').\n\
c906108c
SS
15531With no argument, the targeted breakpoint is the last one set.\n\
15532The commands themselves follow starting on the next line.\n\
15533Type a line containing \"end\" to indicate the end of them.\n\
15534Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15535then no output is printed when it is hit, except what the commands print."));
c906108c 15536
d55637df 15537 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15538Specify breakpoint number N to break only if COND is true.\n\
c906108c 15539Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15540expression to be evaluated whenever breakpoint N is reached."));
d55637df 15541 set_cmd_completer (c, condition_completer);
c906108c 15542
1bedd215 15543 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15544Set a temporary breakpoint.\n\
c906108c
SS
15545Like \"break\" except the breakpoint is only temporary,\n\
15546so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15547by using \"enable delete\" on the breakpoint number.\n\
15548\n"
15549BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15550 set_cmd_completer (c, location_completer);
c94fdfd0 15551
1bedd215 15552 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15553Set a hardware assisted breakpoint.\n\
c906108c 15554Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15555some target hardware may not have this support.\n\
15556\n"
15557BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15558 set_cmd_completer (c, location_completer);
c906108c 15559
1bedd215 15560 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15561Set a temporary hardware assisted breakpoint.\n\
c906108c 15562Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15563so it will be deleted when hit.\n\
15564\n"
15565BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15566 set_cmd_completer (c, location_completer);
c906108c 15567
1bedd215
AC
15568 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15569Enable some breakpoints.\n\
c906108c
SS
15570Give breakpoint numbers (separated by spaces) as arguments.\n\
15571With no subcommand, breakpoints are enabled until you command otherwise.\n\
15572This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15573With a subcommand you can enable temporarily."),
c906108c 15574 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15575
15576 add_com_alias ("en", "enable", class_breakpoint, 1);
15577
84951ab5 15578 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15579Enable some breakpoints.\n\
c906108c
SS
15580Give breakpoint numbers (separated by spaces) as arguments.\n\
15581This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15582May be abbreviated to simply \"enable\".\n"),
c5aa993b 15583 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15584
1a966eab
AC
15585 add_cmd ("once", no_class, enable_once_command, _("\
15586Enable breakpoints for one hit. Give breakpoint numbers.\n\
15587If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15588 &enablebreaklist);
15589
1a966eab
AC
15590 add_cmd ("delete", no_class, enable_delete_command, _("\
15591Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15592If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15593 &enablebreaklist);
15594
816338b5
SS
15595 add_cmd ("count", no_class, enable_count_command, _("\
15596Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15597If a breakpoint is hit while enabled in this fashion,\n\
15598the count is decremented; when it reaches zero, the breakpoint is disabled."),
15599 &enablebreaklist);
15600
1a966eab
AC
15601 add_cmd ("delete", no_class, enable_delete_command, _("\
15602Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15603If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15604 &enablelist);
15605
1a966eab
AC
15606 add_cmd ("once", no_class, enable_once_command, _("\
15607Enable breakpoints for one hit. Give breakpoint numbers.\n\
15608If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15609 &enablelist);
15610
15611 add_cmd ("count", no_class, enable_count_command, _("\
15612Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15613If a breakpoint is hit while enabled in this fashion,\n\
15614the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15615 &enablelist);
15616
1bedd215
AC
15617 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15618Disable some breakpoints.\n\
c906108c
SS
15619Arguments are breakpoint numbers with spaces in between.\n\
15620To disable all breakpoints, give no argument.\n\
64b9b334 15621A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15622 &disablelist, "disable ", 1, &cmdlist);
15623 add_com_alias ("dis", "disable", class_breakpoint, 1);
15624 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15625
1a966eab
AC
15626 add_cmd ("breakpoints", class_alias, disable_command, _("\
15627Disable some breakpoints.\n\
c906108c
SS
15628Arguments are breakpoint numbers with spaces in between.\n\
15629To disable all breakpoints, give no argument.\n\
64b9b334 15630A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15631This command may be abbreviated \"disable\"."),
c906108c
SS
15632 &disablelist);
15633
1bedd215
AC
15634 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15635Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15636Arguments are breakpoint numbers with spaces in between.\n\
15637To delete all breakpoints, give no argument.\n\
15638\n\
15639Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15640The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15641 &deletelist, "delete ", 1, &cmdlist);
15642 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15643 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15644
1a966eab
AC
15645 add_cmd ("breakpoints", class_alias, delete_command, _("\
15646Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15647Arguments are breakpoint numbers with spaces in between.\n\
15648To delete all breakpoints, give no argument.\n\
1a966eab 15649This command may be abbreviated \"delete\"."),
c906108c
SS
15650 &deletelist);
15651
1bedd215 15652 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15653Clear breakpoint at specified location.\n\
15654Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15655\n\
15656With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15657is executing in.\n"
15658"\n" LOCATION_HELP_STRING "\n\
1bedd215 15659See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15660 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15661
1bedd215 15662 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15663Set breakpoint at specified location.\n"
31e2b00f 15664BREAK_ARGS_HELP ("break")));
5ba2abeb 15665 set_cmd_completer (c, location_completer);
c94fdfd0 15666
c906108c
SS
15667 add_com_alias ("b", "break", class_run, 1);
15668 add_com_alias ("br", "break", class_run, 1);
15669 add_com_alias ("bre", "break", class_run, 1);
15670 add_com_alias ("brea", "break", class_run, 1);
15671
c906108c
SS
15672 if (dbx_commands)
15673 {
1bedd215
AC
15674 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15675Break in function/address or break at a line in the current file."),
c5aa993b
JM
15676 &stoplist, "stop ", 1, &cmdlist);
15677 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15678 _("Break in function or address."), &stoplist);
c5aa993b 15679 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15680 _("Break at a line in the current file."), &stoplist);
11db9430 15681 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15682Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15683The \"Type\" column indicates one of:\n\
15684\tbreakpoint - normal breakpoint\n\
15685\twatchpoint - watchpoint\n\
15686The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15687the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15688breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15689address and file/line number respectively.\n\
15690\n\
15691Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15692are set to the address of the last breakpoint listed unless the command\n\
15693is prefixed with \"server \".\n\n\
c906108c 15694Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15695breakpoint set."));
c906108c
SS
15696 }
15697
11db9430 15698 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15699Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15700The \"Type\" column indicates one of:\n\
15701\tbreakpoint - normal breakpoint\n\
15702\twatchpoint - watchpoint\n\
15703The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15704the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15705breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15706address and file/line number respectively.\n\
15707\n\
15708Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15709are set to the address of the last breakpoint listed unless the command\n\
15710is prefixed with \"server \".\n\n\
c906108c 15711Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15712breakpoint set."));
c906108c 15713
6b04bdb7
MS
15714 add_info_alias ("b", "breakpoints", 1);
15715
1a966eab
AC
15716 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15717Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15718The \"Type\" column indicates one of:\n\
15719\tbreakpoint - normal breakpoint\n\
15720\twatchpoint - watchpoint\n\
15721\tlongjmp - internal breakpoint used to step through longjmp()\n\
15722\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15723\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15724\tfinish - internal breakpoint used by the \"finish\" command\n\
15725The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15726the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15727breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15728address and file/line number respectively.\n\
15729\n\
15730Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15731are set to the address of the last breakpoint listed unless the command\n\
15732is prefixed with \"server \".\n\n\
c906108c 15733Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15734breakpoint set."),
c906108c
SS
15735 &maintenanceinfolist);
15736
44feb3ce
TT
15737 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15738Set catchpoints to catch events."),
15739 &catch_cmdlist, "catch ",
15740 0/*allow-unknown*/, &cmdlist);
15741
15742 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15743Set temporary catchpoints to catch events."),
15744 &tcatch_cmdlist, "tcatch ",
15745 0/*allow-unknown*/, &cmdlist);
15746
44feb3ce
TT
15747 add_catch_command ("fork", _("Catch calls to fork."),
15748 catch_fork_command_1,
a96d9b2e 15749 NULL,
44feb3ce
TT
15750 (void *) (uintptr_t) catch_fork_permanent,
15751 (void *) (uintptr_t) catch_fork_temporary);
15752 add_catch_command ("vfork", _("Catch calls to vfork."),
15753 catch_fork_command_1,
a96d9b2e 15754 NULL,
44feb3ce
TT
15755 (void *) (uintptr_t) catch_vfork_permanent,
15756 (void *) (uintptr_t) catch_vfork_temporary);
15757 add_catch_command ("exec", _("Catch calls to exec."),
15758 catch_exec_command_1,
a96d9b2e
SDJ
15759 NULL,
15760 CATCH_PERMANENT,
15761 CATCH_TEMPORARY);
edcc5120
TT
15762 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15763Usage: catch load [REGEX]\n\
15764If REGEX is given, only stop for libraries matching the regular expression."),
15765 catch_load_command_1,
15766 NULL,
15767 CATCH_PERMANENT,
15768 CATCH_TEMPORARY);
15769 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15770Usage: catch unload [REGEX]\n\
15771If REGEX is given, only stop for libraries matching the regular expression."),
15772 catch_unload_command_1,
15773 NULL,
15774 CATCH_PERMANENT,
15775 CATCH_TEMPORARY);
c5aa993b 15776
1bedd215
AC
15777 c = add_com ("watch", class_breakpoint, watch_command, _("\
15778Set a watchpoint for an expression.\n\
06a64a0b 15779Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15780A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15781an expression changes.\n\
15782If -l or -location is given, this evaluates EXPRESSION and watches\n\
15783the memory to which it refers."));
65d12d83 15784 set_cmd_completer (c, expression_completer);
c906108c 15785
1bedd215
AC
15786 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15787Set a read watchpoint for an expression.\n\
06a64a0b 15788Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15789A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15790an expression is read.\n\
15791If -l or -location is given, this evaluates EXPRESSION and watches\n\
15792the memory to which it refers."));
65d12d83 15793 set_cmd_completer (c, expression_completer);
c906108c 15794
1bedd215
AC
15795 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15796Set a watchpoint for an expression.\n\
06a64a0b 15797Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15798A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15799an expression is either read or written.\n\
15800If -l or -location is given, this evaluates EXPRESSION and watches\n\
15801the memory to which it refers."));
65d12d83 15802 set_cmd_completer (c, expression_completer);
c906108c 15803
11db9430 15804 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15805Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15806
920d2a44
AC
15807 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15808 respond to changes - contrary to the description. */
85c07804
AC
15809 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15810 &can_use_hw_watchpoints, _("\
15811Set debugger's willingness to use watchpoint hardware."), _("\
15812Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15813If zero, gdb will not use hardware for new watchpoints, even if\n\
15814such is available. (However, any hardware watchpoints that were\n\
15815created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15816hardware.)"),
15817 NULL,
920d2a44 15818 show_can_use_hw_watchpoints,
85c07804 15819 &setlist, &showlist);
c906108c
SS
15820
15821 can_use_hw_watchpoints = 1;
fa8d40ab 15822
1042e4c0
SS
15823 /* Tracepoint manipulation commands. */
15824
15825 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15826Set a tracepoint at specified location.\n\
1042e4c0
SS
15827\n"
15828BREAK_ARGS_HELP ("trace") "\n\
15829Do \"help tracepoints\" for info on other tracepoint commands."));
15830 set_cmd_completer (c, location_completer);
15831
15832 add_com_alias ("tp", "trace", class_alias, 0);
15833 add_com_alias ("tr", "trace", class_alias, 1);
15834 add_com_alias ("tra", "trace", class_alias, 1);
15835 add_com_alias ("trac", "trace", class_alias, 1);
15836
7a697b8d 15837 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15838Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15839\n"
15840BREAK_ARGS_HELP ("ftrace") "\n\
15841Do \"help tracepoints\" for info on other tracepoint commands."));
15842 set_cmd_completer (c, location_completer);
15843
0fb4aa4b 15844 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15845Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15846\n\
15847strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15848LOCATION may be a linespec, explicit, or address location (described below) \n\
15849or -m MARKER_ID.\n\n\
15850If a marker id is specified, probe the marker with that name. With\n\
15851no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15852Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15853This collects arbitrary user data passed in the probe point call to the\n\
15854tracing library. You can inspect it when analyzing the trace buffer,\n\
15855by printing the $_sdata variable like any other convenience variable.\n\
15856\n\
15857CONDITION is a boolean expression.\n\
629500fa 15858\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15859Multiple tracepoints at one place are permitted, and useful if their\n\
15860conditions are different.\n\
0fb4aa4b
PA
15861\n\
15862Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15863Do \"help tracepoints\" for info on other tracepoint commands."));
15864 set_cmd_completer (c, location_completer);
15865
11db9430 15866 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15867Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15868Convenience variable \"$tpnum\" contains the number of the\n\
15869last tracepoint set."));
15870
15871 add_info_alias ("tp", "tracepoints", 1);
15872
15873 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15874Delete specified tracepoints.\n\
15875Arguments are tracepoint numbers, separated by spaces.\n\
15876No argument means delete all tracepoints."),
15877 &deletelist);
7e20dfcd 15878 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15879
15880 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15881Disable specified tracepoints.\n\
15882Arguments are tracepoint numbers, separated by spaces.\n\
15883No argument means disable all tracepoints."),
15884 &disablelist);
15885 deprecate_cmd (c, "disable");
15886
15887 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15888Enable specified tracepoints.\n\
15889Arguments are tracepoint numbers, separated by spaces.\n\
15890No argument means enable all tracepoints."),
15891 &enablelist);
15892 deprecate_cmd (c, "enable");
15893
15894 add_com ("passcount", class_trace, trace_pass_command, _("\
15895Set the passcount for a tracepoint.\n\
15896The trace will end when the tracepoint has been passed 'count' times.\n\
15897Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15898if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15899
6149aea9
PA
15900 add_prefix_cmd ("save", class_breakpoint, save_command,
15901 _("Save breakpoint definitions as a script."),
15902 &save_cmdlist, "save ",
15903 0/*allow-unknown*/, &cmdlist);
15904
15905 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15906Save current breakpoint definitions as a script.\n\
cce7e648 15907This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15908catchpoints, tracepoints). Use the 'source' command in another debug\n\
15909session to restore them."),
15910 &save_cmdlist);
15911 set_cmd_completer (c, filename_completer);
15912
15913 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15914Save current tracepoint definitions as a script.\n\
6149aea9
PA
15915Use the 'source' command in another debug session to restore them."),
15916 &save_cmdlist);
1042e4c0
SS
15917 set_cmd_completer (c, filename_completer);
15918
6149aea9
PA
15919 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15920 deprecate_cmd (c, "save tracepoints");
15921
1bedd215 15922 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15923Breakpoint specific settings\n\
15924Configure various breakpoint-specific variables such as\n\
1bedd215 15925pending breakpoint behavior"),
fa8d40ab
JJ
15926 &breakpoint_set_cmdlist, "set breakpoint ",
15927 0/*allow-unknown*/, &setlist);
1bedd215 15928 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15929Breakpoint specific settings\n\
15930Configure various breakpoint-specific variables such as\n\
1bedd215 15931pending breakpoint behavior"),
fa8d40ab
JJ
15932 &breakpoint_show_cmdlist, "show breakpoint ",
15933 0/*allow-unknown*/, &showlist);
15934
7915a72c
AC
15935 add_setshow_auto_boolean_cmd ("pending", no_class,
15936 &pending_break_support, _("\
15937Set debugger's behavior regarding pending breakpoints."), _("\
15938Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15939If on, an unrecognized breakpoint location will cause gdb to create a\n\
15940pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15941an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15942user-query to see if a pending breakpoint should be created."),
2c5b56ce 15943 NULL,
920d2a44 15944 show_pending_break_support,
6e1d7d6c
AC
15945 &breakpoint_set_cmdlist,
15946 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15947
15948 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15949
15950 add_setshow_boolean_cmd ("auto-hw", no_class,
15951 &automatic_hardware_breakpoints, _("\
15952Set automatic usage of hardware breakpoints."), _("\
15953Show automatic usage of hardware breakpoints."), _("\
15954If set, the debugger will automatically use hardware breakpoints for\n\
15955breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15956a warning will be emitted for such breakpoints."),
15957 NULL,
15958 show_automatic_hardware_breakpoints,
15959 &breakpoint_set_cmdlist,
15960 &breakpoint_show_cmdlist);
74960c60 15961
a25a5a45
PA
15962 add_setshow_boolean_cmd ("always-inserted", class_support,
15963 &always_inserted_mode, _("\
74960c60
VP
15964Set mode for inserting breakpoints."), _("\
15965Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15966When this mode is on, breakpoints are inserted immediately as soon as\n\
15967they're created, kept inserted even when execution stops, and removed\n\
15968only when the user deletes them. When this mode is off (the default),\n\
15969breakpoints are inserted only when execution continues, and removed\n\
15970when execution stops."),
72d0e2c5
YQ
15971 NULL,
15972 &show_always_inserted_mode,
15973 &breakpoint_set_cmdlist,
15974 &breakpoint_show_cmdlist);
f1310107 15975
b775012e
LM
15976 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15977 condition_evaluation_enums,
15978 &condition_evaluation_mode_1, _("\
15979Set mode of breakpoint condition evaluation."), _("\
15980Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15981When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15982evaluated on the host's side by GDB. When it is set to \"target\",\n\
15983breakpoint conditions will be downloaded to the target (if the target\n\
15984supports such feature) and conditions will be evaluated on the target's side.\n\
15985If this is set to \"auto\" (default), this will be automatically set to\n\
15986\"target\" if it supports condition evaluation, otherwise it will\n\
15987be set to \"gdb\""),
15988 &set_condition_evaluation_mode,
15989 &show_condition_evaluation_mode,
15990 &breakpoint_set_cmdlist,
15991 &breakpoint_show_cmdlist);
15992
f1310107
TJB
15993 add_com ("break-range", class_breakpoint, break_range_command, _("\
15994Set a breakpoint for an address range.\n\
15995break-range START-LOCATION, END-LOCATION\n\
15996where START-LOCATION and END-LOCATION can be one of the following:\n\
15997 LINENUM, for that line in the current file,\n\
15998 FILE:LINENUM, for that line in that file,\n\
15999 +OFFSET, for that number of lines after the current line\n\
16000 or the start of the range\n\
16001 FUNCTION, for the first line in that function,\n\
16002 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16003 *ADDRESS, for the instruction at that address.\n\
16004\n\
16005The breakpoint will stop execution of the inferior whenever it executes\n\
16006an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16007range (including START-LOCATION and END-LOCATION)."));
16008
e7e0cddf 16009 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16010Set a dynamic printf at specified location.\n\
e7e0cddf 16011dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16012location may be a linespec, explicit, or address location.\n"
16013"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16014 set_cmd_completer (c, location_completer);
16015
16016 add_setshow_enum_cmd ("dprintf-style", class_support,
16017 dprintf_style_enums, &dprintf_style, _("\
16018Set the style of usage for dynamic printf."), _("\
16019Show the style of usage for dynamic printf."), _("\
16020This setting chooses how GDB will do a dynamic printf.\n\
16021If the value is \"gdb\", then the printing is done by GDB to its own\n\
16022console, as with the \"printf\" command.\n\
16023If the value is \"call\", the print is done by calling a function in your\n\
16024program; by default printf(), but you can choose a different function or\n\
16025output stream by setting dprintf-function and dprintf-channel."),
16026 update_dprintf_commands, NULL,
16027 &setlist, &showlist);
16028
16029 dprintf_function = xstrdup ("printf");
16030 add_setshow_string_cmd ("dprintf-function", class_support,
16031 &dprintf_function, _("\
16032Set the function to use for dynamic printf"), _("\
16033Show the function to use for dynamic printf"), NULL,
16034 update_dprintf_commands, NULL,
16035 &setlist, &showlist);
16036
16037 dprintf_channel = xstrdup ("");
16038 add_setshow_string_cmd ("dprintf-channel", class_support,
16039 &dprintf_channel, _("\
16040Set the channel to use for dynamic printf"), _("\
16041Show the channel to use for dynamic printf"), NULL,
16042 update_dprintf_commands, NULL,
16043 &setlist, &showlist);
16044
d3ce09f5
SS
16045 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16046 &disconnected_dprintf, _("\
16047Set whether dprintf continues after GDB disconnects."), _("\
16048Show whether dprintf continues after GDB disconnects."), _("\
16049Use this to let dprintf commands continue to hit and produce output\n\
16050even if GDB disconnects or detaches from the target."),
16051 NULL,
16052 NULL,
16053 &setlist, &showlist);
16054
16055 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16056agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16057(target agent only) This is useful for formatted output in user-defined commands."));
16058
765dc015 16059 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16060
16061 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16062 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16063}