Use unique_xmalloc_ptr in c_type_print_base
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
8b93c638 47#include "ui-out.h"
e1507482 48#include "cli/cli-script.h"
fe898f56 49#include "block.h"
a77053c2 50#include "solib.h"
84acb35a
JJ
51#include "solist.h"
52#include "observer.h"
765dc015 53#include "memattr.h"
f7f9143b 54#include "ada-lang.h"
d1aa2f50 55#include "top.h"
79a45b7d 56#include "valprint.h"
4efc6507 57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
55aa24fb
SDJ
59#include "gdb_regex.h"
60#include "probe.h"
e9cafbcc 61#include "cli/cli-utils.h"
be34f849 62#include "continuations.h"
1bfeeb0f
JL
63#include "stack.h"
64#include "skip.h"
b775012e 65#include "ax-gdb.h"
e2e4d78b 66#include "dummy-frame.h"
5589af0e 67#include "interps.h"
d3ce09f5 68#include "format.h"
cfc31633 69#include "thread-fsm.h"
5d5658a1 70#include "tid-parse.h"
d3ce09f5 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
325fac50 81#include <algorithm>
5ed8105e 82#include "progspace-and-thread.h"
6c5b2ebe 83#include "common/array-view.h"
76f9c9cf 84#include "common/gdb_optional.h"
104c1213 85
e7e8980f
YQ
86/* Enums for exception-handling support. */
87enum exception_event_kind
88{
89 EX_EVENT_THROW,
591f19e8 90 EX_EVENT_RETHROW,
e7e8980f
YQ
91 EX_EVENT_CATCH
92};
93
4a64f543 94/* Prototypes for local functions. */
c906108c 95
896b6bda 96static void map_breakpoint_numbers (const char *,
48649e1b 97 gdb::function_view<void (breakpoint *)>);
c906108c 98
a14ed312 99static void ignore_command (char *, int);
c906108c 100
348d480f
PA
101static void breakpoint_re_set_default (struct breakpoint *);
102
f00aae0f
KS
103static void
104 create_sals_from_location_default (const struct event_location *location,
105 struct linespec_result *canonical,
106 enum bptype type_wanted);
983af33b
SDJ
107
108static void create_breakpoints_sal_default (struct gdbarch *,
109 struct linespec_result *,
e1e01040
PA
110 gdb::unique_xmalloc_ptr<char>,
111 gdb::unique_xmalloc_ptr<char>,
112 enum bptype,
983af33b
SDJ
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
44f238bb 116 int, int, int, unsigned);
983af33b 117
6c5b2ebe
PA
118static std::vector<symtab_and_line> decode_location_default
119 (struct breakpoint *b, const struct event_location *location,
120 struct program_space *search_pspace);
983af33b 121
a14ed312 122static void clear_command (char *, int);
c906108c 123
a9634178 124static int can_use_hardware_watchpoint (struct value *);
c906108c 125
a14ed312 126static void mention (struct breakpoint *);
c906108c 127
348d480f
PA
128static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
129 enum bptype,
c0a91b2b 130 const struct breakpoint_ops *);
3742cc8b
YQ
131static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
132 const struct symtab_and_line *);
133
4a64f543
MS
134/* This function is used in gdbtk sources and thus can not be made
135 static. */
63c252f8 136struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 137 struct symtab_and_line,
c0a91b2b
TT
138 enum bptype,
139 const struct breakpoint_ops *);
c906108c 140
06edf0c0
PA
141static struct breakpoint *
142 momentary_breakpoint_from_master (struct breakpoint *orig,
143 enum bptype type,
a1aa2221
LM
144 const struct breakpoint_ops *ops,
145 int loc_enabled);
06edf0c0 146
76897487
KB
147static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
148
a6d9a66e
UW
149static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
150 CORE_ADDR bpaddr,
88f7da05 151 enum bptype bptype);
76897487 152
6c95b8df
PA
153static void describe_other_breakpoints (struct gdbarch *,
154 struct program_space *, CORE_ADDR,
5af949e3 155 struct obj_section *, int);
c906108c 156
85d721b8
PA
157static int watchpoint_locations_match (struct bp_location *loc1,
158 struct bp_location *loc2);
159
f1310107 160static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 161 const struct address_space *aspace,
f1310107
TJB
162 CORE_ADDR addr);
163
d35ae833 164static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 165 const address_space *,
d35ae833
PA
166 CORE_ADDR, int);
167
11db9430 168static void info_breakpoints_command (char *, int);
c906108c 169
11db9430 170static void info_watchpoints_command (char *, int);
d77f58be 171
a14ed312 172static void commands_command (char *, int);
c906108c 173
a14ed312 174static void condition_command (char *, int);
c906108c 175
834c0d03 176static int remove_breakpoint (struct bp_location *);
b2b6a7da 177static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 178
e514a9d6 179static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 180
a14ed312 181static int hw_breakpoint_used_count (void);
c906108c 182
a1398e0c
PA
183static int hw_watchpoint_use_count (struct breakpoint *);
184
185static int hw_watchpoint_used_count_others (struct breakpoint *except,
186 enum bptype type,
187 int *other_type_used);
c906108c 188
a14ed312 189static void hbreak_command (char *, int);
c906108c 190
a14ed312 191static void thbreak_command (char *, int);
c906108c 192
816338b5
SS
193static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
194 int count);
c906108c 195
a14ed312 196static void stop_command (char *arg, int from_tty);
7a292a7a 197
fe3f5fa8 198static void free_bp_location (struct bp_location *loc);
f431efe5
PA
199static void incref_bp_location (struct bp_location *loc);
200static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 201
39d61571 202static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 203
44702360
PA
204/* update_global_location_list's modes of operation wrt to whether to
205 insert locations now. */
206enum ugll_insert_mode
207{
208 /* Don't insert any breakpoint locations into the inferior, only
209 remove already-inserted locations that no longer should be
210 inserted. Functions that delete a breakpoint or breakpoints
211 should specify this mode, so that deleting a breakpoint doesn't
212 have the side effect of inserting the locations of other
213 breakpoints that are marked not-inserted, but should_be_inserted
214 returns true on them.
215
216 This behavior is useful is situations close to tear-down -- e.g.,
217 after an exec, while the target still has execution, but
218 breakpoint shadows of the previous executable image should *NOT*
219 be restored to the new image; or before detaching, where the
220 target still has execution and wants to delete breakpoints from
221 GDB's lists, and all breakpoints had already been removed from
222 the inferior. */
223 UGLL_DONT_INSERT,
224
a25a5a45
PA
225 /* May insert breakpoints iff breakpoints_should_be_inserted_now
226 claims breakpoints should be inserted now. */
04086b45
PA
227 UGLL_MAY_INSERT,
228
a25a5a45
PA
229 /* Insert locations now, irrespective of
230 breakpoints_should_be_inserted_now. E.g., say all threads are
231 stopped right now, and the user did "continue". We need to
232 insert breakpoints _before_ resuming the target, but
233 UGLL_MAY_INSERT wouldn't insert them, because
234 breakpoints_should_be_inserted_now returns false at that point,
235 as no thread is running yet. */
04086b45 236 UGLL_INSERT
44702360
PA
237};
238
239static void update_global_location_list (enum ugll_insert_mode);
a5606eee 240
44702360 241static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 242
d77f58be 243static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
244
245static void insert_breakpoint_locations (void);
a5606eee 246
11db9430 247static void info_tracepoints_command (char *, int);
1042e4c0 248
1042e4c0
SS
249static void enable_trace_command (char *, int);
250
251static void disable_trace_command (char *, int);
252
253static void trace_pass_command (char *, int);
254
558a9d82
YQ
255static void set_tracepoint_count (int num);
256
9c06b0b4
TJB
257static int is_masked_watchpoint (const struct breakpoint *b);
258
b775012e
LM
259static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
260
983af33b
SDJ
261/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
262 otherwise. */
263
264static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 265
2060206e
PA
266/* The breakpoint_ops structure to be inherited by all breakpoint_ops
267 that are implemented on top of software or hardware breakpoints
268 (user breakpoints, internal and momentary breakpoints, etc.). */
269static struct breakpoint_ops bkpt_base_breakpoint_ops;
270
271/* Internal breakpoints class type. */
06edf0c0 272static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
273
274/* Momentary breakpoints class type. */
06edf0c0
PA
275static struct breakpoint_ops momentary_breakpoint_ops;
276
2060206e
PA
277/* The breakpoint_ops structure to be used in regular user created
278 breakpoints. */
279struct breakpoint_ops bkpt_breakpoint_ops;
280
55aa24fb
SDJ
281/* Breakpoints set on probes. */
282static struct breakpoint_ops bkpt_probe_breakpoint_ops;
283
e7e0cddf 284/* Dynamic printf class type. */
c5867ab6 285struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 286
d3ce09f5
SS
287/* The style in which to perform a dynamic printf. This is a user
288 option because different output options have different tradeoffs;
289 if GDB does the printing, there is better error handling if there
290 is a problem with any of the arguments, but using an inferior
291 function lets you have special-purpose printers and sending of
292 output to the same place as compiled-in print functions. */
293
294static const char dprintf_style_gdb[] = "gdb";
295static const char dprintf_style_call[] = "call";
296static const char dprintf_style_agent[] = "agent";
297static const char *const dprintf_style_enums[] = {
298 dprintf_style_gdb,
299 dprintf_style_call,
300 dprintf_style_agent,
301 NULL
302};
303static const char *dprintf_style = dprintf_style_gdb;
304
305/* The function to use for dynamic printf if the preferred style is to
306 call into the inferior. The value is simply a string that is
307 copied into the command, so it can be anything that GDB can
308 evaluate to a callable address, not necessarily a function name. */
309
bde6261a 310static char *dprintf_function;
d3ce09f5
SS
311
312/* The channel to use for dynamic printf if the preferred style is to
313 call into the inferior; if a nonempty string, it will be passed to
314 the call as the first argument, with the format string as the
315 second. As with the dprintf function, this can be anything that
316 GDB knows how to evaluate, so in addition to common choices like
317 "stderr", this could be an app-specific expression like
318 "mystreams[curlogger]". */
319
bde6261a 320static char *dprintf_channel;
d3ce09f5
SS
321
322/* True if dprintf commands should continue to operate even if GDB
323 has disconnected. */
324static int disconnected_dprintf = 1;
325
5cea2a26
PA
326struct command_line *
327breakpoint_commands (struct breakpoint *b)
328{
d1b0a7bf 329 return b->commands ? b->commands.get () : NULL;
5cea2a26 330}
3daf8fe5 331
f3b1572e
PA
332/* Flag indicating that a command has proceeded the inferior past the
333 current breakpoint. */
334
335static int breakpoint_proceeded;
336
956a9fb9 337const char *
2cec12e5
AR
338bpdisp_text (enum bpdisp disp)
339{
4a64f543
MS
340 /* NOTE: the following values are a part of MI protocol and
341 represent values of 'disp' field returned when inferior stops at
342 a breakpoint. */
bc043ef3 343 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 344
2cec12e5
AR
345 return bpdisps[(int) disp];
346}
c906108c 347
4a64f543 348/* Prototypes for exported functions. */
c906108c 349/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 350 if such is available. */
c906108c
SS
351static int can_use_hw_watchpoints;
352
920d2a44
AC
353static void
354show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357{
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Debugger's willingness to use "
360 "watchpoint hardware is %s.\n"),
920d2a44
AC
361 value);
362}
363
fa8d40ab
JJ
364/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
365 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 366 for unrecognized breakpoint locations.
fa8d40ab
JJ
367 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
368static enum auto_boolean pending_break_support;
920d2a44
AC
369static void
370show_pending_break_support (struct ui_file *file, int from_tty,
371 struct cmd_list_element *c,
372 const char *value)
373{
3e43a32a
MS
374 fprintf_filtered (file,
375 _("Debugger's behavior regarding "
376 "pending breakpoints is %s.\n"),
920d2a44
AC
377 value);
378}
fa8d40ab 379
765dc015 380/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 381 set with "break" but falling in read-only memory.
765dc015
VP
382 If 0, gdb will warn about such breakpoints, but won't automatically
383 use hardware breakpoints. */
384static int automatic_hardware_breakpoints;
385static void
386show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
387 struct cmd_list_element *c,
388 const char *value)
389{
3e43a32a
MS
390 fprintf_filtered (file,
391 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
392 value);
393}
394
a25a5a45
PA
395/* If on, GDB keeps breakpoints inserted even if the inferior is
396 stopped, and immediately inserts any new breakpoints as soon as
397 they're created. If off (default), GDB keeps breakpoints off of
398 the target as long as possible. That is, it delays inserting
399 breakpoints until the next resume, and removes them again when the
400 target fully stops. This is a bit safer in case GDB crashes while
401 processing user input. */
402static int always_inserted_mode = 0;
72d0e2c5 403
33e5cbd6 404static void
74960c60 405show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 406 struct cmd_list_element *c, const char *value)
74960c60 407{
a25a5a45
PA
408 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
409 value);
74960c60
VP
410}
411
b57bacec
PA
412/* See breakpoint.h. */
413
33e5cbd6 414int
a25a5a45 415breakpoints_should_be_inserted_now (void)
33e5cbd6 416{
a25a5a45
PA
417 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
418 {
419 /* If breakpoints are global, they should be inserted even if no
420 thread under gdb's control is running, or even if there are
421 no threads under GDB's control yet. */
422 return 1;
423 }
424 else if (target_has_execution)
425 {
372316f1
PA
426 struct thread_info *tp;
427
a25a5a45
PA
428 if (always_inserted_mode)
429 {
430 /* The user wants breakpoints inserted even if all threads
431 are stopped. */
432 return 1;
433 }
434
b57bacec
PA
435 if (threads_are_executing ())
436 return 1;
372316f1
PA
437
438 /* Don't remove breakpoints yet if, even though all threads are
439 stopped, we still have events to process. */
440 ALL_NON_EXITED_THREADS (tp)
441 if (tp->resumed
442 && tp->suspend.waitstatus_pending_p)
443 return 1;
a25a5a45
PA
444 }
445 return 0;
33e5cbd6 446}
765dc015 447
b775012e
LM
448static const char condition_evaluation_both[] = "host or target";
449
450/* Modes for breakpoint condition evaluation. */
451static const char condition_evaluation_auto[] = "auto";
452static const char condition_evaluation_host[] = "host";
453static const char condition_evaluation_target[] = "target";
454static const char *const condition_evaluation_enums[] = {
455 condition_evaluation_auto,
456 condition_evaluation_host,
457 condition_evaluation_target,
458 NULL
459};
460
461/* Global that holds the current mode for breakpoint condition evaluation. */
462static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
463
464/* Global that we use to display information to the user (gets its value from
465 condition_evaluation_mode_1. */
466static const char *condition_evaluation_mode = condition_evaluation_auto;
467
468/* Translate a condition evaluation mode MODE into either "host"
469 or "target". This is used mostly to translate from "auto" to the
470 real setting that is being used. It returns the translated
471 evaluation mode. */
472
473static const char *
474translate_condition_evaluation_mode (const char *mode)
475{
476 if (mode == condition_evaluation_auto)
477 {
478 if (target_supports_evaluation_of_breakpoint_conditions ())
479 return condition_evaluation_target;
480 else
481 return condition_evaluation_host;
482 }
483 else
484 return mode;
485}
486
487/* Discovers what condition_evaluation_auto translates to. */
488
489static const char *
490breakpoint_condition_evaluation_mode (void)
491{
492 return translate_condition_evaluation_mode (condition_evaluation_mode);
493}
494
495/* Return true if GDB should evaluate breakpoint conditions or false
496 otherwise. */
497
498static int
499gdb_evaluates_breakpoint_condition_p (void)
500{
501 const char *mode = breakpoint_condition_evaluation_mode ();
502
503 return (mode == condition_evaluation_host);
504}
505
c906108c
SS
506/* Are we executing breakpoint commands? */
507static int executing_breakpoint_commands;
508
c02f5703
MS
509/* Are overlay event breakpoints enabled? */
510static int overlay_events_enabled;
511
e09342b5
TJB
512/* See description in breakpoint.h. */
513int target_exact_watchpoints = 0;
514
c906108c 515/* Walk the following statement or block through all breakpoints.
e5dd4106 516 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 517 current breakpoint. */
c906108c 518
5c44784c 519#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 520
5c44784c
JM
521#define ALL_BREAKPOINTS_SAFE(B,TMP) \
522 for (B = breakpoint_chain; \
523 B ? (TMP=B->next, 1): 0; \
524 B = TMP)
c906108c 525
4a64f543
MS
526/* Similar iterator for the low-level breakpoints. SAFE variant is
527 not provided so update_global_location_list must not be called
528 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 529
876fa593 530#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
531 for (BP_TMP = bp_locations; \
532 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 533 BP_TMP++)
7cc221ef 534
b775012e
LM
535/* Iterates through locations with address ADDRESS for the currently selected
536 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
537 to where the loop should start from.
538 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
539 appropriate location to start with. */
540
541#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
542 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
543 BP_LOCP_TMP = BP_LOCP_START; \
544 BP_LOCP_START \
f5336ca5 545 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
546 && (*BP_LOCP_TMP)->address == ADDRESS); \
547 BP_LOCP_TMP++)
548
1042e4c0
SS
549/* Iterator for tracepoints only. */
550
551#define ALL_TRACEPOINTS(B) \
552 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 553 if (is_tracepoint (B))
1042e4c0 554
7cc221ef 555/* Chains of all breakpoints defined. */
c906108c
SS
556
557struct breakpoint *breakpoint_chain;
558
f5336ca5 559/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 560
f5336ca5 561static struct bp_location **bp_locations;
876fa593 562
f5336ca5 563/* Number of elements of BP_LOCATIONS. */
876fa593 564
f5336ca5 565static unsigned bp_locations_count;
876fa593 566
4a64f543 567/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 568 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 569 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 570 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 571 an address you need to read. */
876fa593 572
f5336ca5 573static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 574
4a64f543
MS
575/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
576 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
577 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
578 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 579 scan for shadow bytes for an address you need to read. */
876fa593 580
f5336ca5 581static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 582
4a64f543 583/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
584 from the bp_locations array, but for which a hit may still be
585 reported by a target. */
20874c92
VP
586VEC(bp_location_p) *moribund_locations = NULL;
587
c906108c
SS
588/* Number of last breakpoint made. */
589
95a42b64
TT
590static int breakpoint_count;
591
86b17b60
PA
592/* The value of `breakpoint_count' before the last command that
593 created breakpoints. If the last (break-like) command created more
594 than one breakpoint, then the difference between BREAKPOINT_COUNT
595 and PREV_BREAKPOINT_COUNT is more than one. */
596static int prev_breakpoint_count;
c906108c 597
1042e4c0
SS
598/* Number of last tracepoint made. */
599
95a42b64 600static int tracepoint_count;
1042e4c0 601
6149aea9
PA
602static struct cmd_list_element *breakpoint_set_cmdlist;
603static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 604struct cmd_list_element *save_cmdlist;
6149aea9 605
badd37ce
SDJ
606/* See declaration at breakpoint.h. */
607
608struct breakpoint *
609breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
610 void *user_data)
611{
612 struct breakpoint *b = NULL;
613
614 ALL_BREAKPOINTS (b)
615 {
616 if (func (b, user_data) != 0)
617 break;
618 }
619
620 return b;
621}
622
468d015d
JJ
623/* Return whether a breakpoint is an active enabled breakpoint. */
624static int
625breakpoint_enabled (struct breakpoint *b)
626{
0d381245 627 return (b->enable_state == bp_enabled);
468d015d
JJ
628}
629
c906108c
SS
630/* Set breakpoint count to NUM. */
631
95a42b64 632static void
fba45db2 633set_breakpoint_count (int num)
c906108c 634{
86b17b60 635 prev_breakpoint_count = breakpoint_count;
c906108c 636 breakpoint_count = num;
4fa62494 637 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
638}
639
86b17b60
PA
640/* Used by `start_rbreak_breakpoints' below, to record the current
641 breakpoint count before "rbreak" creates any breakpoint. */
642static int rbreak_start_breakpoint_count;
643
95a42b64
TT
644/* Called at the start an "rbreak" command to record the first
645 breakpoint made. */
86b17b60 646
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
SS
8584
8585struct breakpoint *
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
c906108c
SS
8608 return b;
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
611c83ae 8661struct breakpoint *
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,
cfc31633
PA
11149 struct breakpoint *location_breakpoint,
11150 struct breakpoint *caller_breakpoint)
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;
11158 sm->location_breakpoint = location_breakpoint;
11159 sm->caller_breakpoint = caller_breakpoint;
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;
cfc31633
PA
11222 struct breakpoint *location_breakpoint;
11223 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11224 struct cleanup *old_chain;
186c406b
TT
11225 int thread;
11226 struct thread_info *tp;
cfc31633 11227 struct until_break_fsm *sm;
c906108c 11228
70509625 11229 clear_proceed_status (0);
c906108c
SS
11230
11231 /* Set a breakpoint where the user wants it and at return from
4a64f543 11232 this function. */
c5aa993b 11233
ffc2605c 11234 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11235
6c5b2ebe
PA
11236 std::vector<symtab_and_line> sals
11237 = (last_displayed_sal_is_valid ()
11238 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11239 get_last_displayed_symtab (),
11240 get_last_displayed_line ())
11241 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11242 NULL, (struct symtab *) NULL, 0));
c5aa993b 11243
6c5b2ebe 11244 if (sals.size () != 1)
8a3fe4f8 11245 error (_("Couldn't get information on specified line."));
c5aa993b 11246
6c5b2ebe 11247 symtab_and_line &sal = sals[0];
c5aa993b 11248
c906108c 11249 if (*arg)
8a3fe4f8 11250 error (_("Junk at end of arguments."));
c5aa993b 11251
c906108c 11252 resolve_sal_pc (&sal);
c5aa993b 11253
186c406b 11254 tp = inferior_thread ();
5d5658a1 11255 thread = tp->global_num;
186c406b 11256
883bc8d1
PA
11257 old_chain = make_cleanup (null_cleanup, NULL);
11258
8556afb4
PA
11259 /* Note linespec handling above invalidates the frame chain.
11260 Installing a breakpoint also invalidates the frame chain (as it
11261 may need to switch threads), so do any frame handling before
11262 that. */
11263
11264 frame = get_selected_frame (NULL);
11265 frame_gdbarch = get_frame_arch (frame);
11266 stack_frame_id = get_stack_frame_id (frame);
11267 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11268
ae66c1fc
EZ
11269 /* Keep within the current frame, or in frames called by the current
11270 one. */
edb3359d 11271
883bc8d1 11272 if (frame_id_p (caller_frame_id))
c906108c 11273 {
883bc8d1 11274 struct symtab_and_line sal2;
cfc31633 11275 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11276
11277 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11278 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11279 caller_gdbarch = frame_unwind_caller_arch (frame);
11280 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11281 sal2,
11282 caller_frame_id,
11283 bp_until);
11284 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11285
883bc8d1 11286 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11287 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11288 }
c5aa993b 11289
c70a6932
JK
11290 /* set_momentary_breakpoint could invalidate FRAME. */
11291 frame = NULL;
11292
883bc8d1
PA
11293 if (anywhere)
11294 /* If the user told us to continue until a specified location,
11295 we don't specify a frame at which we need to stop. */
cfc31633
PA
11296 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11297 null_frame_id, bp_until);
883bc8d1
PA
11298 else
11299 /* Otherwise, specify the selected frame, because we want to stop
11300 only at the very same frame. */
cfc31633
PA
11301 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11302 stack_frame_id, bp_until);
11303 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11304
8980e177 11305 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11306 location_breakpoint, caller_breakpoint);
cfc31633 11307 tp->thread_fsm = &sm->thread_fsm;
f107f563 11308
cfc31633 11309 discard_cleanups (old_chain);
f107f563 11310
cfc31633 11311 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11312}
ae66c1fc 11313
c906108c
SS
11314/* This function attempts to parse an optional "if <cond>" clause
11315 from the arg string. If one is not found, it returns NULL.
c5aa993b 11316
c906108c
SS
11317 Else, it returns a pointer to the condition string. (It does not
11318 attempt to evaluate the string against a particular block.) And,
11319 it updates arg to point to the first character following the parsed
4a64f543 11320 if clause in the arg string. */
53a5351d 11321
63160a43
PA
11322const char *
11323ep_parse_optional_if_clause (const char **arg)
c906108c 11324{
63160a43 11325 const char *cond_string;
c5aa993b
JM
11326
11327 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11328 return NULL;
c5aa993b 11329
4a64f543 11330 /* Skip the "if" keyword. */
c906108c 11331 (*arg) += 2;
c5aa993b 11332
c906108c 11333 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11334 condition string. */
f1735a53 11335 *arg = skip_spaces (*arg);
c906108c 11336 cond_string = *arg;
c5aa993b 11337
4a64f543
MS
11338 /* Assume that the condition occupies the remainder of the arg
11339 string. */
c906108c 11340 (*arg) += strlen (cond_string);
c5aa993b 11341
c906108c
SS
11342 return cond_string;
11343}
c5aa993b 11344
c906108c
SS
11345/* Commands to deal with catching events, such as signals, exceptions,
11346 process start/exit, etc. */
c5aa993b
JM
11347
11348typedef enum
11349{
44feb3ce
TT
11350 catch_fork_temporary, catch_vfork_temporary,
11351 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11352}
11353catch_fork_kind;
11354
c906108c 11355static void
63160a43 11356catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11357 struct cmd_list_element *command)
c906108c 11358{
63160a43 11359 const char *arg = arg_entry;
a6d9a66e 11360 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11361 const char *cond_string = NULL;
44feb3ce
TT
11362 catch_fork_kind fork_kind;
11363 int tempflag;
11364
11365 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11366 tempflag = (fork_kind == catch_fork_temporary
11367 || fork_kind == catch_vfork_temporary);
c5aa993b 11368
44feb3ce
TT
11369 if (!arg)
11370 arg = "";
f1735a53 11371 arg = skip_spaces (arg);
c5aa993b 11372
c906108c 11373 /* The allowed syntax is:
c5aa993b
JM
11374 catch [v]fork
11375 catch [v]fork if <cond>
11376
4a64f543 11377 First, check if there's an if clause. */
c906108c 11378 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11379
c906108c 11380 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11381 error (_("Junk at end of arguments."));
c5aa993b 11382
c906108c 11383 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11384 and enable reporting of such events. */
c5aa993b
JM
11385 switch (fork_kind)
11386 {
44feb3ce
TT
11387 case catch_fork_temporary:
11388 case catch_fork_permanent:
a6d9a66e 11389 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11390 &catch_fork_breakpoint_ops);
c906108c 11391 break;
44feb3ce
TT
11392 case catch_vfork_temporary:
11393 case catch_vfork_permanent:
a6d9a66e 11394 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11395 &catch_vfork_breakpoint_ops);
c906108c 11396 break;
c5aa993b 11397 default:
8a3fe4f8 11398 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11399 break;
c5aa993b 11400 }
c906108c
SS
11401}
11402
11403static void
63160a43 11404catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11405 struct cmd_list_element *command)
c906108c 11406{
63160a43 11407 const char *arg = arg_entry;
a6d9a66e 11408 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11409 int tempflag;
63160a43 11410 const char *cond_string = NULL;
c906108c 11411
44feb3ce
TT
11412 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11413
11414 if (!arg)
11415 arg = "";
f1735a53 11416 arg = skip_spaces (arg);
c906108c
SS
11417
11418 /* The allowed syntax is:
c5aa993b
JM
11419 catch exec
11420 catch exec if <cond>
c906108c 11421
4a64f543 11422 First, check if there's an if clause. */
c906108c
SS
11423 cond_string = ep_parse_optional_if_clause (&arg);
11424
11425 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11426 error (_("Junk at end of arguments."));
c906108c 11427
b270e6f9
TT
11428 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11429 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11430 &catch_exec_breakpoint_ops);
11431 c->exec_pathname = NULL;
11432
b270e6f9 11433 install_breakpoint (0, std::move (c), 1);
c906108c 11434}
c5aa993b 11435
9ac4176b 11436void
28010a5d
PA
11437init_ada_exception_breakpoint (struct breakpoint *b,
11438 struct gdbarch *gdbarch,
11439 struct symtab_and_line sal,
f2fc3015 11440 const char *addr_string,
c0a91b2b 11441 const struct breakpoint_ops *ops,
28010a5d 11442 int tempflag,
349774ef 11443 int enabled,
28010a5d 11444 int from_tty)
f7f9143b 11445{
f7f9143b
JB
11446 if (from_tty)
11447 {
5af949e3
UW
11448 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11449 if (!loc_gdbarch)
11450 loc_gdbarch = gdbarch;
11451
6c95b8df
PA
11452 describe_other_breakpoints (loc_gdbarch,
11453 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11454 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11455 version for exception catchpoints, because two catchpoints
11456 used for different exception names will use the same address.
11457 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11458 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11459 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11460 the user what type of catchpoint it is. The above is good
11461 enough for now, though. */
11462 }
11463
28010a5d 11464 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11465
349774ef 11466 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11467 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11468 b->location = string_to_event_location (&addr_string,
11469 language_def (language_ada));
f7f9143b 11470 b->language = language_ada;
f7f9143b
JB
11471}
11472
c906108c 11473static void
981a3fb3 11474catch_command (const char *arg, int from_tty)
c906108c 11475{
44feb3ce 11476 error (_("Catch requires an event name."));
c906108c
SS
11477}
11478\f
11479
11480static void
981a3fb3 11481tcatch_command (const char *arg, int from_tty)
c906108c 11482{
44feb3ce 11483 error (_("Catch requires an event name."));
c906108c
SS
11484}
11485
81b1e71c 11486/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11487
11488static int
81b1e71c 11489compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11490{
81b1e71c
TT
11491 uintptr_t ua = (uintptr_t) a;
11492 uintptr_t ub = (uintptr_t) b;
8a2c437b 11493
81b1e71c 11494 if (a->number < b->number)
8a2c437b 11495 return -1;
81b1e71c 11496 else if (a->number > b->number)
8a2c437b
TT
11497 return 1;
11498
11499 /* Now sort by address, in case we see, e..g, two breakpoints with
11500 the number 0. */
11501 if (ua < ub)
11502 return -1;
94b0e70d 11503 return ua > ub ? 1 : 0;
8a2c437b
TT
11504}
11505
80f8a6eb 11506/* Delete breakpoints by address or line. */
c906108c
SS
11507
11508static void
fba45db2 11509clear_command (char *arg, int from_tty)
c906108c 11510{
81b1e71c 11511 struct breakpoint *b;
c906108c 11512 int default_match;
c906108c
SS
11513 int i;
11514
6c5b2ebe
PA
11515 std::vector<symtab_and_line> decoded_sals;
11516 symtab_and_line last_sal;
11517 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11518 if (arg)
11519 {
6c5b2ebe
PA
11520 decoded_sals
11521 = decode_line_with_current_source (arg,
11522 (DECODE_LINE_FUNFIRSTLINE
11523 | DECODE_LINE_LIST_MODE));
c906108c 11524 default_match = 0;
6c5b2ebe 11525 sals = decoded_sals;
c906108c
SS
11526 }
11527 else
11528 {
1bfeeb0f
JL
11529 /* Set sal's line, symtab, pc, and pspace to the values
11530 corresponding to the last call to print_frame_info. If the
11531 codepoint is not valid, this will set all the fields to 0. */
51abb421 11532 last_sal = get_last_displayed_sal ();
6c5b2ebe 11533 if (last_sal.symtab == 0)
8a3fe4f8 11534 error (_("No source file specified."));
c906108c 11535
c906108c 11536 default_match = 1;
6c5b2ebe 11537 sals = last_sal;
c906108c
SS
11538 }
11539
4a64f543
MS
11540 /* We don't call resolve_sal_pc here. That's not as bad as it
11541 seems, because all existing breakpoints typically have both
11542 file/line and pc set. So, if clear is given file/line, we can
11543 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11544
11545 We only support clearing given the address explicitly
11546 present in breakpoint table. Say, we've set breakpoint
4a64f543 11547 at file:line. There were several PC values for that file:line,
ed0616c6 11548 due to optimization, all in one block.
4a64f543
MS
11549
11550 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11551 PC corresponding to the same file:line, the breakpoint won't
11552 be cleared. We probably can still clear the breakpoint, but
11553 since the other PC value is never presented to user, user
11554 can only find it by guessing, and it does not seem important
11555 to support that. */
11556
4a64f543
MS
11557 /* For each line spec given, delete bps which correspond to it. Do
11558 it in two passes, solely to preserve the current behavior that
11559 from_tty is forced true if we delete more than one
11560 breakpoint. */
c906108c 11561
81b1e71c 11562 std::vector<struct breakpoint *> found;
6c5b2ebe 11563 for (const auto &sal : sals)
c906108c 11564 {
05cba821
JK
11565 const char *sal_fullname;
11566
c906108c 11567 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11568 If line given (pc == 0), clear all bpts on specified line.
11569 If defaulting, clear all bpts on default line
c906108c 11570 or at default pc.
c5aa993b
JM
11571
11572 defaulting sal.pc != 0 tests to do
11573
11574 0 1 pc
11575 1 1 pc _and_ line
11576 0 0 line
11577 1 0 <can't happen> */
c906108c 11578
05cba821
JK
11579 sal_fullname = (sal.symtab == NULL
11580 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11581
4a64f543 11582 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11583 ALL_BREAKPOINTS (b)
c5aa993b 11584 {
0d381245 11585 int match = 0;
4a64f543 11586 /* Are we going to delete b? */
cc60f2e3 11587 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11588 {
11589 struct bp_location *loc = b->loc;
11590 for (; loc; loc = loc->next)
11591 {
f8eba3c6
TT
11592 /* If the user specified file:line, don't allow a PC
11593 match. This matches historical gdb behavior. */
11594 int pc_match = (!sal.explicit_line
11595 && sal.pc
11596 && (loc->pspace == sal.pspace)
11597 && (loc->address == sal.pc)
11598 && (!section_is_overlay (loc->section)
11599 || loc->section == sal.section));
4aac40c8
TT
11600 int line_match = 0;
11601
11602 if ((default_match || sal.explicit_line)
2f202fde 11603 && loc->symtab != NULL
05cba821 11604 && sal_fullname != NULL
4aac40c8 11605 && sal.pspace == loc->pspace
05cba821
JK
11606 && loc->line_number == sal.line
11607 && filename_cmp (symtab_to_fullname (loc->symtab),
11608 sal_fullname) == 0)
11609 line_match = 1;
4aac40c8 11610
0d381245
VP
11611 if (pc_match || line_match)
11612 {
11613 match = 1;
11614 break;
11615 }
11616 }
11617 }
11618
11619 if (match)
81b1e71c 11620 found.push_back (b);
c906108c 11621 }
80f8a6eb 11622 }
8a2c437b 11623
80f8a6eb 11624 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11625 if (found.empty ())
80f8a6eb
MS
11626 {
11627 if (arg)
8a3fe4f8 11628 error (_("No breakpoint at %s."), arg);
80f8a6eb 11629 else
8a3fe4f8 11630 error (_("No breakpoint at this line."));
80f8a6eb 11631 }
c906108c 11632
8a2c437b 11633 /* Remove duplicates from the vec. */
81b1e71c
TT
11634 std::sort (found.begin (), found.end (),
11635 [] (const breakpoint *a, const breakpoint *b)
11636 {
11637 return compare_breakpoints (a, b) < 0;
11638 });
11639 found.erase (std::unique (found.begin (), found.end (),
11640 [] (const breakpoint *a, const breakpoint *b)
11641 {
11642 return compare_breakpoints (a, b) == 0;
11643 }),
11644 found.end ());
8a2c437b 11645
81b1e71c 11646 if (found.size () > 1)
4a64f543 11647 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11648 if (from_tty)
a3f17187 11649 {
81b1e71c 11650 if (found.size () == 1)
a3f17187
AC
11651 printf_unfiltered (_("Deleted breakpoint "));
11652 else
11653 printf_unfiltered (_("Deleted breakpoints "));
11654 }
d6e956e5 11655
81b1e71c 11656 for (breakpoint *iter : found)
80f8a6eb 11657 {
c5aa993b 11658 if (from_tty)
81b1e71c
TT
11659 printf_unfiltered ("%d ", iter->number);
11660 delete_breakpoint (iter);
c906108c 11661 }
80f8a6eb
MS
11662 if (from_tty)
11663 putchar_unfiltered ('\n');
c906108c
SS
11664}
11665\f
11666/* Delete breakpoint in BS if they are `delete' breakpoints and
11667 all breakpoints that are marked for deletion, whether hit or not.
11668 This is called after any breakpoint is hit, or after errors. */
11669
11670void
fba45db2 11671breakpoint_auto_delete (bpstat bs)
c906108c 11672{
35df4500 11673 struct breakpoint *b, *b_tmp;
c906108c
SS
11674
11675 for (; bs; bs = bs->next)
f431efe5
PA
11676 if (bs->breakpoint_at
11677 && bs->breakpoint_at->disposition == disp_del
c906108c 11678 && bs->stop)
f431efe5 11679 delete_breakpoint (bs->breakpoint_at);
c906108c 11680
35df4500 11681 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11682 {
b5de0fa7 11683 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11684 delete_breakpoint (b);
11685 }
c906108c
SS
11686}
11687
4a64f543
MS
11688/* A comparison function for bp_location AP and BP being interfaced to
11689 qsort. Sort elements primarily by their ADDRESS (no matter what
11690 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11691 secondarily by ordering first permanent elements and
4a64f543 11692 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11693 qsort being an unstable algorithm. */
876fa593
JK
11694
11695static int
f5336ca5 11696bp_locations_compare (const void *ap, const void *bp)
876fa593 11697{
9a3c8263
SM
11698 const struct bp_location *a = *(const struct bp_location **) ap;
11699 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11700
11701 if (a->address != b->address)
11702 return (a->address > b->address) - (a->address < b->address);
11703
dea2aa5f
LM
11704 /* Sort locations at the same address by their pspace number, keeping
11705 locations of the same inferior (in a multi-inferior environment)
11706 grouped. */
11707
11708 if (a->pspace->num != b->pspace->num)
11709 return ((a->pspace->num > b->pspace->num)
11710 - (a->pspace->num < b->pspace->num));
11711
876fa593 11712 /* Sort permanent breakpoints first. */
1a853c52
PA
11713 if (a->permanent != b->permanent)
11714 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11715
c56a97f9
JK
11716 /* Make the internal GDB representation stable across GDB runs
11717 where A and B memory inside GDB can differ. Breakpoint locations of
11718 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11719
11720 if (a->owner->number != b->owner->number)
c56a97f9
JK
11721 return ((a->owner->number > b->owner->number)
11722 - (a->owner->number < b->owner->number));
876fa593
JK
11723
11724 return (a > b) - (a < b);
11725}
11726
f5336ca5
PA
11727/* Set bp_locations_placed_address_before_address_max and
11728 bp_locations_shadow_len_after_address_max according to the current
11729 content of the bp_locations array. */
f7545552
TT
11730
11731static void
f5336ca5 11732bp_locations_target_extensions_update (void)
f7545552 11733{
876fa593
JK
11734 struct bp_location *bl, **blp_tmp;
11735
f5336ca5
PA
11736 bp_locations_placed_address_before_address_max = 0;
11737 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11738
11739 ALL_BP_LOCATIONS (bl, blp_tmp)
11740 {
11741 CORE_ADDR start, end, addr;
11742
11743 if (!bp_location_has_shadow (bl))
11744 continue;
11745
11746 start = bl->target_info.placed_address;
11747 end = start + bl->target_info.shadow_len;
11748
11749 gdb_assert (bl->address >= start);
11750 addr = bl->address - start;
f5336ca5
PA
11751 if (addr > bp_locations_placed_address_before_address_max)
11752 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11753
11754 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11755
11756 gdb_assert (bl->address < end);
11757 addr = end - bl->address;
f5336ca5
PA
11758 if (addr > bp_locations_shadow_len_after_address_max)
11759 bp_locations_shadow_len_after_address_max = addr;
876fa593 11760 }
f7545552
TT
11761}
11762
1e4d1764
YQ
11763/* Download tracepoint locations if they haven't been. */
11764
11765static void
11766download_tracepoint_locations (void)
11767{
7ed2c994 11768 struct breakpoint *b;
dd2e65cc 11769 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11770
5ed8105e 11771 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11772
7ed2c994 11773 ALL_TRACEPOINTS (b)
1e4d1764 11774 {
7ed2c994 11775 struct bp_location *bl;
1e4d1764 11776 struct tracepoint *t;
f2a8bc8a 11777 int bp_location_downloaded = 0;
1e4d1764 11778
7ed2c994 11779 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11780 ? !may_insert_fast_tracepoints
11781 : !may_insert_tracepoints))
11782 continue;
11783
dd2e65cc
YQ
11784 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11785 {
11786 if (target_can_download_tracepoint ())
11787 can_download_tracepoint = TRIBOOL_TRUE;
11788 else
11789 can_download_tracepoint = TRIBOOL_FALSE;
11790 }
11791
11792 if (can_download_tracepoint == TRIBOOL_FALSE)
11793 break;
11794
7ed2c994
YQ
11795 for (bl = b->loc; bl; bl = bl->next)
11796 {
11797 /* In tracepoint, locations are _never_ duplicated, so
11798 should_be_inserted is equivalent to
11799 unduplicated_should_be_inserted. */
11800 if (!should_be_inserted (bl) || bl->inserted)
11801 continue;
1e4d1764 11802
7ed2c994 11803 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11804
7ed2c994 11805 target_download_tracepoint (bl);
1e4d1764 11806
7ed2c994 11807 bl->inserted = 1;
f2a8bc8a 11808 bp_location_downloaded = 1;
7ed2c994
YQ
11809 }
11810 t = (struct tracepoint *) b;
11811 t->number_on_target = b->number;
f2a8bc8a
YQ
11812 if (bp_location_downloaded)
11813 observer_notify_breakpoint_modified (b);
1e4d1764 11814 }
1e4d1764
YQ
11815}
11816
934709f0
PW
11817/* Swap the insertion/duplication state between two locations. */
11818
11819static void
11820swap_insertion (struct bp_location *left, struct bp_location *right)
11821{
11822 const int left_inserted = left->inserted;
11823 const int left_duplicate = left->duplicate;
b775012e 11824 const int left_needs_update = left->needs_update;
934709f0
PW
11825 const struct bp_target_info left_target_info = left->target_info;
11826
1e4d1764
YQ
11827 /* Locations of tracepoints can never be duplicated. */
11828 if (is_tracepoint (left->owner))
11829 gdb_assert (!left->duplicate);
11830 if (is_tracepoint (right->owner))
11831 gdb_assert (!right->duplicate);
11832
934709f0
PW
11833 left->inserted = right->inserted;
11834 left->duplicate = right->duplicate;
b775012e 11835 left->needs_update = right->needs_update;
934709f0
PW
11836 left->target_info = right->target_info;
11837 right->inserted = left_inserted;
11838 right->duplicate = left_duplicate;
b775012e 11839 right->needs_update = left_needs_update;
934709f0
PW
11840 right->target_info = left_target_info;
11841}
11842
b775012e
LM
11843/* Force the re-insertion of the locations at ADDRESS. This is called
11844 once a new/deleted/modified duplicate location is found and we are evaluating
11845 conditions on the target's side. Such conditions need to be updated on
11846 the target. */
11847
11848static void
11849force_breakpoint_reinsertion (struct bp_location *bl)
11850{
11851 struct bp_location **locp = NULL, **loc2p;
11852 struct bp_location *loc;
11853 CORE_ADDR address = 0;
11854 int pspace_num;
11855
11856 address = bl->address;
11857 pspace_num = bl->pspace->num;
11858
11859 /* This is only meaningful if the target is
11860 evaluating conditions and if the user has
11861 opted for condition evaluation on the target's
11862 side. */
11863 if (gdb_evaluates_breakpoint_condition_p ()
11864 || !target_supports_evaluation_of_breakpoint_conditions ())
11865 return;
11866
11867 /* Flag all breakpoint locations with this address and
11868 the same program space as the location
11869 as "its condition has changed". We need to
11870 update the conditions on the target's side. */
11871 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11872 {
11873 loc = *loc2p;
11874
11875 if (!is_breakpoint (loc->owner)
11876 || pspace_num != loc->pspace->num)
11877 continue;
11878
11879 /* Flag the location appropriately. We use a different state to
11880 let everyone know that we already updated the set of locations
11881 with addr bl->address and program space bl->pspace. This is so
11882 we don't have to keep calling these functions just to mark locations
11883 that have already been marked. */
11884 loc->condition_changed = condition_updated;
11885
11886 /* Free the agent expression bytecode as well. We will compute
11887 it later on. */
833177a4 11888 loc->cond_bytecode.reset ();
b775012e
LM
11889 }
11890}
44702360
PA
11891/* Called whether new breakpoints are created, or existing breakpoints
11892 deleted, to update the global location list and recompute which
11893 locations are duplicate of which.
b775012e 11894
04086b45
PA
11895 The INSERT_MODE flag determines whether locations may not, may, or
11896 shall be inserted now. See 'enum ugll_insert_mode' for more
11897 info. */
b60e7edf 11898
0d381245 11899static void
44702360 11900update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11901{
74960c60 11902 struct breakpoint *b;
876fa593 11903 struct bp_location **locp, *loc;
b775012e
LM
11904 /* Last breakpoint location address that was marked for update. */
11905 CORE_ADDR last_addr = 0;
11906 /* Last breakpoint location program space that was marked for update. */
11907 int last_pspace_num = -1;
f7545552 11908
2d134ed3
PA
11909 /* Used in the duplicates detection below. When iterating over all
11910 bp_locations, points to the first bp_location of a given address.
11911 Breakpoints and watchpoints of different types are never
11912 duplicates of each other. Keep one pointer for each type of
11913 breakpoint/watchpoint, so we only need to loop over all locations
11914 once. */
11915 struct bp_location *bp_loc_first; /* breakpoint */
11916 struct bp_location *wp_loc_first; /* hardware watchpoint */
11917 struct bp_location *awp_loc_first; /* access watchpoint */
11918 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11919
f5336ca5
PA
11920 /* Saved former bp_locations array which we compare against the newly
11921 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11922 struct bp_location **old_locp;
f5336ca5 11923 unsigned old_locations_count;
81b1e71c 11924 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11925
f5336ca5
PA
11926 old_locations_count = bp_locations_count;
11927 bp_locations = NULL;
11928 bp_locations_count = 0;
0d381245 11929
74960c60 11930 ALL_BREAKPOINTS (b)
876fa593 11931 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11932 bp_locations_count++;
876fa593 11933
f5336ca5
PA
11934 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11935 locp = bp_locations;
876fa593
JK
11936 ALL_BREAKPOINTS (b)
11937 for (loc = b->loc; loc; loc = loc->next)
11938 *locp++ = loc;
f5336ca5
PA
11939 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11940 bp_locations_compare);
876fa593 11941
f5336ca5 11942 bp_locations_target_extensions_update ();
74960c60 11943
4a64f543
MS
11944 /* Identify bp_location instances that are no longer present in the
11945 new list, and therefore should be freed. Note that it's not
11946 necessary that those locations should be removed from inferior --
11947 if there's another location at the same address (previously
11948 marked as duplicate), we don't need to remove/insert the
11949 location.
876fa593 11950
4a64f543
MS
11951 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11952 and former bp_location array state respectively. */
876fa593 11953
f5336ca5 11954 locp = bp_locations;
81b1e71c
TT
11955 for (old_locp = old_locations.get ();
11956 old_locp < old_locations.get () + old_locations_count;
876fa593 11957 old_locp++)
74960c60 11958 {
876fa593 11959 struct bp_location *old_loc = *old_locp;
c7d46a38 11960 struct bp_location **loc2p;
876fa593 11961
e5dd4106 11962 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11963 not, we have to free it. */
c7d46a38 11964 int found_object = 0;
20874c92
VP
11965 /* Tells if the location should remain inserted in the target. */
11966 int keep_in_target = 0;
11967 int removed = 0;
876fa593 11968
4a64f543
MS
11969 /* Skip LOCP entries which will definitely never be needed.
11970 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11971 while (locp < bp_locations + bp_locations_count
c7d46a38 11972 && (*locp)->address < old_loc->address)
876fa593 11973 locp++;
c7d46a38
PA
11974
11975 for (loc2p = locp;
f5336ca5 11976 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11977 && (*loc2p)->address == old_loc->address);
11978 loc2p++)
11979 {
b775012e
LM
11980 /* Check if this is a new/duplicated location or a duplicated
11981 location that had its condition modified. If so, we want to send
11982 its condition to the target if evaluation of conditions is taking
11983 place there. */
11984 if ((*loc2p)->condition_changed == condition_modified
11985 && (last_addr != old_loc->address
11986 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11987 {
b775012e
LM
11988 force_breakpoint_reinsertion (*loc2p);
11989 last_pspace_num = old_loc->pspace->num;
c7d46a38 11990 }
b775012e
LM
11991
11992 if (*loc2p == old_loc)
11993 found_object = 1;
c7d46a38 11994 }
74960c60 11995
b775012e
LM
11996 /* We have already handled this address, update it so that we don't
11997 have to go through updates again. */
11998 last_addr = old_loc->address;
11999
12000 /* Target-side condition evaluation: Handle deleted locations. */
12001 if (!found_object)
12002 force_breakpoint_reinsertion (old_loc);
12003
4a64f543
MS
12004 /* If this location is no longer present, and inserted, look if
12005 there's maybe a new location at the same address. If so,
12006 mark that one inserted, and don't remove this one. This is
12007 needed so that we don't have a time window where a breakpoint
12008 at certain location is not inserted. */
74960c60 12009
876fa593 12010 if (old_loc->inserted)
0d381245 12011 {
4a64f543
MS
12012 /* If the location is inserted now, we might have to remove
12013 it. */
74960c60 12014
876fa593 12015 if (found_object && should_be_inserted (old_loc))
74960c60 12016 {
4a64f543
MS
12017 /* The location is still present in the location list,
12018 and still should be inserted. Don't do anything. */
20874c92 12019 keep_in_target = 1;
74960c60
VP
12020 }
12021 else
12022 {
b775012e
LM
12023 /* This location still exists, but it won't be kept in the
12024 target since it may have been disabled. We proceed to
12025 remove its target-side condition. */
12026
4a64f543
MS
12027 /* The location is either no longer present, or got
12028 disabled. See if there's another location at the
12029 same address, in which case we don't need to remove
12030 this one from the target. */
876fa593 12031
2bdf28a0 12032 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12033 if (breakpoint_address_is_meaningful (old_loc->owner))
12034 {
876fa593 12035 for (loc2p = locp;
f5336ca5 12036 (loc2p < bp_locations + bp_locations_count
c7d46a38 12037 && (*loc2p)->address == old_loc->address);
876fa593
JK
12038 loc2p++)
12039 {
12040 struct bp_location *loc2 = *loc2p;
12041
2d134ed3 12042 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12043 {
85d721b8
PA
12044 /* Read watchpoint locations are switched to
12045 access watchpoints, if the former are not
12046 supported, but the latter are. */
12047 if (is_hardware_watchpoint (old_loc->owner))
12048 {
12049 gdb_assert (is_hardware_watchpoint (loc2->owner));
12050 loc2->watchpoint_type = old_loc->watchpoint_type;
12051 }
12052
934709f0
PW
12053 /* loc2 is a duplicated location. We need to check
12054 if it should be inserted in case it will be
12055 unduplicated. */
12056 if (loc2 != old_loc
12057 && unduplicated_should_be_inserted (loc2))
c7d46a38 12058 {
934709f0 12059 swap_insertion (old_loc, loc2);
c7d46a38
PA
12060 keep_in_target = 1;
12061 break;
12062 }
876fa593
JK
12063 }
12064 }
12065 }
74960c60
VP
12066 }
12067
20874c92
VP
12068 if (!keep_in_target)
12069 {
834c0d03 12070 if (remove_breakpoint (old_loc))
20874c92 12071 {
4a64f543
MS
12072 /* This is just about all we can do. We could keep
12073 this location on the global list, and try to
12074 remove it next time, but there's no particular
12075 reason why we will succeed next time.
20874c92 12076
4a64f543
MS
12077 Note that at this point, old_loc->owner is still
12078 valid, as delete_breakpoint frees the breakpoint
12079 only after calling us. */
3e43a32a
MS
12080 printf_filtered (_("warning: Error removing "
12081 "breakpoint %d\n"),
876fa593 12082 old_loc->owner->number);
20874c92
VP
12083 }
12084 removed = 1;
12085 }
0d381245 12086 }
74960c60
VP
12087
12088 if (!found_object)
1c5cfe86 12089 {
fbea99ea 12090 if (removed && target_is_non_stop_p ()
1cf4d951 12091 && need_moribund_for_location_type (old_loc))
20874c92 12092 {
db82e815
PA
12093 /* This location was removed from the target. In
12094 non-stop mode, a race condition is possible where
12095 we've removed a breakpoint, but stop events for that
12096 breakpoint are already queued and will arrive later.
12097 We apply an heuristic to be able to distinguish such
12098 SIGTRAPs from other random SIGTRAPs: we keep this
12099 breakpoint location for a bit, and will retire it
12100 after we see some number of events. The theory here
12101 is that reporting of events should, "on the average",
12102 be fair, so after a while we'll see events from all
12103 threads that have anything of interest, and no longer
12104 need to keep this breakpoint location around. We
12105 don't hold locations forever so to reduce chances of
12106 mistaking a non-breakpoint SIGTRAP for a breakpoint
12107 SIGTRAP.
12108
12109 The heuristic failing can be disastrous on
12110 decr_pc_after_break targets.
12111
12112 On decr_pc_after_break targets, like e.g., x86-linux,
12113 if we fail to recognize a late breakpoint SIGTRAP,
12114 because events_till_retirement has reached 0 too
12115 soon, we'll fail to do the PC adjustment, and report
12116 a random SIGTRAP to the user. When the user resumes
12117 the inferior, it will most likely immediately crash
2dec564e 12118 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12119 corrupted, because of being resumed e.g., in the
12120 middle of a multi-byte instruction, or skipped a
12121 one-byte instruction. This was actually seen happen
12122 on native x86-linux, and should be less rare on
12123 targets that do not support new thread events, like
12124 remote, due to the heuristic depending on
12125 thread_count.
12126
12127 Mistaking a random SIGTRAP for a breakpoint trap
12128 causes similar symptoms (PC adjustment applied when
12129 it shouldn't), but then again, playing with SIGTRAPs
12130 behind the debugger's back is asking for trouble.
12131
12132 Since hardware watchpoint traps are always
12133 distinguishable from other traps, so we don't need to
12134 apply keep hardware watchpoint moribund locations
12135 around. We simply always ignore hardware watchpoint
12136 traps we can no longer explain. */
12137
876fa593
JK
12138 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12139 old_loc->owner = NULL;
20874c92 12140
876fa593 12141 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12142 }
12143 else
f431efe5
PA
12144 {
12145 old_loc->owner = NULL;
12146 decref_bp_location (&old_loc);
12147 }
20874c92 12148 }
74960c60 12149 }
1c5cfe86 12150
348d480f
PA
12151 /* Rescan breakpoints at the same address and section, marking the
12152 first one as "first" and any others as "duplicates". This is so
12153 that the bpt instruction is only inserted once. If we have a
12154 permanent breakpoint at the same place as BPT, make that one the
12155 official one, and the rest as duplicates. Permanent breakpoints
12156 are sorted first for the same address.
12157
12158 Do the same for hardware watchpoints, but also considering the
12159 watchpoint's type (regular/access/read) and length. */
12160
12161 bp_loc_first = NULL;
12162 wp_loc_first = NULL;
12163 awp_loc_first = NULL;
12164 rwp_loc_first = NULL;
12165 ALL_BP_LOCATIONS (loc, locp)
12166 {
12167 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12168 non-NULL. */
348d480f 12169 struct bp_location **loc_first_p;
d3fbdd86 12170 b = loc->owner;
348d480f 12171
6f380991 12172 if (!unduplicated_should_be_inserted (loc)
348d480f 12173 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12174 /* Don't detect duplicate for tracepoint locations because they are
12175 never duplicated. See the comments in field `duplicate' of
12176 `struct bp_location'. */
348d480f 12177 || is_tracepoint (b))
b775012e
LM
12178 {
12179 /* Clear the condition modification flag. */
12180 loc->condition_changed = condition_unchanged;
12181 continue;
12182 }
348d480f 12183
348d480f
PA
12184 if (b->type == bp_hardware_watchpoint)
12185 loc_first_p = &wp_loc_first;
12186 else if (b->type == bp_read_watchpoint)
12187 loc_first_p = &rwp_loc_first;
12188 else if (b->type == bp_access_watchpoint)
12189 loc_first_p = &awp_loc_first;
12190 else
12191 loc_first_p = &bp_loc_first;
12192
12193 if (*loc_first_p == NULL
12194 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12195 || !breakpoint_locations_match (loc, *loc_first_p))
12196 {
12197 *loc_first_p = loc;
12198 loc->duplicate = 0;
b775012e
LM
12199
12200 if (is_breakpoint (loc->owner) && loc->condition_changed)
12201 {
12202 loc->needs_update = 1;
12203 /* Clear the condition modification flag. */
12204 loc->condition_changed = condition_unchanged;
12205 }
348d480f
PA
12206 continue;
12207 }
12208
934709f0
PW
12209
12210 /* This and the above ensure the invariant that the first location
12211 is not duplicated, and is the inserted one.
12212 All following are marked as duplicated, and are not inserted. */
12213 if (loc->inserted)
12214 swap_insertion (loc, *loc_first_p);
348d480f
PA
12215 loc->duplicate = 1;
12216
b775012e
LM
12217 /* Clear the condition modification flag. */
12218 loc->condition_changed = condition_unchanged;
348d480f
PA
12219 }
12220
a25a5a45 12221 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12222 {
04086b45 12223 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12224 insert_breakpoint_locations ();
12225 else
12226 {
44702360
PA
12227 /* Even though the caller told us to not insert new
12228 locations, we may still need to update conditions on the
12229 target's side of breakpoints that were already inserted
12230 if the target is evaluating breakpoint conditions. We
b775012e
LM
12231 only update conditions for locations that are marked
12232 "needs_update". */
12233 update_inserted_breakpoint_locations ();
12234 }
12235 }
348d480f 12236
04086b45 12237 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12238 download_tracepoint_locations ();
348d480f
PA
12239}
12240
12241void
12242breakpoint_retire_moribund (void)
12243{
12244 struct bp_location *loc;
12245 int ix;
12246
12247 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12248 if (--(loc->events_till_retirement) == 0)
12249 {
12250 decref_bp_location (&loc);
12251 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12252 --ix;
12253 }
12254}
12255
12256static void
44702360 12257update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12258{
348d480f 12259
492d29ea
PA
12260 TRY
12261 {
12262 update_global_location_list (insert_mode);
12263 }
12264 CATCH (e, RETURN_MASK_ERROR)
12265 {
12266 }
12267 END_CATCH
348d480f
PA
12268}
12269
12270/* Clear BKP from a BPS. */
12271
12272static void
12273bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12274{
12275 bpstat bs;
12276
12277 for (bs = bps; bs; bs = bs->next)
12278 if (bs->breakpoint_at == bpt)
12279 {
12280 bs->breakpoint_at = NULL;
12281 bs->old_val = NULL;
12282 /* bs->commands will be freed later. */
12283 }
12284}
12285
12286/* Callback for iterate_over_threads. */
12287static int
12288bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12289{
9a3c8263 12290 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12291
12292 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12293 return 0;
12294}
12295
12296/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12297 callbacks. */
12298
12299static void
12300say_where (struct breakpoint *b)
12301{
12302 struct value_print_options opts;
12303
12304 get_user_print_options (&opts);
12305
12306 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12307 single string. */
12308 if (b->loc == NULL)
12309 {
f00aae0f
KS
12310 /* For pending locations, the output differs slightly based
12311 on b->extra_string. If this is non-NULL, it contains either
12312 a condition or dprintf arguments. */
12313 if (b->extra_string == NULL)
12314 {
12315 printf_filtered (_(" (%s) pending."),
d28cd78a 12316 event_location_to_string (b->location.get ()));
f00aae0f
KS
12317 }
12318 else if (b->type == bp_dprintf)
12319 {
12320 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12321 event_location_to_string (b->location.get ()),
f00aae0f
KS
12322 b->extra_string);
12323 }
12324 else
12325 {
12326 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12327 event_location_to_string (b->location.get ()),
f00aae0f
KS
12328 b->extra_string);
12329 }
348d480f
PA
12330 }
12331 else
12332 {
2f202fde 12333 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12334 {
12335 printf_filtered (" at ");
12336 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12337 gdb_stdout);
12338 }
2f202fde 12339 if (b->loc->symtab != NULL)
f8eba3c6
TT
12340 {
12341 /* If there is a single location, we can print the location
12342 more nicely. */
12343 if (b->loc->next == NULL)
12344 printf_filtered (": file %s, line %d.",
05cba821
JK
12345 symtab_to_filename_for_display (b->loc->symtab),
12346 b->loc->line_number);
f8eba3c6
TT
12347 else
12348 /* This is not ideal, but each location may have a
12349 different file name, and this at least reflects the
12350 real situation somewhat. */
f00aae0f 12351 printf_filtered (": %s.",
d28cd78a 12352 event_location_to_string (b->location.get ()));
f8eba3c6 12353 }
348d480f
PA
12354
12355 if (b->loc->next)
12356 {
12357 struct bp_location *loc = b->loc;
12358 int n = 0;
12359 for (; loc; loc = loc->next)
12360 ++n;
12361 printf_filtered (" (%d locations)", n);
12362 }
12363 }
12364}
12365
348d480f
PA
12366/* Default bp_location_ops methods. */
12367
12368static void
12369bp_location_dtor (struct bp_location *self)
12370{
348d480f
PA
12371 xfree (self->function_name);
12372}
12373
12374static const struct bp_location_ops bp_location_ops =
12375{
12376 bp_location_dtor
12377};
12378
c1fc2657 12379/* Destructor for the breakpoint base class. */
348d480f 12380
c1fc2657 12381breakpoint::~breakpoint ()
348d480f 12382{
c1fc2657
SM
12383 xfree (this->cond_string);
12384 xfree (this->extra_string);
12385 xfree (this->filter);
348d480f
PA
12386}
12387
2060206e
PA
12388static struct bp_location *
12389base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12390{
5625a286 12391 return new bp_location (&bp_location_ops, self);
348d480f
PA
12392}
12393
2060206e
PA
12394static void
12395base_breakpoint_re_set (struct breakpoint *b)
12396{
12397 /* Nothing to re-set. */
12398}
12399
12400#define internal_error_pure_virtual_called() \
12401 gdb_assert_not_reached ("pure virtual function called")
12402
12403static int
12404base_breakpoint_insert_location (struct bp_location *bl)
12405{
12406 internal_error_pure_virtual_called ();
12407}
12408
12409static int
73971819
PA
12410base_breakpoint_remove_location (struct bp_location *bl,
12411 enum remove_bp_reason reason)
2060206e
PA
12412{
12413 internal_error_pure_virtual_called ();
12414}
12415
12416static int
12417base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12418 const address_space *aspace,
09ac7c10
TT
12419 CORE_ADDR bp_addr,
12420 const struct target_waitstatus *ws)
2060206e
PA
12421{
12422 internal_error_pure_virtual_called ();
12423}
12424
12425static void
12426base_breakpoint_check_status (bpstat bs)
12427{
12428 /* Always stop. */
12429}
12430
12431/* A "works_in_software_mode" breakpoint_ops method that just internal
12432 errors. */
12433
12434static int
12435base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12436{
12437 internal_error_pure_virtual_called ();
12438}
12439
12440/* A "resources_needed" breakpoint_ops method that just internal
12441 errors. */
12442
12443static int
12444base_breakpoint_resources_needed (const struct bp_location *bl)
12445{
12446 internal_error_pure_virtual_called ();
12447}
12448
12449static enum print_stop_action
12450base_breakpoint_print_it (bpstat bs)
12451{
12452 internal_error_pure_virtual_called ();
12453}
12454
12455static void
12456base_breakpoint_print_one_detail (const struct breakpoint *self,
12457 struct ui_out *uiout)
12458{
12459 /* nothing */
12460}
12461
12462static void
12463base_breakpoint_print_mention (struct breakpoint *b)
12464{
12465 internal_error_pure_virtual_called ();
12466}
12467
12468static void
12469base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12470{
12471 internal_error_pure_virtual_called ();
12472}
12473
983af33b 12474static void
f00aae0f
KS
12475base_breakpoint_create_sals_from_location
12476 (const struct event_location *location,
12477 struct linespec_result *canonical,
12478 enum bptype type_wanted)
983af33b
SDJ
12479{
12480 internal_error_pure_virtual_called ();
12481}
12482
12483static void
12484base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12485 struct linespec_result *c,
e1e01040
PA
12486 gdb::unique_xmalloc_ptr<char> cond_string,
12487 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12488 enum bptype type_wanted,
12489 enum bpdisp disposition,
12490 int thread,
12491 int task, int ignore_count,
12492 const struct breakpoint_ops *o,
12493 int from_tty, int enabled,
44f238bb 12494 int internal, unsigned flags)
983af33b
SDJ
12495{
12496 internal_error_pure_virtual_called ();
12497}
12498
6c5b2ebe 12499static std::vector<symtab_and_line>
f00aae0f
KS
12500base_breakpoint_decode_location (struct breakpoint *b,
12501 const struct event_location *location,
6c5b2ebe 12502 struct program_space *search_pspace)
983af33b
SDJ
12503{
12504 internal_error_pure_virtual_called ();
12505}
12506
ab04a2af
TT
12507/* The default 'explains_signal' method. */
12508
47591c29 12509static int
427cd150 12510base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12511{
47591c29 12512 return 1;
ab04a2af
TT
12513}
12514
9d6e6e84
HZ
12515/* The default "after_condition_true" method. */
12516
12517static void
12518base_breakpoint_after_condition_true (struct bpstats *bs)
12519{
12520 /* Nothing to do. */
12521}
12522
ab04a2af 12523struct breakpoint_ops base_breakpoint_ops =
2060206e 12524{
2060206e
PA
12525 base_breakpoint_allocate_location,
12526 base_breakpoint_re_set,
12527 base_breakpoint_insert_location,
12528 base_breakpoint_remove_location,
12529 base_breakpoint_breakpoint_hit,
12530 base_breakpoint_check_status,
12531 base_breakpoint_resources_needed,
12532 base_breakpoint_works_in_software_mode,
12533 base_breakpoint_print_it,
12534 NULL,
12535 base_breakpoint_print_one_detail,
12536 base_breakpoint_print_mention,
983af33b 12537 base_breakpoint_print_recreate,
5f700d83 12538 base_breakpoint_create_sals_from_location,
983af33b 12539 base_breakpoint_create_breakpoints_sal,
5f700d83 12540 base_breakpoint_decode_location,
9d6e6e84
HZ
12541 base_breakpoint_explains_signal,
12542 base_breakpoint_after_condition_true,
2060206e
PA
12543};
12544
12545/* Default breakpoint_ops methods. */
12546
12547static void
348d480f
PA
12548bkpt_re_set (struct breakpoint *b)
12549{
06edf0c0 12550 /* FIXME: is this still reachable? */
9ef9e6a6 12551 if (breakpoint_event_location_empty_p (b))
06edf0c0 12552 {
f00aae0f 12553 /* Anything without a location can't be re-set. */
348d480f 12554 delete_breakpoint (b);
06edf0c0 12555 return;
348d480f 12556 }
06edf0c0
PA
12557
12558 breakpoint_re_set_default (b);
348d480f
PA
12559}
12560
2060206e 12561static int
348d480f
PA
12562bkpt_insert_location (struct bp_location *bl)
12563{
cd6c3b4f
YQ
12564 CORE_ADDR addr = bl->target_info.reqstd_address;
12565
579c6ad9 12566 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12567 bl->target_info.placed_address = addr;
12568
348d480f 12569 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12570 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12571 else
7c16b83e 12572 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12573}
12574
2060206e 12575static int
73971819 12576bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12577{
12578 if (bl->loc_type == bp_loc_hardware_breakpoint)
12579 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12580 else
73971819 12581 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12582}
12583
2060206e 12584static int
348d480f 12585bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12586 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12587 const struct target_waitstatus *ws)
348d480f 12588{
09ac7c10 12589 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12590 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12591 return 0;
12592
348d480f
PA
12593 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12594 aspace, bp_addr))
12595 return 0;
12596
12597 if (overlay_debugging /* unmapped overlay section */
12598 && section_is_overlay (bl->section)
12599 && !section_is_mapped (bl->section))
12600 return 0;
12601
12602 return 1;
12603}
12604
cd1608cc
PA
12605static int
12606dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12607 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12608 const struct target_waitstatus *ws)
12609{
12610 if (dprintf_style == dprintf_style_agent
12611 && target_can_run_breakpoint_commands ())
12612 {
12613 /* An agent-style dprintf never causes a stop. If we see a trap
12614 for this address it must be for a breakpoint that happens to
12615 be set at the same address. */
12616 return 0;
12617 }
12618
12619 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12620}
12621
2060206e 12622static int
348d480f
PA
12623bkpt_resources_needed (const struct bp_location *bl)
12624{
12625 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12626
12627 return 1;
12628}
12629
2060206e 12630static enum print_stop_action
348d480f
PA
12631bkpt_print_it (bpstat bs)
12632{
348d480f
PA
12633 struct breakpoint *b;
12634 const struct bp_location *bl;
001c8c33 12635 int bp_temp;
79a45e25 12636 struct ui_out *uiout = current_uiout;
348d480f
PA
12637
12638 gdb_assert (bs->bp_location_at != NULL);
12639
12640 bl = bs->bp_location_at;
12641 b = bs->breakpoint_at;
12642
001c8c33
PA
12643 bp_temp = b->disposition == disp_del;
12644 if (bl->address != bl->requested_address)
12645 breakpoint_adjustment_warning (bl->requested_address,
12646 bl->address,
12647 b->number, 1);
12648 annotate_breakpoint (b->number);
f303dbd6
PA
12649 maybe_print_thread_hit_breakpoint (uiout);
12650
001c8c33 12651 if (bp_temp)
112e8700 12652 uiout->text ("Temporary breakpoint ");
001c8c33 12653 else
112e8700
SM
12654 uiout->text ("Breakpoint ");
12655 if (uiout->is_mi_like_p ())
348d480f 12656 {
112e8700 12657 uiout->field_string ("reason",
001c8c33 12658 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12659 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12660 }
112e8700
SM
12661 uiout->field_int ("bkptno", b->number);
12662 uiout->text (", ");
06edf0c0 12663
001c8c33 12664 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12665}
12666
2060206e 12667static void
06edf0c0
PA
12668bkpt_print_mention (struct breakpoint *b)
12669{
112e8700 12670 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12671 return;
12672
12673 switch (b->type)
12674 {
12675 case bp_breakpoint:
12676 case bp_gnu_ifunc_resolver:
12677 if (b->disposition == disp_del)
12678 printf_filtered (_("Temporary breakpoint"));
12679 else
12680 printf_filtered (_("Breakpoint"));
12681 printf_filtered (_(" %d"), b->number);
12682 if (b->type == bp_gnu_ifunc_resolver)
12683 printf_filtered (_(" at gnu-indirect-function resolver"));
12684 break;
12685 case bp_hardware_breakpoint:
12686 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12687 break;
e7e0cddf
SS
12688 case bp_dprintf:
12689 printf_filtered (_("Dprintf %d"), b->number);
12690 break;
06edf0c0
PA
12691 }
12692
12693 say_where (b);
12694}
12695
2060206e 12696static void
06edf0c0
PA
12697bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12698{
12699 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12700 fprintf_unfiltered (fp, "tbreak");
12701 else if (tp->type == bp_breakpoint)
12702 fprintf_unfiltered (fp, "break");
12703 else if (tp->type == bp_hardware_breakpoint
12704 && tp->disposition == disp_del)
12705 fprintf_unfiltered (fp, "thbreak");
12706 else if (tp->type == bp_hardware_breakpoint)
12707 fprintf_unfiltered (fp, "hbreak");
12708 else
12709 internal_error (__FILE__, __LINE__,
12710 _("unhandled breakpoint type %d"), (int) tp->type);
12711
f00aae0f 12712 fprintf_unfiltered (fp, " %s",
d28cd78a 12713 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12714
12715 /* Print out extra_string if this breakpoint is pending. It might
12716 contain, for example, conditions that were set by the user. */
12717 if (tp->loc == NULL && tp->extra_string != NULL)
12718 fprintf_unfiltered (fp, " %s", tp->extra_string);
12719
dd11a36c 12720 print_recreate_thread (tp, fp);
06edf0c0
PA
12721}
12722
983af33b 12723static void
f00aae0f
KS
12724bkpt_create_sals_from_location (const struct event_location *location,
12725 struct linespec_result *canonical,
12726 enum bptype type_wanted)
983af33b 12727{
f00aae0f 12728 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12729}
12730
12731static void
12732bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12733 struct linespec_result *canonical,
e1e01040
PA
12734 gdb::unique_xmalloc_ptr<char> cond_string,
12735 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12736 enum bptype type_wanted,
12737 enum bpdisp disposition,
12738 int thread,
12739 int task, int ignore_count,
12740 const struct breakpoint_ops *ops,
12741 int from_tty, int enabled,
44f238bb 12742 int internal, unsigned flags)
983af33b 12743{
023fa29b 12744 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12745 std::move (cond_string),
12746 std::move (extra_string),
e7e0cddf 12747 type_wanted,
983af33b
SDJ
12748 disposition, thread, task,
12749 ignore_count, ops, from_tty,
44f238bb 12750 enabled, internal, flags);
983af33b
SDJ
12751}
12752
6c5b2ebe 12753static std::vector<symtab_and_line>
f00aae0f
KS
12754bkpt_decode_location (struct breakpoint *b,
12755 const struct event_location *location,
6c5b2ebe 12756 struct program_space *search_pspace)
983af33b 12757{
6c5b2ebe 12758 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12759}
12760
06edf0c0
PA
12761/* Virtual table for internal breakpoints. */
12762
12763static void
12764internal_bkpt_re_set (struct breakpoint *b)
12765{
12766 switch (b->type)
12767 {
12768 /* Delete overlay event and longjmp master breakpoints; they
12769 will be reset later by breakpoint_re_set. */
12770 case bp_overlay_event:
12771 case bp_longjmp_master:
12772 case bp_std_terminate_master:
12773 case bp_exception_master:
12774 delete_breakpoint (b);
12775 break;
12776
12777 /* This breakpoint is special, it's set up when the inferior
12778 starts and we really don't want to touch it. */
12779 case bp_shlib_event:
12780
12781 /* Like bp_shlib_event, this breakpoint type is special. Once
12782 it is set up, we do not want to touch it. */
12783 case bp_thread_event:
12784 break;
12785 }
12786}
12787
12788static void
12789internal_bkpt_check_status (bpstat bs)
12790{
a9b3a50f
PA
12791 if (bs->breakpoint_at->type == bp_shlib_event)
12792 {
12793 /* If requested, stop when the dynamic linker notifies GDB of
12794 events. This allows the user to get control and place
12795 breakpoints in initializer routines for dynamically loaded
12796 objects (among other things). */
12797 bs->stop = stop_on_solib_events;
12798 bs->print = stop_on_solib_events;
12799 }
12800 else
12801 bs->stop = 0;
06edf0c0
PA
12802}
12803
12804static enum print_stop_action
12805internal_bkpt_print_it (bpstat bs)
12806{
06edf0c0 12807 struct breakpoint *b;
06edf0c0 12808
06edf0c0
PA
12809 b = bs->breakpoint_at;
12810
06edf0c0
PA
12811 switch (b->type)
12812 {
348d480f
PA
12813 case bp_shlib_event:
12814 /* Did we stop because the user set the stop_on_solib_events
12815 variable? (If so, we report this as a generic, "Stopped due
12816 to shlib event" message.) */
edcc5120 12817 print_solib_event (0);
348d480f
PA
12818 break;
12819
12820 case bp_thread_event:
12821 /* Not sure how we will get here.
12822 GDB should not stop for these breakpoints. */
12823 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12824 break;
12825
12826 case bp_overlay_event:
12827 /* By analogy with the thread event, GDB should not stop for these. */
12828 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12829 break;
12830
12831 case bp_longjmp_master:
12832 /* These should never be enabled. */
12833 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12834 break;
12835
12836 case bp_std_terminate_master:
12837 /* These should never be enabled. */
12838 printf_filtered (_("std::terminate Master Breakpoint: "
12839 "gdb should not stop!\n"));
348d480f
PA
12840 break;
12841
12842 case bp_exception_master:
12843 /* These should never be enabled. */
12844 printf_filtered (_("Exception Master Breakpoint: "
12845 "gdb should not stop!\n"));
06edf0c0
PA
12846 break;
12847 }
12848
001c8c33 12849 return PRINT_NOTHING;
06edf0c0
PA
12850}
12851
12852static void
12853internal_bkpt_print_mention (struct breakpoint *b)
12854{
12855 /* Nothing to mention. These breakpoints are internal. */
12856}
12857
06edf0c0
PA
12858/* Virtual table for momentary breakpoints */
12859
12860static void
12861momentary_bkpt_re_set (struct breakpoint *b)
12862{
12863 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12864 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12865 Otherwise these should have been blown away via the cleanup chain
12866 or by breakpoint_init_inferior when we rerun the executable. */
12867}
12868
12869static void
12870momentary_bkpt_check_status (bpstat bs)
12871{
12872 /* Nothing. The point of these breakpoints is causing a stop. */
12873}
12874
12875static enum print_stop_action
12876momentary_bkpt_print_it (bpstat bs)
12877{
001c8c33 12878 return PRINT_UNKNOWN;
348d480f
PA
12879}
12880
06edf0c0
PA
12881static void
12882momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12883{
06edf0c0 12884 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12885}
12886
e2e4d78b
JK
12887/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12888
12889 It gets cleared already on the removal of the first one of such placed
12890 breakpoints. This is OK as they get all removed altogether. */
12891
c1fc2657 12892longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12893{
c1fc2657 12894 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12895
c1fc2657 12896 if (tp != NULL)
e2e4d78b 12897 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12898}
12899
55aa24fb
SDJ
12900/* Specific methods for probe breakpoints. */
12901
12902static int
12903bkpt_probe_insert_location (struct bp_location *bl)
12904{
12905 int v = bkpt_insert_location (bl);
12906
12907 if (v == 0)
12908 {
12909 /* The insertion was successful, now let's set the probe's semaphore
12910 if needed. */
0ea5cda8
SDJ
12911 if (bl->probe.probe->pops->set_semaphore != NULL)
12912 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12913 bl->probe.objfile,
12914 bl->gdbarch);
55aa24fb
SDJ
12915 }
12916
12917 return v;
12918}
12919
12920static int
73971819
PA
12921bkpt_probe_remove_location (struct bp_location *bl,
12922 enum remove_bp_reason reason)
55aa24fb
SDJ
12923{
12924 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12925 if (bl->probe.probe->pops->clear_semaphore != NULL)
12926 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12927 bl->probe.objfile,
12928 bl->gdbarch);
55aa24fb 12929
73971819 12930 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12931}
12932
12933static void
f00aae0f 12934bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12935 struct linespec_result *canonical,
f00aae0f 12936 enum bptype type_wanted)
55aa24fb
SDJ
12937{
12938 struct linespec_sals lsal;
12939
c2f4122d 12940 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12941 lsal.canonical
12942 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12943 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12944}
12945
6c5b2ebe 12946static std::vector<symtab_and_line>
f00aae0f
KS
12947bkpt_probe_decode_location (struct breakpoint *b,
12948 const struct event_location *location,
6c5b2ebe 12949 struct program_space *search_pspace)
55aa24fb 12950{
6c5b2ebe
PA
12951 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12952 if (sals.empty ())
55aa24fb 12953 error (_("probe not found"));
6c5b2ebe 12954 return sals;
55aa24fb
SDJ
12955}
12956
348d480f 12957/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12958
348d480f
PA
12959static void
12960tracepoint_re_set (struct breakpoint *b)
12961{
12962 breakpoint_re_set_default (b);
12963}
876fa593 12964
348d480f
PA
12965static int
12966tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12967 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12968 const struct target_waitstatus *ws)
348d480f
PA
12969{
12970 /* By definition, the inferior does not report stops at
12971 tracepoints. */
12972 return 0;
74960c60
VP
12973}
12974
12975static void
348d480f
PA
12976tracepoint_print_one_detail (const struct breakpoint *self,
12977 struct ui_out *uiout)
74960c60 12978{
d9b3f62e
PA
12979 struct tracepoint *tp = (struct tracepoint *) self;
12980 if (tp->static_trace_marker_id)
348d480f
PA
12981 {
12982 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12983
112e8700
SM
12984 uiout->text ("\tmarker id is ");
12985 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12986 tp->static_trace_marker_id);
112e8700 12987 uiout->text ("\n");
348d480f 12988 }
0d381245
VP
12989}
12990
a474d7c2 12991static void
348d480f 12992tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12993{
112e8700 12994 if (current_uiout->is_mi_like_p ())
348d480f 12995 return;
cc59ec59 12996
348d480f
PA
12997 switch (b->type)
12998 {
12999 case bp_tracepoint:
13000 printf_filtered (_("Tracepoint"));
13001 printf_filtered (_(" %d"), b->number);
13002 break;
13003 case bp_fast_tracepoint:
13004 printf_filtered (_("Fast tracepoint"));
13005 printf_filtered (_(" %d"), b->number);
13006 break;
13007 case bp_static_tracepoint:
13008 printf_filtered (_("Static tracepoint"));
13009 printf_filtered (_(" %d"), b->number);
13010 break;
13011 default:
13012 internal_error (__FILE__, __LINE__,
13013 _("unhandled tracepoint type %d"), (int) b->type);
13014 }
13015
13016 say_where (b);
a474d7c2
PA
13017}
13018
348d480f 13019static void
d9b3f62e 13020tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13021{
d9b3f62e
PA
13022 struct tracepoint *tp = (struct tracepoint *) self;
13023
13024 if (self->type == bp_fast_tracepoint)
348d480f 13025 fprintf_unfiltered (fp, "ftrace");
c93e8391 13026 else if (self->type == bp_static_tracepoint)
348d480f 13027 fprintf_unfiltered (fp, "strace");
d9b3f62e 13028 else if (self->type == bp_tracepoint)
348d480f
PA
13029 fprintf_unfiltered (fp, "trace");
13030 else
13031 internal_error (__FILE__, __LINE__,
d9b3f62e 13032 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13033
f00aae0f 13034 fprintf_unfiltered (fp, " %s",
d28cd78a 13035 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13036 print_recreate_thread (self, fp);
13037
13038 if (tp->pass_count)
13039 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13040}
13041
983af33b 13042static void
f00aae0f
KS
13043tracepoint_create_sals_from_location (const struct event_location *location,
13044 struct linespec_result *canonical,
13045 enum bptype type_wanted)
983af33b 13046{
f00aae0f 13047 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13048}
13049
13050static void
13051tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13052 struct linespec_result *canonical,
e1e01040
PA
13053 gdb::unique_xmalloc_ptr<char> cond_string,
13054 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13055 enum bptype type_wanted,
13056 enum bpdisp disposition,
13057 int thread,
13058 int task, int ignore_count,
13059 const struct breakpoint_ops *ops,
13060 int from_tty, int enabled,
44f238bb 13061 int internal, unsigned flags)
983af33b 13062{
023fa29b 13063 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13064 std::move (cond_string),
13065 std::move (extra_string),
e7e0cddf 13066 type_wanted,
983af33b
SDJ
13067 disposition, thread, task,
13068 ignore_count, ops, from_tty,
44f238bb 13069 enabled, internal, flags);
983af33b
SDJ
13070}
13071
6c5b2ebe 13072static std::vector<symtab_and_line>
f00aae0f
KS
13073tracepoint_decode_location (struct breakpoint *b,
13074 const struct event_location *location,
6c5b2ebe 13075 struct program_space *search_pspace)
983af33b 13076{
6c5b2ebe 13077 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13078}
13079
2060206e 13080struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13081
55aa24fb
SDJ
13082/* The breakpoint_ops structure to be use on tracepoints placed in a
13083 static probe. */
13084
13085static void
f00aae0f
KS
13086tracepoint_probe_create_sals_from_location
13087 (const struct event_location *location,
13088 struct linespec_result *canonical,
13089 enum bptype type_wanted)
55aa24fb
SDJ
13090{
13091 /* We use the same method for breakpoint on probes. */
f00aae0f 13092 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13093}
13094
6c5b2ebe 13095static std::vector<symtab_and_line>
f00aae0f
KS
13096tracepoint_probe_decode_location (struct breakpoint *b,
13097 const struct event_location *location,
6c5b2ebe 13098 struct program_space *search_pspace)
55aa24fb
SDJ
13099{
13100 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13101 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13102}
13103
13104static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13105
5c2b4418
HZ
13106/* Dprintf breakpoint_ops methods. */
13107
13108static void
13109dprintf_re_set (struct breakpoint *b)
13110{
13111 breakpoint_re_set_default (b);
13112
f00aae0f
KS
13113 /* extra_string should never be non-NULL for dprintf. */
13114 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13115
13116 /* 1 - connect to target 1, that can run breakpoint commands.
13117 2 - create a dprintf, which resolves fine.
13118 3 - disconnect from target 1
13119 4 - connect to target 2, that can NOT run breakpoint commands.
13120
13121 After steps #3/#4, you'll want the dprintf command list to
13122 be updated, because target 1 and 2 may well return different
13123 answers for target_can_run_breakpoint_commands().
13124 Given absence of finer grained resetting, we get to do
13125 it all the time. */
13126 if (b->extra_string != NULL)
13127 update_dprintf_command_list (b);
13128}
13129
2d9442cc
HZ
13130/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13131
13132static void
13133dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13134{
f00aae0f 13135 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13136 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13137 tp->extra_string);
13138 print_recreate_thread (tp, fp);
13139}
13140
9d6e6e84
HZ
13141/* Implement the "after_condition_true" breakpoint_ops method for
13142 dprintf.
13143
13144 dprintf's are implemented with regular commands in their command
13145 list, but we run the commands here instead of before presenting the
13146 stop to the user, as dprintf's don't actually cause a stop. This
13147 also makes it so that the commands of multiple dprintfs at the same
13148 address are all handled. */
13149
13150static void
13151dprintf_after_condition_true (struct bpstats *bs)
13152{
04afa70c 13153 struct bpstats tmp_bs;
9d6e6e84
HZ
13154 struct bpstats *tmp_bs_p = &tmp_bs;
13155
13156 /* dprintf's never cause a stop. This wasn't set in the
13157 check_status hook instead because that would make the dprintf's
13158 condition not be evaluated. */
13159 bs->stop = 0;
13160
13161 /* Run the command list here. Take ownership of it instead of
13162 copying. We never want these commands to run later in
13163 bpstat_do_actions, if a breakpoint that causes a stop happens to
13164 be set at same address as this dprintf, or even if running the
13165 commands here throws. */
13166 tmp_bs.commands = bs->commands;
13167 bs->commands = NULL;
9d6e6e84
HZ
13168
13169 bpstat_do_actions_1 (&tmp_bs_p);
13170
13171 /* 'tmp_bs.commands' will usually be NULL by now, but
13172 bpstat_do_actions_1 may return early without processing the whole
13173 list. */
9d6e6e84
HZ
13174}
13175
983af33b
SDJ
13176/* The breakpoint_ops structure to be used on static tracepoints with
13177 markers (`-m'). */
13178
13179static void
f00aae0f 13180strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13181 struct linespec_result *canonical,
f00aae0f 13182 enum bptype type_wanted)
983af33b
SDJ
13183{
13184 struct linespec_sals lsal;
f00aae0f 13185 const char *arg_start, *arg;
983af33b 13186
f00aae0f
KS
13187 arg = arg_start = get_linespec_location (location);
13188 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13189
f2fc3015
TT
13190 std::string str (arg_start, arg - arg_start);
13191 const char *ptr = str.c_str ();
13192 canonical->location = new_linespec_location (&ptr);
983af33b 13193
8e9e35b1
TT
13194 lsal.canonical
13195 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13196 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13197}
13198
13199static void
13200strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13201 struct linespec_result *canonical,
e1e01040
PA
13202 gdb::unique_xmalloc_ptr<char> cond_string,
13203 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13204 enum bptype type_wanted,
13205 enum bpdisp disposition,
13206 int thread,
13207 int task, int ignore_count,
13208 const struct breakpoint_ops *ops,
13209 int from_tty, int enabled,
44f238bb 13210 int internal, unsigned flags)
983af33b 13211{
6c5b2ebe 13212 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13213
13214 /* If the user is creating a static tracepoint by marker id
13215 (strace -m MARKER_ID), then store the sals index, so that
13216 breakpoint_re_set can try to match up which of the newly
13217 found markers corresponds to this one, and, don't try to
13218 expand multiple locations for each sal, given than SALS
13219 already should contain all sals for MARKER_ID. */
13220
6c5b2ebe 13221 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13222 {
6c5b2ebe
PA
13223 event_location_up location
13224 = copy_event_location (canonical->location.get ());
983af33b 13225
b270e6f9 13226 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13227 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13228 std::move (location), NULL,
e1e01040
PA
13229 std::move (cond_string),
13230 std::move (extra_string),
e7e0cddf 13231 type_wanted, disposition,
983af33b 13232 thread, task, ignore_count, ops,
44f238bb 13233 from_tty, enabled, internal, flags,
983af33b
SDJ
13234 canonical->special_display);
13235 /* Given that its possible to have multiple markers with
13236 the same string id, if the user is creating a static
13237 tracepoint by marker id ("strace -m MARKER_ID"), then
13238 store the sals index, so that breakpoint_re_set can
13239 try to match up which of the newly found markers
13240 corresponds to this one */
13241 tp->static_trace_marker_id_idx = i;
13242
b270e6f9 13243 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13244 }
13245}
13246
6c5b2ebe 13247static std::vector<symtab_and_line>
f00aae0f
KS
13248strace_marker_decode_location (struct breakpoint *b,
13249 const struct event_location *location,
6c5b2ebe 13250 struct program_space *search_pspace)
983af33b
SDJ
13251{
13252 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13253 const char *s = get_linespec_location (location);
983af33b 13254
6c5b2ebe
PA
13255 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13256 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13257 {
6c5b2ebe
PA
13258 sals[0] = sals[tp->static_trace_marker_id_idx];
13259 sals.resize (1);
13260 return sals;
983af33b
SDJ
13261 }
13262 else
13263 error (_("marker %s not found"), tp->static_trace_marker_id);
13264}
13265
13266static struct breakpoint_ops strace_marker_breakpoint_ops;
13267
13268static int
13269strace_marker_p (struct breakpoint *b)
13270{
13271 return b->ops == &strace_marker_breakpoint_ops;
13272}
13273
53a5351d 13274/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13275 structures. */
c906108c
SS
13276
13277void
fba45db2 13278delete_breakpoint (struct breakpoint *bpt)
c906108c 13279{
52f0bd74 13280 struct breakpoint *b;
c906108c 13281
8a3fe4f8 13282 gdb_assert (bpt != NULL);
c906108c 13283
4a64f543
MS
13284 /* Has this bp already been deleted? This can happen because
13285 multiple lists can hold pointers to bp's. bpstat lists are
13286 especial culprits.
13287
13288 One example of this happening is a watchpoint's scope bp. When
13289 the scope bp triggers, we notice that the watchpoint is out of
13290 scope, and delete it. We also delete its scope bp. But the
13291 scope bp is marked "auto-deleting", and is already on a bpstat.
13292 That bpstat is then checked for auto-deleting bp's, which are
13293 deleted.
13294
13295 A real solution to this problem might involve reference counts in
13296 bp's, and/or giving them pointers back to their referencing
13297 bpstat's, and teaching delete_breakpoint to only free a bp's
13298 storage when no more references were extent. A cheaper bandaid
13299 was chosen. */
c906108c
SS
13300 if (bpt->type == bp_none)
13301 return;
13302
4a64f543
MS
13303 /* At least avoid this stale reference until the reference counting
13304 of breakpoints gets resolved. */
d0fb5eae 13305 if (bpt->related_breakpoint != bpt)
e5a0a904 13306 {
d0fb5eae 13307 struct breakpoint *related;
3a5c3e22 13308 struct watchpoint *w;
d0fb5eae
JK
13309
13310 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13311 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13312 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13313 w = (struct watchpoint *) bpt;
13314 else
13315 w = NULL;
13316 if (w != NULL)
13317 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13318
13319 /* Unlink bpt from the bpt->related_breakpoint ring. */
13320 for (related = bpt; related->related_breakpoint != bpt;
13321 related = related->related_breakpoint);
13322 related->related_breakpoint = bpt->related_breakpoint;
13323 bpt->related_breakpoint = bpt;
e5a0a904
JK
13324 }
13325
a9634178
TJB
13326 /* watch_command_1 creates a watchpoint but only sets its number if
13327 update_watchpoint succeeds in creating its bp_locations. If there's
13328 a problem in that process, we'll be asked to delete the half-created
13329 watchpoint. In that case, don't announce the deletion. */
13330 if (bpt->number)
13331 observer_notify_breakpoint_deleted (bpt);
c906108c 13332
c906108c
SS
13333 if (breakpoint_chain == bpt)
13334 breakpoint_chain = bpt->next;
13335
c906108c
SS
13336 ALL_BREAKPOINTS (b)
13337 if (b->next == bpt)
c5aa993b
JM
13338 {
13339 b->next = bpt->next;
13340 break;
13341 }
c906108c 13342
f431efe5
PA
13343 /* Be sure no bpstat's are pointing at the breakpoint after it's
13344 been freed. */
13345 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13346 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13347 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13348 commands are associated with the bpstat; if we remove it here,
13349 then the later call to bpstat_do_actions (&stop_bpstat); in
13350 event-top.c won't do anything, and temporary breakpoints with
13351 commands won't work. */
13352
13353 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13354
4a64f543
MS
13355 /* Now that breakpoint is removed from breakpoint list, update the
13356 global location list. This will remove locations that used to
13357 belong to this breakpoint. Do this before freeing the breakpoint
13358 itself, since remove_breakpoint looks at location's owner. It
13359 might be better design to have location completely
13360 self-contained, but it's not the case now. */
44702360 13361 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13362
4a64f543
MS
13363 /* On the chance that someone will soon try again to delete this
13364 same bp, we mark it as deleted before freeing its storage. */
c906108c 13365 bpt->type = bp_none;
4d01a485 13366 delete bpt;
c906108c
SS
13367}
13368
4d6140d9
AC
13369static void
13370do_delete_breakpoint_cleanup (void *b)
13371{
9a3c8263 13372 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13373}
13374
13375struct cleanup *
13376make_cleanup_delete_breakpoint (struct breakpoint *b)
13377{
13378 return make_cleanup (do_delete_breakpoint_cleanup, b);
13379}
13380
51be5b68
PA
13381/* Iterator function to call a user-provided callback function once
13382 for each of B and its related breakpoints. */
13383
13384static void
13385iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13386 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13387{
13388 struct breakpoint *related;
13389
13390 related = b;
13391 do
13392 {
13393 struct breakpoint *next;
13394
13395 /* FUNCTION may delete RELATED. */
13396 next = related->related_breakpoint;
13397
13398 if (next == related)
13399 {
13400 /* RELATED is the last ring entry. */
48649e1b 13401 function (related);
51be5b68
PA
13402
13403 /* FUNCTION may have deleted it, so we'd never reach back to
13404 B. There's nothing left to do anyway, so just break
13405 out. */
13406 break;
13407 }
13408 else
48649e1b 13409 function (related);
51be5b68
PA
13410
13411 related = next;
13412 }
13413 while (related != b);
13414}
95a42b64 13415
4495129a 13416static void
981a3fb3 13417delete_command (const char *arg, int from_tty)
c906108c 13418{
35df4500 13419 struct breakpoint *b, *b_tmp;
c906108c 13420
ea9365bb
TT
13421 dont_repeat ();
13422
c906108c
SS
13423 if (arg == 0)
13424 {
13425 int breaks_to_delete = 0;
13426
46c6471b
PA
13427 /* Delete all breakpoints if no argument. Do not delete
13428 internal breakpoints, these have to be deleted with an
13429 explicit breakpoint number argument. */
c5aa993b 13430 ALL_BREAKPOINTS (b)
46c6471b 13431 if (user_breakpoint_p (b))
973d738b
DJ
13432 {
13433 breaks_to_delete = 1;
13434 break;
13435 }
c906108c
SS
13436
13437 /* Ask user only if there are some breakpoints to delete. */
13438 if (!from_tty
e2e0b3e5 13439 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13440 {
35df4500 13441 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13442 if (user_breakpoint_p (b))
c5aa993b 13443 delete_breakpoint (b);
c906108c
SS
13444 }
13445 }
13446 else
48649e1b
TT
13447 map_breakpoint_numbers
13448 (arg, [&] (breakpoint *b)
13449 {
13450 iterate_over_related_breakpoints (b, delete_breakpoint);
13451 });
c906108c
SS
13452}
13453
c2f4122d
PA
13454/* Return true if all locations of B bound to PSPACE are pending. If
13455 PSPACE is NULL, all locations of all program spaces are
13456 considered. */
13457
0d381245 13458static int
c2f4122d 13459all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13460{
c2f4122d
PA
13461 struct bp_location *loc;
13462
13463 for (loc = b->loc; loc != NULL; loc = loc->next)
13464 if ((pspace == NULL
13465 || loc->pspace == pspace)
13466 && !loc->shlib_disabled
8645ff69 13467 && !loc->pspace->executing_startup)
0d381245
VP
13468 return 0;
13469 return 1;
fe3f5fa8
VP
13470}
13471
776592bf
DE
13472/* Subroutine of update_breakpoint_locations to simplify it.
13473 Return non-zero if multiple fns in list LOC have the same name.
13474 Null names are ignored. */
13475
13476static int
13477ambiguous_names_p (struct bp_location *loc)
13478{
13479 struct bp_location *l;
13480 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13481 (int (*) (const void *,
13482 const void *)) streq,
776592bf
DE
13483 NULL, xcalloc, xfree);
13484
13485 for (l = loc; l != NULL; l = l->next)
13486 {
13487 const char **slot;
13488 const char *name = l->function_name;
13489
13490 /* Allow for some names to be NULL, ignore them. */
13491 if (name == NULL)
13492 continue;
13493
13494 slot = (const char **) htab_find_slot (htab, (const void *) name,
13495 INSERT);
4a64f543
MS
13496 /* NOTE: We can assume slot != NULL here because xcalloc never
13497 returns NULL. */
776592bf
DE
13498 if (*slot != NULL)
13499 {
13500 htab_delete (htab);
13501 return 1;
13502 }
13503 *slot = name;
13504 }
13505
13506 htab_delete (htab);
13507 return 0;
13508}
13509
0fb4aa4b
PA
13510/* When symbols change, it probably means the sources changed as well,
13511 and it might mean the static tracepoint markers are no longer at
13512 the same address or line numbers they used to be at last we
13513 checked. Losing your static tracepoints whenever you rebuild is
13514 undesirable. This function tries to resync/rematch gdb static
13515 tracepoints with the markers on the target, for static tracepoints
13516 that have not been set by marker id. Static tracepoint that have
13517 been set by marker id are reset by marker id in breakpoint_re_set.
13518 The heuristic is:
13519
13520 1) For a tracepoint set at a specific address, look for a marker at
13521 the old PC. If one is found there, assume to be the same marker.
13522 If the name / string id of the marker found is different from the
13523 previous known name, assume that means the user renamed the marker
13524 in the sources, and output a warning.
13525
13526 2) For a tracepoint set at a given line number, look for a marker
13527 at the new address of the old line number. If one is found there,
13528 assume to be the same marker. If the name / string id of the
13529 marker found is different from the previous known name, assume that
13530 means the user renamed the marker in the sources, and output a
13531 warning.
13532
13533 3) If a marker is no longer found at the same address or line, it
13534 may mean the marker no longer exists. But it may also just mean
13535 the code changed a bit. Maybe the user added a few lines of code
13536 that made the marker move up or down (in line number terms). Ask
13537 the target for info about the marker with the string id as we knew
13538 it. If found, update line number and address in the matching
13539 static tracepoint. This will get confused if there's more than one
13540 marker with the same ID (possible in UST, although unadvised
13541 precisely because it confuses tools). */
13542
13543static struct symtab_and_line
13544update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13545{
d9b3f62e 13546 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13547 struct static_tracepoint_marker marker;
13548 CORE_ADDR pc;
0fb4aa4b
PA
13549
13550 pc = sal.pc;
13551 if (sal.line)
13552 find_line_pc (sal.symtab, sal.line, &pc);
13553
13554 if (target_static_tracepoint_marker_at (pc, &marker))
13555 {
d9b3f62e 13556 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13557 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13558 b->number,
d9b3f62e 13559 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13560
d9b3f62e
PA
13561 xfree (tp->static_trace_marker_id);
13562 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13563 release_static_tracepoint_marker (&marker);
13564
13565 return sal;
13566 }
13567
13568 /* Old marker wasn't found on target at lineno. Try looking it up
13569 by string ID. */
13570 if (!sal.explicit_pc
13571 && sal.line != 0
13572 && sal.symtab != NULL
d9b3f62e 13573 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13574 {
13575 VEC(static_tracepoint_marker_p) *markers;
13576
13577 markers
d9b3f62e 13578 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13579
13580 if (!VEC_empty(static_tracepoint_marker_p, markers))
13581 {
0fb4aa4b 13582 struct symbol *sym;
80e1d417 13583 struct static_tracepoint_marker *tpmarker;
79a45e25 13584 struct ui_out *uiout = current_uiout;
67994074 13585 struct explicit_location explicit_loc;
0fb4aa4b 13586
80e1d417 13587 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13588
d9b3f62e 13589 xfree (tp->static_trace_marker_id);
80e1d417 13590 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13591
13592 warning (_("marker for static tracepoint %d (%s) not "
13593 "found at previous line number"),
d9b3f62e 13594 b->number, tp->static_trace_marker_id);
0fb4aa4b 13595
51abb421 13596 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13597 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13598 uiout->text ("Now in ");
0fb4aa4b
PA
13599 if (sym)
13600 {
112e8700
SM
13601 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13602 uiout->text (" at ");
0fb4aa4b 13603 }
112e8700 13604 uiout->field_string ("file",
05cba821 13605 symtab_to_filename_for_display (sal2.symtab));
112e8700 13606 uiout->text (":");
0fb4aa4b 13607
112e8700 13608 if (uiout->is_mi_like_p ())
0fb4aa4b 13609 {
0b0865da 13610 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13611
112e8700 13612 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13613 }
13614
112e8700
SM
13615 uiout->field_int ("line", sal2.line);
13616 uiout->text ("\n");
0fb4aa4b 13617
80e1d417 13618 b->loc->line_number = sal2.line;
2f202fde 13619 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13620
d28cd78a 13621 b->location.reset (NULL);
67994074
KS
13622 initialize_explicit_location (&explicit_loc);
13623 explicit_loc.source_filename
00e52e53 13624 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13625 explicit_loc.line_offset.offset = b->loc->line_number;
13626 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13627 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13628
13629 /* Might be nice to check if function changed, and warn if
13630 so. */
13631
80e1d417 13632 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13633 }
13634 }
13635 return sal;
13636}
13637
8d3788bd
VP
13638/* Returns 1 iff locations A and B are sufficiently same that
13639 we don't need to report breakpoint as changed. */
13640
13641static int
13642locations_are_equal (struct bp_location *a, struct bp_location *b)
13643{
13644 while (a && b)
13645 {
13646 if (a->address != b->address)
13647 return 0;
13648
13649 if (a->shlib_disabled != b->shlib_disabled)
13650 return 0;
13651
13652 if (a->enabled != b->enabled)
13653 return 0;
13654
13655 a = a->next;
13656 b = b->next;
13657 }
13658
13659 if ((a == NULL) != (b == NULL))
13660 return 0;
13661
13662 return 1;
13663}
13664
c2f4122d
PA
13665/* Split all locations of B that are bound to PSPACE out of B's
13666 location list to a separate list and return that list's head. If
13667 PSPACE is NULL, hoist out all locations of B. */
13668
13669static struct bp_location *
13670hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13671{
13672 struct bp_location head;
13673 struct bp_location *i = b->loc;
13674 struct bp_location **i_link = &b->loc;
13675 struct bp_location *hoisted = &head;
13676
13677 if (pspace == NULL)
13678 {
13679 i = b->loc;
13680 b->loc = NULL;
13681 return i;
13682 }
13683
13684 head.next = NULL;
13685
13686 while (i != NULL)
13687 {
13688 if (i->pspace == pspace)
13689 {
13690 *i_link = i->next;
13691 i->next = NULL;
13692 hoisted->next = i;
13693 hoisted = i;
13694 }
13695 else
13696 i_link = &i->next;
13697 i = *i_link;
13698 }
13699
13700 return head.next;
13701}
13702
13703/* Create new breakpoint locations for B (a hardware or software
13704 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13705 zero, then B is a ranged breakpoint. Only recreates locations for
13706 FILTER_PSPACE. Locations of other program spaces are left
13707 untouched. */
f1310107 13708
0e30163f 13709void
0d381245 13710update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13711 struct program_space *filter_pspace,
6c5b2ebe
PA
13712 gdb::array_view<const symtab_and_line> sals,
13713 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13714{
13715 int i;
c2f4122d 13716 struct bp_location *existing_locations;
0d381245 13717
6c5b2ebe 13718 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13719 {
13720 /* Ranged breakpoints have only one start location and one end
13721 location. */
13722 b->enable_state = bp_disabled;
f8eba3c6
TT
13723 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13724 "multiple locations found\n"),
13725 b->number);
13726 return;
13727 }
f1310107 13728
4a64f543
MS
13729 /* If there's no new locations, and all existing locations are
13730 pending, don't do anything. This optimizes the common case where
13731 all locations are in the same shared library, that was unloaded.
13732 We'd like to retain the location, so that when the library is
13733 loaded again, we don't loose the enabled/disabled status of the
13734 individual locations. */
6c5b2ebe 13735 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13736 return;
13737
c2f4122d 13738 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13739
6c5b2ebe 13740 for (const auto &sal : sals)
fe3f5fa8 13741 {
f8eba3c6
TT
13742 struct bp_location *new_loc;
13743
6c5b2ebe 13744 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13745
6c5b2ebe 13746 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13747
0d381245
VP
13748 /* Reparse conditions, they might contain references to the
13749 old symtab. */
13750 if (b->cond_string != NULL)
13751 {
bbc13ae3 13752 const char *s;
fe3f5fa8 13753
0d381245 13754 s = b->cond_string;
492d29ea 13755 TRY
0d381245 13756 {
6c5b2ebe
PA
13757 new_loc->cond = parse_exp_1 (&s, sal.pc,
13758 block_for_pc (sal.pc),
0d381245
VP
13759 0);
13760 }
492d29ea 13761 CATCH (e, RETURN_MASK_ERROR)
0d381245 13762 {
3e43a32a
MS
13763 warning (_("failed to reevaluate condition "
13764 "for breakpoint %d: %s"),
0d381245
VP
13765 b->number, e.message);
13766 new_loc->enabled = 0;
13767 }
492d29ea 13768 END_CATCH
0d381245 13769 }
fe3f5fa8 13770
6c5b2ebe 13771 if (!sals_end.empty ())
f1310107 13772 {
6c5b2ebe 13773 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13774
6c5b2ebe 13775 new_loc->length = end - sals[0].pc + 1;
f1310107 13776 }
0d381245 13777 }
fe3f5fa8 13778
4a64f543
MS
13779 /* If possible, carry over 'disable' status from existing
13780 breakpoints. */
0d381245
VP
13781 {
13782 struct bp_location *e = existing_locations;
776592bf
DE
13783 /* If there are multiple breakpoints with the same function name,
13784 e.g. for inline functions, comparing function names won't work.
13785 Instead compare pc addresses; this is just a heuristic as things
13786 may have moved, but in practice it gives the correct answer
13787 often enough until a better solution is found. */
13788 int have_ambiguous_names = ambiguous_names_p (b->loc);
13789
0d381245
VP
13790 for (; e; e = e->next)
13791 {
13792 if (!e->enabled && e->function_name)
13793 {
13794 struct bp_location *l = b->loc;
776592bf
DE
13795 if (have_ambiguous_names)
13796 {
13797 for (; l; l = l->next)
f1310107 13798 if (breakpoint_locations_match (e, l))
776592bf
DE
13799 {
13800 l->enabled = 0;
13801 break;
13802 }
13803 }
13804 else
13805 {
13806 for (; l; l = l->next)
13807 if (l->function_name
13808 && strcmp (e->function_name, l->function_name) == 0)
13809 {
13810 l->enabled = 0;
13811 break;
13812 }
13813 }
0d381245
VP
13814 }
13815 }
13816 }
fe3f5fa8 13817
8d3788bd
VP
13818 if (!locations_are_equal (existing_locations, b->loc))
13819 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13820}
13821
f00aae0f 13822/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13823 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13824
6c5b2ebe 13825static std::vector<symtab_and_line>
f00aae0f 13826location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13827 struct program_space *search_pspace, int *found)
ef23e705 13828{
492d29ea 13829 struct gdb_exception exception = exception_none;
ef23e705 13830
983af33b 13831 gdb_assert (b->ops != NULL);
ef23e705 13832
6c5b2ebe
PA
13833 std::vector<symtab_and_line> sals;
13834
492d29ea 13835 TRY
ef23e705 13836 {
6c5b2ebe 13837 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13838 }
492d29ea 13839 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13840 {
13841 int not_found_and_ok = 0;
492d29ea
PA
13842
13843 exception = e;
13844
ef23e705
TJB
13845 /* For pending breakpoints, it's expected that parsing will
13846 fail until the right shared library is loaded. User has
13847 already told to create pending breakpoints and don't need
13848 extra messages. If breakpoint is in bp_shlib_disabled
13849 state, then user already saw the message about that
13850 breakpoint being disabled, and don't want to see more
13851 errors. */
58438ac1 13852 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13853 && (b->condition_not_parsed
13854 || (b->loc != NULL
13855 && search_pspace != NULL
13856 && b->loc->pspace != search_pspace)
ef23e705 13857 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13858 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13859 || b->enable_state == bp_disabled))
13860 not_found_and_ok = 1;
13861
13862 if (!not_found_and_ok)
13863 {
13864 /* We surely don't want to warn about the same breakpoint
13865 10 times. One solution, implemented here, is disable
13866 the breakpoint on error. Another solution would be to
13867 have separate 'warning emitted' flag. Since this
13868 happens only when a binary has changed, I don't know
13869 which approach is better. */
13870 b->enable_state = bp_disabled;
13871 throw_exception (e);
13872 }
13873 }
492d29ea 13874 END_CATCH
ef23e705 13875
492d29ea 13876 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13877 {
6c5b2ebe
PA
13878 for (auto &sal : sals)
13879 resolve_sal_pc (&sal);
f00aae0f 13880 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13881 {
ed1d1739
KS
13882 char *cond_string, *extra_string;
13883 int thread, task;
ef23e705 13884
6c5b2ebe 13885 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13886 &cond_string, &thread, &task,
13887 &extra_string);
f00aae0f 13888 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13889 if (cond_string)
13890 b->cond_string = cond_string;
13891 b->thread = thread;
13892 b->task = task;
e7e0cddf 13893 if (extra_string)
f00aae0f
KS
13894 {
13895 xfree (b->extra_string);
13896 b->extra_string = extra_string;
13897 }
ef23e705
TJB
13898 b->condition_not_parsed = 0;
13899 }
13900
983af33b 13901 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13902 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13903
58438ac1
TT
13904 *found = 1;
13905 }
13906 else
13907 *found = 0;
ef23e705
TJB
13908
13909 return sals;
13910}
13911
348d480f
PA
13912/* The default re_set method, for typical hardware or software
13913 breakpoints. Reevaluate the breakpoint and recreate its
13914 locations. */
13915
13916static void
28010a5d 13917breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13918{
c2f4122d 13919 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13920 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13921
6c5b2ebe
PA
13922 int found;
13923 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13924 filter_pspace, &found);
ef23e705 13925 if (found)
6c5b2ebe 13926 expanded = std::move (sals);
ef23e705 13927
f00aae0f 13928 if (b->location_range_end != NULL)
f1310107 13929 {
6c5b2ebe
PA
13930 std::vector<symtab_and_line> sals_end
13931 = location_to_sals (b, b->location_range_end.get (),
13932 filter_pspace, &found);
f1310107 13933 if (found)
6c5b2ebe 13934 expanded_end = std::move (sals_end);
f1310107
TJB
13935 }
13936
c2f4122d 13937 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13938}
13939
983af33b
SDJ
13940/* Default method for creating SALs from an address string. It basically
13941 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13942
13943static void
f00aae0f
KS
13944create_sals_from_location_default (const struct event_location *location,
13945 struct linespec_result *canonical,
13946 enum bptype type_wanted)
983af33b 13947{
f00aae0f 13948 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13949}
13950
13951/* Call create_breakpoints_sal for the given arguments. This is the default
13952 function for the `create_breakpoints_sal' method of
13953 breakpoint_ops. */
13954
13955static void
13956create_breakpoints_sal_default (struct gdbarch *gdbarch,
13957 struct linespec_result *canonical,
e1e01040
PA
13958 gdb::unique_xmalloc_ptr<char> cond_string,
13959 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13960 enum bptype type_wanted,
13961 enum bpdisp disposition,
13962 int thread,
13963 int task, int ignore_count,
13964 const struct breakpoint_ops *ops,
13965 int from_tty, int enabled,
44f238bb 13966 int internal, unsigned flags)
983af33b 13967{
e1e01040
PA
13968 create_breakpoints_sal (gdbarch, canonical,
13969 std::move (cond_string),
13970 std::move (extra_string),
983af33b
SDJ
13971 type_wanted, disposition,
13972 thread, task, ignore_count, ops, from_tty,
44f238bb 13973 enabled, internal, flags);
983af33b
SDJ
13974}
13975
13976/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13977 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13978
6c5b2ebe 13979static std::vector<symtab_and_line>
f00aae0f
KS
13980decode_location_default (struct breakpoint *b,
13981 const struct event_location *location,
6c5b2ebe 13982 struct program_space *search_pspace)
983af33b
SDJ
13983{
13984 struct linespec_result canonical;
13985
c2f4122d 13986 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
13987 (struct symtab *) NULL, 0,
13988 &canonical, multiple_symbols_all,
13989 b->filter);
13990
13991 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13992 gdb_assert (canonical.lsals.size () < 2);
983af33b 13993
6c5b2ebe 13994 if (!canonical.lsals.empty ())
983af33b 13995 {
6c5b2ebe
PA
13996 const linespec_sals &lsal = canonical.lsals[0];
13997 return std::move (lsal.sals);
983af33b 13998 }
6c5b2ebe 13999 return {};
983af33b
SDJ
14000}
14001
bf469271 14002/* Reset a breakpoint. */
c906108c 14003
bf469271
PA
14004static void
14005breakpoint_re_set_one (breakpoint *b)
c906108c 14006{
fdf44873
TT
14007 input_radix = b->input_radix;
14008 set_language (b->language);
c906108c 14009
348d480f 14010 b->ops->re_set (b);
c906108c
SS
14011}
14012
c2f4122d
PA
14013/* Re-set breakpoint locations for the current program space.
14014 Locations bound to other program spaces are left untouched. */
14015
c906108c 14016void
69de3c6a 14017breakpoint_re_set (void)
c906108c 14018{
35df4500 14019 struct breakpoint *b, *b_tmp;
2a7f3dff 14020
c5aa993b 14021 {
fdf44873
TT
14022 scoped_restore_current_language save_language;
14023 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14024 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14025
5ed8105e
PA
14026 /* Note: we must not try to insert locations until after all
14027 breakpoints have been re-set. Otherwise, e.g., when re-setting
14028 breakpoint 1, we'd insert the locations of breakpoint 2, which
14029 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14030
5ed8105e
PA
14031 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14032 {
bf469271
PA
14033 TRY
14034 {
14035 breakpoint_re_set_one (b);
14036 }
14037 CATCH (ex, RETURN_MASK_ALL)
14038 {
14039 exception_fprintf (gdb_stderr, ex,
14040 "Error in re-setting breakpoint %d: ",
14041 b->number);
14042 }
14043 END_CATCH
5ed8105e 14044 }
5ed8105e
PA
14045
14046 jit_breakpoint_re_set ();
14047 }
6c95b8df 14048
af02033e
PP
14049 create_overlay_event_breakpoint ();
14050 create_longjmp_master_breakpoint ();
14051 create_std_terminate_master_breakpoint ();
186c406b 14052 create_exception_master_breakpoint ();
2a7f3dff
PA
14053
14054 /* Now we can insert. */
14055 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14056}
14057\f
c906108c
SS
14058/* Reset the thread number of this breakpoint:
14059
14060 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14061 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14062void
fba45db2 14063breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14064{
14065 if (b->thread != -1)
14066 {
39f77062 14067 if (in_thread_list (inferior_ptid))
5d5658a1 14068 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14069
14070 /* We're being called after following a fork. The new fork is
14071 selected as current, and unless this was a vfork will have a
14072 different program space from the original thread. Reset that
14073 as well. */
14074 b->loc->pspace = current_program_space;
c906108c
SS
14075 }
14076}
14077
03ac34d5
MS
14078/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14079 If from_tty is nonzero, it prints a message to that effect,
14080 which ends with a period (no newline). */
14081
c906108c 14082void
fba45db2 14083set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14084{
52f0bd74 14085 struct breakpoint *b;
c906108c
SS
14086
14087 if (count < 0)
14088 count = 0;
14089
14090 ALL_BREAKPOINTS (b)
14091 if (b->number == bptnum)
c5aa993b 14092 {
d77f58be
SS
14093 if (is_tracepoint (b))
14094 {
14095 if (from_tty && count != 0)
14096 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14097 bptnum);
14098 return;
14099 }
14100
c5aa993b 14101 b->ignore_count = count;
221ea385
KS
14102 if (from_tty)
14103 {
14104 if (count == 0)
3e43a32a
MS
14105 printf_filtered (_("Will stop next time "
14106 "breakpoint %d is reached."),
221ea385
KS
14107 bptnum);
14108 else if (count == 1)
a3f17187 14109 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14110 bptnum);
14111 else
3e43a32a
MS
14112 printf_filtered (_("Will ignore next %d "
14113 "crossings of breakpoint %d."),
221ea385
KS
14114 count, bptnum);
14115 }
8d3788bd 14116 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14117 return;
14118 }
c906108c 14119
8a3fe4f8 14120 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14121}
14122
c906108c
SS
14123/* Command to set ignore-count of breakpoint N to COUNT. */
14124
14125static void
fba45db2 14126ignore_command (char *args, int from_tty)
c906108c
SS
14127{
14128 char *p = args;
52f0bd74 14129 int num;
c906108c
SS
14130
14131 if (p == 0)
e2e0b3e5 14132 error_no_arg (_("a breakpoint number"));
c5aa993b 14133
c906108c 14134 num = get_number (&p);
5c44784c 14135 if (num == 0)
8a3fe4f8 14136 error (_("bad breakpoint number: '%s'"), args);
c906108c 14137 if (*p == 0)
8a3fe4f8 14138 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14139
14140 set_ignore_count (num,
14141 longest_to_int (value_as_long (parse_and_eval (p))),
14142 from_tty);
221ea385
KS
14143 if (from_tty)
14144 printf_filtered ("\n");
c906108c
SS
14145}
14146\f
14147/* Call FUNCTION on each of the breakpoints
14148 whose numbers are given in ARGS. */
14149
14150static void
896b6bda 14151map_breakpoint_numbers (const char *args,
48649e1b 14152 gdb::function_view<void (breakpoint *)> function)
c906108c 14153{
52f0bd74
AC
14154 int num;
14155 struct breakpoint *b, *tmp;
c906108c 14156
b9d61307 14157 if (args == 0 || *args == '\0')
e2e0b3e5 14158 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14159
bfd28288 14160 number_or_range_parser parser (args);
197f0a60 14161
bfd28288 14162 while (!parser.finished ())
c906108c 14163 {
bfd28288
PA
14164 const char *p = parser.cur_tok ();
14165 bool match = false;
197f0a60 14166
bfd28288 14167 num = parser.get_number ();
5c44784c 14168 if (num == 0)
c5aa993b 14169 {
8a3fe4f8 14170 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14171 }
14172 else
14173 {
14174 ALL_BREAKPOINTS_SAFE (b, tmp)
14175 if (b->number == num)
14176 {
bfd28288 14177 match = true;
48649e1b 14178 function (b);
11cf8741 14179 break;
5c44784c 14180 }
bfd28288 14181 if (!match)
a3f17187 14182 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14183 }
c906108c
SS
14184 }
14185}
14186
0d381245 14187static struct bp_location *
cb791d59 14188find_location_by_number (const char *number)
0d381245 14189{
cb791d59 14190 const char *p1;
0d381245
VP
14191 int bp_num;
14192 int loc_num;
14193 struct breakpoint *b;
14194 struct bp_location *loc;
14195
0d381245 14196 p1 = number;
cb791d59
TT
14197 bp_num = get_number_trailer (&p1, '.');
14198 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14199 error (_("Bad breakpoint number '%s'"), number);
14200
14201 ALL_BREAKPOINTS (b)
14202 if (b->number == bp_num)
14203 {
14204 break;
14205 }
14206
14207 if (!b || b->number != bp_num)
14208 error (_("Bad breakpoint number '%s'"), number);
14209
cb791d59
TT
14210 /* Skip the dot. */
14211 ++p1;
14212 const char *save = p1;
197f0a60 14213 loc_num = get_number (&p1);
0d381245
VP
14214 if (loc_num == 0)
14215 error (_("Bad breakpoint location number '%s'"), number);
14216
14217 --loc_num;
14218 loc = b->loc;
14219 for (;loc_num && loc; --loc_num, loc = loc->next)
14220 ;
14221 if (!loc)
cb791d59 14222 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14223
14224 return loc;
14225}
14226
14227
1900040c
MS
14228/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14229 If from_tty is nonzero, it prints a message to that effect,
14230 which ends with a period (no newline). */
14231
c906108c 14232void
fba45db2 14233disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14234{
14235 /* Never disable a watchpoint scope breakpoint; we want to
14236 hit them when we leave scope so we can delete both the
14237 watchpoint and its scope breakpoint at that time. */
14238 if (bpt->type == bp_watchpoint_scope)
14239 return;
14240
b5de0fa7 14241 bpt->enable_state = bp_disabled;
c906108c 14242
b775012e
LM
14243 /* Mark breakpoint locations modified. */
14244 mark_breakpoint_modified (bpt);
14245
d248b706
KY
14246 if (target_supports_enable_disable_tracepoint ()
14247 && current_trace_status ()->running && is_tracepoint (bpt))
14248 {
14249 struct bp_location *location;
14250
14251 for (location = bpt->loc; location; location = location->next)
14252 target_disable_tracepoint (location);
14253 }
14254
44702360 14255 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14256
8d3788bd 14257 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14258}
14259
c906108c 14260static void
981a3fb3 14261disable_command (const char *args, int from_tty)
c906108c 14262{
c906108c 14263 if (args == 0)
46c6471b
PA
14264 {
14265 struct breakpoint *bpt;
14266
14267 ALL_BREAKPOINTS (bpt)
14268 if (user_breakpoint_p (bpt))
14269 disable_breakpoint (bpt);
14270 }
9eaabc75 14271 else
0d381245 14272 {
cb791d59 14273 std::string num = extract_arg (&args);
9eaabc75 14274
cb791d59 14275 while (!num.empty ())
d248b706 14276 {
cb791d59 14277 if (num.find ('.') != std::string::npos)
b775012e 14278 {
cb791d59 14279 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14280
14281 if (loc)
14282 {
14283 if (loc->enabled)
14284 {
14285 loc->enabled = 0;
14286 mark_breakpoint_location_modified (loc);
14287 }
14288 if (target_supports_enable_disable_tracepoint ()
14289 && current_trace_status ()->running && loc->owner
14290 && is_tracepoint (loc->owner))
14291 target_disable_tracepoint (loc);
14292 }
44702360 14293 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14294 }
9eaabc75 14295 else
48649e1b
TT
14296 map_breakpoint_numbers
14297 (num.c_str (), [&] (breakpoint *b)
14298 {
14299 iterate_over_related_breakpoints (b, disable_breakpoint);
14300 });
9eaabc75 14301 num = extract_arg (&args);
d248b706 14302 }
0d381245 14303 }
c906108c
SS
14304}
14305
14306static void
816338b5
SS
14307enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14308 int count)
c906108c 14309{
afe38095 14310 int target_resources_ok;
c906108c
SS
14311
14312 if (bpt->type == bp_hardware_breakpoint)
14313 {
14314 int i;
c5aa993b 14315 i = hw_breakpoint_used_count ();
53a5351d 14316 target_resources_ok =
d92524f1 14317 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14318 i + 1, 0);
c906108c 14319 if (target_resources_ok == 0)
8a3fe4f8 14320 error (_("No hardware breakpoint support in the target."));
c906108c 14321 else if (target_resources_ok < 0)
8a3fe4f8 14322 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14323 }
14324
cc60f2e3 14325 if (is_watchpoint (bpt))
c906108c 14326 {
d07205c2 14327 /* Initialize it just to avoid a GCC false warning. */
f486487f 14328 enum enable_state orig_enable_state = bp_disabled;
dde02812 14329
492d29ea 14330 TRY
c906108c 14331 {
3a5c3e22
PA
14332 struct watchpoint *w = (struct watchpoint *) bpt;
14333
1e718ff1
TJB
14334 orig_enable_state = bpt->enable_state;
14335 bpt->enable_state = bp_enabled;
3a5c3e22 14336 update_watchpoint (w, 1 /* reparse */);
c906108c 14337 }
492d29ea 14338 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14339 {
1e718ff1 14340 bpt->enable_state = orig_enable_state;
dde02812
ES
14341 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14342 bpt->number);
14343 return;
c5aa993b 14344 }
492d29ea 14345 END_CATCH
c906108c 14346 }
0101ce28 14347
b775012e
LM
14348 bpt->enable_state = bp_enabled;
14349
14350 /* Mark breakpoint locations modified. */
14351 mark_breakpoint_modified (bpt);
14352
d248b706
KY
14353 if (target_supports_enable_disable_tracepoint ()
14354 && current_trace_status ()->running && is_tracepoint (bpt))
14355 {
14356 struct bp_location *location;
14357
14358 for (location = bpt->loc; location; location = location->next)
14359 target_enable_tracepoint (location);
14360 }
14361
b4c291bb 14362 bpt->disposition = disposition;
816338b5 14363 bpt->enable_count = count;
44702360 14364 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14365
8d3788bd 14366 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14367}
14368
fe3f5fa8 14369
c906108c 14370void
fba45db2 14371enable_breakpoint (struct breakpoint *bpt)
c906108c 14372{
816338b5 14373 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14374}
14375
c906108c
SS
14376/* The enable command enables the specified breakpoints (or all defined
14377 breakpoints) so they once again become (or continue to be) effective
1272ad14 14378 in stopping the inferior. */
c906108c 14379
c906108c 14380static void
981a3fb3 14381enable_command (const char *args, int from_tty)
c906108c 14382{
c906108c 14383 if (args == 0)
46c6471b
PA
14384 {
14385 struct breakpoint *bpt;
14386
14387 ALL_BREAKPOINTS (bpt)
14388 if (user_breakpoint_p (bpt))
14389 enable_breakpoint (bpt);
14390 }
9eaabc75 14391 else
0d381245 14392 {
cb791d59 14393 std::string num = extract_arg (&args);
9eaabc75 14394
cb791d59 14395 while (!num.empty ())
d248b706 14396 {
cb791d59 14397 if (num.find ('.') != std::string::npos)
b775012e 14398 {
cb791d59 14399 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14400
14401 if (loc)
14402 {
14403 if (!loc->enabled)
14404 {
14405 loc->enabled = 1;
14406 mark_breakpoint_location_modified (loc);
14407 }
14408 if (target_supports_enable_disable_tracepoint ()
14409 && current_trace_status ()->running && loc->owner
14410 && is_tracepoint (loc->owner))
14411 target_enable_tracepoint (loc);
14412 }
44702360 14413 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14414 }
9eaabc75 14415 else
48649e1b
TT
14416 map_breakpoint_numbers
14417 (num.c_str (), [&] (breakpoint *b)
14418 {
14419 iterate_over_related_breakpoints (b, enable_breakpoint);
14420 });
9eaabc75 14421 num = extract_arg (&args);
d248b706 14422 }
0d381245 14423 }
c906108c
SS
14424}
14425
c906108c 14426static void
4495129a 14427enable_once_command (const char *args, int from_tty)
c906108c 14428{
48649e1b
TT
14429 map_breakpoint_numbers
14430 (args, [&] (breakpoint *b)
14431 {
14432 iterate_over_related_breakpoints
14433 (b, [&] (breakpoint *bpt)
14434 {
14435 enable_breakpoint_disp (bpt, disp_disable, 1);
14436 });
14437 });
816338b5
SS
14438}
14439
14440static void
4495129a 14441enable_count_command (const char *args, int from_tty)
816338b5 14442{
b9d61307
SM
14443 int count;
14444
14445 if (args == NULL)
14446 error_no_arg (_("hit count"));
14447
14448 count = get_number (&args);
816338b5 14449
48649e1b
TT
14450 map_breakpoint_numbers
14451 (args, [&] (breakpoint *b)
14452 {
14453 iterate_over_related_breakpoints
14454 (b, [&] (breakpoint *bpt)
14455 {
14456 enable_breakpoint_disp (bpt, disp_disable, count);
14457 });
14458 });
c906108c
SS
14459}
14460
c906108c 14461static void
4495129a 14462enable_delete_command (const char *args, int from_tty)
c906108c 14463{
48649e1b
TT
14464 map_breakpoint_numbers
14465 (args, [&] (breakpoint *b)
14466 {
14467 iterate_over_related_breakpoints
14468 (b, [&] (breakpoint *bpt)
14469 {
14470 enable_breakpoint_disp (bpt, disp_del, 1);
14471 });
14472 });
c906108c
SS
14473}
14474\f
fa8d40ab 14475static void
981a3fb3 14476set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14477{
14478}
14479
14480static void
981a3fb3 14481show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14482{
14483}
14484
1f3b5d1b
PP
14485/* Invalidate last known value of any hardware watchpoint if
14486 the memory which that value represents has been written to by
14487 GDB itself. */
14488
14489static void
8de0566d
YQ
14490invalidate_bp_value_on_memory_change (struct inferior *inferior,
14491 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14492 const bfd_byte *data)
14493{
14494 struct breakpoint *bp;
14495
14496 ALL_BREAKPOINTS (bp)
14497 if (bp->enable_state == bp_enabled
3a5c3e22 14498 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14499 {
3a5c3e22 14500 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14501
3a5c3e22
PA
14502 if (wp->val_valid && wp->val)
14503 {
14504 struct bp_location *loc;
14505
14506 for (loc = bp->loc; loc != NULL; loc = loc->next)
14507 if (loc->loc_type == bp_loc_hardware_watchpoint
14508 && loc->address + loc->length > addr
14509 && addr + len > loc->address)
14510 {
14511 value_free (wp->val);
14512 wp->val = NULL;
14513 wp->val_valid = 0;
14514 }
14515 }
1f3b5d1b
PP
14516 }
14517}
14518
8181d85f
DJ
14519/* Create and insert a breakpoint for software single step. */
14520
14521void
6c95b8df 14522insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14523 const address_space *aspace,
4a64f543 14524 CORE_ADDR next_pc)
8181d85f 14525{
7c16b83e
PA
14526 struct thread_info *tp = inferior_thread ();
14527 struct symtab_and_line sal;
14528 CORE_ADDR pc = next_pc;
8181d85f 14529
34b7e8a6
PA
14530 if (tp->control.single_step_breakpoints == NULL)
14531 {
14532 tp->control.single_step_breakpoints
5d5658a1 14533 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14534 }
8181d85f 14535
7c16b83e
PA
14536 sal = find_pc_line (pc, 0);
14537 sal.pc = pc;
14538 sal.section = find_pc_overlay (pc);
14539 sal.explicit_pc = 1;
34b7e8a6 14540 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14541
7c16b83e 14542 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14543}
14544
93f9a11f
YQ
14545/* Insert single step breakpoints according to the current state. */
14546
14547int
14548insert_single_step_breakpoints (struct gdbarch *gdbarch)
14549{
f5ea389a 14550 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14551 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14552
f5ea389a 14553 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14554
a0ff9e1a 14555 if (!next_pcs.empty ())
93f9a11f 14556 {
f5ea389a 14557 struct frame_info *frame = get_current_frame ();
8b86c959 14558 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14559
a0ff9e1a 14560 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14561 insert_single_step_breakpoint (gdbarch, aspace, pc);
14562
93f9a11f
YQ
14563 return 1;
14564 }
14565 else
14566 return 0;
14567}
14568
34b7e8a6 14569/* See breakpoint.h. */
f02253f1
HZ
14570
14571int
7c16b83e 14572breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14573 const address_space *aspace,
7c16b83e 14574 CORE_ADDR pc)
1aafd4da 14575{
7c16b83e 14576 struct bp_location *loc;
1aafd4da 14577
7c16b83e
PA
14578 for (loc = bp->loc; loc != NULL; loc = loc->next)
14579 if (loc->inserted
14580 && breakpoint_location_address_match (loc, aspace, pc))
14581 return 1;
1aafd4da 14582
7c16b83e 14583 return 0;
ef370185
JB
14584}
14585
14586/* Check whether a software single-step breakpoint is inserted at
14587 PC. */
14588
14589int
accd0bcd 14590single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14591 CORE_ADDR pc)
14592{
34b7e8a6
PA
14593 struct breakpoint *bpt;
14594
14595 ALL_BREAKPOINTS (bpt)
14596 {
14597 if (bpt->type == bp_single_step
14598 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14599 return 1;
14600 }
14601 return 0;
1aafd4da
UW
14602}
14603
1042e4c0
SS
14604/* Tracepoint-specific operations. */
14605
14606/* Set tracepoint count to NUM. */
14607static void
14608set_tracepoint_count (int num)
14609{
14610 tracepoint_count = num;
4fa62494 14611 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14612}
14613
70221824 14614static void
f2fc3015 14615trace_command (char *arg_in, int from_tty)
1042e4c0 14616{
f2fc3015 14617 const char *arg = arg_in;
55aa24fb 14618 struct breakpoint_ops *ops;
55aa24fb 14619
ffc2605c
TT
14620 event_location_up location = string_to_event_location (&arg,
14621 current_language);
5b56227b 14622 if (location != NULL
ffc2605c 14623 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14624 ops = &tracepoint_probe_breakpoint_ops;
14625 else
14626 ops = &tracepoint_breakpoint_ops;
14627
558a9d82 14628 create_breakpoint (get_current_arch (),
ffc2605c 14629 location.get (),
f00aae0f 14630 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14631 0 /* tempflag */,
14632 bp_tracepoint /* type_wanted */,
14633 0 /* Ignore count */,
14634 pending_break_support,
14635 ops,
14636 from_tty,
14637 1 /* enabled */,
14638 0 /* internal */, 0);
1042e4c0
SS
14639}
14640
70221824 14641static void
f2fc3015 14642ftrace_command (char *arg_in, int from_tty)
7a697b8d 14643{
f2fc3015 14644 const char *arg = arg_in;
ffc2605c
TT
14645 event_location_up location = string_to_event_location (&arg,
14646 current_language);
558a9d82 14647 create_breakpoint (get_current_arch (),
ffc2605c 14648 location.get (),
f00aae0f 14649 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14650 0 /* tempflag */,
14651 bp_fast_tracepoint /* type_wanted */,
14652 0 /* Ignore count */,
14653 pending_break_support,
14654 &tracepoint_breakpoint_ops,
14655 from_tty,
14656 1 /* enabled */,
14657 0 /* internal */, 0);
0fb4aa4b
PA
14658}
14659
14660/* strace command implementation. Creates a static tracepoint. */
14661
70221824 14662static void
f2fc3015 14663strace_command (char *arg_in, int from_tty)
0fb4aa4b 14664{
f2fc3015 14665 const char *arg = arg_in;
983af33b 14666 struct breakpoint_ops *ops;
ffc2605c 14667 event_location_up location;
983af33b
SDJ
14668
14669 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14670 or with a normal static tracepoint. */
61012eef 14671 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14672 {
14673 ops = &strace_marker_breakpoint_ops;
14674 location = new_linespec_location (&arg);
14675 }
983af33b 14676 else
f00aae0f
KS
14677 {
14678 ops = &tracepoint_breakpoint_ops;
14679 location = string_to_event_location (&arg, current_language);
14680 }
983af33b 14681
558a9d82 14682 create_breakpoint (get_current_arch (),
ffc2605c 14683 location.get (),
f00aae0f 14684 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14685 0 /* tempflag */,
14686 bp_static_tracepoint /* type_wanted */,
14687 0 /* Ignore count */,
14688 pending_break_support,
14689 ops,
14690 from_tty,
14691 1 /* enabled */,
14692 0 /* internal */, 0);
7a697b8d
SS
14693}
14694
409873ef
SS
14695/* Set up a fake reader function that gets command lines from a linked
14696 list that was acquired during tracepoint uploading. */
14697
14698static struct uploaded_tp *this_utp;
3149d8c1 14699static int next_cmd;
409873ef
SS
14700
14701static char *
14702read_uploaded_action (void)
14703{
14704 char *rslt;
14705
3149d8c1 14706 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14707
3149d8c1 14708 next_cmd++;
409873ef
SS
14709
14710 return rslt;
14711}
14712
00bf0b85
SS
14713/* Given information about a tracepoint as recorded on a target (which
14714 can be either a live system or a trace file), attempt to create an
14715 equivalent GDB tracepoint. This is not a reliable process, since
14716 the target does not necessarily have all the information used when
14717 the tracepoint was originally defined. */
14718
d9b3f62e 14719struct tracepoint *
00bf0b85 14720create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14721{
f2fc3015
TT
14722 const char *addr_str;
14723 char small_buf[100];
d9b3f62e 14724 struct tracepoint *tp;
fd9b8c24 14725
409873ef
SS
14726 if (utp->at_string)
14727 addr_str = utp->at_string;
14728 else
14729 {
14730 /* In the absence of a source location, fall back to raw
14731 address. Since there is no way to confirm that the address
14732 means the same thing as when the trace was started, warn the
14733 user. */
3e43a32a
MS
14734 warning (_("Uploaded tracepoint %d has no "
14735 "source location, using raw address"),
409873ef 14736 utp->number);
8c042590 14737 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14738 addr_str = small_buf;
14739 }
14740
14741 /* There's not much we can do with a sequence of bytecodes. */
14742 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14743 warning (_("Uploaded tracepoint %d condition "
14744 "has no source form, ignoring it"),
409873ef 14745 utp->number);
d5551862 14746
ffc2605c
TT
14747 event_location_up location = string_to_event_location (&addr_str,
14748 current_language);
8cdf0e15 14749 if (!create_breakpoint (get_current_arch (),
ffc2605c 14750 location.get (),
f00aae0f 14751 utp->cond_string, -1, addr_str,
e7e0cddf 14752 0 /* parse cond/thread */,
8cdf0e15 14753 0 /* tempflag */,
0fb4aa4b 14754 utp->type /* type_wanted */,
8cdf0e15
VP
14755 0 /* Ignore count */,
14756 pending_break_support,
348d480f 14757 &tracepoint_breakpoint_ops,
8cdf0e15 14758 0 /* from_tty */,
84f4c1fe 14759 utp->enabled /* enabled */,
44f238bb
PA
14760 0 /* internal */,
14761 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14762 return NULL;
fd9b8c24 14763
409873ef 14764 /* Get the tracepoint we just created. */
fd9b8c24
PA
14765 tp = get_tracepoint (tracepoint_count);
14766 gdb_assert (tp != NULL);
d5551862 14767
00bf0b85
SS
14768 if (utp->pass > 0)
14769 {
8c042590 14770 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14771 tp->number);
00bf0b85 14772
409873ef 14773 trace_pass_command (small_buf, 0);
00bf0b85
SS
14774 }
14775
409873ef
SS
14776 /* If we have uploaded versions of the original commands, set up a
14777 special-purpose "reader" function and call the usual command line
14778 reader, then pass the result to the breakpoint command-setting
14779 function. */
3149d8c1 14780 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14781 {
93921405 14782 command_line_up cmd_list;
00bf0b85 14783
409873ef 14784 this_utp = utp;
3149d8c1 14785 next_cmd = 0;
d5551862 14786
409873ef
SS
14787 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14788
c1fc2657 14789 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14790 }
3149d8c1
SS
14791 else if (!VEC_empty (char_ptr, utp->actions)
14792 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14793 warning (_("Uploaded tracepoint %d actions "
14794 "have no source form, ignoring them"),
409873ef 14795 utp->number);
00bf0b85 14796
f196051f 14797 /* Copy any status information that might be available. */
c1fc2657 14798 tp->hit_count = utp->hit_count;
f196051f
SS
14799 tp->traceframe_usage = utp->traceframe_usage;
14800
00bf0b85 14801 return tp;
d9b3f62e 14802}
00bf0b85 14803
1042e4c0
SS
14804/* Print information on tracepoint number TPNUM_EXP, or all if
14805 omitted. */
14806
14807static void
11db9430 14808info_tracepoints_command (char *args, int from_tty)
1042e4c0 14809{
79a45e25 14810 struct ui_out *uiout = current_uiout;
e5a67952 14811 int num_printed;
1042e4c0 14812
e5a67952 14813 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14814
14815 if (num_printed == 0)
1042e4c0 14816 {
e5a67952 14817 if (args == NULL || *args == '\0')
112e8700 14818 uiout->message ("No tracepoints.\n");
d77f58be 14819 else
112e8700 14820 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14821 }
ad443146
SS
14822
14823 default_collect_info ();
1042e4c0
SS
14824}
14825
4a64f543 14826/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14827 Not supported by all targets. */
14828static void
14829enable_trace_command (char *args, int from_tty)
14830{
14831 enable_command (args, from_tty);
14832}
14833
4a64f543 14834/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14835 Not supported by all targets. */
14836static void
14837disable_trace_command (char *args, int from_tty)
14838{
14839 disable_command (args, from_tty);
14840}
14841
4a64f543 14842/* Remove a tracepoint (or all if no argument). */
1042e4c0 14843static void
4495129a 14844delete_trace_command (const char *arg, int from_tty)
1042e4c0 14845{
35df4500 14846 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14847
14848 dont_repeat ();
14849
14850 if (arg == 0)
14851 {
14852 int breaks_to_delete = 0;
14853
14854 /* Delete all breakpoints if no argument.
14855 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14856 have to be deleted with an explicit breakpoint number
14857 argument. */
1042e4c0 14858 ALL_TRACEPOINTS (b)
46c6471b 14859 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14860 {
14861 breaks_to_delete = 1;
14862 break;
14863 }
1042e4c0
SS
14864
14865 /* Ask user only if there are some breakpoints to delete. */
14866 if (!from_tty
14867 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14868 {
35df4500 14869 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14870 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14871 delete_breakpoint (b);
1042e4c0
SS
14872 }
14873 }
14874 else
48649e1b
TT
14875 map_breakpoint_numbers
14876 (arg, [&] (breakpoint *b)
14877 {
14878 iterate_over_related_breakpoints (b, delete_breakpoint);
14879 });
1042e4c0
SS
14880}
14881
197f0a60
TT
14882/* Helper function for trace_pass_command. */
14883
14884static void
d9b3f62e 14885trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14886{
d9b3f62e 14887 tp->pass_count = count;
c1fc2657 14888 observer_notify_breakpoint_modified (tp);
197f0a60
TT
14889 if (from_tty)
14890 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14891 tp->number, count);
197f0a60
TT
14892}
14893
1042e4c0
SS
14894/* Set passcount for tracepoint.
14895
14896 First command argument is passcount, second is tracepoint number.
14897 If tracepoint number omitted, apply to most recently defined.
14898 Also accepts special argument "all". */
14899
14900static void
14901trace_pass_command (char *args, int from_tty)
14902{
d9b3f62e 14903 struct tracepoint *t1;
1042e4c0 14904 unsigned int count;
1042e4c0
SS
14905
14906 if (args == 0 || *args == 0)
3e43a32a
MS
14907 error (_("passcount command requires an "
14908 "argument (count + optional TP num)"));
1042e4c0 14909
4a64f543 14910 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 14911
529480d0 14912 args = skip_spaces (args);
1042e4c0
SS
14913 if (*args && strncasecmp (args, "all", 3) == 0)
14914 {
d9b3f62e
PA
14915 struct breakpoint *b;
14916
1042e4c0 14917 args += 3; /* Skip special argument "all". */
1042e4c0
SS
14918 if (*args)
14919 error (_("Junk at end of arguments."));
1042e4c0 14920
d9b3f62e 14921 ALL_TRACEPOINTS (b)
197f0a60 14922 {
d9b3f62e 14923 t1 = (struct tracepoint *) b;
197f0a60
TT
14924 trace_pass_set_count (t1, count, from_tty);
14925 }
14926 }
14927 else if (*args == '\0')
1042e4c0 14928 {
5fa1d40e 14929 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 14930 if (t1)
197f0a60
TT
14931 trace_pass_set_count (t1, count, from_tty);
14932 }
14933 else
14934 {
bfd28288
PA
14935 number_or_range_parser parser (args);
14936 while (!parser.finished ())
1042e4c0 14937 {
bfd28288 14938 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
14939 if (t1)
14940 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
14941 }
14942 }
1042e4c0
SS
14943}
14944
d9b3f62e 14945struct tracepoint *
1042e4c0
SS
14946get_tracepoint (int num)
14947{
14948 struct breakpoint *t;
14949
14950 ALL_TRACEPOINTS (t)
14951 if (t->number == num)
d9b3f62e 14952 return (struct tracepoint *) t;
1042e4c0
SS
14953
14954 return NULL;
14955}
14956
d5551862
SS
14957/* Find the tracepoint with the given target-side number (which may be
14958 different from the tracepoint number after disconnecting and
14959 reconnecting). */
14960
d9b3f62e 14961struct tracepoint *
d5551862
SS
14962get_tracepoint_by_number_on_target (int num)
14963{
d9b3f62e 14964 struct breakpoint *b;
d5551862 14965
d9b3f62e
PA
14966 ALL_TRACEPOINTS (b)
14967 {
14968 struct tracepoint *t = (struct tracepoint *) b;
14969
14970 if (t->number_on_target == num)
14971 return t;
14972 }
d5551862
SS
14973
14974 return NULL;
14975}
14976
1042e4c0 14977/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 14978 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
14979 If the argument is missing, the most recent tracepoint
14980 (tracepoint_count) is returned. */
14981
d9b3f62e 14982struct tracepoint *
197f0a60 14983get_tracepoint_by_number (char **arg,
bfd28288 14984 number_or_range_parser *parser)
1042e4c0 14985{
1042e4c0
SS
14986 struct breakpoint *t;
14987 int tpnum;
14988 char *instring = arg == NULL ? NULL : *arg;
14989
bfd28288 14990 if (parser != NULL)
197f0a60 14991 {
bfd28288
PA
14992 gdb_assert (!parser->finished ());
14993 tpnum = parser->get_number ();
197f0a60
TT
14994 }
14995 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 14996 tpnum = tracepoint_count;
1042e4c0 14997 else
197f0a60 14998 tpnum = get_number (arg);
1042e4c0
SS
14999
15000 if (tpnum <= 0)
15001 {
15002 if (instring && *instring)
15003 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15004 instring);
15005 else
5fa1d40e 15006 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15007 return NULL;
15008 }
15009
15010 ALL_TRACEPOINTS (t)
15011 if (t->number == tpnum)
15012 {
d9b3f62e 15013 return (struct tracepoint *) t;
1042e4c0
SS
15014 }
15015
1042e4c0
SS
15016 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15017 return NULL;
15018}
15019
d9b3f62e
PA
15020void
15021print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15022{
15023 if (b->thread != -1)
15024 fprintf_unfiltered (fp, " thread %d", b->thread);
15025
15026 if (b->task != 0)
15027 fprintf_unfiltered (fp, " task %d", b->task);
15028
15029 fprintf_unfiltered (fp, "\n");
15030}
15031
6149aea9
PA
15032/* Save information on user settable breakpoints (watchpoints, etc) to
15033 a new script file named FILENAME. If FILTER is non-NULL, call it
15034 on each breakpoint and only include the ones for which it returns
15035 non-zero. */
15036
1042e4c0 15037static void
4495129a 15038save_breakpoints (const char *filename, int from_tty,
6149aea9 15039 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15040{
15041 struct breakpoint *tp;
6149aea9 15042 int any = 0;
6149aea9 15043 int extra_trace_bits = 0;
1042e4c0 15044
6149aea9
PA
15045 if (filename == 0 || *filename == 0)
15046 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15047
15048 /* See if we have anything to save. */
6149aea9 15049 ALL_BREAKPOINTS (tp)
1042e4c0 15050 {
6149aea9 15051 /* Skip internal and momentary breakpoints. */
09d682a4 15052 if (!user_breakpoint_p (tp))
6149aea9
PA
15053 continue;
15054
15055 /* If we have a filter, only save the breakpoints it accepts. */
15056 if (filter && !filter (tp))
15057 continue;
15058
15059 any = 1;
15060
15061 if (is_tracepoint (tp))
15062 {
15063 extra_trace_bits = 1;
15064
15065 /* We can stop searching. */
15066 break;
15067 }
1042e4c0 15068 }
6149aea9
PA
15069
15070 if (!any)
1042e4c0 15071 {
6149aea9 15072 warning (_("Nothing to save."));
1042e4c0
SS
15073 return;
15074 }
15075
ee0c3293 15076 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15077
15078 stdio_file fp;
15079
ee0c3293 15080 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15081 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15082 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15083
6149aea9 15084 if (extra_trace_bits)
d7e74731 15085 save_trace_state_variables (&fp);
8bf6485c 15086
6149aea9 15087 ALL_BREAKPOINTS (tp)
1042e4c0 15088 {
6149aea9 15089 /* Skip internal and momentary breakpoints. */
09d682a4 15090 if (!user_breakpoint_p (tp))
6149aea9 15091 continue;
8bf6485c 15092
6149aea9
PA
15093 /* If we have a filter, only save the breakpoints it accepts. */
15094 if (filter && !filter (tp))
15095 continue;
15096
d7e74731 15097 tp->ops->print_recreate (tp, &fp);
1042e4c0 15098
6149aea9
PA
15099 /* Note, we can't rely on tp->number for anything, as we can't
15100 assume the recreated breakpoint numbers will match. Use $bpnum
15101 instead. */
15102
15103 if (tp->cond_string)
d7e74731 15104 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15105
15106 if (tp->ignore_count)
d7e74731 15107 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15108
2d9442cc 15109 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15110 {
d7e74731 15111 fp.puts (" commands\n");
a7bdde9e 15112
d7e74731 15113 current_uiout->redirect (&fp);
492d29ea 15114 TRY
1042e4c0 15115 {
d1b0a7bf 15116 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15117 }
492d29ea
PA
15118 CATCH (ex, RETURN_MASK_ALL)
15119 {
112e8700 15120 current_uiout->redirect (NULL);
492d29ea
PA
15121 throw_exception (ex);
15122 }
15123 END_CATCH
1042e4c0 15124
112e8700 15125 current_uiout->redirect (NULL);
d7e74731 15126 fp.puts (" end\n");
1042e4c0 15127 }
6149aea9
PA
15128
15129 if (tp->enable_state == bp_disabled)
d7e74731 15130 fp.puts ("disable $bpnum\n");
6149aea9
PA
15131
15132 /* If this is a multi-location breakpoint, check if the locations
15133 should be individually disabled. Watchpoint locations are
15134 special, and not user visible. */
15135 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15136 {
15137 struct bp_location *loc;
15138 int n = 1;
15139
15140 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15141 if (!loc->enabled)
d7e74731 15142 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15143 }
1042e4c0 15144 }
8bf6485c 15145
6149aea9 15146 if (extra_trace_bits && *default_collect)
d7e74731 15147 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15148
1042e4c0 15149 if (from_tty)
ee0c3293 15150 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15151}
15152
15153/* The `save breakpoints' command. */
15154
15155static void
4495129a 15156save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15157{
15158 save_breakpoints (args, from_tty, NULL);
15159}
15160
15161/* The `save tracepoints' command. */
15162
15163static void
4495129a 15164save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15165{
15166 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15167}
15168
15169/* Create a vector of all tracepoints. */
15170
15171VEC(breakpoint_p) *
eeae04df 15172all_tracepoints (void)
1042e4c0
SS
15173{
15174 VEC(breakpoint_p) *tp_vec = 0;
15175 struct breakpoint *tp;
15176
15177 ALL_TRACEPOINTS (tp)
15178 {
15179 VEC_safe_push (breakpoint_p, tp_vec, tp);
15180 }
15181
15182 return tp_vec;
15183}
15184
c906108c 15185\f
629500fa
KS
15186/* This help string is used to consolidate all the help string for specifying
15187 locations used by several commands. */
15188
15189#define LOCATION_HELP_STRING \
15190"Linespecs are colon-separated lists of location parameters, such as\n\
15191source filename, function name, label name, and line number.\n\
15192Example: To specify the start of a label named \"the_top\" in the\n\
15193function \"fact\" in the file \"factorial.c\", use\n\
15194\"factorial.c:fact:the_top\".\n\
15195\n\
15196Address locations begin with \"*\" and specify an exact address in the\n\
15197program. Example: To specify the fourth byte past the start function\n\
15198\"main\", use \"*main + 4\".\n\
15199\n\
15200Explicit locations are similar to linespecs but use an option/argument\n\
15201syntax to specify location parameters.\n\
15202Example: To specify the start of the label named \"the_top\" in the\n\
15203function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15204-function fact -label the_top\".\n"
15205
4a64f543
MS
15206/* This help string is used for the break, hbreak, tbreak and thbreak
15207 commands. It is defined as a macro to prevent duplication.
15208 COMMAND should be a string constant containing the name of the
15209 command. */
629500fa 15210
31e2b00f 15211#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15212command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15213PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15214probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15215guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15216`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15217LOCATION may be a linespec, address, or explicit location as described\n\
15218below.\n\
15219\n\
dc10affe
PA
15220With no LOCATION, uses current execution address of the selected\n\
15221stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15222\n\
15223THREADNUM is the number from \"info threads\".\n\
15224CONDITION is a boolean expression.\n\
629500fa 15225\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15226Multiple breakpoints at one place are permitted, and useful if their\n\
15227conditions are different.\n\
31e2b00f
AS
15228\n\
15229Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15230
44feb3ce
TT
15231/* List of subcommands for "catch". */
15232static struct cmd_list_element *catch_cmdlist;
15233
15234/* List of subcommands for "tcatch". */
15235static struct cmd_list_element *tcatch_cmdlist;
15236
9ac4176b 15237void
a121b7c1 15238add_catch_command (const char *name, const char *docstring,
82ae6c8d 15239 cmd_sfunc_ftype *sfunc,
625e8578 15240 completer_ftype *completer,
44feb3ce
TT
15241 void *user_data_catch,
15242 void *user_data_tcatch)
15243{
15244 struct cmd_list_element *command;
15245
0450cc4c 15246 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15247 &catch_cmdlist);
15248 set_cmd_sfunc (command, sfunc);
15249 set_cmd_context (command, user_data_catch);
a96d9b2e 15250 set_cmd_completer (command, completer);
44feb3ce 15251
0450cc4c 15252 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15253 &tcatch_cmdlist);
15254 set_cmd_sfunc (command, sfunc);
15255 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15256 set_cmd_completer (command, completer);
44feb3ce
TT
15257}
15258
6149aea9 15259static void
981a3fb3 15260save_command (const char *arg, int from_tty)
6149aea9 15261{
3e43a32a
MS
15262 printf_unfiltered (_("\"save\" must be followed by "
15263 "the name of a save subcommand.\n"));
635c7e8a 15264 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15265}
15266
84f4c1fe
PM
15267struct breakpoint *
15268iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15269 void *data)
15270{
35df4500 15271 struct breakpoint *b, *b_tmp;
84f4c1fe 15272
35df4500 15273 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15274 {
15275 if ((*callback) (b, data))
15276 return b;
15277 }
15278
15279 return NULL;
15280}
15281
0574c78f
GB
15282/* Zero if any of the breakpoint's locations could be a location where
15283 functions have been inlined, nonzero otherwise. */
15284
15285static int
15286is_non_inline_function (struct breakpoint *b)
15287{
15288 /* The shared library event breakpoint is set on the address of a
15289 non-inline function. */
15290 if (b->type == bp_shlib_event)
15291 return 1;
15292
15293 return 0;
15294}
15295
15296/* Nonzero if the specified PC cannot be a location where functions
15297 have been inlined. */
15298
15299int
accd0bcd 15300pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15301 const struct target_waitstatus *ws)
0574c78f
GB
15302{
15303 struct breakpoint *b;
15304 struct bp_location *bl;
15305
15306 ALL_BREAKPOINTS (b)
15307 {
15308 if (!is_non_inline_function (b))
15309 continue;
15310
15311 for (bl = b->loc; bl != NULL; bl = bl->next)
15312 {
15313 if (!bl->shlib_disabled
09ac7c10 15314 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15315 return 1;
15316 }
15317 }
15318
15319 return 0;
15320}
15321
2f202fde
JK
15322/* Remove any references to OBJFILE which is going to be freed. */
15323
15324void
15325breakpoint_free_objfile (struct objfile *objfile)
15326{
15327 struct bp_location **locp, *loc;
15328
15329 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15330 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15331 loc->symtab = NULL;
15332}
15333
2060206e
PA
15334void
15335initialize_breakpoint_ops (void)
15336{
15337 static int initialized = 0;
15338
15339 struct breakpoint_ops *ops;
15340
15341 if (initialized)
15342 return;
15343 initialized = 1;
15344
15345 /* The breakpoint_ops structure to be inherit by all kinds of
15346 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15347 internal and momentary breakpoints, etc.). */
15348 ops = &bkpt_base_breakpoint_ops;
15349 *ops = base_breakpoint_ops;
15350 ops->re_set = bkpt_re_set;
15351 ops->insert_location = bkpt_insert_location;
15352 ops->remove_location = bkpt_remove_location;
15353 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15354 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15355 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15356 ops->decode_location = bkpt_decode_location;
2060206e
PA
15357
15358 /* The breakpoint_ops structure to be used in regular breakpoints. */
15359 ops = &bkpt_breakpoint_ops;
15360 *ops = bkpt_base_breakpoint_ops;
15361 ops->re_set = bkpt_re_set;
15362 ops->resources_needed = bkpt_resources_needed;
15363 ops->print_it = bkpt_print_it;
15364 ops->print_mention = bkpt_print_mention;
15365 ops->print_recreate = bkpt_print_recreate;
15366
15367 /* Ranged breakpoints. */
15368 ops = &ranged_breakpoint_ops;
15369 *ops = bkpt_breakpoint_ops;
15370 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15371 ops->resources_needed = resources_needed_ranged_breakpoint;
15372 ops->print_it = print_it_ranged_breakpoint;
15373 ops->print_one = print_one_ranged_breakpoint;
15374 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15375 ops->print_mention = print_mention_ranged_breakpoint;
15376 ops->print_recreate = print_recreate_ranged_breakpoint;
15377
15378 /* Internal breakpoints. */
15379 ops = &internal_breakpoint_ops;
15380 *ops = bkpt_base_breakpoint_ops;
15381 ops->re_set = internal_bkpt_re_set;
15382 ops->check_status = internal_bkpt_check_status;
15383 ops->print_it = internal_bkpt_print_it;
15384 ops->print_mention = internal_bkpt_print_mention;
15385
15386 /* Momentary breakpoints. */
15387 ops = &momentary_breakpoint_ops;
15388 *ops = bkpt_base_breakpoint_ops;
15389 ops->re_set = momentary_bkpt_re_set;
15390 ops->check_status = momentary_bkpt_check_status;
15391 ops->print_it = momentary_bkpt_print_it;
15392 ops->print_mention = momentary_bkpt_print_mention;
15393
55aa24fb
SDJ
15394 /* Probe breakpoints. */
15395 ops = &bkpt_probe_breakpoint_ops;
15396 *ops = bkpt_breakpoint_ops;
15397 ops->insert_location = bkpt_probe_insert_location;
15398 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15399 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15400 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15401
2060206e
PA
15402 /* Watchpoints. */
15403 ops = &watchpoint_breakpoint_ops;
15404 *ops = base_breakpoint_ops;
15405 ops->re_set = re_set_watchpoint;
15406 ops->insert_location = insert_watchpoint;
15407 ops->remove_location = remove_watchpoint;
15408 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15409 ops->check_status = check_status_watchpoint;
15410 ops->resources_needed = resources_needed_watchpoint;
15411 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15412 ops->print_it = print_it_watchpoint;
15413 ops->print_mention = print_mention_watchpoint;
15414 ops->print_recreate = print_recreate_watchpoint;
427cd150 15415 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15416
15417 /* Masked watchpoints. */
15418 ops = &masked_watchpoint_breakpoint_ops;
15419 *ops = watchpoint_breakpoint_ops;
15420 ops->insert_location = insert_masked_watchpoint;
15421 ops->remove_location = remove_masked_watchpoint;
15422 ops->resources_needed = resources_needed_masked_watchpoint;
15423 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15424 ops->print_it = print_it_masked_watchpoint;
15425 ops->print_one_detail = print_one_detail_masked_watchpoint;
15426 ops->print_mention = print_mention_masked_watchpoint;
15427 ops->print_recreate = print_recreate_masked_watchpoint;
15428
15429 /* Tracepoints. */
15430 ops = &tracepoint_breakpoint_ops;
15431 *ops = base_breakpoint_ops;
15432 ops->re_set = tracepoint_re_set;
15433 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15434 ops->print_one_detail = tracepoint_print_one_detail;
15435 ops->print_mention = tracepoint_print_mention;
15436 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15437 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15438 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15439 ops->decode_location = tracepoint_decode_location;
983af33b 15440
55aa24fb
SDJ
15441 /* Probe tracepoints. */
15442 ops = &tracepoint_probe_breakpoint_ops;
15443 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15444 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15445 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15446
983af33b
SDJ
15447 /* Static tracepoints with marker (`-m'). */
15448 ops = &strace_marker_breakpoint_ops;
15449 *ops = tracepoint_breakpoint_ops;
5f700d83 15450 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15451 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15452 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15453
15454 /* Fork catchpoints. */
15455 ops = &catch_fork_breakpoint_ops;
15456 *ops = base_breakpoint_ops;
15457 ops->insert_location = insert_catch_fork;
15458 ops->remove_location = remove_catch_fork;
15459 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15460 ops->print_it = print_it_catch_fork;
15461 ops->print_one = print_one_catch_fork;
15462 ops->print_mention = print_mention_catch_fork;
15463 ops->print_recreate = print_recreate_catch_fork;
15464
15465 /* Vfork catchpoints. */
15466 ops = &catch_vfork_breakpoint_ops;
15467 *ops = base_breakpoint_ops;
15468 ops->insert_location = insert_catch_vfork;
15469 ops->remove_location = remove_catch_vfork;
15470 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15471 ops->print_it = print_it_catch_vfork;
15472 ops->print_one = print_one_catch_vfork;
15473 ops->print_mention = print_mention_catch_vfork;
15474 ops->print_recreate = print_recreate_catch_vfork;
15475
15476 /* Exec catchpoints. */
15477 ops = &catch_exec_breakpoint_ops;
15478 *ops = base_breakpoint_ops;
2060206e
PA
15479 ops->insert_location = insert_catch_exec;
15480 ops->remove_location = remove_catch_exec;
15481 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15482 ops->print_it = print_it_catch_exec;
15483 ops->print_one = print_one_catch_exec;
15484 ops->print_mention = print_mention_catch_exec;
15485 ops->print_recreate = print_recreate_catch_exec;
15486
edcc5120
TT
15487 /* Solib-related catchpoints. */
15488 ops = &catch_solib_breakpoint_ops;
15489 *ops = base_breakpoint_ops;
edcc5120
TT
15490 ops->insert_location = insert_catch_solib;
15491 ops->remove_location = remove_catch_solib;
15492 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15493 ops->check_status = check_status_catch_solib;
15494 ops->print_it = print_it_catch_solib;
15495 ops->print_one = print_one_catch_solib;
15496 ops->print_mention = print_mention_catch_solib;
15497 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15498
15499 ops = &dprintf_breakpoint_ops;
15500 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15501 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15502 ops->resources_needed = bkpt_resources_needed;
15503 ops->print_it = bkpt_print_it;
15504 ops->print_mention = bkpt_print_mention;
2d9442cc 15505 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15506 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15507 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15508}
15509
8bfd80db
YQ
15510/* Chain containing all defined "enable breakpoint" subcommands. */
15511
15512static struct cmd_list_element *enablebreaklist = NULL;
15513
c906108c 15514void
fba45db2 15515_initialize_breakpoint (void)
c906108c
SS
15516{
15517 struct cmd_list_element *c;
15518
2060206e
PA
15519 initialize_breakpoint_ops ();
15520
84acb35a 15521 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15522 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15523 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15524
55aa24fb 15525 breakpoint_objfile_key
43dce439 15526 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
17450429 15527
c906108c
SS
15528 breakpoint_chain = 0;
15529 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15530 before a breakpoint is set. */
15531 breakpoint_count = 0;
15532
1042e4c0
SS
15533 tracepoint_count = 0;
15534
1bedd215
AC
15535 add_com ("ignore", class_breakpoint, ignore_command, _("\
15536Set ignore-count of breakpoint number N to COUNT.\n\
15537Usage is `ignore N COUNT'."));
c906108c 15538
1bedd215 15539 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15540Set commands to be executed when the given breakpoints are hit.\n\
15541Give a space-separated breakpoint list as argument after \"commands\".\n\
15542A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15543(e.g. `5-7').\n\
c906108c
SS
15544With no argument, the targeted breakpoint is the last one set.\n\
15545The commands themselves follow starting on the next line.\n\
15546Type a line containing \"end\" to indicate the end of them.\n\
15547Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15548then no output is printed when it is hit, except what the commands print."));
c906108c 15549
d55637df 15550 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15551Specify breakpoint number N to break only if COND is true.\n\
c906108c 15552Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15553expression to be evaluated whenever breakpoint N is reached."));
d55637df 15554 set_cmd_completer (c, condition_completer);
c906108c 15555
1bedd215 15556 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15557Set a temporary breakpoint.\n\
c906108c
SS
15558Like \"break\" except the breakpoint is only temporary,\n\
15559so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15560by using \"enable delete\" on the breakpoint number.\n\
15561\n"
15562BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15563 set_cmd_completer (c, location_completer);
c94fdfd0 15564
1bedd215 15565 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15566Set a hardware assisted breakpoint.\n\
c906108c 15567Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15568some target hardware may not have this support.\n\
15569\n"
15570BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15571 set_cmd_completer (c, location_completer);
c906108c 15572
1bedd215 15573 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15574Set a temporary hardware assisted breakpoint.\n\
c906108c 15575Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15576so it will be deleted when hit.\n\
15577\n"
15578BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15579 set_cmd_completer (c, location_completer);
c906108c 15580
1bedd215
AC
15581 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15582Enable some breakpoints.\n\
c906108c
SS
15583Give breakpoint numbers (separated by spaces) as arguments.\n\
15584With no subcommand, breakpoints are enabled until you command otherwise.\n\
15585This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15586With a subcommand you can enable temporarily."),
c906108c 15587 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15588
15589 add_com_alias ("en", "enable", class_breakpoint, 1);
15590
84951ab5 15591 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15592Enable some breakpoints.\n\
c906108c
SS
15593Give breakpoint numbers (separated by spaces) as arguments.\n\
15594This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15595May be abbreviated to simply \"enable\".\n"),
c5aa993b 15596 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15597
1a966eab
AC
15598 add_cmd ("once", no_class, enable_once_command, _("\
15599Enable breakpoints for one hit. Give breakpoint numbers.\n\
15600If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15601 &enablebreaklist);
15602
1a966eab
AC
15603 add_cmd ("delete", no_class, enable_delete_command, _("\
15604Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15605If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15606 &enablebreaklist);
15607
816338b5
SS
15608 add_cmd ("count", no_class, enable_count_command, _("\
15609Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15610If a breakpoint is hit while enabled in this fashion,\n\
15611the count is decremented; when it reaches zero, the breakpoint is disabled."),
15612 &enablebreaklist);
15613
1a966eab
AC
15614 add_cmd ("delete", no_class, enable_delete_command, _("\
15615Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15616If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15617 &enablelist);
15618
1a966eab
AC
15619 add_cmd ("once", no_class, enable_once_command, _("\
15620Enable breakpoints for one hit. Give breakpoint numbers.\n\
15621If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15622 &enablelist);
15623
15624 add_cmd ("count", no_class, enable_count_command, _("\
15625Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15626If a breakpoint is hit while enabled in this fashion,\n\
15627the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15628 &enablelist);
15629
1bedd215
AC
15630 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15631Disable some breakpoints.\n\
c906108c
SS
15632Arguments are breakpoint numbers with spaces in between.\n\
15633To disable all breakpoints, give no argument.\n\
64b9b334 15634A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15635 &disablelist, "disable ", 1, &cmdlist);
15636 add_com_alias ("dis", "disable", class_breakpoint, 1);
15637 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15638
1a966eab
AC
15639 add_cmd ("breakpoints", class_alias, disable_command, _("\
15640Disable some breakpoints.\n\
c906108c
SS
15641Arguments are breakpoint numbers with spaces in between.\n\
15642To disable all breakpoints, give no argument.\n\
64b9b334 15643A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15644This command may be abbreviated \"disable\"."),
c906108c
SS
15645 &disablelist);
15646
1bedd215
AC
15647 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15648Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15649Arguments are breakpoint numbers with spaces in between.\n\
15650To delete all breakpoints, give no argument.\n\
15651\n\
15652Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15653The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15654 &deletelist, "delete ", 1, &cmdlist);
15655 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15656 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15657
1a966eab
AC
15658 add_cmd ("breakpoints", class_alias, delete_command, _("\
15659Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15660Arguments are breakpoint numbers with spaces in between.\n\
15661To delete all breakpoints, give no argument.\n\
1a966eab 15662This command may be abbreviated \"delete\"."),
c906108c
SS
15663 &deletelist);
15664
1bedd215 15665 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15666Clear breakpoint at specified location.\n\
15667Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15668\n\
15669With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15670is executing in.\n"
15671"\n" LOCATION_HELP_STRING "\n\
1bedd215 15672See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15673 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15674
1bedd215 15675 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15676Set breakpoint at specified location.\n"
31e2b00f 15677BREAK_ARGS_HELP ("break")));
5ba2abeb 15678 set_cmd_completer (c, location_completer);
c94fdfd0 15679
c906108c
SS
15680 add_com_alias ("b", "break", class_run, 1);
15681 add_com_alias ("br", "break", class_run, 1);
15682 add_com_alias ("bre", "break", class_run, 1);
15683 add_com_alias ("brea", "break", class_run, 1);
15684
c906108c
SS
15685 if (dbx_commands)
15686 {
1bedd215
AC
15687 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15688Break in function/address or break at a line in the current file."),
c5aa993b
JM
15689 &stoplist, "stop ", 1, &cmdlist);
15690 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15691 _("Break in function or address."), &stoplist);
c5aa993b 15692 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15693 _("Break at a line in the current file."), &stoplist);
11db9430 15694 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15695Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15696The \"Type\" column indicates one of:\n\
15697\tbreakpoint - normal breakpoint\n\
15698\twatchpoint - watchpoint\n\
15699The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15700the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15701breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15702address and file/line number respectively.\n\
15703\n\
15704Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15705are set to the address of the last breakpoint listed unless the command\n\
15706is prefixed with \"server \".\n\n\
c906108c 15707Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15708breakpoint set."));
c906108c
SS
15709 }
15710
11db9430 15711 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15712Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15713The \"Type\" column indicates one of:\n\
15714\tbreakpoint - normal breakpoint\n\
15715\twatchpoint - watchpoint\n\
15716The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15717the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15718breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15719address and file/line number respectively.\n\
15720\n\
15721Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15722are set to the address of the last breakpoint listed unless the command\n\
15723is prefixed with \"server \".\n\n\
c906108c 15724Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15725breakpoint set."));
c906108c 15726
6b04bdb7
MS
15727 add_info_alias ("b", "breakpoints", 1);
15728
1a966eab
AC
15729 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15730Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15731The \"Type\" column indicates one of:\n\
15732\tbreakpoint - normal breakpoint\n\
15733\twatchpoint - watchpoint\n\
15734\tlongjmp - internal breakpoint used to step through longjmp()\n\
15735\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15736\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15737\tfinish - internal breakpoint used by the \"finish\" command\n\
15738The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15739the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15740breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15741address and file/line number respectively.\n\
15742\n\
15743Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15744are set to the address of the last breakpoint listed unless the command\n\
15745is prefixed with \"server \".\n\n\
c906108c 15746Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15747breakpoint set."),
c906108c
SS
15748 &maintenanceinfolist);
15749
44feb3ce
TT
15750 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15751Set catchpoints to catch events."),
15752 &catch_cmdlist, "catch ",
15753 0/*allow-unknown*/, &cmdlist);
15754
15755 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15756Set temporary catchpoints to catch events."),
15757 &tcatch_cmdlist, "tcatch ",
15758 0/*allow-unknown*/, &cmdlist);
15759
44feb3ce
TT
15760 add_catch_command ("fork", _("Catch calls to fork."),
15761 catch_fork_command_1,
a96d9b2e 15762 NULL,
44feb3ce
TT
15763 (void *) (uintptr_t) catch_fork_permanent,
15764 (void *) (uintptr_t) catch_fork_temporary);
15765 add_catch_command ("vfork", _("Catch calls to vfork."),
15766 catch_fork_command_1,
a96d9b2e 15767 NULL,
44feb3ce
TT
15768 (void *) (uintptr_t) catch_vfork_permanent,
15769 (void *) (uintptr_t) catch_vfork_temporary);
15770 add_catch_command ("exec", _("Catch calls to exec."),
15771 catch_exec_command_1,
a96d9b2e
SDJ
15772 NULL,
15773 CATCH_PERMANENT,
15774 CATCH_TEMPORARY);
edcc5120
TT
15775 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15776Usage: catch load [REGEX]\n\
15777If REGEX is given, only stop for libraries matching the regular expression."),
15778 catch_load_command_1,
15779 NULL,
15780 CATCH_PERMANENT,
15781 CATCH_TEMPORARY);
15782 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15783Usage: catch unload [REGEX]\n\
15784If REGEX is given, only stop for libraries matching the regular expression."),
15785 catch_unload_command_1,
15786 NULL,
15787 CATCH_PERMANENT,
15788 CATCH_TEMPORARY);
c5aa993b 15789
1bedd215
AC
15790 c = add_com ("watch", class_breakpoint, watch_command, _("\
15791Set a watchpoint for an expression.\n\
06a64a0b 15792Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15793A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15794an expression changes.\n\
15795If -l or -location is given, this evaluates EXPRESSION and watches\n\
15796the memory to which it refers."));
65d12d83 15797 set_cmd_completer (c, expression_completer);
c906108c 15798
1bedd215
AC
15799 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15800Set a read watchpoint for an expression.\n\
06a64a0b 15801Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15802A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15803an expression is read.\n\
15804If -l or -location is given, this evaluates EXPRESSION and watches\n\
15805the memory to which it refers."));
65d12d83 15806 set_cmd_completer (c, expression_completer);
c906108c 15807
1bedd215
AC
15808 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15809Set a watchpoint for an expression.\n\
06a64a0b 15810Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15811A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15812an expression is either read or written.\n\
15813If -l or -location is given, this evaluates EXPRESSION and watches\n\
15814the memory to which it refers."));
65d12d83 15815 set_cmd_completer (c, expression_completer);
c906108c 15816
11db9430 15817 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15818Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15819
920d2a44
AC
15820 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15821 respond to changes - contrary to the description. */
85c07804
AC
15822 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15823 &can_use_hw_watchpoints, _("\
15824Set debugger's willingness to use watchpoint hardware."), _("\
15825Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15826If zero, gdb will not use hardware for new watchpoints, even if\n\
15827such is available. (However, any hardware watchpoints that were\n\
15828created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15829hardware.)"),
15830 NULL,
920d2a44 15831 show_can_use_hw_watchpoints,
85c07804 15832 &setlist, &showlist);
c906108c
SS
15833
15834 can_use_hw_watchpoints = 1;
fa8d40ab 15835
1042e4c0
SS
15836 /* Tracepoint manipulation commands. */
15837
15838 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15839Set a tracepoint at specified location.\n\
1042e4c0
SS
15840\n"
15841BREAK_ARGS_HELP ("trace") "\n\
15842Do \"help tracepoints\" for info on other tracepoint commands."));
15843 set_cmd_completer (c, location_completer);
15844
15845 add_com_alias ("tp", "trace", class_alias, 0);
15846 add_com_alias ("tr", "trace", class_alias, 1);
15847 add_com_alias ("tra", "trace", class_alias, 1);
15848 add_com_alias ("trac", "trace", class_alias, 1);
15849
7a697b8d 15850 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15851Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15852\n"
15853BREAK_ARGS_HELP ("ftrace") "\n\
15854Do \"help tracepoints\" for info on other tracepoint commands."));
15855 set_cmd_completer (c, location_completer);
15856
0fb4aa4b 15857 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15858Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15859\n\
15860strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15861LOCATION may be a linespec, explicit, or address location (described below) \n\
15862or -m MARKER_ID.\n\n\
15863If a marker id is specified, probe the marker with that name. With\n\
15864no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15865Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15866This collects arbitrary user data passed in the probe point call to the\n\
15867tracing library. You can inspect it when analyzing the trace buffer,\n\
15868by printing the $_sdata variable like any other convenience variable.\n\
15869\n\
15870CONDITION is a boolean expression.\n\
629500fa 15871\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15872Multiple tracepoints at one place are permitted, and useful if their\n\
15873conditions are different.\n\
0fb4aa4b
PA
15874\n\
15875Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15876Do \"help tracepoints\" for info on other tracepoint commands."));
15877 set_cmd_completer (c, location_completer);
15878
11db9430 15879 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15880Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15881Convenience variable \"$tpnum\" contains the number of the\n\
15882last tracepoint set."));
15883
15884 add_info_alias ("tp", "tracepoints", 1);
15885
15886 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15887Delete specified tracepoints.\n\
15888Arguments are tracepoint numbers, separated by spaces.\n\
15889No argument means delete all tracepoints."),
15890 &deletelist);
7e20dfcd 15891 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15892
15893 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15894Disable specified tracepoints.\n\
15895Arguments are tracepoint numbers, separated by spaces.\n\
15896No argument means disable all tracepoints."),
15897 &disablelist);
15898 deprecate_cmd (c, "disable");
15899
15900 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15901Enable specified tracepoints.\n\
15902Arguments are tracepoint numbers, separated by spaces.\n\
15903No argument means enable all tracepoints."),
15904 &enablelist);
15905 deprecate_cmd (c, "enable");
15906
15907 add_com ("passcount", class_trace, trace_pass_command, _("\
15908Set the passcount for a tracepoint.\n\
15909The trace will end when the tracepoint has been passed 'count' times.\n\
15910Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
15911if TPNUM is omitted, passcount refers to the last tracepoint defined."));
15912
6149aea9
PA
15913 add_prefix_cmd ("save", class_breakpoint, save_command,
15914 _("Save breakpoint definitions as a script."),
15915 &save_cmdlist, "save ",
15916 0/*allow-unknown*/, &cmdlist);
15917
15918 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
15919Save current breakpoint definitions as a script.\n\
cce7e648 15920This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
15921catchpoints, tracepoints). Use the 'source' command in another debug\n\
15922session to restore them."),
15923 &save_cmdlist);
15924 set_cmd_completer (c, filename_completer);
15925
15926 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 15927Save current tracepoint definitions as a script.\n\
6149aea9
PA
15928Use the 'source' command in another debug session to restore them."),
15929 &save_cmdlist);
1042e4c0
SS
15930 set_cmd_completer (c, filename_completer);
15931
6149aea9
PA
15932 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
15933 deprecate_cmd (c, "save tracepoints");
15934
1bedd215 15935 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
15936Breakpoint specific settings\n\
15937Configure various breakpoint-specific variables such as\n\
1bedd215 15938pending breakpoint behavior"),
fa8d40ab
JJ
15939 &breakpoint_set_cmdlist, "set breakpoint ",
15940 0/*allow-unknown*/, &setlist);
1bedd215 15941 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
15942Breakpoint specific settings\n\
15943Configure various breakpoint-specific variables such as\n\
1bedd215 15944pending breakpoint behavior"),
fa8d40ab
JJ
15945 &breakpoint_show_cmdlist, "show breakpoint ",
15946 0/*allow-unknown*/, &showlist);
15947
7915a72c
AC
15948 add_setshow_auto_boolean_cmd ("pending", no_class,
15949 &pending_break_support, _("\
15950Set debugger's behavior regarding pending breakpoints."), _("\
15951Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
15952If on, an unrecognized breakpoint location will cause gdb to create a\n\
15953pending breakpoint. If off, an unrecognized breakpoint location results in\n\
15954an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 15955user-query to see if a pending breakpoint should be created."),
2c5b56ce 15956 NULL,
920d2a44 15957 show_pending_break_support,
6e1d7d6c
AC
15958 &breakpoint_set_cmdlist,
15959 &breakpoint_show_cmdlist);
fa8d40ab
JJ
15960
15961 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
15962
15963 add_setshow_boolean_cmd ("auto-hw", no_class,
15964 &automatic_hardware_breakpoints, _("\
15965Set automatic usage of hardware breakpoints."), _("\
15966Show automatic usage of hardware breakpoints."), _("\
15967If set, the debugger will automatically use hardware breakpoints for\n\
15968breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
15969a warning will be emitted for such breakpoints."),
15970 NULL,
15971 show_automatic_hardware_breakpoints,
15972 &breakpoint_set_cmdlist,
15973 &breakpoint_show_cmdlist);
74960c60 15974
a25a5a45
PA
15975 add_setshow_boolean_cmd ("always-inserted", class_support,
15976 &always_inserted_mode, _("\
74960c60
VP
15977Set mode for inserting breakpoints."), _("\
15978Show mode for inserting breakpoints."), _("\
a25a5a45
PA
15979When this mode is on, breakpoints are inserted immediately as soon as\n\
15980they're created, kept inserted even when execution stops, and removed\n\
15981only when the user deletes them. When this mode is off (the default),\n\
15982breakpoints are inserted only when execution continues, and removed\n\
15983when execution stops."),
72d0e2c5
YQ
15984 NULL,
15985 &show_always_inserted_mode,
15986 &breakpoint_set_cmdlist,
15987 &breakpoint_show_cmdlist);
f1310107 15988
b775012e
LM
15989 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
15990 condition_evaluation_enums,
15991 &condition_evaluation_mode_1, _("\
15992Set mode of breakpoint condition evaluation."), _("\
15993Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 15994When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
15995evaluated on the host's side by GDB. When it is set to \"target\",\n\
15996breakpoint conditions will be downloaded to the target (if the target\n\
15997supports such feature) and conditions will be evaluated on the target's side.\n\
15998If this is set to \"auto\" (default), this will be automatically set to\n\
15999\"target\" if it supports condition evaluation, otherwise it will\n\
16000be set to \"gdb\""),
16001 &set_condition_evaluation_mode,
16002 &show_condition_evaluation_mode,
16003 &breakpoint_set_cmdlist,
16004 &breakpoint_show_cmdlist);
16005
f1310107
TJB
16006 add_com ("break-range", class_breakpoint, break_range_command, _("\
16007Set a breakpoint for an address range.\n\
16008break-range START-LOCATION, END-LOCATION\n\
16009where START-LOCATION and END-LOCATION can be one of the following:\n\
16010 LINENUM, for that line in the current file,\n\
16011 FILE:LINENUM, for that line in that file,\n\
16012 +OFFSET, for that number of lines after the current line\n\
16013 or the start of the range\n\
16014 FUNCTION, for the first line in that function,\n\
16015 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16016 *ADDRESS, for the instruction at that address.\n\
16017\n\
16018The breakpoint will stop execution of the inferior whenever it executes\n\
16019an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16020range (including START-LOCATION and END-LOCATION)."));
16021
e7e0cddf 16022 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16023Set a dynamic printf at specified location.\n\
e7e0cddf 16024dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16025location may be a linespec, explicit, or address location.\n"
16026"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16027 set_cmd_completer (c, location_completer);
16028
16029 add_setshow_enum_cmd ("dprintf-style", class_support,
16030 dprintf_style_enums, &dprintf_style, _("\
16031Set the style of usage for dynamic printf."), _("\
16032Show the style of usage for dynamic printf."), _("\
16033This setting chooses how GDB will do a dynamic printf.\n\
16034If the value is \"gdb\", then the printing is done by GDB to its own\n\
16035console, as with the \"printf\" command.\n\
16036If the value is \"call\", the print is done by calling a function in your\n\
16037program; by default printf(), but you can choose a different function or\n\
16038output stream by setting dprintf-function and dprintf-channel."),
16039 update_dprintf_commands, NULL,
16040 &setlist, &showlist);
16041
16042 dprintf_function = xstrdup ("printf");
16043 add_setshow_string_cmd ("dprintf-function", class_support,
16044 &dprintf_function, _("\
16045Set the function to use for dynamic printf"), _("\
16046Show the function to use for dynamic printf"), NULL,
16047 update_dprintf_commands, NULL,
16048 &setlist, &showlist);
16049
16050 dprintf_channel = xstrdup ("");
16051 add_setshow_string_cmd ("dprintf-channel", class_support,
16052 &dprintf_channel, _("\
16053Set the channel to use for dynamic printf"), _("\
16054Show the channel to use for dynamic printf"), NULL,
16055 update_dprintf_commands, NULL,
16056 &setlist, &showlist);
16057
d3ce09f5
SS
16058 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16059 &disconnected_dprintf, _("\
16060Set whether dprintf continues after GDB disconnects."), _("\
16061Show whether dprintf continues after GDB disconnects."), _("\
16062Use this to let dprintf commands continue to hit and produce output\n\
16063even if GDB disconnects or detaches from the target."),
16064 NULL,
16065 NULL,
16066 &setlist, &showlist);
16067
16068 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16069agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16070(target agent only) This is useful for formatted output in user-defined commands."));
16071
765dc015 16072 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16073
16074 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16075 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16076}
This page took 3.361582 seconds and 4 git commands to generate.