Eliminate catch_errors
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
61baf725 3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5 69#include "format.h"
cfc31633 70#include "thread-fsm.h"
5d5658a1 71#include "tid-parse.h"
d3ce09f5 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
325fac50 82#include <algorithm>
5ed8105e 83#include "progspace-and-thread.h"
6c5b2ebe 84#include "common/array-view.h"
76f9c9cf 85#include "common/gdb_optional.h"
104c1213 86
e7e8980f
YQ
87/* Enums for exception-handling support. */
88enum exception_event_kind
89{
90 EX_EVENT_THROW,
591f19e8 91 EX_EVENT_RETHROW,
e7e8980f
YQ
92 EX_EVENT_CATCH
93};
94
4a64f543 95/* Prototypes for local functions. */
c906108c 96
a14ed312 97static void disable_command (char *, int);
c906108c 98
a14ed312 99static void enable_command (char *, int);
c906108c 100
896b6bda 101static void map_breakpoint_numbers (const char *,
48649e1b 102 gdb::function_view<void (breakpoint *)>);
c906108c 103
a14ed312 104static void ignore_command (char *, int);
c906108c 105
348d480f
PA
106static void breakpoint_re_set_default (struct breakpoint *);
107
f00aae0f
KS
108static void
109 create_sals_from_location_default (const struct event_location *location,
110 struct linespec_result *canonical,
111 enum bptype type_wanted);
983af33b
SDJ
112
113static void create_breakpoints_sal_default (struct gdbarch *,
114 struct linespec_result *,
e1e01040
PA
115 gdb::unique_xmalloc_ptr<char>,
116 gdb::unique_xmalloc_ptr<char>,
117 enum bptype,
983af33b
SDJ
118 enum bpdisp, int, int,
119 int,
120 const struct breakpoint_ops *,
44f238bb 121 int, int, int, unsigned);
983af33b 122
6c5b2ebe
PA
123static std::vector<symtab_and_line> decode_location_default
124 (struct breakpoint *b, const struct event_location *location,
125 struct program_space *search_pspace);
983af33b 126
a14ed312 127static void clear_command (char *, int);
c906108c 128
a14ed312 129static void catch_command (char *, int);
c906108c 130
a9634178 131static int can_use_hardware_watchpoint (struct value *);
c906108c 132
a14ed312 133static void mention (struct breakpoint *);
c906108c 134
348d480f
PA
135static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
136 enum bptype,
c0a91b2b 137 const struct breakpoint_ops *);
3742cc8b
YQ
138static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
139 const struct symtab_and_line *);
140
4a64f543
MS
141/* This function is used in gdbtk sources and thus can not be made
142 static. */
63c252f8 143struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 144 struct symtab_and_line,
c0a91b2b
TT
145 enum bptype,
146 const struct breakpoint_ops *);
c906108c 147
06edf0c0
PA
148static struct breakpoint *
149 momentary_breakpoint_from_master (struct breakpoint *orig,
150 enum bptype type,
a1aa2221
LM
151 const struct breakpoint_ops *ops,
152 int loc_enabled);
06edf0c0 153
76897487
KB
154static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
155
a6d9a66e
UW
156static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
157 CORE_ADDR bpaddr,
88f7da05 158 enum bptype bptype);
76897487 159
6c95b8df
PA
160static void describe_other_breakpoints (struct gdbarch *,
161 struct program_space *, CORE_ADDR,
5af949e3 162 struct obj_section *, int);
c906108c 163
85d721b8
PA
164static int watchpoint_locations_match (struct bp_location *loc1,
165 struct bp_location *loc2);
166
f1310107
TJB
167static int breakpoint_location_address_match (struct bp_location *bl,
168 struct address_space *aspace,
169 CORE_ADDR addr);
170
d35ae833
PA
171static int breakpoint_location_address_range_overlap (struct bp_location *,
172 struct address_space *,
173 CORE_ADDR, int);
174
11db9430 175static void info_breakpoints_command (char *, int);
c906108c 176
11db9430 177static void info_watchpoints_command (char *, int);
d77f58be 178
4efb68b1 179static void cleanup_executing_breakpoints (void *);
c906108c 180
a14ed312 181static void commands_command (char *, int);
c906108c 182
a14ed312 183static void condition_command (char *, int);
c906108c 184
834c0d03 185static int remove_breakpoint (struct bp_location *);
b2b6a7da 186static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 187
e514a9d6 188static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 189
a14ed312 190static int hw_breakpoint_used_count (void);
c906108c 191
a1398e0c
PA
192static int hw_watchpoint_use_count (struct breakpoint *);
193
194static int hw_watchpoint_used_count_others (struct breakpoint *except,
195 enum bptype type,
196 int *other_type_used);
c906108c 197
a14ed312 198static void hbreak_command (char *, int);
c906108c 199
a14ed312 200static void thbreak_command (char *, int);
c906108c 201
816338b5
SS
202static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
203 int count);
c906108c 204
a14ed312 205static void stop_command (char *arg, int from_tty);
7a292a7a 206
a14ed312 207static void tcatch_command (char *arg, int from_tty);
7a292a7a 208
fe3f5fa8 209static void free_bp_location (struct bp_location *loc);
f431efe5
PA
210static void incref_bp_location (struct bp_location *loc);
211static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 212
39d61571 213static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 214
44702360
PA
215/* update_global_location_list's modes of operation wrt to whether to
216 insert locations now. */
217enum ugll_insert_mode
218{
219 /* Don't insert any breakpoint locations into the inferior, only
220 remove already-inserted locations that no longer should be
221 inserted. Functions that delete a breakpoint or breakpoints
222 should specify this mode, so that deleting a breakpoint doesn't
223 have the side effect of inserting the locations of other
224 breakpoints that are marked not-inserted, but should_be_inserted
225 returns true on them.
226
227 This behavior is useful is situations close to tear-down -- e.g.,
228 after an exec, while the target still has execution, but
229 breakpoint shadows of the previous executable image should *NOT*
230 be restored to the new image; or before detaching, where the
231 target still has execution and wants to delete breakpoints from
232 GDB's lists, and all breakpoints had already been removed from
233 the inferior. */
234 UGLL_DONT_INSERT,
235
a25a5a45
PA
236 /* May insert breakpoints iff breakpoints_should_be_inserted_now
237 claims breakpoints should be inserted now. */
04086b45
PA
238 UGLL_MAY_INSERT,
239
a25a5a45
PA
240 /* Insert locations now, irrespective of
241 breakpoints_should_be_inserted_now. E.g., say all threads are
242 stopped right now, and the user did "continue". We need to
243 insert breakpoints _before_ resuming the target, but
244 UGLL_MAY_INSERT wouldn't insert them, because
245 breakpoints_should_be_inserted_now returns false at that point,
246 as no thread is running yet. */
04086b45 247 UGLL_INSERT
44702360
PA
248};
249
250static void update_global_location_list (enum ugll_insert_mode);
a5606eee 251
44702360 252static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 253
d77f58be 254static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
255
256static void insert_breakpoint_locations (void);
a5606eee 257
11db9430 258static void info_tracepoints_command (char *, int);
1042e4c0 259
1042e4c0
SS
260static void enable_trace_command (char *, int);
261
262static void disable_trace_command (char *, int);
263
264static void trace_pass_command (char *, int);
265
558a9d82
YQ
266static void set_tracepoint_count (int num);
267
9c06b0b4
TJB
268static int is_masked_watchpoint (const struct breakpoint *b);
269
b775012e
LM
270static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
271
983af33b
SDJ
272/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
273 otherwise. */
274
275static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 276
2060206e
PA
277/* The breakpoint_ops structure to be inherited by all breakpoint_ops
278 that are implemented on top of software or hardware breakpoints
279 (user breakpoints, internal and momentary breakpoints, etc.). */
280static struct breakpoint_ops bkpt_base_breakpoint_ops;
281
282/* Internal breakpoints class type. */
06edf0c0 283static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
284
285/* Momentary breakpoints class type. */
06edf0c0
PA
286static struct breakpoint_ops momentary_breakpoint_ops;
287
2060206e
PA
288/* The breakpoint_ops structure to be used in regular user created
289 breakpoints. */
290struct breakpoint_ops bkpt_breakpoint_ops;
291
55aa24fb
SDJ
292/* Breakpoints set on probes. */
293static struct breakpoint_ops bkpt_probe_breakpoint_ops;
294
e7e0cddf 295/* Dynamic printf class type. */
c5867ab6 296struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 297
d3ce09f5
SS
298/* The style in which to perform a dynamic printf. This is a user
299 option because different output options have different tradeoffs;
300 if GDB does the printing, there is better error handling if there
301 is a problem with any of the arguments, but using an inferior
302 function lets you have special-purpose printers and sending of
303 output to the same place as compiled-in print functions. */
304
305static const char dprintf_style_gdb[] = "gdb";
306static const char dprintf_style_call[] = "call";
307static const char dprintf_style_agent[] = "agent";
308static const char *const dprintf_style_enums[] = {
309 dprintf_style_gdb,
310 dprintf_style_call,
311 dprintf_style_agent,
312 NULL
313};
314static const char *dprintf_style = dprintf_style_gdb;
315
316/* The function to use for dynamic printf if the preferred style is to
317 call into the inferior. The value is simply a string that is
318 copied into the command, so it can be anything that GDB can
319 evaluate to a callable address, not necessarily a function name. */
320
bde6261a 321static char *dprintf_function;
d3ce09f5
SS
322
323/* The channel to use for dynamic printf if the preferred style is to
324 call into the inferior; if a nonempty string, it will be passed to
325 the call as the first argument, with the format string as the
326 second. As with the dprintf function, this can be anything that
327 GDB knows how to evaluate, so in addition to common choices like
328 "stderr", this could be an app-specific expression like
329 "mystreams[curlogger]". */
330
bde6261a 331static char *dprintf_channel;
d3ce09f5
SS
332
333/* True if dprintf commands should continue to operate even if GDB
334 has disconnected. */
335static int disconnected_dprintf = 1;
336
5cea2a26
PA
337struct command_line *
338breakpoint_commands (struct breakpoint *b)
339{
d1b0a7bf 340 return b->commands ? b->commands.get () : NULL;
5cea2a26 341}
3daf8fe5 342
f3b1572e
PA
343/* Flag indicating that a command has proceeded the inferior past the
344 current breakpoint. */
345
346static int breakpoint_proceeded;
347
956a9fb9 348const char *
2cec12e5
AR
349bpdisp_text (enum bpdisp disp)
350{
4a64f543
MS
351 /* NOTE: the following values are a part of MI protocol and
352 represent values of 'disp' field returned when inferior stops at
353 a breakpoint. */
bc043ef3 354 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 355
2cec12e5
AR
356 return bpdisps[(int) disp];
357}
c906108c 358
4a64f543 359/* Prototypes for exported functions. */
c906108c 360/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 361 if such is available. */
c906108c
SS
362static int can_use_hw_watchpoints;
363
920d2a44
AC
364static void
365show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
366 struct cmd_list_element *c,
367 const char *value)
368{
3e43a32a
MS
369 fprintf_filtered (file,
370 _("Debugger's willingness to use "
371 "watchpoint hardware is %s.\n"),
920d2a44
AC
372 value);
373}
374
fa8d40ab
JJ
375/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
376 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 377 for unrecognized breakpoint locations.
fa8d40ab
JJ
378 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
379static enum auto_boolean pending_break_support;
920d2a44
AC
380static void
381show_pending_break_support (struct ui_file *file, int from_tty,
382 struct cmd_list_element *c,
383 const char *value)
384{
3e43a32a
MS
385 fprintf_filtered (file,
386 _("Debugger's behavior regarding "
387 "pending breakpoints is %s.\n"),
920d2a44
AC
388 value);
389}
fa8d40ab 390
765dc015 391/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 392 set with "break" but falling in read-only memory.
765dc015
VP
393 If 0, gdb will warn about such breakpoints, but won't automatically
394 use hardware breakpoints. */
395static int automatic_hardware_breakpoints;
396static void
397show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
398 struct cmd_list_element *c,
399 const char *value)
400{
3e43a32a
MS
401 fprintf_filtered (file,
402 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
403 value);
404}
405
a25a5a45
PA
406/* If on, GDB keeps breakpoints inserted even if the inferior is
407 stopped, and immediately inserts any new breakpoints as soon as
408 they're created. If off (default), GDB keeps breakpoints off of
409 the target as long as possible. That is, it delays inserting
410 breakpoints until the next resume, and removes them again when the
411 target fully stops. This is a bit safer in case GDB crashes while
412 processing user input. */
413static int always_inserted_mode = 0;
72d0e2c5 414
33e5cbd6 415static void
74960c60 416show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 417 struct cmd_list_element *c, const char *value)
74960c60 418{
a25a5a45
PA
419 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
420 value);
74960c60
VP
421}
422
b57bacec
PA
423/* See breakpoint.h. */
424
33e5cbd6 425int
a25a5a45 426breakpoints_should_be_inserted_now (void)
33e5cbd6 427{
a25a5a45
PA
428 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
429 {
430 /* If breakpoints are global, they should be inserted even if no
431 thread under gdb's control is running, or even if there are
432 no threads under GDB's control yet. */
433 return 1;
434 }
435 else if (target_has_execution)
436 {
372316f1
PA
437 struct thread_info *tp;
438
a25a5a45
PA
439 if (always_inserted_mode)
440 {
441 /* The user wants breakpoints inserted even if all threads
442 are stopped. */
443 return 1;
444 }
445
b57bacec
PA
446 if (threads_are_executing ())
447 return 1;
372316f1
PA
448
449 /* Don't remove breakpoints yet if, even though all threads are
450 stopped, we still have events to process. */
451 ALL_NON_EXITED_THREADS (tp)
452 if (tp->resumed
453 && tp->suspend.waitstatus_pending_p)
454 return 1;
a25a5a45
PA
455 }
456 return 0;
33e5cbd6 457}
765dc015 458
b775012e
LM
459static const char condition_evaluation_both[] = "host or target";
460
461/* Modes for breakpoint condition evaluation. */
462static const char condition_evaluation_auto[] = "auto";
463static const char condition_evaluation_host[] = "host";
464static const char condition_evaluation_target[] = "target";
465static const char *const condition_evaluation_enums[] = {
466 condition_evaluation_auto,
467 condition_evaluation_host,
468 condition_evaluation_target,
469 NULL
470};
471
472/* Global that holds the current mode for breakpoint condition evaluation. */
473static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
474
475/* Global that we use to display information to the user (gets its value from
476 condition_evaluation_mode_1. */
477static const char *condition_evaluation_mode = condition_evaluation_auto;
478
479/* Translate a condition evaluation mode MODE into either "host"
480 or "target". This is used mostly to translate from "auto" to the
481 real setting that is being used. It returns the translated
482 evaluation mode. */
483
484static const char *
485translate_condition_evaluation_mode (const char *mode)
486{
487 if (mode == condition_evaluation_auto)
488 {
489 if (target_supports_evaluation_of_breakpoint_conditions ())
490 return condition_evaluation_target;
491 else
492 return condition_evaluation_host;
493 }
494 else
495 return mode;
496}
497
498/* Discovers what condition_evaluation_auto translates to. */
499
500static const char *
501breakpoint_condition_evaluation_mode (void)
502{
503 return translate_condition_evaluation_mode (condition_evaluation_mode);
504}
505
506/* Return true if GDB should evaluate breakpoint conditions or false
507 otherwise. */
508
509static int
510gdb_evaluates_breakpoint_condition_p (void)
511{
512 const char *mode = breakpoint_condition_evaluation_mode ();
513
514 return (mode == condition_evaluation_host);
515}
516
c906108c
SS
517/* Are we executing breakpoint commands? */
518static int executing_breakpoint_commands;
519
c02f5703
MS
520/* Are overlay event breakpoints enabled? */
521static int overlay_events_enabled;
522
e09342b5
TJB
523/* See description in breakpoint.h. */
524int target_exact_watchpoints = 0;
525
c906108c 526/* Walk the following statement or block through all breakpoints.
e5dd4106 527 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 528 current breakpoint. */
c906108c 529
5c44784c 530#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 531
5c44784c
JM
532#define ALL_BREAKPOINTS_SAFE(B,TMP) \
533 for (B = breakpoint_chain; \
534 B ? (TMP=B->next, 1): 0; \
535 B = TMP)
c906108c 536
4a64f543
MS
537/* Similar iterator for the low-level breakpoints. SAFE variant is
538 not provided so update_global_location_list must not be called
539 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 540
876fa593 541#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
542 for (BP_TMP = bp_locations; \
543 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 544 BP_TMP++)
7cc221ef 545
b775012e
LM
546/* Iterates through locations with address ADDRESS for the currently selected
547 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
548 to where the loop should start from.
549 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
550 appropriate location to start with. */
551
552#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
553 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
554 BP_LOCP_TMP = BP_LOCP_START; \
555 BP_LOCP_START \
f5336ca5 556 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
557 && (*BP_LOCP_TMP)->address == ADDRESS); \
558 BP_LOCP_TMP++)
559
1042e4c0
SS
560/* Iterator for tracepoints only. */
561
562#define ALL_TRACEPOINTS(B) \
563 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 564 if (is_tracepoint (B))
1042e4c0 565
7cc221ef 566/* Chains of all breakpoints defined. */
c906108c
SS
567
568struct breakpoint *breakpoint_chain;
569
f5336ca5 570/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 571
f5336ca5 572static struct bp_location **bp_locations;
876fa593 573
f5336ca5 574/* Number of elements of BP_LOCATIONS. */
876fa593 575
f5336ca5 576static unsigned bp_locations_count;
876fa593 577
4a64f543 578/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 579 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 580 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 581 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 582 an address you need to read. */
876fa593 583
f5336ca5 584static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 585
4a64f543
MS
586/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
587 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
588 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
589 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 590 scan for shadow bytes for an address you need to read. */
876fa593 591
f5336ca5 592static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 593
4a64f543 594/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
595 from the bp_locations array, but for which a hit may still be
596 reported by a target. */
20874c92
VP
597VEC(bp_location_p) *moribund_locations = NULL;
598
c906108c
SS
599/* Number of last breakpoint made. */
600
95a42b64
TT
601static int breakpoint_count;
602
86b17b60
PA
603/* The value of `breakpoint_count' before the last command that
604 created breakpoints. If the last (break-like) command created more
605 than one breakpoint, then the difference between BREAKPOINT_COUNT
606 and PREV_BREAKPOINT_COUNT is more than one. */
607static int prev_breakpoint_count;
c906108c 608
1042e4c0
SS
609/* Number of last tracepoint made. */
610
95a42b64 611static int tracepoint_count;
1042e4c0 612
6149aea9
PA
613static struct cmd_list_element *breakpoint_set_cmdlist;
614static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 615struct cmd_list_element *save_cmdlist;
6149aea9 616
badd37ce
SDJ
617/* See declaration at breakpoint.h. */
618
619struct breakpoint *
620breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
621 void *user_data)
622{
623 struct breakpoint *b = NULL;
624
625 ALL_BREAKPOINTS (b)
626 {
627 if (func (b, user_data) != 0)
628 break;
629 }
630
631 return b;
632}
633
468d015d
JJ
634/* Return whether a breakpoint is an active enabled breakpoint. */
635static int
636breakpoint_enabled (struct breakpoint *b)
637{
0d381245 638 return (b->enable_state == bp_enabled);
468d015d
JJ
639}
640
c906108c
SS
641/* Set breakpoint count to NUM. */
642
95a42b64 643static void
fba45db2 644set_breakpoint_count (int num)
c906108c 645{
86b17b60 646 prev_breakpoint_count = breakpoint_count;
c906108c 647 breakpoint_count = num;
4fa62494 648 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
649}
650
86b17b60
PA
651/* Used by `start_rbreak_breakpoints' below, to record the current
652 breakpoint count before "rbreak" creates any breakpoint. */
653static int rbreak_start_breakpoint_count;
654
95a42b64
TT
655/* Called at the start an "rbreak" command to record the first
656 breakpoint made. */
86b17b60 657
95a42b64
TT
658void
659start_rbreak_breakpoints (void)
660{
86b17b60 661 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
662}
663
664/* Called at the end of an "rbreak" command to record the last
665 breakpoint made. */
86b17b60 666
95a42b64
TT
667void
668end_rbreak_breakpoints (void)
669{
86b17b60 670 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
671}
672
4a64f543 673/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
674
675void
fba45db2 676clear_breakpoint_hit_counts (void)
c906108c
SS
677{
678 struct breakpoint *b;
679
680 ALL_BREAKPOINTS (b)
681 b->hit_count = 0;
682}
683
c906108c 684\f
48cb2d85
VP
685/* Return the breakpoint with the specified number, or NULL
686 if the number does not refer to an existing breakpoint. */
687
688struct breakpoint *
689get_breakpoint (int num)
690{
691 struct breakpoint *b;
692
693 ALL_BREAKPOINTS (b)
694 if (b->number == num)
695 return b;
696
697 return NULL;
698}
5c44784c 699
c906108c 700\f
adc36818 701
b775012e
LM
702/* Mark locations as "conditions have changed" in case the target supports
703 evaluating conditions on its side. */
704
705static void
706mark_breakpoint_modified (struct breakpoint *b)
707{
708 struct bp_location *loc;
709
710 /* This is only meaningful if the target is
711 evaluating conditions and if the user has
712 opted for condition evaluation on the target's
713 side. */
714 if (gdb_evaluates_breakpoint_condition_p ()
715 || !target_supports_evaluation_of_breakpoint_conditions ())
716 return;
717
718 if (!is_breakpoint (b))
719 return;
720
721 for (loc = b->loc; loc; loc = loc->next)
722 loc->condition_changed = condition_modified;
723}
724
725/* Mark location as "conditions have changed" in case the target supports
726 evaluating conditions on its side. */
727
728static void
729mark_breakpoint_location_modified (struct bp_location *loc)
730{
731 /* This is only meaningful if the target is
732 evaluating conditions and if the user has
733 opted for condition evaluation on the target's
734 side. */
735 if (gdb_evaluates_breakpoint_condition_p ()
736 || !target_supports_evaluation_of_breakpoint_conditions ())
737
738 return;
739
740 if (!is_breakpoint (loc->owner))
741 return;
742
743 loc->condition_changed = condition_modified;
744}
745
746/* Sets the condition-evaluation mode using the static global
747 condition_evaluation_mode. */
748
749static void
750set_condition_evaluation_mode (char *args, int from_tty,
751 struct cmd_list_element *c)
752{
b775012e
LM
753 const char *old_mode, *new_mode;
754
755 if ((condition_evaluation_mode_1 == condition_evaluation_target)
756 && !target_supports_evaluation_of_breakpoint_conditions ())
757 {
758 condition_evaluation_mode_1 = condition_evaluation_mode;
759 warning (_("Target does not support breakpoint condition evaluation.\n"
760 "Using host evaluation mode instead."));
761 return;
762 }
763
764 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
765 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
766
abf1152a
JK
767 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
768 settings was "auto". */
769 condition_evaluation_mode = condition_evaluation_mode_1;
770
b775012e
LM
771 /* Only update the mode if the user picked a different one. */
772 if (new_mode != old_mode)
773 {
774 struct bp_location *loc, **loc_tmp;
775 /* If the user switched to a different evaluation mode, we
776 need to synch the changes with the target as follows:
777
778 "host" -> "target": Send all (valid) conditions to the target.
779 "target" -> "host": Remove all the conditions from the target.
780 */
781
b775012e
LM
782 if (new_mode == condition_evaluation_target)
783 {
784 /* Mark everything modified and synch conditions with the
785 target. */
786 ALL_BP_LOCATIONS (loc, loc_tmp)
787 mark_breakpoint_location_modified (loc);
788 }
789 else
790 {
791 /* Manually mark non-duplicate locations to synch conditions
792 with the target. We do this to remove all the conditions the
793 target knows about. */
794 ALL_BP_LOCATIONS (loc, loc_tmp)
795 if (is_breakpoint (loc->owner) && loc->inserted)
796 loc->needs_update = 1;
797 }
798
799 /* Do the update. */
44702360 800 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
801 }
802
803 return;
804}
805
806/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
807 what "auto" is translating to. */
808
809static void
810show_condition_evaluation_mode (struct ui_file *file, int from_tty,
811 struct cmd_list_element *c, const char *value)
812{
813 if (condition_evaluation_mode == condition_evaluation_auto)
814 fprintf_filtered (file,
815 _("Breakpoint condition evaluation "
816 "mode is %s (currently %s).\n"),
817 value,
818 breakpoint_condition_evaluation_mode ());
819 else
820 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
821 value);
822}
823
824/* A comparison function for bp_location AP and BP that is used by
825 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 826 the more general bp_locations_compare function. */
b775012e
LM
827
828static int
f5336ca5 829bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 830{
9a3c8263
SM
831 const struct bp_location *a = *(const struct bp_location **) ap;
832 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
833
834 if (a->address == b->address)
835 return 0;
836 else
837 return ((a->address > b->address) - (a->address < b->address));
838}
839
840/* Helper function to skip all bp_locations with addresses
841 less than ADDRESS. It returns the first bp_location that
842 is greater than or equal to ADDRESS. If none is found, just
843 return NULL. */
844
845static struct bp_location **
846get_first_locp_gte_addr (CORE_ADDR address)
847{
848 struct bp_location dummy_loc;
849 struct bp_location *dummy_locp = &dummy_loc;
850 struct bp_location **locp_found = NULL;
851
852 /* Initialize the dummy location's address field. */
b775012e
LM
853 dummy_loc.address = address;
854
855 /* Find a close match to the first location at ADDRESS. */
9a3c8263 856 locp_found = ((struct bp_location **)
f5336ca5 857 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 858 sizeof (struct bp_location **),
f5336ca5 859 bp_locations_compare_addrs));
b775012e
LM
860
861 /* Nothing was found, nothing left to do. */
862 if (locp_found == NULL)
863 return NULL;
864
865 /* We may have found a location that is at ADDRESS but is not the first in the
866 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 867 while ((locp_found - 1) >= bp_locations
b775012e
LM
868 && (*(locp_found - 1))->address == address)
869 locp_found--;
870
871 return locp_found;
872}
873
adc36818 874void
7a26bd4d 875set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
876 int from_tty)
877{
3a5c3e22
PA
878 xfree (b->cond_string);
879 b->cond_string = NULL;
adc36818 880
3a5c3e22 881 if (is_watchpoint (b))
adc36818 882 {
3a5c3e22
PA
883 struct watchpoint *w = (struct watchpoint *) b;
884
4d01a485 885 w->cond_exp.reset ();
3a5c3e22
PA
886 }
887 else
888 {
889 struct bp_location *loc;
890
891 for (loc = b->loc; loc; loc = loc->next)
892 {
4d01a485 893 loc->cond.reset ();
b775012e
LM
894
895 /* No need to free the condition agent expression
896 bytecode (if we have one). We will handle this
897 when we go through update_global_location_list. */
3a5c3e22 898 }
adc36818 899 }
adc36818
PM
900
901 if (*exp == 0)
902 {
903 if (from_tty)
904 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
905 }
906 else
907 {
bbc13ae3 908 const char *arg = exp;
cc59ec59 909
adc36818
PM
910 /* I don't know if it matters whether this is the string the user
911 typed in or the decompiled expression. */
912 b->cond_string = xstrdup (arg);
913 b->condition_not_parsed = 0;
914
915 if (is_watchpoint (b))
916 {
3a5c3e22
PA
917 struct watchpoint *w = (struct watchpoint *) b;
918
adc36818
PM
919 innermost_block = NULL;
920 arg = exp;
1bb9788d 921 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
922 if (*arg)
923 error (_("Junk at end of expression"));
3a5c3e22 924 w->cond_exp_valid_block = innermost_block;
adc36818
PM
925 }
926 else
927 {
3a5c3e22
PA
928 struct bp_location *loc;
929
adc36818
PM
930 for (loc = b->loc; loc; loc = loc->next)
931 {
932 arg = exp;
933 loc->cond =
1bb9788d
TT
934 parse_exp_1 (&arg, loc->address,
935 block_for_pc (loc->address), 0);
adc36818
PM
936 if (*arg)
937 error (_("Junk at end of expression"));
938 }
939 }
940 }
b775012e
LM
941 mark_breakpoint_modified (b);
942
8d3788bd 943 observer_notify_breakpoint_modified (b);
adc36818
PM
944}
945
d55637df
TT
946/* Completion for the "condition" command. */
947
eb3ff9a5 948static void
6f937416 949condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 950 completion_tracker &tracker,
6f937416 951 const char *text, const char *word)
d55637df 952{
6f937416 953 const char *space;
d55637df 954
f1735a53
TT
955 text = skip_spaces (text);
956 space = skip_to_space (text);
d55637df
TT
957 if (*space == '\0')
958 {
959 int len;
960 struct breakpoint *b;
961 VEC (char_ptr) *result = NULL;
962
963 if (text[0] == '$')
964 {
965 /* We don't support completion of history indices. */
eb3ff9a5
PA
966 if (!isdigit (text[1]))
967 complete_internalvar (tracker, &text[1]);
968 return;
d55637df
TT
969 }
970
971 /* We're completing the breakpoint number. */
972 len = strlen (text);
973
974 ALL_BREAKPOINTS (b)
58ce7251
SDJ
975 {
976 char number[50];
977
978 xsnprintf (number, sizeof (number), "%d", b->number);
979
980 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
981 {
982 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
983 tracker.add_completion (std::move (copy));
984 }
58ce7251 985 }
d55637df 986
eb3ff9a5 987 return;
d55637df
TT
988 }
989
990 /* We're completing the expression part. */
f1735a53 991 text = skip_spaces (space);
eb3ff9a5 992 expression_completer (cmd, tracker, text, word);
d55637df
TT
993}
994
c906108c
SS
995/* condition N EXP -- set break condition of breakpoint N to EXP. */
996
997static void
fba45db2 998condition_command (char *arg, int from_tty)
c906108c 999{
52f0bd74 1000 struct breakpoint *b;
c906108c 1001 char *p;
52f0bd74 1002 int bnum;
c906108c
SS
1003
1004 if (arg == 0)
e2e0b3e5 1005 error_no_arg (_("breakpoint number"));
c906108c
SS
1006
1007 p = arg;
1008 bnum = get_number (&p);
5c44784c 1009 if (bnum == 0)
8a3fe4f8 1010 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1011
1012 ALL_BREAKPOINTS (b)
1013 if (b->number == bnum)
2f069f6f 1014 {
6dddc817
DE
1015 /* Check if this breakpoint has a "stop" method implemented in an
1016 extension language. This method and conditions entered into GDB
1017 from the CLI are mutually exclusive. */
1018 const struct extension_language_defn *extlang
1019 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1020
1021 if (extlang != NULL)
1022 {
1023 error (_("Only one stop condition allowed. There is currently"
1024 " a %s stop condition defined for this breakpoint."),
1025 ext_lang_capitalized_name (extlang));
1026 }
2566ad2d 1027 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1028
1029 if (is_breakpoint (b))
44702360 1030 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1031
2f069f6f
JB
1032 return;
1033 }
c906108c 1034
8a3fe4f8 1035 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1036}
1037
a7bdde9e
VP
1038/* Check that COMMAND do not contain commands that are suitable
1039 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1040 Throw if any such commands is found. */
1041
a7bdde9e
VP
1042static void
1043check_no_tracepoint_commands (struct command_line *commands)
1044{
1045 struct command_line *c;
cc59ec59 1046
a7bdde9e
VP
1047 for (c = commands; c; c = c->next)
1048 {
1049 int i;
1050
1051 if (c->control_type == while_stepping_control)
3e43a32a
MS
1052 error (_("The 'while-stepping' command can "
1053 "only be used for tracepoints"));
a7bdde9e
VP
1054
1055 for (i = 0; i < c->body_count; ++i)
1056 check_no_tracepoint_commands ((c->body_list)[i]);
1057
1058 /* Not that command parsing removes leading whitespace and comment
4a64f543 1059 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1060 command directly. */
1061 if (strstr (c->line, "collect ") == c->line)
1062 error (_("The 'collect' command can only be used for tracepoints"));
1063
51661e93
VP
1064 if (strstr (c->line, "teval ") == c->line)
1065 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1066 }
1067}
1068
c1fc2657 1069struct longjmp_breakpoint : public breakpoint
3b0871f4 1070{
c1fc2657 1071 ~longjmp_breakpoint () override;
3b0871f4
SM
1072};
1073
d77f58be
SS
1074/* Encapsulate tests for different types of tracepoints. */
1075
3b0871f4
SM
1076static bool
1077is_tracepoint_type (bptype type)
d9b3f62e
PA
1078{
1079 return (type == bp_tracepoint
1080 || type == bp_fast_tracepoint
1081 || type == bp_static_tracepoint);
1082}
1083
3b0871f4
SM
1084static bool
1085is_longjmp_type (bptype type)
1086{
1087 return type == bp_longjmp || type == bp_exception;
1088}
1089
a7bdde9e 1090int
d77f58be 1091is_tracepoint (const struct breakpoint *b)
a7bdde9e 1092{
d9b3f62e 1093 return is_tracepoint_type (b->type);
a7bdde9e 1094}
d9b3f62e 1095
a5e364af
SM
1096/* Factory function to create an appropriate instance of breakpoint given
1097 TYPE. */
1098
1099static std::unique_ptr<breakpoint>
1100new_breakpoint_from_type (bptype type)
1101{
1102 breakpoint *b;
1103
1104 if (is_tracepoint_type (type))
c1fc2657 1105 b = new tracepoint ();
3b0871f4 1106 else if (is_longjmp_type (type))
c1fc2657 1107 b = new longjmp_breakpoint ();
a5e364af
SM
1108 else
1109 b = new breakpoint ();
1110
1111 return std::unique_ptr<breakpoint> (b);
1112}
1113
e5dd4106 1114/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1115 breakpoint. This function will throw an exception if a problem is
1116 found. */
48cb2d85 1117
95a42b64
TT
1118static void
1119validate_commands_for_breakpoint (struct breakpoint *b,
1120 struct command_line *commands)
48cb2d85 1121{
d77f58be 1122 if (is_tracepoint (b))
a7bdde9e 1123 {
c9a6ce02 1124 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1125 struct command_line *c;
1126 struct command_line *while_stepping = 0;
c9a6ce02
PA
1127
1128 /* Reset the while-stepping step count. The previous commands
1129 might have included a while-stepping action, while the new
1130 ones might not. */
1131 t->step_count = 0;
1132
1133 /* We need to verify that each top-level element of commands is
1134 valid for tracepoints, that there's at most one
1135 while-stepping element, and that the while-stepping's body
1136 has valid tracing commands excluding nested while-stepping.
1137 We also need to validate the tracepoint action line in the
1138 context of the tracepoint --- validate_actionline actually
1139 has side effects, like setting the tracepoint's
1140 while-stepping STEP_COUNT, in addition to checking if the
1141 collect/teval actions parse and make sense in the
1142 tracepoint's context. */
a7bdde9e
VP
1143 for (c = commands; c; c = c->next)
1144 {
a7bdde9e
VP
1145 if (c->control_type == while_stepping_control)
1146 {
1147 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1148 error (_("The 'while-stepping' command "
1149 "cannot be used for fast tracepoint"));
0fb4aa4b 1150 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1151 error (_("The 'while-stepping' command "
1152 "cannot be used for static tracepoint"));
a7bdde9e
VP
1153
1154 if (while_stepping)
3e43a32a
MS
1155 error (_("The 'while-stepping' command "
1156 "can be used only once"));
a7bdde9e
VP
1157 else
1158 while_stepping = c;
1159 }
c9a6ce02
PA
1160
1161 validate_actionline (c->line, b);
a7bdde9e
VP
1162 }
1163 if (while_stepping)
1164 {
1165 struct command_line *c2;
1166
1167 gdb_assert (while_stepping->body_count == 1);
1168 c2 = while_stepping->body_list[0];
1169 for (; c2; c2 = c2->next)
1170 {
a7bdde9e
VP
1171 if (c2->control_type == while_stepping_control)
1172 error (_("The 'while-stepping' command cannot be nested"));
1173 }
1174 }
1175 }
1176 else
1177 {
1178 check_no_tracepoint_commands (commands);
1179 }
95a42b64
TT
1180}
1181
0fb4aa4b
PA
1182/* Return a vector of all the static tracepoints set at ADDR. The
1183 caller is responsible for releasing the vector. */
1184
1185VEC(breakpoint_p) *
1186static_tracepoints_here (CORE_ADDR addr)
1187{
1188 struct breakpoint *b;
1189 VEC(breakpoint_p) *found = 0;
1190 struct bp_location *loc;
1191
1192 ALL_BREAKPOINTS (b)
1193 if (b->type == bp_static_tracepoint)
1194 {
1195 for (loc = b->loc; loc; loc = loc->next)
1196 if (loc->address == addr)
1197 VEC_safe_push(breakpoint_p, found, b);
1198 }
1199
1200 return found;
1201}
1202
95a42b64 1203/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1204 validate that only allowed commands are included. */
95a42b64
TT
1205
1206void
4a64f543 1207breakpoint_set_commands (struct breakpoint *b,
93921405 1208 command_line_up &&commands)
95a42b64 1209{
93921405 1210 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1211
d1b0a7bf 1212 b->commands = std::move (commands);
8d3788bd 1213 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1214}
1215
45a43567
TT
1216/* Set the internal `silent' flag on the breakpoint. Note that this
1217 is not the same as the "silent" that may appear in the breakpoint's
1218 commands. */
1219
1220void
1221breakpoint_set_silent (struct breakpoint *b, int silent)
1222{
1223 int old_silent = b->silent;
1224
1225 b->silent = silent;
1226 if (old_silent != silent)
8d3788bd 1227 observer_notify_breakpoint_modified (b);
45a43567
TT
1228}
1229
1230/* Set the thread for this breakpoint. If THREAD is -1, make the
1231 breakpoint work for any thread. */
1232
1233void
1234breakpoint_set_thread (struct breakpoint *b, int thread)
1235{
1236 int old_thread = b->thread;
1237
1238 b->thread = thread;
1239 if (old_thread != thread)
8d3788bd 1240 observer_notify_breakpoint_modified (b);
45a43567
TT
1241}
1242
1243/* Set the task for this breakpoint. If TASK is 0, make the
1244 breakpoint work for any task. */
1245
1246void
1247breakpoint_set_task (struct breakpoint *b, int task)
1248{
1249 int old_task = b->task;
1250
1251 b->task = task;
1252 if (old_task != task)
8d3788bd 1253 observer_notify_breakpoint_modified (b);
45a43567
TT
1254}
1255
95a42b64
TT
1256void
1257check_tracepoint_command (char *line, void *closure)
a7bdde9e 1258{
9a3c8263 1259 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1260
6f937416 1261 validate_actionline (line, b);
a7bdde9e
VP
1262}
1263
95a42b64 1264static void
896b6bda 1265commands_command_1 (const char *arg, int from_tty,
4a64f543 1266 struct command_line *control)
95a42b64 1267{
d1b0a7bf 1268 counted_command_line cmd;
95a42b64 1269
896b6bda
PA
1270 std::string new_arg;
1271
95a42b64
TT
1272 if (arg == NULL || !*arg)
1273 {
86b17b60 1274 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1275 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1276 breakpoint_count);
95a42b64 1277 else if (breakpoint_count > 0)
896b6bda 1278 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1279 arg = new_arg.c_str ();
1280 }
1281
1282 map_breakpoint_numbers
1283 (arg, [&] (breakpoint *b)
1284 {
1285 if (cmd == NULL)
1286 {
48649e1b 1287 if (control != NULL)
d1b0a7bf 1288 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1289 else
1290 {
1291 struct cleanup *old_chain;
1292 char *str;
1293
1294 str = xstrprintf (_("Type commands for breakpoint(s) "
1295 "%s, one per line."),
1296 arg);
1297
1298 old_chain = make_cleanup (xfree, str);
1299
d1b0a7bf
TT
1300 cmd = read_command_lines (str,
1301 from_tty, 1,
1302 (is_tracepoint (b)
1303 ? check_tracepoint_command : 0),
1304 b);
48649e1b
TT
1305
1306 do_cleanups (old_chain);
1307 }
48649e1b
TT
1308 }
1309
1310 /* If a breakpoint was on the list more than once, we don't need to
1311 do anything. */
1312 if (b->commands != cmd)
1313 {
d1b0a7bf 1314 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1315 b->commands = cmd;
1316 observer_notify_breakpoint_modified (b);
1317 }
1318 });
95a42b64 1319
48649e1b 1320 if (cmd == NULL)
95a42b64 1321 error (_("No breakpoints specified."));
95a42b64
TT
1322}
1323
1324static void
1325commands_command (char *arg, int from_tty)
1326{
1327 commands_command_1 (arg, from_tty, NULL);
c906108c 1328}
40c03ae8
EZ
1329
1330/* Like commands_command, but instead of reading the commands from
1331 input stream, takes them from an already parsed command structure.
1332
1333 This is used by cli-script.c to DTRT with breakpoint commands
1334 that are part of if and while bodies. */
1335enum command_control_type
896b6bda 1336commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1337{
95a42b64
TT
1338 commands_command_1 (arg, 0, cmd);
1339 return simple_control;
40c03ae8 1340}
876fa593
JK
1341
1342/* Return non-zero if BL->TARGET_INFO contains valid information. */
1343
1344static int
1345bp_location_has_shadow (struct bp_location *bl)
1346{
1347 if (bl->loc_type != bp_loc_software_breakpoint)
1348 return 0;
1349 if (!bl->inserted)
1350 return 0;
1351 if (bl->target_info.shadow_len == 0)
e5dd4106 1352 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1353 return 0;
1354 return 1;
1355}
1356
9d497a19
PA
1357/* Update BUF, which is LEN bytes read from the target address
1358 MEMADDR, by replacing a memory breakpoint with its shadowed
1359 contents.
1360
1361 If READBUF is not NULL, this buffer must not overlap with the of
1362 the breakpoint location's shadow_contents buffer. Otherwise, a
1363 failed assertion internal error will be raised. */
1364
1365static void
1366one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1367 const gdb_byte *writebuf_org,
1368 ULONGEST memaddr, LONGEST len,
1369 struct bp_target_info *target_info,
1370 struct gdbarch *gdbarch)
1371{
1372 /* Now do full processing of the found relevant range of elements. */
1373 CORE_ADDR bp_addr = 0;
1374 int bp_size = 0;
1375 int bptoffset = 0;
1376
1377 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1378 current_program_space->aspace, 0))
1379 {
1380 /* The breakpoint is inserted in a different address space. */
1381 return;
1382 }
1383
1384 /* Addresses and length of the part of the breakpoint that
1385 we need to copy. */
1386 bp_addr = target_info->placed_address;
1387 bp_size = target_info->shadow_len;
1388
1389 if (bp_addr + bp_size <= memaddr)
1390 {
1391 /* The breakpoint is entirely before the chunk of memory we are
1392 reading. */
1393 return;
1394 }
1395
1396 if (bp_addr >= memaddr + len)
1397 {
1398 /* The breakpoint is entirely after the chunk of memory we are
1399 reading. */
1400 return;
1401 }
1402
1403 /* Offset within shadow_contents. */
1404 if (bp_addr < memaddr)
1405 {
1406 /* Only copy the second part of the breakpoint. */
1407 bp_size -= memaddr - bp_addr;
1408 bptoffset = memaddr - bp_addr;
1409 bp_addr = memaddr;
1410 }
1411
1412 if (bp_addr + bp_size > memaddr + len)
1413 {
1414 /* Only copy the first part of the breakpoint. */
1415 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1416 }
1417
1418 if (readbuf != NULL)
1419 {
1420 /* Verify that the readbuf buffer does not overlap with the
1421 shadow_contents buffer. */
1422 gdb_assert (target_info->shadow_contents >= readbuf + len
1423 || readbuf >= (target_info->shadow_contents
1424 + target_info->shadow_len));
1425
1426 /* Update the read buffer with this inserted breakpoint's
1427 shadow. */
1428 memcpy (readbuf + bp_addr - memaddr,
1429 target_info->shadow_contents + bptoffset, bp_size);
1430 }
1431 else
1432 {
1433 const unsigned char *bp;
0d5ed153
MR
1434 CORE_ADDR addr = target_info->reqstd_address;
1435 int placed_size;
9d497a19
PA
1436
1437 /* Update the shadow with what we want to write to memory. */
1438 memcpy (target_info->shadow_contents + bptoffset,
1439 writebuf_org + bp_addr - memaddr, bp_size);
1440
1441 /* Determine appropriate breakpoint contents and size for this
1442 address. */
0d5ed153 1443 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1444
1445 /* Update the final write buffer with this inserted
1446 breakpoint's INSN. */
1447 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1448 }
1449}
1450
8defab1a 1451/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1452 by replacing any memory breakpoints with their shadowed contents.
1453
35c63cd8
JB
1454 If READBUF is not NULL, this buffer must not overlap with any of
1455 the breakpoint location's shadow_contents buffers. Otherwise,
1456 a failed assertion internal error will be raised.
1457
876fa593 1458 The range of shadowed area by each bp_location is:
f5336ca5
PA
1459 bl->address - bp_locations_placed_address_before_address_max
1460 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1461 The range we were requested to resolve shadows for is:
1462 memaddr ... memaddr + len
1463 Thus the safe cutoff boundaries for performance optimization are
35df4500 1464 memaddr + len <= (bl->address
f5336ca5 1465 - bp_locations_placed_address_before_address_max)
876fa593 1466 and:
f5336ca5 1467 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1468
8defab1a 1469void
f0ba3972
PA
1470breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1471 const gdb_byte *writebuf_org,
1472 ULONGEST memaddr, LONGEST len)
c906108c 1473{
4a64f543
MS
1474 /* Left boundary, right boundary and median element of our binary
1475 search. */
876fa593
JK
1476 unsigned bc_l, bc_r, bc;
1477
4a64f543
MS
1478 /* Find BC_L which is a leftmost element which may affect BUF
1479 content. It is safe to report lower value but a failure to
1480 report higher one. */
876fa593
JK
1481
1482 bc_l = 0;
f5336ca5 1483 bc_r = bp_locations_count;
876fa593
JK
1484 while (bc_l + 1 < bc_r)
1485 {
35df4500 1486 struct bp_location *bl;
876fa593
JK
1487
1488 bc = (bc_l + bc_r) / 2;
f5336ca5 1489 bl = bp_locations[bc];
876fa593 1490
4a64f543
MS
1491 /* Check first BL->ADDRESS will not overflow due to the added
1492 constant. Then advance the left boundary only if we are sure
1493 the BC element can in no way affect the BUF content (MEMADDR
1494 to MEMADDR + LEN range).
876fa593 1495
f5336ca5 1496 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1497 offset so that we cannot miss a breakpoint with its shadow
1498 range tail still reaching MEMADDR. */
c5aa993b 1499
f5336ca5 1500 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1501 >= bl->address)
f5336ca5 1502 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1503 <= memaddr))
876fa593
JK
1504 bc_l = bc;
1505 else
1506 bc_r = bc;
1507 }
1508
128070bb
PA
1509 /* Due to the binary search above, we need to make sure we pick the
1510 first location that's at BC_L's address. E.g., if there are
1511 multiple locations at the same address, BC_L may end up pointing
1512 at a duplicate location, and miss the "master"/"inserted"
1513 location. Say, given locations L1, L2 and L3 at addresses A and
1514 B:
1515
1516 L1@A, L2@A, L3@B, ...
1517
1518 BC_L could end up pointing at location L2, while the "master"
1519 location could be L1. Since the `loc->inserted' flag is only set
1520 on "master" locations, we'd forget to restore the shadow of L1
1521 and L2. */
1522 while (bc_l > 0
f5336ca5 1523 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1524 bc_l--;
1525
876fa593
JK
1526 /* Now do full processing of the found relevant range of elements. */
1527
f5336ca5 1528 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1529 {
f5336ca5 1530 struct bp_location *bl = bp_locations[bc];
876fa593 1531
35df4500
TJB
1532 /* bp_location array has BL->OWNER always non-NULL. */
1533 if (bl->owner->type == bp_none)
8a3fe4f8 1534 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1535 bl->owner->number);
ffce0d52 1536
e5dd4106 1537 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1538 content. */
1539
f5336ca5
PA
1540 if (bl->address >= bp_locations_placed_address_before_address_max
1541 && memaddr + len <= (bl->address
1542 - bp_locations_placed_address_before_address_max))
876fa593
JK
1543 break;
1544
35df4500 1545 if (!bp_location_has_shadow (bl))
c5aa993b 1546 continue;
6c95b8df 1547
9d497a19
PA
1548 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1549 memaddr, len, &bl->target_info, bl->gdbarch);
1550 }
c906108c 1551}
9d497a19 1552
c906108c 1553\f
c5aa993b 1554
b775012e
LM
1555/* Return true if BPT is either a software breakpoint or a hardware
1556 breakpoint. */
1557
1558int
1559is_breakpoint (const struct breakpoint *bpt)
1560{
1561 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1562 || bpt->type == bp_hardware_breakpoint
1563 || bpt->type == bp_dprintf);
b775012e
LM
1564}
1565
60e1c644
PA
1566/* Return true if BPT is of any hardware watchpoint kind. */
1567
a5606eee 1568static int
d77f58be 1569is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1570{
1571 return (bpt->type == bp_hardware_watchpoint
1572 || bpt->type == bp_read_watchpoint
1573 || bpt->type == bp_access_watchpoint);
1574}
7270d8f2 1575
60e1c644
PA
1576/* Return true if BPT is of any watchpoint kind, hardware or
1577 software. */
1578
3a5c3e22 1579int
d77f58be 1580is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1581{
1582 return (is_hardware_watchpoint (bpt)
1583 || bpt->type == bp_watchpoint);
1584}
1585
3a5c3e22
PA
1586/* Returns true if the current thread and its running state are safe
1587 to evaluate or update watchpoint B. Watchpoints on local
1588 expressions need to be evaluated in the context of the thread that
1589 was current when the watchpoint was created, and, that thread needs
1590 to be stopped to be able to select the correct frame context.
1591 Watchpoints on global expressions can be evaluated on any thread,
1592 and in any state. It is presently left to the target allowing
1593 memory accesses when threads are running. */
f6bc2008
PA
1594
1595static int
3a5c3e22 1596watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1597{
c1fc2657 1598 return (b->pspace == current_program_space
d0d8b0c6
JK
1599 && (ptid_equal (b->watchpoint_thread, null_ptid)
1600 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1601 && !is_executing (inferior_ptid))));
f6bc2008
PA
1602}
1603
d0fb5eae
JK
1604/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1605 associated bp_watchpoint_scope breakpoint. */
1606
1607static void
3a5c3e22 1608watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1609{
c1fc2657 1610 if (w->related_breakpoint != w)
d0fb5eae 1611 {
c1fc2657
SM
1612 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1613 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1614 w->related_breakpoint->disposition = disp_del_at_next_stop;
1615 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1616 w->related_breakpoint = w;
d0fb5eae 1617 }
c1fc2657 1618 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1619}
1620
bb9d5f81
PP
1621/* Extract a bitfield value from value VAL using the bit parameters contained in
1622 watchpoint W. */
1623
1624static struct value *
1625extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1626{
1627 struct value *bit_val;
1628
1629 if (val == NULL)
1630 return NULL;
1631
1632 bit_val = allocate_value (value_type (val));
1633
1634 unpack_value_bitfield (bit_val,
1635 w->val_bitpos,
1636 w->val_bitsize,
1637 value_contents_for_printing (val),
1638 value_offset (val),
1639 val);
1640
1641 return bit_val;
1642}
1643
c6d81124
PA
1644/* Allocate a dummy location and add it to B, which must be a software
1645 watchpoint. This is required because even if a software watchpoint
1646 is not watching any memory, bpstat_stop_status requires a location
1647 to be able to report stops. */
1648
1649static void
1650software_watchpoint_add_no_memory_location (struct breakpoint *b,
1651 struct program_space *pspace)
1652{
1653 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1654
1655 b->loc = allocate_bp_location (b);
1656 b->loc->pspace = pspace;
1657 b->loc->address = -1;
1658 b->loc->length = -1;
1659}
1660
1661/* Returns true if B is a software watchpoint that is not watching any
1662 memory (e.g., "watch $pc"). */
1663
1664static int
1665is_no_memory_software_watchpoint (struct breakpoint *b)
1666{
1667 return (b->type == bp_watchpoint
1668 && b->loc != NULL
1669 && b->loc->next == NULL
1670 && b->loc->address == -1
1671 && b->loc->length == -1);
1672}
1673
567e1b4e
JB
1674/* Assuming that B is a watchpoint:
1675 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1676 - Evaluate expression and store the result in B->val
567e1b4e
JB
1677 - Evaluate the condition if there is one, and store the result
1678 in b->loc->cond.
a5606eee
VP
1679 - Update the list of values that must be watched in B->loc.
1680
4a64f543
MS
1681 If the watchpoint disposition is disp_del_at_next_stop, then do
1682 nothing. If this is local watchpoint that is out of scope, delete
1683 it.
1684
1685 Even with `set breakpoint always-inserted on' the watchpoints are
1686 removed + inserted on each stop here. Normal breakpoints must
1687 never be removed because they might be missed by a running thread
1688 when debugging in non-stop mode. On the other hand, hardware
1689 watchpoints (is_hardware_watchpoint; processed here) are specific
1690 to each LWP since they are stored in each LWP's hardware debug
1691 registers. Therefore, such LWP must be stopped first in order to
1692 be able to modify its hardware watchpoints.
1693
1694 Hardware watchpoints must be reset exactly once after being
1695 presented to the user. It cannot be done sooner, because it would
1696 reset the data used to present the watchpoint hit to the user. And
1697 it must not be done later because it could display the same single
1698 watchpoint hit during multiple GDB stops. Note that the latter is
1699 relevant only to the hardware watchpoint types bp_read_watchpoint
1700 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1701 not user-visible - its hit is suppressed if the memory content has
1702 not changed.
1703
1704 The following constraints influence the location where we can reset
1705 hardware watchpoints:
1706
1707 * target_stopped_by_watchpoint and target_stopped_data_address are
1708 called several times when GDB stops.
1709
1710 [linux]
1711 * Multiple hardware watchpoints can be hit at the same time,
1712 causing GDB to stop. GDB only presents one hardware watchpoint
1713 hit at a time as the reason for stopping, and all the other hits
1714 are presented later, one after the other, each time the user
1715 requests the execution to be resumed. Execution is not resumed
1716 for the threads still having pending hit event stored in
1717 LWP_INFO->STATUS. While the watchpoint is already removed from
1718 the inferior on the first stop the thread hit event is kept being
1719 reported from its cached value by linux_nat_stopped_data_address
1720 until the real thread resume happens after the watchpoint gets
1721 presented and thus its LWP_INFO->STATUS gets reset.
1722
1723 Therefore the hardware watchpoint hit can get safely reset on the
1724 watchpoint removal from inferior. */
a79d3c27 1725
b40ce68a 1726static void
3a5c3e22 1727update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1728{
a5606eee 1729 int within_current_scope;
a5606eee 1730 struct frame_id saved_frame_id;
66076460 1731 int frame_saved;
a5606eee 1732
f6bc2008
PA
1733 /* If this is a local watchpoint, we only want to check if the
1734 watchpoint frame is in scope if the current thread is the thread
1735 that was used to create the watchpoint. */
1736 if (!watchpoint_in_thread_scope (b))
1737 return;
1738
c1fc2657 1739 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1740 return;
1741
66076460 1742 frame_saved = 0;
a5606eee
VP
1743
1744 /* Determine if the watchpoint is within scope. */
1745 if (b->exp_valid_block == NULL)
1746 within_current_scope = 1;
1747 else
1748 {
b5db5dfc
UW
1749 struct frame_info *fi = get_current_frame ();
1750 struct gdbarch *frame_arch = get_frame_arch (fi);
1751 CORE_ADDR frame_pc = get_frame_pc (fi);
1752
c9cf6e20
MG
1753 /* If we're at a point where the stack has been destroyed
1754 (e.g. in a function epilogue), unwinding may not work
1755 properly. Do not attempt to recreate locations at this
b5db5dfc 1756 point. See similar comments in watchpoint_check. */
c9cf6e20 1757 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1758 return;
66076460
DJ
1759
1760 /* Save the current frame's ID so we can restore it after
1761 evaluating the watchpoint expression on its own frame. */
1762 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1763 took a frame parameter, so that we didn't have to change the
1764 selected frame. */
1765 frame_saved = 1;
1766 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1767
a5606eee
VP
1768 fi = frame_find_by_id (b->watchpoint_frame);
1769 within_current_scope = (fi != NULL);
1770 if (within_current_scope)
1771 select_frame (fi);
1772 }
1773
b5db5dfc
UW
1774 /* We don't free locations. They are stored in the bp_location array
1775 and update_global_location_list will eventually delete them and
1776 remove breakpoints if needed. */
c1fc2657 1777 b->loc = NULL;
b5db5dfc 1778
a5606eee
VP
1779 if (within_current_scope && reparse)
1780 {
bbc13ae3 1781 const char *s;
d63d0675 1782
4d01a485 1783 b->exp.reset ();
d63d0675 1784 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1785 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1786 /* If the meaning of expression itself changed, the old value is
1787 no longer relevant. We don't want to report a watchpoint hit
1788 to the user when the old value and the new value may actually
1789 be completely different objects. */
1790 value_free (b->val);
fa4727a6
DJ
1791 b->val = NULL;
1792 b->val_valid = 0;
60e1c644
PA
1793
1794 /* Note that unlike with breakpoints, the watchpoint's condition
1795 expression is stored in the breakpoint object, not in the
1796 locations (re)created below. */
c1fc2657 1797 if (b->cond_string != NULL)
60e1c644 1798 {
4d01a485 1799 b->cond_exp.reset ();
60e1c644 1800
c1fc2657 1801 s = b->cond_string;
1bb9788d 1802 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1803 }
a5606eee 1804 }
a5606eee
VP
1805
1806 /* If we failed to parse the expression, for example because
1807 it refers to a global variable in a not-yet-loaded shared library,
1808 don't try to insert watchpoint. We don't automatically delete
1809 such watchpoint, though, since failure to parse expression
1810 is different from out-of-scope watchpoint. */
e8369a73 1811 if (!target_has_execution)
2d134ed3
PA
1812 {
1813 /* Without execution, memory can't change. No use to try and
1814 set watchpoint locations. The watchpoint will be reset when
1815 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1816 if (!can_use_hw_watchpoints)
1817 {
c1fc2657
SM
1818 if (b->ops->works_in_software_mode (b))
1819 b->type = bp_watchpoint;
e8369a73 1820 else
638aa5a1
AB
1821 error (_("Can't set read/access watchpoint when "
1822 "hardware watchpoints are disabled."));
e8369a73 1823 }
2d134ed3
PA
1824 }
1825 else if (within_current_scope && b->exp)
a5606eee 1826 {
0cf6dd15 1827 int pc = 0;
fa4727a6 1828 struct value *val_chain, *v, *result, *next;
2d134ed3 1829 struct program_space *frame_pspace;
a5606eee 1830
4d01a485 1831 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1832
a5606eee
VP
1833 /* Avoid setting b->val if it's already set. The meaning of
1834 b->val is 'the last value' user saw, and we should update
1835 it only if we reported that last value to user. As it
9c06b0b4
TJB
1836 happens, the code that reports it updates b->val directly.
1837 We don't keep track of the memory value for masked
1838 watchpoints. */
c1fc2657 1839 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1840 {
bb9d5f81
PP
1841 if (b->val_bitsize != 0)
1842 {
1843 v = extract_bitfield_from_watchpoint_value (b, v);
1844 if (v != NULL)
1845 release_value (v);
1846 }
fa4727a6
DJ
1847 b->val = v;
1848 b->val_valid = 1;
1849 }
a5606eee 1850
2d134ed3
PA
1851 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1852
a5606eee 1853 /* Look at each value on the value chain. */
9fa40276 1854 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1855 {
1856 /* If it's a memory location, and GDB actually needed
1857 its contents to evaluate the expression, then we
fa4727a6
DJ
1858 must watch it. If the first value returned is
1859 still lazy, that means an error occurred reading it;
1860 watch it anyway in case it becomes readable. */
a5606eee 1861 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1862 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1863 {
1864 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1865
a5606eee
VP
1866 /* We only watch structs and arrays if user asked
1867 for it explicitly, never if they just happen to
1868 appear in the middle of some value chain. */
fa4727a6 1869 if (v == result
a5606eee
VP
1870 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1871 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1872 {
1873 CORE_ADDR addr;
f486487f 1874 enum target_hw_bp_type type;
a5606eee 1875 struct bp_location *loc, **tmp;
bb9d5f81
PP
1876 int bitpos = 0, bitsize = 0;
1877
1878 if (value_bitsize (v) != 0)
1879 {
1880 /* Extract the bit parameters out from the bitfield
1881 sub-expression. */
1882 bitpos = value_bitpos (v);
1883 bitsize = value_bitsize (v);
1884 }
1885 else if (v == result && b->val_bitsize != 0)
1886 {
1887 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1888 lvalue whose bit parameters are saved in the fields
1889 VAL_BITPOS and VAL_BITSIZE. */
1890 bitpos = b->val_bitpos;
1891 bitsize = b->val_bitsize;
1892 }
a5606eee 1893
42ae5230 1894 addr = value_address (v);
bb9d5f81
PP
1895 if (bitsize != 0)
1896 {
1897 /* Skip the bytes that don't contain the bitfield. */
1898 addr += bitpos / 8;
1899 }
1900
a5606eee 1901 type = hw_write;
c1fc2657 1902 if (b->type == bp_read_watchpoint)
a5606eee 1903 type = hw_read;
c1fc2657 1904 else if (b->type == bp_access_watchpoint)
a5606eee 1905 type = hw_access;
3a5c3e22 1906
c1fc2657
SM
1907 loc = allocate_bp_location (b);
1908 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1909 ;
1910 *tmp = loc;
a6d9a66e 1911 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1912
1913 loc->pspace = frame_pspace;
a5606eee 1914 loc->address = addr;
bb9d5f81
PP
1915
1916 if (bitsize != 0)
1917 {
1918 /* Just cover the bytes that make up the bitfield. */
1919 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1920 }
1921 else
1922 loc->length = TYPE_LENGTH (value_type (v));
1923
a5606eee
VP
1924 loc->watchpoint_type = type;
1925 }
1926 }
9fa40276
TJB
1927 }
1928
1929 /* Change the type of breakpoint between hardware assisted or
1930 an ordinary watchpoint depending on the hardware support
1931 and free hardware slots. REPARSE is set when the inferior
1932 is started. */
a9634178 1933 if (reparse)
9fa40276 1934 {
e09342b5 1935 int reg_cnt;
9fa40276
TJB
1936 enum bp_loc_type loc_type;
1937 struct bp_location *bl;
a5606eee 1938
a9634178 1939 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1940
1941 if (reg_cnt)
9fa40276
TJB
1942 {
1943 int i, target_resources_ok, other_type_used;
a1398e0c 1944 enum bptype type;
9fa40276 1945
a9634178
TJB
1946 /* Use an exact watchpoint when there's only one memory region to be
1947 watched, and only one debug register is needed to watch it. */
1948 b->exact = target_exact_watchpoints && reg_cnt == 1;
1949
9fa40276 1950 /* We need to determine how many resources are already
e09342b5
TJB
1951 used for all other hardware watchpoints plus this one
1952 to see if we still have enough resources to also fit
a1398e0c
PA
1953 this watchpoint in as well. */
1954
1955 /* If this is a software watchpoint, we try to turn it
1956 to a hardware one -- count resources as if B was of
1957 hardware watchpoint type. */
c1fc2657 1958 type = b->type;
a1398e0c
PA
1959 if (type == bp_watchpoint)
1960 type = bp_hardware_watchpoint;
1961
1962 /* This watchpoint may or may not have been placed on
1963 the list yet at this point (it won't be in the list
1964 if we're trying to create it for the first time,
1965 through watch_command), so always account for it
1966 manually. */
1967
1968 /* Count resources used by all watchpoints except B. */
c1fc2657 1969 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1970
1971 /* Add in the resources needed for B. */
c1fc2657 1972 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1973
1974 target_resources_ok
1975 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1976 if (target_resources_ok <= 0)
a9634178 1977 {
c1fc2657 1978 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1979
1980 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1981 error (_("Target does not support this type of "
1982 "hardware watchpoint."));
9c06b0b4
TJB
1983 else if (target_resources_ok < 0 && !sw_mode)
1984 error (_("There are not enough available hardware "
1985 "resources for this watchpoint."));
a1398e0c
PA
1986
1987 /* Downgrade to software watchpoint. */
c1fc2657 1988 b->type = bp_watchpoint;
a1398e0c
PA
1989 }
1990 else
1991 {
1992 /* If this was a software watchpoint, we've just
1993 found we have enough resources to turn it to a
1994 hardware watchpoint. Otherwise, this is a
1995 nop. */
c1fc2657 1996 b->type = type;
a9634178 1997 }
9fa40276 1998 }
c1fc2657 1999 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2000 {
2001 if (!can_use_hw_watchpoints)
2002 error (_("Can't set read/access watchpoint when "
2003 "hardware watchpoints are disabled."));
2004 else
2005 error (_("Expression cannot be implemented with "
2006 "read/access watchpoint."));
2007 }
9fa40276 2008 else
c1fc2657 2009 b->type = bp_watchpoint;
9fa40276 2010
c1fc2657 2011 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2012 : bp_loc_hardware_watchpoint);
c1fc2657 2013 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2014 bl->loc_type = loc_type;
2015 }
2016
2017 for (v = val_chain; v; v = next)
2018 {
a5606eee
VP
2019 next = value_next (v);
2020 if (v != b->val)
2021 value_free (v);
2022 }
2023
c7437ca6
PA
2024 /* If a software watchpoint is not watching any memory, then the
2025 above left it without any location set up. But,
2026 bpstat_stop_status requires a location to be able to report
2027 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2028 if (b->type == bp_watchpoint && b->loc == NULL)
2029 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2030 }
2031 else if (!within_current_scope)
7270d8f2 2032 {
ac74f770
MS
2033 printf_filtered (_("\
2034Watchpoint %d deleted because the program has left the block\n\
2035in which its expression is valid.\n"),
c1fc2657 2036 b->number);
d0fb5eae 2037 watchpoint_del_at_next_stop (b);
7270d8f2 2038 }
a5606eee
VP
2039
2040 /* Restore the selected frame. */
66076460
DJ
2041 if (frame_saved)
2042 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2043}
2044
a5606eee 2045
74960c60 2046/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2047 inserted in the inferior. We don't differentiate the type of BL's owner
2048 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2049 breakpoint_ops is not defined, because in insert_bp_location,
2050 tracepoint's insert_location will not be called. */
74960c60 2051static int
35df4500 2052should_be_inserted (struct bp_location *bl)
74960c60 2053{
35df4500 2054 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2055 return 0;
2056
35df4500 2057 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2058 return 0;
2059
35df4500 2060 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2061 return 0;
2062
f8eba3c6
TT
2063 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2064 return 0;
2065
56710373
PA
2066 /* This is set for example, when we're attached to the parent of a
2067 vfork, and have detached from the child. The child is running
2068 free, and we expect it to do an exec or exit, at which point the
2069 OS makes the parent schedulable again (and the target reports
2070 that the vfork is done). Until the child is done with the shared
2071 memory region, do not insert breakpoints in the parent, otherwise
2072 the child could still trip on the parent's breakpoints. Since
2073 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2074 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2075 return 0;
2076
31e77af2 2077 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2078 location, except if the breakpoint is a single-step breakpoint,
2079 and the breakpoint's thread is the thread which is stepping past
2080 a breakpoint. */
31e77af2
PA
2081 if ((bl->loc_type == bp_loc_software_breakpoint
2082 || bl->loc_type == bp_loc_hardware_breakpoint)
2083 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2084 bl->address)
2085 /* The single-step breakpoint may be inserted at the location
2086 we're trying to step if the instruction branches to itself.
2087 However, the instruction won't be executed at all and it may
2088 break the semantics of the instruction, for example, the
2089 instruction is a conditional branch or updates some flags.
2090 We can't fix it unless GDB is able to emulate the instruction
2091 or switch to displaced stepping. */
2092 && !(bl->owner->type == bp_single_step
2093 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2094 {
2095 if (debug_infrun)
2096 {
2097 fprintf_unfiltered (gdb_stdlog,
2098 "infrun: skipping breakpoint: "
2099 "stepping past insn at: %s\n",
2100 paddress (bl->gdbarch, bl->address));
2101 }
2102 return 0;
2103 }
31e77af2 2104
963f9c80
PA
2105 /* Don't insert watchpoints if we're trying to step past the
2106 instruction that triggered one. */
2107 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2108 && stepping_past_nonsteppable_watchpoint ())
2109 {
2110 if (debug_infrun)
2111 {
2112 fprintf_unfiltered (gdb_stdlog,
2113 "infrun: stepping past non-steppable watchpoint. "
2114 "skipping watchpoint at %s:%d\n",
2115 paddress (bl->gdbarch, bl->address),
2116 bl->length);
2117 }
2118 return 0;
2119 }
2120
74960c60
VP
2121 return 1;
2122}
2123
934709f0
PW
2124/* Same as should_be_inserted but does the check assuming
2125 that the location is not duplicated. */
2126
2127static int
2128unduplicated_should_be_inserted (struct bp_location *bl)
2129{
2130 int result;
2131 const int save_duplicate = bl->duplicate;
2132
2133 bl->duplicate = 0;
2134 result = should_be_inserted (bl);
2135 bl->duplicate = save_duplicate;
2136 return result;
2137}
2138
b775012e
LM
2139/* Parses a conditional described by an expression COND into an
2140 agent expression bytecode suitable for evaluation
2141 by the bytecode interpreter. Return NULL if there was
2142 any error during parsing. */
2143
833177a4 2144static agent_expr_up
b775012e
LM
2145parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2146{
833177a4 2147 if (cond == NULL)
b775012e
LM
2148 return NULL;
2149
833177a4
PA
2150 agent_expr_up aexpr;
2151
b775012e
LM
2152 /* We don't want to stop processing, so catch any errors
2153 that may show up. */
492d29ea 2154 TRY
b775012e 2155 {
036e657b 2156 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2157 }
2158
492d29ea 2159 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2160 {
2161 /* If we got here, it means the condition could not be parsed to a valid
2162 bytecode expression and thus can't be evaluated on the target's side.
2163 It's no use iterating through the conditions. */
b775012e 2164 }
492d29ea 2165 END_CATCH
b775012e
LM
2166
2167 /* We have a valid agent expression. */
2168 return aexpr;
2169}
2170
2171/* Based on location BL, create a list of breakpoint conditions to be
2172 passed on to the target. If we have duplicated locations with different
2173 conditions, we will add such conditions to the list. The idea is that the
2174 target will evaluate the list of conditions and will only notify GDB when
2175 one of them is true. */
2176
2177static void
2178build_target_condition_list (struct bp_location *bl)
2179{
2180 struct bp_location **locp = NULL, **loc2p;
2181 int null_condition_or_parse_error = 0;
2182 int modified = bl->needs_update;
2183 struct bp_location *loc;
2184
8b4f3082 2185 /* Release conditions left over from a previous insert. */
3cde5c42 2186 bl->target_info.conditions.clear ();
8b4f3082 2187
b775012e
LM
2188 /* This is only meaningful if the target is
2189 evaluating conditions and if the user has
2190 opted for condition evaluation on the target's
2191 side. */
2192 if (gdb_evaluates_breakpoint_condition_p ()
2193 || !target_supports_evaluation_of_breakpoint_conditions ())
2194 return;
2195
2196 /* Do a first pass to check for locations with no assigned
2197 conditions or conditions that fail to parse to a valid agent expression
2198 bytecode. If any of these happen, then it's no use to send conditions
2199 to the target since this location will always trigger and generate a
2200 response back to GDB. */
2201 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2202 {
2203 loc = (*loc2p);
2204 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2205 {
2206 if (modified)
2207 {
b775012e
LM
2208 /* Re-parse the conditions since something changed. In that
2209 case we already freed the condition bytecodes (see
2210 force_breakpoint_reinsertion). We just
2211 need to parse the condition to bytecodes again. */
833177a4
PA
2212 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2213 loc->cond.get ());
b775012e
LM
2214 }
2215
2216 /* If we have a NULL bytecode expression, it means something
2217 went wrong or we have a null condition expression. */
2218 if (!loc->cond_bytecode)
2219 {
2220 null_condition_or_parse_error = 1;
2221 break;
2222 }
2223 }
2224 }
2225
2226 /* If any of these happened, it means we will have to evaluate the conditions
2227 for the location's address on gdb's side. It is no use keeping bytecodes
2228 for all the other duplicate locations, thus we free all of them here.
2229
2230 This is so we have a finer control over which locations' conditions are
2231 being evaluated by GDB or the remote stub. */
2232 if (null_condition_or_parse_error)
2233 {
2234 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2235 {
2236 loc = (*loc2p);
2237 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2238 {
2239 /* Only go as far as the first NULL bytecode is
2240 located. */
2241 if (!loc->cond_bytecode)
2242 return;
2243
833177a4 2244 loc->cond_bytecode.reset ();
b775012e
LM
2245 }
2246 }
2247 }
2248
2249 /* No NULL conditions or failed bytecode generation. Build a condition list
2250 for this location's address. */
2251 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2252 {
2253 loc = (*loc2p);
2254 if (loc->cond
2255 && is_breakpoint (loc->owner)
2256 && loc->pspace->num == bl->pspace->num
2257 && loc->owner->enable_state == bp_enabled
2258 && loc->enabled)
3cde5c42
PA
2259 {
2260 /* Add the condition to the vector. This will be used later
2261 to send the conditions to the target. */
2262 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2263 }
b775012e
LM
2264 }
2265
2266 return;
2267}
2268
d3ce09f5
SS
2269/* Parses a command described by string CMD into an agent expression
2270 bytecode suitable for evaluation by the bytecode interpreter.
2271 Return NULL if there was any error during parsing. */
2272
833177a4 2273static agent_expr_up
d3ce09f5
SS
2274parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2275{
2276 struct cleanup *old_cleanups = 0;
4d01a485 2277 struct expression **argvec;
bbc13ae3
KS
2278 const char *cmdrest;
2279 const char *format_start, *format_end;
d3ce09f5
SS
2280 struct format_piece *fpieces;
2281 int nargs;
2282 struct gdbarch *gdbarch = get_current_arch ();
2283
833177a4 2284 if (cmd == NULL)
d3ce09f5
SS
2285 return NULL;
2286
2287 cmdrest = cmd;
2288
2289 if (*cmdrest == ',')
2290 ++cmdrest;
f1735a53 2291 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2292
2293 if (*cmdrest++ != '"')
2294 error (_("No format string following the location"));
2295
2296 format_start = cmdrest;
2297
2298 fpieces = parse_format_string (&cmdrest);
2299
2300 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2301
2302 format_end = cmdrest;
2303
2304 if (*cmdrest++ != '"')
2305 error (_("Bad format string, non-terminated '\"'."));
2306
f1735a53 2307 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2308
2309 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2310 error (_("Invalid argument syntax"));
2311
2312 if (*cmdrest == ',')
2313 cmdrest++;
f1735a53 2314 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2315
2316 /* For each argument, make an expression. */
2317
2318 argvec = (struct expression **) alloca (strlen (cmd)
2319 * sizeof (struct expression *));
2320
2321 nargs = 0;
2322 while (*cmdrest != '\0')
2323 {
bbc13ae3 2324 const char *cmd1;
d3ce09f5
SS
2325
2326 cmd1 = cmdrest;
4d01a485
PA
2327 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2328 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2329 cmdrest = cmd1;
2330 if (*cmdrest == ',')
2331 ++cmdrest;
2332 }
2333
833177a4
PA
2334 agent_expr_up aexpr;
2335
d3ce09f5
SS
2336 /* We don't want to stop processing, so catch any errors
2337 that may show up. */
492d29ea 2338 TRY
d3ce09f5 2339 {
036e657b
JB
2340 aexpr = gen_printf (scope, gdbarch, 0, 0,
2341 format_start, format_end - format_start,
2342 fpieces, nargs, argvec);
d3ce09f5 2343 }
492d29ea 2344 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2345 {
2346 /* If we got here, it means the command could not be parsed to a valid
2347 bytecode expression and thus can't be evaluated on the target's side.
2348 It's no use iterating through the other commands. */
d3ce09f5 2349 }
492d29ea
PA
2350 END_CATCH
2351
2352 do_cleanups (old_cleanups);
d3ce09f5 2353
d3ce09f5
SS
2354 /* We have a valid agent expression, return it. */
2355 return aexpr;
2356}
2357
2358/* Based on location BL, create a list of breakpoint commands to be
2359 passed on to the target. If we have duplicated locations with
2360 different commands, we will add any such to the list. */
2361
2362static void
2363build_target_command_list (struct bp_location *bl)
2364{
2365 struct bp_location **locp = NULL, **loc2p;
2366 int null_command_or_parse_error = 0;
2367 int modified = bl->needs_update;
2368 struct bp_location *loc;
2369
3cde5c42
PA
2370 /* Clear commands left over from a previous insert. */
2371 bl->target_info.tcommands.clear ();
8b4f3082 2372
41fac0cf 2373 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2374 return;
2375
41fac0cf
PA
2376 /* For now, limit to agent-style dprintf breakpoints. */
2377 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2378 return;
2379
41fac0cf
PA
2380 /* For now, if we have any duplicate location that isn't a dprintf,
2381 don't install the target-side commands, as that would make the
2382 breakpoint not be reported to the core, and we'd lose
2383 control. */
2384 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2385 {
2386 loc = (*loc2p);
2387 if (is_breakpoint (loc->owner)
2388 && loc->pspace->num == bl->pspace->num
2389 && loc->owner->type != bp_dprintf)
2390 return;
2391 }
2392
d3ce09f5
SS
2393 /* Do a first pass to check for locations with no assigned
2394 conditions or conditions that fail to parse to a valid agent expression
2395 bytecode. If any of these happen, then it's no use to send conditions
2396 to the target since this location will always trigger and generate a
2397 response back to GDB. */
2398 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2399 {
2400 loc = (*loc2p);
2401 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2402 {
2403 if (modified)
2404 {
d3ce09f5
SS
2405 /* Re-parse the commands since something changed. In that
2406 case we already freed the command bytecodes (see
2407 force_breakpoint_reinsertion). We just
2408 need to parse the command to bytecodes again. */
833177a4
PA
2409 loc->cmd_bytecode
2410 = parse_cmd_to_aexpr (bl->address,
2411 loc->owner->extra_string);
d3ce09f5
SS
2412 }
2413
2414 /* If we have a NULL bytecode expression, it means something
2415 went wrong or we have a null command expression. */
2416 if (!loc->cmd_bytecode)
2417 {
2418 null_command_or_parse_error = 1;
2419 break;
2420 }
2421 }
2422 }
2423
2424 /* If anything failed, then we're not doing target-side commands,
2425 and so clean up. */
2426 if (null_command_or_parse_error)
2427 {
2428 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2429 {
2430 loc = (*loc2p);
2431 if (is_breakpoint (loc->owner)
2432 && loc->pspace->num == bl->pspace->num)
2433 {
2434 /* Only go as far as the first NULL bytecode is
2435 located. */
40fb6c5e 2436 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2437 return;
2438
833177a4 2439 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2440 }
2441 }
2442 }
2443
2444 /* No NULL commands or failed bytecode generation. Build a command list
2445 for this location's address. */
2446 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2447 {
2448 loc = (*loc2p);
2449 if (loc->owner->extra_string
2450 && is_breakpoint (loc->owner)
2451 && loc->pspace->num == bl->pspace->num
2452 && loc->owner->enable_state == bp_enabled
2453 && loc->enabled)
3cde5c42
PA
2454 {
2455 /* Add the command to the vector. This will be used later
2456 to send the commands to the target. */
2457 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2458 }
d3ce09f5
SS
2459 }
2460
2461 bl->target_info.persist = 0;
2462 /* Maybe flag this location as persistent. */
2463 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2464 bl->target_info.persist = 1;
2465}
2466
833b7ab5
YQ
2467/* Return the kind of breakpoint on address *ADDR. Get the kind
2468 of breakpoint according to ADDR except single-step breakpoint.
2469 Get the kind of single-step breakpoint according to the current
2470 registers state. */
cd6c3b4f
YQ
2471
2472static int
2473breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2474{
833b7ab5
YQ
2475 if (bl->owner->type == bp_single_step)
2476 {
2477 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2478 struct regcache *regcache;
2479
2480 regcache = get_thread_regcache (thr->ptid);
2481
2482 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2483 regcache, addr);
2484 }
2485 else
2486 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2487}
2488
35df4500
TJB
2489/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2490 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2491 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2492 Returns 0 for success, 1 if the bp_location type is not supported or
2493 -1 for failure.
879bfdc2 2494
4a64f543
MS
2495 NOTE drow/2003-09-09: This routine could be broken down to an
2496 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2497static int
35df4500 2498insert_bp_location (struct bp_location *bl,
26bb91f3 2499 struct ui_file *tmp_error_stream,
3fbb6ffa 2500 int *disabled_breaks,
dd61ec5c
MW
2501 int *hw_breakpoint_error,
2502 int *hw_bp_error_explained_already)
879bfdc2 2503{
0000e5cc
PA
2504 enum errors bp_err = GDB_NO_ERROR;
2505 const char *bp_err_message = NULL;
879bfdc2 2506
b775012e 2507 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2508 return 0;
2509
35c63cd8
JB
2510 /* Note we don't initialize bl->target_info, as that wipes out
2511 the breakpoint location's shadow_contents if the breakpoint
2512 is still inserted at that location. This in turn breaks
2513 target_read_memory which depends on these buffers when
2514 a memory read is requested at the breakpoint location:
2515 Once the target_info has been wiped, we fail to see that
2516 we have a breakpoint inserted at that address and thus
2517 read the breakpoint instead of returning the data saved in
2518 the breakpoint location's shadow contents. */
0d5ed153 2519 bl->target_info.reqstd_address = bl->address;
35df4500 2520 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2521 bl->target_info.length = bl->length;
8181d85f 2522
b775012e
LM
2523 /* When working with target-side conditions, we must pass all the conditions
2524 for the same breakpoint address down to the target since GDB will not
2525 insert those locations. With a list of breakpoint conditions, the target
2526 can decide when to stop and notify GDB. */
2527
2528 if (is_breakpoint (bl->owner))
2529 {
2530 build_target_condition_list (bl);
d3ce09f5
SS
2531 build_target_command_list (bl);
2532 /* Reset the modification marker. */
b775012e
LM
2533 bl->needs_update = 0;
2534 }
2535
35df4500
TJB
2536 if (bl->loc_type == bp_loc_software_breakpoint
2537 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2538 {
35df4500 2539 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2540 {
2541 /* If the explicitly specified breakpoint type
2542 is not hardware breakpoint, check the memory map to see
2543 if the breakpoint address is in read only memory or not.
4a64f543 2544
765dc015
VP
2545 Two important cases are:
2546 - location type is not hardware breakpoint, memory
2547 is readonly. We change the type of the location to
2548 hardware breakpoint.
4a64f543
MS
2549 - location type is hardware breakpoint, memory is
2550 read-write. This means we've previously made the
2551 location hardware one, but then the memory map changed,
2552 so we undo.
765dc015 2553
4a64f543
MS
2554 When breakpoints are removed, remove_breakpoints will use
2555 location types we've just set here, the only possible
2556 problem is that memory map has changed during running
2557 program, but it's not going to work anyway with current
2558 gdb. */
765dc015 2559 struct mem_region *mr
0d5ed153 2560 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2561
2562 if (mr)
2563 {
2564 if (automatic_hardware_breakpoints)
2565 {
765dc015
VP
2566 enum bp_loc_type new_type;
2567
2568 if (mr->attrib.mode != MEM_RW)
2569 new_type = bp_loc_hardware_breakpoint;
2570 else
2571 new_type = bp_loc_software_breakpoint;
2572
35df4500 2573 if (new_type != bl->loc_type)
765dc015
VP
2574 {
2575 static int said = 0;
cc59ec59 2576
35df4500 2577 bl->loc_type = new_type;
765dc015
VP
2578 if (!said)
2579 {
3e43a32a
MS
2580 fprintf_filtered (gdb_stdout,
2581 _("Note: automatically using "
2582 "hardware breakpoints for "
2583 "read-only addresses.\n"));
765dc015
VP
2584 said = 1;
2585 }
2586 }
2587 }
35df4500 2588 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2589 && mr->attrib.mode != MEM_RW)
2590 {
2591 fprintf_unfiltered (tmp_error_stream,
2592 _("Cannot insert breakpoint %d.\n"
2593 "Cannot set software breakpoint "
2594 "at read-only address %s\n"),
2595 bl->owner->number,
2596 paddress (bl->gdbarch, bl->address));
2597 return 1;
2598 }
765dc015
VP
2599 }
2600 }
2601
879bfdc2
DJ
2602 /* First check to see if we have to handle an overlay. */
2603 if (overlay_debugging == ovly_off
35df4500
TJB
2604 || bl->section == NULL
2605 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2606 {
2607 /* No overlay handling: just set the breakpoint. */
492d29ea 2608 TRY
dd61ec5c 2609 {
0000e5cc
PA
2610 int val;
2611
dd61ec5c 2612 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2613 if (val)
2614 bp_err = GENERIC_ERROR;
dd61ec5c 2615 }
492d29ea 2616 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2617 {
0000e5cc
PA
2618 bp_err = e.error;
2619 bp_err_message = e.message;
dd61ec5c 2620 }
492d29ea 2621 END_CATCH
879bfdc2
DJ
2622 }
2623 else
2624 {
4a64f543 2625 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2626 Shall we set a breakpoint at the LMA? */
2627 if (!overlay_events_enabled)
2628 {
2629 /* Yes -- overlay event support is not active,
2630 so we must try to set a breakpoint at the LMA.
2631 This will not work for a hardware breakpoint. */
35df4500 2632 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2633 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2634 bl->owner->number);
879bfdc2
DJ
2635 else
2636 {
35df4500
TJB
2637 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2638 bl->section);
879bfdc2 2639 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2640 bl->overlay_target_info = bl->target_info;
0d5ed153 2641 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2642
2643 /* No overlay handling: just set the breakpoint. */
492d29ea 2644 TRY
0000e5cc
PA
2645 {
2646 int val;
2647
579c6ad9 2648 bl->overlay_target_info.kind
cd6c3b4f
YQ
2649 = breakpoint_kind (bl, &addr);
2650 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2651 val = target_insert_breakpoint (bl->gdbarch,
2652 &bl->overlay_target_info);
2653 if (val)
2654 bp_err = GENERIC_ERROR;
2655 }
492d29ea 2656 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2657 {
2658 bp_err = e.error;
2659 bp_err_message = e.message;
2660 }
492d29ea 2661 END_CATCH
0000e5cc
PA
2662
2663 if (bp_err != GDB_NO_ERROR)
99361f52 2664 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2665 "Overlay breakpoint %d "
2666 "failed: in ROM?\n",
35df4500 2667 bl->owner->number);
879bfdc2
DJ
2668 }
2669 }
2670 /* Shall we set a breakpoint at the VMA? */
35df4500 2671 if (section_is_mapped (bl->section))
879bfdc2
DJ
2672 {
2673 /* Yes. This overlay section is mapped into memory. */
492d29ea 2674 TRY
dd61ec5c 2675 {
0000e5cc
PA
2676 int val;
2677
dd61ec5c 2678 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2679 if (val)
2680 bp_err = GENERIC_ERROR;
dd61ec5c 2681 }
492d29ea 2682 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2683 {
0000e5cc
PA
2684 bp_err = e.error;
2685 bp_err_message = e.message;
dd61ec5c 2686 }
492d29ea 2687 END_CATCH
879bfdc2
DJ
2688 }
2689 else
2690 {
2691 /* No. This breakpoint will not be inserted.
2692 No error, but do not mark the bp as 'inserted'. */
2693 return 0;
2694 }
2695 }
2696
0000e5cc 2697 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2698 {
2699 /* Can't set the breakpoint. */
0000e5cc
PA
2700
2701 /* In some cases, we might not be able to insert a
2702 breakpoint in a shared library that has already been
2703 removed, but we have not yet processed the shlib unload
2704 event. Unfortunately, some targets that implement
076855f9
PA
2705 breakpoint insertion themselves can't tell why the
2706 breakpoint insertion failed (e.g., the remote target
2707 doesn't define error codes), so we must treat generic
2708 errors as memory errors. */
0000e5cc 2709 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2710 && bl->loc_type == bp_loc_software_breakpoint
08351840 2711 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2712 || shared_objfile_contains_address_p (bl->pspace,
2713 bl->address)))
879bfdc2 2714 {
4a64f543 2715 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2716 bl->shlib_disabled = 1;
8d3788bd 2717 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2718 if (!*disabled_breaks)
2719 {
2720 fprintf_unfiltered (tmp_error_stream,
2721 "Cannot insert breakpoint %d.\n",
2722 bl->owner->number);
2723 fprintf_unfiltered (tmp_error_stream,
2724 "Temporarily disabling shared "
2725 "library breakpoints:\n");
2726 }
2727 *disabled_breaks = 1;
879bfdc2 2728 fprintf_unfiltered (tmp_error_stream,
35df4500 2729 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2730 return 0;
879bfdc2
DJ
2731 }
2732 else
879bfdc2 2733 {
35df4500 2734 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2735 {
0000e5cc
PA
2736 *hw_breakpoint_error = 1;
2737 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2738 fprintf_unfiltered (tmp_error_stream,
2739 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2740 bl->owner->number, bp_err_message ? ":" : ".\n");
2741 if (bp_err_message != NULL)
2742 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2743 }
2744 else
2745 {
0000e5cc
PA
2746 if (bp_err_message == NULL)
2747 {
1ccbe998 2748 std::string message
0000e5cc
PA
2749 = memory_error_message (TARGET_XFER_E_IO,
2750 bl->gdbarch, bl->address);
0000e5cc
PA
2751
2752 fprintf_unfiltered (tmp_error_stream,
2753 "Cannot insert breakpoint %d.\n"
2754 "%s\n",
1ccbe998 2755 bl->owner->number, message.c_str ());
0000e5cc
PA
2756 }
2757 else
2758 {
2759 fprintf_unfiltered (tmp_error_stream,
2760 "Cannot insert breakpoint %d: %s\n",
2761 bl->owner->number,
2762 bp_err_message);
2763 }
879bfdc2 2764 }
0000e5cc 2765 return 1;
879bfdc2
DJ
2766
2767 }
2768 }
2769 else
35df4500 2770 bl->inserted = 1;
879bfdc2 2771
0000e5cc 2772 return 0;
879bfdc2
DJ
2773 }
2774
35df4500 2775 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2776 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2777 watchpoints. It's not clear that it's necessary... */
35df4500 2778 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2779 {
0000e5cc
PA
2780 int val;
2781
77b06cd7
TJB
2782 gdb_assert (bl->owner->ops != NULL
2783 && bl->owner->ops->insert_location != NULL);
2784
2785 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2786
2787 /* If trying to set a read-watchpoint, and it turns out it's not
2788 supported, try emulating one with an access watchpoint. */
35df4500 2789 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2790 {
2791 struct bp_location *loc, **loc_temp;
2792
2793 /* But don't try to insert it, if there's already another
2794 hw_access location that would be considered a duplicate
2795 of this one. */
2796 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2797 if (loc != bl
85d721b8 2798 && loc->watchpoint_type == hw_access
35df4500 2799 && watchpoint_locations_match (bl, loc))
85d721b8 2800 {
35df4500
TJB
2801 bl->duplicate = 1;
2802 bl->inserted = 1;
2803 bl->target_info = loc->target_info;
2804 bl->watchpoint_type = hw_access;
85d721b8
PA
2805 val = 0;
2806 break;
2807 }
2808
2809 if (val == 1)
2810 {
77b06cd7
TJB
2811 bl->watchpoint_type = hw_access;
2812 val = bl->owner->ops->insert_location (bl);
2813
2814 if (val)
2815 /* Back to the original value. */
2816 bl->watchpoint_type = hw_read;
85d721b8
PA
2817 }
2818 }
2819
35df4500 2820 bl->inserted = (val == 0);
879bfdc2
DJ
2821 }
2822
35df4500 2823 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2824 {
0000e5cc
PA
2825 int val;
2826
77b06cd7
TJB
2827 gdb_assert (bl->owner->ops != NULL
2828 && bl->owner->ops->insert_location != NULL);
2829
2830 val = bl->owner->ops->insert_location (bl);
2831 if (val)
2832 {
2833 bl->owner->enable_state = bp_disabled;
2834
2835 if (val == 1)
2836 warning (_("\
2837Error inserting catchpoint %d: Your system does not support this type\n\
2838of catchpoint."), bl->owner->number);
2839 else
2840 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2841 }
2842
2843 bl->inserted = (val == 0);
1640b821
DJ
2844
2845 /* We've already printed an error message if there was a problem
2846 inserting this catchpoint, and we've disabled the catchpoint,
2847 so just return success. */
2848 return 0;
879bfdc2
DJ
2849 }
2850
2851 return 0;
2852}
2853
6c95b8df
PA
2854/* This function is called when program space PSPACE is about to be
2855 deleted. It takes care of updating breakpoints to not reference
2856 PSPACE anymore. */
2857
2858void
2859breakpoint_program_space_exit (struct program_space *pspace)
2860{
2861 struct breakpoint *b, *b_temp;
876fa593 2862 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2863
2864 /* Remove any breakpoint that was set through this program space. */
2865 ALL_BREAKPOINTS_SAFE (b, b_temp)
2866 {
2867 if (b->pspace == pspace)
2868 delete_breakpoint (b);
2869 }
2870
2871 /* Breakpoints set through other program spaces could have locations
2872 bound to PSPACE as well. Remove those. */
876fa593 2873 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2874 {
2875 struct bp_location *tmp;
2876
2877 if (loc->pspace == pspace)
2878 {
2bdf28a0 2879 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2880 if (loc->owner->loc == loc)
2881 loc->owner->loc = loc->next;
2882 else
2883 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2884 if (tmp->next == loc)
2885 {
2886 tmp->next = loc->next;
2887 break;
2888 }
2889 }
2890 }
2891
2892 /* Now update the global location list to permanently delete the
2893 removed locations above. */
44702360 2894 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2895}
2896
74960c60
VP
2897/* Make sure all breakpoints are inserted in inferior.
2898 Throws exception on any error.
2899 A breakpoint that is already inserted won't be inserted
2900 again, so calling this function twice is safe. */
2901void
2902insert_breakpoints (void)
2903{
2904 struct breakpoint *bpt;
2905
2906 ALL_BREAKPOINTS (bpt)
2907 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2908 {
2909 struct watchpoint *w = (struct watchpoint *) bpt;
2910
2911 update_watchpoint (w, 0 /* don't reparse. */);
2912 }
74960c60 2913
04086b45
PA
2914 /* Updating watchpoints creates new locations, so update the global
2915 location list. Explicitly tell ugll to insert locations and
2916 ignore breakpoints_always_inserted_mode. */
2917 update_global_location_list (UGLL_INSERT);
74960c60
VP
2918}
2919
20388dd6
YQ
2920/* Invoke CALLBACK for each of bp_location. */
2921
2922void
2923iterate_over_bp_locations (walk_bp_location_callback callback)
2924{
2925 struct bp_location *loc, **loc_tmp;
2926
2927 ALL_BP_LOCATIONS (loc, loc_tmp)
2928 {
2929 callback (loc, NULL);
2930 }
2931}
2932
b775012e
LM
2933/* This is used when we need to synch breakpoint conditions between GDB and the
2934 target. It is the case with deleting and disabling of breakpoints when using
2935 always-inserted mode. */
2936
2937static void
2938update_inserted_breakpoint_locations (void)
2939{
2940 struct bp_location *bl, **blp_tmp;
2941 int error_flag = 0;
2942 int val = 0;
2943 int disabled_breaks = 0;
2944 int hw_breakpoint_error = 0;
dd61ec5c 2945 int hw_bp_details_reported = 0;
b775012e 2946
d7e74731 2947 string_file tmp_error_stream;
b775012e
LM
2948
2949 /* Explicitly mark the warning -- this will only be printed if
2950 there was an error. */
d7e74731 2951 tmp_error_stream.puts ("Warning:\n");
b775012e 2952
5ed8105e 2953 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2954
2955 ALL_BP_LOCATIONS (bl, blp_tmp)
2956 {
2957 /* We only want to update software breakpoints and hardware
2958 breakpoints. */
2959 if (!is_breakpoint (bl->owner))
2960 continue;
2961
2962 /* We only want to update locations that are already inserted
2963 and need updating. This is to avoid unwanted insertion during
2964 deletion of breakpoints. */
2965 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2966 continue;
2967
2968 switch_to_program_space_and_thread (bl->pspace);
2969
2970 /* For targets that support global breakpoints, there's no need
2971 to select an inferior to insert breakpoint to. In fact, even
2972 if we aren't attached to any process yet, we should still
2973 insert breakpoints. */
f5656ead 2974 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2975 && ptid_equal (inferior_ptid, null_ptid))
2976 continue;
2977
d7e74731 2978 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2979 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2980 if (val)
2981 error_flag = val;
2982 }
2983
2984 if (error_flag)
2985 {
223ffa71 2986 target_terminal::ours_for_output ();
b775012e
LM
2987 error_stream (tmp_error_stream);
2988 }
b775012e
LM
2989}
2990
c30eee59 2991/* Used when starting or continuing the program. */
c906108c 2992
74960c60
VP
2993static void
2994insert_breakpoint_locations (void)
c906108c 2995{
a5606eee 2996 struct breakpoint *bpt;
35df4500 2997 struct bp_location *bl, **blp_tmp;
eacd795a 2998 int error_flag = 0;
c906108c 2999 int val = 0;
3fbb6ffa 3000 int disabled_breaks = 0;
81d0cc19 3001 int hw_breakpoint_error = 0;
dd61ec5c 3002 int hw_bp_error_explained_already = 0;
c906108c 3003
d7e74731
PA
3004 string_file tmp_error_stream;
3005
81d0cc19
GS
3006 /* Explicitly mark the warning -- this will only be printed if
3007 there was an error. */
d7e74731 3008 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3009
5ed8105e 3010 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3011
35df4500 3012 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3013 {
b775012e 3014 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3015 continue;
3016
4a64f543
MS
3017 /* There is no point inserting thread-specific breakpoints if
3018 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3019 has BL->OWNER always non-NULL. */
35df4500 3020 if (bl->owner->thread != -1
5d5658a1 3021 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3022 continue;
3023
35df4500 3024 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3025
3026 /* For targets that support global breakpoints, there's no need
3027 to select an inferior to insert breakpoint to. In fact, even
3028 if we aren't attached to any process yet, we should still
3029 insert breakpoints. */
f5656ead 3030 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3031 && ptid_equal (inferior_ptid, null_ptid))
3032 continue;
3033
d7e74731 3034 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3035 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3036 if (val)
eacd795a 3037 error_flag = val;
879bfdc2 3038 }
c906108c 3039
4a64f543
MS
3040 /* If we failed to insert all locations of a watchpoint, remove
3041 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3042 ALL_BREAKPOINTS (bpt)
3043 {
3044 int some_failed = 0;
3045 struct bp_location *loc;
3046
3047 if (!is_hardware_watchpoint (bpt))
3048 continue;
3049
d6b74ac4 3050 if (!breakpoint_enabled (bpt))
a5606eee 3051 continue;
74960c60
VP
3052
3053 if (bpt->disposition == disp_del_at_next_stop)
3054 continue;
a5606eee
VP
3055
3056 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3057 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3058 {
3059 some_failed = 1;
3060 break;
3061 }
3062 if (some_failed)
3063 {
3064 for (loc = bpt->loc; loc; loc = loc->next)
3065 if (loc->inserted)
834c0d03 3066 remove_breakpoint (loc);
a5606eee
VP
3067
3068 hw_breakpoint_error = 1;
d7e74731
PA
3069 tmp_error_stream.printf ("Could not insert "
3070 "hardware watchpoint %d.\n",
3071 bpt->number);
eacd795a 3072 error_flag = -1;
a5606eee
VP
3073 }
3074 }
3075
eacd795a 3076 if (error_flag)
81d0cc19
GS
3077 {
3078 /* If a hardware breakpoint or watchpoint was inserted, add a
3079 message about possibly exhausted resources. */
dd61ec5c 3080 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3081 {
d7e74731 3082 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3083You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3084 }
223ffa71 3085 target_terminal::ours_for_output ();
81d0cc19
GS
3086 error_stream (tmp_error_stream);
3087 }
c906108c
SS
3088}
3089
c30eee59
TJB
3090/* Used when the program stops.
3091 Returns zero if successful, or non-zero if there was a problem
3092 removing a breakpoint location. */
3093
c906108c 3094int
fba45db2 3095remove_breakpoints (void)
c906108c 3096{
35df4500 3097 struct bp_location *bl, **blp_tmp;
3a1bae8e 3098 int val = 0;
c906108c 3099
35df4500 3100 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3101 {
1e4d1764 3102 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3103 val |= remove_breakpoint (bl);
c5aa993b 3104 }
3a1bae8e 3105 return val;
c906108c
SS
3106}
3107
49fa26b0
PA
3108/* When a thread exits, remove breakpoints that are related to
3109 that thread. */
3110
3111static void
3112remove_threaded_breakpoints (struct thread_info *tp, int silent)
3113{
3114 struct breakpoint *b, *b_tmp;
3115
3116 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3117 {
5d5658a1 3118 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3119 {
3120 b->disposition = disp_del_at_next_stop;
3121
3122 printf_filtered (_("\
43792cf0
PA
3123Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3124 b->number, print_thread_id (tp));
49fa26b0
PA
3125
3126 /* Hide it from the user. */
3127 b->number = 0;
3128 }
3129 }
3130}
3131
6c95b8df
PA
3132/* Remove breakpoints of process PID. */
3133
3134int
3135remove_breakpoints_pid (int pid)
3136{
35df4500 3137 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3138 int val;
3139 struct inferior *inf = find_inferior_pid (pid);
3140
35df4500 3141 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3142 {
35df4500 3143 if (bl->pspace != inf->pspace)
6c95b8df
PA
3144 continue;
3145
fc126975 3146 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3147 {
834c0d03 3148 val = remove_breakpoint (bl);
6c95b8df
PA
3149 if (val != 0)
3150 return val;
3151 }
3152 }
3153 return 0;
3154}
3155
c906108c 3156int
fba45db2 3157reattach_breakpoints (int pid)
c906108c 3158{
35df4500 3159 struct bp_location *bl, **blp_tmp;
c906108c 3160 int val;
dd61ec5c 3161 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3162 struct inferior *inf;
3163 struct thread_info *tp;
3164
3165 tp = any_live_thread_of_process (pid);
3166 if (tp == NULL)
3167 return 1;
3168
3169 inf = find_inferior_pid (pid);
6c95b8df 3170
2989a365 3171 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3172 inferior_ptid = tp->ptid;
a4954f26 3173
d7e74731 3174 string_file tmp_error_stream;
c906108c 3175
35df4500 3176 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3177 {
35df4500 3178 if (bl->pspace != inf->pspace)
6c95b8df
PA
3179 continue;
3180
35df4500 3181 if (bl->inserted)
c5aa993b 3182 {
35df4500 3183 bl->inserted = 0;
d7e74731 3184 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b 3185 if (val != 0)
2989a365 3186 return val;
c5aa993b
JM
3187 }
3188 }
c906108c
SS
3189 return 0;
3190}
3191
e58b0e63
PA
3192static int internal_breakpoint_number = -1;
3193
84f4c1fe
PM
3194/* Set the breakpoint number of B, depending on the value of INTERNAL.
3195 If INTERNAL is non-zero, the breakpoint number will be populated
3196 from internal_breakpoint_number and that variable decremented.
e5dd4106 3197 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3198 breakpoint_count and that value incremented. Internal breakpoints
3199 do not set the internal var bpnum. */
3200static void
3201set_breakpoint_number (int internal, struct breakpoint *b)
3202{
3203 if (internal)
3204 b->number = internal_breakpoint_number--;
3205 else
3206 {
3207 set_breakpoint_count (breakpoint_count + 1);
3208 b->number = breakpoint_count;
3209 }
3210}
3211
e62c965a 3212static struct breakpoint *
a6d9a66e 3213create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3214 CORE_ADDR address, enum bptype type,
c0a91b2b 3215 const struct breakpoint_ops *ops)
e62c965a 3216{
51abb421 3217 symtab_and_line sal;
e62c965a
PP
3218 sal.pc = address;
3219 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3220 sal.pspace = current_program_space;
e62c965a 3221
51abb421 3222 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3223 b->number = internal_breakpoint_number--;
3224 b->disposition = disp_donttouch;
3225
3226 return b;
3227}
3228
17450429
PP
3229static const char *const longjmp_names[] =
3230 {
3231 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3232 };
3233#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3234
3235/* Per-objfile data private to breakpoint.c. */
3236struct breakpoint_objfile_data
3237{
3238 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3239 struct bound_minimal_symbol overlay_msym;
17450429
PP
3240
3241 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3242 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3243
28106bc2
SDJ
3244 /* True if we have looked for longjmp probes. */
3245 int longjmp_searched;
3246
3247 /* SystemTap probe points for longjmp (if any). */
3248 VEC (probe_p) *longjmp_probes;
3249
17450429 3250 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3251 struct bound_minimal_symbol terminate_msym;
17450429
PP
3252
3253 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3254 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3255
3256 /* True if we have looked for exception probes. */
3257 int exception_searched;
3258
3259 /* SystemTap probe points for unwinding (if any). */
3260 VEC (probe_p) *exception_probes;
17450429
PP
3261};
3262
3263static const struct objfile_data *breakpoint_objfile_key;
3264
3265/* Minimal symbol not found sentinel. */
3266static struct minimal_symbol msym_not_found;
3267
3268/* Returns TRUE if MSYM point to the "not found" sentinel. */
3269
3270static int
3271msym_not_found_p (const struct minimal_symbol *msym)
3272{
3273 return msym == &msym_not_found;
3274}
3275
3276/* Return per-objfile data needed by breakpoint.c.
3277 Allocate the data if necessary. */
3278
3279static struct breakpoint_objfile_data *
3280get_breakpoint_objfile_data (struct objfile *objfile)
3281{
3282 struct breakpoint_objfile_data *bp_objfile_data;
3283
9a3c8263
SM
3284 bp_objfile_data = ((struct breakpoint_objfile_data *)
3285 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3286 if (bp_objfile_data == NULL)
3287 {
8d749320
SM
3288 bp_objfile_data =
3289 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3290
3291 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3292 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3293 }
3294 return bp_objfile_data;
3295}
3296
28106bc2
SDJ
3297static void
3298free_breakpoint_probes (struct objfile *obj, void *data)
3299{
9a3c8263
SM
3300 struct breakpoint_objfile_data *bp_objfile_data
3301 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3302
3303 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3304 VEC_free (probe_p, bp_objfile_data->exception_probes);
3305}
3306
e62c965a 3307static void
af02033e 3308create_overlay_event_breakpoint (void)
e62c965a 3309{
69de3c6a 3310 struct objfile *objfile;
af02033e 3311 const char *const func_name = "_ovly_debug_event";
e62c965a 3312
69de3c6a
PP
3313 ALL_OBJFILES (objfile)
3314 {
3315 struct breakpoint *b;
17450429
PP
3316 struct breakpoint_objfile_data *bp_objfile_data;
3317 CORE_ADDR addr;
67994074 3318 struct explicit_location explicit_loc;
69de3c6a 3319
17450429
PP
3320 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3321
3b7344d5 3322 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3323 continue;
3324
3b7344d5 3325 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3326 {
3b7344d5 3327 struct bound_minimal_symbol m;
17450429
PP
3328
3329 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3330 if (m.minsym == NULL)
17450429
PP
3331 {
3332 /* Avoid future lookups in this objfile. */
3b7344d5 3333 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3334 continue;
3335 }
3336 bp_objfile_data->overlay_msym = m;
3337 }
e62c965a 3338
77e371c0 3339 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3340 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3341 bp_overlay_event,
3342 &internal_breakpoint_ops);
67994074
KS
3343 initialize_explicit_location (&explicit_loc);
3344 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3345 b->location = new_explicit_location (&explicit_loc);
e62c965a 3346
69de3c6a
PP
3347 if (overlay_debugging == ovly_auto)
3348 {
3349 b->enable_state = bp_enabled;
3350 overlay_events_enabled = 1;
3351 }
3352 else
3353 {
3354 b->enable_state = bp_disabled;
3355 overlay_events_enabled = 0;
3356 }
e62c965a 3357 }
e62c965a
PP
3358}
3359
0fd8e87f 3360static void
af02033e 3361create_longjmp_master_breakpoint (void)
0fd8e87f 3362{
6c95b8df 3363 struct program_space *pspace;
6c95b8df 3364
5ed8105e 3365 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3366
6c95b8df 3367 ALL_PSPACES (pspace)
af02033e
PP
3368 {
3369 struct objfile *objfile;
3370
3371 set_current_program_space (pspace);
3372
3373 ALL_OBJFILES (objfile)
0fd8e87f 3374 {
af02033e
PP
3375 int i;
3376 struct gdbarch *gdbarch;
17450429 3377 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3378
af02033e 3379 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3380
17450429
PP
3381 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3382
28106bc2
SDJ
3383 if (!bp_objfile_data->longjmp_searched)
3384 {
25f9533e
SDJ
3385 VEC (probe_p) *ret;
3386
3387 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3388 if (ret != NULL)
3389 {
3390 /* We are only interested in checking one element. */
3391 struct probe *p = VEC_index (probe_p, ret, 0);
3392
3393 if (!can_evaluate_probe_arguments (p))
3394 {
3395 /* We cannot use the probe interface here, because it does
3396 not know how to evaluate arguments. */
3397 VEC_free (probe_p, ret);
3398 ret = NULL;
3399 }
3400 }
3401 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3402 bp_objfile_data->longjmp_searched = 1;
3403 }
3404
3405 if (bp_objfile_data->longjmp_probes != NULL)
3406 {
3407 int i;
3408 struct probe *probe;
3409 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3410
3411 for (i = 0;
3412 VEC_iterate (probe_p,
3413 bp_objfile_data->longjmp_probes,
3414 i, probe);
3415 ++i)
3416 {
3417 struct breakpoint *b;
3418
729662a5
TT
3419 b = create_internal_breakpoint (gdbarch,
3420 get_probe_address (probe,
3421 objfile),
28106bc2
SDJ
3422 bp_longjmp_master,
3423 &internal_breakpoint_ops);
d28cd78a 3424 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3425 b->enable_state = bp_disabled;
3426 }
3427
3428 continue;
3429 }
3430
0569175e
TSD
3431 if (!gdbarch_get_longjmp_target_p (gdbarch))
3432 continue;
3433
17450429 3434 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3435 {
3436 struct breakpoint *b;
af02033e 3437 const char *func_name;
17450429 3438 CORE_ADDR addr;
67994074 3439 struct explicit_location explicit_loc;
6c95b8df 3440
3b7344d5 3441 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3442 continue;
0fd8e87f 3443
17450429 3444 func_name = longjmp_names[i];
3b7344d5 3445 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3446 {
3b7344d5 3447 struct bound_minimal_symbol m;
17450429
PP
3448
3449 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3450 if (m.minsym == NULL)
17450429
PP
3451 {
3452 /* Prevent future lookups in this objfile. */
3b7344d5 3453 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3454 continue;
3455 }
3456 bp_objfile_data->longjmp_msym[i] = m;
3457 }
3458
77e371c0 3459 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3460 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3461 &internal_breakpoint_ops);
67994074
KS
3462 initialize_explicit_location (&explicit_loc);
3463 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3464 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3465 b->enable_state = bp_disabled;
3466 }
0fd8e87f 3467 }
af02033e 3468 }
0fd8e87f
UW
3469}
3470
af02033e 3471/* Create a master std::terminate breakpoint. */
aa7d318d 3472static void
af02033e 3473create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3474{
3475 struct program_space *pspace;
af02033e 3476 const char *const func_name = "std::terminate()";
aa7d318d 3477
5ed8105e 3478 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3479
3480 ALL_PSPACES (pspace)
17450429
PP
3481 {
3482 struct objfile *objfile;
3483 CORE_ADDR addr;
3484
3485 set_current_program_space (pspace);
3486
aa7d318d
TT
3487 ALL_OBJFILES (objfile)
3488 {
3489 struct breakpoint *b;
17450429 3490 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3491 struct explicit_location explicit_loc;
aa7d318d 3492
17450429 3493 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3494
3b7344d5 3495 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3496 continue;
3497
3b7344d5 3498 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3499 {
3b7344d5 3500 struct bound_minimal_symbol m;
17450429
PP
3501
3502 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3503 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3504 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3505 {
3506 /* Prevent future lookups in this objfile. */
3b7344d5 3507 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3508 continue;
3509 }
3510 bp_objfile_data->terminate_msym = m;
3511 }
aa7d318d 3512
77e371c0 3513 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3514 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3515 bp_std_terminate_master,
3516 &internal_breakpoint_ops);
67994074
KS
3517 initialize_explicit_location (&explicit_loc);
3518 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3519 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3520 b->enable_state = bp_disabled;
3521 }
17450429 3522 }
aa7d318d
TT
3523}
3524
186c406b
TT
3525/* Install a master breakpoint on the unwinder's debug hook. */
3526
70221824 3527static void
186c406b
TT
3528create_exception_master_breakpoint (void)
3529{
3530 struct objfile *objfile;
17450429 3531 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3532
3533 ALL_OBJFILES (objfile)
3534 {
17450429
PP
3535 struct breakpoint *b;
3536 struct gdbarch *gdbarch;
3537 struct breakpoint_objfile_data *bp_objfile_data;
3538 CORE_ADDR addr;
67994074 3539 struct explicit_location explicit_loc;
17450429
PP
3540
3541 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3542
28106bc2
SDJ
3543 /* We prefer the SystemTap probe point if it exists. */
3544 if (!bp_objfile_data->exception_searched)
3545 {
25f9533e
SDJ
3546 VEC (probe_p) *ret;
3547
3548 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3549
3550 if (ret != NULL)
3551 {
3552 /* We are only interested in checking one element. */
3553 struct probe *p = VEC_index (probe_p, ret, 0);
3554
3555 if (!can_evaluate_probe_arguments (p))
3556 {
3557 /* We cannot use the probe interface here, because it does
3558 not know how to evaluate arguments. */
3559 VEC_free (probe_p, ret);
3560 ret = NULL;
3561 }
3562 }
3563 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3564 bp_objfile_data->exception_searched = 1;
3565 }
3566
3567 if (bp_objfile_data->exception_probes != NULL)
3568 {
3569 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3570 int i;
3571 struct probe *probe;
3572
3573 for (i = 0;
3574 VEC_iterate (probe_p,
3575 bp_objfile_data->exception_probes,
3576 i, probe);
3577 ++i)
3578 {
3579 struct breakpoint *b;
3580
729662a5
TT
3581 b = create_internal_breakpoint (gdbarch,
3582 get_probe_address (probe,
3583 objfile),
28106bc2
SDJ
3584 bp_exception_master,
3585 &internal_breakpoint_ops);
d28cd78a 3586 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3587 b->enable_state = bp_disabled;
3588 }
3589
3590 continue;
3591 }
3592
3593 /* Otherwise, try the hook function. */
3594
3b7344d5 3595 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3596 continue;
3597
3598 gdbarch = get_objfile_arch (objfile);
186c406b 3599
3b7344d5 3600 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3601 {
3b7344d5 3602 struct bound_minimal_symbol debug_hook;
186c406b 3603
17450429 3604 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3605 if (debug_hook.minsym == NULL)
17450429 3606 {
3b7344d5 3607 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3608 continue;
3609 }
3610
3611 bp_objfile_data->exception_msym = debug_hook;
186c406b 3612 }
17450429 3613
77e371c0 3614 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3615 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3616 &current_target);
06edf0c0
PA
3617 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3618 &internal_breakpoint_ops);
67994074
KS
3619 initialize_explicit_location (&explicit_loc);
3620 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3621 b->location = new_explicit_location (&explicit_loc);
17450429 3622 b->enable_state = bp_disabled;
186c406b 3623 }
186c406b
TT
3624}
3625
9ef9e6a6
KS
3626/* Does B have a location spec? */
3627
3628static int
3629breakpoint_event_location_empty_p (const struct breakpoint *b)
3630{
d28cd78a 3631 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3632}
3633
c906108c 3634void
fba45db2 3635update_breakpoints_after_exec (void)
c906108c 3636{
35df4500 3637 struct breakpoint *b, *b_tmp;
876fa593 3638 struct bp_location *bploc, **bplocp_tmp;
c906108c 3639
25b22b0a
PA
3640 /* We're about to delete breakpoints from GDB's lists. If the
3641 INSERTED flag is true, GDB will try to lift the breakpoints by
3642 writing the breakpoints' "shadow contents" back into memory. The
3643 "shadow contents" are NOT valid after an exec, so GDB should not
3644 do that. Instead, the target is responsible from marking
3645 breakpoints out as soon as it detects an exec. We don't do that
3646 here instead, because there may be other attempts to delete
3647 breakpoints after detecting an exec and before reaching here. */
876fa593 3648 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3649 if (bploc->pspace == current_program_space)
3650 gdb_assert (!bploc->inserted);
c906108c 3651
35df4500 3652 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3653 {
6c95b8df
PA
3654 if (b->pspace != current_program_space)
3655 continue;
3656
4a64f543 3657 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3658 if (b->type == bp_shlib_event)
3659 {
3660 delete_breakpoint (b);
3661 continue;
3662 }
c906108c 3663
4a64f543 3664 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3665 if (b->type == bp_jit_event)
3666 {
3667 delete_breakpoint (b);
3668 continue;
3669 }
3670
1900040c 3671 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3672 as must overlay event and longjmp master breakpoints. */
3673 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3674 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3675 || b->type == bp_exception_master)
c4093a6a
JM
3676 {
3677 delete_breakpoint (b);
3678 continue;
3679 }
3680
4a64f543 3681 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3682 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3683 {
3684 delete_breakpoint (b);
3685 continue;
3686 }
3687
7c16b83e
PA
3688 /* Just like single-step breakpoints. */
3689 if (b->type == bp_single_step)
3690 {
3691 delete_breakpoint (b);
3692 continue;
3693 }
3694
611c83ae
PA
3695 /* Longjmp and longjmp-resume breakpoints are also meaningless
3696 after an exec. */
186c406b 3697 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3698 || b->type == bp_longjmp_call_dummy
186c406b 3699 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3700 {
3701 delete_breakpoint (b);
3702 continue;
3703 }
3704
ce78b96d
JB
3705 if (b->type == bp_catchpoint)
3706 {
3707 /* For now, none of the bp_catchpoint breakpoints need to
3708 do anything at this point. In the future, if some of
3709 the catchpoints need to something, we will need to add
3710 a new method, and call this method from here. */
3711 continue;
3712 }
3713
c5aa993b
JM
3714 /* bp_finish is a special case. The only way we ought to be able
3715 to see one of these when an exec() has happened, is if the user
3716 caught a vfork, and then said "finish". Ordinarily a finish just
3717 carries them to the call-site of the current callee, by setting
3718 a temporary bp there and resuming. But in this case, the finish
3719 will carry them entirely through the vfork & exec.
3720
3721 We don't want to allow a bp_finish to remain inserted now. But
3722 we can't safely delete it, 'cause finish_command has a handle to
3723 the bp on a bpstat, and will later want to delete it. There's a
3724 chance (and I've seen it happen) that if we delete the bp_finish
3725 here, that its storage will get reused by the time finish_command
3726 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3727 We really must allow finish_command to delete a bp_finish.
3728
e5dd4106 3729 In the absence of a general solution for the "how do we know
53a5351d
JM
3730 it's safe to delete something others may have handles to?"
3731 problem, what we'll do here is just uninsert the bp_finish, and
3732 let finish_command delete it.
3733
3734 (We know the bp_finish is "doomed" in the sense that it's
3735 momentary, and will be deleted as soon as finish_command sees
3736 the inferior stopped. So it doesn't matter that the bp's
3737 address is probably bogus in the new a.out, unlike e.g., the
3738 solib breakpoints.) */
c5aa993b 3739
c5aa993b
JM
3740 if (b->type == bp_finish)
3741 {
3742 continue;
3743 }
3744
3745 /* Without a symbolic address, we have little hope of the
3746 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3747 a.out. */
9ef9e6a6 3748 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3749 {
3750 delete_breakpoint (b);
3751 continue;
3752 }
c5aa993b 3753 }
c906108c
SS
3754}
3755
3756int
d80ee84f 3757detach_breakpoints (ptid_t ptid)
c906108c 3758{
35df4500 3759 struct bp_location *bl, **blp_tmp;
3a1bae8e 3760 int val = 0;
2989a365 3761 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3762 struct inferior *inf = current_inferior ();
c5aa993b 3763
dfd4cc63 3764 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3765 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3766
6c95b8df 3767 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3768 inferior_ptid = ptid;
35df4500 3769 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3770 {
35df4500 3771 if (bl->pspace != inf->pspace)
6c95b8df
PA
3772 continue;
3773
bd9673a4
PW
3774 /* This function must physically remove breakpoints locations
3775 from the specified ptid, without modifying the breakpoint
3776 package's state. Locations of type bp_loc_other are only
3777 maintained at GDB side. So, there is no need to remove
3778 these bp_loc_other locations. Moreover, removing these
3779 would modify the breakpoint package's state. */
3780 if (bl->loc_type == bp_loc_other)
3781 continue;
3782
35df4500 3783 if (bl->inserted)
b2b6a7da 3784 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3785 }
d03285ec 3786
3a1bae8e 3787 return val;
c906108c
SS
3788}
3789
35df4500 3790/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3791 Note that this is used to detach breakpoints from a child fork.
3792 When we get here, the child isn't in the inferior list, and neither
3793 do we have objects to represent its address space --- we should
35df4500 3794 *not* look at bl->pspace->aspace here. */
6c95b8df 3795
c906108c 3796static int
b2b6a7da 3797remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3798{
3799 int val;
c5aa993b 3800
35df4500
TJB
3801 /* BL is never in moribund_locations by our callers. */
3802 gdb_assert (bl->owner != NULL);
2bdf28a0 3803
74960c60
VP
3804 /* The type of none suggests that owner is actually deleted.
3805 This should not ever happen. */
35df4500 3806 gdb_assert (bl->owner->type != bp_none);
0bde7532 3807
35df4500
TJB
3808 if (bl->loc_type == bp_loc_software_breakpoint
3809 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3810 {
c02f5703
MS
3811 /* "Normal" instruction breakpoint: either the standard
3812 trap-instruction bp (bp_breakpoint), or a
3813 bp_hardware_breakpoint. */
3814
3815 /* First check to see if we have to handle an overlay. */
3816 if (overlay_debugging == ovly_off
35df4500
TJB
3817 || bl->section == NULL
3818 || !(section_is_overlay (bl->section)))
c02f5703
MS
3819 {
3820 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3821
3822 /* If we're trying to uninsert a memory breakpoint that we
3823 know is set in a dynamic object that is marked
3824 shlib_disabled, then either the dynamic object was
3825 removed with "remove-symbol-file" or with
3826 "nosharedlibrary". In the former case, we don't know
3827 whether another dynamic object might have loaded over the
3828 breakpoint's address -- the user might well let us know
3829 about it next with add-symbol-file (the whole point of
d03de421 3830 add-symbol-file is letting the user manually maintain a
08351840
PA
3831 list of dynamically loaded objects). If we have the
3832 breakpoint's shadow memory, that is, this is a software
3833 breakpoint managed by GDB, check whether the breakpoint
3834 is still inserted in memory, to avoid overwriting wrong
3835 code with stale saved shadow contents. Note that HW
3836 breakpoints don't have shadow memory, as they're
3837 implemented using a mechanism that is not dependent on
3838 being able to modify the target's memory, and as such
3839 they should always be removed. */
3840 if (bl->shlib_disabled
3841 && bl->target_info.shadow_len != 0
3842 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3843 val = 0;
3844 else
73971819 3845 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3846 }
c906108c
SS
3847 else
3848 {
4a64f543 3849 /* This breakpoint is in an overlay section.
c02f5703
MS
3850 Did we set a breakpoint at the LMA? */
3851 if (!overlay_events_enabled)
3852 {
3853 /* Yes -- overlay event support is not active, so we
3854 should have set a breakpoint at the LMA. Remove it.
3855 */
c02f5703
MS
3856 /* Ignore any failures: if the LMA is in ROM, we will
3857 have already warned when we failed to insert it. */
35df4500
TJB
3858 if (bl->loc_type == bp_loc_hardware_breakpoint)
3859 target_remove_hw_breakpoint (bl->gdbarch,
3860 &bl->overlay_target_info);
c02f5703 3861 else
35df4500 3862 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3863 &bl->overlay_target_info,
3864 reason);
c02f5703
MS
3865 }
3866 /* Did we set a breakpoint at the VMA?
3867 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3868 if (bl->inserted)
c906108c 3869 {
c02f5703
MS
3870 /* Yes -- remove it. Previously we did not bother to
3871 remove the breakpoint if the section had been
3872 unmapped, but let's not rely on that being safe. We
3873 don't know what the overlay manager might do. */
aa67235e
UW
3874
3875 /* However, we should remove *software* breakpoints only
3876 if the section is still mapped, or else we overwrite
3877 wrong code with the saved shadow contents. */
348d480f
PA
3878 if (bl->loc_type == bp_loc_hardware_breakpoint
3879 || section_is_mapped (bl->section))
73971819 3880 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3881 else
3882 val = 0;
c906108c 3883 }
c02f5703
MS
3884 else
3885 {
3886 /* No -- not inserted, so no need to remove. No error. */
3887 val = 0;
3888 }
c906108c 3889 }
879d1e6b 3890
08351840
PA
3891 /* In some cases, we might not be able to remove a breakpoint in
3892 a shared library that has already been removed, but we have
3893 not yet processed the shlib unload event. Similarly for an
3894 unloaded add-symbol-file object - the user might not yet have
3895 had the chance to remove-symbol-file it. shlib_disabled will
3896 be set if the library/object has already been removed, but
3897 the breakpoint hasn't been uninserted yet, e.g., after
3898 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3899 always-inserted mode. */
076855f9 3900 if (val
08351840
PA
3901 && (bl->loc_type == bp_loc_software_breakpoint
3902 && (bl->shlib_disabled
3903 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3904 || shared_objfile_contains_address_p (bl->pspace,
3905 bl->address))))
879d1e6b
UW
3906 val = 0;
3907
c906108c
SS
3908 if (val)
3909 return val;
b2b6a7da 3910 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3911 }
35df4500 3912 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3913 {
77b06cd7
TJB
3914 gdb_assert (bl->owner->ops != NULL
3915 && bl->owner->ops->remove_location != NULL);
3916
b2b6a7da 3917 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3918 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3919
c906108c 3920 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3921 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3922 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3923 bl->owner->number);
c906108c 3924 }
35df4500
TJB
3925 else if (bl->owner->type == bp_catchpoint
3926 && breakpoint_enabled (bl->owner)
3927 && !bl->duplicate)
ce78b96d 3928 {
77b06cd7
TJB
3929 gdb_assert (bl->owner->ops != NULL
3930 && bl->owner->ops->remove_location != NULL);
ce78b96d 3931
73971819 3932 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3933 if (val)
3934 return val;
77b06cd7 3935
b2b6a7da 3936 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3937 }
c906108c
SS
3938
3939 return 0;
3940}
3941
6c95b8df 3942static int
834c0d03 3943remove_breakpoint (struct bp_location *bl)
6c95b8df 3944{
35df4500
TJB
3945 /* BL is never in moribund_locations by our callers. */
3946 gdb_assert (bl->owner != NULL);
2bdf28a0 3947
6c95b8df
PA
3948 /* The type of none suggests that owner is actually deleted.
3949 This should not ever happen. */
35df4500 3950 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3951
5ed8105e 3952 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3953
35df4500 3954 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3955
5ed8105e 3956 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3957}
3958
c906108c
SS
3959/* Clear the "inserted" flag in all breakpoints. */
3960
25b22b0a 3961void
fba45db2 3962mark_breakpoints_out (void)
c906108c 3963{
35df4500 3964 struct bp_location *bl, **blp_tmp;
c906108c 3965
35df4500 3966 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3967 if (bl->pspace == current_program_space)
35df4500 3968 bl->inserted = 0;
c906108c
SS
3969}
3970
53a5351d
JM
3971/* Clear the "inserted" flag in all breakpoints and delete any
3972 breakpoints which should go away between runs of the program.
c906108c
SS
3973
3974 Plus other such housekeeping that has to be done for breakpoints
3975 between runs.
3976
53a5351d
JM
3977 Note: this function gets called at the end of a run (by
3978 generic_mourn_inferior) and when a run begins (by
4a64f543 3979 init_wait_for_inferior). */
c906108c
SS
3980
3981
3982
3983void
fba45db2 3984breakpoint_init_inferior (enum inf_context context)
c906108c 3985{
35df4500 3986 struct breakpoint *b, *b_tmp;
870f88f7 3987 struct bp_location *bl;
1c5cfe86 3988 int ix;
6c95b8df 3989 struct program_space *pspace = current_program_space;
c906108c 3990
50c71eaf
PA
3991 /* If breakpoint locations are shared across processes, then there's
3992 nothing to do. */
f5656ead 3993 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3994 return;
3995
1a853c52 3996 mark_breakpoints_out ();
075f6582 3997
35df4500 3998 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3999 {
6c95b8df
PA
4000 if (b->loc && b->loc->pspace != pspace)
4001 continue;
4002
c5aa993b
JM
4003 switch (b->type)
4004 {
4005 case bp_call_dummy:
e2e4d78b 4006 case bp_longjmp_call_dummy:
c906108c 4007
c5aa993b 4008 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4009 cause problems when the inferior is rerun, so we better get
4010 rid of it. */
4011
4012 case bp_watchpoint_scope:
4013
4014 /* Also get rid of scope breakpoints. */
4015
4016 case bp_shlib_event:
4017
4018 /* Also remove solib event breakpoints. Their addresses may
4019 have changed since the last time we ran the program.
4020 Actually we may now be debugging against different target;
4021 and so the solib backend that installed this breakpoint may
4022 not be used in by the target. E.g.,
4023
4024 (gdb) file prog-linux
4025 (gdb) run # native linux target
4026 ...
4027 (gdb) kill
4028 (gdb) file prog-win.exe
4029 (gdb) tar rem :9999 # remote Windows gdbserver.
4030 */
c906108c 4031
f59f708a
PA
4032 case bp_step_resume:
4033
4034 /* Also remove step-resume breakpoints. */
4035
7c16b83e
PA
4036 case bp_single_step:
4037
4038 /* Also remove single-step breakpoints. */
4039
c5aa993b
JM
4040 delete_breakpoint (b);
4041 break;
c906108c 4042
c5aa993b
JM
4043 case bp_watchpoint:
4044 case bp_hardware_watchpoint:
4045 case bp_read_watchpoint:
4046 case bp_access_watchpoint:
3a5c3e22
PA
4047 {
4048 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4049
3a5c3e22
PA
4050 /* Likewise for watchpoints on local expressions. */
4051 if (w->exp_valid_block != NULL)
4052 delete_breakpoint (b);
63000888 4053 else
3a5c3e22 4054 {
63000888
PA
4055 /* Get rid of existing locations, which are no longer
4056 valid. New ones will be created in
4057 update_watchpoint, when the inferior is restarted.
4058 The next update_global_location_list call will
4059 garbage collect them. */
4060 b->loc = NULL;
4061
4062 if (context == inf_starting)
4063 {
4064 /* Reset val field to force reread of starting value in
4065 insert_breakpoints. */
4066 if (w->val)
4067 value_free (w->val);
4068 w->val = NULL;
4069 w->val_valid = 0;
4070 }
4071 }
3a5c3e22 4072 }
c5aa993b
JM
4073 break;
4074 default:
c5aa993b
JM
4075 break;
4076 }
4077 }
1c5cfe86
PA
4078
4079 /* Get rid of the moribund locations. */
35df4500
TJB
4080 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4081 decref_bp_location (&bl);
1c5cfe86 4082 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4083}
4084
6c95b8df
PA
4085/* These functions concern about actual breakpoints inserted in the
4086 target --- to e.g. check if we need to do decr_pc adjustment or if
4087 we need to hop over the bkpt --- so we check for address space
4088 match, not program space. */
4089
c2c6d25f
JM
4090/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4091 exists at PC. It returns ordinary_breakpoint_here if it's an
4092 ordinary breakpoint, or permanent_breakpoint_here if it's a
4093 permanent breakpoint.
4094 - When continuing from a location with an ordinary breakpoint, we
4095 actually single step once before calling insert_breakpoints.
e5dd4106 4096 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4097 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4098 the target, to advance the PC past the breakpoint. */
c906108c 4099
c2c6d25f 4100enum breakpoint_here
6c95b8df 4101breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4102{
35df4500 4103 struct bp_location *bl, **blp_tmp;
c2c6d25f 4104 int any_breakpoint_here = 0;
c906108c 4105
35df4500 4106 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4107 {
35df4500
TJB
4108 if (bl->loc_type != bp_loc_software_breakpoint
4109 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4110 continue;
4111
f1310107 4112 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4113 if ((breakpoint_enabled (bl->owner)
1a853c52 4114 || bl->permanent)
f1310107 4115 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4116 {
4117 if (overlay_debugging
35df4500
TJB
4118 && section_is_overlay (bl->section)
4119 && !section_is_mapped (bl->section))
075f6582 4120 continue; /* unmapped overlay -- can't be a match */
1a853c52 4121 else if (bl->permanent)
075f6582
DJ
4122 return permanent_breakpoint_here;
4123 else
4124 any_breakpoint_here = 1;
4125 }
4126 }
c906108c 4127
f486487f 4128 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4129}
4130
d35ae833
PA
4131/* See breakpoint.h. */
4132
4133int
4134breakpoint_in_range_p (struct address_space *aspace,
4135 CORE_ADDR addr, ULONGEST len)
4136{
4137 struct bp_location *bl, **blp_tmp;
4138
4139 ALL_BP_LOCATIONS (bl, blp_tmp)
4140 {
4141 if (bl->loc_type != bp_loc_software_breakpoint
4142 && bl->loc_type != bp_loc_hardware_breakpoint)
4143 continue;
4144
4145 if ((breakpoint_enabled (bl->owner)
4146 || bl->permanent)
4147 && breakpoint_location_address_range_overlap (bl, aspace,
4148 addr, len))
4149 {
4150 if (overlay_debugging
4151 && section_is_overlay (bl->section)
4152 && !section_is_mapped (bl->section))
4153 {
4154 /* Unmapped overlay -- can't be a match. */
4155 continue;
4156 }
4157
4158 return 1;
4159 }
4160 }
4161
4162 return 0;
4163}
4164
1c5cfe86
PA
4165/* Return true if there's a moribund breakpoint at PC. */
4166
4167int
6c95b8df 4168moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4169{
4170 struct bp_location *loc;
4171 int ix;
4172
4173 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4174 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4175 return 1;
4176
4177 return 0;
4178}
c2c6d25f 4179
f7ce857f
PA
4180/* Returns non-zero iff BL is inserted at PC, in address space
4181 ASPACE. */
4182
4183static int
4184bp_location_inserted_here_p (struct bp_location *bl,
4185 struct address_space *aspace, CORE_ADDR pc)
4186{
4187 if (bl->inserted
4188 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4189 aspace, pc))
4190 {
4191 if (overlay_debugging
4192 && section_is_overlay (bl->section)
4193 && !section_is_mapped (bl->section))
4194 return 0; /* unmapped overlay -- can't be a match */
4195 else
4196 return 1;
4197 }
4198 return 0;
4199}
4200
a1fd2fa5 4201/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4202
4203int
a1fd2fa5 4204breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4205{
f7ce857f 4206 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4207
f7ce857f 4208 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4209 {
f7ce857f
PA
4210 struct bp_location *bl = *blp;
4211
35df4500
TJB
4212 if (bl->loc_type != bp_loc_software_breakpoint
4213 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4214 continue;
4215
f7ce857f
PA
4216 if (bp_location_inserted_here_p (bl, aspace, pc))
4217 return 1;
c5aa993b 4218 }
c36b740a
VP
4219 return 0;
4220}
4221
a1fd2fa5
PA
4222/* This function returns non-zero iff there is a software breakpoint
4223 inserted at PC. */
c36b740a
VP
4224
4225int
a1fd2fa5
PA
4226software_breakpoint_inserted_here_p (struct address_space *aspace,
4227 CORE_ADDR pc)
4fa8626c 4228{
f7ce857f 4229 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4230
f7ce857f 4231 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4232 {
f7ce857f
PA
4233 struct bp_location *bl = *blp;
4234
35df4500 4235 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4236 continue;
4237
f7ce857f
PA
4238 if (bp_location_inserted_here_p (bl, aspace, pc))
4239 return 1;
4fa8626c
DJ
4240 }
4241
4242 return 0;
9c02b525
PA
4243}
4244
4245/* See breakpoint.h. */
4246
4247int
4248hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4249 CORE_ADDR pc)
4250{
4251 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4252
4253 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4254 {
4255 struct bp_location *bl = *blp;
4256
4257 if (bl->loc_type != bp_loc_hardware_breakpoint)
4258 continue;
4259
4260 if (bp_location_inserted_here_p (bl, aspace, pc))
4261 return 1;
4262 }
4263
4264 return 0;
4fa8626c
DJ
4265}
4266
9093389c
PA
4267int
4268hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4269 CORE_ADDR addr, ULONGEST len)
4270{
4271 struct breakpoint *bpt;
4272
4273 ALL_BREAKPOINTS (bpt)
4274 {
4275 struct bp_location *loc;
4276
4277 if (bpt->type != bp_hardware_watchpoint
4278 && bpt->type != bp_access_watchpoint)
4279 continue;
4280
4281 if (!breakpoint_enabled (bpt))
4282 continue;
4283
4284 for (loc = bpt->loc; loc; loc = loc->next)
4285 if (loc->pspace->aspace == aspace && loc->inserted)
4286 {
4287 CORE_ADDR l, h;
4288
4289 /* Check for intersection. */
768adc05
PA
4290 l = std::max<CORE_ADDR> (loc->address, addr);
4291 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4292 if (l < h)
4293 return 1;
4294 }
4295 }
4296 return 0;
4297}
c906108c 4298\f
c5aa993b 4299
c906108c
SS
4300/* bpstat stuff. External routines' interfaces are documented
4301 in breakpoint.h. */
4302
4303int
c326b90e 4304is_catchpoint (struct breakpoint *ep)
c906108c 4305{
533be4dd 4306 return (ep->type == bp_catchpoint);
c906108c
SS
4307}
4308
f431efe5
PA
4309/* Frees any storage that is part of a bpstat. Does not walk the
4310 'next' chain. */
4311
04afa70c 4312bpstats::~bpstats ()
198757a8 4313{
04afa70c
TT
4314 if (old_val != NULL)
4315 value_free (old_val);
04afa70c
TT
4316 if (bp_location_at != NULL)
4317 decref_bp_location (&bp_location_at);
198757a8
VP
4318}
4319
c906108c
SS
4320/* Clear a bpstat so that it says we are not at any breakpoint.
4321 Also free any storage that is part of a bpstat. */
4322
4323void
fba45db2 4324bpstat_clear (bpstat *bsp)
c906108c
SS
4325{
4326 bpstat p;
4327 bpstat q;
4328
4329 if (bsp == 0)
4330 return;
4331 p = *bsp;
4332 while (p != NULL)
4333 {
4334 q = p->next;
04afa70c 4335 delete p;
c906108c
SS
4336 p = q;
4337 }
4338 *bsp = NULL;
4339}
4340
04afa70c
TT
4341bpstats::bpstats (const bpstats &other)
4342 : next (NULL),
4343 bp_location_at (other.bp_location_at),
4344 breakpoint_at (other.breakpoint_at),
4345 commands (other.commands),
4346 old_val (other.old_val),
4347 print (other.print),
4348 stop (other.stop),
4349 print_it (other.print_it)
4350{
4351 if (old_val != NULL)
4352 {
4353 old_val = value_copy (old_val);
4354 release_value (old_val);
4355 }
4356 incref_bp_location (bp_location_at);
04afa70c
TT
4357}
4358
c906108c
SS
4359/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4360 is part of the bpstat is copied as well. */
4361
4362bpstat
fba45db2 4363bpstat_copy (bpstat bs)
c906108c
SS
4364{
4365 bpstat p = NULL;
4366 bpstat tmp;
4367 bpstat retval = NULL;
4368
4369 if (bs == NULL)
4370 return bs;
4371
4372 for (; bs != NULL; bs = bs->next)
4373 {
04afa70c 4374 tmp = new bpstats (*bs);
31cc81e9 4375
c906108c
SS
4376 if (p == NULL)
4377 /* This is the first thing in the chain. */
4378 retval = tmp;
4379 else
4380 p->next = tmp;
4381 p = tmp;
4382 }
4383 p->next = NULL;
4384 return retval;
4385}
4386
4a64f543 4387/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4388
4389bpstat
fba45db2 4390bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4391{
c5aa993b
JM
4392 if (bsp == NULL)
4393 return NULL;
c906108c 4394
c5aa993b
JM
4395 for (; bsp != NULL; bsp = bsp->next)
4396 {
f431efe5 4397 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4398 return bsp;
4399 }
c906108c
SS
4400 return NULL;
4401}
4402
ab04a2af
TT
4403/* See breakpoint.h. */
4404
47591c29 4405int
427cd150 4406bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4407{
ab04a2af
TT
4408 for (; bsp != NULL; bsp = bsp->next)
4409 {
427cd150
TT
4410 if (bsp->breakpoint_at == NULL)
4411 {
4412 /* A moribund location can never explain a signal other than
4413 GDB_SIGNAL_TRAP. */
4414 if (sig == GDB_SIGNAL_TRAP)
47591c29 4415 return 1;
427cd150
TT
4416 }
4417 else
47591c29
PA
4418 {
4419 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4420 sig))
4421 return 1;
4422 }
ab04a2af
TT
4423 }
4424
47591c29 4425 return 0;
ab04a2af
TT
4426}
4427
4a64f543
MS
4428/* Put in *NUM the breakpoint number of the first breakpoint we are
4429 stopped at. *BSP upon return is a bpstat which points to the
4430 remaining breakpoints stopped at (but which is not guaranteed to be
4431 good for anything but further calls to bpstat_num).
4432
8671a17b
PA
4433 Return 0 if passed a bpstat which does not indicate any breakpoints.
4434 Return -1 if stopped at a breakpoint that has been deleted since
4435 we set it.
4436 Return 1 otherwise. */
c906108c
SS
4437
4438int
8671a17b 4439bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4440{
4441 struct breakpoint *b;
4442
4443 if ((*bsp) == NULL)
4444 return 0; /* No more breakpoint values */
8671a17b 4445
4a64f543
MS
4446 /* We assume we'll never have several bpstats that correspond to a
4447 single breakpoint -- otherwise, this function might return the
4448 same number more than once and this will look ugly. */
f431efe5 4449 b = (*bsp)->breakpoint_at;
8671a17b
PA
4450 *bsp = (*bsp)->next;
4451 if (b == NULL)
4452 return -1; /* breakpoint that's been deleted since */
4453
4454 *num = b->number; /* We have its number */
4455 return 1;
c906108c
SS
4456}
4457
e93ca019 4458/* See breakpoint.h. */
c906108c
SS
4459
4460void
e93ca019 4461bpstat_clear_actions (void)
c906108c 4462{
e93ca019
JK
4463 struct thread_info *tp;
4464 bpstat bs;
4465
4466 if (ptid_equal (inferior_ptid, null_ptid))
4467 return;
4468
4469 tp = find_thread_ptid (inferior_ptid);
4470 if (tp == NULL)
4471 return;
4472
4473 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4474 {
d1b0a7bf 4475 bs->commands = NULL;
abf85f46 4476
c906108c
SS
4477 if (bs->old_val != NULL)
4478 {
4479 value_free (bs->old_val);
4480 bs->old_val = NULL;
4481 }
4482 }
4483}
4484
f3b1572e
PA
4485/* Called when a command is about to proceed the inferior. */
4486
4487static void
4488breakpoint_about_to_proceed (void)
4489{
4490 if (!ptid_equal (inferior_ptid, null_ptid))
4491 {
4492 struct thread_info *tp = inferior_thread ();
4493
4494 /* Allow inferior function calls in breakpoint commands to not
4495 interrupt the command list. When the call finishes
4496 successfully, the inferior will be standing at the same
4497 breakpoint as if nothing happened. */
16c381f0 4498 if (tp->control.in_infcall)
f3b1572e
PA
4499 return;
4500 }
4501
4502 breakpoint_proceeded = 1;
4503}
4504
4a64f543
MS
4505/* Stub for cleaning up our state if we error-out of a breakpoint
4506 command. */
c906108c 4507static void
4efb68b1 4508cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4509{
4510 executing_breakpoint_commands = 0;
4511}
4512
abf85f46
JK
4513/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4514 or its equivalent. */
4515
4516static int
4517command_line_is_silent (struct command_line *cmd)
4518{
4f45d445 4519 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4520}
4521
4a64f543
MS
4522/* Execute all the commands associated with all the breakpoints at
4523 this location. Any of these commands could cause the process to
4524 proceed beyond this point, etc. We look out for such changes by
4525 checking the global "breakpoint_proceeded" after each command.
c906108c 4526
347bddb7
PA
4527 Returns true if a breakpoint command resumed the inferior. In that
4528 case, it is the caller's responsibility to recall it again with the
4529 bpstat of the current thread. */
4530
4531static int
4532bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4533{
4534 bpstat bs;
4535 struct cleanup *old_chain;
347bddb7 4536 int again = 0;
c906108c
SS
4537
4538 /* Avoid endless recursion if a `source' command is contained
4539 in bs->commands. */
4540 if (executing_breakpoint_commands)
347bddb7 4541 return 0;
c906108c
SS
4542
4543 executing_breakpoint_commands = 1;
4544 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4545
1ac32117 4546 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4547
4a64f543 4548 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4549 bs = *bsp;
4550
4551 breakpoint_proceeded = 0;
4552 for (; bs != NULL; bs = bs->next)
4553 {
d1b0a7bf 4554 struct command_line *cmd = NULL;
6c50ab1c
JB
4555
4556 /* Take ownership of the BSP's command tree, if it has one.
4557
4558 The command tree could legitimately contain commands like
4559 'step' and 'next', which call clear_proceed_status, which
4560 frees stop_bpstat's command tree. To make sure this doesn't
4561 free the tree we're executing out from under us, we need to
4562 take ownership of the tree ourselves. Since a given bpstat's
4563 commands are only executed once, we don't need to copy it; we
4564 can clear the pointer in the bpstat, and make sure we free
4565 the tree when we're done. */
d1b0a7bf 4566 counted_command_line ccmd = bs->commands;
9add0f1b 4567 bs->commands = NULL;
d1b0a7bf
TT
4568 if (ccmd != NULL)
4569 cmd = ccmd.get ();
abf85f46
JK
4570 if (command_line_is_silent (cmd))
4571 {
4572 /* The action has been already done by bpstat_stop_status. */
4573 cmd = cmd->next;
4574 }
6c50ab1c 4575
c906108c
SS
4576 while (cmd != NULL)
4577 {
4578 execute_control_command (cmd);
4579
4580 if (breakpoint_proceeded)
4581 break;
4582 else
4583 cmd = cmd->next;
4584 }
6c50ab1c 4585
c906108c 4586 if (breakpoint_proceeded)
32c1e744 4587 {
cb814510 4588 if (current_ui->async)
347bddb7
PA
4589 /* If we are in async mode, then the target might be still
4590 running, not stopped at any breakpoint, so nothing for
4591 us to do here -- just return to the event loop. */
4592 ;
32c1e744
VP
4593 else
4594 /* In sync mode, when execute_control_command returns
4595 we're already standing on the next breakpoint.
347bddb7
PA
4596 Breakpoint commands for that stop were not run, since
4597 execute_command does not run breakpoint commands --
4598 only command_line_handler does, but that one is not
4599 involved in execution of breakpoint commands. So, we
4600 can now execute breakpoint commands. It should be
4601 noted that making execute_command do bpstat actions is
4602 not an option -- in this case we'll have recursive
4603 invocation of bpstat for each breakpoint with a
4604 command, and can easily blow up GDB stack. Instead, we
4605 return true, which will trigger the caller to recall us
4606 with the new stop_bpstat. */
4607 again = 1;
4608 break;
32c1e744 4609 }
c906108c 4610 }
c2b8ed2c 4611 do_cleanups (old_chain);
347bddb7
PA
4612 return again;
4613}
4614
4615void
4616bpstat_do_actions (void)
4617{
353d1d73
JK
4618 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4619
347bddb7
PA
4620 /* Do any commands attached to breakpoint we are stopped at. */
4621 while (!ptid_equal (inferior_ptid, null_ptid)
4622 && target_has_execution
4623 && !is_exited (inferior_ptid)
4624 && !is_executing (inferior_ptid))
4625 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4626 and only return when it is stopped at the next breakpoint, we
4627 keep doing breakpoint actions until it returns false to
4628 indicate the inferior was not resumed. */
16c381f0 4629 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4630 break;
353d1d73
JK
4631
4632 discard_cleanups (cleanup_if_error);
c906108c
SS
4633}
4634
fa4727a6
DJ
4635/* Print out the (old or new) value associated with a watchpoint. */
4636
4637static void
4638watchpoint_value_print (struct value *val, struct ui_file *stream)
4639{
4640 if (val == NULL)
4641 fprintf_unfiltered (stream, _("<unreadable>"));
4642 else
79a45b7d
TT
4643 {
4644 struct value_print_options opts;
4645 get_user_print_options (&opts);
4646 value_print (val, stream, &opts);
4647 }
fa4727a6
DJ
4648}
4649
f303dbd6
PA
4650/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4651 debugging multiple threads. */
4652
4653void
4654maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4655{
112e8700 4656 if (uiout->is_mi_like_p ())
f303dbd6
PA
4657 return;
4658
112e8700 4659 uiout->text ("\n");
f303dbd6
PA
4660
4661 if (show_thread_that_caused_stop ())
4662 {
4663 const char *name;
4664 struct thread_info *thr = inferior_thread ();
4665
112e8700
SM
4666 uiout->text ("Thread ");
4667 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4668
4669 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4670 if (name != NULL)
4671 {
112e8700
SM
4672 uiout->text (" \"");
4673 uiout->field_fmt ("name", "%s", name);
4674 uiout->text ("\"");
f303dbd6
PA
4675 }
4676
112e8700 4677 uiout->text (" hit ");
f303dbd6
PA
4678 }
4679}
4680
e514a9d6 4681/* Generic routine for printing messages indicating why we
4a64f543 4682 stopped. The behavior of this function depends on the value
e514a9d6
JM
4683 'print_it' in the bpstat structure. Under some circumstances we
4684 may decide not to print anything here and delegate the task to
4a64f543 4685 normal_stop(). */
e514a9d6
JM
4686
4687static enum print_stop_action
4688print_bp_stop_message (bpstat bs)
4689{
4690 switch (bs->print_it)
4691 {
4692 case print_it_noop:
4a64f543 4693 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4694 return PRINT_UNKNOWN;
4695 break;
4696
4697 case print_it_done:
4698 /* We still want to print the frame, but we already printed the
4a64f543 4699 relevant messages. */
e514a9d6
JM
4700 return PRINT_SRC_AND_LOC;
4701 break;
4702
4703 case print_it_normal:
4f8d1dc6 4704 {
f431efe5
PA
4705 struct breakpoint *b = bs->breakpoint_at;
4706
1a6a67de
TJB
4707 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4708 which has since been deleted. */
4709 if (b == NULL)
4710 return PRINT_UNKNOWN;
4711
348d480f
PA
4712 /* Normal case. Call the breakpoint's print_it method. */
4713 return b->ops->print_it (bs);
4f8d1dc6 4714 }
348d480f 4715 break;
3086aeae 4716
e514a9d6 4717 default:
8e65ff28 4718 internal_error (__FILE__, __LINE__,
e2e0b3e5 4719 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4720 break;
c906108c 4721 }
c906108c
SS
4722}
4723
edcc5120
TT
4724/* A helper function that prints a shared library stopped event. */
4725
4726static void
4727print_solib_event (int is_catchpoint)
4728{
4729 int any_deleted
4730 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4731 int any_added
4732 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4733
4734 if (!is_catchpoint)
4735 {
4736 if (any_added || any_deleted)
112e8700 4737 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4738 else
112e8700
SM
4739 current_uiout->text (_("Stopped due to shared library event (no "
4740 "libraries added or removed)\n"));
edcc5120
TT
4741 }
4742
112e8700
SM
4743 if (current_uiout->is_mi_like_p ())
4744 current_uiout->field_string ("reason",
4745 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4746
4747 if (any_deleted)
4748 {
edcc5120
TT
4749 char *name;
4750 int ix;
4751
112e8700 4752 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4753 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4754 for (ix = 0;
4755 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4756 ix, name);
4757 ++ix)
4758 {
4759 if (ix > 0)
112e8700
SM
4760 current_uiout->text (" ");
4761 current_uiout->field_string ("library", name);
4762 current_uiout->text ("\n");
edcc5120 4763 }
edcc5120
TT
4764 }
4765
4766 if (any_added)
4767 {
4768 struct so_list *iter;
4769 int ix;
edcc5120 4770
112e8700 4771 current_uiout->text (_(" Inferior loaded "));
10f489e5 4772 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4773 for (ix = 0;
4774 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4775 ix, iter);
4776 ++ix)
4777 {
4778 if (ix > 0)
112e8700
SM
4779 current_uiout->text (" ");
4780 current_uiout->field_string ("library", iter->so_name);
4781 current_uiout->text ("\n");
edcc5120 4782 }
edcc5120
TT
4783 }
4784}
4785
e514a9d6
JM
4786/* Print a message indicating what happened. This is called from
4787 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4788 list - a list of the eventpoints that caused this stop. KIND is
4789 the target_waitkind for the stopping event. This
e514a9d6
JM
4790 routine calls the generic print routine for printing a message
4791 about reasons for stopping. This will print (for example) the
4792 "Breakpoint n," part of the output. The return value of this
4793 routine is one of:
c906108c 4794
4a64f543 4795 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4796 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4797 code to print the location. An example is
c5aa993b
JM
4798 "Breakpoint 1, " which should be followed by
4799 the location.
917317f4 4800 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4801 to also print the location part of the message.
4802 An example is the catch/throw messages, which
4a64f543 4803 don't require a location appended to the end.
917317f4 4804 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4805 further info to be printed. */
c906108c 4806
917317f4 4807enum print_stop_action
36dfb11c 4808bpstat_print (bpstat bs, int kind)
c906108c 4809{
f486487f 4810 enum print_stop_action val;
c5aa993b 4811
c906108c 4812 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4813 (Currently all watchpoints go on the bpstat whether hit or not.
4814 That probably could (should) be changed, provided care is taken
c906108c 4815 with respect to bpstat_explains_signal). */
e514a9d6
JM
4816 for (; bs; bs = bs->next)
4817 {
4818 val = print_bp_stop_message (bs);
4819 if (val == PRINT_SRC_ONLY
4820 || val == PRINT_SRC_AND_LOC
4821 || val == PRINT_NOTHING)
4822 return val;
4823 }
c906108c 4824
36dfb11c
TT
4825 /* If we had hit a shared library event breakpoint,
4826 print_bp_stop_message would print out this message. If we hit an
4827 OS-level shared library event, do the same thing. */
4828 if (kind == TARGET_WAITKIND_LOADED)
4829 {
edcc5120 4830 print_solib_event (0);
36dfb11c
TT
4831 return PRINT_NOTHING;
4832 }
4833
e514a9d6 4834 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4835 with and nothing was printed. */
917317f4 4836 return PRINT_UNKNOWN;
c906108c
SS
4837}
4838
bf469271 4839/* Evaluate the boolean expression EXP and return the result. */
c906108c 4840
bf469271
PA
4841static bool
4842breakpoint_cond_eval (expression *exp)
c906108c 4843{
278cd55f 4844 struct value *mark = value_mark ();
bf469271 4845 bool res = value_true (evaluate_expression (exp));
cc59ec59 4846
c906108c 4847 value_free_to_mark (mark);
bf469271 4848 return res;
c906108c
SS
4849}
4850
5760d0ab 4851/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4852
04afa70c
TT
4853bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4854 : next (NULL),
4855 bp_location_at (bl),
4856 breakpoint_at (bl->owner),
4857 commands (NULL),
4858 old_val (NULL),
4859 print (0),
4860 stop (0),
4861 print_it (print_it_normal)
c906108c 4862{
f431efe5 4863 incref_bp_location (bl);
04afa70c
TT
4864 **bs_link_pointer = this;
4865 *bs_link_pointer = &next;
4866}
4867
4868bpstats::bpstats ()
4869 : next (NULL),
4870 bp_location_at (NULL),
4871 breakpoint_at (NULL),
4872 commands (NULL),
4873 old_val (NULL),
4874 print (0),
4875 stop (0),
4876 print_it (print_it_normal)
4877{
c906108c
SS
4878}
4879\f
d983da9c
DJ
4880/* The target has stopped with waitstatus WS. Check if any hardware
4881 watchpoints have triggered, according to the target. */
4882
4883int
4884watchpoints_triggered (struct target_waitstatus *ws)
4885{
d92524f1 4886 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4887 CORE_ADDR addr;
4888 struct breakpoint *b;
4889
4890 if (!stopped_by_watchpoint)
4891 {
4892 /* We were not stopped by a watchpoint. Mark all watchpoints
4893 as not triggered. */
4894 ALL_BREAKPOINTS (b)
cc60f2e3 4895 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4896 {
4897 struct watchpoint *w = (struct watchpoint *) b;
4898
4899 w->watchpoint_triggered = watch_triggered_no;
4900 }
d983da9c
DJ
4901
4902 return 0;
4903 }
4904
4905 if (!target_stopped_data_address (&current_target, &addr))
4906 {
4907 /* We were stopped by a watchpoint, but we don't know where.
4908 Mark all watchpoints as unknown. */
4909 ALL_BREAKPOINTS (b)
cc60f2e3 4910 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4911 {
4912 struct watchpoint *w = (struct watchpoint *) b;
4913
4914 w->watchpoint_triggered = watch_triggered_unknown;
4915 }
d983da9c 4916
3c4797ba 4917 return 1;
d983da9c
DJ
4918 }
4919
4920 /* The target could report the data address. Mark watchpoints
4921 affected by this data address as triggered, and all others as not
4922 triggered. */
4923
4924 ALL_BREAKPOINTS (b)
cc60f2e3 4925 if (is_hardware_watchpoint (b))
d983da9c 4926 {
3a5c3e22 4927 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4928 struct bp_location *loc;
d983da9c 4929
3a5c3e22 4930 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4931 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4932 {
3a5c3e22 4933 if (is_masked_watchpoint (b))
9c06b0b4 4934 {
3a5c3e22
PA
4935 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4936 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4937
4938 if (newaddr == start)
4939 {
3a5c3e22 4940 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4941 break;
4942 }
4943 }
4944 /* Exact match not required. Within range is sufficient. */
4945 else if (target_watchpoint_addr_within_range (&current_target,
4946 addr, loc->address,
4947 loc->length))
4948 {
3a5c3e22 4949 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4950 break;
4951 }
4952 }
d983da9c
DJ
4953 }
4954
4955 return 1;
4956}
4957
bf469271
PA
4958/* Possible return values for watchpoint_check. */
4959enum wp_check_result
4960 {
4961 /* The watchpoint has been deleted. */
4962 WP_DELETED = 1,
4963
4964 /* The value has changed. */
4965 WP_VALUE_CHANGED = 2,
4966
4967 /* The value has not changed. */
4968 WP_VALUE_NOT_CHANGED = 3,
4969
4970 /* Ignore this watchpoint, no matter if the value changed or not. */
4971 WP_IGNORE = 4,
4972 };
c906108c
SS
4973
4974#define BP_TEMPFLAG 1
4975#define BP_HARDWAREFLAG 2
4976
4a64f543 4977/* Evaluate watchpoint condition expression and check if its value
bf469271 4978 changed. */
553e4c11 4979
bf469271
PA
4980static wp_check_result
4981watchpoint_check (bpstat bs)
c906108c 4982{
3a5c3e22 4983 struct watchpoint *b;
c906108c
SS
4984 struct frame_info *fr;
4985 int within_current_scope;
4986
f431efe5 4987 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4988 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4989 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4990
f6bc2008
PA
4991 /* If this is a local watchpoint, we only want to check if the
4992 watchpoint frame is in scope if the current thread is the thread
4993 that was used to create the watchpoint. */
4994 if (!watchpoint_in_thread_scope (b))
60e1c644 4995 return WP_IGNORE;
f6bc2008 4996
c906108c
SS
4997 if (b->exp_valid_block == NULL)
4998 within_current_scope = 1;
4999 else
5000 {
edb3359d
DJ
5001 struct frame_info *frame = get_current_frame ();
5002 struct gdbarch *frame_arch = get_frame_arch (frame);
5003 CORE_ADDR frame_pc = get_frame_pc (frame);
5004
c9cf6e20 5005 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5006 still in the function but the stack frame has already been
5007 invalidated. Since we can't rely on the values of local
5008 variables after the stack has been destroyed, we are treating
5009 the watchpoint in that state as `not changed' without further
5010 checking. Don't mark watchpoints as changed if the current
5011 frame is in an epilogue - even if they are in some other
5012 frame, our view of the stack is likely to be wrong and
5013 frame_find_by_id could error out. */
c9cf6e20 5014 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5015 return WP_IGNORE;
a0f49112 5016
101dcfbe 5017 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5018 within_current_scope = (fr != NULL);
69fbadd5
DJ
5019
5020 /* If we've gotten confused in the unwinder, we might have
5021 returned a frame that can't describe this variable. */
edb3359d
DJ
5022 if (within_current_scope)
5023 {
5024 struct symbol *function;
5025
5026 function = get_frame_function (fr);
5027 if (function == NULL
5028 || !contained_in (b->exp_valid_block,
5029 SYMBOL_BLOCK_VALUE (function)))
5030 within_current_scope = 0;
5031 }
69fbadd5 5032
edb3359d 5033 if (within_current_scope)
c906108c
SS
5034 /* If we end up stopping, the current frame will get selected
5035 in normal_stop. So this call to select_frame won't affect
5036 the user. */
0f7d239c 5037 select_frame (fr);
c906108c 5038 }
c5aa993b 5039
c906108c
SS
5040 if (within_current_scope)
5041 {
4a64f543
MS
5042 /* We use value_{,free_to_}mark because it could be a *long*
5043 time before we return to the command level and call
5044 free_all_values. We can't call free_all_values because we
5045 might be in the middle of evaluating a function call. */
c906108c 5046
0cf6dd15 5047 int pc = 0;
9c06b0b4 5048 struct value *mark;
fa4727a6
DJ
5049 struct value *new_val;
5050
c1fc2657 5051 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5052 /* Since we don't know the exact trigger address (from
5053 stopped_data_address), just tell the user we've triggered
5054 a mask watchpoint. */
5055 return WP_VALUE_CHANGED;
5056
5057 mark = value_mark ();
4d01a485 5058 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5059
bb9d5f81
PP
5060 if (b->val_bitsize != 0)
5061 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5062
4a64f543
MS
5063 /* We use value_equal_contents instead of value_equal because
5064 the latter coerces an array to a pointer, thus comparing just
5065 the address of the array instead of its contents. This is
5066 not what we want. */
fa4727a6 5067 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5068 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5069 {
fa4727a6
DJ
5070 if (new_val != NULL)
5071 {
5072 release_value (new_val);
5073 value_free_to_mark (mark);
5074 }
c906108c
SS
5075 bs->old_val = b->val;
5076 b->val = new_val;
fa4727a6 5077 b->val_valid = 1;
c906108c
SS
5078 return WP_VALUE_CHANGED;
5079 }
5080 else
5081 {
60e1c644 5082 /* Nothing changed. */
c906108c 5083 value_free_to_mark (mark);
c906108c
SS
5084 return WP_VALUE_NOT_CHANGED;
5085 }
5086 }
5087 else
5088 {
5089 /* This seems like the only logical thing to do because
c5aa993b
JM
5090 if we temporarily ignored the watchpoint, then when
5091 we reenter the block in which it is valid it contains
5092 garbage (in the case of a function, it may have two
5093 garbage values, one before and one after the prologue).
5094 So we can't even detect the first assignment to it and
5095 watch after that (since the garbage may or may not equal
5096 the first value assigned). */
348d480f
PA
5097 /* We print all the stop information in
5098 breakpoint_ops->print_it, but in this case, by the time we
5099 call breakpoint_ops->print_it this bp will be deleted
5100 already. So we have no choice but print the information
5101 here. */
468afe6c 5102
0e454242 5103 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5104 {
5105 struct ui_out *uiout = current_uiout;
5106
112e8700
SM
5107 if (uiout->is_mi_like_p ())
5108 uiout->field_string
5109 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5110 uiout->text ("\nWatchpoint ");
c1fc2657 5111 uiout->field_int ("wpnum", b->number);
112e8700 5112 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5113 "which its expression is valid.\n");
5114 }
4ce44c66 5115
cdac0397 5116 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5117 b->commands = NULL;
d0fb5eae 5118 watchpoint_del_at_next_stop (b);
c906108c
SS
5119
5120 return WP_DELETED;
5121 }
5122}
5123
18a18393 5124/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5125 breakpoint location BL. This function does not check if we should
5126 stop, only if BL explains the stop. */
5127
18a18393 5128static int
6c95b8df 5129bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5130 struct address_space *aspace, CORE_ADDR bp_addr,
5131 const struct target_waitstatus *ws)
18a18393
VP
5132{
5133 struct breakpoint *b = bl->owner;
5134
348d480f 5135 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5136 gdb_assert (b != NULL);
5137
09ac7c10 5138 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5139}
5140
3a5c3e22
PA
5141/* Determine if the watched values have actually changed, and we
5142 should stop. If not, set BS->stop to 0. */
5143
18a18393
VP
5144static void
5145bpstat_check_watchpoint (bpstat bs)
5146{
2bdf28a0 5147 const struct bp_location *bl;
3a5c3e22 5148 struct watchpoint *b;
2bdf28a0
JK
5149
5150 /* BS is built for existing struct breakpoint. */
f431efe5 5151 bl = bs->bp_location_at;
2bdf28a0 5152 gdb_assert (bl != NULL);
3a5c3e22 5153 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5154 gdb_assert (b != NULL);
18a18393 5155
18a18393 5156 {
18a18393
VP
5157 int must_check_value = 0;
5158
c1fc2657 5159 if (b->type == bp_watchpoint)
18a18393
VP
5160 /* For a software watchpoint, we must always check the
5161 watched value. */
5162 must_check_value = 1;
5163 else if (b->watchpoint_triggered == watch_triggered_yes)
5164 /* We have a hardware watchpoint (read, write, or access)
5165 and the target earlier reported an address watched by
5166 this watchpoint. */
5167 must_check_value = 1;
5168 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5169 && b->type == bp_hardware_watchpoint)
18a18393
VP
5170 /* We were stopped by a hardware watchpoint, but the target could
5171 not report the data address. We must check the watchpoint's
5172 value. Access and read watchpoints are out of luck; without
5173 a data address, we can't figure it out. */
5174 must_check_value = 1;
3a5c3e22 5175
18a18393
VP
5176 if (must_check_value)
5177 {
bf469271
PA
5178 wp_check_result e;
5179
5180 TRY
5181 {
5182 e = watchpoint_check (bs);
5183 }
5184 CATCH (ex, RETURN_MASK_ALL)
5185 {
5186 exception_fprintf (gdb_stderr, ex,
5187 "Error evaluating expression "
5188 "for watchpoint %d\n",
5189 b->number);
5190
5191 SWITCH_THRU_ALL_UIS ()
5192 {
5193 printf_filtered (_("Watchpoint %d deleted.\n"),
5194 b->number);
5195 }
5196 watchpoint_del_at_next_stop (b);
5197 e = WP_DELETED;
5198 }
5199 END_CATCH
5200
18a18393
VP
5201 switch (e)
5202 {
5203 case WP_DELETED:
5204 /* We've already printed what needs to be printed. */
5205 bs->print_it = print_it_done;
5206 /* Stop. */
5207 break;
60e1c644
PA
5208 case WP_IGNORE:
5209 bs->print_it = print_it_noop;
5210 bs->stop = 0;
5211 break;
18a18393 5212 case WP_VALUE_CHANGED:
c1fc2657 5213 if (b->type == bp_read_watchpoint)
18a18393 5214 {
85d721b8
PA
5215 /* There are two cases to consider here:
5216
4a64f543 5217 1. We're watching the triggered memory for reads.
85d721b8
PA
5218 In that case, trust the target, and always report
5219 the watchpoint hit to the user. Even though
5220 reads don't cause value changes, the value may
5221 have changed since the last time it was read, and
5222 since we're not trapping writes, we will not see
5223 those, and as such we should ignore our notion of
5224 old value.
5225
4a64f543 5226 2. We're watching the triggered memory for both
85d721b8
PA
5227 reads and writes. There are two ways this may
5228 happen:
5229
4a64f543 5230 2.1. This is a target that can't break on data
85d721b8
PA
5231 reads only, but can break on accesses (reads or
5232 writes), such as e.g., x86. We detect this case
5233 at the time we try to insert read watchpoints.
5234
4a64f543 5235 2.2. Otherwise, the target supports read
85d721b8
PA
5236 watchpoints, but, the user set an access or write
5237 watchpoint watching the same memory as this read
5238 watchpoint.
5239
5240 If we're watching memory writes as well as reads,
5241 ignore watchpoint hits when we find that the
5242 value hasn't changed, as reads don't cause
5243 changes. This still gives false positives when
5244 the program writes the same value to memory as
5245 what there was already in memory (we will confuse
5246 it for a read), but it's much better than
5247 nothing. */
5248
5249 int other_write_watchpoint = 0;
5250
5251 if (bl->watchpoint_type == hw_read)
5252 {
5253 struct breakpoint *other_b;
5254
5255 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5256 if (other_b->type == bp_hardware_watchpoint
5257 || other_b->type == bp_access_watchpoint)
85d721b8 5258 {
3a5c3e22
PA
5259 struct watchpoint *other_w =
5260 (struct watchpoint *) other_b;
5261
5262 if (other_w->watchpoint_triggered
5263 == watch_triggered_yes)
5264 {
5265 other_write_watchpoint = 1;
5266 break;
5267 }
85d721b8
PA
5268 }
5269 }
5270
5271 if (other_write_watchpoint
5272 || bl->watchpoint_type == hw_access)
5273 {
5274 /* We're watching the same memory for writes,
5275 and the value changed since the last time we
5276 updated it, so this trap must be for a write.
5277 Ignore it. */
5278 bs->print_it = print_it_noop;
5279 bs->stop = 0;
5280 }
18a18393
VP
5281 }
5282 break;
5283 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5284 if (b->type == bp_hardware_watchpoint
5285 || b->type == bp_watchpoint)
18a18393
VP
5286 {
5287 /* Don't stop: write watchpoints shouldn't fire if
5288 the value hasn't changed. */
5289 bs->print_it = print_it_noop;
5290 bs->stop = 0;
5291 }
5292 /* Stop. */
5293 break;
5294 default:
5295 /* Can't happen. */
18a18393
VP
5296 break;
5297 }
5298 }
5299 else /* must_check_value == 0 */
5300 {
5301 /* This is a case where some watchpoint(s) triggered, but
5302 not at the address of this watchpoint, or else no
5303 watchpoint triggered after all. So don't print
5304 anything for this watchpoint. */
5305 bs->print_it = print_it_noop;
5306 bs->stop = 0;
5307 }
5308 }
5309}
5310
7d4df6a4
DE
5311/* For breakpoints that are currently marked as telling gdb to stop,
5312 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5313 of breakpoint referred to by BS. If we should not stop for this
5314 breakpoint, set BS->stop to 0. */
f431efe5 5315
18a18393
VP
5316static void
5317bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5318{
2bdf28a0
JK
5319 const struct bp_location *bl;
5320 struct breakpoint *b;
bf469271
PA
5321 /* Assume stop. */
5322 bool condition_result = true;
7d4df6a4
DE
5323 struct expression *cond;
5324
5325 gdb_assert (bs->stop);
2bdf28a0
JK
5326
5327 /* BS is built for existing struct breakpoint. */
f431efe5 5328 bl = bs->bp_location_at;
2bdf28a0 5329 gdb_assert (bl != NULL);
f431efe5 5330 b = bs->breakpoint_at;
2bdf28a0 5331 gdb_assert (b != NULL);
18a18393 5332
b775012e
LM
5333 /* Even if the target evaluated the condition on its end and notified GDB, we
5334 need to do so again since GDB does not know if we stopped due to a
5335 breakpoint or a single step breakpoint. */
5336
18a18393 5337 if (frame_id_p (b->frame_id)
edb3359d 5338 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5339 {
7d4df6a4
DE
5340 bs->stop = 0;
5341 return;
5342 }
60e1c644 5343
12ab52e9
PA
5344 /* If this is a thread/task-specific breakpoint, don't waste cpu
5345 evaluating the condition if this isn't the specified
5346 thread/task. */
5d5658a1 5347 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5348 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5349
6c1b0f7b
DE
5350 {
5351 bs->stop = 0;
5352 return;
5353 }
5354
6dddc817
DE
5355 /* Evaluate extension language breakpoints that have a "stop" method
5356 implemented. */
5357 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5358
7d4df6a4
DE
5359 if (is_watchpoint (b))
5360 {
5361 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5362
4d01a485 5363 cond = w->cond_exp.get ();
7d4df6a4
DE
5364 }
5365 else
4d01a485 5366 cond = bl->cond.get ();
60e1c644 5367
7d4df6a4
DE
5368 if (cond && b->disposition != disp_del_at_next_stop)
5369 {
5370 int within_current_scope = 1;
5371 struct watchpoint * w;
60e1c644 5372
7d4df6a4
DE
5373 /* We use value_mark and value_free_to_mark because it could
5374 be a long time before we return to the command level and
5375 call free_all_values. We can't call free_all_values
5376 because we might be in the middle of evaluating a
5377 function call. */
5378 struct value *mark = value_mark ();
5379
5380 if (is_watchpoint (b))
5381 w = (struct watchpoint *) b;
5382 else
5383 w = NULL;
5384
5385 /* Need to select the frame, with all that implies so that
5386 the conditions will have the right context. Because we
5387 use the frame, we will not see an inlined function's
5388 variables when we arrive at a breakpoint at the start
5389 of the inlined function; the current frame will be the
5390 call site. */
5391 if (w == NULL || w->cond_exp_valid_block == NULL)
5392 select_frame (get_current_frame ());
5393 else
18a18393 5394 {
7d4df6a4
DE
5395 struct frame_info *frame;
5396
5397 /* For local watchpoint expressions, which particular
5398 instance of a local is being watched matters, so we
5399 keep track of the frame to evaluate the expression
5400 in. To evaluate the condition however, it doesn't
5401 really matter which instantiation of the function
5402 where the condition makes sense triggers the
5403 watchpoint. This allows an expression like "watch
5404 global if q > 10" set in `func', catch writes to
5405 global on all threads that call `func', or catch
5406 writes on all recursive calls of `func' by a single
5407 thread. We simply always evaluate the condition in
5408 the innermost frame that's executing where it makes
5409 sense to evaluate the condition. It seems
5410 intuitive. */
5411 frame = block_innermost_frame (w->cond_exp_valid_block);
5412 if (frame != NULL)
5413 select_frame (frame);
5414 else
5415 within_current_scope = 0;
18a18393 5416 }
7d4df6a4 5417 if (within_current_scope)
bf469271
PA
5418 {
5419 TRY
5420 {
5421 condition_result = breakpoint_cond_eval (cond);
5422 }
5423 CATCH (ex, RETURN_MASK_ALL)
5424 {
5425 exception_fprintf (gdb_stderr, ex,
5426 "Error in testing breakpoint condition:\n");
5427 }
5428 END_CATCH
5429 }
7d4df6a4 5430 else
18a18393 5431 {
7d4df6a4
DE
5432 warning (_("Watchpoint condition cannot be tested "
5433 "in the current scope"));
5434 /* If we failed to set the right context for this
5435 watchpoint, unconditionally report it. */
18a18393 5436 }
7d4df6a4
DE
5437 /* FIXME-someday, should give breakpoint #. */
5438 value_free_to_mark (mark);
18a18393 5439 }
7d4df6a4 5440
bf469271 5441 if (cond && !condition_result)
7d4df6a4
DE
5442 {
5443 bs->stop = 0;
5444 }
7d4df6a4
DE
5445 else if (b->ignore_count > 0)
5446 {
5447 b->ignore_count--;
5448 bs->stop = 0;
5449 /* Increase the hit count even though we don't stop. */
5450 ++(b->hit_count);
5451 observer_notify_breakpoint_modified (b);
5452 }
18a18393
VP
5453}
5454
1cf4d951
PA
5455/* Returns true if we need to track moribund locations of LOC's type
5456 on the current target. */
5457
5458static int
5459need_moribund_for_location_type (struct bp_location *loc)
5460{
5461 return ((loc->loc_type == bp_loc_software_breakpoint
5462 && !target_supports_stopped_by_sw_breakpoint ())
5463 || (loc->loc_type == bp_loc_hardware_breakpoint
5464 && !target_supports_stopped_by_hw_breakpoint ()));
5465}
5466
18a18393 5467
9709f61c 5468/* Get a bpstat associated with having just stopped at address
d983da9c 5469 BP_ADDR in thread PTID.
c906108c 5470
d983da9c 5471 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5472 don't understand this stop. Result is a chain of bpstat's such
5473 that:
c906108c 5474
c5aa993b 5475 if we don't understand the stop, the result is a null pointer.
c906108c 5476
c5aa993b 5477 if we understand why we stopped, the result is not null.
c906108c 5478
c5aa993b
JM
5479 Each element of the chain refers to a particular breakpoint or
5480 watchpoint at which we have stopped. (We may have stopped for
5481 several reasons concurrently.)
c906108c 5482
c5aa993b
JM
5483 Each element of the chain has valid next, breakpoint_at,
5484 commands, FIXME??? fields. */
c906108c
SS
5485
5486bpstat
6c95b8df 5487bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5488 CORE_ADDR bp_addr, ptid_t ptid,
5489 const struct target_waitstatus *ws)
c906108c 5490{
0d381245 5491 struct breakpoint *b = NULL;
afe38095 5492 struct bp_location *bl;
20874c92 5493 struct bp_location *loc;
5760d0ab
JK
5494 /* First item of allocated bpstat's. */
5495 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5496 /* Pointer to the last thing in the chain currently. */
5760d0ab 5497 bpstat bs;
20874c92 5498 int ix;
429374b8 5499 int need_remove_insert;
f431efe5 5500 int removed_any;
c906108c 5501
f431efe5
PA
5502 /* First, build the bpstat chain with locations that explain a
5503 target stop, while being careful to not set the target running,
5504 as that may invalidate locations (in particular watchpoint
5505 locations are recreated). Resuming will happen here with
5506 breakpoint conditions or watchpoint expressions that include
5507 inferior function calls. */
c5aa993b 5508
429374b8
JK
5509 ALL_BREAKPOINTS (b)
5510 {
1a853c52 5511 if (!breakpoint_enabled (b))
429374b8 5512 continue;
a5606eee 5513
429374b8
JK
5514 for (bl = b->loc; bl != NULL; bl = bl->next)
5515 {
4a64f543
MS
5516 /* For hardware watchpoints, we look only at the first
5517 location. The watchpoint_check function will work on the
5518 entire expression, not the individual locations. For
5519 read watchpoints, the watchpoints_triggered function has
5520 checked all locations already. */
429374b8
JK
5521 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5522 break;
18a18393 5523
f6592439 5524 if (!bl->enabled || bl->shlib_disabled)
429374b8 5525 continue;
c5aa993b 5526
09ac7c10 5527 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5528 continue;
c5aa993b 5529
4a64f543
MS
5530 /* Come here if it's a watchpoint, or if the break address
5531 matches. */
c5aa993b 5532
04afa70c 5533 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5534 explain stop. */
c5aa993b 5535
f431efe5
PA
5536 /* Assume we stop. Should we find a watchpoint that is not
5537 actually triggered, or if the condition of the breakpoint
5538 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5539 bs->stop = 1;
5540 bs->print = 1;
d983da9c 5541
f431efe5
PA
5542 /* If this is a scope breakpoint, mark the associated
5543 watchpoint as triggered so that we will handle the
5544 out-of-scope event. We'll get to the watchpoint next
5545 iteration. */
d0fb5eae 5546 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5547 {
5548 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5549
5550 w->watchpoint_triggered = watch_triggered_yes;
5551 }
f431efe5
PA
5552 }
5553 }
5554
7c16b83e 5555 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5556 if (!target_supports_stopped_by_sw_breakpoint ()
5557 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5558 {
1cf4d951 5559 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5560 {
1cf4d951
PA
5561 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5562 && need_moribund_for_location_type (loc))
5563 {
04afa70c 5564 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5565 /* For hits of moribund locations, we should just proceed. */
5566 bs->stop = 0;
5567 bs->print = 0;
5568 bs->print_it = print_it_noop;
5569 }
f431efe5
PA
5570 }
5571 }
5572
edcc5120
TT
5573 /* A bit of special processing for shlib breakpoints. We need to
5574 process solib loading here, so that the lists of loaded and
5575 unloaded libraries are correct before we handle "catch load" and
5576 "catch unload". */
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
5578 {
5d268276 5579 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5580 {
5581 handle_solib_event ();
5582 break;
5583 }
5584 }
5585
f431efe5
PA
5586 /* Now go through the locations that caused the target to stop, and
5587 check whether we're interested in reporting this stop to higher
5588 layers, or whether we should resume the target transparently. */
5589
5590 removed_any = 0;
5591
5760d0ab 5592 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5593 {
5594 if (!bs->stop)
5595 continue;
5596
f431efe5 5597 b = bs->breakpoint_at;
348d480f
PA
5598 b->ops->check_status (bs);
5599 if (bs->stop)
28010a5d 5600 {
348d480f 5601 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5602
429374b8
JK
5603 if (bs->stop)
5604 {
5605 ++(b->hit_count);
8d3788bd 5606 observer_notify_breakpoint_modified (b);
c906108c 5607
4a64f543 5608 /* We will stop here. */
429374b8
JK
5609 if (b->disposition == disp_disable)
5610 {
816338b5 5611 --(b->enable_count);
1a853c52 5612 if (b->enable_count <= 0)
429374b8 5613 b->enable_state = bp_disabled;
f431efe5 5614 removed_any = 1;
429374b8
JK
5615 }
5616 if (b->silent)
5617 bs->print = 0;
5618 bs->commands = b->commands;
abf85f46 5619 if (command_line_is_silent (bs->commands
d1b0a7bf 5620 ? bs->commands.get () : NULL))
abf85f46 5621 bs->print = 0;
9d6e6e84
HZ
5622
5623 b->ops->after_condition_true (bs);
429374b8
JK
5624 }
5625
348d480f 5626 }
a9b3a50f
PA
5627
5628 /* Print nothing for this entry if we don't stop or don't
5629 print. */
5630 if (!bs->stop || !bs->print)
5631 bs->print_it = print_it_noop;
429374b8 5632 }
876fa593 5633
d983da9c
DJ
5634 /* If we aren't stopping, the value of some hardware watchpoint may
5635 not have changed, but the intermediate memory locations we are
5636 watching may have. Don't bother if we're stopping; this will get
5637 done later. */
d832cb68 5638 need_remove_insert = 0;
5760d0ab
JK
5639 if (! bpstat_causes_stop (bs_head))
5640 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5641 if (!bs->stop
f431efe5
PA
5642 && bs->breakpoint_at
5643 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5644 {
3a5c3e22
PA
5645 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5646
5647 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5648 need_remove_insert = 1;
d983da9c
DJ
5649 }
5650
d832cb68 5651 if (need_remove_insert)
44702360 5652 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5653 else if (removed_any)
44702360 5654 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5655
5760d0ab 5656 return bs_head;
c906108c 5657}
628fe4e4
JK
5658
5659static void
5660handle_jit_event (void)
5661{
5662 struct frame_info *frame;
5663 struct gdbarch *gdbarch;
5664
243a9253
PA
5665 if (debug_infrun)
5666 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5667
628fe4e4
JK
5668 /* Switch terminal for any messages produced by
5669 breakpoint_re_set. */
223ffa71 5670 target_terminal::ours_for_output ();
628fe4e4
JK
5671
5672 frame = get_current_frame ();
5673 gdbarch = get_frame_arch (frame);
5674
5675 jit_event_handler (gdbarch);
5676
223ffa71 5677 target_terminal::inferior ();
628fe4e4
JK
5678}
5679
5680/* Prepare WHAT final decision for infrun. */
5681
5682/* Decide what infrun needs to do with this bpstat. */
5683
c906108c 5684struct bpstat_what
0e30163f 5685bpstat_what (bpstat bs_head)
c906108c 5686{
c906108c 5687 struct bpstat_what retval;
0e30163f 5688 bpstat bs;
c906108c 5689
628fe4e4 5690 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5691 retval.call_dummy = STOP_NONE;
186c406b 5692 retval.is_longjmp = 0;
628fe4e4 5693
0e30163f 5694 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5695 {
628fe4e4
JK
5696 /* Extract this BS's action. After processing each BS, we check
5697 if its action overrides all we've seem so far. */
5698 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5699 enum bptype bptype;
5700
c906108c 5701 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5702 {
5703 /* I suspect this can happen if it was a momentary
5704 breakpoint which has since been deleted. */
5705 bptype = bp_none;
5706 }
20874c92 5707 else
f431efe5 5708 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5709
5710 switch (bptype)
c906108c
SS
5711 {
5712 case bp_none:
628fe4e4 5713 break;
c906108c
SS
5714 case bp_breakpoint:
5715 case bp_hardware_breakpoint:
7c16b83e 5716 case bp_single_step:
c906108c
SS
5717 case bp_until:
5718 case bp_finish:
a9b3a50f 5719 case bp_shlib_event:
c906108c
SS
5720 if (bs->stop)
5721 {
5722 if (bs->print)
628fe4e4 5723 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5724 else
628fe4e4 5725 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5726 }
5727 else
628fe4e4 5728 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5729 break;
5730 case bp_watchpoint:
5731 case bp_hardware_watchpoint:
5732 case bp_read_watchpoint:
5733 case bp_access_watchpoint:
5734 if (bs->stop)
5735 {
5736 if (bs->print)
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5738 else
628fe4e4 5739 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5740 }
5741 else
628fe4e4
JK
5742 {
5743 /* There was a watchpoint, but we're not stopping.
5744 This requires no further action. */
5745 }
c906108c
SS
5746 break;
5747 case bp_longjmp:
e2e4d78b 5748 case bp_longjmp_call_dummy:
186c406b 5749 case bp_exception:
0a39bb32
PA
5750 if (bs->stop)
5751 {
5752 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5753 retval.is_longjmp = bptype != bp_exception;
5754 }
5755 else
5756 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5757 break;
5758 case bp_longjmp_resume:
186c406b 5759 case bp_exception_resume:
0a39bb32
PA
5760 if (bs->stop)
5761 {
5762 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5763 retval.is_longjmp = bptype == bp_longjmp_resume;
5764 }
5765 else
5766 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5767 break;
5768 case bp_step_resume:
5769 if (bs->stop)
628fe4e4
JK
5770 this_action = BPSTAT_WHAT_STEP_RESUME;
5771 else
c906108c 5772 {
628fe4e4
JK
5773 /* It is for the wrong frame. */
5774 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5775 }
c906108c 5776 break;
2c03e5be
PA
5777 case bp_hp_step_resume:
5778 if (bs->stop)
5779 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5780 else
5781 {
5782 /* It is for the wrong frame. */
5783 this_action = BPSTAT_WHAT_SINGLE;
5784 }
5785 break;
c906108c 5786 case bp_watchpoint_scope:
c4093a6a 5787 case bp_thread_event:
1900040c 5788 case bp_overlay_event:
0fd8e87f 5789 case bp_longjmp_master:
aa7d318d 5790 case bp_std_terminate_master:
186c406b 5791 case bp_exception_master:
628fe4e4 5792 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5793 break;
ce78b96d 5794 case bp_catchpoint:
c5aa993b
JM
5795 if (bs->stop)
5796 {
5797 if (bs->print)
628fe4e4 5798 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5799 else
628fe4e4 5800 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5801 }
5802 else
628fe4e4
JK
5803 {
5804 /* There was a catchpoint, but we're not stopping.
5805 This requires no further action. */
5806 }
5807 break;
628fe4e4 5808 case bp_jit_event:
628fe4e4 5809 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5810 break;
c906108c 5811 case bp_call_dummy:
53a5351d
JM
5812 /* Make sure the action is stop (silent or noisy),
5813 so infrun.c pops the dummy frame. */
aa7d318d 5814 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5815 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5816 break;
5817 case bp_std_terminate:
5818 /* Make sure the action is stop (silent or noisy),
5819 so infrun.c pops the dummy frame. */
aa7d318d 5820 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5821 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5822 break;
1042e4c0 5823 case bp_tracepoint:
7a697b8d 5824 case bp_fast_tracepoint:
0fb4aa4b 5825 case bp_static_tracepoint:
1042e4c0
SS
5826 /* Tracepoint hits should not be reported back to GDB, and
5827 if one got through somehow, it should have been filtered
5828 out already. */
5829 internal_error (__FILE__, __LINE__,
7a697b8d 5830 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5831 break;
5832 case bp_gnu_ifunc_resolver:
5833 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5834 this_action = BPSTAT_WHAT_SINGLE;
5835 break;
5836 case bp_gnu_ifunc_resolver_return:
5837 /* The breakpoint will be removed, execution will restart from the
5838 PC of the former breakpoint. */
5839 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5840 break;
e7e0cddf
SS
5841
5842 case bp_dprintf:
a11cfd87
HZ
5843 if (bs->stop)
5844 this_action = BPSTAT_WHAT_STOP_SILENT;
5845 else
5846 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5847 break;
5848
628fe4e4
JK
5849 default:
5850 internal_error (__FILE__, __LINE__,
5851 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5852 }
628fe4e4 5853
325fac50 5854 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5855 }
628fe4e4 5856
243a9253
PA
5857 return retval;
5858}
628fe4e4 5859
243a9253
PA
5860void
5861bpstat_run_callbacks (bpstat bs_head)
5862{
5863 bpstat bs;
628fe4e4 5864
0e30163f
JK
5865 for (bs = bs_head; bs != NULL; bs = bs->next)
5866 {
5867 struct breakpoint *b = bs->breakpoint_at;
5868
5869 if (b == NULL)
5870 continue;
5871 switch (b->type)
5872 {
243a9253
PA
5873 case bp_jit_event:
5874 handle_jit_event ();
5875 break;
0e30163f
JK
5876 case bp_gnu_ifunc_resolver:
5877 gnu_ifunc_resolver_stop (b);
5878 break;
5879 case bp_gnu_ifunc_resolver_return:
5880 gnu_ifunc_resolver_return_stop (b);
5881 break;
5882 }
5883 }
c906108c
SS
5884}
5885
5886/* Nonzero if we should step constantly (e.g. watchpoints on machines
5887 without hardware support). This isn't related to a specific bpstat,
5888 just to things like whether watchpoints are set. */
5889
c5aa993b 5890int
fba45db2 5891bpstat_should_step (void)
c906108c
SS
5892{
5893 struct breakpoint *b;
cc59ec59 5894
c906108c 5895 ALL_BREAKPOINTS (b)
717a8278 5896 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5897 return 1;
c906108c
SS
5898 return 0;
5899}
5900
67822962
PA
5901int
5902bpstat_causes_stop (bpstat bs)
5903{
5904 for (; bs != NULL; bs = bs->next)
5905 if (bs->stop)
5906 return 1;
5907
5908 return 0;
5909}
5910
c906108c 5911\f
c5aa993b 5912
170b53b2
UW
5913/* Compute a string of spaces suitable to indent the next line
5914 so it starts at the position corresponding to the table column
5915 named COL_NAME in the currently active table of UIOUT. */
5916
5917static char *
5918wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5919{
5920 static char wrap_indent[80];
5921 int i, total_width, width, align;
c5209615 5922 const char *text;
170b53b2
UW
5923
5924 total_width = 0;
112e8700 5925 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5926 {
5927 if (strcmp (text, col_name) == 0)
5928 {
5929 gdb_assert (total_width < sizeof wrap_indent);
5930 memset (wrap_indent, ' ', total_width);
5931 wrap_indent[total_width] = 0;
5932
5933 return wrap_indent;
5934 }
5935
5936 total_width += width + 1;
5937 }
5938
5939 return NULL;
5940}
5941
b775012e
LM
5942/* Determine if the locations of this breakpoint will have their conditions
5943 evaluated by the target, host or a mix of both. Returns the following:
5944
5945 "host": Host evals condition.
5946 "host or target": Host or Target evals condition.
5947 "target": Target evals condition.
5948*/
5949
5950static const char *
5951bp_condition_evaluator (struct breakpoint *b)
5952{
5953 struct bp_location *bl;
5954 char host_evals = 0;
5955 char target_evals = 0;
5956
5957 if (!b)
5958 return NULL;
5959
5960 if (!is_breakpoint (b))
5961 return NULL;
5962
5963 if (gdb_evaluates_breakpoint_condition_p ()
5964 || !target_supports_evaluation_of_breakpoint_conditions ())
5965 return condition_evaluation_host;
5966
5967 for (bl = b->loc; bl; bl = bl->next)
5968 {
5969 if (bl->cond_bytecode)
5970 target_evals++;
5971 else
5972 host_evals++;
5973 }
5974
5975 if (host_evals && target_evals)
5976 return condition_evaluation_both;
5977 else if (target_evals)
5978 return condition_evaluation_target;
5979 else
5980 return condition_evaluation_host;
5981}
5982
5983/* Determine the breakpoint location's condition evaluator. This is
5984 similar to bp_condition_evaluator, but for locations. */
5985
5986static const char *
5987bp_location_condition_evaluator (struct bp_location *bl)
5988{
5989 if (bl && !is_breakpoint (bl->owner))
5990 return NULL;
5991
5992 if (gdb_evaluates_breakpoint_condition_p ()
5993 || !target_supports_evaluation_of_breakpoint_conditions ())
5994 return condition_evaluation_host;
5995
5996 if (bl && bl->cond_bytecode)
5997 return condition_evaluation_target;
5998 else
5999 return condition_evaluation_host;
6000}
6001
859825b8
JK
6002/* Print the LOC location out of the list of B->LOC locations. */
6003
170b53b2
UW
6004static void
6005print_breakpoint_location (struct breakpoint *b,
6006 struct bp_location *loc)
0d381245 6007{
79a45e25 6008 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6009
6010 scoped_restore_current_program_space restore_pspace;
6c95b8df 6011
859825b8
JK
6012 if (loc != NULL && loc->shlib_disabled)
6013 loc = NULL;
6014
6c95b8df
PA
6015 if (loc != NULL)
6016 set_current_program_space (loc->pspace);
6017
56435ebe 6018 if (b->display_canonical)
d28cd78a 6019 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6020 else if (loc && loc->symtab)
0d381245
VP
6021 {
6022 struct symbol *sym
6023 = find_pc_sect_function (loc->address, loc->section);
6024 if (sym)
6025 {
112e8700
SM
6026 uiout->text ("in ");
6027 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6028 uiout->text (" ");
6029 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6030 uiout->text ("at ");
0d381245 6031 }
112e8700 6032 uiout->field_string ("file",
05cba821 6033 symtab_to_filename_for_display (loc->symtab));
112e8700 6034 uiout->text (":");
05cba821 6035
112e8700
SM
6036 if (uiout->is_mi_like_p ())
6037 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6038
112e8700 6039 uiout->field_int ("line", loc->line_number);
0d381245 6040 }
859825b8 6041 else if (loc)
0d381245 6042 {
d7e74731 6043 string_file stb;
170b53b2 6044
d7e74731 6045 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6046 demangle, "");
112e8700 6047 uiout->field_stream ("at", stb);
0d381245 6048 }
859825b8 6049 else
f00aae0f 6050 {
d28cd78a
TT
6051 uiout->field_string ("pending",
6052 event_location_to_string (b->location.get ()));
f00aae0f
KS
6053 /* If extra_string is available, it could be holding a condition
6054 or dprintf arguments. In either case, make sure it is printed,
6055 too, but only for non-MI streams. */
112e8700 6056 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6057 {
6058 if (b->type == bp_dprintf)
112e8700 6059 uiout->text (",");
f00aae0f 6060 else
112e8700
SM
6061 uiout->text (" ");
6062 uiout->text (b->extra_string);
f00aae0f
KS
6063 }
6064 }
6c95b8df 6065
b775012e
LM
6066 if (loc && is_breakpoint (b)
6067 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6068 && bp_condition_evaluator (b) == condition_evaluation_both)
6069 {
112e8700
SM
6070 uiout->text (" (");
6071 uiout->field_string ("evaluated-by",
b775012e 6072 bp_location_condition_evaluator (loc));
112e8700 6073 uiout->text (")");
b775012e 6074 }
0d381245
VP
6075}
6076
269b11a2
PA
6077static const char *
6078bptype_string (enum bptype type)
c906108c 6079{
c4093a6a
JM
6080 struct ep_type_description
6081 {
6082 enum bptype type;
a121b7c1 6083 const char *description;
c4093a6a
JM
6084 };
6085 static struct ep_type_description bptypes[] =
c906108c 6086 {
c5aa993b
JM
6087 {bp_none, "?deleted?"},
6088 {bp_breakpoint, "breakpoint"},
c906108c 6089 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6090 {bp_single_step, "sw single-step"},
c5aa993b
JM
6091 {bp_until, "until"},
6092 {bp_finish, "finish"},
6093 {bp_watchpoint, "watchpoint"},
c906108c 6094 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6095 {bp_read_watchpoint, "read watchpoint"},
6096 {bp_access_watchpoint, "acc watchpoint"},
6097 {bp_longjmp, "longjmp"},
6098 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6099 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6100 {bp_exception, "exception"},
6101 {bp_exception_resume, "exception resume"},
c5aa993b 6102 {bp_step_resume, "step resume"},
2c03e5be 6103 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6104 {bp_watchpoint_scope, "watchpoint scope"},
6105 {bp_call_dummy, "call dummy"},
aa7d318d 6106 {bp_std_terminate, "std::terminate"},
c5aa993b 6107 {bp_shlib_event, "shlib events"},
c4093a6a 6108 {bp_thread_event, "thread events"},
1900040c 6109 {bp_overlay_event, "overlay events"},
0fd8e87f 6110 {bp_longjmp_master, "longjmp master"},
aa7d318d 6111 {bp_std_terminate_master, "std::terminate master"},
186c406b 6112 {bp_exception_master, "exception master"},
ce78b96d 6113 {bp_catchpoint, "catchpoint"},
1042e4c0 6114 {bp_tracepoint, "tracepoint"},
7a697b8d 6115 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6116 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6117 {bp_dprintf, "dprintf"},
4efc6507 6118 {bp_jit_event, "jit events"},
0e30163f
JK
6119 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6120 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6121 };
269b11a2
PA
6122
6123 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6124 || ((int) type != bptypes[(int) type].type))
6125 internal_error (__FILE__, __LINE__,
6126 _("bptypes table does not describe type #%d."),
6127 (int) type);
6128
6129 return bptypes[(int) type].description;
6130}
6131
998580f1
MK
6132/* For MI, output a field named 'thread-groups' with a list as the value.
6133 For CLI, prefix the list with the string 'inf'. */
6134
6135static void
6136output_thread_groups (struct ui_out *uiout,
6137 const char *field_name,
6138 VEC(int) *inf_num,
6139 int mi_only)
6140{
112e8700 6141 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6142 int inf;
6143 int i;
6144
6145 /* For backward compatibility, don't display inferiors in CLI unless
6146 there are several. Always display them for MI. */
6147 if (!is_mi && mi_only)
6148 return;
6149
10f489e5 6150 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6151
998580f1
MK
6152 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6153 {
6154 if (is_mi)
6155 {
6156 char mi_group[10];
6157
6158 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6159 uiout->field_string (NULL, mi_group);
998580f1
MK
6160 }
6161 else
6162 {
6163 if (i == 0)
112e8700 6164 uiout->text (" inf ");
998580f1 6165 else
112e8700 6166 uiout->text (", ");
998580f1 6167
112e8700 6168 uiout->text (plongest (inf));
998580f1
MK
6169 }
6170 }
998580f1
MK
6171}
6172
269b11a2
PA
6173/* Print B to gdb_stdout. */
6174
6175static void
6176print_one_breakpoint_location (struct breakpoint *b,
6177 struct bp_location *loc,
6178 int loc_number,
6179 struct bp_location **last_loc,
269b11a2
PA
6180 int allflag)
6181{
6182 struct command_line *l;
c2c6d25f 6183 static char bpenables[] = "nynny";
c906108c 6184
79a45e25 6185 struct ui_out *uiout = current_uiout;
0d381245
VP
6186 int header_of_multiple = 0;
6187 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6188 struct value_print_options opts;
6189
6190 get_user_print_options (&opts);
0d381245
VP
6191
6192 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6193 /* See comment in print_one_breakpoint concerning treatment of
6194 breakpoints with single disabled location. */
0d381245
VP
6195 if (loc == NULL
6196 && (b->loc != NULL
6197 && (b->loc->next != NULL || !b->loc->enabled)))
6198 header_of_multiple = 1;
6199 if (loc == NULL)
6200 loc = b->loc;
6201
c4093a6a
JM
6202 annotate_record ();
6203
6204 /* 1 */
6205 annotate_field (0);
0d381245
VP
6206 if (part_of_multiple)
6207 {
6208 char *formatted;
0c6773c1 6209 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6210 uiout->field_string ("number", formatted);
0d381245
VP
6211 xfree (formatted);
6212 }
6213 else
6214 {
112e8700 6215 uiout->field_int ("number", b->number);
0d381245 6216 }
c4093a6a
JM
6217
6218 /* 2 */
6219 annotate_field (1);
0d381245 6220 if (part_of_multiple)
112e8700 6221 uiout->field_skip ("type");
269b11a2 6222 else
112e8700 6223 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6224
6225 /* 3 */
6226 annotate_field (2);
0d381245 6227 if (part_of_multiple)
112e8700 6228 uiout->field_skip ("disp");
0d381245 6229 else
112e8700 6230 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6231
c4093a6a
JM
6232
6233 /* 4 */
6234 annotate_field (3);
0d381245 6235 if (part_of_multiple)
112e8700 6236 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6237 else
112e8700
SM
6238 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6239 uiout->spaces (2);
0d381245 6240
c4093a6a
JM
6241
6242 /* 5 and 6 */
3086aeae 6243 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6244 {
4a64f543
MS
6245 /* Although the print_one can possibly print all locations,
6246 calling it here is not likely to get any nice result. So,
6247 make sure there's just one location. */
0d381245 6248 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6249 b->ops->print_one (b, last_loc);
0d381245 6250 }
3086aeae
DJ
6251 else
6252 switch (b->type)
6253 {
6254 case bp_none:
6255 internal_error (__FILE__, __LINE__,
e2e0b3e5 6256 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6257 break;
c906108c 6258
3086aeae
DJ
6259 case bp_watchpoint:
6260 case bp_hardware_watchpoint:
6261 case bp_read_watchpoint:
6262 case bp_access_watchpoint:
3a5c3e22
PA
6263 {
6264 struct watchpoint *w = (struct watchpoint *) b;
6265
6266 /* Field 4, the address, is omitted (which makes the columns
6267 not line up too nicely with the headers, but the effect
6268 is relatively readable). */
6269 if (opts.addressprint)
112e8700 6270 uiout->field_skip ("addr");
3a5c3e22 6271 annotate_field (5);
112e8700 6272 uiout->field_string ("what", w->exp_string);
3a5c3e22 6273 }
3086aeae
DJ
6274 break;
6275
3086aeae
DJ
6276 case bp_breakpoint:
6277 case bp_hardware_breakpoint:
7c16b83e 6278 case bp_single_step:
3086aeae
DJ
6279 case bp_until:
6280 case bp_finish:
6281 case bp_longjmp:
6282 case bp_longjmp_resume:
e2e4d78b 6283 case bp_longjmp_call_dummy:
186c406b
TT
6284 case bp_exception:
6285 case bp_exception_resume:
3086aeae 6286 case bp_step_resume:
2c03e5be 6287 case bp_hp_step_resume:
3086aeae
DJ
6288 case bp_watchpoint_scope:
6289 case bp_call_dummy:
aa7d318d 6290 case bp_std_terminate:
3086aeae
DJ
6291 case bp_shlib_event:
6292 case bp_thread_event:
6293 case bp_overlay_event:
0fd8e87f 6294 case bp_longjmp_master:
aa7d318d 6295 case bp_std_terminate_master:
186c406b 6296 case bp_exception_master:
1042e4c0 6297 case bp_tracepoint:
7a697b8d 6298 case bp_fast_tracepoint:
0fb4aa4b 6299 case bp_static_tracepoint:
e7e0cddf 6300 case bp_dprintf:
4efc6507 6301 case bp_jit_event:
0e30163f
JK
6302 case bp_gnu_ifunc_resolver:
6303 case bp_gnu_ifunc_resolver_return:
79a45b7d 6304 if (opts.addressprint)
3086aeae
DJ
6305 {
6306 annotate_field (4);
54e52265 6307 if (header_of_multiple)
112e8700 6308 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6309 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6310 uiout->field_string ("addr", "<PENDING>");
0101ce28 6311 else
112e8700 6312 uiout->field_core_addr ("addr",
5af949e3 6313 loc->gdbarch, loc->address);
3086aeae
DJ
6314 }
6315 annotate_field (5);
0d381245 6316 if (!header_of_multiple)
170b53b2 6317 print_breakpoint_location (b, loc);
0d381245 6318 if (b->loc)
a6d9a66e 6319 *last_loc = b->loc;
3086aeae
DJ
6320 break;
6321 }
c906108c 6322
6c95b8df 6323
998580f1 6324 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6325 {
6326 struct inferior *inf;
998580f1
MK
6327 VEC(int) *inf_num = NULL;
6328 int mi_only = 1;
6c95b8df 6329
998580f1 6330 ALL_INFERIORS (inf)
6c95b8df
PA
6331 {
6332 if (inf->pspace == loc->pspace)
998580f1 6333 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6334 }
998580f1
MK
6335
6336 /* For backward compatibility, don't display inferiors in CLI unless
6337 there are several. Always display for MI. */
6338 if (allflag
6339 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6340 && (number_of_program_spaces () > 1
6341 || number_of_inferiors () > 1)
6342 /* LOC is for existing B, it cannot be in
6343 moribund_locations and thus having NULL OWNER. */
6344 && loc->owner->type != bp_catchpoint))
6345 mi_only = 0;
6346 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6347 VEC_free (int, inf_num);
6c95b8df
PA
6348 }
6349
4a306c9a 6350 if (!part_of_multiple)
c4093a6a 6351 {
4a306c9a
JB
6352 if (b->thread != -1)
6353 {
6354 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6355 "stop only in" line a little further down. */
112e8700
SM
6356 uiout->text (" thread ");
6357 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6358 }
6359 else if (b->task != 0)
6360 {
112e8700
SM
6361 uiout->text (" task ");
6362 uiout->field_int ("task", b->task);
4a306c9a 6363 }
c4093a6a 6364 }
f1310107 6365
112e8700 6366 uiout->text ("\n");
f1310107 6367
348d480f 6368 if (!part_of_multiple)
f1310107
TJB
6369 b->ops->print_one_detail (b, uiout);
6370
0d381245 6371 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6372 {
6373 annotate_field (6);
112e8700 6374 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6375 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6376 the frame ID. */
112e8700 6377 uiout->field_core_addr ("frame",
5af949e3 6378 b->gdbarch, b->frame_id.stack_addr);
112e8700 6379 uiout->text ("\n");
c4093a6a
JM
6380 }
6381
28010a5d 6382 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6383 {
6384 annotate_field (7);
d77f58be 6385 if (is_tracepoint (b))
112e8700 6386 uiout->text ("\ttrace only if ");
1042e4c0 6387 else
112e8700
SM
6388 uiout->text ("\tstop only if ");
6389 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6390
6391 /* Print whether the target is doing the breakpoint's condition
6392 evaluation. If GDB is doing the evaluation, don't print anything. */
6393 if (is_breakpoint (b)
6394 && breakpoint_condition_evaluation_mode ()
6395 == condition_evaluation_target)
6396 {
112e8700
SM
6397 uiout->text (" (");
6398 uiout->field_string ("evaluated-by",
b775012e 6399 bp_condition_evaluator (b));
112e8700 6400 uiout->text (" evals)");
b775012e 6401 }
112e8700 6402 uiout->text ("\n");
0101ce28
JJ
6403 }
6404
0d381245 6405 if (!part_of_multiple && b->thread != -1)
c4093a6a 6406 {
4a64f543 6407 /* FIXME should make an annotation for this. */
112e8700
SM
6408 uiout->text ("\tstop only in thread ");
6409 if (uiout->is_mi_like_p ())
6410 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6411 else
6412 {
6413 struct thread_info *thr = find_thread_global_id (b->thread);
6414
112e8700 6415 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6416 }
112e8700 6417 uiout->text ("\n");
c4093a6a
JM
6418 }
6419
556ec64d
YQ
6420 if (!part_of_multiple)
6421 {
6422 if (b->hit_count)
31f56a27
YQ
6423 {
6424 /* FIXME should make an annotation for this. */
6425 if (is_catchpoint (b))
112e8700 6426 uiout->text ("\tcatchpoint");
31f56a27 6427 else if (is_tracepoint (b))
112e8700 6428 uiout->text ("\ttracepoint");
31f56a27 6429 else
112e8700
SM
6430 uiout->text ("\tbreakpoint");
6431 uiout->text (" already hit ");
6432 uiout->field_int ("times", b->hit_count);
31f56a27 6433 if (b->hit_count == 1)
112e8700 6434 uiout->text (" time\n");
31f56a27 6435 else
112e8700 6436 uiout->text (" times\n");
31f56a27 6437 }
556ec64d
YQ
6438 else
6439 {
31f56a27 6440 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6441 if (uiout->is_mi_like_p ())
6442 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6443 }
6444 }
8b93c638 6445
0d381245 6446 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6447 {
6448 annotate_field (8);
112e8700
SM
6449 uiout->text ("\tignore next ");
6450 uiout->field_int ("ignore", b->ignore_count);
6451 uiout->text (" hits\n");
c4093a6a 6452 }
059fb39f 6453
816338b5
SS
6454 /* Note that an enable count of 1 corresponds to "enable once"
6455 behavior, which is reported by the combination of enablement and
6456 disposition, so we don't need to mention it here. */
6457 if (!part_of_multiple && b->enable_count > 1)
6458 {
6459 annotate_field (8);
112e8700 6460 uiout->text ("\tdisable after ");
816338b5
SS
6461 /* Tweak the wording to clarify that ignore and enable counts
6462 are distinct, and have additive effect. */
6463 if (b->ignore_count)
112e8700 6464 uiout->text ("additional ");
816338b5 6465 else
112e8700
SM
6466 uiout->text ("next ");
6467 uiout->field_int ("enable", b->enable_count);
6468 uiout->text (" hits\n");
816338b5
SS
6469 }
6470
f196051f
SS
6471 if (!part_of_multiple && is_tracepoint (b))
6472 {
6473 struct tracepoint *tp = (struct tracepoint *) b;
6474
6475 if (tp->traceframe_usage)
6476 {
112e8700
SM
6477 uiout->text ("\ttrace buffer usage ");
6478 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6479 uiout->text (" bytes\n");
f196051f
SS
6480 }
6481 }
d3ce09f5 6482
d1b0a7bf 6483 l = b->commands ? b->commands.get () : NULL;
059fb39f 6484 if (!part_of_multiple && l)
c4093a6a
JM
6485 {
6486 annotate_field (9);
2e783024 6487 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6488 print_command_lines (uiout, l, 4);
c4093a6a 6489 }
d24317b4 6490
d9b3f62e 6491 if (is_tracepoint (b))
1042e4c0 6492 {
d9b3f62e
PA
6493 struct tracepoint *t = (struct tracepoint *) b;
6494
6495 if (!part_of_multiple && t->pass_count)
6496 {
6497 annotate_field (10);
112e8700
SM
6498 uiout->text ("\tpass count ");
6499 uiout->field_int ("pass", t->pass_count);
6500 uiout->text (" \n");
d9b3f62e 6501 }
f2a8bc8a
YQ
6502
6503 /* Don't display it when tracepoint or tracepoint location is
6504 pending. */
6505 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6506 {
6507 annotate_field (11);
6508
112e8700
SM
6509 if (uiout->is_mi_like_p ())
6510 uiout->field_string ("installed",
f2a8bc8a
YQ
6511 loc->inserted ? "y" : "n");
6512 else
6513 {
6514 if (loc->inserted)
112e8700 6515 uiout->text ("\t");
f2a8bc8a 6516 else
112e8700
SM
6517 uiout->text ("\tnot ");
6518 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6519 }
6520 }
1042e4c0
SS
6521 }
6522
112e8700 6523 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6524 {
3a5c3e22
PA
6525 if (is_watchpoint (b))
6526 {
6527 struct watchpoint *w = (struct watchpoint *) b;
6528
112e8700 6529 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6530 }
f00aae0f 6531 else if (b->location != NULL
d28cd78a 6532 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6533 uiout->field_string ("original-location",
d28cd78a 6534 event_location_to_string (b->location.get ()));
d24317b4 6535 }
c4093a6a 6536}
c5aa993b 6537
0d381245
VP
6538static void
6539print_one_breakpoint (struct breakpoint *b,
4a64f543 6540 struct bp_location **last_loc,
6c95b8df 6541 int allflag)
0d381245 6542{
79a45e25 6543 struct ui_out *uiout = current_uiout;
8d3788bd 6544
2e783024
TT
6545 {
6546 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6547
2e783024
TT
6548 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6549 }
0d381245
VP
6550
6551 /* If this breakpoint has custom print function,
6552 it's already printed. Otherwise, print individual
6553 locations, if any. */
6554 if (b->ops == NULL || b->ops->print_one == NULL)
6555 {
4a64f543
MS
6556 /* If breakpoint has a single location that is disabled, we
6557 print it as if it had several locations, since otherwise it's
6558 hard to represent "breakpoint enabled, location disabled"
6559 situation.
6560
6561 Note that while hardware watchpoints have several locations
a3be7890 6562 internally, that's not a property exposed to user. */
0d381245 6563 if (b->loc
a5606eee 6564 && !is_hardware_watchpoint (b)
8d3788bd 6565 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6566 {
6567 struct bp_location *loc;
6568 int n = 1;
8d3788bd 6569
0d381245 6570 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6571 {
2e783024 6572 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6573 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6574 }
0d381245
VP
6575 }
6576 }
6577}
6578
a6d9a66e
UW
6579static int
6580breakpoint_address_bits (struct breakpoint *b)
6581{
6582 int print_address_bits = 0;
6583 struct bp_location *loc;
6584
c6d81124
PA
6585 /* Software watchpoints that aren't watching memory don't have an
6586 address to print. */
6587 if (is_no_memory_software_watchpoint (b))
6588 return 0;
6589
a6d9a66e
UW
6590 for (loc = b->loc; loc; loc = loc->next)
6591 {
c7437ca6
PA
6592 int addr_bit;
6593
c7437ca6 6594 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6595 if (addr_bit > print_address_bits)
6596 print_address_bits = addr_bit;
6597 }
6598
6599 return print_address_bits;
6600}
0d381245 6601
c4093a6a
JM
6602struct captured_breakpoint_query_args
6603 {
6604 int bnum;
6605 };
c5aa993b 6606
c4093a6a 6607static int
2b65245e 6608do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6609{
9a3c8263
SM
6610 struct captured_breakpoint_query_args *args
6611 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6612 struct breakpoint *b;
a6d9a66e 6613 struct bp_location *dummy_loc = NULL;
cc59ec59 6614
c4093a6a
JM
6615 ALL_BREAKPOINTS (b)
6616 {
6617 if (args->bnum == b->number)
c5aa993b 6618 {
12c5a436 6619 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6620 return GDB_RC_OK;
c5aa993b 6621 }
c4093a6a
JM
6622 }
6623 return GDB_RC_NONE;
6624}
c5aa993b 6625
c4093a6a 6626enum gdb_rc
4a64f543
MS
6627gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6628 char **error_message)
c4093a6a
JM
6629{
6630 struct captured_breakpoint_query_args args;
cc59ec59 6631
c4093a6a
JM
6632 args.bnum = bnum;
6633 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6634 an error. */
b0b13bb4
DJ
6635 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6636 error_message, RETURN_MASK_ALL) < 0)
6637 return GDB_RC_FAIL;
6638 else
6639 return GDB_RC_OK;
c4093a6a 6640}
c5aa993b 6641
09d682a4
TT
6642/* Return true if this breakpoint was set by the user, false if it is
6643 internal or momentary. */
6644
6645int
6646user_breakpoint_p (struct breakpoint *b)
6647{
46c6471b 6648 return b->number > 0;
09d682a4
TT
6649}
6650
93daf339
TT
6651/* See breakpoint.h. */
6652
6653int
6654pending_breakpoint_p (struct breakpoint *b)
6655{
6656 return b->loc == NULL;
6657}
6658
7f3b0473 6659/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6660 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6661 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6662 FILTER is non-NULL, call it on each breakpoint and only include the
6663 ones for which it returns non-zero. Return the total number of
6664 breakpoints listed. */
c906108c 6665
d77f58be 6666static int
4495129a 6667breakpoint_1 (const char *args, int allflag,
4a64f543 6668 int (*filter) (const struct breakpoint *))
c4093a6a 6669{
52f0bd74 6670 struct breakpoint *b;
a6d9a66e 6671 struct bp_location *last_loc = NULL;
7f3b0473 6672 int nr_printable_breakpoints;
79a45b7d 6673 struct value_print_options opts;
a6d9a66e 6674 int print_address_bits = 0;
269b11a2 6675 int print_type_col_width = 14;
79a45e25 6676 struct ui_out *uiout = current_uiout;
269b11a2 6677
79a45b7d
TT
6678 get_user_print_options (&opts);
6679
4a64f543
MS
6680 /* Compute the number of rows in the table, as well as the size
6681 required for address fields. */
7f3b0473
AC
6682 nr_printable_breakpoints = 0;
6683 ALL_BREAKPOINTS (b)
e5a67952
MS
6684 {
6685 /* If we have a filter, only list the breakpoints it accepts. */
6686 if (filter && !filter (b))
6687 continue;
6688
6689 /* If we have an "args" string, it is a list of breakpoints to
6690 accept. Skip the others. */
6691 if (args != NULL && *args != '\0')
6692 {
6693 if (allflag && parse_and_eval_long (args) != b->number)
6694 continue;
6695 if (!allflag && !number_is_in_list (args, b->number))
6696 continue;
6697 }
269b11a2 6698
e5a67952
MS
6699 if (allflag || user_breakpoint_p (b))
6700 {
6701 int addr_bit, type_len;
a6d9a66e 6702
e5a67952
MS
6703 addr_bit = breakpoint_address_bits (b);
6704 if (addr_bit > print_address_bits)
6705 print_address_bits = addr_bit;
269b11a2 6706
e5a67952
MS
6707 type_len = strlen (bptype_string (b->type));
6708 if (type_len > print_type_col_width)
6709 print_type_col_width = type_len;
6710
6711 nr_printable_breakpoints++;
6712 }
6713 }
7f3b0473 6714
4a2b031d
TT
6715 {
6716 ui_out_emit_table table_emitter (uiout,
6717 opts.addressprint ? 6 : 5,
6718 nr_printable_breakpoints,
6719 "BreakpointTable");
6720
6721 if (nr_printable_breakpoints > 0)
6722 annotate_breakpoints_headers ();
6723 if (nr_printable_breakpoints > 0)
6724 annotate_field (0);
6725 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (1);
6728 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (2);
6731 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6732 if (nr_printable_breakpoints > 0)
6733 annotate_field (3);
6734 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6735 if (opts.addressprint)
6736 {
6737 if (nr_printable_breakpoints > 0)
6738 annotate_field (4);
6739 if (print_address_bits <= 32)
6740 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6741 else
6742 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6743 }
6744 if (nr_printable_breakpoints > 0)
6745 annotate_field (5);
6746 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6747 uiout->table_body ();
6748 if (nr_printable_breakpoints > 0)
6749 annotate_breakpoints_table ();
6750
6751 ALL_BREAKPOINTS (b)
6752 {
6753 QUIT;
6754 /* If we have a filter, only list the breakpoints it accepts. */
6755 if (filter && !filter (b))
6756 continue;
e5a67952 6757
4a2b031d
TT
6758 /* If we have an "args" string, it is a list of breakpoints to
6759 accept. Skip the others. */
e5a67952 6760
4a2b031d
TT
6761 if (args != NULL && *args != '\0')
6762 {
6763 if (allflag) /* maintenance info breakpoint */
6764 {
6765 if (parse_and_eval_long (args) != b->number)
6766 continue;
6767 }
6768 else /* all others */
6769 {
6770 if (!number_is_in_list (args, b->number))
6771 continue;
6772 }
6773 }
6774 /* We only print out user settable breakpoints unless the
6775 allflag is set. */
6776 if (allflag || user_breakpoint_p (b))
6777 print_one_breakpoint (b, &last_loc, allflag);
6778 }
6779 }
698384cd 6780
7f3b0473 6781 if (nr_printable_breakpoints == 0)
c906108c 6782 {
4a64f543
MS
6783 /* If there's a filter, let the caller decide how to report
6784 empty list. */
d77f58be
SS
6785 if (!filter)
6786 {
e5a67952 6787 if (args == NULL || *args == '\0')
112e8700 6788 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6789 else
112e8700 6790 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6791 args);
d77f58be 6792 }
c906108c
SS
6793 }
6794 else
c4093a6a 6795 {
a6d9a66e
UW
6796 if (last_loc && !server_command)
6797 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6798 }
c906108c 6799
4a64f543 6800 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6801 there have been breakpoints? */
c906108c 6802 annotate_breakpoints_table_end ();
d77f58be
SS
6803
6804 return nr_printable_breakpoints;
c906108c
SS
6805}
6806
ad443146
SS
6807/* Display the value of default-collect in a way that is generally
6808 compatible with the breakpoint list. */
6809
6810static void
6811default_collect_info (void)
6812{
79a45e25
PA
6813 struct ui_out *uiout = current_uiout;
6814
ad443146
SS
6815 /* If it has no value (which is frequently the case), say nothing; a
6816 message like "No default-collect." gets in user's face when it's
6817 not wanted. */
6818 if (!*default_collect)
6819 return;
6820
6821 /* The following phrase lines up nicely with per-tracepoint collect
6822 actions. */
112e8700
SM
6823 uiout->text ("default collect ");
6824 uiout->field_string ("default-collect", default_collect);
6825 uiout->text (" \n");
ad443146
SS
6826}
6827
c906108c 6828static void
11db9430 6829info_breakpoints_command (char *args, int from_tty)
c906108c 6830{
e5a67952 6831 breakpoint_1 (args, 0, NULL);
ad443146
SS
6832
6833 default_collect_info ();
d77f58be
SS
6834}
6835
6836static void
11db9430 6837info_watchpoints_command (char *args, int from_tty)
d77f58be 6838{
e5a67952 6839 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6840 struct ui_out *uiout = current_uiout;
d77f58be
SS
6841
6842 if (num_printed == 0)
6843 {
e5a67952 6844 if (args == NULL || *args == '\0')
112e8700 6845 uiout->message ("No watchpoints.\n");
d77f58be 6846 else
112e8700 6847 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6848 }
c906108c
SS
6849}
6850
7a292a7a 6851static void
4495129a 6852maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6853{
e5a67952 6854 breakpoint_1 (args, 1, NULL);
ad443146
SS
6855
6856 default_collect_info ();
c906108c
SS
6857}
6858
0d381245 6859static int
714835d5 6860breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6861 struct program_space *pspace,
714835d5 6862 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6863{
6864 struct bp_location *bl = b->loc;
cc59ec59 6865
0d381245
VP
6866 for (; bl; bl = bl->next)
6867 {
6c95b8df
PA
6868 if (bl->pspace == pspace
6869 && bl->address == pc
0d381245
VP
6870 && (!overlay_debugging || bl->section == section))
6871 return 1;
6872 }
6873 return 0;
6874}
6875
672f9b60 6876/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6877 concerns with logical breakpoints, so we match program spaces, not
6878 address spaces. */
c906108c
SS
6879
6880static void
6c95b8df
PA
6881describe_other_breakpoints (struct gdbarch *gdbarch,
6882 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6883 struct obj_section *section, int thread)
c906108c 6884{
52f0bd74
AC
6885 int others = 0;
6886 struct breakpoint *b;
c906108c
SS
6887
6888 ALL_BREAKPOINTS (b)
672f9b60
KP
6889 others += (user_breakpoint_p (b)
6890 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6891 if (others > 0)
6892 {
a3f17187
AC
6893 if (others == 1)
6894 printf_filtered (_("Note: breakpoint "));
6895 else /* if (others == ???) */
6896 printf_filtered (_("Note: breakpoints "));
c906108c 6897 ALL_BREAKPOINTS (b)
672f9b60 6898 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6899 {
6900 others--;
6901 printf_filtered ("%d", b->number);
6902 if (b->thread == -1 && thread != -1)
6903 printf_filtered (" (all threads)");
6904 else if (b->thread != -1)
6905 printf_filtered (" (thread %d)", b->thread);
6906 printf_filtered ("%s%s ",
059fb39f 6907 ((b->enable_state == bp_disabled
f8eba3c6 6908 || b->enable_state == bp_call_disabled)
0d381245 6909 ? " (disabled)"
0d381245
VP
6910 : ""),
6911 (others > 1) ? ","
6912 : ((others == 1) ? " and" : ""));
6913 }
a3f17187 6914 printf_filtered (_("also set at pc "));
5af949e3 6915 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6916 printf_filtered (".\n");
6917 }
6918}
6919\f
c906108c 6920
e4f237da 6921/* Return true iff it is meaningful to use the address member of
244558af
LM
6922 BPT locations. For some breakpoint types, the locations' address members
6923 are irrelevant and it makes no sense to attempt to compare them to other
6924 addresses (or use them for any other purpose either).
e4f237da 6925
4a64f543 6926 More specifically, each of the following breakpoint types will
244558af 6927 always have a zero valued location address and we don't want to mark
4a64f543 6928 breakpoints of any of these types to be a duplicate of an actual
244558af 6929 breakpoint location at address zero:
e4f237da
KB
6930
6931 bp_watchpoint
2d134ed3
PA
6932 bp_catchpoint
6933
6934*/
e4f237da
KB
6935
6936static int
6937breakpoint_address_is_meaningful (struct breakpoint *bpt)
6938{
6939 enum bptype type = bpt->type;
6940
2d134ed3
PA
6941 return (type != bp_watchpoint && type != bp_catchpoint);
6942}
6943
6944/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6945 true if LOC1 and LOC2 represent the same watchpoint location. */
6946
6947static int
4a64f543
MS
6948watchpoint_locations_match (struct bp_location *loc1,
6949 struct bp_location *loc2)
2d134ed3 6950{
3a5c3e22
PA
6951 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6952 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6953
6954 /* Both of them must exist. */
6955 gdb_assert (w1 != NULL);
6956 gdb_assert (w2 != NULL);
2bdf28a0 6957
4a64f543
MS
6958 /* If the target can evaluate the condition expression in hardware,
6959 then we we need to insert both watchpoints even if they are at
6960 the same place. Otherwise the watchpoint will only trigger when
6961 the condition of whichever watchpoint was inserted evaluates to
6962 true, not giving a chance for GDB to check the condition of the
6963 other watchpoint. */
3a5c3e22 6964 if ((w1->cond_exp
4a64f543
MS
6965 && target_can_accel_watchpoint_condition (loc1->address,
6966 loc1->length,
0cf6dd15 6967 loc1->watchpoint_type,
4d01a485 6968 w1->cond_exp.get ()))
3a5c3e22 6969 || (w2->cond_exp
4a64f543
MS
6970 && target_can_accel_watchpoint_condition (loc2->address,
6971 loc2->length,
0cf6dd15 6972 loc2->watchpoint_type,
4d01a485 6973 w2->cond_exp.get ())))
0cf6dd15
TJB
6974 return 0;
6975
85d721b8
PA
6976 /* Note that this checks the owner's type, not the location's. In
6977 case the target does not support read watchpoints, but does
6978 support access watchpoints, we'll have bp_read_watchpoint
6979 watchpoints with hw_access locations. Those should be considered
6980 duplicates of hw_read locations. The hw_read locations will
6981 become hw_access locations later. */
2d134ed3
PA
6982 return (loc1->owner->type == loc2->owner->type
6983 && loc1->pspace->aspace == loc2->pspace->aspace
6984 && loc1->address == loc2->address
6985 && loc1->length == loc2->length);
e4f237da
KB
6986}
6987
31e77af2 6988/* See breakpoint.h. */
6c95b8df 6989
31e77af2 6990int
6c95b8df
PA
6991breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6992 struct address_space *aspace2, CORE_ADDR addr2)
6993{
f5656ead 6994 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6995 || aspace1 == aspace2)
6996 && addr1 == addr2);
6997}
6998
f1310107
TJB
6999/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7000 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7001 matches ASPACE2. On targets that have global breakpoints, the address
7002 space doesn't really matter. */
7003
7004static int
7005breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7006 int len1, struct address_space *aspace2,
7007 CORE_ADDR addr2)
7008{
f5656ead 7009 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7010 || aspace1 == aspace2)
7011 && addr2 >= addr1 && addr2 < addr1 + len1);
7012}
7013
7014/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7015 a ranged breakpoint. In most targets, a match happens only if ASPACE
7016 matches the breakpoint's address space. On targets that have global
7017 breakpoints, the address space doesn't really matter. */
7018
7019static int
7020breakpoint_location_address_match (struct bp_location *bl,
7021 struct address_space *aspace,
7022 CORE_ADDR addr)
7023{
7024 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7025 aspace, addr)
7026 || (bl->length
7027 && breakpoint_address_match_range (bl->pspace->aspace,
7028 bl->address, bl->length,
7029 aspace, addr)));
7030}
7031
d35ae833
PA
7032/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7033 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7034 match happens only if ASPACE matches the breakpoint's address
7035 space. On targets that have global breakpoints, the address space
7036 doesn't really matter. */
7037
7038static int
7039breakpoint_location_address_range_overlap (struct bp_location *bl,
7040 struct address_space *aspace,
7041 CORE_ADDR addr, int len)
7042{
7043 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7044 || bl->pspace->aspace == aspace)
7045 {
7046 int bl_len = bl->length != 0 ? bl->length : 1;
7047
7048 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7049 return 1;
7050 }
7051 return 0;
7052}
7053
1e4d1764
YQ
7054/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7055 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7056 true, otherwise returns false. */
7057
7058static int
7059tracepoint_locations_match (struct bp_location *loc1,
7060 struct bp_location *loc2)
7061{
7062 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7063 /* Since tracepoint locations are never duplicated with others', tracepoint
7064 locations at the same address of different tracepoints are regarded as
7065 different locations. */
7066 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7067 else
7068 return 0;
7069}
7070
2d134ed3
PA
7071/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7072 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7073 represent the same location. */
7074
7075static int
4a64f543
MS
7076breakpoint_locations_match (struct bp_location *loc1,
7077 struct bp_location *loc2)
2d134ed3 7078{
2bdf28a0
JK
7079 int hw_point1, hw_point2;
7080
7081 /* Both of them must not be in moribund_locations. */
7082 gdb_assert (loc1->owner != NULL);
7083 gdb_assert (loc2->owner != NULL);
7084
7085 hw_point1 = is_hardware_watchpoint (loc1->owner);
7086 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7087
7088 if (hw_point1 != hw_point2)
7089 return 0;
7090 else if (hw_point1)
7091 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7092 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7093 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7094 else
f1310107
TJB
7095 /* We compare bp_location.length in order to cover ranged breakpoints. */
7096 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7097 loc2->pspace->aspace, loc2->address)
7098 && loc1->length == loc2->length);
2d134ed3
PA
7099}
7100
76897487
KB
7101static void
7102breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7103 int bnum, int have_bnum)
7104{
f63fbe86
MS
7105 /* The longest string possibly returned by hex_string_custom
7106 is 50 chars. These must be at least that big for safety. */
7107 char astr1[64];
7108 char astr2[64];
76897487 7109
bb599908
PH
7110 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7111 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7112 if (have_bnum)
8a3fe4f8 7113 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7114 bnum, astr1, astr2);
7115 else
8a3fe4f8 7116 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7117}
7118
4a64f543
MS
7119/* Adjust a breakpoint's address to account for architectural
7120 constraints on breakpoint placement. Return the adjusted address.
7121 Note: Very few targets require this kind of adjustment. For most
7122 targets, this function is simply the identity function. */
76897487
KB
7123
7124static CORE_ADDR
a6d9a66e
UW
7125adjust_breakpoint_address (struct gdbarch *gdbarch,
7126 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7127{
a6d9a66e 7128 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7129 {
7130 /* Very few targets need any kind of breakpoint adjustment. */
7131 return bpaddr;
7132 }
88f7da05
KB
7133 else if (bptype == bp_watchpoint
7134 || bptype == bp_hardware_watchpoint
7135 || bptype == bp_read_watchpoint
7136 || bptype == bp_access_watchpoint
fe798b75 7137 || bptype == bp_catchpoint)
88f7da05
KB
7138 {
7139 /* Watchpoints and the various bp_catch_* eventpoints should not
7140 have their addresses modified. */
7141 return bpaddr;
7142 }
7c16b83e
PA
7143 else if (bptype == bp_single_step)
7144 {
7145 /* Single-step breakpoints should not have their addresses
7146 modified. If there's any architectural constrain that
7147 applies to this address, then it should have already been
7148 taken into account when the breakpoint was created in the
7149 first place. If we didn't do this, stepping through e.g.,
7150 Thumb-2 IT blocks would break. */
7151 return bpaddr;
7152 }
76897487
KB
7153 else
7154 {
7155 CORE_ADDR adjusted_bpaddr;
7156
7157 /* Some targets have architectural constraints on the placement
7158 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7159 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7160
7161 /* An adjusted breakpoint address can significantly alter
7162 a user's expectations. Print a warning if an adjustment
7163 is required. */
7164 if (adjusted_bpaddr != bpaddr)
7165 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7166
7167 return adjusted_bpaddr;
7168 }
7169}
7170
5625a286 7171bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7172{
5625a286 7173 bp_location *loc = this;
7cc221ef 7174
348d480f
PA
7175 gdb_assert (ops != NULL);
7176
28010a5d
PA
7177 loc->ops = ops;
7178 loc->owner = owner;
b775012e 7179 loc->cond_bytecode = NULL;
0d381245
VP
7180 loc->shlib_disabled = 0;
7181 loc->enabled = 1;
e049a4b5 7182
28010a5d 7183 switch (owner->type)
e049a4b5
DJ
7184 {
7185 case bp_breakpoint:
7c16b83e 7186 case bp_single_step:
e049a4b5
DJ
7187 case bp_until:
7188 case bp_finish:
7189 case bp_longjmp:
7190 case bp_longjmp_resume:
e2e4d78b 7191 case bp_longjmp_call_dummy:
186c406b
TT
7192 case bp_exception:
7193 case bp_exception_resume:
e049a4b5 7194 case bp_step_resume:
2c03e5be 7195 case bp_hp_step_resume:
e049a4b5
DJ
7196 case bp_watchpoint_scope:
7197 case bp_call_dummy:
aa7d318d 7198 case bp_std_terminate:
e049a4b5
DJ
7199 case bp_shlib_event:
7200 case bp_thread_event:
7201 case bp_overlay_event:
4efc6507 7202 case bp_jit_event:
0fd8e87f 7203 case bp_longjmp_master:
aa7d318d 7204 case bp_std_terminate_master:
186c406b 7205 case bp_exception_master:
0e30163f
JK
7206 case bp_gnu_ifunc_resolver:
7207 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7208 case bp_dprintf:
e049a4b5 7209 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7210 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7211 break;
7212 case bp_hardware_breakpoint:
7213 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7214 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7215 break;
7216 case bp_hardware_watchpoint:
7217 case bp_read_watchpoint:
7218 case bp_access_watchpoint:
7219 loc->loc_type = bp_loc_hardware_watchpoint;
7220 break;
7221 case bp_watchpoint:
ce78b96d 7222 case bp_catchpoint:
15c3d785
PA
7223 case bp_tracepoint:
7224 case bp_fast_tracepoint:
0fb4aa4b 7225 case bp_static_tracepoint:
e049a4b5
DJ
7226 loc->loc_type = bp_loc_other;
7227 break;
7228 default:
e2e0b3e5 7229 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7230 }
7231
f431efe5 7232 loc->refc = 1;
28010a5d
PA
7233}
7234
7235/* Allocate a struct bp_location. */
7236
7237static struct bp_location *
7238allocate_bp_location (struct breakpoint *bpt)
7239{
348d480f
PA
7240 return bpt->ops->allocate_location (bpt);
7241}
7cc221ef 7242
f431efe5
PA
7243static void
7244free_bp_location (struct bp_location *loc)
fe3f5fa8 7245{
348d480f 7246 loc->ops->dtor (loc);
4d01a485 7247 delete loc;
fe3f5fa8
VP
7248}
7249
f431efe5
PA
7250/* Increment reference count. */
7251
7252static void
7253incref_bp_location (struct bp_location *bl)
7254{
7255 ++bl->refc;
7256}
7257
7258/* Decrement reference count. If the reference count reaches 0,
7259 destroy the bp_location. Sets *BLP to NULL. */
7260
7261static void
7262decref_bp_location (struct bp_location **blp)
7263{
0807b50c
PA
7264 gdb_assert ((*blp)->refc > 0);
7265
f431efe5
PA
7266 if (--(*blp)->refc == 0)
7267 free_bp_location (*blp);
7268 *blp = NULL;
7269}
7270
346774a9 7271/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7272
b270e6f9
TT
7273static breakpoint *
7274add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7275{
346774a9 7276 struct breakpoint *b1;
b270e6f9 7277 struct breakpoint *result = b.get ();
c906108c 7278
346774a9
PA
7279 /* Add this breakpoint to the end of the chain so that a list of
7280 breakpoints will come out in order of increasing numbers. */
7281
7282 b1 = breakpoint_chain;
7283 if (b1 == 0)
b270e6f9 7284 breakpoint_chain = b.release ();
346774a9
PA
7285 else
7286 {
7287 while (b1->next)
7288 b1 = b1->next;
b270e6f9 7289 b1->next = b.release ();
346774a9 7290 }
b270e6f9
TT
7291
7292 return result;
346774a9
PA
7293}
7294
7295/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7296
7297static void
7298init_raw_breakpoint_without_location (struct breakpoint *b,
7299 struct gdbarch *gdbarch,
28010a5d 7300 enum bptype bptype,
c0a91b2b 7301 const struct breakpoint_ops *ops)
346774a9 7302{
348d480f
PA
7303 gdb_assert (ops != NULL);
7304
28010a5d 7305 b->ops = ops;
4d28f7a8 7306 b->type = bptype;
a6d9a66e 7307 b->gdbarch = gdbarch;
c906108c
SS
7308 b->language = current_language->la_language;
7309 b->input_radix = input_radix;
d0fb5eae 7310 b->related_breakpoint = b;
346774a9
PA
7311}
7312
7313/* Helper to set_raw_breakpoint below. Creates a breakpoint
7314 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7315
7316static struct breakpoint *
7317set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7318 enum bptype bptype,
c0a91b2b 7319 const struct breakpoint_ops *ops)
346774a9 7320{
3b0871f4 7321 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7322
3b0871f4 7323 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7324 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7325}
7326
0e30163f
JK
7327/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7328 resolutions should be made as the user specified the location explicitly
7329 enough. */
7330
0d381245 7331static void
0e30163f 7332set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7333{
2bdf28a0
JK
7334 gdb_assert (loc->owner != NULL);
7335
0d381245 7336 if (loc->owner->type == bp_breakpoint
1042e4c0 7337 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7338 || is_tracepoint (loc->owner))
0d381245 7339 {
0e30163f 7340 int is_gnu_ifunc;
2c02bd72 7341 const char *function_name;
6a3a010b 7342 CORE_ADDR func_addr;
0e30163f 7343
2c02bd72 7344 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7345 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7346
7347 if (is_gnu_ifunc && !explicit_loc)
7348 {
7349 struct breakpoint *b = loc->owner;
7350
7351 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7352 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7353 &loc->requested_address))
7354 {
7355 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7356 loc->address = adjust_breakpoint_address (loc->gdbarch,
7357 loc->requested_address,
7358 b->type);
7359 }
7360 else if (b->type == bp_breakpoint && b->loc == loc
7361 && loc->next == NULL && b->related_breakpoint == b)
7362 {
7363 /* Create only the whole new breakpoint of this type but do not
7364 mess more complicated breakpoints with multiple locations. */
7365 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7366 /* Remember the resolver's address for use by the return
7367 breakpoint. */
7368 loc->related_address = func_addr;
0e30163f
JK
7369 }
7370 }
7371
2c02bd72
DE
7372 if (function_name)
7373 loc->function_name = xstrdup (function_name);
0d381245
VP
7374 }
7375}
7376
a6d9a66e 7377/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7378struct gdbarch *
a6d9a66e
UW
7379get_sal_arch (struct symtab_and_line sal)
7380{
7381 if (sal.section)
7382 return get_objfile_arch (sal.section->objfile);
7383 if (sal.symtab)
eb822aa6 7384 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7385
7386 return NULL;
7387}
7388
346774a9
PA
7389/* Low level routine for partially initializing a breakpoint of type
7390 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7391 file name, and line number are provided by SAL.
0d381245
VP
7392
7393 It is expected that the caller will complete the initialization of
7394 the newly created breakpoint struct as well as output any status
c56053d2 7395 information regarding the creation of a new breakpoint. */
0d381245 7396
346774a9
PA
7397static void
7398init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7399 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7400 const struct breakpoint_ops *ops)
0d381245 7401{
28010a5d 7402 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7403
3742cc8b 7404 add_location_to_breakpoint (b, &sal);
0d381245 7405
6c95b8df
PA
7406 if (bptype != bp_catchpoint)
7407 gdb_assert (sal.pspace != NULL);
7408
f8eba3c6
TT
7409 /* Store the program space that was used to set the breakpoint,
7410 except for ordinary breakpoints, which are independent of the
7411 program space. */
7412 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7413 b->pspace = sal.pspace;
346774a9 7414}
c906108c 7415
346774a9
PA
7416/* set_raw_breakpoint is a low level routine for allocating and
7417 partially initializing a breakpoint of type BPTYPE. The newly
7418 created breakpoint's address, section, source file name, and line
7419 number are provided by SAL. The newly created and partially
7420 initialized breakpoint is added to the breakpoint chain and
7421 is also returned as the value of this function.
7422
7423 It is expected that the caller will complete the initialization of
7424 the newly created breakpoint struct as well as output any status
7425 information regarding the creation of a new breakpoint. In
7426 particular, set_raw_breakpoint does NOT set the breakpoint
7427 number! Care should be taken to not allow an error to occur
7428 prior to completing the initialization of the breakpoint. If this
7429 should happen, a bogus breakpoint will be left on the chain. */
7430
7431struct breakpoint *
7432set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7433 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7434 const struct breakpoint_ops *ops)
346774a9 7435{
3b0871f4 7436 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7437
3b0871f4 7438 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7439 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7440}
7441
53a5351d 7442/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7443 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7444 initiated the operation. */
c906108c
SS
7445
7446void
186c406b 7447set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7448{
35df4500 7449 struct breakpoint *b, *b_tmp;
5d5658a1 7450 int thread = tp->global_num;
0fd8e87f
UW
7451
7452 /* To avoid having to rescan all objfile symbols at every step,
7453 we maintain a list of continually-inserted but always disabled
7454 longjmp "master" breakpoints. Here, we simply create momentary
7455 clones of those and enable them for the requested thread. */
35df4500 7456 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7457 if (b->pspace == current_program_space
186c406b
TT
7458 && (b->type == bp_longjmp_master
7459 || b->type == bp_exception_master))
0fd8e87f 7460 {
06edf0c0
PA
7461 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7462 struct breakpoint *clone;
cc59ec59 7463
e2e4d78b
JK
7464 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7465 after their removal. */
06edf0c0 7466 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7467 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7468 clone->thread = thread;
7469 }
186c406b
TT
7470
7471 tp->initiating_frame = frame;
c906108c
SS
7472}
7473
611c83ae 7474/* Delete all longjmp breakpoints from THREAD. */
c906108c 7475void
611c83ae 7476delete_longjmp_breakpoint (int thread)
c906108c 7477{
35df4500 7478 struct breakpoint *b, *b_tmp;
c906108c 7479
35df4500 7480 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7481 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7482 {
7483 if (b->thread == thread)
7484 delete_breakpoint (b);
7485 }
c906108c
SS
7486}
7487
f59f708a
PA
7488void
7489delete_longjmp_breakpoint_at_next_stop (int thread)
7490{
7491 struct breakpoint *b, *b_tmp;
7492
7493 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7494 if (b->type == bp_longjmp || b->type == bp_exception)
7495 {
7496 if (b->thread == thread)
7497 b->disposition = disp_del_at_next_stop;
7498 }
7499}
7500
e2e4d78b
JK
7501/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7502 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7503 pointer to any of them. Return NULL if this system cannot place longjmp
7504 breakpoints. */
7505
7506struct breakpoint *
7507set_longjmp_breakpoint_for_call_dummy (void)
7508{
7509 struct breakpoint *b, *retval = NULL;
7510
7511 ALL_BREAKPOINTS (b)
7512 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7513 {
7514 struct breakpoint *new_b;
7515
7516 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7517 &momentary_breakpoint_ops,
7518 1);
5d5658a1 7519 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7520
7521 /* Link NEW_B into the chain of RETVAL breakpoints. */
7522
7523 gdb_assert (new_b->related_breakpoint == new_b);
7524 if (retval == NULL)
7525 retval = new_b;
7526 new_b->related_breakpoint = retval;
7527 while (retval->related_breakpoint != new_b->related_breakpoint)
7528 retval = retval->related_breakpoint;
7529 retval->related_breakpoint = new_b;
7530 }
7531
7532 return retval;
7533}
7534
7535/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7536 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7537 stack.
7538
7539 You should call this function only at places where it is safe to currently
7540 unwind the whole stack. Failed stack unwind would discard live dummy
7541 frames. */
7542
7543void
b67a2c6f 7544check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7545{
7546 struct breakpoint *b, *b_tmp;
7547
7548 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7549 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7550 {
7551 struct breakpoint *dummy_b = b->related_breakpoint;
7552
7553 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7554 dummy_b = dummy_b->related_breakpoint;
7555 if (dummy_b->type != bp_call_dummy
7556 || frame_find_by_id (dummy_b->frame_id) != NULL)
7557 continue;
7558
b67a2c6f 7559 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7560
7561 while (b->related_breakpoint != b)
7562 {
7563 if (b_tmp == b->related_breakpoint)
7564 b_tmp = b->related_breakpoint->next;
7565 delete_breakpoint (b->related_breakpoint);
7566 }
7567 delete_breakpoint (b);
7568 }
7569}
7570
1900040c
MS
7571void
7572enable_overlay_breakpoints (void)
7573{
52f0bd74 7574 struct breakpoint *b;
1900040c
MS
7575
7576 ALL_BREAKPOINTS (b)
7577 if (b->type == bp_overlay_event)
7578 {
7579 b->enable_state = bp_enabled;
44702360 7580 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7581 overlay_events_enabled = 1;
1900040c
MS
7582 }
7583}
7584
7585void
7586disable_overlay_breakpoints (void)
7587{
52f0bd74 7588 struct breakpoint *b;
1900040c
MS
7589
7590 ALL_BREAKPOINTS (b)
7591 if (b->type == bp_overlay_event)
7592 {
7593 b->enable_state = bp_disabled;
44702360 7594 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7595 overlay_events_enabled = 0;
1900040c
MS
7596 }
7597}
7598
aa7d318d
TT
7599/* Set an active std::terminate breakpoint for each std::terminate
7600 master breakpoint. */
7601void
7602set_std_terminate_breakpoint (void)
7603{
35df4500 7604 struct breakpoint *b, *b_tmp;
aa7d318d 7605
35df4500 7606 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7607 if (b->pspace == current_program_space
7608 && b->type == bp_std_terminate_master)
7609 {
06edf0c0 7610 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7611 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7612 }
7613}
7614
7615/* Delete all the std::terminate breakpoints. */
7616void
7617delete_std_terminate_breakpoint (void)
7618{
35df4500 7619 struct breakpoint *b, *b_tmp;
aa7d318d 7620
35df4500 7621 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7622 if (b->type == bp_std_terminate)
7623 delete_breakpoint (b);
7624}
7625
c4093a6a 7626struct breakpoint *
a6d9a66e 7627create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7628{
7629 struct breakpoint *b;
c4093a6a 7630
06edf0c0
PA
7631 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7632 &internal_breakpoint_ops);
7633
b5de0fa7 7634 b->enable_state = bp_enabled;
f00aae0f 7635 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7636 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7637
44702360 7638 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7639
c4093a6a
JM
7640 return b;
7641}
7642
0101ce28
JJ
7643struct lang_and_radix
7644 {
7645 enum language lang;
7646 int radix;
7647 };
7648
4efc6507
DE
7649/* Create a breakpoint for JIT code registration and unregistration. */
7650
7651struct breakpoint *
7652create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7653{
2a7f3dff
PA
7654 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7655 &internal_breakpoint_ops);
4efc6507 7656}
0101ce28 7657
03673fc7
PP
7658/* Remove JIT code registration and unregistration breakpoint(s). */
7659
7660void
7661remove_jit_event_breakpoints (void)
7662{
7663 struct breakpoint *b, *b_tmp;
7664
7665 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7666 if (b->type == bp_jit_event
7667 && b->loc->pspace == current_program_space)
7668 delete_breakpoint (b);
7669}
7670
cae688ec
JJ
7671void
7672remove_solib_event_breakpoints (void)
7673{
35df4500 7674 struct breakpoint *b, *b_tmp;
cae688ec 7675
35df4500 7676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7677 if (b->type == bp_shlib_event
7678 && b->loc->pspace == current_program_space)
cae688ec
JJ
7679 delete_breakpoint (b);
7680}
7681
f37f681c
PA
7682/* See breakpoint.h. */
7683
7684void
7685remove_solib_event_breakpoints_at_next_stop (void)
7686{
7687 struct breakpoint *b, *b_tmp;
7688
7689 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7690 if (b->type == bp_shlib_event
7691 && b->loc->pspace == current_program_space)
7692 b->disposition = disp_del_at_next_stop;
7693}
7694
04086b45
PA
7695/* Helper for create_solib_event_breakpoint /
7696 create_and_insert_solib_event_breakpoint. Allows specifying which
7697 INSERT_MODE to pass through to update_global_location_list. */
7698
7699static struct breakpoint *
7700create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7701 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7702{
7703 struct breakpoint *b;
7704
06edf0c0
PA
7705 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7706 &internal_breakpoint_ops);
04086b45 7707 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7708 return b;
7709}
7710
04086b45
PA
7711struct breakpoint *
7712create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7713{
7714 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7715}
7716
f37f681c
PA
7717/* See breakpoint.h. */
7718
7719struct breakpoint *
7720create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7721{
7722 struct breakpoint *b;
7723
04086b45
PA
7724 /* Explicitly tell update_global_location_list to insert
7725 locations. */
7726 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7727 if (!b->loc->inserted)
7728 {
7729 delete_breakpoint (b);
7730 return NULL;
7731 }
7732 return b;
7733}
7734
cae688ec
JJ
7735/* Disable any breakpoints that are on code in shared libraries. Only
7736 apply to enabled breakpoints, disabled ones can just stay disabled. */
7737
7738void
cb851954 7739disable_breakpoints_in_shlibs (void)
cae688ec 7740{
876fa593 7741 struct bp_location *loc, **locp_tmp;
cae688ec 7742
876fa593 7743 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7744 {
2bdf28a0 7745 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7746 struct breakpoint *b = loc->owner;
2bdf28a0 7747
4a64f543
MS
7748 /* We apply the check to all breakpoints, including disabled for
7749 those with loc->duplicate set. This is so that when breakpoint
7750 becomes enabled, or the duplicate is removed, gdb will try to
7751 insert all breakpoints. If we don't set shlib_disabled here,
7752 we'll try to insert those breakpoints and fail. */
1042e4c0 7753 if (((b->type == bp_breakpoint)
508ccb1f 7754 || (b->type == bp_jit_event)
1042e4c0 7755 || (b->type == bp_hardware_breakpoint)
d77f58be 7756 || (is_tracepoint (b)))
6c95b8df 7757 && loc->pspace == current_program_space
0d381245 7758 && !loc->shlib_disabled
6c95b8df 7759 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7760 )
0d381245
VP
7761 {
7762 loc->shlib_disabled = 1;
7763 }
cae688ec
JJ
7764 }
7765}
7766
63644780
NB
7767/* Disable any breakpoints and tracepoints that are in SOLIB upon
7768 notification of unloaded_shlib. Only apply to enabled breakpoints,
7769 disabled ones can just stay disabled. */
84acb35a 7770
75149521 7771static void
84acb35a
JJ
7772disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7773{
876fa593 7774 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7775 int disabled_shlib_breaks = 0;
7776
876fa593 7777 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7778 {
2bdf28a0 7779 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7780 struct breakpoint *b = loc->owner;
cc59ec59 7781
1e4d1764 7782 if (solib->pspace == loc->pspace
e2dd7057 7783 && !loc->shlib_disabled
1e4d1764
YQ
7784 && (((b->type == bp_breakpoint
7785 || b->type == bp_jit_event
7786 || b->type == bp_hardware_breakpoint)
7787 && (loc->loc_type == bp_loc_hardware_breakpoint
7788 || loc->loc_type == bp_loc_software_breakpoint))
7789 || is_tracepoint (b))
e2dd7057 7790 && solib_contains_address_p (solib, loc->address))
84acb35a 7791 {
e2dd7057
PP
7792 loc->shlib_disabled = 1;
7793 /* At this point, we cannot rely on remove_breakpoint
7794 succeeding so we must mark the breakpoint as not inserted
7795 to prevent future errors occurring in remove_breakpoints. */
7796 loc->inserted = 0;
8d3788bd
VP
7797
7798 /* This may cause duplicate notifications for the same breakpoint. */
7799 observer_notify_breakpoint_modified (b);
7800
e2dd7057
PP
7801 if (!disabled_shlib_breaks)
7802 {
223ffa71 7803 target_terminal::ours_for_output ();
3e43a32a
MS
7804 warning (_("Temporarily disabling breakpoints "
7805 "for unloaded shared library \"%s\""),
e2dd7057 7806 solib->so_name);
84acb35a 7807 }
e2dd7057 7808 disabled_shlib_breaks = 1;
84acb35a
JJ
7809 }
7810 }
84acb35a
JJ
7811}
7812
63644780
NB
7813/* Disable any breakpoints and tracepoints in OBJFILE upon
7814 notification of free_objfile. Only apply to enabled breakpoints,
7815 disabled ones can just stay disabled. */
7816
7817static void
7818disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7819{
7820 struct breakpoint *b;
7821
7822 if (objfile == NULL)
7823 return;
7824
d03de421
PA
7825 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7826 managed by the user with add-symbol-file/remove-symbol-file.
7827 Similarly to how breakpoints in shared libraries are handled in
7828 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7829 shlib_disabled so they end up uninserted on the next global
7830 location list update. Shared libraries not loaded by the user
7831 aren't handled here -- they're already handled in
7832 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7833 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7834 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7835 main objfile). */
7836 if ((objfile->flags & OBJF_SHARED) == 0
7837 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7838 return;
7839
7840 ALL_BREAKPOINTS (b)
7841 {
7842 struct bp_location *loc;
7843 int bp_modified = 0;
7844
7845 if (!is_breakpoint (b) && !is_tracepoint (b))
7846 continue;
7847
7848 for (loc = b->loc; loc != NULL; loc = loc->next)
7849 {
7850 CORE_ADDR loc_addr = loc->address;
7851
7852 if (loc->loc_type != bp_loc_hardware_breakpoint
7853 && loc->loc_type != bp_loc_software_breakpoint)
7854 continue;
7855
7856 if (loc->shlib_disabled != 0)
7857 continue;
7858
7859 if (objfile->pspace != loc->pspace)
7860 continue;
7861
7862 if (loc->loc_type != bp_loc_hardware_breakpoint
7863 && loc->loc_type != bp_loc_software_breakpoint)
7864 continue;
7865
7866 if (is_addr_in_objfile (loc_addr, objfile))
7867 {
7868 loc->shlib_disabled = 1;
08351840
PA
7869 /* At this point, we don't know whether the object was
7870 unmapped from the inferior or not, so leave the
7871 inserted flag alone. We'll handle failure to
7872 uninsert quietly, in case the object was indeed
7873 unmapped. */
63644780
NB
7874
7875 mark_breakpoint_location_modified (loc);
7876
7877 bp_modified = 1;
7878 }
7879 }
7880
7881 if (bp_modified)
7882 observer_notify_breakpoint_modified (b);
7883 }
7884}
7885
ce78b96d
JB
7886/* FORK & VFORK catchpoints. */
7887
e29a4733 7888/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7889 catchpoint. A breakpoint is really of this type iff its ops pointer points
7890 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7891
c1fc2657 7892struct fork_catchpoint : public breakpoint
e29a4733 7893{
e29a4733
PA
7894 /* Process id of a child process whose forking triggered this
7895 catchpoint. This field is only valid immediately after this
7896 catchpoint has triggered. */
7897 ptid_t forked_inferior_pid;
7898};
7899
4a64f543
MS
7900/* Implement the "insert" breakpoint_ops method for fork
7901 catchpoints. */
ce78b96d 7902
77b06cd7
TJB
7903static int
7904insert_catch_fork (struct bp_location *bl)
ce78b96d 7905{
dfd4cc63 7906 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7907}
7908
4a64f543
MS
7909/* Implement the "remove" breakpoint_ops method for fork
7910 catchpoints. */
ce78b96d
JB
7911
7912static int
73971819 7913remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7914{
dfd4cc63 7915 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7916}
7917
7918/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7919 catchpoints. */
7920
7921static int
f1310107 7922breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7923 struct address_space *aspace, CORE_ADDR bp_addr,
7924 const struct target_waitstatus *ws)
ce78b96d 7925{
e29a4733
PA
7926 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7927
f90263c1
TT
7928 if (ws->kind != TARGET_WAITKIND_FORKED)
7929 return 0;
7930
7931 c->forked_inferior_pid = ws->value.related_pid;
7932 return 1;
ce78b96d
JB
7933}
7934
4a64f543
MS
7935/* Implement the "print_it" breakpoint_ops method for fork
7936 catchpoints. */
ce78b96d
JB
7937
7938static enum print_stop_action
348d480f 7939print_it_catch_fork (bpstat bs)
ce78b96d 7940{
36dfb11c 7941 struct ui_out *uiout = current_uiout;
348d480f
PA
7942 struct breakpoint *b = bs->breakpoint_at;
7943 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7944
ce78b96d 7945 annotate_catchpoint (b->number);
f303dbd6 7946 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7947 if (b->disposition == disp_del)
112e8700 7948 uiout->text ("Temporary catchpoint ");
36dfb11c 7949 else
112e8700
SM
7950 uiout->text ("Catchpoint ");
7951 if (uiout->is_mi_like_p ())
36dfb11c 7952 {
112e8700
SM
7953 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7954 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7955 }
112e8700
SM
7956 uiout->field_int ("bkptno", b->number);
7957 uiout->text (" (forked process ");
7958 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7959 uiout->text ("), ");
ce78b96d
JB
7960 return PRINT_SRC_AND_LOC;
7961}
7962
4a64f543
MS
7963/* Implement the "print_one" breakpoint_ops method for fork
7964 catchpoints. */
ce78b96d
JB
7965
7966static void
a6d9a66e 7967print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7968{
e29a4733 7969 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7970 struct value_print_options opts;
79a45e25 7971 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7972
7973 get_user_print_options (&opts);
7974
4a64f543
MS
7975 /* Field 4, the address, is omitted (which makes the columns not
7976 line up too nicely with the headers, but the effect is relatively
7977 readable). */
79a45b7d 7978 if (opts.addressprint)
112e8700 7979 uiout->field_skip ("addr");
ce78b96d 7980 annotate_field (5);
112e8700 7981 uiout->text ("fork");
e29a4733 7982 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7983 {
112e8700
SM
7984 uiout->text (", process ");
7985 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7986 uiout->spaces (1);
ce78b96d 7987 }
8ac3646f 7988
112e8700
SM
7989 if (uiout->is_mi_like_p ())
7990 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7991}
7992
7993/* Implement the "print_mention" breakpoint_ops method for fork
7994 catchpoints. */
7995
7996static void
7997print_mention_catch_fork (struct breakpoint *b)
7998{
7999 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8000}
8001
6149aea9
PA
8002/* Implement the "print_recreate" breakpoint_ops method for fork
8003 catchpoints. */
8004
8005static void
8006print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8007{
8008 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8009 print_recreate_thread (b, fp);
6149aea9
PA
8010}
8011
ce78b96d
JB
8012/* The breakpoint_ops structure to be used in fork catchpoints. */
8013
2060206e 8014static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8015
4a64f543
MS
8016/* Implement the "insert" breakpoint_ops method for vfork
8017 catchpoints. */
ce78b96d 8018
77b06cd7
TJB
8019static int
8020insert_catch_vfork (struct bp_location *bl)
ce78b96d 8021{
dfd4cc63 8022 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8023}
8024
4a64f543
MS
8025/* Implement the "remove" breakpoint_ops method for vfork
8026 catchpoints. */
ce78b96d
JB
8027
8028static int
73971819 8029remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8030{
dfd4cc63 8031 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8032}
8033
8034/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8035 catchpoints. */
8036
8037static int
f1310107 8038breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8039 struct address_space *aspace, CORE_ADDR bp_addr,
8040 const struct target_waitstatus *ws)
ce78b96d 8041{
e29a4733
PA
8042 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8043
f90263c1
TT
8044 if (ws->kind != TARGET_WAITKIND_VFORKED)
8045 return 0;
8046
8047 c->forked_inferior_pid = ws->value.related_pid;
8048 return 1;
ce78b96d
JB
8049}
8050
4a64f543
MS
8051/* Implement the "print_it" breakpoint_ops method for vfork
8052 catchpoints. */
ce78b96d
JB
8053
8054static enum print_stop_action
348d480f 8055print_it_catch_vfork (bpstat bs)
ce78b96d 8056{
36dfb11c 8057 struct ui_out *uiout = current_uiout;
348d480f 8058 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8059 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8060
ce78b96d 8061 annotate_catchpoint (b->number);
f303dbd6 8062 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8063 if (b->disposition == disp_del)
112e8700 8064 uiout->text ("Temporary catchpoint ");
36dfb11c 8065 else
112e8700
SM
8066 uiout->text ("Catchpoint ");
8067 if (uiout->is_mi_like_p ())
36dfb11c 8068 {
112e8700
SM
8069 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8070 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8071 }
112e8700
SM
8072 uiout->field_int ("bkptno", b->number);
8073 uiout->text (" (vforked process ");
8074 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8075 uiout->text ("), ");
ce78b96d
JB
8076 return PRINT_SRC_AND_LOC;
8077}
8078
4a64f543
MS
8079/* Implement the "print_one" breakpoint_ops method for vfork
8080 catchpoints. */
ce78b96d
JB
8081
8082static void
a6d9a66e 8083print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8084{
e29a4733 8085 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8086 struct value_print_options opts;
79a45e25 8087 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8088
8089 get_user_print_options (&opts);
4a64f543
MS
8090 /* Field 4, the address, is omitted (which makes the columns not
8091 line up too nicely with the headers, but the effect is relatively
8092 readable). */
79a45b7d 8093 if (opts.addressprint)
112e8700 8094 uiout->field_skip ("addr");
ce78b96d 8095 annotate_field (5);
112e8700 8096 uiout->text ("vfork");
e29a4733 8097 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8098 {
112e8700
SM
8099 uiout->text (", process ");
8100 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8101 uiout->spaces (1);
ce78b96d 8102 }
8ac3646f 8103
112e8700
SM
8104 if (uiout->is_mi_like_p ())
8105 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8106}
8107
8108/* Implement the "print_mention" breakpoint_ops method for vfork
8109 catchpoints. */
8110
8111static void
8112print_mention_catch_vfork (struct breakpoint *b)
8113{
8114 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8115}
8116
6149aea9
PA
8117/* Implement the "print_recreate" breakpoint_ops method for vfork
8118 catchpoints. */
8119
8120static void
8121print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8122{
8123 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8124 print_recreate_thread (b, fp);
6149aea9
PA
8125}
8126
ce78b96d
JB
8127/* The breakpoint_ops structure to be used in vfork catchpoints. */
8128
2060206e 8129static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8130
edcc5120 8131/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8132 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8133 CATCH_SOLIB_BREAKPOINT_OPS. */
8134
c1fc2657 8135struct solib_catchpoint : public breakpoint
edcc5120 8136{
c1fc2657 8137 ~solib_catchpoint () override;
edcc5120
TT
8138
8139 /* True for "catch load", false for "catch unload". */
8140 unsigned char is_load;
8141
8142 /* Regular expression to match, if any. COMPILED is only valid when
8143 REGEX is non-NULL. */
8144 char *regex;
2d7cc5c7 8145 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8146};
8147
c1fc2657 8148solib_catchpoint::~solib_catchpoint ()
edcc5120 8149{
c1fc2657 8150 xfree (this->regex);
edcc5120
TT
8151}
8152
8153static int
8154insert_catch_solib (struct bp_location *ignore)
8155{
8156 return 0;
8157}
8158
8159static int
73971819 8160remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8161{
8162 return 0;
8163}
8164
8165static int
8166breakpoint_hit_catch_solib (const struct bp_location *bl,
8167 struct address_space *aspace,
8168 CORE_ADDR bp_addr,
8169 const struct target_waitstatus *ws)
8170{
8171 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8172 struct breakpoint *other;
8173
8174 if (ws->kind == TARGET_WAITKIND_LOADED)
8175 return 1;
8176
8177 ALL_BREAKPOINTS (other)
8178 {
8179 struct bp_location *other_bl;
8180
8181 if (other == bl->owner)
8182 continue;
8183
8184 if (other->type != bp_shlib_event)
8185 continue;
8186
c1fc2657 8187 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8188 continue;
8189
8190 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8191 {
8192 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8193 return 1;
8194 }
8195 }
8196
8197 return 0;
8198}
8199
8200static void
8201check_status_catch_solib (struct bpstats *bs)
8202{
8203 struct solib_catchpoint *self
8204 = (struct solib_catchpoint *) bs->breakpoint_at;
8205 int ix;
8206
8207 if (self->is_load)
8208 {
8209 struct so_list *iter;
8210
8211 for (ix = 0;
8212 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8213 ix, iter);
8214 ++ix)
8215 {
8216 if (!self->regex
2d7cc5c7 8217 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8218 return;
8219 }
8220 }
8221 else
8222 {
8223 char *iter;
8224
8225 for (ix = 0;
8226 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8227 ix, iter);
8228 ++ix)
8229 {
8230 if (!self->regex
2d7cc5c7 8231 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8232 return;
8233 }
8234 }
8235
8236 bs->stop = 0;
8237 bs->print_it = print_it_noop;
8238}
8239
8240static enum print_stop_action
8241print_it_catch_solib (bpstat bs)
8242{
8243 struct breakpoint *b = bs->breakpoint_at;
8244 struct ui_out *uiout = current_uiout;
8245
8246 annotate_catchpoint (b->number);
f303dbd6 8247 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8248 if (b->disposition == disp_del)
112e8700 8249 uiout->text ("Temporary catchpoint ");
edcc5120 8250 else
112e8700
SM
8251 uiout->text ("Catchpoint ");
8252 uiout->field_int ("bkptno", b->number);
8253 uiout->text ("\n");
8254 if (uiout->is_mi_like_p ())
8255 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8256 print_solib_event (1);
8257 return PRINT_SRC_AND_LOC;
8258}
8259
8260static void
8261print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8262{
8263 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8264 struct value_print_options opts;
8265 struct ui_out *uiout = current_uiout;
8266 char *msg;
8267
8268 get_user_print_options (&opts);
8269 /* Field 4, the address, is omitted (which makes the columns not
8270 line up too nicely with the headers, but the effect is relatively
8271 readable). */
8272 if (opts.addressprint)
8273 {
8274 annotate_field (4);
112e8700 8275 uiout->field_skip ("addr");
edcc5120
TT
8276 }
8277
8278 annotate_field (5);
8279 if (self->is_load)
8280 {
8281 if (self->regex)
8282 msg = xstrprintf (_("load of library matching %s"), self->regex);
8283 else
8284 msg = xstrdup (_("load of library"));
8285 }
8286 else
8287 {
8288 if (self->regex)
8289 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8290 else
8291 msg = xstrdup (_("unload of library"));
8292 }
112e8700 8293 uiout->field_string ("what", msg);
edcc5120 8294 xfree (msg);
8ac3646f 8295
112e8700
SM
8296 if (uiout->is_mi_like_p ())
8297 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8298}
8299
8300static void
8301print_mention_catch_solib (struct breakpoint *b)
8302{
8303 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8304
8305 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8306 self->is_load ? "load" : "unload");
8307}
8308
8309static void
8310print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8311{
8312 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8313
8314 fprintf_unfiltered (fp, "%s %s",
8315 b->disposition == disp_del ? "tcatch" : "catch",
8316 self->is_load ? "load" : "unload");
8317 if (self->regex)
8318 fprintf_unfiltered (fp, " %s", self->regex);
8319 fprintf_unfiltered (fp, "\n");
8320}
8321
8322static struct breakpoint_ops catch_solib_breakpoint_ops;
8323
91985142
MG
8324/* Shared helper function (MI and CLI) for creating and installing
8325 a shared object event catchpoint. If IS_LOAD is non-zero then
8326 the events to be caught are load events, otherwise they are
8327 unload events. If IS_TEMP is non-zero the catchpoint is a
8328 temporary one. If ENABLED is non-zero the catchpoint is
8329 created in an enabled state. */
edcc5120 8330
91985142 8331void
a121b7c1 8332add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8333{
edcc5120 8334 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8335
edcc5120
TT
8336 if (!arg)
8337 arg = "";
f1735a53 8338 arg = skip_spaces (arg);
edcc5120 8339
36bd8eaa 8340 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8341
8342 if (*arg != '\0')
8343 {
2d7cc5c7
PA
8344 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8345 _("Invalid regexp")));
edcc5120
TT
8346 c->regex = xstrdup (arg);
8347 }
8348
8349 c->is_load = is_load;
36bd8eaa 8350 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8351 &catch_solib_breakpoint_ops);
8352
c1fc2657 8353 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8354
b270e6f9 8355 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8356}
8357
91985142
MG
8358/* A helper function that does all the work for "catch load" and
8359 "catch unload". */
8360
8361static void
8362catch_load_or_unload (char *arg, int from_tty, int is_load,
8363 struct cmd_list_element *command)
8364{
8365 int tempflag;
8366 const int enabled = 1;
8367
8368 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8369
8370 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8371}
8372
edcc5120
TT
8373static void
8374catch_load_command_1 (char *arg, int from_tty,
8375 struct cmd_list_element *command)
8376{
8377 catch_load_or_unload (arg, from_tty, 1, command);
8378}
8379
8380static void
8381catch_unload_command_1 (char *arg, int from_tty,
8382 struct cmd_list_element *command)
8383{
8384 catch_load_or_unload (arg, from_tty, 0, command);
8385}
8386
346774a9
PA
8387/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8388 is non-zero, then make the breakpoint temporary. If COND_STRING is
8389 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8390 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8391
ab04a2af 8392void
346774a9
PA
8393init_catchpoint (struct breakpoint *b,
8394 struct gdbarch *gdbarch, int tempflag,
63160a43 8395 const char *cond_string,
c0a91b2b 8396 const struct breakpoint_ops *ops)
c906108c 8397{
51abb421 8398 symtab_and_line sal;
6c95b8df 8399 sal.pspace = current_program_space;
c5aa993b 8400
28010a5d 8401 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8402
1b36a34b 8403 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8404 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8405}
8406
28010a5d 8407void
b270e6f9 8408install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8409{
b270e6f9 8410 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8411 set_breakpoint_number (internal, b);
558a9d82
YQ
8412 if (is_tracepoint (b))
8413 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8414 if (!internal)
8415 mention (b);
c56053d2 8416 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8417
8418 if (update_gll)
44702360 8419 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8420}
8421
9b70b993 8422static void
a6d9a66e 8423create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8424 int tempflag, const char *cond_string,
c0a91b2b 8425 const struct breakpoint_ops *ops)
c906108c 8426{
b270e6f9 8427 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8428
b270e6f9 8429 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8430
8431 c->forked_inferior_pid = null_ptid;
8432
b270e6f9 8433 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8434}
8435
fe798b75
JB
8436/* Exec catchpoints. */
8437
b4d90040 8438/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8439 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8440 CATCH_EXEC_BREAKPOINT_OPS. */
8441
c1fc2657 8442struct exec_catchpoint : public breakpoint
b4d90040 8443{
c1fc2657 8444 ~exec_catchpoint () override;
b4d90040
PA
8445
8446 /* Filename of a program whose exec triggered this catchpoint.
8447 This field is only valid immediately after this catchpoint has
8448 triggered. */
8449 char *exec_pathname;
8450};
8451
c1fc2657 8452/* Exec catchpoint destructor. */
b4d90040 8453
c1fc2657 8454exec_catchpoint::~exec_catchpoint ()
b4d90040 8455{
c1fc2657 8456 xfree (this->exec_pathname);
b4d90040
PA
8457}
8458
77b06cd7
TJB
8459static int
8460insert_catch_exec (struct bp_location *bl)
c906108c 8461{
dfd4cc63 8462 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8463}
c906108c 8464
fe798b75 8465static int
73971819 8466remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8467{
dfd4cc63 8468 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8469}
c906108c 8470
fe798b75 8471static int
f1310107 8472breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8473 struct address_space *aspace, CORE_ADDR bp_addr,
8474 const struct target_waitstatus *ws)
fe798b75 8475{
b4d90040
PA
8476 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8477
f90263c1
TT
8478 if (ws->kind != TARGET_WAITKIND_EXECD)
8479 return 0;
8480
8481 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8482 return 1;
fe798b75 8483}
c906108c 8484
fe798b75 8485static enum print_stop_action
348d480f 8486print_it_catch_exec (bpstat bs)
fe798b75 8487{
36dfb11c 8488 struct ui_out *uiout = current_uiout;
348d480f 8489 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8490 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8491
fe798b75 8492 annotate_catchpoint (b->number);
f303dbd6 8493 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8494 if (b->disposition == disp_del)
112e8700 8495 uiout->text ("Temporary catchpoint ");
36dfb11c 8496 else
112e8700
SM
8497 uiout->text ("Catchpoint ");
8498 if (uiout->is_mi_like_p ())
36dfb11c 8499 {
112e8700
SM
8500 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8501 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8502 }
112e8700
SM
8503 uiout->field_int ("bkptno", b->number);
8504 uiout->text (" (exec'd ");
8505 uiout->field_string ("new-exec", c->exec_pathname);
8506 uiout->text ("), ");
36dfb11c 8507
fe798b75 8508 return PRINT_SRC_AND_LOC;
c906108c
SS
8509}
8510
fe798b75 8511static void
a6d9a66e 8512print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8513{
b4d90040 8514 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8515 struct value_print_options opts;
79a45e25 8516 struct ui_out *uiout = current_uiout;
fe798b75
JB
8517
8518 get_user_print_options (&opts);
8519
8520 /* Field 4, the address, is omitted (which makes the columns
8521 not line up too nicely with the headers, but the effect
8522 is relatively readable). */
8523 if (opts.addressprint)
112e8700 8524 uiout->field_skip ("addr");
fe798b75 8525 annotate_field (5);
112e8700 8526 uiout->text ("exec");
b4d90040 8527 if (c->exec_pathname != NULL)
fe798b75 8528 {
112e8700
SM
8529 uiout->text (", program \"");
8530 uiout->field_string ("what", c->exec_pathname);
8531 uiout->text ("\" ");
fe798b75 8532 }
8ac3646f 8533
112e8700
SM
8534 if (uiout->is_mi_like_p ())
8535 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8536}
8537
8538static void
8539print_mention_catch_exec (struct breakpoint *b)
8540{
8541 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8542}
8543
6149aea9
PA
8544/* Implement the "print_recreate" breakpoint_ops method for exec
8545 catchpoints. */
8546
8547static void
8548print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8549{
8550 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8551 print_recreate_thread (b, fp);
6149aea9
PA
8552}
8553
2060206e 8554static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8555
c906108c 8556static int
fba45db2 8557hw_breakpoint_used_count (void)
c906108c 8558{
c906108c 8559 int i = 0;
f1310107
TJB
8560 struct breakpoint *b;
8561 struct bp_location *bl;
c906108c
SS
8562
8563 ALL_BREAKPOINTS (b)
c5aa993b 8564 {
d6b74ac4 8565 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8566 for (bl = b->loc; bl; bl = bl->next)
8567 {
8568 /* Special types of hardware breakpoints may use more than
8569 one register. */
348d480f 8570 i += b->ops->resources_needed (bl);
f1310107 8571 }
c5aa993b 8572 }
c906108c
SS
8573
8574 return i;
8575}
8576
a1398e0c
PA
8577/* Returns the resources B would use if it were a hardware
8578 watchpoint. */
8579
c906108c 8580static int
a1398e0c 8581hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8582{
c906108c 8583 int i = 0;
e09342b5 8584 struct bp_location *bl;
c906108c 8585
a1398e0c
PA
8586 if (!breakpoint_enabled (b))
8587 return 0;
8588
8589 for (bl = b->loc; bl; bl = bl->next)
8590 {
8591 /* Special types of hardware watchpoints may use more than
8592 one register. */
8593 i += b->ops->resources_needed (bl);
8594 }
8595
8596 return i;
8597}
8598
8599/* Returns the sum the used resources of all hardware watchpoints of
8600 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8601 the sum of the used resources of all hardware watchpoints of other
8602 types _not_ TYPE. */
8603
8604static int
8605hw_watchpoint_used_count_others (struct breakpoint *except,
8606 enum bptype type, int *other_type_used)
8607{
8608 int i = 0;
8609 struct breakpoint *b;
8610
c906108c
SS
8611 *other_type_used = 0;
8612 ALL_BREAKPOINTS (b)
e09342b5 8613 {
a1398e0c
PA
8614 if (b == except)
8615 continue;
e09342b5
TJB
8616 if (!breakpoint_enabled (b))
8617 continue;
8618
a1398e0c
PA
8619 if (b->type == type)
8620 i += hw_watchpoint_use_count (b);
8621 else if (is_hardware_watchpoint (b))
8622 *other_type_used = 1;
e09342b5
TJB
8623 }
8624
c906108c
SS
8625 return i;
8626}
8627
c906108c 8628void
fba45db2 8629disable_watchpoints_before_interactive_call_start (void)
c906108c 8630{
c5aa993b 8631 struct breakpoint *b;
c906108c
SS
8632
8633 ALL_BREAKPOINTS (b)
c5aa993b 8634 {
cc60f2e3 8635 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8636 {
b5de0fa7 8637 b->enable_state = bp_call_disabled;
44702360 8638 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8639 }
8640 }
c906108c
SS
8641}
8642
8643void
fba45db2 8644enable_watchpoints_after_interactive_call_stop (void)
c906108c 8645{
c5aa993b 8646 struct breakpoint *b;
c906108c
SS
8647
8648 ALL_BREAKPOINTS (b)
c5aa993b 8649 {
cc60f2e3 8650 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8651 {
b5de0fa7 8652 b->enable_state = bp_enabled;
44702360 8653 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8654 }
8655 }
c906108c
SS
8656}
8657
8bea4e01
UW
8658void
8659disable_breakpoints_before_startup (void)
8660{
6c95b8df 8661 current_program_space->executing_startup = 1;
44702360 8662 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8663}
8664
8665void
8666enable_breakpoints_after_startup (void)
8667{
6c95b8df 8668 current_program_space->executing_startup = 0;
f8eba3c6 8669 breakpoint_re_set ();
8bea4e01
UW
8670}
8671
7c16b83e
PA
8672/* Create a new single-step breakpoint for thread THREAD, with no
8673 locations. */
c906108c 8674
7c16b83e
PA
8675static struct breakpoint *
8676new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8677{
b270e6f9 8678 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8679
b270e6f9 8680 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8681 &momentary_breakpoint_ops);
8682
8683 b->disposition = disp_donttouch;
8684 b->frame_id = null_frame_id;
8685
8686 b->thread = thread;
8687 gdb_assert (b->thread != 0);
8688
b270e6f9 8689 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8690}
8691
8692/* Set a momentary breakpoint of type TYPE at address specified by
8693 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8694 frame. */
c906108c
SS
8695
8696struct breakpoint *
a6d9a66e
UW
8697set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8698 struct frame_id frame_id, enum bptype type)
c906108c 8699{
52f0bd74 8700 struct breakpoint *b;
edb3359d 8701
193facb3
JK
8702 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8703 tail-called one. */
8704 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8705
06edf0c0 8706 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8707 b->enable_state = bp_enabled;
8708 b->disposition = disp_donttouch;
818dd999 8709 b->frame_id = frame_id;
c906108c 8710
4a64f543
MS
8711 /* If we're debugging a multi-threaded program, then we want
8712 momentary breakpoints to be active in only a single thread of
8713 control. */
39f77062 8714 if (in_thread_list (inferior_ptid))
5d5658a1 8715 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8716
44702360 8717 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8718
c906108c
SS
8719 return b;
8720}
611c83ae 8721
06edf0c0 8722/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8723 The new breakpoint will have type TYPE, use OPS as its
8724 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8725
06edf0c0
PA
8726static struct breakpoint *
8727momentary_breakpoint_from_master (struct breakpoint *orig,
8728 enum bptype type,
a1aa2221
LM
8729 const struct breakpoint_ops *ops,
8730 int loc_enabled)
e58b0e63
PA
8731{
8732 struct breakpoint *copy;
8733
06edf0c0 8734 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8735 copy->loc = allocate_bp_location (copy);
0e30163f 8736 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8737
a6d9a66e 8738 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8739 copy->loc->requested_address = orig->loc->requested_address;
8740 copy->loc->address = orig->loc->address;
8741 copy->loc->section = orig->loc->section;
6c95b8df 8742 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8743 copy->loc->probe = orig->loc->probe;
f8eba3c6 8744 copy->loc->line_number = orig->loc->line_number;
2f202fde 8745 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8746 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8747 copy->frame_id = orig->frame_id;
8748 copy->thread = orig->thread;
6c95b8df 8749 copy->pspace = orig->pspace;
e58b0e63
PA
8750
8751 copy->enable_state = bp_enabled;
8752 copy->disposition = disp_donttouch;
8753 copy->number = internal_breakpoint_number--;
8754
44702360 8755 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8756 return copy;
8757}
8758
06edf0c0
PA
8759/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8760 ORIG is NULL. */
8761
8762struct breakpoint *
8763clone_momentary_breakpoint (struct breakpoint *orig)
8764{
8765 /* If there's nothing to clone, then return nothing. */
8766 if (orig == NULL)
8767 return NULL;
8768
a1aa2221 8769 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8770}
8771
611c83ae 8772struct breakpoint *
a6d9a66e
UW
8773set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8774 enum bptype type)
611c83ae
PA
8775{
8776 struct symtab_and_line sal;
8777
8778 sal = find_pc_line (pc, 0);
8779 sal.pc = pc;
8780 sal.section = find_pc_overlay (pc);
8781 sal.explicit_pc = 1;
8782
a6d9a66e 8783 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8784}
c906108c 8785\f
c5aa993b 8786
c906108c
SS
8787/* Tell the user we have just set a breakpoint B. */
8788
8789static void
fba45db2 8790mention (struct breakpoint *b)
c906108c 8791{
348d480f 8792 b->ops->print_mention (b);
112e8700 8793 if (current_uiout->is_mi_like_p ())
fb40c209 8794 return;
c906108c
SS
8795 printf_filtered ("\n");
8796}
c906108c 8797\f
c5aa993b 8798
1a853c52
PA
8799static int bp_loc_is_permanent (struct bp_location *loc);
8800
0d381245 8801static struct bp_location *
39d61571 8802add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8803 const struct symtab_and_line *sal)
8804{
8805 struct bp_location *loc, **tmp;
3742cc8b
YQ
8806 CORE_ADDR adjusted_address;
8807 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8808
8809 if (loc_gdbarch == NULL)
8810 loc_gdbarch = b->gdbarch;
8811
8812 /* Adjust the breakpoint's address prior to allocating a location.
8813 Once we call allocate_bp_location(), that mostly uninitialized
8814 location will be placed on the location chain. Adjustment of the
8815 breakpoint may cause target_read_memory() to be called and we do
8816 not want its scan of the location chain to find a breakpoint and
8817 location that's only been partially initialized. */
8818 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8819 sal->pc, b->type);
0d381245 8820
d30113d4 8821 /* Sort the locations by their ADDRESS. */
39d61571 8822 loc = allocate_bp_location (b);
d30113d4
JK
8823 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8824 tmp = &((*tmp)->next))
0d381245 8825 ;
d30113d4 8826 loc->next = *tmp;
0d381245 8827 *tmp = loc;
3742cc8b 8828
0d381245 8829 loc->requested_address = sal->pc;
3742cc8b 8830 loc->address = adjusted_address;
6c95b8df 8831 loc->pspace = sal->pspace;
729662a5
TT
8832 loc->probe.probe = sal->probe;
8833 loc->probe.objfile = sal->objfile;
6c95b8df 8834 gdb_assert (loc->pspace != NULL);
0d381245 8835 loc->section = sal->section;
3742cc8b 8836 loc->gdbarch = loc_gdbarch;
f8eba3c6 8837 loc->line_number = sal->line;
2f202fde 8838 loc->symtab = sal->symtab;
f8eba3c6 8839
0e30163f
JK
8840 set_breakpoint_location_function (loc,
8841 sal->explicit_pc || sal->explicit_line);
1a853c52 8842
6ae88661
LM
8843 /* While by definition, permanent breakpoints are already present in the
8844 code, we don't mark the location as inserted. Normally one would expect
8845 that GDB could rely on that breakpoint instruction to stop the program,
8846 thus removing the need to insert its own breakpoint, except that executing
8847 the breakpoint instruction can kill the target instead of reporting a
8848 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8849 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8850 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8851 breakpoint be inserted normally results in QEMU knowing about the GDB
8852 breakpoint, and thus trap before the breakpoint instruction is executed.
8853 (If GDB later needs to continue execution past the permanent breakpoint,
8854 it manually increments the PC, thus avoiding executing the breakpoint
8855 instruction.) */
1a853c52 8856 if (bp_loc_is_permanent (loc))
6ae88661 8857 loc->permanent = 1;
1a853c52 8858
0d381245
VP
8859 return loc;
8860}
514f746b
AR
8861\f
8862
1cf4d951 8863/* See breakpoint.h. */
514f746b 8864
1cf4d951
PA
8865int
8866program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8867{
8868 int len;
8869 CORE_ADDR addr;
1afeeb75 8870 const gdb_byte *bpoint;
514f746b
AR
8871 gdb_byte *target_mem;
8872
1cf4d951
PA
8873 addr = address;
8874 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8875
8876 /* Software breakpoints unsupported? */
8877 if (bpoint == NULL)
8878 return 0;
8879
224c3ddb 8880 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8881
8882 /* Enable the automatic memory restoration from breakpoints while
8883 we read the memory. Otherwise we could say about our temporary
8884 breakpoints they are permanent. */
cb85b21b
TT
8885 scoped_restore restore_memory
8886 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8887
8888 if (target_read_memory (address, target_mem, len) == 0
8889 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8890 return 1;
1cf4d951 8891
cb85b21b 8892 return 0;
1cf4d951
PA
8893}
8894
8895/* Return 1 if LOC is pointing to a permanent breakpoint,
8896 return 0 otherwise. */
8897
8898static int
8899bp_loc_is_permanent (struct bp_location *loc)
8900{
514f746b
AR
8901 gdb_assert (loc != NULL);
8902
244558af
LM
8903 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8904 attempt to read from the addresses the locations of these breakpoint types
8905 point to. program_breakpoint_here_p, below, will attempt to read
8906 memory. */
8907 if (!breakpoint_address_is_meaningful (loc->owner))
8908 return 0;
8909
5ed8105e 8910 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8911 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8912 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8913}
8914
e7e0cddf
SS
8915/* Build a command list for the dprintf corresponding to the current
8916 settings of the dprintf style options. */
8917
8918static void
8919update_dprintf_command_list (struct breakpoint *b)
8920{
8921 char *dprintf_args = b->extra_string;
8922 char *printf_line = NULL;
8923
8924 if (!dprintf_args)
8925 return;
8926
8927 dprintf_args = skip_spaces (dprintf_args);
8928
8929 /* Allow a comma, as it may have terminated a location, but don't
8930 insist on it. */
8931 if (*dprintf_args == ',')
8932 ++dprintf_args;
8933 dprintf_args = skip_spaces (dprintf_args);
8934
8935 if (*dprintf_args != '"')
8936 error (_("Bad format string, missing '\"'."));
8937
d3ce09f5 8938 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8939 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8940 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8941 {
8942 if (!dprintf_function)
8943 error (_("No function supplied for dprintf call"));
8944
8945 if (dprintf_channel && strlen (dprintf_channel) > 0)
8946 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8947 dprintf_function,
8948 dprintf_channel,
8949 dprintf_args);
8950 else
8951 printf_line = xstrprintf ("call (void) %s (%s)",
8952 dprintf_function,
8953 dprintf_args);
8954 }
d3ce09f5
SS
8955 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8956 {
8957 if (target_can_run_breakpoint_commands ())
8958 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8959 else
8960 {
8961 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8962 printf_line = xstrprintf ("printf %s", dprintf_args);
8963 }
8964 }
e7e0cddf
SS
8965 else
8966 internal_error (__FILE__, __LINE__,
8967 _("Invalid dprintf style."));
8968
f28045c2 8969 gdb_assert (printf_line != NULL);
9d6e6e84 8970 /* Manufacture a printf sequence. */
f28045c2 8971 {
8d749320 8972 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8973
f28045c2
YQ
8974 printf_cmd_line->control_type = simple_control;
8975 printf_cmd_line->body_count = 0;
8976 printf_cmd_line->body_list = NULL;
9d6e6e84 8977 printf_cmd_line->next = NULL;
f28045c2 8978 printf_cmd_line->line = printf_line;
e7e0cddf 8979
93921405 8980 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8981 }
e7e0cddf
SS
8982}
8983
8984/* Update all dprintf commands, making their command lists reflect
8985 current style settings. */
8986
8987static void
8988update_dprintf_commands (char *args, int from_tty,
8989 struct cmd_list_element *c)
8990{
8991 struct breakpoint *b;
8992
8993 ALL_BREAKPOINTS (b)
8994 {
8995 if (b->type == bp_dprintf)
8996 update_dprintf_command_list (b);
8997 }
8998}
c3f6f71d 8999
f00aae0f
KS
9000/* Create a breakpoint with SAL as location. Use LOCATION
9001 as a description of the location, and COND_STRING
b35a8b2f
DE
9002 as condition expression. If LOCATION is NULL then create an
9003 "address location" from the address in the SAL. */
018d34a4
VP
9004
9005static void
d9b3f62e 9006init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 9007 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9008 event_location_up &&location,
e1e01040
PA
9009 gdb::unique_xmalloc_ptr<char> filter,
9010 gdb::unique_xmalloc_ptr<char> cond_string,
9011 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9012 enum bptype type, enum bpdisp disposition,
9013 int thread, int task, int ignore_count,
c0a91b2b 9014 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9015 int enabled, int internal, unsigned flags,
9016 int display_canonical)
018d34a4 9017{
0d381245 9018 int i;
018d34a4
VP
9019
9020 if (type == bp_hardware_breakpoint)
9021 {
fbbd034e
AS
9022 int target_resources_ok;
9023
9024 i = hw_breakpoint_used_count ();
9025 target_resources_ok =
9026 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9027 i + 1, 0);
9028 if (target_resources_ok == 0)
9029 error (_("No hardware breakpoint support in the target."));
9030 else if (target_resources_ok < 0)
9031 error (_("Hardware breakpoints used exceeds limit."));
9032 }
9033
6c5b2ebe 9034 gdb_assert (!sals.empty ());
6c95b8df 9035
6c5b2ebe 9036 for (const auto &sal : sals)
0d381245 9037 {
0d381245
VP
9038 struct bp_location *loc;
9039
9040 if (from_tty)
5af949e3
UW
9041 {
9042 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9043 if (!loc_gdbarch)
9044 loc_gdbarch = gdbarch;
9045
9046 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9047 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9048 }
0d381245 9049
6c5b2ebe 9050 if (&sal == &sals[0])
0d381245 9051 {
d9b3f62e 9052 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9053 b->thread = thread;
4a306c9a 9054 b->task = task;
855a6e68 9055
e1e01040
PA
9056 b->cond_string = cond_string.release ();
9057 b->extra_string = extra_string.release ();
0d381245 9058 b->ignore_count = ignore_count;
41447f92 9059 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9060 b->disposition = disposition;
6c95b8df 9061
44f238bb
PA
9062 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9063 b->loc->inserted = 1;
9064
0fb4aa4b
PA
9065 if (type == bp_static_tracepoint)
9066 {
d9b3f62e 9067 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9068 struct static_tracepoint_marker marker;
9069
983af33b 9070 if (strace_marker_p (b))
0fb4aa4b
PA
9071 {
9072 /* We already know the marker exists, otherwise, we
9073 wouldn't see a sal for it. */
d28cd78a
TT
9074 const char *p
9075 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9076 const char *endp;
0fb4aa4b 9077 char *marker_str;
0fb4aa4b 9078
f1735a53 9079 p = skip_spaces (p);
0fb4aa4b 9080
f1735a53 9081 endp = skip_to_space (p);
0fb4aa4b
PA
9082
9083 marker_str = savestring (p, endp - p);
d9b3f62e 9084 t->static_trace_marker_id = marker_str;
0fb4aa4b 9085
3e43a32a
MS
9086 printf_filtered (_("Probed static tracepoint "
9087 "marker \"%s\"\n"),
d9b3f62e 9088 t->static_trace_marker_id);
0fb4aa4b
PA
9089 }
9090 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9091 {
d9b3f62e 9092 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9093 release_static_tracepoint_marker (&marker);
9094
3e43a32a
MS
9095 printf_filtered (_("Probed static tracepoint "
9096 "marker \"%s\"\n"),
d9b3f62e 9097 t->static_trace_marker_id);
0fb4aa4b
PA
9098 }
9099 else
3e43a32a
MS
9100 warning (_("Couldn't determine the static "
9101 "tracepoint marker to probe"));
0fb4aa4b
PA
9102 }
9103
0d381245
VP
9104 loc = b->loc;
9105 }
9106 else
018d34a4 9107 {
39d61571 9108 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9109 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9110 loc->inserted = 1;
0d381245
VP
9111 }
9112
9113 if (b->cond_string)
9114 {
bbc13ae3
KS
9115 const char *arg = b->cond_string;
9116
1bb9788d
TT
9117 loc->cond = parse_exp_1 (&arg, loc->address,
9118 block_for_pc (loc->address), 0);
0d381245 9119 if (*arg)
588ae58c 9120 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9121 }
e7e0cddf
SS
9122
9123 /* Dynamic printf requires and uses additional arguments on the
9124 command line, otherwise it's an error. */
9125 if (type == bp_dprintf)
9126 {
9127 if (b->extra_string)
9128 update_dprintf_command_list (b);
9129 else
9130 error (_("Format string required"));
9131 }
9132 else if (b->extra_string)
588ae58c 9133 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9134 }
018d34a4 9135
56435ebe 9136 b->display_canonical = display_canonical;
f00aae0f 9137 if (location != NULL)
d28cd78a 9138 b->location = std::move (location);
018d34a4 9139 else
d28cd78a 9140 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9141 b->filter = filter.release ();
d9b3f62e 9142}
018d34a4 9143
d9b3f62e
PA
9144static void
9145create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9146 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9147 event_location_up &&location,
e1e01040
PA
9148 gdb::unique_xmalloc_ptr<char> filter,
9149 gdb::unique_xmalloc_ptr<char> cond_string,
9150 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9151 enum bptype type, enum bpdisp disposition,
9152 int thread, int task, int ignore_count,
c0a91b2b 9153 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9154 int enabled, int internal, unsigned flags,
9155 int display_canonical)
d9b3f62e 9156{
a5e364af 9157 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9158
a5e364af 9159 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9160 sals, std::move (location),
e1e01040
PA
9161 std::move (filter),
9162 std::move (cond_string),
9163 std::move (extra_string),
d9b3f62e
PA
9164 type, disposition,
9165 thread, task, ignore_count,
9166 ops, from_tty,
44f238bb
PA
9167 enabled, internal, flags,
9168 display_canonical);
d9b3f62e 9169
b270e6f9 9170 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9171}
9172
9173/* Add SALS.nelts breakpoints to the breakpoint table. For each
9174 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9175 value. COND_STRING, if not NULL, specified the condition to be
9176 used for all breakpoints. Essentially the only case where
9177 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9178 function. In that case, it's still not possible to specify
9179 separate conditions for different overloaded functions, so
9180 we take just a single condition string.
9181
c3f6f71d 9182 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9183 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9184 array contents). If the function fails (error() is called), the
9185 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9186 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9187
9188static void
8cdf0e15 9189create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9190 struct linespec_result *canonical,
e1e01040
PA
9191 gdb::unique_xmalloc_ptr<char> cond_string,
9192 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9193 enum bptype type, enum bpdisp disposition,
9194 int thread, int task, int ignore_count,
c0a91b2b 9195 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9196 int enabled, int internal, unsigned flags)
c906108c 9197{
f8eba3c6 9198 if (canonical->pre_expanded)
6c5b2ebe 9199 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9200
6c5b2ebe 9201 for (const auto &lsal : canonical->lsals)
c3f6f71d 9202 {
f00aae0f 9203 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9204 'break', without arguments. */
ffc2605c 9205 event_location_up location
f00aae0f 9206 = (canonical->location != NULL
8e9e35b1 9207 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9208 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9209 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9210
6c5b2ebe 9211 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9212 std::move (location),
e1e01040
PA
9213 std::move (filter_string),
9214 std::move (cond_string),
9215 std::move (extra_string),
e7e0cddf 9216 type, disposition,
84f4c1fe 9217 thread, task, ignore_count, ops,
44f238bb 9218 from_tty, enabled, internal, flags,
56435ebe 9219 canonical->special_display);
c3f6f71d 9220 }
c3f6f71d 9221}
c906108c 9222
f00aae0f 9223/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9224 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9225 addresses found. LOCATION points to the end of the SAL (for
9226 linespec locations).
9998af43
TJB
9227
9228 The array and the line spec strings are allocated on the heap, it is
9229 the caller's responsibility to free them. */
c906108c 9230
b9362cc7 9231static void
f00aae0f 9232parse_breakpoint_sals (const struct event_location *location,
58438ac1 9233 struct linespec_result *canonical)
c3f6f71d 9234{
f00aae0f
KS
9235 struct symtab_and_line cursal;
9236
9237 if (event_location_type (location) == LINESPEC_LOCATION)
9238 {
9239 const char *address = get_linespec_location (location);
9240
9241 if (address == NULL)
9242 {
9243 /* The last displayed codepoint, if it's valid, is our default
9244 breakpoint address. */
9245 if (last_displayed_sal_is_valid ())
9246 {
f00aae0f
KS
9247 /* Set sal's pspace, pc, symtab, and line to the values
9248 corresponding to the last call to print_frame_info.
9249 Be sure to reinitialize LINE with NOTCURRENT == 0
9250 as the breakpoint line number is inappropriate otherwise.
9251 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9252 symtab_and_line sal = get_last_displayed_sal ();
9253 CORE_ADDR pc = sal.pc;
9254
f00aae0f
KS
9255 sal = find_pc_line (pc, 0);
9256
9257 /* "break" without arguments is equivalent to "break *PC"
9258 where PC is the last displayed codepoint's address. So
9259 make sure to set sal.explicit_pc to prevent GDB from
9260 trying to expand the list of sals to include all other
9261 instances with the same symtab and line. */
9262 sal.pc = pc;
9263 sal.explicit_pc = 1;
9264
6c5b2ebe
PA
9265 struct linespec_sals lsal;
9266 lsal.sals = {sal};
f00aae0f
KS
9267 lsal.canonical = NULL;
9268
6c5b2ebe 9269 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9270 return;
9271 }
9272 else
9273 error (_("No default breakpoint address now."));
c906108c 9274 }
c906108c 9275 }
f00aae0f
KS
9276
9277 /* Force almost all breakpoints to be in terms of the
9278 current_source_symtab (which is decode_line_1's default).
9279 This should produce the results we want almost all of the
9280 time while leaving default_breakpoint_* alone.
9281
9282 ObjC: However, don't match an Objective-C method name which
9283 may have a '+' or '-' succeeded by a '['. */
9284 cursal = get_current_source_symtab_and_line ();
9285 if (last_displayed_sal_is_valid ())
c906108c 9286 {
f00aae0f 9287 const char *address = NULL;
cc80f267 9288
f00aae0f
KS
9289 if (event_location_type (location) == LINESPEC_LOCATION)
9290 address = get_linespec_location (location);
cc80f267 9291
f00aae0f
KS
9292 if (!cursal.symtab
9293 || (address != NULL
9294 && strchr ("+-", address[0]) != NULL
9295 && address[1] != '['))
9296 {
c2f4122d 9297 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9298 get_last_displayed_symtab (),
9299 get_last_displayed_line (),
9300 canonical, NULL, NULL);
9301 return;
9302 }
c906108c 9303 }
f00aae0f 9304
c2f4122d 9305 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9306 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9307}
c906108c 9308
c906108c 9309
c3f6f71d 9310/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9311 inserted as a breakpoint. If it can't throw an error. */
c906108c 9312
b9362cc7 9313static void
6c5b2ebe 9314breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9315{
6c5b2ebe
PA
9316 for (auto &sal : sals)
9317 resolve_sal_pc (&sal);
c3f6f71d
JM
9318}
9319
7a697b8d
SS
9320/* Fast tracepoints may have restrictions on valid locations. For
9321 instance, a fast tracepoint using a jump instead of a trap will
9322 likely have to overwrite more bytes than a trap would, and so can
9323 only be placed where the instruction is longer than the jump, or a
9324 multi-instruction sequence does not have a jump into the middle of
9325 it, etc. */
9326
9327static void
9328check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9329 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9330{
6c5b2ebe 9331 int rslt;
7a697b8d
SS
9332 char *msg;
9333 struct cleanup *old_chain;
9334
6c5b2ebe 9335 for (const auto &sal : sals)
7a697b8d 9336 {
f8eba3c6
TT
9337 struct gdbarch *sarch;
9338
6c5b2ebe 9339 sarch = get_sal_arch (sal);
f8eba3c6
TT
9340 /* We fall back to GDBARCH if there is no architecture
9341 associated with SAL. */
9342 if (sarch == NULL)
9343 sarch = gdbarch;
6c5b2ebe 9344 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9345 old_chain = make_cleanup (xfree, msg);
9346
9347 if (!rslt)
53c3572a 9348 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9349 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9350
9351 do_cleanups (old_chain);
9352 }
9353}
9354
018d34a4
VP
9355/* Given TOK, a string specification of condition and thread, as
9356 accepted by the 'break' command, extract the condition
9357 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9358 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9359 If no condition is found, *COND_STRING is set to NULL.
9360 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9361
9362static void
bbc13ae3 9363find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9364 char **cond_string, int *thread, int *task,
9365 char **rest)
018d34a4
VP
9366{
9367 *cond_string = NULL;
9368 *thread = -1;
ed1d1739
KS
9369 *task = 0;
9370 *rest = NULL;
9371
018d34a4
VP
9372 while (tok && *tok)
9373 {
bbc13ae3 9374 const char *end_tok;
018d34a4 9375 int toklen;
bbc13ae3
KS
9376 const char *cond_start = NULL;
9377 const char *cond_end = NULL;
cc59ec59 9378
f1735a53 9379 tok = skip_spaces (tok);
e7e0cddf
SS
9380
9381 if ((*tok == '"' || *tok == ',') && rest)
9382 {
9383 *rest = savestring (tok, strlen (tok));
9384 return;
9385 }
9386
f1735a53 9387 end_tok = skip_to_space (tok);
d634f2de 9388
018d34a4 9389 toklen = end_tok - tok;
d634f2de 9390
018d34a4
VP
9391 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9392 {
9393 tok = cond_start = end_tok + 1;
4d01a485 9394 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9395 cond_end = tok;
d634f2de 9396 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9397 }
9398 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9399 {
5d5658a1
PA
9400 const char *tmptok;
9401 struct thread_info *thr;
d634f2de 9402
018d34a4 9403 tok = end_tok + 1;
5d5658a1 9404 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9405 if (tok == tmptok)
9406 error (_("Junk after thread keyword."));
5d5658a1 9407 *thread = thr->global_num;
bbc13ae3 9408 tok = tmptok;
018d34a4 9409 }
4a306c9a
JB
9410 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9411 {
9412 char *tmptok;
9413
9414 tok = end_tok + 1;
bbc13ae3 9415 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9416 if (tok == tmptok)
9417 error (_("Junk after task keyword."));
9418 if (!valid_task_id (*task))
b6199126 9419 error (_("Unknown task %d."), *task);
bbc13ae3 9420 tok = tmptok;
4a306c9a 9421 }
e7e0cddf
SS
9422 else if (rest)
9423 {
9424 *rest = savestring (tok, strlen (tok));
ccab2054 9425 return;
e7e0cddf 9426 }
018d34a4
VP
9427 else
9428 error (_("Junk at end of arguments."));
9429 }
9430}
9431
0fb4aa4b
PA
9432/* Decode a static tracepoint marker spec. */
9433
6c5b2ebe 9434static std::vector<symtab_and_line>
f00aae0f 9435decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9436{
9437 VEC(static_tracepoint_marker_p) *markers = NULL;
0fb4aa4b 9438 struct cleanup *old_chain;
f00aae0f
KS
9439 const char *p = &(*arg_p)[3];
9440 const char *endp;
0fb4aa4b
PA
9441 char *marker_str;
9442 int i;
9443
f1735a53 9444 p = skip_spaces (p);
0fb4aa4b 9445
f1735a53 9446 endp = skip_to_space (p);
0fb4aa4b
PA
9447
9448 marker_str = savestring (p, endp - p);
9449 old_chain = make_cleanup (xfree, marker_str);
9450
9451 markers = target_static_tracepoint_markers_by_strid (marker_str);
9452 if (VEC_empty(static_tracepoint_marker_p, markers))
9453 error (_("No known static tracepoint marker named %s"), marker_str);
9454
6c5b2ebe
PA
9455 std::vector<symtab_and_line> sals;
9456 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9457
6c5b2ebe 9458 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9459 {
9460 struct static_tracepoint_marker *marker;
9461
9462 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9463
51abb421 9464 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9465 sal.pc = marker->address;
9466 sals.push_back (sal);
0fb4aa4b
PA
9467
9468 release_static_tracepoint_marker (marker);
9469 }
9470
9471 do_cleanups (old_chain);
9472
9473 *arg_p = endp;
9474 return sals;
9475}
9476
f00aae0f 9477/* See breakpoint.h. */
0101ce28 9478
8cdf0e15
VP
9479int
9480create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9481 const struct event_location *location,
9482 const char *cond_string,
9483 int thread, const char *extra_string,
f00aae0f 9484 int parse_extra,
0fb4aa4b 9485 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9486 int ignore_count,
9487 enum auto_boolean pending_break_support,
c0a91b2b 9488 const struct breakpoint_ops *ops,
44f238bb
PA
9489 int from_tty, int enabled, int internal,
9490 unsigned flags)
c3f6f71d 9491{
7efd8fc2 9492 struct linespec_result canonical;
80c99de1 9493 struct cleanup *bkpt_chain = NULL;
0101ce28 9494 int pending = 0;
4a306c9a 9495 int task = 0;
86b17b60 9496 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9497
348d480f
PA
9498 gdb_assert (ops != NULL);
9499
f00aae0f
KS
9500 /* If extra_string isn't useful, set it to NULL. */
9501 if (extra_string != NULL && *extra_string == '\0')
9502 extra_string = NULL;
9503
492d29ea 9504 TRY
b78a6381 9505 {
f00aae0f 9506 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9507 }
492d29ea 9508 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9509 {
492d29ea
PA
9510 /* If caller is interested in rc value from parse, set
9511 value. */
9512 if (e.error == NOT_FOUND_ERROR)
0101ce28 9513 {
05ff989b
AC
9514 /* If pending breakpoint support is turned off, throw
9515 error. */
fa8d40ab
JJ
9516
9517 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9518 throw_exception (e);
9519
9520 exception_print (gdb_stderr, e);
fa8d40ab 9521
05ff989b
AC
9522 /* If pending breakpoint support is auto query and the user
9523 selects no, then simply return the error code. */
059fb39f 9524 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9525 && !nquery (_("Make %s pending on future shared library load? "),
9526 bptype_string (type_wanted)))
fd9b8c24 9527 return 0;
fa8d40ab 9528
05ff989b
AC
9529 /* At this point, either the user was queried about setting
9530 a pending breakpoint and selected yes, or pending
9531 breakpoint behavior is on and thus a pending breakpoint
9532 is defaulted on behalf of the user. */
f00aae0f 9533 pending = 1;
0101ce28 9534 }
492d29ea
PA
9535 else
9536 throw_exception (e);
0101ce28 9537 }
492d29ea
PA
9538 END_CATCH
9539
6c5b2ebe 9540 if (!pending && canonical.lsals.empty ())
492d29ea 9541 return 0;
c3f6f71d 9542
c3f6f71d
JM
9543 /* ----------------------------- SNIP -----------------------------
9544 Anything added to the cleanup chain beyond this point is assumed
9545 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9546 then the memory is not reclaimed. */
9547 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9548
c3f6f71d
JM
9549 /* Resolve all line numbers to PC's and verify that the addresses
9550 are ok for the target. */
0101ce28 9551 if (!pending)
f8eba3c6 9552 {
6c5b2ebe
PA
9553 for (auto &lsal : canonical.lsals)
9554 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9555 }
c3f6f71d 9556
7a697b8d 9557 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9558 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9559 {
6c5b2ebe
PA
9560 for (const auto &lsal : canonical.lsals)
9561 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9562 }
7a697b8d 9563
c3f6f71d
JM
9564 /* Verify that condition can be parsed, before setting any
9565 breakpoints. Allocate a separate condition expression for each
4a64f543 9566 breakpoint. */
0101ce28 9567 if (!pending)
c3f6f71d 9568 {
e1e01040
PA
9569 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9570 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9571
f00aae0f 9572 if (parse_extra)
72b2ff0e 9573 {
0878d0fa 9574 char *rest;
e1e01040 9575 char *cond;
52d361e1 9576
6c5b2ebe 9577 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9578
0878d0fa
YQ
9579 /* Here we only parse 'arg' to separate condition
9580 from thread number, so parsing in context of first
9581 sal is OK. When setting the breakpoint we'll
9582 re-parse it in context of each sal. */
9583
6c5b2ebe 9584 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9585 &cond, &thread, &task, &rest);
9586 cond_string_copy.reset (cond);
9587 extra_string_copy.reset (rest);
72b2ff0e 9588 }
2f069f6f 9589 else
72b2ff0e 9590 {
f00aae0f
KS
9591 if (type_wanted != bp_dprintf
9592 && extra_string != NULL && *extra_string != '\0')
9593 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9594
9595 /* Create a private copy of condition string. */
9596 if (cond_string)
e1e01040 9597 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9598 /* Create a private copy of any extra string. */
9599 if (extra_string)
e1e01040 9600 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9601 }
0fb4aa4b 9602
52d361e1 9603 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9604 std::move (cond_string_copy),
9605 std::move (extra_string_copy),
9606 type_wanted,
d9b3f62e
PA
9607 tempflag ? disp_del : disp_donttouch,
9608 thread, task, ignore_count, ops,
44f238bb 9609 from_tty, enabled, internal, flags);
c906108c 9610 }
0101ce28
JJ
9611 else
9612 {
a5e364af 9613 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9614
a5e364af 9615 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9616 b->location = copy_event_location (location);
bfccc43c 9617
f00aae0f
KS
9618 if (parse_extra)
9619 b->cond_string = NULL;
e12c7713
MK
9620 else
9621 {
9622 /* Create a private copy of condition string. */
e1e01040 9623 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9624 b->thread = thread;
e12c7713 9625 }
f00aae0f
KS
9626
9627 /* Create a private copy of any extra string. */
e1e01040 9628 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9629 b->ignore_count = ignore_count;
0101ce28 9630 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9631 b->condition_not_parsed = 1;
41447f92 9632 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9633 if ((type_wanted != bp_breakpoint
9634 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9635 b->pspace = current_program_space;
8bea4e01 9636
b270e6f9 9637 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9638 }
9639
6c5b2ebe 9640 if (canonical.lsals.size () > 1)
95a42b64 9641 {
3e43a32a
MS
9642 warning (_("Multiple breakpoints were set.\nUse the "
9643 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9644 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9645 }
9646
80c99de1
PA
9647 /* That's it. Discard the cleanups for data inserted into the
9648 breakpoint. */
9649 discard_cleanups (bkpt_chain);
217dc9e2 9650
80c99de1 9651 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9652 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9653
9654 return 1;
c3f6f71d 9655}
c906108c 9656
348d480f 9657/* Set a breakpoint.
72b2ff0e
VP
9658 ARG is a string describing breakpoint address,
9659 condition, and thread.
9660 FLAG specifies if a breakpoint is hardware on,
9661 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9662 and BP_TEMPFLAG. */
348d480f 9663
98deb0da 9664static void
f2fc3015 9665break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9666{
72b2ff0e 9667 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9668 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9669 ? bp_hardware_breakpoint
9670 : bp_breakpoint);
55aa24fb 9671 struct breakpoint_ops *ops;
f00aae0f 9672
ffc2605c 9673 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9674
9675 /* Matching breakpoints on probes. */
5b56227b 9676 if (location != NULL
ffc2605c 9677 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9678 ops = &bkpt_probe_breakpoint_ops;
9679 else
9680 ops = &bkpt_breakpoint_ops;
c3f6f71d 9681
8cdf0e15 9682 create_breakpoint (get_current_arch (),
ffc2605c 9683 location.get (),
f00aae0f 9684 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9685 tempflag, type_wanted,
8cdf0e15
VP
9686 0 /* Ignore count */,
9687 pending_break_support,
55aa24fb 9688 ops,
8cdf0e15 9689 from_tty,
84f4c1fe 9690 1 /* enabled */,
44f238bb
PA
9691 0 /* internal */,
9692 0);
c906108c
SS
9693}
9694
c906108c
SS
9695/* Helper function for break_command_1 and disassemble_command. */
9696
9697void
fba45db2 9698resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9699{
9700 CORE_ADDR pc;
9701
9702 if (sal->pc == 0 && sal->symtab != NULL)
9703 {
9704 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9705 error (_("No line %d in file \"%s\"."),
05cba821 9706 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9707 sal->pc = pc;
6a048695 9708
4a64f543
MS
9709 /* If this SAL corresponds to a breakpoint inserted using a line
9710 number, then skip the function prologue if necessary. */
6a048695 9711 if (sal->explicit_line)
059acae7 9712 skip_prologue_sal (sal);
c906108c
SS
9713 }
9714
9715 if (sal->section == 0 && sal->symtab != NULL)
9716 {
346d1dfe 9717 const struct blockvector *bv;
3977b71f 9718 const struct block *b;
c5aa993b 9719 struct symbol *sym;
c906108c 9720
43f3e411
DE
9721 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9722 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9723 if (bv != NULL)
9724 {
7f0df278 9725 sym = block_linkage_function (b);
c906108c
SS
9726 if (sym != NULL)
9727 {
eb822aa6
DE
9728 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9729 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9730 sym);
c906108c
SS
9731 }
9732 else
9733 {
4a64f543
MS
9734 /* It really is worthwhile to have the section, so we'll
9735 just have to look harder. This case can be executed
9736 if we have line numbers but no functions (as can
9737 happen in assembly source). */
c906108c 9738
5ed8105e 9739 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9740 switch_to_program_space_and_thread (sal->pspace);
c906108c 9741
5ed8105e 9742 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9743 if (msym.minsym)
efd66ac6 9744 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9745 }
9746 }
9747 }
9748}
9749
9750void
fba45db2 9751break_command (char *arg, int from_tty)
c906108c 9752{
db107f19 9753 break_command_1 (arg, 0, from_tty);
c906108c
SS
9754}
9755
c906108c 9756void
fba45db2 9757tbreak_command (char *arg, int from_tty)
c906108c 9758{
db107f19 9759 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9760}
9761
c906108c 9762static void
fba45db2 9763hbreak_command (char *arg, int from_tty)
c906108c 9764{
db107f19 9765 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9766}
9767
9768static void
fba45db2 9769thbreak_command (char *arg, int from_tty)
c906108c 9770{
db107f19 9771 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9772}
9773
9774static void
fba45db2 9775stop_command (char *arg, int from_tty)
c906108c 9776{
a3f17187 9777 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9778Usage: stop in <function | address>\n\
a3f17187 9779 stop at <line>\n"));
c906108c
SS
9780}
9781
9782static void
4495129a 9783stopin_command (const char *arg, int from_tty)
c906108c
SS
9784{
9785 int badInput = 0;
9786
c5aa993b 9787 if (arg == (char *) NULL)
c906108c
SS
9788 badInput = 1;
9789 else if (*arg != '*')
9790 {
4495129a 9791 const char *argptr = arg;
c906108c
SS
9792 int hasColon = 0;
9793
4a64f543 9794 /* Look for a ':'. If this is a line number specification, then
53a5351d 9795 say it is bad, otherwise, it should be an address or
4a64f543 9796 function/method name. */
c906108c 9797 while (*argptr && !hasColon)
c5aa993b
JM
9798 {
9799 hasColon = (*argptr == ':');
9800 argptr++;
9801 }
c906108c
SS
9802
9803 if (hasColon)
c5aa993b 9804 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9805 else
c5aa993b 9806 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9807 }
9808
9809 if (badInput)
a3f17187 9810 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9811 else
db107f19 9812 break_command_1 (arg, 0, from_tty);
c906108c
SS
9813}
9814
9815static void
4495129a 9816stopat_command (const char *arg, int from_tty)
c906108c
SS
9817{
9818 int badInput = 0;
9819
c5aa993b 9820 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9821 badInput = 1;
9822 else
9823 {
4495129a 9824 const char *argptr = arg;
c906108c
SS
9825 int hasColon = 0;
9826
4a64f543
MS
9827 /* Look for a ':'. If there is a '::' then get out, otherwise
9828 it is probably a line number. */
c906108c 9829 while (*argptr && !hasColon)
c5aa993b
JM
9830 {
9831 hasColon = (*argptr == ':');
9832 argptr++;
9833 }
c906108c
SS
9834
9835 if (hasColon)
c5aa993b 9836 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9837 else
c5aa993b 9838 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9839 }
9840
9841 if (badInput)
a3f17187 9842 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9843 else
db107f19 9844 break_command_1 (arg, 0, from_tty);
c906108c
SS
9845}
9846
e7e0cddf
SS
9847/* The dynamic printf command is mostly like a regular breakpoint, but
9848 with a prewired command list consisting of a single output command,
9849 built from extra arguments supplied on the dprintf command
9850 line. */
9851
da821c7b 9852static void
f2fc3015 9853dprintf_command (char *arg_in, int from_tty)
e7e0cddf 9854{
f2fc3015 9855 const char *arg = arg_in;
ffc2605c 9856 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9857
9858 /* If non-NULL, ARG should have been advanced past the location;
9859 the next character must be ','. */
9860 if (arg != NULL)
9861 {
9862 if (arg[0] != ',' || arg[1] == '\0')
9863 error (_("Format string required"));
9864 else
9865 {
9866 /* Skip the comma. */
9867 ++arg;
9868 }
9869 }
9870
e7e0cddf 9871 create_breakpoint (get_current_arch (),
ffc2605c 9872 location.get (),
f00aae0f 9873 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9874 0, bp_dprintf,
9875 0 /* Ignore count */,
9876 pending_break_support,
9877 &dprintf_breakpoint_ops,
9878 from_tty,
9879 1 /* enabled */,
9880 0 /* internal */,
9881 0);
9882}
9883
d3ce09f5
SS
9884static void
9885agent_printf_command (char *arg, int from_tty)
9886{
9887 error (_("May only run agent-printf on the target"));
9888}
9889
f1310107
TJB
9890/* Implement the "breakpoint_hit" breakpoint_ops method for
9891 ranged breakpoints. */
9892
9893static int
9894breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9895 struct address_space *aspace,
09ac7c10
TT
9896 CORE_ADDR bp_addr,
9897 const struct target_waitstatus *ws)
f1310107 9898{
09ac7c10 9899 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9900 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9901 return 0;
9902
f1310107
TJB
9903 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9904 bl->length, aspace, bp_addr);
9905}
9906
9907/* Implement the "resources_needed" breakpoint_ops method for
9908 ranged breakpoints. */
9909
9910static int
9911resources_needed_ranged_breakpoint (const struct bp_location *bl)
9912{
9913 return target_ranged_break_num_registers ();
9914}
9915
9916/* Implement the "print_it" breakpoint_ops method for
9917 ranged breakpoints. */
9918
9919static enum print_stop_action
348d480f 9920print_it_ranged_breakpoint (bpstat bs)
f1310107 9921{
348d480f 9922 struct breakpoint *b = bs->breakpoint_at;
f1310107 9923 struct bp_location *bl = b->loc;
79a45e25 9924 struct ui_out *uiout = current_uiout;
f1310107
TJB
9925
9926 gdb_assert (b->type == bp_hardware_breakpoint);
9927
9928 /* Ranged breakpoints have only one location. */
9929 gdb_assert (bl && bl->next == NULL);
9930
9931 annotate_breakpoint (b->number);
f303dbd6
PA
9932
9933 maybe_print_thread_hit_breakpoint (uiout);
9934
f1310107 9935 if (b->disposition == disp_del)
112e8700 9936 uiout->text ("Temporary ranged breakpoint ");
f1310107 9937 else
112e8700
SM
9938 uiout->text ("Ranged breakpoint ");
9939 if (uiout->is_mi_like_p ())
f1310107 9940 {
112e8700 9941 uiout->field_string ("reason",
f1310107 9942 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9943 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9944 }
112e8700
SM
9945 uiout->field_int ("bkptno", b->number);
9946 uiout->text (", ");
f1310107
TJB
9947
9948 return PRINT_SRC_AND_LOC;
9949}
9950
9951/* Implement the "print_one" breakpoint_ops method for
9952 ranged breakpoints. */
9953
9954static void
9955print_one_ranged_breakpoint (struct breakpoint *b,
9956 struct bp_location **last_loc)
9957{
9958 struct bp_location *bl = b->loc;
9959 struct value_print_options opts;
79a45e25 9960 struct ui_out *uiout = current_uiout;
f1310107
TJB
9961
9962 /* Ranged breakpoints have only one location. */
9963 gdb_assert (bl && bl->next == NULL);
9964
9965 get_user_print_options (&opts);
9966
9967 if (opts.addressprint)
9968 /* We don't print the address range here, it will be printed later
9969 by print_one_detail_ranged_breakpoint. */
112e8700 9970 uiout->field_skip ("addr");
f1310107
TJB
9971 annotate_field (5);
9972 print_breakpoint_location (b, bl);
9973 *last_loc = bl;
9974}
9975
9976/* Implement the "print_one_detail" breakpoint_ops method for
9977 ranged breakpoints. */
9978
9979static void
9980print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9981 struct ui_out *uiout)
9982{
9983 CORE_ADDR address_start, address_end;
9984 struct bp_location *bl = b->loc;
d7e74731 9985 string_file stb;
f1310107
TJB
9986
9987 gdb_assert (bl);
9988
9989 address_start = bl->address;
9990 address_end = address_start + bl->length - 1;
9991
112e8700 9992 uiout->text ("\taddress range: ");
d7e74731
PA
9993 stb.printf ("[%s, %s]",
9994 print_core_address (bl->gdbarch, address_start),
9995 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9996 uiout->field_stream ("addr", stb);
9997 uiout->text ("\n");
f1310107
TJB
9998}
9999
10000/* Implement the "print_mention" breakpoint_ops method for
10001 ranged breakpoints. */
10002
10003static void
10004print_mention_ranged_breakpoint (struct breakpoint *b)
10005{
10006 struct bp_location *bl = b->loc;
79a45e25 10007 struct ui_out *uiout = current_uiout;
f1310107
TJB
10008
10009 gdb_assert (bl);
10010 gdb_assert (b->type == bp_hardware_breakpoint);
10011
112e8700 10012 if (uiout->is_mi_like_p ())
f1310107
TJB
10013 return;
10014
10015 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10016 b->number, paddress (bl->gdbarch, bl->address),
10017 paddress (bl->gdbarch, bl->address + bl->length - 1));
10018}
10019
10020/* Implement the "print_recreate" breakpoint_ops method for
10021 ranged breakpoints. */
10022
10023static void
10024print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10025{
f00aae0f 10026 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10027 event_location_to_string (b->location.get ()),
10028 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10029 print_recreate_thread (b, fp);
f1310107
TJB
10030}
10031
10032/* The breakpoint_ops structure to be used in ranged breakpoints. */
10033
2060206e 10034static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10035
10036/* Find the address where the end of the breakpoint range should be
10037 placed, given the SAL of the end of the range. This is so that if
10038 the user provides a line number, the end of the range is set to the
10039 last instruction of the given line. */
10040
10041static CORE_ADDR
10042find_breakpoint_range_end (struct symtab_and_line sal)
10043{
10044 CORE_ADDR end;
10045
10046 /* If the user provided a PC value, use it. Otherwise,
10047 find the address of the end of the given location. */
10048 if (sal.explicit_pc)
10049 end = sal.pc;
10050 else
10051 {
10052 int ret;
10053 CORE_ADDR start;
10054
10055 ret = find_line_pc_range (sal, &start, &end);
10056 if (!ret)
10057 error (_("Could not find location of the end of the range."));
10058
10059 /* find_line_pc_range returns the start of the next line. */
10060 end--;
10061 }
10062
10063 return end;
10064}
10065
10066/* Implement the "break-range" CLI command. */
10067
10068static void
f2fc3015 10069break_range_command (char *arg_in, int from_tty)
f1310107 10070{
f2fc3015
TT
10071 const char *arg = arg_in;
10072 const char *arg_start;
10073 char *addr_string_start;
f1310107
TJB
10074 struct linespec_result canonical_start, canonical_end;
10075 int bp_count, can_use_bp, length;
10076 CORE_ADDR end;
10077 struct breakpoint *b;
f1310107
TJB
10078 struct cleanup *cleanup_bkpt;
10079
10080 /* We don't support software ranged breakpoints. */
10081 if (target_ranged_break_num_registers () < 0)
10082 error (_("This target does not support hardware ranged breakpoints."));
10083
10084 bp_count = hw_breakpoint_used_count ();
10085 bp_count += target_ranged_break_num_registers ();
10086 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10087 bp_count, 0);
10088 if (can_use_bp < 0)
10089 error (_("Hardware breakpoints used exceeds limit."));
10090
f8eba3c6 10091 arg = skip_spaces (arg);
f1310107
TJB
10092 if (arg == NULL || arg[0] == '\0')
10093 error(_("No address range specified."));
10094
f8eba3c6 10095 arg_start = arg;
ffc2605c
TT
10096 event_location_up start_location = string_to_event_location (&arg,
10097 current_language);
10098 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10099
10100 if (arg[0] != ',')
10101 error (_("Too few arguments."));
6c5b2ebe 10102 else if (canonical_start.lsals.empty ())
f1310107 10103 error (_("Could not find location of the beginning of the range."));
f8eba3c6 10104
6c5b2ebe 10105 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 10106
6c5b2ebe
PA
10107 if (canonical_start.lsals.size () > 1
10108 || lsal_start.sals.size () != 1)
f1310107
TJB
10109 error (_("Cannot create a ranged breakpoint with multiple locations."));
10110
6c5b2ebe 10111 const symtab_and_line &sal_start = lsal_start.sals[0];
f8eba3c6 10112 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10113 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10114
10115 arg++; /* Skip the comma. */
f8eba3c6 10116 arg = skip_spaces (arg);
f1310107
TJB
10117
10118 /* Parse the end location. */
10119
f1310107
TJB
10120 arg_start = arg;
10121
f8eba3c6 10122 /* We call decode_line_full directly here instead of using
f1310107
TJB
10123 parse_breakpoint_sals because we need to specify the start location's
10124 symtab and line as the default symtab and line for the end of the
10125 range. This makes it possible to have ranges like "foo.c:27, +14",
10126 where +14 means 14 lines from the start location. */
ffc2605c
TT
10127 event_location_up end_location = string_to_event_location (&arg,
10128 current_language);
10129 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10130 sal_start.symtab, sal_start.line,
10131 &canonical_end, NULL, NULL);
10132
6c5b2ebe 10133 if (canonical_end.lsals.empty ())
f1310107 10134 error (_("Could not find location of the end of the range."));
f8eba3c6 10135
6c5b2ebe
PA
10136 const linespec_sals &lsal_end = canonical_end.lsals[0];
10137 if (canonical_end.lsals.size () > 1
10138 || lsal_end.sals.size () != 1)
f1310107
TJB
10139 error (_("Cannot create a ranged breakpoint with multiple locations."));
10140
6c5b2ebe 10141 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10142
10143 end = find_breakpoint_range_end (sal_end);
10144 if (sal_start.pc > end)
177b42fe 10145 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10146
10147 length = end - sal_start.pc + 1;
10148 if (length < 0)
10149 /* Length overflowed. */
10150 error (_("Address range too large."));
10151 else if (length == 1)
10152 {
10153 /* This range is simple enough to be handled by
10154 the `hbreak' command. */
10155 hbreak_command (addr_string_start, 1);
10156
10157 do_cleanups (cleanup_bkpt);
10158
10159 return;
10160 }
10161
10162 /* Now set up the breakpoint. */
10163 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10164 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10165 set_breakpoint_count (breakpoint_count + 1);
10166 b->number = breakpoint_count;
10167 b->disposition = disp_donttouch;
d28cd78a
TT
10168 b->location = std::move (start_location);
10169 b->location_range_end = std::move (end_location);
f1310107
TJB
10170 b->loc->length = length;
10171
f8eba3c6 10172 do_cleanups (cleanup_bkpt);
f1310107
TJB
10173
10174 mention (b);
8d3788bd 10175 observer_notify_breakpoint_created (b);
44702360 10176 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10177}
10178
4a64f543
MS
10179/* Return non-zero if EXP is verified as constant. Returned zero
10180 means EXP is variable. Also the constant detection may fail for
10181 some constant expressions and in such case still falsely return
10182 zero. */
2e6e3d9c 10183
65d79d4b
SDJ
10184static int
10185watchpoint_exp_is_const (const struct expression *exp)
10186{
10187 int i = exp->nelts;
10188
10189 while (i > 0)
10190 {
10191 int oplenp, argsp;
10192
10193 /* We are only interested in the descriptor of each element. */
10194 operator_length (exp, i, &oplenp, &argsp);
10195 i -= oplenp;
10196
10197 switch (exp->elts[i].opcode)
10198 {
10199 case BINOP_ADD:
10200 case BINOP_SUB:
10201 case BINOP_MUL:
10202 case BINOP_DIV:
10203 case BINOP_REM:
10204 case BINOP_MOD:
10205 case BINOP_LSH:
10206 case BINOP_RSH:
10207 case BINOP_LOGICAL_AND:
10208 case BINOP_LOGICAL_OR:
10209 case BINOP_BITWISE_AND:
10210 case BINOP_BITWISE_IOR:
10211 case BINOP_BITWISE_XOR:
10212 case BINOP_EQUAL:
10213 case BINOP_NOTEQUAL:
10214 case BINOP_LESS:
10215 case BINOP_GTR:
10216 case BINOP_LEQ:
10217 case BINOP_GEQ:
10218 case BINOP_REPEAT:
10219 case BINOP_COMMA:
10220 case BINOP_EXP:
10221 case BINOP_MIN:
10222 case BINOP_MAX:
10223 case BINOP_INTDIV:
10224 case BINOP_CONCAT:
65d79d4b
SDJ
10225 case TERNOP_COND:
10226 case TERNOP_SLICE:
65d79d4b
SDJ
10227
10228 case OP_LONG:
10229 case OP_DOUBLE:
10230 case OP_DECFLOAT:
10231 case OP_LAST:
10232 case OP_COMPLEX:
10233 case OP_STRING:
65d79d4b
SDJ
10234 case OP_ARRAY:
10235 case OP_TYPE:
608b4967
TT
10236 case OP_TYPEOF:
10237 case OP_DECLTYPE:
6e72ca20 10238 case OP_TYPEID:
65d79d4b
SDJ
10239 case OP_NAME:
10240 case OP_OBJC_NSSTRING:
10241
10242 case UNOP_NEG:
10243 case UNOP_LOGICAL_NOT:
10244 case UNOP_COMPLEMENT:
10245 case UNOP_ADDR:
10246 case UNOP_HIGH:
aeaa2474 10247 case UNOP_CAST:
9eaf6705
TT
10248
10249 case UNOP_CAST_TYPE:
10250 case UNOP_REINTERPRET_CAST:
10251 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10252 /* Unary, binary and ternary operators: We have to check
10253 their operands. If they are constant, then so is the
10254 result of that operation. For instance, if A and B are
10255 determined to be constants, then so is "A + B".
10256
10257 UNOP_IND is one exception to the rule above, because the
10258 value of *ADDR is not necessarily a constant, even when
10259 ADDR is. */
65d79d4b
SDJ
10260 break;
10261
10262 case OP_VAR_VALUE:
10263 /* Check whether the associated symbol is a constant.
4a64f543 10264
65d79d4b 10265 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10266 possible that a buggy compiler could mark a variable as
10267 constant even when it is not, and TYPE_CONST would return
10268 true in this case, while SYMBOL_CLASS wouldn't.
10269
10270 We also have to check for function symbols because they
10271 are always constant. */
65d79d4b
SDJ
10272 {
10273 struct symbol *s = exp->elts[i + 2].symbol;
10274
10275 if (SYMBOL_CLASS (s) != LOC_BLOCK
10276 && SYMBOL_CLASS (s) != LOC_CONST
10277 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10278 return 0;
10279 break;
10280 }
10281
10282 /* The default action is to return 0 because we are using
10283 the optimistic approach here: If we don't know something,
10284 then it is not a constant. */
10285 default:
10286 return 0;
10287 }
10288 }
10289
10290 return 1;
10291}
10292
c1fc2657 10293/* Watchpoint destructor. */
3a5c3e22 10294
c1fc2657 10295watchpoint::~watchpoint ()
3a5c3e22 10296{
c1fc2657
SM
10297 xfree (this->exp_string);
10298 xfree (this->exp_string_reparse);
10299 value_free (this->val);
3a5c3e22
PA
10300}
10301
348d480f
PA
10302/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10303
10304static void
10305re_set_watchpoint (struct breakpoint *b)
10306{
3a5c3e22
PA
10307 struct watchpoint *w = (struct watchpoint *) b;
10308
348d480f
PA
10309 /* Watchpoint can be either on expression using entirely global
10310 variables, or it can be on local variables.
10311
10312 Watchpoints of the first kind are never auto-deleted, and even
10313 persist across program restarts. Since they can use variables
10314 from shared libraries, we need to reparse expression as libraries
10315 are loaded and unloaded.
10316
10317 Watchpoints on local variables can also change meaning as result
10318 of solib event. For example, if a watchpoint uses both a local
10319 and a global variables in expression, it's a local watchpoint,
10320 but unloading of a shared library will make the expression
10321 invalid. This is not a very common use case, but we still
10322 re-evaluate expression, to avoid surprises to the user.
10323
10324 Note that for local watchpoints, we re-evaluate it only if
10325 watchpoints frame id is still valid. If it's not, it means the
10326 watchpoint is out of scope and will be deleted soon. In fact,
10327 I'm not sure we'll ever be called in this case.
10328
10329 If a local watchpoint's frame id is still valid, then
3a5c3e22 10330 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10331
3a5c3e22
PA
10332 Don't do anything about disabled watchpoints, since they will be
10333 reevaluated again when enabled. */
10334 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10335}
10336
77b06cd7
TJB
10337/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10338
10339static int
10340insert_watchpoint (struct bp_location *bl)
10341{
3a5c3e22
PA
10342 struct watchpoint *w = (struct watchpoint *) bl->owner;
10343 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10344
10345 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10346 w->cond_exp.get ());
77b06cd7
TJB
10347}
10348
10349/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10350
10351static int
73971819 10352remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10353{
3a5c3e22
PA
10354 struct watchpoint *w = (struct watchpoint *) bl->owner;
10355 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10356
10357 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10358 w->cond_exp.get ());
e09342b5
TJB
10359}
10360
e09342b5 10361static int
348d480f 10362breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10363 struct address_space *aspace, CORE_ADDR bp_addr,
10364 const struct target_waitstatus *ws)
e09342b5 10365{
348d480f 10366 struct breakpoint *b = bl->owner;
3a5c3e22 10367 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10368
348d480f
PA
10369 /* Continuable hardware watchpoints are treated as non-existent if the
10370 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10371 some data address). Otherwise gdb won't stop on a break instruction
10372 in the code (not from a breakpoint) when a hardware watchpoint has
10373 been defined. Also skip watchpoints which we know did not trigger
10374 (did not match the data address). */
10375 if (is_hardware_watchpoint (b)
3a5c3e22 10376 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10377 return 0;
9c06b0b4 10378
348d480f 10379 return 1;
9c06b0b4
TJB
10380}
10381
348d480f
PA
10382static void
10383check_status_watchpoint (bpstat bs)
9c06b0b4 10384{
348d480f 10385 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10386
348d480f 10387 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10388}
10389
10390/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10391 hardware watchpoints. */
9c06b0b4
TJB
10392
10393static int
348d480f 10394resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10395{
3a5c3e22
PA
10396 struct watchpoint *w = (struct watchpoint *) bl->owner;
10397 int length = w->exact? 1 : bl->length;
348d480f
PA
10398
10399 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10400}
10401
10402/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10403 hardware watchpoints. */
9c06b0b4
TJB
10404
10405static int
348d480f 10406works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10407{
efa80663
PA
10408 /* Read and access watchpoints only work with hardware support. */
10409 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10410}
10411
9c06b0b4 10412static enum print_stop_action
348d480f 10413print_it_watchpoint (bpstat bs)
9c06b0b4 10414{
348d480f 10415 struct breakpoint *b;
348d480f 10416 enum print_stop_action result;
3a5c3e22 10417 struct watchpoint *w;
79a45e25 10418 struct ui_out *uiout = current_uiout;
348d480f
PA
10419
10420 gdb_assert (bs->bp_location_at != NULL);
10421
348d480f 10422 b = bs->breakpoint_at;
3a5c3e22 10423 w = (struct watchpoint *) b;
348d480f 10424
f303dbd6
PA
10425 annotate_watchpoint (b->number);
10426 maybe_print_thread_hit_breakpoint (uiout);
10427
d7e74731
PA
10428 string_file stb;
10429
76f9c9cf 10430 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10431 switch (b->type)
10432 {
348d480f 10433 case bp_watchpoint:
9c06b0b4 10434 case bp_hardware_watchpoint:
112e8700
SM
10435 if (uiout->is_mi_like_p ())
10436 uiout->field_string
10437 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10438 mention (b);
76f9c9cf 10439 tuple_emitter.emplace (uiout, "value");
112e8700 10440 uiout->text ("\nOld value = ");
d7e74731 10441 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10442 uiout->field_stream ("old", stb);
10443 uiout->text ("\nNew value = ");
d7e74731 10444 watchpoint_value_print (w->val, &stb);
112e8700
SM
10445 uiout->field_stream ("new", stb);
10446 uiout->text ("\n");
348d480f
PA
10447 /* More than one watchpoint may have been triggered. */
10448 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10449 break;
10450
10451 case bp_read_watchpoint:
112e8700
SM
10452 if (uiout->is_mi_like_p ())
10453 uiout->field_string
10454 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10455 mention (b);
76f9c9cf 10456 tuple_emitter.emplace (uiout, "value");
112e8700 10457 uiout->text ("\nValue = ");
d7e74731 10458 watchpoint_value_print (w->val, &stb);
112e8700
SM
10459 uiout->field_stream ("value", stb);
10460 uiout->text ("\n");
348d480f 10461 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10462 break;
10463
10464 case bp_access_watchpoint:
348d480f
PA
10465 if (bs->old_val != NULL)
10466 {
112e8700
SM
10467 if (uiout->is_mi_like_p ())
10468 uiout->field_string
10469 ("reason",
348d480f
PA
10470 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10471 mention (b);
76f9c9cf 10472 tuple_emitter.emplace (uiout, "value");
112e8700 10473 uiout->text ("\nOld value = ");
d7e74731 10474 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10475 uiout->field_stream ("old", stb);
10476 uiout->text ("\nNew value = ");
348d480f
PA
10477 }
10478 else
10479 {
10480 mention (b);
112e8700
SM
10481 if (uiout->is_mi_like_p ())
10482 uiout->field_string
10483 ("reason",
348d480f 10484 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10485 tuple_emitter.emplace (uiout, "value");
112e8700 10486 uiout->text ("\nValue = ");
348d480f 10487 }
d7e74731 10488 watchpoint_value_print (w->val, &stb);
112e8700
SM
10489 uiout->field_stream ("new", stb);
10490 uiout->text ("\n");
348d480f 10491 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10492 break;
10493 default:
348d480f 10494 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10495 }
10496
348d480f
PA
10497 return result;
10498}
10499
10500/* Implement the "print_mention" breakpoint_ops method for hardware
10501 watchpoints. */
10502
10503static void
10504print_mention_watchpoint (struct breakpoint *b)
10505{
3a5c3e22 10506 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10507 struct ui_out *uiout = current_uiout;
46b9c129 10508 const char *tuple_name;
348d480f
PA
10509
10510 switch (b->type)
10511 {
10512 case bp_watchpoint:
112e8700 10513 uiout->text ("Watchpoint ");
46b9c129 10514 tuple_name = "wpt";
348d480f
PA
10515 break;
10516 case bp_hardware_watchpoint:
112e8700 10517 uiout->text ("Hardware watchpoint ");
46b9c129 10518 tuple_name = "wpt";
348d480f
PA
10519 break;
10520 case bp_read_watchpoint:
112e8700 10521 uiout->text ("Hardware read watchpoint ");
46b9c129 10522 tuple_name = "hw-rwpt";
348d480f
PA
10523 break;
10524 case bp_access_watchpoint:
112e8700 10525 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10526 tuple_name = "hw-awpt";
348d480f
PA
10527 break;
10528 default:
10529 internal_error (__FILE__, __LINE__,
10530 _("Invalid hardware watchpoint type."));
10531 }
10532
46b9c129 10533 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10534 uiout->field_int ("number", b->number);
10535 uiout->text (": ");
10536 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10537}
10538
10539/* Implement the "print_recreate" breakpoint_ops method for
10540 watchpoints. */
10541
10542static void
10543print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10544{
3a5c3e22
PA
10545 struct watchpoint *w = (struct watchpoint *) b;
10546
348d480f
PA
10547 switch (b->type)
10548 {
10549 case bp_watchpoint:
10550 case bp_hardware_watchpoint:
10551 fprintf_unfiltered (fp, "watch");
10552 break;
10553 case bp_read_watchpoint:
10554 fprintf_unfiltered (fp, "rwatch");
10555 break;
10556 case bp_access_watchpoint:
10557 fprintf_unfiltered (fp, "awatch");
10558 break;
10559 default:
10560 internal_error (__FILE__, __LINE__,
10561 _("Invalid watchpoint type."));
10562 }
10563
3a5c3e22 10564 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10565 print_recreate_thread (b, fp);
348d480f
PA
10566}
10567
427cd150
TT
10568/* Implement the "explains_signal" breakpoint_ops method for
10569 watchpoints. */
10570
47591c29 10571static int
427cd150
TT
10572explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10573{
10574 /* A software watchpoint cannot cause a signal other than
10575 GDB_SIGNAL_TRAP. */
10576 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10577 return 0;
427cd150 10578
47591c29 10579 return 1;
427cd150
TT
10580}
10581
348d480f
PA
10582/* The breakpoint_ops structure to be used in hardware watchpoints. */
10583
2060206e 10584static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10585
10586/* Implement the "insert" breakpoint_ops method for
10587 masked hardware watchpoints. */
10588
10589static int
10590insert_masked_watchpoint (struct bp_location *bl)
10591{
3a5c3e22
PA
10592 struct watchpoint *w = (struct watchpoint *) bl->owner;
10593
10594 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10595 bl->watchpoint_type);
10596}
10597
10598/* Implement the "remove" breakpoint_ops method for
10599 masked hardware watchpoints. */
10600
10601static int
73971819 10602remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10603{
3a5c3e22
PA
10604 struct watchpoint *w = (struct watchpoint *) bl->owner;
10605
10606 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10607 bl->watchpoint_type);
10608}
10609
10610/* Implement the "resources_needed" breakpoint_ops method for
10611 masked hardware watchpoints. */
10612
10613static int
10614resources_needed_masked_watchpoint (const struct bp_location *bl)
10615{
3a5c3e22
PA
10616 struct watchpoint *w = (struct watchpoint *) bl->owner;
10617
10618 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10619}
10620
10621/* Implement the "works_in_software_mode" breakpoint_ops method for
10622 masked hardware watchpoints. */
10623
10624static int
10625works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10626{
10627 return 0;
10628}
10629
10630/* Implement the "print_it" breakpoint_ops method for
10631 masked hardware watchpoints. */
10632
10633static enum print_stop_action
10634print_it_masked_watchpoint (bpstat bs)
10635{
10636 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10637 struct ui_out *uiout = current_uiout;
348d480f
PA
10638
10639 /* Masked watchpoints have only one location. */
10640 gdb_assert (b->loc && b->loc->next == NULL);
10641
f303dbd6
PA
10642 annotate_watchpoint (b->number);
10643 maybe_print_thread_hit_breakpoint (uiout);
10644
348d480f
PA
10645 switch (b->type)
10646 {
10647 case bp_hardware_watchpoint:
112e8700
SM
10648 if (uiout->is_mi_like_p ())
10649 uiout->field_string
10650 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10651 break;
10652
10653 case bp_read_watchpoint:
112e8700
SM
10654 if (uiout->is_mi_like_p ())
10655 uiout->field_string
10656 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10657 break;
10658
10659 case bp_access_watchpoint:
112e8700
SM
10660 if (uiout->is_mi_like_p ())
10661 uiout->field_string
10662 ("reason",
348d480f
PA
10663 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10664 break;
10665 default:
10666 internal_error (__FILE__, __LINE__,
10667 _("Invalid hardware watchpoint type."));
10668 }
10669
10670 mention (b);
112e8700 10671 uiout->text (_("\n\
9c06b0b4
TJB
10672Check the underlying instruction at PC for the memory\n\
10673address and value which triggered this watchpoint.\n"));
112e8700 10674 uiout->text ("\n");
9c06b0b4
TJB
10675
10676 /* More than one watchpoint may have been triggered. */
10677 return PRINT_UNKNOWN;
10678}
10679
10680/* Implement the "print_one_detail" breakpoint_ops method for
10681 masked hardware watchpoints. */
10682
10683static void
10684print_one_detail_masked_watchpoint (const struct breakpoint *b,
10685 struct ui_out *uiout)
10686{
3a5c3e22
PA
10687 struct watchpoint *w = (struct watchpoint *) b;
10688
9c06b0b4
TJB
10689 /* Masked watchpoints have only one location. */
10690 gdb_assert (b->loc && b->loc->next == NULL);
10691
112e8700
SM
10692 uiout->text ("\tmask ");
10693 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10694 uiout->text ("\n");
9c06b0b4
TJB
10695}
10696
10697/* Implement the "print_mention" breakpoint_ops method for
10698 masked hardware watchpoints. */
10699
10700static void
10701print_mention_masked_watchpoint (struct breakpoint *b)
10702{
3a5c3e22 10703 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10704 struct ui_out *uiout = current_uiout;
46b9c129 10705 const char *tuple_name;
9c06b0b4
TJB
10706
10707 switch (b->type)
10708 {
10709 case bp_hardware_watchpoint:
112e8700 10710 uiout->text ("Masked hardware watchpoint ");
46b9c129 10711 tuple_name = "wpt";
9c06b0b4
TJB
10712 break;
10713 case bp_read_watchpoint:
112e8700 10714 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10715 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10716 break;
10717 case bp_access_watchpoint:
112e8700 10718 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10719 tuple_name = "hw-awpt";
9c06b0b4
TJB
10720 break;
10721 default:
10722 internal_error (__FILE__, __LINE__,
10723 _("Invalid hardware watchpoint type."));
10724 }
10725
46b9c129 10726 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10727 uiout->field_int ("number", b->number);
10728 uiout->text (": ");
10729 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10730}
10731
10732/* Implement the "print_recreate" breakpoint_ops method for
10733 masked hardware watchpoints. */
10734
10735static void
10736print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10737{
3a5c3e22 10738 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10739 char tmp[40];
10740
10741 switch (b->type)
10742 {
10743 case bp_hardware_watchpoint:
10744 fprintf_unfiltered (fp, "watch");
10745 break;
10746 case bp_read_watchpoint:
10747 fprintf_unfiltered (fp, "rwatch");
10748 break;
10749 case bp_access_watchpoint:
10750 fprintf_unfiltered (fp, "awatch");
10751 break;
10752 default:
10753 internal_error (__FILE__, __LINE__,
10754 _("Invalid hardware watchpoint type."));
10755 }
10756
3a5c3e22
PA
10757 sprintf_vma (tmp, w->hw_wp_mask);
10758 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10759 print_recreate_thread (b, fp);
9c06b0b4
TJB
10760}
10761
10762/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10763
2060206e 10764static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10765
10766/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10767
10768static int
10769is_masked_watchpoint (const struct breakpoint *b)
10770{
10771 return b->ops == &masked_watchpoint_breakpoint_ops;
10772}
10773
53a5351d
JM
10774/* accessflag: hw_write: watch write,
10775 hw_read: watch read,
10776 hw_access: watch access (read or write) */
c906108c 10777static void
bbc13ae3 10778watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10779 int just_location, int internal)
c906108c 10780{
c1fc2657 10781 struct breakpoint *scope_breakpoint = NULL;
270140bd 10782 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10783 struct value *val, *mark, *result;
bb9d5f81 10784 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10785 const char *exp_start = NULL;
10786 const char *exp_end = NULL;
10787 const char *tok, *end_tok;
9c06b0b4 10788 int toklen = -1;
bbc13ae3
KS
10789 const char *cond_start = NULL;
10790 const char *cond_end = NULL;
c906108c 10791 enum bptype bp_type;
37e4754d 10792 int thread = -1;
0cf6dd15 10793 int pc = 0;
9c06b0b4
TJB
10794 /* Flag to indicate whether we are going to use masks for
10795 the hardware watchpoint. */
10796 int use_mask = 0;
10797 CORE_ADDR mask = 0;
bbc13ae3
KS
10798 char *expression;
10799 struct cleanup *back_to;
c906108c 10800
37e4754d
LM
10801 /* Make sure that we actually have parameters to parse. */
10802 if (arg != NULL && arg[0] != '\0')
10803 {
bbc13ae3
KS
10804 const char *value_start;
10805
10806 exp_end = arg + strlen (arg);
37e4754d 10807
9c06b0b4
TJB
10808 /* Look for "parameter value" pairs at the end
10809 of the arguments string. */
bbc13ae3 10810 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10811 {
10812 /* Skip whitespace at the end of the argument list. */
10813 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10814 tok--;
10815
10816 /* Find the beginning of the last token.
10817 This is the value of the parameter. */
10818 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10819 tok--;
10820 value_start = tok + 1;
10821
10822 /* Skip whitespace. */
10823 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10824 tok--;
10825
10826 end_tok = tok;
10827
10828 /* Find the beginning of the second to last token.
10829 This is the parameter itself. */
10830 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10831 tok--;
10832 tok++;
10833 toklen = end_tok - tok + 1;
10834
61012eef 10835 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10836 {
5d5658a1 10837 struct thread_info *thr;
9c06b0b4
TJB
10838 /* At this point we've found a "thread" token, which means
10839 the user is trying to set a watchpoint that triggers
10840 only in a specific thread. */
5d5658a1 10841 const char *endp;
37e4754d 10842
9c06b0b4
TJB
10843 if (thread != -1)
10844 error(_("You can specify only one thread."));
37e4754d 10845
9c06b0b4 10846 /* Extract the thread ID from the next token. */
5d5658a1 10847 thr = parse_thread_id (value_start, &endp);
37e4754d 10848
5d5658a1 10849 /* Check if the user provided a valid thread ID. */
9c06b0b4 10850 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10851 invalid_thread_id_error (value_start);
9c06b0b4 10852
5d5658a1 10853 thread = thr->global_num;
9c06b0b4 10854 }
61012eef 10855 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10856 {
10857 /* We've found a "mask" token, which means the user wants to
10858 create a hardware watchpoint that is going to have the mask
10859 facility. */
10860 struct value *mask_value, *mark;
37e4754d 10861
9c06b0b4
TJB
10862 if (use_mask)
10863 error(_("You can specify only one mask."));
37e4754d 10864
9c06b0b4 10865 use_mask = just_location = 1;
37e4754d 10866
9c06b0b4
TJB
10867 mark = value_mark ();
10868 mask_value = parse_to_comma_and_eval (&value_start);
10869 mask = value_as_address (mask_value);
10870 value_free_to_mark (mark);
10871 }
10872 else
10873 /* We didn't recognize what we found. We should stop here. */
10874 break;
37e4754d 10875
9c06b0b4
TJB
10876 /* Truncate the string and get rid of the "parameter value" pair before
10877 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10878 exp_end = tok;
9c06b0b4 10879 }
37e4754d 10880 }
bbc13ae3
KS
10881 else
10882 exp_end = arg;
37e4754d 10883
bbc13ae3
KS
10884 /* Parse the rest of the arguments. From here on out, everything
10885 is in terms of a newly allocated string instead of the original
10886 ARG. */
c906108c 10887 innermost_block = NULL;
bbc13ae3
KS
10888 expression = savestring (arg, exp_end - arg);
10889 back_to = make_cleanup (xfree, expression);
10890 exp_start = arg = expression;
4d01a485 10891 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10892 exp_end = arg;
fa8a61dc
TT
10893 /* Remove trailing whitespace from the expression before saving it.
10894 This makes the eventual display of the expression string a bit
10895 prettier. */
10896 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10897 --exp_end;
10898
65d79d4b 10899 /* Checking if the expression is not constant. */
4d01a485 10900 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10901 {
10902 int len;
10903
10904 len = exp_end - exp_start;
10905 while (len > 0 && isspace (exp_start[len - 1]))
10906 len--;
10907 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10908 }
10909
c906108c
SS
10910 exp_valid_block = innermost_block;
10911 mark = value_mark ();
4d01a485 10912 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10913
bb9d5f81
PP
10914 if (val != NULL && just_location)
10915 {
10916 saved_bitpos = value_bitpos (val);
10917 saved_bitsize = value_bitsize (val);
10918 }
10919
06a64a0b
TT
10920 if (just_location)
10921 {
9c06b0b4
TJB
10922 int ret;
10923
06a64a0b 10924 exp_valid_block = NULL;
a1442452 10925 val = value_addr (result);
06a64a0b
TT
10926 release_value (val);
10927 value_free_to_mark (mark);
9c06b0b4
TJB
10928
10929 if (use_mask)
10930 {
10931 ret = target_masked_watch_num_registers (value_as_address (val),
10932 mask);
10933 if (ret == -1)
10934 error (_("This target does not support masked watchpoints."));
10935 else if (ret == -2)
10936 error (_("Invalid mask or memory region."));
10937 }
06a64a0b
TT
10938 }
10939 else if (val != NULL)
fa4727a6 10940 release_value (val);
c906108c 10941
f1735a53
TT
10942 tok = skip_spaces (arg);
10943 end_tok = skip_to_space (tok);
c906108c
SS
10944
10945 toklen = end_tok - tok;
10946 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10947 {
60e1c644 10948 innermost_block = NULL;
c906108c 10949 tok = cond_start = end_tok + 1;
4d01a485 10950 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10951
10952 /* The watchpoint expression may not be local, but the condition
10953 may still be. E.g.: `watch global if local > 0'. */
10954 cond_exp_valid_block = innermost_block;
10955
c906108c
SS
10956 cond_end = tok;
10957 }
10958 if (*tok)
8a3fe4f8 10959 error (_("Junk at end of command."));
c906108c 10960
441d7c93
PA
10961 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10962
10963 /* Save this because create_internal_breakpoint below invalidates
10964 'wp_frame'. */
10965 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10966
10967 /* If the expression is "local", then set up a "watchpoint scope"
10968 breakpoint at the point where we've left the scope of the watchpoint
10969 expression. Create the scope breakpoint before the watchpoint, so
10970 that we will encounter it first in bpstat_stop_status. */
441d7c93 10971 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10972 {
441d7c93
PA
10973 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10974
10975 if (frame_id_p (caller_frame_id))
edb3359d 10976 {
441d7c93
PA
10977 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10978 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10979
edb3359d 10980 scope_breakpoint
441d7c93 10981 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10982 bp_watchpoint_scope,
10983 &momentary_breakpoint_ops);
d983da9c 10984
441d7c93
PA
10985 /* create_internal_breakpoint could invalidate WP_FRAME. */
10986 wp_frame = NULL;
10987
edb3359d 10988 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10989
edb3359d
DJ
10990 /* Automatically delete the breakpoint when it hits. */
10991 scope_breakpoint->disposition = disp_del;
d983da9c 10992
edb3359d 10993 /* Only break in the proper frame (help with recursion). */
441d7c93 10994 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10995
edb3359d 10996 /* Set the address at which we will stop. */
441d7c93
PA
10997 scope_breakpoint->loc->gdbarch = caller_arch;
10998 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10999 scope_breakpoint->loc->address
a6d9a66e
UW
11000 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11001 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11002 scope_breakpoint->type);
11003 }
d983da9c
DJ
11004 }
11005
e8369a73
AB
11006 /* Now set up the breakpoint. We create all watchpoints as hardware
11007 watchpoints here even if hardware watchpoints are turned off, a call
11008 to update_watchpoint later in this function will cause the type to
11009 drop back to bp_watchpoint (software watchpoint) if required. */
11010
11011 if (accessflag == hw_read)
11012 bp_type = bp_read_watchpoint;
11013 else if (accessflag == hw_access)
11014 bp_type = bp_access_watchpoint;
11015 else
11016 bp_type = bp_hardware_watchpoint;
3a5c3e22 11017
b270e6f9 11018 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 11019
348d480f 11020 if (use_mask)
b270e6f9 11021 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11022 &masked_watchpoint_breakpoint_ops);
348d480f 11023 else
b270e6f9 11024 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11025 &watchpoint_breakpoint_ops);
c1fc2657
SM
11026 w->thread = thread;
11027 w->disposition = disp_donttouch;
11028 w->pspace = current_program_space;
b22e99fd 11029 w->exp = std::move (exp);
3a5c3e22
PA
11030 w->exp_valid_block = exp_valid_block;
11031 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11032 if (just_location)
11033 {
11034 struct type *t = value_type (val);
11035 CORE_ADDR addr = value_as_address (val);
06a64a0b 11036
43cc5389
TT
11037 w->exp_string_reparse
11038 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11039
3a5c3e22 11040 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11041 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11042 }
11043 else
3a5c3e22 11044 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11045
11046 if (use_mask)
11047 {
3a5c3e22 11048 w->hw_wp_mask = mask;
9c06b0b4
TJB
11049 }
11050 else
11051 {
3a5c3e22 11052 w->val = val;
bb9d5f81
PP
11053 w->val_bitpos = saved_bitpos;
11054 w->val_bitsize = saved_bitsize;
3a5c3e22 11055 w->val_valid = 1;
9c06b0b4 11056 }
77b06cd7 11057
c906108c 11058 if (cond_start)
c1fc2657 11059 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 11060 else
c1fc2657 11061 w->cond_string = 0;
c5aa993b 11062
441d7c93 11063 if (frame_id_p (watchpoint_frame))
f6bc2008 11064 {
441d7c93 11065 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11066 w->watchpoint_thread = inferior_ptid;
f6bc2008 11067 }
c906108c 11068 else
f6bc2008 11069 {
3a5c3e22
PA
11070 w->watchpoint_frame = null_frame_id;
11071 w->watchpoint_thread = null_ptid;
f6bc2008 11072 }
c906108c 11073
d983da9c 11074 if (scope_breakpoint != NULL)
c906108c 11075 {
d983da9c
DJ
11076 /* The scope breakpoint is related to the watchpoint. We will
11077 need to act on them together. */
c1fc2657 11078 w->related_breakpoint = scope_breakpoint;
b270e6f9 11079 scope_breakpoint->related_breakpoint = w.get ();
c906108c 11080 }
d983da9c 11081
06a64a0b
TT
11082 if (!just_location)
11083 value_free_to_mark (mark);
2d134ed3 11084
b270e6f9
TT
11085 /* Finally update the new watchpoint. This creates the locations
11086 that should be inserted. */
11087 update_watchpoint (w.get (), 1);
a9634178 11088
b270e6f9 11089 install_breakpoint (internal, std::move (w), 1);
bbc13ae3 11090 do_cleanups (back_to);
c906108c
SS
11091}
11092
e09342b5 11093/* Return count of debug registers needed to watch the given expression.
e09342b5 11094 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11095
c906108c 11096static int
a9634178 11097can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11098{
11099 int found_memory_cnt = 0;
2e70b7b9 11100 struct value *head = v;
c906108c
SS
11101
11102 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11103 if (!can_use_hw_watchpoints)
c906108c 11104 return 0;
c5aa993b 11105
5c44784c
JM
11106 /* Make sure that the value of the expression depends only upon
11107 memory contents, and values computed from them within GDB. If we
11108 find any register references or function calls, we can't use a
11109 hardware watchpoint.
11110
11111 The idea here is that evaluating an expression generates a series
11112 of values, one holding the value of every subexpression. (The
11113 expression a*b+c has five subexpressions: a, b, a*b, c, and
11114 a*b+c.) GDB's values hold almost enough information to establish
11115 the criteria given above --- they identify memory lvalues,
11116 register lvalues, computed values, etcetera. So we can evaluate
11117 the expression, and then scan the chain of values that leaves
11118 behind to decide whether we can detect any possible change to the
11119 expression's final value using only hardware watchpoints.
11120
11121 However, I don't think that the values returned by inferior
11122 function calls are special in any way. So this function may not
11123 notice that an expression involving an inferior function call
11124 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11125 for (; v; v = value_next (v))
c906108c 11126 {
5c44784c 11127 if (VALUE_LVAL (v) == lval_memory)
c906108c 11128 {
8464be76
DJ
11129 if (v != head && value_lazy (v))
11130 /* A lazy memory lvalue in the chain is one that GDB never
11131 needed to fetch; we either just used its address (e.g.,
11132 `a' in `a.b') or we never needed it at all (e.g., `a'
11133 in `a,b'). This doesn't apply to HEAD; if that is
11134 lazy then it was not readable, but watch it anyway. */
5c44784c 11135 ;
53a5351d 11136 else
5c44784c
JM
11137 {
11138 /* Ahh, memory we actually used! Check if we can cover
11139 it with hardware watchpoints. */
df407dfe 11140 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11141
11142 /* We only watch structs and arrays if user asked for it
11143 explicitly, never if they just happen to appear in a
11144 middle of some value chain. */
11145 if (v == head
11146 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11147 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11148 {
42ae5230 11149 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11150 int len;
11151 int num_regs;
11152
a9634178 11153 len = (target_exact_watchpoints
e09342b5
TJB
11154 && is_scalar_type_recursive (vtype))?
11155 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11156
e09342b5
TJB
11157 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11158 if (!num_regs)
2e70b7b9
MS
11159 return 0;
11160 else
e09342b5 11161 found_memory_cnt += num_regs;
2e70b7b9 11162 }
5c44784c 11163 }
c5aa993b 11164 }
5086187c
AC
11165 else if (VALUE_LVAL (v) != not_lval
11166 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11167 return 0; /* These are values from the history (e.g., $1). */
5086187c 11168 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11169 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11170 }
11171
11172 /* The expression itself looks suitable for using a hardware
11173 watchpoint, but give the target machine a chance to reject it. */
11174 return found_memory_cnt;
11175}
11176
8b93c638 11177void
f2fc3015 11178watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11179{
84f4c1fe 11180 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11181}
11182
06a64a0b
TT
11183/* A helper function that looks for the "-location" argument and then
11184 calls watch_command_1. */
11185
11186static void
11187watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11188{
11189 int just_location = 0;
11190
11191 if (arg
11192 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11193 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11194 {
e9cafbcc 11195 arg = skip_spaces (arg);
06a64a0b
TT
11196 just_location = 1;
11197 }
11198
84f4c1fe 11199 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11200}
8926118c 11201
c5aa993b 11202static void
fba45db2 11203watch_command (char *arg, int from_tty)
c906108c 11204{
06a64a0b 11205 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11206}
11207
8b93c638 11208void
f2fc3015 11209rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11210{
84f4c1fe 11211 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11212}
8926118c 11213
c5aa993b 11214static void
fba45db2 11215rwatch_command (char *arg, int from_tty)
c906108c 11216{
06a64a0b 11217 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11218}
11219
8b93c638 11220void
f2fc3015 11221awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11222{
84f4c1fe 11223 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11224}
8926118c 11225
c5aa993b 11226static void
fba45db2 11227awatch_command (char *arg, int from_tty)
c906108c 11228{
06a64a0b 11229 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11230}
c906108c 11231\f
c5aa993b 11232
cfc31633
PA
11233/* Data for the FSM that manages the until(location)/advance commands
11234 in infcmd.c. Here because it uses the mechanisms of
11235 breakpoints. */
c906108c 11236
cfc31633 11237struct until_break_fsm
bfec99b2 11238{
cfc31633
PA
11239 /* The base class. */
11240 struct thread_fsm thread_fsm;
11241
11242 /* The thread that as current when the command was executed. */
11243 int thread;
11244
11245 /* The breakpoint set at the destination location. */
11246 struct breakpoint *location_breakpoint;
11247
11248 /* Breakpoint set at the return address in the caller frame. May be
11249 NULL. */
11250 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11251};
11252
8980e177
PA
11253static void until_break_fsm_clean_up (struct thread_fsm *self,
11254 struct thread_info *thread);
11255static int until_break_fsm_should_stop (struct thread_fsm *self,
11256 struct thread_info *thread);
cfc31633
PA
11257static enum async_reply_reason
11258 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11259
11260/* until_break_fsm's vtable. */
11261
11262static struct thread_fsm_ops until_break_fsm_ops =
11263{
11264 NULL, /* dtor */
11265 until_break_fsm_clean_up,
11266 until_break_fsm_should_stop,
11267 NULL, /* return_value */
11268 until_break_fsm_async_reply_reason,
11269};
11270
11271/* Allocate a new until_break_command_fsm. */
11272
11273static struct until_break_fsm *
8980e177 11274new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11275 struct breakpoint *location_breakpoint,
11276 struct breakpoint *caller_breakpoint)
11277{
11278 struct until_break_fsm *sm;
11279
11280 sm = XCNEW (struct until_break_fsm);
8980e177 11281 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11282
11283 sm->thread = thread;
11284 sm->location_breakpoint = location_breakpoint;
11285 sm->caller_breakpoint = caller_breakpoint;
11286
11287 return sm;
11288}
11289
11290/* Implementation of the 'should_stop' FSM method for the
11291 until(location)/advance commands. */
11292
11293static int
8980e177
PA
11294until_break_fsm_should_stop (struct thread_fsm *self,
11295 struct thread_info *tp)
cfc31633
PA
11296{
11297 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11298
11299 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11300 sm->location_breakpoint) != NULL
11301 || (sm->caller_breakpoint != NULL
11302 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11303 sm->caller_breakpoint) != NULL))
11304 thread_fsm_set_finished (self);
11305
11306 return 1;
11307}
11308
11309/* Implementation of the 'clean_up' FSM method for the
11310 until(location)/advance commands. */
11311
c2c6d25f 11312static void
8980e177
PA
11313until_break_fsm_clean_up (struct thread_fsm *self,
11314 struct thread_info *thread)
43ff13b4 11315{
cfc31633 11316 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11317
cfc31633
PA
11318 /* Clean up our temporary breakpoints. */
11319 if (sm->location_breakpoint != NULL)
11320 {
11321 delete_breakpoint (sm->location_breakpoint);
11322 sm->location_breakpoint = NULL;
11323 }
11324 if (sm->caller_breakpoint != NULL)
11325 {
11326 delete_breakpoint (sm->caller_breakpoint);
11327 sm->caller_breakpoint = NULL;
11328 }
11329 delete_longjmp_breakpoint (sm->thread);
11330}
11331
11332/* Implementation of the 'async_reply_reason' FSM method for the
11333 until(location)/advance commands. */
11334
11335static enum async_reply_reason
11336until_break_fsm_async_reply_reason (struct thread_fsm *self)
11337{
11338 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11339}
11340
c906108c 11341void
f2fc3015 11342until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11343{
8556afb4
PA
11344 struct frame_info *frame;
11345 struct gdbarch *frame_gdbarch;
11346 struct frame_id stack_frame_id;
11347 struct frame_id caller_frame_id;
cfc31633
PA
11348 struct breakpoint *location_breakpoint;
11349 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11350 struct cleanup *old_chain;
186c406b
TT
11351 int thread;
11352 struct thread_info *tp;
cfc31633 11353 struct until_break_fsm *sm;
c906108c 11354
70509625 11355 clear_proceed_status (0);
c906108c
SS
11356
11357 /* Set a breakpoint where the user wants it and at return from
4a64f543 11358 this function. */
c5aa993b 11359
ffc2605c 11360 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11361
6c5b2ebe
PA
11362 std::vector<symtab_and_line> sals
11363 = (last_displayed_sal_is_valid ()
11364 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11365 get_last_displayed_symtab (),
11366 get_last_displayed_line ())
11367 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11368 NULL, (struct symtab *) NULL, 0));
c5aa993b 11369
6c5b2ebe 11370 if (sals.size () != 1)
8a3fe4f8 11371 error (_("Couldn't get information on specified line."));
c5aa993b 11372
6c5b2ebe 11373 symtab_and_line &sal = sals[0];
c5aa993b 11374
c906108c 11375 if (*arg)
8a3fe4f8 11376 error (_("Junk at end of arguments."));
c5aa993b 11377
c906108c 11378 resolve_sal_pc (&sal);
c5aa993b 11379
186c406b 11380 tp = inferior_thread ();
5d5658a1 11381 thread = tp->global_num;
186c406b 11382
883bc8d1
PA
11383 old_chain = make_cleanup (null_cleanup, NULL);
11384
8556afb4
PA
11385 /* Note linespec handling above invalidates the frame chain.
11386 Installing a breakpoint also invalidates the frame chain (as it
11387 may need to switch threads), so do any frame handling before
11388 that. */
11389
11390 frame = get_selected_frame (NULL);
11391 frame_gdbarch = get_frame_arch (frame);
11392 stack_frame_id = get_stack_frame_id (frame);
11393 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11394
ae66c1fc
EZ
11395 /* Keep within the current frame, or in frames called by the current
11396 one. */
edb3359d 11397
883bc8d1 11398 if (frame_id_p (caller_frame_id))
c906108c 11399 {
883bc8d1 11400 struct symtab_and_line sal2;
cfc31633 11401 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11402
11403 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11404 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11405 caller_gdbarch = frame_unwind_caller_arch (frame);
11406 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11407 sal2,
11408 caller_frame_id,
11409 bp_until);
11410 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11411
883bc8d1 11412 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11413 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11414 }
c5aa993b 11415
c70a6932
JK
11416 /* set_momentary_breakpoint could invalidate FRAME. */
11417 frame = NULL;
11418
883bc8d1
PA
11419 if (anywhere)
11420 /* If the user told us to continue until a specified location,
11421 we don't specify a frame at which we need to stop. */
cfc31633
PA
11422 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11423 null_frame_id, bp_until);
883bc8d1
PA
11424 else
11425 /* Otherwise, specify the selected frame, because we want to stop
11426 only at the very same frame. */
cfc31633
PA
11427 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11428 stack_frame_id, bp_until);
11429 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11430
8980e177 11431 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11432 location_breakpoint, caller_breakpoint);
cfc31633 11433 tp->thread_fsm = &sm->thread_fsm;
f107f563 11434
cfc31633 11435 discard_cleanups (old_chain);
f107f563 11436
cfc31633 11437 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11438}
ae66c1fc 11439
c906108c
SS
11440/* This function attempts to parse an optional "if <cond>" clause
11441 from the arg string. If one is not found, it returns NULL.
c5aa993b 11442
c906108c
SS
11443 Else, it returns a pointer to the condition string. (It does not
11444 attempt to evaluate the string against a particular block.) And,
11445 it updates arg to point to the first character following the parsed
4a64f543 11446 if clause in the arg string. */
53a5351d 11447
63160a43
PA
11448const char *
11449ep_parse_optional_if_clause (const char **arg)
c906108c 11450{
63160a43 11451 const char *cond_string;
c5aa993b
JM
11452
11453 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11454 return NULL;
c5aa993b 11455
4a64f543 11456 /* Skip the "if" keyword. */
c906108c 11457 (*arg) += 2;
c5aa993b 11458
c906108c 11459 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11460 condition string. */
f1735a53 11461 *arg = skip_spaces (*arg);
c906108c 11462 cond_string = *arg;
c5aa993b 11463
4a64f543
MS
11464 /* Assume that the condition occupies the remainder of the arg
11465 string. */
c906108c 11466 (*arg) += strlen (cond_string);
c5aa993b 11467
c906108c
SS
11468 return cond_string;
11469}
c5aa993b 11470
c906108c
SS
11471/* Commands to deal with catching events, such as signals, exceptions,
11472 process start/exit, etc. */
c5aa993b
JM
11473
11474typedef enum
11475{
44feb3ce
TT
11476 catch_fork_temporary, catch_vfork_temporary,
11477 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11478}
11479catch_fork_kind;
11480
c906108c 11481static void
63160a43 11482catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11483 struct cmd_list_element *command)
c906108c 11484{
63160a43 11485 const char *arg = arg_entry;
a6d9a66e 11486 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11487 const char *cond_string = NULL;
44feb3ce
TT
11488 catch_fork_kind fork_kind;
11489 int tempflag;
11490
11491 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11492 tempflag = (fork_kind == catch_fork_temporary
11493 || fork_kind == catch_vfork_temporary);
c5aa993b 11494
44feb3ce
TT
11495 if (!arg)
11496 arg = "";
f1735a53 11497 arg = skip_spaces (arg);
c5aa993b 11498
c906108c 11499 /* The allowed syntax is:
c5aa993b
JM
11500 catch [v]fork
11501 catch [v]fork if <cond>
11502
4a64f543 11503 First, check if there's an if clause. */
c906108c 11504 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11505
c906108c 11506 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11507 error (_("Junk at end of arguments."));
c5aa993b 11508
c906108c 11509 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11510 and enable reporting of such events. */
c5aa993b
JM
11511 switch (fork_kind)
11512 {
44feb3ce
TT
11513 case catch_fork_temporary:
11514 case catch_fork_permanent:
a6d9a66e 11515 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11516 &catch_fork_breakpoint_ops);
c906108c 11517 break;
44feb3ce
TT
11518 case catch_vfork_temporary:
11519 case catch_vfork_permanent:
a6d9a66e 11520 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11521 &catch_vfork_breakpoint_ops);
c906108c 11522 break;
c5aa993b 11523 default:
8a3fe4f8 11524 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11525 break;
c5aa993b 11526 }
c906108c
SS
11527}
11528
11529static void
63160a43 11530catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11531 struct cmd_list_element *command)
c906108c 11532{
63160a43 11533 const char *arg = arg_entry;
a6d9a66e 11534 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11535 int tempflag;
63160a43 11536 const char *cond_string = NULL;
c906108c 11537
44feb3ce
TT
11538 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11539
11540 if (!arg)
11541 arg = "";
f1735a53 11542 arg = skip_spaces (arg);
c906108c
SS
11543
11544 /* The allowed syntax is:
c5aa993b
JM
11545 catch exec
11546 catch exec if <cond>
c906108c 11547
4a64f543 11548 First, check if there's an if clause. */
c906108c
SS
11549 cond_string = ep_parse_optional_if_clause (&arg);
11550
11551 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11552 error (_("Junk at end of arguments."));
c906108c 11553
b270e6f9
TT
11554 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11555 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11556 &catch_exec_breakpoint_ops);
11557 c->exec_pathname = NULL;
11558
b270e6f9 11559 install_breakpoint (0, std::move (c), 1);
c906108c 11560}
c5aa993b 11561
9ac4176b 11562void
28010a5d
PA
11563init_ada_exception_breakpoint (struct breakpoint *b,
11564 struct gdbarch *gdbarch,
11565 struct symtab_and_line sal,
f2fc3015 11566 const char *addr_string,
c0a91b2b 11567 const struct breakpoint_ops *ops,
28010a5d 11568 int tempflag,
349774ef 11569 int enabled,
28010a5d 11570 int from_tty)
f7f9143b 11571{
f7f9143b
JB
11572 if (from_tty)
11573 {
5af949e3
UW
11574 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11575 if (!loc_gdbarch)
11576 loc_gdbarch = gdbarch;
11577
6c95b8df
PA
11578 describe_other_breakpoints (loc_gdbarch,
11579 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11580 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11581 version for exception catchpoints, because two catchpoints
11582 used for different exception names will use the same address.
11583 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11584 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11585 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11586 the user what type of catchpoint it is. The above is good
11587 enough for now, though. */
11588 }
11589
28010a5d 11590 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11591
349774ef 11592 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11593 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11594 b->location = string_to_event_location (&addr_string,
11595 language_def (language_ada));
f7f9143b 11596 b->language = language_ada;
f7f9143b
JB
11597}
11598
c906108c 11599static void
fba45db2 11600catch_command (char *arg, int from_tty)
c906108c 11601{
44feb3ce 11602 error (_("Catch requires an event name."));
c906108c
SS
11603}
11604\f
11605
11606static void
fba45db2 11607tcatch_command (char *arg, int from_tty)
c906108c 11608{
44feb3ce 11609 error (_("Catch requires an event name."));
c906108c
SS
11610}
11611
8a2c437b
TT
11612/* A qsort comparison function that sorts breakpoints in order. */
11613
11614static int
11615compare_breakpoints (const void *a, const void *b)
11616{
9a3c8263 11617 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11618 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11619 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11620 uintptr_t ub = (uintptr_t) *bb;
11621
11622 if ((*ba)->number < (*bb)->number)
11623 return -1;
11624 else if ((*ba)->number > (*bb)->number)
11625 return 1;
11626
11627 /* Now sort by address, in case we see, e..g, two breakpoints with
11628 the number 0. */
11629 if (ua < ub)
11630 return -1;
94b0e70d 11631 return ua > ub ? 1 : 0;
8a2c437b
TT
11632}
11633
80f8a6eb 11634/* Delete breakpoints by address or line. */
c906108c
SS
11635
11636static void
fba45db2 11637clear_command (char *arg, int from_tty)
c906108c 11638{
8a2c437b 11639 struct breakpoint *b, *prev;
d6e956e5
VP
11640 VEC(breakpoint_p) *found = 0;
11641 int ix;
c906108c 11642 int default_match;
c906108c 11643 int i;
8a2c437b 11644 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c 11645
6c5b2ebe
PA
11646 std::vector<symtab_and_line> decoded_sals;
11647 symtab_and_line last_sal;
11648 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11649 if (arg)
11650 {
6c5b2ebe
PA
11651 decoded_sals
11652 = decode_line_with_current_source (arg,
11653 (DECODE_LINE_FUNFIRSTLINE
11654 | DECODE_LINE_LIST_MODE));
c906108c 11655 default_match = 0;
6c5b2ebe 11656 sals = decoded_sals;
c906108c
SS
11657 }
11658 else
11659 {
1bfeeb0f
JL
11660 /* Set sal's line, symtab, pc, and pspace to the values
11661 corresponding to the last call to print_frame_info. If the
11662 codepoint is not valid, this will set all the fields to 0. */
51abb421 11663 last_sal = get_last_displayed_sal ();
6c5b2ebe 11664 if (last_sal.symtab == 0)
8a3fe4f8 11665 error (_("No source file specified."));
c906108c 11666
c906108c 11667 default_match = 1;
6c5b2ebe 11668 sals = last_sal;
c906108c
SS
11669 }
11670
4a64f543
MS
11671 /* We don't call resolve_sal_pc here. That's not as bad as it
11672 seems, because all existing breakpoints typically have both
11673 file/line and pc set. So, if clear is given file/line, we can
11674 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11675
11676 We only support clearing given the address explicitly
11677 present in breakpoint table. Say, we've set breakpoint
4a64f543 11678 at file:line. There were several PC values for that file:line,
ed0616c6 11679 due to optimization, all in one block.
4a64f543
MS
11680
11681 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11682 PC corresponding to the same file:line, the breakpoint won't
11683 be cleared. We probably can still clear the breakpoint, but
11684 since the other PC value is never presented to user, user
11685 can only find it by guessing, and it does not seem important
11686 to support that. */
11687
4a64f543
MS
11688 /* For each line spec given, delete bps which correspond to it. Do
11689 it in two passes, solely to preserve the current behavior that
11690 from_tty is forced true if we delete more than one
11691 breakpoint. */
c906108c 11692
80f8a6eb 11693 found = NULL;
8a2c437b 11694 make_cleanup (VEC_cleanup (breakpoint_p), &found);
6c5b2ebe 11695 for (const auto &sal : sals)
c906108c 11696 {
05cba821
JK
11697 const char *sal_fullname;
11698
c906108c 11699 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11700 If line given (pc == 0), clear all bpts on specified line.
11701 If defaulting, clear all bpts on default line
c906108c 11702 or at default pc.
c5aa993b
JM
11703
11704 defaulting sal.pc != 0 tests to do
11705
11706 0 1 pc
11707 1 1 pc _and_ line
11708 0 0 line
11709 1 0 <can't happen> */
c906108c 11710
05cba821
JK
11711 sal_fullname = (sal.symtab == NULL
11712 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11713
4a64f543 11714 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11715 ALL_BREAKPOINTS (b)
c5aa993b 11716 {
0d381245 11717 int match = 0;
4a64f543 11718 /* Are we going to delete b? */
cc60f2e3 11719 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11720 {
11721 struct bp_location *loc = b->loc;
11722 for (; loc; loc = loc->next)
11723 {
f8eba3c6
TT
11724 /* If the user specified file:line, don't allow a PC
11725 match. This matches historical gdb behavior. */
11726 int pc_match = (!sal.explicit_line
11727 && sal.pc
11728 && (loc->pspace == sal.pspace)
11729 && (loc->address == sal.pc)
11730 && (!section_is_overlay (loc->section)
11731 || loc->section == sal.section));
4aac40c8
TT
11732 int line_match = 0;
11733
11734 if ((default_match || sal.explicit_line)
2f202fde 11735 && loc->symtab != NULL
05cba821 11736 && sal_fullname != NULL
4aac40c8 11737 && sal.pspace == loc->pspace
05cba821
JK
11738 && loc->line_number == sal.line
11739 && filename_cmp (symtab_to_fullname (loc->symtab),
11740 sal_fullname) == 0)
11741 line_match = 1;
4aac40c8 11742
0d381245
VP
11743 if (pc_match || line_match)
11744 {
11745 match = 1;
11746 break;
11747 }
11748 }
11749 }
11750
11751 if (match)
d6e956e5 11752 VEC_safe_push(breakpoint_p, found, b);
c906108c 11753 }
80f8a6eb 11754 }
8a2c437b 11755
80f8a6eb 11756 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11757 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11758 {
11759 if (arg)
8a3fe4f8 11760 error (_("No breakpoint at %s."), arg);
80f8a6eb 11761 else
8a3fe4f8 11762 error (_("No breakpoint at this line."));
80f8a6eb 11763 }
c906108c 11764
8a2c437b
TT
11765 /* Remove duplicates from the vec. */
11766 qsort (VEC_address (breakpoint_p, found),
11767 VEC_length (breakpoint_p, found),
11768 sizeof (breakpoint_p),
11769 compare_breakpoints);
11770 prev = VEC_index (breakpoint_p, found, 0);
11771 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11772 {
11773 if (b == prev)
11774 {
11775 VEC_ordered_remove (breakpoint_p, found, ix);
11776 --ix;
11777 }
11778 }
11779
d6e956e5 11780 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11781 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11782 if (from_tty)
a3f17187 11783 {
d6e956e5 11784 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11785 printf_unfiltered (_("Deleted breakpoint "));
11786 else
11787 printf_unfiltered (_("Deleted breakpoints "));
11788 }
d6e956e5
VP
11789
11790 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11791 {
c5aa993b 11792 if (from_tty)
d6e956e5
VP
11793 printf_unfiltered ("%d ", b->number);
11794 delete_breakpoint (b);
c906108c 11795 }
80f8a6eb
MS
11796 if (from_tty)
11797 putchar_unfiltered ('\n');
8a2c437b
TT
11798
11799 do_cleanups (cleanups);
c906108c
SS
11800}
11801\f
11802/* Delete breakpoint in BS if they are `delete' breakpoints and
11803 all breakpoints that are marked for deletion, whether hit or not.
11804 This is called after any breakpoint is hit, or after errors. */
11805
11806void
fba45db2 11807breakpoint_auto_delete (bpstat bs)
c906108c 11808{
35df4500 11809 struct breakpoint *b, *b_tmp;
c906108c
SS
11810
11811 for (; bs; bs = bs->next)
f431efe5
PA
11812 if (bs->breakpoint_at
11813 && bs->breakpoint_at->disposition == disp_del
c906108c 11814 && bs->stop)
f431efe5 11815 delete_breakpoint (bs->breakpoint_at);
c906108c 11816
35df4500 11817 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11818 {
b5de0fa7 11819 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11820 delete_breakpoint (b);
11821 }
c906108c
SS
11822}
11823
4a64f543
MS
11824/* A comparison function for bp_location AP and BP being interfaced to
11825 qsort. Sort elements primarily by their ADDRESS (no matter what
11826 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11827 secondarily by ordering first permanent elements and
4a64f543 11828 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11829 qsort being an unstable algorithm. */
876fa593
JK
11830
11831static int
f5336ca5 11832bp_locations_compare (const void *ap, const void *bp)
876fa593 11833{
9a3c8263
SM
11834 const struct bp_location *a = *(const struct bp_location **) ap;
11835 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11836
11837 if (a->address != b->address)
11838 return (a->address > b->address) - (a->address < b->address);
11839
dea2aa5f
LM
11840 /* Sort locations at the same address by their pspace number, keeping
11841 locations of the same inferior (in a multi-inferior environment)
11842 grouped. */
11843
11844 if (a->pspace->num != b->pspace->num)
11845 return ((a->pspace->num > b->pspace->num)
11846 - (a->pspace->num < b->pspace->num));
11847
876fa593 11848 /* Sort permanent breakpoints first. */
1a853c52
PA
11849 if (a->permanent != b->permanent)
11850 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11851
c56a97f9
JK
11852 /* Make the internal GDB representation stable across GDB runs
11853 where A and B memory inside GDB can differ. Breakpoint locations of
11854 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11855
11856 if (a->owner->number != b->owner->number)
c56a97f9
JK
11857 return ((a->owner->number > b->owner->number)
11858 - (a->owner->number < b->owner->number));
876fa593
JK
11859
11860 return (a > b) - (a < b);
11861}
11862
f5336ca5
PA
11863/* Set bp_locations_placed_address_before_address_max and
11864 bp_locations_shadow_len_after_address_max according to the current
11865 content of the bp_locations array. */
f7545552
TT
11866
11867static void
f5336ca5 11868bp_locations_target_extensions_update (void)
f7545552 11869{
876fa593
JK
11870 struct bp_location *bl, **blp_tmp;
11871
f5336ca5
PA
11872 bp_locations_placed_address_before_address_max = 0;
11873 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11874
11875 ALL_BP_LOCATIONS (bl, blp_tmp)
11876 {
11877 CORE_ADDR start, end, addr;
11878
11879 if (!bp_location_has_shadow (bl))
11880 continue;
11881
11882 start = bl->target_info.placed_address;
11883 end = start + bl->target_info.shadow_len;
11884
11885 gdb_assert (bl->address >= start);
11886 addr = bl->address - start;
f5336ca5
PA
11887 if (addr > bp_locations_placed_address_before_address_max)
11888 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11889
11890 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11891
11892 gdb_assert (bl->address < end);
11893 addr = end - bl->address;
f5336ca5
PA
11894 if (addr > bp_locations_shadow_len_after_address_max)
11895 bp_locations_shadow_len_after_address_max = addr;
876fa593 11896 }
f7545552
TT
11897}
11898
1e4d1764
YQ
11899/* Download tracepoint locations if they haven't been. */
11900
11901static void
11902download_tracepoint_locations (void)
11903{
7ed2c994 11904 struct breakpoint *b;
dd2e65cc 11905 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11906
5ed8105e 11907 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11908
7ed2c994 11909 ALL_TRACEPOINTS (b)
1e4d1764 11910 {
7ed2c994 11911 struct bp_location *bl;
1e4d1764 11912 struct tracepoint *t;
f2a8bc8a 11913 int bp_location_downloaded = 0;
1e4d1764 11914
7ed2c994 11915 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11916 ? !may_insert_fast_tracepoints
11917 : !may_insert_tracepoints))
11918 continue;
11919
dd2e65cc
YQ
11920 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11921 {
11922 if (target_can_download_tracepoint ())
11923 can_download_tracepoint = TRIBOOL_TRUE;
11924 else
11925 can_download_tracepoint = TRIBOOL_FALSE;
11926 }
11927
11928 if (can_download_tracepoint == TRIBOOL_FALSE)
11929 break;
11930
7ed2c994
YQ
11931 for (bl = b->loc; bl; bl = bl->next)
11932 {
11933 /* In tracepoint, locations are _never_ duplicated, so
11934 should_be_inserted is equivalent to
11935 unduplicated_should_be_inserted. */
11936 if (!should_be_inserted (bl) || bl->inserted)
11937 continue;
1e4d1764 11938
7ed2c994 11939 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11940
7ed2c994 11941 target_download_tracepoint (bl);
1e4d1764 11942
7ed2c994 11943 bl->inserted = 1;
f2a8bc8a 11944 bp_location_downloaded = 1;
7ed2c994
YQ
11945 }
11946 t = (struct tracepoint *) b;
11947 t->number_on_target = b->number;
f2a8bc8a
YQ
11948 if (bp_location_downloaded)
11949 observer_notify_breakpoint_modified (b);
1e4d1764 11950 }
1e4d1764
YQ
11951}
11952
934709f0
PW
11953/* Swap the insertion/duplication state between two locations. */
11954
11955static void
11956swap_insertion (struct bp_location *left, struct bp_location *right)
11957{
11958 const int left_inserted = left->inserted;
11959 const int left_duplicate = left->duplicate;
b775012e 11960 const int left_needs_update = left->needs_update;
934709f0
PW
11961 const struct bp_target_info left_target_info = left->target_info;
11962
1e4d1764
YQ
11963 /* Locations of tracepoints can never be duplicated. */
11964 if (is_tracepoint (left->owner))
11965 gdb_assert (!left->duplicate);
11966 if (is_tracepoint (right->owner))
11967 gdb_assert (!right->duplicate);
11968
934709f0
PW
11969 left->inserted = right->inserted;
11970 left->duplicate = right->duplicate;
b775012e 11971 left->needs_update = right->needs_update;
934709f0
PW
11972 left->target_info = right->target_info;
11973 right->inserted = left_inserted;
11974 right->duplicate = left_duplicate;
b775012e 11975 right->needs_update = left_needs_update;
934709f0
PW
11976 right->target_info = left_target_info;
11977}
11978
b775012e
LM
11979/* Force the re-insertion of the locations at ADDRESS. This is called
11980 once a new/deleted/modified duplicate location is found and we are evaluating
11981 conditions on the target's side. Such conditions need to be updated on
11982 the target. */
11983
11984static void
11985force_breakpoint_reinsertion (struct bp_location *bl)
11986{
11987 struct bp_location **locp = NULL, **loc2p;
11988 struct bp_location *loc;
11989 CORE_ADDR address = 0;
11990 int pspace_num;
11991
11992 address = bl->address;
11993 pspace_num = bl->pspace->num;
11994
11995 /* This is only meaningful if the target is
11996 evaluating conditions and if the user has
11997 opted for condition evaluation on the target's
11998 side. */
11999 if (gdb_evaluates_breakpoint_condition_p ()
12000 || !target_supports_evaluation_of_breakpoint_conditions ())
12001 return;
12002
12003 /* Flag all breakpoint locations with this address and
12004 the same program space as the location
12005 as "its condition has changed". We need to
12006 update the conditions on the target's side. */
12007 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12008 {
12009 loc = *loc2p;
12010
12011 if (!is_breakpoint (loc->owner)
12012 || pspace_num != loc->pspace->num)
12013 continue;
12014
12015 /* Flag the location appropriately. We use a different state to
12016 let everyone know that we already updated the set of locations
12017 with addr bl->address and program space bl->pspace. This is so
12018 we don't have to keep calling these functions just to mark locations
12019 that have already been marked. */
12020 loc->condition_changed = condition_updated;
12021
12022 /* Free the agent expression bytecode as well. We will compute
12023 it later on. */
833177a4 12024 loc->cond_bytecode.reset ();
b775012e
LM
12025 }
12026}
44702360
PA
12027/* Called whether new breakpoints are created, or existing breakpoints
12028 deleted, to update the global location list and recompute which
12029 locations are duplicate of which.
b775012e 12030
04086b45
PA
12031 The INSERT_MODE flag determines whether locations may not, may, or
12032 shall be inserted now. See 'enum ugll_insert_mode' for more
12033 info. */
b60e7edf 12034
0d381245 12035static void
44702360 12036update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12037{
74960c60 12038 struct breakpoint *b;
876fa593 12039 struct bp_location **locp, *loc;
f7545552 12040 struct cleanup *cleanups;
b775012e
LM
12041 /* Last breakpoint location address that was marked for update. */
12042 CORE_ADDR last_addr = 0;
12043 /* Last breakpoint location program space that was marked for update. */
12044 int last_pspace_num = -1;
f7545552 12045
2d134ed3
PA
12046 /* Used in the duplicates detection below. When iterating over all
12047 bp_locations, points to the first bp_location of a given address.
12048 Breakpoints and watchpoints of different types are never
12049 duplicates of each other. Keep one pointer for each type of
12050 breakpoint/watchpoint, so we only need to loop over all locations
12051 once. */
12052 struct bp_location *bp_loc_first; /* breakpoint */
12053 struct bp_location *wp_loc_first; /* hardware watchpoint */
12054 struct bp_location *awp_loc_first; /* access watchpoint */
12055 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12056
f5336ca5
PA
12057 /* Saved former bp_locations array which we compare against the newly
12058 built bp_locations from the current state of ALL_BREAKPOINTS. */
12059 struct bp_location **old_locations, **old_locp;
12060 unsigned old_locations_count;
876fa593 12061
f5336ca5
PA
12062 old_locations = bp_locations;
12063 old_locations_count = bp_locations_count;
12064 bp_locations = NULL;
12065 bp_locations_count = 0;
12066 cleanups = make_cleanup (xfree, old_locations);
0d381245 12067
74960c60 12068 ALL_BREAKPOINTS (b)
876fa593 12069 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12070 bp_locations_count++;
876fa593 12071
f5336ca5
PA
12072 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12073 locp = bp_locations;
876fa593
JK
12074 ALL_BREAKPOINTS (b)
12075 for (loc = b->loc; loc; loc = loc->next)
12076 *locp++ = loc;
f5336ca5
PA
12077 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12078 bp_locations_compare);
876fa593 12079
f5336ca5 12080 bp_locations_target_extensions_update ();
74960c60 12081
4a64f543
MS
12082 /* Identify bp_location instances that are no longer present in the
12083 new list, and therefore should be freed. Note that it's not
12084 necessary that those locations should be removed from inferior --
12085 if there's another location at the same address (previously
12086 marked as duplicate), we don't need to remove/insert the
12087 location.
876fa593 12088
4a64f543
MS
12089 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12090 and former bp_location array state respectively. */
876fa593 12091
f5336ca5
PA
12092 locp = bp_locations;
12093 for (old_locp = old_locations;
12094 old_locp < old_locations + old_locations_count;
876fa593 12095 old_locp++)
74960c60 12096 {
876fa593 12097 struct bp_location *old_loc = *old_locp;
c7d46a38 12098 struct bp_location **loc2p;
876fa593 12099
e5dd4106 12100 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12101 not, we have to free it. */
c7d46a38 12102 int found_object = 0;
20874c92
VP
12103 /* Tells if the location should remain inserted in the target. */
12104 int keep_in_target = 0;
12105 int removed = 0;
876fa593 12106
4a64f543
MS
12107 /* Skip LOCP entries which will definitely never be needed.
12108 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12109 while (locp < bp_locations + bp_locations_count
c7d46a38 12110 && (*locp)->address < old_loc->address)
876fa593 12111 locp++;
c7d46a38
PA
12112
12113 for (loc2p = locp;
f5336ca5 12114 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12115 && (*loc2p)->address == old_loc->address);
12116 loc2p++)
12117 {
b775012e
LM
12118 /* Check if this is a new/duplicated location or a duplicated
12119 location that had its condition modified. If so, we want to send
12120 its condition to the target if evaluation of conditions is taking
12121 place there. */
12122 if ((*loc2p)->condition_changed == condition_modified
12123 && (last_addr != old_loc->address
12124 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12125 {
b775012e
LM
12126 force_breakpoint_reinsertion (*loc2p);
12127 last_pspace_num = old_loc->pspace->num;
c7d46a38 12128 }
b775012e
LM
12129
12130 if (*loc2p == old_loc)
12131 found_object = 1;
c7d46a38 12132 }
74960c60 12133
b775012e
LM
12134 /* We have already handled this address, update it so that we don't
12135 have to go through updates again. */
12136 last_addr = old_loc->address;
12137
12138 /* Target-side condition evaluation: Handle deleted locations. */
12139 if (!found_object)
12140 force_breakpoint_reinsertion (old_loc);
12141
4a64f543
MS
12142 /* If this location is no longer present, and inserted, look if
12143 there's maybe a new location at the same address. If so,
12144 mark that one inserted, and don't remove this one. This is
12145 needed so that we don't have a time window where a breakpoint
12146 at certain location is not inserted. */
74960c60 12147
876fa593 12148 if (old_loc->inserted)
0d381245 12149 {
4a64f543
MS
12150 /* If the location is inserted now, we might have to remove
12151 it. */
74960c60 12152
876fa593 12153 if (found_object && should_be_inserted (old_loc))
74960c60 12154 {
4a64f543
MS
12155 /* The location is still present in the location list,
12156 and still should be inserted. Don't do anything. */
20874c92 12157 keep_in_target = 1;
74960c60
VP
12158 }
12159 else
12160 {
b775012e
LM
12161 /* This location still exists, but it won't be kept in the
12162 target since it may have been disabled. We proceed to
12163 remove its target-side condition. */
12164
4a64f543
MS
12165 /* The location is either no longer present, or got
12166 disabled. See if there's another location at the
12167 same address, in which case we don't need to remove
12168 this one from the target. */
876fa593 12169
2bdf28a0 12170 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12171 if (breakpoint_address_is_meaningful (old_loc->owner))
12172 {
876fa593 12173 for (loc2p = locp;
f5336ca5 12174 (loc2p < bp_locations + bp_locations_count
c7d46a38 12175 && (*loc2p)->address == old_loc->address);
876fa593
JK
12176 loc2p++)
12177 {
12178 struct bp_location *loc2 = *loc2p;
12179
2d134ed3 12180 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12181 {
85d721b8
PA
12182 /* Read watchpoint locations are switched to
12183 access watchpoints, if the former are not
12184 supported, but the latter are. */
12185 if (is_hardware_watchpoint (old_loc->owner))
12186 {
12187 gdb_assert (is_hardware_watchpoint (loc2->owner));
12188 loc2->watchpoint_type = old_loc->watchpoint_type;
12189 }
12190
934709f0
PW
12191 /* loc2 is a duplicated location. We need to check
12192 if it should be inserted in case it will be
12193 unduplicated. */
12194 if (loc2 != old_loc
12195 && unduplicated_should_be_inserted (loc2))
c7d46a38 12196 {
934709f0 12197 swap_insertion (old_loc, loc2);
c7d46a38
PA
12198 keep_in_target = 1;
12199 break;
12200 }
876fa593
JK
12201 }
12202 }
12203 }
74960c60
VP
12204 }
12205
20874c92
VP
12206 if (!keep_in_target)
12207 {
834c0d03 12208 if (remove_breakpoint (old_loc))
20874c92 12209 {
4a64f543
MS
12210 /* This is just about all we can do. We could keep
12211 this location on the global list, and try to
12212 remove it next time, but there's no particular
12213 reason why we will succeed next time.
20874c92 12214
4a64f543
MS
12215 Note that at this point, old_loc->owner is still
12216 valid, as delete_breakpoint frees the breakpoint
12217 only after calling us. */
3e43a32a
MS
12218 printf_filtered (_("warning: Error removing "
12219 "breakpoint %d\n"),
876fa593 12220 old_loc->owner->number);
20874c92
VP
12221 }
12222 removed = 1;
12223 }
0d381245 12224 }
74960c60
VP
12225
12226 if (!found_object)
1c5cfe86 12227 {
fbea99ea 12228 if (removed && target_is_non_stop_p ()
1cf4d951 12229 && need_moribund_for_location_type (old_loc))
20874c92 12230 {
db82e815
PA
12231 /* This location was removed from the target. In
12232 non-stop mode, a race condition is possible where
12233 we've removed a breakpoint, but stop events for that
12234 breakpoint are already queued and will arrive later.
12235 We apply an heuristic to be able to distinguish such
12236 SIGTRAPs from other random SIGTRAPs: we keep this
12237 breakpoint location for a bit, and will retire it
12238 after we see some number of events. The theory here
12239 is that reporting of events should, "on the average",
12240 be fair, so after a while we'll see events from all
12241 threads that have anything of interest, and no longer
12242 need to keep this breakpoint location around. We
12243 don't hold locations forever so to reduce chances of
12244 mistaking a non-breakpoint SIGTRAP for a breakpoint
12245 SIGTRAP.
12246
12247 The heuristic failing can be disastrous on
12248 decr_pc_after_break targets.
12249
12250 On decr_pc_after_break targets, like e.g., x86-linux,
12251 if we fail to recognize a late breakpoint SIGTRAP,
12252 because events_till_retirement has reached 0 too
12253 soon, we'll fail to do the PC adjustment, and report
12254 a random SIGTRAP to the user. When the user resumes
12255 the inferior, it will most likely immediately crash
2dec564e 12256 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12257 corrupted, because of being resumed e.g., in the
12258 middle of a multi-byte instruction, or skipped a
12259 one-byte instruction. This was actually seen happen
12260 on native x86-linux, and should be less rare on
12261 targets that do not support new thread events, like
12262 remote, due to the heuristic depending on
12263 thread_count.
12264
12265 Mistaking a random SIGTRAP for a breakpoint trap
12266 causes similar symptoms (PC adjustment applied when
12267 it shouldn't), but then again, playing with SIGTRAPs
12268 behind the debugger's back is asking for trouble.
12269
12270 Since hardware watchpoint traps are always
12271 distinguishable from other traps, so we don't need to
12272 apply keep hardware watchpoint moribund locations
12273 around. We simply always ignore hardware watchpoint
12274 traps we can no longer explain. */
12275
876fa593
JK
12276 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12277 old_loc->owner = NULL;
20874c92 12278
876fa593 12279 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12280 }
12281 else
f431efe5
PA
12282 {
12283 old_loc->owner = NULL;
12284 decref_bp_location (&old_loc);
12285 }
20874c92 12286 }
74960c60 12287 }
1c5cfe86 12288
348d480f
PA
12289 /* Rescan breakpoints at the same address and section, marking the
12290 first one as "first" and any others as "duplicates". This is so
12291 that the bpt instruction is only inserted once. If we have a
12292 permanent breakpoint at the same place as BPT, make that one the
12293 official one, and the rest as duplicates. Permanent breakpoints
12294 are sorted first for the same address.
12295
12296 Do the same for hardware watchpoints, but also considering the
12297 watchpoint's type (regular/access/read) and length. */
12298
12299 bp_loc_first = NULL;
12300 wp_loc_first = NULL;
12301 awp_loc_first = NULL;
12302 rwp_loc_first = NULL;
12303 ALL_BP_LOCATIONS (loc, locp)
12304 {
12305 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12306 non-NULL. */
348d480f 12307 struct bp_location **loc_first_p;
d3fbdd86 12308 b = loc->owner;
348d480f 12309
6f380991 12310 if (!unduplicated_should_be_inserted (loc)
348d480f 12311 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12312 /* Don't detect duplicate for tracepoint locations because they are
12313 never duplicated. See the comments in field `duplicate' of
12314 `struct bp_location'. */
348d480f 12315 || is_tracepoint (b))
b775012e
LM
12316 {
12317 /* Clear the condition modification flag. */
12318 loc->condition_changed = condition_unchanged;
12319 continue;
12320 }
348d480f 12321
348d480f
PA
12322 if (b->type == bp_hardware_watchpoint)
12323 loc_first_p = &wp_loc_first;
12324 else if (b->type == bp_read_watchpoint)
12325 loc_first_p = &rwp_loc_first;
12326 else if (b->type == bp_access_watchpoint)
12327 loc_first_p = &awp_loc_first;
12328 else
12329 loc_first_p = &bp_loc_first;
12330
12331 if (*loc_first_p == NULL
12332 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12333 || !breakpoint_locations_match (loc, *loc_first_p))
12334 {
12335 *loc_first_p = loc;
12336 loc->duplicate = 0;
b775012e
LM
12337
12338 if (is_breakpoint (loc->owner) && loc->condition_changed)
12339 {
12340 loc->needs_update = 1;
12341 /* Clear the condition modification flag. */
12342 loc->condition_changed = condition_unchanged;
12343 }
348d480f
PA
12344 continue;
12345 }
12346
934709f0
PW
12347
12348 /* This and the above ensure the invariant that the first location
12349 is not duplicated, and is the inserted one.
12350 All following are marked as duplicated, and are not inserted. */
12351 if (loc->inserted)
12352 swap_insertion (loc, *loc_first_p);
348d480f
PA
12353 loc->duplicate = 1;
12354
b775012e
LM
12355 /* Clear the condition modification flag. */
12356 loc->condition_changed = condition_unchanged;
348d480f
PA
12357 }
12358
a25a5a45 12359 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12360 {
04086b45 12361 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12362 insert_breakpoint_locations ();
12363 else
12364 {
44702360
PA
12365 /* Even though the caller told us to not insert new
12366 locations, we may still need to update conditions on the
12367 target's side of breakpoints that were already inserted
12368 if the target is evaluating breakpoint conditions. We
b775012e
LM
12369 only update conditions for locations that are marked
12370 "needs_update". */
12371 update_inserted_breakpoint_locations ();
12372 }
12373 }
348d480f 12374
04086b45 12375 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12376 download_tracepoint_locations ();
12377
348d480f
PA
12378 do_cleanups (cleanups);
12379}
12380
12381void
12382breakpoint_retire_moribund (void)
12383{
12384 struct bp_location *loc;
12385 int ix;
12386
12387 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12388 if (--(loc->events_till_retirement) == 0)
12389 {
12390 decref_bp_location (&loc);
12391 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12392 --ix;
12393 }
12394}
12395
12396static void
44702360 12397update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12398{
348d480f 12399
492d29ea
PA
12400 TRY
12401 {
12402 update_global_location_list (insert_mode);
12403 }
12404 CATCH (e, RETURN_MASK_ERROR)
12405 {
12406 }
12407 END_CATCH
348d480f
PA
12408}
12409
12410/* Clear BKP from a BPS. */
12411
12412static void
12413bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12414{
12415 bpstat bs;
12416
12417 for (bs = bps; bs; bs = bs->next)
12418 if (bs->breakpoint_at == bpt)
12419 {
12420 bs->breakpoint_at = NULL;
12421 bs->old_val = NULL;
12422 /* bs->commands will be freed later. */
12423 }
12424}
12425
12426/* Callback for iterate_over_threads. */
12427static int
12428bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12429{
9a3c8263 12430 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12431
12432 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12433 return 0;
12434}
12435
12436/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12437 callbacks. */
12438
12439static void
12440say_where (struct breakpoint *b)
12441{
12442 struct value_print_options opts;
12443
12444 get_user_print_options (&opts);
12445
12446 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12447 single string. */
12448 if (b->loc == NULL)
12449 {
f00aae0f
KS
12450 /* For pending locations, the output differs slightly based
12451 on b->extra_string. If this is non-NULL, it contains either
12452 a condition or dprintf arguments. */
12453 if (b->extra_string == NULL)
12454 {
12455 printf_filtered (_(" (%s) pending."),
d28cd78a 12456 event_location_to_string (b->location.get ()));
f00aae0f
KS
12457 }
12458 else if (b->type == bp_dprintf)
12459 {
12460 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12461 event_location_to_string (b->location.get ()),
f00aae0f
KS
12462 b->extra_string);
12463 }
12464 else
12465 {
12466 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12467 event_location_to_string (b->location.get ()),
f00aae0f
KS
12468 b->extra_string);
12469 }
348d480f
PA
12470 }
12471 else
12472 {
2f202fde 12473 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12474 {
12475 printf_filtered (" at ");
12476 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12477 gdb_stdout);
12478 }
2f202fde 12479 if (b->loc->symtab != NULL)
f8eba3c6
TT
12480 {
12481 /* If there is a single location, we can print the location
12482 more nicely. */
12483 if (b->loc->next == NULL)
12484 printf_filtered (": file %s, line %d.",
05cba821
JK
12485 symtab_to_filename_for_display (b->loc->symtab),
12486 b->loc->line_number);
f8eba3c6
TT
12487 else
12488 /* This is not ideal, but each location may have a
12489 different file name, and this at least reflects the
12490 real situation somewhat. */
f00aae0f 12491 printf_filtered (": %s.",
d28cd78a 12492 event_location_to_string (b->location.get ()));
f8eba3c6 12493 }
348d480f
PA
12494
12495 if (b->loc->next)
12496 {
12497 struct bp_location *loc = b->loc;
12498 int n = 0;
12499 for (; loc; loc = loc->next)
12500 ++n;
12501 printf_filtered (" (%d locations)", n);
12502 }
12503 }
12504}
12505
348d480f
PA
12506/* Default bp_location_ops methods. */
12507
12508static void
12509bp_location_dtor (struct bp_location *self)
12510{
348d480f
PA
12511 xfree (self->function_name);
12512}
12513
12514static const struct bp_location_ops bp_location_ops =
12515{
12516 bp_location_dtor
12517};
12518
c1fc2657 12519/* Destructor for the breakpoint base class. */
348d480f 12520
c1fc2657 12521breakpoint::~breakpoint ()
348d480f 12522{
c1fc2657
SM
12523 xfree (this->cond_string);
12524 xfree (this->extra_string);
12525 xfree (this->filter);
348d480f
PA
12526}
12527
2060206e
PA
12528static struct bp_location *
12529base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12530{
5625a286 12531 return new bp_location (&bp_location_ops, self);
348d480f
PA
12532}
12533
2060206e
PA
12534static void
12535base_breakpoint_re_set (struct breakpoint *b)
12536{
12537 /* Nothing to re-set. */
12538}
12539
12540#define internal_error_pure_virtual_called() \
12541 gdb_assert_not_reached ("pure virtual function called")
12542
12543static int
12544base_breakpoint_insert_location (struct bp_location *bl)
12545{
12546 internal_error_pure_virtual_called ();
12547}
12548
12549static int
73971819
PA
12550base_breakpoint_remove_location (struct bp_location *bl,
12551 enum remove_bp_reason reason)
2060206e
PA
12552{
12553 internal_error_pure_virtual_called ();
12554}
12555
12556static int
12557base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12558 struct address_space *aspace,
09ac7c10
TT
12559 CORE_ADDR bp_addr,
12560 const struct target_waitstatus *ws)
2060206e
PA
12561{
12562 internal_error_pure_virtual_called ();
12563}
12564
12565static void
12566base_breakpoint_check_status (bpstat bs)
12567{
12568 /* Always stop. */
12569}
12570
12571/* A "works_in_software_mode" breakpoint_ops method that just internal
12572 errors. */
12573
12574static int
12575base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12576{
12577 internal_error_pure_virtual_called ();
12578}
12579
12580/* A "resources_needed" breakpoint_ops method that just internal
12581 errors. */
12582
12583static int
12584base_breakpoint_resources_needed (const struct bp_location *bl)
12585{
12586 internal_error_pure_virtual_called ();
12587}
12588
12589static enum print_stop_action
12590base_breakpoint_print_it (bpstat bs)
12591{
12592 internal_error_pure_virtual_called ();
12593}
12594
12595static void
12596base_breakpoint_print_one_detail (const struct breakpoint *self,
12597 struct ui_out *uiout)
12598{
12599 /* nothing */
12600}
12601
12602static void
12603base_breakpoint_print_mention (struct breakpoint *b)
12604{
12605 internal_error_pure_virtual_called ();
12606}
12607
12608static void
12609base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12610{
12611 internal_error_pure_virtual_called ();
12612}
12613
983af33b 12614static void
f00aae0f
KS
12615base_breakpoint_create_sals_from_location
12616 (const struct event_location *location,
12617 struct linespec_result *canonical,
12618 enum bptype type_wanted)
983af33b
SDJ
12619{
12620 internal_error_pure_virtual_called ();
12621}
12622
12623static void
12624base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12625 struct linespec_result *c,
e1e01040
PA
12626 gdb::unique_xmalloc_ptr<char> cond_string,
12627 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12628 enum bptype type_wanted,
12629 enum bpdisp disposition,
12630 int thread,
12631 int task, int ignore_count,
12632 const struct breakpoint_ops *o,
12633 int from_tty, int enabled,
44f238bb 12634 int internal, unsigned flags)
983af33b
SDJ
12635{
12636 internal_error_pure_virtual_called ();
12637}
12638
6c5b2ebe 12639static std::vector<symtab_and_line>
f00aae0f
KS
12640base_breakpoint_decode_location (struct breakpoint *b,
12641 const struct event_location *location,
6c5b2ebe 12642 struct program_space *search_pspace)
983af33b
SDJ
12643{
12644 internal_error_pure_virtual_called ();
12645}
12646
ab04a2af
TT
12647/* The default 'explains_signal' method. */
12648
47591c29 12649static int
427cd150 12650base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12651{
47591c29 12652 return 1;
ab04a2af
TT
12653}
12654
9d6e6e84
HZ
12655/* The default "after_condition_true" method. */
12656
12657static void
12658base_breakpoint_after_condition_true (struct bpstats *bs)
12659{
12660 /* Nothing to do. */
12661}
12662
ab04a2af 12663struct breakpoint_ops base_breakpoint_ops =
2060206e 12664{
2060206e
PA
12665 base_breakpoint_allocate_location,
12666 base_breakpoint_re_set,
12667 base_breakpoint_insert_location,
12668 base_breakpoint_remove_location,
12669 base_breakpoint_breakpoint_hit,
12670 base_breakpoint_check_status,
12671 base_breakpoint_resources_needed,
12672 base_breakpoint_works_in_software_mode,
12673 base_breakpoint_print_it,
12674 NULL,
12675 base_breakpoint_print_one_detail,
12676 base_breakpoint_print_mention,
983af33b 12677 base_breakpoint_print_recreate,
5f700d83 12678 base_breakpoint_create_sals_from_location,
983af33b 12679 base_breakpoint_create_breakpoints_sal,
5f700d83 12680 base_breakpoint_decode_location,
9d6e6e84
HZ
12681 base_breakpoint_explains_signal,
12682 base_breakpoint_after_condition_true,
2060206e
PA
12683};
12684
12685/* Default breakpoint_ops methods. */
12686
12687static void
348d480f
PA
12688bkpt_re_set (struct breakpoint *b)
12689{
06edf0c0 12690 /* FIXME: is this still reachable? */
9ef9e6a6 12691 if (breakpoint_event_location_empty_p (b))
06edf0c0 12692 {
f00aae0f 12693 /* Anything without a location can't be re-set. */
348d480f 12694 delete_breakpoint (b);
06edf0c0 12695 return;
348d480f 12696 }
06edf0c0
PA
12697
12698 breakpoint_re_set_default (b);
348d480f
PA
12699}
12700
2060206e 12701static int
348d480f
PA
12702bkpt_insert_location (struct bp_location *bl)
12703{
cd6c3b4f
YQ
12704 CORE_ADDR addr = bl->target_info.reqstd_address;
12705
579c6ad9 12706 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12707 bl->target_info.placed_address = addr;
12708
348d480f 12709 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12710 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12711 else
7c16b83e 12712 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12713}
12714
2060206e 12715static int
73971819 12716bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12717{
12718 if (bl->loc_type == bp_loc_hardware_breakpoint)
12719 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12720 else
73971819 12721 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12722}
12723
2060206e 12724static int
348d480f 12725bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12726 struct address_space *aspace, CORE_ADDR bp_addr,
12727 const struct target_waitstatus *ws)
348d480f 12728{
09ac7c10 12729 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12730 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12731 return 0;
12732
348d480f
PA
12733 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12734 aspace, bp_addr))
12735 return 0;
12736
12737 if (overlay_debugging /* unmapped overlay section */
12738 && section_is_overlay (bl->section)
12739 && !section_is_mapped (bl->section))
12740 return 0;
12741
12742 return 1;
12743}
12744
cd1608cc
PA
12745static int
12746dprintf_breakpoint_hit (const struct bp_location *bl,
12747 struct address_space *aspace, CORE_ADDR bp_addr,
12748 const struct target_waitstatus *ws)
12749{
12750 if (dprintf_style == dprintf_style_agent
12751 && target_can_run_breakpoint_commands ())
12752 {
12753 /* An agent-style dprintf never causes a stop. If we see a trap
12754 for this address it must be for a breakpoint that happens to
12755 be set at the same address. */
12756 return 0;
12757 }
12758
12759 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12760}
12761
2060206e 12762static int
348d480f
PA
12763bkpt_resources_needed (const struct bp_location *bl)
12764{
12765 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12766
12767 return 1;
12768}
12769
2060206e 12770static enum print_stop_action
348d480f
PA
12771bkpt_print_it (bpstat bs)
12772{
348d480f
PA
12773 struct breakpoint *b;
12774 const struct bp_location *bl;
001c8c33 12775 int bp_temp;
79a45e25 12776 struct ui_out *uiout = current_uiout;
348d480f
PA
12777
12778 gdb_assert (bs->bp_location_at != NULL);
12779
12780 bl = bs->bp_location_at;
12781 b = bs->breakpoint_at;
12782
001c8c33
PA
12783 bp_temp = b->disposition == disp_del;
12784 if (bl->address != bl->requested_address)
12785 breakpoint_adjustment_warning (bl->requested_address,
12786 bl->address,
12787 b->number, 1);
12788 annotate_breakpoint (b->number);
f303dbd6
PA
12789 maybe_print_thread_hit_breakpoint (uiout);
12790
001c8c33 12791 if (bp_temp)
112e8700 12792 uiout->text ("Temporary breakpoint ");
001c8c33 12793 else
112e8700
SM
12794 uiout->text ("Breakpoint ");
12795 if (uiout->is_mi_like_p ())
348d480f 12796 {
112e8700 12797 uiout->field_string ("reason",
001c8c33 12798 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12799 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12800 }
112e8700
SM
12801 uiout->field_int ("bkptno", b->number);
12802 uiout->text (", ");
06edf0c0 12803
001c8c33 12804 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12805}
12806
2060206e 12807static void
06edf0c0
PA
12808bkpt_print_mention (struct breakpoint *b)
12809{
112e8700 12810 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12811 return;
12812
12813 switch (b->type)
12814 {
12815 case bp_breakpoint:
12816 case bp_gnu_ifunc_resolver:
12817 if (b->disposition == disp_del)
12818 printf_filtered (_("Temporary breakpoint"));
12819 else
12820 printf_filtered (_("Breakpoint"));
12821 printf_filtered (_(" %d"), b->number);
12822 if (b->type == bp_gnu_ifunc_resolver)
12823 printf_filtered (_(" at gnu-indirect-function resolver"));
12824 break;
12825 case bp_hardware_breakpoint:
12826 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12827 break;
e7e0cddf
SS
12828 case bp_dprintf:
12829 printf_filtered (_("Dprintf %d"), b->number);
12830 break;
06edf0c0
PA
12831 }
12832
12833 say_where (b);
12834}
12835
2060206e 12836static void
06edf0c0
PA
12837bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12838{
12839 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12840 fprintf_unfiltered (fp, "tbreak");
12841 else if (tp->type == bp_breakpoint)
12842 fprintf_unfiltered (fp, "break");
12843 else if (tp->type == bp_hardware_breakpoint
12844 && tp->disposition == disp_del)
12845 fprintf_unfiltered (fp, "thbreak");
12846 else if (tp->type == bp_hardware_breakpoint)
12847 fprintf_unfiltered (fp, "hbreak");
12848 else
12849 internal_error (__FILE__, __LINE__,
12850 _("unhandled breakpoint type %d"), (int) tp->type);
12851
f00aae0f 12852 fprintf_unfiltered (fp, " %s",
d28cd78a 12853 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12854
12855 /* Print out extra_string if this breakpoint is pending. It might
12856 contain, for example, conditions that were set by the user. */
12857 if (tp->loc == NULL && tp->extra_string != NULL)
12858 fprintf_unfiltered (fp, " %s", tp->extra_string);
12859
dd11a36c 12860 print_recreate_thread (tp, fp);
06edf0c0
PA
12861}
12862
983af33b 12863static void
f00aae0f
KS
12864bkpt_create_sals_from_location (const struct event_location *location,
12865 struct linespec_result *canonical,
12866 enum bptype type_wanted)
983af33b 12867{
f00aae0f 12868 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12869}
12870
12871static void
12872bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12873 struct linespec_result *canonical,
e1e01040
PA
12874 gdb::unique_xmalloc_ptr<char> cond_string,
12875 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12876 enum bptype type_wanted,
12877 enum bpdisp disposition,
12878 int thread,
12879 int task, int ignore_count,
12880 const struct breakpoint_ops *ops,
12881 int from_tty, int enabled,
44f238bb 12882 int internal, unsigned flags)
983af33b 12883{
023fa29b 12884 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12885 std::move (cond_string),
12886 std::move (extra_string),
e7e0cddf 12887 type_wanted,
983af33b
SDJ
12888 disposition, thread, task,
12889 ignore_count, ops, from_tty,
44f238bb 12890 enabled, internal, flags);
983af33b
SDJ
12891}
12892
6c5b2ebe 12893static std::vector<symtab_and_line>
f00aae0f
KS
12894bkpt_decode_location (struct breakpoint *b,
12895 const struct event_location *location,
6c5b2ebe 12896 struct program_space *search_pspace)
983af33b 12897{
6c5b2ebe 12898 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12899}
12900
06edf0c0
PA
12901/* Virtual table for internal breakpoints. */
12902
12903static void
12904internal_bkpt_re_set (struct breakpoint *b)
12905{
12906 switch (b->type)
12907 {
12908 /* Delete overlay event and longjmp master breakpoints; they
12909 will be reset later by breakpoint_re_set. */
12910 case bp_overlay_event:
12911 case bp_longjmp_master:
12912 case bp_std_terminate_master:
12913 case bp_exception_master:
12914 delete_breakpoint (b);
12915 break;
12916
12917 /* This breakpoint is special, it's set up when the inferior
12918 starts and we really don't want to touch it. */
12919 case bp_shlib_event:
12920
12921 /* Like bp_shlib_event, this breakpoint type is special. Once
12922 it is set up, we do not want to touch it. */
12923 case bp_thread_event:
12924 break;
12925 }
12926}
12927
12928static void
12929internal_bkpt_check_status (bpstat bs)
12930{
a9b3a50f
PA
12931 if (bs->breakpoint_at->type == bp_shlib_event)
12932 {
12933 /* If requested, stop when the dynamic linker notifies GDB of
12934 events. This allows the user to get control and place
12935 breakpoints in initializer routines for dynamically loaded
12936 objects (among other things). */
12937 bs->stop = stop_on_solib_events;
12938 bs->print = stop_on_solib_events;
12939 }
12940 else
12941 bs->stop = 0;
06edf0c0
PA
12942}
12943
12944static enum print_stop_action
12945internal_bkpt_print_it (bpstat bs)
12946{
06edf0c0 12947 struct breakpoint *b;
06edf0c0 12948
06edf0c0
PA
12949 b = bs->breakpoint_at;
12950
06edf0c0
PA
12951 switch (b->type)
12952 {
348d480f
PA
12953 case bp_shlib_event:
12954 /* Did we stop because the user set the stop_on_solib_events
12955 variable? (If so, we report this as a generic, "Stopped due
12956 to shlib event" message.) */
edcc5120 12957 print_solib_event (0);
348d480f
PA
12958 break;
12959
12960 case bp_thread_event:
12961 /* Not sure how we will get here.
12962 GDB should not stop for these breakpoints. */
12963 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12964 break;
12965
12966 case bp_overlay_event:
12967 /* By analogy with the thread event, GDB should not stop for these. */
12968 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12969 break;
12970
12971 case bp_longjmp_master:
12972 /* These should never be enabled. */
12973 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12974 break;
12975
12976 case bp_std_terminate_master:
12977 /* These should never be enabled. */
12978 printf_filtered (_("std::terminate Master Breakpoint: "
12979 "gdb should not stop!\n"));
348d480f
PA
12980 break;
12981
12982 case bp_exception_master:
12983 /* These should never be enabled. */
12984 printf_filtered (_("Exception Master Breakpoint: "
12985 "gdb should not stop!\n"));
06edf0c0
PA
12986 break;
12987 }
12988
001c8c33 12989 return PRINT_NOTHING;
06edf0c0
PA
12990}
12991
12992static void
12993internal_bkpt_print_mention (struct breakpoint *b)
12994{
12995 /* Nothing to mention. These breakpoints are internal. */
12996}
12997
06edf0c0
PA
12998/* Virtual table for momentary breakpoints */
12999
13000static void
13001momentary_bkpt_re_set (struct breakpoint *b)
13002{
13003 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13004 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13005 Otherwise these should have been blown away via the cleanup chain
13006 or by breakpoint_init_inferior when we rerun the executable. */
13007}
13008
13009static void
13010momentary_bkpt_check_status (bpstat bs)
13011{
13012 /* Nothing. The point of these breakpoints is causing a stop. */
13013}
13014
13015static enum print_stop_action
13016momentary_bkpt_print_it (bpstat bs)
13017{
001c8c33 13018 return PRINT_UNKNOWN;
348d480f
PA
13019}
13020
06edf0c0
PA
13021static void
13022momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13023{
06edf0c0 13024 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13025}
13026
e2e4d78b
JK
13027/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13028
13029 It gets cleared already on the removal of the first one of such placed
13030 breakpoints. This is OK as they get all removed altogether. */
13031
c1fc2657 13032longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 13033{
c1fc2657 13034 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 13035
c1fc2657 13036 if (tp != NULL)
e2e4d78b 13037 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
13038}
13039
55aa24fb
SDJ
13040/* Specific methods for probe breakpoints. */
13041
13042static int
13043bkpt_probe_insert_location (struct bp_location *bl)
13044{
13045 int v = bkpt_insert_location (bl);
13046
13047 if (v == 0)
13048 {
13049 /* The insertion was successful, now let's set the probe's semaphore
13050 if needed. */
0ea5cda8
SDJ
13051 if (bl->probe.probe->pops->set_semaphore != NULL)
13052 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13053 bl->probe.objfile,
13054 bl->gdbarch);
55aa24fb
SDJ
13055 }
13056
13057 return v;
13058}
13059
13060static int
73971819
PA
13061bkpt_probe_remove_location (struct bp_location *bl,
13062 enum remove_bp_reason reason)
55aa24fb
SDJ
13063{
13064 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13065 if (bl->probe.probe->pops->clear_semaphore != NULL)
13066 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13067 bl->probe.objfile,
13068 bl->gdbarch);
55aa24fb 13069
73971819 13070 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13071}
13072
13073static void
f00aae0f 13074bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13075 struct linespec_result *canonical,
f00aae0f 13076 enum bptype type_wanted)
55aa24fb
SDJ
13077{
13078 struct linespec_sals lsal;
13079
c2f4122d 13080 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13081 lsal.canonical
13082 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13083 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
13084}
13085
6c5b2ebe 13086static std::vector<symtab_and_line>
f00aae0f
KS
13087bkpt_probe_decode_location (struct breakpoint *b,
13088 const struct event_location *location,
6c5b2ebe 13089 struct program_space *search_pspace)
55aa24fb 13090{
6c5b2ebe
PA
13091 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
13092 if (sals.empty ())
55aa24fb 13093 error (_("probe not found"));
6c5b2ebe 13094 return sals;
55aa24fb
SDJ
13095}
13096
348d480f 13097/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13098
348d480f
PA
13099static void
13100tracepoint_re_set (struct breakpoint *b)
13101{
13102 breakpoint_re_set_default (b);
13103}
876fa593 13104
348d480f
PA
13105static int
13106tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13107 struct address_space *aspace, CORE_ADDR bp_addr,
13108 const struct target_waitstatus *ws)
348d480f
PA
13109{
13110 /* By definition, the inferior does not report stops at
13111 tracepoints. */
13112 return 0;
74960c60
VP
13113}
13114
13115static void
348d480f
PA
13116tracepoint_print_one_detail (const struct breakpoint *self,
13117 struct ui_out *uiout)
74960c60 13118{
d9b3f62e
PA
13119 struct tracepoint *tp = (struct tracepoint *) self;
13120 if (tp->static_trace_marker_id)
348d480f
PA
13121 {
13122 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13123
112e8700
SM
13124 uiout->text ("\tmarker id is ");
13125 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13126 tp->static_trace_marker_id);
112e8700 13127 uiout->text ("\n");
348d480f 13128 }
0d381245
VP
13129}
13130
a474d7c2 13131static void
348d480f 13132tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13133{
112e8700 13134 if (current_uiout->is_mi_like_p ())
348d480f 13135 return;
cc59ec59 13136
348d480f
PA
13137 switch (b->type)
13138 {
13139 case bp_tracepoint:
13140 printf_filtered (_("Tracepoint"));
13141 printf_filtered (_(" %d"), b->number);
13142 break;
13143 case bp_fast_tracepoint:
13144 printf_filtered (_("Fast tracepoint"));
13145 printf_filtered (_(" %d"), b->number);
13146 break;
13147 case bp_static_tracepoint:
13148 printf_filtered (_("Static tracepoint"));
13149 printf_filtered (_(" %d"), b->number);
13150 break;
13151 default:
13152 internal_error (__FILE__, __LINE__,
13153 _("unhandled tracepoint type %d"), (int) b->type);
13154 }
13155
13156 say_where (b);
a474d7c2
PA
13157}
13158
348d480f 13159static void
d9b3f62e 13160tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13161{
d9b3f62e
PA
13162 struct tracepoint *tp = (struct tracepoint *) self;
13163
13164 if (self->type == bp_fast_tracepoint)
348d480f 13165 fprintf_unfiltered (fp, "ftrace");
c93e8391 13166 else if (self->type == bp_static_tracepoint)
348d480f 13167 fprintf_unfiltered (fp, "strace");
d9b3f62e 13168 else if (self->type == bp_tracepoint)
348d480f
PA
13169 fprintf_unfiltered (fp, "trace");
13170 else
13171 internal_error (__FILE__, __LINE__,
d9b3f62e 13172 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13173
f00aae0f 13174 fprintf_unfiltered (fp, " %s",
d28cd78a 13175 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13176 print_recreate_thread (self, fp);
13177
13178 if (tp->pass_count)
13179 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13180}
13181
983af33b 13182static void
f00aae0f
KS
13183tracepoint_create_sals_from_location (const struct event_location *location,
13184 struct linespec_result *canonical,
13185 enum bptype type_wanted)
983af33b 13186{
f00aae0f 13187 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13188}
13189
13190static void
13191tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13192 struct linespec_result *canonical,
e1e01040
PA
13193 gdb::unique_xmalloc_ptr<char> cond_string,
13194 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13195 enum bptype type_wanted,
13196 enum bpdisp disposition,
13197 int thread,
13198 int task, int ignore_count,
13199 const struct breakpoint_ops *ops,
13200 int from_tty, int enabled,
44f238bb 13201 int internal, unsigned flags)
983af33b 13202{
023fa29b 13203 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13204 std::move (cond_string),
13205 std::move (extra_string),
e7e0cddf 13206 type_wanted,
983af33b
SDJ
13207 disposition, thread, task,
13208 ignore_count, ops, from_tty,
44f238bb 13209 enabled, internal, flags);
983af33b
SDJ
13210}
13211
6c5b2ebe 13212static std::vector<symtab_and_line>
f00aae0f
KS
13213tracepoint_decode_location (struct breakpoint *b,
13214 const struct event_location *location,
6c5b2ebe 13215 struct program_space *search_pspace)
983af33b 13216{
6c5b2ebe 13217 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13218}
13219
2060206e 13220struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13221
55aa24fb
SDJ
13222/* The breakpoint_ops structure to be use on tracepoints placed in a
13223 static probe. */
13224
13225static void
f00aae0f
KS
13226tracepoint_probe_create_sals_from_location
13227 (const struct event_location *location,
13228 struct linespec_result *canonical,
13229 enum bptype type_wanted)
55aa24fb
SDJ
13230{
13231 /* We use the same method for breakpoint on probes. */
f00aae0f 13232 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13233}
13234
6c5b2ebe 13235static std::vector<symtab_and_line>
f00aae0f
KS
13236tracepoint_probe_decode_location (struct breakpoint *b,
13237 const struct event_location *location,
6c5b2ebe 13238 struct program_space *search_pspace)
55aa24fb
SDJ
13239{
13240 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13241 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13242}
13243
13244static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13245
5c2b4418
HZ
13246/* Dprintf breakpoint_ops methods. */
13247
13248static void
13249dprintf_re_set (struct breakpoint *b)
13250{
13251 breakpoint_re_set_default (b);
13252
f00aae0f
KS
13253 /* extra_string should never be non-NULL for dprintf. */
13254 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13255
13256 /* 1 - connect to target 1, that can run breakpoint commands.
13257 2 - create a dprintf, which resolves fine.
13258 3 - disconnect from target 1
13259 4 - connect to target 2, that can NOT run breakpoint commands.
13260
13261 After steps #3/#4, you'll want the dprintf command list to
13262 be updated, because target 1 and 2 may well return different
13263 answers for target_can_run_breakpoint_commands().
13264 Given absence of finer grained resetting, we get to do
13265 it all the time. */
13266 if (b->extra_string != NULL)
13267 update_dprintf_command_list (b);
13268}
13269
2d9442cc
HZ
13270/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13271
13272static void
13273dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13274{
f00aae0f 13275 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13276 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13277 tp->extra_string);
13278 print_recreate_thread (tp, fp);
13279}
13280
9d6e6e84
HZ
13281/* Implement the "after_condition_true" breakpoint_ops method for
13282 dprintf.
13283
13284 dprintf's are implemented with regular commands in their command
13285 list, but we run the commands here instead of before presenting the
13286 stop to the user, as dprintf's don't actually cause a stop. This
13287 also makes it so that the commands of multiple dprintfs at the same
13288 address are all handled. */
13289
13290static void
13291dprintf_after_condition_true (struct bpstats *bs)
13292{
04afa70c 13293 struct bpstats tmp_bs;
9d6e6e84
HZ
13294 struct bpstats *tmp_bs_p = &tmp_bs;
13295
13296 /* dprintf's never cause a stop. This wasn't set in the
13297 check_status hook instead because that would make the dprintf's
13298 condition not be evaluated. */
13299 bs->stop = 0;
13300
13301 /* Run the command list here. Take ownership of it instead of
13302 copying. We never want these commands to run later in
13303 bpstat_do_actions, if a breakpoint that causes a stop happens to
13304 be set at same address as this dprintf, or even if running the
13305 commands here throws. */
13306 tmp_bs.commands = bs->commands;
13307 bs->commands = NULL;
9d6e6e84
HZ
13308
13309 bpstat_do_actions_1 (&tmp_bs_p);
13310
13311 /* 'tmp_bs.commands' will usually be NULL by now, but
13312 bpstat_do_actions_1 may return early without processing the whole
13313 list. */
9d6e6e84
HZ
13314}
13315
983af33b
SDJ
13316/* The breakpoint_ops structure to be used on static tracepoints with
13317 markers (`-m'). */
13318
13319static void
f00aae0f 13320strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13321 struct linespec_result *canonical,
f00aae0f 13322 enum bptype type_wanted)
983af33b
SDJ
13323{
13324 struct linespec_sals lsal;
f00aae0f 13325 const char *arg_start, *arg;
983af33b 13326
f00aae0f
KS
13327 arg = arg_start = get_linespec_location (location);
13328 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13329
f2fc3015
TT
13330 std::string str (arg_start, arg - arg_start);
13331 const char *ptr = str.c_str ();
13332 canonical->location = new_linespec_location (&ptr);
983af33b 13333
8e9e35b1
TT
13334 lsal.canonical
13335 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13336 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13337}
13338
13339static void
13340strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13341 struct linespec_result *canonical,
e1e01040
PA
13342 gdb::unique_xmalloc_ptr<char> cond_string,
13343 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13344 enum bptype type_wanted,
13345 enum bpdisp disposition,
13346 int thread,
13347 int task, int ignore_count,
13348 const struct breakpoint_ops *ops,
13349 int from_tty, int enabled,
44f238bb 13350 int internal, unsigned flags)
983af33b 13351{
6c5b2ebe 13352 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13353
13354 /* If the user is creating a static tracepoint by marker id
13355 (strace -m MARKER_ID), then store the sals index, so that
13356 breakpoint_re_set can try to match up which of the newly
13357 found markers corresponds to this one, and, don't try to
13358 expand multiple locations for each sal, given than SALS
13359 already should contain all sals for MARKER_ID. */
13360
6c5b2ebe 13361 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13362 {
6c5b2ebe
PA
13363 event_location_up location
13364 = copy_event_location (canonical->location.get ());
983af33b 13365
b270e6f9 13366 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13367 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13368 std::move (location), NULL,
e1e01040
PA
13369 std::move (cond_string),
13370 std::move (extra_string),
e7e0cddf 13371 type_wanted, disposition,
983af33b 13372 thread, task, ignore_count, ops,
44f238bb 13373 from_tty, enabled, internal, flags,
983af33b
SDJ
13374 canonical->special_display);
13375 /* Given that its possible to have multiple markers with
13376 the same string id, if the user is creating a static
13377 tracepoint by marker id ("strace -m MARKER_ID"), then
13378 store the sals index, so that breakpoint_re_set can
13379 try to match up which of the newly found markers
13380 corresponds to this one */
13381 tp->static_trace_marker_id_idx = i;
13382
b270e6f9 13383 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13384 }
13385}
13386
6c5b2ebe 13387static std::vector<symtab_and_line>
f00aae0f
KS
13388strace_marker_decode_location (struct breakpoint *b,
13389 const struct event_location *location,
6c5b2ebe 13390 struct program_space *search_pspace)
983af33b
SDJ
13391{
13392 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13393 const char *s = get_linespec_location (location);
983af33b 13394
6c5b2ebe
PA
13395 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13396 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13397 {
6c5b2ebe
PA
13398 sals[0] = sals[tp->static_trace_marker_id_idx];
13399 sals.resize (1);
13400 return sals;
983af33b
SDJ
13401 }
13402 else
13403 error (_("marker %s not found"), tp->static_trace_marker_id);
13404}
13405
13406static struct breakpoint_ops strace_marker_breakpoint_ops;
13407
13408static int
13409strace_marker_p (struct breakpoint *b)
13410{
13411 return b->ops == &strace_marker_breakpoint_ops;
13412}
13413
53a5351d 13414/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13415 structures. */
c906108c
SS
13416
13417void
fba45db2 13418delete_breakpoint (struct breakpoint *bpt)
c906108c 13419{
52f0bd74 13420 struct breakpoint *b;
c906108c 13421
8a3fe4f8 13422 gdb_assert (bpt != NULL);
c906108c 13423
4a64f543
MS
13424 /* Has this bp already been deleted? This can happen because
13425 multiple lists can hold pointers to bp's. bpstat lists are
13426 especial culprits.
13427
13428 One example of this happening is a watchpoint's scope bp. When
13429 the scope bp triggers, we notice that the watchpoint is out of
13430 scope, and delete it. We also delete its scope bp. But the
13431 scope bp is marked "auto-deleting", and is already on a bpstat.
13432 That bpstat is then checked for auto-deleting bp's, which are
13433 deleted.
13434
13435 A real solution to this problem might involve reference counts in
13436 bp's, and/or giving them pointers back to their referencing
13437 bpstat's, and teaching delete_breakpoint to only free a bp's
13438 storage when no more references were extent. A cheaper bandaid
13439 was chosen. */
c906108c
SS
13440 if (bpt->type == bp_none)
13441 return;
13442
4a64f543
MS
13443 /* At least avoid this stale reference until the reference counting
13444 of breakpoints gets resolved. */
d0fb5eae 13445 if (bpt->related_breakpoint != bpt)
e5a0a904 13446 {
d0fb5eae 13447 struct breakpoint *related;
3a5c3e22 13448 struct watchpoint *w;
d0fb5eae
JK
13449
13450 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13451 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13452 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13453 w = (struct watchpoint *) bpt;
13454 else
13455 w = NULL;
13456 if (w != NULL)
13457 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13458
13459 /* Unlink bpt from the bpt->related_breakpoint ring. */
13460 for (related = bpt; related->related_breakpoint != bpt;
13461 related = related->related_breakpoint);
13462 related->related_breakpoint = bpt->related_breakpoint;
13463 bpt->related_breakpoint = bpt;
e5a0a904
JK
13464 }
13465
a9634178
TJB
13466 /* watch_command_1 creates a watchpoint but only sets its number if
13467 update_watchpoint succeeds in creating its bp_locations. If there's
13468 a problem in that process, we'll be asked to delete the half-created
13469 watchpoint. In that case, don't announce the deletion. */
13470 if (bpt->number)
13471 observer_notify_breakpoint_deleted (bpt);
c906108c 13472
c906108c
SS
13473 if (breakpoint_chain == bpt)
13474 breakpoint_chain = bpt->next;
13475
c906108c
SS
13476 ALL_BREAKPOINTS (b)
13477 if (b->next == bpt)
c5aa993b
JM
13478 {
13479 b->next = bpt->next;
13480 break;
13481 }
c906108c 13482
f431efe5
PA
13483 /* Be sure no bpstat's are pointing at the breakpoint after it's
13484 been freed. */
13485 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13486 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13487 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13488 commands are associated with the bpstat; if we remove it here,
13489 then the later call to bpstat_do_actions (&stop_bpstat); in
13490 event-top.c won't do anything, and temporary breakpoints with
13491 commands won't work. */
13492
13493 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13494
4a64f543
MS
13495 /* Now that breakpoint is removed from breakpoint list, update the
13496 global location list. This will remove locations that used to
13497 belong to this breakpoint. Do this before freeing the breakpoint
13498 itself, since remove_breakpoint looks at location's owner. It
13499 might be better design to have location completely
13500 self-contained, but it's not the case now. */
44702360 13501 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13502
4a64f543
MS
13503 /* On the chance that someone will soon try again to delete this
13504 same bp, we mark it as deleted before freeing its storage. */
c906108c 13505 bpt->type = bp_none;
4d01a485 13506 delete bpt;
c906108c
SS
13507}
13508
4d6140d9
AC
13509static void
13510do_delete_breakpoint_cleanup (void *b)
13511{
9a3c8263 13512 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13513}
13514
13515struct cleanup *
13516make_cleanup_delete_breakpoint (struct breakpoint *b)
13517{
13518 return make_cleanup (do_delete_breakpoint_cleanup, b);
13519}
13520
51be5b68
PA
13521/* Iterator function to call a user-provided callback function once
13522 for each of B and its related breakpoints. */
13523
13524static void
13525iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13526 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13527{
13528 struct breakpoint *related;
13529
13530 related = b;
13531 do
13532 {
13533 struct breakpoint *next;
13534
13535 /* FUNCTION may delete RELATED. */
13536 next = related->related_breakpoint;
13537
13538 if (next == related)
13539 {
13540 /* RELATED is the last ring entry. */
48649e1b 13541 function (related);
51be5b68
PA
13542
13543 /* FUNCTION may have deleted it, so we'd never reach back to
13544 B. There's nothing left to do anyway, so just break
13545 out. */
13546 break;
13547 }
13548 else
48649e1b 13549 function (related);
51be5b68
PA
13550
13551 related = next;
13552 }
13553 while (related != b);
13554}
95a42b64 13555
4495129a 13556static void
fba45db2 13557delete_command (char *arg, int from_tty)
c906108c 13558{
35df4500 13559 struct breakpoint *b, *b_tmp;
c906108c 13560
ea9365bb
TT
13561 dont_repeat ();
13562
c906108c
SS
13563 if (arg == 0)
13564 {
13565 int breaks_to_delete = 0;
13566
46c6471b
PA
13567 /* Delete all breakpoints if no argument. Do not delete
13568 internal breakpoints, these have to be deleted with an
13569 explicit breakpoint number argument. */
c5aa993b 13570 ALL_BREAKPOINTS (b)
46c6471b 13571 if (user_breakpoint_p (b))
973d738b
DJ
13572 {
13573 breaks_to_delete = 1;
13574 break;
13575 }
c906108c
SS
13576
13577 /* Ask user only if there are some breakpoints to delete. */
13578 if (!from_tty
e2e0b3e5 13579 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13580 {
35df4500 13581 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13582 if (user_breakpoint_p (b))
c5aa993b 13583 delete_breakpoint (b);
c906108c
SS
13584 }
13585 }
13586 else
48649e1b
TT
13587 map_breakpoint_numbers
13588 (arg, [&] (breakpoint *b)
13589 {
13590 iterate_over_related_breakpoints (b, delete_breakpoint);
13591 });
c906108c
SS
13592}
13593
c2f4122d
PA
13594/* Return true if all locations of B bound to PSPACE are pending. If
13595 PSPACE is NULL, all locations of all program spaces are
13596 considered. */
13597
0d381245 13598static int
c2f4122d 13599all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13600{
c2f4122d
PA
13601 struct bp_location *loc;
13602
13603 for (loc = b->loc; loc != NULL; loc = loc->next)
13604 if ((pspace == NULL
13605 || loc->pspace == pspace)
13606 && !loc->shlib_disabled
8645ff69 13607 && !loc->pspace->executing_startup)
0d381245
VP
13608 return 0;
13609 return 1;
fe3f5fa8
VP
13610}
13611
776592bf
DE
13612/* Subroutine of update_breakpoint_locations to simplify it.
13613 Return non-zero if multiple fns in list LOC have the same name.
13614 Null names are ignored. */
13615
13616static int
13617ambiguous_names_p (struct bp_location *loc)
13618{
13619 struct bp_location *l;
13620 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13621 (int (*) (const void *,
13622 const void *)) streq,
776592bf
DE
13623 NULL, xcalloc, xfree);
13624
13625 for (l = loc; l != NULL; l = l->next)
13626 {
13627 const char **slot;
13628 const char *name = l->function_name;
13629
13630 /* Allow for some names to be NULL, ignore them. */
13631 if (name == NULL)
13632 continue;
13633
13634 slot = (const char **) htab_find_slot (htab, (const void *) name,
13635 INSERT);
4a64f543
MS
13636 /* NOTE: We can assume slot != NULL here because xcalloc never
13637 returns NULL. */
776592bf
DE
13638 if (*slot != NULL)
13639 {
13640 htab_delete (htab);
13641 return 1;
13642 }
13643 *slot = name;
13644 }
13645
13646 htab_delete (htab);
13647 return 0;
13648}
13649
0fb4aa4b
PA
13650/* When symbols change, it probably means the sources changed as well,
13651 and it might mean the static tracepoint markers are no longer at
13652 the same address or line numbers they used to be at last we
13653 checked. Losing your static tracepoints whenever you rebuild is
13654 undesirable. This function tries to resync/rematch gdb static
13655 tracepoints with the markers on the target, for static tracepoints
13656 that have not been set by marker id. Static tracepoint that have
13657 been set by marker id are reset by marker id in breakpoint_re_set.
13658 The heuristic is:
13659
13660 1) For a tracepoint set at a specific address, look for a marker at
13661 the old PC. If one is found there, assume to be the same marker.
13662 If the name / string id of the marker found is different from the
13663 previous known name, assume that means the user renamed the marker
13664 in the sources, and output a warning.
13665
13666 2) For a tracepoint set at a given line number, look for a marker
13667 at the new address of the old line number. If one is found there,
13668 assume to be the same marker. If the name / string id of the
13669 marker found is different from the previous known name, assume that
13670 means the user renamed the marker in the sources, and output a
13671 warning.
13672
13673 3) If a marker is no longer found at the same address or line, it
13674 may mean the marker no longer exists. But it may also just mean
13675 the code changed a bit. Maybe the user added a few lines of code
13676 that made the marker move up or down (in line number terms). Ask
13677 the target for info about the marker with the string id as we knew
13678 it. If found, update line number and address in the matching
13679 static tracepoint. This will get confused if there's more than one
13680 marker with the same ID (possible in UST, although unadvised
13681 precisely because it confuses tools). */
13682
13683static struct symtab_and_line
13684update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13685{
d9b3f62e 13686 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13687 struct static_tracepoint_marker marker;
13688 CORE_ADDR pc;
0fb4aa4b
PA
13689
13690 pc = sal.pc;
13691 if (sal.line)
13692 find_line_pc (sal.symtab, sal.line, &pc);
13693
13694 if (target_static_tracepoint_marker_at (pc, &marker))
13695 {
d9b3f62e 13696 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13697 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13698 b->number,
d9b3f62e 13699 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13700
d9b3f62e
PA
13701 xfree (tp->static_trace_marker_id);
13702 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13703 release_static_tracepoint_marker (&marker);
13704
13705 return sal;
13706 }
13707
13708 /* Old marker wasn't found on target at lineno. Try looking it up
13709 by string ID. */
13710 if (!sal.explicit_pc
13711 && sal.line != 0
13712 && sal.symtab != NULL
d9b3f62e 13713 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13714 {
13715 VEC(static_tracepoint_marker_p) *markers;
13716
13717 markers
d9b3f62e 13718 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13719
13720 if (!VEC_empty(static_tracepoint_marker_p, markers))
13721 {
0fb4aa4b 13722 struct symbol *sym;
80e1d417 13723 struct static_tracepoint_marker *tpmarker;
79a45e25 13724 struct ui_out *uiout = current_uiout;
67994074 13725 struct explicit_location explicit_loc;
0fb4aa4b 13726
80e1d417 13727 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13728
d9b3f62e 13729 xfree (tp->static_trace_marker_id);
80e1d417 13730 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13731
13732 warning (_("marker for static tracepoint %d (%s) not "
13733 "found at previous line number"),
d9b3f62e 13734 b->number, tp->static_trace_marker_id);
0fb4aa4b 13735
51abb421 13736 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13737 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13738 uiout->text ("Now in ");
0fb4aa4b
PA
13739 if (sym)
13740 {
112e8700
SM
13741 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13742 uiout->text (" at ");
0fb4aa4b 13743 }
112e8700 13744 uiout->field_string ("file",
05cba821 13745 symtab_to_filename_for_display (sal2.symtab));
112e8700 13746 uiout->text (":");
0fb4aa4b 13747
112e8700 13748 if (uiout->is_mi_like_p ())
0fb4aa4b 13749 {
0b0865da 13750 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13751
112e8700 13752 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13753 }
13754
112e8700
SM
13755 uiout->field_int ("line", sal2.line);
13756 uiout->text ("\n");
0fb4aa4b 13757
80e1d417 13758 b->loc->line_number = sal2.line;
2f202fde 13759 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13760
d28cd78a 13761 b->location.reset (NULL);
67994074
KS
13762 initialize_explicit_location (&explicit_loc);
13763 explicit_loc.source_filename
00e52e53 13764 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13765 explicit_loc.line_offset.offset = b->loc->line_number;
13766 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13767 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13768
13769 /* Might be nice to check if function changed, and warn if
13770 so. */
13771
80e1d417 13772 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13773 }
13774 }
13775 return sal;
13776}
13777
8d3788bd
VP
13778/* Returns 1 iff locations A and B are sufficiently same that
13779 we don't need to report breakpoint as changed. */
13780
13781static int
13782locations_are_equal (struct bp_location *a, struct bp_location *b)
13783{
13784 while (a && b)
13785 {
13786 if (a->address != b->address)
13787 return 0;
13788
13789 if (a->shlib_disabled != b->shlib_disabled)
13790 return 0;
13791
13792 if (a->enabled != b->enabled)
13793 return 0;
13794
13795 a = a->next;
13796 b = b->next;
13797 }
13798
13799 if ((a == NULL) != (b == NULL))
13800 return 0;
13801
13802 return 1;
13803}
13804
c2f4122d
PA
13805/* Split all locations of B that are bound to PSPACE out of B's
13806 location list to a separate list and return that list's head. If
13807 PSPACE is NULL, hoist out all locations of B. */
13808
13809static struct bp_location *
13810hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13811{
13812 struct bp_location head;
13813 struct bp_location *i = b->loc;
13814 struct bp_location **i_link = &b->loc;
13815 struct bp_location *hoisted = &head;
13816
13817 if (pspace == NULL)
13818 {
13819 i = b->loc;
13820 b->loc = NULL;
13821 return i;
13822 }
13823
13824 head.next = NULL;
13825
13826 while (i != NULL)
13827 {
13828 if (i->pspace == pspace)
13829 {
13830 *i_link = i->next;
13831 i->next = NULL;
13832 hoisted->next = i;
13833 hoisted = i;
13834 }
13835 else
13836 i_link = &i->next;
13837 i = *i_link;
13838 }
13839
13840 return head.next;
13841}
13842
13843/* Create new breakpoint locations for B (a hardware or software
13844 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13845 zero, then B is a ranged breakpoint. Only recreates locations for
13846 FILTER_PSPACE. Locations of other program spaces are left
13847 untouched. */
f1310107 13848
0e30163f 13849void
0d381245 13850update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13851 struct program_space *filter_pspace,
6c5b2ebe
PA
13852 gdb::array_view<const symtab_and_line> sals,
13853 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13854{
13855 int i;
c2f4122d 13856 struct bp_location *existing_locations;
0d381245 13857
6c5b2ebe 13858 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13859 {
13860 /* Ranged breakpoints have only one start location and one end
13861 location. */
13862 b->enable_state = bp_disabled;
f8eba3c6
TT
13863 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13864 "multiple locations found\n"),
13865 b->number);
13866 return;
13867 }
f1310107 13868
4a64f543
MS
13869 /* If there's no new locations, and all existing locations are
13870 pending, don't do anything. This optimizes the common case where
13871 all locations are in the same shared library, that was unloaded.
13872 We'd like to retain the location, so that when the library is
13873 loaded again, we don't loose the enabled/disabled status of the
13874 individual locations. */
6c5b2ebe 13875 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13876 return;
13877
c2f4122d 13878 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13879
6c5b2ebe 13880 for (const auto &sal : sals)
fe3f5fa8 13881 {
f8eba3c6
TT
13882 struct bp_location *new_loc;
13883
6c5b2ebe 13884 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13885
6c5b2ebe 13886 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13887
0d381245
VP
13888 /* Reparse conditions, they might contain references to the
13889 old symtab. */
13890 if (b->cond_string != NULL)
13891 {
bbc13ae3 13892 const char *s;
fe3f5fa8 13893
0d381245 13894 s = b->cond_string;
492d29ea 13895 TRY
0d381245 13896 {
6c5b2ebe
PA
13897 new_loc->cond = parse_exp_1 (&s, sal.pc,
13898 block_for_pc (sal.pc),
0d381245
VP
13899 0);
13900 }
492d29ea 13901 CATCH (e, RETURN_MASK_ERROR)
0d381245 13902 {
3e43a32a
MS
13903 warning (_("failed to reevaluate condition "
13904 "for breakpoint %d: %s"),
0d381245
VP
13905 b->number, e.message);
13906 new_loc->enabled = 0;
13907 }
492d29ea 13908 END_CATCH
0d381245 13909 }
fe3f5fa8 13910
6c5b2ebe 13911 if (!sals_end.empty ())
f1310107 13912 {
6c5b2ebe 13913 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13914
6c5b2ebe 13915 new_loc->length = end - sals[0].pc + 1;
f1310107 13916 }
0d381245 13917 }
fe3f5fa8 13918
4a64f543
MS
13919 /* If possible, carry over 'disable' status from existing
13920 breakpoints. */
0d381245
VP
13921 {
13922 struct bp_location *e = existing_locations;
776592bf
DE
13923 /* If there are multiple breakpoints with the same function name,
13924 e.g. for inline functions, comparing function names won't work.
13925 Instead compare pc addresses; this is just a heuristic as things
13926 may have moved, but in practice it gives the correct answer
13927 often enough until a better solution is found. */
13928 int have_ambiguous_names = ambiguous_names_p (b->loc);
13929
0d381245
VP
13930 for (; e; e = e->next)
13931 {
13932 if (!e->enabled && e->function_name)
13933 {
13934 struct bp_location *l = b->loc;
776592bf
DE
13935 if (have_ambiguous_names)
13936 {
13937 for (; l; l = l->next)
f1310107 13938 if (breakpoint_locations_match (e, l))
776592bf
DE
13939 {
13940 l->enabled = 0;
13941 break;
13942 }
13943 }
13944 else
13945 {
13946 for (; l; l = l->next)
13947 if (l->function_name
13948 && strcmp (e->function_name, l->function_name) == 0)
13949 {
13950 l->enabled = 0;
13951 break;
13952 }
13953 }
0d381245
VP
13954 }
13955 }
13956 }
fe3f5fa8 13957
8d3788bd
VP
13958 if (!locations_are_equal (existing_locations, b->loc))
13959 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13960}
13961
f00aae0f 13962/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13963 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13964
6c5b2ebe 13965static std::vector<symtab_and_line>
f00aae0f 13966location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13967 struct program_space *search_pspace, int *found)
ef23e705 13968{
492d29ea 13969 struct gdb_exception exception = exception_none;
ef23e705 13970
983af33b 13971 gdb_assert (b->ops != NULL);
ef23e705 13972
6c5b2ebe
PA
13973 std::vector<symtab_and_line> sals;
13974
492d29ea 13975 TRY
ef23e705 13976 {
6c5b2ebe 13977 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13978 }
492d29ea 13979 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13980 {
13981 int not_found_and_ok = 0;
492d29ea
PA
13982
13983 exception = e;
13984
ef23e705
TJB
13985 /* For pending breakpoints, it's expected that parsing will
13986 fail until the right shared library is loaded. User has
13987 already told to create pending breakpoints and don't need
13988 extra messages. If breakpoint is in bp_shlib_disabled
13989 state, then user already saw the message about that
13990 breakpoint being disabled, and don't want to see more
13991 errors. */
58438ac1 13992 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13993 && (b->condition_not_parsed
13994 || (b->loc != NULL
13995 && search_pspace != NULL
13996 && b->loc->pspace != search_pspace)
ef23e705 13997 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13998 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13999 || b->enable_state == bp_disabled))
14000 not_found_and_ok = 1;
14001
14002 if (!not_found_and_ok)
14003 {
14004 /* We surely don't want to warn about the same breakpoint
14005 10 times. One solution, implemented here, is disable
14006 the breakpoint on error. Another solution would be to
14007 have separate 'warning emitted' flag. Since this
14008 happens only when a binary has changed, I don't know
14009 which approach is better. */
14010 b->enable_state = bp_disabled;
14011 throw_exception (e);
14012 }
14013 }
492d29ea 14014 END_CATCH
ef23e705 14015
492d29ea 14016 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14017 {
6c5b2ebe
PA
14018 for (auto &sal : sals)
14019 resolve_sal_pc (&sal);
f00aae0f 14020 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14021 {
ed1d1739
KS
14022 char *cond_string, *extra_string;
14023 int thread, task;
ef23e705 14024
6c5b2ebe 14025 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
14026 &cond_string, &thread, &task,
14027 &extra_string);
f00aae0f 14028 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14029 if (cond_string)
14030 b->cond_string = cond_string;
14031 b->thread = thread;
14032 b->task = task;
e7e0cddf 14033 if (extra_string)
f00aae0f
KS
14034 {
14035 xfree (b->extra_string);
14036 b->extra_string = extra_string;
14037 }
ef23e705
TJB
14038 b->condition_not_parsed = 0;
14039 }
14040
983af33b 14041 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 14042 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 14043
58438ac1
TT
14044 *found = 1;
14045 }
14046 else
14047 *found = 0;
ef23e705
TJB
14048
14049 return sals;
14050}
14051
348d480f
PA
14052/* The default re_set method, for typical hardware or software
14053 breakpoints. Reevaluate the breakpoint and recreate its
14054 locations. */
14055
14056static void
28010a5d 14057breakpoint_re_set_default (struct breakpoint *b)
ef23e705 14058{
c2f4122d 14059 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 14060 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 14061
6c5b2ebe
PA
14062 int found;
14063 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
14064 filter_pspace, &found);
ef23e705 14065 if (found)
6c5b2ebe 14066 expanded = std::move (sals);
ef23e705 14067
f00aae0f 14068 if (b->location_range_end != NULL)
f1310107 14069 {
6c5b2ebe
PA
14070 std::vector<symtab_and_line> sals_end
14071 = location_to_sals (b, b->location_range_end.get (),
14072 filter_pspace, &found);
f1310107 14073 if (found)
6c5b2ebe 14074 expanded_end = std::move (sals_end);
f1310107
TJB
14075 }
14076
c2f4122d 14077 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14078}
14079
983af33b
SDJ
14080/* Default method for creating SALs from an address string. It basically
14081 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14082
14083static void
f00aae0f
KS
14084create_sals_from_location_default (const struct event_location *location,
14085 struct linespec_result *canonical,
14086 enum bptype type_wanted)
983af33b 14087{
f00aae0f 14088 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14089}
14090
14091/* Call create_breakpoints_sal for the given arguments. This is the default
14092 function for the `create_breakpoints_sal' method of
14093 breakpoint_ops. */
14094
14095static void
14096create_breakpoints_sal_default (struct gdbarch *gdbarch,
14097 struct linespec_result *canonical,
e1e01040
PA
14098 gdb::unique_xmalloc_ptr<char> cond_string,
14099 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14100 enum bptype type_wanted,
14101 enum bpdisp disposition,
14102 int thread,
14103 int task, int ignore_count,
14104 const struct breakpoint_ops *ops,
14105 int from_tty, int enabled,
44f238bb 14106 int internal, unsigned flags)
983af33b 14107{
e1e01040
PA
14108 create_breakpoints_sal (gdbarch, canonical,
14109 std::move (cond_string),
14110 std::move (extra_string),
983af33b
SDJ
14111 type_wanted, disposition,
14112 thread, task, ignore_count, ops, from_tty,
44f238bb 14113 enabled, internal, flags);
983af33b
SDJ
14114}
14115
14116/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14117 default function for the `decode_location' method of breakpoint_ops. */
983af33b 14118
6c5b2ebe 14119static std::vector<symtab_and_line>
f00aae0f
KS
14120decode_location_default (struct breakpoint *b,
14121 const struct event_location *location,
6c5b2ebe 14122 struct program_space *search_pspace)
983af33b
SDJ
14123{
14124 struct linespec_result canonical;
14125
c2f4122d 14126 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14127 (struct symtab *) NULL, 0,
14128 &canonical, multiple_symbols_all,
14129 b->filter);
14130
14131 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 14132 gdb_assert (canonical.lsals.size () < 2);
983af33b 14133
6c5b2ebe 14134 if (!canonical.lsals.empty ())
983af33b 14135 {
6c5b2ebe
PA
14136 const linespec_sals &lsal = canonical.lsals[0];
14137 return std::move (lsal.sals);
983af33b 14138 }
6c5b2ebe 14139 return {};
983af33b
SDJ
14140}
14141
28010a5d
PA
14142/* Prepare the global context for a re-set of breakpoint B. */
14143
14144static struct cleanup *
14145prepare_re_set_context (struct breakpoint *b)
14146{
28010a5d 14147 input_radix = b->input_radix;
28010a5d
PA
14148 set_language (b->language);
14149
c2f4122d 14150 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14151}
14152
bf469271 14153/* Reset a breakpoint. */
c906108c 14154
bf469271
PA
14155static void
14156breakpoint_re_set_one (breakpoint *b)
c906108c 14157{
348d480f 14158 struct cleanup *cleanups;
c906108c 14159
348d480f
PA
14160 cleanups = prepare_re_set_context (b);
14161 b->ops->re_set (b);
14162 do_cleanups (cleanups);
c906108c
SS
14163}
14164
c2f4122d
PA
14165/* Re-set breakpoint locations for the current program space.
14166 Locations bound to other program spaces are left untouched. */
14167
c906108c 14168void
69de3c6a 14169breakpoint_re_set (void)
c906108c 14170{
35df4500 14171 struct breakpoint *b, *b_tmp;
c906108c
SS
14172 enum language save_language;
14173 int save_input_radix;
c5aa993b 14174
c906108c
SS
14175 save_language = current_language->la_language;
14176 save_input_radix = input_radix;
2a7f3dff 14177
c5aa993b 14178 {
5ed8105e 14179 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14180
5ed8105e
PA
14181 /* Note: we must not try to insert locations until after all
14182 breakpoints have been re-set. Otherwise, e.g., when re-setting
14183 breakpoint 1, we'd insert the locations of breakpoint 2, which
14184 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14185
5ed8105e
PA
14186 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14187 {
bf469271
PA
14188 TRY
14189 {
14190 breakpoint_re_set_one (b);
14191 }
14192 CATCH (ex, RETURN_MASK_ALL)
14193 {
14194 exception_fprintf (gdb_stderr, ex,
14195 "Error in re-setting breakpoint %d: ",
14196 b->number);
14197 }
14198 END_CATCH
5ed8105e
PA
14199 }
14200 set_language (save_language);
14201 input_radix = save_input_radix;
14202
14203 jit_breakpoint_re_set ();
14204 }
6c95b8df 14205
af02033e
PP
14206 create_overlay_event_breakpoint ();
14207 create_longjmp_master_breakpoint ();
14208 create_std_terminate_master_breakpoint ();
186c406b 14209 create_exception_master_breakpoint ();
2a7f3dff
PA
14210
14211 /* Now we can insert. */
14212 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14213}
14214\f
c906108c
SS
14215/* Reset the thread number of this breakpoint:
14216
14217 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14218 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14219void
fba45db2 14220breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14221{
14222 if (b->thread != -1)
14223 {
39f77062 14224 if (in_thread_list (inferior_ptid))
5d5658a1 14225 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14226
14227 /* We're being called after following a fork. The new fork is
14228 selected as current, and unless this was a vfork will have a
14229 different program space from the original thread. Reset that
14230 as well. */
14231 b->loc->pspace = current_program_space;
c906108c
SS
14232 }
14233}
14234
03ac34d5
MS
14235/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14236 If from_tty is nonzero, it prints a message to that effect,
14237 which ends with a period (no newline). */
14238
c906108c 14239void
fba45db2 14240set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14241{
52f0bd74 14242 struct breakpoint *b;
c906108c
SS
14243
14244 if (count < 0)
14245 count = 0;
14246
14247 ALL_BREAKPOINTS (b)
14248 if (b->number == bptnum)
c5aa993b 14249 {
d77f58be
SS
14250 if (is_tracepoint (b))
14251 {
14252 if (from_tty && count != 0)
14253 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14254 bptnum);
14255 return;
14256 }
14257
c5aa993b 14258 b->ignore_count = count;
221ea385
KS
14259 if (from_tty)
14260 {
14261 if (count == 0)
3e43a32a
MS
14262 printf_filtered (_("Will stop next time "
14263 "breakpoint %d is reached."),
221ea385
KS
14264 bptnum);
14265 else if (count == 1)
a3f17187 14266 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14267 bptnum);
14268 else
3e43a32a
MS
14269 printf_filtered (_("Will ignore next %d "
14270 "crossings of breakpoint %d."),
221ea385
KS
14271 count, bptnum);
14272 }
8d3788bd 14273 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14274 return;
14275 }
c906108c 14276
8a3fe4f8 14277 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14278}
14279
c906108c
SS
14280/* Command to set ignore-count of breakpoint N to COUNT. */
14281
14282static void
fba45db2 14283ignore_command (char *args, int from_tty)
c906108c
SS
14284{
14285 char *p = args;
52f0bd74 14286 int num;
c906108c
SS
14287
14288 if (p == 0)
e2e0b3e5 14289 error_no_arg (_("a breakpoint number"));
c5aa993b 14290
c906108c 14291 num = get_number (&p);
5c44784c 14292 if (num == 0)
8a3fe4f8 14293 error (_("bad breakpoint number: '%s'"), args);
c906108c 14294 if (*p == 0)
8a3fe4f8 14295 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14296
14297 set_ignore_count (num,
14298 longest_to_int (value_as_long (parse_and_eval (p))),
14299 from_tty);
221ea385
KS
14300 if (from_tty)
14301 printf_filtered ("\n");
c906108c
SS
14302}
14303\f
14304/* Call FUNCTION on each of the breakpoints
14305 whose numbers are given in ARGS. */
14306
14307static void
896b6bda 14308map_breakpoint_numbers (const char *args,
48649e1b 14309 gdb::function_view<void (breakpoint *)> function)
c906108c 14310{
52f0bd74
AC
14311 int num;
14312 struct breakpoint *b, *tmp;
c906108c 14313
b9d61307 14314 if (args == 0 || *args == '\0')
e2e0b3e5 14315 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14316
bfd28288 14317 number_or_range_parser parser (args);
197f0a60 14318
bfd28288 14319 while (!parser.finished ())
c906108c 14320 {
bfd28288
PA
14321 const char *p = parser.cur_tok ();
14322 bool match = false;
197f0a60 14323
bfd28288 14324 num = parser.get_number ();
5c44784c 14325 if (num == 0)
c5aa993b 14326 {
8a3fe4f8 14327 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14328 }
14329 else
14330 {
14331 ALL_BREAKPOINTS_SAFE (b, tmp)
14332 if (b->number == num)
14333 {
bfd28288 14334 match = true;
48649e1b 14335 function (b);
11cf8741 14336 break;
5c44784c 14337 }
bfd28288 14338 if (!match)
a3f17187 14339 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14340 }
c906108c
SS
14341 }
14342}
14343
0d381245 14344static struct bp_location *
cb791d59 14345find_location_by_number (const char *number)
0d381245 14346{
cb791d59 14347 const char *p1;
0d381245
VP
14348 int bp_num;
14349 int loc_num;
14350 struct breakpoint *b;
14351 struct bp_location *loc;
14352
0d381245 14353 p1 = number;
cb791d59
TT
14354 bp_num = get_number_trailer (&p1, '.');
14355 if (bp_num == 0 || p1[0] != '.')
0d381245
VP
14356 error (_("Bad breakpoint number '%s'"), number);
14357
14358 ALL_BREAKPOINTS (b)
14359 if (b->number == bp_num)
14360 {
14361 break;
14362 }
14363
14364 if (!b || b->number != bp_num)
14365 error (_("Bad breakpoint number '%s'"), number);
14366
cb791d59
TT
14367 /* Skip the dot. */
14368 ++p1;
14369 const char *save = p1;
197f0a60 14370 loc_num = get_number (&p1);
0d381245
VP
14371 if (loc_num == 0)
14372 error (_("Bad breakpoint location number '%s'"), number);
14373
14374 --loc_num;
14375 loc = b->loc;
14376 for (;loc_num && loc; --loc_num, loc = loc->next)
14377 ;
14378 if (!loc)
cb791d59 14379 error (_("Bad breakpoint location number '%s'"), save);
0d381245
VP
14380
14381 return loc;
14382}
14383
14384
1900040c
MS
14385/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14386 If from_tty is nonzero, it prints a message to that effect,
14387 which ends with a period (no newline). */
14388
c906108c 14389void
fba45db2 14390disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14391{
14392 /* Never disable a watchpoint scope breakpoint; we want to
14393 hit them when we leave scope so we can delete both the
14394 watchpoint and its scope breakpoint at that time. */
14395 if (bpt->type == bp_watchpoint_scope)
14396 return;
14397
b5de0fa7 14398 bpt->enable_state = bp_disabled;
c906108c 14399
b775012e
LM
14400 /* Mark breakpoint locations modified. */
14401 mark_breakpoint_modified (bpt);
14402
d248b706
KY
14403 if (target_supports_enable_disable_tracepoint ()
14404 && current_trace_status ()->running && is_tracepoint (bpt))
14405 {
14406 struct bp_location *location;
14407
14408 for (location = bpt->loc; location; location = location->next)
14409 target_disable_tracepoint (location);
14410 }
14411
44702360 14412 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14413
8d3788bd 14414 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14415}
14416
c906108c 14417static void
fba45db2 14418disable_command (char *args, int from_tty)
c906108c 14419{
c906108c 14420 if (args == 0)
46c6471b
PA
14421 {
14422 struct breakpoint *bpt;
14423
14424 ALL_BREAKPOINTS (bpt)
14425 if (user_breakpoint_p (bpt))
14426 disable_breakpoint (bpt);
14427 }
9eaabc75 14428 else
0d381245 14429 {
cb791d59 14430 std::string num = extract_arg (&args);
9eaabc75 14431
cb791d59 14432 while (!num.empty ())
d248b706 14433 {
cb791d59 14434 if (num.find ('.') != std::string::npos)
b775012e 14435 {
cb791d59 14436 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14437
14438 if (loc)
14439 {
14440 if (loc->enabled)
14441 {
14442 loc->enabled = 0;
14443 mark_breakpoint_location_modified (loc);
14444 }
14445 if (target_supports_enable_disable_tracepoint ()
14446 && current_trace_status ()->running && loc->owner
14447 && is_tracepoint (loc->owner))
14448 target_disable_tracepoint (loc);
14449 }
44702360 14450 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14451 }
9eaabc75 14452 else
48649e1b
TT
14453 map_breakpoint_numbers
14454 (num.c_str (), [&] (breakpoint *b)
14455 {
14456 iterate_over_related_breakpoints (b, disable_breakpoint);
14457 });
9eaabc75 14458 num = extract_arg (&args);
d248b706 14459 }
0d381245 14460 }
c906108c
SS
14461}
14462
14463static void
816338b5
SS
14464enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14465 int count)
c906108c 14466{
afe38095 14467 int target_resources_ok;
c906108c
SS
14468
14469 if (bpt->type == bp_hardware_breakpoint)
14470 {
14471 int i;
c5aa993b 14472 i = hw_breakpoint_used_count ();
53a5351d 14473 target_resources_ok =
d92524f1 14474 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14475 i + 1, 0);
c906108c 14476 if (target_resources_ok == 0)
8a3fe4f8 14477 error (_("No hardware breakpoint support in the target."));
c906108c 14478 else if (target_resources_ok < 0)
8a3fe4f8 14479 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14480 }
14481
cc60f2e3 14482 if (is_watchpoint (bpt))
c906108c 14483 {
d07205c2 14484 /* Initialize it just to avoid a GCC false warning. */
f486487f 14485 enum enable_state orig_enable_state = bp_disabled;
dde02812 14486
492d29ea 14487 TRY
c906108c 14488 {
3a5c3e22
PA
14489 struct watchpoint *w = (struct watchpoint *) bpt;
14490
1e718ff1
TJB
14491 orig_enable_state = bpt->enable_state;
14492 bpt->enable_state = bp_enabled;
3a5c3e22 14493 update_watchpoint (w, 1 /* reparse */);
c906108c 14494 }
492d29ea 14495 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14496 {
1e718ff1 14497 bpt->enable_state = orig_enable_state;
dde02812
ES
14498 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14499 bpt->number);
14500 return;
c5aa993b 14501 }
492d29ea 14502 END_CATCH
c906108c 14503 }
0101ce28 14504
b775012e
LM
14505 bpt->enable_state = bp_enabled;
14506
14507 /* Mark breakpoint locations modified. */
14508 mark_breakpoint_modified (bpt);
14509
d248b706
KY
14510 if (target_supports_enable_disable_tracepoint ()
14511 && current_trace_status ()->running && is_tracepoint (bpt))
14512 {
14513 struct bp_location *location;
14514
14515 for (location = bpt->loc; location; location = location->next)
14516 target_enable_tracepoint (location);
14517 }
14518
b4c291bb 14519 bpt->disposition = disposition;
816338b5 14520 bpt->enable_count = count;
44702360 14521 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14522
8d3788bd 14523 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14524}
14525
fe3f5fa8 14526
c906108c 14527void
fba45db2 14528enable_breakpoint (struct breakpoint *bpt)
c906108c 14529{
816338b5 14530 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14531}
14532
c906108c
SS
14533/* The enable command enables the specified breakpoints (or all defined
14534 breakpoints) so they once again become (or continue to be) effective
1272ad14 14535 in stopping the inferior. */
c906108c 14536
c906108c 14537static void
fba45db2 14538enable_command (char *args, int from_tty)
c906108c 14539{
c906108c 14540 if (args == 0)
46c6471b
PA
14541 {
14542 struct breakpoint *bpt;
14543
14544 ALL_BREAKPOINTS (bpt)
14545 if (user_breakpoint_p (bpt))
14546 enable_breakpoint (bpt);
14547 }
9eaabc75 14548 else
0d381245 14549 {
cb791d59 14550 std::string num = extract_arg (&args);
9eaabc75 14551
cb791d59 14552 while (!num.empty ())
d248b706 14553 {
cb791d59 14554 if (num.find ('.') != std::string::npos)
b775012e 14555 {
cb791d59 14556 struct bp_location *loc = find_location_by_number (num.c_str ());
9eaabc75
MW
14557
14558 if (loc)
14559 {
14560 if (!loc->enabled)
14561 {
14562 loc->enabled = 1;
14563 mark_breakpoint_location_modified (loc);
14564 }
14565 if (target_supports_enable_disable_tracepoint ()
14566 && current_trace_status ()->running && loc->owner
14567 && is_tracepoint (loc->owner))
14568 target_enable_tracepoint (loc);
14569 }
44702360 14570 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14571 }
9eaabc75 14572 else
48649e1b
TT
14573 map_breakpoint_numbers
14574 (num.c_str (), [&] (breakpoint *b)
14575 {
14576 iterate_over_related_breakpoints (b, enable_breakpoint);
14577 });
9eaabc75 14578 num = extract_arg (&args);
d248b706 14579 }
0d381245 14580 }
c906108c
SS
14581}
14582
c906108c 14583static void
4495129a 14584enable_once_command (const char *args, int from_tty)
c906108c 14585{
48649e1b
TT
14586 map_breakpoint_numbers
14587 (args, [&] (breakpoint *b)
14588 {
14589 iterate_over_related_breakpoints
14590 (b, [&] (breakpoint *bpt)
14591 {
14592 enable_breakpoint_disp (bpt, disp_disable, 1);
14593 });
14594 });
816338b5
SS
14595}
14596
14597static void
4495129a 14598enable_count_command (const char *args, int from_tty)
816338b5 14599{
b9d61307
SM
14600 int count;
14601
14602 if (args == NULL)
14603 error_no_arg (_("hit count"));
14604
14605 count = get_number (&args);
816338b5 14606
48649e1b
TT
14607 map_breakpoint_numbers
14608 (args, [&] (breakpoint *b)
14609 {
14610 iterate_over_related_breakpoints
14611 (b, [&] (breakpoint *bpt)
14612 {
14613 enable_breakpoint_disp (bpt, disp_disable, count);
14614 });
14615 });
c906108c
SS
14616}
14617
c906108c 14618static void
4495129a 14619enable_delete_command (const char *args, int from_tty)
c906108c 14620{
48649e1b
TT
14621 map_breakpoint_numbers
14622 (args, [&] (breakpoint *b)
14623 {
14624 iterate_over_related_breakpoints
14625 (b, [&] (breakpoint *bpt)
14626 {
14627 enable_breakpoint_disp (bpt, disp_del, 1);
14628 });
14629 });
c906108c
SS
14630}
14631\f
fa8d40ab
JJ
14632static void
14633set_breakpoint_cmd (char *args, int from_tty)
14634{
14635}
14636
14637static void
14638show_breakpoint_cmd (char *args, int from_tty)
14639{
14640}
14641
1f3b5d1b
PP
14642/* Invalidate last known value of any hardware watchpoint if
14643 the memory which that value represents has been written to by
14644 GDB itself. */
14645
14646static void
8de0566d
YQ
14647invalidate_bp_value_on_memory_change (struct inferior *inferior,
14648 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14649 const bfd_byte *data)
14650{
14651 struct breakpoint *bp;
14652
14653 ALL_BREAKPOINTS (bp)
14654 if (bp->enable_state == bp_enabled
3a5c3e22 14655 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14656 {
3a5c3e22 14657 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14658
3a5c3e22
PA
14659 if (wp->val_valid && wp->val)
14660 {
14661 struct bp_location *loc;
14662
14663 for (loc = bp->loc; loc != NULL; loc = loc->next)
14664 if (loc->loc_type == bp_loc_hardware_watchpoint
14665 && loc->address + loc->length > addr
14666 && addr + len > loc->address)
14667 {
14668 value_free (wp->val);
14669 wp->val = NULL;
14670 wp->val_valid = 0;
14671 }
14672 }
1f3b5d1b
PP
14673 }
14674}
14675
8181d85f
DJ
14676/* Create and insert a breakpoint for software single step. */
14677
14678void
6c95b8df 14679insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14680 struct address_space *aspace,
14681 CORE_ADDR next_pc)
8181d85f 14682{
7c16b83e
PA
14683 struct thread_info *tp = inferior_thread ();
14684 struct symtab_and_line sal;
14685 CORE_ADDR pc = next_pc;
8181d85f 14686
34b7e8a6
PA
14687 if (tp->control.single_step_breakpoints == NULL)
14688 {
14689 tp->control.single_step_breakpoints
5d5658a1 14690 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14691 }
8181d85f 14692
7c16b83e
PA
14693 sal = find_pc_line (pc, 0);
14694 sal.pc = pc;
14695 sal.section = find_pc_overlay (pc);
14696 sal.explicit_pc = 1;
34b7e8a6 14697 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14698
7c16b83e 14699 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14700}
14701
93f9a11f
YQ
14702/* Insert single step breakpoints according to the current state. */
14703
14704int
14705insert_single_step_breakpoints (struct gdbarch *gdbarch)
14706{
f5ea389a 14707 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14708 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14709
f5ea389a 14710 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14711
a0ff9e1a 14712 if (!next_pcs.empty ())
93f9a11f 14713 {
f5ea389a 14714 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14715 struct address_space *aspace = get_frame_address_space (frame);
14716
a0ff9e1a 14717 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14718 insert_single_step_breakpoint (gdbarch, aspace, pc);
14719
93f9a11f
YQ
14720 return 1;
14721 }
14722 else
14723 return 0;
14724}
14725
34b7e8a6 14726/* See breakpoint.h. */
f02253f1
HZ
14727
14728int
7c16b83e
PA
14729breakpoint_has_location_inserted_here (struct breakpoint *bp,
14730 struct address_space *aspace,
14731 CORE_ADDR pc)
1aafd4da 14732{
7c16b83e 14733 struct bp_location *loc;
1aafd4da 14734
7c16b83e
PA
14735 for (loc = bp->loc; loc != NULL; loc = loc->next)
14736 if (loc->inserted
14737 && breakpoint_location_address_match (loc, aspace, pc))
14738 return 1;
1aafd4da 14739
7c16b83e 14740 return 0;
ef370185
JB
14741}
14742
14743/* Check whether a software single-step breakpoint is inserted at
14744 PC. */
14745
14746int
14747single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14748 CORE_ADDR pc)
14749{
34b7e8a6
PA
14750 struct breakpoint *bpt;
14751
14752 ALL_BREAKPOINTS (bpt)
14753 {
14754 if (bpt->type == bp_single_step
14755 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14756 return 1;
14757 }
14758 return 0;
1aafd4da
UW
14759}
14760
1042e4c0
SS
14761/* Tracepoint-specific operations. */
14762
14763/* Set tracepoint count to NUM. */
14764static void
14765set_tracepoint_count (int num)
14766{
14767 tracepoint_count = num;
4fa62494 14768 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14769}
14770
70221824 14771static void
f2fc3015 14772trace_command (char *arg_in, int from_tty)
1042e4c0 14773{
f2fc3015 14774 const char *arg = arg_in;
55aa24fb 14775 struct breakpoint_ops *ops;
55aa24fb 14776
ffc2605c
TT
14777 event_location_up location = string_to_event_location (&arg,
14778 current_language);
5b56227b 14779 if (location != NULL
ffc2605c 14780 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14781 ops = &tracepoint_probe_breakpoint_ops;
14782 else
14783 ops = &tracepoint_breakpoint_ops;
14784
558a9d82 14785 create_breakpoint (get_current_arch (),
ffc2605c 14786 location.get (),
f00aae0f 14787 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14788 0 /* tempflag */,
14789 bp_tracepoint /* type_wanted */,
14790 0 /* Ignore count */,
14791 pending_break_support,
14792 ops,
14793 from_tty,
14794 1 /* enabled */,
14795 0 /* internal */, 0);
1042e4c0
SS
14796}
14797
70221824 14798static void
f2fc3015 14799ftrace_command (char *arg_in, int from_tty)
7a697b8d 14800{
f2fc3015 14801 const char *arg = arg_in;
ffc2605c
TT
14802 event_location_up location = string_to_event_location (&arg,
14803 current_language);
558a9d82 14804 create_breakpoint (get_current_arch (),
ffc2605c 14805 location.get (),
f00aae0f 14806 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14807 0 /* tempflag */,
14808 bp_fast_tracepoint /* type_wanted */,
14809 0 /* Ignore count */,
14810 pending_break_support,
14811 &tracepoint_breakpoint_ops,
14812 from_tty,
14813 1 /* enabled */,
14814 0 /* internal */, 0);
0fb4aa4b
PA
14815}
14816
14817/* strace command implementation. Creates a static tracepoint. */
14818
70221824 14819static void
f2fc3015 14820strace_command (char *arg_in, int from_tty)
0fb4aa4b 14821{
f2fc3015 14822 const char *arg = arg_in;
983af33b 14823 struct breakpoint_ops *ops;
ffc2605c 14824 event_location_up location;
f00aae0f 14825 struct cleanup *back_to;
983af33b
SDJ
14826
14827 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14828 or with a normal static tracepoint. */
61012eef 14829 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14830 {
14831 ops = &strace_marker_breakpoint_ops;
14832 location = new_linespec_location (&arg);
14833 }
983af33b 14834 else
f00aae0f
KS
14835 {
14836 ops = &tracepoint_breakpoint_ops;
14837 location = string_to_event_location (&arg, current_language);
14838 }
983af33b 14839
558a9d82 14840 create_breakpoint (get_current_arch (),
ffc2605c 14841 location.get (),
f00aae0f 14842 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14843 0 /* tempflag */,
14844 bp_static_tracepoint /* type_wanted */,
14845 0 /* Ignore count */,
14846 pending_break_support,
14847 ops,
14848 from_tty,
14849 1 /* enabled */,
14850 0 /* internal */, 0);
7a697b8d
SS
14851}
14852
409873ef
SS
14853/* Set up a fake reader function that gets command lines from a linked
14854 list that was acquired during tracepoint uploading. */
14855
14856static struct uploaded_tp *this_utp;
3149d8c1 14857static int next_cmd;
409873ef
SS
14858
14859static char *
14860read_uploaded_action (void)
14861{
14862 char *rslt;
14863
3149d8c1 14864 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14865
3149d8c1 14866 next_cmd++;
409873ef
SS
14867
14868 return rslt;
14869}
14870
00bf0b85
SS
14871/* Given information about a tracepoint as recorded on a target (which
14872 can be either a live system or a trace file), attempt to create an
14873 equivalent GDB tracepoint. This is not a reliable process, since
14874 the target does not necessarily have all the information used when
14875 the tracepoint was originally defined. */
14876
d9b3f62e 14877struct tracepoint *
00bf0b85 14878create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14879{
f2fc3015
TT
14880 const char *addr_str;
14881 char small_buf[100];
d9b3f62e 14882 struct tracepoint *tp;
fd9b8c24 14883
409873ef
SS
14884 if (utp->at_string)
14885 addr_str = utp->at_string;
14886 else
14887 {
14888 /* In the absence of a source location, fall back to raw
14889 address. Since there is no way to confirm that the address
14890 means the same thing as when the trace was started, warn the
14891 user. */
3e43a32a
MS
14892 warning (_("Uploaded tracepoint %d has no "
14893 "source location, using raw address"),
409873ef 14894 utp->number);
8c042590 14895 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14896 addr_str = small_buf;
14897 }
14898
14899 /* There's not much we can do with a sequence of bytecodes. */
14900 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14901 warning (_("Uploaded tracepoint %d condition "
14902 "has no source form, ignoring it"),
409873ef 14903 utp->number);
d5551862 14904
ffc2605c
TT
14905 event_location_up location = string_to_event_location (&addr_str,
14906 current_language);
8cdf0e15 14907 if (!create_breakpoint (get_current_arch (),
ffc2605c 14908 location.get (),
f00aae0f 14909 utp->cond_string, -1, addr_str,
e7e0cddf 14910 0 /* parse cond/thread */,
8cdf0e15 14911 0 /* tempflag */,
0fb4aa4b 14912 utp->type /* type_wanted */,
8cdf0e15
VP
14913 0 /* Ignore count */,
14914 pending_break_support,
348d480f 14915 &tracepoint_breakpoint_ops,
8cdf0e15 14916 0 /* from_tty */,
84f4c1fe 14917 utp->enabled /* enabled */,
44f238bb
PA
14918 0 /* internal */,
14919 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14920 return NULL;
fd9b8c24 14921
409873ef 14922 /* Get the tracepoint we just created. */
fd9b8c24
PA
14923 tp = get_tracepoint (tracepoint_count);
14924 gdb_assert (tp != NULL);
d5551862 14925
00bf0b85
SS
14926 if (utp->pass > 0)
14927 {
8c042590 14928 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14929 tp->number);
00bf0b85 14930
409873ef 14931 trace_pass_command (small_buf, 0);
00bf0b85
SS
14932 }
14933
409873ef
SS
14934 /* If we have uploaded versions of the original commands, set up a
14935 special-purpose "reader" function and call the usual command line
14936 reader, then pass the result to the breakpoint command-setting
14937 function. */
3149d8c1 14938 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14939 {
93921405 14940 command_line_up cmd_list;
00bf0b85 14941
409873ef 14942 this_utp = utp;
3149d8c1 14943 next_cmd = 0;
d5551862 14944
409873ef
SS
14945 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14946
c1fc2657 14947 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14948 }
3149d8c1
SS
14949 else if (!VEC_empty (char_ptr, utp->actions)
14950 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14951 warning (_("Uploaded tracepoint %d actions "
14952 "have no source form, ignoring them"),
409873ef 14953 utp->number);
00bf0b85 14954
f196051f 14955 /* Copy any status information that might be available. */
c1fc2657 14956 tp->hit_count = utp->hit_count;
f196051f
SS
14957 tp->traceframe_usage = utp->traceframe_usage;
14958
00bf0b85 14959 return tp;
d9b3f62e 14960}
00bf0b85 14961
1042e4c0
SS
14962/* Print information on tracepoint number TPNUM_EXP, or all if
14963 omitted. */
14964
14965static void
11db9430 14966info_tracepoints_command (char *args, int from_tty)
1042e4c0 14967{
79a45e25 14968 struct ui_out *uiout = current_uiout;
e5a67952 14969 int num_printed;
1042e4c0 14970
e5a67952 14971 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14972
14973 if (num_printed == 0)
1042e4c0 14974 {
e5a67952 14975 if (args == NULL || *args == '\0')
112e8700 14976 uiout->message ("No tracepoints.\n");
d77f58be 14977 else
112e8700 14978 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14979 }
ad443146
SS
14980
14981 default_collect_info ();
1042e4c0
SS
14982}
14983
4a64f543 14984/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14985 Not supported by all targets. */
14986static void
14987enable_trace_command (char *args, int from_tty)
14988{
14989 enable_command (args, from_tty);
14990}
14991
4a64f543 14992/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14993 Not supported by all targets. */
14994static void
14995disable_trace_command (char *args, int from_tty)
14996{
14997 disable_command (args, from_tty);
14998}
14999
4a64f543 15000/* Remove a tracepoint (or all if no argument). */
1042e4c0 15001static void
4495129a 15002delete_trace_command (const char *arg, int from_tty)
1042e4c0 15003{
35df4500 15004 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15005
15006 dont_repeat ();
15007
15008 if (arg == 0)
15009 {
15010 int breaks_to_delete = 0;
15011
15012 /* Delete all breakpoints if no argument.
15013 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15014 have to be deleted with an explicit breakpoint number
15015 argument. */
1042e4c0 15016 ALL_TRACEPOINTS (b)
46c6471b 15017 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15018 {
15019 breaks_to_delete = 1;
15020 break;
15021 }
1042e4c0
SS
15022
15023 /* Ask user only if there are some breakpoints to delete. */
15024 if (!from_tty
15025 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15026 {
35df4500 15027 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15028 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15029 delete_breakpoint (b);
1042e4c0
SS
15030 }
15031 }
15032 else
48649e1b
TT
15033 map_breakpoint_numbers
15034 (arg, [&] (breakpoint *b)
15035 {
15036 iterate_over_related_breakpoints (b, delete_breakpoint);
15037 });
1042e4c0
SS
15038}
15039
197f0a60
TT
15040/* Helper function for trace_pass_command. */
15041
15042static void
d9b3f62e 15043trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15044{
d9b3f62e 15045 tp->pass_count = count;
c1fc2657 15046 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15047 if (from_tty)
15048 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15049 tp->number, count);
197f0a60
TT
15050}
15051
1042e4c0
SS
15052/* Set passcount for tracepoint.
15053
15054 First command argument is passcount, second is tracepoint number.
15055 If tracepoint number omitted, apply to most recently defined.
15056 Also accepts special argument "all". */
15057
15058static void
15059trace_pass_command (char *args, int from_tty)
15060{
d9b3f62e 15061 struct tracepoint *t1;
1042e4c0 15062 unsigned int count;
1042e4c0
SS
15063
15064 if (args == 0 || *args == 0)
3e43a32a
MS
15065 error (_("passcount command requires an "
15066 "argument (count + optional TP num)"));
1042e4c0 15067
4a64f543 15068 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15069
529480d0 15070 args = skip_spaces (args);
1042e4c0
SS
15071 if (*args && strncasecmp (args, "all", 3) == 0)
15072 {
d9b3f62e
PA
15073 struct breakpoint *b;
15074
1042e4c0 15075 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15076 if (*args)
15077 error (_("Junk at end of arguments."));
1042e4c0 15078
d9b3f62e 15079 ALL_TRACEPOINTS (b)
197f0a60 15080 {
d9b3f62e 15081 t1 = (struct tracepoint *) b;
197f0a60
TT
15082 trace_pass_set_count (t1, count, from_tty);
15083 }
15084 }
15085 else if (*args == '\0')
1042e4c0 15086 {
5fa1d40e 15087 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15088 if (t1)
197f0a60
TT
15089 trace_pass_set_count (t1, count, from_tty);
15090 }
15091 else
15092 {
bfd28288
PA
15093 number_or_range_parser parser (args);
15094 while (!parser.finished ())
1042e4c0 15095 {
bfd28288 15096 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15097 if (t1)
15098 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15099 }
15100 }
1042e4c0
SS
15101}
15102
d9b3f62e 15103struct tracepoint *
1042e4c0
SS
15104get_tracepoint (int num)
15105{
15106 struct breakpoint *t;
15107
15108 ALL_TRACEPOINTS (t)
15109 if (t->number == num)
d9b3f62e 15110 return (struct tracepoint *) t;
1042e4c0
SS
15111
15112 return NULL;
15113}
15114
d5551862
SS
15115/* Find the tracepoint with the given target-side number (which may be
15116 different from the tracepoint number after disconnecting and
15117 reconnecting). */
15118
d9b3f62e 15119struct tracepoint *
d5551862
SS
15120get_tracepoint_by_number_on_target (int num)
15121{
d9b3f62e 15122 struct breakpoint *b;
d5551862 15123
d9b3f62e
PA
15124 ALL_TRACEPOINTS (b)
15125 {
15126 struct tracepoint *t = (struct tracepoint *) b;
15127
15128 if (t->number_on_target == num)
15129 return t;
15130 }
d5551862
SS
15131
15132 return NULL;
15133}
15134
1042e4c0 15135/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15136 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15137 If the argument is missing, the most recent tracepoint
15138 (tracepoint_count) is returned. */
15139
d9b3f62e 15140struct tracepoint *
197f0a60 15141get_tracepoint_by_number (char **arg,
bfd28288 15142 number_or_range_parser *parser)
1042e4c0 15143{
1042e4c0
SS
15144 struct breakpoint *t;
15145 int tpnum;
15146 char *instring = arg == NULL ? NULL : *arg;
15147
bfd28288 15148 if (parser != NULL)
197f0a60 15149 {
bfd28288
PA
15150 gdb_assert (!parser->finished ());
15151 tpnum = parser->get_number ();
197f0a60
TT
15152 }
15153 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15154 tpnum = tracepoint_count;
1042e4c0 15155 else
197f0a60 15156 tpnum = get_number (arg);
1042e4c0
SS
15157
15158 if (tpnum <= 0)
15159 {
15160 if (instring && *instring)
15161 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15162 instring);
15163 else
5fa1d40e 15164 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15165 return NULL;
15166 }
15167
15168 ALL_TRACEPOINTS (t)
15169 if (t->number == tpnum)
15170 {
d9b3f62e 15171 return (struct tracepoint *) t;
1042e4c0
SS
15172 }
15173
1042e4c0
SS
15174 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15175 return NULL;
15176}
15177
d9b3f62e
PA
15178void
15179print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15180{
15181 if (b->thread != -1)
15182 fprintf_unfiltered (fp, " thread %d", b->thread);
15183
15184 if (b->task != 0)
15185 fprintf_unfiltered (fp, " task %d", b->task);
15186
15187 fprintf_unfiltered (fp, "\n");
15188}
15189
6149aea9
PA
15190/* Save information on user settable breakpoints (watchpoints, etc) to
15191 a new script file named FILENAME. If FILTER is non-NULL, call it
15192 on each breakpoint and only include the ones for which it returns
15193 non-zero. */
15194
1042e4c0 15195static void
4495129a 15196save_breakpoints (const char *filename, int from_tty,
6149aea9 15197 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15198{
15199 struct breakpoint *tp;
6149aea9 15200 int any = 0;
6149aea9 15201 int extra_trace_bits = 0;
1042e4c0 15202
6149aea9
PA
15203 if (filename == 0 || *filename == 0)
15204 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15205
15206 /* See if we have anything to save. */
6149aea9 15207 ALL_BREAKPOINTS (tp)
1042e4c0 15208 {
6149aea9 15209 /* Skip internal and momentary breakpoints. */
09d682a4 15210 if (!user_breakpoint_p (tp))
6149aea9
PA
15211 continue;
15212
15213 /* If we have a filter, only save the breakpoints it accepts. */
15214 if (filter && !filter (tp))
15215 continue;
15216
15217 any = 1;
15218
15219 if (is_tracepoint (tp))
15220 {
15221 extra_trace_bits = 1;
15222
15223 /* We can stop searching. */
15224 break;
15225 }
1042e4c0 15226 }
6149aea9
PA
15227
15228 if (!any)
1042e4c0 15229 {
6149aea9 15230 warning (_("Nothing to save."));
1042e4c0
SS
15231 return;
15232 }
15233
ee0c3293 15234 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15235
15236 stdio_file fp;
15237
ee0c3293 15238 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15239 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15240 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15241
6149aea9 15242 if (extra_trace_bits)
d7e74731 15243 save_trace_state_variables (&fp);
8bf6485c 15244
6149aea9 15245 ALL_BREAKPOINTS (tp)
1042e4c0 15246 {
6149aea9 15247 /* Skip internal and momentary breakpoints. */
09d682a4 15248 if (!user_breakpoint_p (tp))
6149aea9 15249 continue;
8bf6485c 15250
6149aea9
PA
15251 /* If we have a filter, only save the breakpoints it accepts. */
15252 if (filter && !filter (tp))
15253 continue;
15254
d7e74731 15255 tp->ops->print_recreate (tp, &fp);
1042e4c0 15256
6149aea9
PA
15257 /* Note, we can't rely on tp->number for anything, as we can't
15258 assume the recreated breakpoint numbers will match. Use $bpnum
15259 instead. */
15260
15261 if (tp->cond_string)
d7e74731 15262 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15263
15264 if (tp->ignore_count)
d7e74731 15265 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15266
2d9442cc 15267 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15268 {
d7e74731 15269 fp.puts (" commands\n");
a7bdde9e 15270
d7e74731 15271 current_uiout->redirect (&fp);
492d29ea 15272 TRY
1042e4c0 15273 {
d1b0a7bf 15274 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15275 }
492d29ea
PA
15276 CATCH (ex, RETURN_MASK_ALL)
15277 {
112e8700 15278 current_uiout->redirect (NULL);
492d29ea
PA
15279 throw_exception (ex);
15280 }
15281 END_CATCH
1042e4c0 15282
112e8700 15283 current_uiout->redirect (NULL);
d7e74731 15284 fp.puts (" end\n");
1042e4c0 15285 }
6149aea9
PA
15286
15287 if (tp->enable_state == bp_disabled)
d7e74731 15288 fp.puts ("disable $bpnum\n");
6149aea9
PA
15289
15290 /* If this is a multi-location breakpoint, check if the locations
15291 should be individually disabled. Watchpoint locations are
15292 special, and not user visible. */
15293 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15294 {
15295 struct bp_location *loc;
15296 int n = 1;
15297
15298 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15299 if (!loc->enabled)
d7e74731 15300 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15301 }
1042e4c0 15302 }
8bf6485c 15303
6149aea9 15304 if (extra_trace_bits && *default_collect)
d7e74731 15305 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15306
1042e4c0 15307 if (from_tty)
ee0c3293 15308 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15309}
15310
15311/* The `save breakpoints' command. */
15312
15313static void
4495129a 15314save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15315{
15316 save_breakpoints (args, from_tty, NULL);
15317}
15318
15319/* The `save tracepoints' command. */
15320
15321static void
4495129a 15322save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15323{
15324 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15325}
15326
15327/* Create a vector of all tracepoints. */
15328
15329VEC(breakpoint_p) *
eeae04df 15330all_tracepoints (void)
1042e4c0
SS
15331{
15332 VEC(breakpoint_p) *tp_vec = 0;
15333 struct breakpoint *tp;
15334
15335 ALL_TRACEPOINTS (tp)
15336 {
15337 VEC_safe_push (breakpoint_p, tp_vec, tp);
15338 }
15339
15340 return tp_vec;
15341}
15342
c906108c 15343\f
629500fa
KS
15344/* This help string is used to consolidate all the help string for specifying
15345 locations used by several commands. */
15346
15347#define LOCATION_HELP_STRING \
15348"Linespecs are colon-separated lists of location parameters, such as\n\
15349source filename, function name, label name, and line number.\n\
15350Example: To specify the start of a label named \"the_top\" in the\n\
15351function \"fact\" in the file \"factorial.c\", use\n\
15352\"factorial.c:fact:the_top\".\n\
15353\n\
15354Address locations begin with \"*\" and specify an exact address in the\n\
15355program. Example: To specify the fourth byte past the start function\n\
15356\"main\", use \"*main + 4\".\n\
15357\n\
15358Explicit locations are similar to linespecs but use an option/argument\n\
15359syntax to specify location parameters.\n\
15360Example: To specify the start of the label named \"the_top\" in the\n\
15361function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15362-function fact -label the_top\".\n"
15363
4a64f543
MS
15364/* This help string is used for the break, hbreak, tbreak and thbreak
15365 commands. It is defined as a macro to prevent duplication.
15366 COMMAND should be a string constant containing the name of the
15367 command. */
629500fa 15368
31e2b00f 15369#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15370command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15371PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15372probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15373guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15374`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15375LOCATION may be a linespec, address, or explicit location as described\n\
15376below.\n\
15377\n\
dc10affe
PA
15378With no LOCATION, uses current execution address of the selected\n\
15379stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15380\n\
15381THREADNUM is the number from \"info threads\".\n\
15382CONDITION is a boolean expression.\n\
629500fa 15383\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15384Multiple breakpoints at one place are permitted, and useful if their\n\
15385conditions are different.\n\
31e2b00f
AS
15386\n\
15387Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15388
44feb3ce
TT
15389/* List of subcommands for "catch". */
15390static struct cmd_list_element *catch_cmdlist;
15391
15392/* List of subcommands for "tcatch". */
15393static struct cmd_list_element *tcatch_cmdlist;
15394
9ac4176b 15395void
a121b7c1 15396add_catch_command (const char *name, const char *docstring,
82ae6c8d 15397 cmd_sfunc_ftype *sfunc,
625e8578 15398 completer_ftype *completer,
44feb3ce
TT
15399 void *user_data_catch,
15400 void *user_data_tcatch)
15401{
15402 struct cmd_list_element *command;
15403
0450cc4c 15404 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15405 &catch_cmdlist);
15406 set_cmd_sfunc (command, sfunc);
15407 set_cmd_context (command, user_data_catch);
a96d9b2e 15408 set_cmd_completer (command, completer);
44feb3ce 15409
0450cc4c 15410 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15411 &tcatch_cmdlist);
15412 set_cmd_sfunc (command, sfunc);
15413 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15414 set_cmd_completer (command, completer);
44feb3ce
TT
15415}
15416
6149aea9
PA
15417static void
15418save_command (char *arg, int from_tty)
15419{
3e43a32a
MS
15420 printf_unfiltered (_("\"save\" must be followed by "
15421 "the name of a save subcommand.\n"));
635c7e8a 15422 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15423}
15424
84f4c1fe
PM
15425struct breakpoint *
15426iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15427 void *data)
15428{
35df4500 15429 struct breakpoint *b, *b_tmp;
84f4c1fe 15430
35df4500 15431 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15432 {
15433 if ((*callback) (b, data))
15434 return b;
15435 }
15436
15437 return NULL;
15438}
15439
0574c78f
GB
15440/* Zero if any of the breakpoint's locations could be a location where
15441 functions have been inlined, nonzero otherwise. */
15442
15443static int
15444is_non_inline_function (struct breakpoint *b)
15445{
15446 /* The shared library event breakpoint is set on the address of a
15447 non-inline function. */
15448 if (b->type == bp_shlib_event)
15449 return 1;
15450
15451 return 0;
15452}
15453
15454/* Nonzero if the specified PC cannot be a location where functions
15455 have been inlined. */
15456
15457int
09ac7c10
TT
15458pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15459 const struct target_waitstatus *ws)
0574c78f
GB
15460{
15461 struct breakpoint *b;
15462 struct bp_location *bl;
15463
15464 ALL_BREAKPOINTS (b)
15465 {
15466 if (!is_non_inline_function (b))
15467 continue;
15468
15469 for (bl = b->loc; bl != NULL; bl = bl->next)
15470 {
15471 if (!bl->shlib_disabled
09ac7c10 15472 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15473 return 1;
15474 }
15475 }
15476
15477 return 0;
15478}
15479
2f202fde
JK
15480/* Remove any references to OBJFILE which is going to be freed. */
15481
15482void
15483breakpoint_free_objfile (struct objfile *objfile)
15484{
15485 struct bp_location **locp, *loc;
15486
15487 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15488 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15489 loc->symtab = NULL;
15490}
15491
2060206e
PA
15492void
15493initialize_breakpoint_ops (void)
15494{
15495 static int initialized = 0;
15496
15497 struct breakpoint_ops *ops;
15498
15499 if (initialized)
15500 return;
15501 initialized = 1;
15502
15503 /* The breakpoint_ops structure to be inherit by all kinds of
15504 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15505 internal and momentary breakpoints, etc.). */
15506 ops = &bkpt_base_breakpoint_ops;
15507 *ops = base_breakpoint_ops;
15508 ops->re_set = bkpt_re_set;
15509 ops->insert_location = bkpt_insert_location;
15510 ops->remove_location = bkpt_remove_location;
15511 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15512 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15513 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15514 ops->decode_location = bkpt_decode_location;
2060206e
PA
15515
15516 /* The breakpoint_ops structure to be used in regular breakpoints. */
15517 ops = &bkpt_breakpoint_ops;
15518 *ops = bkpt_base_breakpoint_ops;
15519 ops->re_set = bkpt_re_set;
15520 ops->resources_needed = bkpt_resources_needed;
15521 ops->print_it = bkpt_print_it;
15522 ops->print_mention = bkpt_print_mention;
15523 ops->print_recreate = bkpt_print_recreate;
15524
15525 /* Ranged breakpoints. */
15526 ops = &ranged_breakpoint_ops;
15527 *ops = bkpt_breakpoint_ops;
15528 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15529 ops->resources_needed = resources_needed_ranged_breakpoint;
15530 ops->print_it = print_it_ranged_breakpoint;
15531 ops->print_one = print_one_ranged_breakpoint;
15532 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15533 ops->print_mention = print_mention_ranged_breakpoint;
15534 ops->print_recreate = print_recreate_ranged_breakpoint;
15535
15536 /* Internal breakpoints. */
15537 ops = &internal_breakpoint_ops;
15538 *ops = bkpt_base_breakpoint_ops;
15539 ops->re_set = internal_bkpt_re_set;
15540 ops->check_status = internal_bkpt_check_status;
15541 ops->print_it = internal_bkpt_print_it;
15542 ops->print_mention = internal_bkpt_print_mention;
15543
15544 /* Momentary breakpoints. */
15545 ops = &momentary_breakpoint_ops;
15546 *ops = bkpt_base_breakpoint_ops;
15547 ops->re_set = momentary_bkpt_re_set;
15548 ops->check_status = momentary_bkpt_check_status;
15549 ops->print_it = momentary_bkpt_print_it;
15550 ops->print_mention = momentary_bkpt_print_mention;
15551
55aa24fb
SDJ
15552 /* Probe breakpoints. */
15553 ops = &bkpt_probe_breakpoint_ops;
15554 *ops = bkpt_breakpoint_ops;
15555 ops->insert_location = bkpt_probe_insert_location;
15556 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15557 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15558 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15559
2060206e
PA
15560 /* Watchpoints. */
15561 ops = &watchpoint_breakpoint_ops;
15562 *ops = base_breakpoint_ops;
15563 ops->re_set = re_set_watchpoint;
15564 ops->insert_location = insert_watchpoint;
15565 ops->remove_location = remove_watchpoint;
15566 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15567 ops->check_status = check_status_watchpoint;
15568 ops->resources_needed = resources_needed_watchpoint;
15569 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15570 ops->print_it = print_it_watchpoint;
15571 ops->print_mention = print_mention_watchpoint;
15572 ops->print_recreate = print_recreate_watchpoint;
427cd150 15573 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15574
15575 /* Masked watchpoints. */
15576 ops = &masked_watchpoint_breakpoint_ops;
15577 *ops = watchpoint_breakpoint_ops;
15578 ops->insert_location = insert_masked_watchpoint;
15579 ops->remove_location = remove_masked_watchpoint;
15580 ops->resources_needed = resources_needed_masked_watchpoint;
15581 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15582 ops->print_it = print_it_masked_watchpoint;
15583 ops->print_one_detail = print_one_detail_masked_watchpoint;
15584 ops->print_mention = print_mention_masked_watchpoint;
15585 ops->print_recreate = print_recreate_masked_watchpoint;
15586
15587 /* Tracepoints. */
15588 ops = &tracepoint_breakpoint_ops;
15589 *ops = base_breakpoint_ops;
15590 ops->re_set = tracepoint_re_set;
15591 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15592 ops->print_one_detail = tracepoint_print_one_detail;
15593 ops->print_mention = tracepoint_print_mention;
15594 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15595 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15596 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15597 ops->decode_location = tracepoint_decode_location;
983af33b 15598
55aa24fb
SDJ
15599 /* Probe tracepoints. */
15600 ops = &tracepoint_probe_breakpoint_ops;
15601 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15602 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15603 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15604
983af33b
SDJ
15605 /* Static tracepoints with marker (`-m'). */
15606 ops = &strace_marker_breakpoint_ops;
15607 *ops = tracepoint_breakpoint_ops;
5f700d83 15608 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15609 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15610 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15611
15612 /* Fork catchpoints. */
15613 ops = &catch_fork_breakpoint_ops;
15614 *ops = base_breakpoint_ops;
15615 ops->insert_location = insert_catch_fork;
15616 ops->remove_location = remove_catch_fork;
15617 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15618 ops->print_it = print_it_catch_fork;
15619 ops->print_one = print_one_catch_fork;
15620 ops->print_mention = print_mention_catch_fork;
15621 ops->print_recreate = print_recreate_catch_fork;
15622
15623 /* Vfork catchpoints. */
15624 ops = &catch_vfork_breakpoint_ops;
15625 *ops = base_breakpoint_ops;
15626 ops->insert_location = insert_catch_vfork;
15627 ops->remove_location = remove_catch_vfork;
15628 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15629 ops->print_it = print_it_catch_vfork;
15630 ops->print_one = print_one_catch_vfork;
15631 ops->print_mention = print_mention_catch_vfork;
15632 ops->print_recreate = print_recreate_catch_vfork;
15633
15634 /* Exec catchpoints. */
15635 ops = &catch_exec_breakpoint_ops;
15636 *ops = base_breakpoint_ops;
2060206e
PA
15637 ops->insert_location = insert_catch_exec;
15638 ops->remove_location = remove_catch_exec;
15639 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15640 ops->print_it = print_it_catch_exec;
15641 ops->print_one = print_one_catch_exec;
15642 ops->print_mention = print_mention_catch_exec;
15643 ops->print_recreate = print_recreate_catch_exec;
15644
edcc5120
TT
15645 /* Solib-related catchpoints. */
15646 ops = &catch_solib_breakpoint_ops;
15647 *ops = base_breakpoint_ops;
edcc5120
TT
15648 ops->insert_location = insert_catch_solib;
15649 ops->remove_location = remove_catch_solib;
15650 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15651 ops->check_status = check_status_catch_solib;
15652 ops->print_it = print_it_catch_solib;
15653 ops->print_one = print_one_catch_solib;
15654 ops->print_mention = print_mention_catch_solib;
15655 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15656
15657 ops = &dprintf_breakpoint_ops;
15658 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15659 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15660 ops->resources_needed = bkpt_resources_needed;
15661 ops->print_it = bkpt_print_it;
15662 ops->print_mention = bkpt_print_mention;
2d9442cc 15663 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15664 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15665 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15666}
15667
8bfd80db
YQ
15668/* Chain containing all defined "enable breakpoint" subcommands. */
15669
15670static struct cmd_list_element *enablebreaklist = NULL;
15671
c906108c 15672void
fba45db2 15673_initialize_breakpoint (void)
c906108c
SS
15674{
15675 struct cmd_list_element *c;
15676
2060206e
PA
15677 initialize_breakpoint_ops ();
15678
84acb35a 15679 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15680 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15681 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15682
55aa24fb
SDJ
15683 breakpoint_objfile_key
15684 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15685
c906108c
SS
15686 breakpoint_chain = 0;
15687 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15688 before a breakpoint is set. */
15689 breakpoint_count = 0;
15690
1042e4c0
SS
15691 tracepoint_count = 0;
15692
1bedd215
AC
15693 add_com ("ignore", class_breakpoint, ignore_command, _("\
15694Set ignore-count of breakpoint number N to COUNT.\n\
15695Usage is `ignore N COUNT'."));
c906108c 15696
1bedd215 15697 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15698Set commands to be executed when the given breakpoints are hit.\n\
15699Give a space-separated breakpoint list as argument after \"commands\".\n\
15700A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15701(e.g. `5-7').\n\
c906108c
SS
15702With no argument, the targeted breakpoint is the last one set.\n\
15703The commands themselves follow starting on the next line.\n\
15704Type a line containing \"end\" to indicate the end of them.\n\
15705Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15706then no output is printed when it is hit, except what the commands print."));
c906108c 15707
d55637df 15708 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15709Specify breakpoint number N to break only if COND is true.\n\
c906108c 15710Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15711expression to be evaluated whenever breakpoint N is reached."));
d55637df 15712 set_cmd_completer (c, condition_completer);
c906108c 15713
1bedd215 15714 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15715Set a temporary breakpoint.\n\
c906108c
SS
15716Like \"break\" except the breakpoint is only temporary,\n\
15717so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15718by using \"enable delete\" on the breakpoint number.\n\
15719\n"
15720BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15721 set_cmd_completer (c, location_completer);
c94fdfd0 15722
1bedd215 15723 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15724Set a hardware assisted breakpoint.\n\
c906108c 15725Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15726some target hardware may not have this support.\n\
15727\n"
15728BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15729 set_cmd_completer (c, location_completer);
c906108c 15730
1bedd215 15731 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15732Set a temporary hardware assisted breakpoint.\n\
c906108c 15733Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15734so it will be deleted when hit.\n\
15735\n"
15736BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15737 set_cmd_completer (c, location_completer);
c906108c 15738
1bedd215
AC
15739 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15740Enable some breakpoints.\n\
c906108c
SS
15741Give breakpoint numbers (separated by spaces) as arguments.\n\
15742With no subcommand, breakpoints are enabled until you command otherwise.\n\
15743This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15744With a subcommand you can enable temporarily."),
c906108c 15745 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15746
15747 add_com_alias ("en", "enable", class_breakpoint, 1);
15748
84951ab5 15749 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15750Enable some breakpoints.\n\
c906108c
SS
15751Give breakpoint numbers (separated by spaces) as arguments.\n\
15752This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15753May be abbreviated to simply \"enable\".\n"),
c5aa993b 15754 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15755
1a966eab
AC
15756 add_cmd ("once", no_class, enable_once_command, _("\
15757Enable breakpoints for one hit. Give breakpoint numbers.\n\
15758If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15759 &enablebreaklist);
15760
1a966eab
AC
15761 add_cmd ("delete", no_class, enable_delete_command, _("\
15762Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15763If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15764 &enablebreaklist);
15765
816338b5
SS
15766 add_cmd ("count", no_class, enable_count_command, _("\
15767Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15768If a breakpoint is hit while enabled in this fashion,\n\
15769the count is decremented; when it reaches zero, the breakpoint is disabled."),
15770 &enablebreaklist);
15771
1a966eab
AC
15772 add_cmd ("delete", no_class, enable_delete_command, _("\
15773Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15774If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15775 &enablelist);
15776
1a966eab
AC
15777 add_cmd ("once", no_class, enable_once_command, _("\
15778Enable breakpoints for one hit. Give breakpoint numbers.\n\
15779If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15780 &enablelist);
15781
15782 add_cmd ("count", no_class, enable_count_command, _("\
15783Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15784If a breakpoint is hit while enabled in this fashion,\n\
15785the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15786 &enablelist);
15787
1bedd215
AC
15788 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15789Disable some breakpoints.\n\
c906108c
SS
15790Arguments are breakpoint numbers with spaces in between.\n\
15791To disable all breakpoints, give no argument.\n\
64b9b334 15792A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15793 &disablelist, "disable ", 1, &cmdlist);
15794 add_com_alias ("dis", "disable", class_breakpoint, 1);
15795 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15796
1a966eab
AC
15797 add_cmd ("breakpoints", class_alias, disable_command, _("\
15798Disable some breakpoints.\n\
c906108c
SS
15799Arguments are breakpoint numbers with spaces in between.\n\
15800To disable all breakpoints, give no argument.\n\
64b9b334 15801A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15802This command may be abbreviated \"disable\"."),
c906108c
SS
15803 &disablelist);
15804
1bedd215
AC
15805 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15806Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15807Arguments are breakpoint numbers with spaces in between.\n\
15808To delete all breakpoints, give no argument.\n\
15809\n\
15810Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15811The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15812 &deletelist, "delete ", 1, &cmdlist);
15813 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15814 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15815
1a966eab
AC
15816 add_cmd ("breakpoints", class_alias, delete_command, _("\
15817Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15818Arguments are breakpoint numbers with spaces in between.\n\
15819To delete all breakpoints, give no argument.\n\
1a966eab 15820This command may be abbreviated \"delete\"."),
c906108c
SS
15821 &deletelist);
15822
1bedd215 15823 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15824Clear breakpoint at specified location.\n\
15825Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15826\n\
15827With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15828is executing in.\n"
15829"\n" LOCATION_HELP_STRING "\n\
1bedd215 15830See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15831 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15832
1bedd215 15833 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15834Set breakpoint at specified location.\n"
31e2b00f 15835BREAK_ARGS_HELP ("break")));
5ba2abeb 15836 set_cmd_completer (c, location_completer);
c94fdfd0 15837
c906108c
SS
15838 add_com_alias ("b", "break", class_run, 1);
15839 add_com_alias ("br", "break", class_run, 1);
15840 add_com_alias ("bre", "break", class_run, 1);
15841 add_com_alias ("brea", "break", class_run, 1);
15842
c906108c
SS
15843 if (dbx_commands)
15844 {
1bedd215
AC
15845 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15846Break in function/address or break at a line in the current file."),
c5aa993b
JM
15847 &stoplist, "stop ", 1, &cmdlist);
15848 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15849 _("Break in function or address."), &stoplist);
c5aa993b 15850 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15851 _("Break at a line in the current file."), &stoplist);
11db9430 15852 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15853Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15854The \"Type\" column indicates one of:\n\
15855\tbreakpoint - normal breakpoint\n\
15856\twatchpoint - watchpoint\n\
15857The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15858the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15859breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15860address and file/line number respectively.\n\
15861\n\
15862Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15863are set to the address of the last breakpoint listed unless the command\n\
15864is prefixed with \"server \".\n\n\
c906108c 15865Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15866breakpoint set."));
c906108c
SS
15867 }
15868
11db9430 15869 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15870Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15871The \"Type\" column indicates one of:\n\
15872\tbreakpoint - normal breakpoint\n\
15873\twatchpoint - watchpoint\n\
15874The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15875the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15876breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15877address and file/line number respectively.\n\
15878\n\
15879Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15880are set to the address of the last breakpoint listed unless the command\n\
15881is prefixed with \"server \".\n\n\
c906108c 15882Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15883breakpoint set."));
c906108c 15884
6b04bdb7
MS
15885 add_info_alias ("b", "breakpoints", 1);
15886
1a966eab
AC
15887 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15888Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15889The \"Type\" column indicates one of:\n\
15890\tbreakpoint - normal breakpoint\n\
15891\twatchpoint - watchpoint\n\
15892\tlongjmp - internal breakpoint used to step through longjmp()\n\
15893\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15894\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15895\tfinish - internal breakpoint used by the \"finish\" command\n\
15896The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15897the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15898breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15899address and file/line number respectively.\n\
15900\n\
15901Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15902are set to the address of the last breakpoint listed unless the command\n\
15903is prefixed with \"server \".\n\n\
c906108c 15904Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15905breakpoint set."),
c906108c
SS
15906 &maintenanceinfolist);
15907
44feb3ce
TT
15908 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15909Set catchpoints to catch events."),
15910 &catch_cmdlist, "catch ",
15911 0/*allow-unknown*/, &cmdlist);
15912
15913 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15914Set temporary catchpoints to catch events."),
15915 &tcatch_cmdlist, "tcatch ",
15916 0/*allow-unknown*/, &cmdlist);
15917
44feb3ce
TT
15918 add_catch_command ("fork", _("Catch calls to fork."),
15919 catch_fork_command_1,
a96d9b2e 15920 NULL,
44feb3ce
TT
15921 (void *) (uintptr_t) catch_fork_permanent,
15922 (void *) (uintptr_t) catch_fork_temporary);
15923 add_catch_command ("vfork", _("Catch calls to vfork."),
15924 catch_fork_command_1,
a96d9b2e 15925 NULL,
44feb3ce
TT
15926 (void *) (uintptr_t) catch_vfork_permanent,
15927 (void *) (uintptr_t) catch_vfork_temporary);
15928 add_catch_command ("exec", _("Catch calls to exec."),
15929 catch_exec_command_1,
a96d9b2e
SDJ
15930 NULL,
15931 CATCH_PERMANENT,
15932 CATCH_TEMPORARY);
edcc5120
TT
15933 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15934Usage: catch load [REGEX]\n\
15935If REGEX is given, only stop for libraries matching the regular expression."),
15936 catch_load_command_1,
15937 NULL,
15938 CATCH_PERMANENT,
15939 CATCH_TEMPORARY);
15940 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15941Usage: catch unload [REGEX]\n\
15942If REGEX is given, only stop for libraries matching the regular expression."),
15943 catch_unload_command_1,
15944 NULL,
15945 CATCH_PERMANENT,
15946 CATCH_TEMPORARY);
c5aa993b 15947
1bedd215
AC
15948 c = add_com ("watch", class_breakpoint, watch_command, _("\
15949Set a watchpoint for an expression.\n\
06a64a0b 15950Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15951A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15952an expression changes.\n\
15953If -l or -location is given, this evaluates EXPRESSION and watches\n\
15954the memory to which it refers."));
65d12d83 15955 set_cmd_completer (c, expression_completer);
c906108c 15956
1bedd215
AC
15957 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15958Set a read watchpoint for an expression.\n\
06a64a0b 15959Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15960A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15961an expression is read.\n\
15962If -l or -location is given, this evaluates EXPRESSION and watches\n\
15963the memory to which it refers."));
65d12d83 15964 set_cmd_completer (c, expression_completer);
c906108c 15965
1bedd215
AC
15966 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15967Set a watchpoint for an expression.\n\
06a64a0b 15968Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15969A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15970an expression is either read or written.\n\
15971If -l or -location is given, this evaluates EXPRESSION and watches\n\
15972the memory to which it refers."));
65d12d83 15973 set_cmd_completer (c, expression_completer);
c906108c 15974
11db9430 15975 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15976Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15977
920d2a44
AC
15978 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15979 respond to changes - contrary to the description. */
85c07804
AC
15980 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15981 &can_use_hw_watchpoints, _("\
15982Set debugger's willingness to use watchpoint hardware."), _("\
15983Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15984If zero, gdb will not use hardware for new watchpoints, even if\n\
15985such is available. (However, any hardware watchpoints that were\n\
15986created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15987hardware.)"),
15988 NULL,
920d2a44 15989 show_can_use_hw_watchpoints,
85c07804 15990 &setlist, &showlist);
c906108c
SS
15991
15992 can_use_hw_watchpoints = 1;
fa8d40ab 15993
1042e4c0
SS
15994 /* Tracepoint manipulation commands. */
15995
15996 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15997Set a tracepoint at specified location.\n\
1042e4c0
SS
15998\n"
15999BREAK_ARGS_HELP ("trace") "\n\
16000Do \"help tracepoints\" for info on other tracepoint commands."));
16001 set_cmd_completer (c, location_completer);
16002
16003 add_com_alias ("tp", "trace", class_alias, 0);
16004 add_com_alias ("tr", "trace", class_alias, 1);
16005 add_com_alias ("tra", "trace", class_alias, 1);
16006 add_com_alias ("trac", "trace", class_alias, 1);
16007
7a697b8d 16008 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16009Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16010\n"
16011BREAK_ARGS_HELP ("ftrace") "\n\
16012Do \"help tracepoints\" for info on other tracepoint commands."));
16013 set_cmd_completer (c, location_completer);
16014
0fb4aa4b 16015 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16016Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16017\n\
16018strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16019LOCATION may be a linespec, explicit, or address location (described below) \n\
16020or -m MARKER_ID.\n\n\
16021If a marker id is specified, probe the marker with that name. With\n\
16022no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16023Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16024This collects arbitrary user data passed in the probe point call to the\n\
16025tracing library. You can inspect it when analyzing the trace buffer,\n\
16026by printing the $_sdata variable like any other convenience variable.\n\
16027\n\
16028CONDITION is a boolean expression.\n\
629500fa 16029\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16030Multiple tracepoints at one place are permitted, and useful if their\n\
16031conditions are different.\n\
0fb4aa4b
PA
16032\n\
16033Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16034Do \"help tracepoints\" for info on other tracepoint commands."));
16035 set_cmd_completer (c, location_completer);
16036
11db9430 16037 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 16038Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16039Convenience variable \"$tpnum\" contains the number of the\n\
16040last tracepoint set."));
16041
16042 add_info_alias ("tp", "tracepoints", 1);
16043
16044 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16045Delete specified tracepoints.\n\
16046Arguments are tracepoint numbers, separated by spaces.\n\
16047No argument means delete all tracepoints."),
16048 &deletelist);
7e20dfcd 16049 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16050
16051 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16052Disable specified tracepoints.\n\
16053Arguments are tracepoint numbers, separated by spaces.\n\
16054No argument means disable all tracepoints."),
16055 &disablelist);
16056 deprecate_cmd (c, "disable");
16057
16058 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16059Enable specified tracepoints.\n\
16060Arguments are tracepoint numbers, separated by spaces.\n\
16061No argument means enable all tracepoints."),
16062 &enablelist);
16063 deprecate_cmd (c, "enable");
16064
16065 add_com ("passcount", class_trace, trace_pass_command, _("\
16066Set the passcount for a tracepoint.\n\
16067The trace will end when the tracepoint has been passed 'count' times.\n\
16068Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16069if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16070
6149aea9
PA
16071 add_prefix_cmd ("save", class_breakpoint, save_command,
16072 _("Save breakpoint definitions as a script."),
16073 &save_cmdlist, "save ",
16074 0/*allow-unknown*/, &cmdlist);
16075
16076 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16077Save current breakpoint definitions as a script.\n\
cce7e648 16078This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16079catchpoints, tracepoints). Use the 'source' command in another debug\n\
16080session to restore them."),
16081 &save_cmdlist);
16082 set_cmd_completer (c, filename_completer);
16083
16084 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16085Save current tracepoint definitions as a script.\n\
6149aea9
PA
16086Use the 'source' command in another debug session to restore them."),
16087 &save_cmdlist);
1042e4c0
SS
16088 set_cmd_completer (c, filename_completer);
16089
6149aea9
PA
16090 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16091 deprecate_cmd (c, "save tracepoints");
16092
1bedd215 16093 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16094Breakpoint specific settings\n\
16095Configure various breakpoint-specific variables such as\n\
1bedd215 16096pending breakpoint behavior"),
fa8d40ab
JJ
16097 &breakpoint_set_cmdlist, "set breakpoint ",
16098 0/*allow-unknown*/, &setlist);
1bedd215 16099 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16100Breakpoint specific settings\n\
16101Configure various breakpoint-specific variables such as\n\
1bedd215 16102pending breakpoint behavior"),
fa8d40ab
JJ
16103 &breakpoint_show_cmdlist, "show breakpoint ",
16104 0/*allow-unknown*/, &showlist);
16105
7915a72c
AC
16106 add_setshow_auto_boolean_cmd ("pending", no_class,
16107 &pending_break_support, _("\
16108Set debugger's behavior regarding pending breakpoints."), _("\
16109Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16110If on, an unrecognized breakpoint location will cause gdb to create a\n\
16111pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16112an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16113user-query to see if a pending breakpoint should be created."),
2c5b56ce 16114 NULL,
920d2a44 16115 show_pending_break_support,
6e1d7d6c
AC
16116 &breakpoint_set_cmdlist,
16117 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16118
16119 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16120
16121 add_setshow_boolean_cmd ("auto-hw", no_class,
16122 &automatic_hardware_breakpoints, _("\
16123Set automatic usage of hardware breakpoints."), _("\
16124Show automatic usage of hardware breakpoints."), _("\
16125If set, the debugger will automatically use hardware breakpoints for\n\
16126breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16127a warning will be emitted for such breakpoints."),
16128 NULL,
16129 show_automatic_hardware_breakpoints,
16130 &breakpoint_set_cmdlist,
16131 &breakpoint_show_cmdlist);
74960c60 16132
a25a5a45
PA
16133 add_setshow_boolean_cmd ("always-inserted", class_support,
16134 &always_inserted_mode, _("\
74960c60
VP
16135Set mode for inserting breakpoints."), _("\
16136Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16137When this mode is on, breakpoints are inserted immediately as soon as\n\
16138they're created, kept inserted even when execution stops, and removed\n\
16139only when the user deletes them. When this mode is off (the default),\n\
16140breakpoints are inserted only when execution continues, and removed\n\
16141when execution stops."),
72d0e2c5
YQ
16142 NULL,
16143 &show_always_inserted_mode,
16144 &breakpoint_set_cmdlist,
16145 &breakpoint_show_cmdlist);
f1310107 16146
b775012e
LM
16147 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16148 condition_evaluation_enums,
16149 &condition_evaluation_mode_1, _("\
16150Set mode of breakpoint condition evaluation."), _("\
16151Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16152When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16153evaluated on the host's side by GDB. When it is set to \"target\",\n\
16154breakpoint conditions will be downloaded to the target (if the target\n\
16155supports such feature) and conditions will be evaluated on the target's side.\n\
16156If this is set to \"auto\" (default), this will be automatically set to\n\
16157\"target\" if it supports condition evaluation, otherwise it will\n\
16158be set to \"gdb\""),
16159 &set_condition_evaluation_mode,
16160 &show_condition_evaluation_mode,
16161 &breakpoint_set_cmdlist,
16162 &breakpoint_show_cmdlist);
16163
f1310107
TJB
16164 add_com ("break-range", class_breakpoint, break_range_command, _("\
16165Set a breakpoint for an address range.\n\
16166break-range START-LOCATION, END-LOCATION\n\
16167where START-LOCATION and END-LOCATION can be one of the following:\n\
16168 LINENUM, for that line in the current file,\n\
16169 FILE:LINENUM, for that line in that file,\n\
16170 +OFFSET, for that number of lines after the current line\n\
16171 or the start of the range\n\
16172 FUNCTION, for the first line in that function,\n\
16173 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16174 *ADDRESS, for the instruction at that address.\n\
16175\n\
16176The breakpoint will stop execution of the inferior whenever it executes\n\
16177an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16178range (including START-LOCATION and END-LOCATION)."));
16179
e7e0cddf 16180 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16181Set a dynamic printf at specified location.\n\
e7e0cddf 16182dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16183location may be a linespec, explicit, or address location.\n"
16184"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16185 set_cmd_completer (c, location_completer);
16186
16187 add_setshow_enum_cmd ("dprintf-style", class_support,
16188 dprintf_style_enums, &dprintf_style, _("\
16189Set the style of usage for dynamic printf."), _("\
16190Show the style of usage for dynamic printf."), _("\
16191This setting chooses how GDB will do a dynamic printf.\n\
16192If the value is \"gdb\", then the printing is done by GDB to its own\n\
16193console, as with the \"printf\" command.\n\
16194If the value is \"call\", the print is done by calling a function in your\n\
16195program; by default printf(), but you can choose a different function or\n\
16196output stream by setting dprintf-function and dprintf-channel."),
16197 update_dprintf_commands, NULL,
16198 &setlist, &showlist);
16199
16200 dprintf_function = xstrdup ("printf");
16201 add_setshow_string_cmd ("dprintf-function", class_support,
16202 &dprintf_function, _("\
16203Set the function to use for dynamic printf"), _("\
16204Show the function to use for dynamic printf"), NULL,
16205 update_dprintf_commands, NULL,
16206 &setlist, &showlist);
16207
16208 dprintf_channel = xstrdup ("");
16209 add_setshow_string_cmd ("dprintf-channel", class_support,
16210 &dprintf_channel, _("\
16211Set the channel to use for dynamic printf"), _("\
16212Show the channel to use for dynamic printf"), NULL,
16213 update_dprintf_commands, NULL,
16214 &setlist, &showlist);
16215
d3ce09f5
SS
16216 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16217 &disconnected_dprintf, _("\
16218Set whether dprintf continues after GDB disconnects."), _("\
16219Show whether dprintf continues after GDB disconnects."), _("\
16220Use this to let dprintf commands continue to hit and produce output\n\
16221even if GDB disconnects or detaches from the target."),
16222 NULL,
16223 NULL,
16224 &setlist, &showlist);
16225
16226 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16227agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16228(target agent only) This is useful for formatted output in user-defined commands."));
16229
765dc015 16230 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16231
16232 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16233 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16234}
This page took 3.576155 seconds and 4 git commands to generate.