Introduce gdb::array_view
[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"
104c1213 84
e7e8980f
YQ
85/* Enums for exception-handling support. */
86enum exception_event_kind
87{
88 EX_EVENT_THROW,
591f19e8 89 EX_EVENT_RETHROW,
e7e8980f
YQ
90 EX_EVENT_CATCH
91};
92
4a64f543 93/* Prototypes for local functions. */
c906108c 94
a14ed312 95static void enable_delete_command (char *, int);
c906108c 96
a14ed312 97static void enable_once_command (char *, int);
c906108c 98
816338b5
SS
99static void enable_count_command (char *, int);
100
a14ed312 101static void disable_command (char *, int);
c906108c 102
a14ed312 103static void enable_command (char *, int);
c906108c 104
896b6bda
PA
105static void map_breakpoint_numbers (const char *,
106 void (*) (struct breakpoint *,
107 void *),
95a42b64 108 void *);
c906108c 109
a14ed312 110static void ignore_command (char *, int);
c906108c 111
4efb68b1 112static int breakpoint_re_set_one (void *);
c906108c 113
348d480f
PA
114static void breakpoint_re_set_default (struct breakpoint *);
115
f00aae0f
KS
116static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
983af33b
SDJ
120
121static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
e1e01040
PA
123 gdb::unique_xmalloc_ptr<char>,
124 gdb::unique_xmalloc_ptr<char>,
125 enum bptype,
983af33b
SDJ
126 enum bpdisp, int, int,
127 int,
128 const struct breakpoint_ops *,
44f238bb 129 int, int, int, unsigned);
983af33b 130
f00aae0f
KS
131static void decode_location_default (struct breakpoint *b,
132 const struct event_location *location,
c2f4122d 133 struct program_space *search_pspace,
f00aae0f 134 struct symtabs_and_lines *sals);
983af33b 135
a14ed312 136static void clear_command (char *, int);
c906108c 137
a14ed312 138static void catch_command (char *, int);
c906108c 139
a9634178 140static int can_use_hardware_watchpoint (struct value *);
c906108c 141
98deb0da 142static void break_command_1 (char *, int, int);
c906108c 143
a14ed312 144static void mention (struct breakpoint *);
c906108c 145
348d480f
PA
146static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
147 enum bptype,
c0a91b2b 148 const struct breakpoint_ops *);
3742cc8b
YQ
149static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
150 const struct symtab_and_line *);
151
4a64f543
MS
152/* This function is used in gdbtk sources and thus can not be made
153 static. */
63c252f8 154struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 155 struct symtab_and_line,
c0a91b2b
TT
156 enum bptype,
157 const struct breakpoint_ops *);
c906108c 158
06edf0c0
PA
159static struct breakpoint *
160 momentary_breakpoint_from_master (struct breakpoint *orig,
161 enum bptype type,
a1aa2221
LM
162 const struct breakpoint_ops *ops,
163 int loc_enabled);
06edf0c0 164
76897487
KB
165static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
166
a6d9a66e
UW
167static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
168 CORE_ADDR bpaddr,
88f7da05 169 enum bptype bptype);
76897487 170
6c95b8df
PA
171static void describe_other_breakpoints (struct gdbarch *,
172 struct program_space *, CORE_ADDR,
5af949e3 173 struct obj_section *, int);
c906108c 174
85d721b8
PA
175static int watchpoint_locations_match (struct bp_location *loc1,
176 struct bp_location *loc2);
177
f1310107
TJB
178static int breakpoint_location_address_match (struct bp_location *bl,
179 struct address_space *aspace,
180 CORE_ADDR addr);
181
d35ae833
PA
182static int breakpoint_location_address_range_overlap (struct bp_location *,
183 struct address_space *,
184 CORE_ADDR, int);
185
11db9430 186static void info_breakpoints_command (char *, int);
c906108c 187
11db9430 188static void info_watchpoints_command (char *, int);
d77f58be 189
e5a67952
MS
190static int breakpoint_1 (char *, int,
191 int (*) (const struct breakpoint *));
c906108c 192
4efb68b1 193static int breakpoint_cond_eval (void *);
c906108c 194
4efb68b1 195static void cleanup_executing_breakpoints (void *);
c906108c 196
a14ed312 197static void commands_command (char *, int);
c906108c 198
a14ed312 199static void condition_command (char *, int);
c906108c 200
834c0d03 201static int remove_breakpoint (struct bp_location *);
b2b6a7da 202static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 203
e514a9d6 204static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 205
4efb68b1 206static int watchpoint_check (void *);
c906108c 207
a14ed312 208static void maintenance_info_breakpoints (char *, int);
c906108c 209
a14ed312 210static int hw_breakpoint_used_count (void);
c906108c 211
a1398e0c
PA
212static int hw_watchpoint_use_count (struct breakpoint *);
213
214static int hw_watchpoint_used_count_others (struct breakpoint *except,
215 enum bptype type,
216 int *other_type_used);
c906108c 217
a14ed312 218static void hbreak_command (char *, int);
c906108c 219
a14ed312 220static void thbreak_command (char *, int);
c906108c 221
816338b5
SS
222static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
223 int count);
c906108c 224
a14ed312 225static void stop_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void stopin_command (char *arg, int from_tty);
7a292a7a 228
a14ed312 229static void stopat_command (char *arg, int from_tty);
7a292a7a 230
a14ed312 231static void tcatch_command (char *arg, int from_tty);
7a292a7a 232
fe3f5fa8 233static void free_bp_location (struct bp_location *loc);
f431efe5
PA
234static void incref_bp_location (struct bp_location *loc);
235static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 236
39d61571 237static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 238
44702360
PA
239/* update_global_location_list's modes of operation wrt to whether to
240 insert locations now. */
241enum ugll_insert_mode
242{
243 /* Don't insert any breakpoint locations into the inferior, only
244 remove already-inserted locations that no longer should be
245 inserted. Functions that delete a breakpoint or breakpoints
246 should specify this mode, so that deleting a breakpoint doesn't
247 have the side effect of inserting the locations of other
248 breakpoints that are marked not-inserted, but should_be_inserted
249 returns true on them.
250
251 This behavior is useful is situations close to tear-down -- e.g.,
252 after an exec, while the target still has execution, but
253 breakpoint shadows of the previous executable image should *NOT*
254 be restored to the new image; or before detaching, where the
255 target still has execution and wants to delete breakpoints from
256 GDB's lists, and all breakpoints had already been removed from
257 the inferior. */
258 UGLL_DONT_INSERT,
259
a25a5a45
PA
260 /* May insert breakpoints iff breakpoints_should_be_inserted_now
261 claims breakpoints should be inserted now. */
04086b45
PA
262 UGLL_MAY_INSERT,
263
a25a5a45
PA
264 /* Insert locations now, irrespective of
265 breakpoints_should_be_inserted_now. E.g., say all threads are
266 stopped right now, and the user did "continue". We need to
267 insert breakpoints _before_ resuming the target, but
268 UGLL_MAY_INSERT wouldn't insert them, because
269 breakpoints_should_be_inserted_now returns false at that point,
270 as no thread is running yet. */
04086b45 271 UGLL_INSERT
44702360
PA
272};
273
274static void update_global_location_list (enum ugll_insert_mode);
a5606eee 275
44702360 276static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 277
d77f58be 278static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
279
280static void insert_breakpoint_locations (void);
a5606eee 281
11db9430 282static void info_tracepoints_command (char *, int);
1042e4c0
SS
283
284static void delete_trace_command (char *, int);
285
286static void enable_trace_command (char *, int);
287
288static void disable_trace_command (char *, int);
289
290static void trace_pass_command (char *, int);
291
558a9d82
YQ
292static void set_tracepoint_count (int num);
293
9c06b0b4
TJB
294static int is_masked_watchpoint (const struct breakpoint *b);
295
b775012e
LM
296static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
297
983af33b
SDJ
298/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
299 otherwise. */
300
301static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 302
2060206e
PA
303/* The breakpoint_ops structure to be inherited by all breakpoint_ops
304 that are implemented on top of software or hardware breakpoints
305 (user breakpoints, internal and momentary breakpoints, etc.). */
306static struct breakpoint_ops bkpt_base_breakpoint_ops;
307
308/* Internal breakpoints class type. */
06edf0c0 309static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
310
311/* Momentary breakpoints class type. */
06edf0c0
PA
312static struct breakpoint_ops momentary_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
bde6261a 347static char *dprintf_function;
d3ce09f5
SS
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
bde6261a 357static char *dprintf_channel;
d3ce09f5
SS
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
372316f1
PA
474 struct thread_info *tp;
475
a25a5a45
PA
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
b57bacec
PA
483 if (threads_are_executing ())
484 return 1;
372316f1
PA
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593 580#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
581 for (BP_TMP = bp_locations; \
582 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
f5336ca5 595 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
f5336ca5 609/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 610
f5336ca5 611static struct bp_location **bp_locations;
876fa593 612
f5336ca5 613/* Number of elements of BP_LOCATIONS. */
876fa593 614
f5336ca5 615static unsigned bp_locations_count;
876fa593 616
4a64f543 617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 618 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 619 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 620 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 621 an address you need to read. */
876fa593 622
f5336ca5 623static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
627 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 629 scan for shadow bytes for an address you need to read. */
876fa593 630
f5336ca5 631static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 632
4a64f543 633/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
634 from the bp_locations array, but for which a hit may still be
635 reported by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
badd37ce
SDJ
656/* See declaration at breakpoint.h. */
657
658struct breakpoint *
659breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661{
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671}
672
468d015d
JJ
673/* Return whether a breakpoint is an active enabled breakpoint. */
674static int
675breakpoint_enabled (struct breakpoint *b)
676{
0d381245 677 return (b->enable_state == bp_enabled);
468d015d
JJ
678}
679
c906108c
SS
680/* Set breakpoint count to NUM. */
681
95a42b64 682static void
fba45db2 683set_breakpoint_count (int num)
c906108c 684{
86b17b60 685 prev_breakpoint_count = breakpoint_count;
c906108c 686 breakpoint_count = num;
4fa62494 687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
688}
689
86b17b60
PA
690/* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692static int rbreak_start_breakpoint_count;
693
95a42b64
TT
694/* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
86b17b60 696
95a42b64
TT
697void
698start_rbreak_breakpoints (void)
699{
86b17b60 700 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
701}
702
703/* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
86b17b60 705
95a42b64
TT
706void
707end_rbreak_breakpoints (void)
708{
86b17b60 709 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
710}
711
4a64f543 712/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
713
714void
fba45db2 715clear_breakpoint_hit_counts (void)
c906108c
SS
716{
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721}
722
9add0f1b
TT
723/* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726static struct counted_command_line *
727alloc_counted_command_line (struct command_line *commands)
728{
8d749320 729 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 730
9add0f1b
TT
731 result->refc = 1;
732 result->commands = commands;
8d749320 733
9add0f1b
TT
734 return result;
735}
736
737/* Increment reference count. This does nothing if CMD is NULL. */
738
739static void
740incref_counted_command_line (struct counted_command_line *cmd)
741{
742 if (cmd)
743 ++cmd->refc;
744}
745
746/* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750static void
751decref_counted_command_line (struct counted_command_line **cmdp)
752{
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762}
763
764/* A cleanup function that calls decref_counted_command_line. */
765
766static void
767do_cleanup_counted_command_line (void *arg)
768{
9a3c8263 769 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
770}
771
772/* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775static struct cleanup *
776make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777{
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779}
780
c906108c 781\f
48cb2d85
VP
782/* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785struct breakpoint *
786get_breakpoint (int num)
787{
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795}
5c44784c 796
c906108c 797\f
adc36818 798
b775012e
LM
799/* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802static void
803mark_breakpoint_modified (struct breakpoint *b)
804{
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820}
821
822/* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825static void
826mark_breakpoint_location_modified (struct bp_location *loc)
827{
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841}
842
843/* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846static void
847set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849{
b775012e
LM
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
abf1152a
JK
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
b775012e
LM
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
b775012e
LM
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
44702360 897 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
898 }
899
900 return;
901}
902
903/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906static void
907show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909{
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919}
920
921/* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 923 the more general bp_locations_compare function. */
b775012e
LM
924
925static int
f5336ca5 926bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 927{
9a3c8263
SM
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935}
936
937/* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942static struct bp_location **
943get_first_locp_gte_addr (CORE_ADDR address)
944{
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
b775012e
LM
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
9a3c8263 953 locp_found = ((struct bp_location **)
f5336ca5 954 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 955 sizeof (struct bp_location **),
f5336ca5 956 bp_locations_compare_addrs));
b775012e
LM
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 964 while ((locp_found - 1) >= bp_locations
b775012e
LM
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969}
970
adc36818 971void
7a26bd4d 972set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
973 int from_tty)
974{
3a5c3e22
PA
975 xfree (b->cond_string);
976 b->cond_string = NULL;
adc36818 977
3a5c3e22 978 if (is_watchpoint (b))
adc36818 979 {
3a5c3e22
PA
980 struct watchpoint *w = (struct watchpoint *) b;
981
4d01a485 982 w->cond_exp.reset ();
3a5c3e22
PA
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
4d01a485 990 loc->cond.reset ();
b775012e
LM
991
992 /* No need to free the condition agent expression
993 bytecode (if we have one). We will handle this
994 when we go through update_global_location_list. */
3a5c3e22 995 }
adc36818 996 }
adc36818
PM
997
998 if (*exp == 0)
999 {
1000 if (from_tty)
1001 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1002 }
1003 else
1004 {
bbc13ae3 1005 const char *arg = exp;
cc59ec59 1006
adc36818
PM
1007 /* I don't know if it matters whether this is the string the user
1008 typed in or the decompiled expression. */
1009 b->cond_string = xstrdup (arg);
1010 b->condition_not_parsed = 0;
1011
1012 if (is_watchpoint (b))
1013 {
3a5c3e22
PA
1014 struct watchpoint *w = (struct watchpoint *) b;
1015
adc36818
PM
1016 innermost_block = NULL;
1017 arg = exp;
1bb9788d 1018 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1019 if (*arg)
1020 error (_("Junk at end of expression"));
3a5c3e22 1021 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1022 }
1023 else
1024 {
3a5c3e22
PA
1025 struct bp_location *loc;
1026
adc36818
PM
1027 for (loc = b->loc; loc; loc = loc->next)
1028 {
1029 arg = exp;
1030 loc->cond =
1bb9788d
TT
1031 parse_exp_1 (&arg, loc->address,
1032 block_for_pc (loc->address), 0);
adc36818
PM
1033 if (*arg)
1034 error (_("Junk at end of expression"));
1035 }
1036 }
1037 }
b775012e
LM
1038 mark_breakpoint_modified (b);
1039
8d3788bd 1040 observer_notify_breakpoint_modified (b);
adc36818
PM
1041}
1042
d55637df
TT
1043/* Completion for the "condition" command. */
1044
eb3ff9a5 1045static void
6f937416 1046condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 1047 completion_tracker &tracker,
6f937416 1048 const char *text, const char *word)
d55637df 1049{
6f937416 1050 const char *space;
d55637df 1051
6f937416
PA
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
d55637df
TT
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
eb3ff9a5
PA
1063 if (!isdigit (text[1]))
1064 complete_internalvar (tracker, &text[1]);
1065 return;
d55637df
TT
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
1078 {
1079 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
1080 tracker.add_completion (std::move (copy));
1081 }
58ce7251 1082 }
d55637df 1083
eb3ff9a5 1084 return;
d55637df
TT
1085 }
1086
1087 /* We're completing the expression part. */
6f937416 1088 text = skip_spaces_const (space);
eb3ff9a5 1089 expression_completer (cmd, tracker, text, word);
d55637df
TT
1090}
1091
c906108c
SS
1092/* condition N EXP -- set break condition of breakpoint N to EXP. */
1093
1094static void
fba45db2 1095condition_command (char *arg, int from_tty)
c906108c 1096{
52f0bd74 1097 struct breakpoint *b;
c906108c 1098 char *p;
52f0bd74 1099 int bnum;
c906108c
SS
1100
1101 if (arg == 0)
e2e0b3e5 1102 error_no_arg (_("breakpoint number"));
c906108c
SS
1103
1104 p = arg;
1105 bnum = get_number (&p);
5c44784c 1106 if (bnum == 0)
8a3fe4f8 1107 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1108
1109 ALL_BREAKPOINTS (b)
1110 if (b->number == bnum)
2f069f6f 1111 {
6dddc817
DE
1112 /* Check if this breakpoint has a "stop" method implemented in an
1113 extension language. This method and conditions entered into GDB
1114 from the CLI are mutually exclusive. */
1115 const struct extension_language_defn *extlang
1116 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1117
1118 if (extlang != NULL)
1119 {
1120 error (_("Only one stop condition allowed. There is currently"
1121 " a %s stop condition defined for this breakpoint."),
1122 ext_lang_capitalized_name (extlang));
1123 }
2566ad2d 1124 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1125
1126 if (is_breakpoint (b))
44702360 1127 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1128
2f069f6f
JB
1129 return;
1130 }
c906108c 1131
8a3fe4f8 1132 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1133}
1134
a7bdde9e
VP
1135/* Check that COMMAND do not contain commands that are suitable
1136 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1137 Throw if any such commands is found. */
1138
a7bdde9e
VP
1139static void
1140check_no_tracepoint_commands (struct command_line *commands)
1141{
1142 struct command_line *c;
cc59ec59 1143
a7bdde9e
VP
1144 for (c = commands; c; c = c->next)
1145 {
1146 int i;
1147
1148 if (c->control_type == while_stepping_control)
3e43a32a
MS
1149 error (_("The 'while-stepping' command can "
1150 "only be used for tracepoints"));
a7bdde9e
VP
1151
1152 for (i = 0; i < c->body_count; ++i)
1153 check_no_tracepoint_commands ((c->body_list)[i]);
1154
1155 /* Not that command parsing removes leading whitespace and comment
4a64f543 1156 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1157 command directly. */
1158 if (strstr (c->line, "collect ") == c->line)
1159 error (_("The 'collect' command can only be used for tracepoints"));
1160
51661e93
VP
1161 if (strstr (c->line, "teval ") == c->line)
1162 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1163 }
1164}
1165
c1fc2657 1166struct longjmp_breakpoint : public breakpoint
3b0871f4 1167{
c1fc2657 1168 ~longjmp_breakpoint () override;
3b0871f4
SM
1169};
1170
d77f58be
SS
1171/* Encapsulate tests for different types of tracepoints. */
1172
3b0871f4
SM
1173static bool
1174is_tracepoint_type (bptype type)
d9b3f62e
PA
1175{
1176 return (type == bp_tracepoint
1177 || type == bp_fast_tracepoint
1178 || type == bp_static_tracepoint);
1179}
1180
3b0871f4
SM
1181static bool
1182is_longjmp_type (bptype type)
1183{
1184 return type == bp_longjmp || type == bp_exception;
1185}
1186
a7bdde9e 1187int
d77f58be 1188is_tracepoint (const struct breakpoint *b)
a7bdde9e 1189{
d9b3f62e 1190 return is_tracepoint_type (b->type);
a7bdde9e 1191}
d9b3f62e 1192
a5e364af
SM
1193/* Factory function to create an appropriate instance of breakpoint given
1194 TYPE. */
1195
1196static std::unique_ptr<breakpoint>
1197new_breakpoint_from_type (bptype type)
1198{
1199 breakpoint *b;
1200
1201 if (is_tracepoint_type (type))
c1fc2657 1202 b = new tracepoint ();
3b0871f4 1203 else if (is_longjmp_type (type))
c1fc2657 1204 b = new longjmp_breakpoint ();
a5e364af
SM
1205 else
1206 b = new breakpoint ();
1207
1208 return std::unique_ptr<breakpoint> (b);
1209}
1210
e5dd4106 1211/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1212 breakpoint. This function will throw an exception if a problem is
1213 found. */
48cb2d85 1214
95a42b64
TT
1215static void
1216validate_commands_for_breakpoint (struct breakpoint *b,
1217 struct command_line *commands)
48cb2d85 1218{
d77f58be 1219 if (is_tracepoint (b))
a7bdde9e 1220 {
c9a6ce02 1221 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1222 struct command_line *c;
1223 struct command_line *while_stepping = 0;
c9a6ce02
PA
1224
1225 /* Reset the while-stepping step count. The previous commands
1226 might have included a while-stepping action, while the new
1227 ones might not. */
1228 t->step_count = 0;
1229
1230 /* We need to verify that each top-level element of commands is
1231 valid for tracepoints, that there's at most one
1232 while-stepping element, and that the while-stepping's body
1233 has valid tracing commands excluding nested while-stepping.
1234 We also need to validate the tracepoint action line in the
1235 context of the tracepoint --- validate_actionline actually
1236 has side effects, like setting the tracepoint's
1237 while-stepping STEP_COUNT, in addition to checking if the
1238 collect/teval actions parse and make sense in the
1239 tracepoint's context. */
a7bdde9e
VP
1240 for (c = commands; c; c = c->next)
1241 {
a7bdde9e
VP
1242 if (c->control_type == while_stepping_control)
1243 {
1244 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1245 error (_("The 'while-stepping' command "
1246 "cannot be used for fast tracepoint"));
0fb4aa4b 1247 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1248 error (_("The 'while-stepping' command "
1249 "cannot be used for static tracepoint"));
a7bdde9e
VP
1250
1251 if (while_stepping)
3e43a32a
MS
1252 error (_("The 'while-stepping' command "
1253 "can be used only once"));
a7bdde9e
VP
1254 else
1255 while_stepping = c;
1256 }
c9a6ce02
PA
1257
1258 validate_actionline (c->line, b);
a7bdde9e
VP
1259 }
1260 if (while_stepping)
1261 {
1262 struct command_line *c2;
1263
1264 gdb_assert (while_stepping->body_count == 1);
1265 c2 = while_stepping->body_list[0];
1266 for (; c2; c2 = c2->next)
1267 {
a7bdde9e
VP
1268 if (c2->control_type == while_stepping_control)
1269 error (_("The 'while-stepping' command cannot be nested"));
1270 }
1271 }
1272 }
1273 else
1274 {
1275 check_no_tracepoint_commands (commands);
1276 }
95a42b64
TT
1277}
1278
0fb4aa4b
PA
1279/* Return a vector of all the static tracepoints set at ADDR. The
1280 caller is responsible for releasing the vector. */
1281
1282VEC(breakpoint_p) *
1283static_tracepoints_here (CORE_ADDR addr)
1284{
1285 struct breakpoint *b;
1286 VEC(breakpoint_p) *found = 0;
1287 struct bp_location *loc;
1288
1289 ALL_BREAKPOINTS (b)
1290 if (b->type == bp_static_tracepoint)
1291 {
1292 for (loc = b->loc; loc; loc = loc->next)
1293 if (loc->address == addr)
1294 VEC_safe_push(breakpoint_p, found, b);
1295 }
1296
1297 return found;
1298}
1299
95a42b64 1300/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1301 validate that only allowed commands are included. */
95a42b64
TT
1302
1303void
4a64f543 1304breakpoint_set_commands (struct breakpoint *b,
93921405 1305 command_line_up &&commands)
95a42b64 1306{
93921405 1307 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1308
9add0f1b 1309 decref_counted_command_line (&b->commands);
93921405 1310 b->commands = alloc_counted_command_line (commands.release ());
8d3788bd 1311 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1312}
1313
45a43567
TT
1314/* Set the internal `silent' flag on the breakpoint. Note that this
1315 is not the same as the "silent" that may appear in the breakpoint's
1316 commands. */
1317
1318void
1319breakpoint_set_silent (struct breakpoint *b, int silent)
1320{
1321 int old_silent = b->silent;
1322
1323 b->silent = silent;
1324 if (old_silent != silent)
8d3788bd 1325 observer_notify_breakpoint_modified (b);
45a43567
TT
1326}
1327
1328/* Set the thread for this breakpoint. If THREAD is -1, make the
1329 breakpoint work for any thread. */
1330
1331void
1332breakpoint_set_thread (struct breakpoint *b, int thread)
1333{
1334 int old_thread = b->thread;
1335
1336 b->thread = thread;
1337 if (old_thread != thread)
8d3788bd 1338 observer_notify_breakpoint_modified (b);
45a43567
TT
1339}
1340
1341/* Set the task for this breakpoint. If TASK is 0, make the
1342 breakpoint work for any task. */
1343
1344void
1345breakpoint_set_task (struct breakpoint *b, int task)
1346{
1347 int old_task = b->task;
1348
1349 b->task = task;
1350 if (old_task != task)
8d3788bd 1351 observer_notify_breakpoint_modified (b);
45a43567
TT
1352}
1353
95a42b64
TT
1354void
1355check_tracepoint_command (char *line, void *closure)
a7bdde9e 1356{
9a3c8263 1357 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1358
6f937416 1359 validate_actionline (line, b);
a7bdde9e
VP
1360}
1361
95a42b64
TT
1362/* A structure used to pass information through
1363 map_breakpoint_numbers. */
1364
1365struct commands_info
1366{
1367 /* True if the command was typed at a tty. */
1368 int from_tty;
86b17b60
PA
1369
1370 /* The breakpoint range spec. */
896b6bda 1371 const char *arg;
86b17b60 1372
95a42b64
TT
1373 /* Non-NULL if the body of the commands are being read from this
1374 already-parsed command. */
1375 struct command_line *control;
86b17b60 1376
95a42b64
TT
1377 /* The command lines read from the user, or NULL if they have not
1378 yet been read. */
1379 struct counted_command_line *cmd;
1380};
1381
1382/* A callback for map_breakpoint_numbers that sets the commands for
1383 commands_command. */
1384
c906108c 1385static void
95a42b64 1386do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1387{
9a3c8263 1388 struct commands_info *info = (struct commands_info *) data;
c906108c 1389
95a42b64
TT
1390 if (info->cmd == NULL)
1391 {
93921405 1392 command_line_up l;
5c44784c 1393
95a42b64
TT
1394 if (info->control != NULL)
1395 l = copy_command_lines (info->control->body_list[0]);
1396 else
86b17b60
PA
1397 {
1398 struct cleanup *old_chain;
1399 char *str;
c5aa993b 1400
3e43a32a
MS
1401 str = xstrprintf (_("Type commands for breakpoint(s) "
1402 "%s, one per line."),
86b17b60
PA
1403 info->arg);
1404
1405 old_chain = make_cleanup (xfree, str);
1406
1407 l = read_command_lines (str,
1408 info->from_tty, 1,
d77f58be 1409 (is_tracepoint (b)
86b17b60
PA
1410 ? check_tracepoint_command : 0),
1411 b);
1412
1413 do_cleanups (old_chain);
1414 }
a7bdde9e 1415
93921405 1416 info->cmd = alloc_counted_command_line (l.release ());
95a42b64
TT
1417 }
1418
1419 /* If a breakpoint was on the list more than once, we don't need to
1420 do anything. */
1421 if (b->commands != info->cmd)
1422 {
1423 validate_commands_for_breakpoint (b, info->cmd->commands);
1424 incref_counted_command_line (info->cmd);
1425 decref_counted_command_line (&b->commands);
1426 b->commands = info->cmd;
8d3788bd 1427 observer_notify_breakpoint_modified (b);
c5aa993b 1428 }
95a42b64
TT
1429}
1430
1431static void
896b6bda 1432commands_command_1 (const char *arg, int from_tty,
4a64f543 1433 struct command_line *control)
95a42b64
TT
1434{
1435 struct cleanup *cleanups;
1436 struct commands_info info;
1437
1438 info.from_tty = from_tty;
1439 info.control = control;
1440 info.cmd = NULL;
1441 /* If we read command lines from the user, then `info' will hold an
1442 extra reference to the commands that we must clean up. */
1443 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1444
896b6bda
PA
1445 std::string new_arg;
1446
95a42b64
TT
1447 if (arg == NULL || !*arg)
1448 {
86b17b60 1449 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1450 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1451 breakpoint_count);
95a42b64 1452 else if (breakpoint_count > 0)
896b6bda 1453 new_arg = string_printf ("%d", breakpoint_count);
95a42b64 1454 }
9766ced4 1455 else
896b6bda 1456 new_arg = arg;
86b17b60 1457
896b6bda 1458 info.arg = new_arg.c_str ();
95a42b64 1459
896b6bda 1460 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
95a42b64
TT
1461
1462 if (info.cmd == NULL)
1463 error (_("No breakpoints specified."));
1464
1465 do_cleanups (cleanups);
1466}
1467
1468static void
1469commands_command (char *arg, int from_tty)
1470{
1471 commands_command_1 (arg, from_tty, NULL);
c906108c 1472}
40c03ae8
EZ
1473
1474/* Like commands_command, but instead of reading the commands from
1475 input stream, takes them from an already parsed command structure.
1476
1477 This is used by cli-script.c to DTRT with breakpoint commands
1478 that are part of if and while bodies. */
1479enum command_control_type
896b6bda 1480commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1481{
95a42b64
TT
1482 commands_command_1 (arg, 0, cmd);
1483 return simple_control;
40c03ae8 1484}
876fa593
JK
1485
1486/* Return non-zero if BL->TARGET_INFO contains valid information. */
1487
1488static int
1489bp_location_has_shadow (struct bp_location *bl)
1490{
1491 if (bl->loc_type != bp_loc_software_breakpoint)
1492 return 0;
1493 if (!bl->inserted)
1494 return 0;
1495 if (bl->target_info.shadow_len == 0)
e5dd4106 1496 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1497 return 0;
1498 return 1;
1499}
1500
9d497a19
PA
1501/* Update BUF, which is LEN bytes read from the target address
1502 MEMADDR, by replacing a memory breakpoint with its shadowed
1503 contents.
1504
1505 If READBUF is not NULL, this buffer must not overlap with the of
1506 the breakpoint location's shadow_contents buffer. Otherwise, a
1507 failed assertion internal error will be raised. */
1508
1509static void
1510one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1511 const gdb_byte *writebuf_org,
1512 ULONGEST memaddr, LONGEST len,
1513 struct bp_target_info *target_info,
1514 struct gdbarch *gdbarch)
1515{
1516 /* Now do full processing of the found relevant range of elements. */
1517 CORE_ADDR bp_addr = 0;
1518 int bp_size = 0;
1519 int bptoffset = 0;
1520
1521 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1522 current_program_space->aspace, 0))
1523 {
1524 /* The breakpoint is inserted in a different address space. */
1525 return;
1526 }
1527
1528 /* Addresses and length of the part of the breakpoint that
1529 we need to copy. */
1530 bp_addr = target_info->placed_address;
1531 bp_size = target_info->shadow_len;
1532
1533 if (bp_addr + bp_size <= memaddr)
1534 {
1535 /* The breakpoint is entirely before the chunk of memory we are
1536 reading. */
1537 return;
1538 }
1539
1540 if (bp_addr >= memaddr + len)
1541 {
1542 /* The breakpoint is entirely after the chunk of memory we are
1543 reading. */
1544 return;
1545 }
1546
1547 /* Offset within shadow_contents. */
1548 if (bp_addr < memaddr)
1549 {
1550 /* Only copy the second part of the breakpoint. */
1551 bp_size -= memaddr - bp_addr;
1552 bptoffset = memaddr - bp_addr;
1553 bp_addr = memaddr;
1554 }
1555
1556 if (bp_addr + bp_size > memaddr + len)
1557 {
1558 /* Only copy the first part of the breakpoint. */
1559 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1560 }
1561
1562 if (readbuf != NULL)
1563 {
1564 /* Verify that the readbuf buffer does not overlap with the
1565 shadow_contents buffer. */
1566 gdb_assert (target_info->shadow_contents >= readbuf + len
1567 || readbuf >= (target_info->shadow_contents
1568 + target_info->shadow_len));
1569
1570 /* Update the read buffer with this inserted breakpoint's
1571 shadow. */
1572 memcpy (readbuf + bp_addr - memaddr,
1573 target_info->shadow_contents + bptoffset, bp_size);
1574 }
1575 else
1576 {
1577 const unsigned char *bp;
0d5ed153
MR
1578 CORE_ADDR addr = target_info->reqstd_address;
1579 int placed_size;
9d497a19
PA
1580
1581 /* Update the shadow with what we want to write to memory. */
1582 memcpy (target_info->shadow_contents + bptoffset,
1583 writebuf_org + bp_addr - memaddr, bp_size);
1584
1585 /* Determine appropriate breakpoint contents and size for this
1586 address. */
0d5ed153 1587 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1588
1589 /* Update the final write buffer with this inserted
1590 breakpoint's INSN. */
1591 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1592 }
1593}
1594
8defab1a 1595/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1596 by replacing any memory breakpoints with their shadowed contents.
1597
35c63cd8
JB
1598 If READBUF is not NULL, this buffer must not overlap with any of
1599 the breakpoint location's shadow_contents buffers. Otherwise,
1600 a failed assertion internal error will be raised.
1601
876fa593 1602 The range of shadowed area by each bp_location is:
f5336ca5
PA
1603 bl->address - bp_locations_placed_address_before_address_max
1604 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1605 The range we were requested to resolve shadows for is:
1606 memaddr ... memaddr + len
1607 Thus the safe cutoff boundaries for performance optimization are
35df4500 1608 memaddr + len <= (bl->address
f5336ca5 1609 - bp_locations_placed_address_before_address_max)
876fa593 1610 and:
f5336ca5 1611 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1612
8defab1a 1613void
f0ba3972
PA
1614breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1615 const gdb_byte *writebuf_org,
1616 ULONGEST memaddr, LONGEST len)
c906108c 1617{
4a64f543
MS
1618 /* Left boundary, right boundary and median element of our binary
1619 search. */
876fa593
JK
1620 unsigned bc_l, bc_r, bc;
1621
4a64f543
MS
1622 /* Find BC_L which is a leftmost element which may affect BUF
1623 content. It is safe to report lower value but a failure to
1624 report higher one. */
876fa593
JK
1625
1626 bc_l = 0;
f5336ca5 1627 bc_r = bp_locations_count;
876fa593
JK
1628 while (bc_l + 1 < bc_r)
1629 {
35df4500 1630 struct bp_location *bl;
876fa593
JK
1631
1632 bc = (bc_l + bc_r) / 2;
f5336ca5 1633 bl = bp_locations[bc];
876fa593 1634
4a64f543
MS
1635 /* Check first BL->ADDRESS will not overflow due to the added
1636 constant. Then advance the left boundary only if we are sure
1637 the BC element can in no way affect the BUF content (MEMADDR
1638 to MEMADDR + LEN range).
876fa593 1639
f5336ca5 1640 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1641 offset so that we cannot miss a breakpoint with its shadow
1642 range tail still reaching MEMADDR. */
c5aa993b 1643
f5336ca5 1644 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1645 >= bl->address)
f5336ca5 1646 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1647 <= memaddr))
876fa593
JK
1648 bc_l = bc;
1649 else
1650 bc_r = bc;
1651 }
1652
128070bb
PA
1653 /* Due to the binary search above, we need to make sure we pick the
1654 first location that's at BC_L's address. E.g., if there are
1655 multiple locations at the same address, BC_L may end up pointing
1656 at a duplicate location, and miss the "master"/"inserted"
1657 location. Say, given locations L1, L2 and L3 at addresses A and
1658 B:
1659
1660 L1@A, L2@A, L3@B, ...
1661
1662 BC_L could end up pointing at location L2, while the "master"
1663 location could be L1. Since the `loc->inserted' flag is only set
1664 on "master" locations, we'd forget to restore the shadow of L1
1665 and L2. */
1666 while (bc_l > 0
f5336ca5 1667 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1668 bc_l--;
1669
876fa593
JK
1670 /* Now do full processing of the found relevant range of elements. */
1671
f5336ca5 1672 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1673 {
f5336ca5 1674 struct bp_location *bl = bp_locations[bc];
876fa593 1675
35df4500
TJB
1676 /* bp_location array has BL->OWNER always non-NULL. */
1677 if (bl->owner->type == bp_none)
8a3fe4f8 1678 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1679 bl->owner->number);
ffce0d52 1680
e5dd4106 1681 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1682 content. */
1683
f5336ca5
PA
1684 if (bl->address >= bp_locations_placed_address_before_address_max
1685 && memaddr + len <= (bl->address
1686 - bp_locations_placed_address_before_address_max))
876fa593
JK
1687 break;
1688
35df4500 1689 if (!bp_location_has_shadow (bl))
c5aa993b 1690 continue;
6c95b8df 1691
9d497a19
PA
1692 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1693 memaddr, len, &bl->target_info, bl->gdbarch);
1694 }
c906108c 1695}
9d497a19 1696
c906108c 1697\f
c5aa993b 1698
b775012e
LM
1699/* Return true if BPT is either a software breakpoint or a hardware
1700 breakpoint. */
1701
1702int
1703is_breakpoint (const struct breakpoint *bpt)
1704{
1705 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1706 || bpt->type == bp_hardware_breakpoint
1707 || bpt->type == bp_dprintf);
b775012e
LM
1708}
1709
60e1c644
PA
1710/* Return true if BPT is of any hardware watchpoint kind. */
1711
a5606eee 1712static int
d77f58be 1713is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1714{
1715 return (bpt->type == bp_hardware_watchpoint
1716 || bpt->type == bp_read_watchpoint
1717 || bpt->type == bp_access_watchpoint);
1718}
7270d8f2 1719
60e1c644
PA
1720/* Return true if BPT is of any watchpoint kind, hardware or
1721 software. */
1722
3a5c3e22 1723int
d77f58be 1724is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1725{
1726 return (is_hardware_watchpoint (bpt)
1727 || bpt->type == bp_watchpoint);
1728}
1729
3a5c3e22
PA
1730/* Returns true if the current thread and its running state are safe
1731 to evaluate or update watchpoint B. Watchpoints on local
1732 expressions need to be evaluated in the context of the thread that
1733 was current when the watchpoint was created, and, that thread needs
1734 to be stopped to be able to select the correct frame context.
1735 Watchpoints on global expressions can be evaluated on any thread,
1736 and in any state. It is presently left to the target allowing
1737 memory accesses when threads are running. */
f6bc2008
PA
1738
1739static int
3a5c3e22 1740watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1741{
c1fc2657 1742 return (b->pspace == current_program_space
d0d8b0c6
JK
1743 && (ptid_equal (b->watchpoint_thread, null_ptid)
1744 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1745 && !is_executing (inferior_ptid))));
f6bc2008
PA
1746}
1747
d0fb5eae
JK
1748/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1749 associated bp_watchpoint_scope breakpoint. */
1750
1751static void
3a5c3e22 1752watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1753{
c1fc2657 1754 if (w->related_breakpoint != w)
d0fb5eae 1755 {
c1fc2657
SM
1756 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1757 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1758 w->related_breakpoint->disposition = disp_del_at_next_stop;
1759 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1760 w->related_breakpoint = w;
d0fb5eae 1761 }
c1fc2657 1762 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1763}
1764
bb9d5f81
PP
1765/* Extract a bitfield value from value VAL using the bit parameters contained in
1766 watchpoint W. */
1767
1768static struct value *
1769extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1770{
1771 struct value *bit_val;
1772
1773 if (val == NULL)
1774 return NULL;
1775
1776 bit_val = allocate_value (value_type (val));
1777
1778 unpack_value_bitfield (bit_val,
1779 w->val_bitpos,
1780 w->val_bitsize,
1781 value_contents_for_printing (val),
1782 value_offset (val),
1783 val);
1784
1785 return bit_val;
1786}
1787
c6d81124
PA
1788/* Allocate a dummy location and add it to B, which must be a software
1789 watchpoint. This is required because even if a software watchpoint
1790 is not watching any memory, bpstat_stop_status requires a location
1791 to be able to report stops. */
1792
1793static void
1794software_watchpoint_add_no_memory_location (struct breakpoint *b,
1795 struct program_space *pspace)
1796{
1797 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1798
1799 b->loc = allocate_bp_location (b);
1800 b->loc->pspace = pspace;
1801 b->loc->address = -1;
1802 b->loc->length = -1;
1803}
1804
1805/* Returns true if B is a software watchpoint that is not watching any
1806 memory (e.g., "watch $pc"). */
1807
1808static int
1809is_no_memory_software_watchpoint (struct breakpoint *b)
1810{
1811 return (b->type == bp_watchpoint
1812 && b->loc != NULL
1813 && b->loc->next == NULL
1814 && b->loc->address == -1
1815 && b->loc->length == -1);
1816}
1817
567e1b4e
JB
1818/* Assuming that B is a watchpoint:
1819 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1820 - Evaluate expression and store the result in B->val
567e1b4e
JB
1821 - Evaluate the condition if there is one, and store the result
1822 in b->loc->cond.
a5606eee
VP
1823 - Update the list of values that must be watched in B->loc.
1824
4a64f543
MS
1825 If the watchpoint disposition is disp_del_at_next_stop, then do
1826 nothing. If this is local watchpoint that is out of scope, delete
1827 it.
1828
1829 Even with `set breakpoint always-inserted on' the watchpoints are
1830 removed + inserted on each stop here. Normal breakpoints must
1831 never be removed because they might be missed by a running thread
1832 when debugging in non-stop mode. On the other hand, hardware
1833 watchpoints (is_hardware_watchpoint; processed here) are specific
1834 to each LWP since they are stored in each LWP's hardware debug
1835 registers. Therefore, such LWP must be stopped first in order to
1836 be able to modify its hardware watchpoints.
1837
1838 Hardware watchpoints must be reset exactly once after being
1839 presented to the user. It cannot be done sooner, because it would
1840 reset the data used to present the watchpoint hit to the user. And
1841 it must not be done later because it could display the same single
1842 watchpoint hit during multiple GDB stops. Note that the latter is
1843 relevant only to the hardware watchpoint types bp_read_watchpoint
1844 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1845 not user-visible - its hit is suppressed if the memory content has
1846 not changed.
1847
1848 The following constraints influence the location where we can reset
1849 hardware watchpoints:
1850
1851 * target_stopped_by_watchpoint and target_stopped_data_address are
1852 called several times when GDB stops.
1853
1854 [linux]
1855 * Multiple hardware watchpoints can be hit at the same time,
1856 causing GDB to stop. GDB only presents one hardware watchpoint
1857 hit at a time as the reason for stopping, and all the other hits
1858 are presented later, one after the other, each time the user
1859 requests the execution to be resumed. Execution is not resumed
1860 for the threads still having pending hit event stored in
1861 LWP_INFO->STATUS. While the watchpoint is already removed from
1862 the inferior on the first stop the thread hit event is kept being
1863 reported from its cached value by linux_nat_stopped_data_address
1864 until the real thread resume happens after the watchpoint gets
1865 presented and thus its LWP_INFO->STATUS gets reset.
1866
1867 Therefore the hardware watchpoint hit can get safely reset on the
1868 watchpoint removal from inferior. */
a79d3c27 1869
b40ce68a 1870static void
3a5c3e22 1871update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1872{
a5606eee 1873 int within_current_scope;
a5606eee 1874 struct frame_id saved_frame_id;
66076460 1875 int frame_saved;
a5606eee 1876
f6bc2008
PA
1877 /* If this is a local watchpoint, we only want to check if the
1878 watchpoint frame is in scope if the current thread is the thread
1879 that was used to create the watchpoint. */
1880 if (!watchpoint_in_thread_scope (b))
1881 return;
1882
c1fc2657 1883 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1884 return;
1885
66076460 1886 frame_saved = 0;
a5606eee
VP
1887
1888 /* Determine if the watchpoint is within scope. */
1889 if (b->exp_valid_block == NULL)
1890 within_current_scope = 1;
1891 else
1892 {
b5db5dfc
UW
1893 struct frame_info *fi = get_current_frame ();
1894 struct gdbarch *frame_arch = get_frame_arch (fi);
1895 CORE_ADDR frame_pc = get_frame_pc (fi);
1896
c9cf6e20
MG
1897 /* If we're at a point where the stack has been destroyed
1898 (e.g. in a function epilogue), unwinding may not work
1899 properly. Do not attempt to recreate locations at this
b5db5dfc 1900 point. See similar comments in watchpoint_check. */
c9cf6e20 1901 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1902 return;
66076460
DJ
1903
1904 /* Save the current frame's ID so we can restore it after
1905 evaluating the watchpoint expression on its own frame. */
1906 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1907 took a frame parameter, so that we didn't have to change the
1908 selected frame. */
1909 frame_saved = 1;
1910 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1911
a5606eee
VP
1912 fi = frame_find_by_id (b->watchpoint_frame);
1913 within_current_scope = (fi != NULL);
1914 if (within_current_scope)
1915 select_frame (fi);
1916 }
1917
b5db5dfc
UW
1918 /* We don't free locations. They are stored in the bp_location array
1919 and update_global_location_list will eventually delete them and
1920 remove breakpoints if needed. */
c1fc2657 1921 b->loc = NULL;
b5db5dfc 1922
a5606eee
VP
1923 if (within_current_scope && reparse)
1924 {
bbc13ae3 1925 const char *s;
d63d0675 1926
4d01a485 1927 b->exp.reset ();
d63d0675 1928 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1929 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1930 /* If the meaning of expression itself changed, the old value is
1931 no longer relevant. We don't want to report a watchpoint hit
1932 to the user when the old value and the new value may actually
1933 be completely different objects. */
1934 value_free (b->val);
fa4727a6
DJ
1935 b->val = NULL;
1936 b->val_valid = 0;
60e1c644
PA
1937
1938 /* Note that unlike with breakpoints, the watchpoint's condition
1939 expression is stored in the breakpoint object, not in the
1940 locations (re)created below. */
c1fc2657 1941 if (b->cond_string != NULL)
60e1c644 1942 {
4d01a485 1943 b->cond_exp.reset ();
60e1c644 1944
c1fc2657 1945 s = b->cond_string;
1bb9788d 1946 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1947 }
a5606eee 1948 }
a5606eee
VP
1949
1950 /* If we failed to parse the expression, for example because
1951 it refers to a global variable in a not-yet-loaded shared library,
1952 don't try to insert watchpoint. We don't automatically delete
1953 such watchpoint, though, since failure to parse expression
1954 is different from out-of-scope watchpoint. */
e8369a73 1955 if (!target_has_execution)
2d134ed3
PA
1956 {
1957 /* Without execution, memory can't change. No use to try and
1958 set watchpoint locations. The watchpoint will be reset when
1959 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1960 if (!can_use_hw_watchpoints)
1961 {
c1fc2657
SM
1962 if (b->ops->works_in_software_mode (b))
1963 b->type = bp_watchpoint;
e8369a73 1964 else
638aa5a1
AB
1965 error (_("Can't set read/access watchpoint when "
1966 "hardware watchpoints are disabled."));
e8369a73 1967 }
2d134ed3
PA
1968 }
1969 else if (within_current_scope && b->exp)
a5606eee 1970 {
0cf6dd15 1971 int pc = 0;
fa4727a6 1972 struct value *val_chain, *v, *result, *next;
2d134ed3 1973 struct program_space *frame_pspace;
a5606eee 1974
4d01a485 1975 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1976
a5606eee
VP
1977 /* Avoid setting b->val if it's already set. The meaning of
1978 b->val is 'the last value' user saw, and we should update
1979 it only if we reported that last value to user. As it
9c06b0b4
TJB
1980 happens, the code that reports it updates b->val directly.
1981 We don't keep track of the memory value for masked
1982 watchpoints. */
c1fc2657 1983 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1984 {
bb9d5f81
PP
1985 if (b->val_bitsize != 0)
1986 {
1987 v = extract_bitfield_from_watchpoint_value (b, v);
1988 if (v != NULL)
1989 release_value (v);
1990 }
fa4727a6
DJ
1991 b->val = v;
1992 b->val_valid = 1;
1993 }
a5606eee 1994
2d134ed3
PA
1995 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1996
a5606eee 1997 /* Look at each value on the value chain. */
9fa40276 1998 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1999 {
2000 /* If it's a memory location, and GDB actually needed
2001 its contents to evaluate the expression, then we
fa4727a6
DJ
2002 must watch it. If the first value returned is
2003 still lazy, that means an error occurred reading it;
2004 watch it anyway in case it becomes readable. */
a5606eee 2005 if (VALUE_LVAL (v) == lval_memory
fa4727a6 2006 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
2007 {
2008 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2009
a5606eee
VP
2010 /* We only watch structs and arrays if user asked
2011 for it explicitly, never if they just happen to
2012 appear in the middle of some value chain. */
fa4727a6 2013 if (v == result
a5606eee
VP
2014 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2015 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2016 {
2017 CORE_ADDR addr;
f486487f 2018 enum target_hw_bp_type type;
a5606eee 2019 struct bp_location *loc, **tmp;
bb9d5f81
PP
2020 int bitpos = 0, bitsize = 0;
2021
2022 if (value_bitsize (v) != 0)
2023 {
2024 /* Extract the bit parameters out from the bitfield
2025 sub-expression. */
2026 bitpos = value_bitpos (v);
2027 bitsize = value_bitsize (v);
2028 }
2029 else if (v == result && b->val_bitsize != 0)
2030 {
2031 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2032 lvalue whose bit parameters are saved in the fields
2033 VAL_BITPOS and VAL_BITSIZE. */
2034 bitpos = b->val_bitpos;
2035 bitsize = b->val_bitsize;
2036 }
a5606eee 2037
42ae5230 2038 addr = value_address (v);
bb9d5f81
PP
2039 if (bitsize != 0)
2040 {
2041 /* Skip the bytes that don't contain the bitfield. */
2042 addr += bitpos / 8;
2043 }
2044
a5606eee 2045 type = hw_write;
c1fc2657 2046 if (b->type == bp_read_watchpoint)
a5606eee 2047 type = hw_read;
c1fc2657 2048 else if (b->type == bp_access_watchpoint)
a5606eee 2049 type = hw_access;
3a5c3e22 2050
c1fc2657
SM
2051 loc = allocate_bp_location (b);
2052 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2053 ;
2054 *tmp = loc;
a6d9a66e 2055 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2056
2057 loc->pspace = frame_pspace;
a5606eee 2058 loc->address = addr;
bb9d5f81
PP
2059
2060 if (bitsize != 0)
2061 {
2062 /* Just cover the bytes that make up the bitfield. */
2063 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2064 }
2065 else
2066 loc->length = TYPE_LENGTH (value_type (v));
2067
a5606eee
VP
2068 loc->watchpoint_type = type;
2069 }
2070 }
9fa40276
TJB
2071 }
2072
2073 /* Change the type of breakpoint between hardware assisted or
2074 an ordinary watchpoint depending on the hardware support
2075 and free hardware slots. REPARSE is set when the inferior
2076 is started. */
a9634178 2077 if (reparse)
9fa40276 2078 {
e09342b5 2079 int reg_cnt;
9fa40276
TJB
2080 enum bp_loc_type loc_type;
2081 struct bp_location *bl;
a5606eee 2082
a9634178 2083 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2084
2085 if (reg_cnt)
9fa40276
TJB
2086 {
2087 int i, target_resources_ok, other_type_used;
a1398e0c 2088 enum bptype type;
9fa40276 2089
a9634178
TJB
2090 /* Use an exact watchpoint when there's only one memory region to be
2091 watched, and only one debug register is needed to watch it. */
2092 b->exact = target_exact_watchpoints && reg_cnt == 1;
2093
9fa40276 2094 /* We need to determine how many resources are already
e09342b5
TJB
2095 used for all other hardware watchpoints plus this one
2096 to see if we still have enough resources to also fit
a1398e0c
PA
2097 this watchpoint in as well. */
2098
2099 /* If this is a software watchpoint, we try to turn it
2100 to a hardware one -- count resources as if B was of
2101 hardware watchpoint type. */
c1fc2657 2102 type = b->type;
a1398e0c
PA
2103 if (type == bp_watchpoint)
2104 type = bp_hardware_watchpoint;
2105
2106 /* This watchpoint may or may not have been placed on
2107 the list yet at this point (it won't be in the list
2108 if we're trying to create it for the first time,
2109 through watch_command), so always account for it
2110 manually. */
2111
2112 /* Count resources used by all watchpoints except B. */
c1fc2657 2113 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
2114
2115 /* Add in the resources needed for B. */
c1fc2657 2116 i += hw_watchpoint_use_count (b);
a1398e0c
PA
2117
2118 target_resources_ok
2119 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2120 if (target_resources_ok <= 0)
a9634178 2121 {
c1fc2657 2122 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
2123
2124 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2125 error (_("Target does not support this type of "
2126 "hardware watchpoint."));
9c06b0b4
TJB
2127 else if (target_resources_ok < 0 && !sw_mode)
2128 error (_("There are not enough available hardware "
2129 "resources for this watchpoint."));
a1398e0c
PA
2130
2131 /* Downgrade to software watchpoint. */
c1fc2657 2132 b->type = bp_watchpoint;
a1398e0c
PA
2133 }
2134 else
2135 {
2136 /* If this was a software watchpoint, we've just
2137 found we have enough resources to turn it to a
2138 hardware watchpoint. Otherwise, this is a
2139 nop. */
c1fc2657 2140 b->type = type;
a9634178 2141 }
9fa40276 2142 }
c1fc2657 2143 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
2144 {
2145 if (!can_use_hw_watchpoints)
2146 error (_("Can't set read/access watchpoint when "
2147 "hardware watchpoints are disabled."));
2148 else
2149 error (_("Expression cannot be implemented with "
2150 "read/access watchpoint."));
2151 }
9fa40276 2152 else
c1fc2657 2153 b->type = bp_watchpoint;
9fa40276 2154
c1fc2657 2155 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 2156 : bp_loc_hardware_watchpoint);
c1fc2657 2157 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
2158 bl->loc_type = loc_type;
2159 }
2160
2161 for (v = val_chain; v; v = next)
2162 {
a5606eee
VP
2163 next = value_next (v);
2164 if (v != b->val)
2165 value_free (v);
2166 }
2167
c7437ca6
PA
2168 /* If a software watchpoint is not watching any memory, then the
2169 above left it without any location set up. But,
2170 bpstat_stop_status requires a location to be able to report
2171 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
2172 if (b->type == bp_watchpoint && b->loc == NULL)
2173 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
2174 }
2175 else if (!within_current_scope)
7270d8f2 2176 {
ac74f770
MS
2177 printf_filtered (_("\
2178Watchpoint %d deleted because the program has left the block\n\
2179in which its expression is valid.\n"),
c1fc2657 2180 b->number);
d0fb5eae 2181 watchpoint_del_at_next_stop (b);
7270d8f2 2182 }
a5606eee
VP
2183
2184 /* Restore the selected frame. */
66076460
DJ
2185 if (frame_saved)
2186 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2187}
2188
a5606eee 2189
74960c60 2190/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2191 inserted in the inferior. We don't differentiate the type of BL's owner
2192 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2193 breakpoint_ops is not defined, because in insert_bp_location,
2194 tracepoint's insert_location will not be called. */
74960c60 2195static int
35df4500 2196should_be_inserted (struct bp_location *bl)
74960c60 2197{
35df4500 2198 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2199 return 0;
2200
35df4500 2201 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2202 return 0;
2203
35df4500 2204 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2205 return 0;
2206
f8eba3c6
TT
2207 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2208 return 0;
2209
56710373
PA
2210 /* This is set for example, when we're attached to the parent of a
2211 vfork, and have detached from the child. The child is running
2212 free, and we expect it to do an exec or exit, at which point the
2213 OS makes the parent schedulable again (and the target reports
2214 that the vfork is done). Until the child is done with the shared
2215 memory region, do not insert breakpoints in the parent, otherwise
2216 the child could still trip on the parent's breakpoints. Since
2217 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2218 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2219 return 0;
2220
31e77af2 2221 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2222 location, except if the breakpoint is a single-step breakpoint,
2223 and the breakpoint's thread is the thread which is stepping past
2224 a breakpoint. */
31e77af2
PA
2225 if ((bl->loc_type == bp_loc_software_breakpoint
2226 || bl->loc_type == bp_loc_hardware_breakpoint)
2227 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2228 bl->address)
2229 /* The single-step breakpoint may be inserted at the location
2230 we're trying to step if the instruction branches to itself.
2231 However, the instruction won't be executed at all and it may
2232 break the semantics of the instruction, for example, the
2233 instruction is a conditional branch or updates some flags.
2234 We can't fix it unless GDB is able to emulate the instruction
2235 or switch to displaced stepping. */
2236 && !(bl->owner->type == bp_single_step
2237 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2238 {
2239 if (debug_infrun)
2240 {
2241 fprintf_unfiltered (gdb_stdlog,
2242 "infrun: skipping breakpoint: "
2243 "stepping past insn at: %s\n",
2244 paddress (bl->gdbarch, bl->address));
2245 }
2246 return 0;
2247 }
31e77af2 2248
963f9c80
PA
2249 /* Don't insert watchpoints if we're trying to step past the
2250 instruction that triggered one. */
2251 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2252 && stepping_past_nonsteppable_watchpoint ())
2253 {
2254 if (debug_infrun)
2255 {
2256 fprintf_unfiltered (gdb_stdlog,
2257 "infrun: stepping past non-steppable watchpoint. "
2258 "skipping watchpoint at %s:%d\n",
2259 paddress (bl->gdbarch, bl->address),
2260 bl->length);
2261 }
2262 return 0;
2263 }
2264
74960c60
VP
2265 return 1;
2266}
2267
934709f0
PW
2268/* Same as should_be_inserted but does the check assuming
2269 that the location is not duplicated. */
2270
2271static int
2272unduplicated_should_be_inserted (struct bp_location *bl)
2273{
2274 int result;
2275 const int save_duplicate = bl->duplicate;
2276
2277 bl->duplicate = 0;
2278 result = should_be_inserted (bl);
2279 bl->duplicate = save_duplicate;
2280 return result;
2281}
2282
b775012e
LM
2283/* Parses a conditional described by an expression COND into an
2284 agent expression bytecode suitable for evaluation
2285 by the bytecode interpreter. Return NULL if there was
2286 any error during parsing. */
2287
833177a4 2288static agent_expr_up
b775012e
LM
2289parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2290{
833177a4 2291 if (cond == NULL)
b775012e
LM
2292 return NULL;
2293
833177a4
PA
2294 agent_expr_up aexpr;
2295
b775012e
LM
2296 /* We don't want to stop processing, so catch any errors
2297 that may show up. */
492d29ea 2298 TRY
b775012e 2299 {
036e657b 2300 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2301 }
2302
492d29ea 2303 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2304 {
2305 /* If we got here, it means the condition could not be parsed to a valid
2306 bytecode expression and thus can't be evaluated on the target's side.
2307 It's no use iterating through the conditions. */
b775012e 2308 }
492d29ea 2309 END_CATCH
b775012e
LM
2310
2311 /* We have a valid agent expression. */
2312 return aexpr;
2313}
2314
2315/* Based on location BL, create a list of breakpoint conditions to be
2316 passed on to the target. If we have duplicated locations with different
2317 conditions, we will add such conditions to the list. The idea is that the
2318 target will evaluate the list of conditions and will only notify GDB when
2319 one of them is true. */
2320
2321static void
2322build_target_condition_list (struct bp_location *bl)
2323{
2324 struct bp_location **locp = NULL, **loc2p;
2325 int null_condition_or_parse_error = 0;
2326 int modified = bl->needs_update;
2327 struct bp_location *loc;
2328
8b4f3082 2329 /* Release conditions left over from a previous insert. */
3cde5c42 2330 bl->target_info.conditions.clear ();
8b4f3082 2331
b775012e
LM
2332 /* This is only meaningful if the target is
2333 evaluating conditions and if the user has
2334 opted for condition evaluation on the target's
2335 side. */
2336 if (gdb_evaluates_breakpoint_condition_p ()
2337 || !target_supports_evaluation_of_breakpoint_conditions ())
2338 return;
2339
2340 /* Do a first pass to check for locations with no assigned
2341 conditions or conditions that fail to parse to a valid agent expression
2342 bytecode. If any of these happen, then it's no use to send conditions
2343 to the target since this location will always trigger and generate a
2344 response back to GDB. */
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 if (modified)
2351 {
b775012e
LM
2352 /* Re-parse the conditions since something changed. In that
2353 case we already freed the condition bytecodes (see
2354 force_breakpoint_reinsertion). We just
2355 need to parse the condition to bytecodes again. */
833177a4
PA
2356 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2357 loc->cond.get ());
b775012e
LM
2358 }
2359
2360 /* If we have a NULL bytecode expression, it means something
2361 went wrong or we have a null condition expression. */
2362 if (!loc->cond_bytecode)
2363 {
2364 null_condition_or_parse_error = 1;
2365 break;
2366 }
2367 }
2368 }
2369
2370 /* If any of these happened, it means we will have to evaluate the conditions
2371 for the location's address on gdb's side. It is no use keeping bytecodes
2372 for all the other duplicate locations, thus we free all of them here.
2373
2374 This is so we have a finer control over which locations' conditions are
2375 being evaluated by GDB or the remote stub. */
2376 if (null_condition_or_parse_error)
2377 {
2378 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2379 {
2380 loc = (*loc2p);
2381 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2382 {
2383 /* Only go as far as the first NULL bytecode is
2384 located. */
2385 if (!loc->cond_bytecode)
2386 return;
2387
833177a4 2388 loc->cond_bytecode.reset ();
b775012e
LM
2389 }
2390 }
2391 }
2392
2393 /* No NULL conditions or failed bytecode generation. Build a condition list
2394 for this location's address. */
2395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2396 {
2397 loc = (*loc2p);
2398 if (loc->cond
2399 && is_breakpoint (loc->owner)
2400 && loc->pspace->num == bl->pspace->num
2401 && loc->owner->enable_state == bp_enabled
2402 && loc->enabled)
3cde5c42
PA
2403 {
2404 /* Add the condition to the vector. This will be used later
2405 to send the conditions to the target. */
2406 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2407 }
b775012e
LM
2408 }
2409
2410 return;
2411}
2412
d3ce09f5
SS
2413/* Parses a command described by string CMD into an agent expression
2414 bytecode suitable for evaluation by the bytecode interpreter.
2415 Return NULL if there was any error during parsing. */
2416
833177a4 2417static agent_expr_up
d3ce09f5
SS
2418parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2419{
2420 struct cleanup *old_cleanups = 0;
4d01a485 2421 struct expression **argvec;
bbc13ae3
KS
2422 const char *cmdrest;
2423 const char *format_start, *format_end;
d3ce09f5
SS
2424 struct format_piece *fpieces;
2425 int nargs;
2426 struct gdbarch *gdbarch = get_current_arch ();
2427
833177a4 2428 if (cmd == NULL)
d3ce09f5
SS
2429 return NULL;
2430
2431 cmdrest = cmd;
2432
2433 if (*cmdrest == ',')
2434 ++cmdrest;
bbc13ae3 2435 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2436
2437 if (*cmdrest++ != '"')
2438 error (_("No format string following the location"));
2439
2440 format_start = cmdrest;
2441
2442 fpieces = parse_format_string (&cmdrest);
2443
2444 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2445
2446 format_end = cmdrest;
2447
2448 if (*cmdrest++ != '"')
2449 error (_("Bad format string, non-terminated '\"'."));
2450
bbc13ae3 2451 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2452
2453 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2454 error (_("Invalid argument syntax"));
2455
2456 if (*cmdrest == ',')
2457 cmdrest++;
bbc13ae3 2458 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2459
2460 /* For each argument, make an expression. */
2461
2462 argvec = (struct expression **) alloca (strlen (cmd)
2463 * sizeof (struct expression *));
2464
2465 nargs = 0;
2466 while (*cmdrest != '\0')
2467 {
bbc13ae3 2468 const char *cmd1;
d3ce09f5
SS
2469
2470 cmd1 = cmdrest;
4d01a485
PA
2471 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2472 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2473 cmdrest = cmd1;
2474 if (*cmdrest == ',')
2475 ++cmdrest;
2476 }
2477
833177a4
PA
2478 agent_expr_up aexpr;
2479
d3ce09f5
SS
2480 /* We don't want to stop processing, so catch any errors
2481 that may show up. */
492d29ea 2482 TRY
d3ce09f5 2483 {
036e657b
JB
2484 aexpr = gen_printf (scope, gdbarch, 0, 0,
2485 format_start, format_end - format_start,
2486 fpieces, nargs, argvec);
d3ce09f5 2487 }
492d29ea 2488 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2489 {
2490 /* If we got here, it means the command could not be parsed to a valid
2491 bytecode expression and thus can't be evaluated on the target's side.
2492 It's no use iterating through the other commands. */
d3ce09f5 2493 }
492d29ea
PA
2494 END_CATCH
2495
2496 do_cleanups (old_cleanups);
d3ce09f5 2497
d3ce09f5
SS
2498 /* We have a valid agent expression, return it. */
2499 return aexpr;
2500}
2501
2502/* Based on location BL, create a list of breakpoint commands to be
2503 passed on to the target. If we have duplicated locations with
2504 different commands, we will add any such to the list. */
2505
2506static void
2507build_target_command_list (struct bp_location *bl)
2508{
2509 struct bp_location **locp = NULL, **loc2p;
2510 int null_command_or_parse_error = 0;
2511 int modified = bl->needs_update;
2512 struct bp_location *loc;
2513
3cde5c42
PA
2514 /* Clear commands left over from a previous insert. */
2515 bl->target_info.tcommands.clear ();
8b4f3082 2516
41fac0cf 2517 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2518 return;
2519
41fac0cf
PA
2520 /* For now, limit to agent-style dprintf breakpoints. */
2521 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2522 return;
2523
41fac0cf
PA
2524 /* For now, if we have any duplicate location that isn't a dprintf,
2525 don't install the target-side commands, as that would make the
2526 breakpoint not be reported to the core, and we'd lose
2527 control. */
2528 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2529 {
2530 loc = (*loc2p);
2531 if (is_breakpoint (loc->owner)
2532 && loc->pspace->num == bl->pspace->num
2533 && loc->owner->type != bp_dprintf)
2534 return;
2535 }
2536
d3ce09f5
SS
2537 /* Do a first pass to check for locations with no assigned
2538 conditions or conditions that fail to parse to a valid agent expression
2539 bytecode. If any of these happen, then it's no use to send conditions
2540 to the target since this location will always trigger and generate a
2541 response back to GDB. */
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2546 {
2547 if (modified)
2548 {
d3ce09f5
SS
2549 /* Re-parse the commands since something changed. In that
2550 case we already freed the command bytecodes (see
2551 force_breakpoint_reinsertion). We just
2552 need to parse the command to bytecodes again. */
833177a4
PA
2553 loc->cmd_bytecode
2554 = parse_cmd_to_aexpr (bl->address,
2555 loc->owner->extra_string);
d3ce09f5
SS
2556 }
2557
2558 /* If we have a NULL bytecode expression, it means something
2559 went wrong or we have a null command expression. */
2560 if (!loc->cmd_bytecode)
2561 {
2562 null_command_or_parse_error = 1;
2563 break;
2564 }
2565 }
2566 }
2567
2568 /* If anything failed, then we're not doing target-side commands,
2569 and so clean up. */
2570 if (null_command_or_parse_error)
2571 {
2572 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2573 {
2574 loc = (*loc2p);
2575 if (is_breakpoint (loc->owner)
2576 && loc->pspace->num == bl->pspace->num)
2577 {
2578 /* Only go as far as the first NULL bytecode is
2579 located. */
40fb6c5e 2580 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2581 return;
2582
833177a4 2583 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2584 }
2585 }
2586 }
2587
2588 /* No NULL commands or failed bytecode generation. Build a command list
2589 for this location's address. */
2590 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2591 {
2592 loc = (*loc2p);
2593 if (loc->owner->extra_string
2594 && is_breakpoint (loc->owner)
2595 && loc->pspace->num == bl->pspace->num
2596 && loc->owner->enable_state == bp_enabled
2597 && loc->enabled)
3cde5c42
PA
2598 {
2599 /* Add the command to the vector. This will be used later
2600 to send the commands to the target. */
2601 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2602 }
d3ce09f5
SS
2603 }
2604
2605 bl->target_info.persist = 0;
2606 /* Maybe flag this location as persistent. */
2607 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2608 bl->target_info.persist = 1;
2609}
2610
833b7ab5
YQ
2611/* Return the kind of breakpoint on address *ADDR. Get the kind
2612 of breakpoint according to ADDR except single-step breakpoint.
2613 Get the kind of single-step breakpoint according to the current
2614 registers state. */
cd6c3b4f
YQ
2615
2616static int
2617breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2618{
833b7ab5
YQ
2619 if (bl->owner->type == bp_single_step)
2620 {
2621 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2622 struct regcache *regcache;
2623
2624 regcache = get_thread_regcache (thr->ptid);
2625
2626 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2627 regcache, addr);
2628 }
2629 else
2630 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2631}
2632
35df4500
TJB
2633/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2634 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2635 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2636 Returns 0 for success, 1 if the bp_location type is not supported or
2637 -1 for failure.
879bfdc2 2638
4a64f543
MS
2639 NOTE drow/2003-09-09: This routine could be broken down to an
2640 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2641static int
35df4500 2642insert_bp_location (struct bp_location *bl,
26bb91f3 2643 struct ui_file *tmp_error_stream,
3fbb6ffa 2644 int *disabled_breaks,
dd61ec5c
MW
2645 int *hw_breakpoint_error,
2646 int *hw_bp_error_explained_already)
879bfdc2 2647{
0000e5cc
PA
2648 enum errors bp_err = GDB_NO_ERROR;
2649 const char *bp_err_message = NULL;
879bfdc2 2650
b775012e 2651 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2652 return 0;
2653
35c63cd8
JB
2654 /* Note we don't initialize bl->target_info, as that wipes out
2655 the breakpoint location's shadow_contents if the breakpoint
2656 is still inserted at that location. This in turn breaks
2657 target_read_memory which depends on these buffers when
2658 a memory read is requested at the breakpoint location:
2659 Once the target_info has been wiped, we fail to see that
2660 we have a breakpoint inserted at that address and thus
2661 read the breakpoint instead of returning the data saved in
2662 the breakpoint location's shadow contents. */
0d5ed153 2663 bl->target_info.reqstd_address = bl->address;
35df4500 2664 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2665 bl->target_info.length = bl->length;
8181d85f 2666
b775012e
LM
2667 /* When working with target-side conditions, we must pass all the conditions
2668 for the same breakpoint address down to the target since GDB will not
2669 insert those locations. With a list of breakpoint conditions, the target
2670 can decide when to stop and notify GDB. */
2671
2672 if (is_breakpoint (bl->owner))
2673 {
2674 build_target_condition_list (bl);
d3ce09f5
SS
2675 build_target_command_list (bl);
2676 /* Reset the modification marker. */
b775012e
LM
2677 bl->needs_update = 0;
2678 }
2679
35df4500
TJB
2680 if (bl->loc_type == bp_loc_software_breakpoint
2681 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2682 {
35df4500 2683 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2684 {
2685 /* If the explicitly specified breakpoint type
2686 is not hardware breakpoint, check the memory map to see
2687 if the breakpoint address is in read only memory or not.
4a64f543 2688
765dc015
VP
2689 Two important cases are:
2690 - location type is not hardware breakpoint, memory
2691 is readonly. We change the type of the location to
2692 hardware breakpoint.
4a64f543
MS
2693 - location type is hardware breakpoint, memory is
2694 read-write. This means we've previously made the
2695 location hardware one, but then the memory map changed,
2696 so we undo.
765dc015 2697
4a64f543
MS
2698 When breakpoints are removed, remove_breakpoints will use
2699 location types we've just set here, the only possible
2700 problem is that memory map has changed during running
2701 program, but it's not going to work anyway with current
2702 gdb. */
765dc015 2703 struct mem_region *mr
0d5ed153 2704 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2705
2706 if (mr)
2707 {
2708 if (automatic_hardware_breakpoints)
2709 {
765dc015
VP
2710 enum bp_loc_type new_type;
2711
2712 if (mr->attrib.mode != MEM_RW)
2713 new_type = bp_loc_hardware_breakpoint;
2714 else
2715 new_type = bp_loc_software_breakpoint;
2716
35df4500 2717 if (new_type != bl->loc_type)
765dc015
VP
2718 {
2719 static int said = 0;
cc59ec59 2720
35df4500 2721 bl->loc_type = new_type;
765dc015
VP
2722 if (!said)
2723 {
3e43a32a
MS
2724 fprintf_filtered (gdb_stdout,
2725 _("Note: automatically using "
2726 "hardware breakpoints for "
2727 "read-only addresses.\n"));
765dc015
VP
2728 said = 1;
2729 }
2730 }
2731 }
35df4500 2732 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2733 && mr->attrib.mode != MEM_RW)
2734 {
2735 fprintf_unfiltered (tmp_error_stream,
2736 _("Cannot insert breakpoint %d.\n"
2737 "Cannot set software breakpoint "
2738 "at read-only address %s\n"),
2739 bl->owner->number,
2740 paddress (bl->gdbarch, bl->address));
2741 return 1;
2742 }
765dc015
VP
2743 }
2744 }
2745
879bfdc2
DJ
2746 /* First check to see if we have to handle an overlay. */
2747 if (overlay_debugging == ovly_off
35df4500
TJB
2748 || bl->section == NULL
2749 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2750 {
2751 /* No overlay handling: just set the breakpoint. */
492d29ea 2752 TRY
dd61ec5c 2753 {
0000e5cc
PA
2754 int val;
2755
dd61ec5c 2756 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2757 if (val)
2758 bp_err = GENERIC_ERROR;
dd61ec5c 2759 }
492d29ea 2760 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2761 {
0000e5cc
PA
2762 bp_err = e.error;
2763 bp_err_message = e.message;
dd61ec5c 2764 }
492d29ea 2765 END_CATCH
879bfdc2
DJ
2766 }
2767 else
2768 {
4a64f543 2769 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2770 Shall we set a breakpoint at the LMA? */
2771 if (!overlay_events_enabled)
2772 {
2773 /* Yes -- overlay event support is not active,
2774 so we must try to set a breakpoint at the LMA.
2775 This will not work for a hardware breakpoint. */
35df4500 2776 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2777 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2778 bl->owner->number);
879bfdc2
DJ
2779 else
2780 {
35df4500
TJB
2781 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2782 bl->section);
879bfdc2 2783 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2784 bl->overlay_target_info = bl->target_info;
0d5ed153 2785 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2786
2787 /* No overlay handling: just set the breakpoint. */
492d29ea 2788 TRY
0000e5cc
PA
2789 {
2790 int val;
2791
579c6ad9 2792 bl->overlay_target_info.kind
cd6c3b4f
YQ
2793 = breakpoint_kind (bl, &addr);
2794 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2795 val = target_insert_breakpoint (bl->gdbarch,
2796 &bl->overlay_target_info);
2797 if (val)
2798 bp_err = GENERIC_ERROR;
2799 }
492d29ea 2800 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2801 {
2802 bp_err = e.error;
2803 bp_err_message = e.message;
2804 }
492d29ea 2805 END_CATCH
0000e5cc
PA
2806
2807 if (bp_err != GDB_NO_ERROR)
99361f52 2808 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2809 "Overlay breakpoint %d "
2810 "failed: in ROM?\n",
35df4500 2811 bl->owner->number);
879bfdc2
DJ
2812 }
2813 }
2814 /* Shall we set a breakpoint at the VMA? */
35df4500 2815 if (section_is_mapped (bl->section))
879bfdc2
DJ
2816 {
2817 /* Yes. This overlay section is mapped into memory. */
492d29ea 2818 TRY
dd61ec5c 2819 {
0000e5cc
PA
2820 int val;
2821
dd61ec5c 2822 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2823 if (val)
2824 bp_err = GENERIC_ERROR;
dd61ec5c 2825 }
492d29ea 2826 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2827 {
0000e5cc
PA
2828 bp_err = e.error;
2829 bp_err_message = e.message;
dd61ec5c 2830 }
492d29ea 2831 END_CATCH
879bfdc2
DJ
2832 }
2833 else
2834 {
2835 /* No. This breakpoint will not be inserted.
2836 No error, but do not mark the bp as 'inserted'. */
2837 return 0;
2838 }
2839 }
2840
0000e5cc 2841 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2842 {
2843 /* Can't set the breakpoint. */
0000e5cc
PA
2844
2845 /* In some cases, we might not be able to insert a
2846 breakpoint in a shared library that has already been
2847 removed, but we have not yet processed the shlib unload
2848 event. Unfortunately, some targets that implement
076855f9
PA
2849 breakpoint insertion themselves can't tell why the
2850 breakpoint insertion failed (e.g., the remote target
2851 doesn't define error codes), so we must treat generic
2852 errors as memory errors. */
0000e5cc 2853 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2854 && bl->loc_type == bp_loc_software_breakpoint
08351840 2855 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2856 || shared_objfile_contains_address_p (bl->pspace,
2857 bl->address)))
879bfdc2 2858 {
4a64f543 2859 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2860 bl->shlib_disabled = 1;
8d3788bd 2861 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2862 if (!*disabled_breaks)
2863 {
2864 fprintf_unfiltered (tmp_error_stream,
2865 "Cannot insert breakpoint %d.\n",
2866 bl->owner->number);
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Temporarily disabling shared "
2869 "library breakpoints:\n");
2870 }
2871 *disabled_breaks = 1;
879bfdc2 2872 fprintf_unfiltered (tmp_error_stream,
35df4500 2873 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2874 return 0;
879bfdc2
DJ
2875 }
2876 else
879bfdc2 2877 {
35df4500 2878 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2879 {
0000e5cc
PA
2880 *hw_breakpoint_error = 1;
2881 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2882 fprintf_unfiltered (tmp_error_stream,
2883 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2884 bl->owner->number, bp_err_message ? ":" : ".\n");
2885 if (bp_err_message != NULL)
2886 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2887 }
2888 else
2889 {
0000e5cc
PA
2890 if (bp_err_message == NULL)
2891 {
1ccbe998 2892 std::string message
0000e5cc
PA
2893 = memory_error_message (TARGET_XFER_E_IO,
2894 bl->gdbarch, bl->address);
0000e5cc
PA
2895
2896 fprintf_unfiltered (tmp_error_stream,
2897 "Cannot insert breakpoint %d.\n"
2898 "%s\n",
1ccbe998 2899 bl->owner->number, message.c_str ());
0000e5cc
PA
2900 }
2901 else
2902 {
2903 fprintf_unfiltered (tmp_error_stream,
2904 "Cannot insert breakpoint %d: %s\n",
2905 bl->owner->number,
2906 bp_err_message);
2907 }
879bfdc2 2908 }
0000e5cc 2909 return 1;
879bfdc2
DJ
2910
2911 }
2912 }
2913 else
35df4500 2914 bl->inserted = 1;
879bfdc2 2915
0000e5cc 2916 return 0;
879bfdc2
DJ
2917 }
2918
35df4500 2919 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2920 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2921 watchpoints. It's not clear that it's necessary... */
35df4500 2922 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2923 {
0000e5cc
PA
2924 int val;
2925
77b06cd7
TJB
2926 gdb_assert (bl->owner->ops != NULL
2927 && bl->owner->ops->insert_location != NULL);
2928
2929 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2930
2931 /* If trying to set a read-watchpoint, and it turns out it's not
2932 supported, try emulating one with an access watchpoint. */
35df4500 2933 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2934 {
2935 struct bp_location *loc, **loc_temp;
2936
2937 /* But don't try to insert it, if there's already another
2938 hw_access location that would be considered a duplicate
2939 of this one. */
2940 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2941 if (loc != bl
85d721b8 2942 && loc->watchpoint_type == hw_access
35df4500 2943 && watchpoint_locations_match (bl, loc))
85d721b8 2944 {
35df4500
TJB
2945 bl->duplicate = 1;
2946 bl->inserted = 1;
2947 bl->target_info = loc->target_info;
2948 bl->watchpoint_type = hw_access;
85d721b8
PA
2949 val = 0;
2950 break;
2951 }
2952
2953 if (val == 1)
2954 {
77b06cd7
TJB
2955 bl->watchpoint_type = hw_access;
2956 val = bl->owner->ops->insert_location (bl);
2957
2958 if (val)
2959 /* Back to the original value. */
2960 bl->watchpoint_type = hw_read;
85d721b8
PA
2961 }
2962 }
2963
35df4500 2964 bl->inserted = (val == 0);
879bfdc2
DJ
2965 }
2966
35df4500 2967 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2968 {
0000e5cc
PA
2969 int val;
2970
77b06cd7
TJB
2971 gdb_assert (bl->owner->ops != NULL
2972 && bl->owner->ops->insert_location != NULL);
2973
2974 val = bl->owner->ops->insert_location (bl);
2975 if (val)
2976 {
2977 bl->owner->enable_state = bp_disabled;
2978
2979 if (val == 1)
2980 warning (_("\
2981Error inserting catchpoint %d: Your system does not support this type\n\
2982of catchpoint."), bl->owner->number);
2983 else
2984 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2985 }
2986
2987 bl->inserted = (val == 0);
1640b821
DJ
2988
2989 /* We've already printed an error message if there was a problem
2990 inserting this catchpoint, and we've disabled the catchpoint,
2991 so just return success. */
2992 return 0;
879bfdc2
DJ
2993 }
2994
2995 return 0;
2996}
2997
6c95b8df
PA
2998/* This function is called when program space PSPACE is about to be
2999 deleted. It takes care of updating breakpoints to not reference
3000 PSPACE anymore. */
3001
3002void
3003breakpoint_program_space_exit (struct program_space *pspace)
3004{
3005 struct breakpoint *b, *b_temp;
876fa593 3006 struct bp_location *loc, **loc_temp;
6c95b8df
PA
3007
3008 /* Remove any breakpoint that was set through this program space. */
3009 ALL_BREAKPOINTS_SAFE (b, b_temp)
3010 {
3011 if (b->pspace == pspace)
3012 delete_breakpoint (b);
3013 }
3014
3015 /* Breakpoints set through other program spaces could have locations
3016 bound to PSPACE as well. Remove those. */
876fa593 3017 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
3018 {
3019 struct bp_location *tmp;
3020
3021 if (loc->pspace == pspace)
3022 {
2bdf28a0 3023 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3024 if (loc->owner->loc == loc)
3025 loc->owner->loc = loc->next;
3026 else
3027 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3028 if (tmp->next == loc)
3029 {
3030 tmp->next = loc->next;
3031 break;
3032 }
3033 }
3034 }
3035
3036 /* Now update the global location list to permanently delete the
3037 removed locations above. */
44702360 3038 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3039}
3040
74960c60
VP
3041/* Make sure all breakpoints are inserted in inferior.
3042 Throws exception on any error.
3043 A breakpoint that is already inserted won't be inserted
3044 again, so calling this function twice is safe. */
3045void
3046insert_breakpoints (void)
3047{
3048 struct breakpoint *bpt;
3049
3050 ALL_BREAKPOINTS (bpt)
3051 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3052 {
3053 struct watchpoint *w = (struct watchpoint *) bpt;
3054
3055 update_watchpoint (w, 0 /* don't reparse. */);
3056 }
74960c60 3057
04086b45
PA
3058 /* Updating watchpoints creates new locations, so update the global
3059 location list. Explicitly tell ugll to insert locations and
3060 ignore breakpoints_always_inserted_mode. */
3061 update_global_location_list (UGLL_INSERT);
74960c60
VP
3062}
3063
20388dd6
YQ
3064/* Invoke CALLBACK for each of bp_location. */
3065
3066void
3067iterate_over_bp_locations (walk_bp_location_callback callback)
3068{
3069 struct bp_location *loc, **loc_tmp;
3070
3071 ALL_BP_LOCATIONS (loc, loc_tmp)
3072 {
3073 callback (loc, NULL);
3074 }
3075}
3076
b775012e
LM
3077/* This is used when we need to synch breakpoint conditions between GDB and the
3078 target. It is the case with deleting and disabling of breakpoints when using
3079 always-inserted mode. */
3080
3081static void
3082update_inserted_breakpoint_locations (void)
3083{
3084 struct bp_location *bl, **blp_tmp;
3085 int error_flag = 0;
3086 int val = 0;
3087 int disabled_breaks = 0;
3088 int hw_breakpoint_error = 0;
dd61ec5c 3089 int hw_bp_details_reported = 0;
b775012e 3090
d7e74731 3091 string_file tmp_error_stream;
b775012e
LM
3092
3093 /* Explicitly mark the warning -- this will only be printed if
3094 there was an error. */
d7e74731 3095 tmp_error_stream.puts ("Warning:\n");
b775012e 3096
5ed8105e 3097 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
3098
3099 ALL_BP_LOCATIONS (bl, blp_tmp)
3100 {
3101 /* We only want to update software breakpoints and hardware
3102 breakpoints. */
3103 if (!is_breakpoint (bl->owner))
3104 continue;
3105
3106 /* We only want to update locations that are already inserted
3107 and need updating. This is to avoid unwanted insertion during
3108 deletion of breakpoints. */
3109 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3110 continue;
3111
3112 switch_to_program_space_and_thread (bl->pspace);
3113
3114 /* For targets that support global breakpoints, there's no need
3115 to select an inferior to insert breakpoint to. In fact, even
3116 if we aren't attached to any process yet, we should still
3117 insert breakpoints. */
f5656ead 3118 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3119 && ptid_equal (inferior_ptid, null_ptid))
3120 continue;
3121
d7e74731 3122 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3123 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3124 if (val)
3125 error_flag = val;
3126 }
3127
3128 if (error_flag)
3129 {
3130 target_terminal_ours_for_output ();
3131 error_stream (tmp_error_stream);
3132 }
b775012e
LM
3133}
3134
c30eee59 3135/* Used when starting or continuing the program. */
c906108c 3136
74960c60
VP
3137static void
3138insert_breakpoint_locations (void)
c906108c 3139{
a5606eee 3140 struct breakpoint *bpt;
35df4500 3141 struct bp_location *bl, **blp_tmp;
eacd795a 3142 int error_flag = 0;
c906108c 3143 int val = 0;
3fbb6ffa 3144 int disabled_breaks = 0;
81d0cc19 3145 int hw_breakpoint_error = 0;
dd61ec5c 3146 int hw_bp_error_explained_already = 0;
c906108c 3147
d7e74731
PA
3148 string_file tmp_error_stream;
3149
81d0cc19
GS
3150 /* Explicitly mark the warning -- this will only be printed if
3151 there was an error. */
d7e74731 3152 tmp_error_stream.puts ("Warning:\n");
6c95b8df 3153
5ed8105e 3154 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3155
35df4500 3156 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3157 {
b775012e 3158 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3159 continue;
3160
4a64f543
MS
3161 /* There is no point inserting thread-specific breakpoints if
3162 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3163 has BL->OWNER always non-NULL. */
35df4500 3164 if (bl->owner->thread != -1
5d5658a1 3165 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3166 continue;
3167
35df4500 3168 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3169
3170 /* For targets that support global breakpoints, there's no need
3171 to select an inferior to insert breakpoint to. In fact, even
3172 if we aren't attached to any process yet, we should still
3173 insert breakpoints. */
f5656ead 3174 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3175 && ptid_equal (inferior_ptid, null_ptid))
3176 continue;
3177
d7e74731 3178 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3179 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3180 if (val)
eacd795a 3181 error_flag = val;
879bfdc2 3182 }
c906108c 3183
4a64f543
MS
3184 /* If we failed to insert all locations of a watchpoint, remove
3185 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3186 ALL_BREAKPOINTS (bpt)
3187 {
3188 int some_failed = 0;
3189 struct bp_location *loc;
3190
3191 if (!is_hardware_watchpoint (bpt))
3192 continue;
3193
d6b74ac4 3194 if (!breakpoint_enabled (bpt))
a5606eee 3195 continue;
74960c60
VP
3196
3197 if (bpt->disposition == disp_del_at_next_stop)
3198 continue;
a5606eee
VP
3199
3200 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3201 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3202 {
3203 some_failed = 1;
3204 break;
3205 }
3206 if (some_failed)
3207 {
3208 for (loc = bpt->loc; loc; loc = loc->next)
3209 if (loc->inserted)
834c0d03 3210 remove_breakpoint (loc);
a5606eee
VP
3211
3212 hw_breakpoint_error = 1;
d7e74731
PA
3213 tmp_error_stream.printf ("Could not insert "
3214 "hardware watchpoint %d.\n",
3215 bpt->number);
eacd795a 3216 error_flag = -1;
a5606eee
VP
3217 }
3218 }
3219
eacd795a 3220 if (error_flag)
81d0cc19
GS
3221 {
3222 /* If a hardware breakpoint or watchpoint was inserted, add a
3223 message about possibly exhausted resources. */
dd61ec5c 3224 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3225 {
d7e74731 3226 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3227You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3228 }
81d0cc19
GS
3229 target_terminal_ours_for_output ();
3230 error_stream (tmp_error_stream);
3231 }
c906108c
SS
3232}
3233
c30eee59
TJB
3234/* Used when the program stops.
3235 Returns zero if successful, or non-zero if there was a problem
3236 removing a breakpoint location. */
3237
c906108c 3238int
fba45db2 3239remove_breakpoints (void)
c906108c 3240{
35df4500 3241 struct bp_location *bl, **blp_tmp;
3a1bae8e 3242 int val = 0;
c906108c 3243
35df4500 3244 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3245 {
1e4d1764 3246 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3247 val |= remove_breakpoint (bl);
c5aa993b 3248 }
3a1bae8e 3249 return val;
c906108c
SS
3250}
3251
49fa26b0
PA
3252/* When a thread exits, remove breakpoints that are related to
3253 that thread. */
3254
3255static void
3256remove_threaded_breakpoints (struct thread_info *tp, int silent)
3257{
3258 struct breakpoint *b, *b_tmp;
3259
3260 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3261 {
5d5658a1 3262 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3263 {
3264 b->disposition = disp_del_at_next_stop;
3265
3266 printf_filtered (_("\
43792cf0
PA
3267Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3268 b->number, print_thread_id (tp));
49fa26b0
PA
3269
3270 /* Hide it from the user. */
3271 b->number = 0;
3272 }
3273 }
3274}
3275
6c95b8df
PA
3276/* Remove breakpoints of process PID. */
3277
3278int
3279remove_breakpoints_pid (int pid)
3280{
35df4500 3281 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3282 int val;
3283 struct inferior *inf = find_inferior_pid (pid);
3284
35df4500 3285 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3286 {
35df4500 3287 if (bl->pspace != inf->pspace)
6c95b8df
PA
3288 continue;
3289
fc126975 3290 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3291 {
834c0d03 3292 val = remove_breakpoint (bl);
6c95b8df
PA
3293 if (val != 0)
3294 return val;
3295 }
3296 }
3297 return 0;
3298}
3299
c906108c 3300int
fba45db2 3301reattach_breakpoints (int pid)
c906108c 3302{
35df4500 3303 struct bp_location *bl, **blp_tmp;
c906108c 3304 int val;
dd61ec5c 3305 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3306 struct inferior *inf;
3307 struct thread_info *tp;
3308
3309 tp = any_live_thread_of_process (pid);
3310 if (tp == NULL)
3311 return 1;
3312
3313 inf = find_inferior_pid (pid);
6c95b8df 3314
2989a365 3315 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3316 inferior_ptid = tp->ptid;
a4954f26 3317
d7e74731 3318 string_file tmp_error_stream;
c906108c 3319
35df4500 3320 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3321 {
35df4500 3322 if (bl->pspace != inf->pspace)
6c95b8df
PA
3323 continue;
3324
35df4500 3325 if (bl->inserted)
c5aa993b 3326 {
35df4500 3327 bl->inserted = 0;
d7e74731 3328 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b 3329 if (val != 0)
2989a365 3330 return val;
c5aa993b
JM
3331 }
3332 }
c906108c
SS
3333 return 0;
3334}
3335
e58b0e63
PA
3336static int internal_breakpoint_number = -1;
3337
84f4c1fe
PM
3338/* Set the breakpoint number of B, depending on the value of INTERNAL.
3339 If INTERNAL is non-zero, the breakpoint number will be populated
3340 from internal_breakpoint_number and that variable decremented.
e5dd4106 3341 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3342 breakpoint_count and that value incremented. Internal breakpoints
3343 do not set the internal var bpnum. */
3344static void
3345set_breakpoint_number (int internal, struct breakpoint *b)
3346{
3347 if (internal)
3348 b->number = internal_breakpoint_number--;
3349 else
3350 {
3351 set_breakpoint_count (breakpoint_count + 1);
3352 b->number = breakpoint_count;
3353 }
3354}
3355
e62c965a 3356static struct breakpoint *
a6d9a66e 3357create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3358 CORE_ADDR address, enum bptype type,
c0a91b2b 3359 const struct breakpoint_ops *ops)
e62c965a 3360{
e62c965a
PP
3361 struct symtab_and_line sal;
3362 struct breakpoint *b;
3363
4a64f543 3364 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3365
3366 sal.pc = address;
3367 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3368 sal.pspace = current_program_space;
e62c965a 3369
06edf0c0 3370 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3371 b->number = internal_breakpoint_number--;
3372 b->disposition = disp_donttouch;
3373
3374 return b;
3375}
3376
17450429
PP
3377static const char *const longjmp_names[] =
3378 {
3379 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3380 };
3381#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3382
3383/* Per-objfile data private to breakpoint.c. */
3384struct breakpoint_objfile_data
3385{
3386 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3387 struct bound_minimal_symbol overlay_msym;
17450429
PP
3388
3389 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3390 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3391
28106bc2
SDJ
3392 /* True if we have looked for longjmp probes. */
3393 int longjmp_searched;
3394
3395 /* SystemTap probe points for longjmp (if any). */
3396 VEC (probe_p) *longjmp_probes;
3397
17450429 3398 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3399 struct bound_minimal_symbol terminate_msym;
17450429
PP
3400
3401 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3402 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3403
3404 /* True if we have looked for exception probes. */
3405 int exception_searched;
3406
3407 /* SystemTap probe points for unwinding (if any). */
3408 VEC (probe_p) *exception_probes;
17450429
PP
3409};
3410
3411static const struct objfile_data *breakpoint_objfile_key;
3412
3413/* Minimal symbol not found sentinel. */
3414static struct minimal_symbol msym_not_found;
3415
3416/* Returns TRUE if MSYM point to the "not found" sentinel. */
3417
3418static int
3419msym_not_found_p (const struct minimal_symbol *msym)
3420{
3421 return msym == &msym_not_found;
3422}
3423
3424/* Return per-objfile data needed by breakpoint.c.
3425 Allocate the data if necessary. */
3426
3427static struct breakpoint_objfile_data *
3428get_breakpoint_objfile_data (struct objfile *objfile)
3429{
3430 struct breakpoint_objfile_data *bp_objfile_data;
3431
9a3c8263
SM
3432 bp_objfile_data = ((struct breakpoint_objfile_data *)
3433 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3434 if (bp_objfile_data == NULL)
3435 {
8d749320
SM
3436 bp_objfile_data =
3437 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3438
3439 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3440 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3441 }
3442 return bp_objfile_data;
3443}
3444
28106bc2
SDJ
3445static void
3446free_breakpoint_probes (struct objfile *obj, void *data)
3447{
9a3c8263
SM
3448 struct breakpoint_objfile_data *bp_objfile_data
3449 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3450
3451 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3452 VEC_free (probe_p, bp_objfile_data->exception_probes);
3453}
3454
e62c965a 3455static void
af02033e 3456create_overlay_event_breakpoint (void)
e62c965a 3457{
69de3c6a 3458 struct objfile *objfile;
af02033e 3459 const char *const func_name = "_ovly_debug_event";
e62c965a 3460
69de3c6a
PP
3461 ALL_OBJFILES (objfile)
3462 {
3463 struct breakpoint *b;
17450429
PP
3464 struct breakpoint_objfile_data *bp_objfile_data;
3465 CORE_ADDR addr;
67994074 3466 struct explicit_location explicit_loc;
69de3c6a 3467
17450429
PP
3468 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3469
3b7344d5 3470 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3471 continue;
3472
3b7344d5 3473 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3474 {
3b7344d5 3475 struct bound_minimal_symbol m;
17450429
PP
3476
3477 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3478 if (m.minsym == NULL)
17450429
PP
3479 {
3480 /* Avoid future lookups in this objfile. */
3b7344d5 3481 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3482 continue;
3483 }
3484 bp_objfile_data->overlay_msym = m;
3485 }
e62c965a 3486
77e371c0 3487 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3488 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3489 bp_overlay_event,
3490 &internal_breakpoint_ops);
67994074
KS
3491 initialize_explicit_location (&explicit_loc);
3492 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3493 b->location = new_explicit_location (&explicit_loc);
e62c965a 3494
69de3c6a
PP
3495 if (overlay_debugging == ovly_auto)
3496 {
3497 b->enable_state = bp_enabled;
3498 overlay_events_enabled = 1;
3499 }
3500 else
3501 {
3502 b->enable_state = bp_disabled;
3503 overlay_events_enabled = 0;
3504 }
e62c965a 3505 }
e62c965a
PP
3506}
3507
0fd8e87f 3508static void
af02033e 3509create_longjmp_master_breakpoint (void)
0fd8e87f 3510{
6c95b8df 3511 struct program_space *pspace;
6c95b8df 3512
5ed8105e 3513 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3514
6c95b8df 3515 ALL_PSPACES (pspace)
af02033e
PP
3516 {
3517 struct objfile *objfile;
3518
3519 set_current_program_space (pspace);
3520
3521 ALL_OBJFILES (objfile)
0fd8e87f 3522 {
af02033e
PP
3523 int i;
3524 struct gdbarch *gdbarch;
17450429 3525 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3526
af02033e 3527 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3528
17450429
PP
3529 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3530
28106bc2
SDJ
3531 if (!bp_objfile_data->longjmp_searched)
3532 {
25f9533e
SDJ
3533 VEC (probe_p) *ret;
3534
3535 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3536 if (ret != NULL)
3537 {
3538 /* We are only interested in checking one element. */
3539 struct probe *p = VEC_index (probe_p, ret, 0);
3540
3541 if (!can_evaluate_probe_arguments (p))
3542 {
3543 /* We cannot use the probe interface here, because it does
3544 not know how to evaluate arguments. */
3545 VEC_free (probe_p, ret);
3546 ret = NULL;
3547 }
3548 }
3549 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3550 bp_objfile_data->longjmp_searched = 1;
3551 }
3552
3553 if (bp_objfile_data->longjmp_probes != NULL)
3554 {
3555 int i;
3556 struct probe *probe;
3557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3558
3559 for (i = 0;
3560 VEC_iterate (probe_p,
3561 bp_objfile_data->longjmp_probes,
3562 i, probe);
3563 ++i)
3564 {
3565 struct breakpoint *b;
3566
729662a5
TT
3567 b = create_internal_breakpoint (gdbarch,
3568 get_probe_address (probe,
3569 objfile),
28106bc2
SDJ
3570 bp_longjmp_master,
3571 &internal_breakpoint_ops);
d28cd78a 3572 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3573 b->enable_state = bp_disabled;
3574 }
3575
3576 continue;
3577 }
3578
0569175e
TSD
3579 if (!gdbarch_get_longjmp_target_p (gdbarch))
3580 continue;
3581
17450429 3582 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3583 {
3584 struct breakpoint *b;
af02033e 3585 const char *func_name;
17450429 3586 CORE_ADDR addr;
67994074 3587 struct explicit_location explicit_loc;
6c95b8df 3588
3b7344d5 3589 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3590 continue;
0fd8e87f 3591
17450429 3592 func_name = longjmp_names[i];
3b7344d5 3593 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3594 {
3b7344d5 3595 struct bound_minimal_symbol m;
17450429
PP
3596
3597 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3598 if (m.minsym == NULL)
17450429
PP
3599 {
3600 /* Prevent future lookups in this objfile. */
3b7344d5 3601 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3602 continue;
3603 }
3604 bp_objfile_data->longjmp_msym[i] = m;
3605 }
3606
77e371c0 3607 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3608 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3609 &internal_breakpoint_ops);
67994074
KS
3610 initialize_explicit_location (&explicit_loc);
3611 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3612 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3613 b->enable_state = bp_disabled;
3614 }
0fd8e87f 3615 }
af02033e 3616 }
0fd8e87f
UW
3617}
3618
af02033e 3619/* Create a master std::terminate breakpoint. */
aa7d318d 3620static void
af02033e 3621create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3622{
3623 struct program_space *pspace;
af02033e 3624 const char *const func_name = "std::terminate()";
aa7d318d 3625
5ed8105e 3626 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3627
3628 ALL_PSPACES (pspace)
17450429
PP
3629 {
3630 struct objfile *objfile;
3631 CORE_ADDR addr;
3632
3633 set_current_program_space (pspace);
3634
aa7d318d
TT
3635 ALL_OBJFILES (objfile)
3636 {
3637 struct breakpoint *b;
17450429 3638 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3639 struct explicit_location explicit_loc;
aa7d318d 3640
17450429 3641 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3642
3b7344d5 3643 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3644 continue;
3645
3b7344d5 3646 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3647 {
3b7344d5 3648 struct bound_minimal_symbol m;
17450429
PP
3649
3650 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3651 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3652 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3653 {
3654 /* Prevent future lookups in this objfile. */
3b7344d5 3655 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3656 continue;
3657 }
3658 bp_objfile_data->terminate_msym = m;
3659 }
aa7d318d 3660
77e371c0 3661 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3662 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3663 bp_std_terminate_master,
3664 &internal_breakpoint_ops);
67994074
KS
3665 initialize_explicit_location (&explicit_loc);
3666 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3667 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3668 b->enable_state = bp_disabled;
3669 }
17450429 3670 }
aa7d318d
TT
3671}
3672
186c406b
TT
3673/* Install a master breakpoint on the unwinder's debug hook. */
3674
70221824 3675static void
186c406b
TT
3676create_exception_master_breakpoint (void)
3677{
3678 struct objfile *objfile;
17450429 3679 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3680
3681 ALL_OBJFILES (objfile)
3682 {
17450429
PP
3683 struct breakpoint *b;
3684 struct gdbarch *gdbarch;
3685 struct breakpoint_objfile_data *bp_objfile_data;
3686 CORE_ADDR addr;
67994074 3687 struct explicit_location explicit_loc;
17450429
PP
3688
3689 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3690
28106bc2
SDJ
3691 /* We prefer the SystemTap probe point if it exists. */
3692 if (!bp_objfile_data->exception_searched)
3693 {
25f9533e
SDJ
3694 VEC (probe_p) *ret;
3695
3696 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3697
3698 if (ret != NULL)
3699 {
3700 /* We are only interested in checking one element. */
3701 struct probe *p = VEC_index (probe_p, ret, 0);
3702
3703 if (!can_evaluate_probe_arguments (p))
3704 {
3705 /* We cannot use the probe interface here, because it does
3706 not know how to evaluate arguments. */
3707 VEC_free (probe_p, ret);
3708 ret = NULL;
3709 }
3710 }
3711 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3712 bp_objfile_data->exception_searched = 1;
3713 }
3714
3715 if (bp_objfile_data->exception_probes != NULL)
3716 {
3717 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3718 int i;
3719 struct probe *probe;
3720
3721 for (i = 0;
3722 VEC_iterate (probe_p,
3723 bp_objfile_data->exception_probes,
3724 i, probe);
3725 ++i)
3726 {
3727 struct breakpoint *b;
3728
729662a5
TT
3729 b = create_internal_breakpoint (gdbarch,
3730 get_probe_address (probe,
3731 objfile),
28106bc2
SDJ
3732 bp_exception_master,
3733 &internal_breakpoint_ops);
d28cd78a 3734 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3735 b->enable_state = bp_disabled;
3736 }
3737
3738 continue;
3739 }
3740
3741 /* Otherwise, try the hook function. */
3742
3b7344d5 3743 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3744 continue;
3745
3746 gdbarch = get_objfile_arch (objfile);
186c406b 3747
3b7344d5 3748 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3749 {
3b7344d5 3750 struct bound_minimal_symbol debug_hook;
186c406b 3751
17450429 3752 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3753 if (debug_hook.minsym == NULL)
17450429 3754 {
3b7344d5 3755 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3756 continue;
3757 }
3758
3759 bp_objfile_data->exception_msym = debug_hook;
186c406b 3760 }
17450429 3761
77e371c0 3762 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3763 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3764 &current_target);
06edf0c0
PA
3765 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3766 &internal_breakpoint_ops);
67994074
KS
3767 initialize_explicit_location (&explicit_loc);
3768 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3769 b->location = new_explicit_location (&explicit_loc);
17450429 3770 b->enable_state = bp_disabled;
186c406b 3771 }
186c406b
TT
3772}
3773
9ef9e6a6
KS
3774/* Does B have a location spec? */
3775
3776static int
3777breakpoint_event_location_empty_p (const struct breakpoint *b)
3778{
d28cd78a 3779 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3780}
3781
c906108c 3782void
fba45db2 3783update_breakpoints_after_exec (void)
c906108c 3784{
35df4500 3785 struct breakpoint *b, *b_tmp;
876fa593 3786 struct bp_location *bploc, **bplocp_tmp;
c906108c 3787
25b22b0a
PA
3788 /* We're about to delete breakpoints from GDB's lists. If the
3789 INSERTED flag is true, GDB will try to lift the breakpoints by
3790 writing the breakpoints' "shadow contents" back into memory. The
3791 "shadow contents" are NOT valid after an exec, so GDB should not
3792 do that. Instead, the target is responsible from marking
3793 breakpoints out as soon as it detects an exec. We don't do that
3794 here instead, because there may be other attempts to delete
3795 breakpoints after detecting an exec and before reaching here. */
876fa593 3796 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3797 if (bploc->pspace == current_program_space)
3798 gdb_assert (!bploc->inserted);
c906108c 3799
35df4500 3800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3801 {
6c95b8df
PA
3802 if (b->pspace != current_program_space)
3803 continue;
3804
4a64f543 3805 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3806 if (b->type == bp_shlib_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
c906108c 3811
4a64f543 3812 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3813 if (b->type == bp_jit_event)
3814 {
3815 delete_breakpoint (b);
3816 continue;
3817 }
3818
1900040c 3819 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3820 as must overlay event and longjmp master breakpoints. */
3821 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3822 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3823 || b->type == bp_exception_master)
c4093a6a
JM
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
4a64f543 3829 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3830 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
7c16b83e
PA
3836 /* Just like single-step breakpoints. */
3837 if (b->type == bp_single_step)
3838 {
3839 delete_breakpoint (b);
3840 continue;
3841 }
3842
611c83ae
PA
3843 /* Longjmp and longjmp-resume breakpoints are also meaningless
3844 after an exec. */
186c406b 3845 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3846 || b->type == bp_longjmp_call_dummy
186c406b 3847 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3848 {
3849 delete_breakpoint (b);
3850 continue;
3851 }
3852
ce78b96d
JB
3853 if (b->type == bp_catchpoint)
3854 {
3855 /* For now, none of the bp_catchpoint breakpoints need to
3856 do anything at this point. In the future, if some of
3857 the catchpoints need to something, we will need to add
3858 a new method, and call this method from here. */
3859 continue;
3860 }
3861
c5aa993b
JM
3862 /* bp_finish is a special case. The only way we ought to be able
3863 to see one of these when an exec() has happened, is if the user
3864 caught a vfork, and then said "finish". Ordinarily a finish just
3865 carries them to the call-site of the current callee, by setting
3866 a temporary bp there and resuming. But in this case, the finish
3867 will carry them entirely through the vfork & exec.
3868
3869 We don't want to allow a bp_finish to remain inserted now. But
3870 we can't safely delete it, 'cause finish_command has a handle to
3871 the bp on a bpstat, and will later want to delete it. There's a
3872 chance (and I've seen it happen) that if we delete the bp_finish
3873 here, that its storage will get reused by the time finish_command
3874 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3875 We really must allow finish_command to delete a bp_finish.
3876
e5dd4106 3877 In the absence of a general solution for the "how do we know
53a5351d
JM
3878 it's safe to delete something others may have handles to?"
3879 problem, what we'll do here is just uninsert the bp_finish, and
3880 let finish_command delete it.
3881
3882 (We know the bp_finish is "doomed" in the sense that it's
3883 momentary, and will be deleted as soon as finish_command sees
3884 the inferior stopped. So it doesn't matter that the bp's
3885 address is probably bogus in the new a.out, unlike e.g., the
3886 solib breakpoints.) */
c5aa993b 3887
c5aa993b
JM
3888 if (b->type == bp_finish)
3889 {
3890 continue;
3891 }
3892
3893 /* Without a symbolic address, we have little hope of the
3894 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3895 a.out. */
9ef9e6a6 3896 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3897 {
3898 delete_breakpoint (b);
3899 continue;
3900 }
c5aa993b 3901 }
c906108c
SS
3902}
3903
3904int
d80ee84f 3905detach_breakpoints (ptid_t ptid)
c906108c 3906{
35df4500 3907 struct bp_location *bl, **blp_tmp;
3a1bae8e 3908 int val = 0;
2989a365 3909 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3910 struct inferior *inf = current_inferior ();
c5aa993b 3911
dfd4cc63 3912 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3913 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3914
6c95b8df 3915 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3916 inferior_ptid = ptid;
35df4500 3917 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3918 {
35df4500 3919 if (bl->pspace != inf->pspace)
6c95b8df
PA
3920 continue;
3921
bd9673a4
PW
3922 /* This function must physically remove breakpoints locations
3923 from the specified ptid, without modifying the breakpoint
3924 package's state. Locations of type bp_loc_other are only
3925 maintained at GDB side. So, there is no need to remove
3926 these bp_loc_other locations. Moreover, removing these
3927 would modify the breakpoint package's state. */
3928 if (bl->loc_type == bp_loc_other)
3929 continue;
3930
35df4500 3931 if (bl->inserted)
b2b6a7da 3932 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3933 }
d03285ec 3934
3a1bae8e 3935 return val;
c906108c
SS
3936}
3937
35df4500 3938/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3939 Note that this is used to detach breakpoints from a child fork.
3940 When we get here, the child isn't in the inferior list, and neither
3941 do we have objects to represent its address space --- we should
35df4500 3942 *not* look at bl->pspace->aspace here. */
6c95b8df 3943
c906108c 3944static int
b2b6a7da 3945remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3946{
3947 int val;
c5aa993b 3948
35df4500
TJB
3949 /* BL is never in moribund_locations by our callers. */
3950 gdb_assert (bl->owner != NULL);
2bdf28a0 3951
74960c60
VP
3952 /* The type of none suggests that owner is actually deleted.
3953 This should not ever happen. */
35df4500 3954 gdb_assert (bl->owner->type != bp_none);
0bde7532 3955
35df4500
TJB
3956 if (bl->loc_type == bp_loc_software_breakpoint
3957 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3958 {
c02f5703
MS
3959 /* "Normal" instruction breakpoint: either the standard
3960 trap-instruction bp (bp_breakpoint), or a
3961 bp_hardware_breakpoint. */
3962
3963 /* First check to see if we have to handle an overlay. */
3964 if (overlay_debugging == ovly_off
35df4500
TJB
3965 || bl->section == NULL
3966 || !(section_is_overlay (bl->section)))
c02f5703
MS
3967 {
3968 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3969
3970 /* If we're trying to uninsert a memory breakpoint that we
3971 know is set in a dynamic object that is marked
3972 shlib_disabled, then either the dynamic object was
3973 removed with "remove-symbol-file" or with
3974 "nosharedlibrary". In the former case, we don't know
3975 whether another dynamic object might have loaded over the
3976 breakpoint's address -- the user might well let us know
3977 about it next with add-symbol-file (the whole point of
d03de421 3978 add-symbol-file is letting the user manually maintain a
08351840
PA
3979 list of dynamically loaded objects). If we have the
3980 breakpoint's shadow memory, that is, this is a software
3981 breakpoint managed by GDB, check whether the breakpoint
3982 is still inserted in memory, to avoid overwriting wrong
3983 code with stale saved shadow contents. Note that HW
3984 breakpoints don't have shadow memory, as they're
3985 implemented using a mechanism that is not dependent on
3986 being able to modify the target's memory, and as such
3987 they should always be removed. */
3988 if (bl->shlib_disabled
3989 && bl->target_info.shadow_len != 0
3990 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3991 val = 0;
3992 else
73971819 3993 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3994 }
c906108c
SS
3995 else
3996 {
4a64f543 3997 /* This breakpoint is in an overlay section.
c02f5703
MS
3998 Did we set a breakpoint at the LMA? */
3999 if (!overlay_events_enabled)
4000 {
4001 /* Yes -- overlay event support is not active, so we
4002 should have set a breakpoint at the LMA. Remove it.
4003 */
c02f5703
MS
4004 /* Ignore any failures: if the LMA is in ROM, we will
4005 have already warned when we failed to insert it. */
35df4500
TJB
4006 if (bl->loc_type == bp_loc_hardware_breakpoint)
4007 target_remove_hw_breakpoint (bl->gdbarch,
4008 &bl->overlay_target_info);
c02f5703 4009 else
35df4500 4010 target_remove_breakpoint (bl->gdbarch,
73971819
PA
4011 &bl->overlay_target_info,
4012 reason);
c02f5703
MS
4013 }
4014 /* Did we set a breakpoint at the VMA?
4015 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4016 if (bl->inserted)
c906108c 4017 {
c02f5703
MS
4018 /* Yes -- remove it. Previously we did not bother to
4019 remove the breakpoint if the section had been
4020 unmapped, but let's not rely on that being safe. We
4021 don't know what the overlay manager might do. */
aa67235e
UW
4022
4023 /* However, we should remove *software* breakpoints only
4024 if the section is still mapped, or else we overwrite
4025 wrong code with the saved shadow contents. */
348d480f
PA
4026 if (bl->loc_type == bp_loc_hardware_breakpoint
4027 || section_is_mapped (bl->section))
73971819 4028 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
4029 else
4030 val = 0;
c906108c 4031 }
c02f5703
MS
4032 else
4033 {
4034 /* No -- not inserted, so no need to remove. No error. */
4035 val = 0;
4036 }
c906108c 4037 }
879d1e6b 4038
08351840
PA
4039 /* In some cases, we might not be able to remove a breakpoint in
4040 a shared library that has already been removed, but we have
4041 not yet processed the shlib unload event. Similarly for an
4042 unloaded add-symbol-file object - the user might not yet have
4043 had the chance to remove-symbol-file it. shlib_disabled will
4044 be set if the library/object has already been removed, but
4045 the breakpoint hasn't been uninserted yet, e.g., after
4046 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4047 always-inserted mode. */
076855f9 4048 if (val
08351840
PA
4049 && (bl->loc_type == bp_loc_software_breakpoint
4050 && (bl->shlib_disabled
4051 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4052 || shared_objfile_contains_address_p (bl->pspace,
4053 bl->address))))
879d1e6b
UW
4054 val = 0;
4055
c906108c
SS
4056 if (val)
4057 return val;
b2b6a7da 4058 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 4059 }
35df4500 4060 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4061 {
77b06cd7
TJB
4062 gdb_assert (bl->owner->ops != NULL
4063 && bl->owner->ops->remove_location != NULL);
4064
b2b6a7da 4065 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 4066 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 4067
c906108c 4068 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 4069 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 4070 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4071 bl->owner->number);
c906108c 4072 }
35df4500
TJB
4073 else if (bl->owner->type == bp_catchpoint
4074 && breakpoint_enabled (bl->owner)
4075 && !bl->duplicate)
ce78b96d 4076 {
77b06cd7
TJB
4077 gdb_assert (bl->owner->ops != NULL
4078 && bl->owner->ops->remove_location != NULL);
ce78b96d 4079
73971819 4080 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
4081 if (val)
4082 return val;
77b06cd7 4083
b2b6a7da 4084 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 4085 }
c906108c
SS
4086
4087 return 0;
4088}
4089
6c95b8df 4090static int
834c0d03 4091remove_breakpoint (struct bp_location *bl)
6c95b8df 4092{
35df4500
TJB
4093 /* BL is never in moribund_locations by our callers. */
4094 gdb_assert (bl->owner != NULL);
2bdf28a0 4095
6c95b8df
PA
4096 /* The type of none suggests that owner is actually deleted.
4097 This should not ever happen. */
35df4500 4098 gdb_assert (bl->owner->type != bp_none);
6c95b8df 4099
5ed8105e 4100 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 4101
35df4500 4102 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4103
5ed8105e 4104 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
4105}
4106
c906108c
SS
4107/* Clear the "inserted" flag in all breakpoints. */
4108
25b22b0a 4109void
fba45db2 4110mark_breakpoints_out (void)
c906108c 4111{
35df4500 4112 struct bp_location *bl, **blp_tmp;
c906108c 4113
35df4500 4114 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4115 if (bl->pspace == current_program_space)
35df4500 4116 bl->inserted = 0;
c906108c
SS
4117}
4118
53a5351d
JM
4119/* Clear the "inserted" flag in all breakpoints and delete any
4120 breakpoints which should go away between runs of the program.
c906108c
SS
4121
4122 Plus other such housekeeping that has to be done for breakpoints
4123 between runs.
4124
53a5351d
JM
4125 Note: this function gets called at the end of a run (by
4126 generic_mourn_inferior) and when a run begins (by
4a64f543 4127 init_wait_for_inferior). */
c906108c
SS
4128
4129
4130
4131void
fba45db2 4132breakpoint_init_inferior (enum inf_context context)
c906108c 4133{
35df4500 4134 struct breakpoint *b, *b_tmp;
870f88f7 4135 struct bp_location *bl;
1c5cfe86 4136 int ix;
6c95b8df 4137 struct program_space *pspace = current_program_space;
c906108c 4138
50c71eaf
PA
4139 /* If breakpoint locations are shared across processes, then there's
4140 nothing to do. */
f5656ead 4141 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4142 return;
4143
1a853c52 4144 mark_breakpoints_out ();
075f6582 4145
35df4500 4146 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4147 {
6c95b8df
PA
4148 if (b->loc && b->loc->pspace != pspace)
4149 continue;
4150
c5aa993b
JM
4151 switch (b->type)
4152 {
4153 case bp_call_dummy:
e2e4d78b 4154 case bp_longjmp_call_dummy:
c906108c 4155
c5aa993b 4156 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4157 cause problems when the inferior is rerun, so we better get
4158 rid of it. */
4159
4160 case bp_watchpoint_scope:
4161
4162 /* Also get rid of scope breakpoints. */
4163
4164 case bp_shlib_event:
4165
4166 /* Also remove solib event breakpoints. Their addresses may
4167 have changed since the last time we ran the program.
4168 Actually we may now be debugging against different target;
4169 and so the solib backend that installed this breakpoint may
4170 not be used in by the target. E.g.,
4171
4172 (gdb) file prog-linux
4173 (gdb) run # native linux target
4174 ...
4175 (gdb) kill
4176 (gdb) file prog-win.exe
4177 (gdb) tar rem :9999 # remote Windows gdbserver.
4178 */
c906108c 4179
f59f708a
PA
4180 case bp_step_resume:
4181
4182 /* Also remove step-resume breakpoints. */
4183
7c16b83e
PA
4184 case bp_single_step:
4185
4186 /* Also remove single-step breakpoints. */
4187
c5aa993b
JM
4188 delete_breakpoint (b);
4189 break;
c906108c 4190
c5aa993b
JM
4191 case bp_watchpoint:
4192 case bp_hardware_watchpoint:
4193 case bp_read_watchpoint:
4194 case bp_access_watchpoint:
3a5c3e22
PA
4195 {
4196 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4197
3a5c3e22
PA
4198 /* Likewise for watchpoints on local expressions. */
4199 if (w->exp_valid_block != NULL)
4200 delete_breakpoint (b);
63000888 4201 else
3a5c3e22 4202 {
63000888
PA
4203 /* Get rid of existing locations, which are no longer
4204 valid. New ones will be created in
4205 update_watchpoint, when the inferior is restarted.
4206 The next update_global_location_list call will
4207 garbage collect them. */
4208 b->loc = NULL;
4209
4210 if (context == inf_starting)
4211 {
4212 /* Reset val field to force reread of starting value in
4213 insert_breakpoints. */
4214 if (w->val)
4215 value_free (w->val);
4216 w->val = NULL;
4217 w->val_valid = 0;
4218 }
4219 }
3a5c3e22 4220 }
c5aa993b
JM
4221 break;
4222 default:
c5aa993b
JM
4223 break;
4224 }
4225 }
1c5cfe86
PA
4226
4227 /* Get rid of the moribund locations. */
35df4500
TJB
4228 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4229 decref_bp_location (&bl);
1c5cfe86 4230 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4231}
4232
6c95b8df
PA
4233/* These functions concern about actual breakpoints inserted in the
4234 target --- to e.g. check if we need to do decr_pc adjustment or if
4235 we need to hop over the bkpt --- so we check for address space
4236 match, not program space. */
4237
c2c6d25f
JM
4238/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4239 exists at PC. It returns ordinary_breakpoint_here if it's an
4240 ordinary breakpoint, or permanent_breakpoint_here if it's a
4241 permanent breakpoint.
4242 - When continuing from a location with an ordinary breakpoint, we
4243 actually single step once before calling insert_breakpoints.
e5dd4106 4244 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4245 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4246 the target, to advance the PC past the breakpoint. */
c906108c 4247
c2c6d25f 4248enum breakpoint_here
6c95b8df 4249breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4250{
35df4500 4251 struct bp_location *bl, **blp_tmp;
c2c6d25f 4252 int any_breakpoint_here = 0;
c906108c 4253
35df4500 4254 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4255 {
35df4500
TJB
4256 if (bl->loc_type != bp_loc_software_breakpoint
4257 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4258 continue;
4259
f1310107 4260 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4261 if ((breakpoint_enabled (bl->owner)
1a853c52 4262 || bl->permanent)
f1310107 4263 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4264 {
4265 if (overlay_debugging
35df4500
TJB
4266 && section_is_overlay (bl->section)
4267 && !section_is_mapped (bl->section))
075f6582 4268 continue; /* unmapped overlay -- can't be a match */
1a853c52 4269 else if (bl->permanent)
075f6582
DJ
4270 return permanent_breakpoint_here;
4271 else
4272 any_breakpoint_here = 1;
4273 }
4274 }
c906108c 4275
f486487f 4276 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4277}
4278
d35ae833
PA
4279/* See breakpoint.h. */
4280
4281int
4282breakpoint_in_range_p (struct address_space *aspace,
4283 CORE_ADDR addr, ULONGEST len)
4284{
4285 struct bp_location *bl, **blp_tmp;
4286
4287 ALL_BP_LOCATIONS (bl, blp_tmp)
4288 {
4289 if (bl->loc_type != bp_loc_software_breakpoint
4290 && bl->loc_type != bp_loc_hardware_breakpoint)
4291 continue;
4292
4293 if ((breakpoint_enabled (bl->owner)
4294 || bl->permanent)
4295 && breakpoint_location_address_range_overlap (bl, aspace,
4296 addr, len))
4297 {
4298 if (overlay_debugging
4299 && section_is_overlay (bl->section)
4300 && !section_is_mapped (bl->section))
4301 {
4302 /* Unmapped overlay -- can't be a match. */
4303 continue;
4304 }
4305
4306 return 1;
4307 }
4308 }
4309
4310 return 0;
4311}
4312
1c5cfe86
PA
4313/* Return true if there's a moribund breakpoint at PC. */
4314
4315int
6c95b8df 4316moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4317{
4318 struct bp_location *loc;
4319 int ix;
4320
4321 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4322 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4323 return 1;
4324
4325 return 0;
4326}
c2c6d25f 4327
f7ce857f
PA
4328/* Returns non-zero iff BL is inserted at PC, in address space
4329 ASPACE. */
4330
4331static int
4332bp_location_inserted_here_p (struct bp_location *bl,
4333 struct address_space *aspace, CORE_ADDR pc)
4334{
4335 if (bl->inserted
4336 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4337 aspace, pc))
4338 {
4339 if (overlay_debugging
4340 && section_is_overlay (bl->section)
4341 && !section_is_mapped (bl->section))
4342 return 0; /* unmapped overlay -- can't be a match */
4343 else
4344 return 1;
4345 }
4346 return 0;
4347}
4348
a1fd2fa5 4349/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4350
4351int
a1fd2fa5 4352breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4353{
f7ce857f 4354 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4355
f7ce857f 4356 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4357 {
f7ce857f
PA
4358 struct bp_location *bl = *blp;
4359
35df4500
TJB
4360 if (bl->loc_type != bp_loc_software_breakpoint
4361 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4362 continue;
4363
f7ce857f
PA
4364 if (bp_location_inserted_here_p (bl, aspace, pc))
4365 return 1;
c5aa993b 4366 }
c36b740a
VP
4367 return 0;
4368}
4369
a1fd2fa5
PA
4370/* This function returns non-zero iff there is a software breakpoint
4371 inserted at PC. */
c36b740a
VP
4372
4373int
a1fd2fa5
PA
4374software_breakpoint_inserted_here_p (struct address_space *aspace,
4375 CORE_ADDR pc)
4fa8626c 4376{
f7ce857f 4377 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4378
f7ce857f 4379 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4380 {
f7ce857f
PA
4381 struct bp_location *bl = *blp;
4382
35df4500 4383 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4384 continue;
4385
f7ce857f
PA
4386 if (bp_location_inserted_here_p (bl, aspace, pc))
4387 return 1;
4fa8626c
DJ
4388 }
4389
4390 return 0;
9c02b525
PA
4391}
4392
4393/* See breakpoint.h. */
4394
4395int
4396hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4397 CORE_ADDR pc)
4398{
4399 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4400
4401 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4402 {
4403 struct bp_location *bl = *blp;
4404
4405 if (bl->loc_type != bp_loc_hardware_breakpoint)
4406 continue;
4407
4408 if (bp_location_inserted_here_p (bl, aspace, pc))
4409 return 1;
4410 }
4411
4412 return 0;
4fa8626c
DJ
4413}
4414
9093389c
PA
4415int
4416hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4417 CORE_ADDR addr, ULONGEST len)
4418{
4419 struct breakpoint *bpt;
4420
4421 ALL_BREAKPOINTS (bpt)
4422 {
4423 struct bp_location *loc;
4424
4425 if (bpt->type != bp_hardware_watchpoint
4426 && bpt->type != bp_access_watchpoint)
4427 continue;
4428
4429 if (!breakpoint_enabled (bpt))
4430 continue;
4431
4432 for (loc = bpt->loc; loc; loc = loc->next)
4433 if (loc->pspace->aspace == aspace && loc->inserted)
4434 {
4435 CORE_ADDR l, h;
4436
4437 /* Check for intersection. */
768adc05
PA
4438 l = std::max<CORE_ADDR> (loc->address, addr);
4439 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4440 if (l < h)
4441 return 1;
4442 }
4443 }
4444 return 0;
4445}
c906108c 4446\f
c5aa993b 4447
c906108c
SS
4448/* bpstat stuff. External routines' interfaces are documented
4449 in breakpoint.h. */
4450
4451int
c326b90e 4452is_catchpoint (struct breakpoint *ep)
c906108c 4453{
533be4dd 4454 return (ep->type == bp_catchpoint);
c906108c
SS
4455}
4456
f431efe5
PA
4457/* Frees any storage that is part of a bpstat. Does not walk the
4458 'next' chain. */
4459
4460static void
198757a8
VP
4461bpstat_free (bpstat bs)
4462{
4463 if (bs->old_val != NULL)
4464 value_free (bs->old_val);
9add0f1b 4465 decref_counted_command_line (&bs->commands);
f431efe5 4466 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4467 xfree (bs);
4468}
4469
c906108c
SS
4470/* Clear a bpstat so that it says we are not at any breakpoint.
4471 Also free any storage that is part of a bpstat. */
4472
4473void
fba45db2 4474bpstat_clear (bpstat *bsp)
c906108c
SS
4475{
4476 bpstat p;
4477 bpstat q;
4478
4479 if (bsp == 0)
4480 return;
4481 p = *bsp;
4482 while (p != NULL)
4483 {
4484 q = p->next;
198757a8 4485 bpstat_free (p);
c906108c
SS
4486 p = q;
4487 }
4488 *bsp = NULL;
4489}
4490
4491/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4492 is part of the bpstat is copied as well. */
4493
4494bpstat
fba45db2 4495bpstat_copy (bpstat bs)
c906108c
SS
4496{
4497 bpstat p = NULL;
4498 bpstat tmp;
4499 bpstat retval = NULL;
4500
4501 if (bs == NULL)
4502 return bs;
4503
4504 for (; bs != NULL; bs = bs->next)
4505 {
4506 tmp = (bpstat) xmalloc (sizeof (*tmp));
4507 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4508 incref_counted_command_line (tmp->commands);
f431efe5 4509 incref_bp_location (tmp->bp_location_at);
31cc81e9 4510 if (bs->old_val != NULL)
3c3185ac
JK
4511 {
4512 tmp->old_val = value_copy (bs->old_val);
4513 release_value (tmp->old_val);
4514 }
31cc81e9 4515
c906108c
SS
4516 if (p == NULL)
4517 /* This is the first thing in the chain. */
4518 retval = tmp;
4519 else
4520 p->next = tmp;
4521 p = tmp;
4522 }
4523 p->next = NULL;
4524 return retval;
4525}
4526
4a64f543 4527/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4528
4529bpstat
fba45db2 4530bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4531{
c5aa993b
JM
4532 if (bsp == NULL)
4533 return NULL;
c906108c 4534
c5aa993b
JM
4535 for (; bsp != NULL; bsp = bsp->next)
4536 {
f431efe5 4537 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4538 return bsp;
4539 }
c906108c
SS
4540 return NULL;
4541}
4542
ab04a2af
TT
4543/* See breakpoint.h. */
4544
47591c29 4545int
427cd150 4546bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4547{
ab04a2af
TT
4548 for (; bsp != NULL; bsp = bsp->next)
4549 {
427cd150
TT
4550 if (bsp->breakpoint_at == NULL)
4551 {
4552 /* A moribund location can never explain a signal other than
4553 GDB_SIGNAL_TRAP. */
4554 if (sig == GDB_SIGNAL_TRAP)
47591c29 4555 return 1;
427cd150
TT
4556 }
4557 else
47591c29
PA
4558 {
4559 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4560 sig))
4561 return 1;
4562 }
ab04a2af
TT
4563 }
4564
47591c29 4565 return 0;
ab04a2af
TT
4566}
4567
4a64f543
MS
4568/* Put in *NUM the breakpoint number of the first breakpoint we are
4569 stopped at. *BSP upon return is a bpstat which points to the
4570 remaining breakpoints stopped at (but which is not guaranteed to be
4571 good for anything but further calls to bpstat_num).
4572
8671a17b
PA
4573 Return 0 if passed a bpstat which does not indicate any breakpoints.
4574 Return -1 if stopped at a breakpoint that has been deleted since
4575 we set it.
4576 Return 1 otherwise. */
c906108c
SS
4577
4578int
8671a17b 4579bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4580{
4581 struct breakpoint *b;
4582
4583 if ((*bsp) == NULL)
4584 return 0; /* No more breakpoint values */
8671a17b 4585
4a64f543
MS
4586 /* We assume we'll never have several bpstats that correspond to a
4587 single breakpoint -- otherwise, this function might return the
4588 same number more than once and this will look ugly. */
f431efe5 4589 b = (*bsp)->breakpoint_at;
8671a17b
PA
4590 *bsp = (*bsp)->next;
4591 if (b == NULL)
4592 return -1; /* breakpoint that's been deleted since */
4593
4594 *num = b->number; /* We have its number */
4595 return 1;
c906108c
SS
4596}
4597
e93ca019 4598/* See breakpoint.h. */
c906108c
SS
4599
4600void
e93ca019 4601bpstat_clear_actions (void)
c906108c 4602{
e93ca019
JK
4603 struct thread_info *tp;
4604 bpstat bs;
4605
4606 if (ptid_equal (inferior_ptid, null_ptid))
4607 return;
4608
4609 tp = find_thread_ptid (inferior_ptid);
4610 if (tp == NULL)
4611 return;
4612
4613 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4614 {
9add0f1b 4615 decref_counted_command_line (&bs->commands);
abf85f46 4616
c906108c
SS
4617 if (bs->old_val != NULL)
4618 {
4619 value_free (bs->old_val);
4620 bs->old_val = NULL;
4621 }
4622 }
4623}
4624
f3b1572e
PA
4625/* Called when a command is about to proceed the inferior. */
4626
4627static void
4628breakpoint_about_to_proceed (void)
4629{
4630 if (!ptid_equal (inferior_ptid, null_ptid))
4631 {
4632 struct thread_info *tp = inferior_thread ();
4633
4634 /* Allow inferior function calls in breakpoint commands to not
4635 interrupt the command list. When the call finishes
4636 successfully, the inferior will be standing at the same
4637 breakpoint as if nothing happened. */
16c381f0 4638 if (tp->control.in_infcall)
f3b1572e
PA
4639 return;
4640 }
4641
4642 breakpoint_proceeded = 1;
4643}
4644
4a64f543
MS
4645/* Stub for cleaning up our state if we error-out of a breakpoint
4646 command. */
c906108c 4647static void
4efb68b1 4648cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4649{
4650 executing_breakpoint_commands = 0;
4651}
4652
abf85f46
JK
4653/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4654 or its equivalent. */
4655
4656static int
4657command_line_is_silent (struct command_line *cmd)
4658{
4f45d445 4659 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4660}
4661
4a64f543
MS
4662/* Execute all the commands associated with all the breakpoints at
4663 this location. Any of these commands could cause the process to
4664 proceed beyond this point, etc. We look out for such changes by
4665 checking the global "breakpoint_proceeded" after each command.
c906108c 4666
347bddb7
PA
4667 Returns true if a breakpoint command resumed the inferior. In that
4668 case, it is the caller's responsibility to recall it again with the
4669 bpstat of the current thread. */
4670
4671static int
4672bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4673{
4674 bpstat bs;
4675 struct cleanup *old_chain;
347bddb7 4676 int again = 0;
c906108c
SS
4677
4678 /* Avoid endless recursion if a `source' command is contained
4679 in bs->commands. */
4680 if (executing_breakpoint_commands)
347bddb7 4681 return 0;
c906108c
SS
4682
4683 executing_breakpoint_commands = 1;
4684 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4685
1ac32117 4686 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4687
4a64f543 4688 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4689 bs = *bsp;
4690
4691 breakpoint_proceeded = 0;
4692 for (; bs != NULL; bs = bs->next)
4693 {
9add0f1b 4694 struct counted_command_line *ccmd;
6c50ab1c
JB
4695 struct command_line *cmd;
4696 struct cleanup *this_cmd_tree_chain;
4697
4698 /* Take ownership of the BSP's command tree, if it has one.
4699
4700 The command tree could legitimately contain commands like
4701 'step' and 'next', which call clear_proceed_status, which
4702 frees stop_bpstat's command tree. To make sure this doesn't
4703 free the tree we're executing out from under us, we need to
4704 take ownership of the tree ourselves. Since a given bpstat's
4705 commands are only executed once, we don't need to copy it; we
4706 can clear the pointer in the bpstat, and make sure we free
4707 the tree when we're done. */
9add0f1b
TT
4708 ccmd = bs->commands;
4709 bs->commands = NULL;
abf85f46
JK
4710 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4711 cmd = ccmd ? ccmd->commands : NULL;
4712 if (command_line_is_silent (cmd))
4713 {
4714 /* The action has been already done by bpstat_stop_status. */
4715 cmd = cmd->next;
4716 }
6c50ab1c 4717
c906108c
SS
4718 while (cmd != NULL)
4719 {
4720 execute_control_command (cmd);
4721
4722 if (breakpoint_proceeded)
4723 break;
4724 else
4725 cmd = cmd->next;
4726 }
6c50ab1c
JB
4727
4728 /* We can free this command tree now. */
4729 do_cleanups (this_cmd_tree_chain);
4730
c906108c 4731 if (breakpoint_proceeded)
32c1e744 4732 {
cb814510 4733 if (current_ui->async)
347bddb7
PA
4734 /* If we are in async mode, then the target might be still
4735 running, not stopped at any breakpoint, so nothing for
4736 us to do here -- just return to the event loop. */
4737 ;
32c1e744
VP
4738 else
4739 /* In sync mode, when execute_control_command returns
4740 we're already standing on the next breakpoint.
347bddb7
PA
4741 Breakpoint commands for that stop were not run, since
4742 execute_command does not run breakpoint commands --
4743 only command_line_handler does, but that one is not
4744 involved in execution of breakpoint commands. So, we
4745 can now execute breakpoint commands. It should be
4746 noted that making execute_command do bpstat actions is
4747 not an option -- in this case we'll have recursive
4748 invocation of bpstat for each breakpoint with a
4749 command, and can easily blow up GDB stack. Instead, we
4750 return true, which will trigger the caller to recall us
4751 with the new stop_bpstat. */
4752 again = 1;
4753 break;
32c1e744 4754 }
c906108c 4755 }
c2b8ed2c 4756 do_cleanups (old_chain);
347bddb7
PA
4757 return again;
4758}
4759
4760void
4761bpstat_do_actions (void)
4762{
353d1d73
JK
4763 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4764
347bddb7
PA
4765 /* Do any commands attached to breakpoint we are stopped at. */
4766 while (!ptid_equal (inferior_ptid, null_ptid)
4767 && target_has_execution
4768 && !is_exited (inferior_ptid)
4769 && !is_executing (inferior_ptid))
4770 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4771 and only return when it is stopped at the next breakpoint, we
4772 keep doing breakpoint actions until it returns false to
4773 indicate the inferior was not resumed. */
16c381f0 4774 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4775 break;
353d1d73
JK
4776
4777 discard_cleanups (cleanup_if_error);
c906108c
SS
4778}
4779
fa4727a6
DJ
4780/* Print out the (old or new) value associated with a watchpoint. */
4781
4782static void
4783watchpoint_value_print (struct value *val, struct ui_file *stream)
4784{
4785 if (val == NULL)
4786 fprintf_unfiltered (stream, _("<unreadable>"));
4787 else
79a45b7d
TT
4788 {
4789 struct value_print_options opts;
4790 get_user_print_options (&opts);
4791 value_print (val, stream, &opts);
4792 }
fa4727a6
DJ
4793}
4794
f303dbd6
PA
4795/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4796 debugging multiple threads. */
4797
4798void
4799maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4800{
112e8700 4801 if (uiout->is_mi_like_p ())
f303dbd6
PA
4802 return;
4803
112e8700 4804 uiout->text ("\n");
f303dbd6
PA
4805
4806 if (show_thread_that_caused_stop ())
4807 {
4808 const char *name;
4809 struct thread_info *thr = inferior_thread ();
4810
112e8700
SM
4811 uiout->text ("Thread ");
4812 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4813
4814 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4815 if (name != NULL)
4816 {
112e8700
SM
4817 uiout->text (" \"");
4818 uiout->field_fmt ("name", "%s", name);
4819 uiout->text ("\"");
f303dbd6
PA
4820 }
4821
112e8700 4822 uiout->text (" hit ");
f303dbd6
PA
4823 }
4824}
4825
e514a9d6 4826/* Generic routine for printing messages indicating why we
4a64f543 4827 stopped. The behavior of this function depends on the value
e514a9d6
JM
4828 'print_it' in the bpstat structure. Under some circumstances we
4829 may decide not to print anything here and delegate the task to
4a64f543 4830 normal_stop(). */
e514a9d6
JM
4831
4832static enum print_stop_action
4833print_bp_stop_message (bpstat bs)
4834{
4835 switch (bs->print_it)
4836 {
4837 case print_it_noop:
4a64f543 4838 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4839 return PRINT_UNKNOWN;
4840 break;
4841
4842 case print_it_done:
4843 /* We still want to print the frame, but we already printed the
4a64f543 4844 relevant messages. */
e514a9d6
JM
4845 return PRINT_SRC_AND_LOC;
4846 break;
4847
4848 case print_it_normal:
4f8d1dc6 4849 {
f431efe5
PA
4850 struct breakpoint *b = bs->breakpoint_at;
4851
1a6a67de
TJB
4852 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4853 which has since been deleted. */
4854 if (b == NULL)
4855 return PRINT_UNKNOWN;
4856
348d480f
PA
4857 /* Normal case. Call the breakpoint's print_it method. */
4858 return b->ops->print_it (bs);
4f8d1dc6 4859 }
348d480f 4860 break;
3086aeae 4861
e514a9d6 4862 default:
8e65ff28 4863 internal_error (__FILE__, __LINE__,
e2e0b3e5 4864 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4865 break;
c906108c 4866 }
c906108c
SS
4867}
4868
edcc5120
TT
4869/* A helper function that prints a shared library stopped event. */
4870
4871static void
4872print_solib_event (int is_catchpoint)
4873{
4874 int any_deleted
4875 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4876 int any_added
4877 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4878
4879 if (!is_catchpoint)
4880 {
4881 if (any_added || any_deleted)
112e8700 4882 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4883 else
112e8700
SM
4884 current_uiout->text (_("Stopped due to shared library event (no "
4885 "libraries added or removed)\n"));
edcc5120
TT
4886 }
4887
112e8700
SM
4888 if (current_uiout->is_mi_like_p ())
4889 current_uiout->field_string ("reason",
4890 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4891
4892 if (any_deleted)
4893 {
edcc5120
TT
4894 char *name;
4895 int ix;
4896
112e8700 4897 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4898 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4899 for (ix = 0;
4900 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4901 ix, name);
4902 ++ix)
4903 {
4904 if (ix > 0)
112e8700
SM
4905 current_uiout->text (" ");
4906 current_uiout->field_string ("library", name);
4907 current_uiout->text ("\n");
edcc5120 4908 }
edcc5120
TT
4909 }
4910
4911 if (any_added)
4912 {
4913 struct so_list *iter;
4914 int ix;
edcc5120 4915
112e8700 4916 current_uiout->text (_(" Inferior loaded "));
10f489e5 4917 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4918 for (ix = 0;
4919 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4920 ix, iter);
4921 ++ix)
4922 {
4923 if (ix > 0)
112e8700
SM
4924 current_uiout->text (" ");
4925 current_uiout->field_string ("library", iter->so_name);
4926 current_uiout->text ("\n");
edcc5120 4927 }
edcc5120
TT
4928 }
4929}
4930
e514a9d6
JM
4931/* Print a message indicating what happened. This is called from
4932 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4933 list - a list of the eventpoints that caused this stop. KIND is
4934 the target_waitkind for the stopping event. This
e514a9d6
JM
4935 routine calls the generic print routine for printing a message
4936 about reasons for stopping. This will print (for example) the
4937 "Breakpoint n," part of the output. The return value of this
4938 routine is one of:
c906108c 4939
4a64f543 4940 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4941 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4942 code to print the location. An example is
c5aa993b
JM
4943 "Breakpoint 1, " which should be followed by
4944 the location.
917317f4 4945 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4946 to also print the location part of the message.
4947 An example is the catch/throw messages, which
4a64f543 4948 don't require a location appended to the end.
917317f4 4949 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4950 further info to be printed. */
c906108c 4951
917317f4 4952enum print_stop_action
36dfb11c 4953bpstat_print (bpstat bs, int kind)
c906108c 4954{
f486487f 4955 enum print_stop_action val;
c5aa993b 4956
c906108c 4957 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4958 (Currently all watchpoints go on the bpstat whether hit or not.
4959 That probably could (should) be changed, provided care is taken
c906108c 4960 with respect to bpstat_explains_signal). */
e514a9d6
JM
4961 for (; bs; bs = bs->next)
4962 {
4963 val = print_bp_stop_message (bs);
4964 if (val == PRINT_SRC_ONLY
4965 || val == PRINT_SRC_AND_LOC
4966 || val == PRINT_NOTHING)
4967 return val;
4968 }
c906108c 4969
36dfb11c
TT
4970 /* If we had hit a shared library event breakpoint,
4971 print_bp_stop_message would print out this message. If we hit an
4972 OS-level shared library event, do the same thing. */
4973 if (kind == TARGET_WAITKIND_LOADED)
4974 {
edcc5120 4975 print_solib_event (0);
36dfb11c
TT
4976 return PRINT_NOTHING;
4977 }
4978
e514a9d6 4979 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4980 with and nothing was printed. */
917317f4 4981 return PRINT_UNKNOWN;
c906108c
SS
4982}
4983
c42bd95a
DE
4984/* Evaluate the expression EXP and return 1 if value is zero.
4985 This returns the inverse of the condition because it is called
4986 from catch_errors which returns 0 if an exception happened, and if an
4987 exception happens we want execution to stop.
4a64f543 4988 The argument is a "struct expression *" that has been cast to a
c42bd95a 4989 "void *" to make it pass through catch_errors. */
c906108c
SS
4990
4991static int
4efb68b1 4992breakpoint_cond_eval (void *exp)
c906108c 4993{
278cd55f 4994 struct value *mark = value_mark ();
c5aa993b 4995 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4996
c906108c
SS
4997 value_free_to_mark (mark);
4998 return i;
4999}
5000
5760d0ab 5001/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5002
5003static bpstat
5760d0ab 5004bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5005{
5006 bpstat bs;
5007
5008 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5009 bs->next = NULL;
5010 **bs_link_pointer = bs;
5011 *bs_link_pointer = &bs->next;
f431efe5
PA
5012 bs->breakpoint_at = bl->owner;
5013 bs->bp_location_at = bl;
5014 incref_bp_location (bl);
c906108c
SS
5015 /* If the condition is false, etc., don't do the commands. */
5016 bs->commands = NULL;
5017 bs->old_val = NULL;
5018 bs->print_it = print_it_normal;
5019 return bs;
5020}
5021\f
d983da9c
DJ
5022/* The target has stopped with waitstatus WS. Check if any hardware
5023 watchpoints have triggered, according to the target. */
5024
5025int
5026watchpoints_triggered (struct target_waitstatus *ws)
5027{
d92524f1 5028 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5029 CORE_ADDR addr;
5030 struct breakpoint *b;
5031
5032 if (!stopped_by_watchpoint)
5033 {
5034 /* We were not stopped by a watchpoint. Mark all watchpoints
5035 as not triggered. */
5036 ALL_BREAKPOINTS (b)
cc60f2e3 5037 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5038 {
5039 struct watchpoint *w = (struct watchpoint *) b;
5040
5041 w->watchpoint_triggered = watch_triggered_no;
5042 }
d983da9c
DJ
5043
5044 return 0;
5045 }
5046
5047 if (!target_stopped_data_address (&current_target, &addr))
5048 {
5049 /* We were stopped by a watchpoint, but we don't know where.
5050 Mark all watchpoints as unknown. */
5051 ALL_BREAKPOINTS (b)
cc60f2e3 5052 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5053 {
5054 struct watchpoint *w = (struct watchpoint *) b;
5055
5056 w->watchpoint_triggered = watch_triggered_unknown;
5057 }
d983da9c 5058
3c4797ba 5059 return 1;
d983da9c
DJ
5060 }
5061
5062 /* The target could report the data address. Mark watchpoints
5063 affected by this data address as triggered, and all others as not
5064 triggered. */
5065
5066 ALL_BREAKPOINTS (b)
cc60f2e3 5067 if (is_hardware_watchpoint (b))
d983da9c 5068 {
3a5c3e22 5069 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5070 struct bp_location *loc;
d983da9c 5071
3a5c3e22 5072 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5073 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5074 {
3a5c3e22 5075 if (is_masked_watchpoint (b))
9c06b0b4 5076 {
3a5c3e22
PA
5077 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5078 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5079
5080 if (newaddr == start)
5081 {
3a5c3e22 5082 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5083 break;
5084 }
5085 }
5086 /* Exact match not required. Within range is sufficient. */
5087 else if (target_watchpoint_addr_within_range (&current_target,
5088 addr, loc->address,
5089 loc->length))
5090 {
3a5c3e22 5091 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5092 break;
5093 }
5094 }
d983da9c
DJ
5095 }
5096
5097 return 1;
5098}
5099
c906108c
SS
5100/* Possible return values for watchpoint_check (this can't be an enum
5101 because of check_errors). */
5102/* The watchpoint has been deleted. */
5103#define WP_DELETED 1
5104/* The value has changed. */
5105#define WP_VALUE_CHANGED 2
5106/* The value has not changed. */
5107#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5108/* Ignore this watchpoint, no matter if the value changed or not. */
5109#define WP_IGNORE 4
c906108c
SS
5110
5111#define BP_TEMPFLAG 1
5112#define BP_HARDWAREFLAG 2
5113
4a64f543
MS
5114/* Evaluate watchpoint condition expression and check if its value
5115 changed.
553e4c11
JB
5116
5117 P should be a pointer to struct bpstat, but is defined as a void *
5118 in order for this function to be usable with catch_errors. */
c906108c
SS
5119
5120static int
4efb68b1 5121watchpoint_check (void *p)
c906108c
SS
5122{
5123 bpstat bs = (bpstat) p;
3a5c3e22 5124 struct watchpoint *b;
c906108c
SS
5125 struct frame_info *fr;
5126 int within_current_scope;
5127
f431efe5 5128 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5129 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5130 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5131
f6bc2008
PA
5132 /* If this is a local watchpoint, we only want to check if the
5133 watchpoint frame is in scope if the current thread is the thread
5134 that was used to create the watchpoint. */
5135 if (!watchpoint_in_thread_scope (b))
60e1c644 5136 return WP_IGNORE;
f6bc2008 5137
c906108c
SS
5138 if (b->exp_valid_block == NULL)
5139 within_current_scope = 1;
5140 else
5141 {
edb3359d
DJ
5142 struct frame_info *frame = get_current_frame ();
5143 struct gdbarch *frame_arch = get_frame_arch (frame);
5144 CORE_ADDR frame_pc = get_frame_pc (frame);
5145
c9cf6e20 5146 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5147 still in the function but the stack frame has already been
5148 invalidated. Since we can't rely on the values of local
5149 variables after the stack has been destroyed, we are treating
5150 the watchpoint in that state as `not changed' without further
5151 checking. Don't mark watchpoints as changed if the current
5152 frame is in an epilogue - even if they are in some other
5153 frame, our view of the stack is likely to be wrong and
5154 frame_find_by_id could error out. */
c9cf6e20 5155 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5156 return WP_IGNORE;
a0f49112 5157
101dcfbe 5158 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5159 within_current_scope = (fr != NULL);
69fbadd5
DJ
5160
5161 /* If we've gotten confused in the unwinder, we might have
5162 returned a frame that can't describe this variable. */
edb3359d
DJ
5163 if (within_current_scope)
5164 {
5165 struct symbol *function;
5166
5167 function = get_frame_function (fr);
5168 if (function == NULL
5169 || !contained_in (b->exp_valid_block,
5170 SYMBOL_BLOCK_VALUE (function)))
5171 within_current_scope = 0;
5172 }
69fbadd5 5173
edb3359d 5174 if (within_current_scope)
c906108c
SS
5175 /* If we end up stopping, the current frame will get selected
5176 in normal_stop. So this call to select_frame won't affect
5177 the user. */
0f7d239c 5178 select_frame (fr);
c906108c 5179 }
c5aa993b 5180
c906108c
SS
5181 if (within_current_scope)
5182 {
4a64f543
MS
5183 /* We use value_{,free_to_}mark because it could be a *long*
5184 time before we return to the command level and call
5185 free_all_values. We can't call free_all_values because we
5186 might be in the middle of evaluating a function call. */
c906108c 5187
0cf6dd15 5188 int pc = 0;
9c06b0b4 5189 struct value *mark;
fa4727a6
DJ
5190 struct value *new_val;
5191
c1fc2657 5192 if (is_masked_watchpoint (b))
9c06b0b4
TJB
5193 /* Since we don't know the exact trigger address (from
5194 stopped_data_address), just tell the user we've triggered
5195 a mask watchpoint. */
5196 return WP_VALUE_CHANGED;
5197
5198 mark = value_mark ();
4d01a485 5199 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 5200
bb9d5f81
PP
5201 if (b->val_bitsize != 0)
5202 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5203
4a64f543
MS
5204 /* We use value_equal_contents instead of value_equal because
5205 the latter coerces an array to a pointer, thus comparing just
5206 the address of the array instead of its contents. This is
5207 not what we want. */
fa4727a6 5208 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5209 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5210 {
fa4727a6
DJ
5211 if (new_val != NULL)
5212 {
5213 release_value (new_val);
5214 value_free_to_mark (mark);
5215 }
c906108c
SS
5216 bs->old_val = b->val;
5217 b->val = new_val;
fa4727a6 5218 b->val_valid = 1;
c906108c
SS
5219 return WP_VALUE_CHANGED;
5220 }
5221 else
5222 {
60e1c644 5223 /* Nothing changed. */
c906108c 5224 value_free_to_mark (mark);
c906108c
SS
5225 return WP_VALUE_NOT_CHANGED;
5226 }
5227 }
5228 else
5229 {
5230 /* This seems like the only logical thing to do because
c5aa993b
JM
5231 if we temporarily ignored the watchpoint, then when
5232 we reenter the block in which it is valid it contains
5233 garbage (in the case of a function, it may have two
5234 garbage values, one before and one after the prologue).
5235 So we can't even detect the first assignment to it and
5236 watch after that (since the garbage may or may not equal
5237 the first value assigned). */
348d480f
PA
5238 /* We print all the stop information in
5239 breakpoint_ops->print_it, but in this case, by the time we
5240 call breakpoint_ops->print_it this bp will be deleted
5241 already. So we have no choice but print the information
5242 here. */
468afe6c 5243
0e454242 5244 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5245 {
5246 struct ui_out *uiout = current_uiout;
5247
112e8700
SM
5248 if (uiout->is_mi_like_p ())
5249 uiout->field_string
5250 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5251 uiout->text ("\nWatchpoint ");
c1fc2657 5252 uiout->field_int ("wpnum", b->number);
112e8700 5253 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5254 "which its expression is valid.\n");
5255 }
4ce44c66 5256
cdac0397 5257 /* Make sure the watchpoint's commands aren't executed. */
c1fc2657 5258 decref_counted_command_line (&b->commands);
d0fb5eae 5259 watchpoint_del_at_next_stop (b);
c906108c
SS
5260
5261 return WP_DELETED;
5262 }
5263}
5264
18a18393 5265/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5266 breakpoint location BL. This function does not check if we should
5267 stop, only if BL explains the stop. */
5268
18a18393 5269static int
6c95b8df 5270bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5271 struct address_space *aspace, CORE_ADDR bp_addr,
5272 const struct target_waitstatus *ws)
18a18393
VP
5273{
5274 struct breakpoint *b = bl->owner;
5275
348d480f 5276 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5277 gdb_assert (b != NULL);
5278
09ac7c10 5279 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5280}
5281
3a5c3e22
PA
5282/* Determine if the watched values have actually changed, and we
5283 should stop. If not, set BS->stop to 0. */
5284
18a18393
VP
5285static void
5286bpstat_check_watchpoint (bpstat bs)
5287{
2bdf28a0 5288 const struct bp_location *bl;
3a5c3e22 5289 struct watchpoint *b;
2bdf28a0
JK
5290
5291 /* BS is built for existing struct breakpoint. */
f431efe5 5292 bl = bs->bp_location_at;
2bdf28a0 5293 gdb_assert (bl != NULL);
3a5c3e22 5294 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5295 gdb_assert (b != NULL);
18a18393 5296
18a18393 5297 {
18a18393
VP
5298 int must_check_value = 0;
5299
c1fc2657 5300 if (b->type == bp_watchpoint)
18a18393
VP
5301 /* For a software watchpoint, we must always check the
5302 watched value. */
5303 must_check_value = 1;
5304 else if (b->watchpoint_triggered == watch_triggered_yes)
5305 /* We have a hardware watchpoint (read, write, or access)
5306 and the target earlier reported an address watched by
5307 this watchpoint. */
5308 must_check_value = 1;
5309 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5310 && b->type == bp_hardware_watchpoint)
18a18393
VP
5311 /* We were stopped by a hardware watchpoint, but the target could
5312 not report the data address. We must check the watchpoint's
5313 value. Access and read watchpoints are out of luck; without
5314 a data address, we can't figure it out. */
5315 must_check_value = 1;
3a5c3e22 5316
18a18393
VP
5317 if (must_check_value)
5318 {
3e43a32a
MS
5319 char *message
5320 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
c1fc2657 5321 b->number);
18a18393
VP
5322 struct cleanup *cleanups = make_cleanup (xfree, message);
5323 int e = catch_errors (watchpoint_check, bs, message,
5324 RETURN_MASK_ALL);
5325 do_cleanups (cleanups);
5326 switch (e)
5327 {
5328 case WP_DELETED:
5329 /* We've already printed what needs to be printed. */
5330 bs->print_it = print_it_done;
5331 /* Stop. */
5332 break;
60e1c644
PA
5333 case WP_IGNORE:
5334 bs->print_it = print_it_noop;
5335 bs->stop = 0;
5336 break;
18a18393 5337 case WP_VALUE_CHANGED:
c1fc2657 5338 if (b->type == bp_read_watchpoint)
18a18393 5339 {
85d721b8
PA
5340 /* There are two cases to consider here:
5341
4a64f543 5342 1. We're watching the triggered memory for reads.
85d721b8
PA
5343 In that case, trust the target, and always report
5344 the watchpoint hit to the user. Even though
5345 reads don't cause value changes, the value may
5346 have changed since the last time it was read, and
5347 since we're not trapping writes, we will not see
5348 those, and as such we should ignore our notion of
5349 old value.
5350
4a64f543 5351 2. We're watching the triggered memory for both
85d721b8
PA
5352 reads and writes. There are two ways this may
5353 happen:
5354
4a64f543 5355 2.1. This is a target that can't break on data
85d721b8
PA
5356 reads only, but can break on accesses (reads or
5357 writes), such as e.g., x86. We detect this case
5358 at the time we try to insert read watchpoints.
5359
4a64f543 5360 2.2. Otherwise, the target supports read
85d721b8
PA
5361 watchpoints, but, the user set an access or write
5362 watchpoint watching the same memory as this read
5363 watchpoint.
5364
5365 If we're watching memory writes as well as reads,
5366 ignore watchpoint hits when we find that the
5367 value hasn't changed, as reads don't cause
5368 changes. This still gives false positives when
5369 the program writes the same value to memory as
5370 what there was already in memory (we will confuse
5371 it for a read), but it's much better than
5372 nothing. */
5373
5374 int other_write_watchpoint = 0;
5375
5376 if (bl->watchpoint_type == hw_read)
5377 {
5378 struct breakpoint *other_b;
5379
5380 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5381 if (other_b->type == bp_hardware_watchpoint
5382 || other_b->type == bp_access_watchpoint)
85d721b8 5383 {
3a5c3e22
PA
5384 struct watchpoint *other_w =
5385 (struct watchpoint *) other_b;
5386
5387 if (other_w->watchpoint_triggered
5388 == watch_triggered_yes)
5389 {
5390 other_write_watchpoint = 1;
5391 break;
5392 }
85d721b8
PA
5393 }
5394 }
5395
5396 if (other_write_watchpoint
5397 || bl->watchpoint_type == hw_access)
5398 {
5399 /* We're watching the same memory for writes,
5400 and the value changed since the last time we
5401 updated it, so this trap must be for a write.
5402 Ignore it. */
5403 bs->print_it = print_it_noop;
5404 bs->stop = 0;
5405 }
18a18393
VP
5406 }
5407 break;
5408 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5409 if (b->type == bp_hardware_watchpoint
5410 || b->type == bp_watchpoint)
18a18393
VP
5411 {
5412 /* Don't stop: write watchpoints shouldn't fire if
5413 the value hasn't changed. */
5414 bs->print_it = print_it_noop;
5415 bs->stop = 0;
5416 }
5417 /* Stop. */
5418 break;
5419 default:
5420 /* Can't happen. */
5421 case 0:
5422 /* Error from catch_errors. */
468afe6c 5423 {
0e454242 5424 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5425 {
5426 printf_filtered (_("Watchpoint %d deleted.\n"),
c1fc2657 5427 b->number);
468afe6c
PA
5428 }
5429 watchpoint_del_at_next_stop (b);
5430 /* We've already printed what needs to be printed. */
5431 bs->print_it = print_it_done;
5432 }
18a18393
VP
5433 break;
5434 }
5435 }
5436 else /* must_check_value == 0 */
5437 {
5438 /* This is a case where some watchpoint(s) triggered, but
5439 not at the address of this watchpoint, or else no
5440 watchpoint triggered after all. So don't print
5441 anything for this watchpoint. */
5442 bs->print_it = print_it_noop;
5443 bs->stop = 0;
5444 }
5445 }
5446}
5447
7d4df6a4
DE
5448/* For breakpoints that are currently marked as telling gdb to stop,
5449 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5450 of breakpoint referred to by BS. If we should not stop for this
5451 breakpoint, set BS->stop to 0. */
f431efe5 5452
18a18393
VP
5453static void
5454bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5455{
2bdf28a0
JK
5456 const struct bp_location *bl;
5457 struct breakpoint *b;
7d4df6a4
DE
5458 int value_is_zero = 0;
5459 struct expression *cond;
5460
5461 gdb_assert (bs->stop);
2bdf28a0
JK
5462
5463 /* BS is built for existing struct breakpoint. */
f431efe5 5464 bl = bs->bp_location_at;
2bdf28a0 5465 gdb_assert (bl != NULL);
f431efe5 5466 b = bs->breakpoint_at;
2bdf28a0 5467 gdb_assert (b != NULL);
18a18393 5468
b775012e
LM
5469 /* Even if the target evaluated the condition on its end and notified GDB, we
5470 need to do so again since GDB does not know if we stopped due to a
5471 breakpoint or a single step breakpoint. */
5472
18a18393 5473 if (frame_id_p (b->frame_id)
edb3359d 5474 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5475 {
7d4df6a4
DE
5476 bs->stop = 0;
5477 return;
5478 }
60e1c644 5479
12ab52e9
PA
5480 /* If this is a thread/task-specific breakpoint, don't waste cpu
5481 evaluating the condition if this isn't the specified
5482 thread/task. */
5d5658a1 5483 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5484 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5485
6c1b0f7b
DE
5486 {
5487 bs->stop = 0;
5488 return;
5489 }
5490
6dddc817
DE
5491 /* Evaluate extension language breakpoints that have a "stop" method
5492 implemented. */
5493 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5494
7d4df6a4
DE
5495 if (is_watchpoint (b))
5496 {
5497 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5498
4d01a485 5499 cond = w->cond_exp.get ();
7d4df6a4
DE
5500 }
5501 else
4d01a485 5502 cond = bl->cond.get ();
60e1c644 5503
7d4df6a4
DE
5504 if (cond && b->disposition != disp_del_at_next_stop)
5505 {
5506 int within_current_scope = 1;
5507 struct watchpoint * w;
60e1c644 5508
7d4df6a4
DE
5509 /* We use value_mark and value_free_to_mark because it could
5510 be a long time before we return to the command level and
5511 call free_all_values. We can't call free_all_values
5512 because we might be in the middle of evaluating a
5513 function call. */
5514 struct value *mark = value_mark ();
5515
5516 if (is_watchpoint (b))
5517 w = (struct watchpoint *) b;
5518 else
5519 w = NULL;
5520
5521 /* Need to select the frame, with all that implies so that
5522 the conditions will have the right context. Because we
5523 use the frame, we will not see an inlined function's
5524 variables when we arrive at a breakpoint at the start
5525 of the inlined function; the current frame will be the
5526 call site. */
5527 if (w == NULL || w->cond_exp_valid_block == NULL)
5528 select_frame (get_current_frame ());
5529 else
18a18393 5530 {
7d4df6a4
DE
5531 struct frame_info *frame;
5532
5533 /* For local watchpoint expressions, which particular
5534 instance of a local is being watched matters, so we
5535 keep track of the frame to evaluate the expression
5536 in. To evaluate the condition however, it doesn't
5537 really matter which instantiation of the function
5538 where the condition makes sense triggers the
5539 watchpoint. This allows an expression like "watch
5540 global if q > 10" set in `func', catch writes to
5541 global on all threads that call `func', or catch
5542 writes on all recursive calls of `func' by a single
5543 thread. We simply always evaluate the condition in
5544 the innermost frame that's executing where it makes
5545 sense to evaluate the condition. It seems
5546 intuitive. */
5547 frame = block_innermost_frame (w->cond_exp_valid_block);
5548 if (frame != NULL)
5549 select_frame (frame);
5550 else
5551 within_current_scope = 0;
18a18393 5552 }
7d4df6a4
DE
5553 if (within_current_scope)
5554 value_is_zero
5555 = catch_errors (breakpoint_cond_eval, cond,
5556 "Error in testing breakpoint condition:\n",
5557 RETURN_MASK_ALL);
5558 else
18a18393 5559 {
7d4df6a4
DE
5560 warning (_("Watchpoint condition cannot be tested "
5561 "in the current scope"));
5562 /* If we failed to set the right context for this
5563 watchpoint, unconditionally report it. */
5564 value_is_zero = 0;
18a18393 5565 }
7d4df6a4
DE
5566 /* FIXME-someday, should give breakpoint #. */
5567 value_free_to_mark (mark);
18a18393 5568 }
7d4df6a4
DE
5569
5570 if (cond && value_is_zero)
5571 {
5572 bs->stop = 0;
5573 }
7d4df6a4
DE
5574 else if (b->ignore_count > 0)
5575 {
5576 b->ignore_count--;
5577 bs->stop = 0;
5578 /* Increase the hit count even though we don't stop. */
5579 ++(b->hit_count);
5580 observer_notify_breakpoint_modified (b);
5581 }
18a18393
VP
5582}
5583
1cf4d951
PA
5584/* Returns true if we need to track moribund locations of LOC's type
5585 on the current target. */
5586
5587static int
5588need_moribund_for_location_type (struct bp_location *loc)
5589{
5590 return ((loc->loc_type == bp_loc_software_breakpoint
5591 && !target_supports_stopped_by_sw_breakpoint ())
5592 || (loc->loc_type == bp_loc_hardware_breakpoint
5593 && !target_supports_stopped_by_hw_breakpoint ()));
5594}
5595
18a18393 5596
9709f61c 5597/* Get a bpstat associated with having just stopped at address
d983da9c 5598 BP_ADDR in thread PTID.
c906108c 5599
d983da9c 5600 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5601 don't understand this stop. Result is a chain of bpstat's such
5602 that:
c906108c 5603
c5aa993b 5604 if we don't understand the stop, the result is a null pointer.
c906108c 5605
c5aa993b 5606 if we understand why we stopped, the result is not null.
c906108c 5607
c5aa993b
JM
5608 Each element of the chain refers to a particular breakpoint or
5609 watchpoint at which we have stopped. (We may have stopped for
5610 several reasons concurrently.)
c906108c 5611
c5aa993b
JM
5612 Each element of the chain has valid next, breakpoint_at,
5613 commands, FIXME??? fields. */
c906108c
SS
5614
5615bpstat
6c95b8df 5616bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5617 CORE_ADDR bp_addr, ptid_t ptid,
5618 const struct target_waitstatus *ws)
c906108c 5619{
0d381245 5620 struct breakpoint *b = NULL;
afe38095 5621 struct bp_location *bl;
20874c92 5622 struct bp_location *loc;
5760d0ab
JK
5623 /* First item of allocated bpstat's. */
5624 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5625 /* Pointer to the last thing in the chain currently. */
5760d0ab 5626 bpstat bs;
20874c92 5627 int ix;
429374b8 5628 int need_remove_insert;
f431efe5 5629 int removed_any;
c906108c 5630
f431efe5
PA
5631 /* First, build the bpstat chain with locations that explain a
5632 target stop, while being careful to not set the target running,
5633 as that may invalidate locations (in particular watchpoint
5634 locations are recreated). Resuming will happen here with
5635 breakpoint conditions or watchpoint expressions that include
5636 inferior function calls. */
c5aa993b 5637
429374b8
JK
5638 ALL_BREAKPOINTS (b)
5639 {
1a853c52 5640 if (!breakpoint_enabled (b))
429374b8 5641 continue;
a5606eee 5642
429374b8
JK
5643 for (bl = b->loc; bl != NULL; bl = bl->next)
5644 {
4a64f543
MS
5645 /* For hardware watchpoints, we look only at the first
5646 location. The watchpoint_check function will work on the
5647 entire expression, not the individual locations. For
5648 read watchpoints, the watchpoints_triggered function has
5649 checked all locations already. */
429374b8
JK
5650 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5651 break;
18a18393 5652
f6592439 5653 if (!bl->enabled || bl->shlib_disabled)
429374b8 5654 continue;
c5aa993b 5655
09ac7c10 5656 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5657 continue;
c5aa993b 5658
4a64f543
MS
5659 /* Come here if it's a watchpoint, or if the break address
5660 matches. */
c5aa993b 5661
4a64f543
MS
5662 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5663 explain stop. */
c5aa993b 5664
f431efe5
PA
5665 /* Assume we stop. Should we find a watchpoint that is not
5666 actually triggered, or if the condition of the breakpoint
5667 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5668 bs->stop = 1;
5669 bs->print = 1;
d983da9c 5670
f431efe5
PA
5671 /* If this is a scope breakpoint, mark the associated
5672 watchpoint as triggered so that we will handle the
5673 out-of-scope event. We'll get to the watchpoint next
5674 iteration. */
d0fb5eae 5675 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5676 {
5677 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5678
5679 w->watchpoint_triggered = watch_triggered_yes;
5680 }
f431efe5
PA
5681 }
5682 }
5683
7c16b83e 5684 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5685 if (!target_supports_stopped_by_sw_breakpoint ()
5686 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5687 {
1cf4d951 5688 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5689 {
1cf4d951
PA
5690 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5691 && need_moribund_for_location_type (loc))
5692 {
5693 bs = bpstat_alloc (loc, &bs_link);
5694 /* For hits of moribund locations, we should just proceed. */
5695 bs->stop = 0;
5696 bs->print = 0;
5697 bs->print_it = print_it_noop;
5698 }
f431efe5
PA
5699 }
5700 }
5701
edcc5120
TT
5702 /* A bit of special processing for shlib breakpoints. We need to
5703 process solib loading here, so that the lists of loaded and
5704 unloaded libraries are correct before we handle "catch load" and
5705 "catch unload". */
5706 for (bs = bs_head; bs != NULL; bs = bs->next)
5707 {
5d268276 5708 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5709 {
5710 handle_solib_event ();
5711 break;
5712 }
5713 }
5714
f431efe5
PA
5715 /* Now go through the locations that caused the target to stop, and
5716 check whether we're interested in reporting this stop to higher
5717 layers, or whether we should resume the target transparently. */
5718
5719 removed_any = 0;
5720
5760d0ab 5721 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5722 {
5723 if (!bs->stop)
5724 continue;
5725
f431efe5 5726 b = bs->breakpoint_at;
348d480f
PA
5727 b->ops->check_status (bs);
5728 if (bs->stop)
28010a5d 5729 {
348d480f 5730 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5731
429374b8
JK
5732 if (bs->stop)
5733 {
5734 ++(b->hit_count);
8d3788bd 5735 observer_notify_breakpoint_modified (b);
c906108c 5736
4a64f543 5737 /* We will stop here. */
429374b8
JK
5738 if (b->disposition == disp_disable)
5739 {
816338b5 5740 --(b->enable_count);
1a853c52 5741 if (b->enable_count <= 0)
429374b8 5742 b->enable_state = bp_disabled;
f431efe5 5743 removed_any = 1;
429374b8
JK
5744 }
5745 if (b->silent)
5746 bs->print = 0;
5747 bs->commands = b->commands;
9add0f1b 5748 incref_counted_command_line (bs->commands);
abf85f46
JK
5749 if (command_line_is_silent (bs->commands
5750 ? bs->commands->commands : NULL))
5751 bs->print = 0;
9d6e6e84
HZ
5752
5753 b->ops->after_condition_true (bs);
429374b8
JK
5754 }
5755
348d480f 5756 }
a9b3a50f
PA
5757
5758 /* Print nothing for this entry if we don't stop or don't
5759 print. */
5760 if (!bs->stop || !bs->print)
5761 bs->print_it = print_it_noop;
429374b8 5762 }
876fa593 5763
d983da9c
DJ
5764 /* If we aren't stopping, the value of some hardware watchpoint may
5765 not have changed, but the intermediate memory locations we are
5766 watching may have. Don't bother if we're stopping; this will get
5767 done later. */
d832cb68 5768 need_remove_insert = 0;
5760d0ab
JK
5769 if (! bpstat_causes_stop (bs_head))
5770 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5771 if (!bs->stop
f431efe5
PA
5772 && bs->breakpoint_at
5773 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5774 {
3a5c3e22
PA
5775 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5776
5777 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5778 need_remove_insert = 1;
d983da9c
DJ
5779 }
5780
d832cb68 5781 if (need_remove_insert)
44702360 5782 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5783 else if (removed_any)
44702360 5784 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5785
5760d0ab 5786 return bs_head;
c906108c 5787}
628fe4e4
JK
5788
5789static void
5790handle_jit_event (void)
5791{
5792 struct frame_info *frame;
5793 struct gdbarch *gdbarch;
5794
243a9253
PA
5795 if (debug_infrun)
5796 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5797
628fe4e4
JK
5798 /* Switch terminal for any messages produced by
5799 breakpoint_re_set. */
5800 target_terminal_ours_for_output ();
5801
5802 frame = get_current_frame ();
5803 gdbarch = get_frame_arch (frame);
5804
5805 jit_event_handler (gdbarch);
5806
5807 target_terminal_inferior ();
5808}
5809
5810/* Prepare WHAT final decision for infrun. */
5811
5812/* Decide what infrun needs to do with this bpstat. */
5813
c906108c 5814struct bpstat_what
0e30163f 5815bpstat_what (bpstat bs_head)
c906108c 5816{
c906108c 5817 struct bpstat_what retval;
0e30163f 5818 bpstat bs;
c906108c 5819
628fe4e4 5820 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5821 retval.call_dummy = STOP_NONE;
186c406b 5822 retval.is_longjmp = 0;
628fe4e4 5823
0e30163f 5824 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5825 {
628fe4e4
JK
5826 /* Extract this BS's action. After processing each BS, we check
5827 if its action overrides all we've seem so far. */
5828 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5829 enum bptype bptype;
5830
c906108c 5831 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5832 {
5833 /* I suspect this can happen if it was a momentary
5834 breakpoint which has since been deleted. */
5835 bptype = bp_none;
5836 }
20874c92 5837 else
f431efe5 5838 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5839
5840 switch (bptype)
c906108c
SS
5841 {
5842 case bp_none:
628fe4e4 5843 break;
c906108c
SS
5844 case bp_breakpoint:
5845 case bp_hardware_breakpoint:
7c16b83e 5846 case bp_single_step:
c906108c
SS
5847 case bp_until:
5848 case bp_finish:
a9b3a50f 5849 case bp_shlib_event:
c906108c
SS
5850 if (bs->stop)
5851 {
5852 if (bs->print)
628fe4e4 5853 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5854 else
628fe4e4 5855 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5856 }
5857 else
628fe4e4 5858 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5859 break;
5860 case bp_watchpoint:
5861 case bp_hardware_watchpoint:
5862 case bp_read_watchpoint:
5863 case bp_access_watchpoint:
5864 if (bs->stop)
5865 {
5866 if (bs->print)
628fe4e4 5867 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5868 else
628fe4e4 5869 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5870 }
5871 else
628fe4e4
JK
5872 {
5873 /* There was a watchpoint, but we're not stopping.
5874 This requires no further action. */
5875 }
c906108c
SS
5876 break;
5877 case bp_longjmp:
e2e4d78b 5878 case bp_longjmp_call_dummy:
186c406b 5879 case bp_exception:
0a39bb32
PA
5880 if (bs->stop)
5881 {
5882 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5883 retval.is_longjmp = bptype != bp_exception;
5884 }
5885 else
5886 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5887 break;
5888 case bp_longjmp_resume:
186c406b 5889 case bp_exception_resume:
0a39bb32
PA
5890 if (bs->stop)
5891 {
5892 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5893 retval.is_longjmp = bptype == bp_longjmp_resume;
5894 }
5895 else
5896 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5897 break;
5898 case bp_step_resume:
5899 if (bs->stop)
628fe4e4
JK
5900 this_action = BPSTAT_WHAT_STEP_RESUME;
5901 else
c906108c 5902 {
628fe4e4
JK
5903 /* It is for the wrong frame. */
5904 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5905 }
c906108c 5906 break;
2c03e5be
PA
5907 case bp_hp_step_resume:
5908 if (bs->stop)
5909 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5910 else
5911 {
5912 /* It is for the wrong frame. */
5913 this_action = BPSTAT_WHAT_SINGLE;
5914 }
5915 break;
c906108c 5916 case bp_watchpoint_scope:
c4093a6a 5917 case bp_thread_event:
1900040c 5918 case bp_overlay_event:
0fd8e87f 5919 case bp_longjmp_master:
aa7d318d 5920 case bp_std_terminate_master:
186c406b 5921 case bp_exception_master:
628fe4e4 5922 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5923 break;
ce78b96d 5924 case bp_catchpoint:
c5aa993b
JM
5925 if (bs->stop)
5926 {
5927 if (bs->print)
628fe4e4 5928 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5929 else
628fe4e4 5930 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5931 }
5932 else
628fe4e4
JK
5933 {
5934 /* There was a catchpoint, but we're not stopping.
5935 This requires no further action. */
5936 }
5937 break;
628fe4e4 5938 case bp_jit_event:
628fe4e4 5939 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5940 break;
c906108c 5941 case bp_call_dummy:
53a5351d
JM
5942 /* Make sure the action is stop (silent or noisy),
5943 so infrun.c pops the dummy frame. */
aa7d318d 5944 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5945 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5946 break;
5947 case bp_std_terminate:
5948 /* Make sure the action is stop (silent or noisy),
5949 so infrun.c pops the dummy frame. */
aa7d318d 5950 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5951 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5952 break;
1042e4c0 5953 case bp_tracepoint:
7a697b8d 5954 case bp_fast_tracepoint:
0fb4aa4b 5955 case bp_static_tracepoint:
1042e4c0
SS
5956 /* Tracepoint hits should not be reported back to GDB, and
5957 if one got through somehow, it should have been filtered
5958 out already. */
5959 internal_error (__FILE__, __LINE__,
7a697b8d 5960 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5961 break;
5962 case bp_gnu_ifunc_resolver:
5963 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5964 this_action = BPSTAT_WHAT_SINGLE;
5965 break;
5966 case bp_gnu_ifunc_resolver_return:
5967 /* The breakpoint will be removed, execution will restart from the
5968 PC of the former breakpoint. */
5969 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5970 break;
e7e0cddf
SS
5971
5972 case bp_dprintf:
a11cfd87
HZ
5973 if (bs->stop)
5974 this_action = BPSTAT_WHAT_STOP_SILENT;
5975 else
5976 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5977 break;
5978
628fe4e4
JK
5979 default:
5980 internal_error (__FILE__, __LINE__,
5981 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5982 }
628fe4e4 5983
325fac50 5984 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5985 }
628fe4e4 5986
243a9253
PA
5987 return retval;
5988}
628fe4e4 5989
243a9253
PA
5990void
5991bpstat_run_callbacks (bpstat bs_head)
5992{
5993 bpstat bs;
628fe4e4 5994
0e30163f
JK
5995 for (bs = bs_head; bs != NULL; bs = bs->next)
5996 {
5997 struct breakpoint *b = bs->breakpoint_at;
5998
5999 if (b == NULL)
6000 continue;
6001 switch (b->type)
6002 {
243a9253
PA
6003 case bp_jit_event:
6004 handle_jit_event ();
6005 break;
0e30163f
JK
6006 case bp_gnu_ifunc_resolver:
6007 gnu_ifunc_resolver_stop (b);
6008 break;
6009 case bp_gnu_ifunc_resolver_return:
6010 gnu_ifunc_resolver_return_stop (b);
6011 break;
6012 }
6013 }
c906108c
SS
6014}
6015
6016/* Nonzero if we should step constantly (e.g. watchpoints on machines
6017 without hardware support). This isn't related to a specific bpstat,
6018 just to things like whether watchpoints are set. */
6019
c5aa993b 6020int
fba45db2 6021bpstat_should_step (void)
c906108c
SS
6022{
6023 struct breakpoint *b;
cc59ec59 6024
c906108c 6025 ALL_BREAKPOINTS (b)
717a8278 6026 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6027 return 1;
c906108c
SS
6028 return 0;
6029}
6030
67822962
PA
6031int
6032bpstat_causes_stop (bpstat bs)
6033{
6034 for (; bs != NULL; bs = bs->next)
6035 if (bs->stop)
6036 return 1;
6037
6038 return 0;
6039}
6040
c906108c 6041\f
c5aa993b 6042
170b53b2
UW
6043/* Compute a string of spaces suitable to indent the next line
6044 so it starts at the position corresponding to the table column
6045 named COL_NAME in the currently active table of UIOUT. */
6046
6047static char *
6048wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6049{
6050 static char wrap_indent[80];
6051 int i, total_width, width, align;
c5209615 6052 const char *text;
170b53b2
UW
6053
6054 total_width = 0;
112e8700 6055 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
6056 {
6057 if (strcmp (text, col_name) == 0)
6058 {
6059 gdb_assert (total_width < sizeof wrap_indent);
6060 memset (wrap_indent, ' ', total_width);
6061 wrap_indent[total_width] = 0;
6062
6063 return wrap_indent;
6064 }
6065
6066 total_width += width + 1;
6067 }
6068
6069 return NULL;
6070}
6071
b775012e
LM
6072/* Determine if the locations of this breakpoint will have their conditions
6073 evaluated by the target, host or a mix of both. Returns the following:
6074
6075 "host": Host evals condition.
6076 "host or target": Host or Target evals condition.
6077 "target": Target evals condition.
6078*/
6079
6080static const char *
6081bp_condition_evaluator (struct breakpoint *b)
6082{
6083 struct bp_location *bl;
6084 char host_evals = 0;
6085 char target_evals = 0;
6086
6087 if (!b)
6088 return NULL;
6089
6090 if (!is_breakpoint (b))
6091 return NULL;
6092
6093 if (gdb_evaluates_breakpoint_condition_p ()
6094 || !target_supports_evaluation_of_breakpoint_conditions ())
6095 return condition_evaluation_host;
6096
6097 for (bl = b->loc; bl; bl = bl->next)
6098 {
6099 if (bl->cond_bytecode)
6100 target_evals++;
6101 else
6102 host_evals++;
6103 }
6104
6105 if (host_evals && target_evals)
6106 return condition_evaluation_both;
6107 else if (target_evals)
6108 return condition_evaluation_target;
6109 else
6110 return condition_evaluation_host;
6111}
6112
6113/* Determine the breakpoint location's condition evaluator. This is
6114 similar to bp_condition_evaluator, but for locations. */
6115
6116static const char *
6117bp_location_condition_evaluator (struct bp_location *bl)
6118{
6119 if (bl && !is_breakpoint (bl->owner))
6120 return NULL;
6121
6122 if (gdb_evaluates_breakpoint_condition_p ()
6123 || !target_supports_evaluation_of_breakpoint_conditions ())
6124 return condition_evaluation_host;
6125
6126 if (bl && bl->cond_bytecode)
6127 return condition_evaluation_target;
6128 else
6129 return condition_evaluation_host;
6130}
6131
859825b8
JK
6132/* Print the LOC location out of the list of B->LOC locations. */
6133
170b53b2
UW
6134static void
6135print_breakpoint_location (struct breakpoint *b,
6136 struct bp_location *loc)
0d381245 6137{
79a45e25 6138 struct ui_out *uiout = current_uiout;
5ed8105e
PA
6139
6140 scoped_restore_current_program_space restore_pspace;
6c95b8df 6141
859825b8
JK
6142 if (loc != NULL && loc->shlib_disabled)
6143 loc = NULL;
6144
6c95b8df
PA
6145 if (loc != NULL)
6146 set_current_program_space (loc->pspace);
6147
56435ebe 6148 if (b->display_canonical)
d28cd78a 6149 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 6150 else if (loc && loc->symtab)
0d381245
VP
6151 {
6152 struct symbol *sym
6153 = find_pc_sect_function (loc->address, loc->section);
6154 if (sym)
6155 {
112e8700
SM
6156 uiout->text ("in ");
6157 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6158 uiout->text (" ");
6159 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6160 uiout->text ("at ");
0d381245 6161 }
112e8700 6162 uiout->field_string ("file",
05cba821 6163 symtab_to_filename_for_display (loc->symtab));
112e8700 6164 uiout->text (":");
05cba821 6165
112e8700
SM
6166 if (uiout->is_mi_like_p ())
6167 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 6168
112e8700 6169 uiout->field_int ("line", loc->line_number);
0d381245 6170 }
859825b8 6171 else if (loc)
0d381245 6172 {
d7e74731 6173 string_file stb;
170b53b2 6174
d7e74731 6175 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 6176 demangle, "");
112e8700 6177 uiout->field_stream ("at", stb);
0d381245 6178 }
859825b8 6179 else
f00aae0f 6180 {
d28cd78a
TT
6181 uiout->field_string ("pending",
6182 event_location_to_string (b->location.get ()));
f00aae0f
KS
6183 /* If extra_string is available, it could be holding a condition
6184 or dprintf arguments. In either case, make sure it is printed,
6185 too, but only for non-MI streams. */
112e8700 6186 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
6187 {
6188 if (b->type == bp_dprintf)
112e8700 6189 uiout->text (",");
f00aae0f 6190 else
112e8700
SM
6191 uiout->text (" ");
6192 uiout->text (b->extra_string);
f00aae0f
KS
6193 }
6194 }
6c95b8df 6195
b775012e
LM
6196 if (loc && is_breakpoint (b)
6197 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6198 && bp_condition_evaluator (b) == condition_evaluation_both)
6199 {
112e8700
SM
6200 uiout->text (" (");
6201 uiout->field_string ("evaluated-by",
b775012e 6202 bp_location_condition_evaluator (loc));
112e8700 6203 uiout->text (")");
b775012e 6204 }
0d381245
VP
6205}
6206
269b11a2
PA
6207static const char *
6208bptype_string (enum bptype type)
c906108c 6209{
c4093a6a
JM
6210 struct ep_type_description
6211 {
6212 enum bptype type;
a121b7c1 6213 const char *description;
c4093a6a
JM
6214 };
6215 static struct ep_type_description bptypes[] =
c906108c 6216 {
c5aa993b
JM
6217 {bp_none, "?deleted?"},
6218 {bp_breakpoint, "breakpoint"},
c906108c 6219 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6220 {bp_single_step, "sw single-step"},
c5aa993b
JM
6221 {bp_until, "until"},
6222 {bp_finish, "finish"},
6223 {bp_watchpoint, "watchpoint"},
c906108c 6224 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6225 {bp_read_watchpoint, "read watchpoint"},
6226 {bp_access_watchpoint, "acc watchpoint"},
6227 {bp_longjmp, "longjmp"},
6228 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6229 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6230 {bp_exception, "exception"},
6231 {bp_exception_resume, "exception resume"},
c5aa993b 6232 {bp_step_resume, "step resume"},
2c03e5be 6233 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6234 {bp_watchpoint_scope, "watchpoint scope"},
6235 {bp_call_dummy, "call dummy"},
aa7d318d 6236 {bp_std_terminate, "std::terminate"},
c5aa993b 6237 {bp_shlib_event, "shlib events"},
c4093a6a 6238 {bp_thread_event, "thread events"},
1900040c 6239 {bp_overlay_event, "overlay events"},
0fd8e87f 6240 {bp_longjmp_master, "longjmp master"},
aa7d318d 6241 {bp_std_terminate_master, "std::terminate master"},
186c406b 6242 {bp_exception_master, "exception master"},
ce78b96d 6243 {bp_catchpoint, "catchpoint"},
1042e4c0 6244 {bp_tracepoint, "tracepoint"},
7a697b8d 6245 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6246 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6247 {bp_dprintf, "dprintf"},
4efc6507 6248 {bp_jit_event, "jit events"},
0e30163f
JK
6249 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6250 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6251 };
269b11a2
PA
6252
6253 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6254 || ((int) type != bptypes[(int) type].type))
6255 internal_error (__FILE__, __LINE__,
6256 _("bptypes table does not describe type #%d."),
6257 (int) type);
6258
6259 return bptypes[(int) type].description;
6260}
6261
998580f1
MK
6262/* For MI, output a field named 'thread-groups' with a list as the value.
6263 For CLI, prefix the list with the string 'inf'. */
6264
6265static void
6266output_thread_groups (struct ui_out *uiout,
6267 const char *field_name,
6268 VEC(int) *inf_num,
6269 int mi_only)
6270{
112e8700 6271 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6272 int inf;
6273 int i;
6274
6275 /* For backward compatibility, don't display inferiors in CLI unless
6276 there are several. Always display them for MI. */
6277 if (!is_mi && mi_only)
6278 return;
6279
10f489e5 6280 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6281
998580f1
MK
6282 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6283 {
6284 if (is_mi)
6285 {
6286 char mi_group[10];
6287
6288 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6289 uiout->field_string (NULL, mi_group);
998580f1
MK
6290 }
6291 else
6292 {
6293 if (i == 0)
112e8700 6294 uiout->text (" inf ");
998580f1 6295 else
112e8700 6296 uiout->text (", ");
998580f1 6297
112e8700 6298 uiout->text (plongest (inf));
998580f1
MK
6299 }
6300 }
998580f1
MK
6301}
6302
269b11a2
PA
6303/* Print B to gdb_stdout. */
6304
6305static void
6306print_one_breakpoint_location (struct breakpoint *b,
6307 struct bp_location *loc,
6308 int loc_number,
6309 struct bp_location **last_loc,
269b11a2
PA
6310 int allflag)
6311{
6312 struct command_line *l;
c2c6d25f 6313 static char bpenables[] = "nynny";
c906108c 6314
79a45e25 6315 struct ui_out *uiout = current_uiout;
0d381245
VP
6316 int header_of_multiple = 0;
6317 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6318 struct value_print_options opts;
6319
6320 get_user_print_options (&opts);
0d381245
VP
6321
6322 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6323 /* See comment in print_one_breakpoint concerning treatment of
6324 breakpoints with single disabled location. */
0d381245
VP
6325 if (loc == NULL
6326 && (b->loc != NULL
6327 && (b->loc->next != NULL || !b->loc->enabled)))
6328 header_of_multiple = 1;
6329 if (loc == NULL)
6330 loc = b->loc;
6331
c4093a6a
JM
6332 annotate_record ();
6333
6334 /* 1 */
6335 annotate_field (0);
0d381245
VP
6336 if (part_of_multiple)
6337 {
6338 char *formatted;
0c6773c1 6339 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6340 uiout->field_string ("number", formatted);
0d381245
VP
6341 xfree (formatted);
6342 }
6343 else
6344 {
112e8700 6345 uiout->field_int ("number", b->number);
0d381245 6346 }
c4093a6a
JM
6347
6348 /* 2 */
6349 annotate_field (1);
0d381245 6350 if (part_of_multiple)
112e8700 6351 uiout->field_skip ("type");
269b11a2 6352 else
112e8700 6353 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6354
6355 /* 3 */
6356 annotate_field (2);
0d381245 6357 if (part_of_multiple)
112e8700 6358 uiout->field_skip ("disp");
0d381245 6359 else
112e8700 6360 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6361
c4093a6a
JM
6362
6363 /* 4 */
6364 annotate_field (3);
0d381245 6365 if (part_of_multiple)
112e8700 6366 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6367 else
112e8700
SM
6368 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6369 uiout->spaces (2);
0d381245 6370
c4093a6a
JM
6371
6372 /* 5 and 6 */
3086aeae 6373 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6374 {
4a64f543
MS
6375 /* Although the print_one can possibly print all locations,
6376 calling it here is not likely to get any nice result. So,
6377 make sure there's just one location. */
0d381245 6378 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6379 b->ops->print_one (b, last_loc);
0d381245 6380 }
3086aeae
DJ
6381 else
6382 switch (b->type)
6383 {
6384 case bp_none:
6385 internal_error (__FILE__, __LINE__,
e2e0b3e5 6386 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6387 break;
c906108c 6388
3086aeae
DJ
6389 case bp_watchpoint:
6390 case bp_hardware_watchpoint:
6391 case bp_read_watchpoint:
6392 case bp_access_watchpoint:
3a5c3e22
PA
6393 {
6394 struct watchpoint *w = (struct watchpoint *) b;
6395
6396 /* Field 4, the address, is omitted (which makes the columns
6397 not line up too nicely with the headers, but the effect
6398 is relatively readable). */
6399 if (opts.addressprint)
112e8700 6400 uiout->field_skip ("addr");
3a5c3e22 6401 annotate_field (5);
112e8700 6402 uiout->field_string ("what", w->exp_string);
3a5c3e22 6403 }
3086aeae
DJ
6404 break;
6405
3086aeae
DJ
6406 case bp_breakpoint:
6407 case bp_hardware_breakpoint:
7c16b83e 6408 case bp_single_step:
3086aeae
DJ
6409 case bp_until:
6410 case bp_finish:
6411 case bp_longjmp:
6412 case bp_longjmp_resume:
e2e4d78b 6413 case bp_longjmp_call_dummy:
186c406b
TT
6414 case bp_exception:
6415 case bp_exception_resume:
3086aeae 6416 case bp_step_resume:
2c03e5be 6417 case bp_hp_step_resume:
3086aeae
DJ
6418 case bp_watchpoint_scope:
6419 case bp_call_dummy:
aa7d318d 6420 case bp_std_terminate:
3086aeae
DJ
6421 case bp_shlib_event:
6422 case bp_thread_event:
6423 case bp_overlay_event:
0fd8e87f 6424 case bp_longjmp_master:
aa7d318d 6425 case bp_std_terminate_master:
186c406b 6426 case bp_exception_master:
1042e4c0 6427 case bp_tracepoint:
7a697b8d 6428 case bp_fast_tracepoint:
0fb4aa4b 6429 case bp_static_tracepoint:
e7e0cddf 6430 case bp_dprintf:
4efc6507 6431 case bp_jit_event:
0e30163f
JK
6432 case bp_gnu_ifunc_resolver:
6433 case bp_gnu_ifunc_resolver_return:
79a45b7d 6434 if (opts.addressprint)
3086aeae
DJ
6435 {
6436 annotate_field (4);
54e52265 6437 if (header_of_multiple)
112e8700 6438 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6439 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6440 uiout->field_string ("addr", "<PENDING>");
0101ce28 6441 else
112e8700 6442 uiout->field_core_addr ("addr",
5af949e3 6443 loc->gdbarch, loc->address);
3086aeae
DJ
6444 }
6445 annotate_field (5);
0d381245 6446 if (!header_of_multiple)
170b53b2 6447 print_breakpoint_location (b, loc);
0d381245 6448 if (b->loc)
a6d9a66e 6449 *last_loc = b->loc;
3086aeae
DJ
6450 break;
6451 }
c906108c 6452
6c95b8df 6453
998580f1 6454 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6455 {
6456 struct inferior *inf;
998580f1
MK
6457 VEC(int) *inf_num = NULL;
6458 int mi_only = 1;
6c95b8df 6459
998580f1 6460 ALL_INFERIORS (inf)
6c95b8df
PA
6461 {
6462 if (inf->pspace == loc->pspace)
998580f1 6463 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6464 }
998580f1
MK
6465
6466 /* For backward compatibility, don't display inferiors in CLI unless
6467 there are several. Always display for MI. */
6468 if (allflag
6469 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6470 && (number_of_program_spaces () > 1
6471 || number_of_inferiors () > 1)
6472 /* LOC is for existing B, it cannot be in
6473 moribund_locations and thus having NULL OWNER. */
6474 && loc->owner->type != bp_catchpoint))
6475 mi_only = 0;
6476 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6477 VEC_free (int, inf_num);
6c95b8df
PA
6478 }
6479
4a306c9a 6480 if (!part_of_multiple)
c4093a6a 6481 {
4a306c9a
JB
6482 if (b->thread != -1)
6483 {
6484 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6485 "stop only in" line a little further down. */
112e8700
SM
6486 uiout->text (" thread ");
6487 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6488 }
6489 else if (b->task != 0)
6490 {
112e8700
SM
6491 uiout->text (" task ");
6492 uiout->field_int ("task", b->task);
4a306c9a 6493 }
c4093a6a 6494 }
f1310107 6495
112e8700 6496 uiout->text ("\n");
f1310107 6497
348d480f 6498 if (!part_of_multiple)
f1310107
TJB
6499 b->ops->print_one_detail (b, uiout);
6500
0d381245 6501 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6502 {
6503 annotate_field (6);
112e8700 6504 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6505 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6506 the frame ID. */
112e8700 6507 uiout->field_core_addr ("frame",
5af949e3 6508 b->gdbarch, b->frame_id.stack_addr);
112e8700 6509 uiout->text ("\n");
c4093a6a
JM
6510 }
6511
28010a5d 6512 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6513 {
6514 annotate_field (7);
d77f58be 6515 if (is_tracepoint (b))
112e8700 6516 uiout->text ("\ttrace only if ");
1042e4c0 6517 else
112e8700
SM
6518 uiout->text ("\tstop only if ");
6519 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6520
6521 /* Print whether the target is doing the breakpoint's condition
6522 evaluation. If GDB is doing the evaluation, don't print anything. */
6523 if (is_breakpoint (b)
6524 && breakpoint_condition_evaluation_mode ()
6525 == condition_evaluation_target)
6526 {
112e8700
SM
6527 uiout->text (" (");
6528 uiout->field_string ("evaluated-by",
b775012e 6529 bp_condition_evaluator (b));
112e8700 6530 uiout->text (" evals)");
b775012e 6531 }
112e8700 6532 uiout->text ("\n");
0101ce28
JJ
6533 }
6534
0d381245 6535 if (!part_of_multiple && b->thread != -1)
c4093a6a 6536 {
4a64f543 6537 /* FIXME should make an annotation for this. */
112e8700
SM
6538 uiout->text ("\tstop only in thread ");
6539 if (uiout->is_mi_like_p ())
6540 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6541 else
6542 {
6543 struct thread_info *thr = find_thread_global_id (b->thread);
6544
112e8700 6545 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6546 }
112e8700 6547 uiout->text ("\n");
c4093a6a
JM
6548 }
6549
556ec64d
YQ
6550 if (!part_of_multiple)
6551 {
6552 if (b->hit_count)
31f56a27
YQ
6553 {
6554 /* FIXME should make an annotation for this. */
6555 if (is_catchpoint (b))
112e8700 6556 uiout->text ("\tcatchpoint");
31f56a27 6557 else if (is_tracepoint (b))
112e8700 6558 uiout->text ("\ttracepoint");
31f56a27 6559 else
112e8700
SM
6560 uiout->text ("\tbreakpoint");
6561 uiout->text (" already hit ");
6562 uiout->field_int ("times", b->hit_count);
31f56a27 6563 if (b->hit_count == 1)
112e8700 6564 uiout->text (" time\n");
31f56a27 6565 else
112e8700 6566 uiout->text (" times\n");
31f56a27 6567 }
556ec64d
YQ
6568 else
6569 {
31f56a27 6570 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6571 if (uiout->is_mi_like_p ())
6572 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6573 }
6574 }
8b93c638 6575
0d381245 6576 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6577 {
6578 annotate_field (8);
112e8700
SM
6579 uiout->text ("\tignore next ");
6580 uiout->field_int ("ignore", b->ignore_count);
6581 uiout->text (" hits\n");
c4093a6a 6582 }
059fb39f 6583
816338b5
SS
6584 /* Note that an enable count of 1 corresponds to "enable once"
6585 behavior, which is reported by the combination of enablement and
6586 disposition, so we don't need to mention it here. */
6587 if (!part_of_multiple && b->enable_count > 1)
6588 {
6589 annotate_field (8);
112e8700 6590 uiout->text ("\tdisable after ");
816338b5
SS
6591 /* Tweak the wording to clarify that ignore and enable counts
6592 are distinct, and have additive effect. */
6593 if (b->ignore_count)
112e8700 6594 uiout->text ("additional ");
816338b5 6595 else
112e8700
SM
6596 uiout->text ("next ");
6597 uiout->field_int ("enable", b->enable_count);
6598 uiout->text (" hits\n");
816338b5
SS
6599 }
6600
f196051f
SS
6601 if (!part_of_multiple && is_tracepoint (b))
6602 {
6603 struct tracepoint *tp = (struct tracepoint *) b;
6604
6605 if (tp->traceframe_usage)
6606 {
112e8700
SM
6607 uiout->text ("\ttrace buffer usage ");
6608 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6609 uiout->text (" bytes\n");
f196051f
SS
6610 }
6611 }
d3ce09f5 6612
9add0f1b 6613 l = b->commands ? b->commands->commands : NULL;
059fb39f 6614 if (!part_of_multiple && l)
c4093a6a
JM
6615 {
6616 annotate_field (9);
2e783024 6617 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6618 print_command_lines (uiout, l, 4);
c4093a6a 6619 }
d24317b4 6620
d9b3f62e 6621 if (is_tracepoint (b))
1042e4c0 6622 {
d9b3f62e
PA
6623 struct tracepoint *t = (struct tracepoint *) b;
6624
6625 if (!part_of_multiple && t->pass_count)
6626 {
6627 annotate_field (10);
112e8700
SM
6628 uiout->text ("\tpass count ");
6629 uiout->field_int ("pass", t->pass_count);
6630 uiout->text (" \n");
d9b3f62e 6631 }
f2a8bc8a
YQ
6632
6633 /* Don't display it when tracepoint or tracepoint location is
6634 pending. */
6635 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6636 {
6637 annotate_field (11);
6638
112e8700
SM
6639 if (uiout->is_mi_like_p ())
6640 uiout->field_string ("installed",
f2a8bc8a
YQ
6641 loc->inserted ? "y" : "n");
6642 else
6643 {
6644 if (loc->inserted)
112e8700 6645 uiout->text ("\t");
f2a8bc8a 6646 else
112e8700
SM
6647 uiout->text ("\tnot ");
6648 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6649 }
6650 }
1042e4c0
SS
6651 }
6652
112e8700 6653 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6654 {
3a5c3e22
PA
6655 if (is_watchpoint (b))
6656 {
6657 struct watchpoint *w = (struct watchpoint *) b;
6658
112e8700 6659 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6660 }
f00aae0f 6661 else if (b->location != NULL
d28cd78a 6662 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6663 uiout->field_string ("original-location",
d28cd78a 6664 event_location_to_string (b->location.get ()));
d24317b4 6665 }
c4093a6a 6666}
c5aa993b 6667
0d381245
VP
6668static void
6669print_one_breakpoint (struct breakpoint *b,
4a64f543 6670 struct bp_location **last_loc,
6c95b8df 6671 int allflag)
0d381245 6672{
79a45e25 6673 struct ui_out *uiout = current_uiout;
8d3788bd 6674
2e783024
TT
6675 {
6676 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6677
2e783024
TT
6678 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6679 }
0d381245
VP
6680
6681 /* If this breakpoint has custom print function,
6682 it's already printed. Otherwise, print individual
6683 locations, if any. */
6684 if (b->ops == NULL || b->ops->print_one == NULL)
6685 {
4a64f543
MS
6686 /* If breakpoint has a single location that is disabled, we
6687 print it as if it had several locations, since otherwise it's
6688 hard to represent "breakpoint enabled, location disabled"
6689 situation.
6690
6691 Note that while hardware watchpoints have several locations
a3be7890 6692 internally, that's not a property exposed to user. */
0d381245 6693 if (b->loc
a5606eee 6694 && !is_hardware_watchpoint (b)
8d3788bd 6695 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6696 {
6697 struct bp_location *loc;
6698 int n = 1;
8d3788bd 6699
0d381245 6700 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6701 {
2e783024 6702 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6703 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6704 }
0d381245
VP
6705 }
6706 }
6707}
6708
a6d9a66e
UW
6709static int
6710breakpoint_address_bits (struct breakpoint *b)
6711{
6712 int print_address_bits = 0;
6713 struct bp_location *loc;
6714
c6d81124
PA
6715 /* Software watchpoints that aren't watching memory don't have an
6716 address to print. */
6717 if (is_no_memory_software_watchpoint (b))
6718 return 0;
6719
a6d9a66e
UW
6720 for (loc = b->loc; loc; loc = loc->next)
6721 {
c7437ca6
PA
6722 int addr_bit;
6723
c7437ca6 6724 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6725 if (addr_bit > print_address_bits)
6726 print_address_bits = addr_bit;
6727 }
6728
6729 return print_address_bits;
6730}
0d381245 6731
c4093a6a
JM
6732struct captured_breakpoint_query_args
6733 {
6734 int bnum;
6735 };
c5aa993b 6736
c4093a6a 6737static int
2b65245e 6738do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6739{
9a3c8263
SM
6740 struct captured_breakpoint_query_args *args
6741 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6742 struct breakpoint *b;
a6d9a66e 6743 struct bp_location *dummy_loc = NULL;
cc59ec59 6744
c4093a6a
JM
6745 ALL_BREAKPOINTS (b)
6746 {
6747 if (args->bnum == b->number)
c5aa993b 6748 {
12c5a436 6749 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6750 return GDB_RC_OK;
c5aa993b 6751 }
c4093a6a
JM
6752 }
6753 return GDB_RC_NONE;
6754}
c5aa993b 6755
c4093a6a 6756enum gdb_rc
4a64f543
MS
6757gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6758 char **error_message)
c4093a6a
JM
6759{
6760 struct captured_breakpoint_query_args args;
cc59ec59 6761
c4093a6a
JM
6762 args.bnum = bnum;
6763 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6764 an error. */
b0b13bb4
DJ
6765 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6766 error_message, RETURN_MASK_ALL) < 0)
6767 return GDB_RC_FAIL;
6768 else
6769 return GDB_RC_OK;
c4093a6a 6770}
c5aa993b 6771
09d682a4
TT
6772/* Return true if this breakpoint was set by the user, false if it is
6773 internal or momentary. */
6774
6775int
6776user_breakpoint_p (struct breakpoint *b)
6777{
46c6471b 6778 return b->number > 0;
09d682a4
TT
6779}
6780
93daf339
TT
6781/* See breakpoint.h. */
6782
6783int
6784pending_breakpoint_p (struct breakpoint *b)
6785{
6786 return b->loc == NULL;
6787}
6788
7f3b0473 6789/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6790 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6791 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6792 FILTER is non-NULL, call it on each breakpoint and only include the
6793 ones for which it returns non-zero. Return the total number of
6794 breakpoints listed. */
c906108c 6795
d77f58be 6796static int
e5a67952 6797breakpoint_1 (char *args, int allflag,
4a64f543 6798 int (*filter) (const struct breakpoint *))
c4093a6a 6799{
52f0bd74 6800 struct breakpoint *b;
a6d9a66e 6801 struct bp_location *last_loc = NULL;
7f3b0473 6802 int nr_printable_breakpoints;
79a45b7d 6803 struct value_print_options opts;
a6d9a66e 6804 int print_address_bits = 0;
269b11a2 6805 int print_type_col_width = 14;
79a45e25 6806 struct ui_out *uiout = current_uiout;
269b11a2 6807
79a45b7d
TT
6808 get_user_print_options (&opts);
6809
4a64f543
MS
6810 /* Compute the number of rows in the table, as well as the size
6811 required for address fields. */
7f3b0473
AC
6812 nr_printable_breakpoints = 0;
6813 ALL_BREAKPOINTS (b)
e5a67952
MS
6814 {
6815 /* If we have a filter, only list the breakpoints it accepts. */
6816 if (filter && !filter (b))
6817 continue;
6818
6819 /* If we have an "args" string, it is a list of breakpoints to
6820 accept. Skip the others. */
6821 if (args != NULL && *args != '\0')
6822 {
6823 if (allflag && parse_and_eval_long (args) != b->number)
6824 continue;
6825 if (!allflag && !number_is_in_list (args, b->number))
6826 continue;
6827 }
269b11a2 6828
e5a67952
MS
6829 if (allflag || user_breakpoint_p (b))
6830 {
6831 int addr_bit, type_len;
a6d9a66e 6832
e5a67952
MS
6833 addr_bit = breakpoint_address_bits (b);
6834 if (addr_bit > print_address_bits)
6835 print_address_bits = addr_bit;
269b11a2 6836
e5a67952
MS
6837 type_len = strlen (bptype_string (b->type));
6838 if (type_len > print_type_col_width)
6839 print_type_col_width = type_len;
6840
6841 nr_printable_breakpoints++;
6842 }
6843 }
7f3b0473 6844
4a2b031d
TT
6845 {
6846 ui_out_emit_table table_emitter (uiout,
6847 opts.addressprint ? 6 : 5,
6848 nr_printable_breakpoints,
6849 "BreakpointTable");
6850
6851 if (nr_printable_breakpoints > 0)
6852 annotate_breakpoints_headers ();
6853 if (nr_printable_breakpoints > 0)
6854 annotate_field (0);
6855 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6856 if (nr_printable_breakpoints > 0)
6857 annotate_field (1);
6858 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6859 if (nr_printable_breakpoints > 0)
6860 annotate_field (2);
6861 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (3);
6864 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6865 if (opts.addressprint)
6866 {
6867 if (nr_printable_breakpoints > 0)
6868 annotate_field (4);
6869 if (print_address_bits <= 32)
6870 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6871 else
6872 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6873 }
6874 if (nr_printable_breakpoints > 0)
6875 annotate_field (5);
6876 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6877 uiout->table_body ();
6878 if (nr_printable_breakpoints > 0)
6879 annotate_breakpoints_table ();
6880
6881 ALL_BREAKPOINTS (b)
6882 {
6883 QUIT;
6884 /* If we have a filter, only list the breakpoints it accepts. */
6885 if (filter && !filter (b))
6886 continue;
e5a67952 6887
4a2b031d
TT
6888 /* If we have an "args" string, it is a list of breakpoints to
6889 accept. Skip the others. */
e5a67952 6890
4a2b031d
TT
6891 if (args != NULL && *args != '\0')
6892 {
6893 if (allflag) /* maintenance info breakpoint */
6894 {
6895 if (parse_and_eval_long (args) != b->number)
6896 continue;
6897 }
6898 else /* all others */
6899 {
6900 if (!number_is_in_list (args, b->number))
6901 continue;
6902 }
6903 }
6904 /* We only print out user settable breakpoints unless the
6905 allflag is set. */
6906 if (allflag || user_breakpoint_p (b))
6907 print_one_breakpoint (b, &last_loc, allflag);
6908 }
6909 }
698384cd 6910
7f3b0473 6911 if (nr_printable_breakpoints == 0)
c906108c 6912 {
4a64f543
MS
6913 /* If there's a filter, let the caller decide how to report
6914 empty list. */
d77f58be
SS
6915 if (!filter)
6916 {
e5a67952 6917 if (args == NULL || *args == '\0')
112e8700 6918 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6919 else
112e8700 6920 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6921 args);
d77f58be 6922 }
c906108c
SS
6923 }
6924 else
c4093a6a 6925 {
a6d9a66e
UW
6926 if (last_loc && !server_command)
6927 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6928 }
c906108c 6929
4a64f543 6930 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6931 there have been breakpoints? */
c906108c 6932 annotate_breakpoints_table_end ();
d77f58be
SS
6933
6934 return nr_printable_breakpoints;
c906108c
SS
6935}
6936
ad443146
SS
6937/* Display the value of default-collect in a way that is generally
6938 compatible with the breakpoint list. */
6939
6940static void
6941default_collect_info (void)
6942{
79a45e25
PA
6943 struct ui_out *uiout = current_uiout;
6944
ad443146
SS
6945 /* If it has no value (which is frequently the case), say nothing; a
6946 message like "No default-collect." gets in user's face when it's
6947 not wanted. */
6948 if (!*default_collect)
6949 return;
6950
6951 /* The following phrase lines up nicely with per-tracepoint collect
6952 actions. */
112e8700
SM
6953 uiout->text ("default collect ");
6954 uiout->field_string ("default-collect", default_collect);
6955 uiout->text (" \n");
ad443146
SS
6956}
6957
c906108c 6958static void
11db9430 6959info_breakpoints_command (char *args, int from_tty)
c906108c 6960{
e5a67952 6961 breakpoint_1 (args, 0, NULL);
ad443146
SS
6962
6963 default_collect_info ();
d77f58be
SS
6964}
6965
6966static void
11db9430 6967info_watchpoints_command (char *args, int from_tty)
d77f58be 6968{
e5a67952 6969 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6970 struct ui_out *uiout = current_uiout;
d77f58be
SS
6971
6972 if (num_printed == 0)
6973 {
e5a67952 6974 if (args == NULL || *args == '\0')
112e8700 6975 uiout->message ("No watchpoints.\n");
d77f58be 6976 else
112e8700 6977 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6978 }
c906108c
SS
6979}
6980
7a292a7a 6981static void
e5a67952 6982maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6983{
e5a67952 6984 breakpoint_1 (args, 1, NULL);
ad443146
SS
6985
6986 default_collect_info ();
c906108c
SS
6987}
6988
0d381245 6989static int
714835d5 6990breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6991 struct program_space *pspace,
714835d5 6992 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6993{
6994 struct bp_location *bl = b->loc;
cc59ec59 6995
0d381245
VP
6996 for (; bl; bl = bl->next)
6997 {
6c95b8df
PA
6998 if (bl->pspace == pspace
6999 && bl->address == pc
0d381245
VP
7000 && (!overlay_debugging || bl->section == section))
7001 return 1;
7002 }
7003 return 0;
7004}
7005
672f9b60 7006/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7007 concerns with logical breakpoints, so we match program spaces, not
7008 address spaces. */
c906108c
SS
7009
7010static void
6c95b8df
PA
7011describe_other_breakpoints (struct gdbarch *gdbarch,
7012 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7013 struct obj_section *section, int thread)
c906108c 7014{
52f0bd74
AC
7015 int others = 0;
7016 struct breakpoint *b;
c906108c
SS
7017
7018 ALL_BREAKPOINTS (b)
672f9b60
KP
7019 others += (user_breakpoint_p (b)
7020 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7021 if (others > 0)
7022 {
a3f17187
AC
7023 if (others == 1)
7024 printf_filtered (_("Note: breakpoint "));
7025 else /* if (others == ???) */
7026 printf_filtered (_("Note: breakpoints "));
c906108c 7027 ALL_BREAKPOINTS (b)
672f9b60 7028 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7029 {
7030 others--;
7031 printf_filtered ("%d", b->number);
7032 if (b->thread == -1 && thread != -1)
7033 printf_filtered (" (all threads)");
7034 else if (b->thread != -1)
7035 printf_filtered (" (thread %d)", b->thread);
7036 printf_filtered ("%s%s ",
059fb39f 7037 ((b->enable_state == bp_disabled
f8eba3c6 7038 || b->enable_state == bp_call_disabled)
0d381245 7039 ? " (disabled)"
0d381245
VP
7040 : ""),
7041 (others > 1) ? ","
7042 : ((others == 1) ? " and" : ""));
7043 }
a3f17187 7044 printf_filtered (_("also set at pc "));
5af949e3 7045 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7046 printf_filtered (".\n");
7047 }
7048}
7049\f
c906108c 7050
e4f237da 7051/* Return true iff it is meaningful to use the address member of
244558af
LM
7052 BPT locations. For some breakpoint types, the locations' address members
7053 are irrelevant and it makes no sense to attempt to compare them to other
7054 addresses (or use them for any other purpose either).
e4f237da 7055
4a64f543 7056 More specifically, each of the following breakpoint types will
244558af 7057 always have a zero valued location address and we don't want to mark
4a64f543 7058 breakpoints of any of these types to be a duplicate of an actual
244558af 7059 breakpoint location at address zero:
e4f237da
KB
7060
7061 bp_watchpoint
2d134ed3
PA
7062 bp_catchpoint
7063
7064*/
e4f237da
KB
7065
7066static int
7067breakpoint_address_is_meaningful (struct breakpoint *bpt)
7068{
7069 enum bptype type = bpt->type;
7070
2d134ed3
PA
7071 return (type != bp_watchpoint && type != bp_catchpoint);
7072}
7073
7074/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7075 true if LOC1 and LOC2 represent the same watchpoint location. */
7076
7077static int
4a64f543
MS
7078watchpoint_locations_match (struct bp_location *loc1,
7079 struct bp_location *loc2)
2d134ed3 7080{
3a5c3e22
PA
7081 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7082 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7083
7084 /* Both of them must exist. */
7085 gdb_assert (w1 != NULL);
7086 gdb_assert (w2 != NULL);
2bdf28a0 7087
4a64f543
MS
7088 /* If the target can evaluate the condition expression in hardware,
7089 then we we need to insert both watchpoints even if they are at
7090 the same place. Otherwise the watchpoint will only trigger when
7091 the condition of whichever watchpoint was inserted evaluates to
7092 true, not giving a chance for GDB to check the condition of the
7093 other watchpoint. */
3a5c3e22 7094 if ((w1->cond_exp
4a64f543
MS
7095 && target_can_accel_watchpoint_condition (loc1->address,
7096 loc1->length,
0cf6dd15 7097 loc1->watchpoint_type,
4d01a485 7098 w1->cond_exp.get ()))
3a5c3e22 7099 || (w2->cond_exp
4a64f543
MS
7100 && target_can_accel_watchpoint_condition (loc2->address,
7101 loc2->length,
0cf6dd15 7102 loc2->watchpoint_type,
4d01a485 7103 w2->cond_exp.get ())))
0cf6dd15
TJB
7104 return 0;
7105
85d721b8
PA
7106 /* Note that this checks the owner's type, not the location's. In
7107 case the target does not support read watchpoints, but does
7108 support access watchpoints, we'll have bp_read_watchpoint
7109 watchpoints with hw_access locations. Those should be considered
7110 duplicates of hw_read locations. The hw_read locations will
7111 become hw_access locations later. */
2d134ed3
PA
7112 return (loc1->owner->type == loc2->owner->type
7113 && loc1->pspace->aspace == loc2->pspace->aspace
7114 && loc1->address == loc2->address
7115 && loc1->length == loc2->length);
e4f237da
KB
7116}
7117
31e77af2 7118/* See breakpoint.h. */
6c95b8df 7119
31e77af2 7120int
6c95b8df
PA
7121breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7122 struct address_space *aspace2, CORE_ADDR addr2)
7123{
f5656ead 7124 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7125 || aspace1 == aspace2)
7126 && addr1 == addr2);
7127}
7128
f1310107
TJB
7129/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7130 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7131 matches ASPACE2. On targets that have global breakpoints, the address
7132 space doesn't really matter. */
7133
7134static int
7135breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7136 int len1, struct address_space *aspace2,
7137 CORE_ADDR addr2)
7138{
f5656ead 7139 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7140 || aspace1 == aspace2)
7141 && addr2 >= addr1 && addr2 < addr1 + len1);
7142}
7143
7144/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7145 a ranged breakpoint. In most targets, a match happens only if ASPACE
7146 matches the breakpoint's address space. On targets that have global
7147 breakpoints, the address space doesn't really matter. */
7148
7149static int
7150breakpoint_location_address_match (struct bp_location *bl,
7151 struct address_space *aspace,
7152 CORE_ADDR addr)
7153{
7154 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7155 aspace, addr)
7156 || (bl->length
7157 && breakpoint_address_match_range (bl->pspace->aspace,
7158 bl->address, bl->length,
7159 aspace, addr)));
7160}
7161
d35ae833
PA
7162/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7163 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7164 match happens only if ASPACE matches the breakpoint's address
7165 space. On targets that have global breakpoints, the address space
7166 doesn't really matter. */
7167
7168static int
7169breakpoint_location_address_range_overlap (struct bp_location *bl,
7170 struct address_space *aspace,
7171 CORE_ADDR addr, int len)
7172{
7173 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7174 || bl->pspace->aspace == aspace)
7175 {
7176 int bl_len = bl->length != 0 ? bl->length : 1;
7177
7178 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7179 return 1;
7180 }
7181 return 0;
7182}
7183
1e4d1764
YQ
7184/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7185 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7186 true, otherwise returns false. */
7187
7188static int
7189tracepoint_locations_match (struct bp_location *loc1,
7190 struct bp_location *loc2)
7191{
7192 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7193 /* Since tracepoint locations are never duplicated with others', tracepoint
7194 locations at the same address of different tracepoints are regarded as
7195 different locations. */
7196 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7197 else
7198 return 0;
7199}
7200
2d134ed3
PA
7201/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7202 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7203 represent the same location. */
7204
7205static int
4a64f543
MS
7206breakpoint_locations_match (struct bp_location *loc1,
7207 struct bp_location *loc2)
2d134ed3 7208{
2bdf28a0
JK
7209 int hw_point1, hw_point2;
7210
7211 /* Both of them must not be in moribund_locations. */
7212 gdb_assert (loc1->owner != NULL);
7213 gdb_assert (loc2->owner != NULL);
7214
7215 hw_point1 = is_hardware_watchpoint (loc1->owner);
7216 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7217
7218 if (hw_point1 != hw_point2)
7219 return 0;
7220 else if (hw_point1)
7221 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7222 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7223 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7224 else
f1310107
TJB
7225 /* We compare bp_location.length in order to cover ranged breakpoints. */
7226 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7227 loc2->pspace->aspace, loc2->address)
7228 && loc1->length == loc2->length);
2d134ed3
PA
7229}
7230
76897487
KB
7231static void
7232breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7233 int bnum, int have_bnum)
7234{
f63fbe86
MS
7235 /* The longest string possibly returned by hex_string_custom
7236 is 50 chars. These must be at least that big for safety. */
7237 char astr1[64];
7238 char astr2[64];
76897487 7239
bb599908
PH
7240 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7241 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7242 if (have_bnum)
8a3fe4f8 7243 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7244 bnum, astr1, astr2);
7245 else
8a3fe4f8 7246 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7247}
7248
4a64f543
MS
7249/* Adjust a breakpoint's address to account for architectural
7250 constraints on breakpoint placement. Return the adjusted address.
7251 Note: Very few targets require this kind of adjustment. For most
7252 targets, this function is simply the identity function. */
76897487
KB
7253
7254static CORE_ADDR
a6d9a66e
UW
7255adjust_breakpoint_address (struct gdbarch *gdbarch,
7256 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7257{
a6d9a66e 7258 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7259 {
7260 /* Very few targets need any kind of breakpoint adjustment. */
7261 return bpaddr;
7262 }
88f7da05
KB
7263 else if (bptype == bp_watchpoint
7264 || bptype == bp_hardware_watchpoint
7265 || bptype == bp_read_watchpoint
7266 || bptype == bp_access_watchpoint
fe798b75 7267 || bptype == bp_catchpoint)
88f7da05
KB
7268 {
7269 /* Watchpoints and the various bp_catch_* eventpoints should not
7270 have their addresses modified. */
7271 return bpaddr;
7272 }
7c16b83e
PA
7273 else if (bptype == bp_single_step)
7274 {
7275 /* Single-step breakpoints should not have their addresses
7276 modified. If there's any architectural constrain that
7277 applies to this address, then it should have already been
7278 taken into account when the breakpoint was created in the
7279 first place. If we didn't do this, stepping through e.g.,
7280 Thumb-2 IT blocks would break. */
7281 return bpaddr;
7282 }
76897487
KB
7283 else
7284 {
7285 CORE_ADDR adjusted_bpaddr;
7286
7287 /* Some targets have architectural constraints on the placement
7288 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7289 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7290
7291 /* An adjusted breakpoint address can significantly alter
7292 a user's expectations. Print a warning if an adjustment
7293 is required. */
7294 if (adjusted_bpaddr != bpaddr)
7295 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7296
7297 return adjusted_bpaddr;
7298 }
7299}
7300
5625a286 7301bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7302{
5625a286 7303 bp_location *loc = this;
7cc221ef 7304
348d480f
PA
7305 gdb_assert (ops != NULL);
7306
28010a5d
PA
7307 loc->ops = ops;
7308 loc->owner = owner;
b775012e 7309 loc->cond_bytecode = NULL;
0d381245
VP
7310 loc->shlib_disabled = 0;
7311 loc->enabled = 1;
e049a4b5 7312
28010a5d 7313 switch (owner->type)
e049a4b5
DJ
7314 {
7315 case bp_breakpoint:
7c16b83e 7316 case bp_single_step:
e049a4b5
DJ
7317 case bp_until:
7318 case bp_finish:
7319 case bp_longjmp:
7320 case bp_longjmp_resume:
e2e4d78b 7321 case bp_longjmp_call_dummy:
186c406b
TT
7322 case bp_exception:
7323 case bp_exception_resume:
e049a4b5 7324 case bp_step_resume:
2c03e5be 7325 case bp_hp_step_resume:
e049a4b5
DJ
7326 case bp_watchpoint_scope:
7327 case bp_call_dummy:
aa7d318d 7328 case bp_std_terminate:
e049a4b5
DJ
7329 case bp_shlib_event:
7330 case bp_thread_event:
7331 case bp_overlay_event:
4efc6507 7332 case bp_jit_event:
0fd8e87f 7333 case bp_longjmp_master:
aa7d318d 7334 case bp_std_terminate_master:
186c406b 7335 case bp_exception_master:
0e30163f
JK
7336 case bp_gnu_ifunc_resolver:
7337 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7338 case bp_dprintf:
e049a4b5 7339 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7340 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7341 break;
7342 case bp_hardware_breakpoint:
7343 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7344 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7345 break;
7346 case bp_hardware_watchpoint:
7347 case bp_read_watchpoint:
7348 case bp_access_watchpoint:
7349 loc->loc_type = bp_loc_hardware_watchpoint;
7350 break;
7351 case bp_watchpoint:
ce78b96d 7352 case bp_catchpoint:
15c3d785
PA
7353 case bp_tracepoint:
7354 case bp_fast_tracepoint:
0fb4aa4b 7355 case bp_static_tracepoint:
e049a4b5
DJ
7356 loc->loc_type = bp_loc_other;
7357 break;
7358 default:
e2e0b3e5 7359 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7360 }
7361
f431efe5 7362 loc->refc = 1;
28010a5d
PA
7363}
7364
7365/* Allocate a struct bp_location. */
7366
7367static struct bp_location *
7368allocate_bp_location (struct breakpoint *bpt)
7369{
348d480f
PA
7370 return bpt->ops->allocate_location (bpt);
7371}
7cc221ef 7372
f431efe5
PA
7373static void
7374free_bp_location (struct bp_location *loc)
fe3f5fa8 7375{
348d480f 7376 loc->ops->dtor (loc);
4d01a485 7377 delete loc;
fe3f5fa8
VP
7378}
7379
f431efe5
PA
7380/* Increment reference count. */
7381
7382static void
7383incref_bp_location (struct bp_location *bl)
7384{
7385 ++bl->refc;
7386}
7387
7388/* Decrement reference count. If the reference count reaches 0,
7389 destroy the bp_location. Sets *BLP to NULL. */
7390
7391static void
7392decref_bp_location (struct bp_location **blp)
7393{
0807b50c
PA
7394 gdb_assert ((*blp)->refc > 0);
7395
f431efe5
PA
7396 if (--(*blp)->refc == 0)
7397 free_bp_location (*blp);
7398 *blp = NULL;
7399}
7400
346774a9 7401/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7402
b270e6f9
TT
7403static breakpoint *
7404add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7405{
346774a9 7406 struct breakpoint *b1;
b270e6f9 7407 struct breakpoint *result = b.get ();
c906108c 7408
346774a9
PA
7409 /* Add this breakpoint to the end of the chain so that a list of
7410 breakpoints will come out in order of increasing numbers. */
7411
7412 b1 = breakpoint_chain;
7413 if (b1 == 0)
b270e6f9 7414 breakpoint_chain = b.release ();
346774a9
PA
7415 else
7416 {
7417 while (b1->next)
7418 b1 = b1->next;
b270e6f9 7419 b1->next = b.release ();
346774a9 7420 }
b270e6f9
TT
7421
7422 return result;
346774a9
PA
7423}
7424
7425/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7426
7427static void
7428init_raw_breakpoint_without_location (struct breakpoint *b,
7429 struct gdbarch *gdbarch,
28010a5d 7430 enum bptype bptype,
c0a91b2b 7431 const struct breakpoint_ops *ops)
346774a9 7432{
348d480f
PA
7433 gdb_assert (ops != NULL);
7434
28010a5d 7435 b->ops = ops;
4d28f7a8 7436 b->type = bptype;
a6d9a66e 7437 b->gdbarch = gdbarch;
c906108c
SS
7438 b->language = current_language->la_language;
7439 b->input_radix = input_radix;
d0fb5eae 7440 b->related_breakpoint = b;
346774a9
PA
7441}
7442
7443/* Helper to set_raw_breakpoint below. Creates a breakpoint
7444 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7445
7446static struct breakpoint *
7447set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7448 enum bptype bptype,
c0a91b2b 7449 const struct breakpoint_ops *ops)
346774a9 7450{
3b0871f4 7451 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7452
3b0871f4 7453 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7454 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7455}
7456
0e30163f
JK
7457/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7458 resolutions should be made as the user specified the location explicitly
7459 enough. */
7460
0d381245 7461static void
0e30163f 7462set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7463{
2bdf28a0
JK
7464 gdb_assert (loc->owner != NULL);
7465
0d381245 7466 if (loc->owner->type == bp_breakpoint
1042e4c0 7467 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7468 || is_tracepoint (loc->owner))
0d381245 7469 {
0e30163f 7470 int is_gnu_ifunc;
2c02bd72 7471 const char *function_name;
6a3a010b 7472 CORE_ADDR func_addr;
0e30163f 7473
2c02bd72 7474 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7475 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7476
7477 if (is_gnu_ifunc && !explicit_loc)
7478 {
7479 struct breakpoint *b = loc->owner;
7480
7481 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7482 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7483 &loc->requested_address))
7484 {
7485 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7486 loc->address = adjust_breakpoint_address (loc->gdbarch,
7487 loc->requested_address,
7488 b->type);
7489 }
7490 else if (b->type == bp_breakpoint && b->loc == loc
7491 && loc->next == NULL && b->related_breakpoint == b)
7492 {
7493 /* Create only the whole new breakpoint of this type but do not
7494 mess more complicated breakpoints with multiple locations. */
7495 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7496 /* Remember the resolver's address for use by the return
7497 breakpoint. */
7498 loc->related_address = func_addr;
0e30163f
JK
7499 }
7500 }
7501
2c02bd72
DE
7502 if (function_name)
7503 loc->function_name = xstrdup (function_name);
0d381245
VP
7504 }
7505}
7506
a6d9a66e 7507/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7508struct gdbarch *
a6d9a66e
UW
7509get_sal_arch (struct symtab_and_line sal)
7510{
7511 if (sal.section)
7512 return get_objfile_arch (sal.section->objfile);
7513 if (sal.symtab)
eb822aa6 7514 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7515
7516 return NULL;
7517}
7518
346774a9
PA
7519/* Low level routine for partially initializing a breakpoint of type
7520 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7521 file name, and line number are provided by SAL.
0d381245
VP
7522
7523 It is expected that the caller will complete the initialization of
7524 the newly created breakpoint struct as well as output any status
c56053d2 7525 information regarding the creation of a new breakpoint. */
0d381245 7526
346774a9
PA
7527static void
7528init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7529 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7530 const struct breakpoint_ops *ops)
0d381245 7531{
28010a5d 7532 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7533
3742cc8b 7534 add_location_to_breakpoint (b, &sal);
0d381245 7535
6c95b8df
PA
7536 if (bptype != bp_catchpoint)
7537 gdb_assert (sal.pspace != NULL);
7538
f8eba3c6
TT
7539 /* Store the program space that was used to set the breakpoint,
7540 except for ordinary breakpoints, which are independent of the
7541 program space. */
7542 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7543 b->pspace = sal.pspace;
346774a9 7544}
c906108c 7545
346774a9
PA
7546/* set_raw_breakpoint is a low level routine for allocating and
7547 partially initializing a breakpoint of type BPTYPE. The newly
7548 created breakpoint's address, section, source file name, and line
7549 number are provided by SAL. The newly created and partially
7550 initialized breakpoint is added to the breakpoint chain and
7551 is also returned as the value of this function.
7552
7553 It is expected that the caller will complete the initialization of
7554 the newly created breakpoint struct as well as output any status
7555 information regarding the creation of a new breakpoint. In
7556 particular, set_raw_breakpoint does NOT set the breakpoint
7557 number! Care should be taken to not allow an error to occur
7558 prior to completing the initialization of the breakpoint. If this
7559 should happen, a bogus breakpoint will be left on the chain. */
7560
7561struct breakpoint *
7562set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7563 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7564 const struct breakpoint_ops *ops)
346774a9 7565{
3b0871f4 7566 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7567
3b0871f4 7568 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7569 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7570}
7571
53a5351d 7572/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7573 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7574 initiated the operation. */
c906108c
SS
7575
7576void
186c406b 7577set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7578{
35df4500 7579 struct breakpoint *b, *b_tmp;
5d5658a1 7580 int thread = tp->global_num;
0fd8e87f
UW
7581
7582 /* To avoid having to rescan all objfile symbols at every step,
7583 we maintain a list of continually-inserted but always disabled
7584 longjmp "master" breakpoints. Here, we simply create momentary
7585 clones of those and enable them for the requested thread. */
35df4500 7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7587 if (b->pspace == current_program_space
186c406b
TT
7588 && (b->type == bp_longjmp_master
7589 || b->type == bp_exception_master))
0fd8e87f 7590 {
06edf0c0
PA
7591 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7592 struct breakpoint *clone;
cc59ec59 7593
e2e4d78b
JK
7594 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7595 after their removal. */
06edf0c0 7596 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7597 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7598 clone->thread = thread;
7599 }
186c406b
TT
7600
7601 tp->initiating_frame = frame;
c906108c
SS
7602}
7603
611c83ae 7604/* Delete all longjmp breakpoints from THREAD. */
c906108c 7605void
611c83ae 7606delete_longjmp_breakpoint (int thread)
c906108c 7607{
35df4500 7608 struct breakpoint *b, *b_tmp;
c906108c 7609
35df4500 7610 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7611 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7612 {
7613 if (b->thread == thread)
7614 delete_breakpoint (b);
7615 }
c906108c
SS
7616}
7617
f59f708a
PA
7618void
7619delete_longjmp_breakpoint_at_next_stop (int thread)
7620{
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_longjmp || b->type == bp_exception)
7625 {
7626 if (b->thread == thread)
7627 b->disposition = disp_del_at_next_stop;
7628 }
7629}
7630
e2e4d78b
JK
7631/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7632 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7633 pointer to any of them. Return NULL if this system cannot place longjmp
7634 breakpoints. */
7635
7636struct breakpoint *
7637set_longjmp_breakpoint_for_call_dummy (void)
7638{
7639 struct breakpoint *b, *retval = NULL;
7640
7641 ALL_BREAKPOINTS (b)
7642 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7643 {
7644 struct breakpoint *new_b;
7645
7646 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7647 &momentary_breakpoint_ops,
7648 1);
5d5658a1 7649 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7650
7651 /* Link NEW_B into the chain of RETVAL breakpoints. */
7652
7653 gdb_assert (new_b->related_breakpoint == new_b);
7654 if (retval == NULL)
7655 retval = new_b;
7656 new_b->related_breakpoint = retval;
7657 while (retval->related_breakpoint != new_b->related_breakpoint)
7658 retval = retval->related_breakpoint;
7659 retval->related_breakpoint = new_b;
7660 }
7661
7662 return retval;
7663}
7664
7665/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7666 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7667 stack.
7668
7669 You should call this function only at places where it is safe to currently
7670 unwind the whole stack. Failed stack unwind would discard live dummy
7671 frames. */
7672
7673void
b67a2c6f 7674check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7675{
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7679 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7680 {
7681 struct breakpoint *dummy_b = b->related_breakpoint;
7682
7683 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7684 dummy_b = dummy_b->related_breakpoint;
7685 if (dummy_b->type != bp_call_dummy
7686 || frame_find_by_id (dummy_b->frame_id) != NULL)
7687 continue;
7688
b67a2c6f 7689 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7690
7691 while (b->related_breakpoint != b)
7692 {
7693 if (b_tmp == b->related_breakpoint)
7694 b_tmp = b->related_breakpoint->next;
7695 delete_breakpoint (b->related_breakpoint);
7696 }
7697 delete_breakpoint (b);
7698 }
7699}
7700
1900040c
MS
7701void
7702enable_overlay_breakpoints (void)
7703{
52f0bd74 7704 struct breakpoint *b;
1900040c
MS
7705
7706 ALL_BREAKPOINTS (b)
7707 if (b->type == bp_overlay_event)
7708 {
7709 b->enable_state = bp_enabled;
44702360 7710 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7711 overlay_events_enabled = 1;
1900040c
MS
7712 }
7713}
7714
7715void
7716disable_overlay_breakpoints (void)
7717{
52f0bd74 7718 struct breakpoint *b;
1900040c
MS
7719
7720 ALL_BREAKPOINTS (b)
7721 if (b->type == bp_overlay_event)
7722 {
7723 b->enable_state = bp_disabled;
44702360 7724 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7725 overlay_events_enabled = 0;
1900040c
MS
7726 }
7727}
7728
aa7d318d
TT
7729/* Set an active std::terminate breakpoint for each std::terminate
7730 master breakpoint. */
7731void
7732set_std_terminate_breakpoint (void)
7733{
35df4500 7734 struct breakpoint *b, *b_tmp;
aa7d318d 7735
35df4500 7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7737 if (b->pspace == current_program_space
7738 && b->type == bp_std_terminate_master)
7739 {
06edf0c0 7740 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7741 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7742 }
7743}
7744
7745/* Delete all the std::terminate breakpoints. */
7746void
7747delete_std_terminate_breakpoint (void)
7748{
35df4500 7749 struct breakpoint *b, *b_tmp;
aa7d318d 7750
35df4500 7751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7752 if (b->type == bp_std_terminate)
7753 delete_breakpoint (b);
7754}
7755
c4093a6a 7756struct breakpoint *
a6d9a66e 7757create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7758{
7759 struct breakpoint *b;
c4093a6a 7760
06edf0c0
PA
7761 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7762 &internal_breakpoint_ops);
7763
b5de0fa7 7764 b->enable_state = bp_enabled;
f00aae0f 7765 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7766 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7767
44702360 7768 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7769
c4093a6a
JM
7770 return b;
7771}
7772
0101ce28
JJ
7773struct lang_and_radix
7774 {
7775 enum language lang;
7776 int radix;
7777 };
7778
4efc6507
DE
7779/* Create a breakpoint for JIT code registration and unregistration. */
7780
7781struct breakpoint *
7782create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7783{
2a7f3dff
PA
7784 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7785 &internal_breakpoint_ops);
4efc6507 7786}
0101ce28 7787
03673fc7
PP
7788/* Remove JIT code registration and unregistration breakpoint(s). */
7789
7790void
7791remove_jit_event_breakpoints (void)
7792{
7793 struct breakpoint *b, *b_tmp;
7794
7795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7796 if (b->type == bp_jit_event
7797 && b->loc->pspace == current_program_space)
7798 delete_breakpoint (b);
7799}
7800
cae688ec
JJ
7801void
7802remove_solib_event_breakpoints (void)
7803{
35df4500 7804 struct breakpoint *b, *b_tmp;
cae688ec 7805
35df4500 7806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7807 if (b->type == bp_shlib_event
7808 && b->loc->pspace == current_program_space)
cae688ec
JJ
7809 delete_breakpoint (b);
7810}
7811
f37f681c
PA
7812/* See breakpoint.h. */
7813
7814void
7815remove_solib_event_breakpoints_at_next_stop (void)
7816{
7817 struct breakpoint *b, *b_tmp;
7818
7819 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7820 if (b->type == bp_shlib_event
7821 && b->loc->pspace == current_program_space)
7822 b->disposition = disp_del_at_next_stop;
7823}
7824
04086b45
PA
7825/* Helper for create_solib_event_breakpoint /
7826 create_and_insert_solib_event_breakpoint. Allows specifying which
7827 INSERT_MODE to pass through to update_global_location_list. */
7828
7829static struct breakpoint *
7830create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7831 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7832{
7833 struct breakpoint *b;
7834
06edf0c0
PA
7835 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7836 &internal_breakpoint_ops);
04086b45 7837 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7838 return b;
7839}
7840
04086b45
PA
7841struct breakpoint *
7842create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7843{
7844 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7845}
7846
f37f681c
PA
7847/* See breakpoint.h. */
7848
7849struct breakpoint *
7850create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7851{
7852 struct breakpoint *b;
7853
04086b45
PA
7854 /* Explicitly tell update_global_location_list to insert
7855 locations. */
7856 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7857 if (!b->loc->inserted)
7858 {
7859 delete_breakpoint (b);
7860 return NULL;
7861 }
7862 return b;
7863}
7864
cae688ec
JJ
7865/* Disable any breakpoints that are on code in shared libraries. Only
7866 apply to enabled breakpoints, disabled ones can just stay disabled. */
7867
7868void
cb851954 7869disable_breakpoints_in_shlibs (void)
cae688ec 7870{
876fa593 7871 struct bp_location *loc, **locp_tmp;
cae688ec 7872
876fa593 7873 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7874 {
2bdf28a0 7875 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7876 struct breakpoint *b = loc->owner;
2bdf28a0 7877
4a64f543
MS
7878 /* We apply the check to all breakpoints, including disabled for
7879 those with loc->duplicate set. This is so that when breakpoint
7880 becomes enabled, or the duplicate is removed, gdb will try to
7881 insert all breakpoints. If we don't set shlib_disabled here,
7882 we'll try to insert those breakpoints and fail. */
1042e4c0 7883 if (((b->type == bp_breakpoint)
508ccb1f 7884 || (b->type == bp_jit_event)
1042e4c0 7885 || (b->type == bp_hardware_breakpoint)
d77f58be 7886 || (is_tracepoint (b)))
6c95b8df 7887 && loc->pspace == current_program_space
0d381245 7888 && !loc->shlib_disabled
6c95b8df 7889 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7890 )
0d381245
VP
7891 {
7892 loc->shlib_disabled = 1;
7893 }
cae688ec
JJ
7894 }
7895}
7896
63644780
NB
7897/* Disable any breakpoints and tracepoints that are in SOLIB upon
7898 notification of unloaded_shlib. Only apply to enabled breakpoints,
7899 disabled ones can just stay disabled. */
84acb35a 7900
75149521 7901static void
84acb35a
JJ
7902disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7903{
876fa593 7904 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7905 int disabled_shlib_breaks = 0;
7906
876fa593 7907 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7908 {
2bdf28a0 7909 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7910 struct breakpoint *b = loc->owner;
cc59ec59 7911
1e4d1764 7912 if (solib->pspace == loc->pspace
e2dd7057 7913 && !loc->shlib_disabled
1e4d1764
YQ
7914 && (((b->type == bp_breakpoint
7915 || b->type == bp_jit_event
7916 || b->type == bp_hardware_breakpoint)
7917 && (loc->loc_type == bp_loc_hardware_breakpoint
7918 || loc->loc_type == bp_loc_software_breakpoint))
7919 || is_tracepoint (b))
e2dd7057 7920 && solib_contains_address_p (solib, loc->address))
84acb35a 7921 {
e2dd7057
PP
7922 loc->shlib_disabled = 1;
7923 /* At this point, we cannot rely on remove_breakpoint
7924 succeeding so we must mark the breakpoint as not inserted
7925 to prevent future errors occurring in remove_breakpoints. */
7926 loc->inserted = 0;
8d3788bd
VP
7927
7928 /* This may cause duplicate notifications for the same breakpoint. */
7929 observer_notify_breakpoint_modified (b);
7930
e2dd7057
PP
7931 if (!disabled_shlib_breaks)
7932 {
7933 target_terminal_ours_for_output ();
3e43a32a
MS
7934 warning (_("Temporarily disabling breakpoints "
7935 "for unloaded shared library \"%s\""),
e2dd7057 7936 solib->so_name);
84acb35a 7937 }
e2dd7057 7938 disabled_shlib_breaks = 1;
84acb35a
JJ
7939 }
7940 }
84acb35a
JJ
7941}
7942
63644780
NB
7943/* Disable any breakpoints and tracepoints in OBJFILE upon
7944 notification of free_objfile. Only apply to enabled breakpoints,
7945 disabled ones can just stay disabled. */
7946
7947static void
7948disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7949{
7950 struct breakpoint *b;
7951
7952 if (objfile == NULL)
7953 return;
7954
d03de421
PA
7955 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7956 managed by the user with add-symbol-file/remove-symbol-file.
7957 Similarly to how breakpoints in shared libraries are handled in
7958 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7959 shlib_disabled so they end up uninserted on the next global
7960 location list update. Shared libraries not loaded by the user
7961 aren't handled here -- they're already handled in
7962 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7963 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7964 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7965 main objfile). */
7966 if ((objfile->flags & OBJF_SHARED) == 0
7967 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7968 return;
7969
7970 ALL_BREAKPOINTS (b)
7971 {
7972 struct bp_location *loc;
7973 int bp_modified = 0;
7974
7975 if (!is_breakpoint (b) && !is_tracepoint (b))
7976 continue;
7977
7978 for (loc = b->loc; loc != NULL; loc = loc->next)
7979 {
7980 CORE_ADDR loc_addr = loc->address;
7981
7982 if (loc->loc_type != bp_loc_hardware_breakpoint
7983 && loc->loc_type != bp_loc_software_breakpoint)
7984 continue;
7985
7986 if (loc->shlib_disabled != 0)
7987 continue;
7988
7989 if (objfile->pspace != loc->pspace)
7990 continue;
7991
7992 if (loc->loc_type != bp_loc_hardware_breakpoint
7993 && loc->loc_type != bp_loc_software_breakpoint)
7994 continue;
7995
7996 if (is_addr_in_objfile (loc_addr, objfile))
7997 {
7998 loc->shlib_disabled = 1;
08351840
PA
7999 /* At this point, we don't know whether the object was
8000 unmapped from the inferior or not, so leave the
8001 inserted flag alone. We'll handle failure to
8002 uninsert quietly, in case the object was indeed
8003 unmapped. */
63644780
NB
8004
8005 mark_breakpoint_location_modified (loc);
8006
8007 bp_modified = 1;
8008 }
8009 }
8010
8011 if (bp_modified)
8012 observer_notify_breakpoint_modified (b);
8013 }
8014}
8015
ce78b96d
JB
8016/* FORK & VFORK catchpoints. */
8017
e29a4733 8018/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
8019 catchpoint. A breakpoint is really of this type iff its ops pointer points
8020 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 8021
c1fc2657 8022struct fork_catchpoint : public breakpoint
e29a4733 8023{
e29a4733
PA
8024 /* Process id of a child process whose forking triggered this
8025 catchpoint. This field is only valid immediately after this
8026 catchpoint has triggered. */
8027 ptid_t forked_inferior_pid;
8028};
8029
4a64f543
MS
8030/* Implement the "insert" breakpoint_ops method for fork
8031 catchpoints. */
ce78b96d 8032
77b06cd7
TJB
8033static int
8034insert_catch_fork (struct bp_location *bl)
ce78b96d 8035{
dfd4cc63 8036 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8037}
8038
4a64f543
MS
8039/* Implement the "remove" breakpoint_ops method for fork
8040 catchpoints. */
ce78b96d
JB
8041
8042static int
73971819 8043remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8044{
dfd4cc63 8045 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8046}
8047
8048/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8049 catchpoints. */
8050
8051static int
f1310107 8052breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8053 struct address_space *aspace, CORE_ADDR bp_addr,
8054 const struct target_waitstatus *ws)
ce78b96d 8055{
e29a4733
PA
8056 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8057
f90263c1
TT
8058 if (ws->kind != TARGET_WAITKIND_FORKED)
8059 return 0;
8060
8061 c->forked_inferior_pid = ws->value.related_pid;
8062 return 1;
ce78b96d
JB
8063}
8064
4a64f543
MS
8065/* Implement the "print_it" breakpoint_ops method for fork
8066 catchpoints. */
ce78b96d
JB
8067
8068static enum print_stop_action
348d480f 8069print_it_catch_fork (bpstat bs)
ce78b96d 8070{
36dfb11c 8071 struct ui_out *uiout = current_uiout;
348d480f
PA
8072 struct breakpoint *b = bs->breakpoint_at;
8073 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8074
ce78b96d 8075 annotate_catchpoint (b->number);
f303dbd6 8076 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8077 if (b->disposition == disp_del)
112e8700 8078 uiout->text ("Temporary catchpoint ");
36dfb11c 8079 else
112e8700
SM
8080 uiout->text ("Catchpoint ");
8081 if (uiout->is_mi_like_p ())
36dfb11c 8082 {
112e8700
SM
8083 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8084 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8085 }
112e8700
SM
8086 uiout->field_int ("bkptno", b->number);
8087 uiout->text (" (forked process ");
8088 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8089 uiout->text ("), ");
ce78b96d
JB
8090 return PRINT_SRC_AND_LOC;
8091}
8092
4a64f543
MS
8093/* Implement the "print_one" breakpoint_ops method for fork
8094 catchpoints. */
ce78b96d
JB
8095
8096static void
a6d9a66e 8097print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8098{
e29a4733 8099 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8100 struct value_print_options opts;
79a45e25 8101 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8102
8103 get_user_print_options (&opts);
8104
4a64f543
MS
8105 /* Field 4, the address, is omitted (which makes the columns not
8106 line up too nicely with the headers, but the effect is relatively
8107 readable). */
79a45b7d 8108 if (opts.addressprint)
112e8700 8109 uiout->field_skip ("addr");
ce78b96d 8110 annotate_field (5);
112e8700 8111 uiout->text ("fork");
e29a4733 8112 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8113 {
112e8700
SM
8114 uiout->text (", process ");
8115 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8116 uiout->spaces (1);
ce78b96d 8117 }
8ac3646f 8118
112e8700
SM
8119 if (uiout->is_mi_like_p ())
8120 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
8121}
8122
8123/* Implement the "print_mention" breakpoint_ops method for fork
8124 catchpoints. */
8125
8126static void
8127print_mention_catch_fork (struct breakpoint *b)
8128{
8129 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8130}
8131
6149aea9
PA
8132/* Implement the "print_recreate" breakpoint_ops method for fork
8133 catchpoints. */
8134
8135static void
8136print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8137{
8138 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8139 print_recreate_thread (b, fp);
6149aea9
PA
8140}
8141
ce78b96d
JB
8142/* The breakpoint_ops structure to be used in fork catchpoints. */
8143
2060206e 8144static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8145
4a64f543
MS
8146/* Implement the "insert" breakpoint_ops method for vfork
8147 catchpoints. */
ce78b96d 8148
77b06cd7
TJB
8149static int
8150insert_catch_vfork (struct bp_location *bl)
ce78b96d 8151{
dfd4cc63 8152 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8153}
8154
4a64f543
MS
8155/* Implement the "remove" breakpoint_ops method for vfork
8156 catchpoints. */
ce78b96d
JB
8157
8158static int
73971819 8159remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 8160{
dfd4cc63 8161 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8162}
8163
8164/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8165 catchpoints. */
8166
8167static int
f1310107 8168breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8169 struct address_space *aspace, CORE_ADDR bp_addr,
8170 const struct target_waitstatus *ws)
ce78b96d 8171{
e29a4733
PA
8172 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8173
f90263c1
TT
8174 if (ws->kind != TARGET_WAITKIND_VFORKED)
8175 return 0;
8176
8177 c->forked_inferior_pid = ws->value.related_pid;
8178 return 1;
ce78b96d
JB
8179}
8180
4a64f543
MS
8181/* Implement the "print_it" breakpoint_ops method for vfork
8182 catchpoints. */
ce78b96d
JB
8183
8184static enum print_stop_action
348d480f 8185print_it_catch_vfork (bpstat bs)
ce78b96d 8186{
36dfb11c 8187 struct ui_out *uiout = current_uiout;
348d480f 8188 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8189 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8190
ce78b96d 8191 annotate_catchpoint (b->number);
f303dbd6 8192 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8193 if (b->disposition == disp_del)
112e8700 8194 uiout->text ("Temporary catchpoint ");
36dfb11c 8195 else
112e8700
SM
8196 uiout->text ("Catchpoint ");
8197 if (uiout->is_mi_like_p ())
36dfb11c 8198 {
112e8700
SM
8199 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8200 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8201 }
112e8700
SM
8202 uiout->field_int ("bkptno", b->number);
8203 uiout->text (" (vforked process ");
8204 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8205 uiout->text ("), ");
ce78b96d
JB
8206 return PRINT_SRC_AND_LOC;
8207}
8208
4a64f543
MS
8209/* Implement the "print_one" breakpoint_ops method for vfork
8210 catchpoints. */
ce78b96d
JB
8211
8212static void
a6d9a66e 8213print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8214{
e29a4733 8215 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8216 struct value_print_options opts;
79a45e25 8217 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8218
8219 get_user_print_options (&opts);
4a64f543
MS
8220 /* Field 4, the address, is omitted (which makes the columns not
8221 line up too nicely with the headers, but the effect is relatively
8222 readable). */
79a45b7d 8223 if (opts.addressprint)
112e8700 8224 uiout->field_skip ("addr");
ce78b96d 8225 annotate_field (5);
112e8700 8226 uiout->text ("vfork");
e29a4733 8227 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 8228 {
112e8700
SM
8229 uiout->text (", process ");
8230 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8231 uiout->spaces (1);
ce78b96d 8232 }
8ac3646f 8233
112e8700
SM
8234 if (uiout->is_mi_like_p ())
8235 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
8236}
8237
8238/* Implement the "print_mention" breakpoint_ops method for vfork
8239 catchpoints. */
8240
8241static void
8242print_mention_catch_vfork (struct breakpoint *b)
8243{
8244 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8245}
8246
6149aea9
PA
8247/* Implement the "print_recreate" breakpoint_ops method for vfork
8248 catchpoints. */
8249
8250static void
8251print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8252{
8253 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8254 print_recreate_thread (b, fp);
6149aea9
PA
8255}
8256
ce78b96d
JB
8257/* The breakpoint_ops structure to be used in vfork catchpoints. */
8258
2060206e 8259static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8260
edcc5120 8261/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8262 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8263 CATCH_SOLIB_BREAKPOINT_OPS. */
8264
c1fc2657 8265struct solib_catchpoint : public breakpoint
edcc5120 8266{
c1fc2657 8267 ~solib_catchpoint () override;
edcc5120
TT
8268
8269 /* True for "catch load", false for "catch unload". */
8270 unsigned char is_load;
8271
8272 /* Regular expression to match, if any. COMPILED is only valid when
8273 REGEX is non-NULL. */
8274 char *regex;
2d7cc5c7 8275 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8276};
8277
c1fc2657 8278solib_catchpoint::~solib_catchpoint ()
edcc5120 8279{
c1fc2657 8280 xfree (this->regex);
edcc5120
TT
8281}
8282
8283static int
8284insert_catch_solib (struct bp_location *ignore)
8285{
8286 return 0;
8287}
8288
8289static int
73971819 8290remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8291{
8292 return 0;
8293}
8294
8295static int
8296breakpoint_hit_catch_solib (const struct bp_location *bl,
8297 struct address_space *aspace,
8298 CORE_ADDR bp_addr,
8299 const struct target_waitstatus *ws)
8300{
8301 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8302 struct breakpoint *other;
8303
8304 if (ws->kind == TARGET_WAITKIND_LOADED)
8305 return 1;
8306
8307 ALL_BREAKPOINTS (other)
8308 {
8309 struct bp_location *other_bl;
8310
8311 if (other == bl->owner)
8312 continue;
8313
8314 if (other->type != bp_shlib_event)
8315 continue;
8316
c1fc2657 8317 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8318 continue;
8319
8320 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8321 {
8322 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8323 return 1;
8324 }
8325 }
8326
8327 return 0;
8328}
8329
8330static void
8331check_status_catch_solib (struct bpstats *bs)
8332{
8333 struct solib_catchpoint *self
8334 = (struct solib_catchpoint *) bs->breakpoint_at;
8335 int ix;
8336
8337 if (self->is_load)
8338 {
8339 struct so_list *iter;
8340
8341 for (ix = 0;
8342 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8343 ix, iter);
8344 ++ix)
8345 {
8346 if (!self->regex
2d7cc5c7 8347 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8348 return;
8349 }
8350 }
8351 else
8352 {
8353 char *iter;
8354
8355 for (ix = 0;
8356 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8357 ix, iter);
8358 ++ix)
8359 {
8360 if (!self->regex
2d7cc5c7 8361 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8362 return;
8363 }
8364 }
8365
8366 bs->stop = 0;
8367 bs->print_it = print_it_noop;
8368}
8369
8370static enum print_stop_action
8371print_it_catch_solib (bpstat bs)
8372{
8373 struct breakpoint *b = bs->breakpoint_at;
8374 struct ui_out *uiout = current_uiout;
8375
8376 annotate_catchpoint (b->number);
f303dbd6 8377 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8378 if (b->disposition == disp_del)
112e8700 8379 uiout->text ("Temporary catchpoint ");
edcc5120 8380 else
112e8700
SM
8381 uiout->text ("Catchpoint ");
8382 uiout->field_int ("bkptno", b->number);
8383 uiout->text ("\n");
8384 if (uiout->is_mi_like_p ())
8385 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8386 print_solib_event (1);
8387 return PRINT_SRC_AND_LOC;
8388}
8389
8390static void
8391print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8392{
8393 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8394 struct value_print_options opts;
8395 struct ui_out *uiout = current_uiout;
8396 char *msg;
8397
8398 get_user_print_options (&opts);
8399 /* Field 4, the address, is omitted (which makes the columns not
8400 line up too nicely with the headers, but the effect is relatively
8401 readable). */
8402 if (opts.addressprint)
8403 {
8404 annotate_field (4);
112e8700 8405 uiout->field_skip ("addr");
edcc5120
TT
8406 }
8407
8408 annotate_field (5);
8409 if (self->is_load)
8410 {
8411 if (self->regex)
8412 msg = xstrprintf (_("load of library matching %s"), self->regex);
8413 else
8414 msg = xstrdup (_("load of library"));
8415 }
8416 else
8417 {
8418 if (self->regex)
8419 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8420 else
8421 msg = xstrdup (_("unload of library"));
8422 }
112e8700 8423 uiout->field_string ("what", msg);
edcc5120 8424 xfree (msg);
8ac3646f 8425
112e8700
SM
8426 if (uiout->is_mi_like_p ())
8427 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8428}
8429
8430static void
8431print_mention_catch_solib (struct breakpoint *b)
8432{
8433 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8434
8435 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8436 self->is_load ? "load" : "unload");
8437}
8438
8439static void
8440print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8441{
8442 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8443
8444 fprintf_unfiltered (fp, "%s %s",
8445 b->disposition == disp_del ? "tcatch" : "catch",
8446 self->is_load ? "load" : "unload");
8447 if (self->regex)
8448 fprintf_unfiltered (fp, " %s", self->regex);
8449 fprintf_unfiltered (fp, "\n");
8450}
8451
8452static struct breakpoint_ops catch_solib_breakpoint_ops;
8453
91985142
MG
8454/* Shared helper function (MI and CLI) for creating and installing
8455 a shared object event catchpoint. If IS_LOAD is non-zero then
8456 the events to be caught are load events, otherwise they are
8457 unload events. If IS_TEMP is non-zero the catchpoint is a
8458 temporary one. If ENABLED is non-zero the catchpoint is
8459 created in an enabled state. */
edcc5120 8460
91985142 8461void
a121b7c1 8462add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8463{
edcc5120 8464 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8465
edcc5120
TT
8466 if (!arg)
8467 arg = "";
a121b7c1 8468 arg = skip_spaces_const (arg);
edcc5120 8469
36bd8eaa 8470 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8471
8472 if (*arg != '\0')
8473 {
2d7cc5c7
PA
8474 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8475 _("Invalid regexp")));
edcc5120
TT
8476 c->regex = xstrdup (arg);
8477 }
8478
8479 c->is_load = is_load;
36bd8eaa 8480 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8481 &catch_solib_breakpoint_ops);
8482
c1fc2657 8483 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8484
b270e6f9 8485 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8486}
8487
91985142
MG
8488/* A helper function that does all the work for "catch load" and
8489 "catch unload". */
8490
8491static void
8492catch_load_or_unload (char *arg, int from_tty, int is_load,
8493 struct cmd_list_element *command)
8494{
8495 int tempflag;
8496 const int enabled = 1;
8497
8498 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8499
8500 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8501}
8502
edcc5120
TT
8503static void
8504catch_load_command_1 (char *arg, int from_tty,
8505 struct cmd_list_element *command)
8506{
8507 catch_load_or_unload (arg, from_tty, 1, command);
8508}
8509
8510static void
8511catch_unload_command_1 (char *arg, int from_tty,
8512 struct cmd_list_element *command)
8513{
8514 catch_load_or_unload (arg, from_tty, 0, command);
8515}
8516
346774a9
PA
8517/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8518 is non-zero, then make the breakpoint temporary. If COND_STRING is
8519 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8520 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8521
ab04a2af 8522void
346774a9
PA
8523init_catchpoint (struct breakpoint *b,
8524 struct gdbarch *gdbarch, int tempflag,
63160a43 8525 const char *cond_string,
c0a91b2b 8526 const struct breakpoint_ops *ops)
c906108c 8527{
c5aa993b 8528 struct symtab_and_line sal;
346774a9 8529
fe39c653 8530 init_sal (&sal);
6c95b8df 8531 sal.pspace = current_program_space;
c5aa993b 8532
28010a5d 8533 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8534
1b36a34b 8535 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8536 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8537}
8538
28010a5d 8539void
b270e6f9 8540install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8541{
b270e6f9 8542 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8543 set_breakpoint_number (internal, b);
558a9d82
YQ
8544 if (is_tracepoint (b))
8545 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8546 if (!internal)
8547 mention (b);
c56053d2 8548 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8549
8550 if (update_gll)
44702360 8551 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8552}
8553
9b70b993 8554static void
a6d9a66e 8555create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8556 int tempflag, const char *cond_string,
c0a91b2b 8557 const struct breakpoint_ops *ops)
c906108c 8558{
b270e6f9 8559 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8560
b270e6f9 8561 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8562
8563 c->forked_inferior_pid = null_ptid;
8564
b270e6f9 8565 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8566}
8567
fe798b75
JB
8568/* Exec catchpoints. */
8569
b4d90040 8570/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8571 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8572 CATCH_EXEC_BREAKPOINT_OPS. */
8573
c1fc2657 8574struct exec_catchpoint : public breakpoint
b4d90040 8575{
c1fc2657 8576 ~exec_catchpoint () override;
b4d90040
PA
8577
8578 /* Filename of a program whose exec triggered this catchpoint.
8579 This field is only valid immediately after this catchpoint has
8580 triggered. */
8581 char *exec_pathname;
8582};
8583
c1fc2657 8584/* Exec catchpoint destructor. */
b4d90040 8585
c1fc2657 8586exec_catchpoint::~exec_catchpoint ()
b4d90040 8587{
c1fc2657 8588 xfree (this->exec_pathname);
b4d90040
PA
8589}
8590
77b06cd7
TJB
8591static int
8592insert_catch_exec (struct bp_location *bl)
c906108c 8593{
dfd4cc63 8594 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8595}
c906108c 8596
fe798b75 8597static int
73971819 8598remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8599{
dfd4cc63 8600 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8601}
c906108c 8602
fe798b75 8603static int
f1310107 8604breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8605 struct address_space *aspace, CORE_ADDR bp_addr,
8606 const struct target_waitstatus *ws)
fe798b75 8607{
b4d90040
PA
8608 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8609
f90263c1
TT
8610 if (ws->kind != TARGET_WAITKIND_EXECD)
8611 return 0;
8612
8613 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8614 return 1;
fe798b75 8615}
c906108c 8616
fe798b75 8617static enum print_stop_action
348d480f 8618print_it_catch_exec (bpstat bs)
fe798b75 8619{
36dfb11c 8620 struct ui_out *uiout = current_uiout;
348d480f 8621 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8622 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8623
fe798b75 8624 annotate_catchpoint (b->number);
f303dbd6 8625 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8626 if (b->disposition == disp_del)
112e8700 8627 uiout->text ("Temporary catchpoint ");
36dfb11c 8628 else
112e8700
SM
8629 uiout->text ("Catchpoint ");
8630 if (uiout->is_mi_like_p ())
36dfb11c 8631 {
112e8700
SM
8632 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8633 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8634 }
112e8700
SM
8635 uiout->field_int ("bkptno", b->number);
8636 uiout->text (" (exec'd ");
8637 uiout->field_string ("new-exec", c->exec_pathname);
8638 uiout->text ("), ");
36dfb11c 8639
fe798b75 8640 return PRINT_SRC_AND_LOC;
c906108c
SS
8641}
8642
fe798b75 8643static void
a6d9a66e 8644print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8645{
b4d90040 8646 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8647 struct value_print_options opts;
79a45e25 8648 struct ui_out *uiout = current_uiout;
fe798b75
JB
8649
8650 get_user_print_options (&opts);
8651
8652 /* Field 4, the address, is omitted (which makes the columns
8653 not line up too nicely with the headers, but the effect
8654 is relatively readable). */
8655 if (opts.addressprint)
112e8700 8656 uiout->field_skip ("addr");
fe798b75 8657 annotate_field (5);
112e8700 8658 uiout->text ("exec");
b4d90040 8659 if (c->exec_pathname != NULL)
fe798b75 8660 {
112e8700
SM
8661 uiout->text (", program \"");
8662 uiout->field_string ("what", c->exec_pathname);
8663 uiout->text ("\" ");
fe798b75 8664 }
8ac3646f 8665
112e8700
SM
8666 if (uiout->is_mi_like_p ())
8667 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8668}
8669
8670static void
8671print_mention_catch_exec (struct breakpoint *b)
8672{
8673 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8674}
8675
6149aea9
PA
8676/* Implement the "print_recreate" breakpoint_ops method for exec
8677 catchpoints. */
8678
8679static void
8680print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8681{
8682 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8683 print_recreate_thread (b, fp);
6149aea9
PA
8684}
8685
2060206e 8686static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8687
c906108c 8688static int
fba45db2 8689hw_breakpoint_used_count (void)
c906108c 8690{
c906108c 8691 int i = 0;
f1310107
TJB
8692 struct breakpoint *b;
8693 struct bp_location *bl;
c906108c
SS
8694
8695 ALL_BREAKPOINTS (b)
c5aa993b 8696 {
d6b74ac4 8697 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8698 for (bl = b->loc; bl; bl = bl->next)
8699 {
8700 /* Special types of hardware breakpoints may use more than
8701 one register. */
348d480f 8702 i += b->ops->resources_needed (bl);
f1310107 8703 }
c5aa993b 8704 }
c906108c
SS
8705
8706 return i;
8707}
8708
a1398e0c
PA
8709/* Returns the resources B would use if it were a hardware
8710 watchpoint. */
8711
c906108c 8712static int
a1398e0c 8713hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8714{
c906108c 8715 int i = 0;
e09342b5 8716 struct bp_location *bl;
c906108c 8717
a1398e0c
PA
8718 if (!breakpoint_enabled (b))
8719 return 0;
8720
8721 for (bl = b->loc; bl; bl = bl->next)
8722 {
8723 /* Special types of hardware watchpoints may use more than
8724 one register. */
8725 i += b->ops->resources_needed (bl);
8726 }
8727
8728 return i;
8729}
8730
8731/* Returns the sum the used resources of all hardware watchpoints of
8732 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8733 the sum of the used resources of all hardware watchpoints of other
8734 types _not_ TYPE. */
8735
8736static int
8737hw_watchpoint_used_count_others (struct breakpoint *except,
8738 enum bptype type, int *other_type_used)
8739{
8740 int i = 0;
8741 struct breakpoint *b;
8742
c906108c
SS
8743 *other_type_used = 0;
8744 ALL_BREAKPOINTS (b)
e09342b5 8745 {
a1398e0c
PA
8746 if (b == except)
8747 continue;
e09342b5
TJB
8748 if (!breakpoint_enabled (b))
8749 continue;
8750
a1398e0c
PA
8751 if (b->type == type)
8752 i += hw_watchpoint_use_count (b);
8753 else if (is_hardware_watchpoint (b))
8754 *other_type_used = 1;
e09342b5
TJB
8755 }
8756
c906108c
SS
8757 return i;
8758}
8759
c906108c 8760void
fba45db2 8761disable_watchpoints_before_interactive_call_start (void)
c906108c 8762{
c5aa993b 8763 struct breakpoint *b;
c906108c
SS
8764
8765 ALL_BREAKPOINTS (b)
c5aa993b 8766 {
cc60f2e3 8767 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8768 {
b5de0fa7 8769 b->enable_state = bp_call_disabled;
44702360 8770 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8771 }
8772 }
c906108c
SS
8773}
8774
8775void
fba45db2 8776enable_watchpoints_after_interactive_call_stop (void)
c906108c 8777{
c5aa993b 8778 struct breakpoint *b;
c906108c
SS
8779
8780 ALL_BREAKPOINTS (b)
c5aa993b 8781 {
cc60f2e3 8782 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8783 {
b5de0fa7 8784 b->enable_state = bp_enabled;
44702360 8785 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8786 }
8787 }
c906108c
SS
8788}
8789
8bea4e01
UW
8790void
8791disable_breakpoints_before_startup (void)
8792{
6c95b8df 8793 current_program_space->executing_startup = 1;
44702360 8794 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8795}
8796
8797void
8798enable_breakpoints_after_startup (void)
8799{
6c95b8df 8800 current_program_space->executing_startup = 0;
f8eba3c6 8801 breakpoint_re_set ();
8bea4e01
UW
8802}
8803
7c16b83e
PA
8804/* Create a new single-step breakpoint for thread THREAD, with no
8805 locations. */
c906108c 8806
7c16b83e
PA
8807static struct breakpoint *
8808new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8809{
b270e6f9 8810 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8811
b270e6f9 8812 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8813 &momentary_breakpoint_ops);
8814
8815 b->disposition = disp_donttouch;
8816 b->frame_id = null_frame_id;
8817
8818 b->thread = thread;
8819 gdb_assert (b->thread != 0);
8820
b270e6f9 8821 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8822}
8823
8824/* Set a momentary breakpoint of type TYPE at address specified by
8825 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8826 frame. */
c906108c
SS
8827
8828struct breakpoint *
a6d9a66e
UW
8829set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8830 struct frame_id frame_id, enum bptype type)
c906108c 8831{
52f0bd74 8832 struct breakpoint *b;
edb3359d 8833
193facb3
JK
8834 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8835 tail-called one. */
8836 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8837
06edf0c0 8838 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8839 b->enable_state = bp_enabled;
8840 b->disposition = disp_donttouch;
818dd999 8841 b->frame_id = frame_id;
c906108c 8842
4a64f543
MS
8843 /* If we're debugging a multi-threaded program, then we want
8844 momentary breakpoints to be active in only a single thread of
8845 control. */
39f77062 8846 if (in_thread_list (inferior_ptid))
5d5658a1 8847 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8848
44702360 8849 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8850
c906108c
SS
8851 return b;
8852}
611c83ae 8853
06edf0c0 8854/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8855 The new breakpoint will have type TYPE, use OPS as its
8856 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8857
06edf0c0
PA
8858static struct breakpoint *
8859momentary_breakpoint_from_master (struct breakpoint *orig,
8860 enum bptype type,
a1aa2221
LM
8861 const struct breakpoint_ops *ops,
8862 int loc_enabled)
e58b0e63
PA
8863{
8864 struct breakpoint *copy;
8865
06edf0c0 8866 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8867 copy->loc = allocate_bp_location (copy);
0e30163f 8868 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8869
a6d9a66e 8870 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8871 copy->loc->requested_address = orig->loc->requested_address;
8872 copy->loc->address = orig->loc->address;
8873 copy->loc->section = orig->loc->section;
6c95b8df 8874 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8875 copy->loc->probe = orig->loc->probe;
f8eba3c6 8876 copy->loc->line_number = orig->loc->line_number;
2f202fde 8877 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8878 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8879 copy->frame_id = orig->frame_id;
8880 copy->thread = orig->thread;
6c95b8df 8881 copy->pspace = orig->pspace;
e58b0e63
PA
8882
8883 copy->enable_state = bp_enabled;
8884 copy->disposition = disp_donttouch;
8885 copy->number = internal_breakpoint_number--;
8886
44702360 8887 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8888 return copy;
8889}
8890
06edf0c0
PA
8891/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8892 ORIG is NULL. */
8893
8894struct breakpoint *
8895clone_momentary_breakpoint (struct breakpoint *orig)
8896{
8897 /* If there's nothing to clone, then return nothing. */
8898 if (orig == NULL)
8899 return NULL;
8900
a1aa2221 8901 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8902}
8903
611c83ae 8904struct breakpoint *
a6d9a66e
UW
8905set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8906 enum bptype type)
611c83ae
PA
8907{
8908 struct symtab_and_line sal;
8909
8910 sal = find_pc_line (pc, 0);
8911 sal.pc = pc;
8912 sal.section = find_pc_overlay (pc);
8913 sal.explicit_pc = 1;
8914
a6d9a66e 8915 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8916}
c906108c 8917\f
c5aa993b 8918
c906108c
SS
8919/* Tell the user we have just set a breakpoint B. */
8920
8921static void
fba45db2 8922mention (struct breakpoint *b)
c906108c 8923{
348d480f 8924 b->ops->print_mention (b);
112e8700 8925 if (current_uiout->is_mi_like_p ())
fb40c209 8926 return;
c906108c
SS
8927 printf_filtered ("\n");
8928}
c906108c 8929\f
c5aa993b 8930
1a853c52
PA
8931static int bp_loc_is_permanent (struct bp_location *loc);
8932
0d381245 8933static struct bp_location *
39d61571 8934add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8935 const struct symtab_and_line *sal)
8936{
8937 struct bp_location *loc, **tmp;
3742cc8b
YQ
8938 CORE_ADDR adjusted_address;
8939 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8940
8941 if (loc_gdbarch == NULL)
8942 loc_gdbarch = b->gdbarch;
8943
8944 /* Adjust the breakpoint's address prior to allocating a location.
8945 Once we call allocate_bp_location(), that mostly uninitialized
8946 location will be placed on the location chain. Adjustment of the
8947 breakpoint may cause target_read_memory() to be called and we do
8948 not want its scan of the location chain to find a breakpoint and
8949 location that's only been partially initialized. */
8950 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8951 sal->pc, b->type);
0d381245 8952
d30113d4 8953 /* Sort the locations by their ADDRESS. */
39d61571 8954 loc = allocate_bp_location (b);
d30113d4
JK
8955 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8956 tmp = &((*tmp)->next))
0d381245 8957 ;
d30113d4 8958 loc->next = *tmp;
0d381245 8959 *tmp = loc;
3742cc8b 8960
0d381245 8961 loc->requested_address = sal->pc;
3742cc8b 8962 loc->address = adjusted_address;
6c95b8df 8963 loc->pspace = sal->pspace;
729662a5
TT
8964 loc->probe.probe = sal->probe;
8965 loc->probe.objfile = sal->objfile;
6c95b8df 8966 gdb_assert (loc->pspace != NULL);
0d381245 8967 loc->section = sal->section;
3742cc8b 8968 loc->gdbarch = loc_gdbarch;
f8eba3c6 8969 loc->line_number = sal->line;
2f202fde 8970 loc->symtab = sal->symtab;
f8eba3c6 8971
0e30163f
JK
8972 set_breakpoint_location_function (loc,
8973 sal->explicit_pc || sal->explicit_line);
1a853c52 8974
6ae88661
LM
8975 /* While by definition, permanent breakpoints are already present in the
8976 code, we don't mark the location as inserted. Normally one would expect
8977 that GDB could rely on that breakpoint instruction to stop the program,
8978 thus removing the need to insert its own breakpoint, except that executing
8979 the breakpoint instruction can kill the target instead of reporting a
8980 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8981 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8982 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8983 breakpoint be inserted normally results in QEMU knowing about the GDB
8984 breakpoint, and thus trap before the breakpoint instruction is executed.
8985 (If GDB later needs to continue execution past the permanent breakpoint,
8986 it manually increments the PC, thus avoiding executing the breakpoint
8987 instruction.) */
1a853c52 8988 if (bp_loc_is_permanent (loc))
6ae88661 8989 loc->permanent = 1;
1a853c52 8990
0d381245
VP
8991 return loc;
8992}
514f746b
AR
8993\f
8994
1cf4d951 8995/* See breakpoint.h. */
514f746b 8996
1cf4d951
PA
8997int
8998program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8999{
9000 int len;
9001 CORE_ADDR addr;
1afeeb75 9002 const gdb_byte *bpoint;
514f746b 9003 gdb_byte *target_mem;
939c61fa
JK
9004 struct cleanup *cleanup;
9005 int retval = 0;
514f746b 9006
1cf4d951
PA
9007 addr = address;
9008 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9009
9010 /* Software breakpoints unsupported? */
9011 if (bpoint == NULL)
9012 return 0;
9013
224c3ddb 9014 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9015
9016 /* Enable the automatic memory restoration from breakpoints while
9017 we read the memory. Otherwise we could say about our temporary
9018 breakpoints they are permanent. */
9019 cleanup = make_show_memory_breakpoints_cleanup (0);
9020
9021 if (target_read_memory (address, target_mem, len) == 0
9022 && memcmp (target_mem, bpoint, len) == 0)
9023 retval = 1;
9024
9025 do_cleanups (cleanup);
9026
9027 return retval;
9028}
9029
9030/* Return 1 if LOC is pointing to a permanent breakpoint,
9031 return 0 otherwise. */
9032
9033static int
9034bp_loc_is_permanent (struct bp_location *loc)
9035{
514f746b
AR
9036 gdb_assert (loc != NULL);
9037
244558af
LM
9038 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9039 attempt to read from the addresses the locations of these breakpoint types
9040 point to. program_breakpoint_here_p, below, will attempt to read
9041 memory. */
9042 if (!breakpoint_address_is_meaningful (loc->owner))
9043 return 0;
9044
5ed8105e 9045 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9046 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 9047 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
9048}
9049
e7e0cddf
SS
9050/* Build a command list for the dprintf corresponding to the current
9051 settings of the dprintf style options. */
9052
9053static void
9054update_dprintf_command_list (struct breakpoint *b)
9055{
9056 char *dprintf_args = b->extra_string;
9057 char *printf_line = NULL;
9058
9059 if (!dprintf_args)
9060 return;
9061
9062 dprintf_args = skip_spaces (dprintf_args);
9063
9064 /* Allow a comma, as it may have terminated a location, but don't
9065 insist on it. */
9066 if (*dprintf_args == ',')
9067 ++dprintf_args;
9068 dprintf_args = skip_spaces (dprintf_args);
9069
9070 if (*dprintf_args != '"')
9071 error (_("Bad format string, missing '\"'."));
9072
d3ce09f5 9073 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9074 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9075 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9076 {
9077 if (!dprintf_function)
9078 error (_("No function supplied for dprintf call"));
9079
9080 if (dprintf_channel && strlen (dprintf_channel) > 0)
9081 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9082 dprintf_function,
9083 dprintf_channel,
9084 dprintf_args);
9085 else
9086 printf_line = xstrprintf ("call (void) %s (%s)",
9087 dprintf_function,
9088 dprintf_args);
9089 }
d3ce09f5
SS
9090 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9091 {
9092 if (target_can_run_breakpoint_commands ())
9093 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9094 else
9095 {
9096 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9097 printf_line = xstrprintf ("printf %s", dprintf_args);
9098 }
9099 }
e7e0cddf
SS
9100 else
9101 internal_error (__FILE__, __LINE__,
9102 _("Invalid dprintf style."));
9103
f28045c2 9104 gdb_assert (printf_line != NULL);
9d6e6e84 9105 /* Manufacture a printf sequence. */
f28045c2 9106 {
8d749320 9107 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9108
f28045c2
YQ
9109 printf_cmd_line->control_type = simple_control;
9110 printf_cmd_line->body_count = 0;
9111 printf_cmd_line->body_list = NULL;
9d6e6e84 9112 printf_cmd_line->next = NULL;
f28045c2 9113 printf_cmd_line->line = printf_line;
e7e0cddf 9114
93921405 9115 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 9116 }
e7e0cddf
SS
9117}
9118
9119/* Update all dprintf commands, making their command lists reflect
9120 current style settings. */
9121
9122static void
9123update_dprintf_commands (char *args, int from_tty,
9124 struct cmd_list_element *c)
9125{
9126 struct breakpoint *b;
9127
9128 ALL_BREAKPOINTS (b)
9129 {
9130 if (b->type == bp_dprintf)
9131 update_dprintf_command_list (b);
9132 }
9133}
c3f6f71d 9134
f00aae0f
KS
9135/* Create a breakpoint with SAL as location. Use LOCATION
9136 as a description of the location, and COND_STRING
b35a8b2f
DE
9137 as condition expression. If LOCATION is NULL then create an
9138 "address location" from the address in the SAL. */
018d34a4
VP
9139
9140static void
d9b3f62e 9141init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f 9142 struct symtabs_and_lines sals,
ffc2605c 9143 event_location_up &&location,
e1e01040
PA
9144 gdb::unique_xmalloc_ptr<char> filter,
9145 gdb::unique_xmalloc_ptr<char> cond_string,
9146 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9147 enum bptype type, enum bpdisp disposition,
9148 int thread, int task, int ignore_count,
c0a91b2b 9149 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9150 int enabled, int internal, unsigned flags,
9151 int display_canonical)
018d34a4 9152{
0d381245 9153 int i;
018d34a4
VP
9154
9155 if (type == bp_hardware_breakpoint)
9156 {
fbbd034e
AS
9157 int target_resources_ok;
9158
9159 i = hw_breakpoint_used_count ();
9160 target_resources_ok =
9161 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9162 i + 1, 0);
9163 if (target_resources_ok == 0)
9164 error (_("No hardware breakpoint support in the target."));
9165 else if (target_resources_ok < 0)
9166 error (_("Hardware breakpoints used exceeds limit."));
9167 }
9168
6c95b8df
PA
9169 gdb_assert (sals.nelts > 0);
9170
0d381245
VP
9171 for (i = 0; i < sals.nelts; ++i)
9172 {
9173 struct symtab_and_line sal = sals.sals[i];
9174 struct bp_location *loc;
9175
9176 if (from_tty)
5af949e3
UW
9177 {
9178 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9179 if (!loc_gdbarch)
9180 loc_gdbarch = gdbarch;
9181
9182 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9183 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9184 }
0d381245
VP
9185
9186 if (i == 0)
9187 {
d9b3f62e 9188 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9189 b->thread = thread;
4a306c9a 9190 b->task = task;
855a6e68 9191
e1e01040
PA
9192 b->cond_string = cond_string.release ();
9193 b->extra_string = extra_string.release ();
0d381245 9194 b->ignore_count = ignore_count;
41447f92 9195 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9196 b->disposition = disposition;
6c95b8df 9197
44f238bb
PA
9198 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9199 b->loc->inserted = 1;
9200
0fb4aa4b
PA
9201 if (type == bp_static_tracepoint)
9202 {
d9b3f62e 9203 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9204 struct static_tracepoint_marker marker;
9205
983af33b 9206 if (strace_marker_p (b))
0fb4aa4b
PA
9207 {
9208 /* We already know the marker exists, otherwise, we
9209 wouldn't see a sal for it. */
d28cd78a
TT
9210 const char *p
9211 = &event_location_to_string (b->location.get ())[3];
f00aae0f 9212 const char *endp;
0fb4aa4b 9213 char *marker_str;
0fb4aa4b 9214
f00aae0f 9215 p = skip_spaces_const (p);
0fb4aa4b 9216
f00aae0f 9217 endp = skip_to_space_const (p);
0fb4aa4b
PA
9218
9219 marker_str = savestring (p, endp - p);
d9b3f62e 9220 t->static_trace_marker_id = marker_str;
0fb4aa4b 9221
3e43a32a
MS
9222 printf_filtered (_("Probed static tracepoint "
9223 "marker \"%s\"\n"),
d9b3f62e 9224 t->static_trace_marker_id);
0fb4aa4b
PA
9225 }
9226 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9227 {
d9b3f62e 9228 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9229 release_static_tracepoint_marker (&marker);
9230
3e43a32a
MS
9231 printf_filtered (_("Probed static tracepoint "
9232 "marker \"%s\"\n"),
d9b3f62e 9233 t->static_trace_marker_id);
0fb4aa4b
PA
9234 }
9235 else
3e43a32a
MS
9236 warning (_("Couldn't determine the static "
9237 "tracepoint marker to probe"));
0fb4aa4b
PA
9238 }
9239
0d381245
VP
9240 loc = b->loc;
9241 }
9242 else
018d34a4 9243 {
39d61571 9244 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9245 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9246 loc->inserted = 1;
0d381245
VP
9247 }
9248
9249 if (b->cond_string)
9250 {
bbc13ae3
KS
9251 const char *arg = b->cond_string;
9252
1bb9788d
TT
9253 loc->cond = parse_exp_1 (&arg, loc->address,
9254 block_for_pc (loc->address), 0);
0d381245 9255 if (*arg)
588ae58c 9256 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9257 }
e7e0cddf
SS
9258
9259 /* Dynamic printf requires and uses additional arguments on the
9260 command line, otherwise it's an error. */
9261 if (type == bp_dprintf)
9262 {
9263 if (b->extra_string)
9264 update_dprintf_command_list (b);
9265 else
9266 error (_("Format string required"));
9267 }
9268 else if (b->extra_string)
588ae58c 9269 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9270 }
018d34a4 9271
56435ebe 9272 b->display_canonical = display_canonical;
f00aae0f 9273 if (location != NULL)
d28cd78a 9274 b->location = std::move (location);
018d34a4 9275 else
d28cd78a 9276 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9277 b->filter = filter.release ();
d9b3f62e 9278}
018d34a4 9279
d9b3f62e
PA
9280static void
9281create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f 9282 struct symtabs_and_lines sals,
ffc2605c 9283 event_location_up &&location,
e1e01040
PA
9284 gdb::unique_xmalloc_ptr<char> filter,
9285 gdb::unique_xmalloc_ptr<char> cond_string,
9286 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9287 enum bptype type, enum bpdisp disposition,
9288 int thread, int task, int ignore_count,
c0a91b2b 9289 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9290 int enabled, int internal, unsigned flags,
9291 int display_canonical)
d9b3f62e 9292{
a5e364af 9293 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9294
a5e364af 9295 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9296 sals, std::move (location),
e1e01040
PA
9297 std::move (filter),
9298 std::move (cond_string),
9299 std::move (extra_string),
d9b3f62e
PA
9300 type, disposition,
9301 thread, task, ignore_count,
9302 ops, from_tty,
44f238bb
PA
9303 enabled, internal, flags,
9304 display_canonical);
d9b3f62e 9305
b270e6f9 9306 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9307}
9308
9309/* Add SALS.nelts breakpoints to the breakpoint table. For each
9310 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9311 value. COND_STRING, if not NULL, specified the condition to be
9312 used for all breakpoints. Essentially the only case where
9313 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9314 function. In that case, it's still not possible to specify
9315 separate conditions for different overloaded functions, so
9316 we take just a single condition string.
9317
c3f6f71d 9318 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9319 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9320 array contents). If the function fails (error() is called), the
9321 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9322 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9323
9324static void
8cdf0e15 9325create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9326 struct linespec_result *canonical,
e1e01040
PA
9327 gdb::unique_xmalloc_ptr<char> cond_string,
9328 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9329 enum bptype type, enum bpdisp disposition,
9330 int thread, int task, int ignore_count,
c0a91b2b 9331 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9332 int enabled, int internal, unsigned flags)
c906108c 9333{
018d34a4 9334 int i;
f8eba3c6 9335 struct linespec_sals *lsal;
cc59ec59 9336
f8eba3c6
TT
9337 if (canonical->pre_expanded)
9338 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9339
9340 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9341 {
f00aae0f 9342 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9343 'break', without arguments. */
ffc2605c 9344 event_location_up location
f00aae0f 9345 = (canonical->location != NULL
8e9e35b1 9346 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040
PA
9347 gdb::unique_xmalloc_ptr<char> filter_string
9348 (lsal->canonical != NULL ? xstrdup (lsal->canonical) : NULL);
0d381245 9349
f8eba3c6 9350 create_breakpoint_sal (gdbarch, lsal->sals,
ffc2605c 9351 std::move (location),
e1e01040
PA
9352 std::move (filter_string),
9353 std::move (cond_string),
9354 std::move (extra_string),
e7e0cddf 9355 type, disposition,
84f4c1fe 9356 thread, task, ignore_count, ops,
44f238bb 9357 from_tty, enabled, internal, flags,
56435ebe 9358 canonical->special_display);
c3f6f71d 9359 }
c3f6f71d 9360}
c906108c 9361
f00aae0f 9362/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9363 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9364 addresses found. LOCATION points to the end of the SAL (for
9365 linespec locations).
9998af43
TJB
9366
9367 The array and the line spec strings are allocated on the heap, it is
9368 the caller's responsibility to free them. */
c906108c 9369
b9362cc7 9370static void
f00aae0f 9371parse_breakpoint_sals (const struct event_location *location,
58438ac1 9372 struct linespec_result *canonical)
c3f6f71d 9373{
f00aae0f
KS
9374 struct symtab_and_line cursal;
9375
9376 if (event_location_type (location) == LINESPEC_LOCATION)
9377 {
9378 const char *address = get_linespec_location (location);
9379
9380 if (address == NULL)
9381 {
9382 /* The last displayed codepoint, if it's valid, is our default
9383 breakpoint address. */
9384 if (last_displayed_sal_is_valid ())
9385 {
9386 struct linespec_sals lsal;
9387 struct symtab_and_line sal;
9388 CORE_ADDR pc;
9389
9390 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9391 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9392
9393 /* Set sal's pspace, pc, symtab, and line to the values
9394 corresponding to the last call to print_frame_info.
9395 Be sure to reinitialize LINE with NOTCURRENT == 0
9396 as the breakpoint line number is inappropriate otherwise.
9397 find_pc_line would adjust PC, re-set it back. */
9398 get_last_displayed_sal (&sal);
9399 pc = sal.pc;
9400 sal = find_pc_line (pc, 0);
9401
9402 /* "break" without arguments is equivalent to "break *PC"
9403 where PC is the last displayed codepoint's address. So
9404 make sure to set sal.explicit_pc to prevent GDB from
9405 trying to expand the list of sals to include all other
9406 instances with the same symtab and line. */
9407 sal.pc = pc;
9408 sal.explicit_pc = 1;
9409
9410 lsal.sals.sals[0] = sal;
9411 lsal.sals.nelts = 1;
9412 lsal.canonical = NULL;
9413
9414 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9415 return;
9416 }
9417 else
9418 error (_("No default breakpoint address now."));
c906108c 9419 }
c906108c 9420 }
f00aae0f
KS
9421
9422 /* Force almost all breakpoints to be in terms of the
9423 current_source_symtab (which is decode_line_1's default).
9424 This should produce the results we want almost all of the
9425 time while leaving default_breakpoint_* alone.
9426
9427 ObjC: However, don't match an Objective-C method name which
9428 may have a '+' or '-' succeeded by a '['. */
9429 cursal = get_current_source_symtab_and_line ();
9430 if (last_displayed_sal_is_valid ())
c906108c 9431 {
f00aae0f 9432 const char *address = NULL;
cc80f267 9433
f00aae0f
KS
9434 if (event_location_type (location) == LINESPEC_LOCATION)
9435 address = get_linespec_location (location);
cc80f267 9436
f00aae0f
KS
9437 if (!cursal.symtab
9438 || (address != NULL
9439 && strchr ("+-", address[0]) != NULL
9440 && address[1] != '['))
9441 {
c2f4122d 9442 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9443 get_last_displayed_symtab (),
9444 get_last_displayed_line (),
9445 canonical, NULL, NULL);
9446 return;
9447 }
c906108c 9448 }
f00aae0f 9449
c2f4122d 9450 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9451 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9452}
c906108c 9453
c906108c 9454
c3f6f71d 9455/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9456 inserted as a breakpoint. If it can't throw an error. */
c906108c 9457
b9362cc7 9458static void
23e7acfb 9459breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9460{
9461 int i;
cc59ec59 9462
c3f6f71d 9463 for (i = 0; i < sals->nelts; i++)
ee53e872 9464 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9465}
9466
7a697b8d
SS
9467/* Fast tracepoints may have restrictions on valid locations. For
9468 instance, a fast tracepoint using a jump instead of a trap will
9469 likely have to overwrite more bytes than a trap would, and so can
9470 only be placed where the instruction is longer than the jump, or a
9471 multi-instruction sequence does not have a jump into the middle of
9472 it, etc. */
9473
9474static void
9475check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9476 struct symtabs_and_lines *sals)
9477{
9478 int i, rslt;
9479 struct symtab_and_line *sal;
9480 char *msg;
9481 struct cleanup *old_chain;
9482
9483 for (i = 0; i < sals->nelts; i++)
9484 {
f8eba3c6
TT
9485 struct gdbarch *sarch;
9486
7a697b8d
SS
9487 sal = &sals->sals[i];
9488
f8eba3c6
TT
9489 sarch = get_sal_arch (*sal);
9490 /* We fall back to GDBARCH if there is no architecture
9491 associated with SAL. */
9492 if (sarch == NULL)
9493 sarch = gdbarch;
6b940e6a 9494 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9495 old_chain = make_cleanup (xfree, msg);
9496
9497 if (!rslt)
53c3572a 9498 error (_("May not have a fast tracepoint at %s%s"),
f8eba3c6 9499 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9500
9501 do_cleanups (old_chain);
9502 }
9503}
9504
018d34a4
VP
9505/* Given TOK, a string specification of condition and thread, as
9506 accepted by the 'break' command, extract the condition
9507 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9508 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9509 If no condition is found, *COND_STRING is set to NULL.
9510 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9511
9512static void
bbc13ae3 9513find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9514 char **cond_string, int *thread, int *task,
9515 char **rest)
018d34a4
VP
9516{
9517 *cond_string = NULL;
9518 *thread = -1;
ed1d1739
KS
9519 *task = 0;
9520 *rest = NULL;
9521
018d34a4
VP
9522 while (tok && *tok)
9523 {
bbc13ae3 9524 const char *end_tok;
018d34a4 9525 int toklen;
bbc13ae3
KS
9526 const char *cond_start = NULL;
9527 const char *cond_end = NULL;
cc59ec59 9528
bbc13ae3 9529 tok = skip_spaces_const (tok);
e7e0cddf
SS
9530
9531 if ((*tok == '"' || *tok == ',') && rest)
9532 {
9533 *rest = savestring (tok, strlen (tok));
9534 return;
9535 }
9536
bbc13ae3 9537 end_tok = skip_to_space_const (tok);
d634f2de 9538
018d34a4 9539 toklen = end_tok - tok;
d634f2de 9540
018d34a4
VP
9541 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9542 {
9543 tok = cond_start = end_tok + 1;
4d01a485 9544 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9545 cond_end = tok;
d634f2de 9546 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9547 }
9548 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9549 {
5d5658a1
PA
9550 const char *tmptok;
9551 struct thread_info *thr;
d634f2de 9552
018d34a4 9553 tok = end_tok + 1;
5d5658a1 9554 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9555 if (tok == tmptok)
9556 error (_("Junk after thread keyword."));
5d5658a1 9557 *thread = thr->global_num;
bbc13ae3 9558 tok = tmptok;
018d34a4 9559 }
4a306c9a
JB
9560 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9561 {
9562 char *tmptok;
9563
9564 tok = end_tok + 1;
bbc13ae3 9565 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9566 if (tok == tmptok)
9567 error (_("Junk after task keyword."));
9568 if (!valid_task_id (*task))
b6199126 9569 error (_("Unknown task %d."), *task);
bbc13ae3 9570 tok = tmptok;
4a306c9a 9571 }
e7e0cddf
SS
9572 else if (rest)
9573 {
9574 *rest = savestring (tok, strlen (tok));
ccab2054 9575 return;
e7e0cddf 9576 }
018d34a4
VP
9577 else
9578 error (_("Junk at end of arguments."));
9579 }
9580}
9581
0fb4aa4b
PA
9582/* Decode a static tracepoint marker spec. */
9583
9584static struct symtabs_and_lines
f00aae0f 9585decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9586{
9587 VEC(static_tracepoint_marker_p) *markers = NULL;
9588 struct symtabs_and_lines sals;
0fb4aa4b 9589 struct cleanup *old_chain;
f00aae0f
KS
9590 const char *p = &(*arg_p)[3];
9591 const char *endp;
0fb4aa4b
PA
9592 char *marker_str;
9593 int i;
9594
f00aae0f 9595 p = skip_spaces_const (p);
0fb4aa4b 9596
f00aae0f 9597 endp = skip_to_space_const (p);
0fb4aa4b
PA
9598
9599 marker_str = savestring (p, endp - p);
9600 old_chain = make_cleanup (xfree, marker_str);
9601
9602 markers = target_static_tracepoint_markers_by_strid (marker_str);
9603 if (VEC_empty(static_tracepoint_marker_p, markers))
9604 error (_("No known static tracepoint marker named %s"), marker_str);
9605
9606 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9607 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9608
9609 for (i = 0; i < sals.nelts; i++)
9610 {
9611 struct static_tracepoint_marker *marker;
9612
9613 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9614
9615 init_sal (&sals.sals[i]);
9616
9617 sals.sals[i] = find_pc_line (marker->address, 0);
9618 sals.sals[i].pc = marker->address;
9619
9620 release_static_tracepoint_marker (marker);
9621 }
9622
9623 do_cleanups (old_chain);
9624
9625 *arg_p = endp;
9626 return sals;
9627}
9628
f00aae0f 9629/* See breakpoint.h. */
0101ce28 9630
8cdf0e15
VP
9631int
9632create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9633 const struct event_location *location,
9634 const char *cond_string,
9635 int thread, const char *extra_string,
f00aae0f 9636 int parse_extra,
0fb4aa4b 9637 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9638 int ignore_count,
9639 enum auto_boolean pending_break_support,
c0a91b2b 9640 const struct breakpoint_ops *ops,
44f238bb
PA
9641 int from_tty, int enabled, int internal,
9642 unsigned flags)
c3f6f71d 9643{
7efd8fc2 9644 struct linespec_result canonical;
80c99de1 9645 struct cleanup *bkpt_chain = NULL;
0101ce28 9646 int pending = 0;
4a306c9a 9647 int task = 0;
86b17b60 9648 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9649
348d480f
PA
9650 gdb_assert (ops != NULL);
9651
f00aae0f
KS
9652 /* If extra_string isn't useful, set it to NULL. */
9653 if (extra_string != NULL && *extra_string == '\0')
9654 extra_string = NULL;
9655
492d29ea 9656 TRY
b78a6381 9657 {
f00aae0f 9658 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9659 }
492d29ea 9660 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9661 {
492d29ea
PA
9662 /* If caller is interested in rc value from parse, set
9663 value. */
9664 if (e.error == NOT_FOUND_ERROR)
0101ce28 9665 {
05ff989b
AC
9666 /* If pending breakpoint support is turned off, throw
9667 error. */
fa8d40ab
JJ
9668
9669 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9670 throw_exception (e);
9671
9672 exception_print (gdb_stderr, e);
fa8d40ab 9673
05ff989b
AC
9674 /* If pending breakpoint support is auto query and the user
9675 selects no, then simply return the error code. */
059fb39f 9676 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9677 && !nquery (_("Make %s pending on future shared library load? "),
9678 bptype_string (type_wanted)))
fd9b8c24 9679 return 0;
fa8d40ab 9680
05ff989b
AC
9681 /* At this point, either the user was queried about setting
9682 a pending breakpoint and selected yes, or pending
9683 breakpoint behavior is on and thus a pending breakpoint
9684 is defaulted on behalf of the user. */
f00aae0f 9685 pending = 1;
0101ce28 9686 }
492d29ea
PA
9687 else
9688 throw_exception (e);
0101ce28 9689 }
492d29ea
PA
9690 END_CATCH
9691
f00aae0f 9692 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9693 return 0;
c3f6f71d 9694
c3f6f71d
JM
9695 /* ----------------------------- SNIP -----------------------------
9696 Anything added to the cleanup chain beyond this point is assumed
9697 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9698 then the memory is not reclaimed. */
9699 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9700
c3f6f71d
JM
9701 /* Resolve all line numbers to PC's and verify that the addresses
9702 are ok for the target. */
0101ce28 9703 if (!pending)
f8eba3c6
TT
9704 {
9705 int ix;
9706 struct linespec_sals *iter;
9707
9708 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9709 breakpoint_sals_to_pc (&iter->sals);
9710 }
c3f6f71d 9711
7a697b8d 9712 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9713 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9714 {
9715 int ix;
9716 struct linespec_sals *iter;
9717
9718 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9719 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9720 }
7a697b8d 9721
c3f6f71d
JM
9722 /* Verify that condition can be parsed, before setting any
9723 breakpoints. Allocate a separate condition expression for each
4a64f543 9724 breakpoint. */
0101ce28 9725 if (!pending)
c3f6f71d 9726 {
e1e01040
PA
9727 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9728 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9729
f00aae0f 9730 if (parse_extra)
72b2ff0e 9731 {
0878d0fa 9732 char *rest;
e1e01040 9733 char *cond;
52d361e1
YQ
9734 struct linespec_sals *lsal;
9735
9736 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9737
0878d0fa
YQ
9738 /* Here we only parse 'arg' to separate condition
9739 from thread number, so parsing in context of first
9740 sal is OK. When setting the breakpoint we'll
9741 re-parse it in context of each sal. */
9742
f00aae0f 9743 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
e1e01040
PA
9744 &cond, &thread, &task, &rest);
9745 cond_string_copy.reset (cond);
9746 extra_string_copy.reset (rest);
72b2ff0e 9747 }
2f069f6f 9748 else
72b2ff0e 9749 {
f00aae0f
KS
9750 if (type_wanted != bp_dprintf
9751 && extra_string != NULL && *extra_string != '\0')
9752 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9753
9754 /* Create a private copy of condition string. */
9755 if (cond_string)
e1e01040 9756 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9757 /* Create a private copy of any extra string. */
9758 if (extra_string)
e1e01040 9759 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9760 }
0fb4aa4b 9761
52d361e1 9762 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9763 std::move (cond_string_copy),
9764 std::move (extra_string_copy),
9765 type_wanted,
d9b3f62e
PA
9766 tempflag ? disp_del : disp_donttouch,
9767 thread, task, ignore_count, ops,
44f238bb 9768 from_tty, enabled, internal, flags);
c906108c 9769 }
0101ce28
JJ
9770 else
9771 {
a5e364af 9772 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9773
a5e364af 9774 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9775 b->location = copy_event_location (location);
bfccc43c 9776
f00aae0f
KS
9777 if (parse_extra)
9778 b->cond_string = NULL;
e12c7713
MK
9779 else
9780 {
9781 /* Create a private copy of condition string. */
e1e01040 9782 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9783 b->thread = thread;
e12c7713 9784 }
f00aae0f
KS
9785
9786 /* Create a private copy of any extra string. */
e1e01040 9787 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9788 b->ignore_count = ignore_count;
0101ce28 9789 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9790 b->condition_not_parsed = 1;
41447f92 9791 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9792 if ((type_wanted != bp_breakpoint
9793 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9794 b->pspace = current_program_space;
8bea4e01 9795
b270e6f9 9796 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9797 }
9798
f8eba3c6 9799 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9800 {
3e43a32a
MS
9801 warning (_("Multiple breakpoints were set.\nUse the "
9802 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9803 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9804 }
9805
80c99de1
PA
9806 /* That's it. Discard the cleanups for data inserted into the
9807 breakpoint. */
9808 discard_cleanups (bkpt_chain);
217dc9e2 9809
80c99de1 9810 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9811 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9812
9813 return 1;
c3f6f71d 9814}
c906108c 9815
348d480f 9816/* Set a breakpoint.
72b2ff0e
VP
9817 ARG is a string describing breakpoint address,
9818 condition, and thread.
9819 FLAG specifies if a breakpoint is hardware on,
9820 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9821 and BP_TEMPFLAG. */
348d480f 9822
98deb0da 9823static void
72b2ff0e 9824break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9825{
72b2ff0e 9826 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9827 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9828 ? bp_hardware_breakpoint
9829 : bp_breakpoint);
55aa24fb 9830 struct breakpoint_ops *ops;
f00aae0f 9831
ffc2605c 9832 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9833
9834 /* Matching breakpoints on probes. */
5b56227b 9835 if (location != NULL
ffc2605c 9836 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9837 ops = &bkpt_probe_breakpoint_ops;
9838 else
9839 ops = &bkpt_breakpoint_ops;
c3f6f71d 9840
8cdf0e15 9841 create_breakpoint (get_current_arch (),
ffc2605c 9842 location.get (),
f00aae0f 9843 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9844 tempflag, type_wanted,
8cdf0e15
VP
9845 0 /* Ignore count */,
9846 pending_break_support,
55aa24fb 9847 ops,
8cdf0e15 9848 from_tty,
84f4c1fe 9849 1 /* enabled */,
44f238bb
PA
9850 0 /* internal */,
9851 0);
c906108c
SS
9852}
9853
c906108c
SS
9854/* Helper function for break_command_1 and disassemble_command. */
9855
9856void
fba45db2 9857resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9858{
9859 CORE_ADDR pc;
9860
9861 if (sal->pc == 0 && sal->symtab != NULL)
9862 {
9863 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9864 error (_("No line %d in file \"%s\"."),
05cba821 9865 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9866 sal->pc = pc;
6a048695 9867
4a64f543
MS
9868 /* If this SAL corresponds to a breakpoint inserted using a line
9869 number, then skip the function prologue if necessary. */
6a048695 9870 if (sal->explicit_line)
059acae7 9871 skip_prologue_sal (sal);
c906108c
SS
9872 }
9873
9874 if (sal->section == 0 && sal->symtab != NULL)
9875 {
346d1dfe 9876 const struct blockvector *bv;
3977b71f 9877 const struct block *b;
c5aa993b 9878 struct symbol *sym;
c906108c 9879
43f3e411
DE
9880 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9881 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9882 if (bv != NULL)
9883 {
7f0df278 9884 sym = block_linkage_function (b);
c906108c
SS
9885 if (sym != NULL)
9886 {
eb822aa6
DE
9887 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9888 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9889 sym);
c906108c
SS
9890 }
9891 else
9892 {
4a64f543
MS
9893 /* It really is worthwhile to have the section, so we'll
9894 just have to look harder. This case can be executed
9895 if we have line numbers but no functions (as can
9896 happen in assembly source). */
c906108c 9897
5ed8105e 9898 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9899 switch_to_program_space_and_thread (sal->pspace);
c906108c 9900
5ed8105e 9901 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9902 if (msym.minsym)
efd66ac6 9903 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9904 }
9905 }
9906 }
9907}
9908
9909void
fba45db2 9910break_command (char *arg, int from_tty)
c906108c 9911{
db107f19 9912 break_command_1 (arg, 0, from_tty);
c906108c
SS
9913}
9914
c906108c 9915void
fba45db2 9916tbreak_command (char *arg, int from_tty)
c906108c 9917{
db107f19 9918 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9919}
9920
c906108c 9921static void
fba45db2 9922hbreak_command (char *arg, int from_tty)
c906108c 9923{
db107f19 9924 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9925}
9926
9927static void
fba45db2 9928thbreak_command (char *arg, int from_tty)
c906108c 9929{
db107f19 9930 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9931}
9932
9933static void
fba45db2 9934stop_command (char *arg, int from_tty)
c906108c 9935{
a3f17187 9936 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9937Usage: stop in <function | address>\n\
a3f17187 9938 stop at <line>\n"));
c906108c
SS
9939}
9940
9941static void
fba45db2 9942stopin_command (char *arg, int from_tty)
c906108c
SS
9943{
9944 int badInput = 0;
9945
c5aa993b 9946 if (arg == (char *) NULL)
c906108c
SS
9947 badInput = 1;
9948 else if (*arg != '*')
9949 {
9950 char *argptr = arg;
9951 int hasColon = 0;
9952
4a64f543 9953 /* Look for a ':'. If this is a line number specification, then
53a5351d 9954 say it is bad, otherwise, it should be an address or
4a64f543 9955 function/method name. */
c906108c 9956 while (*argptr && !hasColon)
c5aa993b
JM
9957 {
9958 hasColon = (*argptr == ':');
9959 argptr++;
9960 }
c906108c
SS
9961
9962 if (hasColon)
c5aa993b 9963 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9964 else
c5aa993b 9965 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9966 }
9967
9968 if (badInput)
a3f17187 9969 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9970 else
db107f19 9971 break_command_1 (arg, 0, from_tty);
c906108c
SS
9972}
9973
9974static void
fba45db2 9975stopat_command (char *arg, int from_tty)
c906108c
SS
9976{
9977 int badInput = 0;
9978
c5aa993b 9979 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9980 badInput = 1;
9981 else
9982 {
9983 char *argptr = arg;
9984 int hasColon = 0;
9985
4a64f543
MS
9986 /* Look for a ':'. If there is a '::' then get out, otherwise
9987 it is probably a line number. */
c906108c 9988 while (*argptr && !hasColon)
c5aa993b
JM
9989 {
9990 hasColon = (*argptr == ':');
9991 argptr++;
9992 }
c906108c
SS
9993
9994 if (hasColon)
c5aa993b 9995 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9996 else
c5aa993b 9997 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9998 }
9999
10000 if (badInput)
a3f17187 10001 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10002 else
db107f19 10003 break_command_1 (arg, 0, from_tty);
c906108c
SS
10004}
10005
e7e0cddf
SS
10006/* The dynamic printf command is mostly like a regular breakpoint, but
10007 with a prewired command list consisting of a single output command,
10008 built from extra arguments supplied on the dprintf command
10009 line. */
10010
da821c7b 10011static void
e7e0cddf
SS
10012dprintf_command (char *arg, int from_tty)
10013{
ffc2605c 10014 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
10015
10016 /* If non-NULL, ARG should have been advanced past the location;
10017 the next character must be ','. */
10018 if (arg != NULL)
10019 {
10020 if (arg[0] != ',' || arg[1] == '\0')
10021 error (_("Format string required"));
10022 else
10023 {
10024 /* Skip the comma. */
10025 ++arg;
10026 }
10027 }
10028
e7e0cddf 10029 create_breakpoint (get_current_arch (),
ffc2605c 10030 location.get (),
f00aae0f 10031 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10032 0, bp_dprintf,
10033 0 /* Ignore count */,
10034 pending_break_support,
10035 &dprintf_breakpoint_ops,
10036 from_tty,
10037 1 /* enabled */,
10038 0 /* internal */,
10039 0);
10040}
10041
d3ce09f5
SS
10042static void
10043agent_printf_command (char *arg, int from_tty)
10044{
10045 error (_("May only run agent-printf on the target"));
10046}
10047
f1310107
TJB
10048/* Implement the "breakpoint_hit" breakpoint_ops method for
10049 ranged breakpoints. */
10050
10051static int
10052breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10053 struct address_space *aspace,
09ac7c10
TT
10054 CORE_ADDR bp_addr,
10055 const struct target_waitstatus *ws)
f1310107 10056{
09ac7c10 10057 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10058 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10059 return 0;
10060
f1310107
TJB
10061 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10062 bl->length, aspace, bp_addr);
10063}
10064
10065/* Implement the "resources_needed" breakpoint_ops method for
10066 ranged breakpoints. */
10067
10068static int
10069resources_needed_ranged_breakpoint (const struct bp_location *bl)
10070{
10071 return target_ranged_break_num_registers ();
10072}
10073
10074/* Implement the "print_it" breakpoint_ops method for
10075 ranged breakpoints. */
10076
10077static enum print_stop_action
348d480f 10078print_it_ranged_breakpoint (bpstat bs)
f1310107 10079{
348d480f 10080 struct breakpoint *b = bs->breakpoint_at;
f1310107 10081 struct bp_location *bl = b->loc;
79a45e25 10082 struct ui_out *uiout = current_uiout;
f1310107
TJB
10083
10084 gdb_assert (b->type == bp_hardware_breakpoint);
10085
10086 /* Ranged breakpoints have only one location. */
10087 gdb_assert (bl && bl->next == NULL);
10088
10089 annotate_breakpoint (b->number);
f303dbd6
PA
10090
10091 maybe_print_thread_hit_breakpoint (uiout);
10092
f1310107 10093 if (b->disposition == disp_del)
112e8700 10094 uiout->text ("Temporary ranged breakpoint ");
f1310107 10095 else
112e8700
SM
10096 uiout->text ("Ranged breakpoint ");
10097 if (uiout->is_mi_like_p ())
f1310107 10098 {
112e8700 10099 uiout->field_string ("reason",
f1310107 10100 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 10101 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 10102 }
112e8700
SM
10103 uiout->field_int ("bkptno", b->number);
10104 uiout->text (", ");
f1310107
TJB
10105
10106 return PRINT_SRC_AND_LOC;
10107}
10108
10109/* Implement the "print_one" breakpoint_ops method for
10110 ranged breakpoints. */
10111
10112static void
10113print_one_ranged_breakpoint (struct breakpoint *b,
10114 struct bp_location **last_loc)
10115{
10116 struct bp_location *bl = b->loc;
10117 struct value_print_options opts;
79a45e25 10118 struct ui_out *uiout = current_uiout;
f1310107
TJB
10119
10120 /* Ranged breakpoints have only one location. */
10121 gdb_assert (bl && bl->next == NULL);
10122
10123 get_user_print_options (&opts);
10124
10125 if (opts.addressprint)
10126 /* We don't print the address range here, it will be printed later
10127 by print_one_detail_ranged_breakpoint. */
112e8700 10128 uiout->field_skip ("addr");
f1310107
TJB
10129 annotate_field (5);
10130 print_breakpoint_location (b, bl);
10131 *last_loc = bl;
10132}
10133
10134/* Implement the "print_one_detail" breakpoint_ops method for
10135 ranged breakpoints. */
10136
10137static void
10138print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10139 struct ui_out *uiout)
10140{
10141 CORE_ADDR address_start, address_end;
10142 struct bp_location *bl = b->loc;
d7e74731 10143 string_file stb;
f1310107
TJB
10144
10145 gdb_assert (bl);
10146
10147 address_start = bl->address;
10148 address_end = address_start + bl->length - 1;
10149
112e8700 10150 uiout->text ("\taddress range: ");
d7e74731
PA
10151 stb.printf ("[%s, %s]",
10152 print_core_address (bl->gdbarch, address_start),
10153 print_core_address (bl->gdbarch, address_end));
112e8700
SM
10154 uiout->field_stream ("addr", stb);
10155 uiout->text ("\n");
f1310107
TJB
10156}
10157
10158/* Implement the "print_mention" breakpoint_ops method for
10159 ranged breakpoints. */
10160
10161static void
10162print_mention_ranged_breakpoint (struct breakpoint *b)
10163{
10164 struct bp_location *bl = b->loc;
79a45e25 10165 struct ui_out *uiout = current_uiout;
f1310107
TJB
10166
10167 gdb_assert (bl);
10168 gdb_assert (b->type == bp_hardware_breakpoint);
10169
112e8700 10170 if (uiout->is_mi_like_p ())
f1310107
TJB
10171 return;
10172
10173 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10174 b->number, paddress (bl->gdbarch, bl->address),
10175 paddress (bl->gdbarch, bl->address + bl->length - 1));
10176}
10177
10178/* Implement the "print_recreate" breakpoint_ops method for
10179 ranged breakpoints. */
10180
10181static void
10182print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10183{
f00aae0f 10184 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
10185 event_location_to_string (b->location.get ()),
10186 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 10187 print_recreate_thread (b, fp);
f1310107
TJB
10188}
10189
10190/* The breakpoint_ops structure to be used in ranged breakpoints. */
10191
2060206e 10192static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10193
10194/* Find the address where the end of the breakpoint range should be
10195 placed, given the SAL of the end of the range. This is so that if
10196 the user provides a line number, the end of the range is set to the
10197 last instruction of the given line. */
10198
10199static CORE_ADDR
10200find_breakpoint_range_end (struct symtab_and_line sal)
10201{
10202 CORE_ADDR end;
10203
10204 /* If the user provided a PC value, use it. Otherwise,
10205 find the address of the end of the given location. */
10206 if (sal.explicit_pc)
10207 end = sal.pc;
10208 else
10209 {
10210 int ret;
10211 CORE_ADDR start;
10212
10213 ret = find_line_pc_range (sal, &start, &end);
10214 if (!ret)
10215 error (_("Could not find location of the end of the range."));
10216
10217 /* find_line_pc_range returns the start of the next line. */
10218 end--;
10219 }
10220
10221 return end;
10222}
10223
10224/* Implement the "break-range" CLI command. */
10225
10226static void
10227break_range_command (char *arg, int from_tty)
10228{
870f88f7 10229 char *arg_start, *addr_string_start;
f1310107
TJB
10230 struct linespec_result canonical_start, canonical_end;
10231 int bp_count, can_use_bp, length;
10232 CORE_ADDR end;
10233 struct breakpoint *b;
10234 struct symtab_and_line sal_start, sal_end;
f1310107 10235 struct cleanup *cleanup_bkpt;
f8eba3c6 10236 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10237
10238 /* We don't support software ranged breakpoints. */
10239 if (target_ranged_break_num_registers () < 0)
10240 error (_("This target does not support hardware ranged breakpoints."));
10241
10242 bp_count = hw_breakpoint_used_count ();
10243 bp_count += target_ranged_break_num_registers ();
10244 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10245 bp_count, 0);
10246 if (can_use_bp < 0)
10247 error (_("Hardware breakpoints used exceeds limit."));
10248
f8eba3c6 10249 arg = skip_spaces (arg);
f1310107
TJB
10250 if (arg == NULL || arg[0] == '\0')
10251 error(_("No address range specified."));
10252
f8eba3c6 10253 arg_start = arg;
ffc2605c
TT
10254 event_location_up start_location = string_to_event_location (&arg,
10255 current_language);
10256 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
10257
10258 if (arg[0] != ',')
10259 error (_("Too few arguments."));
f8eba3c6 10260 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10261 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10262
10263 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10264
10265 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10266 || lsal_start->sals.nelts != 1)
f1310107
TJB
10267 error (_("Cannot create a ranged breakpoint with multiple locations."));
10268
f8eba3c6
TT
10269 sal_start = lsal_start->sals.sals[0];
10270 addr_string_start = savestring (arg_start, arg - arg_start);
16e802b9 10271 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
f1310107
TJB
10272
10273 arg++; /* Skip the comma. */
f8eba3c6 10274 arg = skip_spaces (arg);
f1310107
TJB
10275
10276 /* Parse the end location. */
10277
f1310107
TJB
10278 arg_start = arg;
10279
f8eba3c6 10280 /* We call decode_line_full directly here instead of using
f1310107
TJB
10281 parse_breakpoint_sals because we need to specify the start location's
10282 symtab and line as the default symtab and line for the end of the
10283 range. This makes it possible to have ranges like "foo.c:27, +14",
10284 where +14 means 14 lines from the start location. */
ffc2605c
TT
10285 event_location_up end_location = string_to_event_location (&arg,
10286 current_language);
10287 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10288 sal_start.symtab, sal_start.line,
10289 &canonical_end, NULL, NULL);
10290
f8eba3c6 10291 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10292 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10293
10294 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10295 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10296 || lsal_end->sals.nelts != 1)
f1310107
TJB
10297 error (_("Cannot create a ranged breakpoint with multiple locations."));
10298
f8eba3c6 10299 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10300
10301 end = find_breakpoint_range_end (sal_end);
10302 if (sal_start.pc > end)
177b42fe 10303 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10304
10305 length = end - sal_start.pc + 1;
10306 if (length < 0)
10307 /* Length overflowed. */
10308 error (_("Address range too large."));
10309 else if (length == 1)
10310 {
10311 /* This range is simple enough to be handled by
10312 the `hbreak' command. */
10313 hbreak_command (addr_string_start, 1);
10314
10315 do_cleanups (cleanup_bkpt);
10316
10317 return;
10318 }
10319
10320 /* Now set up the breakpoint. */
10321 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10322 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10323 set_breakpoint_count (breakpoint_count + 1);
10324 b->number = breakpoint_count;
10325 b->disposition = disp_donttouch;
d28cd78a
TT
10326 b->location = std::move (start_location);
10327 b->location_range_end = std::move (end_location);
f1310107
TJB
10328 b->loc->length = length;
10329
f8eba3c6 10330 do_cleanups (cleanup_bkpt);
f1310107
TJB
10331
10332 mention (b);
8d3788bd 10333 observer_notify_breakpoint_created (b);
44702360 10334 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10335}
10336
4a64f543
MS
10337/* Return non-zero if EXP is verified as constant. Returned zero
10338 means EXP is variable. Also the constant detection may fail for
10339 some constant expressions and in such case still falsely return
10340 zero. */
2e6e3d9c 10341
65d79d4b
SDJ
10342static int
10343watchpoint_exp_is_const (const struct expression *exp)
10344{
10345 int i = exp->nelts;
10346
10347 while (i > 0)
10348 {
10349 int oplenp, argsp;
10350
10351 /* We are only interested in the descriptor of each element. */
10352 operator_length (exp, i, &oplenp, &argsp);
10353 i -= oplenp;
10354
10355 switch (exp->elts[i].opcode)
10356 {
10357 case BINOP_ADD:
10358 case BINOP_SUB:
10359 case BINOP_MUL:
10360 case BINOP_DIV:
10361 case BINOP_REM:
10362 case BINOP_MOD:
10363 case BINOP_LSH:
10364 case BINOP_RSH:
10365 case BINOP_LOGICAL_AND:
10366 case BINOP_LOGICAL_OR:
10367 case BINOP_BITWISE_AND:
10368 case BINOP_BITWISE_IOR:
10369 case BINOP_BITWISE_XOR:
10370 case BINOP_EQUAL:
10371 case BINOP_NOTEQUAL:
10372 case BINOP_LESS:
10373 case BINOP_GTR:
10374 case BINOP_LEQ:
10375 case BINOP_GEQ:
10376 case BINOP_REPEAT:
10377 case BINOP_COMMA:
10378 case BINOP_EXP:
10379 case BINOP_MIN:
10380 case BINOP_MAX:
10381 case BINOP_INTDIV:
10382 case BINOP_CONCAT:
65d79d4b
SDJ
10383 case TERNOP_COND:
10384 case TERNOP_SLICE:
65d79d4b
SDJ
10385
10386 case OP_LONG:
10387 case OP_DOUBLE:
10388 case OP_DECFLOAT:
10389 case OP_LAST:
10390 case OP_COMPLEX:
10391 case OP_STRING:
65d79d4b
SDJ
10392 case OP_ARRAY:
10393 case OP_TYPE:
608b4967
TT
10394 case OP_TYPEOF:
10395 case OP_DECLTYPE:
6e72ca20 10396 case OP_TYPEID:
65d79d4b
SDJ
10397 case OP_NAME:
10398 case OP_OBJC_NSSTRING:
10399
10400 case UNOP_NEG:
10401 case UNOP_LOGICAL_NOT:
10402 case UNOP_COMPLEMENT:
10403 case UNOP_ADDR:
10404 case UNOP_HIGH:
aeaa2474 10405 case UNOP_CAST:
9eaf6705
TT
10406
10407 case UNOP_CAST_TYPE:
10408 case UNOP_REINTERPRET_CAST:
10409 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10410 /* Unary, binary and ternary operators: We have to check
10411 their operands. If they are constant, then so is the
10412 result of that operation. For instance, if A and B are
10413 determined to be constants, then so is "A + B".
10414
10415 UNOP_IND is one exception to the rule above, because the
10416 value of *ADDR is not necessarily a constant, even when
10417 ADDR is. */
65d79d4b
SDJ
10418 break;
10419
10420 case OP_VAR_VALUE:
10421 /* Check whether the associated symbol is a constant.
4a64f543 10422
65d79d4b 10423 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10424 possible that a buggy compiler could mark a variable as
10425 constant even when it is not, and TYPE_CONST would return
10426 true in this case, while SYMBOL_CLASS wouldn't.
10427
10428 We also have to check for function symbols because they
10429 are always constant. */
65d79d4b
SDJ
10430 {
10431 struct symbol *s = exp->elts[i + 2].symbol;
10432
10433 if (SYMBOL_CLASS (s) != LOC_BLOCK
10434 && SYMBOL_CLASS (s) != LOC_CONST
10435 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10436 return 0;
10437 break;
10438 }
10439
10440 /* The default action is to return 0 because we are using
10441 the optimistic approach here: If we don't know something,
10442 then it is not a constant. */
10443 default:
10444 return 0;
10445 }
10446 }
10447
10448 return 1;
10449}
10450
c1fc2657 10451/* Watchpoint destructor. */
3a5c3e22 10452
c1fc2657 10453watchpoint::~watchpoint ()
3a5c3e22 10454{
c1fc2657
SM
10455 xfree (this->exp_string);
10456 xfree (this->exp_string_reparse);
10457 value_free (this->val);
3a5c3e22
PA
10458}
10459
348d480f
PA
10460/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10461
10462static void
10463re_set_watchpoint (struct breakpoint *b)
10464{
3a5c3e22
PA
10465 struct watchpoint *w = (struct watchpoint *) b;
10466
348d480f
PA
10467 /* Watchpoint can be either on expression using entirely global
10468 variables, or it can be on local variables.
10469
10470 Watchpoints of the first kind are never auto-deleted, and even
10471 persist across program restarts. Since they can use variables
10472 from shared libraries, we need to reparse expression as libraries
10473 are loaded and unloaded.
10474
10475 Watchpoints on local variables can also change meaning as result
10476 of solib event. For example, if a watchpoint uses both a local
10477 and a global variables in expression, it's a local watchpoint,
10478 but unloading of a shared library will make the expression
10479 invalid. This is not a very common use case, but we still
10480 re-evaluate expression, to avoid surprises to the user.
10481
10482 Note that for local watchpoints, we re-evaluate it only if
10483 watchpoints frame id is still valid. If it's not, it means the
10484 watchpoint is out of scope and will be deleted soon. In fact,
10485 I'm not sure we'll ever be called in this case.
10486
10487 If a local watchpoint's frame id is still valid, then
3a5c3e22 10488 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10489
3a5c3e22
PA
10490 Don't do anything about disabled watchpoints, since they will be
10491 reevaluated again when enabled. */
10492 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10493}
10494
77b06cd7
TJB
10495/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10496
10497static int
10498insert_watchpoint (struct bp_location *bl)
10499{
3a5c3e22
PA
10500 struct watchpoint *w = (struct watchpoint *) bl->owner;
10501 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10502
10503 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10504 w->cond_exp.get ());
77b06cd7
TJB
10505}
10506
10507/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10508
10509static int
73971819 10510remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10511{
3a5c3e22
PA
10512 struct watchpoint *w = (struct watchpoint *) bl->owner;
10513 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10514
10515 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10516 w->cond_exp.get ());
e09342b5
TJB
10517}
10518
e09342b5 10519static int
348d480f 10520breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10521 struct address_space *aspace, CORE_ADDR bp_addr,
10522 const struct target_waitstatus *ws)
e09342b5 10523{
348d480f 10524 struct breakpoint *b = bl->owner;
3a5c3e22 10525 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10526
348d480f
PA
10527 /* Continuable hardware watchpoints are treated as non-existent if the
10528 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10529 some data address). Otherwise gdb won't stop on a break instruction
10530 in the code (not from a breakpoint) when a hardware watchpoint has
10531 been defined. Also skip watchpoints which we know did not trigger
10532 (did not match the data address). */
10533 if (is_hardware_watchpoint (b)
3a5c3e22 10534 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10535 return 0;
9c06b0b4 10536
348d480f 10537 return 1;
9c06b0b4
TJB
10538}
10539
348d480f
PA
10540static void
10541check_status_watchpoint (bpstat bs)
9c06b0b4 10542{
348d480f 10543 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10544
348d480f 10545 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10546}
10547
10548/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10549 hardware watchpoints. */
9c06b0b4
TJB
10550
10551static int
348d480f 10552resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10553{
3a5c3e22
PA
10554 struct watchpoint *w = (struct watchpoint *) bl->owner;
10555 int length = w->exact? 1 : bl->length;
348d480f
PA
10556
10557 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10558}
10559
10560/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10561 hardware watchpoints. */
9c06b0b4
TJB
10562
10563static int
348d480f 10564works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10565{
efa80663
PA
10566 /* Read and access watchpoints only work with hardware support. */
10567 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10568}
10569
9c06b0b4 10570static enum print_stop_action
348d480f 10571print_it_watchpoint (bpstat bs)
9c06b0b4 10572{
348d480f
PA
10573 struct cleanup *old_chain;
10574 struct breakpoint *b;
348d480f 10575 enum print_stop_action result;
3a5c3e22 10576 struct watchpoint *w;
79a45e25 10577 struct ui_out *uiout = current_uiout;
348d480f
PA
10578
10579 gdb_assert (bs->bp_location_at != NULL);
10580
348d480f 10581 b = bs->breakpoint_at;
3a5c3e22 10582 w = (struct watchpoint *) b;
348d480f 10583
d7e74731 10584 old_chain = make_cleanup (null_cleanup, NULL);
9c06b0b4 10585
f303dbd6
PA
10586 annotate_watchpoint (b->number);
10587 maybe_print_thread_hit_breakpoint (uiout);
10588
d7e74731
PA
10589 string_file stb;
10590
9c06b0b4
TJB
10591 switch (b->type)
10592 {
348d480f 10593 case bp_watchpoint:
9c06b0b4 10594 case bp_hardware_watchpoint:
112e8700
SM
10595 if (uiout->is_mi_like_p ())
10596 uiout->field_string
10597 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10598 mention (b);
10599 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10600 uiout->text ("\nOld value = ");
d7e74731 10601 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10602 uiout->field_stream ("old", stb);
10603 uiout->text ("\nNew value = ");
d7e74731 10604 watchpoint_value_print (w->val, &stb);
112e8700
SM
10605 uiout->field_stream ("new", stb);
10606 uiout->text ("\n");
348d480f
PA
10607 /* More than one watchpoint may have been triggered. */
10608 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10609 break;
10610
10611 case bp_read_watchpoint:
112e8700
SM
10612 if (uiout->is_mi_like_p ())
10613 uiout->field_string
10614 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10615 mention (b);
10616 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10617 uiout->text ("\nValue = ");
d7e74731 10618 watchpoint_value_print (w->val, &stb);
112e8700
SM
10619 uiout->field_stream ("value", stb);
10620 uiout->text ("\n");
348d480f 10621 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10622 break;
10623
10624 case bp_access_watchpoint:
348d480f
PA
10625 if (bs->old_val != NULL)
10626 {
112e8700
SM
10627 if (uiout->is_mi_like_p ())
10628 uiout->field_string
10629 ("reason",
348d480f
PA
10630 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10631 mention (b);
10632 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10633 uiout->text ("\nOld value = ");
d7e74731 10634 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10635 uiout->field_stream ("old", stb);
10636 uiout->text ("\nNew value = ");
348d480f
PA
10637 }
10638 else
10639 {
10640 mention (b);
112e8700
SM
10641 if (uiout->is_mi_like_p ())
10642 uiout->field_string
10643 ("reason",
348d480f
PA
10644 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10645 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
112e8700 10646 uiout->text ("\nValue = ");
348d480f 10647 }
d7e74731 10648 watchpoint_value_print (w->val, &stb);
112e8700
SM
10649 uiout->field_stream ("new", stb);
10650 uiout->text ("\n");
348d480f 10651 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10652 break;
10653 default:
348d480f 10654 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10655 }
10656
348d480f
PA
10657 do_cleanups (old_chain);
10658 return result;
10659}
10660
10661/* Implement the "print_mention" breakpoint_ops method for hardware
10662 watchpoints. */
10663
10664static void
10665print_mention_watchpoint (struct breakpoint *b)
10666{
3a5c3e22 10667 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10668 struct ui_out *uiout = current_uiout;
46b9c129 10669 const char *tuple_name;
348d480f
PA
10670
10671 switch (b->type)
10672 {
10673 case bp_watchpoint:
112e8700 10674 uiout->text ("Watchpoint ");
46b9c129 10675 tuple_name = "wpt";
348d480f
PA
10676 break;
10677 case bp_hardware_watchpoint:
112e8700 10678 uiout->text ("Hardware watchpoint ");
46b9c129 10679 tuple_name = "wpt";
348d480f
PA
10680 break;
10681 case bp_read_watchpoint:
112e8700 10682 uiout->text ("Hardware read watchpoint ");
46b9c129 10683 tuple_name = "hw-rwpt";
348d480f
PA
10684 break;
10685 case bp_access_watchpoint:
112e8700 10686 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10687 tuple_name = "hw-awpt";
348d480f
PA
10688 break;
10689 default:
10690 internal_error (__FILE__, __LINE__,
10691 _("Invalid hardware watchpoint type."));
10692 }
10693
46b9c129 10694 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10695 uiout->field_int ("number", b->number);
10696 uiout->text (": ");
10697 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10698}
10699
10700/* Implement the "print_recreate" breakpoint_ops method for
10701 watchpoints. */
10702
10703static void
10704print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10705{
3a5c3e22
PA
10706 struct watchpoint *w = (struct watchpoint *) b;
10707
348d480f
PA
10708 switch (b->type)
10709 {
10710 case bp_watchpoint:
10711 case bp_hardware_watchpoint:
10712 fprintf_unfiltered (fp, "watch");
10713 break;
10714 case bp_read_watchpoint:
10715 fprintf_unfiltered (fp, "rwatch");
10716 break;
10717 case bp_access_watchpoint:
10718 fprintf_unfiltered (fp, "awatch");
10719 break;
10720 default:
10721 internal_error (__FILE__, __LINE__,
10722 _("Invalid watchpoint type."));
10723 }
10724
3a5c3e22 10725 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10726 print_recreate_thread (b, fp);
348d480f
PA
10727}
10728
427cd150
TT
10729/* Implement the "explains_signal" breakpoint_ops method for
10730 watchpoints. */
10731
47591c29 10732static int
427cd150
TT
10733explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10734{
10735 /* A software watchpoint cannot cause a signal other than
10736 GDB_SIGNAL_TRAP. */
10737 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10738 return 0;
427cd150 10739
47591c29 10740 return 1;
427cd150
TT
10741}
10742
348d480f
PA
10743/* The breakpoint_ops structure to be used in hardware watchpoints. */
10744
2060206e 10745static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10746
10747/* Implement the "insert" breakpoint_ops method for
10748 masked hardware watchpoints. */
10749
10750static int
10751insert_masked_watchpoint (struct bp_location *bl)
10752{
3a5c3e22
PA
10753 struct watchpoint *w = (struct watchpoint *) bl->owner;
10754
10755 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10756 bl->watchpoint_type);
10757}
10758
10759/* Implement the "remove" breakpoint_ops method for
10760 masked hardware watchpoints. */
10761
10762static int
73971819 10763remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10764{
3a5c3e22
PA
10765 struct watchpoint *w = (struct watchpoint *) bl->owner;
10766
10767 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10768 bl->watchpoint_type);
10769}
10770
10771/* Implement the "resources_needed" breakpoint_ops method for
10772 masked hardware watchpoints. */
10773
10774static int
10775resources_needed_masked_watchpoint (const struct bp_location *bl)
10776{
3a5c3e22
PA
10777 struct watchpoint *w = (struct watchpoint *) bl->owner;
10778
10779 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10780}
10781
10782/* Implement the "works_in_software_mode" breakpoint_ops method for
10783 masked hardware watchpoints. */
10784
10785static int
10786works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10787{
10788 return 0;
10789}
10790
10791/* Implement the "print_it" breakpoint_ops method for
10792 masked hardware watchpoints. */
10793
10794static enum print_stop_action
10795print_it_masked_watchpoint (bpstat bs)
10796{
10797 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10798 struct ui_out *uiout = current_uiout;
348d480f
PA
10799
10800 /* Masked watchpoints have only one location. */
10801 gdb_assert (b->loc && b->loc->next == NULL);
10802
f303dbd6
PA
10803 annotate_watchpoint (b->number);
10804 maybe_print_thread_hit_breakpoint (uiout);
10805
348d480f
PA
10806 switch (b->type)
10807 {
10808 case bp_hardware_watchpoint:
112e8700
SM
10809 if (uiout->is_mi_like_p ())
10810 uiout->field_string
10811 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10812 break;
10813
10814 case bp_read_watchpoint:
112e8700
SM
10815 if (uiout->is_mi_like_p ())
10816 uiout->field_string
10817 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10818 break;
10819
10820 case bp_access_watchpoint:
112e8700
SM
10821 if (uiout->is_mi_like_p ())
10822 uiout->field_string
10823 ("reason",
348d480f
PA
10824 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10825 break;
10826 default:
10827 internal_error (__FILE__, __LINE__,
10828 _("Invalid hardware watchpoint type."));
10829 }
10830
10831 mention (b);
112e8700 10832 uiout->text (_("\n\
9c06b0b4
TJB
10833Check the underlying instruction at PC for the memory\n\
10834address and value which triggered this watchpoint.\n"));
112e8700 10835 uiout->text ("\n");
9c06b0b4
TJB
10836
10837 /* More than one watchpoint may have been triggered. */
10838 return PRINT_UNKNOWN;
10839}
10840
10841/* Implement the "print_one_detail" breakpoint_ops method for
10842 masked hardware watchpoints. */
10843
10844static void
10845print_one_detail_masked_watchpoint (const struct breakpoint *b,
10846 struct ui_out *uiout)
10847{
3a5c3e22
PA
10848 struct watchpoint *w = (struct watchpoint *) b;
10849
9c06b0b4
TJB
10850 /* Masked watchpoints have only one location. */
10851 gdb_assert (b->loc && b->loc->next == NULL);
10852
112e8700
SM
10853 uiout->text ("\tmask ");
10854 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10855 uiout->text ("\n");
9c06b0b4
TJB
10856}
10857
10858/* Implement the "print_mention" breakpoint_ops method for
10859 masked hardware watchpoints. */
10860
10861static void
10862print_mention_masked_watchpoint (struct breakpoint *b)
10863{
3a5c3e22 10864 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10865 struct ui_out *uiout = current_uiout;
46b9c129 10866 const char *tuple_name;
9c06b0b4
TJB
10867
10868 switch (b->type)
10869 {
10870 case bp_hardware_watchpoint:
112e8700 10871 uiout->text ("Masked hardware watchpoint ");
46b9c129 10872 tuple_name = "wpt";
9c06b0b4
TJB
10873 break;
10874 case bp_read_watchpoint:
112e8700 10875 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10876 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10877 break;
10878 case bp_access_watchpoint:
112e8700 10879 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10880 tuple_name = "hw-awpt";
9c06b0b4
TJB
10881 break;
10882 default:
10883 internal_error (__FILE__, __LINE__,
10884 _("Invalid hardware watchpoint type."));
10885 }
10886
46b9c129 10887 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10888 uiout->field_int ("number", b->number);
10889 uiout->text (": ");
10890 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10891}
10892
10893/* Implement the "print_recreate" breakpoint_ops method for
10894 masked hardware watchpoints. */
10895
10896static void
10897print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10898{
3a5c3e22 10899 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10900 char tmp[40];
10901
10902 switch (b->type)
10903 {
10904 case bp_hardware_watchpoint:
10905 fprintf_unfiltered (fp, "watch");
10906 break;
10907 case bp_read_watchpoint:
10908 fprintf_unfiltered (fp, "rwatch");
10909 break;
10910 case bp_access_watchpoint:
10911 fprintf_unfiltered (fp, "awatch");
10912 break;
10913 default:
10914 internal_error (__FILE__, __LINE__,
10915 _("Invalid hardware watchpoint type."));
10916 }
10917
3a5c3e22
PA
10918 sprintf_vma (tmp, w->hw_wp_mask);
10919 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10920 print_recreate_thread (b, fp);
9c06b0b4
TJB
10921}
10922
10923/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10924
2060206e 10925static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10926
10927/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10928
10929static int
10930is_masked_watchpoint (const struct breakpoint *b)
10931{
10932 return b->ops == &masked_watchpoint_breakpoint_ops;
10933}
10934
53a5351d
JM
10935/* accessflag: hw_write: watch write,
10936 hw_read: watch read,
10937 hw_access: watch access (read or write) */
c906108c 10938static void
bbc13ae3 10939watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10940 int just_location, int internal)
c906108c 10941{
c1fc2657 10942 struct breakpoint *scope_breakpoint = NULL;
270140bd 10943 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10944 struct value *val, *mark, *result;
bb9d5f81 10945 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10946 const char *exp_start = NULL;
10947 const char *exp_end = NULL;
10948 const char *tok, *end_tok;
9c06b0b4 10949 int toklen = -1;
bbc13ae3
KS
10950 const char *cond_start = NULL;
10951 const char *cond_end = NULL;
c906108c 10952 enum bptype bp_type;
37e4754d 10953 int thread = -1;
0cf6dd15 10954 int pc = 0;
9c06b0b4
TJB
10955 /* Flag to indicate whether we are going to use masks for
10956 the hardware watchpoint. */
10957 int use_mask = 0;
10958 CORE_ADDR mask = 0;
bbc13ae3
KS
10959 char *expression;
10960 struct cleanup *back_to;
c906108c 10961
37e4754d
LM
10962 /* Make sure that we actually have parameters to parse. */
10963 if (arg != NULL && arg[0] != '\0')
10964 {
bbc13ae3
KS
10965 const char *value_start;
10966
10967 exp_end = arg + strlen (arg);
37e4754d 10968
9c06b0b4
TJB
10969 /* Look for "parameter value" pairs at the end
10970 of the arguments string. */
bbc13ae3 10971 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10972 {
10973 /* Skip whitespace at the end of the argument list. */
10974 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10975 tok--;
10976
10977 /* Find the beginning of the last token.
10978 This is the value of the parameter. */
10979 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10980 tok--;
10981 value_start = tok + 1;
10982
10983 /* Skip whitespace. */
10984 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10985 tok--;
10986
10987 end_tok = tok;
10988
10989 /* Find the beginning of the second to last token.
10990 This is the parameter itself. */
10991 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10992 tok--;
10993 tok++;
10994 toklen = end_tok - tok + 1;
10995
61012eef 10996 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10997 {
5d5658a1 10998 struct thread_info *thr;
9c06b0b4
TJB
10999 /* At this point we've found a "thread" token, which means
11000 the user is trying to set a watchpoint that triggers
11001 only in a specific thread. */
5d5658a1 11002 const char *endp;
37e4754d 11003
9c06b0b4
TJB
11004 if (thread != -1)
11005 error(_("You can specify only one thread."));
37e4754d 11006
9c06b0b4 11007 /* Extract the thread ID from the next token. */
5d5658a1 11008 thr = parse_thread_id (value_start, &endp);
37e4754d 11009
5d5658a1 11010 /* Check if the user provided a valid thread ID. */
9c06b0b4 11011 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11012 invalid_thread_id_error (value_start);
9c06b0b4 11013
5d5658a1 11014 thread = thr->global_num;
9c06b0b4 11015 }
61012eef 11016 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11017 {
11018 /* We've found a "mask" token, which means the user wants to
11019 create a hardware watchpoint that is going to have the mask
11020 facility. */
11021 struct value *mask_value, *mark;
37e4754d 11022
9c06b0b4
TJB
11023 if (use_mask)
11024 error(_("You can specify only one mask."));
37e4754d 11025
9c06b0b4 11026 use_mask = just_location = 1;
37e4754d 11027
9c06b0b4
TJB
11028 mark = value_mark ();
11029 mask_value = parse_to_comma_and_eval (&value_start);
11030 mask = value_as_address (mask_value);
11031 value_free_to_mark (mark);
11032 }
11033 else
11034 /* We didn't recognize what we found. We should stop here. */
11035 break;
37e4754d 11036
9c06b0b4
TJB
11037 /* Truncate the string and get rid of the "parameter value" pair before
11038 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11039 exp_end = tok;
9c06b0b4 11040 }
37e4754d 11041 }
bbc13ae3
KS
11042 else
11043 exp_end = arg;
37e4754d 11044
bbc13ae3
KS
11045 /* Parse the rest of the arguments. From here on out, everything
11046 is in terms of a newly allocated string instead of the original
11047 ARG. */
c906108c 11048 innermost_block = NULL;
bbc13ae3
KS
11049 expression = savestring (arg, exp_end - arg);
11050 back_to = make_cleanup (xfree, expression);
11051 exp_start = arg = expression;
4d01a485 11052 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11053 exp_end = arg;
fa8a61dc
TT
11054 /* Remove trailing whitespace from the expression before saving it.
11055 This makes the eventual display of the expression string a bit
11056 prettier. */
11057 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11058 --exp_end;
11059
65d79d4b 11060 /* Checking if the expression is not constant. */
4d01a485 11061 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
11062 {
11063 int len;
11064
11065 len = exp_end - exp_start;
11066 while (len > 0 && isspace (exp_start[len - 1]))
11067 len--;
11068 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11069 }
11070
c906108c
SS
11071 exp_valid_block = innermost_block;
11072 mark = value_mark ();
4d01a485 11073 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 11074
bb9d5f81
PP
11075 if (val != NULL && just_location)
11076 {
11077 saved_bitpos = value_bitpos (val);
11078 saved_bitsize = value_bitsize (val);
11079 }
11080
06a64a0b
TT
11081 if (just_location)
11082 {
9c06b0b4
TJB
11083 int ret;
11084
06a64a0b 11085 exp_valid_block = NULL;
a1442452 11086 val = value_addr (result);
06a64a0b
TT
11087 release_value (val);
11088 value_free_to_mark (mark);
9c06b0b4
TJB
11089
11090 if (use_mask)
11091 {
11092 ret = target_masked_watch_num_registers (value_as_address (val),
11093 mask);
11094 if (ret == -1)
11095 error (_("This target does not support masked watchpoints."));
11096 else if (ret == -2)
11097 error (_("Invalid mask or memory region."));
11098 }
06a64a0b
TT
11099 }
11100 else if (val != NULL)
fa4727a6 11101 release_value (val);
c906108c 11102
bbc13ae3
KS
11103 tok = skip_spaces_const (arg);
11104 end_tok = skip_to_space_const (tok);
c906108c
SS
11105
11106 toklen = end_tok - tok;
11107 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11108 {
60e1c644 11109 innermost_block = NULL;
c906108c 11110 tok = cond_start = end_tok + 1;
4d01a485 11111 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11112
11113 /* The watchpoint expression may not be local, but the condition
11114 may still be. E.g.: `watch global if local > 0'. */
11115 cond_exp_valid_block = innermost_block;
11116
c906108c
SS
11117 cond_end = tok;
11118 }
11119 if (*tok)
8a3fe4f8 11120 error (_("Junk at end of command."));
c906108c 11121
441d7c93
PA
11122 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11123
11124 /* Save this because create_internal_breakpoint below invalidates
11125 'wp_frame'. */
11126 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
11127
11128 /* If the expression is "local", then set up a "watchpoint scope"
11129 breakpoint at the point where we've left the scope of the watchpoint
11130 expression. Create the scope breakpoint before the watchpoint, so
11131 that we will encounter it first in bpstat_stop_status. */
441d7c93 11132 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 11133 {
441d7c93
PA
11134 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11135
11136 if (frame_id_p (caller_frame_id))
edb3359d 11137 {
441d7c93
PA
11138 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11139 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11140
edb3359d 11141 scope_breakpoint
441d7c93 11142 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
11143 bp_watchpoint_scope,
11144 &momentary_breakpoint_ops);
d983da9c 11145
441d7c93
PA
11146 /* create_internal_breakpoint could invalidate WP_FRAME. */
11147 wp_frame = NULL;
11148
edb3359d 11149 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11150
edb3359d
DJ
11151 /* Automatically delete the breakpoint when it hits. */
11152 scope_breakpoint->disposition = disp_del;
d983da9c 11153
edb3359d 11154 /* Only break in the proper frame (help with recursion). */
441d7c93 11155 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 11156
edb3359d 11157 /* Set the address at which we will stop. */
441d7c93
PA
11158 scope_breakpoint->loc->gdbarch = caller_arch;
11159 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 11160 scope_breakpoint->loc->address
a6d9a66e
UW
11161 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11162 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11163 scope_breakpoint->type);
11164 }
d983da9c
DJ
11165 }
11166
e8369a73
AB
11167 /* Now set up the breakpoint. We create all watchpoints as hardware
11168 watchpoints here even if hardware watchpoints are turned off, a call
11169 to update_watchpoint later in this function will cause the type to
11170 drop back to bp_watchpoint (software watchpoint) if required. */
11171
11172 if (accessflag == hw_read)
11173 bp_type = bp_read_watchpoint;
11174 else if (accessflag == hw_access)
11175 bp_type = bp_access_watchpoint;
11176 else
11177 bp_type = bp_hardware_watchpoint;
3a5c3e22 11178
b270e6f9 11179 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 11180
348d480f 11181 if (use_mask)
b270e6f9 11182 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11183 &masked_watchpoint_breakpoint_ops);
348d480f 11184 else
b270e6f9 11185 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 11186 &watchpoint_breakpoint_ops);
c1fc2657
SM
11187 w->thread = thread;
11188 w->disposition = disp_donttouch;
11189 w->pspace = current_program_space;
b22e99fd 11190 w->exp = std::move (exp);
3a5c3e22
PA
11191 w->exp_valid_block = exp_valid_block;
11192 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11193 if (just_location)
11194 {
11195 struct type *t = value_type (val);
11196 CORE_ADDR addr = value_as_address (val);
06a64a0b 11197
43cc5389
TT
11198 w->exp_string_reparse
11199 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 11200
3a5c3e22 11201 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 11202 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
11203 }
11204 else
3a5c3e22 11205 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11206
11207 if (use_mask)
11208 {
3a5c3e22 11209 w->hw_wp_mask = mask;
9c06b0b4
TJB
11210 }
11211 else
11212 {
3a5c3e22 11213 w->val = val;
bb9d5f81
PP
11214 w->val_bitpos = saved_bitpos;
11215 w->val_bitsize = saved_bitsize;
3a5c3e22 11216 w->val_valid = 1;
9c06b0b4 11217 }
77b06cd7 11218
c906108c 11219 if (cond_start)
c1fc2657 11220 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 11221 else
c1fc2657 11222 w->cond_string = 0;
c5aa993b 11223
441d7c93 11224 if (frame_id_p (watchpoint_frame))
f6bc2008 11225 {
441d7c93 11226 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 11227 w->watchpoint_thread = inferior_ptid;
f6bc2008 11228 }
c906108c 11229 else
f6bc2008 11230 {
3a5c3e22
PA
11231 w->watchpoint_frame = null_frame_id;
11232 w->watchpoint_thread = null_ptid;
f6bc2008 11233 }
c906108c 11234
d983da9c 11235 if (scope_breakpoint != NULL)
c906108c 11236 {
d983da9c
DJ
11237 /* The scope breakpoint is related to the watchpoint. We will
11238 need to act on them together. */
c1fc2657 11239 w->related_breakpoint = scope_breakpoint;
b270e6f9 11240 scope_breakpoint->related_breakpoint = w.get ();
c906108c 11241 }
d983da9c 11242
06a64a0b
TT
11243 if (!just_location)
11244 value_free_to_mark (mark);
2d134ed3 11245
b270e6f9
TT
11246 /* Finally update the new watchpoint. This creates the locations
11247 that should be inserted. */
11248 update_watchpoint (w.get (), 1);
a9634178 11249
b270e6f9 11250 install_breakpoint (internal, std::move (w), 1);
bbc13ae3 11251 do_cleanups (back_to);
c906108c
SS
11252}
11253
e09342b5 11254/* Return count of debug registers needed to watch the given expression.
e09342b5 11255 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11256
c906108c 11257static int
a9634178 11258can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11259{
11260 int found_memory_cnt = 0;
2e70b7b9 11261 struct value *head = v;
c906108c
SS
11262
11263 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11264 if (!can_use_hw_watchpoints)
c906108c 11265 return 0;
c5aa993b 11266
5c44784c
JM
11267 /* Make sure that the value of the expression depends only upon
11268 memory contents, and values computed from them within GDB. If we
11269 find any register references or function calls, we can't use a
11270 hardware watchpoint.
11271
11272 The idea here is that evaluating an expression generates a series
11273 of values, one holding the value of every subexpression. (The
11274 expression a*b+c has five subexpressions: a, b, a*b, c, and
11275 a*b+c.) GDB's values hold almost enough information to establish
11276 the criteria given above --- they identify memory lvalues,
11277 register lvalues, computed values, etcetera. So we can evaluate
11278 the expression, and then scan the chain of values that leaves
11279 behind to decide whether we can detect any possible change to the
11280 expression's final value using only hardware watchpoints.
11281
11282 However, I don't think that the values returned by inferior
11283 function calls are special in any way. So this function may not
11284 notice that an expression involving an inferior function call
11285 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11286 for (; v; v = value_next (v))
c906108c 11287 {
5c44784c 11288 if (VALUE_LVAL (v) == lval_memory)
c906108c 11289 {
8464be76
DJ
11290 if (v != head && value_lazy (v))
11291 /* A lazy memory lvalue in the chain is one that GDB never
11292 needed to fetch; we either just used its address (e.g.,
11293 `a' in `a.b') or we never needed it at all (e.g., `a'
11294 in `a,b'). This doesn't apply to HEAD; if that is
11295 lazy then it was not readable, but watch it anyway. */
5c44784c 11296 ;
53a5351d 11297 else
5c44784c
JM
11298 {
11299 /* Ahh, memory we actually used! Check if we can cover
11300 it with hardware watchpoints. */
df407dfe 11301 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11302
11303 /* We only watch structs and arrays if user asked for it
11304 explicitly, never if they just happen to appear in a
11305 middle of some value chain. */
11306 if (v == head
11307 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11308 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11309 {
42ae5230 11310 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11311 int len;
11312 int num_regs;
11313
a9634178 11314 len = (target_exact_watchpoints
e09342b5
TJB
11315 && is_scalar_type_recursive (vtype))?
11316 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11317
e09342b5
TJB
11318 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11319 if (!num_regs)
2e70b7b9
MS
11320 return 0;
11321 else
e09342b5 11322 found_memory_cnt += num_regs;
2e70b7b9 11323 }
5c44784c 11324 }
c5aa993b 11325 }
5086187c
AC
11326 else if (VALUE_LVAL (v) != not_lval
11327 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11328 return 0; /* These are values from the history (e.g., $1). */
5086187c 11329 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11330 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11331 }
11332
11333 /* The expression itself looks suitable for using a hardware
11334 watchpoint, but give the target machine a chance to reject it. */
11335 return found_memory_cnt;
11336}
11337
8b93c638 11338void
84f4c1fe 11339watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11340{
84f4c1fe 11341 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11342}
11343
06a64a0b
TT
11344/* A helper function that looks for the "-location" argument and then
11345 calls watch_command_1. */
11346
11347static void
11348watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11349{
11350 int just_location = 0;
11351
11352 if (arg
11353 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11354 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11355 {
e9cafbcc 11356 arg = skip_spaces (arg);
06a64a0b
TT
11357 just_location = 1;
11358 }
11359
84f4c1fe 11360 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11361}
8926118c 11362
c5aa993b 11363static void
fba45db2 11364watch_command (char *arg, int from_tty)
c906108c 11365{
06a64a0b 11366 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11367}
11368
8b93c638 11369void
84f4c1fe 11370rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11371{
84f4c1fe 11372 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11373}
8926118c 11374
c5aa993b 11375static void
fba45db2 11376rwatch_command (char *arg, int from_tty)
c906108c 11377{
06a64a0b 11378 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11379}
11380
8b93c638 11381void
84f4c1fe 11382awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11383{
84f4c1fe 11384 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11385}
8926118c 11386
c5aa993b 11387static void
fba45db2 11388awatch_command (char *arg, int from_tty)
c906108c 11389{
06a64a0b 11390 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11391}
c906108c 11392\f
c5aa993b 11393
cfc31633
PA
11394/* Data for the FSM that manages the until(location)/advance commands
11395 in infcmd.c. Here because it uses the mechanisms of
11396 breakpoints. */
c906108c 11397
cfc31633 11398struct until_break_fsm
bfec99b2 11399{
cfc31633
PA
11400 /* The base class. */
11401 struct thread_fsm thread_fsm;
11402
11403 /* The thread that as current when the command was executed. */
11404 int thread;
11405
11406 /* The breakpoint set at the destination location. */
11407 struct breakpoint *location_breakpoint;
11408
11409 /* Breakpoint set at the return address in the caller frame. May be
11410 NULL. */
11411 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11412};
11413
8980e177
PA
11414static void until_break_fsm_clean_up (struct thread_fsm *self,
11415 struct thread_info *thread);
11416static int until_break_fsm_should_stop (struct thread_fsm *self,
11417 struct thread_info *thread);
cfc31633
PA
11418static enum async_reply_reason
11419 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11420
11421/* until_break_fsm's vtable. */
11422
11423static struct thread_fsm_ops until_break_fsm_ops =
11424{
11425 NULL, /* dtor */
11426 until_break_fsm_clean_up,
11427 until_break_fsm_should_stop,
11428 NULL, /* return_value */
11429 until_break_fsm_async_reply_reason,
11430};
11431
11432/* Allocate a new until_break_command_fsm. */
11433
11434static struct until_break_fsm *
8980e177 11435new_until_break_fsm (struct interp *cmd_interp, int thread,
cfc31633
PA
11436 struct breakpoint *location_breakpoint,
11437 struct breakpoint *caller_breakpoint)
11438{
11439 struct until_break_fsm *sm;
11440
11441 sm = XCNEW (struct until_break_fsm);
8980e177 11442 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11443
11444 sm->thread = thread;
11445 sm->location_breakpoint = location_breakpoint;
11446 sm->caller_breakpoint = caller_breakpoint;
11447
11448 return sm;
11449}
11450
11451/* Implementation of the 'should_stop' FSM method for the
11452 until(location)/advance commands. */
11453
11454static int
8980e177
PA
11455until_break_fsm_should_stop (struct thread_fsm *self,
11456 struct thread_info *tp)
cfc31633
PA
11457{
11458 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11459
11460 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11461 sm->location_breakpoint) != NULL
11462 || (sm->caller_breakpoint != NULL
11463 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11464 sm->caller_breakpoint) != NULL))
11465 thread_fsm_set_finished (self);
11466
11467 return 1;
11468}
11469
11470/* Implementation of the 'clean_up' FSM method for the
11471 until(location)/advance commands. */
11472
c2c6d25f 11473static void
8980e177
PA
11474until_break_fsm_clean_up (struct thread_fsm *self,
11475 struct thread_info *thread)
43ff13b4 11476{
cfc31633 11477 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11478
cfc31633
PA
11479 /* Clean up our temporary breakpoints. */
11480 if (sm->location_breakpoint != NULL)
11481 {
11482 delete_breakpoint (sm->location_breakpoint);
11483 sm->location_breakpoint = NULL;
11484 }
11485 if (sm->caller_breakpoint != NULL)
11486 {
11487 delete_breakpoint (sm->caller_breakpoint);
11488 sm->caller_breakpoint = NULL;
11489 }
11490 delete_longjmp_breakpoint (sm->thread);
11491}
11492
11493/* Implementation of the 'async_reply_reason' FSM method for the
11494 until(location)/advance commands. */
11495
11496static enum async_reply_reason
11497until_break_fsm_async_reply_reason (struct thread_fsm *self)
11498{
11499 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11500}
11501
c906108c 11502void
ae66c1fc 11503until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11504{
11505 struct symtabs_and_lines sals;
11506 struct symtab_and_line sal;
8556afb4
PA
11507 struct frame_info *frame;
11508 struct gdbarch *frame_gdbarch;
11509 struct frame_id stack_frame_id;
11510 struct frame_id caller_frame_id;
cfc31633
PA
11511 struct breakpoint *location_breakpoint;
11512 struct breakpoint *caller_breakpoint = NULL;
ffc2605c 11513 struct cleanup *old_chain;
186c406b
TT
11514 int thread;
11515 struct thread_info *tp;
cfc31633 11516 struct until_break_fsm *sm;
c906108c 11517
70509625 11518 clear_proceed_status (0);
c906108c
SS
11519
11520 /* Set a breakpoint where the user wants it and at return from
4a64f543 11521 this function. */
c5aa993b 11522
ffc2605c 11523 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11524
1bfeeb0f 11525 if (last_displayed_sal_is_valid ())
ffc2605c 11526 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
1bfeeb0f 11527 get_last_displayed_symtab (),
f8eba3c6 11528 get_last_displayed_line ());
c906108c 11529 else
ffc2605c 11530 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
c2f4122d 11531 NULL, (struct symtab *) NULL, 0);
c5aa993b 11532
c906108c 11533 if (sals.nelts != 1)
8a3fe4f8 11534 error (_("Couldn't get information on specified line."));
c5aa993b 11535
c906108c 11536 sal = sals.sals[0];
4a64f543 11537 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11538
c906108c 11539 if (*arg)
8a3fe4f8 11540 error (_("Junk at end of arguments."));
c5aa993b 11541
c906108c 11542 resolve_sal_pc (&sal);
c5aa993b 11543
186c406b 11544 tp = inferior_thread ();
5d5658a1 11545 thread = tp->global_num;
186c406b 11546
883bc8d1
PA
11547 old_chain = make_cleanup (null_cleanup, NULL);
11548
8556afb4
PA
11549 /* Note linespec handling above invalidates the frame chain.
11550 Installing a breakpoint also invalidates the frame chain (as it
11551 may need to switch threads), so do any frame handling before
11552 that. */
11553
11554 frame = get_selected_frame (NULL);
11555 frame_gdbarch = get_frame_arch (frame);
11556 stack_frame_id = get_stack_frame_id (frame);
11557 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11558
ae66c1fc
EZ
11559 /* Keep within the current frame, or in frames called by the current
11560 one. */
edb3359d 11561
883bc8d1 11562 if (frame_id_p (caller_frame_id))
c906108c 11563 {
883bc8d1 11564 struct symtab_and_line sal2;
cfc31633 11565 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11566
11567 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11568 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11569 caller_gdbarch = frame_unwind_caller_arch (frame);
11570 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11571 sal2,
11572 caller_frame_id,
11573 bp_until);
11574 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11575
883bc8d1 11576 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11577 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11578 }
c5aa993b 11579
c70a6932
JK
11580 /* set_momentary_breakpoint could invalidate FRAME. */
11581 frame = NULL;
11582
883bc8d1
PA
11583 if (anywhere)
11584 /* If the user told us to continue until a specified location,
11585 we don't specify a frame at which we need to stop. */
cfc31633
PA
11586 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11587 null_frame_id, bp_until);
883bc8d1
PA
11588 else
11589 /* Otherwise, specify the selected frame, because we want to stop
11590 only at the very same frame. */
cfc31633
PA
11591 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11592 stack_frame_id, bp_until);
11593 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11594
8980e177 11595 sm = new_until_break_fsm (command_interp (), tp->global_num,
5d5658a1 11596 location_breakpoint, caller_breakpoint);
cfc31633 11597 tp->thread_fsm = &sm->thread_fsm;
f107f563 11598
cfc31633 11599 discard_cleanups (old_chain);
f107f563 11600
cfc31633 11601 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11602}
ae66c1fc 11603
c906108c
SS
11604/* This function attempts to parse an optional "if <cond>" clause
11605 from the arg string. If one is not found, it returns NULL.
c5aa993b 11606
c906108c
SS
11607 Else, it returns a pointer to the condition string. (It does not
11608 attempt to evaluate the string against a particular block.) And,
11609 it updates arg to point to the first character following the parsed
4a64f543 11610 if clause in the arg string. */
53a5351d 11611
63160a43
PA
11612const char *
11613ep_parse_optional_if_clause (const char **arg)
c906108c 11614{
63160a43 11615 const char *cond_string;
c5aa993b
JM
11616
11617 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11618 return NULL;
c5aa993b 11619
4a64f543 11620 /* Skip the "if" keyword. */
c906108c 11621 (*arg) += 2;
c5aa993b 11622
c906108c 11623 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11624 condition string. */
63160a43 11625 *arg = skip_spaces_const (*arg);
c906108c 11626 cond_string = *arg;
c5aa993b 11627
4a64f543
MS
11628 /* Assume that the condition occupies the remainder of the arg
11629 string. */
c906108c 11630 (*arg) += strlen (cond_string);
c5aa993b 11631
c906108c
SS
11632 return cond_string;
11633}
c5aa993b 11634
c906108c
SS
11635/* Commands to deal with catching events, such as signals, exceptions,
11636 process start/exit, etc. */
c5aa993b
JM
11637
11638typedef enum
11639{
44feb3ce
TT
11640 catch_fork_temporary, catch_vfork_temporary,
11641 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11642}
11643catch_fork_kind;
11644
c906108c 11645static void
63160a43 11646catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11647 struct cmd_list_element *command)
c906108c 11648{
63160a43 11649 const char *arg = arg_entry;
a6d9a66e 11650 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11651 const char *cond_string = NULL;
44feb3ce
TT
11652 catch_fork_kind fork_kind;
11653 int tempflag;
11654
11655 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11656 tempflag = (fork_kind == catch_fork_temporary
11657 || fork_kind == catch_vfork_temporary);
c5aa993b 11658
44feb3ce
TT
11659 if (!arg)
11660 arg = "";
63160a43 11661 arg = skip_spaces_const (arg);
c5aa993b 11662
c906108c 11663 /* The allowed syntax is:
c5aa993b
JM
11664 catch [v]fork
11665 catch [v]fork if <cond>
11666
4a64f543 11667 First, check if there's an if clause. */
c906108c 11668 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11669
c906108c 11670 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11671 error (_("Junk at end of arguments."));
c5aa993b 11672
c906108c 11673 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11674 and enable reporting of such events. */
c5aa993b
JM
11675 switch (fork_kind)
11676 {
44feb3ce
TT
11677 case catch_fork_temporary:
11678 case catch_fork_permanent:
a6d9a66e 11679 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11680 &catch_fork_breakpoint_ops);
c906108c 11681 break;
44feb3ce
TT
11682 case catch_vfork_temporary:
11683 case catch_vfork_permanent:
a6d9a66e 11684 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11685 &catch_vfork_breakpoint_ops);
c906108c 11686 break;
c5aa993b 11687 default:
8a3fe4f8 11688 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11689 break;
c5aa993b 11690 }
c906108c
SS
11691}
11692
11693static void
63160a43 11694catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11695 struct cmd_list_element *command)
c906108c 11696{
63160a43 11697 const char *arg = arg_entry;
a6d9a66e 11698 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11699 int tempflag;
63160a43 11700 const char *cond_string = NULL;
c906108c 11701
44feb3ce
TT
11702 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11703
11704 if (!arg)
11705 arg = "";
63160a43 11706 arg = skip_spaces_const (arg);
c906108c
SS
11707
11708 /* The allowed syntax is:
c5aa993b
JM
11709 catch exec
11710 catch exec if <cond>
c906108c 11711
4a64f543 11712 First, check if there's an if clause. */
c906108c
SS
11713 cond_string = ep_parse_optional_if_clause (&arg);
11714
11715 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11716 error (_("Junk at end of arguments."));
c906108c 11717
b270e6f9
TT
11718 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11719 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11720 &catch_exec_breakpoint_ops);
11721 c->exec_pathname = NULL;
11722
b270e6f9 11723 install_breakpoint (0, std::move (c), 1);
c906108c 11724}
c5aa993b 11725
9ac4176b 11726void
28010a5d
PA
11727init_ada_exception_breakpoint (struct breakpoint *b,
11728 struct gdbarch *gdbarch,
11729 struct symtab_and_line sal,
11730 char *addr_string,
c0a91b2b 11731 const struct breakpoint_ops *ops,
28010a5d 11732 int tempflag,
349774ef 11733 int enabled,
28010a5d 11734 int from_tty)
f7f9143b 11735{
f7f9143b
JB
11736 if (from_tty)
11737 {
5af949e3
UW
11738 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11739 if (!loc_gdbarch)
11740 loc_gdbarch = gdbarch;
11741
6c95b8df
PA
11742 describe_other_breakpoints (loc_gdbarch,
11743 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11744 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11745 version for exception catchpoints, because two catchpoints
11746 used for different exception names will use the same address.
11747 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11748 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11749 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11750 the user what type of catchpoint it is. The above is good
11751 enough for now, though. */
11752 }
11753
28010a5d 11754 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11755
349774ef 11756 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11757 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11758 b->location = string_to_event_location (&addr_string,
11759 language_def (language_ada));
f7f9143b 11760 b->language = language_ada;
f7f9143b
JB
11761}
11762
c906108c 11763static void
fba45db2 11764catch_command (char *arg, int from_tty)
c906108c 11765{
44feb3ce 11766 error (_("Catch requires an event name."));
c906108c
SS
11767}
11768\f
11769
11770static void
fba45db2 11771tcatch_command (char *arg, int from_tty)
c906108c 11772{
44feb3ce 11773 error (_("Catch requires an event name."));
c906108c
SS
11774}
11775
8a2c437b
TT
11776/* A qsort comparison function that sorts breakpoints in order. */
11777
11778static int
11779compare_breakpoints (const void *a, const void *b)
11780{
9a3c8263 11781 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11782 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11783 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11784 uintptr_t ub = (uintptr_t) *bb;
11785
11786 if ((*ba)->number < (*bb)->number)
11787 return -1;
11788 else if ((*ba)->number > (*bb)->number)
11789 return 1;
11790
11791 /* Now sort by address, in case we see, e..g, two breakpoints with
11792 the number 0. */
11793 if (ua < ub)
11794 return -1;
94b0e70d 11795 return ua > ub ? 1 : 0;
8a2c437b
TT
11796}
11797
80f8a6eb 11798/* Delete breakpoints by address or line. */
c906108c
SS
11799
11800static void
fba45db2 11801clear_command (char *arg, int from_tty)
c906108c 11802{
8a2c437b 11803 struct breakpoint *b, *prev;
d6e956e5
VP
11804 VEC(breakpoint_p) *found = 0;
11805 int ix;
c906108c
SS
11806 int default_match;
11807 struct symtabs_and_lines sals;
11808 struct symtab_and_line sal;
c906108c 11809 int i;
8a2c437b 11810 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11811
11812 if (arg)
11813 {
39cf75f7
DE
11814 sals = decode_line_with_current_source (arg,
11815 (DECODE_LINE_FUNFIRSTLINE
11816 | DECODE_LINE_LIST_MODE));
cf4ded82 11817 make_cleanup (xfree, sals.sals);
c906108c
SS
11818 default_match = 0;
11819 }
11820 else
11821 {
8d749320 11822 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11823 make_cleanup (xfree, sals.sals);
4a64f543 11824 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11825
11826 /* Set sal's line, symtab, pc, and pspace to the values
11827 corresponding to the last call to print_frame_info. If the
11828 codepoint is not valid, this will set all the fields to 0. */
11829 get_last_displayed_sal (&sal);
c906108c 11830 if (sal.symtab == 0)
8a3fe4f8 11831 error (_("No source file specified."));
c906108c
SS
11832
11833 sals.sals[0] = sal;
11834 sals.nelts = 1;
11835
11836 default_match = 1;
11837 }
11838
4a64f543
MS
11839 /* We don't call resolve_sal_pc here. That's not as bad as it
11840 seems, because all existing breakpoints typically have both
11841 file/line and pc set. So, if clear is given file/line, we can
11842 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11843
11844 We only support clearing given the address explicitly
11845 present in breakpoint table. Say, we've set breakpoint
4a64f543 11846 at file:line. There were several PC values for that file:line,
ed0616c6 11847 due to optimization, all in one block.
4a64f543
MS
11848
11849 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11850 PC corresponding to the same file:line, the breakpoint won't
11851 be cleared. We probably can still clear the breakpoint, but
11852 since the other PC value is never presented to user, user
11853 can only find it by guessing, and it does not seem important
11854 to support that. */
11855
4a64f543
MS
11856 /* For each line spec given, delete bps which correspond to it. Do
11857 it in two passes, solely to preserve the current behavior that
11858 from_tty is forced true if we delete more than one
11859 breakpoint. */
c906108c 11860
80f8a6eb 11861 found = NULL;
8a2c437b 11862 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11863 for (i = 0; i < sals.nelts; i++)
11864 {
05cba821
JK
11865 const char *sal_fullname;
11866
c906108c 11867 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11868 If line given (pc == 0), clear all bpts on specified line.
11869 If defaulting, clear all bpts on default line
c906108c 11870 or at default pc.
c5aa993b
JM
11871
11872 defaulting sal.pc != 0 tests to do
11873
11874 0 1 pc
11875 1 1 pc _and_ line
11876 0 0 line
11877 1 0 <can't happen> */
c906108c
SS
11878
11879 sal = sals.sals[i];
05cba821
JK
11880 sal_fullname = (sal.symtab == NULL
11881 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11882
4a64f543 11883 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11884 ALL_BREAKPOINTS (b)
c5aa993b 11885 {
0d381245 11886 int match = 0;
4a64f543 11887 /* Are we going to delete b? */
cc60f2e3 11888 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11889 {
11890 struct bp_location *loc = b->loc;
11891 for (; loc; loc = loc->next)
11892 {
f8eba3c6
TT
11893 /* If the user specified file:line, don't allow a PC
11894 match. This matches historical gdb behavior. */
11895 int pc_match = (!sal.explicit_line
11896 && sal.pc
11897 && (loc->pspace == sal.pspace)
11898 && (loc->address == sal.pc)
11899 && (!section_is_overlay (loc->section)
11900 || loc->section == sal.section));
4aac40c8
TT
11901 int line_match = 0;
11902
11903 if ((default_match || sal.explicit_line)
2f202fde 11904 && loc->symtab != NULL
05cba821 11905 && sal_fullname != NULL
4aac40c8 11906 && sal.pspace == loc->pspace
05cba821
JK
11907 && loc->line_number == sal.line
11908 && filename_cmp (symtab_to_fullname (loc->symtab),
11909 sal_fullname) == 0)
11910 line_match = 1;
4aac40c8 11911
0d381245
VP
11912 if (pc_match || line_match)
11913 {
11914 match = 1;
11915 break;
11916 }
11917 }
11918 }
11919
11920 if (match)
d6e956e5 11921 VEC_safe_push(breakpoint_p, found, b);
c906108c 11922 }
80f8a6eb 11923 }
8a2c437b 11924
80f8a6eb 11925 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11926 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11927 {
11928 if (arg)
8a3fe4f8 11929 error (_("No breakpoint at %s."), arg);
80f8a6eb 11930 else
8a3fe4f8 11931 error (_("No breakpoint at this line."));
80f8a6eb 11932 }
c906108c 11933
8a2c437b
TT
11934 /* Remove duplicates from the vec. */
11935 qsort (VEC_address (breakpoint_p, found),
11936 VEC_length (breakpoint_p, found),
11937 sizeof (breakpoint_p),
11938 compare_breakpoints);
11939 prev = VEC_index (breakpoint_p, found, 0);
11940 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11941 {
11942 if (b == prev)
11943 {
11944 VEC_ordered_remove (breakpoint_p, found, ix);
11945 --ix;
11946 }
11947 }
11948
d6e956e5 11949 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11950 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11951 if (from_tty)
a3f17187 11952 {
d6e956e5 11953 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11954 printf_unfiltered (_("Deleted breakpoint "));
11955 else
11956 printf_unfiltered (_("Deleted breakpoints "));
11957 }
d6e956e5
VP
11958
11959 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11960 {
c5aa993b 11961 if (from_tty)
d6e956e5
VP
11962 printf_unfiltered ("%d ", b->number);
11963 delete_breakpoint (b);
c906108c 11964 }
80f8a6eb
MS
11965 if (from_tty)
11966 putchar_unfiltered ('\n');
8a2c437b
TT
11967
11968 do_cleanups (cleanups);
c906108c
SS
11969}
11970\f
11971/* Delete breakpoint in BS if they are `delete' breakpoints and
11972 all breakpoints that are marked for deletion, whether hit or not.
11973 This is called after any breakpoint is hit, or after errors. */
11974
11975void
fba45db2 11976breakpoint_auto_delete (bpstat bs)
c906108c 11977{
35df4500 11978 struct breakpoint *b, *b_tmp;
c906108c
SS
11979
11980 for (; bs; bs = bs->next)
f431efe5
PA
11981 if (bs->breakpoint_at
11982 && bs->breakpoint_at->disposition == disp_del
c906108c 11983 && bs->stop)
f431efe5 11984 delete_breakpoint (bs->breakpoint_at);
c906108c 11985
35df4500 11986 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11987 {
b5de0fa7 11988 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11989 delete_breakpoint (b);
11990 }
c906108c
SS
11991}
11992
4a64f543
MS
11993/* A comparison function for bp_location AP and BP being interfaced to
11994 qsort. Sort elements primarily by their ADDRESS (no matter what
11995 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11996 secondarily by ordering first permanent elements and
4a64f543 11997 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11998 qsort being an unstable algorithm. */
876fa593
JK
11999
12000static int
f5336ca5 12001bp_locations_compare (const void *ap, const void *bp)
876fa593 12002{
9a3c8263
SM
12003 const struct bp_location *a = *(const struct bp_location **) ap;
12004 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12005
12006 if (a->address != b->address)
12007 return (a->address > b->address) - (a->address < b->address);
12008
dea2aa5f
LM
12009 /* Sort locations at the same address by their pspace number, keeping
12010 locations of the same inferior (in a multi-inferior environment)
12011 grouped. */
12012
12013 if (a->pspace->num != b->pspace->num)
12014 return ((a->pspace->num > b->pspace->num)
12015 - (a->pspace->num < b->pspace->num));
12016
876fa593 12017 /* Sort permanent breakpoints first. */
1a853c52
PA
12018 if (a->permanent != b->permanent)
12019 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12020
c56a97f9
JK
12021 /* Make the internal GDB representation stable across GDB runs
12022 where A and B memory inside GDB can differ. Breakpoint locations of
12023 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12024
12025 if (a->owner->number != b->owner->number)
c56a97f9
JK
12026 return ((a->owner->number > b->owner->number)
12027 - (a->owner->number < b->owner->number));
876fa593
JK
12028
12029 return (a > b) - (a < b);
12030}
12031
f5336ca5
PA
12032/* Set bp_locations_placed_address_before_address_max and
12033 bp_locations_shadow_len_after_address_max according to the current
12034 content of the bp_locations array. */
f7545552
TT
12035
12036static void
f5336ca5 12037bp_locations_target_extensions_update (void)
f7545552 12038{
876fa593
JK
12039 struct bp_location *bl, **blp_tmp;
12040
f5336ca5
PA
12041 bp_locations_placed_address_before_address_max = 0;
12042 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
12043
12044 ALL_BP_LOCATIONS (bl, blp_tmp)
12045 {
12046 CORE_ADDR start, end, addr;
12047
12048 if (!bp_location_has_shadow (bl))
12049 continue;
12050
12051 start = bl->target_info.placed_address;
12052 end = start + bl->target_info.shadow_len;
12053
12054 gdb_assert (bl->address >= start);
12055 addr = bl->address - start;
f5336ca5
PA
12056 if (addr > bp_locations_placed_address_before_address_max)
12057 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
12058
12059 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12060
12061 gdb_assert (bl->address < end);
12062 addr = end - bl->address;
f5336ca5
PA
12063 if (addr > bp_locations_shadow_len_after_address_max)
12064 bp_locations_shadow_len_after_address_max = addr;
876fa593 12065 }
f7545552
TT
12066}
12067
1e4d1764
YQ
12068/* Download tracepoint locations if they haven't been. */
12069
12070static void
12071download_tracepoint_locations (void)
12072{
7ed2c994 12073 struct breakpoint *b;
dd2e65cc 12074 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 12075
5ed8105e 12076 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 12077
7ed2c994 12078 ALL_TRACEPOINTS (b)
1e4d1764 12079 {
7ed2c994 12080 struct bp_location *bl;
1e4d1764 12081 struct tracepoint *t;
f2a8bc8a 12082 int bp_location_downloaded = 0;
1e4d1764 12083
7ed2c994 12084 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12085 ? !may_insert_fast_tracepoints
12086 : !may_insert_tracepoints))
12087 continue;
12088
dd2e65cc
YQ
12089 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12090 {
12091 if (target_can_download_tracepoint ())
12092 can_download_tracepoint = TRIBOOL_TRUE;
12093 else
12094 can_download_tracepoint = TRIBOOL_FALSE;
12095 }
12096
12097 if (can_download_tracepoint == TRIBOOL_FALSE)
12098 break;
12099
7ed2c994
YQ
12100 for (bl = b->loc; bl; bl = bl->next)
12101 {
12102 /* In tracepoint, locations are _never_ duplicated, so
12103 should_be_inserted is equivalent to
12104 unduplicated_should_be_inserted. */
12105 if (!should_be_inserted (bl) || bl->inserted)
12106 continue;
1e4d1764 12107
7ed2c994 12108 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12109
7ed2c994 12110 target_download_tracepoint (bl);
1e4d1764 12111
7ed2c994 12112 bl->inserted = 1;
f2a8bc8a 12113 bp_location_downloaded = 1;
7ed2c994
YQ
12114 }
12115 t = (struct tracepoint *) b;
12116 t->number_on_target = b->number;
f2a8bc8a
YQ
12117 if (bp_location_downloaded)
12118 observer_notify_breakpoint_modified (b);
1e4d1764 12119 }
1e4d1764
YQ
12120}
12121
934709f0
PW
12122/* Swap the insertion/duplication state between two locations. */
12123
12124static void
12125swap_insertion (struct bp_location *left, struct bp_location *right)
12126{
12127 const int left_inserted = left->inserted;
12128 const int left_duplicate = left->duplicate;
b775012e 12129 const int left_needs_update = left->needs_update;
934709f0
PW
12130 const struct bp_target_info left_target_info = left->target_info;
12131
1e4d1764
YQ
12132 /* Locations of tracepoints can never be duplicated. */
12133 if (is_tracepoint (left->owner))
12134 gdb_assert (!left->duplicate);
12135 if (is_tracepoint (right->owner))
12136 gdb_assert (!right->duplicate);
12137
934709f0
PW
12138 left->inserted = right->inserted;
12139 left->duplicate = right->duplicate;
b775012e 12140 left->needs_update = right->needs_update;
934709f0
PW
12141 left->target_info = right->target_info;
12142 right->inserted = left_inserted;
12143 right->duplicate = left_duplicate;
b775012e 12144 right->needs_update = left_needs_update;
934709f0
PW
12145 right->target_info = left_target_info;
12146}
12147
b775012e
LM
12148/* Force the re-insertion of the locations at ADDRESS. This is called
12149 once a new/deleted/modified duplicate location is found and we are evaluating
12150 conditions on the target's side. Such conditions need to be updated on
12151 the target. */
12152
12153static void
12154force_breakpoint_reinsertion (struct bp_location *bl)
12155{
12156 struct bp_location **locp = NULL, **loc2p;
12157 struct bp_location *loc;
12158 CORE_ADDR address = 0;
12159 int pspace_num;
12160
12161 address = bl->address;
12162 pspace_num = bl->pspace->num;
12163
12164 /* This is only meaningful if the target is
12165 evaluating conditions and if the user has
12166 opted for condition evaluation on the target's
12167 side. */
12168 if (gdb_evaluates_breakpoint_condition_p ()
12169 || !target_supports_evaluation_of_breakpoint_conditions ())
12170 return;
12171
12172 /* Flag all breakpoint locations with this address and
12173 the same program space as the location
12174 as "its condition has changed". We need to
12175 update the conditions on the target's side. */
12176 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12177 {
12178 loc = *loc2p;
12179
12180 if (!is_breakpoint (loc->owner)
12181 || pspace_num != loc->pspace->num)
12182 continue;
12183
12184 /* Flag the location appropriately. We use a different state to
12185 let everyone know that we already updated the set of locations
12186 with addr bl->address and program space bl->pspace. This is so
12187 we don't have to keep calling these functions just to mark locations
12188 that have already been marked. */
12189 loc->condition_changed = condition_updated;
12190
12191 /* Free the agent expression bytecode as well. We will compute
12192 it later on. */
833177a4 12193 loc->cond_bytecode.reset ();
b775012e
LM
12194 }
12195}
44702360
PA
12196/* Called whether new breakpoints are created, or existing breakpoints
12197 deleted, to update the global location list and recompute which
12198 locations are duplicate of which.
b775012e 12199
04086b45
PA
12200 The INSERT_MODE flag determines whether locations may not, may, or
12201 shall be inserted now. See 'enum ugll_insert_mode' for more
12202 info. */
b60e7edf 12203
0d381245 12204static void
44702360 12205update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12206{
74960c60 12207 struct breakpoint *b;
876fa593 12208 struct bp_location **locp, *loc;
f7545552 12209 struct cleanup *cleanups;
b775012e
LM
12210 /* Last breakpoint location address that was marked for update. */
12211 CORE_ADDR last_addr = 0;
12212 /* Last breakpoint location program space that was marked for update. */
12213 int last_pspace_num = -1;
f7545552 12214
2d134ed3
PA
12215 /* Used in the duplicates detection below. When iterating over all
12216 bp_locations, points to the first bp_location of a given address.
12217 Breakpoints and watchpoints of different types are never
12218 duplicates of each other. Keep one pointer for each type of
12219 breakpoint/watchpoint, so we only need to loop over all locations
12220 once. */
12221 struct bp_location *bp_loc_first; /* breakpoint */
12222 struct bp_location *wp_loc_first; /* hardware watchpoint */
12223 struct bp_location *awp_loc_first; /* access watchpoint */
12224 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12225
f5336ca5
PA
12226 /* Saved former bp_locations array which we compare against the newly
12227 built bp_locations from the current state of ALL_BREAKPOINTS. */
12228 struct bp_location **old_locations, **old_locp;
12229 unsigned old_locations_count;
876fa593 12230
f5336ca5
PA
12231 old_locations = bp_locations;
12232 old_locations_count = bp_locations_count;
12233 bp_locations = NULL;
12234 bp_locations_count = 0;
12235 cleanups = make_cleanup (xfree, old_locations);
0d381245 12236
74960c60 12237 ALL_BREAKPOINTS (b)
876fa593 12238 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 12239 bp_locations_count++;
876fa593 12240
f5336ca5
PA
12241 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12242 locp = bp_locations;
876fa593
JK
12243 ALL_BREAKPOINTS (b)
12244 for (loc = b->loc; loc; loc = loc->next)
12245 *locp++ = loc;
f5336ca5
PA
12246 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12247 bp_locations_compare);
876fa593 12248
f5336ca5 12249 bp_locations_target_extensions_update ();
74960c60 12250
4a64f543
MS
12251 /* Identify bp_location instances that are no longer present in the
12252 new list, and therefore should be freed. Note that it's not
12253 necessary that those locations should be removed from inferior --
12254 if there's another location at the same address (previously
12255 marked as duplicate), we don't need to remove/insert the
12256 location.
876fa593 12257
4a64f543
MS
12258 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12259 and former bp_location array state respectively. */
876fa593 12260
f5336ca5
PA
12261 locp = bp_locations;
12262 for (old_locp = old_locations;
12263 old_locp < old_locations + old_locations_count;
876fa593 12264 old_locp++)
74960c60 12265 {
876fa593 12266 struct bp_location *old_loc = *old_locp;
c7d46a38 12267 struct bp_location **loc2p;
876fa593 12268
e5dd4106 12269 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12270 not, we have to free it. */
c7d46a38 12271 int found_object = 0;
20874c92
VP
12272 /* Tells if the location should remain inserted in the target. */
12273 int keep_in_target = 0;
12274 int removed = 0;
876fa593 12275
4a64f543
MS
12276 /* Skip LOCP entries which will definitely never be needed.
12277 Stop either at or being the one matching OLD_LOC. */
f5336ca5 12278 while (locp < bp_locations + bp_locations_count
c7d46a38 12279 && (*locp)->address < old_loc->address)
876fa593 12280 locp++;
c7d46a38
PA
12281
12282 for (loc2p = locp;
f5336ca5 12283 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
12284 && (*loc2p)->address == old_loc->address);
12285 loc2p++)
12286 {
b775012e
LM
12287 /* Check if this is a new/duplicated location or a duplicated
12288 location that had its condition modified. If so, we want to send
12289 its condition to the target if evaluation of conditions is taking
12290 place there. */
12291 if ((*loc2p)->condition_changed == condition_modified
12292 && (last_addr != old_loc->address
12293 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12294 {
b775012e
LM
12295 force_breakpoint_reinsertion (*loc2p);
12296 last_pspace_num = old_loc->pspace->num;
c7d46a38 12297 }
b775012e
LM
12298
12299 if (*loc2p == old_loc)
12300 found_object = 1;
c7d46a38 12301 }
74960c60 12302
b775012e
LM
12303 /* We have already handled this address, update it so that we don't
12304 have to go through updates again. */
12305 last_addr = old_loc->address;
12306
12307 /* Target-side condition evaluation: Handle deleted locations. */
12308 if (!found_object)
12309 force_breakpoint_reinsertion (old_loc);
12310
4a64f543
MS
12311 /* If this location is no longer present, and inserted, look if
12312 there's maybe a new location at the same address. If so,
12313 mark that one inserted, and don't remove this one. This is
12314 needed so that we don't have a time window where a breakpoint
12315 at certain location is not inserted. */
74960c60 12316
876fa593 12317 if (old_loc->inserted)
0d381245 12318 {
4a64f543
MS
12319 /* If the location is inserted now, we might have to remove
12320 it. */
74960c60 12321
876fa593 12322 if (found_object && should_be_inserted (old_loc))
74960c60 12323 {
4a64f543
MS
12324 /* The location is still present in the location list,
12325 and still should be inserted. Don't do anything. */
20874c92 12326 keep_in_target = 1;
74960c60
VP
12327 }
12328 else
12329 {
b775012e
LM
12330 /* This location still exists, but it won't be kept in the
12331 target since it may have been disabled. We proceed to
12332 remove its target-side condition. */
12333
4a64f543
MS
12334 /* The location is either no longer present, or got
12335 disabled. See if there's another location at the
12336 same address, in which case we don't need to remove
12337 this one from the target. */
876fa593 12338
2bdf28a0 12339 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12340 if (breakpoint_address_is_meaningful (old_loc->owner))
12341 {
876fa593 12342 for (loc2p = locp;
f5336ca5 12343 (loc2p < bp_locations + bp_locations_count
c7d46a38 12344 && (*loc2p)->address == old_loc->address);
876fa593
JK
12345 loc2p++)
12346 {
12347 struct bp_location *loc2 = *loc2p;
12348
2d134ed3 12349 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12350 {
85d721b8
PA
12351 /* Read watchpoint locations are switched to
12352 access watchpoints, if the former are not
12353 supported, but the latter are. */
12354 if (is_hardware_watchpoint (old_loc->owner))
12355 {
12356 gdb_assert (is_hardware_watchpoint (loc2->owner));
12357 loc2->watchpoint_type = old_loc->watchpoint_type;
12358 }
12359
934709f0
PW
12360 /* loc2 is a duplicated location. We need to check
12361 if it should be inserted in case it will be
12362 unduplicated. */
12363 if (loc2 != old_loc
12364 && unduplicated_should_be_inserted (loc2))
c7d46a38 12365 {
934709f0 12366 swap_insertion (old_loc, loc2);
c7d46a38
PA
12367 keep_in_target = 1;
12368 break;
12369 }
876fa593
JK
12370 }
12371 }
12372 }
74960c60
VP
12373 }
12374
20874c92
VP
12375 if (!keep_in_target)
12376 {
834c0d03 12377 if (remove_breakpoint (old_loc))
20874c92 12378 {
4a64f543
MS
12379 /* This is just about all we can do. We could keep
12380 this location on the global list, and try to
12381 remove it next time, but there's no particular
12382 reason why we will succeed next time.
20874c92 12383
4a64f543
MS
12384 Note that at this point, old_loc->owner is still
12385 valid, as delete_breakpoint frees the breakpoint
12386 only after calling us. */
3e43a32a
MS
12387 printf_filtered (_("warning: Error removing "
12388 "breakpoint %d\n"),
876fa593 12389 old_loc->owner->number);
20874c92
VP
12390 }
12391 removed = 1;
12392 }
0d381245 12393 }
74960c60
VP
12394
12395 if (!found_object)
1c5cfe86 12396 {
fbea99ea 12397 if (removed && target_is_non_stop_p ()
1cf4d951 12398 && need_moribund_for_location_type (old_loc))
20874c92 12399 {
db82e815
PA
12400 /* This location was removed from the target. In
12401 non-stop mode, a race condition is possible where
12402 we've removed a breakpoint, but stop events for that
12403 breakpoint are already queued and will arrive later.
12404 We apply an heuristic to be able to distinguish such
12405 SIGTRAPs from other random SIGTRAPs: we keep this
12406 breakpoint location for a bit, and will retire it
12407 after we see some number of events. The theory here
12408 is that reporting of events should, "on the average",
12409 be fair, so after a while we'll see events from all
12410 threads that have anything of interest, and no longer
12411 need to keep this breakpoint location around. We
12412 don't hold locations forever so to reduce chances of
12413 mistaking a non-breakpoint SIGTRAP for a breakpoint
12414 SIGTRAP.
12415
12416 The heuristic failing can be disastrous on
12417 decr_pc_after_break targets.
12418
12419 On decr_pc_after_break targets, like e.g., x86-linux,
12420 if we fail to recognize a late breakpoint SIGTRAP,
12421 because events_till_retirement has reached 0 too
12422 soon, we'll fail to do the PC adjustment, and report
12423 a random SIGTRAP to the user. When the user resumes
12424 the inferior, it will most likely immediately crash
2dec564e 12425 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12426 corrupted, because of being resumed e.g., in the
12427 middle of a multi-byte instruction, or skipped a
12428 one-byte instruction. This was actually seen happen
12429 on native x86-linux, and should be less rare on
12430 targets that do not support new thread events, like
12431 remote, due to the heuristic depending on
12432 thread_count.
12433
12434 Mistaking a random SIGTRAP for a breakpoint trap
12435 causes similar symptoms (PC adjustment applied when
12436 it shouldn't), but then again, playing with SIGTRAPs
12437 behind the debugger's back is asking for trouble.
12438
12439 Since hardware watchpoint traps are always
12440 distinguishable from other traps, so we don't need to
12441 apply keep hardware watchpoint moribund locations
12442 around. We simply always ignore hardware watchpoint
12443 traps we can no longer explain. */
12444
876fa593
JK
12445 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12446 old_loc->owner = NULL;
20874c92 12447
876fa593 12448 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12449 }
12450 else
f431efe5
PA
12451 {
12452 old_loc->owner = NULL;
12453 decref_bp_location (&old_loc);
12454 }
20874c92 12455 }
74960c60 12456 }
1c5cfe86 12457
348d480f
PA
12458 /* Rescan breakpoints at the same address and section, marking the
12459 first one as "first" and any others as "duplicates". This is so
12460 that the bpt instruction is only inserted once. If we have a
12461 permanent breakpoint at the same place as BPT, make that one the
12462 official one, and the rest as duplicates. Permanent breakpoints
12463 are sorted first for the same address.
12464
12465 Do the same for hardware watchpoints, but also considering the
12466 watchpoint's type (regular/access/read) and length. */
12467
12468 bp_loc_first = NULL;
12469 wp_loc_first = NULL;
12470 awp_loc_first = NULL;
12471 rwp_loc_first = NULL;
12472 ALL_BP_LOCATIONS (loc, locp)
12473 {
12474 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12475 non-NULL. */
348d480f 12476 struct bp_location **loc_first_p;
d3fbdd86 12477 b = loc->owner;
348d480f 12478
6f380991 12479 if (!unduplicated_should_be_inserted (loc)
348d480f 12480 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12481 /* Don't detect duplicate for tracepoint locations because they are
12482 never duplicated. See the comments in field `duplicate' of
12483 `struct bp_location'. */
348d480f 12484 || is_tracepoint (b))
b775012e
LM
12485 {
12486 /* Clear the condition modification flag. */
12487 loc->condition_changed = condition_unchanged;
12488 continue;
12489 }
348d480f 12490
348d480f
PA
12491 if (b->type == bp_hardware_watchpoint)
12492 loc_first_p = &wp_loc_first;
12493 else if (b->type == bp_read_watchpoint)
12494 loc_first_p = &rwp_loc_first;
12495 else if (b->type == bp_access_watchpoint)
12496 loc_first_p = &awp_loc_first;
12497 else
12498 loc_first_p = &bp_loc_first;
12499
12500 if (*loc_first_p == NULL
12501 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12502 || !breakpoint_locations_match (loc, *loc_first_p))
12503 {
12504 *loc_first_p = loc;
12505 loc->duplicate = 0;
b775012e
LM
12506
12507 if (is_breakpoint (loc->owner) && loc->condition_changed)
12508 {
12509 loc->needs_update = 1;
12510 /* Clear the condition modification flag. */
12511 loc->condition_changed = condition_unchanged;
12512 }
348d480f
PA
12513 continue;
12514 }
12515
934709f0
PW
12516
12517 /* This and the above ensure the invariant that the first location
12518 is not duplicated, and is the inserted one.
12519 All following are marked as duplicated, and are not inserted. */
12520 if (loc->inserted)
12521 swap_insertion (loc, *loc_first_p);
348d480f
PA
12522 loc->duplicate = 1;
12523
b775012e
LM
12524 /* Clear the condition modification flag. */
12525 loc->condition_changed = condition_unchanged;
348d480f
PA
12526 }
12527
a25a5a45 12528 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12529 {
04086b45 12530 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12531 insert_breakpoint_locations ();
12532 else
12533 {
44702360
PA
12534 /* Even though the caller told us to not insert new
12535 locations, we may still need to update conditions on the
12536 target's side of breakpoints that were already inserted
12537 if the target is evaluating breakpoint conditions. We
b775012e
LM
12538 only update conditions for locations that are marked
12539 "needs_update". */
12540 update_inserted_breakpoint_locations ();
12541 }
12542 }
348d480f 12543
04086b45 12544 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12545 download_tracepoint_locations ();
12546
348d480f
PA
12547 do_cleanups (cleanups);
12548}
12549
12550void
12551breakpoint_retire_moribund (void)
12552{
12553 struct bp_location *loc;
12554 int ix;
12555
12556 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12557 if (--(loc->events_till_retirement) == 0)
12558 {
12559 decref_bp_location (&loc);
12560 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12561 --ix;
12562 }
12563}
12564
12565static void
44702360 12566update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12567{
348d480f 12568
492d29ea
PA
12569 TRY
12570 {
12571 update_global_location_list (insert_mode);
12572 }
12573 CATCH (e, RETURN_MASK_ERROR)
12574 {
12575 }
12576 END_CATCH
348d480f
PA
12577}
12578
12579/* Clear BKP from a BPS. */
12580
12581static void
12582bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12583{
12584 bpstat bs;
12585
12586 for (bs = bps; bs; bs = bs->next)
12587 if (bs->breakpoint_at == bpt)
12588 {
12589 bs->breakpoint_at = NULL;
12590 bs->old_val = NULL;
12591 /* bs->commands will be freed later. */
12592 }
12593}
12594
12595/* Callback for iterate_over_threads. */
12596static int
12597bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12598{
9a3c8263 12599 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12600
12601 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12602 return 0;
12603}
12604
12605/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12606 callbacks. */
12607
12608static void
12609say_where (struct breakpoint *b)
12610{
12611 struct value_print_options opts;
12612
12613 get_user_print_options (&opts);
12614
12615 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12616 single string. */
12617 if (b->loc == NULL)
12618 {
f00aae0f
KS
12619 /* For pending locations, the output differs slightly based
12620 on b->extra_string. If this is non-NULL, it contains either
12621 a condition or dprintf arguments. */
12622 if (b->extra_string == NULL)
12623 {
12624 printf_filtered (_(" (%s) pending."),
d28cd78a 12625 event_location_to_string (b->location.get ()));
f00aae0f
KS
12626 }
12627 else if (b->type == bp_dprintf)
12628 {
12629 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12630 event_location_to_string (b->location.get ()),
f00aae0f
KS
12631 b->extra_string);
12632 }
12633 else
12634 {
12635 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12636 event_location_to_string (b->location.get ()),
f00aae0f
KS
12637 b->extra_string);
12638 }
348d480f
PA
12639 }
12640 else
12641 {
2f202fde 12642 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12643 {
12644 printf_filtered (" at ");
12645 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12646 gdb_stdout);
12647 }
2f202fde 12648 if (b->loc->symtab != NULL)
f8eba3c6
TT
12649 {
12650 /* If there is a single location, we can print the location
12651 more nicely. */
12652 if (b->loc->next == NULL)
12653 printf_filtered (": file %s, line %d.",
05cba821
JK
12654 symtab_to_filename_for_display (b->loc->symtab),
12655 b->loc->line_number);
f8eba3c6
TT
12656 else
12657 /* This is not ideal, but each location may have a
12658 different file name, and this at least reflects the
12659 real situation somewhat. */
f00aae0f 12660 printf_filtered (": %s.",
d28cd78a 12661 event_location_to_string (b->location.get ()));
f8eba3c6 12662 }
348d480f
PA
12663
12664 if (b->loc->next)
12665 {
12666 struct bp_location *loc = b->loc;
12667 int n = 0;
12668 for (; loc; loc = loc->next)
12669 ++n;
12670 printf_filtered (" (%d locations)", n);
12671 }
12672 }
12673}
12674
348d480f
PA
12675/* Default bp_location_ops methods. */
12676
12677static void
12678bp_location_dtor (struct bp_location *self)
12679{
348d480f
PA
12680 xfree (self->function_name);
12681}
12682
12683static const struct bp_location_ops bp_location_ops =
12684{
12685 bp_location_dtor
12686};
12687
c1fc2657 12688/* Destructor for the breakpoint base class. */
348d480f 12689
c1fc2657 12690breakpoint::~breakpoint ()
348d480f 12691{
c1fc2657
SM
12692 decref_counted_command_line (&this->commands);
12693 xfree (this->cond_string);
12694 xfree (this->extra_string);
12695 xfree (this->filter);
348d480f
PA
12696}
12697
2060206e
PA
12698static struct bp_location *
12699base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12700{
5625a286 12701 return new bp_location (&bp_location_ops, self);
348d480f
PA
12702}
12703
2060206e
PA
12704static void
12705base_breakpoint_re_set (struct breakpoint *b)
12706{
12707 /* Nothing to re-set. */
12708}
12709
12710#define internal_error_pure_virtual_called() \
12711 gdb_assert_not_reached ("pure virtual function called")
12712
12713static int
12714base_breakpoint_insert_location (struct bp_location *bl)
12715{
12716 internal_error_pure_virtual_called ();
12717}
12718
12719static int
73971819
PA
12720base_breakpoint_remove_location (struct bp_location *bl,
12721 enum remove_bp_reason reason)
2060206e
PA
12722{
12723 internal_error_pure_virtual_called ();
12724}
12725
12726static int
12727base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12728 struct address_space *aspace,
09ac7c10
TT
12729 CORE_ADDR bp_addr,
12730 const struct target_waitstatus *ws)
2060206e
PA
12731{
12732 internal_error_pure_virtual_called ();
12733}
12734
12735static void
12736base_breakpoint_check_status (bpstat bs)
12737{
12738 /* Always stop. */
12739}
12740
12741/* A "works_in_software_mode" breakpoint_ops method that just internal
12742 errors. */
12743
12744static int
12745base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12746{
12747 internal_error_pure_virtual_called ();
12748}
12749
12750/* A "resources_needed" breakpoint_ops method that just internal
12751 errors. */
12752
12753static int
12754base_breakpoint_resources_needed (const struct bp_location *bl)
12755{
12756 internal_error_pure_virtual_called ();
12757}
12758
12759static enum print_stop_action
12760base_breakpoint_print_it (bpstat bs)
12761{
12762 internal_error_pure_virtual_called ();
12763}
12764
12765static void
12766base_breakpoint_print_one_detail (const struct breakpoint *self,
12767 struct ui_out *uiout)
12768{
12769 /* nothing */
12770}
12771
12772static void
12773base_breakpoint_print_mention (struct breakpoint *b)
12774{
12775 internal_error_pure_virtual_called ();
12776}
12777
12778static void
12779base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12780{
12781 internal_error_pure_virtual_called ();
12782}
12783
983af33b 12784static void
f00aae0f
KS
12785base_breakpoint_create_sals_from_location
12786 (const struct event_location *location,
12787 struct linespec_result *canonical,
12788 enum bptype type_wanted)
983af33b
SDJ
12789{
12790 internal_error_pure_virtual_called ();
12791}
12792
12793static void
12794base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12795 struct linespec_result *c,
e1e01040
PA
12796 gdb::unique_xmalloc_ptr<char> cond_string,
12797 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12798 enum bptype type_wanted,
12799 enum bpdisp disposition,
12800 int thread,
12801 int task, int ignore_count,
12802 const struct breakpoint_ops *o,
12803 int from_tty, int enabled,
44f238bb 12804 int internal, unsigned flags)
983af33b
SDJ
12805{
12806 internal_error_pure_virtual_called ();
12807}
12808
12809static void
f00aae0f
KS
12810base_breakpoint_decode_location (struct breakpoint *b,
12811 const struct event_location *location,
c2f4122d 12812 struct program_space *search_pspace,
983af33b
SDJ
12813 struct symtabs_and_lines *sals)
12814{
12815 internal_error_pure_virtual_called ();
12816}
12817
ab04a2af
TT
12818/* The default 'explains_signal' method. */
12819
47591c29 12820static int
427cd150 12821base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12822{
47591c29 12823 return 1;
ab04a2af
TT
12824}
12825
9d6e6e84
HZ
12826/* The default "after_condition_true" method. */
12827
12828static void
12829base_breakpoint_after_condition_true (struct bpstats *bs)
12830{
12831 /* Nothing to do. */
12832}
12833
ab04a2af 12834struct breakpoint_ops base_breakpoint_ops =
2060206e 12835{
2060206e
PA
12836 base_breakpoint_allocate_location,
12837 base_breakpoint_re_set,
12838 base_breakpoint_insert_location,
12839 base_breakpoint_remove_location,
12840 base_breakpoint_breakpoint_hit,
12841 base_breakpoint_check_status,
12842 base_breakpoint_resources_needed,
12843 base_breakpoint_works_in_software_mode,
12844 base_breakpoint_print_it,
12845 NULL,
12846 base_breakpoint_print_one_detail,
12847 base_breakpoint_print_mention,
983af33b 12848 base_breakpoint_print_recreate,
5f700d83 12849 base_breakpoint_create_sals_from_location,
983af33b 12850 base_breakpoint_create_breakpoints_sal,
5f700d83 12851 base_breakpoint_decode_location,
9d6e6e84
HZ
12852 base_breakpoint_explains_signal,
12853 base_breakpoint_after_condition_true,
2060206e
PA
12854};
12855
12856/* Default breakpoint_ops methods. */
12857
12858static void
348d480f
PA
12859bkpt_re_set (struct breakpoint *b)
12860{
06edf0c0 12861 /* FIXME: is this still reachable? */
9ef9e6a6 12862 if (breakpoint_event_location_empty_p (b))
06edf0c0 12863 {
f00aae0f 12864 /* Anything without a location can't be re-set. */
348d480f 12865 delete_breakpoint (b);
06edf0c0 12866 return;
348d480f 12867 }
06edf0c0
PA
12868
12869 breakpoint_re_set_default (b);
348d480f
PA
12870}
12871
2060206e 12872static int
348d480f
PA
12873bkpt_insert_location (struct bp_location *bl)
12874{
cd6c3b4f
YQ
12875 CORE_ADDR addr = bl->target_info.reqstd_address;
12876
579c6ad9 12877 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12878 bl->target_info.placed_address = addr;
12879
348d480f 12880 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12881 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12882 else
7c16b83e 12883 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12884}
12885
2060206e 12886static int
73971819 12887bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12888{
12889 if (bl->loc_type == bp_loc_hardware_breakpoint)
12890 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12891 else
73971819 12892 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12893}
12894
2060206e 12895static int
348d480f 12896bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12897 struct address_space *aspace, CORE_ADDR bp_addr,
12898 const struct target_waitstatus *ws)
348d480f 12899{
09ac7c10 12900 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12901 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12902 return 0;
12903
348d480f
PA
12904 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12905 aspace, bp_addr))
12906 return 0;
12907
12908 if (overlay_debugging /* unmapped overlay section */
12909 && section_is_overlay (bl->section)
12910 && !section_is_mapped (bl->section))
12911 return 0;
12912
12913 return 1;
12914}
12915
cd1608cc
PA
12916static int
12917dprintf_breakpoint_hit (const struct bp_location *bl,
12918 struct address_space *aspace, CORE_ADDR bp_addr,
12919 const struct target_waitstatus *ws)
12920{
12921 if (dprintf_style == dprintf_style_agent
12922 && target_can_run_breakpoint_commands ())
12923 {
12924 /* An agent-style dprintf never causes a stop. If we see a trap
12925 for this address it must be for a breakpoint that happens to
12926 be set at the same address. */
12927 return 0;
12928 }
12929
12930 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12931}
12932
2060206e 12933static int
348d480f
PA
12934bkpt_resources_needed (const struct bp_location *bl)
12935{
12936 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12937
12938 return 1;
12939}
12940
2060206e 12941static enum print_stop_action
348d480f
PA
12942bkpt_print_it (bpstat bs)
12943{
348d480f
PA
12944 struct breakpoint *b;
12945 const struct bp_location *bl;
001c8c33 12946 int bp_temp;
79a45e25 12947 struct ui_out *uiout = current_uiout;
348d480f
PA
12948
12949 gdb_assert (bs->bp_location_at != NULL);
12950
12951 bl = bs->bp_location_at;
12952 b = bs->breakpoint_at;
12953
001c8c33
PA
12954 bp_temp = b->disposition == disp_del;
12955 if (bl->address != bl->requested_address)
12956 breakpoint_adjustment_warning (bl->requested_address,
12957 bl->address,
12958 b->number, 1);
12959 annotate_breakpoint (b->number);
f303dbd6
PA
12960 maybe_print_thread_hit_breakpoint (uiout);
12961
001c8c33 12962 if (bp_temp)
112e8700 12963 uiout->text ("Temporary breakpoint ");
001c8c33 12964 else
112e8700
SM
12965 uiout->text ("Breakpoint ");
12966 if (uiout->is_mi_like_p ())
348d480f 12967 {
112e8700 12968 uiout->field_string ("reason",
001c8c33 12969 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12970 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12971 }
112e8700
SM
12972 uiout->field_int ("bkptno", b->number);
12973 uiout->text (", ");
06edf0c0 12974
001c8c33 12975 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12976}
12977
2060206e 12978static void
06edf0c0
PA
12979bkpt_print_mention (struct breakpoint *b)
12980{
112e8700 12981 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12982 return;
12983
12984 switch (b->type)
12985 {
12986 case bp_breakpoint:
12987 case bp_gnu_ifunc_resolver:
12988 if (b->disposition == disp_del)
12989 printf_filtered (_("Temporary breakpoint"));
12990 else
12991 printf_filtered (_("Breakpoint"));
12992 printf_filtered (_(" %d"), b->number);
12993 if (b->type == bp_gnu_ifunc_resolver)
12994 printf_filtered (_(" at gnu-indirect-function resolver"));
12995 break;
12996 case bp_hardware_breakpoint:
12997 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12998 break;
e7e0cddf
SS
12999 case bp_dprintf:
13000 printf_filtered (_("Dprintf %d"), b->number);
13001 break;
06edf0c0
PA
13002 }
13003
13004 say_where (b);
13005}
13006
2060206e 13007static void
06edf0c0
PA
13008bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13009{
13010 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13011 fprintf_unfiltered (fp, "tbreak");
13012 else if (tp->type == bp_breakpoint)
13013 fprintf_unfiltered (fp, "break");
13014 else if (tp->type == bp_hardware_breakpoint
13015 && tp->disposition == disp_del)
13016 fprintf_unfiltered (fp, "thbreak");
13017 else if (tp->type == bp_hardware_breakpoint)
13018 fprintf_unfiltered (fp, "hbreak");
13019 else
13020 internal_error (__FILE__, __LINE__,
13021 _("unhandled breakpoint type %d"), (int) tp->type);
13022
f00aae0f 13023 fprintf_unfiltered (fp, " %s",
d28cd78a 13024 event_location_to_string (tp->location.get ()));
f00aae0f
KS
13025
13026 /* Print out extra_string if this breakpoint is pending. It might
13027 contain, for example, conditions that were set by the user. */
13028 if (tp->loc == NULL && tp->extra_string != NULL)
13029 fprintf_unfiltered (fp, " %s", tp->extra_string);
13030
dd11a36c 13031 print_recreate_thread (tp, fp);
06edf0c0
PA
13032}
13033
983af33b 13034static void
f00aae0f
KS
13035bkpt_create_sals_from_location (const struct event_location *location,
13036 struct linespec_result *canonical,
13037 enum bptype type_wanted)
983af33b 13038{
f00aae0f 13039 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13040}
13041
13042static void
13043bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13044 struct linespec_result *canonical,
e1e01040
PA
13045 gdb::unique_xmalloc_ptr<char> cond_string,
13046 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13047 enum bptype type_wanted,
13048 enum bpdisp disposition,
13049 int thread,
13050 int task, int ignore_count,
13051 const struct breakpoint_ops *ops,
13052 int from_tty, int enabled,
44f238bb 13053 int internal, unsigned flags)
983af33b 13054{
023fa29b 13055 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13056 std::move (cond_string),
13057 std::move (extra_string),
e7e0cddf 13058 type_wanted,
983af33b
SDJ
13059 disposition, thread, task,
13060 ignore_count, ops, from_tty,
44f238bb 13061 enabled, internal, flags);
983af33b
SDJ
13062}
13063
13064static void
f00aae0f
KS
13065bkpt_decode_location (struct breakpoint *b,
13066 const struct event_location *location,
c2f4122d 13067 struct program_space *search_pspace,
983af33b
SDJ
13068 struct symtabs_and_lines *sals)
13069{
c2f4122d 13070 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13071}
13072
06edf0c0
PA
13073/* Virtual table for internal breakpoints. */
13074
13075static void
13076internal_bkpt_re_set (struct breakpoint *b)
13077{
13078 switch (b->type)
13079 {
13080 /* Delete overlay event and longjmp master breakpoints; they
13081 will be reset later by breakpoint_re_set. */
13082 case bp_overlay_event:
13083 case bp_longjmp_master:
13084 case bp_std_terminate_master:
13085 case bp_exception_master:
13086 delete_breakpoint (b);
13087 break;
13088
13089 /* This breakpoint is special, it's set up when the inferior
13090 starts and we really don't want to touch it. */
13091 case bp_shlib_event:
13092
13093 /* Like bp_shlib_event, this breakpoint type is special. Once
13094 it is set up, we do not want to touch it. */
13095 case bp_thread_event:
13096 break;
13097 }
13098}
13099
13100static void
13101internal_bkpt_check_status (bpstat bs)
13102{
a9b3a50f
PA
13103 if (bs->breakpoint_at->type == bp_shlib_event)
13104 {
13105 /* If requested, stop when the dynamic linker notifies GDB of
13106 events. This allows the user to get control and place
13107 breakpoints in initializer routines for dynamically loaded
13108 objects (among other things). */
13109 bs->stop = stop_on_solib_events;
13110 bs->print = stop_on_solib_events;
13111 }
13112 else
13113 bs->stop = 0;
06edf0c0
PA
13114}
13115
13116static enum print_stop_action
13117internal_bkpt_print_it (bpstat bs)
13118{
06edf0c0 13119 struct breakpoint *b;
06edf0c0 13120
06edf0c0
PA
13121 b = bs->breakpoint_at;
13122
06edf0c0
PA
13123 switch (b->type)
13124 {
348d480f
PA
13125 case bp_shlib_event:
13126 /* Did we stop because the user set the stop_on_solib_events
13127 variable? (If so, we report this as a generic, "Stopped due
13128 to shlib event" message.) */
edcc5120 13129 print_solib_event (0);
348d480f
PA
13130 break;
13131
13132 case bp_thread_event:
13133 /* Not sure how we will get here.
13134 GDB should not stop for these breakpoints. */
13135 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13136 break;
13137
13138 case bp_overlay_event:
13139 /* By analogy with the thread event, GDB should not stop for these. */
13140 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13141 break;
13142
13143 case bp_longjmp_master:
13144 /* These should never be enabled. */
13145 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13146 break;
13147
13148 case bp_std_terminate_master:
13149 /* These should never be enabled. */
13150 printf_filtered (_("std::terminate Master Breakpoint: "
13151 "gdb should not stop!\n"));
348d480f
PA
13152 break;
13153
13154 case bp_exception_master:
13155 /* These should never be enabled. */
13156 printf_filtered (_("Exception Master Breakpoint: "
13157 "gdb should not stop!\n"));
06edf0c0
PA
13158 break;
13159 }
13160
001c8c33 13161 return PRINT_NOTHING;
06edf0c0
PA
13162}
13163
13164static void
13165internal_bkpt_print_mention (struct breakpoint *b)
13166{
13167 /* Nothing to mention. These breakpoints are internal. */
13168}
13169
06edf0c0
PA
13170/* Virtual table for momentary breakpoints */
13171
13172static void
13173momentary_bkpt_re_set (struct breakpoint *b)
13174{
13175 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13176 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13177 Otherwise these should have been blown away via the cleanup chain
13178 or by breakpoint_init_inferior when we rerun the executable. */
13179}
13180
13181static void
13182momentary_bkpt_check_status (bpstat bs)
13183{
13184 /* Nothing. The point of these breakpoints is causing a stop. */
13185}
13186
13187static enum print_stop_action
13188momentary_bkpt_print_it (bpstat bs)
13189{
001c8c33 13190 return PRINT_UNKNOWN;
348d480f
PA
13191}
13192
06edf0c0
PA
13193static void
13194momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13195{
06edf0c0 13196 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13197}
13198
e2e4d78b
JK
13199/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13200
13201 It gets cleared already on the removal of the first one of such placed
13202 breakpoints. This is OK as they get all removed altogether. */
13203
c1fc2657 13204longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 13205{
c1fc2657 13206 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 13207
c1fc2657 13208 if (tp != NULL)
e2e4d78b 13209 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
13210}
13211
55aa24fb
SDJ
13212/* Specific methods for probe breakpoints. */
13213
13214static int
13215bkpt_probe_insert_location (struct bp_location *bl)
13216{
13217 int v = bkpt_insert_location (bl);
13218
13219 if (v == 0)
13220 {
13221 /* The insertion was successful, now let's set the probe's semaphore
13222 if needed. */
0ea5cda8
SDJ
13223 if (bl->probe.probe->pops->set_semaphore != NULL)
13224 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13225 bl->probe.objfile,
13226 bl->gdbarch);
55aa24fb
SDJ
13227 }
13228
13229 return v;
13230}
13231
13232static int
73971819
PA
13233bkpt_probe_remove_location (struct bp_location *bl,
13234 enum remove_bp_reason reason)
55aa24fb
SDJ
13235{
13236 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13237 if (bl->probe.probe->pops->clear_semaphore != NULL)
13238 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13239 bl->probe.objfile,
13240 bl->gdbarch);
55aa24fb 13241
73971819 13242 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
13243}
13244
13245static void
f00aae0f 13246bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13247 struct linespec_result *canonical,
f00aae0f 13248 enum bptype type_wanted)
55aa24fb
SDJ
13249{
13250 struct linespec_sals lsal;
13251
c2f4122d 13252 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
13253 lsal.canonical
13254 = xstrdup (event_location_to_string (canonical->location.get ()));
55aa24fb
SDJ
13255 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13256}
13257
13258static void
f00aae0f
KS
13259bkpt_probe_decode_location (struct breakpoint *b,
13260 const struct event_location *location,
c2f4122d 13261 struct program_space *search_pspace,
55aa24fb
SDJ
13262 struct symtabs_and_lines *sals)
13263{
c2f4122d 13264 *sals = parse_probes (location, search_pspace, NULL);
55aa24fb
SDJ
13265 if (!sals->sals)
13266 error (_("probe not found"));
13267}
13268
348d480f 13269/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13270
348d480f
PA
13271static void
13272tracepoint_re_set (struct breakpoint *b)
13273{
13274 breakpoint_re_set_default (b);
13275}
876fa593 13276
348d480f
PA
13277static int
13278tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13279 struct address_space *aspace, CORE_ADDR bp_addr,
13280 const struct target_waitstatus *ws)
348d480f
PA
13281{
13282 /* By definition, the inferior does not report stops at
13283 tracepoints. */
13284 return 0;
74960c60
VP
13285}
13286
13287static void
348d480f
PA
13288tracepoint_print_one_detail (const struct breakpoint *self,
13289 struct ui_out *uiout)
74960c60 13290{
d9b3f62e
PA
13291 struct tracepoint *tp = (struct tracepoint *) self;
13292 if (tp->static_trace_marker_id)
348d480f
PA
13293 {
13294 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13295
112e8700
SM
13296 uiout->text ("\tmarker id is ");
13297 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 13298 tp->static_trace_marker_id);
112e8700 13299 uiout->text ("\n");
348d480f 13300 }
0d381245
VP
13301}
13302
a474d7c2 13303static void
348d480f 13304tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13305{
112e8700 13306 if (current_uiout->is_mi_like_p ())
348d480f 13307 return;
cc59ec59 13308
348d480f
PA
13309 switch (b->type)
13310 {
13311 case bp_tracepoint:
13312 printf_filtered (_("Tracepoint"));
13313 printf_filtered (_(" %d"), b->number);
13314 break;
13315 case bp_fast_tracepoint:
13316 printf_filtered (_("Fast tracepoint"));
13317 printf_filtered (_(" %d"), b->number);
13318 break;
13319 case bp_static_tracepoint:
13320 printf_filtered (_("Static tracepoint"));
13321 printf_filtered (_(" %d"), b->number);
13322 break;
13323 default:
13324 internal_error (__FILE__, __LINE__,
13325 _("unhandled tracepoint type %d"), (int) b->type);
13326 }
13327
13328 say_where (b);
a474d7c2
PA
13329}
13330
348d480f 13331static void
d9b3f62e 13332tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13333{
d9b3f62e
PA
13334 struct tracepoint *tp = (struct tracepoint *) self;
13335
13336 if (self->type == bp_fast_tracepoint)
348d480f 13337 fprintf_unfiltered (fp, "ftrace");
c93e8391 13338 else if (self->type == bp_static_tracepoint)
348d480f 13339 fprintf_unfiltered (fp, "strace");
d9b3f62e 13340 else if (self->type == bp_tracepoint)
348d480f
PA
13341 fprintf_unfiltered (fp, "trace");
13342 else
13343 internal_error (__FILE__, __LINE__,
d9b3f62e 13344 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13345
f00aae0f 13346 fprintf_unfiltered (fp, " %s",
d28cd78a 13347 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13348 print_recreate_thread (self, fp);
13349
13350 if (tp->pass_count)
13351 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13352}
13353
983af33b 13354static void
f00aae0f
KS
13355tracepoint_create_sals_from_location (const struct event_location *location,
13356 struct linespec_result *canonical,
13357 enum bptype type_wanted)
983af33b 13358{
f00aae0f 13359 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13360}
13361
13362static void
13363tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13364 struct linespec_result *canonical,
e1e01040
PA
13365 gdb::unique_xmalloc_ptr<char> cond_string,
13366 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13367 enum bptype type_wanted,
13368 enum bpdisp disposition,
13369 int thread,
13370 int task, int ignore_count,
13371 const struct breakpoint_ops *ops,
13372 int from_tty, int enabled,
44f238bb 13373 int internal, unsigned flags)
983af33b 13374{
023fa29b 13375 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13376 std::move (cond_string),
13377 std::move (extra_string),
e7e0cddf 13378 type_wanted,
983af33b
SDJ
13379 disposition, thread, task,
13380 ignore_count, ops, from_tty,
44f238bb 13381 enabled, internal, flags);
983af33b
SDJ
13382}
13383
13384static void
f00aae0f
KS
13385tracepoint_decode_location (struct breakpoint *b,
13386 const struct event_location *location,
c2f4122d 13387 struct program_space *search_pspace,
983af33b
SDJ
13388 struct symtabs_and_lines *sals)
13389{
c2f4122d 13390 decode_location_default (b, location, search_pspace, sals);
983af33b
SDJ
13391}
13392
2060206e 13393struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13394
55aa24fb
SDJ
13395/* The breakpoint_ops structure to be use on tracepoints placed in a
13396 static probe. */
13397
13398static void
f00aae0f
KS
13399tracepoint_probe_create_sals_from_location
13400 (const struct event_location *location,
13401 struct linespec_result *canonical,
13402 enum bptype type_wanted)
55aa24fb
SDJ
13403{
13404 /* We use the same method for breakpoint on probes. */
f00aae0f 13405 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13406}
13407
13408static void
f00aae0f
KS
13409tracepoint_probe_decode_location (struct breakpoint *b,
13410 const struct event_location *location,
c2f4122d 13411 struct program_space *search_pspace,
55aa24fb
SDJ
13412 struct symtabs_and_lines *sals)
13413{
13414 /* We use the same method for breakpoint on probes. */
c2f4122d 13415 bkpt_probe_decode_location (b, location, search_pspace, sals);
55aa24fb
SDJ
13416}
13417
13418static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13419
5c2b4418
HZ
13420/* Dprintf breakpoint_ops methods. */
13421
13422static void
13423dprintf_re_set (struct breakpoint *b)
13424{
13425 breakpoint_re_set_default (b);
13426
f00aae0f
KS
13427 /* extra_string should never be non-NULL for dprintf. */
13428 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13429
13430 /* 1 - connect to target 1, that can run breakpoint commands.
13431 2 - create a dprintf, which resolves fine.
13432 3 - disconnect from target 1
13433 4 - connect to target 2, that can NOT run breakpoint commands.
13434
13435 After steps #3/#4, you'll want the dprintf command list to
13436 be updated, because target 1 and 2 may well return different
13437 answers for target_can_run_breakpoint_commands().
13438 Given absence of finer grained resetting, we get to do
13439 it all the time. */
13440 if (b->extra_string != NULL)
13441 update_dprintf_command_list (b);
13442}
13443
2d9442cc
HZ
13444/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13445
13446static void
13447dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13448{
f00aae0f 13449 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13450 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13451 tp->extra_string);
13452 print_recreate_thread (tp, fp);
13453}
13454
9d6e6e84
HZ
13455/* Implement the "after_condition_true" breakpoint_ops method for
13456 dprintf.
13457
13458 dprintf's are implemented with regular commands in their command
13459 list, but we run the commands here instead of before presenting the
13460 stop to the user, as dprintf's don't actually cause a stop. This
13461 also makes it so that the commands of multiple dprintfs at the same
13462 address are all handled. */
13463
13464static void
13465dprintf_after_condition_true (struct bpstats *bs)
13466{
13467 struct cleanup *old_chain;
13468 struct bpstats tmp_bs = { NULL };
13469 struct bpstats *tmp_bs_p = &tmp_bs;
13470
13471 /* dprintf's never cause a stop. This wasn't set in the
13472 check_status hook instead because that would make the dprintf's
13473 condition not be evaluated. */
13474 bs->stop = 0;
13475
13476 /* Run the command list here. Take ownership of it instead of
13477 copying. We never want these commands to run later in
13478 bpstat_do_actions, if a breakpoint that causes a stop happens to
13479 be set at same address as this dprintf, or even if running the
13480 commands here throws. */
13481 tmp_bs.commands = bs->commands;
13482 bs->commands = NULL;
13483 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13484
13485 bpstat_do_actions_1 (&tmp_bs_p);
13486
13487 /* 'tmp_bs.commands' will usually be NULL by now, but
13488 bpstat_do_actions_1 may return early without processing the whole
13489 list. */
13490 do_cleanups (old_chain);
13491}
13492
983af33b
SDJ
13493/* The breakpoint_ops structure to be used on static tracepoints with
13494 markers (`-m'). */
13495
13496static void
f00aae0f 13497strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13498 struct linespec_result *canonical,
f00aae0f 13499 enum bptype type_wanted)
983af33b
SDJ
13500{
13501 struct linespec_sals lsal;
f00aae0f
KS
13502 const char *arg_start, *arg;
13503 char *str;
13504 struct cleanup *cleanup;
983af33b 13505
f00aae0f
KS
13506 arg = arg_start = get_linespec_location (location);
13507 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13508
f00aae0f
KS
13509 str = savestring (arg_start, arg - arg_start);
13510 cleanup = make_cleanup (xfree, str);
8e9e35b1 13511 canonical->location = new_linespec_location (&str);
f00aae0f 13512 do_cleanups (cleanup);
983af33b 13513
8e9e35b1
TT
13514 lsal.canonical
13515 = xstrdup (event_location_to_string (canonical->location.get ()));
983af33b
SDJ
13516 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13517}
13518
13519static void
13520strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13521 struct linespec_result *canonical,
e1e01040
PA
13522 gdb::unique_xmalloc_ptr<char> cond_string,
13523 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13524 enum bptype type_wanted,
13525 enum bpdisp disposition,
13526 int thread,
13527 int task, int ignore_count,
13528 const struct breakpoint_ops *ops,
13529 int from_tty, int enabled,
44f238bb 13530 int internal, unsigned flags)
983af33b
SDJ
13531{
13532 int i;
52d361e1
YQ
13533 struct linespec_sals *lsal = VEC_index (linespec_sals,
13534 canonical->sals, 0);
983af33b
SDJ
13535
13536 /* If the user is creating a static tracepoint by marker id
13537 (strace -m MARKER_ID), then store the sals index, so that
13538 breakpoint_re_set can try to match up which of the newly
13539 found markers corresponds to this one, and, don't try to
13540 expand multiple locations for each sal, given than SALS
13541 already should contain all sals for MARKER_ID. */
13542
13543 for (i = 0; i < lsal->sals.nelts; ++i)
13544 {
13545 struct symtabs_and_lines expanded;
ffc2605c 13546 event_location_up location;
983af33b
SDJ
13547
13548 expanded.nelts = 1;
13549 expanded.sals = &lsal->sals.sals[i];
13550
8e9e35b1 13551 location = copy_event_location (canonical->location.get ());
983af33b 13552
b270e6f9
TT
13553 std::unique_ptr<tracepoint> tp (new tracepoint ());
13554 init_breakpoint_sal (tp.get (), gdbarch, expanded,
ffc2605c 13555 std::move (location), NULL,
e1e01040
PA
13556 std::move (cond_string),
13557 std::move (extra_string),
e7e0cddf 13558 type_wanted, disposition,
983af33b 13559 thread, task, ignore_count, ops,
44f238bb 13560 from_tty, enabled, internal, flags,
983af33b
SDJ
13561 canonical->special_display);
13562 /* Given that its possible to have multiple markers with
13563 the same string id, if the user is creating a static
13564 tracepoint by marker id ("strace -m MARKER_ID"), then
13565 store the sals index, so that breakpoint_re_set can
13566 try to match up which of the newly found markers
13567 corresponds to this one */
13568 tp->static_trace_marker_id_idx = i;
13569
b270e6f9 13570 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13571 }
13572}
13573
13574static void
f00aae0f
KS
13575strace_marker_decode_location (struct breakpoint *b,
13576 const struct event_location *location,
c2f4122d 13577 struct program_space *search_pspace,
983af33b
SDJ
13578 struct symtabs_and_lines *sals)
13579{
13580 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13581 const char *s = get_linespec_location (location);
983af33b 13582
f00aae0f 13583 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13584 if (sals->nelts > tp->static_trace_marker_id_idx)
13585 {
13586 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13587 sals->nelts = 1;
13588 }
13589 else
13590 error (_("marker %s not found"), tp->static_trace_marker_id);
13591}
13592
13593static struct breakpoint_ops strace_marker_breakpoint_ops;
13594
13595static int
13596strace_marker_p (struct breakpoint *b)
13597{
13598 return b->ops == &strace_marker_breakpoint_ops;
13599}
13600
53a5351d 13601/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13602 structures. */
c906108c
SS
13603
13604void
fba45db2 13605delete_breakpoint (struct breakpoint *bpt)
c906108c 13606{
52f0bd74 13607 struct breakpoint *b;
c906108c 13608
8a3fe4f8 13609 gdb_assert (bpt != NULL);
c906108c 13610
4a64f543
MS
13611 /* Has this bp already been deleted? This can happen because
13612 multiple lists can hold pointers to bp's. bpstat lists are
13613 especial culprits.
13614
13615 One example of this happening is a watchpoint's scope bp. When
13616 the scope bp triggers, we notice that the watchpoint is out of
13617 scope, and delete it. We also delete its scope bp. But the
13618 scope bp is marked "auto-deleting", and is already on a bpstat.
13619 That bpstat is then checked for auto-deleting bp's, which are
13620 deleted.
13621
13622 A real solution to this problem might involve reference counts in
13623 bp's, and/or giving them pointers back to their referencing
13624 bpstat's, and teaching delete_breakpoint to only free a bp's
13625 storage when no more references were extent. A cheaper bandaid
13626 was chosen. */
c906108c
SS
13627 if (bpt->type == bp_none)
13628 return;
13629
4a64f543
MS
13630 /* At least avoid this stale reference until the reference counting
13631 of breakpoints gets resolved. */
d0fb5eae 13632 if (bpt->related_breakpoint != bpt)
e5a0a904 13633 {
d0fb5eae 13634 struct breakpoint *related;
3a5c3e22 13635 struct watchpoint *w;
d0fb5eae
JK
13636
13637 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13638 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13639 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13640 w = (struct watchpoint *) bpt;
13641 else
13642 w = NULL;
13643 if (w != NULL)
13644 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13645
13646 /* Unlink bpt from the bpt->related_breakpoint ring. */
13647 for (related = bpt; related->related_breakpoint != bpt;
13648 related = related->related_breakpoint);
13649 related->related_breakpoint = bpt->related_breakpoint;
13650 bpt->related_breakpoint = bpt;
e5a0a904
JK
13651 }
13652
a9634178
TJB
13653 /* watch_command_1 creates a watchpoint but only sets its number if
13654 update_watchpoint succeeds in creating its bp_locations. If there's
13655 a problem in that process, we'll be asked to delete the half-created
13656 watchpoint. In that case, don't announce the deletion. */
13657 if (bpt->number)
13658 observer_notify_breakpoint_deleted (bpt);
c906108c 13659
c906108c
SS
13660 if (breakpoint_chain == bpt)
13661 breakpoint_chain = bpt->next;
13662
c906108c
SS
13663 ALL_BREAKPOINTS (b)
13664 if (b->next == bpt)
c5aa993b
JM
13665 {
13666 b->next = bpt->next;
13667 break;
13668 }
c906108c 13669
f431efe5
PA
13670 /* Be sure no bpstat's are pointing at the breakpoint after it's
13671 been freed. */
13672 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13673 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13674 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13675 commands are associated with the bpstat; if we remove it here,
13676 then the later call to bpstat_do_actions (&stop_bpstat); in
13677 event-top.c won't do anything, and temporary breakpoints with
13678 commands won't work. */
13679
13680 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13681
4a64f543
MS
13682 /* Now that breakpoint is removed from breakpoint list, update the
13683 global location list. This will remove locations that used to
13684 belong to this breakpoint. Do this before freeing the breakpoint
13685 itself, since remove_breakpoint looks at location's owner. It
13686 might be better design to have location completely
13687 self-contained, but it's not the case now. */
44702360 13688 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13689
4a64f543
MS
13690 /* On the chance that someone will soon try again to delete this
13691 same bp, we mark it as deleted before freeing its storage. */
c906108c 13692 bpt->type = bp_none;
4d01a485 13693 delete bpt;
c906108c
SS
13694}
13695
4d6140d9
AC
13696static void
13697do_delete_breakpoint_cleanup (void *b)
13698{
9a3c8263 13699 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13700}
13701
13702struct cleanup *
13703make_cleanup_delete_breakpoint (struct breakpoint *b)
13704{
13705 return make_cleanup (do_delete_breakpoint_cleanup, b);
13706}
13707
51be5b68
PA
13708/* Iterator function to call a user-provided callback function once
13709 for each of B and its related breakpoints. */
13710
13711static void
13712iterate_over_related_breakpoints (struct breakpoint *b,
13713 void (*function) (struct breakpoint *,
13714 void *),
13715 void *data)
13716{
13717 struct breakpoint *related;
13718
13719 related = b;
13720 do
13721 {
13722 struct breakpoint *next;
13723
13724 /* FUNCTION may delete RELATED. */
13725 next = related->related_breakpoint;
13726
13727 if (next == related)
13728 {
13729 /* RELATED is the last ring entry. */
13730 function (related, data);
13731
13732 /* FUNCTION may have deleted it, so we'd never reach back to
13733 B. There's nothing left to do anyway, so just break
13734 out. */
13735 break;
13736 }
13737 else
13738 function (related, data);
13739
13740 related = next;
13741 }
13742 while (related != b);
13743}
95a42b64
TT
13744
13745static void
13746do_delete_breakpoint (struct breakpoint *b, void *ignore)
13747{
13748 delete_breakpoint (b);
13749}
13750
51be5b68
PA
13751/* A callback for map_breakpoint_numbers that calls
13752 delete_breakpoint. */
13753
13754static void
13755do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13756{
13757 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13758}
13759
c906108c 13760void
fba45db2 13761delete_command (char *arg, int from_tty)
c906108c 13762{
35df4500 13763 struct breakpoint *b, *b_tmp;
c906108c 13764
ea9365bb
TT
13765 dont_repeat ();
13766
c906108c
SS
13767 if (arg == 0)
13768 {
13769 int breaks_to_delete = 0;
13770
46c6471b
PA
13771 /* Delete all breakpoints if no argument. Do not delete
13772 internal breakpoints, these have to be deleted with an
13773 explicit breakpoint number argument. */
c5aa993b 13774 ALL_BREAKPOINTS (b)
46c6471b 13775 if (user_breakpoint_p (b))
973d738b
DJ
13776 {
13777 breaks_to_delete = 1;
13778 break;
13779 }
c906108c
SS
13780
13781 /* Ask user only if there are some breakpoints to delete. */
13782 if (!from_tty
e2e0b3e5 13783 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13784 {
35df4500 13785 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13786 if (user_breakpoint_p (b))
c5aa993b 13787 delete_breakpoint (b);
c906108c
SS
13788 }
13789 }
13790 else
51be5b68 13791 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13792}
13793
c2f4122d
PA
13794/* Return true if all locations of B bound to PSPACE are pending. If
13795 PSPACE is NULL, all locations of all program spaces are
13796 considered. */
13797
0d381245 13798static int
c2f4122d 13799all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13800{
c2f4122d
PA
13801 struct bp_location *loc;
13802
13803 for (loc = b->loc; loc != NULL; loc = loc->next)
13804 if ((pspace == NULL
13805 || loc->pspace == pspace)
13806 && !loc->shlib_disabled
8645ff69 13807 && !loc->pspace->executing_startup)
0d381245
VP
13808 return 0;
13809 return 1;
fe3f5fa8
VP
13810}
13811
776592bf
DE
13812/* Subroutine of update_breakpoint_locations to simplify it.
13813 Return non-zero if multiple fns in list LOC have the same name.
13814 Null names are ignored. */
13815
13816static int
13817ambiguous_names_p (struct bp_location *loc)
13818{
13819 struct bp_location *l;
13820 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13821 (int (*) (const void *,
13822 const void *)) streq,
776592bf
DE
13823 NULL, xcalloc, xfree);
13824
13825 for (l = loc; l != NULL; l = l->next)
13826 {
13827 const char **slot;
13828 const char *name = l->function_name;
13829
13830 /* Allow for some names to be NULL, ignore them. */
13831 if (name == NULL)
13832 continue;
13833
13834 slot = (const char **) htab_find_slot (htab, (const void *) name,
13835 INSERT);
4a64f543
MS
13836 /* NOTE: We can assume slot != NULL here because xcalloc never
13837 returns NULL. */
776592bf
DE
13838 if (*slot != NULL)
13839 {
13840 htab_delete (htab);
13841 return 1;
13842 }
13843 *slot = name;
13844 }
13845
13846 htab_delete (htab);
13847 return 0;
13848}
13849
0fb4aa4b
PA
13850/* When symbols change, it probably means the sources changed as well,
13851 and it might mean the static tracepoint markers are no longer at
13852 the same address or line numbers they used to be at last we
13853 checked. Losing your static tracepoints whenever you rebuild is
13854 undesirable. This function tries to resync/rematch gdb static
13855 tracepoints with the markers on the target, for static tracepoints
13856 that have not been set by marker id. Static tracepoint that have
13857 been set by marker id are reset by marker id in breakpoint_re_set.
13858 The heuristic is:
13859
13860 1) For a tracepoint set at a specific address, look for a marker at
13861 the old PC. If one is found there, assume to be the same marker.
13862 If the name / string id of the marker found is different from the
13863 previous known name, assume that means the user renamed the marker
13864 in the sources, and output a warning.
13865
13866 2) For a tracepoint set at a given line number, look for a marker
13867 at the new address of the old line number. If one is found there,
13868 assume to be the same marker. If the name / string id of the
13869 marker found is different from the previous known name, assume that
13870 means the user renamed the marker in the sources, and output a
13871 warning.
13872
13873 3) If a marker is no longer found at the same address or line, it
13874 may mean the marker no longer exists. But it may also just mean
13875 the code changed a bit. Maybe the user added a few lines of code
13876 that made the marker move up or down (in line number terms). Ask
13877 the target for info about the marker with the string id as we knew
13878 it. If found, update line number and address in the matching
13879 static tracepoint. This will get confused if there's more than one
13880 marker with the same ID (possible in UST, although unadvised
13881 precisely because it confuses tools). */
13882
13883static struct symtab_and_line
13884update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13885{
d9b3f62e 13886 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13887 struct static_tracepoint_marker marker;
13888 CORE_ADDR pc;
0fb4aa4b
PA
13889
13890 pc = sal.pc;
13891 if (sal.line)
13892 find_line_pc (sal.symtab, sal.line, &pc);
13893
13894 if (target_static_tracepoint_marker_at (pc, &marker))
13895 {
d9b3f62e 13896 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13897 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13898 b->number,
d9b3f62e 13899 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13900
d9b3f62e
PA
13901 xfree (tp->static_trace_marker_id);
13902 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13903 release_static_tracepoint_marker (&marker);
13904
13905 return sal;
13906 }
13907
13908 /* Old marker wasn't found on target at lineno. Try looking it up
13909 by string ID. */
13910 if (!sal.explicit_pc
13911 && sal.line != 0
13912 && sal.symtab != NULL
d9b3f62e 13913 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13914 {
13915 VEC(static_tracepoint_marker_p) *markers;
13916
13917 markers
d9b3f62e 13918 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13919
13920 if (!VEC_empty(static_tracepoint_marker_p, markers))
13921 {
80e1d417 13922 struct symtab_and_line sal2;
0fb4aa4b 13923 struct symbol *sym;
80e1d417 13924 struct static_tracepoint_marker *tpmarker;
79a45e25 13925 struct ui_out *uiout = current_uiout;
67994074 13926 struct explicit_location explicit_loc;
0fb4aa4b 13927
80e1d417 13928 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13929
d9b3f62e 13930 xfree (tp->static_trace_marker_id);
80e1d417 13931 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13932
13933 warning (_("marker for static tracepoint %d (%s) not "
13934 "found at previous line number"),
d9b3f62e 13935 b->number, tp->static_trace_marker_id);
0fb4aa4b 13936
80e1d417 13937 init_sal (&sal2);
0fb4aa4b 13938
80e1d417 13939 sal2.pc = tpmarker->address;
0fb4aa4b 13940
80e1d417
AS
13941 sal2 = find_pc_line (tpmarker->address, 0);
13942 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13943 uiout->text ("Now in ");
0fb4aa4b
PA
13944 if (sym)
13945 {
112e8700
SM
13946 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13947 uiout->text (" at ");
0fb4aa4b 13948 }
112e8700 13949 uiout->field_string ("file",
05cba821 13950 symtab_to_filename_for_display (sal2.symtab));
112e8700 13951 uiout->text (":");
0fb4aa4b 13952
112e8700 13953 if (uiout->is_mi_like_p ())
0fb4aa4b 13954 {
0b0865da 13955 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13956
112e8700 13957 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13958 }
13959
112e8700
SM
13960 uiout->field_int ("line", sal2.line);
13961 uiout->text ("\n");
0fb4aa4b 13962
80e1d417 13963 b->loc->line_number = sal2.line;
2f202fde 13964 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13965
d28cd78a 13966 b->location.reset (NULL);
67994074
KS
13967 initialize_explicit_location (&explicit_loc);
13968 explicit_loc.source_filename
00e52e53 13969 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13970 explicit_loc.line_offset.offset = b->loc->line_number;
13971 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13972 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13973
13974 /* Might be nice to check if function changed, and warn if
13975 so. */
13976
80e1d417 13977 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13978 }
13979 }
13980 return sal;
13981}
13982
8d3788bd
VP
13983/* Returns 1 iff locations A and B are sufficiently same that
13984 we don't need to report breakpoint as changed. */
13985
13986static int
13987locations_are_equal (struct bp_location *a, struct bp_location *b)
13988{
13989 while (a && b)
13990 {
13991 if (a->address != b->address)
13992 return 0;
13993
13994 if (a->shlib_disabled != b->shlib_disabled)
13995 return 0;
13996
13997 if (a->enabled != b->enabled)
13998 return 0;
13999
14000 a = a->next;
14001 b = b->next;
14002 }
14003
14004 if ((a == NULL) != (b == NULL))
14005 return 0;
14006
14007 return 1;
14008}
14009
c2f4122d
PA
14010/* Split all locations of B that are bound to PSPACE out of B's
14011 location list to a separate list and return that list's head. If
14012 PSPACE is NULL, hoist out all locations of B. */
14013
14014static struct bp_location *
14015hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14016{
14017 struct bp_location head;
14018 struct bp_location *i = b->loc;
14019 struct bp_location **i_link = &b->loc;
14020 struct bp_location *hoisted = &head;
14021
14022 if (pspace == NULL)
14023 {
14024 i = b->loc;
14025 b->loc = NULL;
14026 return i;
14027 }
14028
14029 head.next = NULL;
14030
14031 while (i != NULL)
14032 {
14033 if (i->pspace == pspace)
14034 {
14035 *i_link = i->next;
14036 i->next = NULL;
14037 hoisted->next = i;
14038 hoisted = i;
14039 }
14040 else
14041 i_link = &i->next;
14042 i = *i_link;
14043 }
14044
14045 return head.next;
14046}
14047
14048/* Create new breakpoint locations for B (a hardware or software
14049 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14050 zero, then B is a ranged breakpoint. Only recreates locations for
14051 FILTER_PSPACE. Locations of other program spaces are left
14052 untouched. */
f1310107 14053
0e30163f 14054void
0d381245 14055update_breakpoint_locations (struct breakpoint *b,
c2f4122d 14056 struct program_space *filter_pspace,
f1310107
TJB
14057 struct symtabs_and_lines sals,
14058 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14059{
14060 int i;
c2f4122d 14061 struct bp_location *existing_locations;
0d381245 14062
f8eba3c6
TT
14063 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14064 {
14065 /* Ranged breakpoints have only one start location and one end
14066 location. */
14067 b->enable_state = bp_disabled;
f8eba3c6
TT
14068 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14069 "multiple locations found\n"),
14070 b->number);
14071 return;
14072 }
f1310107 14073
4a64f543
MS
14074 /* If there's no new locations, and all existing locations are
14075 pending, don't do anything. This optimizes the common case where
14076 all locations are in the same shared library, that was unloaded.
14077 We'd like to retain the location, so that when the library is
14078 loaded again, we don't loose the enabled/disabled status of the
14079 individual locations. */
c2f4122d 14080 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
fe3f5fa8
VP
14081 return;
14082
c2f4122d 14083 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 14084
0d381245 14085 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14086 {
f8eba3c6
TT
14087 struct bp_location *new_loc;
14088
14089 switch_to_program_space_and_thread (sals.sals[i].pspace);
14090
14091 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14092
0d381245
VP
14093 /* Reparse conditions, they might contain references to the
14094 old symtab. */
14095 if (b->cond_string != NULL)
14096 {
bbc13ae3 14097 const char *s;
fe3f5fa8 14098
0d381245 14099 s = b->cond_string;
492d29ea 14100 TRY
0d381245 14101 {
1bb9788d
TT
14102 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14103 block_for_pc (sals.sals[i].pc),
0d381245
VP
14104 0);
14105 }
492d29ea 14106 CATCH (e, RETURN_MASK_ERROR)
0d381245 14107 {
3e43a32a
MS
14108 warning (_("failed to reevaluate condition "
14109 "for breakpoint %d: %s"),
0d381245
VP
14110 b->number, e.message);
14111 new_loc->enabled = 0;
14112 }
492d29ea 14113 END_CATCH
0d381245 14114 }
fe3f5fa8 14115
f1310107
TJB
14116 if (sals_end.nelts)
14117 {
14118 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14119
14120 new_loc->length = end - sals.sals[0].pc + 1;
14121 }
0d381245 14122 }
fe3f5fa8 14123
4a64f543
MS
14124 /* If possible, carry over 'disable' status from existing
14125 breakpoints. */
0d381245
VP
14126 {
14127 struct bp_location *e = existing_locations;
776592bf
DE
14128 /* If there are multiple breakpoints with the same function name,
14129 e.g. for inline functions, comparing function names won't work.
14130 Instead compare pc addresses; this is just a heuristic as things
14131 may have moved, but in practice it gives the correct answer
14132 often enough until a better solution is found. */
14133 int have_ambiguous_names = ambiguous_names_p (b->loc);
14134
0d381245
VP
14135 for (; e; e = e->next)
14136 {
14137 if (!e->enabled && e->function_name)
14138 {
14139 struct bp_location *l = b->loc;
776592bf
DE
14140 if (have_ambiguous_names)
14141 {
14142 for (; l; l = l->next)
f1310107 14143 if (breakpoint_locations_match (e, l))
776592bf
DE
14144 {
14145 l->enabled = 0;
14146 break;
14147 }
14148 }
14149 else
14150 {
14151 for (; l; l = l->next)
14152 if (l->function_name
14153 && strcmp (e->function_name, l->function_name) == 0)
14154 {
14155 l->enabled = 0;
14156 break;
14157 }
14158 }
0d381245
VP
14159 }
14160 }
14161 }
fe3f5fa8 14162
8d3788bd
VP
14163 if (!locations_are_equal (existing_locations, b->loc))
14164 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
14165}
14166
f00aae0f 14167/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14168 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14169
14170static struct symtabs_and_lines
f00aae0f 14171location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 14172 struct program_space *search_pspace, int *found)
ef23e705 14173{
02d20e4a 14174 struct symtabs_and_lines sals = {0};
492d29ea 14175 struct gdb_exception exception = exception_none;
ef23e705 14176
983af33b 14177 gdb_assert (b->ops != NULL);
ef23e705 14178
492d29ea 14179 TRY
ef23e705 14180 {
c2f4122d 14181 b->ops->decode_location (b, location, search_pspace, &sals);
ef23e705 14182 }
492d29ea 14183 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14184 {
14185 int not_found_and_ok = 0;
492d29ea
PA
14186
14187 exception = e;
14188
ef23e705
TJB
14189 /* For pending breakpoints, it's expected that parsing will
14190 fail until the right shared library is loaded. User has
14191 already told to create pending breakpoints and don't need
14192 extra messages. If breakpoint is in bp_shlib_disabled
14193 state, then user already saw the message about that
14194 breakpoint being disabled, and don't want to see more
14195 errors. */
58438ac1 14196 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
14197 && (b->condition_not_parsed
14198 || (b->loc != NULL
14199 && search_pspace != NULL
14200 && b->loc->pspace != search_pspace)
ef23e705 14201 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14202 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14203 || b->enable_state == bp_disabled))
14204 not_found_and_ok = 1;
14205
14206 if (!not_found_and_ok)
14207 {
14208 /* We surely don't want to warn about the same breakpoint
14209 10 times. One solution, implemented here, is disable
14210 the breakpoint on error. Another solution would be to
14211 have separate 'warning emitted' flag. Since this
14212 happens only when a binary has changed, I don't know
14213 which approach is better. */
14214 b->enable_state = bp_disabled;
14215 throw_exception (e);
14216 }
14217 }
492d29ea 14218 END_CATCH
ef23e705 14219
492d29ea 14220 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14221 {
f8eba3c6 14222 int i;
ef23e705 14223
f8eba3c6
TT
14224 for (i = 0; i < sals.nelts; ++i)
14225 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14226 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14227 {
ed1d1739
KS
14228 char *cond_string, *extra_string;
14229 int thread, task;
ef23e705 14230
f00aae0f 14231 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14232 &cond_string, &thread, &task,
14233 &extra_string);
f00aae0f 14234 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14235 if (cond_string)
14236 b->cond_string = cond_string;
14237 b->thread = thread;
14238 b->task = task;
e7e0cddf 14239 if (extra_string)
f00aae0f
KS
14240 {
14241 xfree (b->extra_string);
14242 b->extra_string = extra_string;
14243 }
ef23e705
TJB
14244 b->condition_not_parsed = 0;
14245 }
14246
983af33b 14247 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14248 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14249
58438ac1
TT
14250 *found = 1;
14251 }
14252 else
14253 *found = 0;
ef23e705
TJB
14254
14255 return sals;
14256}
14257
348d480f
PA
14258/* The default re_set method, for typical hardware or software
14259 breakpoints. Reevaluate the breakpoint and recreate its
14260 locations. */
14261
14262static void
28010a5d 14263breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14264{
14265 int found;
f1310107 14266 struct symtabs_and_lines sals, sals_end;
ef23e705 14267 struct symtabs_and_lines expanded = {0};
f1310107 14268 struct symtabs_and_lines expanded_end = {0};
c2f4122d 14269 struct program_space *filter_pspace = current_program_space;
ef23e705 14270
d28cd78a 14271 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
ef23e705
TJB
14272 if (found)
14273 {
14274 make_cleanup (xfree, sals.sals);
f8eba3c6 14275 expanded = sals;
ef23e705
TJB
14276 }
14277
f00aae0f 14278 if (b->location_range_end != NULL)
f1310107 14279 {
d28cd78a 14280 sals_end = location_to_sals (b, b->location_range_end.get (),
c2f4122d 14281 filter_pspace, &found);
f1310107
TJB
14282 if (found)
14283 {
14284 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14285 expanded_end = sals_end;
f1310107
TJB
14286 }
14287 }
14288
c2f4122d 14289 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
14290}
14291
983af33b
SDJ
14292/* Default method for creating SALs from an address string. It basically
14293 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14294
14295static void
f00aae0f
KS
14296create_sals_from_location_default (const struct event_location *location,
14297 struct linespec_result *canonical,
14298 enum bptype type_wanted)
983af33b 14299{
f00aae0f 14300 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14301}
14302
14303/* Call create_breakpoints_sal for the given arguments. This is the default
14304 function for the `create_breakpoints_sal' method of
14305 breakpoint_ops. */
14306
14307static void
14308create_breakpoints_sal_default (struct gdbarch *gdbarch,
14309 struct linespec_result *canonical,
e1e01040
PA
14310 gdb::unique_xmalloc_ptr<char> cond_string,
14311 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
14312 enum bptype type_wanted,
14313 enum bpdisp disposition,
14314 int thread,
14315 int task, int ignore_count,
14316 const struct breakpoint_ops *ops,
14317 int from_tty, int enabled,
44f238bb 14318 int internal, unsigned flags)
983af33b 14319{
e1e01040
PA
14320 create_breakpoints_sal (gdbarch, canonical,
14321 std::move (cond_string),
14322 std::move (extra_string),
983af33b
SDJ
14323 type_wanted, disposition,
14324 thread, task, ignore_count, ops, from_tty,
44f238bb 14325 enabled, internal, flags);
983af33b
SDJ
14326}
14327
14328/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14329 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14330
14331static void
f00aae0f
KS
14332decode_location_default (struct breakpoint *b,
14333 const struct event_location *location,
c2f4122d 14334 struct program_space *search_pspace,
983af33b
SDJ
14335 struct symtabs_and_lines *sals)
14336{
14337 struct linespec_result canonical;
14338
c2f4122d 14339 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
14340 (struct symtab *) NULL, 0,
14341 &canonical, multiple_symbols_all,
14342 b->filter);
14343
14344 /* We should get 0 or 1 resulting SALs. */
14345 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14346
14347 if (VEC_length (linespec_sals, canonical.sals) > 0)
14348 {
14349 struct linespec_sals *lsal;
14350
14351 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14352 *sals = lsal->sals;
14353 /* Arrange it so the destructor does not free the
14354 contents. */
14355 lsal->sals.sals = NULL;
14356 }
983af33b
SDJ
14357}
14358
28010a5d
PA
14359/* Prepare the global context for a re-set of breakpoint B. */
14360
14361static struct cleanup *
14362prepare_re_set_context (struct breakpoint *b)
14363{
28010a5d 14364 input_radix = b->input_radix;
28010a5d
PA
14365 set_language (b->language);
14366
c2f4122d 14367 return make_cleanup (null_cleanup, NULL);
ef23e705
TJB
14368}
14369
c906108c
SS
14370/* Reset a breakpoint given it's struct breakpoint * BINT.
14371 The value we return ends up being the return value from catch_errors.
14372 Unused in this case. */
14373
14374static int
4efb68b1 14375breakpoint_re_set_one (void *bint)
c906108c 14376{
4a64f543 14377 /* Get past catch_errs. */
53a5351d 14378 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14379 struct cleanup *cleanups;
c906108c 14380
348d480f
PA
14381 cleanups = prepare_re_set_context (b);
14382 b->ops->re_set (b);
14383 do_cleanups (cleanups);
c906108c
SS
14384 return 0;
14385}
14386
c2f4122d
PA
14387/* Re-set breakpoint locations for the current program space.
14388 Locations bound to other program spaces are left untouched. */
14389
c906108c 14390void
69de3c6a 14391breakpoint_re_set (void)
c906108c 14392{
35df4500 14393 struct breakpoint *b, *b_tmp;
c906108c
SS
14394 enum language save_language;
14395 int save_input_radix;
c5aa993b 14396
c906108c
SS
14397 save_language = current_language->la_language;
14398 save_input_radix = input_radix;
2a7f3dff 14399
c5aa993b 14400 {
5ed8105e 14401 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14402
5ed8105e
PA
14403 /* Note: we must not try to insert locations until after all
14404 breakpoints have been re-set. Otherwise, e.g., when re-setting
14405 breakpoint 1, we'd insert the locations of breakpoint 2, which
14406 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14407
5ed8105e
PA
14408 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14409 {
14410 /* Format possible error msg. */
14411 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14412 b->number);
14413 struct cleanup *cleanups = make_cleanup (xfree, message);
14414 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14415 do_cleanups (cleanups);
14416 }
14417 set_language (save_language);
14418 input_radix = save_input_radix;
14419
14420 jit_breakpoint_re_set ();
14421 }
6c95b8df 14422
af02033e
PP
14423 create_overlay_event_breakpoint ();
14424 create_longjmp_master_breakpoint ();
14425 create_std_terminate_master_breakpoint ();
186c406b 14426 create_exception_master_breakpoint ();
2a7f3dff
PA
14427
14428 /* Now we can insert. */
14429 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14430}
14431\f
c906108c
SS
14432/* Reset the thread number of this breakpoint:
14433
14434 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14435 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14436void
fba45db2 14437breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14438{
14439 if (b->thread != -1)
14440 {
39f77062 14441 if (in_thread_list (inferior_ptid))
5d5658a1 14442 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14443
14444 /* We're being called after following a fork. The new fork is
14445 selected as current, and unless this was a vfork will have a
14446 different program space from the original thread. Reset that
14447 as well. */
14448 b->loc->pspace = current_program_space;
c906108c
SS
14449 }
14450}
14451
03ac34d5
MS
14452/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14453 If from_tty is nonzero, it prints a message to that effect,
14454 which ends with a period (no newline). */
14455
c906108c 14456void
fba45db2 14457set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14458{
52f0bd74 14459 struct breakpoint *b;
c906108c
SS
14460
14461 if (count < 0)
14462 count = 0;
14463
14464 ALL_BREAKPOINTS (b)
14465 if (b->number == bptnum)
c5aa993b 14466 {
d77f58be
SS
14467 if (is_tracepoint (b))
14468 {
14469 if (from_tty && count != 0)
14470 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14471 bptnum);
14472 return;
14473 }
14474
c5aa993b 14475 b->ignore_count = count;
221ea385
KS
14476 if (from_tty)
14477 {
14478 if (count == 0)
3e43a32a
MS
14479 printf_filtered (_("Will stop next time "
14480 "breakpoint %d is reached."),
221ea385
KS
14481 bptnum);
14482 else if (count == 1)
a3f17187 14483 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14484 bptnum);
14485 else
3e43a32a
MS
14486 printf_filtered (_("Will ignore next %d "
14487 "crossings of breakpoint %d."),
221ea385
KS
14488 count, bptnum);
14489 }
8d3788bd 14490 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14491 return;
14492 }
c906108c 14493
8a3fe4f8 14494 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14495}
14496
c906108c
SS
14497/* Command to set ignore-count of breakpoint N to COUNT. */
14498
14499static void
fba45db2 14500ignore_command (char *args, int from_tty)
c906108c
SS
14501{
14502 char *p = args;
52f0bd74 14503 int num;
c906108c
SS
14504
14505 if (p == 0)
e2e0b3e5 14506 error_no_arg (_("a breakpoint number"));
c5aa993b 14507
c906108c 14508 num = get_number (&p);
5c44784c 14509 if (num == 0)
8a3fe4f8 14510 error (_("bad breakpoint number: '%s'"), args);
c906108c 14511 if (*p == 0)
8a3fe4f8 14512 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14513
14514 set_ignore_count (num,
14515 longest_to_int (value_as_long (parse_and_eval (p))),
14516 from_tty);
221ea385
KS
14517 if (from_tty)
14518 printf_filtered ("\n");
c906108c
SS
14519}
14520\f
14521/* Call FUNCTION on each of the breakpoints
14522 whose numbers are given in ARGS. */
14523
14524static void
896b6bda
PA
14525map_breakpoint_numbers (const char *args,
14526 void (*function) (struct breakpoint *,
14527 void *),
95a42b64 14528 void *data)
c906108c 14529{
52f0bd74
AC
14530 int num;
14531 struct breakpoint *b, *tmp;
c906108c 14532
b9d61307 14533 if (args == 0 || *args == '\0')
e2e0b3e5 14534 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14535
bfd28288 14536 number_or_range_parser parser (args);
197f0a60 14537
bfd28288 14538 while (!parser.finished ())
c906108c 14539 {
bfd28288
PA
14540 const char *p = parser.cur_tok ();
14541 bool match = false;
197f0a60 14542
bfd28288 14543 num = parser.get_number ();
5c44784c 14544 if (num == 0)
c5aa993b 14545 {
8a3fe4f8 14546 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14547 }
14548 else
14549 {
14550 ALL_BREAKPOINTS_SAFE (b, tmp)
14551 if (b->number == num)
14552 {
bfd28288 14553 match = true;
cdac0397 14554 function (b, data);
11cf8741 14555 break;
5c44784c 14556 }
bfd28288 14557 if (!match)
a3f17187 14558 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14559 }
c906108c
SS
14560 }
14561}
14562
0d381245
VP
14563static struct bp_location *
14564find_location_by_number (char *number)
14565{
14566 char *dot = strchr (number, '.');
14567 char *p1;
14568 int bp_num;
14569 int loc_num;
14570 struct breakpoint *b;
14571 struct bp_location *loc;
14572
14573 *dot = '\0';
14574
14575 p1 = number;
197f0a60 14576 bp_num = get_number (&p1);
0d381245
VP
14577 if (bp_num == 0)
14578 error (_("Bad breakpoint number '%s'"), number);
14579
14580 ALL_BREAKPOINTS (b)
14581 if (b->number == bp_num)
14582 {
14583 break;
14584 }
14585
14586 if (!b || b->number != bp_num)
14587 error (_("Bad breakpoint number '%s'"), number);
14588
14589 p1 = dot+1;
197f0a60 14590 loc_num = get_number (&p1);
0d381245
VP
14591 if (loc_num == 0)
14592 error (_("Bad breakpoint location number '%s'"), number);
14593
14594 --loc_num;
14595 loc = b->loc;
14596 for (;loc_num && loc; --loc_num, loc = loc->next)
14597 ;
14598 if (!loc)
14599 error (_("Bad breakpoint location number '%s'"), dot+1);
14600
14601 return loc;
14602}
14603
14604
1900040c
MS
14605/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14606 If from_tty is nonzero, it prints a message to that effect,
14607 which ends with a period (no newline). */
14608
c906108c 14609void
fba45db2 14610disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14611{
14612 /* Never disable a watchpoint scope breakpoint; we want to
14613 hit them when we leave scope so we can delete both the
14614 watchpoint and its scope breakpoint at that time. */
14615 if (bpt->type == bp_watchpoint_scope)
14616 return;
14617
b5de0fa7 14618 bpt->enable_state = bp_disabled;
c906108c 14619
b775012e
LM
14620 /* Mark breakpoint locations modified. */
14621 mark_breakpoint_modified (bpt);
14622
d248b706
KY
14623 if (target_supports_enable_disable_tracepoint ()
14624 && current_trace_status ()->running && is_tracepoint (bpt))
14625 {
14626 struct bp_location *location;
14627
14628 for (location = bpt->loc; location; location = location->next)
14629 target_disable_tracepoint (location);
14630 }
14631
44702360 14632 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14633
8d3788bd 14634 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14635}
14636
51be5b68
PA
14637/* A callback for iterate_over_related_breakpoints. */
14638
14639static void
14640do_disable_breakpoint (struct breakpoint *b, void *ignore)
14641{
14642 disable_breakpoint (b);
14643}
14644
95a42b64
TT
14645/* A callback for map_breakpoint_numbers that calls
14646 disable_breakpoint. */
14647
14648static void
14649do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14650{
51be5b68 14651 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14652}
14653
c906108c 14654static void
fba45db2 14655disable_command (char *args, int from_tty)
c906108c 14656{
c906108c 14657 if (args == 0)
46c6471b
PA
14658 {
14659 struct breakpoint *bpt;
14660
14661 ALL_BREAKPOINTS (bpt)
14662 if (user_breakpoint_p (bpt))
14663 disable_breakpoint (bpt);
14664 }
9eaabc75 14665 else
0d381245 14666 {
9eaabc75
MW
14667 char *num = extract_arg (&args);
14668
14669 while (num)
d248b706 14670 {
9eaabc75 14671 if (strchr (num, '.'))
b775012e 14672 {
9eaabc75
MW
14673 struct bp_location *loc = find_location_by_number (num);
14674
14675 if (loc)
14676 {
14677 if (loc->enabled)
14678 {
14679 loc->enabled = 0;
14680 mark_breakpoint_location_modified (loc);
14681 }
14682 if (target_supports_enable_disable_tracepoint ()
14683 && current_trace_status ()->running && loc->owner
14684 && is_tracepoint (loc->owner))
14685 target_disable_tracepoint (loc);
14686 }
44702360 14687 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14688 }
9eaabc75
MW
14689 else
14690 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14691 num = extract_arg (&args);
d248b706 14692 }
0d381245 14693 }
c906108c
SS
14694}
14695
14696static void
816338b5
SS
14697enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14698 int count)
c906108c 14699{
afe38095 14700 int target_resources_ok;
c906108c
SS
14701
14702 if (bpt->type == bp_hardware_breakpoint)
14703 {
14704 int i;
c5aa993b 14705 i = hw_breakpoint_used_count ();
53a5351d 14706 target_resources_ok =
d92524f1 14707 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14708 i + 1, 0);
c906108c 14709 if (target_resources_ok == 0)
8a3fe4f8 14710 error (_("No hardware breakpoint support in the target."));
c906108c 14711 else if (target_resources_ok < 0)
8a3fe4f8 14712 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14713 }
14714
cc60f2e3 14715 if (is_watchpoint (bpt))
c906108c 14716 {
d07205c2 14717 /* Initialize it just to avoid a GCC false warning. */
f486487f 14718 enum enable_state orig_enable_state = bp_disabled;
dde02812 14719
492d29ea 14720 TRY
c906108c 14721 {
3a5c3e22
PA
14722 struct watchpoint *w = (struct watchpoint *) bpt;
14723
1e718ff1
TJB
14724 orig_enable_state = bpt->enable_state;
14725 bpt->enable_state = bp_enabled;
3a5c3e22 14726 update_watchpoint (w, 1 /* reparse */);
c906108c 14727 }
492d29ea 14728 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14729 {
1e718ff1 14730 bpt->enable_state = orig_enable_state;
dde02812
ES
14731 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14732 bpt->number);
14733 return;
c5aa993b 14734 }
492d29ea 14735 END_CATCH
c906108c 14736 }
0101ce28 14737
b775012e
LM
14738 bpt->enable_state = bp_enabled;
14739
14740 /* Mark breakpoint locations modified. */
14741 mark_breakpoint_modified (bpt);
14742
d248b706
KY
14743 if (target_supports_enable_disable_tracepoint ()
14744 && current_trace_status ()->running && is_tracepoint (bpt))
14745 {
14746 struct bp_location *location;
14747
14748 for (location = bpt->loc; location; location = location->next)
14749 target_enable_tracepoint (location);
14750 }
14751
b4c291bb 14752 bpt->disposition = disposition;
816338b5 14753 bpt->enable_count = count;
44702360 14754 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14755
8d3788bd 14756 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14757}
14758
fe3f5fa8 14759
c906108c 14760void
fba45db2 14761enable_breakpoint (struct breakpoint *bpt)
c906108c 14762{
816338b5 14763 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14764}
14765
14766static void
14767do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14768{
14769 enable_breakpoint (bpt);
c906108c
SS
14770}
14771
95a42b64
TT
14772/* A callback for map_breakpoint_numbers that calls
14773 enable_breakpoint. */
14774
14775static void
14776do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14777{
51be5b68 14778 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14779}
14780
c906108c
SS
14781/* The enable command enables the specified breakpoints (or all defined
14782 breakpoints) so they once again become (or continue to be) effective
1272ad14 14783 in stopping the inferior. */
c906108c 14784
c906108c 14785static void
fba45db2 14786enable_command (char *args, int from_tty)
c906108c 14787{
c906108c 14788 if (args == 0)
46c6471b
PA
14789 {
14790 struct breakpoint *bpt;
14791
14792 ALL_BREAKPOINTS (bpt)
14793 if (user_breakpoint_p (bpt))
14794 enable_breakpoint (bpt);
14795 }
9eaabc75 14796 else
0d381245 14797 {
9eaabc75
MW
14798 char *num = extract_arg (&args);
14799
14800 while (num)
d248b706 14801 {
9eaabc75 14802 if (strchr (num, '.'))
b775012e 14803 {
9eaabc75
MW
14804 struct bp_location *loc = find_location_by_number (num);
14805
14806 if (loc)
14807 {
14808 if (!loc->enabled)
14809 {
14810 loc->enabled = 1;
14811 mark_breakpoint_location_modified (loc);
14812 }
14813 if (target_supports_enable_disable_tracepoint ()
14814 && current_trace_status ()->running && loc->owner
14815 && is_tracepoint (loc->owner))
14816 target_enable_tracepoint (loc);
14817 }
44702360 14818 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14819 }
9eaabc75
MW
14820 else
14821 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14822 num = extract_arg (&args);
d248b706 14823 }
0d381245 14824 }
c906108c
SS
14825}
14826
816338b5
SS
14827/* This struct packages up disposition data for application to multiple
14828 breakpoints. */
14829
14830struct disp_data
14831{
14832 enum bpdisp disp;
14833 int count;
14834};
14835
c906108c 14836static void
51be5b68
PA
14837do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14838{
816338b5 14839 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14840
816338b5 14841 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14842}
14843
14844static void
14845do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14846{
816338b5 14847 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14848
14849 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14850}
14851
c906108c 14852static void
fba45db2 14853enable_once_command (char *args, int from_tty)
c906108c 14854{
51be5b68 14855 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14856}
14857
816338b5
SS
14858static void
14859do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14860{
14861 struct disp_data disp = { disp_disable, *(int *) countptr };
14862
14863 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14864}
14865
14866static void
14867enable_count_command (char *args, int from_tty)
14868{
b9d61307
SM
14869 int count;
14870
14871 if (args == NULL)
14872 error_no_arg (_("hit count"));
14873
14874 count = get_number (&args);
816338b5
SS
14875
14876 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14877}
14878
c906108c 14879static void
51be5b68 14880do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14881{
816338b5 14882 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14883
14884 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14885}
14886
c906108c 14887static void
fba45db2 14888enable_delete_command (char *args, int from_tty)
c906108c 14889{
51be5b68 14890 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14891}
14892\f
fa8d40ab
JJ
14893static void
14894set_breakpoint_cmd (char *args, int from_tty)
14895{
14896}
14897
14898static void
14899show_breakpoint_cmd (char *args, int from_tty)
14900{
14901}
14902
1f3b5d1b
PP
14903/* Invalidate last known value of any hardware watchpoint if
14904 the memory which that value represents has been written to by
14905 GDB itself. */
14906
14907static void
8de0566d
YQ
14908invalidate_bp_value_on_memory_change (struct inferior *inferior,
14909 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14910 const bfd_byte *data)
14911{
14912 struct breakpoint *bp;
14913
14914 ALL_BREAKPOINTS (bp)
14915 if (bp->enable_state == bp_enabled
3a5c3e22 14916 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14917 {
3a5c3e22 14918 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14919
3a5c3e22
PA
14920 if (wp->val_valid && wp->val)
14921 {
14922 struct bp_location *loc;
14923
14924 for (loc = bp->loc; loc != NULL; loc = loc->next)
14925 if (loc->loc_type == bp_loc_hardware_watchpoint
14926 && loc->address + loc->length > addr
14927 && addr + len > loc->address)
14928 {
14929 value_free (wp->val);
14930 wp->val = NULL;
14931 wp->val_valid = 0;
14932 }
14933 }
1f3b5d1b
PP
14934 }
14935}
14936
8181d85f
DJ
14937/* Create and insert a breakpoint for software single step. */
14938
14939void
6c95b8df 14940insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14941 struct address_space *aspace,
14942 CORE_ADDR next_pc)
8181d85f 14943{
7c16b83e
PA
14944 struct thread_info *tp = inferior_thread ();
14945 struct symtab_and_line sal;
14946 CORE_ADDR pc = next_pc;
8181d85f 14947
34b7e8a6
PA
14948 if (tp->control.single_step_breakpoints == NULL)
14949 {
14950 tp->control.single_step_breakpoints
5d5658a1 14951 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14952 }
8181d85f 14953
7c16b83e
PA
14954 sal = find_pc_line (pc, 0);
14955 sal.pc = pc;
14956 sal.section = find_pc_overlay (pc);
14957 sal.explicit_pc = 1;
34b7e8a6 14958 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14959
7c16b83e 14960 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14961}
14962
93f9a11f
YQ
14963/* Insert single step breakpoints according to the current state. */
14964
14965int
14966insert_single_step_breakpoints (struct gdbarch *gdbarch)
14967{
f5ea389a 14968 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14969 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14970
f5ea389a 14971 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14972
a0ff9e1a 14973 if (!next_pcs.empty ())
93f9a11f 14974 {
f5ea389a 14975 struct frame_info *frame = get_current_frame ();
93f9a11f
YQ
14976 struct address_space *aspace = get_frame_address_space (frame);
14977
a0ff9e1a 14978 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14979 insert_single_step_breakpoint (gdbarch, aspace, pc);
14980
93f9a11f
YQ
14981 return 1;
14982 }
14983 else
14984 return 0;
14985}
14986
34b7e8a6 14987/* See breakpoint.h. */
f02253f1
HZ
14988
14989int
7c16b83e
PA
14990breakpoint_has_location_inserted_here (struct breakpoint *bp,
14991 struct address_space *aspace,
14992 CORE_ADDR pc)
1aafd4da 14993{
7c16b83e 14994 struct bp_location *loc;
1aafd4da 14995
7c16b83e
PA
14996 for (loc = bp->loc; loc != NULL; loc = loc->next)
14997 if (loc->inserted
14998 && breakpoint_location_address_match (loc, aspace, pc))
14999 return 1;
1aafd4da 15000
7c16b83e 15001 return 0;
ef370185
JB
15002}
15003
15004/* Check whether a software single-step breakpoint is inserted at
15005 PC. */
15006
15007int
15008single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15009 CORE_ADDR pc)
15010{
34b7e8a6
PA
15011 struct breakpoint *bpt;
15012
15013 ALL_BREAKPOINTS (bpt)
15014 {
15015 if (bpt->type == bp_single_step
15016 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15017 return 1;
15018 }
15019 return 0;
1aafd4da
UW
15020}
15021
1042e4c0
SS
15022/* Tracepoint-specific operations. */
15023
15024/* Set tracepoint count to NUM. */
15025static void
15026set_tracepoint_count (int num)
15027{
15028 tracepoint_count = num;
4fa62494 15029 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15030}
15031
70221824 15032static void
1042e4c0
SS
15033trace_command (char *arg, int from_tty)
15034{
55aa24fb 15035 struct breakpoint_ops *ops;
55aa24fb 15036
ffc2605c
TT
15037 event_location_up location = string_to_event_location (&arg,
15038 current_language);
5b56227b 15039 if (location != NULL
ffc2605c 15040 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
15041 ops = &tracepoint_probe_breakpoint_ops;
15042 else
15043 ops = &tracepoint_breakpoint_ops;
15044
558a9d82 15045 create_breakpoint (get_current_arch (),
ffc2605c 15046 location.get (),
f00aae0f 15047 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15048 0 /* tempflag */,
15049 bp_tracepoint /* type_wanted */,
15050 0 /* Ignore count */,
15051 pending_break_support,
15052 ops,
15053 from_tty,
15054 1 /* enabled */,
15055 0 /* internal */, 0);
1042e4c0
SS
15056}
15057
70221824 15058static void
7a697b8d
SS
15059ftrace_command (char *arg, int from_tty)
15060{
ffc2605c
TT
15061 event_location_up location = string_to_event_location (&arg,
15062 current_language);
558a9d82 15063 create_breakpoint (get_current_arch (),
ffc2605c 15064 location.get (),
f00aae0f 15065 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15066 0 /* tempflag */,
15067 bp_fast_tracepoint /* type_wanted */,
15068 0 /* Ignore count */,
15069 pending_break_support,
15070 &tracepoint_breakpoint_ops,
15071 from_tty,
15072 1 /* enabled */,
15073 0 /* internal */, 0);
0fb4aa4b
PA
15074}
15075
15076/* strace command implementation. Creates a static tracepoint. */
15077
70221824 15078static void
0fb4aa4b
PA
15079strace_command (char *arg, int from_tty)
15080{
983af33b 15081 struct breakpoint_ops *ops;
ffc2605c 15082 event_location_up location;
f00aae0f 15083 struct cleanup *back_to;
983af33b
SDJ
15084
15085 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15086 or with a normal static tracepoint. */
61012eef 15087 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15088 {
15089 ops = &strace_marker_breakpoint_ops;
15090 location = new_linespec_location (&arg);
15091 }
983af33b 15092 else
f00aae0f
KS
15093 {
15094 ops = &tracepoint_breakpoint_ops;
15095 location = string_to_event_location (&arg, current_language);
15096 }
983af33b 15097
558a9d82 15098 create_breakpoint (get_current_arch (),
ffc2605c 15099 location.get (),
f00aae0f 15100 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15101 0 /* tempflag */,
15102 bp_static_tracepoint /* type_wanted */,
15103 0 /* Ignore count */,
15104 pending_break_support,
15105 ops,
15106 from_tty,
15107 1 /* enabled */,
15108 0 /* internal */, 0);
7a697b8d
SS
15109}
15110
409873ef
SS
15111/* Set up a fake reader function that gets command lines from a linked
15112 list that was acquired during tracepoint uploading. */
15113
15114static struct uploaded_tp *this_utp;
3149d8c1 15115static int next_cmd;
409873ef
SS
15116
15117static char *
15118read_uploaded_action (void)
15119{
15120 char *rslt;
15121
3149d8c1 15122 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15123
3149d8c1 15124 next_cmd++;
409873ef
SS
15125
15126 return rslt;
15127}
15128
00bf0b85
SS
15129/* Given information about a tracepoint as recorded on a target (which
15130 can be either a live system or a trace file), attempt to create an
15131 equivalent GDB tracepoint. This is not a reliable process, since
15132 the target does not necessarily have all the information used when
15133 the tracepoint was originally defined. */
15134
d9b3f62e 15135struct tracepoint *
00bf0b85 15136create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15137{
409873ef 15138 char *addr_str, small_buf[100];
d9b3f62e 15139 struct tracepoint *tp;
fd9b8c24 15140
409873ef
SS
15141 if (utp->at_string)
15142 addr_str = utp->at_string;
15143 else
15144 {
15145 /* In the absence of a source location, fall back to raw
15146 address. Since there is no way to confirm that the address
15147 means the same thing as when the trace was started, warn the
15148 user. */
3e43a32a
MS
15149 warning (_("Uploaded tracepoint %d has no "
15150 "source location, using raw address"),
409873ef 15151 utp->number);
8c042590 15152 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15153 addr_str = small_buf;
15154 }
15155
15156 /* There's not much we can do with a sequence of bytecodes. */
15157 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15158 warning (_("Uploaded tracepoint %d condition "
15159 "has no source form, ignoring it"),
409873ef 15160 utp->number);
d5551862 15161
ffc2605c
TT
15162 event_location_up location = string_to_event_location (&addr_str,
15163 current_language);
8cdf0e15 15164 if (!create_breakpoint (get_current_arch (),
ffc2605c 15165 location.get (),
f00aae0f 15166 utp->cond_string, -1, addr_str,
e7e0cddf 15167 0 /* parse cond/thread */,
8cdf0e15 15168 0 /* tempflag */,
0fb4aa4b 15169 utp->type /* type_wanted */,
8cdf0e15
VP
15170 0 /* Ignore count */,
15171 pending_break_support,
348d480f 15172 &tracepoint_breakpoint_ops,
8cdf0e15 15173 0 /* from_tty */,
84f4c1fe 15174 utp->enabled /* enabled */,
44f238bb
PA
15175 0 /* internal */,
15176 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 15177 return NULL;
fd9b8c24 15178
409873ef 15179 /* Get the tracepoint we just created. */
fd9b8c24
PA
15180 tp = get_tracepoint (tracepoint_count);
15181 gdb_assert (tp != NULL);
d5551862 15182
00bf0b85
SS
15183 if (utp->pass > 0)
15184 {
8c042590 15185 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 15186 tp->number);
00bf0b85 15187
409873ef 15188 trace_pass_command (small_buf, 0);
00bf0b85
SS
15189 }
15190
409873ef
SS
15191 /* If we have uploaded versions of the original commands, set up a
15192 special-purpose "reader" function and call the usual command line
15193 reader, then pass the result to the breakpoint command-setting
15194 function. */
3149d8c1 15195 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15196 {
93921405 15197 command_line_up cmd_list;
00bf0b85 15198
409873ef 15199 this_utp = utp;
3149d8c1 15200 next_cmd = 0;
d5551862 15201
409873ef
SS
15202 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15203
c1fc2657 15204 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 15205 }
3149d8c1
SS
15206 else if (!VEC_empty (char_ptr, utp->actions)
15207 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15208 warning (_("Uploaded tracepoint %d actions "
15209 "have no source form, ignoring them"),
409873ef 15210 utp->number);
00bf0b85 15211
f196051f 15212 /* Copy any status information that might be available. */
c1fc2657 15213 tp->hit_count = utp->hit_count;
f196051f
SS
15214 tp->traceframe_usage = utp->traceframe_usage;
15215
00bf0b85 15216 return tp;
d9b3f62e 15217}
00bf0b85 15218
1042e4c0
SS
15219/* Print information on tracepoint number TPNUM_EXP, or all if
15220 omitted. */
15221
15222static void
11db9430 15223info_tracepoints_command (char *args, int from_tty)
1042e4c0 15224{
79a45e25 15225 struct ui_out *uiout = current_uiout;
e5a67952 15226 int num_printed;
1042e4c0 15227
e5a67952 15228 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15229
15230 if (num_printed == 0)
1042e4c0 15231 {
e5a67952 15232 if (args == NULL || *args == '\0')
112e8700 15233 uiout->message ("No tracepoints.\n");
d77f58be 15234 else
112e8700 15235 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 15236 }
ad443146
SS
15237
15238 default_collect_info ();
1042e4c0
SS
15239}
15240
4a64f543 15241/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15242 Not supported by all targets. */
15243static void
15244enable_trace_command (char *args, int from_tty)
15245{
15246 enable_command (args, from_tty);
15247}
15248
4a64f543 15249/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15250 Not supported by all targets. */
15251static void
15252disable_trace_command (char *args, int from_tty)
15253{
15254 disable_command (args, from_tty);
15255}
15256
4a64f543 15257/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15258static void
15259delete_trace_command (char *arg, int from_tty)
15260{
35df4500 15261 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15262
15263 dont_repeat ();
15264
15265 if (arg == 0)
15266 {
15267 int breaks_to_delete = 0;
15268
15269 /* Delete all breakpoints if no argument.
15270 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15271 have to be deleted with an explicit breakpoint number
15272 argument. */
1042e4c0 15273 ALL_TRACEPOINTS (b)
46c6471b 15274 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15275 {
15276 breaks_to_delete = 1;
15277 break;
15278 }
1042e4c0
SS
15279
15280 /* Ask user only if there are some breakpoints to delete. */
15281 if (!from_tty
15282 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15283 {
35df4500 15284 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15285 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15286 delete_breakpoint (b);
1042e4c0
SS
15287 }
15288 }
15289 else
51be5b68 15290 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15291}
15292
197f0a60
TT
15293/* Helper function for trace_pass_command. */
15294
15295static void
d9b3f62e 15296trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15297{
d9b3f62e 15298 tp->pass_count = count;
c1fc2657 15299 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15300 if (from_tty)
15301 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15302 tp->number, count);
197f0a60
TT
15303}
15304
1042e4c0
SS
15305/* Set passcount for tracepoint.
15306
15307 First command argument is passcount, second is tracepoint number.
15308 If tracepoint number omitted, apply to most recently defined.
15309 Also accepts special argument "all". */
15310
15311static void
15312trace_pass_command (char *args, int from_tty)
15313{
d9b3f62e 15314 struct tracepoint *t1;
1042e4c0 15315 unsigned int count;
1042e4c0
SS
15316
15317 if (args == 0 || *args == 0)
3e43a32a
MS
15318 error (_("passcount command requires an "
15319 "argument (count + optional TP num)"));
1042e4c0 15320
4a64f543 15321 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15322
529480d0 15323 args = skip_spaces (args);
1042e4c0
SS
15324 if (*args && strncasecmp (args, "all", 3) == 0)
15325 {
d9b3f62e
PA
15326 struct breakpoint *b;
15327
1042e4c0 15328 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15329 if (*args)
15330 error (_("Junk at end of arguments."));
1042e4c0 15331
d9b3f62e 15332 ALL_TRACEPOINTS (b)
197f0a60 15333 {
d9b3f62e 15334 t1 = (struct tracepoint *) b;
197f0a60
TT
15335 trace_pass_set_count (t1, count, from_tty);
15336 }
15337 }
15338 else if (*args == '\0')
1042e4c0 15339 {
5fa1d40e 15340 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15341 if (t1)
197f0a60
TT
15342 trace_pass_set_count (t1, count, from_tty);
15343 }
15344 else
15345 {
bfd28288
PA
15346 number_or_range_parser parser (args);
15347 while (!parser.finished ())
1042e4c0 15348 {
bfd28288 15349 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15350 if (t1)
15351 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15352 }
15353 }
1042e4c0
SS
15354}
15355
d9b3f62e 15356struct tracepoint *
1042e4c0
SS
15357get_tracepoint (int num)
15358{
15359 struct breakpoint *t;
15360
15361 ALL_TRACEPOINTS (t)
15362 if (t->number == num)
d9b3f62e 15363 return (struct tracepoint *) t;
1042e4c0
SS
15364
15365 return NULL;
15366}
15367
d5551862
SS
15368/* Find the tracepoint with the given target-side number (which may be
15369 different from the tracepoint number after disconnecting and
15370 reconnecting). */
15371
d9b3f62e 15372struct tracepoint *
d5551862
SS
15373get_tracepoint_by_number_on_target (int num)
15374{
d9b3f62e 15375 struct breakpoint *b;
d5551862 15376
d9b3f62e
PA
15377 ALL_TRACEPOINTS (b)
15378 {
15379 struct tracepoint *t = (struct tracepoint *) b;
15380
15381 if (t->number_on_target == num)
15382 return t;
15383 }
d5551862
SS
15384
15385 return NULL;
15386}
15387
1042e4c0 15388/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15389 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15390 If the argument is missing, the most recent tracepoint
15391 (tracepoint_count) is returned. */
15392
d9b3f62e 15393struct tracepoint *
197f0a60 15394get_tracepoint_by_number (char **arg,
bfd28288 15395 number_or_range_parser *parser)
1042e4c0 15396{
1042e4c0
SS
15397 struct breakpoint *t;
15398 int tpnum;
15399 char *instring = arg == NULL ? NULL : *arg;
15400
bfd28288 15401 if (parser != NULL)
197f0a60 15402 {
bfd28288
PA
15403 gdb_assert (!parser->finished ());
15404 tpnum = parser->get_number ();
197f0a60
TT
15405 }
15406 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15407 tpnum = tracepoint_count;
1042e4c0 15408 else
197f0a60 15409 tpnum = get_number (arg);
1042e4c0
SS
15410
15411 if (tpnum <= 0)
15412 {
15413 if (instring && *instring)
15414 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15415 instring);
15416 else
5fa1d40e 15417 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15418 return NULL;
15419 }
15420
15421 ALL_TRACEPOINTS (t)
15422 if (t->number == tpnum)
15423 {
d9b3f62e 15424 return (struct tracepoint *) t;
1042e4c0
SS
15425 }
15426
1042e4c0
SS
15427 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15428 return NULL;
15429}
15430
d9b3f62e
PA
15431void
15432print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15433{
15434 if (b->thread != -1)
15435 fprintf_unfiltered (fp, " thread %d", b->thread);
15436
15437 if (b->task != 0)
15438 fprintf_unfiltered (fp, " task %d", b->task);
15439
15440 fprintf_unfiltered (fp, "\n");
15441}
15442
6149aea9
PA
15443/* Save information on user settable breakpoints (watchpoints, etc) to
15444 a new script file named FILENAME. If FILTER is non-NULL, call it
15445 on each breakpoint and only include the ones for which it returns
15446 non-zero. */
15447
1042e4c0 15448static void
6149aea9
PA
15449save_breakpoints (char *filename, int from_tty,
15450 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15451{
15452 struct breakpoint *tp;
6149aea9 15453 int any = 0;
6149aea9 15454 int extra_trace_bits = 0;
1042e4c0 15455
6149aea9
PA
15456 if (filename == 0 || *filename == 0)
15457 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15458
15459 /* See if we have anything to save. */
6149aea9 15460 ALL_BREAKPOINTS (tp)
1042e4c0 15461 {
6149aea9 15462 /* Skip internal and momentary breakpoints. */
09d682a4 15463 if (!user_breakpoint_p (tp))
6149aea9
PA
15464 continue;
15465
15466 /* If we have a filter, only save the breakpoints it accepts. */
15467 if (filter && !filter (tp))
15468 continue;
15469
15470 any = 1;
15471
15472 if (is_tracepoint (tp))
15473 {
15474 extra_trace_bits = 1;
15475
15476 /* We can stop searching. */
15477 break;
15478 }
1042e4c0 15479 }
6149aea9
PA
15480
15481 if (!any)
1042e4c0 15482 {
6149aea9 15483 warning (_("Nothing to save."));
1042e4c0
SS
15484 return;
15485 }
15486
ee0c3293 15487 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15488
15489 stdio_file fp;
15490
ee0c3293 15491 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15492 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15493 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15494
6149aea9 15495 if (extra_trace_bits)
d7e74731 15496 save_trace_state_variables (&fp);
8bf6485c 15497
6149aea9 15498 ALL_BREAKPOINTS (tp)
1042e4c0 15499 {
6149aea9 15500 /* Skip internal and momentary breakpoints. */
09d682a4 15501 if (!user_breakpoint_p (tp))
6149aea9 15502 continue;
8bf6485c 15503
6149aea9
PA
15504 /* If we have a filter, only save the breakpoints it accepts. */
15505 if (filter && !filter (tp))
15506 continue;
15507
d7e74731 15508 tp->ops->print_recreate (tp, &fp);
1042e4c0 15509
6149aea9
PA
15510 /* Note, we can't rely on tp->number for anything, as we can't
15511 assume the recreated breakpoint numbers will match. Use $bpnum
15512 instead. */
15513
15514 if (tp->cond_string)
d7e74731 15515 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15516
15517 if (tp->ignore_count)
d7e74731 15518 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15519
2d9442cc 15520 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15521 {
d7e74731 15522 fp.puts (" commands\n");
a7bdde9e 15523
d7e74731 15524 current_uiout->redirect (&fp);
492d29ea 15525 TRY
1042e4c0 15526 {
79a45e25 15527 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15528 }
492d29ea
PA
15529 CATCH (ex, RETURN_MASK_ALL)
15530 {
112e8700 15531 current_uiout->redirect (NULL);
492d29ea
PA
15532 throw_exception (ex);
15533 }
15534 END_CATCH
1042e4c0 15535
112e8700 15536 current_uiout->redirect (NULL);
d7e74731 15537 fp.puts (" end\n");
1042e4c0 15538 }
6149aea9
PA
15539
15540 if (tp->enable_state == bp_disabled)
d7e74731 15541 fp.puts ("disable $bpnum\n");
6149aea9
PA
15542
15543 /* If this is a multi-location breakpoint, check if the locations
15544 should be individually disabled. Watchpoint locations are
15545 special, and not user visible. */
15546 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15547 {
15548 struct bp_location *loc;
15549 int n = 1;
15550
15551 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15552 if (!loc->enabled)
d7e74731 15553 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15554 }
1042e4c0 15555 }
8bf6485c 15556
6149aea9 15557 if (extra_trace_bits && *default_collect)
d7e74731 15558 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15559
1042e4c0 15560 if (from_tty)
ee0c3293 15561 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15562}
15563
15564/* The `save breakpoints' command. */
15565
15566static void
15567save_breakpoints_command (char *args, int from_tty)
15568{
15569 save_breakpoints (args, from_tty, NULL);
15570}
15571
15572/* The `save tracepoints' command. */
15573
15574static void
15575save_tracepoints_command (char *args, int from_tty)
15576{
15577 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15578}
15579
15580/* Create a vector of all tracepoints. */
15581
15582VEC(breakpoint_p) *
eeae04df 15583all_tracepoints (void)
1042e4c0
SS
15584{
15585 VEC(breakpoint_p) *tp_vec = 0;
15586 struct breakpoint *tp;
15587
15588 ALL_TRACEPOINTS (tp)
15589 {
15590 VEC_safe_push (breakpoint_p, tp_vec, tp);
15591 }
15592
15593 return tp_vec;
15594}
15595
c906108c 15596\f
629500fa
KS
15597/* This help string is used to consolidate all the help string for specifying
15598 locations used by several commands. */
15599
15600#define LOCATION_HELP_STRING \
15601"Linespecs are colon-separated lists of location parameters, such as\n\
15602source filename, function name, label name, and line number.\n\
15603Example: To specify the start of a label named \"the_top\" in the\n\
15604function \"fact\" in the file \"factorial.c\", use\n\
15605\"factorial.c:fact:the_top\".\n\
15606\n\
15607Address locations begin with \"*\" and specify an exact address in the\n\
15608program. Example: To specify the fourth byte past the start function\n\
15609\"main\", use \"*main + 4\".\n\
15610\n\
15611Explicit locations are similar to linespecs but use an option/argument\n\
15612syntax to specify location parameters.\n\
15613Example: To specify the start of the label named \"the_top\" in the\n\
15614function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15615-function fact -label the_top\".\n"
15616
4a64f543
MS
15617/* This help string is used for the break, hbreak, tbreak and thbreak
15618 commands. It is defined as a macro to prevent duplication.
15619 COMMAND should be a string constant containing the name of the
15620 command. */
629500fa 15621
31e2b00f 15622#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15623command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15624PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15625probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15626guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15627`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15628LOCATION may be a linespec, address, or explicit location as described\n\
15629below.\n\
15630\n\
dc10affe
PA
15631With no LOCATION, uses current execution address of the selected\n\
15632stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15633\n\
15634THREADNUM is the number from \"info threads\".\n\
15635CONDITION is a boolean expression.\n\
629500fa 15636\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15637Multiple breakpoints at one place are permitted, and useful if their\n\
15638conditions are different.\n\
31e2b00f
AS
15639\n\
15640Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15641
44feb3ce
TT
15642/* List of subcommands for "catch". */
15643static struct cmd_list_element *catch_cmdlist;
15644
15645/* List of subcommands for "tcatch". */
15646static struct cmd_list_element *tcatch_cmdlist;
15647
9ac4176b 15648void
a121b7c1 15649add_catch_command (const char *name, const char *docstring,
82ae6c8d 15650 cmd_sfunc_ftype *sfunc,
625e8578 15651 completer_ftype *completer,
44feb3ce
TT
15652 void *user_data_catch,
15653 void *user_data_tcatch)
15654{
15655 struct cmd_list_element *command;
15656
15657 command = add_cmd (name, class_breakpoint, NULL, docstring,
15658 &catch_cmdlist);
15659 set_cmd_sfunc (command, sfunc);
15660 set_cmd_context (command, user_data_catch);
a96d9b2e 15661 set_cmd_completer (command, completer);
44feb3ce
TT
15662
15663 command = add_cmd (name, class_breakpoint, NULL, docstring,
15664 &tcatch_cmdlist);
15665 set_cmd_sfunc (command, sfunc);
15666 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15667 set_cmd_completer (command, completer);
44feb3ce
TT
15668}
15669
6149aea9
PA
15670static void
15671save_command (char *arg, int from_tty)
15672{
3e43a32a
MS
15673 printf_unfiltered (_("\"save\" must be followed by "
15674 "the name of a save subcommand.\n"));
635c7e8a 15675 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15676}
15677
84f4c1fe
PM
15678struct breakpoint *
15679iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15680 void *data)
15681{
35df4500 15682 struct breakpoint *b, *b_tmp;
84f4c1fe 15683
35df4500 15684 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15685 {
15686 if ((*callback) (b, data))
15687 return b;
15688 }
15689
15690 return NULL;
15691}
15692
0574c78f
GB
15693/* Zero if any of the breakpoint's locations could be a location where
15694 functions have been inlined, nonzero otherwise. */
15695
15696static int
15697is_non_inline_function (struct breakpoint *b)
15698{
15699 /* The shared library event breakpoint is set on the address of a
15700 non-inline function. */
15701 if (b->type == bp_shlib_event)
15702 return 1;
15703
15704 return 0;
15705}
15706
15707/* Nonzero if the specified PC cannot be a location where functions
15708 have been inlined. */
15709
15710int
09ac7c10
TT
15711pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15712 const struct target_waitstatus *ws)
0574c78f
GB
15713{
15714 struct breakpoint *b;
15715 struct bp_location *bl;
15716
15717 ALL_BREAKPOINTS (b)
15718 {
15719 if (!is_non_inline_function (b))
15720 continue;
15721
15722 for (bl = b->loc; bl != NULL; bl = bl->next)
15723 {
15724 if (!bl->shlib_disabled
09ac7c10 15725 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15726 return 1;
15727 }
15728 }
15729
15730 return 0;
15731}
15732
2f202fde
JK
15733/* Remove any references to OBJFILE which is going to be freed. */
15734
15735void
15736breakpoint_free_objfile (struct objfile *objfile)
15737{
15738 struct bp_location **locp, *loc;
15739
15740 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15741 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15742 loc->symtab = NULL;
15743}
15744
2060206e
PA
15745void
15746initialize_breakpoint_ops (void)
15747{
15748 static int initialized = 0;
15749
15750 struct breakpoint_ops *ops;
15751
15752 if (initialized)
15753 return;
15754 initialized = 1;
15755
15756 /* The breakpoint_ops structure to be inherit by all kinds of
15757 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15758 internal and momentary breakpoints, etc.). */
15759 ops = &bkpt_base_breakpoint_ops;
15760 *ops = base_breakpoint_ops;
15761 ops->re_set = bkpt_re_set;
15762 ops->insert_location = bkpt_insert_location;
15763 ops->remove_location = bkpt_remove_location;
15764 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15765 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15766 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15767 ops->decode_location = bkpt_decode_location;
2060206e
PA
15768
15769 /* The breakpoint_ops structure to be used in regular breakpoints. */
15770 ops = &bkpt_breakpoint_ops;
15771 *ops = bkpt_base_breakpoint_ops;
15772 ops->re_set = bkpt_re_set;
15773 ops->resources_needed = bkpt_resources_needed;
15774 ops->print_it = bkpt_print_it;
15775 ops->print_mention = bkpt_print_mention;
15776 ops->print_recreate = bkpt_print_recreate;
15777
15778 /* Ranged breakpoints. */
15779 ops = &ranged_breakpoint_ops;
15780 *ops = bkpt_breakpoint_ops;
15781 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15782 ops->resources_needed = resources_needed_ranged_breakpoint;
15783 ops->print_it = print_it_ranged_breakpoint;
15784 ops->print_one = print_one_ranged_breakpoint;
15785 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15786 ops->print_mention = print_mention_ranged_breakpoint;
15787 ops->print_recreate = print_recreate_ranged_breakpoint;
15788
15789 /* Internal breakpoints. */
15790 ops = &internal_breakpoint_ops;
15791 *ops = bkpt_base_breakpoint_ops;
15792 ops->re_set = internal_bkpt_re_set;
15793 ops->check_status = internal_bkpt_check_status;
15794 ops->print_it = internal_bkpt_print_it;
15795 ops->print_mention = internal_bkpt_print_mention;
15796
15797 /* Momentary breakpoints. */
15798 ops = &momentary_breakpoint_ops;
15799 *ops = bkpt_base_breakpoint_ops;
15800 ops->re_set = momentary_bkpt_re_set;
15801 ops->check_status = momentary_bkpt_check_status;
15802 ops->print_it = momentary_bkpt_print_it;
15803 ops->print_mention = momentary_bkpt_print_mention;
15804
55aa24fb
SDJ
15805 /* Probe breakpoints. */
15806 ops = &bkpt_probe_breakpoint_ops;
15807 *ops = bkpt_breakpoint_ops;
15808 ops->insert_location = bkpt_probe_insert_location;
15809 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15810 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15811 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15812
2060206e
PA
15813 /* Watchpoints. */
15814 ops = &watchpoint_breakpoint_ops;
15815 *ops = base_breakpoint_ops;
15816 ops->re_set = re_set_watchpoint;
15817 ops->insert_location = insert_watchpoint;
15818 ops->remove_location = remove_watchpoint;
15819 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15820 ops->check_status = check_status_watchpoint;
15821 ops->resources_needed = resources_needed_watchpoint;
15822 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15823 ops->print_it = print_it_watchpoint;
15824 ops->print_mention = print_mention_watchpoint;
15825 ops->print_recreate = print_recreate_watchpoint;
427cd150 15826 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15827
15828 /* Masked watchpoints. */
15829 ops = &masked_watchpoint_breakpoint_ops;
15830 *ops = watchpoint_breakpoint_ops;
15831 ops->insert_location = insert_masked_watchpoint;
15832 ops->remove_location = remove_masked_watchpoint;
15833 ops->resources_needed = resources_needed_masked_watchpoint;
15834 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15835 ops->print_it = print_it_masked_watchpoint;
15836 ops->print_one_detail = print_one_detail_masked_watchpoint;
15837 ops->print_mention = print_mention_masked_watchpoint;
15838 ops->print_recreate = print_recreate_masked_watchpoint;
15839
15840 /* Tracepoints. */
15841 ops = &tracepoint_breakpoint_ops;
15842 *ops = base_breakpoint_ops;
15843 ops->re_set = tracepoint_re_set;
15844 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15845 ops->print_one_detail = tracepoint_print_one_detail;
15846 ops->print_mention = tracepoint_print_mention;
15847 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15848 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15849 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15850 ops->decode_location = tracepoint_decode_location;
983af33b 15851
55aa24fb
SDJ
15852 /* Probe tracepoints. */
15853 ops = &tracepoint_probe_breakpoint_ops;
15854 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15855 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15856 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15857
983af33b
SDJ
15858 /* Static tracepoints with marker (`-m'). */
15859 ops = &strace_marker_breakpoint_ops;
15860 *ops = tracepoint_breakpoint_ops;
5f700d83 15861 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15862 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15863 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15864
15865 /* Fork catchpoints. */
15866 ops = &catch_fork_breakpoint_ops;
15867 *ops = base_breakpoint_ops;
15868 ops->insert_location = insert_catch_fork;
15869 ops->remove_location = remove_catch_fork;
15870 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15871 ops->print_it = print_it_catch_fork;
15872 ops->print_one = print_one_catch_fork;
15873 ops->print_mention = print_mention_catch_fork;
15874 ops->print_recreate = print_recreate_catch_fork;
15875
15876 /* Vfork catchpoints. */
15877 ops = &catch_vfork_breakpoint_ops;
15878 *ops = base_breakpoint_ops;
15879 ops->insert_location = insert_catch_vfork;
15880 ops->remove_location = remove_catch_vfork;
15881 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15882 ops->print_it = print_it_catch_vfork;
15883 ops->print_one = print_one_catch_vfork;
15884 ops->print_mention = print_mention_catch_vfork;
15885 ops->print_recreate = print_recreate_catch_vfork;
15886
15887 /* Exec catchpoints. */
15888 ops = &catch_exec_breakpoint_ops;
15889 *ops = base_breakpoint_ops;
2060206e
PA
15890 ops->insert_location = insert_catch_exec;
15891 ops->remove_location = remove_catch_exec;
15892 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15893 ops->print_it = print_it_catch_exec;
15894 ops->print_one = print_one_catch_exec;
15895 ops->print_mention = print_mention_catch_exec;
15896 ops->print_recreate = print_recreate_catch_exec;
15897
edcc5120
TT
15898 /* Solib-related catchpoints. */
15899 ops = &catch_solib_breakpoint_ops;
15900 *ops = base_breakpoint_ops;
edcc5120
TT
15901 ops->insert_location = insert_catch_solib;
15902 ops->remove_location = remove_catch_solib;
15903 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15904 ops->check_status = check_status_catch_solib;
15905 ops->print_it = print_it_catch_solib;
15906 ops->print_one = print_one_catch_solib;
15907 ops->print_mention = print_mention_catch_solib;
15908 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15909
15910 ops = &dprintf_breakpoint_ops;
15911 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15912 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15913 ops->resources_needed = bkpt_resources_needed;
15914 ops->print_it = bkpt_print_it;
15915 ops->print_mention = bkpt_print_mention;
2d9442cc 15916 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15917 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15918 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15919}
15920
8bfd80db
YQ
15921/* Chain containing all defined "enable breakpoint" subcommands. */
15922
15923static struct cmd_list_element *enablebreaklist = NULL;
15924
c906108c 15925void
fba45db2 15926_initialize_breakpoint (void)
c906108c
SS
15927{
15928 struct cmd_list_element *c;
15929
2060206e
PA
15930 initialize_breakpoint_ops ();
15931
84acb35a 15932 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15933 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15934 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15935
55aa24fb
SDJ
15936 breakpoint_objfile_key
15937 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15938
c906108c
SS
15939 breakpoint_chain = 0;
15940 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15941 before a breakpoint is set. */
15942 breakpoint_count = 0;
15943
1042e4c0
SS
15944 tracepoint_count = 0;
15945
1bedd215
AC
15946 add_com ("ignore", class_breakpoint, ignore_command, _("\
15947Set ignore-count of breakpoint number N to COUNT.\n\
15948Usage is `ignore N COUNT'."));
c906108c 15949
1bedd215 15950 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15951Set commands to be executed when the given breakpoints are hit.\n\
15952Give a space-separated breakpoint list as argument after \"commands\".\n\
15953A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15954(e.g. `5-7').\n\
c906108c
SS
15955With no argument, the targeted breakpoint is the last one set.\n\
15956The commands themselves follow starting on the next line.\n\
15957Type a line containing \"end\" to indicate the end of them.\n\
15958Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15959then no output is printed when it is hit, except what the commands print."));
c906108c 15960
d55637df 15961 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15962Specify breakpoint number N to break only if COND is true.\n\
c906108c 15963Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15964expression to be evaluated whenever breakpoint N is reached."));
d55637df 15965 set_cmd_completer (c, condition_completer);
c906108c 15966
1bedd215 15967 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15968Set a temporary breakpoint.\n\
c906108c
SS
15969Like \"break\" except the breakpoint is only temporary,\n\
15970so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15971by using \"enable delete\" on the breakpoint number.\n\
15972\n"
15973BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15974 set_cmd_completer (c, location_completer);
c94fdfd0 15975
1bedd215 15976 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15977Set a hardware assisted breakpoint.\n\
c906108c 15978Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15979some target hardware may not have this support.\n\
15980\n"
15981BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15982 set_cmd_completer (c, location_completer);
c906108c 15983
1bedd215 15984 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15985Set a temporary hardware assisted breakpoint.\n\
c906108c 15986Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15987so it will be deleted when hit.\n\
15988\n"
15989BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15990 set_cmd_completer (c, location_completer);
c906108c 15991
1bedd215
AC
15992 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15993Enable some breakpoints.\n\
c906108c
SS
15994Give breakpoint numbers (separated by spaces) as arguments.\n\
15995With no subcommand, breakpoints are enabled until you command otherwise.\n\
15996This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15997With a subcommand you can enable temporarily."),
c906108c 15998 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15999
16000 add_com_alias ("en", "enable", class_breakpoint, 1);
16001
84951ab5 16002 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16003Enable some breakpoints.\n\
c906108c
SS
16004Give breakpoint numbers (separated by spaces) as arguments.\n\
16005This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16006May be abbreviated to simply \"enable\".\n"),
c5aa993b 16007 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16008
1a966eab
AC
16009 add_cmd ("once", no_class, enable_once_command, _("\
16010Enable breakpoints for one hit. Give breakpoint numbers.\n\
16011If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16012 &enablebreaklist);
16013
1a966eab
AC
16014 add_cmd ("delete", no_class, enable_delete_command, _("\
16015Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16016If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16017 &enablebreaklist);
16018
816338b5
SS
16019 add_cmd ("count", no_class, enable_count_command, _("\
16020Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16021If a breakpoint is hit while enabled in this fashion,\n\
16022the count is decremented; when it reaches zero, the breakpoint is disabled."),
16023 &enablebreaklist);
16024
1a966eab
AC
16025 add_cmd ("delete", no_class, enable_delete_command, _("\
16026Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16027If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16028 &enablelist);
16029
1a966eab
AC
16030 add_cmd ("once", no_class, enable_once_command, _("\
16031Enable breakpoints for one hit. Give breakpoint numbers.\n\
16032If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16033 &enablelist);
16034
16035 add_cmd ("count", no_class, enable_count_command, _("\
16036Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16037If a breakpoint is hit while enabled in this fashion,\n\
16038the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16039 &enablelist);
16040
1bedd215
AC
16041 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16042Disable some breakpoints.\n\
c906108c
SS
16043Arguments are breakpoint numbers with spaces in between.\n\
16044To disable all breakpoints, give no argument.\n\
64b9b334 16045A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16046 &disablelist, "disable ", 1, &cmdlist);
16047 add_com_alias ("dis", "disable", class_breakpoint, 1);
16048 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16049
1a966eab
AC
16050 add_cmd ("breakpoints", class_alias, disable_command, _("\
16051Disable some breakpoints.\n\
c906108c
SS
16052Arguments are breakpoint numbers with spaces in between.\n\
16053To disable all breakpoints, give no argument.\n\
64b9b334 16054A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16055This command may be abbreviated \"disable\"."),
c906108c
SS
16056 &disablelist);
16057
1bedd215
AC
16058 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16059Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16060Arguments are breakpoint numbers with spaces in between.\n\
16061To delete all breakpoints, give no argument.\n\
16062\n\
16063Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16064The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16065 &deletelist, "delete ", 1, &cmdlist);
16066 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16067 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16068
1a966eab
AC
16069 add_cmd ("breakpoints", class_alias, delete_command, _("\
16070Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16071Arguments are breakpoint numbers with spaces in between.\n\
16072To delete all breakpoints, give no argument.\n\
1a966eab 16073This command may be abbreviated \"delete\"."),
c906108c
SS
16074 &deletelist);
16075
1bedd215 16076 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16077Clear breakpoint at specified location.\n\
16078Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16079\n\
16080With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16081is executing in.\n"
16082"\n" LOCATION_HELP_STRING "\n\
1bedd215 16083See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16084 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16085
1bedd215 16086 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16087Set breakpoint at specified location.\n"
31e2b00f 16088BREAK_ARGS_HELP ("break")));
5ba2abeb 16089 set_cmd_completer (c, location_completer);
c94fdfd0 16090
c906108c
SS
16091 add_com_alias ("b", "break", class_run, 1);
16092 add_com_alias ("br", "break", class_run, 1);
16093 add_com_alias ("bre", "break", class_run, 1);
16094 add_com_alias ("brea", "break", class_run, 1);
16095
c906108c
SS
16096 if (dbx_commands)
16097 {
1bedd215
AC
16098 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16099Break in function/address or break at a line in the current file."),
c5aa993b
JM
16100 &stoplist, "stop ", 1, &cmdlist);
16101 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16102 _("Break in function or address."), &stoplist);
c5aa993b 16103 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16104 _("Break at a line in the current file."), &stoplist);
11db9430 16105 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 16106Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16107The \"Type\" column indicates one of:\n\
16108\tbreakpoint - normal breakpoint\n\
16109\twatchpoint - watchpoint\n\
16110The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16111the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16112breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16113address and file/line number respectively.\n\
16114\n\
16115Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16116are set to the address of the last breakpoint listed unless the command\n\
16117is prefixed with \"server \".\n\n\
c906108c 16118Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16119breakpoint set."));
c906108c
SS
16120 }
16121
11db9430 16122 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 16123Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16124The \"Type\" column indicates one of:\n\
16125\tbreakpoint - normal breakpoint\n\
16126\twatchpoint - watchpoint\n\
16127The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16128the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16129breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16130address and file/line number respectively.\n\
16131\n\
16132Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16133are set to the address of the last breakpoint listed unless the command\n\
16134is prefixed with \"server \".\n\n\
c906108c 16135Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16136breakpoint set."));
c906108c 16137
6b04bdb7
MS
16138 add_info_alias ("b", "breakpoints", 1);
16139
1a966eab
AC
16140 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16141Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16142The \"Type\" column indicates one of:\n\
16143\tbreakpoint - normal breakpoint\n\
16144\twatchpoint - watchpoint\n\
16145\tlongjmp - internal breakpoint used to step through longjmp()\n\
16146\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16147\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16148\tfinish - internal breakpoint used by the \"finish\" command\n\
16149The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16150the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16151breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16152address and file/line number respectively.\n\
16153\n\
16154Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16155are set to the address of the last breakpoint listed unless the command\n\
16156is prefixed with \"server \".\n\n\
c906108c 16157Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16158breakpoint set."),
c906108c
SS
16159 &maintenanceinfolist);
16160
44feb3ce
TT
16161 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16162Set catchpoints to catch events."),
16163 &catch_cmdlist, "catch ",
16164 0/*allow-unknown*/, &cmdlist);
16165
16166 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16167Set temporary catchpoints to catch events."),
16168 &tcatch_cmdlist, "tcatch ",
16169 0/*allow-unknown*/, &cmdlist);
16170
44feb3ce
TT
16171 add_catch_command ("fork", _("Catch calls to fork."),
16172 catch_fork_command_1,
a96d9b2e 16173 NULL,
44feb3ce
TT
16174 (void *) (uintptr_t) catch_fork_permanent,
16175 (void *) (uintptr_t) catch_fork_temporary);
16176 add_catch_command ("vfork", _("Catch calls to vfork."),
16177 catch_fork_command_1,
a96d9b2e 16178 NULL,
44feb3ce
TT
16179 (void *) (uintptr_t) catch_vfork_permanent,
16180 (void *) (uintptr_t) catch_vfork_temporary);
16181 add_catch_command ("exec", _("Catch calls to exec."),
16182 catch_exec_command_1,
a96d9b2e
SDJ
16183 NULL,
16184 CATCH_PERMANENT,
16185 CATCH_TEMPORARY);
edcc5120
TT
16186 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16187Usage: catch load [REGEX]\n\
16188If REGEX is given, only stop for libraries matching the regular expression."),
16189 catch_load_command_1,
16190 NULL,
16191 CATCH_PERMANENT,
16192 CATCH_TEMPORARY);
16193 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16194Usage: catch unload [REGEX]\n\
16195If REGEX is given, only stop for libraries matching the regular expression."),
16196 catch_unload_command_1,
16197 NULL,
16198 CATCH_PERMANENT,
16199 CATCH_TEMPORARY);
c5aa993b 16200
1bedd215
AC
16201 c = add_com ("watch", class_breakpoint, watch_command, _("\
16202Set a watchpoint for an expression.\n\
06a64a0b 16203Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16204A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16205an expression changes.\n\
16206If -l or -location is given, this evaluates EXPRESSION and watches\n\
16207the memory to which it refers."));
65d12d83 16208 set_cmd_completer (c, expression_completer);
c906108c 16209
1bedd215
AC
16210 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16211Set a read watchpoint for an expression.\n\
06a64a0b 16212Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16213A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16214an expression is read.\n\
16215If -l or -location is given, this evaluates EXPRESSION and watches\n\
16216the memory to which it refers."));
65d12d83 16217 set_cmd_completer (c, expression_completer);
c906108c 16218
1bedd215
AC
16219 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16220Set a watchpoint for an expression.\n\
06a64a0b 16221Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16222A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16223an expression is either read or written.\n\
16224If -l or -location is given, this evaluates EXPRESSION and watches\n\
16225the memory to which it refers."));
65d12d83 16226 set_cmd_completer (c, expression_completer);
c906108c 16227
11db9430 16228 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 16229Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16230
920d2a44
AC
16231 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16232 respond to changes - contrary to the description. */
85c07804
AC
16233 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16234 &can_use_hw_watchpoints, _("\
16235Set debugger's willingness to use watchpoint hardware."), _("\
16236Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16237If zero, gdb will not use hardware for new watchpoints, even if\n\
16238such is available. (However, any hardware watchpoints that were\n\
16239created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16240hardware.)"),
16241 NULL,
920d2a44 16242 show_can_use_hw_watchpoints,
85c07804 16243 &setlist, &showlist);
c906108c
SS
16244
16245 can_use_hw_watchpoints = 1;
fa8d40ab 16246
1042e4c0
SS
16247 /* Tracepoint manipulation commands. */
16248
16249 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16250Set a tracepoint at specified location.\n\
1042e4c0
SS
16251\n"
16252BREAK_ARGS_HELP ("trace") "\n\
16253Do \"help tracepoints\" for info on other tracepoint commands."));
16254 set_cmd_completer (c, location_completer);
16255
16256 add_com_alias ("tp", "trace", class_alias, 0);
16257 add_com_alias ("tr", "trace", class_alias, 1);
16258 add_com_alias ("tra", "trace", class_alias, 1);
16259 add_com_alias ("trac", "trace", class_alias, 1);
16260
7a697b8d 16261 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16262Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16263\n"
16264BREAK_ARGS_HELP ("ftrace") "\n\
16265Do \"help tracepoints\" for info on other tracepoint commands."));
16266 set_cmd_completer (c, location_completer);
16267
0fb4aa4b 16268 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16269Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16270\n\
16271strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16272LOCATION may be a linespec, explicit, or address location (described below) \n\
16273or -m MARKER_ID.\n\n\
16274If a marker id is specified, probe the marker with that name. With\n\
16275no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16276Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16277This collects arbitrary user data passed in the probe point call to the\n\
16278tracing library. You can inspect it when analyzing the trace buffer,\n\
16279by printing the $_sdata variable like any other convenience variable.\n\
16280\n\
16281CONDITION is a boolean expression.\n\
629500fa 16282\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16283Multiple tracepoints at one place are permitted, and useful if their\n\
16284conditions are different.\n\
0fb4aa4b
PA
16285\n\
16286Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16287Do \"help tracepoints\" for info on other tracepoint commands."));
16288 set_cmd_completer (c, location_completer);
16289
11db9430 16290 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 16291Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16292Convenience variable \"$tpnum\" contains the number of the\n\
16293last tracepoint set."));
16294
16295 add_info_alias ("tp", "tracepoints", 1);
16296
16297 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16298Delete specified tracepoints.\n\
16299Arguments are tracepoint numbers, separated by spaces.\n\
16300No argument means delete all tracepoints."),
16301 &deletelist);
7e20dfcd 16302 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16303
16304 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16305Disable specified tracepoints.\n\
16306Arguments are tracepoint numbers, separated by spaces.\n\
16307No argument means disable all tracepoints."),
16308 &disablelist);
16309 deprecate_cmd (c, "disable");
16310
16311 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16312Enable specified tracepoints.\n\
16313Arguments are tracepoint numbers, separated by spaces.\n\
16314No argument means enable all tracepoints."),
16315 &enablelist);
16316 deprecate_cmd (c, "enable");
16317
16318 add_com ("passcount", class_trace, trace_pass_command, _("\
16319Set the passcount for a tracepoint.\n\
16320The trace will end when the tracepoint has been passed 'count' times.\n\
16321Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16322if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16323
6149aea9
PA
16324 add_prefix_cmd ("save", class_breakpoint, save_command,
16325 _("Save breakpoint definitions as a script."),
16326 &save_cmdlist, "save ",
16327 0/*allow-unknown*/, &cmdlist);
16328
16329 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16330Save current breakpoint definitions as a script.\n\
cce7e648 16331This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16332catchpoints, tracepoints). Use the 'source' command in another debug\n\
16333session to restore them."),
16334 &save_cmdlist);
16335 set_cmd_completer (c, filename_completer);
16336
16337 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16338Save current tracepoint definitions as a script.\n\
6149aea9
PA
16339Use the 'source' command in another debug session to restore them."),
16340 &save_cmdlist);
1042e4c0
SS
16341 set_cmd_completer (c, filename_completer);
16342
6149aea9
PA
16343 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16344 deprecate_cmd (c, "save tracepoints");
16345
1bedd215 16346 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16347Breakpoint specific settings\n\
16348Configure various breakpoint-specific variables such as\n\
1bedd215 16349pending breakpoint behavior"),
fa8d40ab
JJ
16350 &breakpoint_set_cmdlist, "set breakpoint ",
16351 0/*allow-unknown*/, &setlist);
1bedd215 16352 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16353Breakpoint specific settings\n\
16354Configure various breakpoint-specific variables such as\n\
1bedd215 16355pending breakpoint behavior"),
fa8d40ab
JJ
16356 &breakpoint_show_cmdlist, "show breakpoint ",
16357 0/*allow-unknown*/, &showlist);
16358
7915a72c
AC
16359 add_setshow_auto_boolean_cmd ("pending", no_class,
16360 &pending_break_support, _("\
16361Set debugger's behavior regarding pending breakpoints."), _("\
16362Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16363If on, an unrecognized breakpoint location will cause gdb to create a\n\
16364pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16365an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16366user-query to see if a pending breakpoint should be created."),
2c5b56ce 16367 NULL,
920d2a44 16368 show_pending_break_support,
6e1d7d6c
AC
16369 &breakpoint_set_cmdlist,
16370 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16371
16372 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16373
16374 add_setshow_boolean_cmd ("auto-hw", no_class,
16375 &automatic_hardware_breakpoints, _("\
16376Set automatic usage of hardware breakpoints."), _("\
16377Show automatic usage of hardware breakpoints."), _("\
16378If set, the debugger will automatically use hardware breakpoints for\n\
16379breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16380a warning will be emitted for such breakpoints."),
16381 NULL,
16382 show_automatic_hardware_breakpoints,
16383 &breakpoint_set_cmdlist,
16384 &breakpoint_show_cmdlist);
74960c60 16385
a25a5a45
PA
16386 add_setshow_boolean_cmd ("always-inserted", class_support,
16387 &always_inserted_mode, _("\
74960c60
VP
16388Set mode for inserting breakpoints."), _("\
16389Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16390When this mode is on, breakpoints are inserted immediately as soon as\n\
16391they're created, kept inserted even when execution stops, and removed\n\
16392only when the user deletes them. When this mode is off (the default),\n\
16393breakpoints are inserted only when execution continues, and removed\n\
16394when execution stops."),
72d0e2c5
YQ
16395 NULL,
16396 &show_always_inserted_mode,
16397 &breakpoint_set_cmdlist,
16398 &breakpoint_show_cmdlist);
f1310107 16399
b775012e
LM
16400 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16401 condition_evaluation_enums,
16402 &condition_evaluation_mode_1, _("\
16403Set mode of breakpoint condition evaluation."), _("\
16404Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16405When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16406evaluated on the host's side by GDB. When it is set to \"target\",\n\
16407breakpoint conditions will be downloaded to the target (if the target\n\
16408supports such feature) and conditions will be evaluated on the target's side.\n\
16409If this is set to \"auto\" (default), this will be automatically set to\n\
16410\"target\" if it supports condition evaluation, otherwise it will\n\
16411be set to \"gdb\""),
16412 &set_condition_evaluation_mode,
16413 &show_condition_evaluation_mode,
16414 &breakpoint_set_cmdlist,
16415 &breakpoint_show_cmdlist);
16416
f1310107
TJB
16417 add_com ("break-range", class_breakpoint, break_range_command, _("\
16418Set a breakpoint for an address range.\n\
16419break-range START-LOCATION, END-LOCATION\n\
16420where START-LOCATION and END-LOCATION can be one of the following:\n\
16421 LINENUM, for that line in the current file,\n\
16422 FILE:LINENUM, for that line in that file,\n\
16423 +OFFSET, for that number of lines after the current line\n\
16424 or the start of the range\n\
16425 FUNCTION, for the first line in that function,\n\
16426 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16427 *ADDRESS, for the instruction at that address.\n\
16428\n\
16429The breakpoint will stop execution of the inferior whenever it executes\n\
16430an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16431range (including START-LOCATION and END-LOCATION)."));
16432
e7e0cddf 16433 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16434Set a dynamic printf at specified location.\n\
e7e0cddf 16435dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16436location may be a linespec, explicit, or address location.\n"
16437"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16438 set_cmd_completer (c, location_completer);
16439
16440 add_setshow_enum_cmd ("dprintf-style", class_support,
16441 dprintf_style_enums, &dprintf_style, _("\
16442Set the style of usage for dynamic printf."), _("\
16443Show the style of usage for dynamic printf."), _("\
16444This setting chooses how GDB will do a dynamic printf.\n\
16445If the value is \"gdb\", then the printing is done by GDB to its own\n\
16446console, as with the \"printf\" command.\n\
16447If the value is \"call\", the print is done by calling a function in your\n\
16448program; by default printf(), but you can choose a different function or\n\
16449output stream by setting dprintf-function and dprintf-channel."),
16450 update_dprintf_commands, NULL,
16451 &setlist, &showlist);
16452
16453 dprintf_function = xstrdup ("printf");
16454 add_setshow_string_cmd ("dprintf-function", class_support,
16455 &dprintf_function, _("\
16456Set the function to use for dynamic printf"), _("\
16457Show the function to use for dynamic printf"), NULL,
16458 update_dprintf_commands, NULL,
16459 &setlist, &showlist);
16460
16461 dprintf_channel = xstrdup ("");
16462 add_setshow_string_cmd ("dprintf-channel", class_support,
16463 &dprintf_channel, _("\
16464Set the channel to use for dynamic printf"), _("\
16465Show the channel to use for dynamic printf"), NULL,
16466 update_dprintf_commands, NULL,
16467 &setlist, &showlist);
16468
d3ce09f5
SS
16469 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16470 &disconnected_dprintf, _("\
16471Set whether dprintf continues after GDB disconnects."), _("\
16472Show whether dprintf continues after GDB disconnects."), _("\
16473Use this to let dprintf commands continue to hit and produce output\n\
16474even if GDB disconnects or detaches from the target."),
16475 NULL,
16476 NULL,
16477 &setlist, &showlist);
16478
16479 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16480agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16481(target agent only) This is useful for formatted output in user-defined commands."));
16482
765dc015 16483 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16484
16485 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16486 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16487}
This page took 2.992681 seconds and 4 git commands to generate.